1 /* vi: set sw=4 ts=4: */
3 * adduser - add users to /etc/passwd and /etc/shadow
5 * Copyright (C) 1999 by Lineo, inc. and John Beppu
6 * Copyright (C) 1999,2000,2001 by John Beppu <beppu@codepoet.org>
8 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
10 //config:config ADDUSER
11 //config: bool "adduser (15 kb)"
13 //config: select LONG_OPTS
15 //config: Utility for creating a new user account.
17 //config:config FEATURE_CHECK_NAMES
18 //config: bool "Enable sanity check on user/group names in adduser and addgroup"
20 //config: depends on ADDUSER || ADDGROUP
22 //config: Enable sanity check on user and group names in adduser and addgroup.
23 //config: To avoid problems, the user or group name should consist only of
24 //config: letters, digits, underscores, periods, at signs and dashes,
25 //config: and not start with a dash (as defined by IEEE Std 1003.1-2001).
26 //config: For compatibility with Samba machine accounts "$" is also supported
27 //config: at the end of the user or group name.
29 //config:config LAST_ID
30 //config: int "Last valid uid or gid for adduser and addgroup"
31 //config: depends on ADDUSER || ADDGROUP
32 //config: default 60000
34 //config: Last valid uid or gid for adduser and addgroup
36 //config:config FIRST_SYSTEM_ID
37 //config: int "First valid system uid or gid for adduser and addgroup"
38 //config: depends on ADDUSER || ADDGROUP
39 //config: range 0 LAST_ID
42 //config: First valid system uid or gid for adduser and addgroup
44 //config:config LAST_SYSTEM_ID
45 //config: int "Last valid system uid or gid for adduser and addgroup"
46 //config: depends on ADDUSER || ADDGROUP
47 //config: range FIRST_SYSTEM_ID LAST_ID
50 //config: Last valid system uid or gid for adduser and addgroup
52 //applet:IF_ADDUSER(APPLET_NOEXEC(adduser, adduser, BB_DIR_USR_SBIN, BB_SUID_DROP, adduser))
54 //kbuild:lib-$(CONFIG_ADDUSER) += adduser.o
56 //usage:#define adduser_trivial_usage
57 //usage: "[OPTIONS] USER [GROUP]"
58 //usage:#define adduser_full_usage "\n\n"
59 //usage: "Create new user, or add USER to GROUP\n"
60 //usage: "\n -h DIR Home directory"
61 //usage: "\n -g GECOS GECOS field"
62 //usage: "\n -s SHELL Login shell"
63 //usage: "\n -G GRP Group"
64 //usage: "\n -S Create a system user"
65 //usage: "\n -D Don't assign a password"
66 //usage: "\n -H Don't create home directory"
67 //usage: "\n -u UID User id"
68 //usage: "\n -k SKEL Skeleton directory (/etc/skel)"
72 #if CONFIG_LAST_SYSTEM_ID < CONFIG_FIRST_SYSTEM_ID
73 #error Bad LAST_SYSTEM_ID or FIRST_SYSTEM_ID in .config
75 #if CONFIG_LAST_ID < CONFIG_LAST_SYSTEM_ID
76 #error Bad LAST_ID or LAST_SYSTEM_ID in .config
80 /* #define OPT_HOME (1 << 0) */ /* unused */
81 /* #define OPT_GECOS (1 << 1) */ /* unused */
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)
91 /* recoded such that the uid may be passed in *p */
92 static void passwd_study(struct passwd *p)
94 int max = CONFIG_LAST_ID;
96 if (getpwnam(p->pw_name)) {
97 bb_error_msg_and_die("%s '%s' in use", "user", p->pw_name);
98 /* this format string is reused in adduser and addgroup */
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;
106 p->pw_uid = CONFIG_LAST_SYSTEM_ID + 1;
109 /* check for a free uid (and maybe gid) */
110 while (getpwuid(p->pw_uid) || (p->pw_gid == (gid_t)-1 && getgrgid(p->pw_uid))) {
111 if (option_mask32 & OPT_UID) {
112 /* -u N, cannot pick uid other than N: error */
113 bb_error_msg_and_die("%s '%s' in use", "uid", itoa(p->pw_uid));
114 /* this format string is reused in adduser and addgroup */
116 if (p->pw_uid == max) {
117 bb_error_msg_and_die("no %cids left", 'u');
118 /* this format string is reused in adduser and addgroup */
123 if (p->pw_gid == (gid_t)-1) {
124 p->pw_gid = p->pw_uid; /* new gid = uid */
125 if (getgrnam(p->pw_name)) {
126 bb_error_msg_and_die("%s '%s' in use", "group", p->pw_name);
127 /* this format string is reused in adduser and addgroup */
132 static int addgroup_wrapper(struct passwd *p, const char *group_name)
136 argv[0] = (char*)"addgroup";
138 /* Add user to existing group */
139 argv[1] = (char*)"--";
140 argv[2] = p->pw_name;
141 argv[3] = (char*)group_name;
144 /* Add user to his own group with the first free gid
145 * found in passwd_study.
147 argv[1] = (char*)"--gid";
148 argv[2] = utoa(p->pw_gid);
149 argv[3] = (char*)"--";
150 argv[4] = p->pw_name;
154 return spawn_and_wait(argv);
157 static void passwd_wrapper(const char *login_name) NORETURN;
159 static void passwd_wrapper(const char *login_name)
161 BB_EXECLP("passwd", "passwd", "--", login_name, NULL);
162 bb_error_msg_and_die("can't execute passwd, you must set password manually");
165 //FIXME: upstream adduser has no short options! NOT COMPATIBLE!
166 static 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"
180 * adduser will take a login_name as its first parameter.
181 * home, shell, gecos:
182 * can be customized via command-line parameters.
184 int adduser_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
185 int adduser_main(int argc UNUSED_PARAM, char **argv)
188 const char *usegroup = NULL;
192 const char *skel = "/etc/skel";
196 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
199 pw.pw_gecos = (char *)"Linux User,,,";
200 /* We assume that newly created users "inherit" root's shell setting */
201 pw.pw_shell = (char *)get_shell_name();
204 opts = getopt32long(argv, "^"
206 /* at least one and at most two non-option args */
207 /* disable interactive passwd for system accounts */
210 &pw.pw_dir, &pw.pw_gecos, &pw.pw_shell,
211 &usegroup, &uid, &skel
214 pw.pw_uid = xatou_range(uid, 0, CONFIG_LAST_ID);
217 pw.pw_name = argv[0];
219 if (!opts && argv[1]) {
220 /* if called with two non-option arguments, adduser
221 * will add an existing user to an existing group.
223 return addgroup_wrapper(&pw, argv[1]);
226 /* fill in the passwd struct */
227 die_if_bad_username(pw.pw_name);
229 /* create string for $HOME if not specified already */
230 pw.pw_dir = xasprintf("/home/%s", argv[0]);
232 pw.pw_passwd = (char *)"x";
233 if (opts & OPT_SYSTEM_ACCOUNT) {
235 usegroup = "nogroup";
237 if (!(opts & OPT_SHELL)) {
238 pw.pw_shell = (char *) "/bin/false";
241 pw.pw_gid = usegroup ? xgroup2gid(usegroup) : -1; /* exits on failure */
243 /* make sure everything is kosher and setup uid && maybe gid */
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) {
252 if (ENABLE_FEATURE_CLEAN_UP)
254 #if ENABLE_FEATURE_SHADOWPASSWDS
255 /* /etc/shadow fields:
257 * 2. encrypted password
258 * 3. last password change (unix date (unix time/24*60*60))
259 * 4. minimum days required between password changes
260 * 5. maximum days password is valid
261 * 6. days before password is to expire that user is warned
262 * 7. days after password expires that account is disabled
263 * 8. unix date when login expires (i.e. when it may no longer be used)
265 /* fields: 2 3 4 5 6 78 */
266 p = xasprintf("!:%u:0:99999:7:::", (unsigned)(time(NULL)) / (24*60*60));
267 /* ignore errors: if file is missing we suppose admin doesn't want it */
268 update_passwd(bb_path_shadow_file, pw.pw_name, p, NULL);
269 if (ENABLE_FEATURE_CLEAN_UP)
274 addgroup_wrapper(&pw, usegroup);
276 /* clear the umask for this process so it doesn't
277 * screw up the permissions on the mkdir and chown. */
279 if (!(opts & OPT_DONT_MAKE_HOME)) {
280 /* set the owner and group so it is owned by the new user,
281 * then fix up the permissions to 2755. Can't do it before
282 * since chown will clear the setgid bit */
283 int mkdir_err = mkdir(pw.pw_dir, 0755);
284 if (mkdir_err == 0) {
285 /* New home. Copy /etc/skel to it */
286 const char *args[] = {
289 xasprintf("%u:%u", (int)pw.pw_uid, (int)pw.pw_gid),
293 /* Be silent on any errors (like: no /etc/skel) */
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);
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 /* set setgid bit on homedir */
304 bb_simple_perror_msg(pw.pw_dir);
308 if (!(opts & OPT_DONT_SET_PASS)) {
309 /* interactively set passwd */
310 passwd_wrapper(pw.pw_name);