adduser: close /etc/{passwd,shadow} before calling passwd etc.
[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 #include "libbb.h"
12
13 #define OPT_DONT_SET_PASS  (1 << 4)
14 #define OPT_DONT_MAKE_HOME (1 << 6)
15
16
17 /* remix */
18 /* EDR recoded such that the uid may be passed in *p */
19 static int passwd_study(const char *filename, struct passwd *p)
20 {
21         enum { min = 500, max = 65000 };
22         FILE *passwd;
23         /* We are using reentrant fgetpwent_r() in order to avoid
24          * pulling in static buffers from libc (think static build here) */
25         char buffer[256];
26         struct passwd pw;
27         struct passwd *result;
28
29         passwd = xfopen(filename, "r");
30
31         /* EDR if uid is out of bounds, set to min */
32         if ((p->pw_uid > max) || (p->pw_uid < min))
33                 p->pw_uid = min;
34
35         /* stuff to do:
36          * make sure login isn't taken;
37          * find free uid and gid;
38          */
39         while (!fgetpwent_r(passwd, &pw, buffer, sizeof(buffer), &result)) {
40                 if (strcmp(pw.pw_name, p->pw_name) == 0) {
41                         /* return 0; */
42                         return 1;
43                 }
44                 if ((pw.pw_uid >= p->pw_uid) && (pw.pw_uid < max)
45                         && (pw.pw_uid >= min)) {
46                         p->pw_uid = pw.pw_uid + 1;
47                 }
48         }
49
50         if (p->pw_gid == 0) {
51                 /* EDR check for an already existing gid */
52                 while (getgrgid(p->pw_uid) != NULL)
53                         p->pw_uid++;
54
55                 /* EDR also check for an existing group definition */
56                 if (getgrnam(p->pw_name) != NULL)
57                         return 3;
58
59                 /* EDR create new gid always = uid */
60                 p->pw_gid = p->pw_uid;
61         }
62
63         /* EDR bounds check */
64         if ((p->pw_uid > max) || (p->pw_uid < min))
65                 return 2;
66
67         /* return 1; */
68         return 0;
69 }
70
71 static void addgroup_wrapper(struct passwd *p)
72 {
73         char *cmd;
74
75         cmd = xasprintf("addgroup -g %d \"%s\"", p->pw_gid, p->pw_name);
76         system(cmd);
77         free(cmd);
78 }
79
80 static void passwd_wrapper(const char *login) ATTRIBUTE_NORETURN;
81
82 static void passwd_wrapper(const char *login)
83 {
84         static const char prog[] = "passwd";
85         BB_EXECLP(prog, prog, login, NULL);
86         bb_error_msg_and_die("failed to execute '%s', you must set the password for '%s' manually", prog, login);
87 }
88
89 /* putpwent(3) remix */
90 static int adduser(struct passwd *p)
91 {
92         FILE *file;
93         int addgroup = !p->pw_gid;
94
95         /* make sure everything is kosher and setup uid && gid */
96         file = xfopen(bb_path_passwd_file, "a");
97         fseek(file, 0, SEEK_END);
98
99         switch (passwd_study(bb_path_passwd_file, p)) {
100                 case 1:
101                         bb_error_msg_and_die("%s: login already in use", p->pw_name);
102                 case 2:
103                         bb_error_msg_and_die("illegal uid or no uids left");
104                 case 3:
105                         bb_error_msg_and_die("%s: group name already in use", p->pw_name);
106         }
107
108         /* add to passwd */
109         if (putpwent(p, file) == -1) {
110                 bb_perror_nomsg_and_die();
111         }
112         /* Do fclose even if !ENABLE_FEATURE_CLEAN_UP.
113          * We will exec passwd, files must be flushed & closed before that! */
114         fclose(file);
115
116 #if ENABLE_FEATURE_SHADOWPASSWDS
117         /* add to shadow if necessary */
118         file = fopen_or_warn(bb_path_shadow_file, "a");
119         if (file) {
120                 fseek(file, 0, SEEK_END);
121                 fprintf(file, "%s:!:%ld:%d:%d:%d:::\n",
122                                 p->pw_name,             /* username */
123                                 time(NULL) / 86400,     /* sp->sp_lstchg */
124                                 0,                      /* sp->sp_min */
125                                 99999,                  /* sp->sp_max */
126                                 7);                     /* sp->sp_warn */
127                 fclose(file);
128         }
129 #endif
130
131         /* add to group */
132         /* addgroup should be responsible for dealing w/ gshadow */
133         /* if using a pre-existing group, don't create one */
134         if (addgroup) addgroup_wrapper(p);
135
136         /* Clear the umask for this process so it doesn't
137          * screw up the permissions on the mkdir and chown. */
138         umask(0);
139         if (!(option_mask32 & OPT_DONT_MAKE_HOME)) {
140                 /* Set the owner and group so it is owned by the new user,
141                    then fix up the permissions to 2755. Can't do it before
142                    since chown will clear the setgid bit */
143                 if (mkdir(p->pw_dir, 0755)
144                 || chown(p->pw_dir, p->pw_uid, p->pw_gid)
145                 || chmod(p->pw_dir, 02755)) {
146                         bb_perror_msg("%s", p->pw_dir);
147                 }
148         }
149
150         if (!(option_mask32 & OPT_DONT_SET_PASS)) {
151                 /* interactively set passwd */
152                 passwd_wrapper(p->pw_name);
153         }
154
155         return 0;
156 }
157
158 /*
159  * adduser will take a login_name as its first parameter.
160  *
161  * home
162  * shell
163  * gecos
164  *
165  * can be customized via command-line parameters.
166  */
167 int adduser_main(int argc, char **argv);
168 int adduser_main(int argc, char **argv)
169 {
170         struct passwd pw;
171         const char *usegroup = NULL;
172
173         /* got root? */
174         if (geteuid()) {
175                 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
176         }
177
178         pw.pw_gecos = (char *)"Linux User,,,";
179         pw.pw_shell = (char *)DEFAULT_SHELL;
180         pw.pw_dir = NULL;
181
182         /* check for min, max and missing args and exit on error */
183         opt_complementary = "-1:?1:?";
184         getopt32(argc, argv, "h:g:s:G:DSH", &pw.pw_dir, &pw.pw_gecos, &pw.pw_shell, &usegroup);
185         argv += optind;
186
187         /* create a passwd struct */
188         pw.pw_name = argv[0];
189         if (!pw.pw_dir) {
190                 /* create string for $HOME if not specified already */
191                 pw.pw_dir = xasprintf("/home/%s", argv[0]);
192         }
193         pw.pw_passwd = (char *)"x";
194         pw.pw_uid = 0;
195         pw.pw_gid = usegroup ? xgroup2gid(usegroup) : 0; /* exits on failure */
196
197         /* grand finale */
198         return adduser(&pw);
199 }