use user's shell instead of hardwired "/bin/sh" (android needs this)
[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 #include "libbb.h"
11
12 #if CONFIG_LAST_SYSTEM_ID < CONFIG_FIRST_SYSTEM_ID
13 #error Bad LAST_SYSTEM_ID or FIRST_SYSTEM_ID in .config
14 #endif
15
16 /* #define OPT_HOME           (1 << 0) */ /* unused */
17 /* #define OPT_GECOS          (1 << 1) */ /* unused */
18 #define OPT_SHELL          (1 << 2)
19 #define OPT_GID            (1 << 3)
20 #define OPT_DONT_SET_PASS  (1 << 4)
21 #define OPT_SYSTEM_ACCOUNT (1 << 5)
22 #define OPT_DONT_MAKE_HOME (1 << 6)
23 #define OPT_UID            (1 << 7)
24
25 /* We assume UID_T_MAX == INT_MAX */
26 /* remix */
27 /* recoded such that the uid may be passed in *p */
28 static void passwd_study(struct passwd *p)
29 {
30         int max = UINT_MAX;
31
32         if (getpwnam(p->pw_name)) {
33                 bb_error_msg_and_die("%s '%s' in use", "user", p->pw_name);
34                 /* this format string is reused in adduser and addgroup */
35         }
36
37         if (!(option_mask32 & OPT_UID)) {
38                 if (option_mask32 & OPT_SYSTEM_ACCOUNT) {
39                         p->pw_uid = CONFIG_FIRST_SYSTEM_ID;
40                         max = CONFIG_LAST_SYSTEM_ID;
41                 } else {
42                         p->pw_uid = CONFIG_LAST_SYSTEM_ID + 1;
43                         max = 64999;
44                 }
45         }
46         /* check for a free uid (and maybe gid) */
47         while (getpwuid(p->pw_uid) || (p->pw_gid == (gid_t)-1 && getgrgid(p->pw_uid))) {
48                 if (option_mask32 & OPT_UID) {
49                         /* -u N, cannot pick uid other than N: error */
50                         bb_error_msg_and_die("%s '%s' in use", "uid", itoa(p->pw_uid));
51                         /* this format string is reused in adduser and addgroup */
52                 }
53                 if (p->pw_uid == max) {
54                         bb_error_msg_and_die("no %cids left", 'u');
55                 }
56                 p->pw_uid++;
57         }
58
59         if (p->pw_gid == (gid_t)-1) {
60                 p->pw_gid = p->pw_uid; /* new gid = uid */
61                 if (getgrnam(p->pw_name)) {
62                         bb_error_msg_and_die("%s '%s' in use", "group", p->pw_name);
63                         /* this format string is reused in adduser and addgroup */
64                 }
65         }
66 }
67
68 static void addgroup_wrapper(struct passwd *p, const char *group_name)
69 {
70         char *argv[5];
71
72         argv[0] = (char*)"addgroup";
73         if (group_name) {
74                 /* Add user to existing group */
75                 argv[1] = p->pw_name;
76                 argv[2] = (char*)group_name;
77                 argv[3] = NULL;
78         } else {
79                 /* Add user to his own group with the first free gid found in passwd_study */
80 //TODO: to be compatible with external addgroup programs we should use --gid instead...
81                 argv[1] = (char*)"-g";
82                 argv[2] = utoa(p->pw_gid);
83                 argv[3] = p->pw_name;
84                 argv[4] = NULL;
85         }
86
87         spawn_and_wait(argv);
88 }
89
90 static void passwd_wrapper(const char *login_name) NORETURN;
91
92 static void passwd_wrapper(const char *login_name)
93 {
94         BB_EXECLP("passwd", "passwd", login_name, NULL);
95         bb_error_msg_and_die("can't execute passwd, you must set password manually");
96 }
97
98 #if ENABLE_FEATURE_ADDUSER_LONG_OPTIONS
99 static const char adduser_longopts[] ALIGN1 =
100                 "home\0"                Required_argument "h"
101                 "gecos\0"               Required_argument "g"
102                 "shell\0"               Required_argument "s"
103                 "ingroup\0"             Required_argument "G"
104                 "disabled-password\0"   No_argument       "D"
105                 "empty-password\0"      No_argument       "D"
106                 "system\0"              No_argument       "S"
107                 "no-create-home\0"      No_argument       "H"
108                 "uid\0"                 Required_argument "u"
109                 ;
110 #endif
111
112 /*
113  * adduser will take a login_name as its first parameter.
114  * home, shell, gecos:
115  * can be customized via command-line parameters.
116  */
117 int adduser_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
118 int adduser_main(int argc UNUSED_PARAM, char **argv)
119 {
120         struct passwd pw;
121         const char *usegroup = NULL;
122         char *p;
123         unsigned opts;
124
125 #if ENABLE_FEATURE_ADDUSER_LONG_OPTIONS
126         applet_long_options = adduser_longopts;
127 #endif
128
129         /* got root? */
130         if (geteuid()) {
131                 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
132         }
133
134         pw.pw_gecos = (char *)"Linux User,,,";
135         /* We assume that newly created users "inherit" root's shell setting */
136         pw.pw_shell = (char *)get_shell_name();
137         pw.pw_dir = NULL;
138
139         /* exactly one non-option arg */
140         /* disable interactive passwd for system accounts */
141         opt_complementary = "=1:SD:u+";
142         if (sizeof(pw.pw_uid) == sizeof(int)) {
143                 opts = getopt32(argv, "h:g:s:G:DSHu:", &pw.pw_dir, &pw.pw_gecos, &pw.pw_shell, &usegroup, &pw.pw_uid);
144         } else {
145                 unsigned uid;
146                 opts = getopt32(argv, "h:g:s:G:DSHu:", &pw.pw_dir, &pw.pw_gecos, &pw.pw_shell, &usegroup, &uid);
147                 if (opts & OPT_UID) {
148                         pw.pw_uid = uid;
149                 }
150         }
151         argv += optind;
152
153         /* fill in the passwd struct */
154         pw.pw_name = argv[0];
155         die_if_bad_username(pw.pw_name);
156         if (!pw.pw_dir) {
157                 /* create string for $HOME if not specified already */
158                 pw.pw_dir = xasprintf("/home/%s", argv[0]);
159         }
160         pw.pw_passwd = (char *)"x";
161         if (opts & OPT_SYSTEM_ACCOUNT) {
162                 if (!usegroup) {
163                         usegroup = "nogroup";
164                 }
165                 if (!(opts & OPT_SHELL)) {
166                         pw.pw_shell = (char *) "/bin/false";
167                 }
168         }
169         pw.pw_gid = usegroup ? xgroup2gid(usegroup) : -1; /* exits on failure */
170
171         /* make sure everything is kosher and setup uid && maybe gid */
172         passwd_study(&pw);
173
174         p = xasprintf("x:%u:%u:%s:%s:%s",
175                         (unsigned) pw.pw_uid, (unsigned) pw.pw_gid,
176                         pw.pw_gecos, pw.pw_dir, pw.pw_shell);
177         if (update_passwd(bb_path_passwd_file, pw.pw_name, p, NULL) < 0) {
178                 return EXIT_FAILURE;
179         }
180         if (ENABLE_FEATURE_CLEAN_UP)
181                 free(p);
182
183 #if ENABLE_FEATURE_SHADOWPASSWDS
184         /* /etc/shadow fields:
185          * 1. username
186          * 2. encrypted password
187          * 3. last password change (unix date (unix time/24*60*60))
188          * 4. minimum days required between password changes
189          * 5. maximum days password is valid
190          * 6. days before password is to expire that user is warned
191          * 7. days after password expires that account is disabled
192          * 8. unix date when login expires (i.e. when it may no longer be used)
193          */
194         /* fields:     2 3  4 5     6 78 */
195         p = xasprintf("!:%u:0:99999:7:::", (unsigned)(time(NULL)) / (24*60*60));
196         /* ignore errors: if file is missing we suppose admin doesn't want it */
197         update_passwd(bb_path_shadow_file, pw.pw_name, p, NULL);
198         if (ENABLE_FEATURE_CLEAN_UP)
199                 free(p);
200 #endif
201
202         /* add to group */
203         addgroup_wrapper(&pw, usegroup);
204
205         /* clear the umask for this process so it doesn't
206          * screw up the permissions on the mkdir and chown. */
207         umask(0);
208         if (!(opts & OPT_DONT_MAKE_HOME)) {
209                 /* set the owner and group so it is owned by the new user,
210                  * then fix up the permissions to 2755. Can't do it before
211                  * since chown will clear the setgid bit */
212                 int mkdir_err = mkdir(pw.pw_dir, 0755);
213                 if (mkdir_err == 0) {
214                         /* New home. Copy /etc/skel to it */
215                         const char *args[] = {
216                                 "chown", "-R",
217                                 xasprintf("%u:%u", (int)pw.pw_uid, (int)pw.pw_gid),
218                                 pw.pw_dir, NULL
219                         };
220                         /* Be silent on any errors (like: no /etc/skel) */
221                         logmode = LOGMODE_NONE;
222                         copy_file("/etc/skel", pw.pw_dir, FILEUTILS_RECUR);
223                         logmode = LOGMODE_STDIO;
224                         chown_main(4, (char**)args);
225                 }
226                 if ((mkdir_err != 0 && errno != EEXIST)
227                  || chown(pw.pw_dir, pw.pw_uid, pw.pw_gid) != 0
228                  || chmod(pw.pw_dir, 02755) != 0 /* set setgid bit on homedir */
229                 ) {
230                         bb_simple_perror_msg(pw.pw_dir);
231                 }
232         }
233
234         if (!(opts & OPT_DONT_SET_PASS)) {
235                 /* interactively set passwd */
236                 passwd_wrapper(pw.pw_name);
237         }
238
239         return EXIT_SUCCESS;
240 }