1 /* vi: set sw=4 ts=4: */
12 #include <sys/resource.h>
17 static char crypt_passwd[128];
19 static int create_backup(const char *backup, FILE * fp);
20 static int new_password(const struct passwd *pw, int amroot, int algo);
21 static void set_filesize_limit(int blocks);
26 int x = 0; /* standart: DES */
28 if (strcasecmp(a, "md5") == 0)
34 extern int update_passwd(const struct passwd *pw, char *crypt_pw)
49 if (access(shadow_file, F_OK) == 0) {
53 snprintf(filename, sizeof filename, "%s", shadow_file);
55 snprintf(filename, sizeof filename, "%s", passwd_file);
58 if (((fp = fopen(filename, "r+")) == 0) || (fstat(fileno(fp), &sb))) {
63 /* Lock the password file before updating */
64 lock.l_type = F_WRLCK;
65 lock.l_whence = SEEK_SET;
68 if (fcntl(fileno(fp), F_SETLK, &lock) < 0) {
69 fprintf(stderr, "%s: %s\n", filename, strerror(errno));
72 lock.l_type = F_UNLCK;
74 snprintf(buf, sizeof buf, "%s-", filename);
75 if (create_backup(buf, fp)) {
76 fcntl(fileno(fp), F_SETLK, &lock);
80 snprintf(buf, sizeof buf, "%s+", filename);
82 out_fp = fopen(buf, "w");
84 if ((!out_fp) || (fchmod(fileno(out_fp), sb.st_mode & 0777))
85 || (fchown(fileno(out_fp), sb.st_uid, sb.st_gid))) {
86 fcntl(fileno(fp), F_SETLK, &lock);
93 snprintf(username, sizeof username, "%s:", pw->pw_name);
96 fgets(buffer, sizeof buffer, fp);
97 if (!continued) { // Check to see if we're updating this line.
98 if (strncmp(username, buffer, strlen(username)) == 0) { // we have a match.
99 pw_rest = strchr(buffer, ':');
101 pw_rest = strchr(pw_rest, ':');
102 fprintf(out_fp, "%s:%s%s", buffer, crypt_pw, pw_rest);
104 fputs(buffer, out_fp);
107 fputs(buffer, out_fp);
109 if (buffer[strlen(buffer) - 1] == '\n') {
114 bzero(buffer, sizeof buffer);
117 if (fflush(out_fp) || fsync(fileno(out_fp)) || fclose(out_fp)) {
119 fcntl(fileno(fp), F_SETLK, &lock);
123 if (rename(buf, filename) < 0) {
124 fcntl(fileno(fp), F_SETLK, &lock);
128 fcntl(fileno(fp), F_SETLK, &lock);
135 extern int passwd_main(int argc, char **argv)
143 int algo = 0; /* -a - password algorithm */
144 int lflg = 0; /* -l - lock account */
145 int uflg = 0; /* -u - unlock account */
146 int dflg = 0; /* -d - delete password */
147 const struct passwd *pw;
150 #ifdef CONFIG_FEATURE_SHADOWPASSWDS
151 const struct spwd *sp;
152 #endif /* CONFIG_FEATURE_SHADOWPASSWDS */
153 amroot = (getuid() == 0);
154 openlog("passwd", LOG_PID | LOG_CONS | LOG_NOWAIT, LOG_AUTH);
155 while ((flag = getopt(argc, argv, "a:dlu")) != EOF) {
158 algo = get_algo(optarg);
174 pw = (struct passwd *) getpwuid(ruid);
176 error_msg_and_die("Cannot determine your user name.");
178 myname = (char *) xstrdup(pw->pw_name);
184 if ((lflg || uflg || dflg) && (optind >= argc || !amroot)) {
189 error_msg_and_die("Unknown user %s\n", name);
191 if (!amroot && pw->pw_uid != getuid()) {
192 syslog(LOG_WARNING, "can't change pwd for `%s'", name);
193 error_msg_and_die("Permission denied.\n");
195 #ifdef CONFIG_FEATURE_SHADOWPASSWDS
198 sp = (struct spwd *) pwd_to_spwd(pw);
205 #endif /* CONFIG_FEATURE_SHADOWPASSWDS */
207 safe_strncpy(crypt_passwd, cp, sizeof(crypt_passwd));
208 if (!(dflg || lflg || uflg)) {
211 syslog(LOG_WARNING, "password locked for `%s'", np);
212 error_msg_and_die( "The password for `%s' cannot be changed.\n", np);
215 printf("Changing password for %s\n", name);
216 if (new_password(pw, amroot, algo)) {
217 error_msg_and_die( "The password for %s is unchanged.\n", name);
220 if (crypt_passwd[0] != '!') {
221 memmove(&crypt_passwd[1], crypt_passwd,
222 sizeof crypt_passwd - 1);
223 crypt_passwd[sizeof crypt_passwd - 1] = '\0';
224 crypt_passwd[0] = '!';
227 if (crypt_passwd[0] == '!') {
228 memmove(crypt_passwd, &crypt_passwd[1],
229 sizeof crypt_passwd - 1);
232 crypt_passwd[0] = '\0';
234 set_filesize_limit(30000);
235 signal(SIGHUP, SIG_IGN);
236 signal(SIGINT, SIG_IGN);
237 signal(SIGQUIT, SIG_IGN);
240 syslog(LOG_ERR, "can't setuid(0)");
241 error_msg_and_die( "Cannot change ID to root.\n");
243 if (!update_passwd(pw, crypt_passwd)) {
244 syslog(LOG_INFO, "password for `%s' changed by user `%s'", name,
246 printf("Password changed.\n");
248 syslog(LOG_WARNING, "an error occurred updating the password file");
249 error_msg_and_die("An error occurred updating the password file.\n");
256 static int create_backup(const char *backup, FILE * fp)
263 if (fstat(fileno(fp), &sb))
268 bkfp = fopen(backup, "w");
274 /* TODO: faster copy, not one-char-at-a-time. --marekm */
276 while ((c = getc(fp)) != EOF) {
277 if (putc(c, bkfp) == EOF)
280 if (c != EOF || fflush(bkfp)) {
289 ub.actime = sb.st_atime;
290 ub.modtime = sb.st_mtime;
295 static int i64c(int i)
301 if (i >= 2 && i < 12)
302 return ('0' - 2 + i);
303 if (i >= 12 && i < 38)
304 return ('A' - 12 + i);
305 if (i >= 38 && i < 63)
306 return ('a' - 38 + i);
310 static char *crypt_make_salt(void)
313 static unsigned long x;
314 static char result[3];
317 x += now + getpid() + clock();
318 result[0] = i64c(((x >> 18) ^ (x >> 6)) & 077);
319 result[1] = i64c(((x >> 12) ^ x) & 077);
325 static int new_password(const struct passwd *pw, int amroot, int algo)
334 if (!amroot && crypt_passwd[0]) {
335 if (!(clear = getpass("Old password:"))) {
339 cipher = pw_encrypt(clear, crypt_passwd);
340 if (strcmp(cipher, crypt_passwd) != 0) {
341 syslog(LOG_WARNING, "incorrect password for `%s'",
345 while (difftime(now, start) < FAIL_DELAY) {
349 fprintf(stderr, "Incorrect password.\n");
353 safe_strncpy(orig, clear, sizeof(orig));
354 bzero(clear, strlen(clear));
355 bzero(cipher, strlen(cipher));
359 if (! (cp=getpass("Enter the new password (minimum of 5, maximum of 8 characters)\n"
360 "Please use a combination of upper and lower case letters and numbers.\n"
361 "Enter new password: ")))
363 bzero(orig, sizeof orig);
367 safe_strncpy(pass, cp, sizeof(pass));
368 bzero(cp, strlen(cp));
369 /* if (!obscure(orig, pass, pw)) { */
370 if (obscure(orig, pass, pw)) {
372 printf("\nWarning: weak password (continuing).\n");
378 if (!(cp = getpass("Re-enter new password: "))) {
379 bzero(orig, sizeof orig);
383 if (strcmp(cp, pass)) {
384 fprintf(stderr, "Passwords do not match.\n");
388 bzero(cp, strlen(cp));
389 bzero(orig, sizeof(orig));
392 cp = pw_encrypt(pass, "$1$");
394 cp = pw_encrypt(pass, crypt_make_salt());
395 bzero(pass, sizeof pass);
396 safe_strncpy(crypt_passwd, cp, sizeof(crypt_passwd));
400 static void set_filesize_limit(int blocks)
402 struct rlimit rlimit_fsize;
404 rlimit_fsize.rlim_cur = rlimit_fsize.rlim_max = 512L * blocks;
405 setrlimit(RLIMIT_FSIZE, &rlimit_fsize);