]> git.cameronkatri.com Git - pw-darwin.git/blob - chpass/util.c
Merge sync of head
[pw-darwin.git] / chpass / util.c
1 /*-
2 * Copyright (c) 1988, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 2002 Networks Associates Technology, Inc.
5 * All rights reserved.
6 *
7 * Portions of this software were developed for the FreeBSD Project by
8 * ThinkSec AS and NAI Labs, the Security Research Division of Network
9 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035
10 * ("CBOSS"), as part of the DARPA CHATS research program.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 */
40
41 #ifndef lint
42 #if 0
43 static char sccsid[] = "@(#)util.c 8.4 (Berkeley) 4/2/94";
44 #endif
45 #endif /* not lint */
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
48
49 #include <sys/types.h>
50
51 #include <ctype.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <time.h>
56 #include <unistd.h>
57
58 #include "chpass.h"
59
60 static const char *months[] =
61 { "January", "February", "March", "April", "May", "June",
62 "July", "August", "September", "October", "November",
63 "December", NULL };
64
65 char *
66 ttoa(time_t tval)
67 {
68 struct tm *tp;
69 static char tbuf[50];
70
71 if (tval) {
72 tp = localtime(&tval);
73 (void)sprintf(tbuf, "%s %d, %d", months[tp->tm_mon],
74 tp->tm_mday, tp->tm_year + 1900);
75 }
76 else
77 *tbuf = '\0';
78 return (tbuf);
79 }
80
81 int
82 atot(char *p, time_t *store)
83 {
84 static struct tm *lt;
85 char *t;
86 const char **mp;
87 time_t tval;
88 int day, month, year;
89
90 if (!*p) {
91 *store = 0;
92 return (0);
93 }
94 if (!lt) {
95 unsetenv("TZ");
96 (void)time(&tval);
97 lt = localtime(&tval);
98 }
99 if (!(t = strtok(p, " \t")))
100 goto bad;
101 if (isdigit(*t)) {
102 month = atoi(t);
103 } else {
104 for (mp = months;; ++mp) {
105 if (!*mp)
106 goto bad;
107 if (!strncasecmp(*mp, t, 3)) {
108 month = mp - months + 1;
109 break;
110 }
111 }
112 }
113 if (!(t = strtok(NULL, " \t,")) || !isdigit(*t))
114 goto bad;
115 day = atoi(t);
116 if (!(t = strtok(NULL, " \t,")) || !isdigit(*t))
117 goto bad;
118 year = atoi(t);
119 if (day < 1 || day > 31 || month < 1 || month > 12)
120 goto bad;
121 /* Allow two digit years 1969-2068 */
122 if (year < 69)
123 year += 2000;
124 else if (year < 100)
125 year += 1900;
126 if (year < 1969)
127 bad: return (1);
128 lt->tm_year = year - 1900;
129 lt->tm_mon = month - 1;
130 lt->tm_mday = day;
131 lt->tm_hour = 0;
132 lt->tm_min = 0;
133 lt->tm_sec = 0;
134 lt->tm_isdst = -1;
135 if ((tval = mktime(lt)) < 0)
136 return (1);
137 *store = tval;
138 return (0);
139 }
140
141 int
142 ok_shell(char *name)
143 {
144 char *p, *sh;
145
146 setusershell();
147 while ((sh = getusershell())) {
148 if (!strcmp(name, sh)) {
149 endusershell();
150 return (1);
151 }
152 /* allow just shell name, but use "real" path */
153 if ((p = strrchr(sh, '/')) && strcmp(name, p + 1) == 0) {
154 endusershell();
155 return (1);
156 }
157 }
158 endusershell();
159 return (0);
160 }
161
162 char *
163 dup_shell(char *name)
164 {
165 char *p, *sh, *ret;
166
167 setusershell();
168 while ((sh = getusershell())) {
169 if (!strcmp(name, sh)) {
170 endusershell();
171 return (strdup(name));
172 }
173 /* allow just shell name, but use "real" path */
174 if ((p = strrchr(sh, '/')) && strcmp(name, p + 1) == 0) {
175 ret = strdup(sh);
176 endusershell();
177 return (ret);
178 }
179 }
180 endusershell();
181 return (NULL);
182 }