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