getexpiration.c 1.5 KB

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