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/pwd_mkdb Pull up revision 1.19 (approved by re...



details:   https://anonhg.NetBSD.org/src/rev/b1065b670c82
branches:  netbsd-1-5
changeset: 488963:b1065b670c82
user:      ad <ad%NetBSD.org@localhost>
date:      Mon Aug 07 14:26:06 2000 +0000

description:
Pull up revision 1.19 (approved by releng-1-5):
  - Move some duplicated code from main() into subroutines. In doing this,
    fix -p (it was broken if used in conjunction with either -B or -L).
  - const, KNF.

diffstat:

 usr.sbin/pwd_mkdb/pwd_mkdb.c |  459 +++++++++++++++++++-----------------------
 1 files changed, 203 insertions(+), 256 deletions(-)

diffs (truncated from 665 to 300 lines):

diff -r 5c1bc4824ee4 -r b1065b670c82 usr.sbin/pwd_mkdb/pwd_mkdb.c
--- a/usr.sbin/pwd_mkdb/pwd_mkdb.c      Mon Aug 07 14:25:00 2000 +0000
+++ b/usr.sbin/pwd_mkdb/pwd_mkdb.c      Mon Aug 07 14:26:06 2000 +0000
@@ -1,3 +1,5 @@
+/*     $NetBSD: pwd_mkdb.c,v 1.17.4.2 2000/08/07 14:26:06 ad Exp $     */
+
 /*
  * Copyright (c) 1991, 1993, 1994
  *     The Regents of the University of California.  All rights reserved.
@@ -39,7 +41,7 @@
 Copyright (c) 1991, 1993, 1994\n\
        The Regents of the University of California.  All rights reserved.\n");
 __SCCSID("from: @(#)pwd_mkdb.c 8.5 (Berkeley) 4/20/94");
-__RCSID("$NetBSD: pwd_mkdb.c,v 1.17.4.1 2000/07/27 16:25:17 itojun Exp $");
+__RCSID("$NetBSD: pwd_mkdb.c,v 1.17.4.2 2000/08/07 14:26:06 ad Exp $");
 #endif /* not lint */
 
 #include <sys/param.h>
@@ -60,8 +62,8 @@
 
 #define        INSECURE        1
 #define        SECURE          2
-#define        PERM_INSECURE   (S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
-#define        PERM_SECURE     (S_IRUSR|S_IWUSR)
+#define        PERM_INSECURE   (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
+#define        PERM_SECURE     (S_IRUSR | S_IWUSR)
 
 /* pull this out of the C library. */
 extern const char __yp_token[];
@@ -75,47 +77,42 @@
        0               /* lorder */
 };
 
-static enum state { FILE_INSECURE, FILE_SECURE, FILE_ORIG } clean;
-static struct passwd pwd;                      /* password structure */
+static enum { FILE_INSECURE, FILE_SECURE, FILE_ORIG } clean;
 static char *pname;                            /* password file name */
 static char prefix[MAXPATHLEN];
 static char oldpwdfile[MAX(MAXPATHLEN, LINE_MAX * 2)];
 static char pwd_db_tmp[MAX(MAXPATHLEN, LINE_MAX * 2)];
 static char pwd_Sdb_tmp[MAX(MAXPATHLEN, LINE_MAX * 2)];
+static int lorder = BYTE_ORDER;
 
-void   cleanup __P((void));
-void   error __P((char *));
-void   wr_error __P((char *));
-int    main __P((int, char **));
-void   mv __P((char *, char *));
-void   rm __P((char *));
-int    scan __P((FILE *, struct passwd *, u_int32_t *));
-void   usage __P((void));
+void   cleanup(void);
+void   error(const char *);
+void   wr_error(const char *);
+int    main(int, char **);
+void   install(const char *, const char *);
+void   rm(const char *);
+int    scan(FILE *, struct passwd *, int *, int *);
+void   usage(void);
+void   putdbent(DB *, struct passwd *, const char *, int, const char *, int);
+void   putyptoken(DB *dp, const char *fn);
 
 int
