addgroup: mention -S in the help text
[oweals/busybox.git] / loginutils / adduser.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * adduser - add users to /etc/passwd and /etc/shadow
4  *
5  * Copyright (C) 1999 by Lineo, inc. and John Beppu
6  * Copyright (C) 1999,2000,2001 by John Beppu <beppu@codepoet.org>
7  *
8  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
9  */
10
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"
23
24 #include "libbb.h"
25
26 #if CONFIG_LAST_SYSTEM_ID < CONFIG_FIRST_SYSTEM_ID
27 #error Bad LAST_SYSTEM_ID or FIRST_SYSTEM_ID in .config
28 #endif
29 #if CONFIG_LAST_ID < CONFIG_LAST_SYSTEM_ID
30 #error Bad LAST_ID or LAST_SYSTEM_ID in .config
31 #endif
32
33
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)
42
43 /* remix */
44 /* recoded such that the uid may be passed in *p */
45 static void passwd_study(struct passwd *p)
46 {
47         int max = CONFIG_LAST_ID;
48
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 */
52         }
53
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;
58                 } else {
59                         p->pw_uid = CONFIG_LAST_SYSTEM_ID + 1;
60                 }
61         }
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 */
68                 }
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 */
72                 }
73                 p->pw_uid++;
74         }
75
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 */
81                 }
82         }
83 }
84
85 static int addgroup_wrapper(struct passwd *p, const char *group_name)
86 {
87         char *argv[6];
88
89         argv[0] = (char*)"addgroup";
90         if (group_name) {
91                 /* Add user to existing group */
92                 argv[1] = (char*)"--";
93                 argv[2] = p->pw_name;
94                 argv[3] = (char*)group_name;
95                 argv[4] = NULL;
96         } else {
97                 /* Add user to his own group with the first free gid
98                  * found in passwd_study.
99                  */
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.
103                  */
104                 argv[1] = (char*)"--gid";
105 #else
106                 /* Breaks if system in fact does NOT use busybox addgroup */
107                 argv[1] = (char*)"-g";
108 #endif
109                 argv[2] = utoa(p->pw_gid);
110                 argv[3] = (char*)"--";
111                 argv[4] = p->pw_name;
112                 argv[5] = NULL;
113         }
114
115         return spawn_and_wait(argv);
116 }
117
118 static void passwd_wrapper(const char *login_name) NORETURN;
119
120 static void passwd_wrapper(const char *login_name)
121 {
122         BB_EXECLP("passwd", "passwd", "--", login_name, NULL);
123         bb_error_msg_and_die("can't execute passwd, you must set password manually");
124 }
125
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"
137                 ;
138 #endif
139
140 /*
141  * adduser will take a login_name as its first parameter.
142  * home, shell, gecos:
143  * can be customized via command-line parameters.
144  */
145 int adduser_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
146 int adduser_main(int argc UNUSED_PARAM, char **argv)
147 {
148         struct passwd pw;
149         const char *usegroup = NULL;
150         char *p;
151         unsigned opts;
152         char *uid;
153
154 #if ENABLE_FEATURE_ADDUSER_LONG_OPTIONS
155         applet_long_options = adduser_longopts;
156 #endif
157
158         /* got root? */
159         if (geteuid()) {
160                 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
161         }
162
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();
166         pw.pw_dir = NULL;
167
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);
172         if (opts & OPT_UID)
173                 pw.pw_uid = xatou_range(uid, 0, CONFIG_LAST_ID);
174
175         argv += optind;
176         pw.pw_name = argv[0];
177
178         if (!opts && argv[1]) {
179                 /* if called with two non-option arguments, adduser
180                  * will add an existing user to an existing group.
181                  */
182                 return addgroup_wrapper(&pw, argv[1]);
183         }
184
185         /* fill in the passwd struct */
186         die_if_bad_username(pw.pw_name);
187         if (!pw.pw_dir) {
188                 /* create string for $HOME if not specified already */
189                 pw.pw_dir = xasprintf("/home/%s", argv[0]);
190         }
191         pw.pw_passwd = (char *)"x";
192         if (opts & OPT_SYSTEM_ACCOUNT) {
193                 if (!usegroup) {
194                         usegroup = "nogroup";
195                 }
196                 if (!(opts & OPT_SHELL)) {
197                         pw.pw_shell = (char *) "/bin/false";
198                 }
199         }
200         pw.pw_gid = usegroup ? xgroup2gid(usegroup) : -1; /* exits on failure */
201
202         /* make sure everything is kosher and setup uid && maybe gid */
203         passwd_study(&pw);
204
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) {
209                 return EXIT_FAILURE;
210         }
211         if (ENABLE_FEATURE_CLEAN_UP)
212                 free(p);
213 #if ENABLE_FEATURE_SHADOWPASSWDS
214         /* /etc/shadow fields:
215          * 1. username
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)
223          */
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)
229                 free(p);
230 #endif
231
232         /* add to group */
233         addgroup_wrapper(&pw, usegroup);
234
235         /* clear the umask for this process so it doesn't
236          * screw up the permissions on the mkdir and chown. */
237         umask(0);
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[] = {
246                                 "chown",
247                                 "-R",
248                                 xasprintf("%u:%u", (int)pw.pw_uid, (int)pw.pw_gid),
249                                 pw.pw_dir,
250                                 NULL
251                         };
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);
257                 }
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 */
261                 ) {
262                         bb_simple_perror_msg(pw.pw_dir);
263                 }
264         }
265
266         if (!(opts & OPT_DONT_SET_PASS)) {
267                 /* interactively set passwd */
268                 passwd_wrapper(pw.pw_name);
269         }
270
271         return EXIT_SUCCESS;
272 }