prettyness police
[unix-history] / usr / src / usr.bin / chpass / field.c
index 2e7bc21..d9af0c3 100644 (file)
 /*
 /*
- * Copyright (c) 1988 The Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1988, 1993
+ *     The Regents of the University of California.  All rights reserved.
  *
  *
- * Redistribution and use in source and binary forms are permitted
- * provided that the above copyright notice and this paragraph are
- * duplicated in all such forms and that any documentation,
- * advertising materials, and other materials related to such
- * distribution and use acknowledge that the software was developed
- * by the University of California, Berkeley.  The name of the
- * University may not be used to endorse or promote products derived
- * from this software without specific prior written permission.
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ * %sccs.include.redist.c%
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)field.c    5.11 (Berkeley) %G%";
+static char sccsid[] = "@(#)field.c    8.3 (Berkeley) %G%";
 #endif /* not lint */
 
 #include <sys/param.h>
 #endif /* not lint */
 
 #include <sys/param.h>
-#include <pwd.h>
+
+#include <ctype.h>
+#include <err.h>
+#include <errno.h>
 #include <grp.h>
 #include <grp.h>
-#include <string.h>
+#include <pwd.h>
 #include <stdio.h>
 #include <stdio.h>
-#include <ctype.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
 #include "chpass.h"
 #include "pathnames.h"
 
 /* ARGSUSED */
 #include "chpass.h"
 #include "pathnames.h"
 
 /* ARGSUSED */
+int
 p_login(p, pw, ep)
        char *p;
        struct passwd *pw;
 p_login(p, pw, ep)
        char *p;
        struct passwd *pw;
-       struct entry *ep;
+       ENTRY *ep;
 {
        if (!*p) {
 {
        if (!*p) {
-               (void)fprintf(stderr, "chpass: empty login field.\n");
-               return(1);
+               warnx("empty login field");
+               return (1);
        }
        if (*p == '-') {
        }
        if (*p == '-') {
-               (void)fprintf(stderr,
-                   "chpass: login names may not begin with a hyphen.\n");
-               return(1);
+               warnx("login names may not begin with a hyphen");
+               return (1);
        }
        if (!(pw->pw_name = strdup(p))) {
        }
        if (!(pw->pw_name = strdup(p))) {
-               (void)fprintf(stderr, "chpass: can't save entry.\n");
-               return(1);
+               warnx("can't save entry");
+               return (1);
        }
        }
-       if (index(p, '.'))
-               (void)fprintf(stderr,
-                   "chpass: \'.\' is dangerous in a login name.\n");
+       if (strchr(p, '.'))
+               warnx("\'.\' is dangerous in a login name");
        for (; *p; ++p)
                if (isupper(*p)) {
        for (; *p; ++p)
                if (isupper(*p)) {
-                       (void)fprintf(stderr,
-                           "chpass: upper-case letters are dangerous in a login name.\n");
+                       warnx("upper-case letters are dangerous in a login name");
                        break;
                }
                        break;
                }
-       return(0);
+       return (0);
 }
 
 /* ARGSUSED */
 }
 
 /* ARGSUSED */
+int
 p_passwd(p, pw, ep)
        char *p;
        struct passwd *pw;
 p_passwd(p, pw, ep)
        char *p;
        struct passwd *pw;
-       struct entry *ep;
+       ENTRY *ep;
 {
        if (!*p)
                pw->pw_passwd = "";     /* "NOLOGIN"; */
        else if (!(pw->pw_passwd = strdup(p))) {
 {
        if (!*p)
                pw->pw_passwd = "";     /* "NOLOGIN"; */
        else if (!(pw->pw_passwd = strdup(p))) {
-               (void)fprintf(stderr, "chpass: can't save password entry.\n");
-               return(1);
+               warnx("can't save password entry");
+               return (1);
        }
        
        }
        
-       return(0);
+       return (0);
 }
 
 /* ARGSUSED */
 }
 
 /* ARGSUSED */
+int
 p_uid(p, pw, ep)
 p_uid(p, pw, ep)
-       register char *p;
+       char *p;
        struct passwd *pw;
        struct passwd *pw;
