1 /* vi: set sw=4 ts=4: */
3 * adduser - add users to /etc/passwd and /etc/shadow
5 * Copyright (C) 1999 by Lineo, inc. and John Beppu
6 * Copyright (C) 1999,2000,2001 by John Beppu <beppu@codepoet.org>
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.
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.
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
36 #include <sys/param.h>
38 #include <sys/types.h>
43 /* structs __________________________ */
50 /* data _____________________________ */
52 /* defaults : should this be in an external file? */
53 static const char default_passwd[] = "x";
54 static const char default_gecos[] = "Linux User,,,";
55 static const char default_home_prefix[] = "/home";
56 static const char default_shell[] = "/bin/sh";
58 #ifdef CONFIG_FEATURE_SHADOWPASSWDS
60 static int shadow_enabled = 0;
64 /* EDR recoded such that the uid may be passed in *p */
65 static int passwd_study(const char *filename, struct passwd *p)
71 const int max = 65000;
73 passwd = bb_wfopen(filename, "r");
77 /* EDR if uid is out of bounds, set to min */
78 if ((p->pw_uid > max) || (p->pw_uid < min))
82 * make sure login isn't taken;
83 * find free uid and gid;
85 while ((pw = fgetpwent(passwd))) {
86 if (strcmp(pw->pw_name, p->pw_name) == 0) {
90 if ((pw->pw_uid >= p->pw_uid) && (pw->pw_uid < max)
91 && (pw->pw_uid >= min)) {
92 p->pw_uid = pw->pw_uid + 1;
97 /* EDR check for an already existing gid */
98 while (getgrgid(p->pw_uid) != NULL)
101 /* EDR also check for an existing group definition */
102 if (getgrnam(p->pw_name) != NULL)
105 /* EDR create new gid always = uid */
106 p->pw_gid = p->pw_uid;
109 /* EDR bounds check */
110 if ((p->pw_uid > max) || (p->pw_uid < min))
117 static void addgroup_wrapper(const char *login, gid_t gid)
121 bb_xasprintf(&cmd, "addgroup -g %d %s", gid, login);
126 static void passwd_wrapper(const char *login) __attribute__ ((noreturn));
128 static void passwd_wrapper(const char *login)
130 static const char prog[] = "passwd";
131 execlp(prog, prog, login, NULL);
132 bb_error_msg_and_die("Failed to execute '%s', you must set the password for '%s' manually", prog, login);
135 /* putpwent(3) remix */
136 static int adduser(const char *filename, struct passwd *p, int makehome, int setpass)
140 #ifdef CONFIG_FEATURE_SHADOWPASSWDS
146 /* if using a pre-existing group, don't create one */
150 /* make sure everything is kosher and setup uid && gid */
151 passwd = bb_wfopen(filename, "a");
152 if (passwd == NULL) {
155 fseek(passwd, 0, SEEK_END);
157 /* if (passwd_study(filename, p) == 0) { */
158 r = passwd_study(filename, p);
161 bb_error_msg("%s: login already in use", p->pw_name);
163 bb_error_msg("illegal uid or no uids left");
165 bb_error_msg("group name %s already in use", p->pw_name);
167 bb_error_msg("generic error.");
172 if (putpwent(p, passwd) == -1) {
177 #ifdef CONFIG_FEATURE_SHADOWPASSWDS
178 /* add to shadow if necessary */
179 if (shadow_enabled) {
180 shadow = bb_wfopen(bb_path_shadow_file, "a");
181 if (shadow == NULL) {
184 fseek(shadow, 0, SEEK_END);
186 sp->sp_max = 99999; /* debianish */
188 fprintf(shadow, "%s:!:%ld:%ld:%ld:%ld:::\n",
189 sp->sp_namp, sp->sp_lstchg, sp->sp_min, sp->sp_max,
197 /* addgroup should be responsible for dealing w/ gshadow */
198 addgroup_wrapper(p->pw_name, p->pw_gid);
201 /* Clear the umask for this process so it doesn't
202 * * screw up the permissions on the mkdir and chown. */
207 if (mkdir(p->pw_dir, 0755)) {
208 bb_perror_msg("%s", p->pw_dir);
210 /* Set the owner and group so it is owned by the new user. */
211 if (chown(p->pw_dir, p->pw_uid, p->pw_gid)) {
212 bb_perror_msg("%s", p->pw_dir);
214 /* Now fix up the permissions to 2755. Can't do it before now
215 * since chown will clear the setgid bit */
216 if (chmod(p->pw_dir, 02755)) {
217 bb_perror_msg("%s", p->pw_dir);
222 /* interactively set passwd */
223 passwd_wrapper(p->pw_name);
230 /* return current uid (root is always uid == 0, right?) */
231 #ifndef CONFIG_ADDGROUP
232 static inline void if_i_am_not_root(void)
234 void if_i_am_not_root(void)
238 bb_error_msg_and_die( "Only root may add a user or group to the system.");
246 * adduser will take a login_name as its first parameter.
252 * can be customized via command-line parameters.
253 * ________________________________________________________________________ */
254 int adduser_main(int argc, char **argv)
258 const char *gecos = default_gecos;
259 const char *home = NULL;
260 const char *shell = default_shell;
261 const char *usegroup = NULL;
271 flags = bb_getopt_ulflags(argc, argv, "h:g:s:G:DSH", &home, &gecos, &shell, &usegroup);
273 if (flags & SETPASS) {
276 if (flags & MAKEHOME) {
284 if (optind >= argc) {
285 bb_error_msg_and_die( "no user specified");
287 login = argv[optind];
289 /* create string for $HOME if not specified already */
291 home = concat_path_file(default_home_prefix, login);
293 #ifdef CONFIG_FEATURE_SHADOWPASSWDS
294 /* is /etc/shadow in use? */
295 shadow_enabled = (0 == access(bb_path_shadow_file, F_OK));
298 /* create a passwd struct */
299 pw.pw_name = (char *)login;
300 pw.pw_passwd = (char *)default_passwd;
303 pw.pw_gecos = (char *)gecos;
304 pw.pw_dir = (char *)home;
305 pw.pw_shell = (char *)shell;
308 /* Add user to a group that already exists */
311 g = getgrnam(usegroup);
313 bb_error_msg_and_die("group %s does not exist", usegroup);
315 pw.pw_gid = g->gr_gid;
319 return adduser(bb_path_passwd_file, &pw, makehome, setpass);