]> git.cameronkatri.com Git - pw-darwin.git/blobdiff - chpass/chpass.c
Trim a domain part for wtmp as same as showed by "netstat -r".
[pw-darwin.git] / chpass / chpass.c
index 0852c536d2a7d26be62258ee5e3d612640a55c13..982548fa5a46986318f3f6b27f7f6e95d4c5e9b3 100644 (file)
@@ -38,7 +38,9 @@ static char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)chpass.c   8.4 (Berkeley) 4/2/94";
+static char sccsid[] = "From: @(#)chpass.c     8.4 (Berkeley) 4/2/94";
+static char rcsid[] =
+       "$Id: chpass.c,v 1.13 1997/02/22 19:54:25 peter Exp $";
 #endif /* not lint */
 
 #include <sys/param.h>
@@ -60,11 +62,15 @@ static char sccsid[] = "@(#)chpass.c        8.4 (Berkeley) 4/2/94";
 #include <pw_scan.h>
 #include <pw_util.h>
 #include "pw_copy.h"
+#ifdef YP
+#include <rpcsvc/yp.h>
+int yp_errno = YP_TRUE;
+#include "pw_yp.h"
+#endif
 
 #include "chpass.h"
 #include "pathnames.h"
 
-char *progname = "chpass";
 char *tempname;
 uid_t uid;
 
@@ -76,13 +82,22 @@ main(argc, argv)
        int argc;
        char **argv;
 {
-       enum { NEWSH, LOADENTRY, EDITENTRY } op;
+       enum { NEWSH, LOADENTRY, EDITENTRY, NEWPW, NEWEXP } op;
        struct passwd *pw, lpw;
+       char *username = NULL;
        int ch, pfd, tfd;
        char *arg;
+#ifdef YP
+       int force_local = 0;
+       int force_yp = 0;
+#endif
 
        op = EDITENTRY;
-       while ((ch = getopt(argc, argv, "a:s:")) != EOF)
+#ifdef YP
+       while ((ch = getopt(argc, argv, "a:p:s:e:d:h:oly")) != -1)
+#else
+       while ((ch = getopt(argc, argv, "a:p:s:e:")) != -1)
+#endif
                switch(ch) {
                case 'a':
                        op = LOADENTRY;
@@ -92,6 +107,50 @@ main(argc, argv)
                        op = NEWSH;
                        arg = optarg;
                        break;
+               case 'p':
+                       op = NEWPW;
+                       arg = optarg;
+                       break;
+               case 'e':
+                       op = NEWEXP;
+                       arg = optarg;
+                       break;
+#ifdef YP
+               case 'h':
+#ifdef PARANOID
+                       if (getuid()) {
+                               warnx("Only the superuser can use the -h flag");
+                       } else {
+#endif
+                               yp_server = optarg;
+#ifdef PARANOID
+                       }
+#endif
+                       break;
+               case 'd':
+#ifdef PARANOID
+                       if (getuid()) {
+                               warnx("Only the superuser can use the -d flag");
+                       } else {
+#endif
+                               yp_domain = optarg;
+                               if (yp_server == NULL)
+                                       yp_server = "localhost";
+#ifdef PARANOID
+                       }
+#endif
+                       break;
+               case 'l':
+                       _use_yp = 0;
+                       force_local = 1;
+                       break;
+               case 'y':
+                       _use_yp = force_yp = 1;
+                       break;
+               case 'o':
+                       force_old++;
+                       break;
+#endif
                case '?':
                default:
                        usage();
@@ -101,8 +160,17 @@ main(argc, argv)
 
        uid = getuid();
 
-       if (op == EDITENTRY || op == NEWSH)
+       if (op == EDITENTRY || op == NEWSH || op == NEWPW || op == NEWEXP)
                switch(argc) {
+#ifdef YP
+               case 0:
+                       GETPWUID(uid)
+                       get_yp_master(1); /* XXX just to set the suser flag */
+                       break;
+               case 1:
+                       GETPWNAM(*argv)
+                       get_yp_master(1); /* XXX just to set the suser flag */
+#else
                case 0:
                        if (!(pw = getpwuid(uid)))
                                errx(1, "unknown user: uid %u", uid);
@@ -110,13 +178,13 @@ main(argc, argv)
                case 1:
                        if (!(pw = getpwnam(*argv)))
                                errx(1, "unknown user: %s", *argv);
+#endif
                        if (uid && uid != pw->pw_uid)
                                baduser();
                        break;
                default:
                        usage();
                }
-
        if (op == NEWSH) {
                /* protect p_shell -- it thinks NULL is /bin/sh */
                if (!arg[0])
@@ -125,6 +193,13 @@ main(argc, argv)
                        pw_error((char *)NULL, 0, 1);
        }
 
+       if (op == NEWEXP) {
+               if (uid)        /* only root can change expire */
+                       baduser();
+               if (p_expire(arg, pw, (ENTRY *)NULL))
+                       pw_error((char *)NULL, 0, 1);
+       }
+
        if (op == LOADENTRY) {
                if (uid)
                        baduser();
@@ -132,6 +207,17 @@ main(argc, argv)
                if (!pw_scan(arg, pw))
                        exit(1);
        }
+       username = pw->pw_name;
+
+       if (op == NEWPW) {
+               if (uid)
+                       baduser();
+
+               if(strchr(arg, ':')) {
+                       errx(1, "invalid format for password");
+               }
+               pw->pw_passwd = arg;
+       }
 
        /*
         * The temporary file/file descriptor usage is a little tricky here.
@@ -168,18 +254,26 @@ main(argc, argv)
                (void)unlink(tempname);
                tfd = pw_tmp();
        }
-               
+
+#ifdef YP
+       if (_use_yp) {
+               yp_submit(pw);
+               (void)unlink(tempname);
+       } else {
+#endif /* YP */
        pw_copy(pfd, tfd, pw);
 
-       if (!pw_mkdb())
+       if (!pw_mkdb(username))
                pw_error((char *)NULL, 0, 1);
+#ifdef YP
+       }
+#endif /* YP */
        exit(0);
 }
 
 void
 baduser()
 {
-
        errx(1, "%s", strerror(EACCES));
 }
 
@@ -187,6 +281,11 @@ void
 usage()
 {
 
-       (void)fprintf(stderr, "usage: chpass [-a list] [-s shell] [user]\n");
+       (void)fprintf(stderr,
+#ifdef YP
+               "usage: chpass [-l] [-y] [-d domain [-h host]] [-a list] [-p encpass] [-s shell] [-e mmm dd yy] [user]\n");
+#else
+               "usage: chpass [-a list] [-p encpass] [-s shell] [-e mmm dd yy] [user]\n");
+#endif
        exit(1);
 }