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