/* 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];
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) {
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];
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) {
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);
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);
}
char *clear;
char *cipher;
char *cp;
+ char salt[12]; /* "$N$XXXXXXXX" or "XX" */
char orig[200];
char pass[200];
}
memset(cp, 0, strlen(cp));
memset(orig, 0, sizeof(orig));
+ memset(salt, 0, sizeof(salt));
if (algo == 1) {
- cp = pw_encrypt(pass, "$1$");
- } else
- cp = pw_encrypt(pass, crypt_make_salt());
+ strcpy(salt, "$1$");
+ strcat(salt, crypt_make_salt());
+ strcat(salt, crypt_make_salt());
+ strcat(salt, crypt_make_salt());
+ }
+
+ strcat(salt, crypt_make_salt());
+ cp = pw_encrypt(pass, salt);
+
memset(pass, 0, sizeof pass);
safe_strncpy(crypt_passwd, cp, sizeof(crypt_passwd));
return 0;