Port over the last of the tinylogin applets
[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  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  *
22  */
23
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <time.h>
31 #include <unistd.h>
32 #include <sys/param.h>
33 #include <sys/stat.h>
34 #include <sys/types.h>
35 #include "busybox.h"
36
37
38
39 /* structs __________________________ */
40
41 typedef struct {
42         uid_t u;
43         gid_t g;
44 } Id;
45
46 /* data _____________________________ */
47
48 /* defaults : should this be in an external file? */
49 static const char default_passwd[] = "x";
50 static const char default_gecos[] = "Linux User,,,";
51 static const char default_home_prefix[] = "/home";
52 static const char default_shell[] = "/bin/sh";
53
54 #ifdef CONFIG_FEATURE_SHADOWPASSWDS
55 /* shadow in use? */
56 static int shadow_enabled = 0;
57 #endif
58
59 /* remix */
60 /* EDR recoded such that the uid may be passed in *p */
61 static int passwd_study(const char *filename, struct passwd *p)
62 {
63         struct passwd *pw;
64         FILE *passwd;
65
66         const int min = 500;
67         const int max = 65000;
68
69         passwd = wfopen(filename, "r");
70         if (!passwd)
71                 return 4;
72
73         /* EDR if uid is out of bounds, set to min */
74         if ((p->pw_uid > max) || (p->pw_uid < min))
75                 p->pw_uid = min;
76
77         /* stuff to do:  
78          * make sure login isn't taken;
79          * find free uid and gid;
80          */
81         while ((pw = fgetpwent(passwd))) {
82                 if (strcmp(pw->pw_name, p->pw_name) == 0) {
83                         /* return 0; */
84                         return 1;
85                 }
86                 if ((pw->pw_uid >= p->pw_uid) && (pw->pw_uid < max)
87                         && (pw->pw_uid >= min)) {
88                         p->pw_uid = pw->pw_uid + 1;
89                 }
90         }
91
92         /* EDR check for an already existing gid */
93         while (getgrgid(p->pw_uid) != NULL)
94                 p->pw_uid++;
95
96         /* EDR also check for an existing group definition */
97         if (getgrnam(p->pw_name) != NULL)
98                 return 3;
99
100         /* EDR bounds check */
101         if ((p->pw_uid > max) || (p->pw_uid < min))
102                 return 2;
103
104         /* EDR create new gid always = uid */
105         p->pw_gid = p->pw_uid;
106
107         /* return 1; */
108         return 0;
109 }
110
111 static void addgroup_wrapper(const char *login, gid_t gid)
112 {
113         int argc = 3;
114         const char *argv0_save;
115         char group_id[8];
116         char group_name[32];
117         char *argv[] = { group_name, "-g", group_id };
118
119         argv0_save = applet_name;
120         applet_name = "addgroup";
121         safe_strncpy(group_name, login, 32);
122         sprintf(group_id, "%d", gid);
123         addgroup_main(argc, argv);
124         applet_name = argv0_save;
125 }
126
127 static void passwd_wrapper(const char *login)
128 {
129         static const char prog[] = "passwd";
130         execlp(prog, prog, login, NULL);
131         error_msg_and_die("Failed to execute 'passwd', you must set the password for '%s' manually", login);
132 }
133
134 /* putpwent(3) remix */
135 static int adduser(const char *filename, struct passwd *p)
136 {
137         FILE *passwd;
138         int r;
139 #ifdef CONFIG_FEATURE_SHADOWPASSWDS
140         FILE *shadow;
141         struct spwd *sp;
142 #endif
143
144         /* make sure everything is kosher and setup uid && gid */
145         passwd = wfopen(filename, "a");
146         if (passwd == NULL) {
147                 /* return -1; */
148                 return 1;
149         }
150         fseek(passwd, 0, SEEK_END);
151
152         /* if (passwd_study(filename, p) == 0) { */
153         r = passwd_study(filename, p);
154         if (r) {
155                 if (r == 1)
156                         error_msg("%s: login already in use", p->pw_name);
157                 else if (r == 2)
158                         error_msg("illegal uid or no uids left");
159                 else if (r == 3)
160                         error_msg("group name %s already in use", p->pw_name);
161                 else
162                         error_msg("generic error.");
163                 /* return -1; */
164                 return 1;
165         }
166
167         /* add to passwd */
168         if (putpwent(p, passwd) == -1) {
169                 /* return -1; */
170                 return 1;
171         }
172         fclose(passwd);
173
174 #ifdef CONFIG_FEATURE_SHADOWPASSWDS
175         /* add to shadow if necessary */
176         if (shadow_enabled) {
177                 shadow = wfopen(shadow_file, "a");
178                 if (shadow == NULL) {
179                         /* return -1; */
180                         return 1;
181                 }
182                 fseek(shadow, 0, SEEK_END);
183                 sp = pwd_to_spwd(p);
184                 sp->sp_max = 99999;             /* debianish */
185                 sp->sp_warn = 7;
186                 fprintf(shadow, "%s:!:%ld:%ld:%ld:%ld:::\n",
187                                 sp->sp_namp, sp->sp_lstchg, sp->sp_min, sp->sp_max,
188                                 sp->sp_warn);
189                 fclose(shadow);
190         }
191 #endif
192
193         /* add to group */
194         /* addgroup should be responsible for dealing w/ gshadow */
195         addgroup_wrapper(p->pw_name, p->pw_gid);
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
201         /* mkdir */
202         if (mkdir(p->pw_dir, 0755)) {
203                 perror_msg("%s", p->pw_dir);
204         }
205         /* Set the owner and group so it is owned by the new user. */
206         if (chown(p->pw_dir, p->pw_uid, p->pw_gid)) {
207                 perror_msg("%s", p->pw_dir);
208         }
209         /* Now fix up the permissions to 2755. Can't do it before now
210          * since chown will clear the setgid bit */
211         if (chmod(p->pw_dir, 02755)) {
212                 perror_msg("%s", p->pw_dir);
213         }
214         /* interactively set passwd */
215         passwd_wrapper(p->pw_name);
216
217         return 0;
218 }
219
220
221 /* return current uid (root is always uid == 0, right?) */
222 static inline uid_t i_am_not_root(void)
223 {
224         return geteuid();
225 }
226
227 /*
228  * adduser will take a login_name as its first parameter.
229  *
230  * home
231  * shell
232  * gecos 
233  *
234  * can be customized via command-line parameters.
235  * ________________________________________________________________________ */
236 int adduser_main(int argc, char **argv)
237 {
238         int i = 0;
239         char opt;
240         const char *login;
241         const char *gecos;
242         const char *home = NULL;
243         const char *shell;
244
245         struct passwd pw;
246
247         /* init */
248         if (argc < 2) {
249                 show_usage();
250         }
251         gecos = default_gecos;
252         shell = default_shell;
253
254         /* get args */
255         while ((opt = getopt (argc, argv, "h:g:s:")) != -1)
256                 switch (opt) {
257                         case 'h':
258                                 home = argv[++i];
259                                 break;
260                         case 'g':
261                                 gecos = argv[++i];
262                                 break;
263                         case 's':
264                                 shell = argv[++i];
265                                 break;
266                         default:
267                                 show_usage ();
268                                 break;
269                 }
270
271         /* got root? */
272         if (i_am_not_root()) {
273                 error_msg_and_die( "Only root may add a user or group to the system.");
274         }
275
276         /* get login */
277         if (optind >= argc) {
278                 error_msg_and_die( "no user specified");
279         }
280         login = argv[optind];
281
282         /* create string for $HOME if not specified already */
283         if (!home) {
284                 home = concat_path_file(default_home_prefix, login);
285         }
286 #ifdef CONFIG_FEATURE_SHADOWPASSWDS
287         /* is /etc/shadow in use? */
288         shadow_enabled = (0 == access(shadow_file, F_OK));
289 #endif
290
291         /* create a passwd struct */
292         pw.pw_name = (char *)login;
293         pw.pw_passwd = (char *)default_passwd;
294         pw.pw_uid = 0;
295         pw.pw_gid = 0;
296         pw.pw_gecos = (char *)gecos;
297         pw.pw_dir = (char *)home;
298         pw.pw_shell = (char *)shell;
299
300         /* grand finale */
301         return adduser(passwd_file, &pw);
302 }
303
304 /* $Id: adduser.c,v 1.2 2002/06/23 04:24:24 andersen Exp $ */