field.c revision 1.8 1 1.8 simonb /* $NetBSD: field.c,v 1.8 2002/05/09 01:58:39 simonb Exp $ */
2 1.3 glass
3 1.1 cgd /*
4 1.3 glass * Copyright (c) 1988, 1993, 1994
5 1.3 glass * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.4 lukem #include <sys/cdefs.h>
37 1.1 cgd #ifndef lint
38 1.3 glass #if 0
39 1.3 glass static char sccsid[] = "@(#)field.c 8.4 (Berkeley) 4/2/94";
40 1.3 glass #else
41 1.8 simonb __RCSID("$NetBSD: field.c,v 1.8 2002/05/09 01:58:39 simonb Exp $");
42 1.3 glass #endif
43 1.1 cgd #endif /* not lint */
44 1.1 cgd
45 1.1 cgd #include <sys/param.h>
46 1.3 glass
47 1.3 glass #include <ctype.h>
48 1.3 glass #include <err.h>
49 1.3 glass #include <errno.h>
50 1.3 glass #include <grp.h>
51 1.1 cgd #include <pwd.h>
52 1.3 glass #include <stdio.h>
53 1.3 glass #include <stdlib.h>
54 1.1 cgd #include <string.h>
55 1.3 glass #include <unistd.h>
56 1.3 glass
57 1.1 cgd #include "chpass.h"
58 1.1 cgd #include "pathnames.h"
59 1.1 cgd
60 1.1 cgd /* ARGSUSED */
61 1.3 glass int
62 1.1 cgd p_login(p, pw, ep)
63 1.6 mycroft const char *p;
64 1.1 cgd struct passwd *pw;
65 1.1 cgd ENTRY *ep;
66 1.1 cgd {
67 1.5 mrg
68 1.1 cgd if (!*p) {
69 1.3 glass warnx("empty login field");
70 1.3 glass return (1);
71 1.1 cgd }
72 1.1 cgd if (*p == '-') {
73 1.3 glass warnx("login names may not begin with a hyphen");
74 1.3 glass return (1);
75 1.1 cgd }
76 1.1 cgd if (!(pw->pw_name = strdup(p))) {
77 1.3 glass warnx("can't save entry");
78 1.3 glass return (1);
79 1.1 cgd }
80 1.3 glass if (strchr(p, '.'))
81 1.3 glass warnx("\'.\' is dangerous in a login name");
82 1.1 cgd for (; *p; ++p)
83 1.1 cgd if (isupper(*p)) {
84 1.3 glass warnx("upper-case letters are dangerous in a login name");
85 1.1 cgd break;
86 1.1 cgd }
87 1.3 glass return (0);
88 1.1 cgd }
89 1.1 cgd
90 1.1 cgd /* ARGSUSED */
91 1.3 glass int
92 1.1 cgd p_passwd(p, pw, ep)
93 1.6 mycroft const char *p;
94 1.1 cgd struct passwd *pw;
95 1.1 cgd ENTRY *ep;
96 1.1 cgd {
97 1.5 mrg
98 1.1 cgd if (!*p)
99 1.1 cgd pw->pw_passwd = ""; /* "NOLOGIN"; */
100 1.1 cgd else if (!(pw->pw_passwd = strdup(p))) {
101 1.3 glass warnx("can't save password entry");
102 1.3 glass return (1);
103 1.1 cgd }
104 1.1 cgd
105 1.3 glass return (0);
106 1.1 cgd }
107 1.1 cgd
108 1.1 cgd /* ARGSUSED */
109 1.3 glass int
110 1.1 cgd p_uid(p, pw, ep)
111 1.6 mycroft const char *p;
112 1.1 cgd struct passwd *pw;
113 1.1 cgd ENTRY *ep;
114 1.1 cgd {
115 1.8 simonb unsigned long id;
116 1.3 glass char *np;
117 1.1 cgd
118 1.1 cgd if (!*p) {
119 1.3 glass warnx("empty uid field");
120 1.3 glass return (1);
121 1.1 cgd }
122 1.1 cgd if (!isdigit(*p)) {
123 1.3 glass warnx("illegal uid");
124 1.3 glass return (1);
125 1.1 cgd }
126 1.3 glass errno = 0;
127 1.3 glass id = strtoul(p, &np, 10);
128 1.8 simonb /*
129 1.8 simonb * We don't need to check the return value of strtoul()
130 1.8 simonb * since ULONG_MAX is greater than UID_MAX.
131 1.8 simonb */
132 1.8 simonb if (*np || id > UID_MAX) {
133 1.3 glass warnx("illegal uid");
134 1.3 glass return (1);
135 1.1 cgd }
136 1.8 simonb pw->pw_uid = (uid_t)id;
137 1.3 glass return (0);
138 1.1 cgd }
139 1.1 cgd
140 1.1 cgd /* ARGSUSED */
141 1.3 glass int
142 1.1 cgd p_gid(p, pw, ep)
143 1.6 mycroft const char *p;
144 1.1 cgd struct passwd *pw;
145 1.1 cgd ENTRY *ep;
146 1.1 cgd {
147 1.1 cgd struct group *gr;
148 1.8 simonb unsigned long id;
149 1.3 glass char *np;
150 1.1 cgd
151 1.1 cgd if (!*p) {
152 1.3 glass warnx("empty gid field");
153 1.3 glass return (1);
154 1.1 cgd }
155 1.1 cgd if (!isdigit(*p)) {
156 1.1 cgd if (!(gr = getgrnam(p))) {
157 1.3 glass warnx("unknown group %s", p);
158 1.3 glass return (1);
159 1.1 cgd }
160 1.1 cgd pw->pw_gid = gr->gr_gid;
161 1.3 glass return (0);
162 1.1 cgd }
163 1.3 glass errno = 0;
164 1.3 glass id = strtoul(p, &np, 10);
165 1.8 simonb /*
166 1.8 simonb * We don't need to check the return value of strtoul()
167 1.8 simonb * since ULONG_MAX is greater than GID_MAX.
168 1.8 simonb */
169 1.8 simonb if (*np || id > GID_MAX) {
170 1.3 glass warnx("illegal gid");
171 1.3 glass return (1);
172 1.1 cgd }
173 1.8 simonb pw->pw_gid = (gid_t)id;
174 1.3 glass return (0);
175 1.1 cgd }
176 1.1 cgd
177 1.1 cgd /* ARGSUSED */
178 1.3 glass int
179 1.1 cgd p_class(p, pw, ep)
180 1.6 mycroft const char *p;
181 1.1 cgd struct passwd *pw;
182 1.1 cgd ENTRY *ep;
183 1.1 cgd {
184 1.5 mrg
185 1.1 cgd if (!*p)
186 1.1 cgd pw->pw_class = "";
187 1.1 cgd else if (!(pw->pw_class = strdup(p))) {
188 1.3 glass warnx("can't save entry");
189 1.3 glass return (1);
190 1.1 cgd }
191 1.1 cgd
192 1.3 glass return (0);
193 1.1 cgd }
194 1.1 cgd
195 1.1 cgd /* ARGSUSED */
196 1.3 glass int
197 1.1 cgd p_change(p, pw, ep)
198 1.6 mycroft const char *p;
199 1.1 cgd struct passwd *pw;
200 1.1 cgd ENTRY *ep;
201 1.1 cgd {
202 1.5 mrg
203 1.1 cgd if (!atot(p, &pw->pw_change))
204 1.3 glass return (0);
205 1.3 glass warnx("illegal date for change field");
206 1.3 glass return (1);
207 1.1 cgd }
208 1.1 cgd
209 1.1 cgd /* ARGSUSED */
210 1.3 glass int
211 1.1 cgd p_expire(p, pw, ep)
212 1.6 mycroft const char *p;
213 1.1 cgd struct passwd *pw;
214 1.1 cgd ENTRY *ep;
215 1.1 cgd {
216 1.5 mrg
217 1.1 cgd if (!atot(p, &pw->pw_expire))
218 1.3 glass return (0);
219 1.3 glass warnx("illegal date for expire field");
220 1.3 glass return (1);
221 1.1 cgd }
222 1.1 cgd
223 1.1 cgd /* ARGSUSED */
224 1.3 glass int
225 1.1 cgd p_gecos(p, pw, ep)
226 1.6 mycroft const char *p;
227 1.1 cgd struct passwd *pw;
228 1.1 cgd ENTRY *ep;
229 1.1 cgd {
230 1.5 mrg
231 1.7 kim if (!(ep->save = strdup(p))) {
232 1.3 glass warnx("can't save entry");
233 1.3 glass return (1);
234 1.1 cgd }
235 1.3 glass return (0);
236 1.1 cgd }
237 1.1 cgd
238 1.1 cgd /* ARGSUSED */
239 1.3 glass int
240 1.1 cgd p_hdir(p, pw, ep)
241 1.6 mycroft const char *p;
242 1.1 cgd struct passwd *pw;
243 1.1 cgd ENTRY *ep;
244 1.1 cgd {
245 1.5 mrg
246 1.1 cgd if (!*p) {
247 1.3 glass warnx("empty home directory field");
248 1.3 glass return (1);
249 1.1 cgd }
250 1.1 cgd if (!(pw->pw_dir = strdup(p))) {
251 1.3 glass warnx("can't save entry");
252 1.3 glass return (1);
253 1.1 cgd }
254 1.3 glass return (0);
255 1.1 cgd }
256 1.1 cgd
257 1.1 cgd /* ARGSUSED */
258 1.3 glass int
259 1.1 cgd p_shell(p, pw, ep)
260 1.6 mycroft const char *p;
261 1.1 cgd struct passwd *pw;
262 1.1 cgd ENTRY *ep;
263 1.1 cgd {
264 1.6 mycroft const char *t;
265 1.1 cgd
266 1.1 cgd if (!*p) {
267 1.1 cgd pw->pw_shell = _PATH_BSHELL;
268 1.3 glass return (0);
269 1.1 cgd }
270 1.1 cgd /* only admin can change from or to "restricted" shells */
271 1.1 cgd if (uid && pw->pw_shell && !ok_shell(pw->pw_shell)) {
272 1.3 glass warnx("%s: current shell non-standard", pw->pw_shell);
273 1.3 glass return (1);
274 1.1 cgd }
275 1.1 cgd if (!(t = ok_shell(p))) {
276 1.1 cgd if (uid) {
277 1.3 glass warnx("%s: non-standard shell", p);
278 1.3 glass return (1);
279 1.1 cgd }
280 1.1 cgd }
281 1.1 cgd else
282 1.1 cgd p = t;
283 1.1 cgd if (!(pw->pw_shell = strdup(p))) {
284 1.3 glass warnx("can't save entry");
285 1.3 glass return (1);
286 1.1 cgd }
287 1.3 glass return (0);
288 1.1 cgd }
289