field.c revision 1.10 1 /* $NetBSD: field.c,v 1.10 2004/10/30 17:11:24 dsl Exp $ */
2
3 /*
4 * Copyright (c) 1988, 1993, 1994
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.
15 * 3. Neither the name of the University nor the names of its contributors
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
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)field.c 8.4 (Berkeley) 4/2/94";
36 #else
37 __RCSID("$NetBSD: field.c,v 1.10 2004/10/30 17:11:24 dsl Exp $");
38 #endif
39 #endif /* not lint */
40
41 #include <sys/param.h>
42
43 #include <ctype.h>
44 #include <err.h>
45 #include <errno.h>
46 #include <grp.h>
47 #include <pwd.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
52
53 #include "chpass.h"
54 #include "pathnames.h"
55
56 /* ARGSUSED */
57 int
58 p_login(p, pw, ep)
59 const char *p;
60 struct passwd *pw;
61 ENTRY *ep;
62 {
63
64 if (!*p) {
65 warnx("empty login field");
66 return (1);
67 }
68 if (*p == '-') {
69 warnx("login names may not begin with a hyphen");
70 return (1);
71 }
72 if (!(pw->pw_name = strdup(p))) {
73 warnx("can't save entry");
74 return (1);
75 }
76 if (strchr(p, '.'))
77 warnx("\'.\' is dangerous in a login name");
78 for (; *p; ++p)
79 if (isupper((unsigned char)*p)) {
80 warnx("upper-case letters are dangerous in a login name");
81 break;
82 }
83 return (0);
84 }
85
86 /* ARGSUSED */
87 int
88 p_passwd(p, pw, ep)
89 const char *p;
90 struct passwd *pw;
91 ENTRY *ep;
92 {
93
94 if (!*p)
95 pw->pw_passwd = ""; /* "NOLOGIN"; */
96 else if (!(pw->pw_passwd = strdup(p))) {
97 warnx("can't save password entry");
98 return (1);
99 }
100
101 return (0);
102 }
103
104 /* ARGSUSED */
105 int
106 p_uid(p, pw, ep)
107 const char *p;
108 struct passwd *pw;
109 ENTRY *ep;
110 {
111 unsigned long id;
112 char *np;
113
114 if (!*p) {
115 warnx("empty uid field");
116 return (1);
117 }
118 if (!isdigit((unsigned char)*p)) {
119 warnx("illegal uid");
120 return (1);
121 }
122 errno = 0;
123 id = strtoul(p, &np, 10);
124 /*
125 * We don't need to check the return value of strtoul()
126 * since ULONG_MAX is greater than UID_MAX.
127 */
128 if (*np || id > UID_MAX) {
129 warnx("illegal uid");
130 return (1);
131 }
132 pw->pw_uid = (uid_t)id;
133 return (0);
134 }
135
136 /* ARGSUSED */
137 int
138 p_gid(p, pw, ep)
139 const char *p;
140 struct passwd *pw;
141 ENTRY *ep;
142 {
143 struct group *gr;
144 unsigned long id;
145 char *np;
146
147 if (!*p) {
148 warnx("empty gid field");
149 return (1);
150 }
151 if (!isdigit((unsigned char)*p)) {
152 if (!(gr = getgrnam(p))) {
153 warnx("unknown group %s", p);
154 return (1);
155 }
156 pw->pw_gid = gr->gr_gid;
157 return (0);
158 }
159 errno = 0;
160 id = strtoul(p, &np, 10);
161 /*
162 * We don't need to check the return value of strtoul()
163 * since ULONG_MAX is greater than GID_MAX.
164 */
165 if (*np || id > GID_MAX) {
166 warnx("illegal gid");
167 return (1);
168 }
169 pw->pw_gid = (gid_t)id;
170 return (0);
171 }
172
173 /* ARGSUSED */
174 int
175 p_class(p, pw, ep)
176 const char *p;
177 struct passwd *pw;
178 ENTRY *ep;
179 {
180
181 if (!*p)
182 pw->pw_class = "";
183 else if (!(pw->pw_class = strdup(p))) {
184 warnx("can't save entry");
185 return (1);
186 }
187
188 return (0);
189 }
190
191 /* ARGSUSED */
192 int
193 p_change(p, pw, ep)
194 const char *p;
195 struct passwd *pw;
196 ENTRY *ep;
197 {
198
199 if (!atot(p, &pw->pw_change))
200 return (0);
201 warnx("illegal date for change field");
202 return (1);
203 }
204
205 /* ARGSUSED */
206 int
207 p_expire(p, pw, ep)
208 const char *p;
209 struct passwd *pw;
210 ENTRY *ep;
211 {
212
213 if (!atot(p, &pw->pw_expire))
214 return (0);
215 warnx("illegal date for expire field");
216 return (1);
217 }
218
219 /* ARGSUSED */
220 int
221 p_gecos(p, pw, ep)
222 const char *p;
223 struct passwd *pw;
224 ENTRY *ep;
225 {
226
227 if (!(ep->save = strdup(p))) {
228 warnx("can't save entry");
229 return (1);
230 }
231 return (0);
232 }
233
234 /* ARGSUSED */
235 int
236 p_hdir(p, pw, ep)
237 const char *p;
238 struct passwd *pw;
239 ENTRY *ep;
240 {
241
242 if (!*p) {
243 warnx("empty home directory field");
244 return (1);
245 }
246 if (!(pw->pw_dir = strdup(p))) {
247 warnx("can't save entry");
248 return (1);
249 }
250 return (0);
251 }
252
253 /* ARGSUSED */
254 int
255 p_shell(p, pw, ep)
256 const char *p;
257 struct passwd *pw;
258 ENTRY *ep;
259 {
260 const char *t;
261
262 if (!*p) {
263 pw->pw_shell = _PATH_BSHELL;
264 return (0);
265 }
266 /* only admin can change from or to "restricted" shells */
267 if (uid && pw->pw_shell && !ok_shell(pw->pw_shell)) {
268 warnx("%s: current shell non-standard", pw->pw_shell);
269 return (1);
270 }
271 if (!(t = ok_shell(p))) {
272 if (uid) {
273 warnx("%s: non-standard shell", p);
274 return (1);
275 }
276 }
277 else
278 p = t;
279 if (!(pw->pw_shell = strdup(p))) {
280 warnx("can't save entry");
281 return (1);
282 }
283 return (0);
284 }
285