xref: /openbsd-src/usr.bin/chpass/field.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: field.c,v 1.10 2009/03/05 20:53:13 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.10 2009/03/05 20:53:13 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
58 p_login(char *p, struct passwd *pw, ENTRY *ep)
59 {
60 	if (!*p) {
61 		warnx("empty login field");
62 		return (1);
63 	}
64 	if (*p == '-') {
65 		warnx("login names may not begin with a hyphen");
66 		return (1);
67 	}
68 	/* XXX - what about truncated names? */
69 	if (strcmp(pw->pw_name, p) != 0 && getpwnam(p) != NULL) {
70 		warnx("login %s already exists", p);
71 		return (1);
72 	}
73 	if (!(pw->pw_name = strdup(p))) {
74 		warnx("can't save entry");
75 		return (1);
76 	}
77 	if (strchr(p, '.'))
78 		warnx("\'.\' is dangerous in a login name");
79 	for (; *p; ++p)
80 		if (isupper(*p)) {
81 			warnx("upper-case letters are dangerous in a login name");
82 			break;
83 		}
84 	return (0);
85 }
86 
87 /* ARGSUSED */
88 int
89 p_passwd(char *p, struct passwd *pw, ENTRY *ep)
90 {
91 	if (!*p)
92 		pw->pw_passwd = "";	/* "NOLOGIN"; */
93 	else if (!(pw->pw_passwd = strdup(p))) {
94 		warnx("can't save password entry");
95 		return (1);
96 	}
97 
98 	return (0);
99 }
100 
101 /* ARGSUSED */
102 int
103 p_uid(char *p, struct passwd *pw, ENTRY *ep)
104 {
105 	uid_t id;
106 	const char *errstr;
107 
108 	if (!*p) {
109 		warnx("empty uid field");
110 		return (1);
111 	}
112 	id = (uid_t)strtonum(p, 0, UID_MAX, &errstr);
113 	if (errstr) {
114 		warnx("uid is %s", errstr);
115 		return (1);
116 	}
117 	pw->pw_uid = id;
118 	return (0);
119 }
120 
121 /* ARGSUSED */
122 int
123 p_gid(char *p, struct passwd *pw, ENTRY *ep)
124 {
125 	struct group *gr;
126 	const char *errstr;
127 	gid_t id;
128 
129 	if (!*p) {
130 		warnx("empty gid field");
131 		return (1);
132 	}
133 	if (!isdigit(*p)) {
134 		if (!(gr = getgrnam(p))) {
135 			warnx("unknown group %s", p);
136 			return (1);
137 		}
138 		pw->pw_gid = gr->gr_gid;
139 		return (0);
140 	}
141 	id = (uid_t)strtonum(p, 0, GID_MAX, &errstr);
142 	if (errstr) {
143 		warnx("gid is %s", errstr);
144 		return (1);
145 	}
146 	pw->pw_gid = id;
147 	return (0);
148 }
149 
150 /* ARGSUSED */
151 int
152 p_class(char *p, struct passwd *pw, ENTRY *ep)
153 {
154 	if (!*p)
155 		pw->pw_class = "";
156 	else if (!(pw->pw_class = strdup(p))) {
157 		warnx("can't save entry");
158 		return (1);
159 	}
160 
161 	return (0);
162 }
163 
164 /* ARGSUSED */
165 int
166 p_change(char *p, struct passwd *pw, ENTRY *ep)
167 {
168 	if (!atot(p, &pw->pw_change))
169 		return (0);
170 	warnx("illegal date for change field");
171 	return (1);
172 }
173 
174 /* ARGSUSED */
175 int
176 p_expire(char *p, struct passwd *pw, ENTRY *ep)
177 {
178 	if (!atot(p, &pw->pw_expire))
179 		return (0);
180 	warnx("illegal date for expire field");
181 	return (1);
182 }
183 
184 /* ARGSUSED */
185 int
186 p_gecos(char *p, struct passwd *pw, ENTRY *ep)
187 {
188 	if (!*p)
189 		ep->save = "";
190 	else if (!(ep->save = strdup(p))) {
191 		warnx("can't save entry");
192 		return (1);
193 	}
194 	return (0);
195 }
196 
197 /* ARGSUSED */
198 int
199 p_hdir(char *p, struct passwd *pw, ENTRY *ep)
200 {
201 	if (!*p) {
202 		warnx("empty home directory field");
203 		return (1);
204 	}
205 	if (!(pw->pw_dir = strdup(p))) {
206 		warnx("can't save entry");
207 		return (1);
208 	}
209 	return (0);
210 }
211 
212 /* ARGSUSED */
213 int
214 p_shell(char *p, struct passwd *pw, ENTRY *ep)
215 {
216 	char *t;
217 
218 	if (!*p) {
219 		pw->pw_shell = _PATH_BSHELL;
220 		return (0);
221 	}
222 	/* only admin can change from or to "restricted" shells */
223 	if (uid && pw->pw_shell && !ok_shell(pw->pw_shell, NULL)) {
224 		warnx("%s: current shell non-standard", pw->pw_shell);
225 		return (1);
226 	}
227 	if (!ok_shell(p, &t)) {
228 		if (uid) {
229 			warnx("%s: non-standard shell", p);
230 			return (1);
231 		}
232 	}
233 	if (!(pw->pw_shell = t)) {
234 		warnx("can't save entry");
235 		return (1);
236 	}
237 	return (0);
238 }
239