Commit | Line | Data |
---|---|---|
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" | |
37 | #include "hex.h" | |
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 | 45 | int authorize(disorder_client *client, const char *user, const char *rights) { |
460b9539 | 46 | uint8_t pwbin[10]; |
47 | const struct passwd *pw, *jbpw; | |
48 | gid_t jbgid; | |
49 | char *c, *t, *pwhex; | |
50 | int fd; | |
51 | FILE *fp; | |
52 | ||
53 | if(!(jbpw = getpwnam(config->user))) | |
54 | fatal(0, "cannot find user %s", config->user); | |
55 | jbgid = jbpw->pw_gid; | |
56 | if(!(pw = getpwnam(user))) | |
57 | fatal(0, "no such user as %s", user); | |
58 | if((c = config_userconf(0, pw)) && access(c, F_OK) == 0) { | |
59 | error(0, "%s already exists", c); | |
60 | return -1; | |
61 | } | |
62 | if((c = config_usersysconf(pw)) && access(c, F_OK) == 0) { | |
63 | error(0, "%s already exists", c); | |
64 | return -1; | |
65 | } | |
66 | byte_xasprintf(&t, "%s.new", c); | |
67 | gcry_randomize(pwbin, sizeof pwbin, GCRY_STRONG_RANDOM); | |
68 | pwhex = hex(pwbin, sizeof pwbin); | |
69 | ||
318ad6a8 | 70 | /* create config.USER, to end up with mode 400 user:<anything> */ |
460b9539 | 71 | if((fd = open(t, O_WRONLY|O_CREAT|O_EXCL, 0600)) < 0) |
72 | fatal(errno, "error creating %s", t); | |
73 | if(fchown(fd, pw->pw_uid, -1) < 0) | |
74 | fatal(errno, "error chowning %s", t); | |
75 | if(fchmod(fd, 0400) < 0) | |
76 | fatal(errno, "error chmoding %s", t); | |
77 | if(!(fp = fdopen(fd, "w"))) | |
78 | fatal(errno, "error calling fdopen"); | |
79 | if(fprintf(fp, "password %s\n", pwhex) < 0 | |
80 | || fclose(fp) < 0) | |
81 | fatal(errno, "error writing to %s", t); | |
82 | if(rename(t, c) < 0) | |
83 | fatal(errno, "error renaming %s to %s", t, c); | |
84 | ||
f0feb22e | 85 | /* create the user on the server */ |
0f55e905 | 86 | if(disorder_adduser(client, user, pwhex, rights)) |
f0feb22e RK |
87 | return -1; |
88 | ||
460b9539 | 89 | return 0; |
90 | } | |
91 | ||
92 | /* | |
93 | Local Variables: | |
94 | c-basic-offset:2 | |
95 | comment-column:40 | |
96 | fill-column:79 | |
97 | indent-tabs-mode:nil | |
98 | End: | |
99 | */ |