]>
Commit | Line | Data |
---|---|---|
c3a08f59 A |
1 | /* $OpenBSD: passwd.c,v 1.42 2003/06/26 16:34:42 deraadt Exp $ */ |
2 | ||
1815bff5 | 3 | /* |
c3a08f59 | 4 | * Copyright (c) 1987, 1993, 1994, 1995 |
1815bff5 A |
5 | * The Regents of the University of California. All rights reserved. |
6 | * | |
7 | * Redistribution and use in source and binary forms, with or without | |
8 | * modification, are permitted provided that the following conditions | |
9 | * are met: | |
10 | * 1. Redistributions of source code must retain the above copyright | |
11 | * notice, this list of conditions and the following disclaimer. | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | |
c3a08f59 | 15 | * 3. Neither the name of the University nor the names of its contributors |
1815bff5 A |
16 | * may be used to endorse or promote products derived from this software |
17 | * without specific prior written permission. | |
18 | * | |
19 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
29 | * SUCH DAMAGE. | |
30 | */ | |
31 | ||
c3a08f59 A |
32 | #if defined(LIBC_SCCS) && !defined(lint) |
33 | static const char rcsid[] = "$OpenBSD: passwd.c,v 1.42 2003/06/26 16:34:42 deraadt Exp $"; | |
34 | #endif /* LIBC_SCCS and not lint */ | |
1815bff5 | 35 | |
c3a08f59 A |
36 | #include <sys/types.h> |
37 | #include <sys/stat.h> | |
38 | #include <sys/time.h> | |
39 | #include <sys/resource.h> | |
40 | #include <sys/wait.h> | |
1815bff5 | 41 | |
1815bff5 | 42 | #include <fcntl.h> |
c3a08f59 A |
43 | #include <unistd.h> |
44 | #include <stdlib.h> | |
1815bff5 A |
45 | #include <stdio.h> |
46 | #include <string.h> | |
c3a08f59 A |
47 | #include <ctype.h> |
48 | #include <pwd.h> | |
49 | #include <err.h> | |
50 | #include <errno.h> | |
51 | #include <paths.h> | |
52 | #include <signal.h> | |
53 | #include <limits.h> | |
1815bff5 | 54 | |
c3a08f59 | 55 | #include "util.h" |
1815bff5 A |
56 | |
57 | int | |
c3a08f59 | 58 | pw_scan(char *bp, struct passwd *pw, int *flags) |
1815bff5 | 59 | { |
c3a08f59 | 60 | u_long id; |
1815bff5 | 61 | int root; |
c3a08f59 A |
62 | char *p, *sh, *p2; |
63 | ||
64 | if (flags != (int *)NULL) | |
65 | *flags = 0; | |
1815bff5 | 66 | |
83f6dbe8 A |
67 | #ifdef __APPLE__ |
68 | if (bp[0] == '#') { | |
69 | pw->pw_name = NULL; | |
70 | return(1); | |
71 | } | |
72 | #endif | |
73 | ||
c3a08f59 | 74 | if (!(p = strsep(&bp, ":")) || *p == '\0') /* login */ |
1815bff5 | 75 | goto fmt; |
c3a08f59 | 76 | pw->pw_name = p; |
1815bff5 A |
77 | root = !strcmp(pw->pw_name, "root"); |
78 | ||
79 | if (!(pw->pw_passwd = strsep(&bp, ":"))) /* passwd */ | |
80 | goto fmt; | |
81 | ||
82 | if (!(p = strsep(&bp, ":"))) /* uid */ | |
83 | goto fmt; | |
c3a08f59 | 84 | id = strtoul(p, &p2, 10); |
1815bff5 A |
85 | if (root && id) { |
86 | warnx("root uid should be 0"); | |
87 | return (0); | |
88 | } | |
c3a08f59 A |
89 | if (*p2 != '\0') { |
90 | warnx("illegal uid field"); | |
1815bff5 A |
91 | return (0); |
92 | } | |
c3a08f59 A |
93 | #ifndef __APPLE__ |
94 | /* Apple's UID_MAX is too small (sizeof signed) 3091256 */ | |
95 | if (id > UID_MAX) { | |
96 | /* errno is set to ERANGE by strtoul(3) */ | |
97 | warnx("uid greater than %u", UID_MAX-1); | |
98 | return (0); | |
99 | } | |
100 | #endif | |
101 | pw->pw_uid = (uid_t)id; | |
102 | if ((*p == '\0') && (flags != (int *)NULL)) | |
103 | *flags |= _PASSWORD_NOUID; | |
1815bff5 A |
104 | |
105 | if (!(p = strsep(&bp, ":"))) /* gid */ | |
106 | goto fmt; | |
c3a08f59 A |
107 | id = strtoul(p, &p2, 10); |
108 | if (*p2 != '\0') { | |
109 | warnx("illegal gid field"); | |
110 | return (0); | |
20e66415 | 111 | } |
c3a08f59 A |
112 | #ifndef __APPLE__ |
113 | /* Apple's UID_MAX is too small (sizeof signed) 3091256 */ | |
114 | if (id > UID_MAX) { | |
115 | /* errno is set to ERANGE by strtoul(3) */ | |
116 | warnx("gid greater than %u", UID_MAX-1); | |
1815bff5 A |
117 | return (0); |
118 | } | |
c3a08f59 A |
119 | #endif |
120 | pw->pw_gid = (gid_t)id; | |
121 | if ((*p == '\0') && (flags != (int *)NULL)) | |
122 | *flags |= _PASSWORD_NOGID; | |
1815bff5 A |
123 | |
124 | pw->pw_class = strsep(&bp, ":"); /* class */ | |
125 | if (!(p = strsep(&bp, ":"))) /* change */ | |
126 | goto fmt; | |
127 | pw->pw_change = atol(p); | |
c3a08f59 A |
128 | if ((*p == '\0') && (flags != (int *)NULL)) |
129 | *flags |= _PASSWORD_NOCHG; | |
1815bff5 A |
130 | if (!(p = strsep(&bp, ":"))) /* expire */ |
131 | goto fmt; | |
132 | pw->pw_expire = atol(p); | |
c3a08f59 A |
133 | if ((*p == '\0') && (flags != (int *)NULL)) |
134 | *flags |= _PASSWORD_NOEXP; | |
1815bff5 A |
135 | pw->pw_gecos = strsep(&bp, ":"); /* gecos */ |
136 | pw->pw_dir = strsep(&bp, ":"); /* directory */ | |
137 | if (!(pw->pw_shell = strsep(&bp, ":"))) /* shell */ | |
138 | goto fmt; | |
139 | ||
140 | p = pw->pw_shell; | |
c3a08f59 | 141 | if (root && *p) { /* empty == /bin/sh */ |
1815bff5 A |
142 | for (setusershell();;) { |
143 | if (!(sh = getusershell())) { | |
144 | warnx("warning, unknown root shell"); | |
145 | break; | |
146 | } | |
147 | if (!strcmp(p, sh)) | |
c3a08f59 | 148 | break; |
1815bff5 | 149 | } |
c3a08f59 A |
150 | endusershell(); |
151 | } | |
1815bff5 | 152 | |
c3a08f59 | 153 | if ((p = strsep(&bp, ":"))) { /* too many */ |
1815bff5 A |
154 | fmt: warnx("corrupted entry"); |
155 | return (0); | |
156 | } | |
c3a08f59 | 157 | |
1815bff5 A |
158 | return (1); |
159 | } |