-       struct entry *ep;
+       ENTRY *ep;
 {
 {
-       int id;
+       uid_t id;
+       char *np;
 
        if (!*p) {
 
        if (!*p) {
-               (void)fprintf(stderr, "chpass: empty uid field.\n");
-               return(1);
+               warnx("empty uid field");
+               return (1);
        }
        if (!isdigit(*p)) {
        }
        if (!isdigit(*p)) {
-               (void)fprintf(stderr, "chpass: illegal uid.\n");
-               return(1);
+               warnx("illegal uid");
+               return (1);
        }
        }
-       id = atoi(p);
-       if ((u_int)id > USHRT_MAX) {
-               (void)fprintf(stderr, "chpass: %d > max uid value (%d).\n",
-                   id, USHRT_MAX);
-               return(1);
+       errno = 0;
+       id = strtoul(p, &np, 10);
+       if (*np || (id == ULONG_MAX && errno == ERANGE)) {
+               warnx("illegal uid");
+               return (1);
        }
        pw->pw_uid = id;
        }
        pw->pw_uid = id;
-       return(0);
+       return (0);
 }
 
 /* ARGSUSED */
 }
 
 /* ARGSUSED */
+int
 p_gid(p, pw, ep)
 p_gid(p, pw, ep)
-       register char *p;
+       char *p;
        struct passwd *pw;
        struct passwd *pw;
-       struct entry *ep;
+       ENTRY *ep;
 {
        struct group *gr;
 {
        struct group *gr;
-       int id;
+       gid_t id;
+       char *np;
 
        if (!*p) {
 
        if (!*p) {
-               (void)fprintf(stderr, "chpass: empty gid field.\n");
-               return(1);
+               warnx("empty gid field");
+               return (1);
        }
        if (!isdigit(*p)) {
                if (!(gr = getgrnam(p))) {
        }
        if (!isdigit(*p)) {
                if (!(gr = getgrnam(p))) {
-                       (void)fprintf(stderr,
-                           "chpass: unknown group %s.\n", p);
-                       return(1);
+                       warnx("unknown group %s", p);
+                       return (1);
                }
                pw->pw_gid = gr->gr_gid;
                }
                pw->pw_gid = gr->gr_gid;
-               return(0);
+               return (0);
        }
        }
-       id = atoi(p);
-       if ((u_int)id > USHRT_MAX) {
-               (void)fprintf(stderr, "chpass: %d > max gid value (%d).\n",
-                   id, USHRT_MAX);
-               return(1);
+       errno = 0;
+       id = strtoul(p, &np, 10);
+       if (*np || (id == ULONG_MAX && errno == ERANGE)) {
+               warnx("illegal gid");
+               return (1);
        }
        pw->pw_gid = id;
        }
        pw->pw_gid = id;
-       return(0);
+       return (0);
 }
 
 /* ARGSUSED */
 }
 
 /* ARGSUSED */
+int
 p_class(p, pw, ep)
        char *p;
        struct passwd *pw;
 p_class(p, pw, ep)
        char *p;
        struct passwd *pw;
-       struct entry *ep;
+       ENTRY *ep;
 {
        if (!*p)
                pw->pw_class = "";
        else if (!(pw->pw_class = strdup(p))) {
 {
        if (!*p)
                pw->pw_class = "";
        else if (!(pw->pw_class = strdup(p))) {
-               (void)fprintf(stderr, "chpass: can't save entry.\n");
-               return(1);
+               warnx("can't save entry");
+               return (1);
        }
        
        }
        
-       return(0);
+       return (0);
 }
 
 /* ARGSUSED */
 }
 
 /* ARGSUSED */
+int
 p_change(p, pw, ep)
        char *p;
        struct passwd *pw;
 p_change(p, pw, ep)
        char *p;
        struct passwd *pw;
-       struct entry *ep;
+       ENTRY *ep;
 {
        if (!atot(p, &pw->pw_change))
 {
        if (!atot(p, &pw->pw_change))
-               return(0);
-       (void)fprintf(stderr, "chpass: illegal date for change field.\n");
-       return(1);
+               return (0);
+       warnx("illegal date for change field");
+       return (1);
 }
 
 /* ARGSUSED */
 }
 
 /* ARGSUSED */
