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: #if !defined(lint) && defined(DOSCCS) 19: static char sccsid[] = "@(#)util.c 5.9.1 (2.11BSD) 1996/1/12"; 20: #endif 21: 22: #include <sys/types.h> 23: #include <sys/time.h> 24: #include <pwd.h> 25: #include <stdio.h> 26: #include <chpass.h> 27: #include <strings.h> 28: #include <ctype.h> 29: #include "pathnames.h" 30: 31: static char dmsize[] = 32: { -1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; 33: static char *months[] = 34: { "January", "February", "March", "April", "May", "June", 35: "July", "August", "September", "October", "November", 36: "December", NULL }; 37: char * 38: ttoa(tval) 39: time_t tval; 40: { 41: register struct tm *tp; 42: static char tbuf[50]; 43: 44: if (tval) { 45: tp = localtime(&tval); 46: (void)sprintf(tbuf, "%s %d, 19%d", months[tp->tm_mon], 47: tp->tm_mday, tp->tm_year); 48: } 49: else 50: *tbuf = '\0'; 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 (!*p) { 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: 91: #define TM_YEAR_BASE 1900 92: #define EPOCH_YEAR 1970 93: #define DAYSPERNYEAR 365 94: #define DAYSPERLYEAR 366 95: #define HOURSPERDAY 24 96: #define MINSPERHOUR 60 97: #define SECSPERMIN 60 98: #define isleap(y) (((y) % 4) == 0 && ((y) % 100) != 0 || ((y) % 400) == 0) 99: 100: if (year < 100) 101: year += TM_YEAR_BASE; 102: if (year <= EPOCH_YEAR) 103: bad: return(1); 104: tval = isleap(year) && month > 2; 105: for (--year; year >= EPOCH_YEAR; --year) 106: tval += isleap(year) ? 107: DAYSPERLYEAR : DAYSPERNYEAR; 108: while (--month) 109: tval += dmsize[month]; 110: tval += day; 111: tval = tval * HOURSPERDAY * MINSPERHOUR * SECSPERMIN; 112: tval -= lt->tm_gmtoff; 113: *store = tval; 114: return(0); 115: } 116: 117: print(fp, pw) 118: register FILE *fp; 119: struct passwd *pw; 120: { 121: register char *p; 122: char *bp; 123: char *getusershell(), *ttoa(); 124: 125: fprintf(fp, "#Changing user database information for %s.\n", 126: pw->pw_name); 127: if (!uid) { 128: fprintf(fp, "Login: %s\n", pw->pw_name); 129: fprintf(fp, "Password: %s\n", pw->pw_passwd); 130: fprintf(fp, "Uid [#]: %d\n", pw->pw_uid); 131: fprintf(fp, "Gid [# or name]: %d\n", pw->pw_gid); 132: fprintf(fp, "Change [month day year]: %s\n", ttoa(pw->pw_change)); 133: fprintf(fp, "Expire [month day year]: %s\n", ttoa(pw->pw_expire)); 134: fprintf(fp, "Class: %s\n", pw->pw_class); 135: fprintf(fp, "Home directory: %s\n", pw->pw_dir); 136: fprintf(fp, "Shell: %s\n", 137: *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL); 138: } 139: else { 140: /* only admin can change "restricted" shells */ 141: setusershell(); 142: for (;;) 143: if (!(p = getusershell())) 144: break; 145: else if (!strcmp(pw->pw_shell, p)) { 146: fprintf(fp, "Shell: %s\n", *pw->pw_shell ? 147: pw->pw_shell : _PATH_BSHELL); 148: break; 149: } 150: } 151: bp = pw->pw_gecos; 152: p = strsep(&bp, ","); 153: fprintf(fp, "Full Name: %s\n", p ? p : ""); 154: p = strsep(&bp, ","); 155: fprintf(fp, "Location: %s\n", p ? p : ""); 156: p = strsep(&bp, ","); 157: fprintf(fp, "Office Phone: %s\n", p ? p : ""); 158: p = strsep(&bp, ","); 159: fprintf(fp, "Home Phone: %s\n", p ? p : ""); 160: }