1 #include <u.h> 2 #include <libc.h> 3 #include <ctype.h> 4 #include "authcmdlib.h" 5 6 /* 7 * get the date in the format yyyymmdd 8 */ 9 Tm 10 getdate(char *d) 11 { 12 Tm date; 13 int i; 14 15 date.year = date.mon = date.mday = 0; 16 date.hour = date.min = date.sec = 0; 17 for(i = 0; i < 8; i++) 18 if(!isdigit(d[i])) 19 return date; 20 date.year = (d[0]-'0')*1000 + (d[1]-'0')*100 + (d[2]-'0')*10 + d[3]-'0'; 21 date.year -= 1900; 22 d += 4; 23 date.mon = (d[0]-'0')*10 + d[1]-'0' - 1; 24 d += 2; 25 date.mday = (d[0]-'0')*10 + d[1]-'0'; 26 return date; 27 } 28 29 long 30 getexpiration(char *db, char *u) 31 { 32 char buf[Maxpath]; 33 char prompt[128]; 34 char cdate[32]; 35 Tm date; 36 ulong secs, now, fd; 37 int n; 38 39 /* read current expiration (if any) */ 40 snprint(buf, sizeof buf, "%s/%s/expire", db, u); 41 fd = open(buf, OREAD); 42 buf[0] = 0; 43 if(fd >= 0){ 44 n = read(fd, buf, sizeof(buf)-1); 45 if(n > 0) 46 buf[n-1] = 0; 47 close(fd); 48 } 49 50 if(buf[0]){ 51 if(strncmp(buf, "never", 5)){ 52 secs = atoi(buf); 53 memmove(&date, localtime(secs), sizeof(date)); 54 sprint(buf, "%4.4d%2.2d%2.2d", date.year+1900, date.mon+1, date.mday); 55 } else 56 buf[5] = 0; 57 } else 58 strcpy(buf, "never"); 59 sprint(prompt, "Expiration date (YYYYMMDD or never)[return = %s]: ", buf); 60 61 now = time(0); 62 for(;;){ 63 readln(prompt, cdate, sizeof cdate, 0); 64 if(*cdate == 0) 65 return -1; 66 if(strcmp(cdate, "never") == 0) 67 return 0; 68 date = getdate(cdate); 69 secs = tm2sec(&date); 70 if(secs > now && secs < now + 2*365*24*60*60) 71 break; 72 print("expiration time must fall between now and 2 years from now\n"); 73 } 74 return secs; 75 } 76