]> git.cameronkatri.com Git - pw-darwin.git/blobdiff - chpass/edit.c
Forbid adding duplicate users with the name of "0".
[pw-darwin.git] / chpass / edit.c
index 2968c84fa3d4d0baf2d588d5af468d1bad3ce677..849d5d9acff7873979420b8529424224515479c2 100644 (file)
  * 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.
+ *
+ * $FreeBSD$
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)edit.c     8.3 (Berkeley) 4/2/94";
+static const char sccsid[] = "@(#)edit.c       8.3 (Berkeley) 4/2/94";
 #endif /* not lint */
 
 #include <sys/param.h>
@@ -41,6 +43,7 @@ static char sccsid[] = "@(#)edit.c    8.3 (Berkeley) 4/2/94";
 #include <ctype.h>
 #include <err.h>
 #include <errno.h>
+#include <md5.h>
 #include <paths.h>
 #include <pwd.h>
 #include <stdio.h>
@@ -63,17 +66,23 @@ edit(pw)
        struct passwd *pw;
 {
        struct stat begin, end;
+       char *begin_sum, *end_sum;
 
        for (;;) {
                if (stat(tempname, &begin))
                        pw_error(tempname, 1, 1);
+               begin_sum = MD5File(tempname, (char *)NULL);
                pw_edit(1);
                if (stat(tempname, &end))
                        pw_error(tempname, 1, 1);
-               if (begin.st_mtime == end.st_mtime) {
+               end_sum = MD5File(tempname, (char *)NULL);
+               if ((begin.st_mtime == end.st_mtime) &&
+                   (strcmp(begin_sum, end_sum) == 0)) {
                        warnx("no changes made");
                        pw_error(NULL, 0, 0);
                }
+               free(begin_sum);
+               free(end_sum);
                if (verify(pw))
                        break;
                pw_prompt();
@@ -101,13 +110,14 @@ display(fd, pw)
            "#Changing %s information for %s.\n", _use_yp ? "NIS" : "user database", pw->pw_name);
        if (!uid && (!_use_yp || suser_override)) {
 #else
-           "#Changing user database information for %s.\n", pw->pw_name)
+           "#Changing user database information for %s.\n", pw->pw_name);
        if (!uid) {
 #endif /* YP */
                (void)fprintf(fp, "Login: %s\n", pw->pw_name);
                (void)fprintf(fp, "Password: %s\n", pw->pw_passwd);
-               (void)fprintf(fp, "Uid [#]: %d\n", pw->pw_uid);
-               (void)fprintf(fp, "Gid [# or name]: %d\n", pw->pw_gid);
+               (void)fprintf(fp, "Uid [#]: %lu\n", (unsigned long)pw->pw_uid);
+               (void)fprintf(fp, "Gid [# or name]: %lu\n",
+                   (unsigned long)pw->pw_gid);
                (void)fprintf(fp, "Change [month day year]: %s\n",
                    ttoa(pw->pw_change));
                (void)fprintf(fp, "Expire [month day year]: %s\n",
@@ -118,7 +128,7 @@ display(fd, pw)
                    *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL);
        }
        /* Only admin can change "restricted" shells. */
-#ifdef 0
+#if 0
        else if (ok_shell(pw->pw_shell))
                /*
                 * Make shell a restricted field.  Ugly with a
@@ -138,28 +148,33 @@ display(fd, pw)
        bp = pw->pw_gecos;
 
        p = strsep(&bp, ",");
-       if (p)
-         list[E_NAME].save = strdup(p);
+       p = strdup(p ? p : "");
+       list[E_NAME].save = p;
        if (!list[E_NAME].restricted || !uid)
-         (void)fprintf(fp, "Full Name: %s\n", p ? p : "");
+         (void)fprintf(fp, "Full Name: %s\n", p);
 
         p = strsep(&bp, ",");
-       if (p)
-         list[E_LOCATE].save = strdup(p);
+       p = strdup(p ? p : "");
+       list[E_LOCATE].save = p;
        if (!list[E_LOCATE].restricted || !uid)
-         (void)fprintf(fp, "Location: %s\n", p ? p : "");
+         (void)fprintf(fp, "Office Location: %s\n", p);
 
         p = strsep(&bp, ",");
-       if (p)
-         list[E_BPHONE].save = strdup(p);
+       p = strdup(p ? p : "");
+       list[E_BPHONE].save = p;
        if (!list[E_BPHONE].restricted || !uid)
-         (void)fprintf(fp, "Office Phone: %s\n", p ? p : "");
+         (void)fprintf(fp, "Office Phone: %s\n", p);
 
         p = strsep(&bp, ",");
-       if (p)
-         list[E_HPHONE].save = strdup(p);
+       p = strdup(p ? p : "");
+       list[E_HPHONE].save = p;
        if (!list[E_HPHONE].restricted || !uid)
-         (void)fprintf(fp, "Home Phone: %s\n", p ? p : "");
+         (void)fprintf(fp, "Home Phone: %s\n", p);
+
+       bp = strdup(bp ? bp : "");
+       list[E_OTHER].save = bp;
+       if (!list[E_OTHER].restricted || !uid)
+         (void)fprintf(fp, "Other information: %s\n", bp);
 
        (void)fchown(fd, getuid(), getgid());
        (void)fclose(fp);
@@ -173,8 +188,8 @@ verify(pw)
        char *p;
        struct stat sb;
        FILE *fp;
-       int len;
-       char buf[LINE_MAX];
+       int len, line;
+       static char buf[LINE_MAX];
 
        if (!(fp = fopen(tempname, "r")))
                pw_error(tempname, 1, 1);
@@ -184,17 +199,19 @@ verify(pw)
                warnx("corrupted temporary file");
                goto bad;
        }
+       line = 0;
        while (fgets(buf, sizeof(buf), fp)) {
+               line++;
                if (!buf[0] || buf[0] == '#')
                        continue;
                if (!(p = strchr(buf, '\n'))) {
-                       warnx("line too long");
+                       warnx("line %d too long", line);
                        goto bad;
                }
                *p = '\0';
                for (ep = list;; ++ep) {
                        if (!ep->prompt) {
-                               warnx("unrecognized field");
+                               warnx("unrecognized field on line %d", line);
                                goto bad;
                        }
                        if (!strncasecmp(buf, ep->prompt, ep->len)) {
@@ -205,7 +222,7 @@ verify(pw)
                                        goto bad;
                                }
                                if (!(p = strchr(buf, ':'))) {
-                                       warnx("line corrupted");
+                                       warnx("line %d corrupted", line);
                                        goto bad;
                                }
                                while (isspace(*++p));
@@ -227,19 +244,27 @@ bad:                                      (void)fclose(fp);
 
        /* Build the gecos field. */
        len = strlen(list[E_NAME].save) + strlen(list[E_BPHONE].save) +
-           strlen(list[E_HPHONE].save) + strlen(list[E_LOCATE].save) + 4;
+           strlen(list[E_HPHONE].save) + strlen(list[E_LOCATE].save) +
+           strlen(list[E_OTHER].save) + 5;
        if (!(p = malloc(len)))
                err(1, NULL);
-       (void)sprintf(pw->pw_gecos = p, "%s,%s,%s,%s", list[E_NAME].save,
-           list[E_LOCATE].save, list[E_BPHONE].save, list[E_HPHONE].save);
+       (void)sprintf(pw->pw_gecos = p, "%s,%s,%s,%s,%s", list[E_NAME].save,
+           list[E_LOCATE].save, list[E_BPHONE].save, list[E_HPHONE].save,
+           list[E_OTHER].save);
+
+       while ((len = strlen(pw->pw_gecos)) && pw->pw_gecos[len - 1] == ',')
+               pw->pw_gecos[len - 1] = '\0';
 
        if (snprintf(buf, sizeof(buf),
-           "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s",
-           pw->pw_name, pw->pw_passwd, pw->pw_uid, pw->pw_gid, pw->pw_class,
-           pw->pw_change, pw->pw_expire, pw->pw_gecos, pw->pw_dir,
+           "%s:%s:%lu:%lu:%s:%ld:%ld:%s:%s:%s",
+           pw->pw_name, pw->pw_passwd, (unsigned long)pw->pw_uid, 
+           (unsigned long)pw->pw_gid, pw->pw_class, (long)pw->pw_change,
+           (long)pw->pw_expire, pw->pw_gecos, pw->pw_dir,
            pw->pw_shell) >= sizeof(buf)) {
                warnx("entries too long");
+               free(p);
                return (0);
        }
-       return (pw_scan(buf, pw));
+       free(p);
+       return (__pw_scan(buf, pw, _PWSCAN_WARN|_PWSCAN_MASTER));
 }