xfunc: fix: && -> &. Also nuked two double semicolons...
[oweals/busybox.git] / loginutils / passwd.c
index a1ad02bf084bf9e29ecfa1dbf57486e3e193d43c..aa75dd260fa73cc6db133391592de84051216f7e 100644 (file)
@@ -1,18 +1,10 @@
 /* vi: set sw=4 ts=4: */
-#include <fcntl.h>
-#include <stdio.h>
-#include <string.h>
-#include <signal.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <unistd.h>
-#include <utime.h>
-#include <syslog.h>
-#include <time.h>
-#include <sys/resource.h>
-#include <errno.h>
+/*
+ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
+ */
 
 #include "busybox.h"
+#include <syslog.h>
 
 static char crypt_passwd[128];
 
@@ -146,9 +138,6 @@ int passwd_main(int argc, char **argv)
        int dflg = 0;                           /* -d - delete password */
        const struct passwd *pw;
 
-#if ENABLE_FEATURE_SHADOWPASSWDS
-       const struct spwd *sp;
-#endif
        amroot = (getuid() == 0);
        openlog("passwd", LOG_PID | LOG_CONS | LOG_NOWAIT, LOG_AUTH);
        while ((flag = getopt(argc, argv, "a:dlu")) != EOF) {
@@ -169,7 +158,7 @@ int passwd_main(int argc, char **argv)
                        bb_show_usage();
                }
        }
-       myname = (char *) bb_xstrdup(bb_getpwuid(NULL, getuid(), -1));
+       myname = (char *) xstrdup(bb_getpwuid(NULL, getuid(), -1));
        /* exits on error */
        if (optind < argc) {
                name = argv[optind];
@@ -187,18 +176,13 @@ int passwd_main(int argc, char **argv)
                syslog(LOG_WARNING, "can't change pwd for `%s'", name);
                bb_error_msg_and_die("Permission denied.\n");
        }
-#if ENABLE_FEATURE_SHADOWPASSWDS
-       sp = getspnam(name);
-       if (!sp) {
-               sp = (struct spwd *) pwd_to_spwd(pw);
-       }
-       cp = sp->sp_pwdp;
-       np = sp->sp_namp;
-#else
-       cp = pw->pw_passwd;
-       np = name;
-#endif
+       if (ENABLE_FEATURE_SHADOWPASSWDS) {
+               struct spwd *sp = getspnam(name);
+               if (!sp) bb_error_msg_and_die("Unknown user %s", name);
+               cp = sp->sp_pwdp;
+       } else cp = pw->pw_passwd;
 
+       np = name;
        safe_strncpy(crypt_passwd, cp, sizeof(crypt_passwd));
        if (!(dflg || lflg || uflg)) {
                if (!amroot) {
@@ -231,10 +215,7 @@ int passwd_main(int argc, char **argv)
        signal(SIGINT, SIG_IGN);
        signal(SIGQUIT, SIG_IGN);
        umask(077);
-       if (setuid(0)) {
-               syslog(LOG_ERR, "can't setuid(0)");
-               bb_error_msg_and_die( "Cannot change ID to root.\n");
-       }
+       xsetuid(0);
        if (!update_passwd(pw, crypt_passwd)) {
                syslog(LOG_INFO, "password for `%s' changed by user `%s'", name,
                           myname);
@@ -243,6 +224,7 @@ int passwd_main(int argc, char **argv)
                syslog(LOG_WARNING, "an error occurred updating the password file");
                bb_error_msg_and_die("An error occurred updating the password file.\n");
        }
+       if (ENABLE_FEATURE_CLEAN_UP) free(myname);
        return (0);
 }