5f593ac165c28eecc4b306c7a532cb7057c2395c
[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 the GPL v2 or later, see the file LICENSE in this tarball.
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 *cmd;
71
72         cmd = xasprintf("addgroup '%s' '%s'",
73                         p->pw_name, group_name);
74         system(cmd);
75         free(cmd);
76 }
77
78 static void passwd_wrapper(const char *login) NORETURN;
79
80 static void passwd_wrapper(const char *login)
81 {
82         static const char prog[] ALIGN1 = "passwd";
83
84         BB_EXECLP(prog, prog, login, NULL);
85         bb_error_msg_and_die("can't execute %s, you must set password manually", prog);
86 }
87
88 #if ENABLE_FEATURE_ADDUSER_LONG_OPTIONS
89 static const char adduser_longopts[] ALIGN1 =
90                 "home\0"                Required_argument "h"
91                 "gecos\0"               Required_argument "g"
92                 "shell\0"               Required_argument "s"
93                 "ingroup\0"             Required_argument "G"
94                 "disabled-password\0"   No_argument       "D"
95                 "empty-password\0"      No_argument       "D"
96                 "system\0"              No_argument       "S"
97                 "no-create-home\0"      No_argument       "H"
98                 "uid\0"                 Required_argument "u"
99                 ;
100 #endif
101
102 /*
103  * adduser will take a login_name as its first parameter.
104  * home, shell, gecos:
105  * can be customized via command-line parameters.
106  */
107 int adduser_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
108 int adduser_main(int argc UNUSED_PARAM, char **argv)
109 {
110         struct passwd pw;
111         const char *usegroup = NULL;
112         char *p;
113         unsigned opts;
114
115 #if ENABLE_FEATURE_ADDUSER_LONG_OPTIONS
116         applet_long_options = adduser_longopts;
117 #endif
118
119         /* got root? */
120         if (geteuid()) {
121                 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
122         }
123
124         pw.pw_gecos = (char *)"Linux User,,,";
125         pw.pw_shell = (char *)DEFAULT_SHELL;
126         pw.pw_dir = NULL;
127
128         /* exactly one non-option arg */
129         /* disable interactive passwd for system accounts */
130         opt_complementary = "=1:SD:u+";
131         if (sizeof(pw.pw_uid) == sizeof(int)) {
132                 opts = getopt32(argv, "h:g:s:G:DSHu:", &pw.pw_dir, &pw.pw_gecos, &pw.pw_shell, &usegroup, &pw.pw_uid);
133         } else {
134                 unsigned uid;
135                 opts = getopt32(argv, "h:g:s:G:DSHu:", &pw.pw_dir, &pw.pw_gecos, &pw.pw_shell, &usegroup, &uid);
136                 if (opts & OPT_UID) {
137                         pw.pw_uid = uid;
138                 }
139         }
140         argv += optind;
141
142         /* fill in the passwd struct */
143         pw.pw_name = argv[0];
144         die_if_bad_username(pw.pw_name);
145         if (!pw.pw_dir) {
146                 /* create string for $HOME if not specified already */
147                 pw.pw_dir = xasprintf("/home/%s", argv[0]);
148         }
149         pw.pw_passwd = (char *)"x";
150         if (opts & OPT_SYSTEM_ACCOUNT) {
151                 if (!usegroup) {
152                         usegroup = "nogroup";
153                 }
154                 if (!(opts & OPT_SHELL)) {
155                         pw.pw_shell = (char *) "/bin/false";
156                 }
157         }
158         pw.pw_gid = usegroup ? xgroup2gid(usegroup) : -1; /* exits on failure */
159
160         /* make sure everything is kosher and setup uid && maybe gid */
161         passwd_study(&pw);
162
163         p = xasprintf("x:%u:%u:%s:%s:%s",
164                         (unsigned) pw.pw_uid, (unsigned) pw.pw_gid,
165                         pw.pw_gecos, pw.pw_dir, pw.pw_shell);
166         if (update_passwd(bb_path_passwd_file, pw.pw_name, p, NULL) < 0) {
167                 return EXIT_FAILURE;
168         }
169         if (ENABLE_FEATURE_CLEAN_UP)
170                 free(p);
171
172 #if ENABLE_FEATURE_SHADOWPASSWDS
173         /* /etc/shadow fields:
174          * 1. username
175          * 2. encrypted password
176          * 3. last password change (unix date (unix time/24*60*60))
177          * 4. minimum days required between password changes
178          * 5. maximum days password is valid
179          * 6. days before password is to expire that user is warned
180          * 7. days after password expires that account is disabled
181          * 8. unix date when login expires (i.e. when it may no longer be used)
182          */
183         /* fields:     2 3  4 5     6 78 */
184         p = xasprintf("!:%u:0:99999:7:::", (unsigned)(time(NULL)) / (24*60*60));
185         /* ignore errors: if file is missing we suppose admin doesn't want it */
186         update_passwd(bb_path_shadow_file, pw.pw_name, p, NULL);
187         if (ENABLE_FEATURE_CLEAN_UP)
188                 free(p);
189 #endif
190
191         /* add to group */
192         /* addgroup should be responsible for dealing w/ gshadow */
193         /* if using a pre-existing group, don't create one */
194         if (usegroup)
195                 addgroup_wrapper(&pw, usegroup);
196
197         /* clear the umask for this process so it doesn't
198          * screw up the permissions on the mkdir and chown. */
199         umask(0);
200         if (!(opts & OPT_DONT_MAKE_HOME)) {
201                 /* set the owner and group so it is owned by the new user,
202                  * then fix up the permissions to 2755. Can't do it before
203                  * since chown will clear the setgid bit */
204                 if (mkdir(pw.pw_dir, 0755)
205                  || chown(pw.pw_dir, pw.pw_uid, pw.pw_gid)
206                  || chmod(pw.pw_dir, 02755) /* set setgid bit on homedir */
207                 ) {
208                         bb_simple_perror_msg(pw.pw_dir);
209                 }
210         }
211
212         if (!(opts & OPT_DONT_SET_PASS)) {
213                 /* interactively set passwd */
214                 passwd_wrapper(pw.pw_name);
215         }
216
217         return 0;
218 }