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