chkey, chpass, enigma, lock, newgrp, pw - Handle NULL return from crypt(3)
authorSamuel J. Greear <sjg@thesjg.com>
Mon, 20 Feb 2012 17:34:19 +0000 (10:34 -0700)
committerSamuel J. Greear <sjg@thesjg.com>
Mon, 20 Feb 2012 17:34:19 +0000 (10:34 -0700)
usr.bin/chkey/chkey.c
usr.bin/chpass/chpass.c
usr.bin/enigma/enigma.c
usr.bin/lock/lock.c
usr.bin/newgrp/newgrp.c
usr.sbin/pw/pw_user.c

index 6cb853d..f31c719 100644 (file)
@@ -89,6 +89,9 @@ main(int argc, char **argv)
 #ifdef YP
        char *master;
 #endif
+#ifdef YPPASSWD
+       char *cryptpw;
+#endif
 
        while ((ch = getopt(argc, argv, "f")) != -1)
                switch(ch) {
@@ -144,7 +147,8 @@ main(int argc, char **argv)
        pass = getpass("Password:");
 #ifdef YPPASSWD
        if (!force) {
-               if (strcmp(crypt(pass, pw->pw_passwd), pw->pw_passwd) != 0)
+               cryptpw = crypt(pass, pw->pw_passwd);
+               if (cryptpw == NULL || strcmp(cryptpw, pw->pw_passwd) != 0)
                        errx(1, "invalid password");
        }
 #else
index 2a86edd..82b6943 100644 (file)
@@ -73,6 +73,7 @@ main(int argc, char **argv)
        struct passwd lpw, *old_pw, *pw;
        int ch, pfd, tfd;
        const char *password;
+       char *cryptpw;
        char *arg = NULL;
        uid_t uid;
 #ifdef YP
@@ -219,8 +220,8 @@ main(int argc, char **argv)
 
        if (old_pw && !master_mode) {
                password = getpass("Password: ");
-               if (strcmp(crypt(password, old_pw->pw_passwd),
-                   old_pw->pw_passwd) != 0)
+               cryptpw = crypt(password, old_pw->pw_passwd);
+               if (cryptpw == NULL || strcmp(cryptpw, old_pw->pw_passwd) != 0)
                        baduser();
        } else {
                password = "";
index dbad535..c3ea266 100644 (file)
@@ -39,9 +39,15 @@ setup(const char *pw)
        char salt[3];
        unsigned rnd;
        long seed;
+       char *cryptpw;
 
        strncpy(salt, pw, sizeof(salt));
-       memcpy(buf, crypt(pw, salt), sizeof(buf));
+       cryptpw = crypt(pw, salt);
+       if (cryptpw == NULL) {
+               fprintf(stderr, "crypt(3) failure\n");
+               exit(1);
+       }
+       memcpy(buf, cryptpw, sizeof(buf));
        seed = 123;
        for (i=0; i<13; i++)
                seed = seed*buf[i] + i;
index 2b72015..50cc371 100644 (file)
@@ -86,7 +86,7 @@ main(int argc, char **argv)
        struct tm *timp;
        int ch, failures, sectimeout, usemine, vtylock;
        long tmp;
-       char *ap, *ep, *mypw, *ttynam, *tzn;
+       char *ap, *ep, *mypw, *cryptpw, *ttynam, *tzn;
        char hostname[MAXHOSTNAMELEN + 1], s[BUFSIZ], s1[BUFSIZ];
 
        openlog("lock", LOG_ODELAY, LOG_AUTH);
@@ -211,7 +211,8 @@ main(int argc, char **argv)
                }
                if (usemine) {
                        s[strlen(s) - 1] = '\0';
-                       if (!strcmp(mypw, crypt(s, mypw)))
+                       cryptpw = crypt(s, mypw);
+                       if (cryptpw == NULL || !strcmp(mypw, cryptpw))
                                break;
                }
                else if (!strcmp(s, s1))
index 4513b17..64074cf 100644 (file)
@@ -151,7 +151,7 @@ addgroup(const char *grpname)
        int dbmember, i, ngrps;
        gid_t egid;
        struct group *grp;
-       char *ep, *pass;
+       char *ep, *pass, *cryptpw;
        char **p;
 
        egid = getegid();
@@ -178,8 +178,10 @@ addgroup(const char *grpname)
                }
        if (!dbmember && *grp->gr_passwd != '\0' && getuid() != 0) {
                pass = getpass("Password:");
-               if (pass == NULL ||
-                   strcmp(grp->gr_passwd, crypt(pass, grp->gr_passwd)) != 0) {
+               if (pass == NULL)
+                       return;
+               cryptpw = crypt(pass, grp->gr_passwd);
+               if (cryptpw == NULL || strcmp(grp->gr_passwd, cryptpw) != 0) {
                        fprintf(stderr, "Sorry\n");
                        return;
                }
index b8491ac..6755e99 100644 (file)
@@ -1003,6 +1003,7 @@ pw_pwcrypt(char *password)
 {
        int             i;
        char            salt[12];
+       char            *cryptpw;
 
        static char     buf[256];
 
@@ -1013,7 +1014,10 @@ pw_pwcrypt(char *password)
                salt[i] = chars[arc4random() % 63];
        salt[i] = '\0';
 
-       return strcpy(buf, crypt(password, salt));
+       cryptpw = crypt(password, salt);
+       if (cryptpw == NULL)
+               errx(EX_CONFIG, "crypt(3) failure");
+       return strcpy(buf, cryptpw);
 }
 
 #if defined(USE_MD5RAND)