Deleted Added
sdiff udiff text old ( 43780 ) new ( 44229 )
full compact
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 */
26
27#ifndef lint
28static const char rcsid[] =
29 "$Id: pw_user.c,v 1.25 1999/01/04 14:07:53 billf 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"
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;
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")) {
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
275 setpwent();
276 while ((pwd = getpwent()) != NULL)
277 print_user(pwd, pretty);
278 endpwent();
279 return EXIT_SUCCESS;
280 }
281 if ((a_name = getarg(args, 'n')) != NULL)
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 */
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
332 /*
333 * Remove skey record from /etc/skeykeys
334 */
335
336 rmskey(pwd->pw_name);
337
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);
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
364 /*
365 * Remove mail file
366 */
367 remove(file);
368
369 /*
370 * Remove at jobs
371 */
372 if (getpwuid(uid) == NULL)
373 rmat(uid);
374
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 ");
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 */
406 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");
452 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 */
553 if ((pwd = getpwnam(a_name->val)) == NULL)
554 errx(EX_NOUSER, "user '%s' disappeared during update", a_name->val);
555
556 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
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
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 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);
588 }
589 pclose(pfp);
590 pw_log(cnf, mode, W_USER, "%s(%ld) new user mail sent",
591 pwd->pw_name, (long) pwd->pw_uid);
592 }
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 */
601 if (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
623 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 */
643 setpwent();
644 while ((pwd = getpwent()) != NULL)
645 if (pwd->pw_uid >= (int) cnf->min_uid && pwd->pw_uid <= (int) cnf->max_uid)
646 bm_setbit(&bm, pwd->pw_uid - cnf->min_uid);
647 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 */
682 setgrent();
683 if (a_gid != NULL) {
684 if ((grp = getgrnam(a_gid->val)) == NULL) {
685 gid = (gid_t) atol(a_gid->val);
686 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;
690 } 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 */
708 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);
721 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 }
731 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;
971 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);
1019 setgrent();
1020 j = 0;
1021 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 }
1034 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 ---