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