diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2010-01-09 20:57:06 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2010-01-09 20:57:06 +0100 |
commit | 4653e442ba66bd4917a6069293d4daff9691c61f (patch) | |
tree | 3a5e179afc32ebb62a365bbcc9ee1827d6641df6 | |
parent | 02dd96f688994b5ef4f79a7cdf940b844d6ac52b (diff) | |
download | busybox-w32-4653e442ba66bd4917a6069293d4daff9691c61f.tar.gz busybox-w32-4653e442ba66bd4917a6069293d4daff9691c61f.tar.bz2 busybox-w32-4653e442ba66bd4917a6069293d4daff9691c61f.zip |
adduser: more fixes to "add user to specified group"
function old new delta
update_passwd 1246 1295 +49
adduser_main 727 725 -2
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | libbb/update_passwd.c | 25 | ||||
-rw-r--r-- | loginutils/adduser.c | 7 |
2 files changed, 19 insertions, 13 deletions
diff --git a/libbb/update_passwd.c b/libbb/update_passwd.c index 301893be1..f5ce1f955 100644 --- a/libbb/update_passwd.c +++ b/libbb/update_passwd.c | |||
@@ -81,6 +81,7 @@ int FAST_FUNC update_passwd(const char *filename, | |||
81 | FILE *new_fp; | 81 | FILE *new_fp; |
82 | char *fnamesfx; | 82 | char *fnamesfx; |
83 | char *sfx_char; | 83 | char *sfx_char; |
84 | char *name_colon; | ||
84 | unsigned user_len; | 85 | unsigned user_len; |
85 | int old_fd; | 86 | int old_fd; |
86 | int new_fd; | 87 | int new_fd; |
@@ -103,8 +104,8 @@ int FAST_FUNC update_passwd(const char *filename, | |||
103 | /* New passwd file, "/etc/passwd+" for now */ | 104 | /* New passwd file, "/etc/passwd+" for now */ |
104 | fnamesfx = xasprintf("%s+", filename); | 105 | fnamesfx = xasprintf("%s+", filename); |
105 | sfx_char = &fnamesfx[strlen(fnamesfx)-1]; | 106 | sfx_char = &fnamesfx[strlen(fnamesfx)-1]; |
106 | name = xasprintf("%s:", name); | 107 | name_colon = xasprintf("%s:", name); |
107 | user_len = strlen(name); | 108 | user_len = strlen(name_colon); |
108 | 109 | ||
109 | if (shadow) | 110 | if (shadow) |
110 | old_fp = fopen(filename, "r+"); | 111 | old_fp = fopen(filename, "r+"); |
@@ -166,7 +167,7 @@ int FAST_FUNC update_passwd(const char *filename, | |||
166 | line = xmalloc_fgetline(old_fp); | 167 | line = xmalloc_fgetline(old_fp); |
167 | if (!line) /* EOF/error */ | 168 | if (!line) /* EOF/error */ |
168 | break; | 169 | break; |
169 | if (strncmp(name, line, user_len) != 0) { | 170 | if (strncmp(name_colon, line, user_len) != 0) { |
170 | fprintf(new_fp, "%s\n", line); | 171 | fprintf(new_fp, "%s\n", line); |
171 | goto next; | 172 | goto next; |
172 | } | 173 | } |
@@ -225,11 +226,11 @@ int FAST_FUNC update_passwd(const char *filename, | |||
225 | /* move past old change date */ | 226 | /* move past old change date */ |
226 | cp = strchrnul(cp + 1, ':'); | 227 | cp = strchrnul(cp + 1, ':'); |
227 | /* "name:" + "new_passwd" + ":" + "change date" + ":rest of line" */ | 228 | /* "name:" + "new_passwd" + ":" + "change date" + ":rest of line" */ |
228 | fprintf(new_fp, "%s%s:%u%s\n", name, new_passwd, | 229 | fprintf(new_fp, "%s%s:%u%s\n", name_colon, new_passwd, |
229 | (unsigned)(time(NULL)) / (24*60*60), cp); | 230 | (unsigned)(time(NULL)) / (24*60*60), cp); |
230 | } else { | 231 | } else { |
231 | /* "name:" + "new_passwd" + ":rest of line" */ | 232 | /* "name:" + "new_passwd" + ":rest of line" */ |
232 | fprintf(new_fp, "%s%s%s\n", name, new_passwd, cp); | 233 | fprintf(new_fp, "%s%s%s\n", name_colon, new_passwd, cp); |
233 | } | 234 | } |
234 | changed_lines++; | 235 | changed_lines++; |
235 | } /* else delete user or group: skip the line */ | 236 | } /* else delete user or group: skip the line */ |
@@ -238,15 +239,19 @@ int FAST_FUNC update_passwd(const char *filename, | |||
238 | } | 239 | } |
239 | 240 | ||
240 | if (changed_lines == 0) { | 241 | if (changed_lines == 0) { |
241 | #if ENABLE_FEATURE_DEL_USER_FROM_GROUP | 242 | #if ENABLE_FEATURE_ADDUSER_TO_GROUP || ENABLE_FEATURE_DEL_USER_FROM_GROUP |
242 | if (member) | 243 | if (member) { |
243 | bb_error_msg("can't find %s in %s", member, filename); | 244 | if (ENABLE_ADDGROUP && applet_name[0] == 'a') |
245 | bb_error_msg("can't find %s in %s", name, filename); | ||
246 | if (ENABLE_DELGROUP && applet_name[0] == 'd') | ||
247 | bb_error_msg("can't find %s in %s", member, filename); | ||
248 | } | ||
244 | #endif | 249 | #endif |
245 | if ((ENABLE_ADDUSER || ENABLE_ADDGROUP) | 250 | if ((ENABLE_ADDUSER || ENABLE_ADDGROUP) |
246 | && applet_name[0] == 'a' && !member | 251 | && applet_name[0] == 'a' && !member |
247 | ) { | 252 | ) { |
248 | /* add user or group */ | 253 | /* add user or group */ |
249 | fprintf(new_fp, "%s%s\n", name, new_passwd); | 254 | fprintf(new_fp, "%s%s\n", name_colon, new_passwd); |
250 | changed_lines++; | 255 | changed_lines++; |
251 | } | 256 | } |
252 | } | 257 | } |
@@ -275,6 +280,6 @@ int FAST_FUNC update_passwd(const char *filename, | |||
275 | free_mem: | 280 | free_mem: |
276 | free(fnamesfx); | 281 | free(fnamesfx); |
277 | free((char *)filename); | 282 | free((char *)filename); |
278 | free((char *)name); | 283 | free(name_colon); |
279 | return ret; | 284 | return ret; |
280 | } | 285 | } |
diff --git a/loginutils/adduser.c b/loginutils/adduser.c index ff30a592d..5f593ac16 100644 --- a/loginutils/adduser.c +++ b/loginutils/adduser.c | |||
@@ -65,11 +65,12 @@ static void passwd_study(struct passwd *p) | |||
65 | } | 65 | } |
66 | } | 66 | } |
67 | 67 | ||
68 | static void addgroup_wrapper(struct passwd *p) | 68 | static void addgroup_wrapper(struct passwd *p, const char *group_name) |
69 | { | 69 | { |
70 | char *cmd; | 70 | char *cmd; |
71 | 71 | ||
72 | cmd = xasprintf("addgroup -g %u '%s'", (unsigned)p->pw_gid, p->pw_name); | 72 | cmd = xasprintf("addgroup '%s' '%s'", |
73 | p->pw_name, group_name); | ||
73 | system(cmd); | 74 | system(cmd); |
74 | free(cmd); | 75 | free(cmd); |
75 | } | 76 | } |
@@ -191,7 +192,7 @@ int adduser_main(int argc UNUSED_PARAM, char **argv) | |||
191 | /* addgroup should be responsible for dealing w/ gshadow */ | 192 | /* addgroup should be responsible for dealing w/ gshadow */ |
192 | /* if using a pre-existing group, don't create one */ | 193 | /* if using a pre-existing group, don't create one */ |
193 | if (usegroup) | 194 | if (usegroup) |
194 | addgroup_wrapper(&pw); | 195 | addgroup_wrapper(&pw, usegroup); |
195 | 196 | ||
196 | /* clear the umask for this process so it doesn't | 197 | /* clear the umask for this process so it doesn't |
197 | * screw up the permissions on the mkdir and chown. */ | 198 | * screw up the permissions on the mkdir and chown. */ |