Commit | Line | Data |
---|---|---|
460b9539 | 1 | /* |
2 | * This file is part of DisOrder | |
5aff007d | 3 | * Copyright (C) 2005, 2007, 2008 Richard Kettlewell |
460b9539 | 4 | * |
e7eb3a27 | 5 | * This program is free software: you can redistribute it and/or modify |
460b9539 | 6 | * it under the terms of the GNU General Public License as published by |
e7eb3a27 | 7 | * the Free Software Foundation, either version 3 of the License, or |
460b9539 | 8 | * (at your option) any later version. |
9 | * | |
e7eb3a27 RK |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
460b9539 | 15 | * You should have received a copy of the GNU General Public License |
e7eb3a27 | 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
460b9539 | 17 | */ |
132a5a4a RK |
18 | /** @file clients/authorize.c |
19 | * @brief Create a new login | |
20 | */ | |
460b9539 | 21 | |
05b75f8d | 22 | #include "common.h" |
460b9539 | 23 | |
24 | #include <pwd.h> | |
25 | #include <gcrypt.h> | |
26 | #include <errno.h> | |
27 | #include <unistd.h> | |
28 | #include <sys/stat.h> | |
29 | #include <fcntl.h> | |
460b9539 | 30 | |
f0feb22e | 31 | #include "client.h" |
460b9539 | 32 | #include "authorize.h" |
33 | #include "log.h" | |
34 | #include "configuration.h" | |
35 | #include "printf.h" | |
5b14453f | 36 | #include "base64.h" |
460b9539 | 37 | |
f0feb22e RK |
38 | /** @brief Create a DisOrder login for the calling user, called @p user |
39 | * @param client DisOrder client | |
e86021e4 | 40 | * @param user Username to create (UTF-8) |
0f55e905 | 41 | * @param rights Initial rights or NULL for default |
f0feb22e RK |
42 | * @return 0 on success, non-0 on error |
43 | */ | |
0f55e905 | 44 | int authorize(disorder_client *client, const char *user, const char *rights) { |
5b14453f RK |
45 | /* base64 is 3-into-4 so we make the password a multiple of 3 bytes long */ |
46 | uint8_t pwbin[12]; | |
47 | const struct passwd *pw; | |
48 | char *pwhex; | |
460b9539 | 49 | int fd; |
50 | FILE *fp; | |
5b14453f RK |
51 | char *configdir, *configpath, *configpathtmp; |
52 | struct stat sb; | |
53 | uid_t old_uid = getuid(); | |
54 | gid_t old_gid = getgid(); | |
460b9539 | 55 | |
460b9539 | 56 | if(!(pw = getpwnam(user))) |
5b14453f RK |
57 | /* If it's a NIS world then /etc/passwd may be a lie, but it emphasizes |
58 | * that it's talking about the login user, not the DisOrder user */ | |
2e9ba080 | 59 | disorder_fatal(0, "no such user as %s in /etc/passwd", user); |
5b14453f RK |
60 | |
61 | /* Choose a random password */ | |
62 | gcry_randomize(pwbin, sizeof pwbin, GCRY_STRONG_RANDOM); | |
63 | pwhex = mime_to_base64(pwbin, sizeof pwbin); | |
64 | ||
65 | /* Create the user on the server */ | |
66 | if(disorder_adduser(client, user, pwhex, rights)) | |
460b9539 | 67 | return -1; |
5b14453f RK |
68 | |
69 | /* Become the target user */ | |
70 | if(setegid(pw->pw_gid) < 0) | |
2e9ba080 | 71 | disorder_fatal(errno, "setegid %lu", (unsigned long)pw->pw_gid); |
5b14453f | 72 | if(seteuid(pw->pw_uid) < 0) |
2e9ba080 | 73 | disorder_fatal(errno, "seteuid %lu", (unsigned long)pw->pw_uid); |
5b14453f RK |
74 | |
75 | /* Make sure the configuration directory exists*/ | |
76 | byte_xasprintf(&configdir, "%s/.disorder", pw->pw_dir); | |
77 | if(mkdir(configdir, 02700) < 0) { | |
78 | if(errno != EEXIST) | |
2e9ba080 | 79 | disorder_fatal(errno, "creating %s", configdir); |
460b9539 | 80 | } |
460b9539 | 81 | |
5b14453f RK |
82 | /* Make sure the configuration file does not exist */ |
83 | byte_xasprintf(&configpath, "%s/passwd", configdir); | |
84 | if(lstat(configpath, &sb) == 0) | |
2e9ba080 | 85 | disorder_fatal(0, "%s already exists", configpath); |
5b14453f | 86 | if(errno != ENOENT) |
2e9ba080 | 87 | disorder_fatal(errno, " checking %s", configpath); |
5b14453f RK |
88 | |
89 | byte_xasprintf(&configpathtmp, "%s.new", configpath); | |
90 | ||
91 | /* Create config file with mode 600 */ | |
92 | if((fd = open(configpathtmp, O_WRONLY|O_CREAT, 0600)) < 0) | |
2e9ba080 | 93 | disorder_fatal(errno, "error creating %s", configpathtmp); |
5b14453f RK |
94 | |
95 | /* Write password */ | |
460b9539 | 96 | if(!(fp = fdopen(fd, "w"))) |
2e9ba080 | 97 | disorder_fatal(errno, "error calling fdopen"); |
460b9539 | 98 | if(fprintf(fp, "password %s\n", pwhex) < 0 |
99 | || fclose(fp) < 0) | |
2e9ba080 | 100 | disorder_fatal(errno, "error writing to %s", configpathtmp); |
460b9539 | 101 | |
5b14453f RK |
102 | /* Rename config file into place */ |
103 | if(rename(configpathtmp, configpath) < 0) | |
2e9ba080 | 104 | disorder_fatal(errno, "error renaming %s to %s", configpathtmp, configpath); |
f0feb22e | 105 | |
5b14453f RK |
106 | /* Put our identity back */ |
107 | if(seteuid(old_uid) < 0) | |
2e9ba080 | 108 | disorder_fatal(errno, "seteuid %lu", (unsigned long)old_uid); |
5b14453f | 109 | if(setegid(old_gid) < 0) |
2e9ba080 | 110 | disorder_fatal(errno, "setegid %lu", (unsigned long)old_gid); |
5b14453f | 111 | |
460b9539 | 112 | return 0; |
113 | } | |
114 | ||
115 | /* | |
116 | Local Variables: | |
117 | c-basic-offset:2 | |
118 | comment-column:40 | |
119 | fill-column:79 | |
120 | indent-tabs-mode:nil | |
121 | End: | |
122 | */ |