-main(argc, argv)
-       int argc;
-       char *argv[];
+main(int argc, char *argv[])
 {
        DB *dp, *edp;
-       DBT data, key;
        FILE *fp, *oldfp;
        sigset_t set;
-       int ch, len, makeold, tfd;
-       u_int32_t x, cnt, flags;
-       char *p;
-       const char *t;
-       char buf[MAX(MAXPATHLEN, LINE_MAX * 2)], tbuf[1024];
-       int hasyp = 0;
-       DBT ypdata, ypkey;
-       int lorder = BYTE_ORDER;
+       int ch, makeold, tfd, hasyp, flags, lineno;
+       struct passwd pwd;
 
+       hasyp = 0;
        oldfp = NULL;
        strcpy(prefix, "/");
        makeold = 0;
+       
        while ((ch = getopt(argc, argv, "d:pvBL")) != -1)
                switch(ch) {
-               case 'd':
+               case 'd':                       /* set prefix */
                        strncpy(prefix, optarg, sizeof(prefix));
                        prefix[sizeof(prefix)-1] = '\0';
                        break;
@@ -124,10 +121,10 @@
                        break;
                case 'v':                       /* backward compatible */
                        break;
-               case 'B':
+               case 'B':                       /* big-endian output */
                        lorder = BIG_ENDIAN;
                        break;
-               case 'L':
+               case 'L':                       /* little-endian output */
                        lorder = LITTLE_ENDIAN;
                        break;
                case '?':
@@ -157,16 +154,16 @@
 
        pname = *argv;
        /* Open the original password file */
-       if (!(fp = fopen(pname, "r")))
+       if ((fp = fopen(pname, "r")) == NULL)
                error(pname);
 
        openinfo.lorder = lorder;
 
        /* Open the temporary insecure password database. */
        (void)snprintf(pwd_db_tmp, sizeof(pwd_db_tmp), "%s%s.tmp", prefix,
-               _PATH_MP_DB);
-       dp = dbopen(pwd_db_tmp,
-           O_RDWR|O_CREAT|O_EXCL, PERM_INSECURE, DB_HASH, &openinfo);
+           _PATH_MP_DB);
+       dp = dbopen(pwd_db_tmp, O_RDWR | O_CREAT | O_EXCL, PERM_INSECURE,
+           DB_HASH, &openinfo);
        if (dp == NULL)
                error(pwd_db_tmp);
        clean = FILE_INSECURE;
@@ -180,9 +177,9 @@
         */
        if (makeold) {
                (void)snprintf(oldpwdfile, sizeof(oldpwdfile), "%s.orig",
-                       pname);
-               if ((tfd = open(oldpwdfile,
-                   O_WRONLY|O_CREAT|O_EXCL, PERM_INSECURE)) < 0)
+                   pname);
+               if ((tfd = open(oldpwdfile, O_WRONLY | O_CREAT | O_EXCL,
+                   PERM_INSECURE)) < 0)
                        error(oldpwdfile);
                if ((oldfp = fdopen(tfd, "w")) == NULL)
                        error(oldpwdfile);
@@ -190,27 +187,13 @@
        }
 
        /*
-        * The databases actually contain three copies of the original data.
-        * Each password file entry is converted into a rough approximation
-        * of a ``struct passwd'', with the strings placed inline.  This
-        * object is then stored as the data for three separate keys.  The
-        * first key * is the pw_name field prepended by the _PW_KEYBYNAME
-        * character.  The second key is the pw_uid field prepended by the
-        * _PW_KEYBYUID character.  The third key is the line number in the
-        * original file prepended by the _PW_KEYBYNUM character.  (The special
-        * characters are prepended to ensure that the keys do not collide.)
-        *
         * If we see something go by that looks like YP, we save a special
         * pointer record, which if YP is enabled in the C lib, will speed
         * things up.
         */
