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.
11 //usage:#define adduser_trivial_usage
12 //usage: "[OPTIONS] USER [GROUP]"
13 //usage:#define adduser_full_usage "\n\n"
14 //usage: "Create new user, or add USER to GROUP\n"
15 //usage: "\n -h DIR Home directory"
16 //usage: "\n -g GECOS GECOS field"
17 //usage: "\n -s SHELL Login shell"
18 //usage: "\n -G GRP Add user to existing group"
19 //usage: "\n -S Create a system user"
20 //usage: "\n -D Don't assign a password"
21 //usage: "\n -H Don't create home directory"
22 //usage: "\n -u UID User id"
26 #if CONFIG_LAST_SYSTEM_ID < CONFIG_FIRST_SYSTEM_ID
27 #error Bad LAST_SYSTEM_ID or FIRST_SYSTEM_ID in .config
29 #if CONFIG_LAST_ID < CONFIG_LAST_SYSTEM_ID
30 #error Bad LAST_ID or LAST_SYSTEM_ID in .config
34 /* #define OPT_HOME (1 << 0) */ /* unused */
35 /* #define OPT_GECOS (1 << 1) */ /* unused */
36 #define OPT_SHELL (1 << 2)
37 #define OPT_GID (1 << 3)
38 #define OPT_DONT_SET_PASS (1 << 4)
39 #define OPT_SYSTEM_ACCOUNT (1 << 5)
40 #define OPT_DONT_MAKE_HOME (1 << 6)
41 #define OPT_UID (1 << 7)
44 /* recoded such that the uid may be passed in *p */
45 static void passwd_study(struct passwd *p)
47 int max = CONFIG_LAST_ID;
49 if (getpwnam(p->pw_name)) {
50 bb_error_msg_and_die("%s '%s' in use", "user", p->pw_name);
51 /* this format string is reused in adduser and addgroup */
54 if (!(option_mask32 & OPT_UID)) {
55 if (option_mask32 & OPT_SYSTEM_ACCOUNT) {
56 p->pw_uid = CONFIG_FIRST_SYSTEM_ID;
57 max = CONFIG_LAST_SYSTEM_ID;
59 p->pw_uid = CONFIG_LAST_SYSTEM_ID + 1;
62 /* check for a free uid (and maybe gid) */
63 while (getpwuid(p->pw_uid) || (p->pw_gid == (gid_t)-1 && getgrgid(p->pw_uid))) {
64 if (option_mask32 & OPT_UID) {
65 /* -u N, cannot pick uid other than N: error */
66 bb_error_msg_and_die("%s '%s' in use", "uid", itoa(p->pw_uid));
67 /* this format string is reused in adduser and addgroup */
69 if (p->pw_uid == max) {
70 bb_error_msg_and_die("no %cids left", 'u');
71 /* this format string is reused in adduser and addgroup */
76 if (p->pw_gid == (gid_t)-1) {
77 p->pw_gid = p->pw_uid; /* new gid = uid */
78 if (getgrnam(p->pw_name)) {
79 bb_error_msg_and_die("%s '%s' in use", "group", p->pw_name);
80 /* this format string is reused in adduser and addgroup */
85 static int addgroup_wrapper(struct passwd *p, const char *group_name)
89 argv[0] = (char*)"addgroup";
91 /* Add user to existing group */
92 argv[1] = (char*)"--";
94 argv[3] = (char*)group_name;
97 /* Add user to his own group with the first free gid
98 * found in passwd_study.
100 #if ENABLE_FEATURE_ADDGROUP_LONG_OPTIONS || !ENABLE_ADDGROUP
101 /* We try to use --gid, not -g, because "standard" addgroup
102 * has no short option -g, it has only long --gid.
104 argv[1] = (char*)"--gid";
106 /* Breaks if system in fact does NOT use busybox addgroup */
107 argv[1] = (char*)"-g";
109 argv[2] = utoa(p->pw_gid);
110 argv[3] = (char*)"--";
111 argv[4] = p->pw_name;
115 return spawn_and_wait(argv);
118 static void passwd_wrapper(const char *login_name) NORETURN;
120 static void passwd_wrapper(const char *login_name)
122 BB_EXECLP("passwd", "passwd", "--", login_name, NULL);
123 bb_error_msg_and_die("can't execute passwd, you must set password manually");
126 #if ENABLE_FEATURE_ADDUSER_LONG_OPTIONS
127 static const char adduser_longopts[] ALIGN1 =
128 "home\0" Required_argument "h"
129 "gecos\0" Required_argument "g"
130 "shell\0" Required_argument "s"
131 "ingroup\0" Required_argument "G"
132 "disabled-password\0" No_argument "D"
133 "empty-password\0" No_argument "D"
134 "system\0" No_argument "S"
135 "no-create-home\0" No_argument "H"
136 "uid\0" Required_argument "u"
141 * adduser will take a login_name as its first parameter.
142 * home, shell, gecos:
143 * can be customized via command-line parameters.
145 int adduser_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
146 int adduser_main(int argc UNUSED_PARAM, char **argv)
149 const char *usegroup = NULL;
154 #if ENABLE_FEATURE_ADDUSER_LONG_OPTIONS
155 applet_long_options = adduser_longopts;
160 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
163 pw.pw_gecos = (char *)"Linux User,,,";
164 /* We assume that newly created users "inherit" root's shell setting */
165 pw.pw_shell = (char *)get_shell_name();
168 /* at least one and at most two non-option args */
169 /* disable interactive passwd for system accounts */
170 opt_complementary = "-1:?2:SD";
171 opts = getopt32(argv, "h:g:s:G:DSHu:", &pw.pw_dir, &pw.pw_gecos, &pw.pw_shell, &usegroup, &uid);
173 pw.pw_uid = xatou_range(uid, 0, CONFIG_LAST_ID);
176 pw.pw_name = argv[0];
178 if (!opts && argv[1]) {
179 /* if called with two non-option arguments, adduser
180 * will add an existing user to an existing group.
182 return addgroup_wrapper(&pw, argv[1]);
185 /* fill in the passwd struct */
186 die_if_bad_username(pw.pw_name);
188 /* create string for $HOME if not specified already */
189 pw.pw_dir = xasprintf("/home/%s", argv[0]);
191 pw.pw_passwd = (char *)"x";
192 if (opts & OPT_SYSTEM_ACCOUNT) {
194 usegroup = "nogroup";
196 if (!(opts & OPT_SHELL)) {
197 pw.pw_shell = (char *) "/bin/false";
200 pw.pw_gid = usegroup ? xgroup2gid(usegroup) : -1; /* exits on failure */
202 /* make sure everything is kosher and setup uid && maybe gid */
205 p = xasprintf("x:%u:%u:%s:%s:%s",
206 (unsigned) pw.pw_uid, (unsigned) pw.pw_gid,
207 pw.pw_gecos, pw.pw_dir, pw.pw_shell);
208 if (update_passwd(bb_path_passwd_file, pw.pw_name, p, NULL) < 0) {
211 if (ENABLE_FEATURE_CLEAN_UP)
213 #if ENABLE_FEATURE_SHADOWPASSWDS
214 /* /etc/shadow fields:
216 * 2. encrypted password
217 * 3. last password change (unix date (unix time/24*60*60))
218 * 4. minimum days required between password changes
219 * 5. maximum days password is valid
220 * 6. days before password is to expire that user is warned
221 * 7. days after password expires that account is disabled
222 * 8. unix date when login expires (i.e. when it may no longer be used)
224 /* fields: 2 3 4 5 6 78 */
225 p = xasprintf("!:%u:0:99999:7:::", (unsigned)(time(NULL)) / (24*60*60));
226 /* ignore errors: if file is missing we suppose admin doesn't want it */
227 update_passwd(bb_path_shadow_file, pw.pw_name, p, NULL);
228 if (ENABLE_FEATURE_CLEAN_UP)
233 addgroup_wrapper(&pw, usegroup);
235 /* clear the umask for this process so it doesn't
236 * screw up the permissions on the mkdir and chown. */
238 if (!(opts & OPT_DONT_MAKE_HOME)) {
239 /* set the owner and group so it is owned by the new user,
240 * then fix up the permissions to 2755. Can't do it before
241 * since chown will clear the setgid bit */
242 int mkdir_err = mkdir(pw.pw_dir, 0755);
243 if (mkdir_err == 0) {
244 /* New home. Copy /etc/skel to it */
245 const char *args[] = {
248 xasprintf("%u:%u", (int)pw.pw_uid, (int)pw.pw_gid),
252 /* Be silent on any errors (like: no /etc/skel) */
253 logmode = LOGMODE_NONE;
254 copy_file("/etc/skel", pw.pw_dir, FILEUTILS_RECUR);
255 logmode = LOGMODE_STDIO;
256 chown_main(4, (char**)args);
258 if ((mkdir_err != 0 && errno != EEXIST)
259 || chown(pw.pw_dir, pw.pw_uid, pw.pw_gid) != 0
260 || chmod(pw.pw_dir, 02755) != 0 /* set setgid bit on homedir */
262 bb_simple_perror_msg(pw.pw_dir);
266 if (!(opts & OPT_DONT_SET_PASS)) {
267 /* interactively set passwd */
268 passwd_wrapper(pw.pw_name);