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