+int
 p_expire(p, pw, ep)
        char *p;
        struct passwd *pw;
 p_expire(p, pw, ep)
        char *p;
        struct passwd *pw;
-       struct entry *ep;
+       ENTRY *ep;
 {
        if (!atot(p, &pw->pw_expire))
 {
        if (!atot(p, &pw->pw_expire))
-               return(0);
-       (void)fprintf(stderr, "chpass: illegal date for expire field.\n");
-       return(1);
+               return (0);
+       warnx("illegal date for expire field");
+       return (1);
 }
 
 /* ARGSUSED */
 }
 
 /* ARGSUSED */
+int
 p_gecos(p, pw, ep)
        char *p;
        struct passwd *pw;
 p_gecos(p, pw, ep)
        char *p;
        struct passwd *pw;
-       struct entry *ep;
+       ENTRY *ep;
 {
        if (!*p)
                ep->save = "";
        else if (!(ep->save = strdup(p))) {
 {
        if (!*p)
                ep->save = "";
        else if (!(ep->save = strdup(p))) {
-               (void)fprintf(stderr, "chpass: can't save entry.\n");
-               return(1);
+               warnx("can't save entry");
+               return (1);
        }
        }
-       return(0);
+       return (0);
 }
 
 /* ARGSUSED */
 }
 
 /* ARGSUSED */
+int
 p_hdir(p, pw, ep)
        char *p;
        struct passwd *pw;
 p_hdir(p, pw, ep)
        char *p;
        struct passwd *pw;
-       struct entry *ep;
+       ENTRY *ep;
 {
        if (!*p) {
 {
        if (!*p) {
-               (void)fprintf(stderr, "chpass: empty home directory field.\n");
-               return(1);
+               warnx("empty home directory field");
+               return (1);
        }
        if (!(pw->pw_dir = strdup(p))) {
        }
        if (!(pw->pw_dir = strdup(p))) {
-               (void)fprintf(stderr, "chpass: can't save entry.\n");
-               return(1);
+               warnx("can't save entry");
+               return (1);
        }
        }
-       return(0);
+       return (0);
 }
 
 /* ARGSUSED */
 }
 
 /* ARGSUSED */
+int
 p_shell(p, pw, ep)
 p_shell(p, pw, ep)
-       register char *p;
+       char *p;
        struct passwd *pw;
        struct passwd *pw;
-       struct entry *ep;
+       ENTRY *ep;
 {
        char *t, *ok_shell();
 
        if (!*p) {
                pw->pw_shell = _PATH_BSHELL;
 {
        char *t, *ok_shell();
 
        if (!*p) {
                pw->pw_shell = _PATH_BSHELL;
-               return(0);
+               return (0);
        }
        /* only admin can change from or to "restricted" shells */
        if (uid && pw->pw_shell && !ok_shell(pw->pw_shell)) {
        }
        /* only admin can change from or to "restricted" shells */
        if (uid && pw->pw_shell && !ok_shell(pw->pw_shell)) {
-               (void)fprintf(stderr,
-                   "chpass: %s: current shell non-standard.\n", pw->pw_shell);
-               return(1);
+               warnx("%s: current shell non-standard", pw->pw_shell);
+               return (1);
        }
        if (!(t = ok_shell(p))) {
                if (uid) {
        }
        if (!(t = ok_shell(p))) {
                if (uid) {
-                       (void)fprintf(stderr,
-                           "chpass: %s: non-standard shell.\n", p);
-                       return(1);
+                       warnx("%s: non-standard shell", p);
+                       return (1);
                }
        }
        else
                p = t;
        if (!(pw->pw_shell = strdup(p))) {
                }
        }
        else
                p = t;
        if (!(pw->pw_shell = strdup(p))) {
-               (void)fprintf(stderr, "chpass: can't save entry.\n");
-               return(1);
+               warnx("can't save entry");
+               return (1);
        }
        }
-       return(0);
+       return (0);
 }
 }