Source-Changes-HG archive

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

[src/trunk]: src/usr.sbin/pwd_mkdb add syslog logging.



details:   https://anonhg.NetBSD.org/src/rev/225bba5e22a4
branches:  trunk
changeset: 757186:225bba5e22a4
user:      christos <christos%NetBSD.org@localhost>
date:      Wed Aug 18 08:06:39 2010 +0000

description:
add syslog logging.

diffstat:

 usr.sbin/pwd_mkdb/pwd_mkdb.8 |   11 ++-
 usr.sbin/pwd_mkdb/pwd_mkdb.c |  149 +++++++++++++++++++++++-------------------
 2 files changed, 88 insertions(+), 72 deletions(-)

diffs (truncated from 431 to 300 lines):

diff -r 8b3031373a35 -r 225bba5e22a4 usr.sbin/pwd_mkdb/pwd_mkdb.8
--- a/usr.sbin/pwd_mkdb/pwd_mkdb.8      Wed Aug 18 04:16:12 2010 +0000
+++ b/usr.sbin/pwd_mkdb/pwd_mkdb.8      Wed Aug 18 08:06:39 2010 +0000
@@ -1,4 +1,4 @@
-.\"    $NetBSD: pwd_mkdb.8,v 1.26 2009/06/19 05:50:39 wiz Exp $
+.\"    $NetBSD: pwd_mkdb.8,v 1.27 2010/08/18 08:06:39 christos Exp $
 .\"
 .\" Copyright (c) 1991, 1993
 .\"    The Regents of the University of California.  All rights reserved.
@@ -29,7 +29,7 @@
 .\"
 .\"    from: @(#)pwd_mkdb.8    8.2 (Berkeley) 4/27/95
 .\"
-.Dd June 18, 2009
+.Dd August 18, 2010
 .Dt PWD_MKDB 8
 .Os
 .Sh NAME
@@ -37,7 +37,7 @@
 .Nd generate the password databases
 .Sh SYNOPSIS
 .Nm
-.Op Fl BLpsvw
+.Op Fl BLlpsvw
 .Op Fl c Ar cachesize
 .Op Fl d Ar directory
 .Op Fl u Ar username
@@ -84,6 +84,10 @@
 .It Fl L
 Store data in little-endian format (see also
 .Fl B ) .
+.It Fl l
+Use
+.Xr syslog 3
+to report errors.
 .It Fl p
 Create a Version 7 style password file and install it into
 .Dq Pa /etc/passwd .
@@ -159,6 +163,7 @@
 .Xr db 3 ,
 .Xr getpwent 3 ,
 .Xr pw_mkdb 3 ,
+.Xr syslog 3 ,
 .Xr passwd 5 ,
 .Xr useradd 8 ,
 .Xr userdel 8 ,
diff -r 8b3031373a35 -r 225bba5e22a4 usr.sbin/pwd_mkdb/pwd_mkdb.c
--- a/usr.sbin/pwd_mkdb/pwd_mkdb.c      Wed Aug 18 04:16:12 2010 +0000
+++ b/usr.sbin/pwd_mkdb/pwd_mkdb.c      Wed Aug 18 08:06:39 2010 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pwd_mkdb.c,v 1.51 2010/01/10 19:04:35 snj Exp $        */
+/*     $NetBSD: pwd_mkdb.c,v 1.52 2010/08/18 08:06:39 christos Exp $   */
 
 /*
  * Copyright (c) 2000, 2009 The NetBSD Foundation, Inc.
@@ -90,7 +90,7 @@
  The NetBSD Foundation, Inc.  All rights reserved.\
   Copyright (c) 1991, 1993, 1994\
  The Regents of the University of California.  All rights reserved.");
-__RCSID("$NetBSD: pwd_mkdb.c,v 1.51 2010/01/10 19:04:35 snj Exp $");
+__RCSID("$NetBSD: pwd_mkdb.c,v 1.52 2010/08/18 08:06:39 christos Exp $");
 #endif /* not lint */
 
 #if HAVE_NBTOOL_CONFIG_H
@@ -111,10 +111,12 @@
 #include <err.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <syslog.h>
 #include <limits.h>
 #include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <stdarg.h>
 #include <string.h>
 #include <unistd.h>
 #include <util.h>
