Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/netbsd-1-5]: src/usr.sbin/user Pull up revisions 1.38-1.52 (requested by...



details:   https://anonhg.NetBSD.org/src/rev/195e3d756d94
branches:  netbsd-1-5
changeset: 493155:195e3d756d94
user:      he <he%NetBSD.org@localhost>
date:      Wed Jun 26 17:20:11 2002 +0000

description:
Pull up revisions 1.38-1.52 (requested by grant):
  Synchronize with recent developments:
   o add usermgmt.conf manual page
   o accept "month day year" for -e and -f option arguments of ``user''
   o support multiple uid ranges
   o fix skeleton directory changes
   o improve error handling
   o fix varargs usage

diffstat:

 usr.sbin/user/user.c |  299 ++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 226 insertions(+), 73 deletions(-)

diffs (truncated from 761 to 300 lines):

diff -r 4f2892236e6e -r 195e3d756d94 usr.sbin/user/user.c
--- a/usr.sbin/user/user.c      Wed Jun 26 17:19:55 2002 +0000
+++ b/usr.sbin/user/user.c      Wed Jun 26 17:20:11 2002 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: user.c,v 1.20.4.8 2001/02/26 18:01:17 he Exp $ */
+/* $NetBSD: user.c,v 1.20.4.9 2002/06/26 17:20:11 he Exp $ */
 
 /*
  * Copyright (c) 1999 Alistair G. Crooks.  All rights reserved.
@@ -35,7 +35,7 @@
 #ifndef lint
 __COPYRIGHT("@(#) Copyright (c) 1999 \
                The NetBSD Foundation, Inc.  All rights reserved.");
-__RCSID("$NetBSD: user.c,v 1.20.4.8 2001/02/26 18:01:17 he Exp $");
+__RCSID("$NetBSD: user.c,v 1.20.4.9 2002/06/26 17:20:11 he Exp $");
 #endif
 
 #include <sys/types.h>
@@ -80,8 +80,9 @@
        char            *u_shell;               /* user's shell */
        char            *u_basedir;             /* base directory for home */
        char            *u_expire;              /* when password will expire */
-       int             u_inactive;             /* inactive */
+       char            *u_inactive;            /* when account will expire */
        char            *u_skeldir;             /* directory for startup files */
+       char            *u_class;               /* login class */
        unsigned        u_rsize;                /* size of range array */
        unsigned        u_rc;                   /* # of ranges */
        range_t         *u_rv;                  /* the ranges */
@@ -102,7 +103,8 @@
        F_SECGROUP      = 0x0100,
        F_SHELL         = 0x0200,
        F_UID           = 0x0400,
-       F_USERNAME      = 0x0800
+       F_USERNAME      = 0x0800,
+       F_CLASS         = 0x1000
 };
 
 #define CONFFILE       "/etc/usermgmt.conf"
@@ -143,6 +145,10 @@
 #define DEF_EXPIRE     NULL
 #endif
 
+#ifndef DEF_CLASS
+#define DEF_CLASS      ""
+#endif
+
 #ifndef WAITSECS
 #define WAITSECS       10
 #endif
@@ -174,6 +180,7 @@
 #define PAX            "/bin/pax"
 #define RM             "/bin/rm"
 
+#define UNSET_INACTIVE "Null (unset)"
 #define UNSET_EXPIRY   "Null (unset)"
 
 static int asystem(const char *fmt, ...)
@@ -313,17 +320,17 @@
        FILE f;
        unsigned char *_base;
 
-       va_start(ap, fmt);
        f._flags = __SWR | __SSTR | __SALC;
        f._bf._base = f._p = (unsigned char *)malloc(128);
        if (f._bf._base == NULL)
                goto err;
        f._bf._size = f._w = 127;               /* Leave room for the NUL */
+       va_start(ap, fmt);
        ret = vfprintf(&f, fmt, ap);
+       va_end(ap);
        if (ret == -1)
                goto err;
        *f._p = '\0';
-       va_end(ap);
        _base = realloc(f._bf._base, (size_t)(ret + 1));
        if (_base == NULL)
                goto err;
