diff options
-rw-r--r-- | loginutils/adduser.c | 28 |
1 files changed, 15 insertions, 13 deletions
diff --git a/loginutils/adduser.c b/loginutils/adduser.c index 3154806b6..d4b50130c 100644 --- a/loginutils/adduser.c +++ b/loginutils/adduser.c | |||
@@ -33,18 +33,17 @@ static void passwd_study(struct passwd *p) | |||
33 | } | 33 | } |
34 | 34 | ||
35 | /* check for a free uid (and maybe gid) */ | 35 | /* check for a free uid (and maybe gid) */ |
36 | while (getpwuid(p->pw_uid) || (!p->pw_gid && getgrgid(p->pw_uid))) | 36 | while (getpwuid(p->pw_uid) || (p->pw_gid == (gid_t)-1 && getgrgid(p->pw_uid))) { |
37 | p->pw_uid++; | 37 | p->pw_uid++; |
38 | if (p->pw_uid > max) | ||
39 | bb_error_msg_and_die("no free uids left"); | ||
40 | } | ||
38 | 41 | ||
39 | if (!p->pw_gid) { | 42 | if (p->pw_gid == (gid_t)-1) { |
40 | /* new gid = uid */ | 43 | p->pw_gid = p->pw_uid; /* new gid = uid */ |
41 | p->pw_gid = p->pw_uid; | ||
42 | if (getgrnam(p->pw_name)) | 44 | if (getgrnam(p->pw_name)) |
43 | bb_error_msg_and_die("group name '%s' is in use", p->pw_name); | 45 | bb_error_msg_and_die("group name '%s' is in use", p->pw_name); |
44 | } | 46 | } |
45 | |||
46 | if (p->pw_uid > max) | ||
47 | bb_error_msg_and_die("no free uids left"); | ||
48 | } | 47 | } |
49 | 48 | ||
50 | static void addgroup_wrapper(struct passwd *p) | 49 | static void addgroup_wrapper(struct passwd *p) |
@@ -90,6 +89,7 @@ int adduser_main(int argc UNUSED_PARAM, char **argv) | |||
90 | struct passwd pw; | 89 | struct passwd pw; |
91 | const char *usegroup = NULL; | 90 | const char *usegroup = NULL; |
92 | FILE *file; | 91 | FILE *file; |
92 | int fd; | ||
93 | 93 | ||
94 | #if ENABLE_FEATURE_ADDUSER_LONG_OPTIONS | 94 | #if ENABLE_FEATURE_ADDUSER_LONG_OPTIONS |
95 | applet_long_options = adduser_longopts; | 95 | applet_long_options = adduser_longopts; |
@@ -117,7 +117,7 @@ int adduser_main(int argc UNUSED_PARAM, char **argv) | |||
117 | pw.pw_dir = xasprintf("/home/%s", argv[0]); | 117 | pw.pw_dir = xasprintf("/home/%s", argv[0]); |
118 | } | 118 | } |
119 | pw.pw_passwd = (char *)"x"; | 119 | pw.pw_passwd = (char *)"x"; |
120 | pw.pw_gid = usegroup ? xgroup2gid(usegroup) : 0; /* exits on failure */ | 120 | pw.pw_gid = usegroup ? xgroup2gid(usegroup) : -1; /* exits on failure */ |
121 | 121 | ||
122 | /* make sure everything is kosher and setup uid && maybe gid */ | 122 | /* make sure everything is kosher and setup uid && maybe gid */ |
123 | passwd_study(&pw); | 123 | passwd_study(&pw); |
@@ -134,17 +134,19 @@ int adduser_main(int argc UNUSED_PARAM, char **argv) | |||
134 | 134 | ||
135 | #if ENABLE_FEATURE_SHADOWPASSWDS | 135 | #if ENABLE_FEATURE_SHADOWPASSWDS |
136 | /* add to shadow if necessary */ | 136 | /* add to shadow if necessary */ |
137 | file = fopen_or_warn(bb_path_shadow_file, "a"); | 137 | /* fopen(..., "a"); would create shadow file, which is wrong. |
138 | if (file) { | 138 | * If shadow file doesn't exist, admin probably does not want it */ |
139 | //fseek(file, 0, SEEK_END); | 139 | fd = open_or_warn(bb_path_shadow_file, O_WRONLY | O_APPEND); |
140 | fprintf(file, "%s:!:%u:0:99999:7:::\n", | 140 | if (fd >= 0) { |
141 | char *s = xasprintf("%s:!:%u:0:99999:7:::\n", | ||
141 | pw.pw_name, /* username */ | 142 | pw.pw_name, /* username */ |
142 | (unsigned)(time(NULL) / 86400) /* sp->sp_lstchg */ | 143 | (unsigned)(time(NULL) / 86400) /* sp->sp_lstchg */ |
143 | /*0,*/ /* sp->sp_min */ | 144 | /*0,*/ /* sp->sp_min */ |
144 | /*99999,*/ /* sp->sp_max */ | 145 | /*99999,*/ /* sp->sp_max */ |
145 | /*7*/ /* sp->sp_warn */ | 146 | /*7*/ /* sp->sp_warn */ |
146 | ); | 147 | ); |
147 | fclose(file); | 148 | xwrite(fd, s, strlen(s)); |
149 | close(fd); | ||
148 | } | 150 | } |
149 | #endif | 151 | #endif |
150 | 152 | ||