1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2013 Gary Mills
*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Copyright (c) 2013 RackTop Systems.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <limits.h>
#include <string.h>
#include <userdefs.h>
#include <errno.h>
#include <project.h>
#include <unistd.h>
#include <user_attr.h>
#include <libcmdutils.h>
#include "users.h"
#include "messages.h"
#include "userdisp.h"
#include "funcs.h"
/*
* useradd [-u uid [-o] | -g group | -G group [[, group]...]
* | -d dir [-m [-z|Z]]
* | -s shell | -c comment | -k skel_dir | -b base_dir] ]
* [ -A authorization [, authorization ...]]
* [ -P profile [, profile ...]]
* [ -K key=value ]
* [ -R role [, role ...]] [-p project [, project ...]] login
* useradd -D [ -g group ] [ -b base_dir | -f inactive | -e expire |
* -s shell | -k skel_dir ]
* [ -A authorization [, authorization ...]]
* [ -P profile [, profile ...]] [ -K key=value ]
* [ -R role [, role ...]] [-p project [, project ...]] login
*
* This command adds new user logins to the system. Arguments are:
*
* uid - an integer
* group - an existing group's integer ID or char string name
* dir - home directory
* shell - a program to be used as a shell
* comment - any text string
* skel_dir - a skeleton directory
* base_dir - a directory
* login - a string of printable chars except colon(:)
* authorization - One or more comma separated authorizations defined
* in auth_attr(5).
* profile - One or more comma separated execution profiles defined
* in prof_attr(5)
* role - One or more comma-separated role names defined in user_attr(5)
* project - One or more comma-separated project names or numbers
*
*/
extern struct userdefs *getusrdef();
extern void dispusrdef();
static void cleanup();
extern int check_perm(), valid_expire();
extern int putusrdef(), valid_uid();
extern int call_passmgmt(), edit_group(), create_home();
extern int edit_project();
extern int **valid_lgroup();
extern projid_t **valid_lproject();
extern void update_def(struct userdefs *);
extern void import_def(struct userdefs *);
extern int get_default_zfs_flags();
static uid_t uid; /* new uid */
static char *logname; /* login name to add */
static struct userdefs *usrdefs; /* defaults for useradd */
char *cmdname;
static char homedir[ PATH_MAX + 1 ]; /* home directory */
static char gidstring[32]; /* group id string representation */
static gid_t gid; /* gid of new login */
static char uidstring[32]; /* user id string representation */
static char *uidstr = NULL; /* uid from command line */
static char *base_dir = NULL; /* base_dir from command line */
static char *group = NULL; /* group from command line */
static char *grps = NULL; /* multi groups from command line */
static char *dir = NULL; /* home dir from command line */
static char *shell = NULL; /* shell from command line */
static char *comment = NULL; /* comment from command line */
static char *skel_dir = NULL; /* skel dir from command line */
static long inact; /* inactive days */
static char *inactstr = NULL; /* inactive from command line */
static char inactstring[10]; /* inactivity string representation */
static char *expirestr = NULL; /* expiration date from command line */
static char *projects = NULL; /* project id's from command line */
static char *usertype = NULL; /* type of user, either role or normal */
typedef enum {
BASEDIR = 0,
SKELDIR,
SHELL
} path_opt_t;
static void valid_input(path_opt_t, const char *);
int
main(argc, argv)
int argc;
char *argv[];
{
int ch, ret, mflag = 0, oflag = 0, Dflag = 0;
int zflag = 0, Zflag = 0, **gidlist = NULL;
projid_t **projlist = NULL;
char *ptr; /* loc in a str, may be set by strtol */
struct group *g_ptr;
struct project p_ptr;
char mybuf[PROJECT_BUFSZ];
struct stat statbuf; /* status buffer for stat */
int warning;
int busy = 0;
char **nargv; /* arguments for execvp of passmgmt */
int argindex; /* argument index into nargv */
int zfs_flags = 0; /* create_home flags */
cmdname = argv[0];
if (geteuid() != 0) {
errmsg(M_PERM_DENIED);
exit(EX_NO_PERM);
}
opterr = 0; /* no print errors from getopt */
usertype = getusertype(argv[0]);
change_key(USERATTR_TYPE_KW, usertype);
while ((ch = getopt(argc, argv,
"b:c:Dd:e:f:G:g:k:mzZop:s:u:A:P:R:K:")) != EOF)
switch (ch) {
case 'b':
base_dir = optarg;
break;
case 'c':
comment = optarg;
break;
case 'D':
Dflag++;
break;
case 'd':
dir = optarg;
break;
case 'e':
expirestr = optarg;
break;
case 'f':
inactstr = optarg;
break;
case 'G':
grps = optarg;
break;
case 'g':
group = optarg;
break;
case 'k':
skel_dir = optarg;
break;
case 'm':
mflag++;
break;
case 'o':
oflag++;
break;
case 'p':
projects = optarg;
break;
case 's':
shell = optarg;
break;
case 'u':
uidstr = optarg;
break;
case 'Z':
Zflag++;
break;
case 'z':
zflag++;
break;
case 'A':
change_key(USERATTR_AUTHS_KW, optarg);
break;
case 'P':
change_key(USERATTR_PROFILES_KW, optarg);
break;
case 'R':
if (is_role(usertype)) {
errmsg(M_ARUSAGE);
exit(EX_SYNTAX);
}
change_key(USERATTR_ROLES_KW, optarg);
break;
case 'K':
change_key(NULL, optarg);
break;
default:
case '?':
if (is_role(usertype))
errmsg(M_ARUSAGE);
else
errmsg(M_AUSAGE);
exit(EX_SYNTAX);
}
if (((!mflag) && (zflag || Zflag)) || (zflag && Zflag) ||
(mflag > 1 && (zflag || Zflag))) {
if (is_role(usertype))
errmsg(M_ARUSAGE);
else
errmsg(M_AUSAGE);
exit(EX_SYNTAX);
}
/* get defaults for adding new users */
usrdefs = getusrdef(usertype);
if (Dflag) {
/* DISPLAY mode */
/* check syntax */
if (optind != argc) {
if (is_role(usertype))
errmsg(M_ARUSAGE);
else
errmsg(M_AUSAGE);
exit(EX_SYNTAX);
}
if (uidstr != NULL || oflag || grps != NULL ||
dir != NULL || mflag || comment != NULL) {
if (is_role(usertype))
errmsg(M_ARUSAGE);
else
errmsg(M_AUSAGE);
exit(EX_SYNTAX);
}
/* Group must be an existing group */
if (group != NULL) {
switch (valid_group(group, &g_ptr, &warning)) {
case INVALID:
errmsg(M_INVALID, group, "group id");
exit(EX_BADARG);
/*NOTREACHED*/
case TOOBIG:
errmsg(M_TOOBIG, "gid", group);
exit(EX_BADARG);
/*NOTREACHED*/
case RESERVED:
case UNIQUE:
errmsg(M_GRP_NOTUSED, group);
exit(EX_NAME_NOT_EXIST);
}
if (warning)
warningmsg(warning, group);
usrdefs->defgroup = g_ptr->gr_gid;
usrdefs->defgname = g_ptr->gr_name;
}
/* project must be an existing project */
if (projects != NULL) {
switch (valid_project(projects, &p_ptr, mybuf,
sizeof (mybuf), &warning)) {
case INVALID:
errmsg(M_INVALID, projects, "project id");
exit(EX_BADARG);
/*NOTREACHED*/
case TOOBIG:
errmsg(M_TOOBIG, "projid", projects);
exit(EX_BADARG);
/*NOTREACHED*/
case UNIQUE:
errmsg(M_PROJ_NOTUSED, projects);
exit(EX_NAME_NOT_EXIST);
}
if (warning)
warningmsg(warning, projects);
usrdefs->defproj = p_ptr.pj_projid;
usrdefs->defprojname = p_ptr.pj_name;
}
/* base_dir must be an existing directory */
if (base_dir != NULL) {
valid_input(BASEDIR, base_dir);
usrdefs->defparent = base_dir;
}
/* inactivity period is an integer */
if (inactstr != NULL) {
/* convert inactstr to integer */
inact = strtol(inactstr, &ptr, 10);
if (*ptr || inact < 0) {
errmsg(M_INVALID, inactstr,
"inactivity period");
exit(EX_BADARG);
}
usrdefs->definact = inact;
}
/* expiration string is a date, newer than today */
if (expirestr != NULL) {
if (*expirestr) {
if (valid_expire(expirestr, (time_t *)0)
== INVALID) {
errmsg(M_INVALID, expirestr,
"expiration date");
exit(EX_BADARG);
}
usrdefs->defexpire = expirestr;
} else
/* Unset the expiration date */
usrdefs->defexpire = "";
}
if (shell != NULL) {
valid_input(SHELL, shell);
usrdefs->defshell = shell;
}
if (skel_dir != NULL) {
valid_input(SKELDIR, skel_dir);
usrdefs->defskel = skel_dir;
}
update_def(usrdefs);
/* change defaults for useradd */
if (putusrdef(usrdefs, usertype) < 0) {
errmsg(M_UPDATE, "created");
exit(EX_UPDATE);
}
/* Now, display */
dispusrdef(stdout, (D_ALL & ~D_RID), usertype);
exit(EX_SUCCESS);
}
/* ADD mode */
/* check syntax */
if (optind != argc - 1 || (skel_dir != NULL && !mflag)) {
if (is_role(usertype))
errmsg(M_ARUSAGE);
else
errmsg(M_AUSAGE);
exit(EX_SYNTAX);
}
logname = argv[optind];
switch (valid_login(logname, (struct passwd **)NULL, &warning)) {
case INVALID:
errmsg(M_INVALID, logname, "login name");
exit(EX_BADARG);
/*NOTREACHED*/
case NOTUNIQUE:
errmsg(M_USED, logname);
exit(EX_NAME_EXISTS);
/*NOTREACHED*/
case LONGNAME:
errmsg(M_TOO_LONG, logname);
exit(EX_BADARG);
/*NOTREACHED*/
}
if (warning)
warningmsg(warning, logname);
if (uidstr != NULL) {
/* convert uidstr to integer */
errno = 0;
uid = (uid_t)strtol(uidstr, &ptr, (int)10);
if (*ptr || errno == ERANGE) {
errmsg(M_INVALID, uidstr, "user id");
exit(EX_BADARG);
}
switch (valid_uid(uid, NULL)) {
case NOTUNIQUE:
if (!oflag) {
/* override not specified */
errmsg(M_UID_USED, uid);
exit(EX_ID_EXISTS);
}
break;
case RESERVED:
errmsg(M_RESERVED, uid);
break;
case TOOBIG:
errmsg(M_TOOBIG, "uid", uid);
exit(EX_BADARG);
break;
}
} else {
if (findnextuid(DEFRID+1, MAXUID, &uid) != 0) {
errmsg(M_INVALID, "default id", "user id");
exit(EX_ID_EXISTS);
}
}
if (group != NULL) {
switch (valid_group(group, &g_ptr, &warning)) {
case INVALID:
errmsg(M_INVALID, group, "group id");
exit(EX_BADARG);
/*NOTREACHED*/
case TOOBIG:
errmsg(M_TOOBIG, "gid", group);
exit(EX_BADARG);
/*NOTREACHED*/
case RESERVED:
case UNIQUE:
errmsg(M_GRP_NOTUSED, group);
exit(EX_NAME_NOT_EXIST);
/*NOTREACHED*/
}
if (warning)
warningmsg(warning, group);
gid = g_ptr->gr_gid;
} else gid = usrdefs->defgroup;
if (grps != NULL) {
if (!*grps)
/* ignore -G "" */
grps = (char *)0;
else if (!(gidlist = valid_lgroup(grps, gid)))
exit(EX_BADARG);
}
if (projects != NULL) {
if (! *projects)
projects = (char *)0;
else if (! (projlist = valid_lproject(projects)))
exit(EX_BADARG);
}
/* if base_dir is provided, check its validity; otherwise default */
if (base_dir != NULL)
valid_input(BASEDIR, base_dir);
else
base_dir = usrdefs->defparent;
if (dir == NULL) {
/* set homedir to home directory made from base_dir */
(void) sprintf(homedir, "%s/%s", base_dir, logname);
} else if (REL_PATH(dir)) {
errmsg(M_RELPATH, dir);
exit(EX_BADARG);
} else
(void) strcpy(homedir, dir);
if (mflag) {
/* Does home dir. already exist? */
if (stat(homedir, &statbuf) == 0) {
/* directory exists - don't try to create */
mflag = 0;
if (check_perm(statbuf, uid, gid, S_IXOTH) != 0)
errmsg(M_NO_PERM, logname, homedir);
}
}
/*
* if shell, skel_dir are provided, check their validity.
* Otherwise default.
*/
if (shell != NULL)
valid_input(SHELL, shell);
else
shell = usrdefs->defshell;
if (skel_dir != NULL)
valid_input(SKELDIR, skel_dir);
else
skel_dir = usrdefs->defskel;
if (inactstr != NULL) {
/* convert inactstr to integer */
inact = strtol(inactstr, &ptr, 10);
if (*ptr || inact < 0) {
errmsg(M_INVALID, inactstr, "inactivity period");
exit(EX_BADARG);
}
} else inact = usrdefs->definact;
/* expiration string is a date, newer than today */
if (expirestr != NULL) {
if (*expirestr) {
if (valid_expire(expirestr, (time_t *)0) == INVALID) {
errmsg(M_INVALID, expirestr, "expiration date");
exit(EX_BADARG);
}
usrdefs->defexpire = expirestr;
} else
/* Unset the expiration date */
expirestr = (char *)0;
} else expirestr = usrdefs->defexpire;
import_def(usrdefs);
/* must now call passmgmt */
/* set up arguments to passmgmt in nargv array */
nargv = malloc((30 + nkeys * 2) * sizeof (char *));
argindex = 0;
nargv[argindex++] = PASSMGMT;
nargv[argindex++] = "-a"; /* add */
if (comment != NULL) {
/* comment */
nargv[argindex++] = "-c";
nargv[argindex++] = comment;
}
/* flags for home directory */
nargv[argindex++] = "-h";
nargv[argindex++] = homedir;
/* set gid flag */
nargv[argindex++] = "-g";
(void) sprintf(gidstring, "%u", gid);
nargv[argindex++] = gidstring;
/* shell */
nargv[argindex++] = "-s";
nargv[argindex++] = shell;
/* set inactive */
nargv[argindex++] = "-f";
(void) sprintf(inactstring, "%ld", inact);
nargv[argindex++] = inactstring;
/* set expiration date */
if (expirestr != NULL) {
nargv[argindex++] = "-e";
nargv[argindex++] = expirestr;
}
/* set uid flag */
nargv[argindex++] = "-u";
(void) sprintf(uidstring, "%u", uid);
nargv[argindex++] = uidstring;
if (oflag) nargv[argindex++] = "-o";
if (nkeys > 1)
addkey_args(nargv, &argindex);
/* finally - login name */
nargv[argindex++] = logname;
/* set the last to null */
nargv[argindex++] = NULL;
/* now call passmgmt */
ret = PEX_FAILED;
/*
* If call_passmgmt fails for any reason other than PEX_BADUID, exit
* is invoked with an appropriate error message. If PEX_BADUID is
* returned, then if the user specified the ID, exit is invoked
* with an appropriate error message. Otherwise we try to pick a
* different ID and try again. If we run out of IDs, i.e. no more
* users can be created, then -1 is returned and we terminate via exit.
* If PEX_BUSY is returned we increment a count, since we will stop
* trying if PEX_BUSY reaches 3. For PEX_SUCCESS we immediately
* terminate the loop.
*/
while (busy < 3 && ret != PEX_SUCCESS) {
switch (ret = call_passmgmt(nargv)) {
case PEX_SUCCESS:
break;
case PEX_BUSY:
busy++;
break;
case PEX_HOSED_FILES:
errmsg(M_HOSED_FILES);
exit(EX_INCONSISTENT);
break;
case PEX_SYNTAX:
case PEX_BADARG:
/* should NEVER occur that passmgmt usage is wrong */
if (is_role(usertype))
errmsg(M_ARUSAGE);
else
errmsg(M_AUSAGE);
exit(EX_SYNTAX);
break;
case PEX_BADUID:
/*
* The uid has been taken. If it was specified by a
* user, then we must fail. Otherwise, keep trying
* to get a good uid until we run out of IDs.
*/
if (uidstr != NULL) {
errmsg(M_UID_USED, uid);
exit(EX_ID_EXISTS);
} else {
if (findnextuid(DEFRID+1, MAXUID, &uid) != 0) {
errmsg(M_INVALID, "default id",
"user id");
exit(EX_ID_EXISTS);
}
(void) sprintf(uidstring, "%u", uid);
}
break;
case PEX_BADNAME:
/* invalid loname */
errmsg(M_USED, logname);
exit(EX_NAME_EXISTS);
break;
default:
errmsg(M_UPDATE, "created");
exit(ret);
break;
}
}
if (busy == 3) {
errmsg(M_UPDATE, "created");
exit(ret);
}
/* add group entry */
if ((grps != NULL) && edit_group(logname, (char *)0, gidlist, 0)) {
errmsg(M_UPDATE, "created");
cleanup(logname);
exit(EX_UPDATE);
}
/* update project database */
if ((projects != NULL) &&
edit_project(logname, (char *)NULL, projlist, 0)) {
errmsg(M_UPDATE, "created");
cleanup(logname);
exit(EX_UPDATE);
}
/* create home directory */
if (mflag) {
zfs_flags = get_default_zfs_flags();
if (zflag || mflag > 1)
zfs_flags |= MANAGE_ZFS;
else if (Zflag)
zfs_flags &= ~MANAGE_ZFS;
ret = create_home(homedir, skel_dir, uid, gid, zfs_flags);
}
if (ret != EX_SUCCESS) {
(void) edit_project(logname, (char *)NULL, (projid_t **)NULL,
0);
(void) edit_group(logname, (char *)0, (int **)0, 1);
cleanup(logname);
exit(EX_HOMEDIR);
}
return (ret);
}
static void
cleanup(logname)
char *logname;
{
char *nargv[4];
nargv[0] = PASSMGMT;
nargv[1] = "-d";
nargv[2] = logname;
nargv[3] = NULL;
switch (call_passmgmt(nargv)) {
case PEX_SUCCESS:
break;
case PEX_SYNTAX:
/* should NEVER occur that passmgmt usage is wrong */
if (is_role(usertype))
errmsg(M_ARUSAGE);
else
errmsg(M_AUSAGE);
break;
case PEX_BADUID:
/* uid is used - shouldn't happen but print message anyway */
errmsg(M_UID_USED, uid);
break;
case PEX_BADNAME:
/* invalid loname */
errmsg(M_USED, logname);
break;
default:
errmsg(M_UPDATE, "created");
break;
}
}
/* Check the validity for shell, base_dir and skel_dir */
void
valid_input(path_opt_t opt, const char *input)
{
struct stat statbuf;
if (REL_PATH(input)) {
errmsg(M_RELPATH, input);
exit(EX_BADARG);
}
if (stat(input, &statbuf) == -1) {
errmsg(M_INVALID, input, "path");
exit(EX_BADARG);
}
if (opt == SHELL) {
if (!S_ISREG(statbuf.st_mode) ||
(statbuf.st_mode & 0555) != 0555) {
errmsg(M_INVALID, input, "shell");
exit(EX_BADARG);
}
} else {
if (!S_ISDIR(statbuf.st_mode)) {
errmsg(M_INVALID, input, "directory");
exit(EX_BADARG);
}
}
}
|