chiark / gitweb /
Play catchup with Apple dscl's API churn
[disorder] / clients / authorize.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder
318ad6a8 3 * Copyright (C) 2005, 2007 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
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
f0feb22e 32#include "client.h"
460b9539 33#include "authorize.h"
34#include "log.h"
35#include "configuration.h"
36#include "printf.h"
5b14453f 37#include "base64.h"
460b9539 38
f0feb22e
RK
39/** @brief Create a DisOrder login for the calling user, called @p user
40 * @param client DisOrder client
e86021e4 41 * @param user Username to create (UTF-8)
0f55e905 42 * @param rights Initial rights or NULL for default
f0feb22e
RK
43 * @return 0 on success, non-0 on error
44 */
0f55e905 45int authorize(disorder_client *client, const char *user, const char *rights) {
5b14453f
RK
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;
460b9539 50 int fd;
51 FILE *fp;
5b14453f
RK
52 char *configdir, *configpath, *configpathtmp;
53 struct stat sb;
54 uid_t old_uid = getuid();
55 gid_t old_gid = getgid();
460b9539 56
460b9539 57 if(!(pw = getpwnam(user)))
5b14453f
RK
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))
460b9539 68 return -1;
5b14453f
RK
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);
460b9539 81 }
460b9539 82
5b14453f
RK
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 */
460b9539 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)
5b14453f 101 fatal(errno, "error writing to %s", configpathtmp);
460b9539 102
5b14453f
RK
103 /* Rename config file into place */
104 if(rename(configpathtmp, configpath) < 0)
105 fatal(errno, "error renaming %s to %s", configpathtmp, configpath);
f0feb22e 106
5b14453f
RK
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
460b9539 113 return 0;
114}
115
116/*
117Local Variables:
118c-basic-offset:2
119comment-column:40
120fill-column:79
121indent-tabs-mode:nil
122End:
123*/