2 * This file is part of DisOrder
3 * Copyright (C) 2005, 2007, 2008 Richard Kettlewell
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.
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.
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
31 #include "authorize.h"
33 #include "configuration.h"
37 /** @brief Create a DisOrder login for the calling user, called @p user
38 * @param client DisOrder client
39 * @param user Username to create (UTF-8)
40 * @param rights Initial rights or NULL for default
41 * @return 0 on success, non-0 on error
43 int authorize(disorder_client *client, const char *user, const char *rights) {
44 /* base64 is 3-into-4 so we make the password a multiple of 3 bytes long */
46 const struct passwd *pw;
50 char *configdir, *configpath, *configpathtmp;
52 uid_t old_uid = getuid();
53 gid_t old_gid = getgid();
55 if(!(pw = getpwnam(user)))
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);
60 /* Choose a random password */
61 gcry_randomize(pwbin, sizeof pwbin, GCRY_STRONG_RANDOM);
62 pwhex = mime_to_base64(pwbin, sizeof pwbin);
64 /* Create the user on the server */
65 if(disorder_adduser(client, user, pwhex, rights))
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);
74 /* Make sure the configuration directory exists*/
75 byte_xasprintf(&configdir, "%s/.disorder", pw->pw_dir);
76 if(mkdir(configdir, 02700) < 0) {
78 fatal(errno, "creating %s", configdir);
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);
86 fatal(errno, " checking %s", configpath);
88 byte_xasprintf(&configpathtmp, "%s.new", configpath);
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);
95 if(!(fp = fdopen(fd, "w")))
96 fatal(errno, "error calling fdopen");
97 if(fprintf(fp, "password %s\n", pwhex) < 0
99 fatal(errno, "error writing to %s", configpathtmp);
101 /* Rename config file into place */
102 if(rename(configpathtmp, configpath) < 0)
103 fatal(errno, "error renaming %s to %s", configpathtmp, configpath);
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);