xref: /original-bsd/usr.bin/chpass/util.c (revision f7fca458)
1 /*
2  * Copyright (c) 1988 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 static char sccsid[] = "@(#)util.c	5.5 (Berkeley) 03/09/89";
20 #endif /* not lint */
21 
22 #include <sys/types.h>
23 #include <sys/time.h>
24 #include <tzfile.h>
25 #include <pwd.h>
26 #include <stdio.h>
27 #include <chpass.h>
28 #include <strings.h>
29 #include <ctype.h>
30 #include "pathnames.h"
31 
32 static int dmsize[] =
33 	{ -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
34 static char *months[] =
35 	{ "January", "February", "March", "April", "May", "June",
36 	  "July", "August", "September", "October", "November",
37 	  "December", NULL };
38 char *
39 ttoa(tval)
40 	time_t tval;
41 {
42 	struct tm *tp;
43 	static char tbuf[10];
44 
45 	if (tval) {
46 		tp = localtime(&tval);
47 		(void)sprintf(tbuf, "%s %d, 19%d", months[tp->tm_mon],
48 		    tp->tm_mday, tp->tm_year);
49 	}
50 	else
51 		*tbuf = '\0';
52 	return(tbuf);
53 }
54 
55 atot(p, store)
56 	char *p;
57 	time_t *store;
58 {
59 	register char *t, **mp;
60 	static struct tm *lt;
61 	time_t tval, time();
62 	int day, month, year;
63 
64 	if (!*p) {
65 		*store = 0;
66 		return(0);
67 	}
68 	if (!lt) {
69 		unsetenv("TZ");
70 		(void)time(&tval);
71 		lt = localtime(&tval);
72 	}
73 	if (!(t = strtok(p, " \t")))
74 		goto bad;
75 	for (mp = months;; ++mp) {
76 		if (!*mp)
77 			goto bad;
78 		if (!strncasecmp(*mp, t, 3)) {
79 			month = mp - months + 1;
80 			break;
81 		}
82 	}
83 	if (!(t = strtok((char *)NULL, " \t,")) || !isdigit(*t))
84 		goto bad;
85 	day = atoi(t);
86 	if (!(t = strtok((char *)NULL, " \t,")) || !isdigit(*t))
87 		goto bad;
88 	year = atoi(t);
89 	if (day < 1 || day > 31 || month < 1 || month > 12 || !year)
90 		goto bad;
91 	if (year < 100)
92 		year += TM_YEAR_BASE;
93 	if (year <= EPOCH_YEAR)
94 bad:		return(1);
95 	tval = isleap(year) && month > 2;
96 	for (--year; year >= EPOCH_YEAR; --year)
97 		tval += isleap(year) ?
98 		    DAYS_PER_LYEAR : DAYS_PER_NYEAR;
99 	while (--month)
100 		tval += dmsize[month];
101 	tval += day - 1;
102 	tval = tval * HOURS_PER_DAY * MINS_PER_HOUR * SECS_PER_MIN;
103 	tval -= lt->tm_gmtoff;
104 	*store = tval;
105 	return(0);
106 }
107 
108 print(fp, pw)
109 	FILE *fp;
110 	struct passwd *pw;
111 {
112 	register char *p;
113 	char *getusershell(), *ttoa();
114 
115 	fprintf(fp, "Changing user database information for %s.\n",
116 	    pw->pw_name);
117 	if (!uid) {
118 		fprintf(fp, "Login: %s\n", pw->pw_name);
119 		fprintf(fp, "Uid [#]: %d\n", pw->pw_uid);
120 		fprintf(fp, "Gid [# or name]: %d\n", pw->pw_gid);
121 		fprintf(fp, "Change [month day year]: %s\n", ttoa(pw->pw_change));
122 		fprintf(fp, "Expire [month day year]: %s\n", ttoa(pw->pw_expire));
123 		fprintf(fp, "Class: %s\n", pw->pw_class);
124 		fprintf(fp, "Home directory: %s\n", pw->pw_dir);
125 		fprintf(fp, "Shell: %s\n",
126 		    *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL);
127 	}
128 	else {
129 		/* only admin can change "restricted" shells */
130 		setusershell();
131 		for (;;)
132 			if (!(p = getusershell()))
133 				break;
134 			else if (!strcmp(pw->pw_shell, p)) {
135 				fprintf(fp, "Shell: %s\n", *pw->pw_shell ?
136 				    pw->pw_shell : _PATH_BSHELL);
137 				break;
138 			}
139 	}
140 	p = strsep(pw->pw_gecos, ",");
141 	fprintf(fp, "Full Name: %s\n", p ? p : "");
142 	p = strsep((char *)NULL, ",");
143 	fprintf(fp, "Location: %s\n", p ? p : "");
144 	p = strsep((char *)NULL, ",");
145 	fprintf(fp, "Office Phone: %s\n", p ? p : "");
146 	p = strsep((char *)NULL, ",");
147 	fprintf(fp, "Home Phone: %s\n", p ? p : "");
148 }
149