2 * This file is part of DisOrder
3 * Copyright (C) 2005, 2007 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
33 #include "authorize.h"
35 #include "configuration.h"
39 /** @brief Create a DisOrder login for the calling user, called @p user
40 * @param client DisOrder client
41 * @param user Username to create
42 * @return 0 on success, non-0 on error
44 int authorize(disorder_client *client, const char *user) {
46 const struct passwd *pw, *jbpw;
52 if(!(jbpw = getpwnam(config->user)))
53 fatal(0, "cannot find user %s", config->user);
55 if(!(pw = getpwnam(user)))
56 fatal(0, "no such user as %s", user);
57 if((c = config_userconf(0, pw)) && access(c, F_OK) == 0) {
58 error(0, "%s already exists", c);
61 if((c = config_usersysconf(pw)) && access(c, F_OK) == 0) {
62 error(0, "%s already exists", c);
65 byte_xasprintf(&t, "%s.new", c);
66 gcry_randomize(pwbin, sizeof pwbin, GCRY_STRONG_RANDOM);
67 pwhex = hex(pwbin, sizeof pwbin);
69 /* create config.USER, to end up with mode 400 user:<anything> */
70 if((fd = open(t, O_WRONLY|O_CREAT|O_EXCL, 0600)) < 0)
71 fatal(errno, "error creating %s", t);
72 if(fchown(fd, pw->pw_uid, -1) < 0)
73 fatal(errno, "error chowning %s", t);
74 if(fchmod(fd, 0400) < 0)
75 fatal(errno, "error chmoding %s", t);
76 if(!(fp = fdopen(fd, "w")))
77 fatal(errno, "error calling fdopen");
78 if(fprintf(fp, "password %s\n", pwhex) < 0
80 fatal(errno, "error writing to %s", t);
82 fatal(errno, "error renaming %s to %s", t, c);
84 /* create the user on the server */
85 if(disorder_adduser(client, user, pwhex))