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