Ahem: add new faq entry to list at top of FAQ.html
[oweals/busybox.git] / loginutils / passwd.c
index 079791c4cf1b5519e23d6593b4c17a6a81e77716..c8940eed7dfa0d0e462f7b0ff934096fe98905a8 100644 (file)
@@ -21,24 +21,23 @@ static int new_password(const struct passwd *pw, int amroot, int algo);
 static void set_filesize_limit(int blocks);
 
 
-int get_algo(char *a)
+static int get_algo(char *a)
 {
-       int x = 0;                                      /* standart: DES */
+       int x = 1;                                      /* standard: MD5 */
 
-       if (strcasecmp(a, "md5") == 0)
-               x = 1;
+       if (strcasecmp(a, "des") == 0)
+               x = 0;
        return x;
 }
 
 
-extern int update_passwd(const struct passwd *pw, char *crypt_pw)
+static int update_passwd(const struct passwd *pw, char *crypt_pw)
 {
        char filename[1024];
        char buf[1025];
        char buffer[80];
        char username[32];
        char *pw_rest;
-       int has_shadow = 0;
        int mask;
        int continued;
        FILE *fp;
@@ -46,13 +45,13 @@ extern int update_passwd(const struct passwd *pw, char *crypt_pw)
        struct stat sb;
        struct flock lock;
 
-       if (access(shadow_file, F_OK) == 0) {
-               has_shadow = 1;
-       }
-       if (has_shadow) {
-               snprintf(filename, sizeof filename, "%s", shadow_file);
-       } else {
-               snprintf(filename, sizeof filename, "%s", passwd_file);
+#ifdef CONFIG_FEATURE_SHADOWPASSWDS
+       if (access(bb_path_shadow_file, F_OK) == 0) {
+               snprintf(filename, sizeof filename, "%s", bb_path_shadow_file);
+       } else
+#endif
+       {
+               snprintf(filename, sizeof filename, "%s", bb_path_passwd_file);
        }
 
        if (((fp = fopen(filename, "r+")) == 0) || (fstat(fileno(fp), &sb))) {
@@ -140,12 +139,11 @@ extern int passwd_main(int argc, char **argv)
        char *name;
        char *myname;
        int flag;
-       int algo = 0;                           /* -a - password algorithm */
+       int algo = 1;                           /* -a - password algorithm */
        int lflg = 0;                           /* -l - lock account */
        int uflg = 0;                           /* -u - unlock account */
        int dflg = 0;                           /* -d - delete password */
        const struct passwd *pw;
-       unsigned short ruid;
 
 #ifdef CONFIG_FEATURE_SHADOWPASSWDS
        const struct spwd *sp;
@@ -167,30 +165,26 @@ extern int passwd_main(int argc, char **argv)
                        uflg++;
                        break;
                default:
-                       show_usage();
+                       bb_show_usage();
                }
        }
-       ruid = getuid();
-       pw = (struct passwd *) getpwuid(ruid);
-       if (!pw) {
-               error_msg_and_die("Cannot determine your user name.\n");
-       }
-       myname = (char *) xstrdup(pw->pw_name);
+       myname = (char *) bb_xstrdup(my_getpwuid(NULL, getuid(), -1));
+       /* exits on error */
        if (optind < argc) {
                name = argv[optind];
        } else {
                name = myname;
        }
        if ((lflg || uflg || dflg) && (optind >= argc || !amroot)) {
-               show_usage();
+               bb_show_usage();
        }
        pw = getpwnam(name);
        if (!pw) {
-               error_msg_and_die("Unknown user %s\n", name);
+               bb_error_msg_and_die("Unknown user %s\n", name);
        }
        if (!amroot && pw->pw_uid != getuid()) {
                syslog(LOG_WARNING, "can't change pwd for `%s'", name);
-               error_msg_and_die("Permission denied.\n");
+               bb_error_msg_and_die("Permission denied.\n");
        }
 #ifdef CONFIG_FEATURE_SHADOWPASSWDS
        sp = getspnam(name);
@@ -209,12 +203,12 @@ extern int passwd_main(int argc, char **argv)
                if (!amroot) {
                        if (cp[0] == '!') {
                                syslog(LOG_WARNING, "password locked for `%s'", np);
-                               error_msg_and_die( "The password for `%s' cannot be changed.\n", np);
+                               bb_error_msg_and_die( "The password for `%s' cannot be changed.\n", np);
                        }
                }
                printf("Changing password for %s\n", name);
                if (new_password(pw, amroot, algo)) {
-                       error_msg_and_die( "The password for %s is unchanged.\n", name);
+                       bb_error_msg_and_die( "The password for %s is unchanged.\n", name);
                }
        } else if (lflg) {
                if (crypt_passwd[0] != '!') {
@@ -238,7 +232,7 @@ extern int passwd_main(int argc, char **argv)
        umask(077);
        if (setuid(0)) {
                syslog(LOG_ERR, "can't setuid(0)");
-               error_msg_and_die( "Cannot change ID to root.\n");
+               bb_error_msg_and_die( "Cannot change ID to root.\n");
        }
        if (!update_passwd(pw, crypt_passwd)) {
                syslog(LOG_INFO, "password for `%s' changed by user `%s'", name,
@@ -246,7 +240,7 @@ extern int passwd_main(int argc, char **argv)
                printf("Password changed.\n");
        } else {
                syslog(LOG_WARNING, "an error occurred updating the password file");
-               error_msg_and_die("An error occurred updating the password file.\n");
+               bb_error_msg_and_die("An error occurred updating the password file.\n");
        }
        return (0);
 }
@@ -332,7 +326,7 @@ static int new_password(const struct passwd *pw, int amroot, int algo)
        time_t start, now;
 
        if (!amroot && crypt_passwd[0]) {
-               if (!(clear = getpass("Old password:"))) {
+               if (!(clear = bb_askpass(0, "Old password:"))) {
                        /* return -1; */
                        return 1;
                }
@@ -356,7 +350,7 @@ static int new_password(const struct passwd *pw, int amroot, int algo)
        } else {
                orig[0] = '\0';
        }
-       if (! (cp=getpass("Enter the new password (minimum of 5, maximum of 8 characters)\n"
+       if (! (cp=bb_askpass(0, "Enter the new password (minimum of 5, maximum of 8 characters)\n"
                                          "Please use a combination of upper and lower case letters and numbers.\n"
                                          "Enter new password: ")))
        {
@@ -375,7 +369,7 @@ static int new_password(const struct passwd *pw, int amroot, int algo)
                        return 1;
                }
        }
-       if (!(cp = getpass("Re-enter new password: "))) {
+       if (!(cp = bb_askpass(0, "Re-enter new password: "))) {
                bzero(orig, sizeof orig);
                /* return -1; */
                return 1;