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