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.10 (Berkeley) 05/11/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 <strings.h> 28 #include <ctype.h> 29 #include "chpass.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[50]; 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 DAYSPERLYEAR : DAYSPERNYEAR; 99 while (--month) 100 tval += dmsize[month]; 101 tval += day; 102 tval = tval * HOURSPERDAY * MINSPERHOUR * SECSPERMIN; 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, "Password: %s\n", pw->pw_passwd); 120 fprintf(fp, "Uid [#]: %d\n", pw->pw_uid); 121 fprintf(fp, "Gid [# or name]: %d\n", pw->pw_gid); 122 fprintf(fp, "Change [month day year]: %s\n", ttoa(pw->pw_change)); 123 fprintf(fp, "Expire [month day year]: %s\n", ttoa(pw->pw_expire)); 124 fprintf(fp, "Class: %s\n", pw->pw_class); 125 fprintf(fp, "Home directory: %s\n", pw->pw_dir); 126 fprintf(fp, "Shell: %s\n", 127 *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL); 128 } 129 else { 130 /* only admin can change "restricted" shells */ 131 setusershell(); 132 for (;;) 133 if (!(p = getusershell())) 134 break; 135 else if (!strcmp(pw->pw_shell, p)) { 136 fprintf(fp, "Shell: %s\n", *pw->pw_shell ? 137 pw->pw_shell : _PATH_BSHELL); 138 break; 139 } 140 } 141 p = strsep(pw->pw_gecos, ","); 142 fprintf(fp, "Full Name: %s\n", p ? p : ""); 143 p = strsep((char *)NULL, ","); 144 fprintf(fp, "Location: %s\n", p ? p : ""); 145 p = strsep((char *)NULL, ","); 146 fprintf(fp, "Office Phone: %s\n", p ? p : ""); 147 p = strsep((char *)NULL, ","); 148 fprintf(fp, "Home Phone: %s\n", p ? p : ""); 149 } 150