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 snprint(buf, sizeof buf, "%4.4d%2.2d%2.2d", 57 date.year+1900, date.mon+1, date.mday); 58 } else 59 buf[5] = 0; 60 } else 61 strcpy(buf, "never"); 62 snprint(prompt, sizeof prompt, 63 "Expiration date (YYYYMMDD or never)[return = %s]: ", buf); 64 65 now = time(0); 66 for(;;){ 67 readln(prompt, cdate, sizeof cdate, 0); 68 if(*cdate == 0) 69 return -1; 70 if(strcmp(cdate, "never") == 0) 71 return 0; 72 date = getdate(cdate); 73 secs = tm2sec(&date); 74 if(secs > now && secs < now + 2*365*24*60*60) 75 break; 76 print("expiration time must fall between now and 2 years from now\n"); 77 } 78 return secs; 79 } 80