Deleted Added
full compact
pw_user.c (43780) pw_user.c (44229)
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

--- 8 unchanged lines hidden (view full) ---

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.
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

--- 8 unchanged lines hidden (view full) ---

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 *
25 */
26
27#ifndef lint
28static const char rcsid[] =
26 */
27
28#ifndef lint
29static const char rcsid[] =
29 "$Id: pw_user.c,v 1.25 1999/01/04 14:07:53 billf Exp $";
30 "$Id: pw_user.c,v 1.26 1999/02/08 21:26:44 des Exp $";
30#endif /* not lint */
31
32#include <ctype.h>
33#include <err.h>
34#include <fcntl.h>
35#include <sys/param.h>
36#include <dirent.h>
37#include <paths.h>
38#include <termios.h>
39#include <sys/types.h>
40#include <sys/time.h>
41#include <sys/resource.h>
42#include <unistd.h>
43#include <utmp.h>
44#if defined(USE_MD5RAND)
45#include <md5.h>
46#endif
47#include "pw.h"
48#include "bitmap.h"
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 <utmp.h>
45#if defined(USE_MD5RAND)
46#include <md5.h>
47#endif
48#include "pw.h"
49#include "bitmap.h"
49#include "pwupd.h"
50
51#if (MAXLOGNAME-1) > UT_NAMESIZE
52#define LOGNAMESIZE UT_NAMESIZE
53#else
54#define LOGNAMESIZE (MAXLOGNAME-1)
55#endif
56
57static randinit;

--- 152 unchanged lines hidden (view full) ---

210 if ((arg = getarg(args, 'y')) != NULL)
211 cnf->nispasswd = arg->val;
212
213 if ((arg = getarg(args, 'p')) != NULL && arg->val)
214 cnf->password_days = atoi(arg->val);
215
216 if ((arg = getarg(args, 'g')) != NULL) {
217 p = arg->val;
50
51#if (MAXLOGNAME-1) > UT_NAMESIZE
52#define LOGNAMESIZE UT_NAMESIZE
53#else
54#define LOGNAMESIZE (MAXLOGNAME-1)
55#endif
56
57static randinit;

--- 152 unchanged lines hidden (view full) ---

210 if ((arg = getarg(args, 'y')) != NULL)
211 cnf->nispasswd = arg->val;
212
213 if ((arg = getarg(args, 'p')) != NULL && arg->val)
214 cnf->password_days = atoi(arg->val);
215
216 if ((arg = getarg(args, 'g')) != NULL) {
217 p = arg->val;
218 if ((grp = getgrnam(p)) == NULL) {
219 if (!isdigit(*p) || (grp = getgrgid((gid_t) atoi(p))) == NULL)
218 if ((grp = GETGRNAM(p)) == NULL) {
219 if (!isdigit(*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL)
220 errx(EX_NOUSER, "group `%s' does not exist", p);
221 }
222 cnf->default_group = newstr(grp->gr_name);
223 }
224 if ((arg = getarg(args, 'L')) != NULL)
225 cnf->default_class = pw_checkname((u_char *)arg->val, 0);
226
227 if ((arg = getarg(args, 'G')) != NULL && arg->val) {
228 int i = 0;
229
230 for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) {
220 errx(EX_NOUSER, "group `%s' does not exist", p);
221 }
222 cnf->default_group = newstr(grp->gr_name);
223 }
224 if ((arg = getarg(args, 'L')) != NULL)
225 cnf->default_class = pw_checkname((u_char *)arg->val, 0);
226
227 if ((arg = getarg(args, 'G')) != NULL && arg->val) {
228 int i = 0;
229
230 for (p = strtok(arg->val, ", \t"); p != NULL; p = strtok(NULL, ", \t")) {
231 if ((grp = getgrnam(p)) == NULL) {
232 if (!isdigit(*p) || (grp = getgrgid((gid_t) atoi(p))) == NULL)
231 if ((grp = GETGRNAM(p)) == NULL) {
232 if (!isdigit(*p) || (grp = GETGRGID((gid_t) atoi(p))) == NULL)
233 errx(EX_NOUSER, "group `%s' does not exist", p);
234 }
235 if (extendarray(&cnf->groups, &cnf->numgroups, i + 2) != -1)
236 cnf->groups[i++] = newstr(grp->gr_name);
237 }
238 while (i < cnf->numgroups)
239 cnf->groups[i++] = NULL;
240 }

--- 26 unchanged lines hidden (view full) ---

267 if (write_userconfig(arg ? arg->val : NULL))
268 return EXIT_SUCCESS;
269 warn("config update");
270 return EX_IOERR;
271 }
272 if (mode == M_PRINT && getarg(args, 'a')) {
273 int pretty = getarg(args, 'P') != NULL;
274
233 errx(EX_NOUSER, "group `%s' does not exist", p);
234 }
235 if (extendarray(&cnf->groups, &cnf->numgroups, i + 2) != -1)
236 cnf->groups[i++] = newstr(grp->gr_name);
237 }
238 while (i < cnf->numgroups)
239 cnf->groups[i++] = NULL;
240 }

