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 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
12 #include <sys/types.h>
21 #define DONT_SET_PASS (1 << 4)
22 #define DONT_MAKE_HOME (1 << 6)
26 /* EDR recoded such that the uid may be passed in *p */
27 static int passwd_study(const char *filename, struct passwd *p)
33 const int max = 65000;
35 passwd = bb_xfopen(filename, "r");
37 /* EDR if uid is out of bounds, set to min */
38 if ((p->pw_uid > max) || (p->pw_uid < min))
42 * make sure login isn't taken;
43 * find free uid and gid;
45 while ((pw = fgetpwent(passwd))) {
46 if (strcmp(pw->pw_name, p->pw_name) == 0) {
50 if ((pw->pw_uid >= p->pw_uid) && (pw->pw_uid < max)
51 && (pw->pw_uid >= min)) {
52 p->pw_uid = pw->pw_uid + 1;
57 /* EDR check for an already existing gid */
58 while (getgrgid(p->pw_uid) != NULL)
61 /* EDR also check for an existing group definition */
62 if (getgrnam(p->pw_name) != NULL)
65 /* EDR create new gid always = uid */
66 p->pw_gid = p->pw_uid;
69 /* EDR bounds check */
70 if ((p->pw_uid > max) || (p->pw_uid < min))
77 static void addgroup_wrapper(struct passwd *p)
81 cmd = bb_xasprintf("addgroup -g %d \"%s\"", p->pw_gid, p->pw_name);
86 static void passwd_wrapper(const char *login) ATTRIBUTE_NORETURN;
88 static void passwd_wrapper(const char *login)
90 static const char prog[] = "passwd";
91 execlp(prog, prog, login, NULL);
92 bb_error_msg_and_die("Failed to execute '%s', you must set the password for '%s' manually", prog, login);
95 /* putpwent(3) remix */
96 static int adduser(struct passwd *p, unsigned long flags)
99 int addgroup = !p->pw_gid;
101 /* make sure everything is kosher and setup uid && gid */
102 file = bb_xfopen(bb_path_passwd_file, "a");
103 fseek(file, 0, SEEK_END);
105 switch (passwd_study(bb_path_passwd_file, p)) {
107 bb_error_msg_and_die("%s: login already in use", p->pw_name);
109 bb_error_msg_and_die("illegal uid or no uids left");
111 bb_error_msg_and_die("%s: group name already in use", p->pw_name);
115 if (putpwent(p, file) == -1) {
116 bb_perror_nomsg_and_die();
120 #if ENABLE_FEATURE_SHADOWPASSWDS
121 /* add to shadow if necessary */
122 file = bb_xfopen(bb_path_shadow_file, "a");
123 fseek(file, 0, SEEK_END);
124 fprintf(file, "%s:!:%ld:%d:%d:%d:::\n",
125 p->pw_name, /* username */
126 time(NULL) / 86400, /* sp->sp_lstchg */
128 99999, /* sp->sp_max */
129 7); /* sp->sp_warn */
134 /* addgroup should be responsible for dealing w/ gshadow */
135 /* if using a pre-existing group, don't create one */
136 if (addgroup) addgroup_wrapper(p);
138 /* Clear the umask for this process so it doesn't
139 * * screw up the permissions on the mkdir and chown. */
141 if (!(flags & DONT_MAKE_HOME)) {
142 /* Set the owner and group so it is owned by the new user,
143 then fix up the permissions to 2755. Can't do it before
144 since chown will clear the setgid bit */
145 if (mkdir(p->pw_dir, 0755)
146 || chown(p->pw_dir, p->pw_uid, p->pw_gid)
147 || chmod(p->pw_dir, 02755)) {
148 bb_perror_msg("%s", p->pw_dir);
152 if (!(flags & DONT_SET_PASS)) {
153 /* interactively set passwd */
154 passwd_wrapper(p->pw_name);
161 * adduser will take a login_name as its first parameter.
167 * can be customized via command-line parameters.
168 * ________________________________________________________________________ */
169 int adduser_main(int argc, char **argv)
172 const char *usegroup = NULL;
175 pw.pw_gecos = "Linux User,,,";
176 pw.pw_shell = (char *)DEFAULT_SHELL;
179 /* check for min, max and missing args and exit on error */
180 bb_opt_complementally = "-1:?1:?";
181 flags = bb_getopt_ulflags(argc, argv, "h:g:s:G:DSH", &pw.pw_dir, &pw.pw_gecos, &pw.pw_shell, &usegroup);
185 bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
188 /* create string for $HOME if not specified already */
190 snprintf(bb_common_bufsiz1, BUFSIZ, "/home/%s", argv[optind]);
191 pw.pw_dir = &bb_common_bufsiz1[0];
194 /* create a passwd struct */
195 pw.pw_name = argv[optind];
198 pw.pw_gid = (usegroup) ? bb_xgetgrnam(usegroup) : 0; /* exits on failure */
201 return adduser(&pw, flags);