xref: /freebsd/usr.bin/chpass/util.c (revision 5e3934b15a2741b2de6b217e77dc9d798d740804)
19b50d902SRodney W. Grimes /*-
2df57947fSPedro F. Giffuni  * SPDX-License-Identifier: BSD-4-Clause
3df57947fSPedro F. Giffuni  *
49b50d902SRodney W. Grimes  * Copyright (c) 1988, 1993, 1994
59b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
6f1d05925SDag-Erling Smørgrav  * Copyright (c) 2002 Networks Associates Technology, Inc.
7f1d05925SDag-Erling Smørgrav  * All rights reserved.
8f1d05925SDag-Erling Smørgrav  *
9f1d05925SDag-Erling Smørgrav  * Portions of this software were developed for the FreeBSD Project by
10f1d05925SDag-Erling Smørgrav  * ThinkSec AS and NAI Labs, the Security Research Division of Network
11f1d05925SDag-Erling Smørgrav  * Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
12f1d05925SDag-Erling Smørgrav  * ("CBOSS"), as part of the DARPA CHATS research program.
139b50d902SRodney W. Grimes  *
149b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
159b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
169b50d902SRodney W. Grimes  * are met:
179b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
189b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
199b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
209b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
219b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
229b50d902SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
239b50d902SRodney W. Grimes  *    must display the following acknowledgement:
249b50d902SRodney W. Grimes  *	This product includes software developed by the University of
259b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
269b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
279b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
289b50d902SRodney W. Grimes  *    without specific prior written permission.
299b50d902SRodney W. Grimes  *
309b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
319b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
329b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
339b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
349b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
359b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
369b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
379b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
389b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
399b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
409b50d902SRodney W. Grimes  * SUCH DAMAGE.
419b50d902SRodney W. Grimes  */
429b50d902SRodney W. Grimes 
439b50d902SRodney W. Grimes #include <sys/types.h>
449b50d902SRodney W. Grimes 
459b50d902SRodney W. Grimes #include <ctype.h>
46*ba85da07SEd Maste #include <stdint.h>
479b50d902SRodney W. Grimes #include <stdio.h>
489b50d902SRodney W. Grimes #include <stdlib.h>
499b50d902SRodney W. Grimes #include <string.h>
509b50d902SRodney W. Grimes #include <time.h>
519b50d902SRodney W. Grimes #include <unistd.h>
529b50d902SRodney W. Grimes 
539b50d902SRodney W. Grimes #include "chpass.h"
549b50d902SRodney W. Grimes 
555ea73378SMark Murray static const char *months[] =
569b50d902SRodney W. Grimes 	{ "January", "February", "March", "April", "May", "June",
579b50d902SRodney W. Grimes 	  "July", "August", "September", "October", "November",
589b50d902SRodney W. Grimes 	  "December", NULL };
599b50d902SRodney W. Grimes 
609b50d902SRodney W. Grimes char *
ttoa(time_t tval)615ea73378SMark Murray ttoa(time_t tval)
629b50d902SRodney W. Grimes {
639b50d902SRodney W. Grimes 	struct tm *tp;
649b50d902SRodney W. Grimes 	static char tbuf[50];
659b50d902SRodney W. Grimes 
669b50d902SRodney W. Grimes 	if (tval) {
679b50d902SRodney W. Grimes 		tp = localtime(&tval);
689b50d902SRodney W. Grimes 		(void)sprintf(tbuf, "%s %d, %d", months[tp->tm_mon],
69656dcd43SGarrett Wollman 		    tp->tm_mday, tp->tm_year + 1900);
709b50d902SRodney W. Grimes 	}
719b50d902SRodney W. Grimes 	else
729b50d902SRodney W. Grimes 		*tbuf = '\0';
739b50d902SRodney W. Grimes 	return (tbuf);
749b50d902SRodney W. Grimes }
759b50d902SRodney W. Grimes 
769b50d902SRodney W. Grimes int
atot(char * p,time_t * store)775ea73378SMark Murray atot(char *p, time_t *store)
789b50d902SRodney W. Grimes {
799b50d902SRodney W. Grimes 	static struct tm *lt;
805ea73378SMark Murray 	char *t;
815ea73378SMark Murray 	const char **mp;
829b50d902SRodney W. Grimes 	time_t tval;
839b50d902SRodney W. Grimes 	int day, month, year;
849b50d902SRodney W. Grimes 
859b50d902SRodney W. Grimes 	if (!*p) {
869b50d902SRodney W. Grimes 		*store = 0;
879b50d902SRodney W. Grimes 		return (0);
889b50d902SRodney W. Grimes 	}
899b50d902SRodney W. Grimes 	if (!lt) {
909b50d902SRodney W. Grimes 		unsetenv("TZ");
919b50d902SRodney W. Grimes 		(void)time(&tval);
929b50d902SRodney W. Grimes 		lt = localtime(&tval);
939b50d902SRodney W. Grimes 	}
949b50d902SRodney W. Grimes 	if (!(t = strtok(p, " \t")))
959b50d902SRodney W. Grimes 		goto bad;
96366982a5SPeter Wemm 	if (isdigit(*t)) {
97366982a5SPeter Wemm 		month = atoi(t);
98366982a5SPeter Wemm 	} else {
999b50d902SRodney W. Grimes 		for (mp = months;; ++mp) {
1009b50d902SRodney W. Grimes 			if (!*mp)
1019b50d902SRodney W. Grimes 				goto bad;
1029b50d902SRodney W. Grimes 			if (!strncasecmp(*mp, t, 3)) {
1039b50d902SRodney W. Grimes 				month = mp - months + 1;
1049b50d902SRodney W. Grimes 				break;
1059b50d902SRodney W. Grimes 			}
1069b50d902SRodney W. Grimes 		}
107366982a5SPeter Wemm 	}
10801bd93ceSKevin Lo 	if (!(t = strtok(NULL, " \t,")) || !isdigit(*t))
1099b50d902SRodney W. Grimes 		goto bad;
1109b50d902SRodney W. Grimes 	day = atoi(t);
11101bd93ceSKevin Lo 	if (!(t = strtok(NULL, " \t,")) || !isdigit(*t))
1129b50d902SRodney W. Grimes 		goto bad;
1139b50d902SRodney W. Grimes 	year = atoi(t);
11492b19a57SCrist J. Clark 	if (day < 1 || day > 31 || month < 1 || month > 12)
1159b50d902SRodney W. Grimes 		goto bad;
116ea5cc207SDaniel O'Callaghan 	/* Allow two digit years 1969-2068 */
117ea5cc207SDaniel O'Callaghan 	if (year < 69)
118ea5cc207SDaniel O'Callaghan 		year += 2000;
119ea5cc207SDaniel O'Callaghan 	else if (year < 100)
120656dcd43SGarrett Wollman 		year += 1900;
121ea5cc207SDaniel O'Callaghan 	if (year < 1969)
1229b50d902SRodney W. Grimes bad:		return (1);
123656dcd43SGarrett Wollman 	lt->tm_year = year - 1900;
12455cc9917SMike Pritchard 	lt->tm_mon = month - 1;
12555cc9917SMike Pritchard 	lt->tm_mday = day;
12655cc9917SMike Pritchard 	lt->tm_hour = 0;
12755cc9917SMike Pritchard 	lt->tm_min = 0;
12855cc9917SMike Pritchard 	lt->tm_sec = 0;
12955cc9917SMike Pritchard 	lt->tm_isdst = -1;
13055cc9917SMike Pritchard 	if ((tval = mktime(lt)) < 0)
13155cc9917SMike Pritchard 		return (1);
132*ba85da07SEd Maste #ifndef __i386__
133*ba85da07SEd Maste 	/*
134*ba85da07SEd Maste 	 * PR227589: The pwd.db and spwd.db files store the change and expire
135*ba85da07SEd Maste 	 * dates as unsigned 32-bit ints which overflow in 2106, so larger
136*ba85da07SEd Maste 	 * values must be rejected until the introduction of a v5 password
137*ba85da07SEd Maste 	 * database.  i386 has 32-bit time_t and so dates beyond y2038 are
138*ba85da07SEd Maste 	 * already rejected by mktime above.
139*ba85da07SEd Maste 	 */
140*ba85da07SEd Maste 	if (tval > UINT32_MAX)
141*ba85da07SEd Maste 		return (1);
142*ba85da07SEd Maste #endif
1439b50d902SRodney W. Grimes 	*store = tval;
1449b50d902SRodney W. Grimes 	return (0);
1459b50d902SRodney W. Grimes }
1469b50d902SRodney W. Grimes 
147612956f6SPhilippe Charnier int
ok_shell(char * name)1485ea73378SMark Murray ok_shell(char *name)
1499b50d902SRodney W. Grimes {
1509b50d902SRodney W. Grimes 	char *p, *sh;
1519b50d902SRodney W. Grimes 
1529b50d902SRodney W. Grimes 	setusershell();
153522571b3SPhilippe Charnier 	while ((sh = getusershell())) {
154612956f6SPhilippe Charnier 		if (!strcmp(name, sh)) {
155612956f6SPhilippe Charnier 			endusershell();
156612956f6SPhilippe Charnier 			return (1);
1579b50d902SRodney W. Grimes 		}
158612956f6SPhilippe Charnier 		/* allow just shell name, but use "real" path */
159612956f6SPhilippe Charnier 		if ((p = strrchr(sh, '/')) && strcmp(name, p + 1) == 0) {
160612956f6SPhilippe Charnier 			endusershell();
161612956f6SPhilippe Charnier 			return (1);
162612956f6SPhilippe Charnier 		}
163612956f6SPhilippe Charnier 	}
164612956f6SPhilippe Charnier 	endusershell();
165612956f6SPhilippe Charnier 	return (0);
166612956f6SPhilippe Charnier }
167612956f6SPhilippe Charnier 
168612956f6SPhilippe Charnier char *
dup_shell(char * name)169612956f6SPhilippe Charnier dup_shell(char *name)
170612956f6SPhilippe Charnier {
171612956f6SPhilippe Charnier 	char *p, *sh, *ret;
172612956f6SPhilippe Charnier 
173612956f6SPhilippe Charnier 	setusershell();
174612956f6SPhilippe Charnier 	while ((sh = getusershell())) {
175612956f6SPhilippe Charnier 		if (!strcmp(name, sh)) {
176612956f6SPhilippe Charnier 			endusershell();
177612956f6SPhilippe Charnier 			return (strdup(name));
178612956f6SPhilippe Charnier 		}
179612956f6SPhilippe Charnier 		/* allow just shell name, but use "real" path */
180612956f6SPhilippe Charnier 		if ((p = strrchr(sh, '/')) && strcmp(name, p + 1) == 0) {
181612956f6SPhilippe Charnier 			ret = strdup(sh);
182612956f6SPhilippe Charnier 			endusershell();
183612956f6SPhilippe Charnier 			return (ret);
184612956f6SPhilippe Charnier 		}
185612956f6SPhilippe Charnier 	}
186612956f6SPhilippe Charnier 	endusershell();
1879b50d902SRodney W. Grimes 	return (NULL);
1889b50d902SRodney W. Grimes }
189