1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70#include "libbb.h"
71
72#if CONFIG_LAST_SYSTEM_ID < CONFIG_FIRST_SYSTEM_ID
73#error Bad LAST_SYSTEM_ID or FIRST_SYSTEM_ID in .config
74#endif
75#if CONFIG_LAST_ID < CONFIG_LAST_SYSTEM_ID
76#error Bad LAST_ID or LAST_SYSTEM_ID in .config
77#endif
78
79
80
81
82#define OPT_SHELL (1 << 2)
83#define OPT_GID (1 << 3)
84#define OPT_DONT_SET_PASS (1 << 4)
85#define OPT_SYSTEM_ACCOUNT (1 << 5)
86#define OPT_DONT_MAKE_HOME (1 << 6)
87#define OPT_UID (1 << 7)
88#define OPT_SKEL (1 << 8)
89
90
91
92static void passwd_study(struct passwd *p)
93{
94 int max = CONFIG_LAST_ID;
95
96 if (getpwnam(p->pw_name)) {
97 bb_error_msg_and_die("%s '%s' in use", "user", p->pw_name);
98
99 }
100
101 if (!(option_mask32 & OPT_UID)) {
102 if (option_mask32 & OPT_SYSTEM_ACCOUNT) {
103 p->pw_uid = CONFIG_FIRST_SYSTEM_ID;
104 max = CONFIG_LAST_SYSTEM_ID;
105 } else {
106 p->pw_uid = CONFIG_LAST_SYSTEM_ID + 1;
107 }
108 }
109
110 while (getpwuid(p->pw_uid) || (p->pw_gid == (gid_t)-1 && getgrgid(p->pw_uid))) {
111 if (option_mask32 & OPT_UID) {
112
113 bb_error_msg_and_die("%s '%s' in use", "uid", itoa(p->pw_uid));
114
115 }
116 if (p->pw_uid == max) {
117 bb_error_msg_and_die("no %cids left", 'u');
118
119 }
120 p->pw_uid++;
121 }
122
123 if (p->pw_gid == (gid_t)-1) {
124 p->pw_gid = p->pw_uid;
125 if (getgrnam(p->pw_name)) {
126 bb_error_msg_and_die("%s '%s' in use", "group", p->pw_name);
127
128 }
129 }
130}
131
132static int addgroup_wrapper(struct passwd *p, const char *group_name)
133{
134 char *argv[6];
135
136 argv[0] = (char*)"addgroup";
137 if (group_name) {
138
139 argv[1] = (char*)"--";
140 argv[2] = p->pw_name;
141 argv[3] = (char*)group_name;
142 argv[4] = NULL;
143 } else {
144
145
146
147 argv[1] = (char*)"--gid";
148 argv[2] = utoa(p->pw_gid);
149 argv[3] = (char*)"--";
150 argv[4] = p->pw_name;
151 argv[5] = NULL;
152 }
153
154 return spawn_and_wait(argv);
155}
156
157static void passwd_wrapper(const char *login_name) NORETURN;
158
159static void passwd_wrapper(const char *login_name)
160{
161 BB_EXECLP("passwd", "passwd", "--", login_name, NULL);
162 bb_simple_error_msg_and_die("can't execute passwd, you must set password manually");
163}
164
165
166static const char adduser_longopts[] ALIGN1 =
167 "home\0" Required_argument "h"
168 "gecos\0" Required_argument "g"
169 "shell\0" Required_argument "s"
170 "ingroup\0" Required_argument "G"
171 "disabled-password\0" No_argument "D"
172 "empty-password\0" No_argument "D"
173 "system\0" No_argument "S"
174 "no-create-home\0" No_argument "H"
175 "uid\0" Required_argument "u"
176 "skel\0" Required_argument "k"
177 ;
178
179
180
181
182
183
184int adduser_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
185int adduser_main(int argc UNUSED_PARAM, char **argv)
186{
187 struct passwd pw;
188 const char *usegroup = NULL;
189 char *p;
190 unsigned opts;
191 char *uid;
192 const char *skel = "/etc/skel";
193
194
195 if (geteuid()) {
196 bb_simple_error_msg_and_die(bb_msg_perm_denied_are_you_root);
197 }
198
199 pw.pw_gecos = (char *)"Linux User,,,";
200
201 pw.pw_shell = xstrdup(get_shell_name());
202 pw.pw_dir = NULL;
203
204 opts = getopt32long(argv, "^"
205 "h:g:s:G:DSHu:k:"
206
207
208 "\0" "-1:?2:SD",
209 adduser_longopts,
210 &pw.pw_dir, &pw.pw_gecos, &pw.pw_shell,
211 &usegroup, &uid, &skel
212 );
213 if (opts & OPT_UID)
214 pw.pw_uid = xatou_range(uid, 0, CONFIG_LAST_ID);
215
216 argv += optind;
217 pw.pw_name = argv[0];
218
219 if (!opts && argv[1]) {
220
221
222
223 return addgroup_wrapper(&pw, argv[1]);
224 }
225
226
227 die_if_bad_username(pw.pw_name);
228 if (!pw.pw_dir) {
229
230 pw.pw_dir = xasprintf("/home/%s", argv[0]);
231 }
232 pw.pw_passwd = (char *)"x";
233 if (opts & OPT_SYSTEM_ACCOUNT) {
234 if (!usegroup) {
235 usegroup = "nogroup";
236 }
237 if (!(opts & OPT_SHELL)) {
238 pw.pw_shell = (char *) "/bin/false";
239 }
240 }
241 pw.pw_gid = usegroup ? xgroup2gid(usegroup) : -1;
242
243
244 passwd_study(&pw);
245
246 p = xasprintf("x:%u:%u:%s:%s:%s",
247 (unsigned) pw.pw_uid, (unsigned) pw.pw_gid,
248 pw.pw_gecos, pw.pw_dir, pw.pw_shell);
249 if (update_passwd(bb_path_passwd_file, pw.pw_name, p, NULL) < 0) {
250 return EXIT_FAILURE;
251 }
252 if (ENABLE_FEATURE_CLEAN_UP)
253 free(p);
254#if ENABLE_FEATURE_SHADOWPASSWDS
255
256
257
258
259
260
261
262
263
264
265
266 p = xasprintf("!:%u:0:99999:7:::", (unsigned)(time(NULL)) / (24*60*60));
267
268 update_passwd(bb_path_shadow_file, pw.pw_name, p, NULL);
269 if (ENABLE_FEATURE_CLEAN_UP)
270 free(p);
271#endif
272
273
274 addgroup_wrapper(&pw, usegroup);
275
276
277
278 umask(0);
279 if (!(opts & OPT_DONT_MAKE_HOME)) {
280
281
282
283 int mkdir_err = mkdir(pw.pw_dir, 0755);
284 if (mkdir_err == 0) {
285
286 const char *args[] = {
287 "chown",
288 "-R",
289 xasprintf("%u:%u", (int)pw.pw_uid, (int)pw.pw_gid),
290 pw.pw_dir,
291 NULL
292 };
293
294 if (!(opts & OPT_SKEL))
295 logmode = LOGMODE_NONE;
296 copy_file(skel, pw.pw_dir, FILEUTILS_RECUR);
297 logmode = LOGMODE_STDIO;
298 chown_main(4, (char**)args);
299 }
300 if ((mkdir_err != 0 && errno != EEXIST)
301 || chown(pw.pw_dir, pw.pw_uid, pw.pw_gid) != 0
302 || chmod(pw.pw_dir, 02755) != 0
303 ) {
304 bb_simple_perror_msg(pw.pw_dir);
305 }
306 }
307
308 if (!(opts & OPT_DONT_SET_PASS)) {
309
310 passwd_wrapper(pw.pw_name);
311 }
312
313 return EXIT_SUCCESS;
314}
315