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