1 /*	$OpenBSD: field.c,v 1.7 2004/07/05 18:47:49 millert Exp $	*/
2 /*	$NetBSD: field.c,v 1.3 1995/03/26 04:55:28 glass Exp $	*/
3 
4 /*
5  * Copyright (c) 1988, 1993, 1994
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)field.c	8.4 (Berkeley) 4/2/94";
36 #else
37 static char rcsid[] = "$OpenBSD: field.c,v 1.7 2004/07/05 18:47:49 millert 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 <paths.h>
48 #include <pwd.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <unistd.h>
53 
54 #include "chpass.h"
55 
56 /* ARGSUSED */
57 int
p_login(char * p,struct passwd * pw,ENTRY * ep)58 p_login(char *p, struct passwd *pw, ENTRY *ep)
59 {
60 	struct passwd *tpw;
61 
62 	if (!*p) {
63 		warnx("empty login field");
64 		return (1);
65 	}
66 	if (*p == '-') {
67 		warnx("login names may not begin with a hyphen");
68 		return (1);
69 	}
70 	/* XXX - what about truncated names? */
71 	if (strcmp(pw->pw_name, p) != 0 && (tpw = getpwnam(p)) != NULL) {
72 		warnx("login %s already exists", p);
73 		return (1);
74 	}
75 	if (!(pw->pw_name = strdup(p))) {
76 		warnx("can't save entry");
77 		return (1);
78 	}
79 	if (strchr(p, '.'))
80 		warnx("\'.\' is dangerous in a login name");
81 	for (; *p; ++p)
82 		if (isupper(*p)) {
83 			warnx("upper-case letters are dangerous in a login name");
84 			break;
85 		}
86 	return (0);
87 }
88 
89 /* ARGSUSED */
90 int
p_passwd(char * p,struct passwd * pw,ENTRY * ep)91 p_passwd(char *p, struct passwd *pw, ENTRY *ep)
92 {
93 	if (!*p)
94 		pw->pw_passwd = "";	/* "NOLOGIN"; */
95 	else if (!(pw->pw_passwd = strdup(p))) {
96 		warnx("can't save password entry");
97 		return (1);
98 	}
99 
100 	return (0);
101 }
102 
103 /* ARGSUSED */
104 int
p_uid(char * p,struct passwd * pw,ENTRY * ep)105 p_uid(char *p, struct passwd *pw, ENTRY *ep)
106 {
107 	uid_t id;
108 	const char *errstr;
109 
110 	if (!*p) {
111 		warnx("empty uid field");
112 		return (1);
113 	}
114 	id = strtonum(p, 0, UID_MAX, &errstr);
115 	if (errstr) {
116 		warnx("uid is %s", errstr);
117 		return (1);
118 	}
119 	pw->pw_uid = id;
120 	return (0);
121 }
122 
123 /* ARGSUSED */
124 int
p_gid(char * p,struct passwd * pw,ENTRY * ep)125 p_gid(char *p, struct passwd *pw, ENTRY *ep)
126 {
127 	struct group *gr;
128 	const char *errstr;
129 	gid_t id;
130 
131 	if (!*p) {
132 		warnx("empty gid field");
133 		return (1);
134 	}
135 	if (!isdigit(*p)) {
136 		if (!(gr = getgrnam(p))) {
137 			warnx("unknown group %s", p);
138 			return (1);
139 		}
140 		pw->pw_gid = gr->gr_gid;
141 		return (0);
142 	}
143 	id = strtonum(p, 0, GID_MAX, &errstr);
144 	if (errstr) {
145 		warnx("gid is %s", errstr);
146 		return (1);
147 	}
148 	pw->pw_gid = id;
149 	return (0);
150 }
151 
152 /* ARGSUSED */
153 int
p_class(char * p,struct passwd * pw,ENTRY * ep)154 p_class(char *p, struct passwd *pw, ENTRY *ep)
155 {
156 	if (!*p)
157 		pw->pw_class = "";
158 	else if (!(pw->pw_class = strdup(p))) {
159 		warnx("can't save entry");
160 		return (1);
161 	}
162 
163 	return (0);
164 }
165 
166 /* ARGSUSED */
167 int
p_change(char * p,struct passwd * pw,ENTRY * ep)168 p_change(char *p, struct passwd *pw, ENTRY *ep)
169 {
170 	if (!atot(p, &pw->pw_change))
171 		return (0);
172 	warnx("illegal date for change field");
173 	return (1);
174 }
175 
176 /* ARGSUSED */
177 int
p_expire(char * p,struct passwd * pw,ENTRY * ep)178 p_expire(char *p, struct passwd *pw, ENTRY *ep)
179 {
180 	if (!atot(p, &pw->pw_expire))
181 		return (0);
182 	warnx("illegal date for expire field");
183 	return (1);
184 }
185 
186 /* ARGSUSED */
187 int
p_gecos(char * p,struct passwd * pw,ENTRY * ep)188 p_gecos(char *p, struct passwd *pw, ENTRY *ep)
189 {
190 	if (!*p)
191 		ep->save = "";
192 	else if (!(ep->save = strdup(p))) {
193 		warnx("can't save entry");
194 		return (1);
195 	}
196 	return (0);
197 }
198 
199 /* ARGSUSED */
200 int
p_hdir(char * p,struct passwd * pw,ENTRY * ep)201 p_hdir(char *p, struct passwd *pw, ENTRY *ep)
202 {
203 	if (!*p) {
204 		warnx("empty home directory field");
205 		return (1);
206 	}
207 	if (!(pw->pw_dir = strdup(p))) {
208 		warnx("can't save entry");
209 		return (1);
210 	}
211 	return (0);
212 }
213 
214 /* ARGSUSED */
215 int
p_shell(char * p,struct passwd * pw,ENTRY * ep)216 p_shell(char *p, struct passwd *pw, ENTRY *ep)
217 {
218 	char *t;
219 
220 	if (!*p) {
221 		pw->pw_shell = _PATH_BSHELL;
222 		return (0);
223 	}
224 	/* only admin can change from or to "restricted" shells */
225 	if (uid && pw->pw_shell && !ok_shell(pw->pw_shell)) {
226 		warnx("%s: current shell non-standard", pw->pw_shell);
227 		return (1);
228 	}
229 	if (!(t = ok_shell(p))) {
230 		if (uid) {
231 			warnx("%s: non-standard shell", p);
232 			return (1);
233 		}
234 	} else
235 		p = t;
236 	if (!(pw->pw_shell = strdup(p))) {
237 		warnx("can't save entry");
238 		return (1);
239 	}
240 	return (0);
241 }
242