diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-07-21 13:25:28 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-07-21 13:25:28 +0000 |
commit | 557fb713e0f943ac9b87c9f3804ba24e73d55bb0 (patch) | |
tree | eda14f3179f0bddf125c2b8e8549cd6e35fa7242 | |
parent | 1ec5eaecba0a0323f214825b83fabcc18a41884d (diff) | |
download | busybox-w32-557fb713e0f943ac9b87c9f3804ba24e73d55bb0.tar.gz busybox-w32-557fb713e0f943ac9b87c9f3804ba24e73d55bb0.tar.bz2 busybox-w32-557fb713e0f943ac9b87c9f3804ba24e73d55bb0.zip |
chpasswd: fixes and code shrink
update_passwd 732 734 +2
chpasswd_main 318 292 -26
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/1 up/down: 2/-26) Total: -24 bytes
text data bss dec hex filename
781564 1168 11900 794632 c2008 busybox_old
781548 1168 11900 794616 c1ff8 busybox_unstripped
-rw-r--r-- | libbb/update_passwd.c | 40 | ||||
-rw-r--r-- | loginutils/chpasswd.c | 29 | ||||
-rw-r--r-- | runit/runit_lib.c | 2 | ||||
-rw-r--r-- | runit/runit_lib.h | 2 | ||||
-rwxr-xr-x | scripts/trylink | 2 |
5 files changed, 36 insertions, 39 deletions
diff --git a/libbb/update_passwd.c b/libbb/update_passwd.c index 5572db547..8914b8b45 100644 --- a/libbb/update_passwd.c +++ b/libbb/update_passwd.c | |||
@@ -18,18 +18,18 @@ int update_passwd(const char *filename, const char *username, | |||
18 | struct flock lock; | 18 | struct flock lock; |
19 | FILE *old_fp; | 19 | FILE *old_fp; |
20 | FILE *new_fp; | 20 | FILE *new_fp; |
21 | char *new_name; | 21 | char *fnamesfx; |
22 | char *last_char; | 22 | char *sfx_char; |
23 | unsigned user_len; | 23 | unsigned user_len; |
24 | int old_fd; | 24 | int old_fd; |
25 | int new_fd; | 25 | int new_fd; |
26 | int i; | 26 | int i; |
27 | int cnt = 0; | 27 | int cnt = 0; |
28 | int ret = 1; /* failure */ | 28 | int ret = -1; /* failure */ |
29 | 29 | ||
30 | /* New passwd file, "/etc/passwd+" for now */ | 30 | /* New passwd file, "/etc/passwd+" for now */ |
31 | new_name = xasprintf("%s+", filename); | 31 | fnamesfx = xasprintf("%s+", filename); |
32 | last_char = &new_name[strlen(new_name)-1]; | 32 | sfx_char = &fnamesfx[strlen(fnamesfx)-1]; |
33 | username = xasprintf("%s:", username); | 33 | username = xasprintf("%s:", username); |
34 | user_len = strlen(username); | 34 | user_len = strlen(username); |
35 | 35 | ||
@@ -42,12 +42,12 @@ int update_passwd(const char *filename, const char *username, | |||
42 | i = 30; | 42 | i = 30; |
43 | do { | 43 | do { |
44 | // FIXME: on last iteration try w/o O_EXCL but with O_TRUNC? | 44 | // FIXME: on last iteration try w/o O_EXCL but with O_TRUNC? |
45 | new_fd = open(new_name, O_WRONLY|O_CREAT|O_EXCL,0600); | 45 | new_fd = open(fnamesfx, O_WRONLY|O_CREAT|O_EXCL, 0600); |
46 | if (new_fd >= 0) goto created; | 46 | if (new_fd >= 0) goto created; |
47 | if (errno != EEXIST) break; | 47 | if (errno != EEXIST) break; |
48 | usleep(100000); /* 0.1 sec */ | 48 | usleep(100000); /* 0.1 sec */ |
49 | } while (--i); | 49 | } while (--i); |
50 | bb_perror_msg("cannot create '%s'", new_name); | 50 | bb_perror_msg("cannot create '%s'", fnamesfx); |
51 | goto close_old_fp; | 51 | goto close_old_fp; |
52 | 52 | ||
53 | created: | 53 | created: |
@@ -62,12 +62,13 @@ int update_passwd(const char *filename, const char *username, | |||
62 | } | 62 | } |
63 | 63 | ||
64 | /* Backup file is "/etc/passwd-" */ | 64 | /* Backup file is "/etc/passwd-" */ |
65 | last_char[0] = '-'; | 65 | *sfx_char = '-'; |
66 | /* Delete old one, create new as a hardlink to current */ | 66 | /* Delete old backup */ |
67 | i = (unlink(new_name) && errno != ENOENT); | 67 | i = (unlink(fnamesfx) && errno != ENOENT); |
68 | if (i || link(filename, new_name)) | 68 | /* Create backup as a hardlink to current */ |
69 | bb_perror_msg("warning: cannot create backup copy '%s'", new_name); | 69 | if (i || link(filename, fnamesfx)) |
70 | last_char[0] = '+'; | 70 | bb_perror_msg("warning: cannot create backup copy '%s'", fnamesfx); |
71 | *sfx_char = '+'; | ||
71 | 72 | ||
72 | /* Lock the password file before updating */ | 73 | /* Lock the password file before updating */ |
73 | lock.l_type = F_WRLCK; | 74 | lock.l_type = F_WRLCK; |
@@ -78,7 +79,7 @@ int update_passwd(const char *filename, const char *username, | |||
78 | bb_perror_msg("warning: cannot lock '%s'", filename); | 79 | bb_perror_msg("warning: cannot lock '%s'", filename); |
79 | lock.l_type = F_UNLCK; | 80 | lock.l_type = F_UNLCK; |
80 | 81 | ||
81 | /* Read current password file, write updated one */ | 82 | /* Read current password file, write updated /etc/passwd+ */ |
82 | while (1) { | 83 | while (1) { |
83 | char *line = xmalloc_fgets(old_fp); | 84 | char *line = xmalloc_fgets(old_fp); |
84 | if (!line) break; /* EOF/error */ | 85 | if (!line) break; /* EOF/error */ |
@@ -86,8 +87,7 @@ int update_passwd(const char *filename, const char *username, | |||
86 | /* we have a match with "username:"... */ | 87 | /* we have a match with "username:"... */ |
87 | const char *cp = line + user_len; | 88 | const char *cp = line + user_len; |
88 | /* now cp -> old passwd, skip it: */ | 89 | /* now cp -> old passwd, skip it: */ |
89 | cp = strchr(cp, ':'); | 90 | cp = strchrnul(cp, ':'); |
90 | if (!cp) cp = ""; | ||
91 | /* now cp -> ':' after old passwd or -> "" */ | 91 | /* now cp -> ':' after old passwd or -> "" */ |
92 | fprintf(new_fp, "%s%s%s", username, new_pw, cp); | 92 | fprintf(new_fp, "%s%s%s", username, new_pw, cp); |
93 | cnt++; | 93 | cnt++; |
@@ -99,7 +99,7 @@ int update_passwd(const char *filename, const char *username, | |||
99 | 99 | ||
100 | /* We do want all of them to execute, thus | instead of || */ | 100 | /* We do want all of them to execute, thus | instead of || */ |
101 | if ((ferror(old_fp) | fflush(new_fp) | fsync(new_fd) | fclose(new_fp)) | 101 | if ((ferror(old_fp) | fflush(new_fp) | fsync(new_fd) | fclose(new_fp)) |
102 | || rename(new_name, filename) | 102 | || rename(fnamesfx, filename) |
103 | ) { | 103 | ) { |
104 | /* At least one of those failed */ | 104 | /* At least one of those failed */ |
105 | goto unlink_new; | 105 | goto unlink_new; |
@@ -107,13 +107,13 @@ int update_passwd(const char *filename, const char *username, | |||
107 | ret = cnt; /* whee, success! */ | 107 | ret = cnt; /* whee, success! */ |
108 | 108 | ||
109 | unlink_new: | 109 | unlink_new: |
110 | if (ret) unlink(new_name); | 110 | if (ret < 0) unlink(fnamesfx); |
111 | 111 | ||
112 | close_old_fp: | 112 | close_old_fp: |
113 | fclose(old_fp); | 113 | fclose(old_fp); |
114 | 114 | ||
115 | free_mem: | 115 | free_mem: |
116 | if (ENABLE_FEATURE_CLEAN_UP) free(new_name); | 116 | free(fnamesfx); |
117 | if (ENABLE_FEATURE_CLEAN_UP) free((char*)username); | 117 | free((char*)username); |
118 | return ret; | 118 | return ret; |
119 | } | 119 | } |
diff --git a/loginutils/chpasswd.c b/loginutils/chpasswd.c index 124fc86e2..d5de424f0 100644 --- a/loginutils/chpasswd.c +++ b/loginutils/chpasswd.c | |||
@@ -26,19 +26,13 @@ int chpasswd_main(int argc, char **argv) | |||
26 | { | 26 | { |
27 | char *name, *pass; | 27 | char *name, *pass; |
28 | char salt[sizeof("$N$XXXXXXXX")]; | 28 | char salt[sizeof("$N$XXXXXXXX")]; |
29 | int opt, rc; | 29 | int opt; |
30 | int rnd = rnd; /* we *want* it to be non-initialized! */ | 30 | int rnd = rnd; /* we *want* it to be non-initialized! */ |
31 | const char *pwfile = bb_path_passwd_file; | ||
32 | 31 | ||
33 | if (getuid() != 0) | 32 | if (getuid()) |
34 | bb_error_msg_and_die(bb_msg_perm_denied_are_you_root); | 33 | bb_error_msg_and_die(bb_msg_perm_denied_are_you_root); |
35 | 34 | ||
36 | #if ENABLE_FEATURE_SHADOWPASSWDS | 35 | opt_complementary = "?m--e:e--m"; |
37 | if (access(bb_path_shadow_file, F_OK) == 0) | ||
38 | pwfile = bb_path_shadow_file; | ||
39 | #endif | ||
40 | |||
41 | opt_complementary = "m--e"; | ||
42 | USE_GETOPT_LONG(applet_long_options = chpasswd_opts;) | 36 | USE_GETOPT_LONG(applet_long_options = chpasswd_opts;) |
43 | opt = getopt32(argc, argv, "em"); | 37 | opt = getopt32(argc, argv, "em"); |
44 | 38 | ||
@@ -48,8 +42,7 @@ int chpasswd_main(int argc, char **argv) | |||
48 | bb_error_msg_and_die("missing new password"); | 42 | bb_error_msg_and_die("missing new password"); |
49 | *pass++ = '\0'; | 43 | *pass++ = '\0'; |
50 | 44 | ||
51 | //if (!getpwnam(name)) | 45 | xuname2uid(name); /* dies if there is no such user */ |
52 | // bb_error_msg_and_die("unknown user %s", name); | ||
53 | 46 | ||
54 | if (!(opt & OPT_ENC)) { | 47 | if (!(opt & OPT_ENC)) { |
55 | rnd = crypt_make_salt(salt, 1, rnd); | 48 | rnd = crypt_make_salt(salt, 1, rnd); |
@@ -60,15 +53,17 @@ int chpasswd_main(int argc, char **argv) | |||
60 | pass = pw_encrypt(pass, salt); | 53 | pass = pw_encrypt(pass, salt); |
61 | } | 54 | } |
62 | 55 | ||
63 | rc = update_passwd(pwfile, name, pass); | ||
64 | /* LOGMODE_BOTH logs to syslog */ | 56 | /* LOGMODE_BOTH logs to syslog */ |
65 | logmode = LOGMODE_BOTH; | 57 | logmode = LOGMODE_BOTH; |
66 | if (rc < 0) | 58 | |
67 | bb_error_msg_and_die("an error occurred updating %s", pwfile); | 59 | if ((ENABLE_FEATURE_SHADOWPASSWDS |
68 | if (rc > 0) | 60 | && !update_passwd(bb_path_shadow_file, name, pass)) |
61 | || !update_passwd(bb_path_passwd_file, name, pass) | ||
62 | ) { | ||
63 | bb_error_msg_and_die("an error occurred updating password for %s", name); | ||
64 | } else { | ||
69 | bb_info_msg("Password for '%s' changed", name); | 65 | bb_info_msg("Password for '%s' changed", name); |
70 | else | 66 | } |
71 | bb_info_msg("User '%s' not found", name); | ||
72 | logmode = LOGMODE_STDIO; | 67 | logmode = LOGMODE_STDIO; |
73 | free(name); | 68 | free(name); |
74 | } | 69 | } |
diff --git a/runit/runit_lib.c b/runit/runit_lib.c index fcb66c3db..295b45f09 100644 --- a/runit/runit_lib.c +++ b/runit/runit_lib.c | |||
@@ -382,7 +382,6 @@ int seek_set(int fd,seek_pos pos) | |||
382 | { | 382 | { |
383 | if (lseek(fd,(off_t) pos,SEEK_SET) == -1) return -1; return 0; | 383 | if (lseek(fd,(off_t) pos,SEEK_SET) == -1) return -1; return 0; |
384 | } | 384 | } |
385 | #endif | ||
386 | 385 | ||
387 | 386 | ||
388 | /*** str_chr.c ***/ | 387 | /*** str_chr.c ***/ |
@@ -402,3 +401,4 @@ unsigned str_chr(const char *s,int c) | |||
402 | } | 401 | } |
403 | return t - s; | 402 | return t - s; |
404 | } | 403 | } |
404 | #endif | ||
diff --git a/runit/runit_lib.h b/runit/runit_lib.h index 25aeeaf70..1dadb6e47 100644 --- a/runit/runit_lib.h +++ b/runit/runit_lib.h | |||
@@ -125,7 +125,7 @@ extern unsigned pmatch(const char *, const char *, unsigned); | |||
125 | 125 | ||
126 | /*** str.h ***/ | 126 | /*** str.h ***/ |
127 | 127 | ||
128 | extern unsigned str_chr(const char *,int); /* never returns NULL */ | 128 | //extern unsigned str_chr(const char *,int); /* never returns NULL */ |
129 | 129 | ||
130 | #define str_diff(s,t) strcmp((s), (t)) | 130 | #define str_diff(s,t) strcmp((s), (t)) |
131 | #define str_equal(s,t) (!strcmp((s), (t))) | 131 | #define str_equal(s,t) (!strcmp((s), (t))) |
diff --git a/scripts/trylink b/scripts/trylink index cbd702338..bfc67bf5d 100755 --- a/scripts/trylink +++ b/scripts/trylink | |||
@@ -22,6 +22,8 @@ try "-Wl,--start-group $l_list -Wl,--end-group" "$@" \ | |||
22 | cat busybox_ld.err | 22 | cat busybox_ld.err |
23 | exit 1 | 23 | exit 1 |
24 | } | 24 | } |
25 | # Hack: we are not supposed to know executable name, | ||
26 | # but this hack cuts down link time | ||
25 | mv busybox_unstripped busybox_unstripped.tmp | 27 | mv busybox_unstripped busybox_unstripped.tmp |
26 | 28 | ||
27 | # Now try to remove each lib and build without. | 29 | # Now try to remove each lib and build without. |