11590Srgrimes/*-
21590Srgrimes * Copyright (c) 1988, 1993, 1994
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
496201Sdes * Copyright (c) 2002 Networks Associates Technology, Inc.
596201Sdes * All rights reserved.
61590Srgrimes *
796201Sdes * Portions of this software were developed for the FreeBSD Project by
896201Sdes * ThinkSec AS and NAI Labs, the Security Research Division of Network
996201Sdes * Associates, Inc.  under DARPA/SPAWAR contract N66001-01-C-8035
1096201Sdes * ("CBOSS"), as part of the DARPA CHATS research program.
1196201Sdes *
121590Srgrimes * Redistribution and use in source and binary forms, with or without
131590Srgrimes * modification, are permitted provided that the following conditions
141590Srgrimes * are met:
151590Srgrimes * 1. Redistributions of source code must retain the above copyright
161590Srgrimes *    notice, this list of conditions and the following disclaimer.
171590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
181590Srgrimes *    notice, this list of conditions and the following disclaimer in the
191590Srgrimes *    documentation and/or other materials provided with the distribution.
201590Srgrimes * 3. All advertising materials mentioning features or use of this software
211590Srgrimes *    must display the following acknowledgement:
221590Srgrimes *	This product includes software developed by the University of
231590Srgrimes *	California, Berkeley and its contributors.
241590Srgrimes * 4. Neither the name of the University nor the names of its contributors
251590Srgrimes *    may be used to endorse or promote products derived from this software
261590Srgrimes *    without specific prior written permission.
271590Srgrimes *
281590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
291590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
301590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
311590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
321590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
331590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
341590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
351590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
361590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
371590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
381590Srgrimes * SUCH DAMAGE.
391590Srgrimes */
401590Srgrimes
411590Srgrimes#ifndef lint
4299112Sobrien#if 0
4399112Sobrienstatic char sccsid[] = "@(#)util.c	8.4 (Berkeley) 4/2/94";
4499112Sobrien#endif
451590Srgrimes#endif /* not lint */
4693086Smarkm#include <sys/cdefs.h>
4793086Smarkm__FBSDID("$FreeBSD$");
4893086Smarkm
491590Srgrimes#include <sys/types.h>
501590Srgrimes
511590Srgrimes#include <ctype.h>
521590Srgrimes#include <stdio.h>
531590Srgrimes#include <stdlib.h>
541590Srgrimes#include <string.h>
551590Srgrimes#include <time.h>
561590Srgrimes#include <unistd.h>
571590Srgrimes
581590Srgrimes#include "chpass.h"
591590Srgrimes
6093086Smarkmstatic const char *months[] =
611590Srgrimes	{ "January", "February", "March", "April", "May", "June",
621590Srgrimes	  "July", "August", "September", "October", "November",
631590Srgrimes	  "December", NULL };
641590Srgrimes
651590Srgrimeschar *
6693086Smarkmttoa(time_t tval)
671590Srgrimes{
681590Srgrimes	struct tm *tp;
691590Srgrimes	static char tbuf[50];
701590Srgrimes
711590Srgrimes	if (tval) {
721590Srgrimes		tp = localtime(&tval);
731590Srgrimes		(void)sprintf(tbuf, "%s %d, %d", months[tp->tm_mon],
749987Swollman		    tp->tm_mday, tp->tm_year + 1900);
751590Srgrimes	}
761590Srgrimes	else
771590Srgrimes		*tbuf = '\0';
781590Srgrimes	return (tbuf);
798874Srgrimes}
801590Srgrimes
811590Srgrimesint
8293086Smarkmatot(char *p, time_t *store)
831590Srgrimes{
841590Srgrimes	static struct tm *lt;
8593086Smarkm	char *t;
8693086Smarkm	const char **mp;
871590Srgrimes	time_t tval;
881590Srgrimes	int day, month, year;
891590Srgrimes
901590Srgrimes	if (!*p) {
911590Srgrimes		*store = 0;
921590Srgrimes		return (0);
931590Srgrimes	}
941590Srgrimes	if (!lt) {
951590Srgrimes		unsetenv("TZ");
961590Srgrimes		(void)time(&tval);
971590Srgrimes		lt = localtime(&tval);
981590Srgrimes	}
991590Srgrimes	if (!(t = strtok(p, " \t")))
1001590Srgrimes		goto bad;
10117544Speter	if (isdigit(*t)) {
10217544Speter		month = atoi(t);
10317544Speter	} else {
10417544Speter		for (mp = months;; ++mp) {
10517544Speter			if (!*mp)
10617544Speter				goto bad;
10717544Speter			if (!strncasecmp(*mp, t, 3)) {
10817544Speter				month = mp - months + 1;
10917544Speter				break;
11017544Speter			}
1111590Srgrimes		}
1121590Srgrimes	}
1131590Srgrimes	if (!(t = strtok((char *)NULL, " \t,")) || !isdigit(*t))
1141590Srgrimes		goto bad;
1151590Srgrimes	day = atoi(t);
1161590Srgrimes	if (!(t = strtok((char *)NULL, " \t,")) || !isdigit(*t))
1171590Srgrimes		goto bad;
1181590Srgrimes	year = atoi(t);
11992554Scjc	if (day < 1 || day > 31 || month < 1 || month > 12)
1201590Srgrimes		goto bad;
12142815Sdanny	/* Allow two digit years 1969-2068 */
12242815Sdanny	if (year < 69)
12342815Sdanny		year += 2000;
12442815Sdanny	else if (year < 100)
1259987Swollman		year += 1900;
12642815Sdanny	if (year < 1969)
1271590Srgrimesbad:		return (1);
1289987Swollman	lt->tm_year = year - 1900;
1299554Smpp	lt->tm_mon = month - 1;
1309554Smpp	lt->tm_mday = day;
1319554Smpp	lt->tm_hour = 0;
1329554Smpp	lt->tm_min = 0;
1339554Smpp	lt->tm_sec = 0;
1349554Smpp	lt->tm_isdst = -1;
1359554Smpp	if ((tval = mktime(lt)) < 0)
1369554Smpp		return (1);
1371590Srgrimes	*store = tval;
1381590Srgrimes	return (0);
1391590Srgrimes}
1401590Srgrimes
141124692Scharnierint
14293086Smarkmok_shell(char *name)
1431590Srgrimes{
1441590Srgrimes	char *p, *sh;
1451590Srgrimes
1461590Srgrimes	setusershell();
14734797Scharnier	while ((sh = getusershell())) {
148124692Scharnier		if (!strcmp(name, sh)) {
149124692Scharnier			endusershell();
150124692Scharnier			return (1);
151124692Scharnier		}
1521590Srgrimes		/* allow just shell name, but use "real" path */
153124692Scharnier		if ((p = strrchr(sh, '/')) && strcmp(name, p + 1) == 0) {
154124692Scharnier			endusershell();
155124692Scharnier			return (1);
156124692Scharnier		}
1571590Srgrimes	}
158124692Scharnier	endusershell();
159124692Scharnier	return (0);
160124692Scharnier}
161124692Scharnier
162124692Scharnierchar *
163124692Scharnierdup_shell(char *name)
164124692Scharnier{
165124692Scharnier	char *p, *sh, *ret;
166124692Scharnier
167124692Scharnier	setusershell();
168124692Scharnier	while ((sh = getusershell())) {
169124692Scharnier		if (!strcmp(name, sh)) {
170124692Scharnier			endusershell();
171124692Scharnier			return (strdup(name));
172124692Scharnier		}
173124692Scharnier		/* allow just shell name, but use "real" path */
174124692Scharnier		if ((p = strrchr(sh, '/')) && strcmp(name, p + 1) == 0) {
175124692Scharnier			ret = strdup(sh);
176124692Scharnier			endusershell();
177124692Scharnier			return (ret);
178124692Scharnier		}
179124692Scharnier	}
180124692Scharnier	endusershell();
1811590Srgrimes	return (NULL);
1821590Srgrimes}
183