1 /* vi: set sw=4 ts=4: */
3 * adduser - add users to /etc/passwd and /etc/shadow
6 * Copyright (C) 1999 by Lineo, inc.
7 * Written by John Beppu <beppu@lineo.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 #include <sys/param.h>
36 #include <sys/types.h>
38 #include "pwd_grp/pwd.h"
39 #include "pwd_grp/grp.h"
41 #define PASSWD_FILE "/etc/passwd"
42 #define SHADOW_FILE "/etc/gshadow"
45 # define PASSWD_FILE "passwd"
46 # define SHADOW_FILE "shadow"
49 /* structs __________________________ */
56 /* data _____________________________ */
58 /* defaults : should this be in an external file? */
59 static char *default_passwd = "x";
60 static char *default_gecos = "Embedix User,,,";
61 static char *default_home_prefix = "/home";
62 static char *default_shell = "/bin/sh";
65 static int shadow_enabled = 0;
67 /* I was doing this all over the place */
68 static FILE *fopen_wrapper(const char *filename, const char *mode)
72 f = fopen(filename, mode);
74 fprintf(stderr, "adduser: %s: %s\n", filename, strerror(errno));
80 /* EDR recoded such that the uid may be passed in *p */
81 static int passwd_study(const char *filename, struct passwd *p)
87 const int max = 65000;
89 passwd = fopen_wrapper(filename, "r");
93 /* EDR if uid is out of bounds, set to min */
94 if ((p->pw_uid > max) || (p->pw_uid < min))
98 * make sure login isn't taken;
99 * find free uid and gid;
101 while ((pw = fgetpwent(passwd))) {
102 if (strcmp(pw->pw_name, p->pw_name) == 0) {
106 if ((pw->pw_uid >= p->pw_uid) && (pw->pw_uid < max)
107 && (pw->pw_uid >= min)) {
108 p->pw_uid = pw->pw_uid + 1;
112 /* EDR check for an already existing gid */
113 while (getgrgid(p->pw_uid) != NULL)
116 /* EDR also check for an existing group definition */
117 if (getgrnam(p->pw_name) != NULL)
120 /* EDR bounds check */
121 if ((p->pw_uid > max) || (p->pw_uid < min))
124 /* EDR create new gid always = uid */
125 p->pw_gid = p->pw_uid;
131 static void addgroup_wrapper(const char *login, gid_t gid)
134 char *argv[] = { "addgroup", "-g", NULL, NULL };
138 strncpy(group_name, login, 32);
139 argv[3] = group_name;
140 sprintf(group_id, "%d", gid);
142 addgroup_main(argc, argv);
145 static void passwd_wrapper(const char *login)
147 char *prog = "passwd";
148 execlp(prog, prog, login, NULL);
149 error_msg_and_die("Failed to execute 'passwd', you must set the password for '%s' manually\n", login);
153 * pwd_to_spwd - create entries for new spwd structure
155 * pwd_to_spwd() creates a new (struct spwd) containing the
156 * information in the pointed-to (struct passwd).
158 #define DAY (24L*3600L)
161 static struct spwd *pwd_to_spwd(const struct passwd *pw)
163 static struct spwd sp;
166 * Nice, easy parts first. The name and passwd map directly
167 * from the old password structure to the new one.
169 sp.sp_namp = pw->pw_name;
170 sp.sp_pwdp = pw->pw_passwd;
173 * Defaults used if there is no pw_age information.
176 sp.sp_max = (10000L * DAY) / SCALE;
177 sp.sp_lstchg = time((time_t *) 0) / SCALE;
180 * These fields have no corresponding information in the password
181 * file. They are set to uninitialized values.
191 /* putpwent(3) remix */
192 static int adduser(const char *filename, struct passwd *p)
199 /* make sure everything is kosher and setup uid && gid */
200 passwd = fopen_wrapper(filename, "a");
201 if (passwd == NULL) {
205 fseek(passwd, 0, SEEK_END);
207 /* if (passwd_study(filename, p) == 0) { */
208 r = passwd_study(filename, p);
211 error_msg("%s: login already in use\n", p->pw_name);
213 error_msg("illegal uid or no uids left\n");
215 error_msg("group name %s already in use\n", p->pw_name);
217 error_msg("generic error.\n");
223 if (putpwent(p, passwd) == -1) {
229 /* add to shadow if necessary */
230 if (shadow_enabled) {
231 shadow = fopen_wrapper(SHADOW_FILE, "a");
232 if (shadow == NULL) {
236 fseek(shadow, 0, SEEK_END);
238 sp->sp_max = 99999; /* debianish */
240 fprintf(shadow, "%s:!:%ld:%ld:%ld:%ld:::\n",
241 sp->sp_namp, sp->sp_lstchg, sp->sp_min, sp->sp_max,
247 /* addgroup should be responsible for dealing w/ gshadow */
248 addgroup_wrapper(p->pw_name, p->pw_gid);
250 /* Clear the umask for this process so it doesn't
251 * * screw up the permissions on the mkdir and chown. */
255 if (mkdir(p->pw_dir, 0755)) {
256 perror_msg("%s", p->pw_dir);
258 /* Set the owner and group so it is owned by the new user. */
259 if (chown(p->pw_dir, p->pw_uid, p->pw_gid)) {
260 perror_msg("%s", p->pw_dir);
262 /* Now fix up the permissions to 2755. Can't do it before now
263 * since chown will clear the setgid bit */
264 if (chmod(p->pw_dir, 02755)) {
265 perror_msg("%s", p->pw_dir);
267 /* interactively set passwd */
268 passwd_wrapper(p->pw_name);
274 /* return current uid (root is always uid == 0, right?) */
275 static uid_t i_am_not_root()
281 * adduser will take a login_name as its first parameter.
287 * can be customized via command-line parameters.
288 * ________________________________________________________________________ */
289 int adduser_main(int argc, char **argv)
297 char path[MAXPATHLEN];
305 gecos = default_gecos;
306 shell = default_shell;
309 for (i = 1; i < argc; i++) {
310 if (argv[i][0] == '-') {
323 error_msg("invalid option -- %c\n", opt);
332 if (i_am_not_root()) {
333 error_msg_and_die( "Only root may add a user or group to the system.\n");
338 error_msg_and_die( "adduser: no user specified\n");
342 /* create string for $HOME if not specified already */
344 snprintf(path, MAXPATHLEN, "%s/%s", default_home_prefix, login);
345 path[MAXPATHLEN - 1] = 0;
348 /* is /etc/shadow in use? */
349 shadow_enabled = (0 == access(SHADOW_FILE, F_OK));
351 /* create a passwd struct */
353 pw.pw_passwd = default_passwd;
361 i = adduser(PASSWD_FILE, &pw);
366 /* $Id: adduser.c,v 1.1 2001/08/21 16:18:59 andersen Exp $ */