Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / usr.bin / chpass / field.c
blobacd7d67754bf4515eca851983812c27e489640c8
1 /* $NetBSD: field.c,v 1.11 2005/02/17 17:09:48 xtraeme Exp $ */
3 /*
4 * Copyright (c) 1988, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
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.
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.
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.11 2005/02/17 17:09:48 xtraeme Exp $");
38 #endif
39 #endif /* not lint */
41 #include <sys/param.h>
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>
53 #include "chpass.h"
54 #include "pathnames.h"
56 /* ARGSUSED */
57 int
58 p_login(const char *p, struct passwd *pw, ENTRY *ep)
61 if (!*p) {
62 warnx("empty login field");
63 return (1);
65 if (*p == '-') {
66 warnx("login names may not begin with a hyphen");
67 return (1);
69 if (!(pw->pw_name = strdup(p))) {
70 warnx("can't save entry");
71 return (1);
73 if (strchr(p, '.'))
74 warnx("\'.\' is dangerous in a login name");
75 for (; *p; ++p)
76 if (isupper((unsigned char)*p)) {
77 warnx("upper-case letters are dangerous in a login name");
78 break;
80 return (0);
83 /* ARGSUSED */
84 int
85 p_passwd(const char *p, struct passwd *pw, ENTRY *ep)
88 if (!(pw->pw_passwd = strdup(p))) {
89 warnx("can't save password entry");
90 return (1);
93 return (0);
96 /* ARGSUSED */
97 int
98 p_uid(const char *p, struct passwd *pw, ENTRY *ep)
100 unsigned long id;
101 char *np;
103 if (!*p) {
104 warnx("empty uid field");
105 return (1);
107 if (!isdigit((unsigned char)*p)) {
108 warnx("illegal uid");
109 return (1);
111 errno = 0;
112 id = strtoul(p, &np, 10);
114 * We don't need to check the return value of strtoul()
115 * since ULONG_MAX is greater than UID_MAX.
117 if (*np || id > UID_MAX) {
118 warnx("illegal uid");
119 return (1);
121 pw->pw_uid = (uid_t)id;
122 return (0);
125 /* ARGSUSED */
127 p_gid(const char *p, struct passwd *pw, ENTRY *ep)
129 struct group *gr;
130 unsigned long id;
131 char *np;
133 if (!*p) {
134 warnx("empty gid field");
135 return (1);
137 if (!isdigit((unsigned char)*p)) {
138 if (!(gr = getgrnam(p))) {
139 warnx("unknown group %s", p);
140 return (1);
142 pw->pw_gid = gr->gr_gid;
143 return (0);
145 errno = 0;
146 id = strtoul(p, &np, 10);
148 * We don't need to check the return value of strtoul()
149 * since ULONG_MAX is greater than GID_MAX.
151 if (*np || id > GID_MAX) {
152 warnx("illegal gid");
153 return (1);
155 pw->pw_gid = (gid_t)id;
156 return (0);
159 /* ARGSUSED */
161 p_class(const char *p, struct passwd *pw, ENTRY *ep)
164 if (!(pw->pw_class = strdup(p))) {
165 warnx("can't save entry");
166 return (1);
169 return (0);
172 /* ARGSUSED */
174 p_change(const char *p, struct passwd *pw, ENTRY *ep)
177 if (!atot(p, &pw->pw_change))
178 return (0);
179 warnx("illegal date for change field");
180 return (1);
183 /* ARGSUSED */
185 p_expire(const char *p, struct passwd *pw, ENTRY *ep)
188 if (!atot(p, &pw->pw_expire))
189 return (0);
190 warnx("illegal date for expire field");
191 return (1);
194 /* ARGSUSED */
196 p_gecos(const char *p, struct passwd *pw, ENTRY *ep)
199 if (!(ep->save = strdup(p))) {
200 warnx("can't save entry");
201 return (1);
203 return (0);
206 /* ARGSUSED */
208 p_hdir(const char *p, struct passwd *pw, ENTRY *ep)
211 if (!*p) {
212 warnx("empty home directory field");
213 return (1);
215 if (!(pw->pw_dir = strdup(p))) {
216 warnx("can't save entry");
217 return (1);
219 return (0);
222 /* ARGSUSED */
224 p_shell(const char *p, struct passwd *pw, ENTRY *ep)
226 const char *t;
228 if (!*p) {
229 if (!(pw->pw_shell = strdup(_PATH_BSHELL))) {
230 warnx("can't save entry");
231 return (1);
233 return (0);
235 /* only admin can change from or to "restricted" shells */
236 if (uid && pw->pw_shell && !ok_shell(pw->pw_shell)) {
237 warnx("%s: current shell non-standard", pw->pw_shell);
238 return (1);
240 if (!(t = ok_shell(p))) {
241 if (uid) {
242 warnx("%s: non-standard shell", p);
243 return (1);
246 else
247 p = t;
248 if (!(pw->pw_shell = strdup(p))) {
249 warnx("can't save entry");
250 return (1);
252 return (0);