Deleted Added
full compact
pw_user.c (273043) pw_user.c (274082)
1/*-
2 * Copyright (C) 1996
3 * David L. Nugent. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 */
27
28#ifndef lint
29static const char rcsid[] =
1/*-
2 * Copyright (C) 1996
3 * David L. Nugent. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY DAVID L. NUGENT AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL DAVID L. NUGENT OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 */
27
28#ifndef lint
29static const char rcsid[] =
30 "$FreeBSD: stable/10/usr.sbin/pw/pw_user.c 273043 2014-10-13 15:56:47Z des $";
30 "$FreeBSD: stable/10/usr.sbin/pw/pw_user.c 274082 2014-11-04 07:50:48Z bapt $";
31#endif /* not lint */
32
33#include <ctype.h>
34#include <err.h>
35#include <fcntl.h>
36#include <sys/param.h>
37#include <dirent.h>
38#include <paths.h>
39#include <termios.h>
40#include <sys/types.h>
41#include <sys/time.h>
42#include <sys/resource.h>
43#include <unistd.h>
44#include <login_cap.h>
45#include <pwd.h>
46#include <grp.h>
47#include <libutil.h>
48#include "pw.h"
49#include "bitmap.h"
50
51#define LOGNAMESIZE (MAXLOGNAME-1)
52
53static char locked_str[] = "*LOCKED*";
54
55static int print_user(struct passwd * pwd, int pretty, int v7);
56static uid_t pw_uidpolicy(struct userconf * cnf, struct cargs * args);
57static uid_t pw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer);
58static time_t pw_pwdpolicy(struct userconf * cnf, struct cargs * args);
59static time_t pw_exppolicy(struct userconf * cnf, struct cargs * args);
60static char *pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user);
61static char *pw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell);
62static char *pw_password(struct userconf * cnf, struct cargs * args, char const * user);
63static char *shell_path(char const * path, char *shells[], char *sh);
64static void rmat(uid_t uid);
65static void rmopie(char const * name);
66
67/*-
68 * -C config configuration file
69 * -q quiet operation
70 * -n name login name
71 * -u uid user id
72 * -c comment user name/comment
73 * -d directory home directory
74 * -e date account expiry date
75 * -p date password expiry date
76 * -g grp primary group
77 * -G grp1,grp2 additional groups
78 * -m [ -k dir ] create and set up home
79 * -s shell name of login shell
80 * -o duplicate uid ok
81 * -L class user class
82 * -l name new login name
83 * -h fd password filehandle
84 * -H fd encrypted password filehandle
85 * -F force print or add
86 * Setting defaults:
87 * -D set user defaults
88 * -b dir default home root dir
89 * -e period default expiry period
90 * -p period default password change period
91 * -g group default group
92 * -G grp1,grp2.. default additional groups
93 * -L class default login class
94 * -k dir default home skeleton
95 * -s shell default shell
96 * -w method default password method
97 */
98
99int
100pw_user(struct userconf * cnf, int mode, struct cargs * args)
101{
102 int rc, edited = 0;
103 char *p = NULL;
104 char *passtmp;
105 struct carg *a_name;
106 struct carg *a_uid;
107 struct carg *arg;
108 struct passwd *pwd = NULL;
109 struct group *grp;
110 struct stat st;
111 char line[_PASSWORD_LEN+1];
112 FILE *fp;
113 char *dmode_c;
114 void *set = NULL;
115
116 static struct passwd fakeuser =
117 {
118 NULL,
119 "*",
120 -1,
121 -1,
122 0,
123 "",
124 "User &",
125 "/nonexistent",
126 "/bin/sh",
127 0
128#if defined(__FreeBSD__)
129 ,0
130#endif
131 };
132
133
134 /*
135 * With M_NEXT, we only need to return the
136 * next uid to stdout
137 */
138 if (mode == M_NEXT)
139 {
140 uid_t next = pw_uidpolicy(cnf, args);
141 if (getarg(args, 'q'))
142 return next;
143 printf("%ld:", (long)next);
144 pw_group(cnf, mode, args);
145 return EXIT_SUCCESS;
146 }
147
148 /*
149 * We can do all of the common legwork here
150 */
151
152 if ((arg = getarg(args, 'b')) != NULL) {
153 cnf->home = arg->val;
154 }
155
156 if ((arg = getarg(args, 'M')) != NULL) {
157 dmode_c = arg->val;
158 if ((set = setmode(dmode_c)) == NULL)
159 errx(EX_DATAERR, "invalid directory creation mode '%s'",
160 dmode_c);
161 cnf->homemode = getmode(set, _DEF_DIRMODE);
162 free(set);
163 }
164
165 /*
166 * If we'll need to use it or we're updating it,
167 * then create the base home directory if necessary
168 */
169 if (arg != NULL || getarg(args, 'm') != NULL) {
170 int l = strlen(cnf->home);
171
172 if (l > 1 && cnf->home[l-1] == '/') /* Shave off any trailing path delimiter */
173 cnf->home[--l] = '\0';
174
175 if (l < 2 || *cnf->home != '/') /* Check for absolute path name */
176 errx(EX_DATAERR, "invalid base directory for home '%s'", cnf->home);
177
178 if (stat(cnf->home, &st) == -1) {
179 char dbuf[MAXPATHLEN];
180
181 /*
182 * This is a kludge especially for Joerg :)
183 * If the home directory would be created in the root partition, then
184 * we really create it under /usr which is likely to have more space.
185 * But we create a symlink from cnf->home -> "/usr" -> cnf->home
186 */
187 if (strchr(cnf->home+1, '/') == NULL) {
188 strcpy(dbuf, "/usr");
189 strncat(dbuf, cnf->home, MAXPATHLEN-5);
190 if (mkdir(dbuf, _DEF_DIRMODE) != -1 || errno == EEXIST) {
191 chown(dbuf, 0, 0);
192 /*
193 * Skip first "/" and create symlink:
194 * /home -> usr/home
195 */
196 symlink(dbuf+1, cnf->home);
197 }
198 /* If this falls, fall back to old method */
199 }
200 strlcpy(dbuf, cnf->home, sizeof(dbuf));
201 p = dbuf;
202 if (stat(dbuf, &st) == -1) {
203 while ((p = strchr(p + 1, '/')) != NULL) {
204 *p = '\0';
205 if (stat(dbuf, &st) == -1) {
206 if (mkdir(dbuf, _DEF_DIRMODE) == -1)
207 goto direrr;
208 chown(dbuf, 0, 0);
209 } else if (!S_ISDIR(st.st_mode))
210 errx(EX_OSFILE, "'%s' (root home parent) is not a directory", dbuf);
211 *p = '/';
212 }
213 }
214 if (stat(dbuf, &st) == -1) {
215 if (mkdir(dbuf, _DEF_DIRMODE) == -1) {
216 direrr: err(EX_OSFILE, "mkdir '%s'", dbuf);
217 }
218 chown(dbuf, 0, 0);
219 }
220 } else if (!S_ISDIR(st.st_mode))
221 errx(EX_OSFILE, "root home `%s' is not a directory", cnf->home);
222 }
223
224 if ((arg = getarg(args, 'e')) != NULL)
225 cnf->expire_days = atoi(arg->val);
226
227 if ((arg = getarg(args, 'y')) != NULL)
228 cnf->nispasswd = arg->val;
229
230 if ((arg = getarg(args, 'p')) != NULL && arg->val)
231 cnf->password_days = atoi(arg->val);
232
233 if ((arg = getarg(args, 'g')) != NULL) {
234 if (!*(p = arg->val)) /* Handle empty group list specially */
235 cnf->default_group = "";
236 else {
237 if ((grp = GETGRNAM(p)) == NULL) {
238 if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL)
239 errx(EX_NOUSER, "group `%s' does not exist", p);
240 }
241 cnf->default_group = newstr(grp->gr_name);
242 }
243 }
244 if ((arg = getarg(args, 'L')) != NULL)
245 cnf->default_class = pw_checkname((u_char *)arg->val, 0);
246
247 if ((arg = getarg(args, 'G')) != NULL && arg->val) {
248 int i = 0;
249
250 for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) {
251 if ((grp = GETGRNAM(p)) == NULL) {
252 if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL)
253 errx(EX_NOUSER, "group `%s' does not exist", p);
254 }
255 if (extendarray(&cnf->groups, &cnf->numgroups, i + 2) != -1)
256 cnf->groups[i++] = newstr(grp->gr_name);
257 }
258 while (i < cnf->numgroups)
259 cnf->groups[i++] = NULL;
260 }
261
262 if ((arg = getarg(args, 'k')) != NULL) {
263 if (stat(cnf->dotdir = arg->val, &st) == -1 || !S_ISDIR(st.st_mode))
264 errx(EX_OSFILE, "skeleton `%s' is not a directory or does not exist", cnf->dotdir);
265 }
266
267 if ((arg = getarg(args, 's')) != NULL)
268 cnf->shell_default = arg->val;
269
270 if ((arg = getarg(args, 'w')) != NULL)
271 cnf->default_password = boolean_val(arg->val, cnf->default_password);
272 if (mode == M_ADD && getarg(args, 'D')) {
273 if (getarg(args, 'n') != NULL)
274 errx(EX_DATAERR, "can't combine `-D' with `-n name'");
275 if ((arg = getarg(args, 'u')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) {
276 if ((cnf->min_uid = (uid_t) atoi(p)) == 0)
277 cnf->min_uid = 1000;
278 if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_uid = (uid_t) atoi(p)) < cnf->min_uid)
279 cnf->max_uid = 32000;
280 }
281 if ((arg = getarg(args, 'i')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) {
282 if ((cnf->min_gid = (gid_t) atoi(p)) == 0)
283 cnf->min_gid = 1000;
284 if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_gid = (gid_t) atoi(p)) < cnf->min_gid)
285 cnf->max_gid = 32000;
286 }
287
288 arg = getarg(args, 'C');
289 if (write_userconfig(arg ? arg->val : NULL))
290 return EXIT_SUCCESS;
291 warn("config update");
292 return EX_IOERR;
293 }
294
295 if (mode == M_PRINT && getarg(args, 'a')) {
296 int pretty = getarg(args, 'P') != NULL;
297 int v7 = getarg(args, '7') != NULL;
298 SETPWENT();
299 while ((pwd = GETPWENT()) != NULL)
300 print_user(pwd, pretty, v7);
301 ENDPWENT();
302 return EXIT_SUCCESS;
303 }
304
305 if ((a_name = getarg(args, 'n')) != NULL)
306 pwd = GETPWNAM(pw_checkname((u_char *)a_name->val, 0));
307 a_uid = getarg(args, 'u');
308
309 if (a_uid == NULL) {
310 if (a_name == NULL)
311 errx(EX_DATAERR, "user name or id required");
312
313 /*
314 * Determine whether 'n' switch is name or uid - we don't
315 * really don't really care which we have, but we need to
316 * know.
317 */
318 if (mode != M_ADD && pwd == NULL
319 && strspn(a_name->val, "0123456789") == strlen(a_name->val)
320 && *a_name->val) {
321 (a_uid = a_name)->ch = 'u';
322 a_name = NULL;
323 }
31#endif /* not lint */
32
33#include <ctype.h>
34#include <err.h>
35#include <fcntl.h>
36#include <sys/param.h>
37#include <dirent.h>
38#include <paths.h>
39#include <termios.h>
40#include <sys/types.h>
41#include <sys/time.h>
42#include <sys/resource.h>
43#include <unistd.h>
44#include <login_cap.h>
45#include <pwd.h>
46#include <grp.h>
47#include <libutil.h>
48#include "pw.h"
49#include "bitmap.h"
50
51#define LOGNAMESIZE (MAXLOGNAME-1)
52
53static char locked_str[] = "*LOCKED*";
54
55static int print_user(struct passwd * pwd, int pretty, int v7);
56static uid_t pw_uidpolicy(struct userconf * cnf, struct cargs * args);
57static uid_t pw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer);
58static time_t pw_pwdpolicy(struct userconf * cnf, struct cargs * args);
59static time_t pw_exppolicy(struct userconf * cnf, struct cargs * args);
60static char *pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user);
61static char *pw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell);
62static char *pw_password(struct userconf * cnf, struct cargs * args, char const * user);
63static char *shell_path(char const * path, char *shells[], char *sh);
64static void rmat(uid_t uid);
65static void rmopie(char const * name);
66
67/*-
68 * -C config configuration file
69 * -q quiet operation
70 * -n name login name
71 * -u uid user id
72 * -c comment user name/comment
73 * -d directory home directory
74 * -e date account expiry date
75 * -p date password expiry date
76 * -g grp primary group
77 * -G grp1,grp2 additional groups
78 * -m [ -k dir ] create and set up home
79 * -s shell name of login shell
80 * -o duplicate uid ok
81 * -L class user class
82 * -l name new login name
83 * -h fd password filehandle
84 * -H fd encrypted password filehandle
85 * -F force print or add
86 * Setting defaults:
87 * -D set user defaults
88 * -b dir default home root dir
89 * -e period default expiry period
90 * -p period default password change period
91 * -g group default group
92 * -G grp1,grp2.. default additional groups
93 * -L class default login class
94 * -k dir default home skeleton
95 * -s shell default shell
96 * -w method default password method
97 */
98
99int
100pw_user(struct userconf * cnf, int mode, struct cargs * args)
101{
102 int rc, edited = 0;
103 char *p = NULL;
104 char *passtmp;
105 struct carg *a_name;
106 struct carg *a_uid;
107 struct carg *arg;
108 struct passwd *pwd = NULL;
109 struct group *grp;
110 struct stat st;
111 char line[_PASSWORD_LEN+1];
112 FILE *fp;
113 char *dmode_c;
114 void *set = NULL;
115
116 static struct passwd fakeuser =
117 {
118 NULL,
119 "*",
120 -1,
121 -1,
122 0,
123 "",
124 "User &",
125 "/nonexistent",
126 "/bin/sh",
127 0
128#if defined(__FreeBSD__)
129 ,0
130#endif
131 };
132
133
134 /*
135 * With M_NEXT, we only need to return the
136 * next uid to stdout
137 */
138 if (mode == M_NEXT)
139 {
140 uid_t next = pw_uidpolicy(cnf, args);
141 if (getarg(args, 'q'))
142 return next;
143 printf("%ld:", (long)next);
144 pw_group(cnf, mode, args);
145 return EXIT_SUCCESS;
146 }
147
148 /*
149 * We can do all of the common legwork here
150 */
151
152 if ((arg = getarg(args, 'b')) != NULL) {
153 cnf->home = arg->val;
154 }
155
156 if ((arg = getarg(args, 'M')) != NULL) {
157 dmode_c = arg->val;
158 if ((set = setmode(dmode_c)) == NULL)
159 errx(EX_DATAERR, "invalid directory creation mode '%s'",
160 dmode_c);
161 cnf->homemode = getmode(set, _DEF_DIRMODE);
162 free(set);
163 }
164
165 /*
166 * If we'll need to use it or we're updating it,
167 * then create the base home directory if necessary
168 */
169 if (arg != NULL || getarg(args, 'm') != NULL) {
170 int l = strlen(cnf->home);
171
172 if (l > 1 && cnf->home[l-1] == '/') /* Shave off any trailing path delimiter */
173 cnf->home[--l] = '\0';
174
175 if (l < 2 || *cnf->home != '/') /* Check for absolute path name */
176 errx(EX_DATAERR, "invalid base directory for home '%s'", cnf->home);
177
178 if (stat(cnf->home, &st) == -1) {
179 char dbuf[MAXPATHLEN];
180
181 /*
182 * This is a kludge especially for Joerg :)
183 * If the home directory would be created in the root partition, then
184 * we really create it under /usr which is likely to have more space.
185 * But we create a symlink from cnf->home -> "/usr" -> cnf->home
186 */
187 if (strchr(cnf->home+1, '/') == NULL) {
188 strcpy(dbuf, "/usr");
189 strncat(dbuf, cnf->home, MAXPATHLEN-5);
190 if (mkdir(dbuf, _DEF_DIRMODE) != -1 || errno == EEXIST) {
191 chown(dbuf, 0, 0);
192 /*
193 * Skip first "/" and create symlink:
194 * /home -> usr/home
195 */
196 symlink(dbuf+1, cnf->home);
197 }
198 /* If this falls, fall back to old method */
199 }
200 strlcpy(dbuf, cnf->home, sizeof(dbuf));
201 p = dbuf;
202 if (stat(dbuf, &st) == -1) {
203 while ((p = strchr(p + 1, '/')) != NULL) {
204 *p = '\0';
205 if (stat(dbuf, &st) == -1) {
206 if (mkdir(dbuf, _DEF_DIRMODE) == -1)
207 goto direrr;
208 chown(dbuf, 0, 0);
209 } else if (!S_ISDIR(st.st_mode))
210 errx(EX_OSFILE, "'%s' (root home parent) is not a directory", dbuf);
211 *p = '/';
212 }
213 }
214 if (stat(dbuf, &st) == -1) {
215 if (mkdir(dbuf, _DEF_DIRMODE) == -1) {
216 direrr: err(EX_OSFILE, "mkdir '%s'", dbuf);
217 }
218 chown(dbuf, 0, 0);
219 }
220 } else if (!S_ISDIR(st.st_mode))
221 errx(EX_OSFILE, "root home `%s' is not a directory", cnf->home);
222 }
223
224 if ((arg = getarg(args, 'e')) != NULL)
225 cnf->expire_days = atoi(arg->val);
226
227 if ((arg = getarg(args, 'y')) != NULL)
228 cnf->nispasswd = arg->val;
229
230 if ((arg = getarg(args, 'p')) != NULL && arg->val)
231 cnf->password_days = atoi(arg->val);
232
233 if ((arg = getarg(args, 'g')) != NULL) {
234 if (!*(p = arg->val)) /* Handle empty group list specially */
235 cnf->default_group = "";
236 else {
237 if ((grp = GETGRNAM(p)) == NULL) {
238 if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL)
239 errx(EX_NOUSER, "group `%s' does not exist", p);
240 }
241 cnf->default_group = newstr(grp->gr_name);
242 }
243 }
244 if ((arg = getarg(args, 'L')) != NULL)
245 cnf->default_class = pw_checkname((u_char *)arg->val, 0);
246
247 if ((arg = getarg(args, 'G')) != NULL && arg->val) {
248 int i = 0;
249
250 for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) {
251 if ((grp = GETGRNAM(p)) == NULL) {
252 if (!isdigit((unsigned char)*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL)
253 errx(EX_NOUSER, "group `%s' does not exist", p);
254 }
255 if (extendarray(&cnf->groups, &cnf->numgroups, i + 2) != -1)
256 cnf->groups[i++] = newstr(grp->gr_name);
257 }
258 while (i < cnf->numgroups)
259 cnf->groups[i++] = NULL;
260 }
261
262 if ((arg = getarg(args, 'k')) != NULL) {
263 if (stat(cnf->dotdir = arg->val, &st) == -1 || !S_ISDIR(st.st_mode))
264 errx(EX_OSFILE, "skeleton `%s' is not a directory or does not exist", cnf->dotdir);
265 }
266
267 if ((arg = getarg(args, 's')) != NULL)
268 cnf->shell_default = arg->val;
269
270 if ((arg = getarg(args, 'w')) != NULL)
271 cnf->default_password = boolean_val(arg->val, cnf->default_password);
272 if (mode == M_ADD && getarg(args, 'D')) {
273 if (getarg(args, 'n') != NULL)
274 errx(EX_DATAERR, "can't combine `-D' with `-n name'");
275 if ((arg = getarg(args, 'u')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) {
276 if ((cnf->min_uid = (uid_t) atoi(p)) == 0)
277 cnf->min_uid = 1000;
278 if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_uid = (uid_t) atoi(p)) < cnf->min_uid)
279 cnf->max_uid = 32000;
280 }
281 if ((arg = getarg(args, 'i')) != NULL && (p = strtok(arg->val, ", \t")) != NULL) {
282 if ((cnf->min_gid = (gid_t) atoi(p)) == 0)
283 cnf->min_gid = 1000;
284 if ((p = strtok(NULL, " ,\t")) == NULL || (cnf->max_gid = (gid_t) atoi(p)) < cnf->min_gid)
285 cnf->max_gid = 32000;
286 }
287
288 arg = getarg(args, 'C');
289 if (write_userconfig(arg ? arg->val : NULL))
290 return EXIT_SUCCESS;
291 warn("config update");
292 return EX_IOERR;
293 }
294
295 if (mode == M_PRINT && getarg(args, 'a')) {
296 int pretty = getarg(args, 'P') != NULL;
297 int v7 = getarg(args, '7') != NULL;
298 SETPWENT();
299 while ((pwd = GETPWENT()) != NULL)
300 print_user(pwd, pretty, v7);
301 ENDPWENT();
302 return EXIT_SUCCESS;
303 }
304
305 if ((a_name = getarg(args, 'n')) != NULL)
306 pwd = GETPWNAM(pw_checkname((u_char *)a_name->val, 0));
307 a_uid = getarg(args, 'u');
308
309 if (a_uid == NULL) {
310 if (a_name == NULL)
311 errx(EX_DATAERR, "user name or id required");
312
313 /*
314 * Determine whether 'n' switch is name or uid - we don't
315 * really don't really care which we have, but we need to
316 * know.
317 */
318 if (mode != M_ADD && pwd == NULL
319 && strspn(a_name->val, "0123456789") == strlen(a_name->val)
320 && *a_name->val) {
321 (a_uid = a_name)->ch = 'u';
322 a_name = NULL;
323 }
324 } else {
325 if (strspn(a_uid->val, "0123456789") != strlen(a_uid->val))
326 errx(EX_USAGE, "-u expects a number");
324 }
325
326 /*
327 * Update, delete & print require that the user exists
328 */
329 if (mode == M_UPDATE || mode == M_DELETE ||
330 mode == M_PRINT || mode == M_LOCK || mode == M_UNLOCK) {
331
332 if (a_name == NULL && pwd == NULL) /* Try harder */
333 pwd = GETPWUID(atoi(a_uid->val));
334
335 if (pwd == NULL) {
336 if (mode == M_PRINT && getarg(args, 'F')) {
337 fakeuser.pw_name = a_name ? a_name->val : "nouser";
338 fakeuser.pw_uid = a_uid ? (uid_t) atol(a_uid->val) : -1;
339 return print_user(&fakeuser,
340 getarg(args, 'P') != NULL,
341 getarg(args, '7') != NULL);
342 }
343 if (a_name == NULL)
344 errx(EX_NOUSER, "no such uid `%s'", a_uid->val);
345 errx(EX_NOUSER, "no such user `%s'", a_name->val);
346 }
347
348 if (a_name == NULL) /* May be needed later */
349 a_name = addarg(args, 'n', newstr(pwd->pw_name));
350
351 /*
352 * The M_LOCK and M_UNLOCK functions simply add or remove
353 * a "*LOCKED*" prefix from in front of the password to
354 * prevent it decoding correctly, and therefore prevents
355 * access. Of course, this only prevents access via
356 * password authentication (not ssh, kerberos or any
357 * other method that does not use the UNIX password) but
358 * that is a known limitation.
359 */
360
361 if (mode == M_LOCK) {
362 if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) == 0)
363 errx(EX_DATAERR, "user '%s' is already locked", pwd->pw_name);
364 passtmp = malloc(strlen(pwd->pw_passwd) + sizeof(locked_str));
365 if (passtmp == NULL) /* disaster */
366 errx(EX_UNAVAILABLE, "out of memory");
367 strcpy(passtmp, locked_str);
368 strcat(passtmp, pwd->pw_passwd);
369 pwd->pw_passwd = passtmp;
370 edited = 1;
371 } else if (mode == M_UNLOCK) {
372 if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) != 0)
373 errx(EX_DATAERR, "user '%s' is not locked", pwd->pw_name);
374 pwd->pw_passwd += sizeof(locked_str)-1;
375 edited = 1;
376 } else if (mode == M_DELETE) {
377 /*
378 * Handle deletions now
379 */
380 char file[MAXPATHLEN];
381 char home[MAXPATHLEN];
382 uid_t uid = pwd->pw_uid;
383 struct group *gr;
384 char grname[LOGNAMESIZE];
385
386 if (strcmp(pwd->pw_name, "root") == 0)
387 errx(EX_DATAERR, "cannot remove user 'root'");
388
389 if (!PWALTDIR()) {
390 /*
391 * Remove opie record from /etc/opiekeys
392 */
393
394 rmopie(pwd->pw_name);
395
396 /*
397 * Remove crontabs
398 */
399 snprintf(file, sizeof(file), "/var/cron/tabs/%s", pwd->pw_name);
400 if (access(file, F_OK) == 0) {
401 sprintf(file, "crontab -u %s -r", pwd->pw_name);
402 system(file);
403 }
404 }
405 /*
406 * Save these for later, since contents of pwd may be
407 * invalidated by deletion
408 */
409 sprintf(file, "%s/%s", _PATH_MAILDIR, pwd->pw_name);
410 strlcpy(home, pwd->pw_dir, sizeof(home));
411 gr = GETGRGID(pwd->pw_gid);
412 if (gr != NULL)
413 strlcpy(grname, gr->gr_name, LOGNAMESIZE);
414 else
415 grname[0] = '\0';
416
417 rc = delpwent(pwd);
418 if (rc == -1)
419 err(EX_IOERR, "user '%s' does not exist", pwd->pw_name);
420 else if (rc != 0) {
421 warn("passwd update");
422 return EX_IOERR;
423 }
424
425 if (cnf->nispasswd && *cnf->nispasswd=='/') {
426 rc = delnispwent(cnf->nispasswd, a_name->val);
427 if (rc == -1)
428 warnx("WARNING: user '%s' does not exist in NIS passwd", pwd->pw_name);
429 else if (rc != 0)
430 warn("WARNING: NIS passwd update");
431 /* non-fatal */
432 }
433
434 grp = GETGRNAM(a_name->val);
435 if (grp != NULL &&
436 (grp->gr_mem == NULL || *grp->gr_mem == NULL) &&
437 strcmp(a_name->val, grname) == 0)
438 delgrent(GETGRNAM(a_name->val));
439 SETGRENT();
440 while ((grp = GETGRENT()) != NULL) {
441 int i, j;
442 char group[MAXLOGNAME];
443 if (grp->gr_mem != NULL) {
444 for (i = 0; grp->gr_mem[i] != NULL; i++) {
445 if (!strcmp(grp->gr_mem[i], a_name->val)) {
446 for (j = i; grp->gr_mem[j] != NULL; j++)
447 grp->gr_mem[j] = grp->gr_mem[j+1];
448 strlcpy(group, grp->gr_name, MAXLOGNAME);
449 chggrent(group, grp);
450 }
451 }
452 }
453 }
454 ENDGRENT();
455
456 pw_log(cnf, mode, W_USER, "%s(%ld) account removed", a_name->val, (long) uid);
457
458 if (!PWALTDIR()) {
459 /*
460 * Remove mail file
461 */
462 remove(file);
463
464 /*
465 * Remove at jobs
466 */
467 if (getpwuid(uid) == NULL)
468 rmat(uid);
469
470 /*
471 * Remove home directory and contents
472 */
473 if (getarg(args, 'r') != NULL && *home == '/' && getpwuid(uid) == NULL) {
474 if (stat(home, &st) != -1) {
475 rm_r(home, uid);
476 pw_log(cnf, mode, W_USER, "%s(%ld) home '%s' %sremoved",
477 a_name->val, (long) uid, home,
478 stat(home, &st) == -1 ? "" : "not completely ");
479 }
480 }
481 }
482 return EXIT_SUCCESS;
483 } else if (mode == M_PRINT)
484 return print_user(pwd,
485 getarg(args, 'P') != NULL,
486 getarg(args, '7') != NULL);
487
488 /*
489 * The rest is edit code
490 */
491 if ((arg = getarg(args, 'l')) != NULL) {
492 if (strcmp(pwd->pw_name, "root") == 0)
493 errx(EX_DATAERR, "can't rename `root' account");
494 pwd->pw_name = pw_checkname((u_char *)arg->val, 0);
495 edited = 1;
496 }
497
498 if ((arg = getarg(args, 'u')) != NULL && isdigit((unsigned char)*arg->val)) {
499 pwd->pw_uid = (uid_t) atol(arg->val);
500 edited = 1;
501 if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0)
502 errx(EX_DATAERR, "can't change uid of `root' account");
503 if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
504 warnx("WARNING: account `%s' will have a uid of 0 (superuser access!)", pwd->pw_name);
505 }
506
507 if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0) { /* Already checked this */
508 gid_t newgid = (gid_t) GETGRNAM(cnf->default_group)->gr_gid;
509 if (newgid != pwd->pw_gid) {
510 edited = 1;
511 pwd->pw_gid = newgid;
512 }
513 }
514
515 if ((arg = getarg(args, 'p')) != NULL) {
516 if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) {
517 if (pwd->pw_change != 0) {
518 pwd->pw_change = 0;
519 edited = 1;
520 }
521 }
522 else {
523 time_t now = time(NULL);
524 time_t expire = parse_date(now, arg->val);
525
526 if (pwd->pw_change != expire) {
527 pwd->pw_change = expire;
528 edited = 1;
529 }
530 }
531 }
532
533 if ((arg = getarg(args, 'e')) != NULL) {
534 if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) {
535 if (pwd->pw_expire != 0) {
536 pwd->pw_expire = 0;
537 edited = 1;
538 }
539 }
540 else {
541 time_t now = time(NULL);
542 time_t expire = parse_date(now, arg->val);
543
544 if (pwd->pw_expire != expire) {
545 pwd->pw_expire = expire;
546 edited = 1;
547 }
548 }
549 }
550
551 if ((arg = getarg(args, 's')) != NULL) {
552 char *shell = shell_path(cnf->shelldir, cnf->shells, arg->val);
553 if (shell == NULL)
554 shell = "";
555 if (strcmp(shell, pwd->pw_shell) != 0) {
556 pwd->pw_shell = shell;
557 edited = 1;
558 }
559 }
560
561 if (getarg(args, 'L')) {
562 if (cnf->default_class == NULL)
563 cnf->default_class = "";
564 if (strcmp(pwd->pw_class, cnf->default_class) != 0) {
565 pwd->pw_class = cnf->default_class;
566 edited = 1;
567 }
568 }
569
570 if ((arg = getarg(args, 'd')) != NULL) {
571 if (strcmp(pwd->pw_dir, arg->val))
572 edited = 1;
573 if (stat(pwd->pw_dir = arg->val, &st) == -1) {
574 if (getarg(args, 'm') == NULL && strcmp(pwd->pw_dir, "/nonexistent") != 0)
575 warnx("WARNING: home `%s' does not exist", pwd->pw_dir);
576 } else if (!S_ISDIR(st.st_mode))
577 warnx("WARNING: home `%s' is not a directory", pwd->pw_dir);
578 }
579
580 if ((arg = getarg(args, 'w')) != NULL &&
581 getarg(args, 'h') == NULL && getarg(args, 'H') == NULL) {
582 login_cap_t *lc;
583
584 lc = login_getpwclass(pwd);
585 if (lc == NULL ||
586 login_setcryptfmt(lc, "sha512", NULL) == NULL)
587 warn("setting crypt(3) format");
588 login_close(lc);
589 pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name);
590 edited = 1;
591 }
592
593 } else {
594 login_cap_t *lc;
595
596 /*
597 * Add code
598 */
599
600 if (a_name == NULL) /* Required */
601 errx(EX_DATAERR, "login name required");
602 else if ((pwd = GETPWNAM(a_name->val)) != NULL) /* Exists */
603 errx(EX_DATAERR, "login name `%s' already exists", a_name->val);
604
605 /*
606 * Now, set up defaults for a new user
607 */
608 pwd = &fakeuser;
609 pwd->pw_name = a_name->val;
610 pwd->pw_class = cnf->default_class ? cnf->default_class : "";
611 pwd->pw_uid = pw_uidpolicy(cnf, args);
612 pwd->pw_gid = pw_gidpolicy(cnf, args, pwd->pw_name, (gid_t) pwd->pw_uid);
613 pwd->pw_change = pw_pwdpolicy(cnf, args);
614 pwd->pw_expire = pw_exppolicy(cnf, args);
615 pwd->pw_dir = pw_homepolicy(cnf, args, pwd->pw_name);
616 pwd->pw_shell = pw_shellpolicy(cnf, args, NULL);
617 lc = login_getpwclass(pwd);
618 if (lc == NULL || login_setcryptfmt(lc, "sha512", NULL) == NULL)
619 warn("setting crypt(3) format");
620 login_close(lc);
621 pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name);
622 edited = 1;
623
624 if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
625 warnx("WARNING: new account `%s' has a uid of 0 (superuser access!)", pwd->pw_name);
626 }
627
628 /*
629 * Shared add/edit code
630 */
631 if ((arg = getarg(args, 'c')) != NULL) {
632 char *gecos = pw_checkname((u_char *)arg->val, 1);
633 if (strcmp(pwd->pw_gecos, gecos) != 0) {
634 pwd->pw_gecos = gecos;
635 edited = 1;
636 }
637 }
638
639 if ((arg = getarg(args, 'h')) != NULL ||
640 (arg = getarg(args, 'H')) != NULL) {
641 if (strcmp(arg->val, "-") == 0) {
642 if (!pwd->pw_passwd || *pwd->pw_passwd != '*') {
643 pwd->pw_passwd = "*"; /* No access */
644 edited = 1;
645 }
646 } else {
647 int fd = atoi(arg->val);
648 int precrypt = (arg->ch == 'H');
649 int b;
650 int istty = isatty(fd);
651 struct termios t;
652 login_cap_t *lc;
653
654 if (istty) {
655 if (tcgetattr(fd, &t) == -1)
656 istty = 0;
657 else {
658 struct termios n = t;
659
660 /* Disable echo */
661 n.c_lflag &= ~(ECHO);
662 tcsetattr(fd, TCSANOW, &n);
663 printf("%s%spassword for user %s:",
664 (mode == M_UPDATE) ? "new " : "",
665 precrypt ? "encrypted " : "",
666 pwd->pw_name);
667 fflush(stdout);
668 }
669 }
670 b = read(fd, line, sizeof(line) - 1);
671 if (istty) { /* Restore state */
672 tcsetattr(fd, TCSANOW, &t);
673 fputc('\n', stdout);
674 fflush(stdout);
675 }
676 if (b < 0) {
677 warn("-%c file descriptor", precrypt ? 'H' :
678 'h');
679 return EX_IOERR;
680 }
681 line[b] = '\0';
682 if ((p = strpbrk(line, "\r\n")) != NULL)
683 *p = '\0';
684 if (!*line)
685 errx(EX_DATAERR, "empty password read on file descriptor %d", fd);
686 if (precrypt) {
687 if (strchr(line, ':') != NULL)
688 return EX_DATAERR;
689 pwd->pw_passwd = line;
690 } else {
691 lc = login_getpwclass(pwd);
692 if (lc == NULL ||
693 login_setcryptfmt(lc, "sha512", NULL) == NULL)
694 warn("setting crypt(3) format");
695 login_close(lc);
696 pwd->pw_passwd = pw_pwcrypt(line);
697 }
698 edited = 1;
699 }
700 }
701
702 /*
703 * Special case: -N only displays & exits
704 */
705 if (getarg(args, 'N') != NULL)
706 return print_user(pwd,
707 getarg(args, 'P') != NULL,
708 getarg(args, '7') != NULL);
709
710 if (mode == M_ADD) {
711 edited = 1; /* Always */
712 rc = addpwent(pwd);
713 if (rc == -1) {
714 warnx("user '%s' already exists", pwd->pw_name);
715 return EX_IOERR;
716 } else if (rc != 0) {
717 warn("passwd file update");
718 return EX_IOERR;
719 }
720 if (cnf->nispasswd && *cnf->nispasswd=='/') {
721 rc = addnispwent(cnf->nispasswd, pwd);
722 if (rc == -1)
723 warnx("User '%s' already exists in NIS passwd", pwd->pw_name);
724 else
725 warn("NIS passwd update");
726 /* NOTE: we treat NIS-only update errors as non-fatal */
727 }
728 } else if (mode == M_UPDATE || mode == M_LOCK || mode == M_UNLOCK) {
729 if (edited) { /* Only updated this if required */
730 rc = chgpwent(a_name->val, pwd);
731 if (rc == -1) {
732 warnx("user '%s' does not exist (NIS?)", pwd->pw_name);
733 return EX_IOERR;
734 } else if (rc != 0) {
735 warn("passwd file update");
736 return EX_IOERR;
737 }
738 if ( cnf->nispasswd && *cnf->nispasswd=='/') {
739 rc = chgnispwent(cnf->nispasswd, a_name->val, pwd);
740 if (rc == -1)
741 warn("User '%s' not found in NIS passwd", pwd->pw_name);
742 else
743 warn("NIS passwd update");
744 /* NOTE: NIS-only update errors are not fatal */
745 }
746 }
747 }
748
749 /*
750 * Ok, user is created or changed - now edit group file
751 */
752
753 if (mode == M_ADD || getarg(args, 'G') != NULL) {
327 }
328
329 /*
330 * Update, delete & print require that the user exists
331 */
332 if (mode == M_UPDATE || mode == M_DELETE ||
333 mode == M_PRINT || mode == M_LOCK || mode == M_UNLOCK) {
334
335 if (a_name == NULL && pwd == NULL) /* Try harder */
336 pwd = GETPWUID(atoi(a_uid->val));
337
338 if (pwd == NULL) {
339 if (mode == M_PRINT && getarg(args, 'F')) {
340 fakeuser.pw_name = a_name ? a_name->val : "nouser";
341 fakeuser.pw_uid = a_uid ? (uid_t) atol(a_uid->val) : -1;
342 return print_user(&fakeuser,
343 getarg(args, 'P') != NULL,
344 getarg(args, '7') != NULL);
345 }
346 if (a_name == NULL)
347 errx(EX_NOUSER, "no such uid `%s'", a_uid->val);
348 errx(EX_NOUSER, "no such user `%s'", a_name->val);
349 }
350
351 if (a_name == NULL) /* May be needed later */
352 a_name = addarg(args, 'n', newstr(pwd->pw_name));
353
354 /*
355 * The M_LOCK and M_UNLOCK functions simply add or remove
356 * a "*LOCKED*" prefix from in front of the password to
357 * prevent it decoding correctly, and therefore prevents
358 * access. Of course, this only prevents access via
359 * password authentication (not ssh, kerberos or any
360 * other method that does not use the UNIX password) but
361 * that is a known limitation.
362 */
363
364 if (mode == M_LOCK) {
365 if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) == 0)
366 errx(EX_DATAERR, "user '%s' is already locked", pwd->pw_name);
367 passtmp = malloc(strlen(pwd->pw_passwd) + sizeof(locked_str));
368 if (passtmp == NULL) /* disaster */
369 errx(EX_UNAVAILABLE, "out of memory");
370 strcpy(passtmp, locked_str);
371 strcat(passtmp, pwd->pw_passwd);
372 pwd->pw_passwd = passtmp;
373 edited = 1;
374 } else if (mode == M_UNLOCK) {
375 if (strncmp(pwd->pw_passwd, locked_str, sizeof(locked_str)-1) != 0)
376 errx(EX_DATAERR, "user '%s' is not locked", pwd->pw_name);
377 pwd->pw_passwd += sizeof(locked_str)-1;
378 edited = 1;
379 } else if (mode == M_DELETE) {
380 /*
381 * Handle deletions now
382 */
383 char file[MAXPATHLEN];
384 char home[MAXPATHLEN];
385 uid_t uid = pwd->pw_uid;
386 struct group *gr;
387 char grname[LOGNAMESIZE];
388
389 if (strcmp(pwd->pw_name, "root") == 0)
390 errx(EX_DATAERR, "cannot remove user 'root'");
391
392 if (!PWALTDIR()) {
393 /*
394 * Remove opie record from /etc/opiekeys
395 */
396
397 rmopie(pwd->pw_name);
398
399 /*
400 * Remove crontabs
401 */
402 snprintf(file, sizeof(file), "/var/cron/tabs/%s", pwd->pw_name);
403 if (access(file, F_OK) == 0) {
404 sprintf(file, "crontab -u %s -r", pwd->pw_name);
405 system(file);
406 }
407 }
408 /*
409 * Save these for later, since contents of pwd may be
410 * invalidated by deletion
411 */
412 sprintf(file, "%s/%s", _PATH_MAILDIR, pwd->pw_name);
413 strlcpy(home, pwd->pw_dir, sizeof(home));
414 gr = GETGRGID(pwd->pw_gid);
415 if (gr != NULL)
416 strlcpy(grname, gr->gr_name, LOGNAMESIZE);
417 else
418 grname[0] = '\0';
419
420 rc = delpwent(pwd);
421 if (rc == -1)
422 err(EX_IOERR, "user '%s' does not exist", pwd->pw_name);
423 else if (rc != 0) {
424 warn("passwd update");
425 return EX_IOERR;
426 }
427
428 if (cnf->nispasswd && *cnf->nispasswd=='/') {
429 rc = delnispwent(cnf->nispasswd, a_name->val);
430 if (rc == -1)
431 warnx("WARNING: user '%s' does not exist in NIS passwd", pwd->pw_name);
432 else if (rc != 0)
433 warn("WARNING: NIS passwd update");
434 /* non-fatal */
435 }
436
437 grp = GETGRNAM(a_name->val);
438 if (grp != NULL &&
439 (grp->gr_mem == NULL || *grp->gr_mem == NULL) &&
440 strcmp(a_name->val, grname) == 0)
441 delgrent(GETGRNAM(a_name->val));
442 SETGRENT();
443 while ((grp = GETGRENT()) != NULL) {
444 int i, j;
445 char group[MAXLOGNAME];
446 if (grp->gr_mem != NULL) {
447 for (i = 0; grp->gr_mem[i] != NULL; i++) {
448 if (!strcmp(grp->gr_mem[i], a_name->val)) {
449 for (j = i; grp->gr_mem[j] != NULL; j++)
450 grp->gr_mem[j] = grp->gr_mem[j+1];
451 strlcpy(group, grp->gr_name, MAXLOGNAME);
452 chggrent(group, grp);
453 }
454 }
455 }
456 }
457 ENDGRENT();
458
459 pw_log(cnf, mode, W_USER, "%s(%ld) account removed", a_name->val, (long) uid);
460
461 if (!PWALTDIR()) {
462 /*
463 * Remove mail file
464 */
465 remove(file);
466
467 /*
468 * Remove at jobs
469 */
470 if (getpwuid(uid) == NULL)
471 rmat(uid);
472
473 /*
474 * Remove home directory and contents
475 */
476 if (getarg(args, 'r') != NULL && *home == '/' && getpwuid(uid) == NULL) {
477 if (stat(home, &st) != -1) {
478 rm_r(home, uid);
479 pw_log(cnf, mode, W_USER, "%s(%ld) home '%s' %sremoved",
480 a_name->val, (long) uid, home,
481 stat(home, &st) == -1 ? "" : "not completely ");
482 }
483 }
484 }
485 return EXIT_SUCCESS;
486 } else if (mode == M_PRINT)
487 return print_user(pwd,
488 getarg(args, 'P') != NULL,
489 getarg(args, '7') != NULL);
490
491 /*
492 * The rest is edit code
493 */
494 if ((arg = getarg(args, 'l')) != NULL) {
495 if (strcmp(pwd->pw_name, "root") == 0)
496 errx(EX_DATAERR, "can't rename `root' account");
497 pwd->pw_name = pw_checkname((u_char *)arg->val, 0);
498 edited = 1;
499 }
500
501 if ((arg = getarg(args, 'u')) != NULL && isdigit((unsigned char)*arg->val)) {
502 pwd->pw_uid = (uid_t) atol(arg->val);
503 edited = 1;
504 if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0)
505 errx(EX_DATAERR, "can't change uid of `root' account");
506 if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
507 warnx("WARNING: account `%s' will have a uid of 0 (superuser access!)", pwd->pw_name);
508 }
509
510 if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0) { /* Already checked this */
511 gid_t newgid = (gid_t) GETGRNAM(cnf->default_group)->gr_gid;
512 if (newgid != pwd->pw_gid) {
513 edited = 1;
514 pwd->pw_gid = newgid;
515 }
516 }
517
518 if ((arg = getarg(args, 'p')) != NULL) {
519 if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) {
520 if (pwd->pw_change != 0) {
521 pwd->pw_change = 0;
522 edited = 1;
523 }
524 }
525 else {
526 time_t now = time(NULL);
527 time_t expire = parse_date(now, arg->val);
528
529 if (pwd->pw_change != expire) {
530 pwd->pw_change = expire;
531 edited = 1;
532 }
533 }
534 }
535
536 if ((arg = getarg(args, 'e')) != NULL) {
537 if (*arg->val == '\0' || strcmp(arg->val, "0") == 0) {
538 if (pwd->pw_expire != 0) {
539 pwd->pw_expire = 0;
540 edited = 1;
541 }
542 }
543 else {
544 time_t now = time(NULL);
545 time_t expire = parse_date(now, arg->val);
546
547 if (pwd->pw_expire != expire) {
548 pwd->pw_expire = expire;
549 edited = 1;
550 }
551 }
552 }
553
554 if ((arg = getarg(args, 's')) != NULL) {
555 char *shell = shell_path(cnf->shelldir, cnf->shells, arg->val);
556 if (shell == NULL)
557 shell = "";
558 if (strcmp(shell, pwd->pw_shell) != 0) {
559 pwd->pw_shell = shell;
560 edited = 1;
561 }
562 }
563
564 if (getarg(args, 'L')) {
565 if (cnf->default_class == NULL)
566 cnf->default_class = "";
567 if (strcmp(pwd->pw_class, cnf->default_class) != 0) {
568 pwd->pw_class = cnf->default_class;
569 edited = 1;
570 }
571 }
572
573 if ((arg = getarg(args, 'd')) != NULL) {
574 if (strcmp(pwd->pw_dir, arg->val))
575 edited = 1;
576 if (stat(pwd->pw_dir = arg->val, &st) == -1) {
577 if (getarg(args, 'm') == NULL && strcmp(pwd->pw_dir, "/nonexistent") != 0)
578 warnx("WARNING: home `%s' does not exist", pwd->pw_dir);
579 } else if (!S_ISDIR(st.st_mode))
580 warnx("WARNING: home `%s' is not a directory", pwd->pw_dir);
581 }
582
583 if ((arg = getarg(args, 'w')) != NULL &&
584 getarg(args, 'h') == NULL && getarg(args, 'H') == NULL) {
585 login_cap_t *lc;
586
587 lc = login_getpwclass(pwd);
588 if (lc == NULL ||
589 login_setcryptfmt(lc, "sha512", NULL) == NULL)
590 warn("setting crypt(3) format");
591 login_close(lc);
592 pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name);
593 edited = 1;
594 }
595
596 } else {
597 login_cap_t *lc;
598
599 /*
600 * Add code
601 */
602
603 if (a_name == NULL) /* Required */
604 errx(EX_DATAERR, "login name required");
605 else if ((pwd = GETPWNAM(a_name->val)) != NULL) /* Exists */
606 errx(EX_DATAERR, "login name `%s' already exists", a_name->val);
607
608 /*
609 * Now, set up defaults for a new user
610 */
611 pwd = &fakeuser;
612 pwd->pw_name = a_name->val;
613 pwd->pw_class = cnf->default_class ? cnf->default_class : "";
614 pwd->pw_uid = pw_uidpolicy(cnf, args);
615 pwd->pw_gid = pw_gidpolicy(cnf, args, pwd->pw_name, (gid_t) pwd->pw_uid);
616 pwd->pw_change = pw_pwdpolicy(cnf, args);
617 pwd->pw_expire = pw_exppolicy(cnf, args);
618 pwd->pw_dir = pw_homepolicy(cnf, args, pwd->pw_name);
619 pwd->pw_shell = pw_shellpolicy(cnf, args, NULL);
620 lc = login_getpwclass(pwd);
621 if (lc == NULL || login_setcryptfmt(lc, "sha512", NULL) == NULL)
622 warn("setting crypt(3) format");
623 login_close(lc);
624 pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name);
625 edited = 1;
626
627 if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
628 warnx("WARNING: new account `%s' has a uid of 0 (superuser access!)", pwd->pw_name);
629 }
630
631 /*
632 * Shared add/edit code
633 */
634 if ((arg = getarg(args, 'c')) != NULL) {
635 char *gecos = pw_checkname((u_char *)arg->val, 1);
636 if (strcmp(pwd->pw_gecos, gecos) != 0) {
637 pwd->pw_gecos = gecos;
638 edited = 1;
639 }
640 }
641
642 if ((arg = getarg(args, 'h')) != NULL ||
643 (arg = getarg(args, 'H')) != NULL) {
644 if (strcmp(arg->val, "-") == 0) {
645 if (!pwd->pw_passwd || *pwd->pw_passwd != '*') {
646 pwd->pw_passwd = "*"; /* No access */
647 edited = 1;
648 }
649 } else {
650 int fd = atoi(arg->val);
651 int precrypt = (arg->ch == 'H');
652 int b;
653 int istty = isatty(fd);
654 struct termios t;
655 login_cap_t *lc;
656
657 if (istty) {
658 if (tcgetattr(fd, &t) == -1)
659 istty = 0;
660 else {
661 struct termios n = t;
662
663 /* Disable echo */
664 n.c_lflag &= ~(ECHO);
665 tcsetattr(fd, TCSANOW, &n);
666 printf("%s%spassword for user %s:",
667 (mode == M_UPDATE) ? "new " : "",
668 precrypt ? "encrypted " : "",
669 pwd->pw_name);
670 fflush(stdout);
671 }
672 }
673 b = read(fd, line, sizeof(line) - 1);
674 if (istty) { /* Restore state */
675 tcsetattr(fd, TCSANOW, &t);
676 fputc('\n', stdout);
677 fflush(stdout);
678 }
679 if (b < 0) {
680 warn("-%c file descriptor", precrypt ? 'H' :
681 'h');
682 return EX_IOERR;
683 }
684 line[b] = '\0';
685 if ((p = strpbrk(line, "\r\n")) != NULL)
686 *p = '\0';
687 if (!*line)
688 errx(EX_DATAERR, "empty password read on file descriptor %d", fd);
689 if (precrypt) {
690 if (strchr(line, ':') != NULL)
691 return EX_DATAERR;
692 pwd->pw_passwd = line;
693 } else {
694 lc = login_getpwclass(pwd);
695 if (lc == NULL ||
696 login_setcryptfmt(lc, "sha512", NULL) == NULL)
697 warn("setting crypt(3) format");
698 login_close(lc);
699 pwd->pw_passwd = pw_pwcrypt(line);
700 }
701 edited = 1;
702 }
703 }
704
705 /*
706 * Special case: -N only displays & exits
707 */
708 if (getarg(args, 'N') != NULL)
709 return print_user(pwd,
710 getarg(args, 'P') != NULL,
711 getarg(args, '7') != NULL);
712
713 if (mode == M_ADD) {
714 edited = 1; /* Always */
715 rc = addpwent(pwd);
716 if (rc == -1) {
717 warnx("user '%s' already exists", pwd->pw_name);
718 return EX_IOERR;
719 } else if (rc != 0) {
720 warn("passwd file update");
721 return EX_IOERR;
722 }
723 if (cnf->nispasswd && *cnf->nispasswd=='/') {
724 rc = addnispwent(cnf->nispasswd, pwd);
725 if (rc == -1)
726 warnx("User '%s' already exists in NIS passwd", pwd->pw_name);
727 else
728 warn("NIS passwd update");
729 /* NOTE: we treat NIS-only update errors as non-fatal */
730 }
731 } else if (mode == M_UPDATE || mode == M_LOCK || mode == M_UNLOCK) {
732 if (edited) { /* Only updated this if required */
733 rc = chgpwent(a_name->val, pwd);
734 if (rc == -1) {
735 warnx("user '%s' does not exist (NIS?)", pwd->pw_name);
736 return EX_IOERR;
737 } else if (rc != 0) {
738 warn("passwd file update");
739 return EX_IOERR;
740 }
741 if ( cnf->nispasswd && *cnf->nispasswd=='/') {
742 rc = chgnispwent(cnf->nispasswd, a_name->val, pwd);
743 if (rc == -1)
744 warn("User '%s' not found in NIS passwd", pwd->pw_name);
745 else
746 warn("NIS passwd update");
747 /* NOTE: NIS-only update errors are not fatal */
748 }
749 }
750 }
751
752 /*
753 * Ok, user is created or changed - now edit group file
754 */
755
756 if (mode == M_ADD || getarg(args, 'G') != NULL) {
754 int i;
757 int i, j;
758 /* First remove the user from all group */
759 SETGRENT();
760 while ((grp = GETGRENT()) != NULL) {
761 char group[MAXLOGNAME];
762 if (grp->gr_mem == NULL)
763 continue;
764 for (i = 0; grp->gr_mem[i] != NULL; i++) {
765 if (strcmp(grp->gr_mem[i] , pwd->pw_name) != 0)
766 continue;
767 for (j = i; grp->gr_mem[j] != NULL ; j++)
768 grp->gr_mem[j] = grp->gr_mem[j+1];
769 strlcpy(group, grp->gr_name, MAXLOGNAME);
770 chggrent(group, grp);
771 }
772 }
773 ENDGRENT();
774
775 /* now add to group where needed */
755 for (i = 0; cnf->groups[i] != NULL; i++) {
756 grp = GETGRNAM(cnf->groups[i]);
757 grp = gr_add(grp, pwd->pw_name);
758 /*
759 * grp can only be NULL in 2 cases:
760 * - the new member is already a member
761 * - a problem with memory occurs
762 * in both cases we want to skip now.
763 */
764 if (grp == NULL)
765 continue;
766 chggrent(cnf->groups[i], grp);
767 free(grp);
768 }
769 }
770
771
772 /* go get a current version of pwd */
773 pwd = GETPWNAM(a_name->val);
774 if (pwd == NULL) {
775 /* This will fail when we rename, so special case that */
776 if (mode == M_UPDATE && (arg = getarg(args, 'l')) != NULL) {
777 a_name->val = arg->val; /* update new name */
778 pwd = GETPWNAM(a_name->val); /* refetch renamed rec */
779 }
780 }
781 if (pwd == NULL) /* can't go on without this */
782 errx(EX_NOUSER, "user '%s' disappeared during update", a_name->val);
783
784 grp = GETGRGID(pwd->pw_gid);
785 pw_log(cnf, mode, W_USER, "%s(%ld):%s(%ld):%s:%s:%s",
786 pwd->pw_name, (long) pwd->pw_uid,
787 grp ? grp->gr_name : "unknown", (long) (grp ? grp->gr_gid : -1),
788 pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell);
789
790 /*
791 * If adding, let's touch and chown the user's mail file. This is not
792 * strictly necessary under BSD with a 0755 maildir but it also
793 * doesn't hurt anything to create the empty mailfile
794 */
795 if (mode == M_ADD) {
796 if (!PWALTDIR()) {
797 sprintf(line, "%s/%s", _PATH_MAILDIR, pwd->pw_name);
798 close(open(line, O_RDWR | O_CREAT, 0600)); /* Preserve contents &
799 * mtime */
800 chown(line, pwd->pw_uid, pwd->pw_gid);
801 }
802 }
803
804 /*
805 * Let's create and populate the user's home directory. Note
806 * that this also `works' for editing users if -m is used, but
807 * existing files will *not* be overwritten.
808 */
809 if (!PWALTDIR() && getarg(args, 'm') != NULL && pwd->pw_dir && *pwd->pw_dir == '/' && pwd->pw_dir[1]) {
810 copymkdir(pwd->pw_dir, cnf->dotdir, cnf->homemode, pwd->pw_uid, pwd->pw_gid);
811 pw_log(cnf, mode, W_USER, "%s(%ld) home %s made",
812 pwd->pw_name, (long) pwd->pw_uid, pwd->pw_dir);
813 }
814
815
816 /*
817 * Finally, send mail to the new user as well, if we are asked to
818 */
819 if (mode == M_ADD && !PWALTDIR() && cnf->newmail && *cnf->newmail && (fp = fopen(cnf->newmail, "r")) != NULL) {
820 FILE *pfp = popen(_PATH_SENDMAIL " -t", "w");
821
822 if (pfp == NULL)
823 warn("sendmail");
824 else {
825 fprintf(pfp, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd->pw_name);
826 while (fgets(line, sizeof(line), fp) != NULL) {
827 /* Do substitutions? */
828 fputs(line, pfp);
829 }
830 pclose(pfp);
831 pw_log(cnf, mode, W_USER, "%s(%ld) new user mail sent",
832 pwd->pw_name, (long) pwd->pw_uid);
833 }
834 fclose(fp);
835 }
836
837 return EXIT_SUCCESS;
838}
839
840
841static uid_t
842pw_uidpolicy(struct userconf * cnf, struct cargs * args)
843{
844 struct passwd *pwd;
845 uid_t uid = (uid_t) - 1;
846 struct carg *a_uid = getarg(args, 'u');
847
848 /*
849 * Check the given uid, if any
850 */
851 if (a_uid != NULL) {
852 uid = (uid_t) atol(a_uid->val);
853
854 if ((pwd = GETPWUID(uid)) != NULL && getarg(args, 'o') == NULL)
855 errx(EX_DATAERR, "uid `%ld' has already been allocated", (long) pwd->pw_uid);
856 } else {
857 struct bitmap bm;
858
859 /*
860 * We need to allocate the next available uid under one of
861 * two policies a) Grab the first unused uid b) Grab the
862 * highest possible unused uid
863 */
864 if (cnf->min_uid >= cnf->max_uid) { /* Sanity
865 * claus^H^H^H^Hheck */
866 cnf->min_uid = 1000;
867 cnf->max_uid = 32000;
868 }
869 bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1);
870
871 /*
872 * Now, let's fill the bitmap from the password file
873 */
874 SETPWENT();
875 while ((pwd = GETPWENT()) != NULL)
876 if (pwd->pw_uid >= (uid_t) cnf->min_uid && pwd->pw_uid <= (uid_t) cnf->max_uid)
877 bm_setbit(&bm, pwd->pw_uid - cnf->min_uid);
878 ENDPWENT();
879
880 /*
881 * Then apply the policy, with fallback to reuse if necessary
882 */
883 if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid)
884 uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid);
885
886 /*
887 * Another sanity check
888 */
889 if (uid < cnf->min_uid || uid > cnf->max_uid)
890 errx(EX_SOFTWARE, "unable to allocate a new uid - range fully used");
891 bm_dealloc(&bm);
892 }
893 return uid;
894}
895
896
897static uid_t
898pw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer)
899{
900 struct group *grp;
901 gid_t gid = (uid_t) - 1;
902 struct carg *a_gid = getarg(args, 'g');
903
904 /*
905 * If no arg given, see if default can help out
906 */
907 if (a_gid == NULL && cnf->default_group && *cnf->default_group)
908 a_gid = addarg(args, 'g', cnf->default_group);
909
910 /*
911 * Check the given gid, if any
912 */
913 SETGRENT();
914 if (a_gid != NULL) {
915 if ((grp = GETGRNAM(a_gid->val)) == NULL) {
916 gid = (gid_t) atol(a_gid->val);
917 if ((gid == 0 && !isdigit((unsigned char)*a_gid->val)) || (grp = GETGRGID(gid)) == NULL)
918 errx(EX_NOUSER, "group `%s' is not defined", a_gid->val);
919 }
920 gid = grp->gr_gid;
921 } else if ((grp = GETGRNAM(nam)) != NULL &&
922 (grp->gr_mem == NULL || grp->gr_mem[0] == NULL)) {
923 gid = grp->gr_gid; /* Already created? Use it anyway... */
924 } else {
925 struct cargs grpargs;
926 char tmp[32];
927
928 LIST_INIT(&grpargs);
929 addarg(&grpargs, 'n', nam);
930
931 /*
932 * We need to auto-create a group with the user's name. We
933 * can send all the appropriate output to our sister routine
934 * bit first see if we can create a group with gid==uid so we
935 * can keep the user and group ids in sync. We purposely do
936 * NOT check the gid range if we can force the sync. If the
937 * user's name dups an existing group, then the group add
938 * function will happily handle that case for us and exit.
939 */
940 if (GETGRGID(prefer) == NULL) {
941 sprintf(tmp, "%lu", (unsigned long) prefer);
942 addarg(&grpargs, 'g', tmp);
943 }
944 if (getarg(args, 'N'))
945 {
946 addarg(&grpargs, 'N', NULL);
947 addarg(&grpargs, 'q', NULL);
948 gid = pw_group(cnf, M_NEXT, &grpargs);
949 }
950 else
951 {
952 pw_group(cnf, M_ADD, &grpargs);
953 if ((grp = GETGRNAM(nam)) != NULL)
954 gid = grp->gr_gid;
955 }
956 a_gid = LIST_FIRST(&grpargs);
957 while (a_gid != NULL) {
958 struct carg *t = LIST_NEXT(a_gid, list);
959 LIST_REMOVE(a_gid, list);
960 a_gid = t;
961 }
962 }
963 ENDGRENT();
964 return gid;
965}
966
967
968static time_t
969pw_pwdpolicy(struct userconf * cnf, struct cargs * args)
970{
971 time_t result = 0;
972 time_t now = time(NULL);
973 struct carg *arg = getarg(args, 'p');
974
975 if (arg != NULL) {
976 if ((result = parse_date(now, arg->val)) == now)
977 errx(EX_DATAERR, "invalid date/time `%s'", arg->val);
978 } else if (cnf->password_days > 0)
979 result = now + ((long) cnf->password_days * 86400L);
980 return result;
981}
982
983
984static time_t
985pw_exppolicy(struct userconf * cnf, struct cargs * args)
986{
987 time_t result = 0;
988 time_t now = time(NULL);
989 struct carg *arg = getarg(args, 'e');
990
991 if (arg != NULL) {
992 if ((result = parse_date(now, arg->val)) == now)
993 errx(EX_DATAERR, "invalid date/time `%s'", arg->val);
994 } else if (cnf->expire_days > 0)
995 result = now + ((long) cnf->expire_days * 86400L);
996 return result;
997}
998
999
1000static char *
1001pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user)
1002{
1003 struct carg *arg = getarg(args, 'd');
1004
1005 if (arg)
1006 return arg->val;
1007 else {
1008 static char home[128];
1009
1010 if (cnf->home == NULL || *cnf->home == '\0')
1011 errx(EX_CONFIG, "no base home directory set");
1012 sprintf(home, "%s/%s", cnf->home, user);
1013 return home;
1014 }
1015}
1016
1017static char *
1018shell_path(char const * path, char *shells[], char *sh)
1019{
1020 if (sh != NULL && (*sh == '/' || *sh == '\0'))
1021 return sh; /* specified full path or forced none */
1022 else {
1023 char *p;
1024 char paths[_UC_MAXLINE];
1025
1026 /*
1027 * We need to search paths
1028 */
1029 strlcpy(paths, path, sizeof(paths));
1030 for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) {
1031 int i;
1032 static char shellpath[256];
1033
1034 if (sh != NULL) {
1035 sprintf(shellpath, "%s/%s", p, sh);
1036 if (access(shellpath, X_OK) == 0)
1037 return shellpath;
1038 } else
1039 for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) {
1040 sprintf(shellpath, "%s/%s", p, shells[i]);
1041 if (access(shellpath, X_OK) == 0)
1042 return shellpath;
1043 }
1044 }
1045 if (sh == NULL)
1046 errx(EX_OSFILE, "can't find shell `%s' in shell paths", sh);
1047 errx(EX_CONFIG, "no default shell available or defined");
1048 return NULL;
1049 }
1050}
1051
1052
1053static char *
1054pw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell)
1055{
1056 char *sh = newshell;
1057 struct carg *arg = getarg(args, 's');
1058
1059 if (newshell == NULL && arg != NULL)
1060 sh = arg->val;
1061 return shell_path(cnf->shelldir, cnf->shells, sh ? sh : cnf->shell_default);
1062}
1063
1064#define SALTSIZE 32
1065
1066static char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./";
1067
1068char *
1069pw_pwcrypt(char *password)
1070{
1071 int i;
1072 char salt[SALTSIZE + 1];
1073 char *cryptpw;
1074
1075 static char buf[256];
1076
1077 /*
1078 * Calculate a salt value
1079 */
1080 for (i = 0; i < SALTSIZE; i++)
1081 salt[i] = chars[arc4random_uniform(sizeof(chars) - 1)];
1082 salt[SALTSIZE] = '\0';
1083
1084 cryptpw = crypt(password, salt);
1085 if (cryptpw == NULL)
1086 errx(EX_CONFIG, "crypt(3) failure");
1087 return strcpy(buf, cryptpw);
1088}
1089
1090
1091static char *
1092pw_password(struct userconf * cnf, struct cargs * args, char const * user)
1093{
1094 int i, l;
1095 char pwbuf[32];
1096
1097 switch (cnf->default_password) {
1098 case -1: /* Random password */
1099 l = (arc4random() % 8 + 8); /* 8 - 16 chars */
1100 for (i = 0; i < l; i++)
1101 pwbuf[i] = chars[arc4random_uniform(sizeof(chars)-1)];
1102 pwbuf[i] = '\0';
1103
1104 /*
1105 * We give this information back to the user
1106 */
1107 if (getarg(args, 'h') == NULL && getarg(args, 'H') == NULL &&
1108 getarg(args, 'N') == NULL) {
1109 if (isatty(STDOUT_FILENO))
1110 printf("Password for '%s' is: ", user);
1111 printf("%s\n", pwbuf);
1112 fflush(stdout);
1113 }
1114 break;
1115
1116 case -2: /* No password at all! */
1117 return "";
1118
1119 case 0: /* No login - default */
1120 default:
1121 return "*";
1122
1123 case 1: /* user's name */
1124 strlcpy(pwbuf, user, sizeof(pwbuf));
1125 break;
1126 }
1127 return pw_pwcrypt(pwbuf);
1128}
1129
1130
1131static int
1132print_user(struct passwd * pwd, int pretty, int v7)
1133{
1134 if (!pretty) {
1135 char *buf;
1136
1137 if (!v7)
1138 pwd->pw_passwd = (pwd->pw_passwd == NULL) ? "" : "*";
1139
1140 buf = v7 ? pw_make_v7(pwd) : pw_make(pwd);
1141 printf("%s\n", buf);
1142 free(buf);
1143 } else {
1144 int j;
1145 char *p;
1146 struct group *grp = GETGRGID(pwd->pw_gid);
1147 char uname[60] = "User &", office[60] = "[None]",
1148 wphone[60] = "[None]", hphone[60] = "[None]";
1149 char acexpire[32] = "[None]", pwexpire[32] = "[None]";
1150 struct tm * tptr;
1151
1152 if ((p = strtok(pwd->pw_gecos, ",")) != NULL) {
1153 strlcpy(uname, p, sizeof(uname));
1154 if ((p = strtok(NULL, ",")) != NULL) {
1155 strlcpy(office, p, sizeof(office));
1156 if ((p = strtok(NULL, ",")) != NULL) {
1157 strlcpy(wphone, p, sizeof(wphone));
1158 if ((p = strtok(NULL, "")) != NULL) {
1159 strlcpy(hphone, p,
1160 sizeof(hphone));
1161 }
1162 }
1163 }
1164 }
1165 /*
1166 * Handle '&' in gecos field
1167 */
1168 if ((p = strchr(uname, '&')) != NULL) {
1169 int l = strlen(pwd->pw_name);
1170 int m = strlen(p);
1171
1172 memmove(p + l, p + 1, m);
1173 memmove(p, pwd->pw_name, l);
1174 *p = (char) toupper((unsigned char)*p);
1175 }
1176 if (pwd->pw_expire > (time_t)0 && (tptr = localtime(&pwd->pw_expire)) != NULL)
1177 strftime(acexpire, sizeof acexpire, "%c", tptr);
1178 if (pwd->pw_change > (time_t)0 && (tptr = localtime(&pwd->pw_change)) != NULL)
1179 strftime(pwexpire, sizeof pwexpire, "%c", tptr);
1180 printf("Login Name: %-15s #%-12ld Group: %-15s #%ld\n"
1181 " Full Name: %s\n"
1182 " Home: %-26.26s Class: %s\n"
1183 " Shell: %-26.26s Office: %s\n"
1184 "Work Phone: %-26.26s Home Phone: %s\n"
1185 "Acc Expire: %-26.26s Pwd Expire: %s\n",
1186 pwd->pw_name, (long) pwd->pw_uid,
1187 grp ? grp->gr_name : "(invalid)", (long) pwd->pw_gid,
1188 uname, pwd->pw_dir, pwd->pw_class,
1189 pwd->pw_shell, office, wphone, hphone,
1190 acexpire, pwexpire);
1191 SETGRENT();
1192 j = 0;
1193 while ((grp=GETGRENT()) != NULL)
1194 {
1195 int i = 0;
1196 if (grp->gr_mem != NULL) {
1197 while (grp->gr_mem[i] != NULL)
1198 {
1199 if (strcmp(grp->gr_mem[i], pwd->pw_name)==0)
1200 {
1201 printf(j++ == 0 ? " Groups: %s" : ",%s", grp->gr_name);
1202 break;
1203 }
1204 ++i;
1205 }
1206 }
1207 }
1208 ENDGRENT();
1209 printf("%s", j ? "\n" : "");
1210 }
1211 return EXIT_SUCCESS;
1212}
1213
1214char *
1215pw_checkname(u_char *name, int gecos)
1216{
1217 char showch[8];
1218 u_char const *badchars, *ch, *showtype;
1219 int reject;
1220
1221 ch = name;
1222 reject = 0;
1223 if (gecos) {
1224 /* See if the name is valid as a gecos (comment) field. */
1225 badchars = ":!@";
1226 showtype = "gecos field";
1227 } else {
1228 /* See if the name is valid as a userid or group. */
1229 badchars = " ,\t:+&#%$^()!@~*?<>=|\\/\"";
1230 showtype = "userid/group name";
1231 /* Userids and groups can not have a leading '-'. */
1232 if (*ch == '-')
1233 reject = 1;
1234 }
1235 if (!reject) {
1236 while (*ch) {
1237 if (strchr(badchars, *ch) != NULL || *ch < ' ' ||
1238 *ch == 127) {
1239 reject = 1;
1240 break;
1241 }
1242 /* 8-bit characters are only allowed in GECOS fields */
1243 if (!gecos && (*ch & 0x80)) {
1244 reject = 1;
1245 break;
1246 }
1247 ch++;
1248 }
1249 }
1250 /*
1251 * A `$' is allowed as the final character for userids and groups,
1252 * mainly for the benefit of samba.
1253 */
1254 if (reject && !gecos) {
1255 if (*ch == '$' && *(ch + 1) == '\0') {
1256 reject = 0;
1257 ch++;
1258 }
1259 }
1260 if (reject) {
1261 snprintf(showch, sizeof(showch), (*ch >= ' ' && *ch < 127)
1262 ? "`%c'" : "0x%02x", *ch);
1263 errx(EX_DATAERR, "invalid character %s at position %td in %s",
1264 showch, (ch - name), showtype);
1265 }
1266 if (!gecos && (ch - name) > LOGNAMESIZE)
1267 errx(EX_DATAERR, "name too long `%s' (max is %d)", name,
1268 LOGNAMESIZE);
1269 return (char *)name;
1270}
1271
1272
1273static void
1274rmat(uid_t uid)
1275{
1276 DIR *d = opendir("/var/at/jobs");
1277
1278 if (d != NULL) {
1279 struct dirent *e;
1280
1281 while ((e = readdir(d)) != NULL) {
1282 struct stat st;
1283
1284 if (strncmp(e->d_name, ".lock", 5) != 0 &&
1285 stat(e->d_name, &st) == 0 &&
1286 !S_ISDIR(st.st_mode) &&
1287 st.st_uid == uid) {
1288 char tmp[MAXPATHLEN];
1289
1290 sprintf(tmp, "/usr/bin/atrm %s", e->d_name);
1291 system(tmp);
1292 }
1293 }
1294 closedir(d);
1295 }
1296}
1297
1298static void
1299rmopie(char const * name)
1300{
1301 static const char etcopie[] = "/etc/opiekeys";
1302 FILE *fp = fopen(etcopie, "r+");
1303
1304 if (fp != NULL) {
1305 char tmp[1024];
1306 off_t atofs = 0;
1307 int length = strlen(name);
1308
1309 while (fgets(tmp, sizeof tmp, fp) != NULL) {
1310 if (strncmp(name, tmp, length) == 0 && tmp[length]==' ') {
1311 if (fseek(fp, atofs, SEEK_SET) == 0) {
1312 fwrite("#", 1, 1, fp); /* Comment username out */
1313 }
1314 break;
1315 }
1316 atofs = ftell(fp);
1317 }
1318 /*
1319 * If we got an error of any sort, don't update!
1320 */
1321 fclose(fp);
1322 }
1323}
1324
776 for (i = 0; cnf->groups[i] != NULL; i++) {
777 grp = GETGRNAM(cnf->groups[i]);
778 grp = gr_add(grp, pwd->pw_name);
779 /*
780 * grp can only be NULL in 2 cases:
781 * - the new member is already a member
782 * - a problem with memory occurs
783 * in both cases we want to skip now.
784 */
785 if (grp == NULL)
786 continue;
787 chggrent(cnf->groups[i], grp);
788 free(grp);
789 }
790 }
791
792
793 /* go get a current version of pwd */
794 pwd = GETPWNAM(a_name->val);
795 if (pwd == NULL) {
796 /* This will fail when we rename, so special case that */
797 if (mode == M_UPDATE && (arg = getarg(args, 'l')) != NULL) {
798 a_name->val = arg->val; /* update new name */
799 pwd = GETPWNAM(a_name->val); /* refetch renamed rec */
800 }
801 }
802 if (pwd == NULL) /* can't go on without this */
803 errx(EX_NOUSER, "user '%s' disappeared during update", a_name->val);
804
805 grp = GETGRGID(pwd->pw_gid);
806 pw_log(cnf, mode, W_USER, "%s(%ld):%s(%ld):%s:%s:%s",
807 pwd->pw_name, (long) pwd->pw_uid,
808 grp ? grp->gr_name : "unknown", (long) (grp ? grp->gr_gid : -1),
809 pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell);
810
811 /*
812 * If adding, let's touch and chown the user's mail file. This is not
813 * strictly necessary under BSD with a 0755 maildir but it also
814 * doesn't hurt anything to create the empty mailfile
815 */
816 if (mode == M_ADD) {
817 if (!PWALTDIR()) {
818 sprintf(line, "%s/%s", _PATH_MAILDIR, pwd->pw_name);
819 close(open(line, O_RDWR | O_CREAT, 0600)); /* Preserve contents &
820 * mtime */
821 chown(line, pwd->pw_uid, pwd->pw_gid);
822 }
823 }
824
825 /*
826 * Let's create and populate the user's home directory. Note
827 * that this also `works' for editing users if -m is used, but
828 * existing files will *not* be overwritten.
829 */
830 if (!PWALTDIR() && getarg(args, 'm') != NULL && pwd->pw_dir && *pwd->pw_dir == '/' && pwd->pw_dir[1]) {
831 copymkdir(pwd->pw_dir, cnf->dotdir, cnf->homemode, pwd->pw_uid, pwd->pw_gid);
832 pw_log(cnf, mode, W_USER, "%s(%ld) home %s made",
833 pwd->pw_name, (long) pwd->pw_uid, pwd->pw_dir);
834 }
835
836
837 /*
838 * Finally, send mail to the new user as well, if we are asked to
839 */
840 if (mode == M_ADD && !PWALTDIR() && cnf->newmail && *cnf->newmail && (fp = fopen(cnf->newmail, "r")) != NULL) {
841 FILE *pfp = popen(_PATH_SENDMAIL " -t", "w");
842
843 if (pfp == NULL)
844 warn("sendmail");
845 else {
846 fprintf(pfp, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd->pw_name);
847 while (fgets(line, sizeof(line), fp) != NULL) {
848 /* Do substitutions? */
849 fputs(line, pfp);
850 }
851 pclose(pfp);
852 pw_log(cnf, mode, W_USER, "%s(%ld) new user mail sent",
853 pwd->pw_name, (long) pwd->pw_uid);
854 }
855 fclose(fp);
856 }
857
858 return EXIT_SUCCESS;
859}
860
861
862static uid_t
863pw_uidpolicy(struct userconf * cnf, struct cargs * args)
864{
865 struct passwd *pwd;
866 uid_t uid = (uid_t) - 1;
867 struct carg *a_uid = getarg(args, 'u');
868
869 /*
870 * Check the given uid, if any
871 */
872 if (a_uid != NULL) {
873 uid = (uid_t) atol(a_uid->val);
874
875 if ((pwd = GETPWUID(uid)) != NULL && getarg(args, 'o') == NULL)
876 errx(EX_DATAERR, "uid `%ld' has already been allocated", (long) pwd->pw_uid);
877 } else {
878 struct bitmap bm;
879
880 /*
881 * We need to allocate the next available uid under one of
882 * two policies a) Grab the first unused uid b) Grab the
883 * highest possible unused uid
884 */
885 if (cnf->min_uid >= cnf->max_uid) { /* Sanity
886 * claus^H^H^H^Hheck */
887 cnf->min_uid = 1000;
888 cnf->max_uid = 32000;
889 }
890 bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1);
891
892 /*
893 * Now, let's fill the bitmap from the password file
894 */
895 SETPWENT();
896 while ((pwd = GETPWENT()) != NULL)
897 if (pwd->pw_uid >= (uid_t) cnf->min_uid && pwd->pw_uid <= (uid_t) cnf->max_uid)
898 bm_setbit(&bm, pwd->pw_uid - cnf->min_uid);
899 ENDPWENT();
900
901 /*
902 * Then apply the policy, with fallback to reuse if necessary
903 */
904 if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid)
905 uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid);
906
907 /*
908 * Another sanity check
909 */
910 if (uid < cnf->min_uid || uid > cnf->max_uid)
911 errx(EX_SOFTWARE, "unable to allocate a new uid - range fully used");
912 bm_dealloc(&bm);
913 }
914 return uid;
915}
916
917
918static uid_t
919pw_gidpolicy(struct userconf * cnf, struct cargs * args, char *nam, gid_t prefer)
920{
921 struct group *grp;
922 gid_t gid = (uid_t) - 1;
923 struct carg *a_gid = getarg(args, 'g');
924
925 /*
926 * If no arg given, see if default can help out
927 */
928 if (a_gid == NULL && cnf->default_group && *cnf->default_group)
929 a_gid = addarg(args, 'g', cnf->default_group);
930
931 /*
932 * Check the given gid, if any
933 */
934 SETGRENT();
935 if (a_gid != NULL) {
936 if ((grp = GETGRNAM(a_gid->val)) == NULL) {
937 gid = (gid_t) atol(a_gid->val);
938 if ((gid == 0 && !isdigit((unsigned char)*a_gid->val)) || (grp = GETGRGID(gid)) == NULL)
939 errx(EX_NOUSER, "group `%s' is not defined", a_gid->val);
940 }
941 gid = grp->gr_gid;
942 } else if ((grp = GETGRNAM(nam)) != NULL &&
943 (grp->gr_mem == NULL || grp->gr_mem[0] == NULL)) {
944 gid = grp->gr_gid; /* Already created? Use it anyway... */
945 } else {
946 struct cargs grpargs;
947 char tmp[32];
948
949 LIST_INIT(&grpargs);
950 addarg(&grpargs, 'n', nam);
951
952 /*
953 * We need to auto-create a group with the user's name. We
954 * can send all the appropriate output to our sister routine
955 * bit first see if we can create a group with gid==uid so we
956 * can keep the user and group ids in sync. We purposely do
957 * NOT check the gid range if we can force the sync. If the
958 * user's name dups an existing group, then the group add
959 * function will happily handle that case for us and exit.
960 */
961 if (GETGRGID(prefer) == NULL) {
962 sprintf(tmp, "%lu", (unsigned long) prefer);
963 addarg(&grpargs, 'g', tmp);
964 }
965 if (getarg(args, 'N'))
966 {
967 addarg(&grpargs, 'N', NULL);
968 addarg(&grpargs, 'q', NULL);
969 gid = pw_group(cnf, M_NEXT, &grpargs);
970 }
971 else
972 {
973 pw_group(cnf, M_ADD, &grpargs);
974 if ((grp = GETGRNAM(nam)) != NULL)
975 gid = grp->gr_gid;
976 }
977 a_gid = LIST_FIRST(&grpargs);
978 while (a_gid != NULL) {
979 struct carg *t = LIST_NEXT(a_gid, list);
980 LIST_REMOVE(a_gid, list);
981 a_gid = t;
982 }
983 }
984 ENDGRENT();
985 return gid;
986}
987
988
989static time_t
990pw_pwdpolicy(struct userconf * cnf, struct cargs * args)
991{
992 time_t result = 0;
993 time_t now = time(NULL);
994 struct carg *arg = getarg(args, 'p');
995
996 if (arg != NULL) {
997 if ((result = parse_date(now, arg->val)) == now)
998 errx(EX_DATAERR, "invalid date/time `%s'", arg->val);
999 } else if (cnf->password_days > 0)
1000 result = now + ((long) cnf->password_days * 86400L);
1001 return result;
1002}
1003
1004
1005static time_t
1006pw_exppolicy(struct userconf * cnf, struct cargs * args)
1007{
1008 time_t result = 0;
1009 time_t now = time(NULL);
1010 struct carg *arg = getarg(args, 'e');
1011
1012 if (arg != NULL) {
1013 if ((result = parse_date(now, arg->val)) == now)
1014 errx(EX_DATAERR, "invalid date/time `%s'", arg->val);
1015 } else if (cnf->expire_days > 0)
1016 result = now + ((long) cnf->expire_days * 86400L);
1017 return result;
1018}
1019
1020
1021static char *
1022pw_homepolicy(struct userconf * cnf, struct cargs * args, char const * user)
1023{
1024 struct carg *arg = getarg(args, 'd');
1025
1026 if (arg)
1027 return arg->val;
1028 else {
1029 static char home[128];
1030
1031 if (cnf->home == NULL || *cnf->home == '\0')
1032 errx(EX_CONFIG, "no base home directory set");
1033 sprintf(home, "%s/%s", cnf->home, user);
1034 return home;
1035 }
1036}
1037
1038static char *
1039shell_path(char const * path, char *shells[], char *sh)
1040{
1041 if (sh != NULL && (*sh == '/' || *sh == '\0'))
1042 return sh; /* specified full path or forced none */
1043 else {
1044 char *p;
1045 char paths[_UC_MAXLINE];
1046
1047 /*
1048 * We need to search paths
1049 */
1050 strlcpy(paths, path, sizeof(paths));
1051 for (p = strtok(paths, ": \t\r\n"); p != NULL; p = strtok(NULL, ": \t\r\n")) {
1052 int i;
1053 static char shellpath[256];
1054
1055 if (sh != NULL) {
1056 sprintf(shellpath, "%s/%s", p, sh);
1057 if (access(shellpath, X_OK) == 0)
1058 return shellpath;
1059 } else
1060 for (i = 0; i < _UC_MAXSHELLS && shells[i] != NULL; i++) {
1061 sprintf(shellpath, "%s/%s", p, shells[i]);
1062 if (access(shellpath, X_OK) == 0)
1063 return shellpath;
1064 }
1065 }
1066 if (sh == NULL)
1067 errx(EX_OSFILE, "can't find shell `%s' in shell paths", sh);
1068 errx(EX_CONFIG, "no default shell available or defined");
1069 return NULL;
1070 }
1071}
1072
1073
1074static char *
1075pw_shellpolicy(struct userconf * cnf, struct cargs * args, char *newshell)
1076{
1077 char *sh = newshell;
1078 struct carg *arg = getarg(args, 's');
1079
1080 if (newshell == NULL && arg != NULL)
1081 sh = arg->val;
1082 return shell_path(cnf->shelldir, cnf->shells, sh ? sh : cnf->shell_default);
1083}
1084
1085#define SALTSIZE 32
1086
1087static char const chars[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ./";
1088
1089char *
1090pw_pwcrypt(char *password)
1091{
1092 int i;
1093 char salt[SALTSIZE + 1];
1094 char *cryptpw;
1095
1096 static char buf[256];
1097
1098 /*
1099 * Calculate a salt value
1100 */
1101 for (i = 0; i < SALTSIZE; i++)
1102 salt[i] = chars[arc4random_uniform(sizeof(chars) - 1)];
1103 salt[SALTSIZE] = '\0';
1104
1105 cryptpw = crypt(password, salt);
1106 if (cryptpw == NULL)
1107 errx(EX_CONFIG, "crypt(3) failure");
1108 return strcpy(buf, cryptpw);
1109}
1110
1111
1112static char *
1113pw_password(struct userconf * cnf, struct cargs * args, char const * user)
1114{
1115 int i, l;
1116 char pwbuf[32];
1117
1118 switch (cnf->default_password) {
1119 case -1: /* Random password */
1120 l = (arc4random() % 8 + 8); /* 8 - 16 chars */
1121 for (i = 0; i < l; i++)
1122 pwbuf[i] = chars[arc4random_uniform(sizeof(chars)-1)];
1123 pwbuf[i] = '\0';
1124
1125 /*
1126 * We give this information back to the user
1127 */
1128 if (getarg(args, 'h') == NULL && getarg(args, 'H') == NULL &&
1129 getarg(args, 'N') == NULL) {
1130 if (isatty(STDOUT_FILENO))
1131 printf("Password for '%s' is: ", user);
1132 printf("%s\n", pwbuf);
1133 fflush(stdout);
1134 }
1135 break;
1136
1137 case -2: /* No password at all! */
1138 return "";
1139
1140 case 0: /* No login - default */
1141 default:
1142 return "*";
1143
1144 case 1: /* user's name */
1145 strlcpy(pwbuf, user, sizeof(pwbuf));
1146 break;
1147 }
1148 return pw_pwcrypt(pwbuf);
1149}
1150
1151
1152static int
1153print_user(struct passwd * pwd, int pretty, int v7)
1154{
1155 if (!pretty) {
1156 char *buf;
1157
1158 if (!v7)
1159 pwd->pw_passwd = (pwd->pw_passwd == NULL) ? "" : "*";
1160
1161 buf = v7 ? pw_make_v7(pwd) : pw_make(pwd);
1162 printf("%s\n", buf);
1163 free(buf);
1164 } else {
1165 int j;
1166 char *p;
1167 struct group *grp = GETGRGID(pwd->pw_gid);
1168 char uname[60] = "User &", office[60] = "[None]",
1169 wphone[60] = "[None]", hphone[60] = "[None]";
1170 char acexpire[32] = "[None]", pwexpire[32] = "[None]";
1171 struct tm * tptr;
1172
1173 if ((p = strtok(pwd->pw_gecos, ",")) != NULL) {
1174 strlcpy(uname, p, sizeof(uname));
1175 if ((p = strtok(NULL, ",")) != NULL) {
1176 strlcpy(office, p, sizeof(office));
1177 if ((p = strtok(NULL, ",")) != NULL) {
1178 strlcpy(wphone, p, sizeof(wphone));
1179 if ((p = strtok(NULL, "")) != NULL) {
1180 strlcpy(hphone, p,
1181 sizeof(hphone));
1182 }
1183 }
1184 }
1185 }
1186 /*
1187 * Handle '&' in gecos field
1188 */
1189 if ((p = strchr(uname, '&')) != NULL) {
1190 int l = strlen(pwd->pw_name);
1191 int m = strlen(p);
1192
1193 memmove(p + l, p + 1, m);
1194 memmove(p, pwd->pw_name, l);
1195 *p = (char) toupper((unsigned char)*p);
1196 }
1197 if (pwd->pw_expire > (time_t)0 && (tptr = localtime(&pwd->pw_expire)) != NULL)
1198 strftime(acexpire, sizeof acexpire, "%c", tptr);
1199 if (pwd->pw_change > (time_t)0 && (tptr = localtime(&pwd->pw_change)) != NULL)
1200 strftime(pwexpire, sizeof pwexpire, "%c", tptr);
1201 printf("Login Name: %-15s #%-12ld Group: %-15s #%ld\n"
1202 " Full Name: %s\n"
1203 " Home: %-26.26s Class: %s\n"
1204 " Shell: %-26.26s Office: %s\n"
1205 "Work Phone: %-26.26s Home Phone: %s\n"
1206 "Acc Expire: %-26.26s Pwd Expire: %s\n",
1207 pwd->pw_name, (long) pwd->pw_uid,
1208 grp ? grp->gr_name : "(invalid)", (long) pwd->pw_gid,
1209 uname, pwd->pw_dir, pwd->pw_class,
1210 pwd->pw_shell, office, wphone, hphone,
1211 acexpire, pwexpire);
1212 SETGRENT();
1213 j = 0;
1214 while ((grp=GETGRENT()) != NULL)
1215 {
1216 int i = 0;
1217 if (grp->gr_mem != NULL) {
1218 while (grp->gr_mem[i] != NULL)
1219 {
1220 if (strcmp(grp->gr_mem[i], pwd->pw_name)==0)
1221 {
1222 printf(j++ == 0 ? " Groups: %s" : ",%s", grp->gr_name);
1223 break;
1224 }
1225 ++i;
1226 }
1227 }
1228 }
1229 ENDGRENT();
1230 printf("%s", j ? "\n" : "");
1231 }
1232 return EXIT_SUCCESS;
1233}
1234
1235char *
1236pw_checkname(u_char *name, int gecos)
1237{
1238 char showch[8];
1239 u_char const *badchars, *ch, *showtype;
1240 int reject;
1241
1242 ch = name;
1243 reject = 0;
1244 if (gecos) {
1245 /* See if the name is valid as a gecos (comment) field. */
1246 badchars = ":!@";
1247 showtype = "gecos field";
1248 } else {
1249 /* See if the name is valid as a userid or group. */
1250 badchars = " ,\t:+&#%$^()!@~*?<>=|\\/\"";
1251 showtype = "userid/group name";
1252 /* Userids and groups can not have a leading '-'. */
1253 if (*ch == '-')
1254 reject = 1;
1255 }
1256 if (!reject) {
1257 while (*ch) {
1258 if (strchr(badchars, *ch) != NULL || *ch < ' ' ||
1259 *ch == 127) {
1260 reject = 1;
1261 break;
1262 }
1263 /* 8-bit characters are only allowed in GECOS fields */
1264 if (!gecos && (*ch & 0x80)) {
1265 reject = 1;
1266 break;
1267 }
1268 ch++;
1269 }
1270 }
1271 /*
1272 * A `$' is allowed as the final character for userids and groups,
1273 * mainly for the benefit of samba.
1274 */
1275 if (reject && !gecos) {
1276 if (*ch == '$' && *(ch + 1) == '\0') {
1277 reject = 0;
1278 ch++;
1279 }
1280 }
1281 if (reject) {
1282 snprintf(showch, sizeof(showch), (*ch >= ' ' && *ch < 127)
1283 ? "`%c'" : "0x%02x", *ch);
1284 errx(EX_DATAERR, "invalid character %s at position %td in %s",
1285 showch, (ch - name), showtype);
1286 }
1287 if (!gecos && (ch - name) > LOGNAMESIZE)
1288 errx(EX_DATAERR, "name too long `%s' (max is %d)", name,
1289 LOGNAMESIZE);
1290 return (char *)name;
1291}
1292
1293
1294static void
1295rmat(uid_t uid)
1296{
1297 DIR *d = opendir("/var/at/jobs");
1298
1299 if (d != NULL) {
1300 struct dirent *e;
1301
1302 while ((e = readdir(d)) != NULL) {
1303 struct stat st;
1304
1305 if (strncmp(e->d_name, ".lock", 5) != 0 &&
1306 stat(e->d_name, &st) == 0 &&
1307 !S_ISDIR(st.st_mode) &&
1308 st.st_uid == uid) {
1309 char tmp[MAXPATHLEN];
1310
1311 sprintf(tmp, "/usr/bin/atrm %s", e->d_name);
1312 system(tmp);
1313 }
1314 }
1315 closedir(d);
1316 }
1317}
1318
1319static void
1320rmopie(char const * name)
1321{
1322 static const char etcopie[] = "/etc/opiekeys";
1323 FILE *fp = fopen(etcopie, "r+");
1324
1325 if (fp != NULL) {
1326 char tmp[1024];
1327 off_t atofs = 0;
1328 int length = strlen(name);
1329
1330 while (fgets(tmp, sizeof tmp, fp) != NULL) {
1331 if (strncmp(name, tmp, length) == 0 && tmp[length]==' ') {
1332 if (fseek(fp, atofs, SEEK_SET) == 0) {
1333 fwrite("#", 1, 1, fp); /* Comment username out */
1334 }
1335 break;
1336 }
1337 atofs = ftell(fp);
1338 }
1339 /*
1340 * If we got an error of any sort, don't update!
1341 */
1342 fclose(fp);
1343 }
1344}
1345