xref: /original-bsd/usr.bin/chpass/util.c (revision 375a25e0)
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.3 (Berkeley) 02/23/89";
20 #endif /* not lint */
21 
22 #include <sys/types.h>
23 #include <sys/time.h>
24 #include <tzfile.h>
25 #include </usr/src/include/pwd.h>
26 #include <stdio.h>
27 #include <chpass.h>
28 #include <strings.h>
29 #include <ctype.h>
30 
31 static int dmsize[] =
32 	{ -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
33 static char *months[] =
34 	{ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug",
35 	  "Sep", "Oct", "Nov", "Dec", NULL };
36 char *
37 ttoa(tval)
38 	time_t tval;
39 {
40 	struct tm *tp;
41 	static char tbuf[10];
42 
43 	if (!tval)
44 		bcopy("None", tbuf, 6);
45 	else {
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 	return(tbuf);
51 }
52 
53 atot(p, store)
54 	char *p;
55 	time_t *store;
56 {
57 	register char *t, **mp;
58 	static struct tm *lt;
59 	time_t tval, time();
60 	int day, month, year;
61 
62 	if (!strncasecmp(p, "none", 4)) {
63 		*store = 0;
64 		return(0);
65 	}
66 	if (!lt) {
67 		unsetenv("TZ");
68 		(void)time(&tval);
69 		lt = localtime(&tval);
70 	}
71 	if (!(t = strtok(p, " \t")))
72 		goto bad;
73 	for (mp = months;; ++mp) {
74 		if (!*mp)
75 			goto bad;
76 		if (!strncasecmp(*mp, t, 3)) {
77 			month = mp - months + 1;
78 			break;
79 		}
80 	}
81 	if (!(t = strtok((char *)NULL, " \t,")) || !isdigit(*t))
82 		goto bad;
83 	day = atoi(t);
84 	if (!(t = strtok((char *)NULL, " \t,")) || !isdigit(*t))
85 		goto bad;
86 	year = atoi(t);
87 	if (day < 1 || day > 31 || month < 1 || month > 12 || !year)
88 		goto bad;
89 	if (year < 100)
90 		year += TM_YEAR_BASE;
91 	if (year <= EPOCH_YEAR)
92 bad:		return(1);
93 	tval = isleap(year) && month > 2;
94 	for (--year; year >= EPOCH_YEAR; --year)
95 		tval += isleap(year) ?
96 		    DAYS_PER_LYEAR : DAYS_PER_NYEAR;
97 	while (--month)
98 		tval += dmsize[month];
99 	tval += day - 1;
100 	tval = tval * HOURS_PER_DAY * MINS_PER_HOUR * SECS_PER_MIN;
101 	tval -= lt->tm_gmtoff;
102 	*store = tval;
103 	return(0);
104 }
105 
106 print(fp, pw)
107 	FILE *fp;
108 	struct passwd *pw;
109 {
110 	register char *p;
111 	char *getusershell(), *ttoa();
112 
113 	fprintf(fp, "Changing user database information for %s.\n",
114 	    pw->pw_name);
115 	if (!uid) {
116 		fprintf(fp, "Login: %s\n", pw->pw_name);
117 		fprintf(fp, "Uid [#]: %d\n", pw->pw_uid);
118 		fprintf(fp, "Gid [# or name]: %d\n", pw->pw_gid);
119 		fprintf(fp, "Change [dd month yy]: %s\n", ttoa(pw->pw_change));
120 		fprintf(fp, "Expire [dd month yy]: %s\n", ttoa(pw->pw_expire));
121 		fprintf(fp, "Class: %s\n", pw->pw_class);
122 		fprintf(fp, "Home directory: %s\n", pw->pw_dir);
123 		fprintf(fp, "Shell: %s\n",
124 		    *pw->pw_shell ? pw->pw_shell : "/bin/sh");
125 	}
126 	else {
127 		/* only admin can change "restricted" shells */
128 		setusershell();
129 		for (;;)
130 			if (!(p = getusershell()))
131 				break;
132 			else if (!strcmp(pw->pw_shell, p)) {
133 				fprintf(fp, "Shell: %s\n",
134 				    *pw->pw_shell ? pw->pw_shell : "/bin/sh");
135 				break;
136 			}
137 	}
138 	p = strsep(pw->pw_gecos, ",");
139 	fprintf(fp, "Full Name: %s\n", p ? p : "");
140 	p = strsep((char *)NULL, ",");
141 	fprintf(fp, "Location: %s\n", p ? p : "");
142 	p = strsep((char *)NULL, ",");
143 	fprintf(fp, "Office Phone: %s\n", p ? p : "");
144 	p = strsep((char *)NULL, ",");
145 	fprintf(fp, "Home Phone: %s\n", p ? p : "");
146 }
147