/
user.c
2559 lines (2432 loc) · 62.5 KB
/
user.c
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
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $NetBSD: user.c,v 1.129.6.1 2013/02/25 00:30:49 tls Exp $ */
/*
* Copyright (c) 1999 Alistair G. Crooks. All rights reserved.
* Copyright (c) 2005 Liam J. Foy. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1999\
The NetBSD Foundation, Inc. All rights reserved.");
__RCSID("$NetBSD: user.c,v 1.129.6.1 2013/02/25 00:30:49 tls Exp $");
#endif
#include <sys/types.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <ctype.h>
#include <dirent.h>
#include <err.h>
#include <fcntl.h>
#include <grp.h>
#ifdef EXTENSIONS
#include <login_cap.h>
#endif
#include <pwd.h>
#include <regex.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <time.h>
#include <unistd.h>
#include <util.h>
#include <errno.h>
#include "pathnames.h"
#include "defs.h"
#include "usermgmt.h"
/* this struct describes a uid range */
typedef struct range_t {
int r_from; /* low uid */
int r_to; /* high uid */
} range_t;
typedef struct rangelist_t {
unsigned rl_rsize; /* size of range array */
unsigned rl_rc; /* # of ranges */
range_t *rl_rv; /* the ranges */
unsigned rl_defrc; /* # of ranges in defaults */
} rangelist_t;
/* this struct encapsulates the user and group information */
typedef struct user_t {
int u_flags; /* see below */
int u_uid; /* uid of user */
char *u_password; /* encrypted password */
char *u_comment; /* comment field */
char *u_home; /* home directory */
mode_t u_homeperm; /* permissions of home dir */
char *u_primgrp; /* primary group */
int u_groupc; /* # of secondary groups */
const char *u_groupv[NGROUPS_MAX]; /* secondary groups */
char *u_shell; /* user's shell */
char *u_basedir; /* base directory for home */
char *u_expire; /* when password will expire */
char *u_inactive; /* when account will expire */
char *u_skeldir; /* directory for startup files */
char *u_class; /* login class */
rangelist_t u_r; /* list of ranges */
unsigned u_defrc; /* # of ranges in defaults */
int u_preserve; /* preserve uids on deletion */
int u_allow_samba; /* allow trailing '$' for samba login names */
int u_locked; /* user account lock */
} user_t;
#define u_rsize u_r.rl_rsize
#define u_rc u_r.rl_rc
#define u_rv u_r.rl_rv
#define u_defrc u_r.rl_defrc
/* this struct encapsulates the user and group information */
typedef struct group_t {
rangelist_t g_r; /* list of ranges */
} group_t;
#define g_rsize g_r.rl_rsize
#define g_rc g_r.rl_rc
#define g_rv g_r.rl_rv
#define g_defrc g_r.rl_defrc
typedef struct def_t {
user_t user;
group_t group;
} def_t;
/* flags for which fields of the user_t replace the passwd entry */
enum {
F_COMMENT = 0x0001,
F_DUPUID = 0x0002,
F_EXPIRE = 0x0004,
F_GROUP = 0x0008,
F_HOMEDIR = 0x0010,
F_MKDIR = 0x0020,
F_INACTIVE = 0x0040,
F_PASSWORD = 0x0080,
F_SECGROUP = 0x0100,
F_SHELL = 0x0200,
F_UID = 0x0400,
F_USERNAME = 0x0800,
F_CLASS = 0x1000
};
#define UNLOCK 0
#define LOCK 1
#define LOCKED "*LOCKED*"
#ifndef DEF_GROUP
#define DEF_GROUP "users"
#endif
#ifndef DEF_BASEDIR
#define DEF_BASEDIR "/home"
#endif
#ifndef DEF_SKELDIR
#define DEF_SKELDIR "/etc/skel"
#endif
#ifndef DEF_SHELL
#define DEF_SHELL _PATH_BSHELL
#endif
#ifndef DEF_COMMENT
#define DEF_COMMENT ""
#endif
#ifndef DEF_LOWUID
#define DEF_LOWUID 1000
#endif
#ifndef DEF_HIGHUID
#define DEF_HIGHUID 60000
#endif
#ifndef DEF_INACTIVE
#define DEF_INACTIVE 0
#endif
#ifndef DEF_EXPIRE
#define DEF_EXPIRE NULL
#endif
#ifndef DEF_CLASS
#define DEF_CLASS ""
#endif
#ifndef WAITSECS
#define WAITSECS 10
#endif
#ifndef NOBODY_UID
#define NOBODY_UID 32767
#endif
#ifndef DEF_HOMEPERM
#define DEF_HOMEPERM 0755
#endif
/* some useful constants */
enum {
MaxShellNameLen = 256,
MaxFileNameLen = MAXPATHLEN,
MaxUserNameLen = LOGIN_NAME_MAX - 1,
MaxCommandLen = 2048,
MaxEntryLen = 2048,
PasswordLength = 2048,
DES_Len = 13,
};
#define UNSET_INACTIVE "Null (unset)"
#define UNSET_EXPIRY "Null (unset)"
static int asystem(const char *fmt, ...) __printflike(1, 2);
static int is_number(const char *);
static struct group *find_group_info(const char *);
static int verbose;
static char *
skipspace(char *s)
{
for (; *s && isspace((unsigned char)*s) ; s++) {
}
return s;
}
static int
check_numeric(const char *val, const char *name)
{
if (!is_number(val)) {
errx(EXIT_FAILURE, "When using [-%c %s], "
"the %s must be numeric", *name, name, name);
}
return atoi(val);
}
/* resize *cpp appropriately then assign `n' chars of `s' to it */
static void
memsave(char **cpp, const char *s, size_t n)
{
RENEW(char, *cpp, n + 1, exit(1));
(void)memcpy(*cpp, s, n);
(*cpp)[n] = '\0';
}
/* a replacement for system(3) */
static int
asystem(const char *fmt, ...)
{
va_list vp;
char buf[MaxCommandLen];
int ret;
va_start(vp, fmt);
(void)vsnprintf(buf, sizeof(buf), fmt, vp);
va_end(vp);
if (verbose) {
(void)printf("Command: %s\n", buf);
}
ret = system(buf);
if (ret == -1) {
warn("Error running `%s'", buf);
} else if (WIFSIGNALED(ret)) {
warnx("Error running `%s': Signal %d", buf, WTERMSIG(ret));
} else if (WIFEXITED(ret) && WEXITSTATUS(ret) != 0) {
warnx("Error running `%s': Exit %d", buf, WEXITSTATUS(ret));
}
return ret;
}
/* remove a users home directory, returning 1 for success (ie, no problems encountered) */
static int
removehomedir(struct passwd *pwp)
{
struct stat st;
/* userid not root? */
if (pwp->pw_uid == 0) {
warnx("Not deleting home directory `%s'; userid is 0", pwp->pw_dir);
return 0;
}
/* directory exists (and is a directory!) */
if (stat(pwp->pw_dir, &st) < 0) {
warn("Cannot access home directory `%s'", pwp->pw_dir);
return 0;
}
if (!S_ISDIR(st.st_mode)) {
warnx("Home directory `%s' is not a directory", pwp->pw_dir);
return 0;
}
/* userid matches directory owner? */
if (st.st_uid != pwp->pw_uid) {
warnx("User `%s' doesn't own directory `%s', not removed",
pwp->pw_name, pwp->pw_dir);
return 0;
}
(void)seteuid(pwp->pw_uid);
/* we add the "|| true" to keep asystem() quiet if there is a non-zero exit status. */
(void)asystem("%s -rf %s > /dev/null 2>&1 || true", _PATH_RM,
pwp->pw_dir);
(void)seteuid(0);
if (rmdir(pwp->pw_dir) < 0) {
warn("Unable to remove all files in `%s'", pwp->pw_dir);
return 0;
}
return 1;
}
/* return 1 if all of `s' is numeric */
static int
is_number(const char *s)
{
for ( ; *s ; s++) {
if (!isdigit((unsigned char) *s)) {
return 0;
}
}
return 1;
}
/*
* check that the effective uid is 0 - called from funcs which will
* modify data and config files.
*/
static void
checkeuid(void)
{
if (geteuid() != 0) {
errx(EXIT_FAILURE, "Program must be run as root");
}
}
/* copy any dot files into the user's home directory */
static int
copydotfiles(char *skeldir, int uid, int gid, char *dir, mode_t homeperm)
{
struct dirent *dp;
DIR *dirp;
int n;
if ((dirp = opendir(skeldir)) == NULL) {
warn("Can't open source . files dir `%s'", skeldir);
return 0;
}
for (n = 0; (dp = readdir(dirp)) != NULL && n == 0 ; ) {
if (strcmp(dp->d_name, ".") == 0 ||
strcmp(dp->d_name, "..") == 0) {
continue;
}
n = 1;
}
(void)closedir(dirp);
if (n == 0) {
warnx("No \"dot\" initialisation files found");
} else {
(void)asystem("cd %s && %s -rw -pe %s . %s",
skeldir, _PATH_PAX, (verbose) ? "-v" : "", dir);
}
(void)asystem("%s -R -h %d:%d %s", _PATH_CHOWN, uid, gid, dir);
(void)asystem("%s -R u+w %s", _PATH_CHMOD, dir);
#ifdef EXTENSIONS
(void)asystem("%s 0%o %s", _PATH_CHMOD, homeperm, dir);
#endif
return n;
}
/* create a group entry with gid `gid' */
static int
creategid(char *group, int gid, const char *name)
{
struct stat st;
FILE *from;
FILE *to;
char buf[MaxEntryLen];
char f[MaxFileNameLen];
int fd;
int cc;
if (getgrnam(group) != NULL) {
warnx("Can't create group `%s': already exists", group);
return 0;
}
if ((from = fopen(_PATH_GROUP, "r")) == NULL) {
warn("Can't create group `%s': can't open `%s'", name,
_PATH_GROUP);
return 0;
}
if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) {
warn("Can't lock `%s'", _PATH_GROUP);
(void)fclose(from);
return 0;
}
(void)fstat(fileno(from), &st);
(void)snprintf(f, sizeof(f), "%s.XXXXXX", _PATH_GROUP);
if ((fd = mkstemp(f)) < 0) {
warn("Can't create group `%s': mkstemp failed", group);
(void)fclose(from);
return 0;
}
if ((to = fdopen(fd, "w")) == NULL) {
warn("Can't create group `%s': fdopen `%s' failed",
group, f);
(void)fclose(from);
(void)close(fd);
(void)unlink(f);
return 0;
}
while ((cc = fread(buf, sizeof(char), sizeof(buf), from)) > 0) {
if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) {
warn("Can't create group `%s': short write to `%s'",
group, f);
(void)fclose(from);
(void)close(fd);
(void)unlink(f);
return 0;
}
}
(void)fprintf(to, "%s:*:%d:%s\n", group, gid, name);
(void)fclose(from);
(void)fclose(to);
if (rename(f, _PATH_GROUP) < 0) {
warn("Can't create group `%s': can't rename `%s' to `%s'",
group, f, _PATH_GROUP);
(void)unlink(f);
return 0;
}
(void)chmod(_PATH_GROUP, st.st_mode & 07777);
syslog(LOG_INFO, "New group added: name=%s, gid=%d", group, gid);
return 1;
}
/* modify the group entry with name `group' to be newent */
static int
modify_gid(char *group, char *newent)
{
struct stat st;
FILE *from;
FILE *to;
char buf[MaxEntryLen];
char f[MaxFileNameLen];
char *colon;
int groupc;
int entc;
int fd;
int cc;
if ((from = fopen(_PATH_GROUP, "r")) == NULL) {
warn("Can't modify group `%s': can't open `%s'",
group, _PATH_GROUP);
return 0;
}
if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) {
warn("Can't modify group `%s': can't lock `%s'",
group, _PATH_GROUP);
(void)fclose(from);
return 0;
}
(void)fstat(fileno(from), &st);
(void)snprintf(f, sizeof(f), "%s.XXXXXX", _PATH_GROUP);
if ((fd = mkstemp(f)) < 0) {
warn("Can't modify group `%s': mkstemp failed", group);
(void)fclose(from);
return 0;
}
if ((to = fdopen(fd, "w")) == NULL) {
warn("Can't modify group `%s': fdopen `%s' failed", group, f);
(void)fclose(from);
(void)close(fd);
(void)unlink(f);
return 0;
}
groupc = strlen(group);
while (fgets(buf, sizeof(buf), from) != NULL) {
cc = strlen(buf);
if ((colon = strchr(buf, ':')) == NULL) {
warnx("Badly formed entry `%s'", buf);
continue;
}
entc = (int)(colon - buf);
if (entc == groupc &&
strncmp(group, buf, (unsigned) entc) == 0) {
if (newent == NULL) {
struct group *grp_rm;
struct passwd *user_pwd;
/*
* Check that the group being removed
* isn't any user's Primary group. Just
* warn if it is. This could cause problems
* if the group GID was reused for a
* different purpose.
*/
grp_rm = find_group_info(group);
while ((user_pwd = getpwent()) != NULL) {
if (user_pwd->pw_gid == grp_rm->gr_gid) {
warnx("Warning: group `%s'(%d)"
" is the primary group of"
" `%s'. Use caution if you"
" later add this GID.",
grp_rm->gr_name,
grp_rm->gr_gid, user_pwd->pw_name);
}
}
endpwent();
continue;
} else {
cc = strlen(newent);
(void)strlcpy(buf, newent, sizeof(buf));
}
}
if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) {
warn("Can't modify group `%s': short write to `%s'",
group, f);
(void)fclose(from);
(void)close(fd);
(void)unlink(f);
return 0;
}
}
(void)fclose(from);
(void)fclose(to);
if (rename(f, _PATH_GROUP) < 0) {
warn("Can't modify group `%s': can't rename `%s' to `%s'",
group, f, _PATH_GROUP);
(void)unlink(f);
return 0;
}
(void)chmod(_PATH_GROUP, st.st_mode & 07777);
if (newent == NULL) {
syslog(LOG_INFO, "group deleted: name=%s", group);
} else {
syslog(LOG_INFO, "group information modified: name=%s", group);
}
return 1;
}
/* modify the group entries for all `groups', by adding `user' */
static int
append_group(char *user, int ngroups, const char **groups)
{
struct group *grp;
struct stat st;
FILE *from;
FILE *to;
char buf[MaxEntryLen];
char f[MaxFileNameLen];
char *colon;
int groupc;
int entc;
int fd;
int nc;
int cc;
int i;
int j;
for (i = 0 ; i < ngroups ; i++) {
if ((grp = getgrnam(groups[i])) == NULL) {
warnx("Can't append group `%s' for user `%s'",
groups[i], user);
} else {
for (j = 0 ; grp->gr_mem[j] ; j++) {
if (strcmp(user, grp->gr_mem[j]) == 0) {
/* already in it */
groups[i] = "";
}
}
}
}
if ((from = fopen(_PATH_GROUP, "r")) == NULL) {
warn("Can't append group(s) for `%s': can't open `%s'",
user, _PATH_GROUP);
return 0;
}
if (flock(fileno(from), LOCK_EX | LOCK_NB) < 0) {
warn("Can't append group(s) for `%s': can't lock `%s'",
user, _PATH_GROUP);
(void)fclose(from);
return 0;
}
(void)fstat(fileno(from), &st);
(void)snprintf(f, sizeof(f), "%s.XXXXXX", _PATH_GROUP);
if ((fd = mkstemp(f)) < 0) {
warn("Can't append group(s) for `%s': mkstemp failed",
user);
(void)fclose(from);
return 0;
}
if ((to = fdopen(fd, "w")) == NULL) {
warn("Can't append group(s) for `%s': fdopen `%s' failed",
user, f);
(void)fclose(from);
(void)close(fd);
(void)unlink(f);
return 0;
}
while (fgets(buf, sizeof(buf), from) != NULL) {
cc = strlen(buf);
if ((colon = strchr(buf, ':')) == NULL) {
warnx("Badly formed entry `%s'", buf);
continue;
}
entc = (int)(colon - buf);
for (i = 0 ; i < ngroups ; i++) {
if ((groupc = strlen(groups[i])) == 0) {
continue;
}
if (entc == groupc &&
strncmp(groups[i], buf, (unsigned) entc) == 0) {
if ((nc = snprintf(&buf[cc - 1],
sizeof(buf) - cc + 1, "%s%s\n",
(buf[cc - 2] == ':') ? "" : ",", user)) < 0) {
warnx("Warning: group `%s' "
"entry too long", groups[i]);
}
cc += nc - 1;
}
}
if (fwrite(buf, sizeof(char), (unsigned) cc, to) != cc) {
warn("Can't append group(s) for `%s':"
" short write to `%s'", user, f);
(void)fclose(from);
(void)close(fd);
(void)unlink(f);
return 0;
}
}
(void)fclose(from);
(void)fclose(to);
if (rename(f, _PATH_GROUP) < 0) {
warn("Can't append group(s) for `%s': "
"can't rename `%s' to `%s'", user, f, _PATH_GROUP);
(void)unlink(f);
return 0;
}
(void)chmod(_PATH_GROUP, st.st_mode & 07777);
return 1;
}
/* the valid characters for login and group names */
#define VALID_CHAR(c) (isalnum(c) || (c) == '.' || (c) == '_' || (c) == '-')
/* return 1 if `login' is a valid login name */
static int
valid_login(char *login_name, int allow_samba)
{
unsigned char *cp;
/* First character of a login name cannot be '-'. */
if (*login_name == '-') {
return 0;
}
if (strlen(login_name) >= LOGIN_NAME_MAX) {
return 0;
}
for (cp = (unsigned char *)login_name ; *cp ; cp++) {
if (!VALID_CHAR(*cp)) {
#ifdef EXTENSIONS
/* check for a trailing '$' in a Samba user name */
if (allow_samba && *cp == '$' && *(cp + 1) == 0x0) {
return 1;
}
#endif
return 0;
}
}
return 1;
}
/* return 1 if `group' is a valid group name */
static int
valid_group(char *group)
{
unsigned char *cp;
for (cp = (unsigned char *)group; *cp; cp++) {
if (!VALID_CHAR(*cp)) {
return 0;
}
}
return 1;
}
/* find the next gid in the range lo .. hi */
static int
getnextgid(int *gidp, int lo, int hi)
{
for (*gidp = lo ; *gidp < hi ; *gidp += 1) {
if (getgrgid((gid_t)*gidp) == NULL) {
return 1;
}
}
return 0;
}
#ifdef EXTENSIONS
/* save a range of uids */
static int
save_range(rangelist_t *rlp, char *cp)
{
int from;
int to;
int i;
if (rlp->rl_rsize == 0) {
rlp->rl_rsize = 32;
NEWARRAY(range_t, rlp->rl_rv, rlp->rl_rsize, return(0));
} else if (rlp->rl_rc == rlp->rl_rsize) {
rlp->rl_rsize *= 2;
RENEW(range_t, rlp->rl_rv, rlp->rl_rsize, return(0));
}
if (rlp->rl_rv && sscanf(cp, "%d..%d", &from, &to) == 2) {
for (i = rlp->rl_defrc ; i < rlp->rl_rc ; i++) {
if (rlp->rl_rv[i].r_from == from &&
rlp->rl_rv[i].r_to == to) {
break;
}
}
if (i == rlp->rl_rc) {
rlp->rl_rv[rlp->rl_rc].r_from = from;
rlp->rl_rv[rlp->rl_rc].r_to = to;
rlp->rl_rc += 1;
}
} else {
warnx("Bad range `%s'", cp);
return 0;
}
return 1;
}
#endif
/* set the defaults in the defaults file */
static int
setdefaults(user_t *up)
{
char template[MaxFileNameLen];
FILE *fp;
int ret;
int fd;
#ifdef EXTENSIONS
int i;
#endif
(void)snprintf(template, sizeof(template), "%s.XXXXXX",
_PATH_USERMGMT_CONF);
if ((fd = mkstemp(template)) < 0) {
warn("Can't set defaults: can't mkstemp `%s' for writing",
_PATH_USERMGMT_CONF);
return 0;
}
if ((fp = fdopen(fd, "w")) == NULL) {
warn("Can't set defaults: can't fdopen `%s' for writing",
_PATH_USERMGMT_CONF);
return 0;
}
ret = 1;
if (fprintf(fp, "group\t\t%s\n", up->u_primgrp) <= 0 ||
fprintf(fp, "base_dir\t%s\n", up->u_basedir) <= 0 ||
fprintf(fp, "skel_dir\t%s\n", up->u_skeldir) <= 0 ||
fprintf(fp, "shell\t\t%s\n", up->u_shell) <= 0 ||
#ifdef EXTENSIONS
fprintf(fp, "class\t\t%s\n", up->u_class) <= 0 ||
fprintf(fp, "homeperm\t0%o\n", up->u_homeperm) <= 0 ||
#endif
fprintf(fp, "inactive\t%s\n", (up->u_inactive == NULL) ?
UNSET_INACTIVE : up->u_inactive) <= 0 ||
fprintf(fp, "expire\t\t%s\n", (up->u_expire == NULL) ?
UNSET_EXPIRY : up->u_expire) <= 0 ||
fprintf(fp, "preserve\t%s\n", (up->u_preserve == 0) ?
"false" : "true") <= 0) {
warn("Can't write to `%s'", _PATH_USERMGMT_CONF);
ret = 0;
}
#ifdef EXTENSIONS
for (i = (up->u_defrc != up->u_rc) ? up->u_defrc : 0;
i < up->u_rc ; i++) {
if (fprintf(fp, "range\t\t%d..%d\n", up->u_rv[i].r_from,
up->u_rv[i].r_to) <= 0) {
warn("Can't set defaults: can't write to `%s'",
_PATH_USERMGMT_CONF);
ret = 0;
}
}
#endif
(void)fclose(fp);
if (ret) {
ret = ((rename(template, _PATH_USERMGMT_CONF) == 0) &&
(chmod(_PATH_USERMGMT_CONF, 0644) == 0));
}
return ret;
}
/* read the defaults file */
static void
read_defaults(def_t *dp)
{
struct stat st;
size_t lineno;
size_t len;
FILE *fp;
char *cp;
char *s;
user_t *up = &dp->user;
group_t *gp = &dp->group;
(void)memset(dp, 0, sizeof(*dp));
memsave(&up->u_primgrp, DEF_GROUP, strlen(DEF_GROUP));
memsave(&up->u_basedir, DEF_BASEDIR, strlen(DEF_BASEDIR));
memsave(&up->u_skeldir, DEF_SKELDIR, strlen(DEF_SKELDIR));
memsave(&up->u_shell, DEF_SHELL, strlen(DEF_SHELL));
memsave(&up->u_comment, DEF_COMMENT, strlen(DEF_COMMENT));
#ifdef EXTENSIONS
memsave(&up->u_class, DEF_CLASS, strlen(DEF_CLASS));
#endif
up->u_rsize = 16;
up->u_defrc = 0;
NEWARRAY(range_t, up->u_rv, up->u_rsize, exit(1));
up->u_inactive = DEF_INACTIVE;
up->u_expire = DEF_EXPIRE;
gp->g_rsize = 16;
gp->g_defrc = 0;
NEWARRAY(range_t, gp->g_rv, gp->g_rsize, exit(1));
if ((fp = fopen(_PATH_USERMGMT_CONF, "r")) == NULL) {
if (stat(_PATH_USERMGMT_CONF, &st) < 0 && !setdefaults(up)) {
warn("Can't create `%s' defaults file",
_PATH_USERMGMT_CONF);
}
fp = fopen(_PATH_USERMGMT_CONF, "r");
}
if (fp != NULL) {
while ((s = fparseln(fp, &len, &lineno, NULL, 0)) != NULL) {
if (strncmp(s, "group", 5) == 0) {
cp = skipspace(s + 5);
memsave(&up->u_primgrp, (char *)cp, strlen(cp));
} else if (strncmp(s, "base_dir", 8) == 0) {
cp = skipspace(s + 8);
memsave(&up->u_basedir, (char *)cp, strlen(cp));
} else if (strncmp(s, "skel_dir", 8) == 0) {
cp = skipspace(s + 8);
memsave(&up->u_skeldir, (char *)cp, strlen(cp));
} else if (strncmp(s, "shell", 5) == 0) {
cp = skipspace(s + 5);
memsave(&up->u_shell, cp, strlen(cp));
#ifdef EXTENSIONS
} else if (strncmp((char *)s, "class", 5) == 0) {
cp = skipspace(s + 5);
memsave(&up->u_class, cp, strlen(cp));
#endif
#ifdef EXTENSIONS
} else if (strncmp(s, "homeperm", 8) == 0) {
for (cp = s + 8; *cp &&
isspace((unsigned char)*cp); cp++)
;
up->u_homeperm = strtoul(cp, NULL, 8);
#endif
} else if (strncmp(s, "inactive", 8) == 0) {
cp = skipspace(s + 8);
if (strcmp(cp, UNSET_INACTIVE) == 0) {
if (up->u_inactive) {
FREE(up->u_inactive);
}
up->u_inactive = NULL;
} else {
memsave(&up->u_inactive, cp, strlen(cp));
}
#ifdef EXTENSIONS
} else if (strncmp(s, "range", 5) == 0) {
cp = skipspace(s + 5);
(void)save_range(&up->u_r, cp);
#endif
#ifdef EXTENSIONS
} else if (strncmp(s, "preserve", 8) == 0) {
cp = skipspace(s + 8);
up->u_preserve =
(strncmp(cp, "true", 4) == 0) ? 1 :
(strncmp(cp, "yes", 3) == 0) ? 1 : atoi(cp);
#endif
} else if (strncmp(s, "expire", 6) == 0) {
cp = skipspace(s + 6);
if (strcmp(cp, UNSET_EXPIRY) == 0) {
if (up->u_expire) {
FREE(up->u_expire);
}
up->u_expire = NULL;
} else {
memsave(&up->u_expire, cp, strlen(cp));
}
#ifdef EXTENSIONS
} else if (strncmp(s, "gid_range", 9) == 0) {
cp = skipspace(s + 9);
(void)save_range(&gp->g_r, cp);
#endif
}
(void)free(s);
}
(void)fclose(fp);
}
if (up->u_rc == 0) {
up->u_rv[up->u_rc].r_from = DEF_LOWUID;
up->u_rv[up->u_rc].r_to = DEF_HIGHUID;
up->u_rc += 1;
}
up->u_defrc = up->u_rc;
up->u_homeperm = DEF_HOMEPERM;
}
/* return the next valid unused uid */
static int
getnextuid(int sync_uid_gid, int *uid, int low_uid, int high_uid)
{
for (*uid = low_uid ; *uid <= high_uid ; (*uid)++) {
if (getpwuid((uid_t)(*uid)) == NULL && *uid != NOBODY_UID) {
if (sync_uid_gid) {
if (getgrgid((gid_t)(*uid)) == NULL) {
return 1;
}
} else {
return 1;
}
}
}
return 0;
}
/* structure which defines a password type */
typedef struct passwd_type_t {
const char *type; /* optional type descriptor */
size_t desc_length; /* length of type descriptor */
size_t length; /* length of password */
const char *regex; /* regexp to output the password */
size_t re_sub; /* subscript of regexp to use */
} passwd_type_t;
static passwd_type_t passwd_types[] = {
{ "$sha1", 5, 28, "\\$[^$]+\\$[^$]+\\$[^$]+\\$(.*)", 1 }, /* SHA1 */
{ "$2a", 3, 53, "\\$[^$]+\\$[^$]+\\$(.*)", 1 }, /* Blowfish */
{ "$1", 2, 34, NULL, 0 }, /* MD5 */
{ "", 0, DES_Len,NULL, 0 }, /* standard DES */
{ NULL, (size_t)~0, (size_t)~0, NULL, 0 }
/* none - terminate search */
};
/* return non-zero if it's a valid password - check length for cipher type */
static int
valid_password_length(char *newpasswd)
{
passwd_type_t *pwtp;
regmatch_t matchv[10];
regex_t r;
for (pwtp = passwd_types; pwtp->desc_length != (size_t)~0; pwtp++) {
if (strncmp(newpasswd, pwtp->type, pwtp->desc_length) == 0) {
if (pwtp->regex == NULL) {
return strlen(newpasswd) == pwtp->length;
}
(void)regcomp(&r, pwtp->regex, REG_EXTENDED);
if (regexec(&r, newpasswd, 10, matchv, 0) == 0) {
regfree(&r);
return (int)(matchv[pwtp->re_sub].rm_eo -
matchv[pwtp->re_sub].rm_so) ==
pwtp->length;
}
regfree(&r);
}
}
return 0;
}
#ifdef EXTENSIONS
/* return 1 if `class' is a valid login class */
static int
valid_class(char *class)
{
login_cap_t *lc;
if (class == NULL || *class == '\0') {
return 1;
}
/*
* Check if /etc/login.conf exists. login_getclass() will
* return 1 due to it not existing, so not informing the
* user the actual login class does not exist.
*/
if (access(_PATH_LOGINCONF, R_OK) == -1) {
warn("Access failed for `%s'; will not validate class `%s'",
_PATH_LOGINCONF, class);
return 1;
}
if ((lc = login_getclass(class)) != NULL) {
login_close(lc);
return 1;
}
return 0;
}
/* return 1 if the `shellname' is a valid user shell */