user.c revision 1.132 1 1.132 zafer /* $NetBSD: user.c,v 1.132 2018/06/10 07:52:05 zafer Exp $ */
2 1.1 agc
3 1.1 agc /*
4 1.4 lukem * Copyright (c) 1999 Alistair G. Crooks. All rights reserved.
5 1.96 christos * Copyright (c) 2005 Liam J. Foy. All rights reserved.
6 1.1 agc *
7 1.1 agc * Redistribution and use in source and binary forms, with or without
8 1.1 agc * modification, are permitted provided that the following conditions
9 1.1 agc * are met:
10 1.1 agc * 1. Redistributions of source code must retain the above copyright
11 1.1 agc * notice, this list of conditions and the following disclaimer.
12 1.1 agc * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 agc * notice, this list of conditions and the following disclaimer in the
14 1.1 agc * documentation and/or other materials provided with the distribution.
15 1.98 agc * 3. The name of the author may not be used to endorse or promote
16 1.1 agc * products derived from this software without specific prior written
17 1.1 agc * permission.
18 1.1 agc *
19 1.1 agc * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
20 1.1 agc * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 1.1 agc * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23 1.1 agc * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25 1.1 agc * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 agc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 1.1 agc * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 1.1 agc * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 1.1 agc * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 1.1 agc */
31 1.11 agc #include <sys/cdefs.h>
32 1.11 agc
33 1.11 agc #ifndef lint
34 1.120 lukem __COPYRIGHT("@(#) Copyright (c) 1999\
35 1.120 lukem The NetBSD Foundation, Inc. All rights reserved.");
36 1.132 zafer __RCSID("$NetBSD: user.c,v 1.132 2018/06/10 07:52:05 zafer Exp $");
37 1.11 agc #endif
38 1.11 agc
39 1.1 agc #include <sys/types.h>
40 1.1 agc #include <sys/param.h>
41 1.1 agc #include <sys/stat.h>
42 1.128 dholland #include <sys/wait.h>
43 1.1 agc
44 1.5 lukem #include <ctype.h>
45 1.5 lukem #include <dirent.h>
46 1.5 lukem #include <err.h>
47 1.5 lukem #include <fcntl.h>
48 1.5 lukem #include <grp.h>
49 1.82 christos #ifdef EXTENSIONS
50 1.82 christos #include <login_cap.h>
51 1.82 christos #endif
52 1.5 lukem #include <pwd.h>
53 1.53 agc #include <regex.h>
54 1.5 lukem #include <stdarg.h>
55 1.1 agc #include <stdio.h>
56 1.1 agc #include <stdlib.h>
57 1.1 agc #include <string.h>
58 1.59 agc #include <syslog.h>
59 1.1 agc #include <time.h>
60 1.5 lukem #include <unistd.h>
61 1.1 agc #include <util.h>
62 1.82 christos #include <errno.h>
63 1.1 agc
64 1.129 dholland #include "pathnames.h"
65 1.1 agc #include "defs.h"
66 1.1 agc #include "usermgmt.h"
67 1.1 agc
68 1.26 simonb
69 1.9 agc /* this struct describes a uid range */
70 1.9 agc typedef struct range_t {
71 1.9 agc int r_from; /* low uid */
72 1.9 agc int r_to; /* high uid */
73 1.9 agc } range_t;
74 1.9 agc
75 1.125 mlelstv typedef struct rangelist_t {
76 1.125 mlelstv unsigned rl_rsize; /* size of range array */
77 1.125 mlelstv unsigned rl_rc; /* # of ranges */
78 1.125 mlelstv range_t *rl_rv; /* the ranges */
79 1.125 mlelstv unsigned rl_defrc; /* # of ranges in defaults */
80 1.125 mlelstv } rangelist_t;
81 1.125 mlelstv
82 1.125 mlelstv /* this struct encapsulates the user and group information */
83 1.9 agc typedef struct user_t {
84 1.26 simonb int u_flags; /* see below */
85 1.9 agc int u_uid; /* uid of user */
86 1.64 agc char *u_password; /* encrypted password */
87 1.64 agc char *u_comment; /* comment field */
88 1.64 agc char *u_home; /* home directory */
89 1.115 elad mode_t u_homeperm; /* permissions of home dir */
90 1.64 agc char *u_primgrp; /* primary group */
91 1.9 agc int u_groupc; /* # of secondary groups */
92 1.53 agc const char *u_groupv[NGROUPS_MAX]; /* secondary groups */
93 1.64 agc char *u_shell; /* user's shell */
94 1.64 agc char *u_basedir; /* base directory for home */
95 1.64 agc char *u_expire; /* when password will expire */
96 1.64 agc char *u_inactive; /* when account will expire */
97 1.64 agc char *u_skeldir; /* directory for startup files */
98 1.64 agc char *u_class; /* login class */
99 1.125 mlelstv rangelist_t u_r; /* list of ranges */
100 1.9 agc unsigned u_defrc; /* # of ranges in defaults */
101 1.9 agc int u_preserve; /* preserve uids on deletion */
102 1.75 agc int u_allow_samba; /* allow trailing '$' for samba login names */
103 1.82 christos int u_locked; /* user account lock */
104 1.9 agc } user_t;
105 1.125 mlelstv #define u_rsize u_r.rl_rsize
106 1.125 mlelstv #define u_rc u_r.rl_rc
107 1.125 mlelstv #define u_rv u_r.rl_rv
108 1.125 mlelstv #define u_defrc u_r.rl_defrc
109 1.125 mlelstv
110 1.125 mlelstv /* this struct encapsulates the user and group information */
111 1.125 mlelstv typedef struct group_t {
112 1.125 mlelstv rangelist_t g_r; /* list of ranges */
113 1.125 mlelstv } group_t;
114 1.125 mlelstv #define g_rsize g_r.rl_rsize
115 1.125 mlelstv #define g_rc g_r.rl_rc
116 1.125 mlelstv #define g_rv g_r.rl_rv
117 1.125 mlelstv #define g_defrc g_r.rl_defrc
118 1.125 mlelstv
119 1.125 mlelstv typedef struct def_t {
120 1.125 mlelstv user_t user;
121 1.125 mlelstv group_t group;
122 1.125 mlelstv } def_t;
123 1.9 agc
124 1.26 simonb /* flags for which fields of the user_t replace the passwd entry */
125 1.26 simonb enum {
126 1.26 simonb F_COMMENT = 0x0001,
127 1.26 simonb F_DUPUID = 0x0002,
128 1.26 simonb F_EXPIRE = 0x0004,
129 1.26 simonb F_GROUP = 0x0008,
130 1.26 simonb F_HOMEDIR = 0x0010,
131 1.26 simonb F_MKDIR = 0x0020,
132 1.26 simonb F_INACTIVE = 0x0040,
133 1.26 simonb F_PASSWORD = 0x0080,
134 1.26 simonb F_SECGROUP = 0x0100,
135 1.26 simonb F_SHELL = 0x0200,
136 1.26 simonb F_UID = 0x0400,
137 1.42 christos F_USERNAME = 0x0800,
138 1.42 christos F_CLASS = 0x1000
139 1.26 simonb };
140 1.26 simonb
141 1.82 christos #define UNLOCK 0
142 1.82 christos #define LOCK 1
143 1.82 christos #define LOCKED "*LOCKED*"
144 1.82 christos
145 1.9 agc #ifndef DEF_GROUP
146 1.9 agc #define DEF_GROUP "users"
147 1.9 agc #endif
148 1.9 agc
149 1.9 agc #ifndef DEF_BASEDIR
150 1.9 agc #define DEF_BASEDIR "/home"
151 1.9 agc #endif
152 1.9 agc
153 1.9 agc #ifndef DEF_SKELDIR
154 1.9 agc #define DEF_SKELDIR "/etc/skel"
155 1.9 agc #endif
156 1.9 agc
157 1.9 agc #ifndef DEF_SHELL
158 1.117 pavel #define DEF_SHELL _PATH_BSHELL
159 1.9 agc #endif
160 1.9 agc
161 1.9 agc #ifndef DEF_COMMENT
162 1.9 agc #define DEF_COMMENT ""
163 1.9 agc #endif
164 1.9 agc
165 1.9 agc #ifndef DEF_LOWUID
166 1.9 agc #define DEF_LOWUID 1000
167 1.9 agc #endif
168 1.9 agc
169 1.9 agc #ifndef DEF_HIGHUID
170 1.9 agc #define DEF_HIGHUID 60000
171 1.9 agc #endif
172 1.9 agc
173 1.9 agc #ifndef DEF_INACTIVE
174 1.9 agc #define DEF_INACTIVE 0
175 1.9 agc #endif
176 1.9 agc
177 1.9 agc #ifndef DEF_EXPIRE
178 1.24 simonb #define DEF_EXPIRE NULL
179 1.9 agc #endif
180 1.9 agc
181 1.42 christos #ifndef DEF_CLASS
182 1.42 christos #define DEF_CLASS ""
183 1.42 christos #endif
184 1.42 christos
185 1.9 agc #ifndef WAITSECS
186 1.9 agc #define WAITSECS 10
187 1.9 agc #endif
188 1.9 agc
189 1.9 agc #ifndef NOBODY_UID
190 1.9 agc #define NOBODY_UID 32767
191 1.9 agc #endif
192 1.9 agc
193 1.115 elad #ifndef DEF_HOMEPERM
194 1.115 elad #define DEF_HOMEPERM 0755
195 1.115 elad #endif
196 1.115 elad
197 1.9 agc /* some useful constants */
198 1.9 agc enum {
199 1.9 agc MaxShellNameLen = 256,
200 1.9 agc MaxFileNameLen = MAXPATHLEN,
201 1.92 christos MaxUserNameLen = LOGIN_NAME_MAX - 1,
202 1.9 agc MaxCommandLen = 2048,
203 1.9 agc MaxEntryLen = 2048,
204 1.60 itojun PasswordLength = 2048,
205 1.9 agc
206 1.65 agc DES_Len = 13,
207 1.9 agc };
208 1.9 agc
209 1.52 grant #define UNSET_INACTIVE "Null (unset)"
210 1.9 agc #define UNSET_EXPIRY "Null (unset)"
211 1.9 agc
212 1.127 dholland static int asystem(const char *fmt, ...) __printflike(1, 2);
213 1.91 christos static int is_number(const char *);
214 1.91 christos static struct group *find_group_info(const char *);
215 1.91 christos static int verbose;
216 1.1 agc
217 1.82 christos static char *
218 1.82 christos skipspace(char *s)
219 1.82 christos {
220 1.93 agc for (; *s && isspace((unsigned char)*s) ; s++) {
221 1.93 agc }
222 1.82 christos return s;
223 1.82 christos }
224 1.82 christos
225 1.82 christos static int
226 1.82 christos check_numeric(const char *val, const char *name)
227 1.82 christos {
228 1.93 agc if (!is_number(val)) {
229 1.82 christos errx(EXIT_FAILURE, "When using [-%c %s], "
230 1.82 christos "the %s must be numeric", *name, name, name);
231 1.93 agc }
232 1.82 christos return atoi(val);
233 1.82 christos }
234 1.82 christos
235 1.111 christos /* resize *cpp appropriately then assign `n' chars of `s' to it */
236 1.1 agc static void
237 1.53 agc memsave(char **cpp, const char *s, size_t n)
238 1.1 agc {
239 1.111 christos RENEW(char, *cpp, n + 1, exit(1));
240 1.82 christos (void)memcpy(*cpp, s, n);
241 1.14 soren (*cpp)[n] = '\0';
242 1.1 agc }
243 1.1 agc
244 1.1 agc /* a replacement for system(3) */
245 1.1 agc static int
246 1.25 is asystem(const char *fmt, ...)
247 1.1 agc {
248 1.1 agc va_list vp;
249 1.1 agc char buf[MaxCommandLen];
250 1.1 agc int ret;
251 1.1 agc
252 1.1 agc va_start(vp, fmt);
253 1.82 christos (void)vsnprintf(buf, sizeof(buf), fmt, vp);
254 1.1 agc va_end(vp);
255 1.1 agc if (verbose) {
256 1.82 christos (void)printf("Command: %s\n", buf);
257 1.1 agc }
258 1.128 dholland ret = system(buf);
259 1.128 dholland if (ret == -1) {
260 1.82 christos warn("Error running `%s'", buf);
261 1.128 dholland } else if (WIFSIGNALED(ret)) {
262 1.128 dholland warnx("Error running `%s': Signal %d", buf, WTERMSIG(ret));
263 1.128 dholland } else if (WIFEXITED(ret) && WEXITSTATUS(ret) != 0) {
264 1.128 dholland warnx("Error running `%s': Exit %d", buf, WEXITSTATUS(ret));
265 1.1 agc }
266 1.1 agc return ret;
267 1.1 agc }
268 1.9 agc
269 1.26 simonb /* remove a users home directory, returning 1 for success (ie, no problems encountered) */
270 1.26 simonb static int
271 1.96 christos removehomedir(struct passwd *pwp)
272 1.26 simonb {
273 1.26 simonb struct stat st;
274 1.26 simonb
275 1.26 simonb /* userid not root? */
276 1.96 christos if (pwp->pw_uid == 0) {
277 1.96 christos warnx("Not deleting home directory `%s'; userid is 0", pwp->pw_dir);
278 1.26 simonb return 0;
279 1.26 simonb }
280 1.26 simonb
281 1.26 simonb /* directory exists (and is a directory!) */
282 1.96 christos if (stat(pwp->pw_dir, &st) < 0) {
283 1.96 christos warn("Cannot access home directory `%s'", pwp->pw_dir);
284 1.26 simonb return 0;
285 1.26 simonb }
286 1.26 simonb if (!S_ISDIR(st.st_mode)) {
287 1.96 christos warnx("Home directory `%s' is not a directory", pwp->pw_dir);
288 1.26 simonb return 0;
289 1.26 simonb }
290 1.26 simonb
291 1.26 simonb /* userid matches directory owner? */
292 1.96 christos if (st.st_uid != pwp->pw_uid) {
293 1.82 christos warnx("User `%s' doesn't own directory `%s', not removed",
294 1.96 christos pwp->pw_name, pwp->pw_dir);
295 1.26 simonb return 0;
296 1.26 simonb }
297 1.26 simonb
298 1.96 christos (void)seteuid(pwp->pw_uid);
299 1.26 simonb /* we add the "|| true" to keep asystem() quiet if there is a non-zero exit status. */
300 1.129 dholland (void)asystem("%s -rf %s > /dev/null 2>&1 || true", _PATH_RM,
301 1.129 dholland pwp->pw_dir);
302 1.82 christos (void)seteuid(0);
303 1.96 christos if (rmdir(pwp->pw_dir) < 0) {
304 1.96 christos warn("Unable to remove all files in `%s'", pwp->pw_dir);
305 1.26 simonb return 0;
306 1.26 simonb }
307 1.26 simonb return 1;
308 1.26 simonb }
309 1.26 simonb
310 1.9 agc /* return 1 if all of `s' is numeric */
311 1.9 agc static int
312 1.81 agc is_number(const char *s)
313 1.9 agc {
314 1.9 agc for ( ; *s ; s++) {
315 1.67 jrf if (!isdigit((unsigned char) *s)) {
316 1.9 agc return 0;
317 1.9 agc }
318 1.9 agc }
319 1.9 agc return 1;
320 1.9 agc }
321 1.9 agc
322 1.9 agc /*
323 1.9 agc * check that the effective uid is 0 - called from funcs which will
324 1.9 agc * modify data and config files.
325 1.9 agc */
326 1.9 agc static void
327 1.9 agc checkeuid(void)
328 1.9 agc {
329 1.9 agc if (geteuid() != 0) {
330 1.9 agc errx(EXIT_FAILURE, "Program must be run as root");
331 1.9 agc }
332 1.9 agc }
333 1.9 agc
334 1.1 agc /* copy any dot files into the user's home directory */
335 1.1 agc static int
336 1.115 elad copydotfiles(char *skeldir, int uid, int gid, char *dir, mode_t homeperm)
337 1.1 agc {
338 1.1 agc struct dirent *dp;
339 1.1 agc DIR *dirp;
340 1.1 agc int n;
341 1.1 agc
342 1.24 simonb if ((dirp = opendir(skeldir)) == NULL) {
343 1.82 christos warn("Can't open source . files dir `%s'", skeldir);
344 1.1 agc return 0;
345 1.1 agc }
346 1.24 simonb for (n = 0; (dp = readdir(dirp)) != NULL && n == 0 ; ) {
347 1.1 agc if (strcmp(dp->d_name, ".") == 0 ||
348 1.1 agc strcmp(dp->d_name, "..") == 0) {
349 1.1 agc continue;
350 1.1 agc }
351 1.10 agc n = 1;
352 1.1 agc }
353 1.82 christos (void)closedir(dirp);
354 1.1 agc if (n == 0) {
355 1.1 agc warnx("No \"dot\" initialisation files found");
356 1.1 agc } else {
357 1.96 christos (void)asystem("cd %s && %s -rw -pe %s . %s",
358 1.129 dholland skeldir, _PATH_PAX, (verbose) ? "-v" : "", dir);
359 1.1 agc }
360 1.129 dholland (void)asystem("%s -R -h %d:%d %s", _PATH_CHOWN, uid, gid, dir);
361 1.129 dholland (void)asystem("%s -R u+w %s", _PATH_CHMOD, dir);
362 1.115 elad #ifdef EXTENSIONS
363 1.129 dholland (void)asystem("%s 0%o %s", _PATH_CHMOD, homeperm, dir);
364 1.115 elad #endif
365 1.1 agc return n;
366 1.1 agc }
367 1.1 agc
368 1.1 agc /* create a group entry with gid `gid' */
369 1.1 agc static int
370 1.53 agc creategid(char *group, int gid, const char *name)
371 1.1 agc {
372 1.1 agc struct stat st;
373 1.1 agc FILE *from;
374 1.1 agc FILE *to;
375 1.1 agc char buf[MaxEntryLen];
376 1.1 agc char f[MaxFileNameLen];
377 1.1 agc int fd;
378 1.1 agc int cc;
379 1.1 agc
380 1.24 simonb if (getgrnam(group) != NULL) {
381 1.96 christos warnx("Can't create group `%s': already exists", group);
382 1.12 agc return 0;
383 1.12 agc }
384 1.24 simonb if ((from = fopen(_PATH_GROUP, "r")) == NULL) {
385 1.96 christos warn("Can't create group `%s': can't open `%s'", name,
386 1.82 christos _PATH_GROUP);
387 1.1 agc return 0;
388 1.1 agc }
389 1.1 agc if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) {
390 1.82 christos warn("Can't lock `%s'", _PATH_GROUP);
391 1.107 joerg (void)fclose(from);
392 1.96 christos return 0;
393 1.1 agc }
394 1.82 christos (void)fstat(fileno(from), &st);
395 1.82 christos (void)snprintf(f, sizeof(f), "%s.XXXXXX", _PATH_GROUP);
396 1.1 agc if ((fd = mkstemp(f)) < 0) {
397 1.96 christos warn("Can't create group `%s': mkstemp failed", group);
398 1.82 christos (void)fclose(from);
399 1.1 agc return 0;
400 1.1 agc }
401 1.24 simonb if ((to = fdopen(fd, "w")) == NULL) {
402 1.96 christos warn("Can't create group `%s': fdopen `%s' failed",
403 1.96 christos group, f);
404 1.82 christos (void)fclose(from);
405 1.82 christos (void)close(fd);
406 1.82 christos (void)unlink(f);
407 1.1 agc return 0;
408 1.1 agc }
409 1.1 agc while ((cc = fread(buf, sizeof(char), sizeof(buf), from)) > 0) {
410 1.1 agc if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) {
411 1.96 christos warn("Can't create group `%s': short write to `%s'",
412 1.96 christos group, f);
413 1.82 christos (void)fclose(from);
414 1.82 christos (void)close(fd);
415 1.82 christos (void)unlink(f);
416 1.1 agc return 0;
417 1.1 agc }
418 1.1 agc }
419 1.82 christos (void)fprintf(to, "%s:*:%d:%s\n", group, gid, name);
420 1.82 christos (void)fclose(from);
421 1.82 christos (void)fclose(to);
422 1.20 agc if (rename(f, _PATH_GROUP) < 0) {
423 1.96 christos warn("Can't create group `%s': can't rename `%s' to `%s'",
424 1.96 christos group, f, _PATH_GROUP);
425 1.82 christos (void)unlink(f);
426 1.1 agc return 0;
427 1.1 agc }
428 1.82 christos (void)chmod(_PATH_GROUP, st.st_mode & 07777);
429 1.82 christos syslog(LOG_INFO, "New group added: name=%s, gid=%d", group, gid);
430 1.1 agc return 1;
431 1.1 agc }
432 1.1 agc
433 1.1 agc /* modify the group entry with name `group' to be newent */
434 1.1 agc static int
435 1.1 agc modify_gid(char *group, char *newent)
436 1.1 agc {
437 1.1 agc struct stat st;
438 1.1 agc FILE *from;
439 1.1 agc FILE *to;
440 1.1 agc char buf[MaxEntryLen];
441 1.1 agc char f[MaxFileNameLen];
442 1.1 agc char *colon;
443 1.1 agc int groupc;
444 1.1 agc int entc;
445 1.1 agc int fd;
446 1.1 agc int cc;
447 1.1 agc
448 1.24 simonb if ((from = fopen(_PATH_GROUP, "r")) == NULL) {
449 1.96 christos warn("Can't modify group `%s': can't open `%s'",
450 1.82 christos group, _PATH_GROUP);
451 1.1 agc return 0;
452 1.1 agc }
453 1.1 agc if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) {
454 1.96 christos warn("Can't modify group `%s': can't lock `%s'",
455 1.96 christos group, _PATH_GROUP);
456 1.107 joerg (void)fclose(from);
457 1.96 christos return 0;
458 1.1 agc }
459 1.82 christos (void)fstat(fileno(from), &st);
460 1.82 christos (void)snprintf(f, sizeof(f), "%s.XXXXXX", _PATH_GROUP);
461 1.1 agc if ((fd = mkstemp(f)) < 0) {
462 1.96 christos warn("Can't modify group `%s': mkstemp failed", group);
463 1.82 christos (void)fclose(from);
464 1.1 agc return 0;
465 1.1 agc }
466 1.24 simonb if ((to = fdopen(fd, "w")) == NULL) {
467 1.96 christos warn("Can't modify group `%s': fdopen `%s' failed", group, f);
468 1.82 christos (void)fclose(from);
469 1.82 christos (void)close(fd);
470 1.82 christos (void)unlink(f);
471 1.1 agc return 0;
472 1.1 agc }
473 1.1 agc groupc = strlen(group);
474 1.15 soren while (fgets(buf, sizeof(buf), from) != NULL) {
475 1.12 agc cc = strlen(buf);
476 1.15 soren if ((colon = strchr(buf, ':')) == NULL) {
477 1.82 christos warnx("Badly formed entry `%s'", buf);
478 1.1 agc continue;
479 1.1 agc }
480 1.1 agc entc = (int)(colon - buf);
481 1.82 christos if (entc == groupc &&
482 1.82 christos strncmp(group, buf, (unsigned) entc) == 0) {
483 1.15 soren if (newent == NULL) {
484 1.91 christos struct group *grp_rm;
485 1.91 christos struct passwd *user_pwd;
486 1.91 christos
487 1.91 christos /*
488 1.91 christos * Check that the group being removed
489 1.91 christos * isn't any user's Primary group. Just
490 1.91 christos * warn if it is. This could cause problems
491 1.91 christos * if the group GID was reused for a
492 1.91 christos * different purpose.
493 1.91 christos */
494 1.91 christos
495 1.91 christos grp_rm = find_group_info(group);
496 1.91 christos while ((user_pwd = getpwent()) != NULL) {
497 1.91 christos if (user_pwd->pw_gid == grp_rm->gr_gid) {
498 1.91 christos warnx("Warning: group `%s'(%d)"
499 1.91 christos " is the primary group of"
500 1.91 christos " `%s'. Use caution if you"
501 1.91 christos " later add this GID.",
502 1.91 christos grp_rm->gr_name,
503 1.91 christos grp_rm->gr_gid, user_pwd->pw_name);
504 1.91 christos }
505 1.91 christos }
506 1.91 christos endpwent();
507 1.1 agc continue;
508 1.1 agc } else {
509 1.1 agc cc = strlen(newent);
510 1.82 christos (void)strlcpy(buf, newent, sizeof(buf));
511 1.1 agc }
512 1.1 agc }
513 1.1 agc if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) {
514 1.96 christos warn("Can't modify group `%s': short write to `%s'",
515 1.96 christos group, f);
516 1.82 christos (void)fclose(from);
517 1.82 christos (void)close(fd);
518 1.82 christos (void)unlink(f);
519 1.1 agc return 0;
520 1.1 agc }
521 1.1 agc }
522 1.82 christos (void)fclose(from);
523 1.82 christos (void)fclose(to);
524 1.20 agc if (rename(f, _PATH_GROUP) < 0) {
525 1.96 christos warn("Can't modify group `%s': can't rename `%s' to `%s'",
526 1.96 christos group, f, _PATH_GROUP);
527 1.82 christos (void)unlink(f);
528 1.1 agc return 0;
529 1.1 agc }
530 1.82 christos (void)chmod(_PATH_GROUP, st.st_mode & 07777);
531 1.59 agc if (newent == NULL) {
532 1.59 agc syslog(LOG_INFO, "group deleted: name=%s", group);
533 1.59 agc } else {
534 1.59 agc syslog(LOG_INFO, "group information modified: name=%s", group);
535 1.59 agc }
536 1.1 agc return 1;
537 1.1 agc }
538 1.1 agc
539 1.23 agc /* modify the group entries for all `groups', by adding `user' */
540 1.23 agc static int
541 1.53 agc append_group(char *user, int ngroups, const char **groups)
542 1.23 agc {
543 1.23 agc struct group *grp;
544 1.23 agc struct stat st;
545 1.23 agc FILE *from;
546 1.23 agc FILE *to;
547 1.23 agc char buf[MaxEntryLen];
548 1.23 agc char f[MaxFileNameLen];
549 1.23 agc char *colon;
550 1.23 agc int groupc;
551 1.23 agc int entc;
552 1.23 agc int fd;
553 1.23 agc int nc;
554 1.23 agc int cc;
555 1.23 agc int i;
556 1.23 agc int j;
557 1.23 agc
558 1.23 agc for (i = 0 ; i < ngroups ; i++) {
559 1.27 simonb if ((grp = getgrnam(groups[i])) == NULL) {
560 1.82 christos warnx("Can't append group `%s' for user `%s'",
561 1.82 christos groups[i], user);
562 1.27 simonb } else {
563 1.23 agc for (j = 0 ; grp->gr_mem[j] ; j++) {
564 1.23 agc if (strcmp(user, grp->gr_mem[j]) == 0) {
565 1.23 agc /* already in it */
566 1.23 agc groups[i] = "";
567 1.23 agc }
568 1.23 agc }
569 1.23 agc }
570 1.23 agc }
571 1.24 simonb if ((from = fopen(_PATH_GROUP, "r")) == NULL) {
572 1.96 christos warn("Can't append group(s) for `%s': can't open `%s'",
573 1.82 christos user, _PATH_GROUP);
574 1.23 agc return 0;
575 1.23 agc }
576 1.23 agc if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) {
577 1.96 christos warn("Can't append group(s) for `%s': can't lock `%s'",
578 1.96 christos user, _PATH_GROUP);
579 1.107 joerg (void)fclose(from);
580 1.96 christos return 0;
581 1.23 agc }
582 1.82 christos (void)fstat(fileno(from), &st);
583 1.82 christos (void)snprintf(f, sizeof(f), "%s.XXXXXX", _PATH_GROUP);
584 1.23 agc if ((fd = mkstemp(f)) < 0) {
585 1.96 christos warn("Can't append group(s) for `%s': mkstemp failed",
586 1.96 christos user);
587 1.82 christos (void)fclose(from);
588 1.23 agc return 0;
589 1.23 agc }
590 1.24 simonb if ((to = fdopen(fd, "w")) == NULL) {
591 1.96 christos warn("Can't append group(s) for `%s': fdopen `%s' failed",
592 1.96 christos user, f);
593 1.82 christos (void)fclose(from);
594 1.82 christos (void)close(fd);
595 1.82 christos (void)unlink(f);
596 1.23 agc return 0;
597 1.23 agc }
598 1.23 agc while (fgets(buf, sizeof(buf), from) != NULL) {
599 1.23 agc cc = strlen(buf);
600 1.23 agc if ((colon = strchr(buf, ':')) == NULL) {
601 1.82 christos warnx("Badly formed entry `%s'", buf);
602 1.23 agc continue;
603 1.23 agc }
604 1.23 agc entc = (int)(colon - buf);
605 1.23 agc for (i = 0 ; i < ngroups ; i++) {
606 1.23 agc if ((groupc = strlen(groups[i])) == 0) {
607 1.23 agc continue;
608 1.23 agc }
609 1.82 christos if (entc == groupc &&
610 1.82 christos strncmp(groups[i], buf, (unsigned) entc) == 0) {
611 1.23 agc if ((nc = snprintf(&buf[cc - 1],
612 1.82 christos sizeof(buf) - cc + 1, "%s%s\n",
613 1.93 agc (buf[cc - 2] == ':') ? "" : ",", user)) < 0) {
614 1.82 christos warnx("Warning: group `%s' "
615 1.82 christos "entry too long", groups[i]);
616 1.93 agc }
617 1.23 agc cc += nc - 1;
618 1.23 agc }
619 1.23 agc }
620 1.23 agc if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) {
621 1.96 christos warn("Can't append group(s) for `%s':"
622 1.96 christos " short write to `%s'", user, f);
623 1.82 christos (void)fclose(from);
624 1.82 christos (void)close(fd);
625 1.82 christos (void)unlink(f);
626 1.23 agc return 0;
627 1.23 agc }
628 1.23 agc }
629 1.82 christos (void)fclose(from);
630 1.82 christos (void)fclose(to);
631 1.23 agc if (rename(f, _PATH_GROUP) < 0) {
632 1.96 christos warn("Can't append group(s) for `%s': "
633 1.96 christos "can't rename `%s' to `%s'", user, f, _PATH_GROUP);
634 1.82 christos (void)unlink(f);
635 1.23 agc return 0;
636 1.23 agc }
637 1.82 christos (void)chmod(_PATH_GROUP, st.st_mode & 07777);
638 1.23 agc return 1;
639 1.23 agc }
640 1.23 agc
641 1.79 agc /* the valid characters for login and group names */
642 1.79 agc #define VALID_CHAR(c) (isalnum(c) || (c) == '.' || (c) == '_' || (c) == '-')
643 1.79 agc
644 1.1 agc /* return 1 if `login' is a valid login name */
645 1.1 agc static int
646 1.75 agc valid_login(char *login_name, int allow_samba)
647 1.1 agc {
648 1.67 jrf unsigned char *cp;
649 1.1 agc
650 1.92 christos /* First character of a login name cannot be '-'. */
651 1.93 agc if (*login_name == '-') {
652 1.92 christos return 0;
653 1.93 agc }
654 1.66 agc if (strlen(login_name) >= LOGIN_NAME_MAX) {
655 1.66 agc return 0;
656 1.66 agc }
657 1.82 christos for (cp = (unsigned char *)login_name ; *cp ; cp++) {
658 1.79 agc if (!VALID_CHAR(*cp)) {
659 1.75 agc #ifdef EXTENSIONS
660 1.75 agc /* check for a trailing '$' in a Samba user name */
661 1.75 agc if (allow_samba && *cp == '$' && *(cp + 1) == 0x0) {
662 1.75 agc return 1;
663 1.96 christos }
664 1.75 agc #endif
665 1.1 agc return 0;
666 1.1 agc }
667 1.1 agc }
668 1.1 agc return 1;
669 1.1 agc }
670 1.1 agc
671 1.1 agc /* return 1 if `group' is a valid group name */
672 1.1 agc static int
673 1.1 agc valid_group(char *group)
674 1.1 agc {
675 1.67 jrf unsigned char *cp;
676 1.1 agc
677 1.82 christos for (cp = (unsigned char *)group; *cp; cp++) {
678 1.79 agc if (!VALID_CHAR(*cp)) {
679 1.1 agc return 0;
680 1.1 agc }
681 1.1 agc }
682 1.1 agc return 1;
683 1.1 agc }
684 1.1 agc
685 1.1 agc /* find the next gid in the range lo .. hi */
686 1.1 agc static int
687 1.1 agc getnextgid(int *gidp, int lo, int hi)
688 1.1 agc {
689 1.1 agc for (*gidp = lo ; *gidp < hi ; *gidp += 1) {
690 1.24 simonb if (getgrgid((gid_t)*gidp) == NULL) {
691 1.1 agc return 1;
692 1.1 agc }
693 1.1 agc }
694 1.1 agc return 0;
695 1.1 agc }
696 1.1 agc
697 1.1 agc #ifdef EXTENSIONS
698 1.1 agc /* save a range of uids */
699 1.1 agc static int
700 1.125 mlelstv save_range(rangelist_t *rlp, char *cp)
701 1.1 agc {
702 1.1 agc int from;
703 1.1 agc int to;
704 1.1 agc int i;
705 1.1 agc
706 1.125 mlelstv if (rlp->rl_rsize == 0) {
707 1.125 mlelstv rlp->rl_rsize = 32;
708 1.125 mlelstv NEWARRAY(range_t, rlp->rl_rv, rlp->rl_rsize, return(0));
709 1.125 mlelstv } else if (rlp->rl_rc == rlp->rl_rsize) {
710 1.125 mlelstv rlp->rl_rsize *= 2;
711 1.125 mlelstv RENEW(range_t, rlp->rl_rv, rlp->rl_rsize, return(0));
712 1.125 mlelstv }
713 1.125 mlelstv if (rlp->rl_rv && sscanf(cp, "%d..%d", &from, &to) == 2) {
714 1.125 mlelstv for (i = rlp->rl_defrc ; i < rlp->rl_rc ; i++) {
715 1.125 mlelstv if (rlp->rl_rv[i].r_from == from &&
716 1.125 mlelstv rlp->rl_rv[i].r_to == to) {
717 1.1 agc break;
718 1.1 agc }
719 1.1 agc }
720 1.125 mlelstv if (i == rlp->rl_rc) {
721 1.125 mlelstv rlp->rl_rv[rlp->rl_rc].r_from = from;
722 1.125 mlelstv rlp->rl_rv[rlp->rl_rc].r_to = to;
723 1.125 mlelstv rlp->rl_rc += 1;
724 1.1 agc }
725 1.1 agc } else {
726 1.1 agc warnx("Bad range `%s'", cp);
727 1.1 agc return 0;
728 1.1 agc }
729 1.1 agc return 1;
730 1.1 agc }
731 1.1 agc #endif
732 1.1 agc
733 1.1 agc /* set the defaults in the defaults file */
734 1.1 agc static int
735 1.1 agc setdefaults(user_t *up)
736 1.1 agc {
737 1.1 agc char template[MaxFileNameLen];
738 1.1 agc FILE *fp;
739 1.1 agc int ret;
740 1.1 agc int fd;
741 1.42 christos #ifdef EXTENSIONS
742 1.1 agc int i;
743 1.42 christos #endif
744 1.1 agc
745 1.96 christos (void)snprintf(template, sizeof(template), "%s.XXXXXX",
746 1.94 rpaulo _PATH_USERMGMT_CONF);
747 1.1 agc if ((fd = mkstemp(template)) < 0) {
748 1.96 christos warn("Can't set defaults: can't mkstemp `%s' for writing",
749 1.96 christos _PATH_USERMGMT_CONF);
750 1.1 agc return 0;
751 1.1 agc }
752 1.24 simonb if ((fp = fdopen(fd, "w")) == NULL) {
753 1.96 christos warn("Can't set defaults: can't fdopen `%s' for writing",
754 1.96 christos _PATH_USERMGMT_CONF);
755 1.1 agc return 0;
756 1.1 agc }
757 1.1 agc ret = 1;
758 1.1 agc if (fprintf(fp, "group\t\t%s\n", up->u_primgrp) <= 0 ||
759 1.1 agc fprintf(fp, "base_dir\t%s\n", up->u_basedir) <= 0 ||
760 1.1 agc fprintf(fp, "skel_dir\t%s\n", up->u_skeldir) <= 0 ||
761 1.1 agc fprintf(fp, "shell\t\t%s\n", up->u_shell) <= 0 ||
762 1.42 christos #ifdef EXTENSIONS
763 1.42 christos fprintf(fp, "class\t\t%s\n", up->u_class) <= 0 ||
764 1.115 elad fprintf(fp, "homeperm\t0%o\n", up->u_homeperm) <= 0 ||
765 1.42 christos #endif
766 1.96 christos fprintf(fp, "inactive\t%s\n", (up->u_inactive == NULL) ?
767 1.82 christos UNSET_INACTIVE : up->u_inactive) <= 0 ||
768 1.82 christos fprintf(fp, "expire\t\t%s\n", (up->u_expire == NULL) ?
769 1.82 christos UNSET_EXPIRY : up->u_expire) <= 0 ||
770 1.82 christos fprintf(fp, "preserve\t%s\n", (up->u_preserve == 0) ?
771 1.82 christos "false" : "true") <= 0) {
772 1.94 rpaulo warn("Can't write to `%s'", _PATH_USERMGMT_CONF);
773 1.1 agc ret = 0;
774 1.1 agc }
775 1.1 agc #ifdef EXTENSIONS
776 1.82 christos for (i = (up->u_defrc != up->u_rc) ? up->u_defrc : 0;
777 1.82 christos i < up->u_rc ; i++) {
778 1.82 christos if (fprintf(fp, "range\t\t%d..%d\n", up->u_rv[i].r_from,
779 1.82 christos up->u_rv[i].r_to) <= 0) {
780 1.96 christos warn("Can't set defaults: can't write to `%s'",
781 1.96 christos _PATH_USERMGMT_CONF);
782 1.1 agc ret = 0;
783 1.1 agc }
784 1.1 agc }
785 1.1 agc #endif
786 1.82 christos (void)fclose(fp);
787 1.1 agc if (ret) {
788 1.94 rpaulo ret = ((rename(template, _PATH_USERMGMT_CONF) == 0) &&
789 1.94 rpaulo (chmod(_PATH_USERMGMT_CONF, 0644) == 0));
790 1.1 agc }
791 1.1 agc return ret;
792 1.1 agc }
793 1.1 agc
794 1.1 agc /* read the defaults file */
795 1.1 agc static void
796 1.125 mlelstv read_defaults(def_t *dp)
797 1.1 agc {
798 1.1 agc struct stat st;
799 1.1 agc size_t lineno;
800 1.1 agc size_t len;
801 1.1 agc FILE *fp;
802 1.82 christos char *cp;
803 1.82 christos char *s;
804 1.125 mlelstv user_t *up = &dp->user;
805 1.125 mlelstv group_t *gp = &dp->group;
806 1.125 mlelstv
807 1.125 mlelstv (void)memset(dp, 0, sizeof(*dp));
808 1.1 agc
809 1.1 agc memsave(&up->u_primgrp, DEF_GROUP, strlen(DEF_GROUP));
810 1.1 agc memsave(&up->u_basedir, DEF_BASEDIR, strlen(DEF_BASEDIR));
811 1.1 agc memsave(&up->u_skeldir, DEF_SKELDIR, strlen(DEF_SKELDIR));
812 1.1 agc memsave(&up->u_shell, DEF_SHELL, strlen(DEF_SHELL));
813 1.1 agc memsave(&up->u_comment, DEF_COMMENT, strlen(DEF_COMMENT));
814 1.42 christos #ifdef EXTENSIONS
815 1.42 christos memsave(&up->u_class, DEF_CLASS, strlen(DEF_CLASS));
816 1.42 christos #endif
817 1.1 agc up->u_rsize = 16;
818 1.49 agc up->u_defrc = 0;
819 1.1 agc NEWARRAY(range_t, up->u_rv, up->u_rsize, exit(1));
820 1.9 agc up->u_inactive = DEF_INACTIVE;
821 1.1 agc up->u_expire = DEF_EXPIRE;
822 1.132 zafer up->u_homeperm = DEF_HOMEPERM;
823 1.125 mlelstv gp->g_rsize = 16;
824 1.125 mlelstv gp->g_defrc = 0;
825 1.125 mlelstv NEWARRAY(range_t, gp->g_rv, gp->g_rsize, exit(1));
826 1.94 rpaulo if ((fp = fopen(_PATH_USERMGMT_CONF, "r")) == NULL) {
827 1.94 rpaulo if (stat(_PATH_USERMGMT_CONF, &st) < 0 && !setdefaults(up)) {
828 1.94 rpaulo warn("Can't create `%s' defaults file",
829 1.94 rpaulo _PATH_USERMGMT_CONF);
830 1.1 agc }
831 1.94 rpaulo fp = fopen(_PATH_USERMGMT_CONF, "r");
832 1.1 agc }
833 1.24 simonb if (fp != NULL) {
834 1.15 soren while ((s = fparseln(fp, &len, &lineno, NULL, 0)) != NULL) {
835 1.1 agc if (strncmp(s, "group", 5) == 0) {
836 1.82 christos cp = skipspace(s + 5);
837 1.82 christos memsave(&up->u_primgrp, (char *)cp, strlen(cp));
838 1.1 agc } else if (strncmp(s, "base_dir", 8) == 0) {
839 1.82 christos cp = skipspace(s + 8);
840 1.82 christos memsave(&up->u_basedir, (char *)cp, strlen(cp));
841 1.1 agc } else if (strncmp(s, "skel_dir", 8) == 0) {
842 1.82 christos cp = skipspace(s + 8);
843 1.82 christos memsave(&up->u_skeldir, (char *)cp, strlen(cp));
844 1.1 agc } else if (strncmp(s, "shell", 5) == 0) {
845 1.82 christos cp = skipspace(s + 5);
846 1.1 agc memsave(&up->u_shell, cp, strlen(cp));
847 1.42 christos #ifdef EXTENSIONS
848 1.82 christos } else if (strncmp((char *)s, "class", 5) == 0) {
849 1.82 christos cp = skipspace(s + 5);
850 1.42 christos memsave(&up->u_class, cp, strlen(cp));
851 1.42 christos #endif
852 1.115 elad #ifdef EXTENSIONS
853 1.115 elad } else if (strncmp(s, "homeperm", 8) == 0) {
854 1.115 elad for (cp = s + 8; *cp &&
855 1.115 elad isspace((unsigned char)*cp); cp++)
856 1.115 elad ;
857 1.115 elad up->u_homeperm = strtoul(cp, NULL, 8);
858 1.115 elad #endif
859 1.1 agc } else if (strncmp(s, "inactive", 8) == 0) {
860 1.82 christos cp = skipspace(s + 8);
861 1.52 grant if (strcmp(cp, UNSET_INACTIVE) == 0) {
862 1.52 grant if (up->u_inactive) {
863 1.52 grant FREE(up->u_inactive);
864 1.52 grant }
865 1.52 grant up->u_inactive = NULL;
866 1.52 grant } else {
867 1.52 grant memsave(&up->u_inactive, cp, strlen(cp));
868 1.52 grant }
869 1.1 agc #ifdef EXTENSIONS
870 1.1 agc } else if (strncmp(s, "range", 5) == 0) {
871 1.82 christos cp = skipspace(s + 5);
872 1.125 mlelstv (void)save_range(&up->u_r, cp);
873 1.1 agc #endif
874 1.1 agc #ifdef EXTENSIONS
875 1.1 agc } else if (strncmp(s, "preserve", 8) == 0) {
876 1.82 christos cp = skipspace(s + 8);
877 1.96 christos up->u_preserve =
878 1.82 christos (strncmp(cp, "true", 4) == 0) ? 1 :
879 1.82 christos (strncmp(cp, "yes", 3) == 0) ? 1 : atoi(cp);
880 1.1 agc #endif
881 1.1 agc } else if (strncmp(s, "expire", 6) == 0) {
882 1.82 christos cp = skipspace(s + 6);
883 1.1 agc if (strcmp(cp, UNSET_EXPIRY) == 0) {
884 1.1 agc if (up->u_expire) {
885 1.1 agc FREE(up->u_expire);
886 1.1 agc }
887 1.15 soren up->u_expire = NULL;
888 1.1 agc } else {
889 1.1 agc memsave(&up->u_expire, cp, strlen(cp));
890 1.1 agc }
891 1.125 mlelstv #ifdef EXTENSIONS
892 1.125 mlelstv } else if (strncmp(s, "gid_range", 9) == 0) {
893 1.125 mlelstv cp = skipspace(s + 9);
894 1.125 mlelstv (void)save_range(&gp->g_r, cp);
895 1.125 mlelstv #endif
896 1.1 agc }
897 1.82 christos (void)free(s);
898 1.1 agc }
899 1.82 christos (void)fclose(fp);
900 1.1 agc }
901 1.9 agc if (up->u_rc == 0) {
902 1.9 agc up->u_rv[up->u_rc].r_from = DEF_LOWUID;
903 1.9 agc up->u_rv[up->u_rc].r_to = DEF_HIGHUID;
904 1.9 agc up->u_rc += 1;
905 1.9 agc }
906 1.9 agc up->u_defrc = up->u_rc;
907 1.1 agc }
908 1.1 agc
909 1.1 agc /* return the next valid unused uid */
910 1.1 agc static int
911 1.1 agc getnextuid(int sync_uid_gid, int *uid, int low_uid, int high_uid)
912 1.1 agc {
913 1.1 agc for (*uid = low_uid ; *uid <= high_uid ; (*uid)++) {
914 1.24 simonb if (getpwuid((uid_t)(*uid)) == NULL && *uid != NOBODY_UID) {
915 1.1 agc if (sync_uid_gid) {
916 1.24 simonb if (getgrgid((gid_t)(*uid)) == NULL) {
917 1.1 agc return 1;
918 1.1 agc }
919 1.1 agc } else {
920 1.1 agc return 1;
921 1.1 agc }
922 1.1 agc }
923 1.1 agc }
924 1.1 agc return 0;
925 1.1 agc }
926 1.1 agc
927 1.61 agc /* structure which defines a password type */
928 1.61 agc typedef struct passwd_type_t {
929 1.62 agc const char *type; /* optional type descriptor */
930 1.82 christos size_t desc_length; /* length of type descriptor */
931 1.82 christos size_t length; /* length of password */
932 1.62 agc const char *regex; /* regexp to output the password */
933 1.82 christos size_t re_sub; /* subscript of regexp to use */
934 1.61 agc } passwd_type_t;
935 1.61 agc
936 1.61 agc static passwd_type_t passwd_types[] = {
937 1.76 agc { "$sha1", 5, 28, "\\$[^$]+\\$[^$]+\\$[^$]+\\$(.*)", 1 }, /* SHA1 */
938 1.124 hubertf { "$2a", 3, 53, "\\$[^$]+\\$[^$]+\\$(.*)", 1 }, /* Blowfish */
939 1.62 agc { "$1", 2, 34, NULL, 0 }, /* MD5 */
940 1.65 agc { "", 0, DES_Len,NULL, 0 }, /* standard DES */
941 1.82 christos { NULL, (size_t)~0, (size_t)~0, NULL, 0 }
942 1.82 christos /* none - terminate search */
943 1.61 agc };
944 1.61 agc
945 1.62 agc /* return non-zero if it's a valid password - check length for cipher type */
946 1.61 agc static int
947 1.62 agc valid_password_length(char *newpasswd)
948 1.61 agc {
949 1.61 agc passwd_type_t *pwtp;
950 1.62 agc regmatch_t matchv[10];
951 1.62 agc regex_t r;
952 1.61 agc
953 1.82 christos for (pwtp = passwd_types; pwtp->desc_length != (size_t)~0; pwtp++) {
954 1.61 agc if (strncmp(newpasswd, pwtp->type, pwtp->desc_length) == 0) {
955 1.62 agc if (pwtp->regex == NULL) {
956 1.62 agc return strlen(newpasswd) == pwtp->length;
957 1.62 agc }
958 1.82 christos (void)regcomp(&r, pwtp->regex, REG_EXTENDED);
959 1.62 agc if (regexec(&r, newpasswd, 10, matchv, 0) == 0) {
960 1.62 agc regfree(&r);
961 1.82 christos return (int)(matchv[pwtp->re_sub].rm_eo -
962 1.124 hubertf matchv[pwtp->re_sub].rm_so) ==
963 1.82 christos pwtp->length;
964 1.62 agc }
965 1.62 agc regfree(&r);
966 1.61 agc }
967 1.61 agc }
968 1.62 agc return 0;
969 1.61 agc }
970 1.61 agc
971 1.82 christos #ifdef EXTENSIONS
972 1.102 agc /* return 1 if `class' is a valid login class */
973 1.82 christos static int
974 1.82 christos valid_class(char *class)
975 1.82 christos {
976 1.82 christos login_cap_t *lc;
977 1.96 christos
978 1.93 agc if (class == NULL || *class == '\0') {
979 1.86 christos return 1;
980 1.93 agc }
981 1.82 christos /*
982 1.82 christos * Check if /etc/login.conf exists. login_getclass() will
983 1.82 christos * return 1 due to it not existing, so not informing the
984 1.82 christos * user the actual login class does not exist.
985 1.82 christos */
986 1.82 christos
987 1.129 dholland if (access(_PATH_LOGINCONF, R_OK) == -1) {
988 1.84 christos warn("Access failed for `%s'; will not validate class `%s'",
989 1.129 dholland _PATH_LOGINCONF, class);
990 1.84 christos return 1;
991 1.84 christos }
992 1.82 christos
993 1.86 christos if ((lc = login_getclass(class)) != NULL) {
994 1.82 christos login_close(lc);
995 1.86 christos return 1;
996 1.86 christos }
997 1.86 christos return 0;
998 1.82 christos }
999 1.99 christos
1000 1.102 agc /* return 1 if the `shellname' is a valid user shell */
1001 1.99 christos static int
1002 1.99 christos valid_shell(const char *shellname)
1003 1.99 christos {
1004 1.99 christos char *shellp;
1005 1.99 christos
1006 1.99 christos if (access(_PATH_SHELLS, R_OK) == -1) {
1007 1.99 christos /* Don't exit */
1008 1.99 christos warn("Access failed for `%s'; will not validate shell `%s'",
1009 1.99 christos _PATH_SHELLS, shellname);
1010 1.99 christos return 1;
1011 1.99 christos }
1012 1.99 christos
1013 1.102 agc /* if nologin is used as a shell, consider it a valid shell */
1014 1.129 dholland if (strcmp(shellname, _PATH_SBIN_NOLOGIN) == 0)
1015 1.102 agc return 1;
1016 1.100 christos
1017 1.104 christos while ((shellp = getusershell()) != NULL)
1018 1.104 christos if (strcmp(shellp, shellname) == 0)
1019 1.99 christos return 1;
1020 1.99 christos
1021 1.104 christos warnx("Shell `%s' not found in `%s'", shellname, _PATH_SHELLS);
1022 1.104 christos
1023 1.104 christos return access(shellname, X_OK) != -1;
1024 1.99 christos }
1025 1.82 christos #endif
1026 1.82 christos
1027 1.64 agc /* look for a valid time, return 0 if it was specified but bad */
1028 1.64 agc static int
1029 1.64 agc scantime(time_t *tp, char *s)
1030 1.64 agc {
1031 1.64 agc struct tm tm;
1032 1.87 christos char *ep;
1033 1.88 he long val;
1034 1.64 agc
1035 1.64 agc *tp = 0;
1036 1.64 agc if (s != NULL) {
1037 1.82 christos (void)memset(&tm, 0, sizeof(tm));
1038 1.64 agc if (strptime(s, "%c", &tm) != NULL) {
1039 1.64 agc *tp = mktime(&tm);
1040 1.93 agc return (*tp == -1) ? 0 : 1;
1041 1.64 agc } else if (strptime(s, "%B %d %Y", &tm) != NULL) {
1042 1.64 agc *tp = mktime(&tm);
1043 1.93 agc return (*tp == -1) ? 0 : 1;
1044 1.64 agc } else {
1045 1.88 he errno = 0;
1046 1.88 he *tp = val = strtol(s, &ep, 10);
1047 1.88 he if (*ep != '\0' || *tp < -1 || errno == ERANGE) {
1048 1.87 christos *tp = 0;
1049 1.87 christos return 0;
1050 1.87 christos }
1051 1.93 agc if (*tp != val) {
1052 1.88 he return 0;
1053 1.93 agc }
1054 1.64 agc }
1055 1.64 agc }
1056 1.64 agc return 1;
1057 1.64 agc }
1058 1.64 agc
1059 1.1 agc /* add a user */
1060 1.1 agc static int
1061 1.53 agc adduser(char *login_name, user_t *up)
1062 1.1 agc {
1063 1.1 agc struct group *grp;
1064 1.1 agc struct stat st;
1065 1.1 agc time_t expire;
1066 1.52 grant time_t inactive;
1067 1.1 agc char password[PasswordLength + 1];
1068 1.1 agc char home[MaxFileNameLen];
1069 1.1 agc char buf[MaxFileNameLen];
1070 1.1 agc int sync_uid_gid;
1071 1.1 agc int masterfd;
1072 1.1 agc int ptmpfd;
1073 1.1 agc int gid;
1074 1.1 agc int cc;
1075 1.1 agc int i;
1076 1.1 agc
1077 1.75 agc if (!valid_login(login_name, up->u_allow_samba)) {
1078 1.96 christos errx(EXIT_FAILURE, "Can't add user `%s': invalid login name", login_name);
1079 1.1 agc }
1080 1.82 christos #ifdef EXTENSIONS
1081 1.85 christos if (!valid_class(up->u_class)) {
1082 1.96 christos errx(EXIT_FAILURE, "Can't add user `%s': no such login class `%s'",
1083 1.96 christos login_name, up->u_class);
1084 1.82 christos }
1085 1.82 christos #endif
1086 1.20 agc if ((masterfd = open(_PATH_MASTERPASSWD, O_RDONLY)) < 0) {
1087 1.96 christos err(EXIT_FAILURE, "Can't add user `%s': can't open `%s'",
1088 1.96 christos login_name, _PATH_MASTERPASSWD);
1089 1.1 agc }
1090 1.1 agc if (flock(masterfd, LOCK_EX | LOCK_NB) < 0) {
1091 1.96 christos err(EXIT_FAILURE, "Can't add user `%s': can't lock `%s'",
1092 1.96 christos login_name, _PATH_MASTERPASSWD);
1093 1.1 agc }
1094 1.1 agc pw_init();
1095 1.1 agc if ((ptmpfd = pw_lock(WAITSECS)) < 0) {
1096 1.82 christos int serrno = errno;
1097 1.82 christos (void)close(masterfd);
1098 1.82 christos errno = serrno;
1099 1.96 christos err(EXIT_FAILURE, "Can't add user `%s': can't obtain pw_lock",
1100 1.96 christos login_name);
1101 1.1 agc }
1102 1.1 agc while ((cc = read(masterfd, buf, sizeof(buf))) > 0) {
1103 1.1 agc if (write(ptmpfd, buf, (size_t)(cc)) != cc) {
1104 1.82 christos int serrno = errno;
1105 1.82 christos (void)close(masterfd);
1106 1.82 christos (void)close(ptmpfd);
1107 1.82 christos (void)pw_abort();
1108 1.82 christos errno = serrno;
1109 1.96 christos err(EXIT_FAILURE, "Can't add user `%s': "
1110 1.96 christos "short write to /etc/ptmp", login_name);
1111 1.1 agc }
1112 1.1 agc }
1113 1.126 wiz (void)close(masterfd);
1114 1.1 agc /* if no uid was specified, get next one in [low_uid..high_uid] range */
1115 1.1 agc sync_uid_gid = (strcmp(up->u_primgrp, "=uid") == 0);
1116 1.1 agc if (up->u_uid == -1) {
1117 1.51 agc int got_id = 0;
1118 1.51 agc
1119 1.51 agc /*
1120 1.51 agc * Look for a free UID in the command line ranges (if any).
1121 1.51 agc * These start after the ranges specified in the config file.
1122 1.51 agc */
1123 1.51 agc for (i = up->u_defrc; !got_id && i < up->u_rc ; i++) {
1124 1.51 agc got_id = getnextuid(sync_uid_gid, &up->u_uid,
1125 1.51 agc up->u_rv[i].r_from, up->u_rv[i].r_to);
1126 1.51 agc }
1127 1.51 agc /*
1128 1.51 agc * If there were no free UIDs in the command line ranges,
1129 1.51 agc * try the ranges from the config file (there will always
1130 1.51 agc * be at least one default).
1131 1.51 agc */
1132 1.51 agc for (i = 0; !got_id && i < up->u_defrc; i++) {
1133 1.51 agc got_id = getnextuid(sync_uid_gid, &up->u_uid,
1134 1.51 agc up->u_rv[i].r_from, up->u_rv[i].r_to);
1135 1.1 agc }
1136 1.51 agc if (!got_id) {
1137 1.82 christos (void)close(ptmpfd);
1138 1.82 christos (void)pw_abort();
1139 1.96 christos errx(EXIT_FAILURE, "Can't add user `%s': "
1140 1.96 christos "can't get next uid for %d", login_name,
1141 1.82 christos up->u_uid);
1142 1.1 agc }
1143 1.1 agc }
1144 1.1 agc /* check uid isn't already allocated */
1145 1.26 simonb if (!(up->u_flags & F_DUPUID) && getpwuid((uid_t)(up->u_uid)) != NULL) {
1146 1.82 christos (void)close(ptmpfd);
1147 1.82 christos (void)pw_abort();
1148 1.96 christos errx(EXIT_FAILURE, "Can't add user `%s': "
1149 1.96 christos "uid %d is already in use", login_name, up->u_uid);
1150 1.1 agc }
1151 1.1 agc /* if -g=uid was specified, check gid is unused */
1152 1.1 agc if (sync_uid_gid) {
1153 1.24 simonb if (getgrgid((gid_t)(up->u_uid)) != NULL) {
1154 1.82 christos (void)close(ptmpfd);
1155 1.82 christos (void)pw_abort();
1156 1.96 christos errx(EXIT_FAILURE, "Can't add user `%s': "
1157 1.96 christos "gid %d is already in use", login_name,
1158 1.82 christos up->u_uid);
1159 1.1 agc }
1160 1.1 agc gid = up->u_uid;
1161 1.24 simonb } else if ((grp = getgrnam(up->u_primgrp)) != NULL) {
1162 1.1 agc gid = grp->gr_gid;
1163 1.9 agc } else if (is_number(up->u_primgrp) &&
1164 1.24 simonb (grp = getgrgid((gid_t)atoi(up->u_primgrp))) != NULL) {
1165 1.1 agc gid = grp->gr_gid;
1166 1.1 agc } else {
1167 1.82 christos (void)close(ptmpfd);
1168 1.82 christos (void)pw_abort();
1169 1.96 christos errx(EXIT_FAILURE, "Can't add user `%s': group %s not found",
1170 1.96 christos login_name, up->u_primgrp);
1171 1.1 agc }
1172 1.1 agc /* check name isn't already in use */
1173 1.53 agc if (!(up->u_flags & F_DUPUID) && getpwnam(login_name) != NULL) {
1174 1.82 christos (void)close(ptmpfd);
1175 1.82 christos (void)pw_abort();
1176 1.96 christos errx(EXIT_FAILURE, "Can't add user `%s': "
1177 1.96 christos "`%s' is already a user", login_name, login_name);
1178 1.1 agc }
1179 1.26 simonb if (up->u_flags & F_HOMEDIR) {
1180 1.82 christos (void)strlcpy(home, up->u_home, sizeof(home));
1181 1.13 jlam } else {
1182 1.13 jlam /* if home directory hasn't been given, make it up */
1183 1.82 christos (void)snprintf(home, sizeof(home), "%s/%s", up->u_basedir,
1184 1.82 christos login_name);
1185 1.1 agc }
1186 1.99 christos if (up->u_flags & F_SHELL) {
1187 1.99 christos #ifdef EXTENSIONS
1188 1.99 christos if (!valid_shell(up->u_shell)) {
1189 1.104 christos int oerrno = errno;
1190 1.99 christos (void)close(ptmpfd);
1191 1.99 christos (void)pw_abort();
1192 1.104 christos errno = oerrno;
1193 1.99 christos errx(EXIT_FAILURE, "Can't add user `%s': "
1194 1.104 christos "Cannot access shell `%s'",
1195 1.104 christos login_name, up->u_shell);
1196 1.99 christos }
1197 1.99 christos #endif
1198 1.99 christos }
1199 1.99 christos
1200 1.64 agc if (!scantime(&inactive, up->u_inactive)) {
1201 1.87 christos warnx("Warning: inactive time `%s' invalid, password expiry off",
1202 1.52 grant up->u_inactive);
1203 1.52 grant }
1204 1.87 christos if (!scantime(&expire, up->u_expire) || expire == -1) {
1205 1.87 christos warnx("Warning: expire time `%s' invalid, account expiry off",
1206 1.52 grant up->u_expire);
1207 1.87 christos expire = 0; /* Just in case. */
1208 1.21 agc }
1209 1.26 simonb if (lstat(home, &st) < 0 && !(up->u_flags & F_MKDIR)) {
1210 1.82 christos warnx("Warning: home directory `%s' doesn't exist, "
1211 1.82 christos "and -m was not specified", home);
1212 1.1 agc }
1213 1.60 itojun password[sizeof(password) - 1] = '\0';
1214 1.62 agc if (up->u_password != NULL && valid_password_length(up->u_password)) {
1215 1.82 christos (void)strlcpy(password, up->u_password, sizeof(password));
1216 1.1 agc } else {
1217 1.82 christos (void)memset(password, '*', DES_Len);
1218 1.65 agc password[DES_Len] = 0;
1219 1.15 soren if (up->u_password != NULL) {
1220 1.2 simonb warnx("Password `%s' is invalid: setting it to `%s'",
1221 1.9 agc up->u_password, password);
1222 1.1 agc }
1223 1.1 agc }
1224 1.52 grant cc = snprintf(buf, sizeof(buf), "%s:%s:%d:%d:%s:%ld:%ld:%s:%s:%s\n",
1225 1.53 agc login_name,
1226 1.1 agc password,
1227 1.1 agc up->u_uid,
1228 1.1 agc gid,
1229 1.42 christos #ifdef EXTENSIONS
1230 1.42 christos up->u_class,
1231 1.42 christos #else
1232 1.42 christos "",
1233 1.42 christos #endif
1234 1.52 grant (long) inactive,
1235 1.9 agc (long) expire,
1236 1.1 agc up->u_comment,
1237 1.1 agc home,
1238 1.1 agc up->u_shell);
1239 1.1 agc if (write(ptmpfd, buf, (size_t) cc) != cc) {
1240 1.82 christos int serrno = errno;
1241 1.82 christos (void)close(ptmpfd);
1242 1.82 christos (void)pw_abort();
1243 1.82 christos errno = serrno;
1244 1.96 christos err(EXIT_FAILURE, "Can't add user `%s': write failed",
1245 1.96 christos login_name);
1246 1.1 agc }
1247 1.26 simonb if (up->u_flags & F_MKDIR) {
1248 1.33 simonb if (lstat(home, &st) == 0) {
1249 1.82 christos (void)close(ptmpfd);
1250 1.82 christos (void)pw_abort();
1251 1.82 christos errx(EXIT_FAILURE,
1252 1.96 christos "Can't add user `%s': home directory `%s' "
1253 1.96 christos "already exists", login_name, home);
1254 1.33 simonb } else {
1255 1.129 dholland if (asystem("%s -p %s", _PATH_MKDIR, home) != 0) {
1256 1.82 christos (void)close(ptmpfd);
1257 1.82 christos (void)pw_abort();
1258 1.96 christos errx(EXIT_FAILURE, "Can't add user `%s': "
1259 1.96 christos "can't mkdir `%s'", login_name, home);
1260 1.33 simonb }
1261 1.115 elad (void)copydotfiles(up->u_skeldir, up->u_uid, gid, home,
1262 1.115 elad up->u_homeperm);
1263 1.1 agc }
1264 1.1 agc }
1265 1.1 agc if (strcmp(up->u_primgrp, "=uid") == 0 &&
1266 1.53 agc getgrnam(login_name) == NULL &&
1267 1.53 agc !creategid(login_name, gid, login_name)) {
1268 1.82 christos (void)close(ptmpfd);
1269 1.82 christos (void)pw_abort();
1270 1.96 christos errx(EXIT_FAILURE, "Can't add user `%s': can't create gid %d ",
1271 1.96 christos login_name, gid);
1272 1.82 christos }
1273 1.82 christos if (up->u_groupc > 0 &&
1274 1.82 christos !append_group(login_name, up->u_groupc, up->u_groupv)) {
1275 1.82 christos (void)close(ptmpfd);
1276 1.82 christos (void)pw_abort();
1277 1.96 christos errx(EXIT_FAILURE, "Can't add user `%s': can't append "
1278 1.96 christos "to new groups", login_name);
1279 1.1 agc }
1280 1.82 christos (void)close(ptmpfd);
1281 1.45 agc #if PW_MKDB_ARGC == 2
1282 1.82 christos if (pw_mkdb(login_name, 0) < 0)
1283 1.45 agc #else
1284 1.82 christos if (pw_mkdb() < 0)
1285 1.82 christos #endif
1286 1.82 christos {
1287 1.82 christos (void)pw_abort();
1288 1.96 christos errx(EXIT_FAILURE, "Can't add user `%s': pw_mkdb failed",
1289 1.96 christos login_name);
1290 1.45 agc }
1291 1.82 christos syslog(LOG_INFO, "New user added: name=%s, uid=%d, gid=%d, home=%s, "
1292 1.82 christos "shell=%s", login_name, up->u_uid, gid, home, up->u_shell);
1293 1.1 agc return 1;
1294 1.1 agc }
1295 1.1 agc
1296 1.53 agc /* remove a user from the groups file */
1297 1.53 agc static int
1298 1.53 agc rm_user_from_groups(char *login_name)
1299 1.53 agc {
1300 1.53 agc struct stat st;
1301 1.53 agc regmatch_t matchv[10];
1302 1.53 agc regex_t r;
1303 1.53 agc FILE *from;
1304 1.53 agc FILE *to;
1305 1.53 agc char line[MaxEntryLen];
1306 1.53 agc char buf[MaxEntryLen];
1307 1.53 agc char f[MaxFileNameLen];
1308 1.53 agc int fd;
1309 1.53 agc int cc;
1310 1.53 agc int sc;
1311 1.53 agc
1312 1.82 christos (void)snprintf(line, sizeof(line), "(:|,)(%s)(,|$)", login_name);
1313 1.82 christos if ((sc = regcomp(&r, line, REG_EXTENDED|REG_NEWLINE)) != 0) {
1314 1.82 christos (void)regerror(sc, &r, buf, sizeof(buf));
1315 1.82 christos warnx("Can't compile regular expression `%s' (%s)", line,
1316 1.82 christos buf);
1317 1.53 agc return 0;
1318 1.53 agc }
1319 1.53 agc if ((from = fopen(_PATH_GROUP, "r")) == NULL) {
1320 1.96 christos warn("Can't remove user `%s' from `%s': can't open `%s'",
1321 1.96 christos login_name, _PATH_GROUP, _PATH_GROUP);
1322 1.53 agc return 0;
1323 1.53 agc }
1324 1.53 agc if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) {
1325 1.96 christos warn("Can't remove user `%s' from `%s': can't lock `%s'",
1326 1.96 christos login_name, _PATH_GROUP, _PATH_GROUP);
1327 1.107 joerg (void)fclose(from);
1328 1.96 christos return 0;
1329 1.53 agc }
1330 1.82 christos (void)fstat(fileno(from), &st);
1331 1.82 christos (void)snprintf(f, sizeof(f), "%s.XXXXXX", _PATH_GROUP);
1332 1.53 agc if ((fd = mkstemp(f)) < 0) {
1333 1.96 christos warn("Can't remove user `%s' from `%s': mkstemp failed",
1334 1.96 christos login_name, _PATH_GROUP);
1335 1.82 christos (void)fclose(from);
1336 1.53 agc return 0;
1337 1.53 agc }
1338 1.53 agc if ((to = fdopen(fd, "w")) == NULL) {
1339 1.96 christos warn("Can't remove user `%s' from `%s': fdopen `%s' failed",
1340 1.96 christos login_name, _PATH_GROUP, f);
1341 1.82 christos (void)fclose(from);
1342 1.82 christos (void)close(fd);
1343 1.82 christos (void)unlink(f);
1344 1.53 agc return 0;
1345 1.53 agc }
1346 1.82 christos while (fgets(buf, sizeof(buf), from) != NULL) {
1347 1.53 agc cc = strlen(buf);
1348 1.53 agc if (regexec(&r, buf, 10, matchv, 0) == 0) {
1349 1.70 agc if (buf[(int)matchv[1].rm_so] == ',') {
1350 1.70 agc matchv[2].rm_so = matchv[1].rm_so;
1351 1.70 agc } else if (matchv[2].rm_eo != matchv[3].rm_eo) {
1352 1.70 agc matchv[2].rm_eo = matchv[3].rm_eo;
1353 1.53 agc }
1354 1.70 agc cc -= (int) matchv[2].rm_eo;
1355 1.70 agc sc = (int) matchv[2].rm_so;
1356 1.82 christos if (fwrite(buf, sizeof(char), (size_t)sc, to) != sc ||
1357 1.82 christos fwrite(&buf[(int)matchv[2].rm_eo], sizeof(char),
1358 1.82 christos (size_t)cc, to) != cc) {
1359 1.96 christos warn("Can't remove user `%s' from `%s': "
1360 1.96 christos "short write to `%s'", login_name,
1361 1.96 christos _PATH_GROUP, f);
1362 1.82 christos (void)fclose(from);
1363 1.82 christos (void)close(fd);
1364 1.82 christos (void)unlink(f);
1365 1.53 agc return 0;
1366 1.53 agc }
1367 1.53 agc } else if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) {
1368 1.96 christos warn("Can't remove user `%s' from `%s': "
1369 1.96 christos "short write to `%s'", login_name, _PATH_GROUP, f);
1370 1.82 christos (void)fclose(from);
1371 1.82 christos (void)close(fd);
1372 1.82 christos (void)unlink(f);
1373 1.53 agc return 0;
1374 1.53 agc }
1375 1.53 agc }
1376 1.82 christos (void)fclose(from);
1377 1.82 christos (void)fclose(to);
1378 1.53 agc if (rename(f, _PATH_GROUP) < 0) {
1379 1.96 christos warn("Can't remove user `%s' from `%s': "
1380 1.96 christos "can't rename `%s' to `%s'",
1381 1.96 christos login_name, _PATH_GROUP, f, _PATH_GROUP);
1382 1.82 christos (void)unlink(f);
1383 1.53 agc return 0;
1384 1.53 agc }
1385 1.82 christos (void)chmod(_PATH_GROUP, st.st_mode & 07777);
1386 1.53 agc return 1;
1387 1.53 agc }
1388 1.53 agc
1389 1.58 agc /* check that the user or group is local, not from YP/NIS */
1390 1.56 agc static int
1391 1.58 agc is_local(char *name, const char *file)
1392 1.56 agc {
1393 1.56 agc FILE *fp;
1394 1.56 agc char buf[MaxEntryLen];
1395 1.82 christos size_t len;
1396 1.56 agc int ret;
1397 1.56 agc
1398 1.58 agc if ((fp = fopen(file, "r")) == NULL) {
1399 1.82 christos err(EXIT_FAILURE, "Can't open `%s'", file);
1400 1.56 agc }
1401 1.75 agc len = strlen(name);
1402 1.56 agc for (ret = 0 ; fgets(buf, sizeof(buf), fp) != NULL ; ) {
1403 1.75 agc if (strncmp(buf, name, len) == 0 && buf[len] == ':') {
1404 1.56 agc ret = 1;
1405 1.56 agc break;
1406 1.56 agc }
1407 1.56 agc }
1408 1.82 christos (void)fclose(fp);
1409 1.56 agc return ret;
1410 1.56 agc }
1411 1.56 agc
1412 1.1 agc /* modify a user */
1413 1.1 agc static int
1414 1.75 agc moduser(char *login_name, char *newlogin, user_t *up, int allow_samba)
1415 1.1 agc {
1416 1.121 christos struct passwd *pwp, pw;
1417 1.56 agc struct group *grp;
1418 1.56 agc const char *homedir;
1419 1.82 christos char *locked_pwd;
1420 1.56 agc size_t colonc;
1421 1.56 agc size_t loginc;
1422 1.56 agc size_t len;
1423 1.56 agc FILE *master;
1424 1.45 agc char newdir[MaxFileNameLen];
1425 1.75 agc char buf[MaxEntryLen];
1426 1.121 christos char pwbuf[MaxEntryLen];
1427 1.56 agc char *colon;
1428 1.1 agc int masterfd;
1429 1.1 agc int ptmpfd;
1430 1.41 ad int error;
1431 1.1 agc
1432 1.75 agc if (!valid_login(newlogin, allow_samba)) {
1433 1.96 christos errx(EXIT_FAILURE, "Can't modify user `%s': invalid login name",
1434 1.82 christos login_name);
1435 1.1 agc }
1436 1.121 christos if (getpwnam_r(login_name, &pw, pwbuf, sizeof(pwbuf), &pwp) != 0
1437 1.121 christos || pwp == NULL) {
1438 1.96 christos errx(EXIT_FAILURE, "Can't modify user `%s': no such user",
1439 1.96 christos login_name);
1440 1.1 agc }
1441 1.58 agc if (!is_local(login_name, _PATH_MASTERPASSWD)) {
1442 1.96 christos errx(EXIT_FAILURE, "Can't modify user `%s': must be a local user",
1443 1.82 christos login_name);
1444 1.56 agc }
1445 1.35 wiz /* keep dir name in case we need it for '-m' */
1446 1.35 wiz homedir = pwp->pw_dir;
1447 1.35 wiz
1448 1.20 agc if ((masterfd = open(_PATH_MASTERPASSWD, O_RDONLY)) < 0) {
1449 1.96 christos err(EXIT_FAILURE, "Can't modify user `%s': can't open `%s'",
1450 1.96 christos login_name, _PATH_MASTERPASSWD);
1451 1.1 agc }
1452 1.1 agc if (flock(masterfd, LOCK_EX | LOCK_NB) < 0) {
1453 1.96 christos err(EXIT_FAILURE, "Can't modify user `%s': can't lock `%s'",
1454 1.96 christos login_name, _PATH_MASTERPASSWD);
1455 1.1 agc }
1456 1.1 agc pw_init();
1457 1.1 agc if ((ptmpfd = pw_lock(WAITSECS)) < 0) {
1458 1.82 christos int serrno = errno;
1459 1.82 christos (void)close(masterfd);
1460 1.82 christos errno = serrno;
1461 1.96 christos err(EXIT_FAILURE, "Can't modify user `%s': "
1462 1.96 christos "can't obtain pw_lock", login_name);
1463 1.1 agc }
1464 1.24 simonb if ((master = fdopen(masterfd, "r")) == NULL) {
1465 1.82 christos int serrno = errno;
1466 1.82 christos (void)close(masterfd);
1467 1.82 christos (void)close(ptmpfd);
1468 1.82 christos (void)pw_abort();
1469 1.82 christos errno = serrno;
1470 1.96 christos err(EXIT_FAILURE, "Can't modify user `%s': "
1471 1.96 christos "fdopen fd for %s", login_name, _PATH_MASTERPASSWD);
1472 1.1 agc }
1473 1.24 simonb if (up != NULL) {
1474 1.26 simonb if (up->u_flags & F_USERNAME) {
1475 1.82 christos /*
1476 1.82 christos * If changing name,
1477 1.82 christos * check new name isn't already in use
1478 1.82 christos */
1479 1.82 christos if (strcmp(login_name, newlogin) != 0 &&
1480 1.82 christos getpwnam(newlogin) != NULL) {
1481 1.82 christos (void)close(ptmpfd);
1482 1.82 christos (void)pw_abort();
1483 1.96 christos errx(EXIT_FAILURE, "Can't modify user `%s': "
1484 1.96 christos "`%s' is already a user", login_name,
1485 1.82 christos newlogin);
1486 1.26 simonb }
1487 1.35 wiz pwp->pw_name = newlogin;
1488 1.36 wiz
1489 1.36 wiz /*
1490 1.36 wiz * Provide a new directory name in case the
1491 1.36 wiz * home directory is to be moved.
1492 1.36 wiz */
1493 1.36 wiz if (up->u_flags & F_MKDIR) {
1494 1.82 christos (void)snprintf(newdir, sizeof(newdir), "%s/%s",
1495 1.82 christos up->u_basedir, newlogin);
1496 1.36 wiz pwp->pw_dir = newdir;
1497 1.36 wiz }
1498 1.1 agc }
1499 1.26 simonb if (up->u_flags & F_PASSWORD) {
1500 1.62 agc if (up->u_password != NULL) {
1501 1.63 itojun if (!valid_password_length(up->u_password)) {
1502 1.82 christos (void)close(ptmpfd);
1503 1.82 christos (void)pw_abort();
1504 1.82 christos errx(EXIT_FAILURE,
1505 1.96 christos "Can't modify user `%s': "
1506 1.96 christos "invalid password: `%s'",
1507 1.96 christos login_name, up->u_password);
1508 1.61 agc }
1509 1.82 christos if ((locked_pwd =
1510 1.82 christos strstr(pwp->pw_passwd, LOCKED)) != NULL) {
1511 1.82 christos /*
1512 1.82 christos * account is locked - keep it locked
1513 1.82 christos * and just change the password.
1514 1.82 christos */
1515 1.82 christos if (asprintf(&locked_pwd, "%s%s",
1516 1.93 agc LOCKED, up->u_password) == -1) {
1517 1.95 christos (void)close(ptmpfd);
1518 1.95 christos (void)pw_abort();
1519 1.82 christos err(EXIT_FAILURE,
1520 1.96 christos "Can't modify user `%s': "
1521 1.96 christos "asprintf failed",
1522 1.96 christos login_name);
1523 1.93 agc }
1524 1.82 christos pwp->pw_passwd = locked_pwd;
1525 1.93 agc } else {
1526 1.82 christos pwp->pw_passwd = up->u_password;
1527 1.93 agc }
1528 1.82 christos }
1529 1.82 christos }
1530 1.96 christos
1531 1.82 christos /* check whether we should lock the account. */
1532 1.82 christos if (up->u_locked == LOCK) {
1533 1.82 christos /* check to see account if already locked. */
1534 1.82 christos if ((locked_pwd = strstr(pwp->pw_passwd, LOCKED))
1535 1.93 agc != NULL) {
1536 1.82 christos warnx("Account is already locked");
1537 1.93 agc } else {
1538 1.82 christos if (asprintf(&locked_pwd, "%s%s", LOCKED,
1539 1.93 agc pwp->pw_passwd) == -1) {
1540 1.95 christos (void)close(ptmpfd);
1541 1.95 christos (void)pw_abort();
1542 1.96 christos err(EXIT_FAILURE,
1543 1.96 christos "Can't modify user `%s': "
1544 1.96 christos "asprintf failed", login_name);
1545 1.93 agc }
1546 1.82 christos pwp->pw_passwd = locked_pwd;
1547 1.82 christos }
1548 1.82 christos } else if (up->u_locked == UNLOCK) {
1549 1.82 christos if ((locked_pwd = strstr(pwp->pw_passwd, LOCKED))
1550 1.93 agc == NULL) {
1551 1.96 christos warnx("Can't modify user `%s': "
1552 1.96 christos "account is not locked", login_name);
1553 1.93 agc } else {
1554 1.82 christos pwp->pw_passwd = locked_pwd + strlen(LOCKED);
1555 1.93 agc }
1556 1.26 simonb }
1557 1.82 christos
1558 1.26 simonb if (up->u_flags & F_UID) {
1559 1.26 simonb /* check uid isn't already allocated */
1560 1.82 christos if (!(up->u_flags & F_DUPUID) &&
1561 1.82 christos getpwuid((uid_t)(up->u_uid)) != NULL) {
1562 1.82 christos (void)close(ptmpfd);
1563 1.82 christos (void)pw_abort();
1564 1.96 christos errx(EXIT_FAILURE, "Can't modify user `%s': "
1565 1.96 christos "uid `%d' is already in use", login_name,
1566 1.82 christos up->u_uid);
1567 1.26 simonb }
1568 1.35 wiz pwp->pw_uid = up->u_uid;
1569 1.1 agc }
1570 1.26 simonb if (up->u_flags & F_GROUP) {
1571 1.26 simonb /* if -g=uid was specified, check gid is unused */
1572 1.26 simonb if (strcmp(up->u_primgrp, "=uid") == 0) {
1573 1.123 mike if (getgrgid((gid_t)(pwp->pw_uid)) != NULL) {
1574 1.82 christos (void)close(ptmpfd);
1575 1.82 christos (void)pw_abort();
1576 1.96 christos errx(EXIT_FAILURE,
1577 1.96 christos "Can't modify user `%s': "
1578 1.96 christos "gid %d is already in use",
1579 1.131 blymn login_name, pwp->pw_uid);
1580 1.26 simonb }
1581 1.123 mike pwp->pw_gid = pwp->pw_uid;
1582 1.130 blymn if (!creategid(newlogin, pwp->pw_uid, "")) {
1583 1.130 blymn errx(EXIT_FAILURE,
1584 1.130 blymn "Could not create group %s "
1585 1.130 blymn "with uid %d", newlogin,
1586 1.130 blymn up->u_uid);
1587 1.130 blymn }
1588 1.26 simonb } else if ((grp = getgrnam(up->u_primgrp)) != NULL) {
1589 1.35 wiz pwp->pw_gid = grp->gr_gid;
1590 1.26 simonb } else if (is_number(up->u_primgrp) &&
1591 1.82 christos (grp = getgrgid(
1592 1.82 christos (gid_t)atoi(up->u_primgrp))) != NULL) {
1593 1.35 wiz pwp->pw_gid = grp->gr_gid;
1594 1.26 simonb } else {
1595 1.82 christos (void)close(ptmpfd);
1596 1.82 christos (void)pw_abort();
1597 1.96 christos errx(EXIT_FAILURE, "Can't modify user `%s': "
1598 1.96 christos "group %s not found", login_name,
1599 1.82 christos up->u_primgrp);
1600 1.1 agc }
1601 1.1 agc }
1602 1.52 grant if (up->u_flags & F_INACTIVE) {
1603 1.64 agc if (!scantime(&pwp->pw_change, up->u_inactive)) {
1604 1.82 christos warnx("Warning: inactive time `%s' invalid, "
1605 1.82 christos "password expiry off",
1606 1.52 grant up->u_inactive);
1607 1.52 grant }
1608 1.52 grant }
1609 1.26 simonb if (up->u_flags & F_EXPIRE) {
1610 1.87 christos if (!scantime(&pwp->pw_expire, up->u_expire) ||
1611 1.87 christos pwp->pw_expire == -1) {
1612 1.82 christos warnx("Warning: expire time `%s' invalid, "
1613 1.87 christos "account expiry off",
1614 1.52 grant up->u_expire);
1615 1.87 christos pwp->pw_expire = 0;
1616 1.52 grant }
1617 1.1 agc }
1618 1.93 agc if (up->u_flags & F_COMMENT) {
1619 1.35 wiz pwp->pw_gecos = up->u_comment;
1620 1.93 agc }
1621 1.93 agc if (up->u_flags & F_HOMEDIR) {
1622 1.35 wiz pwp->pw_dir = up->u_home;
1623 1.93 agc }
1624 1.93 agc if (up->u_flags & F_SHELL) {
1625 1.99 christos #ifdef EXTENSIONS
1626 1.99 christos if (!valid_shell(up->u_shell)) {
1627 1.104 christos int oerrno = errno;
1628 1.99 christos (void)close(ptmpfd);
1629 1.99 christos (void)pw_abort();
1630 1.104 christos errno = oerrno;
1631 1.99 christos errx(EXIT_FAILURE, "Can't modify user `%s': "
1632 1.104 christos "Cannot access shell `%s'",
1633 1.104 christos login_name, up->u_shell);
1634 1.99 christos }
1635 1.99 christos pwp->pw_shell = up->u_shell;
1636 1.99 christos #else
1637 1.99 christos pwp->pw_shell = up->u_shell;
1638 1.99 christos #endif
1639 1.93 agc }
1640 1.42 christos #ifdef EXTENSIONS
1641 1.82 christos if (up->u_flags & F_CLASS) {
1642 1.82 christos if (!valid_class(up->u_class)) {
1643 1.82 christos (void)close(ptmpfd);
1644 1.82 christos (void)pw_abort();
1645 1.96 christos errx(EXIT_FAILURE, "Can't modify user `%s': "
1646 1.96 christos "no such login class `%s'", login_name,
1647 1.82 christos up->u_class);
1648 1.82 christos }
1649 1.42 christos pwp->pw_class = up->u_class;
1650 1.82 christos }
1651 1.42 christos #endif
1652 1.1 agc }
1653 1.53 agc loginc = strlen(login_name);
1654 1.74 agc while (fgets(buf, sizeof(buf), master) != NULL) {
1655 1.74 agc if ((colon = strchr(buf, ':')) == NULL) {
1656 1.74 agc warnx("Malformed entry `%s'. Skipping", buf);
1657 1.1 agc continue;
1658 1.1 agc }
1659 1.74 agc colonc = (size_t)(colon - buf);
1660 1.74 agc if (strncmp(login_name, buf, loginc) == 0 && loginc == colonc) {
1661 1.24 simonb if (up != NULL) {
1662 1.74 agc len = snprintf(buf, sizeof(buf), "%s:%s:%d:%d:"
1663 1.42 christos #ifdef EXTENSIONS
1664 1.96 christos "%s"
1665 1.42 christos #endif
1666 1.82 christos ":%ld:%ld:%s:%s:%s\n",
1667 1.82 christos newlogin,
1668 1.82 christos pwp->pw_passwd,
1669 1.82 christos pwp->pw_uid,
1670 1.82 christos pwp->pw_gid,
1671 1.82 christos #ifdef EXTENSIONS
1672 1.82 christos pwp->pw_class,
1673 1.82 christos #endif
1674 1.82 christos (long)pwp->pw_change,
1675 1.82 christos (long)pwp->pw_expire,
1676 1.82 christos pwp->pw_gecos,
1677 1.82 christos pwp->pw_dir,
1678 1.82 christos pwp->pw_shell);
1679 1.26 simonb if (write(ptmpfd, buf, len) != len) {
1680 1.82 christos int serrno = errno;
1681 1.82 christos (void)close(ptmpfd);
1682 1.82 christos (void)pw_abort();
1683 1.82 christos errno = serrno;
1684 1.96 christos err(EXIT_FAILURE, "Can't modify user "
1685 1.96 christos "`%s': write", login_name);
1686 1.1 agc }
1687 1.1 agc }
1688 1.74 agc } else {
1689 1.74 agc len = strlen(buf);
1690 1.108 joerg if (write(ptmpfd, buf, len) != len) {
1691 1.82 christos int serrno = errno;
1692 1.82 christos (void)close(masterfd);
1693 1.82 christos (void)close(ptmpfd);
1694 1.82 christos (void)pw_abort();
1695 1.82 christos errno = serrno;
1696 1.96 christos err(EXIT_FAILURE, "Can't modify `%s': "
1697 1.96 christos "write", login_name);
1698 1.74 agc }
1699 1.1 agc }
1700 1.1 agc }
1701 1.24 simonb if (up != NULL) {
1702 1.26 simonb if ((up->u_flags & F_MKDIR) &&
1703 1.129 dholland asystem("%s %s %s", _PATH_MV, homedir, pwp->pw_dir) != 0) {
1704 1.82 christos (void)close(ptmpfd);
1705 1.82 christos (void)pw_abort();
1706 1.96 christos errx(EXIT_FAILURE, "Can't modify user `%s': "
1707 1.96 christos "can't move `%s' to `%s'",
1708 1.96 christos login_name, homedir, pwp->pw_dir);
1709 1.23 agc }
1710 1.23 agc if (up->u_groupc > 0 &&
1711 1.23 agc !append_group(newlogin, up->u_groupc, up->u_groupv)) {
1712 1.82 christos (void)close(ptmpfd);
1713 1.82 christos (void)pw_abort();
1714 1.96 christos errx(EXIT_FAILURE, "Can't modify user `%s': "
1715 1.96 christos "can't append `%s' to new groups",
1716 1.96 christos login_name, newlogin);
1717 1.23 agc }
1718 1.1 agc }
1719 1.82 christos (void)close(ptmpfd);
1720 1.109 jnemeth (void)fclose(master);
1721 1.45 agc #if PW_MKDB_ARGC == 2
1722 1.53 agc if (up != NULL && strcmp(login_name, newlogin) == 0) {
1723 1.53 agc error = pw_mkdb(login_name, 0);
1724 1.41 ad } else {
1725 1.41 ad error = pw_mkdb(NULL, 0);
1726 1.41 ad }
1727 1.45 agc #else
1728 1.45 agc error = pw_mkdb();
1729 1.45 agc #endif
1730 1.41 ad if (error < 0) {
1731 1.82 christos (void)pw_abort();
1732 1.96 christos errx(EXIT_FAILURE, "Can't modify user `%s': pw_mkdb failed",
1733 1.96 christos login_name);
1734 1.1 agc }
1735 1.59 agc if (up == NULL) {
1736 1.82 christos syslog(LOG_INFO, "User removed: name=%s", login_name);
1737 1.59 agc } else if (strcmp(login_name, newlogin) == 0) {
1738 1.82 christos syslog(LOG_INFO, "User information modified: name=%s, uid=%d, "
1739 1.96 christos "gid=%d, home=%s, shell=%s",
1740 1.82 christos login_name, pwp->pw_uid, pwp->pw_gid, pwp->pw_dir,
1741 1.82 christos pwp->pw_shell);
1742 1.59 agc } else {
1743 1.82 christos syslog(LOG_INFO, "User information modified: name=%s, "
1744 1.96 christos "new name=%s, uid=%d, gid=%d, home=%s, shell=%s",
1745 1.82 christos login_name, newlogin, pwp->pw_uid, pwp->pw_gid,
1746 1.82 christos pwp->pw_dir, pwp->pw_shell);
1747 1.59 agc }
1748 1.1 agc return 1;
1749 1.1 agc }
1750 1.1 agc
1751 1.9 agc #ifdef EXTENSIONS
1752 1.9 agc /* see if we can find out the user struct */
1753 1.9 agc static struct passwd *
1754 1.81 agc find_user_info(const char *name)
1755 1.9 agc {
1756 1.9 agc struct passwd *pwp;
1757 1.9 agc
1758 1.24 simonb if ((pwp = getpwnam(name)) != NULL) {
1759 1.9 agc return pwp;
1760 1.9 agc }
1761 1.24 simonb if (is_number(name) && (pwp = getpwuid((uid_t)atoi(name))) != NULL) {
1762 1.9 agc return pwp;
1763 1.9 agc }
1764 1.24 simonb return NULL;
1765 1.9 agc }
1766 1.9 agc #endif
1767 1.9 agc
1768 1.9 agc /* see if we can find out the group struct */
1769 1.9 agc static struct group *
1770 1.81 agc find_group_info(const char *name)
1771 1.9 agc {
1772 1.9 agc struct group *grp;
1773 1.9 agc
1774 1.24 simonb if ((grp = getgrnam(name)) != NULL) {
1775 1.9 agc return grp;
1776 1.9 agc }
1777 1.24 simonb if (is_number(name) && (grp = getgrgid((gid_t)atoi(name))) != NULL) {
1778 1.9 agc return grp;
1779 1.9 agc }
1780 1.24 simonb return NULL;
1781 1.9 agc }
1782 1.9 agc
1783 1.1 agc /* print out usage message, and then exit */
1784 1.9 agc void
1785 1.38 cgd usermgmt_usage(const char *prog)
1786 1.1 agc {
1787 1.1 agc if (strcmp(prog, "useradd") == 0) {
1788 1.89 wiz (void)fprintf(stderr, "usage: %s -D [-F] [-b base-dir] "
1789 1.89 wiz "[-e expiry-time] [-f inactive-time]\n"
1790 1.89 wiz "\t[-g gid | name | =uid] [-k skel-dir] [-L login-class]\n"
1791 1.116 wiz "\t[-M homeperm] [-r lowuid..highuid] [-s shell]\n", prog);
1792 1.89 wiz (void)fprintf(stderr, "usage: %s [-moSv] [-b base-dir] "
1793 1.89 wiz "[-c comment] [-d home-dir] [-e expiry-time]\n"
1794 1.89 wiz "\t[-f inactive-time] [-G secondary-group] "
1795 1.116 wiz "[-g gid | name | =uid]\n"
1796 1.116 wiz "\t[-k skeletondir] [-L login-class] [-M homeperm] "
1797 1.116 wiz "[-p password]\n"
1798 1.116 wiz "\t[-r lowuid..highuid] [-s shell] [-u uid] user\n",
1799 1.16 soren prog);
1800 1.9 agc } else if (strcmp(prog, "usermod") == 0) {
1801 1.89 wiz (void)fprintf(stderr, "usage: %s [-FmoSv] [-C yes/no] "
1802 1.89 wiz "[-c comment] [-d home-dir] [-e expiry-time]\n"
1803 1.89 wiz "\t[-f inactive] [-G secondary-group] "
1804 1.89 wiz "[-g gid | name | =uid]\n"
1805 1.89 wiz "\t[-L login-class] [-l new-login] [-p password] "
1806 1.89 wiz "[-s shell] [-u uid]\n"
1807 1.89 wiz "\tuser\n", prog);
1808 1.7 lukem } else if (strcmp(prog, "userdel") == 0) {
1809 1.89 wiz (void)fprintf(stderr, "usage: %s -D [-p preserve-value]\n", prog);
1810 1.82 christos (void)fprintf(stderr,
1811 1.89 wiz "usage: %s [-rSv] [-p preserve-value] user\n", prog);
1812 1.9 agc #ifdef EXTENSIONS
1813 1.9 agc } else if (strcmp(prog, "userinfo") == 0) {
1814 1.119 reed (void)fprintf(stderr, "usage: %s [-e] user\n", prog);
1815 1.9 agc #endif
1816 1.1 agc } else if (strcmp(prog, "groupadd") == 0) {
1817 1.90 wiz (void)fprintf(stderr, "usage: %s [-ov] [-g gid]"
1818 1.90 wiz " [-r lowgid..highgid] group\n", prog);
1819 1.1 agc } else if (strcmp(prog, "groupdel") == 0) {
1820 1.82 christos (void)fprintf(stderr, "usage: %s [-v] group\n", prog);
1821 1.1 agc } else if (strcmp(prog, "groupmod") == 0) {
1822 1.82 christos (void)fprintf(stderr,
1823 1.90 wiz "usage: %s [-ov] [-g gid] [-n newname] group\n", prog);
1824 1.9 agc } else if (strcmp(prog, "user") == 0 || strcmp(prog, "group") == 0) {
1825 1.82 christos (void)fprintf(stderr,
1826 1.73 jmmv "usage: %s ( add | del | mod | info ) ...\n", prog);
1827 1.9 agc #ifdef EXTENSIONS
1828 1.9 agc } else if (strcmp(prog, "groupinfo") == 0) {
1829 1.90 wiz (void)fprintf(stderr, "usage: %s [-ev] group\n", prog);
1830 1.9 agc #endif
1831 1.1 agc }
1832 1.1 agc exit(EXIT_FAILURE);
1833 1.1 agc /* NOTREACHED */
1834 1.1 agc }
1835 1.1 agc
1836 1.1 agc #ifdef EXTENSIONS
1837 1.115 elad #define ADD_OPT_EXTENSIONS "M:p:r:vL:S"
1838 1.1 agc #else
1839 1.96 christos #define ADD_OPT_EXTENSIONS
1840 1.1 agc #endif
1841 1.1 agc
1842 1.9 agc int
1843 1.1 agc useradd(int argc, char **argv)
1844 1.1 agc {
1845 1.125 mlelstv def_t def;
1846 1.125 mlelstv user_t *up = &def.user;
1847 1.1 agc int defaultfield;
1848 1.1 agc int bigD;
1849 1.1 agc int c;
1850 1.42 christos #ifdef EXTENSIONS
1851 1.1 agc int i;
1852 1.42 christos #endif
1853 1.1 agc
1854 1.125 mlelstv read_defaults(&def);
1855 1.125 mlelstv up->u_uid = -1;
1856 1.1 agc defaultfield = bigD = 0;
1857 1.87 christos while ((c = getopt(argc, argv, "DFG:b:c:d:e:f:g:k:mou:s:"
1858 1.82 christos ADD_OPT_EXTENSIONS)) != -1) {
1859 1.1 agc switch(c) {
1860 1.1 agc case 'D':
1861 1.1 agc bigD = 1;
1862 1.1 agc break;
1863 1.87 christos case 'F':
1864 1.87 christos /*
1865 1.87 christos * Setting -1 will force the new user to
1866 1.87 christos * change their password as soon as they
1867 1.89 wiz * next log in - passwd(5).
1868 1.87 christos */
1869 1.87 christos defaultfield = 1;
1870 1.125 mlelstv memsave(&up->u_inactive, "-1", strlen("-1"));
1871 1.87 christos break;
1872 1.1 agc case 'G':
1873 1.125 mlelstv while (up->u_groupc < NGROUPS_MAX &&
1874 1.125 mlelstv (up->u_groupv[up->u_groupc] = strsep(&optarg, ",")) != NULL) {
1875 1.125 mlelstv if (up->u_groupv[up->u_groupc][0] != 0) {
1876 1.125 mlelstv up->u_groupc++;
1877 1.20 agc }
1878 1.20 agc }
1879 1.20 agc if (optarg != NULL) {
1880 1.82 christos warnx("Truncated list of secondary groups "
1881 1.82 christos "to %d entries", NGROUPS_MAX);
1882 1.20 agc }
1883 1.1 agc break;
1884 1.75 agc #ifdef EXTENSIONS
1885 1.75 agc case 'S':
1886 1.125 mlelstv up->u_allow_samba = 1;
1887 1.75 agc break;
1888 1.75 agc #endif
1889 1.1 agc case 'b':
1890 1.1 agc defaultfield = 1;
1891 1.125 mlelstv memsave(&up->u_basedir, optarg, strlen(optarg));
1892 1.1 agc break;
1893 1.1 agc case 'c':
1894 1.125 mlelstv memsave(&up->u_comment, optarg, strlen(optarg));
1895 1.1 agc break;
1896 1.1 agc case 'd':
1897 1.125 mlelstv memsave(&up->u_home, optarg, strlen(optarg));
1898 1.125 mlelstv up->u_flags |= F_HOMEDIR;
1899 1.1 agc break;
1900 1.1 agc case 'e':
1901 1.1 agc defaultfield = 1;
1902 1.125 mlelstv memsave(&up->u_expire, optarg, strlen(optarg));
1903 1.1 agc break;
1904 1.1 agc case 'f':
1905 1.1 agc defaultfield = 1;
1906 1.125 mlelstv memsave(&up->u_inactive, optarg, strlen(optarg));
1907 1.1 agc break;
1908 1.1 agc case 'g':
1909 1.1 agc defaultfield = 1;
1910 1.125 mlelstv memsave(&up->u_primgrp, optarg, strlen(optarg));
1911 1.1 agc break;
1912 1.1 agc case 'k':
1913 1.50 agc defaultfield = 1;
1914 1.125 mlelstv memsave(&up->u_skeldir, optarg, strlen(optarg));
1915 1.1 agc break;
1916 1.42 christos #ifdef EXTENSIONS
1917 1.42 christos case 'L':
1918 1.42 christos defaultfield = 1;
1919 1.125 mlelstv memsave(&up->u_class, optarg, strlen(optarg));
1920 1.42 christos break;
1921 1.42 christos #endif
1922 1.1 agc case 'm':
1923 1.125 mlelstv up->u_flags |= F_MKDIR;
1924 1.1 agc break;
1925 1.115 elad #ifdef EXTENSIONS
1926 1.115 elad case 'M':
1927 1.115 elad defaultfield = 1;
1928 1.125 mlelstv up->u_homeperm = strtoul(optarg, NULL, 8);
1929 1.115 elad break;
1930 1.115 elad #endif
1931 1.1 agc case 'o':
1932 1.125 mlelstv up->u_flags |= F_DUPUID;
1933 1.1 agc break;
1934 1.1 agc #ifdef EXTENSIONS
1935 1.1 agc case 'p':
1936 1.125 mlelstv memsave(&up->u_password, optarg, strlen(optarg));
1937 1.1 agc break;
1938 1.1 agc #endif
1939 1.1 agc #ifdef EXTENSIONS
1940 1.1 agc case 'r':
1941 1.1 agc defaultfield = 1;
1942 1.125 mlelstv (void)save_range(&up->u_r, optarg);
1943 1.1 agc break;
1944 1.1 agc #endif
1945 1.1 agc case 's':
1946 1.125 mlelstv up->u_flags |= F_SHELL;
1947 1.1 agc defaultfield = 1;
1948 1.125 mlelstv memsave(&up->u_shell, optarg, strlen(optarg));
1949 1.1 agc break;
1950 1.1 agc case 'u':
1951 1.125 mlelstv up->u_uid = check_numeric(optarg, "uid");
1952 1.1 agc break;
1953 1.1 agc #ifdef EXTENSIONS
1954 1.1 agc case 'v':
1955 1.1 agc verbose = 1;
1956 1.1 agc break;
1957 1.1 agc #endif
1958 1.69 agc default:
1959 1.69 agc usermgmt_usage("useradd");
1960 1.69 agc /* NOTREACHED */
1961 1.1 agc }
1962 1.1 agc }
1963 1.1 agc if (bigD) {
1964 1.1 agc if (defaultfield) {
1965 1.9 agc checkeuid();
1966 1.125 mlelstv return setdefaults(up) ? EXIT_SUCCESS : EXIT_FAILURE;
1967 1.1 agc }
1968 1.125 mlelstv (void)printf("group\t\t%s\n", up->u_primgrp);
1969 1.125 mlelstv (void)printf("base_dir\t%s\n", up->u_basedir);
1970 1.125 mlelstv (void)printf("skel_dir\t%s\n", up->u_skeldir);
1971 1.125 mlelstv (void)printf("shell\t\t%s\n", up->u_shell);
1972 1.125 mlelstv #ifdef EXTENSIONS
1973 1.125 mlelstv (void)printf("class\t\t%s\n", up->u_class);
1974 1.125 mlelstv (void)printf("homeperm\t0%o\n", up->u_homeperm);
1975 1.125 mlelstv #endif
1976 1.125 mlelstv (void)printf("inactive\t%s\n", (up->u_inactive == NULL) ?
1977 1.125 mlelstv UNSET_INACTIVE : up->u_inactive);
1978 1.125 mlelstv (void)printf("expire\t\t%s\n", (up->u_expire == NULL) ?
1979 1.125 mlelstv UNSET_EXPIRY : up->u_expire);
1980 1.1 agc #ifdef EXTENSIONS
1981 1.125 mlelstv for (i = 0 ; i < up->u_rc ; i++) {
1982 1.82 christos (void)printf("range\t\t%d..%d\n",
1983 1.125 mlelstv up->u_rv[i].r_from, up->u_rv[i].r_to);
1984 1.1 agc }
1985 1.1 agc #endif
1986 1.1 agc return EXIT_SUCCESS;
1987 1.1 agc }
1988 1.39 itojun argc -= optind;
1989 1.39 itojun argv += optind;
1990 1.39 itojun if (argc != 1) {
1991 1.9 agc usermgmt_usage("useradd");
1992 1.1 agc }
1993 1.9 agc checkeuid();
1994 1.59 agc openlog("useradd", LOG_PID, LOG_USER);
1995 1.125 mlelstv return adduser(*argv, up) ? EXIT_SUCCESS : EXIT_FAILURE;
1996 1.1 agc }
1997 1.1 agc
1998 1.1 agc #ifdef EXTENSIONS
1999 1.75 agc #define MOD_OPT_EXTENSIONS "p:vL:S"
2000 1.1 agc #else
2001 1.96 christos #define MOD_OPT_EXTENSIONS
2002 1.1 agc #endif
2003 1.1 agc
2004 1.9 agc int
2005 1.1 agc usermod(int argc, char **argv)
2006 1.1 agc {
2007 1.125 mlelstv def_t def;
2008 1.125 mlelstv user_t *up = &def.user;
2009 1.1 agc char newuser[MaxUserNameLen + 1];
2010 1.26 simonb int c, have_new_user;
2011 1.1 agc
2012 1.82 christos (void)memset(newuser, 0, sizeof(newuser));
2013 1.125 mlelstv read_defaults(&def);
2014 1.1 agc have_new_user = 0;
2015 1.125 mlelstv up->u_locked = -1;
2016 1.87 christos while ((c = getopt(argc, argv, "C:FG:c:d:e:f:g:l:mos:u:"
2017 1.82 christos MOD_OPT_EXTENSIONS)) != -1) {
2018 1.1 agc switch(c) {
2019 1.1 agc case 'G':
2020 1.125 mlelstv while (up->u_groupc < NGROUPS_MAX &&
2021 1.125 mlelstv (up->u_groupv[up->u_groupc] =
2022 1.106 dmcmahil strsep(&optarg, ",")) != NULL) {
2023 1.125 mlelstv if (up->u_groupv[up->u_groupc][0] != 0) {
2024 1.125 mlelstv up->u_groupc++;
2025 1.20 agc }
2026 1.20 agc }
2027 1.20 agc if (optarg != NULL) {
2028 1.82 christos warnx("Truncated list of secondary groups "
2029 1.82 christos "to %d entries", NGROUPS_MAX);
2030 1.20 agc }
2031 1.125 mlelstv up->u_flags |= F_SECGROUP;
2032 1.1 agc break;
2033 1.75 agc #ifdef EXTENSIONS
2034 1.75 agc case 'S':
2035 1.125 mlelstv up->u_allow_samba = 1;
2036 1.75 agc break;
2037 1.75 agc #endif
2038 1.1 agc case 'c':
2039 1.125 mlelstv memsave(&up->u_comment, optarg, strlen(optarg));
2040 1.125 mlelstv up->u_flags |= F_COMMENT;
2041 1.1 agc break;
2042 1.82 christos case 'C':
2043 1.93 agc if (strcasecmp(optarg, "yes") == 0) {
2044 1.125 mlelstv up->u_locked = LOCK;
2045 1.93 agc } else if (strcasecmp(optarg, "no") == 0) {
2046 1.125 mlelstv up->u_locked = UNLOCK;
2047 1.93 agc } else {
2048 1.82 christos /* No idea. */
2049 1.93 agc errx(EXIT_FAILURE,
2050 1.93 agc "Please type 'yes' or 'no'");
2051 1.93 agc }
2052 1.96 christos break;
2053 1.87 christos case 'F':
2054 1.125 mlelstv memsave(&up->u_inactive, "-1", strlen("-1"));
2055 1.125 mlelstv up->u_flags |= F_INACTIVE;
2056 1.87 christos break;
2057 1.1 agc case 'd':
2058 1.125 mlelstv memsave(&up->u_home, optarg, strlen(optarg));
2059 1.125 mlelstv up->u_flags |= F_HOMEDIR;
2060 1.1 agc break;
2061 1.1 agc case 'e':
2062 1.125 mlelstv memsave(&up->u_expire, optarg, strlen(optarg));
2063 1.125 mlelstv up->u_flags |= F_EXPIRE;
2064 1.1 agc break;
2065 1.1 agc case 'f':
2066 1.125 mlelstv memsave(&up->u_inactive, optarg, strlen(optarg));
2067 1.125 mlelstv up->u_flags |= F_INACTIVE;
2068 1.1 agc break;
2069 1.1 agc case 'g':
2070 1.125 mlelstv memsave(&up->u_primgrp, optarg, strlen(optarg));
2071 1.125 mlelstv up->u_flags |= F_GROUP;
2072 1.1 agc break;
2073 1.1 agc case 'l':
2074 1.82 christos (void)strlcpy(newuser, optarg, sizeof(newuser));
2075 1.1 agc have_new_user = 1;
2076 1.125 mlelstv up->u_flags |= F_USERNAME;
2077 1.1 agc break;
2078 1.42 christos #ifdef EXTENSIONS
2079 1.42 christos case 'L':
2080 1.125 mlelstv memsave(&up->u_class, optarg, strlen(optarg));
2081 1.125 mlelstv up->u_flags |= F_CLASS;
2082 1.42 christos break;
2083 1.42 christos #endif
2084 1.1 agc case 'm':
2085 1.125 mlelstv up->u_flags |= F_MKDIR;
2086 1.1 agc break;
2087 1.1 agc case 'o':
2088 1.125 mlelstv up->u_flags |= F_DUPUID;
2089 1.1 agc break;
2090 1.1 agc #ifdef EXTENSIONS
2091 1.1 agc case 'p':
2092 1.125 mlelstv memsave(&up->u_password, optarg, strlen(optarg));
2093 1.125 mlelstv up->u_flags |= F_PASSWORD;
2094 1.1 agc break;
2095 1.1 agc #endif
2096 1.1 agc case 's':
2097 1.125 mlelstv memsave(&up->u_shell, optarg, strlen(optarg));
2098 1.125 mlelstv up->u_flags |= F_SHELL;
2099 1.1 agc break;
2100 1.1 agc case 'u':
2101 1.125 mlelstv up->u_uid = check_numeric(optarg, "uid");
2102 1.125 mlelstv up->u_flags |= F_UID;
2103 1.1 agc break;
2104 1.1 agc #ifdef EXTENSIONS
2105 1.1 agc case 'v':
2106 1.1 agc verbose = 1;
2107 1.1 agc break;
2108 1.1 agc #endif
2109 1.69 agc default:
2110 1.69 agc usermgmt_usage("usermod");
2111 1.69 agc /* NOTREACHED */
2112 1.1 agc }
2113 1.1 agc }
2114 1.125 mlelstv if ((up->u_flags & F_MKDIR) && !(up->u_flags & F_HOMEDIR) &&
2115 1.125 mlelstv !(up->u_flags & F_USERNAME)) {
2116 1.82 christos warnx("Option 'm' useless without 'd' or 'l' -- ignored");
2117 1.125 mlelstv up->u_flags &= ~F_MKDIR;
2118 1.35 wiz }
2119 1.39 itojun argc -= optind;
2120 1.39 itojun argv += optind;
2121 1.39 itojun if (argc != 1) {
2122 1.9 agc usermgmt_usage("usermod");
2123 1.1 agc }
2124 1.16 soren checkeuid();
2125 1.59 agc openlog("usermod", LOG_PID, LOG_USER);
2126 1.125 mlelstv return moduser(*argv, (have_new_user) ? newuser : *argv, up,
2127 1.125 mlelstv up->u_allow_samba) ? EXIT_SUCCESS : EXIT_FAILURE;
2128 1.1 agc }
2129 1.1 agc
2130 1.1 agc #ifdef EXTENSIONS
2131 1.75 agc #define DEL_OPT_EXTENSIONS "Dp:vS"
2132 1.1 agc #else
2133 1.96 christos #define DEL_OPT_EXTENSIONS
2134 1.1 agc #endif
2135 1.1 agc
2136 1.9 agc int
2137 1.1 agc userdel(int argc, char **argv)
2138 1.1 agc {
2139 1.1 agc struct passwd *pwp;
2140 1.125 mlelstv def_t def;
2141 1.125 mlelstv user_t *up = &def.user;
2142 1.1 agc char password[PasswordLength + 1];
2143 1.1 agc int defaultfield;
2144 1.1 agc int rmhome;
2145 1.1 agc int bigD;
2146 1.1 agc int c;
2147 1.1 agc
2148 1.125 mlelstv read_defaults(&def);
2149 1.1 agc defaultfield = bigD = rmhome = 0;
2150 1.1 agc while ((c = getopt(argc, argv, "r" DEL_OPT_EXTENSIONS)) != -1) {
2151 1.1 agc switch(c) {
2152 1.1 agc #ifdef EXTENSIONS
2153 1.1 agc case 'D':
2154 1.1 agc bigD = 1;
2155 1.1 agc break;
2156 1.1 agc #endif
2157 1.1 agc #ifdef EXTENSIONS
2158 1.75 agc case 'S':
2159 1.125 mlelstv up->u_allow_samba = 1;
2160 1.75 agc break;
2161 1.75 agc #endif
2162 1.75 agc #ifdef EXTENSIONS
2163 1.1 agc case 'p':
2164 1.1 agc defaultfield = 1;
2165 1.125 mlelstv up->u_preserve = (strcmp(optarg, "true") == 0) ? 1 :
2166 1.1 agc (strcmp(optarg, "yes") == 0) ? 1 :
2167 1.1 agc atoi(optarg);
2168 1.1 agc break;
2169 1.1 agc #endif
2170 1.1 agc case 'r':
2171 1.1 agc rmhome = 1;
2172 1.1 agc break;
2173 1.1 agc #ifdef EXTENSIONS
2174 1.1 agc case 'v':
2175 1.1 agc verbose = 1;
2176 1.1 agc break;
2177 1.1 agc #endif
2178 1.69 agc default:
2179 1.69 agc usermgmt_usage("userdel");
2180 1.69 agc /* NOTREACHED */
2181 1.1 agc }
2182 1.1 agc }
2183 1.1 agc #ifdef EXTENSIONS
2184 1.1 agc if (bigD) {
2185 1.1 agc if (defaultfield) {
2186 1.9 agc checkeuid();
2187 1.125 mlelstv return setdefaults(up) ? EXIT_SUCCESS : EXIT_FAILURE;
2188 1.1 agc }
2189 1.125 mlelstv (void)printf("preserve\t%s\n", (up->u_preserve) ? "true" :
2190 1.82 christos "false");
2191 1.1 agc return EXIT_SUCCESS;
2192 1.1 agc }
2193 1.1 agc #endif
2194 1.39 itojun argc -= optind;
2195 1.39 itojun argv += optind;
2196 1.39 itojun if (argc != 1) {
2197 1.9 agc usermgmt_usage("userdel");
2198 1.1 agc }
2199 1.9 agc checkeuid();
2200 1.39 itojun if ((pwp = getpwnam(*argv)) == NULL) {
2201 1.39 itojun warnx("No such user `%s'", *argv);
2202 1.1 agc return EXIT_FAILURE;
2203 1.1 agc }
2204 1.93 agc if (rmhome) {
2205 1.96 christos (void)removehomedir(pwp);
2206 1.93 agc }
2207 1.125 mlelstv if (up->u_preserve) {
2208 1.125 mlelstv up->u_flags |= F_SHELL;
2209 1.129 dholland memsave(&up->u_shell, _PATH_SBIN_NOLOGIN,
2210 1.129 dholland strlen(_PATH_SBIN_NOLOGIN));
2211 1.82 christos (void)memset(password, '*', DES_Len);
2212 1.65 agc password[DES_Len] = 0;
2213 1.125 mlelstv memsave(&up->u_password, password, strlen(password));
2214 1.125 mlelstv up->u_flags |= F_PASSWORD;
2215 1.59 agc openlog("userdel", LOG_PID, LOG_USER);
2216 1.125 mlelstv return moduser(*argv, *argv, up, up->u_allow_samba) ?
2217 1.82 christos EXIT_SUCCESS : EXIT_FAILURE;
2218 1.53 agc }
2219 1.53 agc if (!rm_user_from_groups(*argv)) {
2220 1.53 agc return 0;
2221 1.1 agc }
2222 1.59 agc openlog("userdel", LOG_PID, LOG_USER);
2223 1.125 mlelstv return moduser(*argv, *argv, NULL, up->u_allow_samba) ?
2224 1.82 christos EXIT_SUCCESS : EXIT_FAILURE;
2225 1.1 agc }
2226 1.1 agc
2227 1.1 agc #ifdef EXTENSIONS
2228 1.77 jmmv #define GROUP_ADD_OPT_EXTENSIONS "r:v"
2229 1.1 agc #else
2230 1.96 christos #define GROUP_ADD_OPT_EXTENSIONS
2231 1.1 agc #endif
2232 1.1 agc
2233 1.1 agc /* add a group */
2234 1.1 agc int
2235 1.1 agc groupadd(int argc, char **argv)
2236 1.1 agc {
2237 1.125 mlelstv def_t def;
2238 1.125 mlelstv group_t *gp = &def.group;
2239 1.1 agc int dupgid;
2240 1.1 agc int gid;
2241 1.1 agc int c;
2242 1.1 agc
2243 1.1 agc gid = -1;
2244 1.1 agc dupgid = 0;
2245 1.125 mlelstv read_defaults(&def);
2246 1.1 agc while ((c = getopt(argc, argv, "g:o" GROUP_ADD_OPT_EXTENSIONS)) != -1) {
2247 1.1 agc switch(c) {
2248 1.1 agc case 'g':
2249 1.82 christos gid = check_numeric(optarg, "gid");
2250 1.1 agc break;
2251 1.1 agc case 'o':
2252 1.1 agc dupgid = 1;
2253 1.1 agc break;
2254 1.1 agc #ifdef EXTENSIONS
2255 1.77 jmmv case 'r':
2256 1.125 mlelstv (void)save_range(&gp->g_r, optarg);
2257 1.77 jmmv break;
2258 1.1 agc case 'v':
2259 1.1 agc verbose = 1;
2260 1.1 agc break;
2261 1.1 agc #endif
2262 1.69 agc default:
2263 1.69 agc usermgmt_usage("groupadd");
2264 1.69 agc /* NOTREACHED */
2265 1.1 agc }
2266 1.1 agc }
2267 1.39 itojun argc -= optind;
2268 1.39 itojun argv += optind;
2269 1.39 itojun if (argc != 1) {
2270 1.9 agc usermgmt_usage("groupadd");
2271 1.1 agc }
2272 1.125 mlelstv if (gp->g_rc == 0) {
2273 1.125 mlelstv gp->g_rv[gp->g_rc].r_from = DEF_LOWUID;
2274 1.125 mlelstv gp->g_rv[gp->g_rc].r_to = DEF_HIGHUID;
2275 1.125 mlelstv gp->g_rc += 1;
2276 1.125 mlelstv }
2277 1.125 mlelstv gp->g_defrc = gp->g_rc;
2278 1.16 soren checkeuid();
2279 1.125 mlelstv if (gid == -1) {
2280 1.125 mlelstv int got_id = 0;
2281 1.125 mlelstv int i;
2282 1.125 mlelstv
2283 1.125 mlelstv /*
2284 1.125 mlelstv * Look for a free GID in the command line ranges (if any).
2285 1.125 mlelstv * These start after the ranges specified in the config file.
2286 1.125 mlelstv */
2287 1.125 mlelstv for (i = gp->g_defrc; !got_id && i < gp->g_rc ; i++) {
2288 1.125 mlelstv got_id = getnextgid(&gid,
2289 1.125 mlelstv gp->g_rv[i].r_from, gp->g_rv[i].r_to);
2290 1.125 mlelstv }
2291 1.125 mlelstv /*
2292 1.125 mlelstv * If there were no free GIDs in the command line ranges,
2293 1.125 mlelstv * try the ranges from the config file (there will always
2294 1.125 mlelstv * be at least one default).
2295 1.125 mlelstv */
2296 1.125 mlelstv for (i = 0; !got_id && i < gp->g_defrc; i++) {
2297 1.125 mlelstv got_id = getnextgid(&gid,
2298 1.125 mlelstv gp->g_rv[i].r_from, gp->g_rv[i].r_to);
2299 1.125 mlelstv }
2300 1.125 mlelstv if (!got_id)
2301 1.125 mlelstv errx(EXIT_FAILURE, "Can't add group: can't get next gid");
2302 1.1 agc }
2303 1.24 simonb if (!dupgid && getgrgid((gid_t) gid) != NULL) {
2304 1.82 christos errx(EXIT_FAILURE, "Can't add group: gid %d is a duplicate",
2305 1.82 christos gid);
2306 1.1 agc }
2307 1.39 itojun if (!valid_group(*argv)) {
2308 1.82 christos warnx("Invalid group name `%s'", *argv);
2309 1.1 agc }
2310 1.59 agc openlog("groupadd", LOG_PID, LOG_USER);
2311 1.96 christos if (!creategid(*argv, gid, ""))
2312 1.96 christos exit(EXIT_FAILURE);
2313 1.96 christos
2314 1.1 agc return EXIT_SUCCESS;
2315 1.1 agc }
2316 1.1 agc
2317 1.1 agc #ifdef EXTENSIONS
2318 1.1 agc #define GROUP_DEL_OPT_EXTENSIONS "v"
2319 1.1 agc #else
2320 1.96 christos #define GROUP_DEL_OPT_EXTENSIONS
2321 1.1 agc #endif
2322 1.1 agc
2323 1.1 agc /* remove a group */
2324 1.1 agc int
2325 1.1 agc groupdel(int argc, char **argv)
2326 1.1 agc {
2327 1.1 agc int c;
2328 1.1 agc
2329 1.1 agc while ((c = getopt(argc, argv, "" GROUP_DEL_OPT_EXTENSIONS)) != -1) {
2330 1.1 agc switch(c) {
2331 1.1 agc #ifdef EXTENSIONS
2332 1.1 agc case 'v':
2333 1.1 agc verbose = 1;
2334 1.1 agc break;
2335 1.1 agc #endif
2336 1.69 agc default:
2337 1.69 agc usermgmt_usage("groupdel");
2338 1.69 agc /* NOTREACHED */
2339 1.1 agc }
2340 1.1 agc }
2341 1.39 itojun argc -= optind;
2342 1.39 itojun argv += optind;
2343 1.39 itojun if (argc != 1) {
2344 1.9 agc usermgmt_usage("groupdel");
2345 1.1 agc }
2346 1.80 agc if (getgrnam(*argv) == NULL) {
2347 1.80 agc errx(EXIT_FAILURE, "No such group `%s'", *argv);
2348 1.80 agc }
2349 1.16 soren checkeuid();
2350 1.59 agc openlog("groupdel", LOG_PID, LOG_USER);
2351 1.96 christos if (!modify_gid(*argv, NULL))
2352 1.96 christos exit(EXIT_FAILURE);
2353 1.96 christos
2354 1.1 agc return EXIT_SUCCESS;
2355 1.1 agc }
2356 1.1 agc
2357 1.1 agc #ifdef EXTENSIONS
2358 1.1 agc #define GROUP_MOD_OPT_EXTENSIONS "v"
2359 1.1 agc #else
2360 1.96 christos #define GROUP_MOD_OPT_EXTENSIONS
2361 1.1 agc #endif
2362 1.1 agc
2363 1.1 agc /* modify a group */
2364 1.1 agc int
2365 1.1 agc groupmod(int argc, char **argv)
2366 1.1 agc {
2367 1.1 agc struct group *grp;
2368 1.1 agc char buf[MaxEntryLen];
2369 1.1 agc char *newname;
2370 1.1 agc char **cpp;
2371 1.1 agc int dupgid;
2372 1.1 agc int gid;
2373 1.1 agc int cc;
2374 1.1 agc int c;
2375 1.1 agc
2376 1.1 agc gid = -1;
2377 1.1 agc dupgid = 0;
2378 1.15 soren newname = NULL;
2379 1.1 agc while ((c = getopt(argc, argv, "g:on:" GROUP_MOD_OPT_EXTENSIONS)) != -1) {
2380 1.1 agc switch(c) {
2381 1.1 agc case 'g':
2382 1.82 christos gid = check_numeric(optarg, "gid");
2383 1.1 agc break;
2384 1.1 agc case 'o':
2385 1.1 agc dupgid = 1;
2386 1.1 agc break;
2387 1.1 agc case 'n':
2388 1.1 agc memsave(&newname, optarg, strlen(optarg));
2389 1.1 agc break;
2390 1.1 agc #ifdef EXTENSIONS
2391 1.1 agc case 'v':
2392 1.1 agc verbose = 1;
2393 1.1 agc break;
2394 1.1 agc #endif
2395 1.69 agc default:
2396 1.69 agc usermgmt_usage("groupmod");
2397 1.69 agc /* NOTREACHED */
2398 1.1 agc }
2399 1.1 agc }
2400 1.39 itojun argc -= optind;
2401 1.39 itojun argv += optind;
2402 1.39 itojun if (argc != 1) {
2403 1.9 agc usermgmt_usage("groupmod");
2404 1.1 agc }
2405 1.16 soren checkeuid();
2406 1.15 soren if (gid < 0 && newname == NULL) {
2407 1.81 agc errx(EXIT_FAILURE, "Nothing to change");
2408 1.1 agc }
2409 1.1 agc if (dupgid && gid < 0) {
2410 1.81 agc errx(EXIT_FAILURE, "Duplicate which gid?");
2411 1.1 agc }
2412 1.122 mike if (!dupgid && getgrgid((gid_t) gid) != NULL) {
2413 1.122 mike errx(EXIT_FAILURE, "Can't modify group: gid %d is a duplicate",
2414 1.122 mike gid);
2415 1.122 mike }
2416 1.81 agc if ((grp = find_group_info(*argv)) == NULL) {
2417 1.82 christos errx(EXIT_FAILURE, "Can't find group `%s' to modify", *argv);
2418 1.58 agc }
2419 1.58 agc if (!is_local(*argv, _PATH_GROUP)) {
2420 1.58 agc errx(EXIT_FAILURE, "Group `%s' must be a local group", *argv);
2421 1.1 agc }
2422 1.15 soren if (newname != NULL && !valid_group(newname)) {
2423 1.82 christos warnx("Invalid group name `%s'", newname);
2424 1.1 agc }
2425 1.1 agc cc = snprintf(buf, sizeof(buf), "%s:%s:%d:",
2426 1.1 agc (newname) ? newname : grp->gr_name,
2427 1.1 agc grp->gr_passwd,
2428 1.1 agc (gid < 0) ? grp->gr_gid : gid);
2429 1.1 agc for (cpp = grp->gr_mem ; *cpp && cc < sizeof(buf) ; cpp++) {
2430 1.1 agc cc += snprintf(&buf[cc], sizeof(buf) - cc, "%s%s", *cpp,
2431 1.15 soren (cpp[1] == NULL) ? "" : ",");
2432 1.1 agc }
2433 1.37 lukem cc += snprintf(&buf[cc], sizeof(buf) - cc, "\n");
2434 1.109 jnemeth if (newname != NULL)
2435 1.109 jnemeth free(newname);
2436 1.59 agc openlog("groupmod", LOG_PID, LOG_USER);
2437 1.96 christos if (!modify_gid(*argv, buf))
2438 1.96 christos exit(EXIT_FAILURE);
2439 1.96 christos
2440 1.1 agc return EXIT_SUCCESS;
2441 1.1 agc }
2442 1.1 agc
2443 1.9 agc #ifdef EXTENSIONS
2444 1.9 agc /* display user information */
2445 1.9 agc int
2446 1.9 agc userinfo(int argc, char **argv)
2447 1.9 agc {
2448 1.9 agc struct passwd *pwp;
2449 1.9 agc struct group *grp;
2450 1.9 agc char buf[MaxEntryLen];
2451 1.9 agc char **cpp;
2452 1.9 agc int exists;
2453 1.9 agc int cc;
2454 1.9 agc int i;
2455 1.1 agc
2456 1.9 agc exists = 0;
2457 1.112 pavel buf[0] = '\0';
2458 1.119 reed while ((i = getopt(argc, argv, "e")) != -1) {
2459 1.9 agc switch(i) {
2460 1.9 agc case 'e':
2461 1.9 agc exists = 1;
2462 1.9 agc break;
2463 1.69 agc default:
2464 1.69 agc usermgmt_usage("userinfo");
2465 1.69 agc /* NOTREACHED */
2466 1.9 agc }
2467 1.9 agc }
2468 1.39 itojun argc -= optind;
2469 1.39 itojun argv += optind;
2470 1.39 itojun if (argc != 1) {
2471 1.9 agc usermgmt_usage("userinfo");
2472 1.9 agc }
2473 1.39 itojun pwp = find_user_info(*argv);
2474 1.9 agc if (exists) {
2475 1.9 agc exit((pwp) ? EXIT_SUCCESS : EXIT_FAILURE);
2476 1.9 agc }
2477 1.24 simonb if (pwp == NULL) {
2478 1.82 christos errx(EXIT_FAILURE, "Can't find user `%s'", *argv);
2479 1.9 agc }
2480 1.82 christos (void)printf("login\t%s\n", pwp->pw_name);
2481 1.82 christos (void)printf("passwd\t%s\n", pwp->pw_passwd);
2482 1.82 christos (void)printf("uid\t%d\n", pwp->pw_uid);
2483 1.24 simonb for (cc = 0 ; (grp = getgrent()) != NULL ; ) {
2484 1.9 agc for (cpp = grp->gr_mem ; *cpp ; cpp++) {
2485 1.82 christos if (strcmp(*cpp, *argv) == 0 &&
2486 1.82 christos grp->gr_gid != pwp->pw_gid) {
2487 1.82 christos cc += snprintf(&buf[cc], sizeof(buf) - cc,
2488 1.82 christos "%s ", grp->gr_name);
2489 1.9 agc }
2490 1.9 agc }
2491 1.9 agc }
2492 1.24 simonb if ((grp = getgrgid(pwp->pw_gid)) == NULL) {
2493 1.82 christos (void)printf("groups\t%d %s\n", pwp->pw_gid, buf);
2494 1.9 agc } else {
2495 1.82 christos (void)printf("groups\t%s %s\n", grp->gr_name, buf);
2496 1.9 agc }
2497 1.92 christos (void)printf("change\t%s", pwp->pw_change > 0 ?
2498 1.92 christos ctime(&pwp->pw_change) : pwp->pw_change == -1 ?
2499 1.92 christos "NEXT LOGIN\n" : "NEVER\n");
2500 1.82 christos (void)printf("class\t%s\n", pwp->pw_class);
2501 1.82 christos (void)printf("gecos\t%s\n", pwp->pw_gecos);
2502 1.82 christos (void)printf("dir\t%s\n", pwp->pw_dir);
2503 1.82 christos (void)printf("shell\t%s\n", pwp->pw_shell);
2504 1.82 christos (void)printf("expire\t%s", pwp->pw_expire ?
2505 1.82 christos ctime(&pwp->pw_expire) : "NEVER\n");
2506 1.9 agc return EXIT_SUCCESS;
2507 1.9 agc }
2508 1.9 agc #endif
2509 1.1 agc
2510 1.9 agc #ifdef EXTENSIONS
2511 1.9 agc /* display user information */
2512 1.1 agc int
2513 1.9 agc groupinfo(int argc, char **argv)
2514 1.1 agc {
2515 1.9 agc struct group *grp;
2516 1.9 agc char **cpp;
2517 1.9 agc int exists;
2518 1.9 agc int i;
2519 1.1 agc
2520 1.9 agc exists = 0;
2521 1.9 agc while ((i = getopt(argc, argv, "ev")) != -1) {
2522 1.9 agc switch(i) {
2523 1.9 agc case 'e':
2524 1.9 agc exists = 1;
2525 1.9 agc break;
2526 1.9 agc case 'v':
2527 1.9 agc verbose = 1;
2528 1.9 agc break;
2529 1.69 agc default:
2530 1.69 agc usermgmt_usage("groupinfo");
2531 1.69 agc /* NOTREACHED */
2532 1.9 agc }
2533 1.9 agc }
2534 1.39 itojun argc -= optind;
2535 1.39 itojun argv += optind;
2536 1.39 itojun if (argc != 1) {
2537 1.9 agc usermgmt_usage("groupinfo");
2538 1.9 agc }
2539 1.39 itojun grp = find_group_info(*argv);
2540 1.9 agc if (exists) {
2541 1.9 agc exit((grp) ? EXIT_SUCCESS : EXIT_FAILURE);
2542 1.9 agc }
2543 1.24 simonb if (grp == NULL) {
2544 1.82 christos errx(EXIT_FAILURE, "Can't find group `%s'", *argv);
2545 1.9 agc }
2546 1.82 christos (void)printf("name\t%s\n", grp->gr_name);
2547 1.82 christos (void)printf("passwd\t%s\n", grp->gr_passwd);
2548 1.82 christos (void)printf("gid\t%d\n", grp->gr_gid);
2549 1.82 christos (void)printf("members\t");
2550 1.9 agc for (cpp = grp->gr_mem ; *cpp ; cpp++) {
2551 1.82 christos (void)printf("%s", *cpp);
2552 1.93 agc if (*(cpp + 1)) {
2553 1.81 agc (void) printf(", ");
2554 1.93 agc }
2555 1.9 agc }
2556 1.82 christos (void)fputc('\n', stdout);
2557 1.9 agc return EXIT_SUCCESS;
2558 1.1 agc }
2559 1.9 agc #endif
2560