Source-Changes-HG archive

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

[src/netbsd-1-4]: src/usr.sbin/user Pull up revisions 1.1-1.10 (new) (request...



details:   https://anonhg.NetBSD.org/src/rev/e7d65200eee8
branches:  netbsd-1-4
changeset: 470127:e7d65200eee8
user:      he <he%NetBSD.org@localhost>
date:      Fri Jan 21 00:01:07 2000 +0000

description:
Pull up revisions 1.1-1.10 (new) (requested by agc):
  Add tools to manage users and groups.

diffstat:

 usr.sbin/user/user.c |  1511 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 1511 insertions(+), 0 deletions(-)

diffs (truncated from 1515 to 300 lines):

diff -r 8ddd3f5f860c -r e7d65200eee8 usr.sbin/user/user.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/usr.sbin/user/user.c      Fri Jan 21 00:01:07 2000 +0000
@@ -0,0 +1,1511 @@
+/* $NetBSD: user.c,v 1.10.2.2 2000/01/21 00:01:07 he Exp $ */
+
+/*
+ * Copyright (c) 1999 Alistair G. Crooks.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by Alistair G. Crooks.
+ * 4. The name of the author may not be used to endorse or promote
+ *    products derived from this software without specific prior written
+ *    permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
+ * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
+ * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+#include <sys/types.h>
+#include <sys/param.h>
+#include <sys/stat.h>
+
+#include <ctype.h>
+#include <dirent.h>
+#include <err.h>
+#include <fcntl.h>
+#include <grp.h>
+#include <pwd.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+#include <unistd.h>
+#include <util.h>
+
+#include "defs.h"
+#include "usermgmt.h"
+
+/* this struct describes a uid range */
+typedef struct range_t {
+       int     r_from;         /* low uid */
+       int     r_to;           /* high uid */
+} range_t;
+
+/* this struct encapsulates the user information */
+typedef struct user_t {
+       int             u_uid;                  /* uid of user */
+       char            *u_password;            /* encrypted password */
+       char            *u_comment;             /* comment field */
+       int             u_homeset;              /* home dir has been set */
+       char            *u_home;                /* home directory */
+       char            *u_primgrp;             /* primary group */
+       int             u_groupc;               /* # of secondary groups */
+       char            *u_groupv[NGROUPS_MAX]; /* secondary groups */
+       char            *u_shell;               /* user's shell */
+       char            *u_basedir;             /* base directory for home */
+       char            *u_expire;              /* when password will expire */
+       int             u_inactive;             /* inactive */
+       int             u_mkdir;                /* make the home directory */
+       int             u_dupuid;               /* duplicate uids are allowed */
+       char            *u_skeldir;             /* directory for startup files */
+       unsigned        u_rsize;                /* size of range array */
+       unsigned        u_rc;                   /* # of ranges */
+       range_t         *u_rv;                  /* the ranges */
+       unsigned        u_defrc;                /* # of ranges in defaults */
+       int             u_preserve;             /* preserve uids on deletion */
+} user_t;
+
+#define CONFFILE       "/etc/usermgmt.conf"
+
+#ifndef DEF_GROUP
+#define DEF_GROUP      "users"
+#endif
+
+#ifndef DEF_BASEDIR
+#define DEF_BASEDIR    "/home"
+#endif
+
+#ifndef DEF_SKELDIR
+#define DEF_SKELDIR    "/etc/skel"
+#endif
+
+#ifndef DEF_SHELL
+#define DEF_SHELL      "/bin/csh"
+#endif
+
+#ifndef DEF_COMMENT
+#define DEF_COMMENT    ""
+#endif
+
+#ifndef DEF_LOWUID
+#define DEF_LOWUID     1000
+#endif
+
+#ifndef DEF_HIGHUID
+#define DEF_HIGHUID    60000
+#endif
+
+#ifndef DEF_INACTIVE
+#define DEF_INACTIVE   0
+#endif
+
+#ifndef DEF_EXPIRE
+#define DEF_EXPIRE     (char *) NULL
+#endif
+
+#ifndef MASTER
+#define MASTER         "/etc/master.passwd"
+#endif
+
+#ifndef ETCGROUP
+#define ETCGROUP       "/etc/group"
+#endif
+
+#ifndef WAITSECS
+#define WAITSECS       10
+#endif
+
+#ifndef NOBODY_UID
+#define NOBODY_UID     32767
+#endif
+
+/* some useful constants */
+enum {
+       MaxShellNameLen = 256,
+       MaxFileNameLen = MAXPATHLEN,
+       MaxUserNameLen = 32,
+       MaxFieldNameLen = 32,
+       MaxCommandLen = 2048,
+       MaxEntryLen = 2048,
+       PasswordLength = 13,
+
+       LowGid = DEF_LOWUID,
+       HighGid = DEF_HIGHUID
+};
+
+/* Full paths of programs used here */
+#define CHOWN          "/usr/sbin/chown"
+#define MKDIR          "/bin/mkdir"
+#define MV             "/bin/mv"
+#define NOLOGIN                "/sbin/nologin"
+#define PAX            "/bin/pax"
+#define RM             "/bin/rm"
+
+#define UNSET_EXPIRY   "Null (unset)"
+
+static int     verbose;
+
+/* if *cpp is non-null, free it, then assign `n' chars of `s' to it */
+static void
+memsave(char **cpp, char *s, size_t n)
+{
+       if (*cpp != (char *) NULL) {
+               FREE(*cpp);
+       }
+       NEWARRAY(char, *cpp, n + 1, exit(1));
+       (void) memcpy(*cpp, s, n);
+       (*cpp)[n] = 0;
+}
+
+/* a replacement for system(3) */
+static int
+asystem(char *fmt, ...)
+{
+       va_list vp;
+       char    buf[MaxCommandLen];
+       int     ret;
+
+       va_start(vp, fmt);
+       (void) vsnprintf(buf, sizeof(buf), fmt, vp);
+       va_end(vp);
+       if (verbose) {
+               (void) printf("Command: %s\n", buf);
+       }
+       if ((ret = system(buf)) != 0) {
+               warnx("[Warning] can't system `%s'", buf);
+       }
+       return ret;
+}
+
+#define NetBSD_1_4_K   104110000
+
+#if defined(__NetBSD_Version__) && (__NetBSD_Version__ < NetBSD_1_4_K)
+/* bounds checking strncpy */
+static int
+strlcpy(char *to, char *from, size_t tosize)
+{
+       size_t  n;
+       int     fromsize;
+
+       fromsize = strlen(from);
+       n = MIN(tosize - 1, fromsize);
+       (void) memcpy(to, from, n);
+       to[n] = 0;
+       return fromsize;
+}
+#endif
+
+#ifdef EXTENSIONS
+/* return 1 if all of `s' is numeric */
+static int
+is_number(char *s)
+{
+       for ( ; *s ; s++) {
+               if (!isdigit(*s)) {
+                       return 0;
+               }
+       }
+       return 1;
+}
+#endif
+
+/*
+ * check that the effective uid is 0 - called from funcs which will
+ * modify data and config files.
+ */
+static void
+checkeuid(void)
+{
+       if (geteuid() != 0) {
+               errx(EXIT_FAILURE, "Program must be run as root");
+       }
+}
+
+/* copy any dot files into the user's home directory */
+static int
+copydotfiles(char *skeldir, int uid, int gid, char *dir)
+{
+       struct dirent   *dp;
+       DIR             *dirp;
+       int             n;
+
+       if ((dirp = opendir(skeldir)) == (DIR *) NULL) {
+               warn("can't open source . files dir `%s'", skeldir);
+               return 0;
+       }
+       for (n = 0; (dp = readdir(dirp)) != (struct dirent *) NULL && n == 0 ; ) {
+               if (strcmp(dp->d_name, ".") == 0 ||
+                   strcmp(dp->d_name, "..") == 0) {
+                       continue;
+               }
+               n = 1;
+       }
+       (void) closedir(dirp);
+       if (n == 0) {
+               warnx("No \"dot\" initialisation files found");
+       } else {
+               (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);
+       return n;
+}
+
+/* create a group entry with gid `gid' */
+static int
+creategid(char *group, int gid, char *name)
+{
+       struct stat     st;
+       FILE            *from;
+       FILE            *to;
+       char            buf[MaxEntryLen];
+       char            f[MaxFileNameLen];
+       int             fd;
+       int             cc;
+
+       if ((from = fopen(ETCGROUP, "r")) == (FILE *) NULL) {
+               warn("can't create gid for %s: can't open %s", name, ETCGROUP);
+               return 0;
+       }
+       if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) {
+               warn("can't lock `%s'", ETCGROUP);
+       }
+       (void) fstat(fileno(from), &st);
+       (void) snprintf(f, sizeof(f), "%s.XXXXXX", ETCGROUP);
+       if ((fd = mkstemp(f)) < 0) {
+               (void) fclose(from);
+               warn("can't create gid: mkstemp failed");
+               return 0;
+       }
+       if ((to = fdopen(fd, "w")) == (FILE *) NULL) {



Home | Main Index | Thread Index | Old Index