- add platform.h.
[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
11 #ifndef _GNU_SOURCE
12 #define _GNU_SOURCE
13 #endif
14 #include <errno.h>
15 #include <fcntl.h>
16 #include <stdarg.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <time.h>
21 #include <unistd.h>
22 #include <getopt.h>
23 #include <sys/param.h>
24 #include <sys/stat.h>
25 #include <sys/types.h>
26 #include "busybox.h"
27
28
29
30 /* structs __________________________ */
31
32 typedef struct {
33         uid_t u;
34         gid_t g;
35 } Id;
36
37 /* data _____________________________ */
38
39 /* defaults : should this be in an external file? */
40 static const char default_passwd[] = "x";
41 static const char default_gecos[] = "Linux User,,,";
42 static const char default_home_prefix[] = "/home";
43
44 #ifdef CONFIG_FEATURE_SHADOWPASSWDS
45 /* shadow in use? */
46 static int shadow_enabled = 0;
47 #endif
48
49 /* remix */
50 /* EDR recoded such that the uid may be passed in *p */
51 static int passwd_study(const char *filename, struct passwd *p)
52 {
53         struct passwd *pw;
54         FILE *passwd;
55
56         const int min = 500;
57         const int max = 65000;
58
59         passwd = bb_wfopen(filename, "r");
60         if (!passwd)
61                 return 4;
62
63         /* EDR if uid is out of bounds, set to min */
64         if ((p->pw_uid > max) || (p->pw_uid < min))
65                 p->pw_uid = min;
66
67         /* stuff to do:
68          * make sure login isn't taken;
69          * find free uid and gid;
70          */
71         while ((pw = fgetpwent(passwd))) {
72                 if (strcmp(pw->pw_name, p->pw_name) == 0) {
73                         /* return 0; */
74                         return 1;
75                 }
76                 if ((pw->pw_uid >= p->pw_uid) && (pw->pw_uid < max)
77                         && (pw->pw_uid >= min)) {
78                         p->pw_uid = pw->pw_uid + 1;
79                 }
80         }
81
82         if (p->pw_gid == 0) {
83                 /* EDR check for an already existing gid */
84                 while (getgrgid(p->pw_uid) != NULL)
85                         p->pw_uid++;
86
87                 /* EDR also check for an existing group definition */
88                 if (getgrnam(p->pw_name) != NULL)
89                         return 3;
90
91                 /* EDR create new gid always = uid */
92                 p->pw_gid = p->pw_uid;
93         }
94
95         /* EDR bounds check */
96         if ((p->pw_uid > max) || (p->pw_uid < min))
97                 return 2;
98
99         /* return 1; */
100         return 0;
101 }
102
103 static void addgroup_wrapper(const char *login, gid_t gid)
104 {
105         char *cmd;
106
107         cmd = bb_xasprintf("addgroup -g %d \"%s\"", gid, login);
108         system(cmd);
109         free(cmd);
110 }
111
112 static void passwd_wrapper(const char *login) ATTRIBUTE_NORETURN;
113
114 static void passwd_wrapper(const char *login)
115 {
116         static const char prog[] = "passwd";
117         execlp(prog, prog, login, NULL);
118         bb_error_msg_and_die("Failed to execute '%s', you must set the password for '%s' manually", prog, login);
119 }
120
121 /* putpwent(3) remix */
122 static int adduser(const char *filename, struct passwd *p, int makehome, int setpass)
123 {
124         FILE *passwd;
125         int r;
126 #ifdef CONFIG_FEATURE_SHADOWPASSWDS
127         FILE *shadow;
128         struct spwd *sp;
129 #endif
130         int new_group = 1;
131
132         /* if using a pre-existing group, don't create one */
133         if (p->pw_gid != 0)
134                 new_group = 0;
135
136         /* make sure everything is kosher and setup uid && gid */
137         passwd = bb_wfopen(filename, "a");
138         if (passwd == NULL) {
139                 return 1;
140         }
141         fseek(passwd, 0, SEEK_END);
142
143         /* if (passwd_study(filename, p) == 0) { */
144         r = passwd_study(filename, p);
145         if (r) {
146                 if (r == 1)
147                         bb_error_msg("%s: login already in use", p->pw_name);
148                 else if (r == 2)
149                         bb_error_msg("illegal uid or no uids left");
150                 else if (r == 3)
151                         bb_error_msg("group name %s already in use", p->pw_name);
152                 else
153                         bb_error_msg("generic error.");
154                 return 1;
155         }
156
157         /* add to passwd */
158         if (putpwent(p, passwd) == -1) {
159                 return 1;
160         }
161         fclose(passwd);
162
163 #ifdef CONFIG_FEATURE_SHADOWPASSWDS
164         /* add to shadow if necessary */
165         if (shadow_enabled) {
166                 shadow = bb_wfopen(bb_path_shadow_file, "a");
167                 if (shadow == NULL) {
168                         return 1;
169                 }
170                 fseek(shadow, 0, SEEK_END);
171                 sp = pwd_to_spwd(p);
172                 sp->sp_max = 99999;             /* debianish */
173                 sp->sp_warn = 7;
174                 fprintf(shadow, "%s:!:%ld:%ld:%ld:%ld:::\n",
175                                 sp->sp_namp, sp->sp_lstchg, sp->sp_min, sp->sp_max,
176                                 sp->sp_warn);
177                 fclose(shadow);
178         }
179 #endif
180
181         if (new_group) {
182                 /* add to group */
183                 /* addgroup should be responsible for dealing w/ gshadow */
184                 addgroup_wrapper(p->pw_name, p->pw_gid);
185         }
186
187         /* Clear the umask for this process so it doesn't
188          * * screw up the permissions on the mkdir and chown. */
189         umask(0);
190
191         if (makehome) {
192                 /* mkdir */
193                 if (mkdir(p->pw_dir, 0755)) {
194                         bb_perror_msg("%s", p->pw_dir);
195                 }
196                 /* Set the owner and group so it is owned by the new user. */
197                 if (chown(p->pw_dir, p->pw_uid, p->pw_gid)) {
198                         bb_perror_msg("%s", p->pw_dir);
199                 }
200                 /* Now fix up the permissions to 2755. Can't do it before now
201                  * since chown will clear the setgid bit */
202                 if (chmod(p->pw_dir, 02755)) {
203                         bb_perror_msg("%s", p->pw_dir);
204                 }
205         }
206
207         if (setpass) {
208                 /* interactively set passwd */
209                 passwd_wrapper(p->pw_name);
210         }
211
212         return 0;
213 }
214
215
216 /* return current uid (root is always uid == 0, right?) */
217 #ifndef CONFIG_ADDGROUP
218 static inline void if_i_am_not_root(void)
219 #else
220 void if_i_am_not_root(void)
221 #endif
222 {
223         if (geteuid()) {
224                 bb_error_msg_and_die( "Only root may add a user or group to the system.");
225         }
226 }
227
228 #define SETPASS                         (1 << 4)
229 #define MAKEHOME                        (1 << 6)
230
231 /*
232  * adduser will take a login_name as its first parameter.
233  *
234  * home
235  * shell
236  * gecos
237  *
238  * can be customized via command-line parameters.
239  * ________________________________________________________________________ */
240 int adduser_main(int argc, char **argv)
241 {
242         struct passwd pw;
243         const char *login;
244         const char *gecos = default_gecos;
245         const char *home = NULL;
246         const char *shell = DEFAULT_SHELL;
247         const char *usegroup = NULL;
248         int flags;
249         int setpass = 1;
250         int makehome = 1;
251
252         /* init */
253         if (argc < 2) {
254                 bb_show_usage();
255         }
256         /* get args */
257         flags = bb_getopt_ulflags(argc, argv, "h:g:s:G:DSH", &home, &gecos, &shell, &usegroup);
258
259         if (flags & SETPASS) {
260                 setpass = 0;
261         }
262         if (flags & MAKEHOME) {
263                 makehome = 0;
264         }
265
266         /* got root? */
267         if_i_am_not_root();
268
269         /* get login */
270         if (optind >= argc) {
271                 bb_error_msg_and_die( "no user specified");
272         }
273         login = argv[optind];
274
275         /* create string for $HOME if not specified already */
276         if (!home) {
277                 home = concat_path_file(default_home_prefix, login);
278         }
279 #ifdef CONFIG_FEATURE_SHADOWPASSWDS
280         /* is /etc/shadow in use? */
281         shadow_enabled = (0 == access(bb_path_shadow_file, F_OK));
282 #endif
283
284         /* create a passwd struct */
285         pw.pw_name = (char *)login;
286         pw.pw_passwd = (char *)default_passwd;
287         pw.pw_uid = 0;
288         pw.pw_gid = 0;
289         pw.pw_gecos = (char *)gecos;
290         pw.pw_dir = (char *)home;
291         pw.pw_shell = (char *)shell;
292
293         if (usegroup) {
294                 /* Add user to a group that already exists */
295                 pw.pw_gid = bb_xgetgrnam(usegroup);
296                 /* exits on error */    
297         }
298
299         /* grand finale */
300         return adduser(bb_path_passwd_file, &pw, makehome, setpass);
301 }