-       data.data = (u_char *)buf;
-       key.data = (u_char *)tbuf;
-       for (cnt = 1; scan(fp, &pwd, &flags); ++cnt) {
-#define        COMPACT(e)      t = e; while ((*p++ = *t++));
-
+       for (lineno = 0; scan(fp, &pwd, &flags, &lineno);) {
                /* look like YP? */
-               if((pwd.pw_name[0] == '+') || (pwd.pw_name[0] == '-'))
+               if (pwd.pw_name[0] == '+' || pwd.pw_name[0] == '-')
                        hasyp++;
 
                /*
@@ -220,249 +203,109 @@
                        if ((flags & _PASSWORD_NOUID) == 0 && pwd.pw_uid == 0)
                                warnx(
                                "line %d: superuser override in YP inclusion",
-                                   cnt);
+                                   lineno);
                        if ((flags & _PASSWORD_NOGID) == 0 && pwd.pw_gid == 0)
-                               warnx("line %d: wheel override in YP inclusion",
-                                   cnt);
-               }
-
-               if (lorder != BYTE_ORDER) {
-                       M_32_SWAP(pwd.pw_uid);
-                       M_32_SWAP(pwd.pw_gid);
-                       M_32_SWAP(pwd.pw_change);
-                       M_32_SWAP(pwd.pw_expire);
-                       M_32_SWAP(flags);
+                               warnx(
+                                   "line %d: wheel override in YP inclusion",
+                                   lineno);
                }
 
-               /* Create insecure data. */
-               p = buf;
-               COMPACT(pwd.pw_name);
-               COMPACT("*");
-               memmove(p, &pwd.pw_uid, sizeof(pwd.pw_uid));
-               p += sizeof(pwd.pw_uid);
-               memmove(p, &pwd.pw_gid, sizeof(pwd.pw_gid));
-               p += sizeof(pwd.pw_gid);
-               memmove(p, &pwd.pw_change, sizeof(pwd.pw_change));
-               p += sizeof(pwd.pw_change);
-               COMPACT(pwd.pw_class);
-               COMPACT(pwd.pw_gecos);
-               COMPACT(pwd.pw_dir);
-               COMPACT(pwd.pw_shell);
-               memmove(p, &pwd.pw_expire, sizeof(pwd.pw_expire));
-               p += sizeof(pwd.pw_expire);
-               memmove(p, &flags, sizeof(flags));
-               p += sizeof(flags);
-               data.size = p - buf;
-
-               /* Store insecure by name. */
-               tbuf[0] = _PW_KEYBYNAME;
-               len = strlen(pwd.pw_name);
-               memmove(tbuf + 1, pwd.pw_name, len);
-               key.size = len + 1;
-               if ((dp->put)(dp, &key, &data, R_NOOVERWRITE) == -1)
-                       wr_error(pwd_db_tmp);
-
-               /* Store insecure by number. */
-               tbuf[0] = _PW_KEYBYNUM;
-               x = cnt;
-               if (lorder != BYTE_ORDER)
-                       M_32_SWAP(x);
-               memmove(tbuf + 1, &x, sizeof(x));
-               key.size = sizeof(x) + 1;
-               if ((dp->put)(dp, &key, &data, R_NOOVERWRITE) == -1)
-                       wr_error(pwd_db_tmp);
-
-               /* Store insecure by uid. */
-               tbuf[0] = _PW_KEYBYUID;
-               memmove(tbuf + 1, &pwd.pw_uid, sizeof(pwd.pw_uid));
-               key.size = sizeof(pwd.pw_uid) + 1;
-               if ((dp->put)(dp, &key, &data, R_NOOVERWRITE) == -1)
-                       wr_error(pwd_db_tmp);
+               putdbent(dp, &pwd, "*", flags, pwd_db_tmp, lineno);
 
                /* Create original format password file entry */
                if (makeold) {
                        (void)fprintf(oldfp, "%s:*:%d:%d:%s:%s:%s\n",
                            pwd.pw_name, pwd.pw_uid, pwd.pw_gid, pwd.pw_gecos,
                            pwd.pw_dir, pwd.pw_shell);
-                       if (ferror(oldfp)) {
+                       if (ferror(oldfp))
                                wr_error(oldpwdfile);
-                       }
                }
        }
 
        /* Store YP token, if needed. */
-       if(hasyp) {
-               ypkey.data = (u_char *)__yp_token;
-               ypkey.size = strlen(__yp_token);
-               ypdata.data = (u_char *)NULL;
-               ypdata.size = 0;
-
-               if ((dp->put)(dp, &ypkey, &ypdata, R_NOOVERWRITE) == -1)
-                       wr_error(pwd_db_tmp);
-       }
+       if (hasyp)
+               putyptoken(dp, pwd_db_tmp);
 
-       if ((dp->close)(dp) < 0) {
+       if ((dp->close)(dp) < 0)
                wr_error(pwd_db_tmp);
-       }
        if (makeold) {
-               if (fflush(oldfp) == EOF) {
+               if (fflush(oldfp) == EOF)
                        wr_error(oldpwdfile);
-               }
-               if (fclose(oldfp) == EOF) {
+               if (fclose(oldfp) == EOF)
                        wr_error(oldpwdfile);
-               }
        }
 
        /* Open the temporary encrypted password database. */
        (void)snprintf(pwd_Sdb_tmp, sizeof(pwd_Sdb_tmp), "%s%s.tmp", prefix,
                _PATH_SMP_DB);
-       edp = dbopen(pwd_Sdb_tmp,
-           O_RDWR|O_CREAT|O_EXCL, PERM_SECURE, DB_HASH, &openinfo);
+       edp = dbopen(pwd_Sdb_tmp, O_RDWR | O_CREAT | O_EXCL, PERM_SECURE,
+          DB_HASH, &openinfo);
        if (!edp)
                error(pwd_Sdb_tmp);
        clean = FILE_SECURE;
 
        rewind(fp);
-       for (cnt = 1; scan(fp, &pwd, &flags); ++cnt) {
-
-               if (lorder != BYTE_ORDER) {
-                       M_32_SWAP(pwd.pw_uid);
-                       M_32_SWAP(pwd.pw_gid);
-                       M_32_SWAP(pwd.pw_change);
-                       M_32_SWAP(pwd.pw_expire);
-                       M_32_SWAP(flags);



Home | Main Index | Thread Index | Old Index