@@ -385,7 +392,7 @@
        if (n == 0) {
                warnx("No \"dot\" initialisation files found");
        } else {
-               (void) asystem("cd %s; %s -rw -pe %s . %s", 
+               (void) asystem("cd %s && %s -rw -pe %s . %s", 
                                skeldir, PAX, (verbose) ? "-v" : "", dir);
        }
        (void) asystem("%s -R -h %d:%d %s", CHOWN, uid, gid, dir);
@@ -410,7 +417,7 @@
                return 0;
        }
        if ((from = fopen(_PATH_GROUP, "r")) == NULL) {
-               warn("can't create gid for %s: can't open %s", name, _PATH_GROUP);
+               warn("can't create gid for `%s': can't open `%s'", name, _PATH_GROUP);
                return 0;
        }
        if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) {
@@ -443,6 +450,7 @@
        (void) fclose(from);
        (void) fclose(to);
        if (rename(f, _PATH_GROUP) < 0) {
+               (void) unlink(f);
                warn("can't create gid: can't rename `%s' to `%s'", f, _PATH_GROUP);
                return 0;
        }
@@ -466,7 +474,7 @@
        int             cc;
 
        if ((from = fopen(_PATH_GROUP, "r")) == NULL) {
-               warn("can't create gid for %s: can't open %s", group, _PATH_GROUP);
+               warn("can't create gid for `%s': can't open `%s'", group, _PATH_GROUP);
                return 0;
        }
        if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) {
@@ -513,6 +521,7 @@
        (void) fclose(from);
        (void) fclose(to);
        if (rename(f, _PATH_GROUP) < 0) {
+               (void) unlink(f);
                warn("can't create gid: can't rename `%s' to `%s'", f, _PATH_GROUP);
                return 0;
        }
@@ -552,7 +561,7 @@
                }
        }
        if ((from = fopen(_PATH_GROUP, "r")) == NULL) {
-               warn("can't append group for %s: can't open %s", user, _PATH_GROUP);
+               warn("can't append group for `%s': can't open `%s'", user, _PATH_GROUP);
                return 0;
        }
        if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) {
@@ -605,6 +614,7 @@
        (void) fclose(from);
        (void) fclose(to);
        if (rename(f, _PATH_GROUP) < 0) {
+               (void) unlink(f);
                warn("can't create gid: can't rename `%s' to `%s'", f, _PATH_GROUP);
                return 0;
        }
@@ -669,7 +679,7 @@
                RENEW(range_t, up->u_rv, up->u_rsize, return(0));
        }
        if (up->u_rv && sscanf(cp, "%d..%d", &from, &to) == 2) {
-               for (i = 0 ; i < up->u_rc ; i++) {
+               for (i = up->u_defrc ; i < up->u_rc ; i++) {
                        if (up->u_rv[i].r_from == from && up->u_rv[i].r_to == to) {
                                break;
                        }
@@ -695,7 +705,9 @@
        FILE    *fp;
        int     ret;
        int     fd;
+#ifdef EXTENSIONS
        int     i;
+#endif
 
        (void) snprintf(template, sizeof(template), "%s.XXXXXX", CONFFILE);
        if ((fd = mkstemp(template)) < 0) {
@@ -711,7 +723,10 @@
            fprintf(fp, "base_dir\t%s\n", up->u_basedir) <= 0 ||
            fprintf(fp, "skel_dir\t%s\n", up->u_skeldir) <= 0 ||
            fprintf(fp, "shell\t\t%s\n", up->u_shell) <= 0 ||
-           fprintf(fp, "inactive\t%d\n", up->u_inactive) <= 0 ||
+#ifdef EXTENSIONS
+           fprintf(fp, "class\t\t%s\n", up->u_class) <= 0 ||
+#endif
+           fprintf(fp, "inactive\t%s\n", (up->u_inactive == NULL) ?  UNSET_INACTIVE : up->u_inactive) <= 0 ||
            fprintf(fp, "expire\t\t%s\n", (up->u_expire == NULL) ?  UNSET_EXPIRY : up->u_expire) <= 0 ||
            fprintf(fp, "preserve\t%s\n", (up->u_preserve == 0) ? "false" : "true") <= 0) {
                warn("can't write to `%s'", CONFFILE);
@@ -748,7 +763,11 @@
        memsave(&up->u_skeldir, DEF_SKELDIR, strlen(DEF_SKELDIR));
        memsave(&up->u_shell, DEF_SHELL, strlen(DEF_SHELL));
        memsave(&up->u_comment, DEF_COMMENT, strlen(DEF_COMMENT));
+#ifdef EXTENSIONS
+       memsave(&up->u_class, DEF_CLASS, strlen(DEF_CLASS));
+#endif
        up->u_rsize = 16;
+       up->u_defrc = 0;
        NEWARRAY(range_t, up->u_rv, up->u_rsize, exit(1));
        up->u_inactive = DEF_INACTIVE;
        up->u_expire = DEF_EXPIRE;
@@ -776,10 +795,23 @@
                                for (cp = s + 5 ; *cp && isspace(*cp) ; cp++) {
                                }
                                memsave(&up->u_shell, cp, strlen(cp));
+#ifdef EXTENSIONS
+                       } else if (strncmp(s, "class", 5) == 0) {
+                               for (cp = s + 5 ; *cp && isspace(*cp) ; cp++) {
+                               }
+                               memsave(&up->u_class, cp, strlen(cp));
+#endif
                        } else if (strncmp(s, "inactive", 8) == 0) {
                                for (cp = s + 8 ; *cp && isspace(*cp) ; cp++) {
                                }
-                               up->u_inactive = atoi(cp);
+                               if (strcmp(cp, UNSET_INACTIVE) == 0) {
+                                       if (up->u_inactive) {
+                                               FREE(up->u_inactive);
+                                       }
+                                       up->u_inactive = NULL;
+                               } else {
+                                       memsave(&up->u_inactive, cp, strlen(cp));
+                               }
 #ifdef EXTENSIONS
                        } else if (strncmp(s, "range", 5) == 0) {
                                for (cp = s + 5 ; *cp && isspace(*cp) ; cp++) {
@@ -844,6 +876,7 @@
        struct stat     st;
        struct tm       tm;
        time_t          expire;
+       time_t          inactive;
        char            password[PasswordLength + 1];
        char            home[MaxFileNameLen];
        char            buf[MaxFileNameLen];
@@ -879,12 +912,26 @@
        /* if no uid was specified, get next one in [low_uid..high_uid] range */
        sync_uid_gid = (strcmp(up->u_primgrp, "=uid") == 0);
        if (up->u_uid == -1) {
-               for (i = 0 ; i < up->u_rc ; i++) {
-                       if (getnextuid(sync_uid_gid, &up->u_uid, up->u_rv[i].r_from, up->u_rv[i].r_to)) {
-                               break;
-                       }
+               int     got_id = 0;
+
+               /*
+                * Look for a free UID in the command line ranges (if any).
+                * These start after the ranges specified in the config file.
+                */
+               for (i = up->u_defrc; !got_id && i < up->u_rc ; i++) {
+                       got_id = getnextuid(sync_uid_gid, &up->u_uid,
+                                       up->u_rv[i].r_from, up->u_rv[i].r_to);
                }
-               if (i == up->u_rc) {
+               /*
+                * If there were no free UIDs in the command line ranges,
+                * try the ranges from the config file (there will always
+                * be at least one default).
+                */
+               for (i = 0; !got_id && i < up->u_defrc; i++) {
+                       got_id = getnextuid(sync_uid_gid, &up->u_uid,
+                                       up->u_rv[i].r_from, up->u_rv[i].r_to);
+               }
+               if (!got_id) {
                        (void) close(ptmpfd);
                        (void) pw_abort();
                        errx(EXIT_FAILURE, "can't get next uid for %d", up->u_uid);
@@ -926,13 +973,32 @@
                /* if home directory hasn't been given, make it up */
                (void) snprintf(home, sizeof(home), "%s/%s", up->u_basedir, login);
        }
+       inactive = 0;
+       if (up->u_inactive != NULL) {
+               (void) memset(&tm, 0, sizeof(tm));
+               if (strptime(up->u_inactive, "%c", &tm) != NULL) {
+                       inactive = mktime(&tm);
+               } else if (strptime(up->u_inactive, "%B %d %Y", &tm) != NULL) {
+                       inactive = mktime(&tm);
+               } else if (isdigit(up->u_inactive[0]) != NULL) {
+                       inactive = atoi(up->u_inactive);
+               } else {
+                       warnx("Warning: inactive time `%s' invalid, account expiry off",
+                               up->u_inactive);
+               }
+       }
        expire = 0;
        if (up->u_expire != NULL) {
                (void) memset(&tm, 0, sizeof(tm));
-               if (strptime(up->u_expire, "%c", &tm) == NULL) {
-                       warnx("invalid time format `%s'", optarg);
+               if (strptime(up->u_expire, "%c", &tm) != NULL) {
+                       expire = mktime(&tm);
+               } else if (strptime(up->u_expire, "%B %d %Y", &tm) != NULL) {
+                       expire = mktime(&tm);
+               } else if (isdigit(up->u_expire[0]) != NULL) {
+                       expire = atoi(up->u_expire);
                } else {
-                       expire = mktime(&tm);
+                       warnx("Warning: expire time `%s' invalid, password expiry off",
+                               up->u_expire);
                }
        }
        if (lstat(home, &st) < 0 && !(up->u_flags & F_MKDIR)) {
@@ -950,12 +1016,17 @@
                                up->u_password, password);
                }
        }
-       cc = snprintf(buf, sizeof(buf), "%s:%s:%d:%d::%d:%ld:%s:%s:%s\n",
+       cc = snprintf(buf, sizeof(buf), "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n",
                        login,
                        password,
                        up->u_uid,
                        gid,
-                       up->u_inactive,
+#ifdef EXTENSIONS
+                       up->u_class,
+#else
+                       "",
+#endif
+                       (long) inactive,
                        (long) expire,



Home | Main Index | Thread Index | Old Index