--- 26 unchanged lines hidden (view full) ---

267 if (write_userconfig(arg ? arg->val : NULL))
268 return EXIT_SUCCESS;
269 warn("config update");
270 return EX_IOERR;
271 }
272 if (mode == M_PRINT && getarg(args, 'a')) {
273 int pretty = getarg(args, 'P') != NULL;
274
275 setpwent();
276 while ((pwd = getpwent()) != NULL)
275 SETPWENT();
276 while ((pwd = GETPWENT()) != NULL)
277 print_user(pwd, pretty);
277 print_user(pwd, pretty);
278 endpwent();
278 ENDPWENT();
279 return EXIT_SUCCESS;
280 }
281 if ((a_name = getarg(args, 'n')) != NULL)
279 return EXIT_SUCCESS;
280 }
281 if ((a_name = getarg(args, 'n')) != NULL)
282 pwd = getpwnam(pw_checkname((u_char *)a_name->val, 0));
282 pwd = GETPWNAM(pw_checkname((u_char *)a_name->val, 0));
283 a_uid = getarg(args, 'u');
284
285 if (a_uid == NULL) {
286 if (a_name == NULL)
287 errx(EX_DATAERR, "user name or id required");
288
289 /*
290 * Determine whether 'n' switch is name or uid - we don't

--- 7 unchanged lines hidden (view full) ---

298 a_name = NULL;
299 }
300 }
301 /*
302 * Update, delete & print require that the user exists
303 */
304 if (mode == M_UPDATE || mode == M_DELETE || mode == M_PRINT) {
305 if (a_name == NULL && pwd == NULL) /* Try harder */
283 a_uid = getarg(args, 'u');
284
285 if (a_uid == NULL) {
286 if (a_name == NULL)
287 errx(EX_DATAERR, "user name or id required");
288
289 /*
290 * Determine whether 'n' switch is name or uid - we don't

--- 7 unchanged lines hidden (view full) ---

298 a_name = NULL;
299 }
300 }
301 /*
302 * Update, delete & print require that the user exists
303 */
304 if (mode == M_UPDATE || mode == M_DELETE || mode == M_PRINT) {
305 if (a_name == NULL && pwd == NULL) /* Try harder */
306 pwd = getpwuid(atoi(a_uid->val));
306 pwd = GETPWUID(atoi(a_uid->val));
307
308 if (pwd == NULL) {
309 if (mode == M_PRINT && getarg(args, 'F')) {
310 fakeuser.pw_name = a_name ? a_name->val : "nouser";
311 fakeuser.pw_uid = a_uid ? (uid_t) atol(a_uid->val) : -1;
312 return print_user(&fakeuser, getarg(args, 'P') != NULL);
313 }
314 if (a_name == NULL)

--- 9 unchanged lines hidden (view full) ---

324 if (mode == M_DELETE) {
325 char file[MAXPATHLEN];
326 char home[MAXPATHLEN];
327 uid_t uid = pwd->pw_uid;
328
329 if (strcmp(pwd->pw_name, "root") == 0)
330 errx(EX_DATAERR, "cannot remove user 'root'");
331
307
308 if (pwd == NULL) {
309 if (mode == M_PRINT && getarg(args, 'F')) {
310 fakeuser.pw_name = a_name ? a_name->val : "nouser";
311 fakeuser.pw_uid = a_uid ? (uid_t) atol(a_uid->val) : -1;
312 return print_user(&fakeuser, getarg(args, 'P') != NULL);
313 }
314 if (a_name == NULL)

--- 9 unchanged lines hidden (view full) ---

324 if (mode == M_DELETE) {
325 char file[MAXPATHLEN];
326 char home[MAXPATHLEN];
327 uid_t uid = pwd->pw_uid;
328
329 if (strcmp(pwd->pw_name, "root") == 0)
330 errx(EX_DATAERR, "cannot remove user 'root'");
331
332 /*
333 * Remove skey record from /etc/skeykeys
334 */
332 if (!PWALTDIR()) {
333 /*
334 * Remove skey record from /etc/skeykeys
335 */
335
336
336 rmskey(pwd->pw_name);
337 rmskey(pwd->pw_name);
337
338
338 /*
339 * Remove crontabs
340 */
341 sprintf(file, "/var/cron/tabs/%s", pwd->pw_name);
342 if (access(file, F_OK) == 0) {
343 sprintf(file, "crontab -u %s -r", pwd->pw_name);
344 system(file);
339 /*
340 * Remove crontabs
341 */
342 sprintf(file, "/var/cron/tabs/%s", pwd->pw_name);
343 if (access(file, F_OK) == 0) {
344 sprintf(file, "crontab -u %s -r", pwd->pw_name);
345 system(file);
346 }
345 }
346 /*
347 * Save these for later, since contents of pwd may be
348 * invalidated by deletion
349 */
350 sprintf(file, "%s/%s", _PATH_MAILDIR, pwd->pw_name);
351 strncpy(home, pwd->pw_dir, sizeof home);
352 home[sizeof home - 1] = '\0';
353
354 if (!delpwent(pwd))
355 err(EX_IOERR, "error updating passwd file");
356
357 if (cnf->nispasswd && *cnf->nispasswd=='/' && !delnispwent(cnf->nispasswd, a_name->val))
358 warn("WARNING: NIS passwd update");
359
360 editgroups(a_name->val, NULL);
361
362 pw_log(cnf, mode, W_USER, "%s(%ld) account removed", a_name->val, (long) uid);
363
347 }
348 /*
349 * Save these for later, since contents of pwd may be
350 * invalidated by deletion
351 */
352 sprintf(file, "%s/%s", _PATH_MAILDIR, pwd->pw_name);
353 strncpy(home, pwd->pw_dir, sizeof home);
354 home[sizeof home - 1] = '\0';
355
356 if (!delpwent(pwd))
357 err(EX_IOERR, "error updating passwd file");
358
359 if (cnf->nispasswd && *cnf->nispasswd=='/' && !delnispwent(cnf->nispasswd, a_name->val))
360 warn("WARNING: NIS passwd update");
361
362 editgroups(a_name->val, NULL);
363
364 pw_log(cnf, mode, W_USER, "%s(%ld) account removed", a_name->val, (long) uid);
365
364 /*
365 * Remove mail file
366 */
367 remove(file);
366 if (!PWALTDIR()) {
367 /*
368 * Remove mail file
369 */
370 remove(file);
368
371
369 /*
370 * Remove at jobs
371 */
372 if (getpwuid(uid) == NULL)
373 rmat(uid);
372 /*
373 * Remove at jobs
374 */
375 if (getpwuid(uid) == NULL)
376 rmat(uid);
374
377
375 /*
376 * Remove home directory and contents
377 */
378 if (getarg(args, 'r') != NULL && *home == '/' && getpwuid(uid) == NULL) {
379 if (stat(home, &st) != -1) {
380 rm_r(home, uid);
381 pw_log(cnf, mode, W_USER, "%s(%ld) home '%s' %sremoved",
382 a_name->val, (long) uid, home,
383 stat(home, &st) == -1 ? "" : "not completely ");
378 /*
379 * Remove home directory and contents
380 */
381 if (getarg(args, 'r') != NULL && *home == '/' && getpwuid(uid) == NULL) {
382 if (stat(home, &st) != -1) {
383 rm_r(home, uid);
384 pw_log(cnf, mode, W_USER, "%s(%ld) home '%s' %sremoved",
385 a_name->val, (long) uid, home,
386 stat(home, &st) == -1 ? "" : "not completely ");
387 }
384 }
385 }
386 return EXIT_SUCCESS;
387 } else if (mode == M_PRINT)
388 return print_user(pwd, getarg(args, 'P') != NULL);
389
390 /*
391 * The rest is edit code

--- 6 unchanged lines hidden (view full) ---

398 if ((arg = getarg(args, 'u')) != NULL && isdigit(*arg->val)) {
399 pwd->pw_uid = (uid_t) atol(arg->val);
400 if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0)
401 errx(EX_DATAERR, "can't change uid of `root' account");
402 if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
403 warnx("WARNING: account `%s' will have a uid of 0 (superuser access!)", pwd->pw_name);
404 }
405 if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0) /* Already checked this */
388 }
389 }
390 return EXIT_SUCCESS;
391 } else if (mode == M_PRINT)
392 return print_user(pwd, getarg(args, 'P') != NULL);
393
394 /*
395 * The rest is edit code

--- 6 unchanged lines hidden (view full) ---

402 if ((arg = getarg(args, 'u')) != NULL && isdigit(*arg->val)) {
403 pwd->pw_uid = (uid_t) atol(arg->val);
404 if (pwd->pw_uid != 0 && strcmp(pwd->pw_name, "root") == 0)
405 errx(EX_DATAERR, "can't change uid of `root' account");
406 if (pwd->pw_uid == 0 && strcmp(pwd->pw_name, "root") != 0)
407 warnx("WARNING: account `%s' will have a uid of 0 (superuser access!)", pwd->pw_name);
408 }
409 if ((arg = getarg(args, 'g')) != NULL && pwd->pw_uid != 0) /* Already checked this */
406 pwd->pw_gid = (gid_t) getgrnam(cnf->default_group)->gr_gid;
410 pwd->pw_gid = (gid_t) GETGRNAM(cnf->default_group)->gr_gid;
407
408 if ((arg = getarg(args, 'p')) != NULL) {
409 if (*arg->val == '\0' || strcmp(arg->val, "0") == 0)
410 pwd->pw_change = 0;
411 else {
412 time_t now = time(NULL);
413 time_t expire = parse_date(now, arg->val);
414

--- 29 unchanged lines hidden (view full) ---

444 }
445
446 if ((arg = getarg(args, 'w')) != NULL && getarg(args, 'h') == NULL)
447 pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name);
448
449 } else {
450 if (a_name == NULL) /* Required */
451 errx(EX_DATAERR, "login name required");
411
412 if ((arg = getarg(args, 'p')) != NULL) {
413 if (*arg->val == '\0' || strcmp(arg->val, "0") == 0)
414 pwd->pw_change = 0;
415 else {
416 time_t now = time(NULL);
417 time_t expire = parse_date(now, arg->val);
418

--- 29 unchanged lines hidden (view full) ---

448 }
449
450 if ((arg = getarg(args, 'w')) != NULL && getarg(args, 'h') == NULL)
451 pwd->pw_passwd = pw_password(cnf, args, pwd->pw_name);
452
453 } else {
454 if (a_name == NULL) /* Required */
455 errx(EX_DATAERR, "login name required");
452 else if ((pwd = getpwnam(a_name->val)) != NULL) /* Exists */
456 else if ((pwd = GETPWNAM(a_name->val)) != NULL) /* Exists */
453 errx(EX_DATAERR, "login name `%s' already exists", a_name->val);
454
455 /*
456 * Now, set up defaults for a new user
457 */
458 pwd = &fakeuser;
459 pwd->pw_name = a_name->val;
460 pwd->pw_class = cnf->default_class ? cnf->default_class : "";

--- 84 unchanged lines hidden (view full) ---

545 /*
546 * Ok, user is created or changed - now edit group file
547 */
548
549 if (mode == M_ADD || getarg(args, 'G') != NULL)
550 editgroups(pwd->pw_name, cnf->groups);
551
552 /* pwd may have been invalidated */
457 errx(EX_DATAERR, "login name `%s' already exists", a_name->val);
458
459 /*
460 * Now, set up defaults for a new user
461 */
462 pwd = &fakeuser;
463 pwd->pw_name = a_name->val;
464 pwd->pw_class = cnf->default_class ? cnf->default_class : "";

--- 84 unchanged lines hidden (view full) ---

549 /*
550 * Ok, user is created or changed - now edit group file
551 */
552
553 if (mode == M_ADD || getarg(args, 'G') != NULL)
554 editgroups(pwd->pw_name, cnf->groups);
555
556 /* pwd may have been invalidated */
553 if ((pwd = getpwnam(a_name->val)) == NULL)
557 if ((pwd = GETPWNAM(a_name->val)) == NULL)
554 errx(EX_NOUSER, "user '%s' disappeared during update", a_name->val);
555
558 errx(EX_NOUSER, "user '%s' disappeared during update", a_name->val);
559
556 grp = getgrgid(pwd->pw_gid);
560 grp = GETGRGID(pwd->pw_gid);
557 pw_log(cnf, mode, W_USER, "%s(%ld):%s(%d):%s:%s:%s",
558 pwd->pw_name, (long) pwd->pw_uid,
559 grp ? grp->gr_name : "unknown", (long) (grp ? grp->gr_gid : -1),
560 pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell);
561
562 /*
563 * If adding, let's touch and chown the user's mail file. This is not
564 * strictly necessary under BSD with a 0755 maildir but it also
565 * doesn't hurt anything to create the empty mailfile
566 */
567 if (mode == M_ADD) {
568 FILE *fp;
569
561 pw_log(cnf, mode, W_USER, "%s(%ld):%s(%d):%s:%s:%s",
562 pwd->pw_name, (long) pwd->pw_uid,
563 grp ? grp->gr_name : "unknown", (long) (grp ? grp->gr_gid : -1),
564 pwd->pw_gecos, pwd->pw_dir, pwd->pw_shell);
565
566 /*
567 * If adding, let's touch and chown the user's mail file. This is not
568 * strictly necessary under BSD with a 0755 maildir but it also
569 * doesn't hurt anything to create the empty mailfile
570 */
571 if (mode == M_ADD) {
572 FILE *fp;
573
570 sprintf(line, "%s/%s", _PATH_MAILDIR, pwd->pw_name);
571 close(open(line, O_RDWR | O_CREAT, 0600)); /* Preserve contents &
572 * mtime */
573 chown(line, pwd->pw_uid, pwd->pw_gid);
574 if (!PWALTDIR()) {
575 sprintf(line, "%s/%s", _PATH_MAILDIR, pwd->pw_name);
576 close(open(line, O_RDWR | O_CREAT, 0600)); /* Preserve contents &
577 * mtime */
578 chown(line, pwd->pw_uid, pwd->pw_gid);
574
579
575 /*
576 * Send mail to the new user as well, if we are asked to
577 */
578 if (cnf->newmail && *cnf->newmail && (fp = fopen(cnf->newmail, "r")) != NULL) {
579 FILE *pfp = popen(_PATH_SENDMAIL " -t", "w");
580 /*
581 * Send mail to the new user as well, if we are asked to
582 */
583 if (cnf->newmail && *cnf->newmail && (fp = fopen(cnf->newmail, "r")) != NULL) {
584 FILE *pfp = popen(_PATH_SENDMAIL " -t", "w");
580
585
581 if (pfp == NULL)
582 warn("sendmail");
583 else {
584 fprintf(pfp, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd->pw_name);
585 while (fgets(line, sizeof(line), fp) != NULL) {
586 /* Do substitutions? */
587 fputs(line, pfp);
586 if (pfp == NULL)
587 warn("sendmail");
588 else {
589 fprintf(pfp, "From: root\n" "To: %s\n" "Subject: Welcome!\n\n", pwd->pw_name);
590 while (fgets(line, sizeof(line), fp) != NULL) {
591 /* Do substitutions? */
592 fputs(line, pfp);
593 }
594 pclose(pfp);
595 pw_log(cnf, mode, W_USER, "%s(%ld) new user mail sent",
596 pwd->pw_name, (long) pwd->pw_uid);
588 }
597 }
589 pclose(pfp);
590 pw_log(cnf, mode, W_USER, "%s(%ld) new user mail sent",
591 pwd->pw_name, (long) pwd->pw_uid);
598 fclose(fp);
592 }
599 }
593 fclose(fp);
594 }
595 }
596 /*
597 * Finally, let's create and populate the user's home directory. Note
598 * that this also `works' for editing users if -m is used, but
599 * existing files will *not* be overwritten.
600 */
600 }
601 }
602 /*
603 * Finally, let's create and populate the user's home directory. Note
604 * that this also `works' for editing users if -m is used, but
605 * existing files will *not* be overwritten.
606 */
601 if (getarg(args, 'm') != NULL && pwd->pw_dir && *pwd->pw_dir == '/' && pwd->pw_dir[1]) {
607 if (!PWALTDIR() && getarg(args, 'm') != NULL && pwd->pw_dir && *pwd->pw_dir == '/' && pwd->pw_dir[1]) {
602 copymkdir(pwd->pw_dir, cnf->dotdir, 0755, pwd->pw_uid, pwd->pw_gid);
603 pw_log(cnf, mode, W_USER, "%s(%ld) home %s made",
604 pwd->pw_name, (long) pwd->pw_uid, pwd->pw_dir);
605 }
606 return EXIT_SUCCESS;
607}
608
609

--- 5 unchanged lines hidden (view full) ---

615 struct carg *a_uid = getarg(args, 'u');
616
617 /*
618 * Check the given uid, if any
619 */
620 if (a_uid != NULL) {
621 uid = (uid_t) atol(a_uid->val);
622
608 copymkdir(pwd->pw_dir, cnf->dotdir, 0755, pwd->pw_uid, pwd->pw_gid);
609 pw_log(cnf, mode, W_USER, "%s(%ld) home %s made",
610 pwd->pw_name, (long) pwd->pw_uid, pwd->pw_dir);
611 }
612 return EXIT_SUCCESS;
613}
614
615

--- 5 unchanged lines hidden (view full) ---

621 struct carg *a_uid = getarg(args, 'u');
622
623 /*
624 * Check the given uid, if any
625 */
626 if (a_uid != NULL) {
627 uid = (uid_t) atol(a_uid->val);
628
623 if ((pwd = getpwuid(uid)) != NULL && getarg(args, 'o') == NULL)
629 if ((pwd = GETPWUID(uid)) != NULL && getarg(args, 'o') == NULL)
624 errx(EX_DATAERR, "uid `%ld' has already been allocated", (long) pwd->pw_uid);
625 } else {
626 struct bitmap bm;
627
628 /*
629 * We need to allocate the next available uid under one of
630 * two policies a) Grab the first unused uid b) Grab the
631 * highest possible unused uid
632 */
633 if (cnf->min_uid >= cnf->max_uid) { /* Sanity
634 * claus^H^H^H^Hheck */
635 cnf->min_uid = 1000;
636 cnf->max_uid = 32000;
637 }
638 bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1);
639
640 /*
641 * Now, let's fill the bitmap from the password file
642 */
630 errx(EX_DATAERR, "uid `%ld' has already been allocated", (long) pwd->pw_uid);
631 } else {
632 struct bitmap bm;
633
634 /*
635 * We need to allocate the next available uid under one of
636 * two policies a) Grab the first unused uid b) Grab the
637 * highest possible unused uid
638 */
639 if (cnf->min_uid >= cnf->max_uid) { /* Sanity
640 * claus^H^H^H^Hheck */
641 cnf->min_uid = 1000;
642 cnf->max_uid = 32000;
643 }
644 bm = bm_alloc(cnf->max_uid - cnf->min_uid + 1);
645
646 /*
647 * Now, let's fill the bitmap from the password file
648 */
643 setpwent();
644 while ((pwd = getpwent()) != NULL)
645 if (pwd->pw_uid >= (int) cnf->min_uid && pwd->pw_uid <= (int) cnf->max_uid)
649 SETPWENT();
650 while ((pwd = GETPWENT()) != NULL)
651 if (pwd->pw_uid >= (uid_t) cnf->min_uid && pwd->pw_uid <= (uid_t) cnf->max_uid)
646 bm_setbit(&bm, pwd->pw_uid - cnf->min_uid);
652 bm_setbit(&bm, pwd->pw_uid - cnf->min_uid);
647 endpwent();
653 ENDPWENT();
648
649 /*
650 * Then apply the policy, with fallback to reuse if necessary
651 */
652 if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid)
653 uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid);
654
655 /*

--- 18 unchanged lines hidden (view full) ---

674 * If no arg given, see if default can help out
675 */
676 if (a_gid == NULL && cnf->default_group && *cnf->default_group)
677 a_gid = addarg(args, 'g', cnf->default_group);
678
679 /*
680 * Check the given gid, if any
681 */
654
655 /*
656 * Then apply the policy, with fallback to reuse if necessary
657 */
658 if (cnf->reuse_uids || (uid = (uid_t) (bm_lastset(&bm) + cnf->min_uid + 1)) > cnf->max_uid)
659 uid = (uid_t) (bm_firstunset(&bm) + cnf->min_uid);
660
661 /*

--- 18 unchanged lines hidden (view full) ---

680 * If no arg given, see if default can help out
681 */
682 if (a_gid == NULL && cnf->default_group && *cnf->default_group)
683 a_gid = addarg(args, 'g', cnf->default_group);
684
685 /*
686 * Check the given gid, if any
687 */
682 setgrent();
688 SETGRENT();
683 if (a_gid != NULL) {
689 if (a_gid != NULL) {
684 if ((grp = getgrnam(a_gid->val)) == NULL) {
690 if ((grp = GETGRNAM(a_gid->val)) == NULL) {
685 gid = (gid_t) atol(a_gid->val);
691 gid = (gid_t) atol(a_gid->val);
686 if ((gid == 0 && !isdigit(*a_gid->val)) || (grp = getgrgid(gid)) == NULL)
692 if ((gid == 0 && !isdigit(*a_gid->val)) || (grp = GETGRGID(gid)) == NULL)
687 errx(EX_NOUSER, "group `%s' is not defined", a_gid->val);
688 }
689 gid = grp->gr_gid;
693 errx(EX_NOUSER, "group `%s' is not defined", a_gid->val);
694 }
695 gid = grp->gr_gid;
690 } else if ((grp = getgrnam(nam)) != NULL && grp->gr_mem[0] == NULL) {
696 } else if ((grp = GETGRNAM(nam)) != NULL && grp->gr_mem[0] == NULL) {
691 gid = grp->gr_gid; /* Already created? Use it anyway... */
692 } else {
693 struct cargs grpargs;
694 char tmp[32];
695
696 LIST_INIT(&grpargs);
697 addarg(&grpargs, 'n', nam);
698
699 /*
700 * We need to auto-create a group with the user's name. We
701 * can send all the appropriate output to our sister routine
702 * bit first see if we can create a group with gid==uid so we
703 * can keep the user and group ids in sync. We purposely do
704 * NOT check the gid range if we can force the sync. If the
705 * user's name dups an existing group, then the group add
706 * function will happily handle that case for us and exit.
707 */
697 gid = grp->gr_gid; /* Already created? Use it anyway... */
698 } else {
699 struct cargs grpargs;
700 char tmp[32];
701
702 LIST_INIT(&grpargs);
703 addarg(&grpargs, 'n', nam);
704
705 /*
706 * We need to auto-create a group with the user's name. We
707 * can send all the appropriate output to our sister routine
708 * bit first see if we can create a group with gid==uid so we
709 * can keep the user and group ids in sync. We purposely do
710 * NOT check the gid range if we can force the sync. If the
711 * user's name dups an existing group, then the group add
712 * function will happily handle that case for us and exit.
713 */
708 if (getgrgid(prefer) == NULL) {
714 if (GETGRGID(prefer) == NULL) {
709 sprintf(tmp, "%lu", (unsigned long) prefer);
710 addarg(&grpargs, 'g', tmp);
711 }
712 if (getarg(args, 'N'))
713 {
714 addarg(&grpargs, 'N', NULL);
715 addarg(&grpargs, 'q', NULL);
716 gid = pw_group(cnf, M_NEXT, &grpargs);
717 }
718 else
719 {
720 pw_group(cnf, M_ADD, &grpargs);
715 sprintf(tmp, "%lu", (unsigned long) prefer);
716 addarg(&grpargs, 'g', tmp);
717 }
718 if (getarg(args, 'N'))
719 {
720 addarg(&grpargs, 'N', NULL);
721 addarg(&grpargs, 'q', NULL);
722 gid = pw_group(cnf, M_NEXT, &grpargs);
723 }
724 else
725 {
726 pw_group(cnf, M_ADD, &grpargs);
721 if ((grp = getgrnam(nam)) != NULL)
727 if ((grp = GETGRNAM(nam)) != NULL)
722 gid = grp->gr_gid;
723 }
724 a_gid = grpargs.lh_first;
725 while (a_gid != NULL) {
726 struct carg *t = a_gid->list.le_next;
727 LIST_REMOVE(a_gid, list);
728 a_gid = t;
729 }
730 }
728 gid = grp->gr_gid;
729 }
730 a_gid = grpargs.lh_first;
731 while (a_gid != NULL) {
732 struct carg *t = a_gid->list.le_next;
733 LIST_REMOVE(a_gid, list);
734 a_gid = t;
735 }
736 }
731 endgrent();
737 ENDGRENT();
732 return gid;
733}
734
735
736static time_t
737pw_pwdpolicy(struct userconf * cnf, struct cargs * args)
738{
739 time_t result = 0;

--- 223 unchanged lines hidden (view full) ---

963 if (!pretty) {
964 char buf[_UC_MAXLINE];
965
966 fmtpwent(buf, pwd);
967 fputs(buf, stdout);
968 } else {
969 int j;
970 char *p;
738 return gid;
739}
740
741
742static time_t
743pw_pwdpolicy(struct userconf * cnf, struct cargs * args)
744{
745 time_t result = 0;

--- 223 unchanged lines hidden (view full) ---

969 if (!pretty) {
970 char buf[_UC_MAXLINE];
971
972 fmtpwent(buf, pwd);
973 fputs(buf, stdout);
974 } else {
975 int j;
976 char *p;
971 struct group *grp = getgrgid(pwd->pw_gid);
977 struct group *grp = GETGRGID(pwd->pw_gid);
972 char uname[60] = "User &", office[60] = "[None]",
973 wphone[60] = "[None]", hphone[60] = "[None]";
974 char acexpire[32] = "[None]", pwexpire[32] = "[None]";
975 struct tm * tptr;
976
977 if ((p = strtok(pwd->pw_gecos, ",")) != NULL) {
978 strncpy(uname, p, sizeof uname);
979 uname[sizeof uname - 1] = '\0';

--- 31 unchanged lines hidden (view full) ---

1011 " Shell: %-26.26s Office: %s\n"
1012 "Work Phone: %-26.26s Home Phone: %s\n"
1013 "Acc Expire: %-26.26s Pwd Expire: %s\n",
1014 pwd->pw_name, (long) pwd->pw_uid,
1015 grp ? grp->gr_name : "(invalid)", (long) pwd->pw_gid,
1016 uname, pwd->pw_dir, pwd->pw_class,
1017 pwd->pw_shell, office, wphone, hphone,
1018 acexpire, pwexpire);
978 char uname[60] = "User &", office[60] = "[None]",
979 wphone[60] = "[None]", hphone[60] = "[None]";
980 char acexpire[32] = "[None]", pwexpire[32] = "[None]";
981 struct tm * tptr;
982
983 if ((p = strtok(pwd->pw_gecos, ",")) != NULL) {
984 strncpy(uname, p, sizeof uname);
985 uname[sizeof uname - 1] = '\0';

--- 31 unchanged lines hidden (view full) ---

1017 " Shell: %-26.26s Office: %s\n"
1018 "Work Phone: %-26.26s Home Phone: %s\n"
1019 "Acc Expire: %-26.26s Pwd Expire: %s\n",
1020 pwd->pw_name, (long) pwd->pw_uid,
1021 grp ? grp->gr_name : "(invalid)", (long) pwd->pw_gid,
1022 uname, pwd->pw_dir, pwd->pw_class,
1023 pwd->pw_shell, office, wphone, hphone,
1024 acexpire, pwexpire);
1019 setgrent();
1025 SETGRENT();
1020 j = 0;
1026 j = 0;
1021 while ((grp=getgrent()) != NULL)
1027 while ((grp=GETGRENT()) != NULL)
1022 {
1023 int i = 0;
1024 while (grp->gr_mem[i] != NULL)
1025 {
1026 if (strcmp(grp->gr_mem[i], pwd->pw_name)==0)
1027 {
1028 printf(j++ == 0 ? " Groups: %s" : ",%s", grp->gr_name);
1029 break;
1030 }
1031 ++i;
1032 }
1033 }
1028 {
1029 int i = 0;
1030 while (grp->gr_mem[i] != NULL)
1031 {
1032 if (strcmp(grp->gr_mem[i], pwd->pw_name)==0)
1033 {
1034 printf(j++ == 0 ? " Groups: %s" : ",%s", grp->gr_name);
1035 break;
1036 }
1037 ++i;
1038 }
1039 }
1034 endgrent();
1040 ENDGRENT();
1035 printf("%s\n", j ? "\n" : "");
1036 }
1037 return EXIT_SUCCESS;
1038}
1039
1040char *
1041pw_checkname(u_char *name, int gecos)
1042{

--- 71 unchanged lines hidden ---
1041 printf("%s\n", j ? "\n" : "");
1042 }
1043 return EXIT_SUCCESS;
1044}
1045
1046char *
1047pw_checkname(u_char *name, int gecos)
1048{

--- 71 unchanged lines hidden ---