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
32 #include <sys/param.h>
34 #include <sys/types.h>
39 /* structs __________________________ */
46 /* data _____________________________ */
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";
54 #ifdef CONFIG_FEATURE_SHADOWPASSWDS
56 static int shadow_enabled = 0;
60 /* EDR recoded such that the uid may be passed in *p */
61 static int passwd_study(const char *filename, struct passwd *p)
67 const int max = 65000;
69 passwd = wfopen(filename, "r");
73 /* EDR if uid is out of bounds, set to min */
74 if ((p->pw_uid > max) || (p->pw_uid < min))
78 * make sure login isn't taken;
79 * find free uid and gid;
81 while ((pw = fgetpwent(passwd))) {
82 if (strcmp(pw->pw_name, p->pw_name) == 0) {
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;
92 /* EDR check for an already existing gid */
93 while (getgrgid(p->pw_uid) != NULL)
96 /* EDR also check for an existing group definition */
97 if (getgrnam(p->pw_name) != NULL)
100 /* EDR bounds check */
101 if ((p->pw_uid > max) || (p->pw_uid < min))
104 /* EDR create new gid always = uid */
105 p->pw_gid = p->pw_uid;
111 static void addgroup_wrapper(const char *login, gid_t gid)
113 char *cmd = xmalloc(strlen(login)+32);
115 sprintf(cmd, "addgroup -g %d %s", gid, login);
120 static void passwd_wrapper(const char *login)
122 static const char prog[] = "passwd";
123 execlp(prog, prog, login, NULL);
124 error_msg_and_die("Failed to execute 'passwd', you must set the password for '%s' manually", login);
127 /* putpwent(3) remix */
128 static int adduser(const char *filename, struct passwd *p)
132 #ifdef CONFIG_FEATURE_SHADOWPASSWDS
137 /* make sure everything is kosher and setup uid && gid */
138 passwd = wfopen(filename, "a");
139 if (passwd == NULL) {
143 fseek(passwd, 0, SEEK_END);
145 /* if (passwd_study(filename, p) == 0) { */
146 r = passwd_study(filename, p);
149 error_msg("%s: login already in use", p->pw_name);
151 error_msg("illegal uid or no uids left");
153 error_msg("group name %s already in use", p->pw_name);
155 error_msg("generic error.");
161 if (putpwent(p, passwd) == -1) {
167 #ifdef CONFIG_FEATURE_SHADOWPASSWDS
168 /* add to shadow if necessary */
169 if (shadow_enabled) {
170 shadow = wfopen(shadow_file, "a");
171 if (shadow == NULL) {
175 fseek(shadow, 0, SEEK_END);
177 sp->sp_max = 99999; /* debianish */
179 fprintf(shadow, "%s:!:%ld:%ld:%ld:%ld:::\n",
180 sp->sp_namp, sp->sp_lstchg, sp->sp_min, sp->sp_max,
187 /* addgroup should be responsible for dealing w/ gshadow */
188 addgroup_wrapper(p->pw_name, p->pw_gid);
190 /* Clear the umask for this process so it doesn't
191 * * screw up the permissions on the mkdir and chown. */
195 if (mkdir(p->pw_dir, 0755)) {
196 perror_msg("%s", p->pw_dir);
198 /* Set the owner and group so it is owned by the new user. */
199 if (chown(p->pw_dir, p->pw_uid, p->pw_gid)) {
200 perror_msg("%s", p->pw_dir);
202 /* Now fix up the permissions to 2755. Can't do it before now
203 * since chown will clear the setgid bit */
204 if (chmod(p->pw_dir, 02755)) {
205 perror_msg("%s", p->pw_dir);
207 /* interactively set passwd */
208 passwd_wrapper(p->pw_name);
214 /* return current uid (root is always uid == 0, right?) */
215 static inline uid_t i_am_not_root(void)
221 * adduser will take a login_name as its first parameter.
227 * can be customized via command-line parameters.
228 * ________________________________________________________________________ */
229 int adduser_main(int argc, char **argv)
234 const char *home = NULL;
243 gecos = default_gecos;
244 shell = default_shell;
247 while ((opt = getopt (argc, argv, "h:g:s:")) != -1)
264 if (i_am_not_root()) {
265 error_msg_and_die( "Only root may add a user or group to the system.");
269 if (optind >= argc) {
270 error_msg_and_die( "no user specified");
272 login = argv[optind];
274 /* create string for $HOME if not specified already */
276 home = concat_path_file(default_home_prefix, login);
278 #ifdef CONFIG_FEATURE_SHADOWPASSWDS
279 /* is /etc/shadow in use? */
280 shadow_enabled = (0 == access(shadow_file, F_OK));
283 /* create a passwd struct */
284 pw.pw_name = (char *)login;
285 pw.pw_passwd = (char *)default_passwd;
288 pw.pw_gecos = (char *)gecos;
289 pw.pw_dir = (char *)home;
290 pw.pw_shell = (char *)shell;
293 return adduser(passwd_file, &pw);
296 /* $Id: adduser.c,v 1.3 2002/07/16 23:50:05 sandman Exp $ */