@@ -158,6 +160,7 @@
 static char    prefix[MAXPATHLEN];
 static char    oldpwdfile[MAX(MAXPATHLEN, LINE_MAX * 2)];
 static int     lorder = BYTE_ORDER;
+static int     logsyslog;
 static int     clean;
 static int     verbose;
 static int     warning;
@@ -167,7 +170,8 @@
 void   bailout(void) __attribute__((__noreturn__));
 void   cp(const char *, const char *, mode_t);
 void   deldbent(struct pwddb *, int, void *);
-void   error(const char *);
+void   mkpw_error(const char *, ...);
+void   mkpw_warning(const char *, ...);
 int    getdbent(struct pwddb *, int, void *, struct passwd **);
 void   inconsistency(void);
 void   install(const char *, const char *);
@@ -214,7 +218,7 @@
 
        db->db = dbopen(db->dbname, flags, perm, DB_HASH, &openinfo);
        if (db->db == NULL)
-               error(db->dbname);
+               mkpw_error("Cannot open `%s'", db->dbname);
 
        db->fname = dbname;
        db->rversion = getversion(dbname);
@@ -224,28 +228,25 @@
                db->wversion = req_version;
 
        if (warning && db->rversion == 0 && db->wversion == 0) {
-               warnx("Database %s is a version %u database.",
+               mkpw_warning("Database %s is a version %u database.",
                    db->fname, db->rversion);
-               warnx("Use %s -V 1 to upgrade once you've recompiled "
+               mkpw_warning("Use %s -V 1 to upgrade once you've recompiled "
                    "all your binaries.", getprogname());
        }
        if (db->wversion != db->rversion) {
                if (username != NULL) {
-                       (void)fprintf(stderr, "%s: you cannot change a single "
+                       mkpw_warning("You cannot change a single "
                            "record from version %u to version %u\n",
-                           getprogname(), db->rversion, db->wversion);
+                           db->rversion, db->wversion);
                        bailout();
                } else if (verbose) {
-                   (void)fprintf(stderr, "%s: changing %s from version "
-                       "%u to version %u\n",
-                       getprogname(), db->fname,
-                       db->rversion, db->wversion);
+                   mkpw_warning("Changing %s from version %u to version %u",
+                       db->fname, db->rversion, db->wversion);
                }
        } else {
                if (verbose)
-                       (void)fprintf(stderr, "%s: %s version %u "
-                           "requested %u\n", getprogname(), db->fname,
-                           db->rversion, db->wversion);
+                       mkpw_warning("File `%s' version %u requested %u",
+                           db->fname, db->rversion, db->wversion);
        }
 
        setversion(db);
@@ -276,9 +277,10 @@
        cachesize = 0;
        verbose = 0;
        warning = 0;
+       logsyslog = 0;
        req_version = ~0U;
 
-       while ((ch = getopt(argc, argv, "BLc:d:psu:V:vw")) != -1)
+       while ((ch = getopt(argc, argv, "BLc:d:lpsu:V:vw")) != -1)
                switch (ch) {
                case 'B':                       /* big-endian output */
                        lorder = BIG_ENDIAN;
@@ -292,6 +294,10 @@
                case 'd':                       /* set prefix */
                        (void)strlcpy(prefix, optarg, sizeof(prefix));
                        break;
+               case 'l':
+                       openlog(getprogname(), LOG_PID, LOG_AUTH);
+                       logsyslog = 1;
+                       break;
                case 'p':                       /* create V7 "file.orig" */
                        makeold = 1;
                        break;
@@ -303,8 +309,10 @@
                        break;
                case 'V':
                        req_version = (uint32_t)atoi(optarg);
-                       if (req_version > 1)
-                               err(1, "Unknown version %u\n", req_version);
+                       if (req_version > 1) {
+                               mkpw_warning("Unknown version %u", req_version);
+                               return EXIT_FAILURE;
+                       }
                        break;
                case 'v':
                        verbose++;
@@ -350,12 +358,12 @@
        pname = *argv;
        /* Open the original password file */
        if ((fp = fopen(pname, "r")) == NULL)
-               error(pname);
+               mkpw_error("Cannot open `%s'", pname);
 
        openinfo.lorder = lorder;
 
        if (fstat(fileno(fp), &st) == -1)
-               error(pname);
+               mkpw_error("Cannot stat `%s'", pname);
 
        if (cachesize) {
                openinfo.cachesize = cachesize;
@@ -393,10 +401,10 @@
                    pname);
                if ((tfd = open(oldpwdfile, O_WRONLY | O_CREAT | O_EXCL,
                    PERM_INSECURE)) < 0)
-                       error(oldpwdfile);
+                       mkpw_error("Cannot create `%s'", oldpwdfile);
                clean |= FILE_ORIG;
                if ((oldfp = fdopen(tfd, "w")) == NULL)
-                       error(oldpwdfile);
+                       mkpw_error("Cannot fdopen `%s'", oldpwdfile);
        }
 
        if (username != NULL) {
@@ -444,11 +452,11 @@
                        if (pwd.pw_name[0] == '+') {
                                if ((flags & _PASSWORD_NOUID) == 0 &&
                                    pwd.pw_uid == 0)
-                                       warnx("line %d: superuser override "
-                                           "in YP inclusion", lineno);
+                                       mkpw_warning("line %d: superuser "
+                                           "override in YP inclusion", lineno);
                                if ((flags & _PASSWORD_NOGID) == 0 &&
                                    pwd.pw_gid == 0)
-                                       warnx("line %d: wheel override "
+                                       mkpw_warning("line %d: wheel override "
                                            "in YP inclusion", lineno);
                        }
 
@@ -461,7 +469,7 @@
                        continue;
 
                if (found) {
-                       warnx("user `%s' listed twice in password file",
+                       mkpw_warning("user `%s' listed twice in password file",
                            username);
                        bailout();
                }
@@ -537,7 +545,7 @@
                if (hasyp)
                        putyptoken(&sdb);
        } else if (!found) {
-               warnx("user `%s' not found in password file", username);
+               mkpw_warning("user `%s' not found in password file", username);
                bailout();
        }
 
@@ -592,9 +600,8 @@
         *      -- The Who
         */
        if ((p = strchr(line, '\n')) == NULL) {
-               warnx("line too long");
                errno = EFTYPE; /* XXX */
-               error(pname);
+               mkpw_error("%s, %d: line too long", pname, *lineno);
        }
        *p = '\0';
        if (strcmp(line, "+") == 0) {
@@ -603,9 +610,8 @@
        }
        oflags = 0;
        if (!pw_scan(line, pw, &oflags)) {
-               warnx("at line #%d", *lineno);
                errno = EFTYPE; /* XXX */
-               error(pname);
+               mkpw_error("%s, %d: Syntax mkpw_error", pname, *lineno);
        }
        *flags = oflags;
 
@@ -616,16 +622,10 @@
 install(const char *from, const char *to)
 {
        char buf[MAXPATHLEN];
-       char errbuf[BUFSIZ];
-       int sverrno;
 
        (void)snprintf(buf, sizeof(buf), "%s%s", prefix, to);
-       if (rename(from, buf)) {
-               sverrno = errno;
-               (void)snprintf(errbuf, sizeof(errbuf), "%s to %s", from, buf);
-               errno = sverrno;
-               error(errbuf);
-       }
+       if (rename(from, buf))
+               mkpw_error("Cannot rename `%s' to `%s'", from, buf);
 }
 
 void
@@ -640,13 +640,13 @@
 cp(const char *from, const char *to, mode_t mode)              
 {               
        static char buf[MAXBSIZE];
-       int from_fd, to_fd, sverrno;
+       int from_fd, to_fd;
        ssize_t rcount, wcount;
 
        if ((from_fd = open(from, O_RDONLY, 0)) < 0)
-               error(from);
+               mkpw_error("Cannot open `%s'", from);
        if ((to_fd = open(to, O_WRONLY | O_CREAT | O_EXCL, mode)) < 0)
-               error(to);
+               mkpw_error("Cannot open `%s'", to);
        while ((rcount = read(from_fd, buf, MAXBSIZE)) > 0) {
                wcount = write(to_fd, buf, (size_t)rcount);
                if (rcount != wcount || wcount == -1)
@@ -661,41 +661,50 @@
        return;
 
 on_error:



Home | Main Index | Thread Index | Old Index