xref: /freebsd/usr.bin/chpass/field.c (revision d33c4953e76ad3c07c4d25c2c1b238fcacedf851)
19b50d902SRodney W. Grimes /*
29b50d902SRodney W. Grimes  * Copyright (c) 1988, 1993, 1994
39b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
49b50d902SRodney W. Grimes  *
59b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
69b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
79b50d902SRodney W. Grimes  * are met:
89b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
99b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
109b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
129b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
139b50d902SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
149b50d902SRodney W. Grimes  *    must display the following acknowledgement:
159b50d902SRodney W. Grimes  *	This product includes software developed by the University of
169b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
179b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
189b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
199b50d902SRodney W. Grimes  *    without specific prior written permission.
209b50d902SRodney W. Grimes  *
219b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
229b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
239b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
249b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
259b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
269b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
279b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
289b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
299b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
309b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
319b50d902SRodney W. Grimes  * SUCH DAMAGE.
329b50d902SRodney W. Grimes  */
339b50d902SRodney W. Grimes 
349b50d902SRodney W. Grimes #ifndef lint
359b50d902SRodney W. Grimes static char sccsid[] = "@(#)field.c	8.4 (Berkeley) 4/2/94";
369b50d902SRodney W. Grimes #endif /* not lint */
379b50d902SRodney W. Grimes 
389b50d902SRodney W. Grimes #include <sys/param.h>
39d33c4953SMike Pritchard #include <sys/stat.h>
409b50d902SRodney W. Grimes 
419b50d902SRodney W. Grimes #include <ctype.h>
429b50d902SRodney W. Grimes #include <err.h>
439b50d902SRodney W. Grimes #include <errno.h>
449b50d902SRodney W. Grimes #include <grp.h>
459b50d902SRodney W. Grimes #include <pwd.h>
469b50d902SRodney W. Grimes #include <stdio.h>
479b50d902SRodney W. Grimes #include <stdlib.h>
489b50d902SRodney W. Grimes #include <string.h>
499b50d902SRodney W. Grimes #include <unistd.h>
509b50d902SRodney W. Grimes 
519b50d902SRodney W. Grimes #include "chpass.h"
529b50d902SRodney W. Grimes #include "pathnames.h"
539b50d902SRodney W. Grimes 
549b50d902SRodney W. Grimes /* ARGSUSED */
559b50d902SRodney W. Grimes int
569b50d902SRodney W. Grimes p_login(p, pw, ep)
579b50d902SRodney W. Grimes 	char *p;
589b50d902SRodney W. Grimes 	struct passwd *pw;
599b50d902SRodney W. Grimes 	ENTRY *ep;
609b50d902SRodney W. Grimes {
619b50d902SRodney W. Grimes 	if (!*p) {
629b50d902SRodney W. Grimes 		warnx("empty login field");
639b50d902SRodney W. Grimes 		return (1);
649b50d902SRodney W. Grimes 	}
659b50d902SRodney W. Grimes 	if (*p == '-') {
669b50d902SRodney W. Grimes 		warnx("login names may not begin with a hyphen");
679b50d902SRodney W. Grimes 		return (1);
689b50d902SRodney W. Grimes 	}
699b50d902SRodney W. Grimes 	if (!(pw->pw_name = strdup(p))) {
709b50d902SRodney W. Grimes 		warnx("can't save entry");
719b50d902SRodney W. Grimes 		return (1);
729b50d902SRodney W. Grimes 	}
739b50d902SRodney W. Grimes 	if (strchr(p, '.'))
749b50d902SRodney W. Grimes 		warnx("\'.\' is dangerous in a login name");
759b50d902SRodney W. Grimes 	for (; *p; ++p)
769b50d902SRodney W. Grimes 		if (isupper(*p)) {
779b50d902SRodney W. Grimes 			warnx("upper-case letters are dangerous in a login name");
789b50d902SRodney W. Grimes 			break;
799b50d902SRodney W. Grimes 		}
809b50d902SRodney W. Grimes 	return (0);
819b50d902SRodney W. Grimes }
829b50d902SRodney W. Grimes 
839b50d902SRodney W. Grimes /* ARGSUSED */
849b50d902SRodney W. Grimes int
859b50d902SRodney W. Grimes p_passwd(p, pw, ep)
869b50d902SRodney W. Grimes 	char *p;
879b50d902SRodney W. Grimes 	struct passwd *pw;
889b50d902SRodney W. Grimes 	ENTRY *ep;
899b50d902SRodney W. Grimes {
909b50d902SRodney W. Grimes 	if (!*p)
919b50d902SRodney W. Grimes 		pw->pw_passwd = "";	/* "NOLOGIN"; */
929b50d902SRodney W. Grimes 	else if (!(pw->pw_passwd = strdup(p))) {
939b50d902SRodney W. Grimes 		warnx("can't save password entry");
949b50d902SRodney W. Grimes 		return (1);
959b50d902SRodney W. Grimes 	}
969b50d902SRodney W. Grimes 
979b50d902SRodney W. Grimes 	return (0);
989b50d902SRodney W. Grimes }
999b50d902SRodney W. Grimes 
1009b50d902SRodney W. Grimes /* ARGSUSED */
1019b50d902SRodney W. Grimes int
1029b50d902SRodney W. Grimes p_uid(p, pw, ep)
1039b50d902SRodney W. Grimes 	char *p;
1049b50d902SRodney W. Grimes 	struct passwd *pw;
1059b50d902SRodney W. Grimes 	ENTRY *ep;
1069b50d902SRodney W. Grimes {
1079b50d902SRodney W. Grimes 	uid_t id;
1089b50d902SRodney W. Grimes 	char *np;
1099b50d902SRodney W. Grimes 
1109b50d902SRodney W. Grimes 	if (!*p) {
1119b50d902SRodney W. Grimes 		warnx("empty uid field");
1129b50d902SRodney W. Grimes 		return (1);
1139b50d902SRodney W. Grimes 	}
1149b50d902SRodney W. Grimes 	if (!isdigit(*p)) {
1159b50d902SRodney W. Grimes 		warnx("illegal uid");
1169b50d902SRodney W. Grimes 		return (1);
1179b50d902SRodney W. Grimes 	}
1189b50d902SRodney W. Grimes 	errno = 0;
1199b50d902SRodney W. Grimes 	id = strtoul(p, &np, 10);
1209b50d902SRodney W. Grimes 	if (*np || (id == ULONG_MAX && errno == ERANGE)) {
1219b50d902SRodney W. Grimes 		warnx("illegal uid");
1229b50d902SRodney W. Grimes 		return (1);
1239b50d902SRodney W. Grimes 	}
1249b50d902SRodney W. Grimes 	pw->pw_uid = id;
1259b50d902SRodney W. Grimes 	return (0);
1269b50d902SRodney W. Grimes }
1279b50d902SRodney W. Grimes 
1289b50d902SRodney W. Grimes /* ARGSUSED */
1299b50d902SRodney W. Grimes int
1309b50d902SRodney W. Grimes p_gid(p, pw, ep)
1319b50d902SRodney W. Grimes 	char *p;
1329b50d902SRodney W. Grimes 	struct passwd *pw;
1339b50d902SRodney W. Grimes 	ENTRY *ep;
1349b50d902SRodney W. Grimes {
1359b50d902SRodney W. Grimes 	struct group *gr;
1369b50d902SRodney W. Grimes 	gid_t id;
1379b50d902SRodney W. Grimes 	char *np;
1389b50d902SRodney W. Grimes 
1399b50d902SRodney W. Grimes 	if (!*p) {
1409b50d902SRodney W. Grimes 		warnx("empty gid field");
1419b50d902SRodney W. Grimes 		return (1);
1429b50d902SRodney W. Grimes 	}
1439b50d902SRodney W. Grimes 	if (!isdigit(*p)) {
1449b50d902SRodney W. Grimes 		if (!(gr = getgrnam(p))) {
1459b50d902SRodney W. Grimes 			warnx("unknown group %s", p);
1469b50d902SRodney W. Grimes 			return (1);
1479b50d902SRodney W. Grimes 		}
1489b50d902SRodney W. Grimes 		pw->pw_gid = gr->gr_gid;
1499b50d902SRodney W. Grimes 		return (0);
1509b50d902SRodney W. Grimes 	}
1519b50d902SRodney W. Grimes 	errno = 0;
1529b50d902SRodney W. Grimes 	id = strtoul(p, &np, 10);
1539b50d902SRodney W. Grimes 	if (*np || (id == ULONG_MAX && errno == ERANGE)) {
1549b50d902SRodney W. Grimes 		warnx("illegal gid");
1559b50d902SRodney W. Grimes 		return (1);
1569b50d902SRodney W. Grimes 	}
1579b50d902SRodney W. Grimes 	pw->pw_gid = id;
1589b50d902SRodney W. Grimes 	return (0);
1599b50d902SRodney W. Grimes }
1609b50d902SRodney W. Grimes 
1619b50d902SRodney W. Grimes /* ARGSUSED */
1629b50d902SRodney W. Grimes int
1639b50d902SRodney W. Grimes p_class(p, pw, ep)
1649b50d902SRodney W. Grimes 	char *p;
1659b50d902SRodney W. Grimes 	struct passwd *pw;
1669b50d902SRodney W. Grimes 	ENTRY *ep;
1679b50d902SRodney W. Grimes {
1689b50d902SRodney W. Grimes 	if (!*p)
1699b50d902SRodney W. Grimes 		pw->pw_class = "";
1709b50d902SRodney W. Grimes 	else if (!(pw->pw_class = strdup(p))) {
1719b50d902SRodney W. Grimes 		warnx("can't save entry");
1729b50d902SRodney W. Grimes 		return (1);
1739b50d902SRodney W. Grimes 	}
1749b50d902SRodney W. Grimes 
1759b50d902SRodney W. Grimes 	return (0);
1769b50d902SRodney W. Grimes }
1779b50d902SRodney W. Grimes 
1789b50d902SRodney W. Grimes /* ARGSUSED */
1799b50d902SRodney W. Grimes int
1809b50d902SRodney W. Grimes p_change(p, pw, ep)
1819b50d902SRodney W. Grimes 	char *p;
1829b50d902SRodney W. Grimes 	struct passwd *pw;
1839b50d902SRodney W. Grimes 	ENTRY *ep;
1849b50d902SRodney W. Grimes {
1859b50d902SRodney W. Grimes 	if (!atot(p, &pw->pw_change))
1869b50d902SRodney W. Grimes 		return (0);
1879b50d902SRodney W. Grimes 	warnx("illegal date for change field");
1889b50d902SRodney W. Grimes 	return (1);
1899b50d902SRodney W. Grimes }
1909b50d902SRodney W. Grimes 
1919b50d902SRodney W. Grimes /* ARGSUSED */
1929b50d902SRodney W. Grimes int
1939b50d902SRodney W. Grimes p_expire(p, pw, ep)
1949b50d902SRodney W. Grimes 	char *p;
1959b50d902SRodney W. Grimes 	struct passwd *pw;
1969b50d902SRodney W. Grimes 	ENTRY *ep;
1979b50d902SRodney W. Grimes {
1989b50d902SRodney W. Grimes 	if (!atot(p, &pw->pw_expire))
1999b50d902SRodney W. Grimes 		return (0);
2009b50d902SRodney W. Grimes 	warnx("illegal date for expire field");
2019b50d902SRodney W. Grimes 	return (1);
2029b50d902SRodney W. Grimes }
2039b50d902SRodney W. Grimes 
2049b50d902SRodney W. Grimes /* ARGSUSED */
2059b50d902SRodney W. Grimes int
2069b50d902SRodney W. Grimes p_gecos(p, pw, ep)
2079b50d902SRodney W. Grimes 	char *p;
2089b50d902SRodney W. Grimes 	struct passwd *pw;
2099b50d902SRodney W. Grimes 	ENTRY *ep;
2109b50d902SRodney W. Grimes {
2119b50d902SRodney W. Grimes 	if (!*p)
2129b50d902SRodney W. Grimes 		ep->save = "";
2139b50d902SRodney W. Grimes 	else if (!(ep->save = strdup(p))) {
2149b50d902SRodney W. Grimes 		warnx("can't save entry");
2159b50d902SRodney W. Grimes 		return (1);
2169b50d902SRodney W. Grimes 	}
2179b50d902SRodney W. Grimes 	return (0);
2189b50d902SRodney W. Grimes }
2199b50d902SRodney W. Grimes 
2209b50d902SRodney W. Grimes /* ARGSUSED */
2219b50d902SRodney W. Grimes int
2229b50d902SRodney W. Grimes p_hdir(p, pw, ep)
2239b50d902SRodney W. Grimes 	char *p;
2249b50d902SRodney W. Grimes 	struct passwd *pw;
2259b50d902SRodney W. Grimes 	ENTRY *ep;
2269b50d902SRodney W. Grimes {
2279b50d902SRodney W. Grimes 	if (!*p) {
2289b50d902SRodney W. Grimes 		warnx("empty home directory field");
2299b50d902SRodney W. Grimes 		return (1);
2309b50d902SRodney W. Grimes 	}
2319b50d902SRodney W. Grimes 	if (!(pw->pw_dir = strdup(p))) {
2329b50d902SRodney W. Grimes 		warnx("can't save entry");
2339b50d902SRodney W. Grimes 		return (1);
2349b50d902SRodney W. Grimes 	}
2359b50d902SRodney W. Grimes 	return (0);
2369b50d902SRodney W. Grimes }
2379b50d902SRodney W. Grimes 
2389b50d902SRodney W. Grimes /* ARGSUSED */
2399b50d902SRodney W. Grimes int
2409b50d902SRodney W. Grimes p_shell(p, pw, ep)
2419b50d902SRodney W. Grimes 	char *p;
2429b50d902SRodney W. Grimes 	struct passwd *pw;
2439b50d902SRodney W. Grimes 	ENTRY *ep;
2449b50d902SRodney W. Grimes {
2459b50d902SRodney W. Grimes 	char *t, *ok_shell();
246d33c4953SMike Pritchard 	struct stat sbuf;
2479b50d902SRodney W. Grimes 
2489b50d902SRodney W. Grimes 	if (!*p) {
2499b50d902SRodney W. Grimes 		pw->pw_shell = _PATH_BSHELL;
2509b50d902SRodney W. Grimes 		return (0);
2519b50d902SRodney W. Grimes 	}
2529b50d902SRodney W. Grimes 	/* only admin can change from or to "restricted" shells */
2539b50d902SRodney W. Grimes 	if (uid && pw->pw_shell && !ok_shell(pw->pw_shell)) {
2549b50d902SRodney W. Grimes 		warnx("%s: current shell non-standard", pw->pw_shell);
2559b50d902SRodney W. Grimes 		return (1);
2569b50d902SRodney W. Grimes 	}
2579b50d902SRodney W. Grimes 	if (!(t = ok_shell(p))) {
2589b50d902SRodney W. Grimes 		if (uid) {
2599b50d902SRodney W. Grimes 			warnx("%s: non-standard shell", p);
2609b50d902SRodney W. Grimes 			return (1);
2619b50d902SRodney W. Grimes 		}
2629b50d902SRodney W. Grimes 	}
2639b50d902SRodney W. Grimes 	else
2649b50d902SRodney W. Grimes 		p = t;
2659b50d902SRodney W. Grimes 	if (!(pw->pw_shell = strdup(p))) {
2669b50d902SRodney W. Grimes 		warnx("can't save entry");
2679b50d902SRodney W. Grimes 		return (1);
2689b50d902SRodney W. Grimes 	}
269d33c4953SMike Pritchard 	if (stat(pw->pw_shell, &sbuf) < 0) {
270d33c4953SMike Pritchard 		if (errno == ENOENT)
271d33c4953SMike Pritchard 			warnx("WARNING: shell '%s' does not exist",
272d33c4953SMike Pritchard 			    pw->pw_shell);
273d33c4953SMike Pritchard 		else
274d33c4953SMike Pritchard 			warn("WARNING: can't stat shell '%s'",  pw->pw_shell);
275d33c4953SMike Pritchard 		return (0);
276d33c4953SMike Pritchard 	}
277d33c4953SMike Pritchard 	if (!S_ISREG(sbuf.st_mode)) {
278d33c4953SMike Pritchard 		warnx("WARNING: shell '%s' is not a regular file",
279d33c4953SMike Pritchard 			pw->pw_shell);
280d33c4953SMike Pritchard 		return (0);
281d33c4953SMike Pritchard 	}
282d33c4953SMike Pritchard 	if ((sbuf.st_mode & (S_IXOTH | S_IXGRP | S_IXUSR)) == 0) {
283d33c4953SMike Pritchard 		warnx("WARNING: shell '%s' is not executable", pw->pw_shell);
284d33c4953SMike Pritchard 		return (0);
285d33c4953SMike Pritchard 	}
2869b50d902SRodney W. Grimes 	return (0);
2879b50d902SRodney W. Grimes }
288