password.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /* password.c */
  10. #include <u.h>
  11. #include <libc.h>
  12. #include <bio.h>
  13. #include <mp.h>
  14. #include <libsec.h>
  15. #include "SConn.h"
  16. #include "secstore.h"
  17. static Biobuf*
  18. openPW(char *id, int mode)
  19. {
  20. int nfn = strlen(SECSTORE_DIR)+strlen(id)+20;
  21. char *fn;
  22. Biobuf *b;
  23. if(validatefile(id) == nil || strcmp(id,".") == 0)
  24. return nil;
  25. fn = emalloc(nfn);
  26. snprint(fn, nfn, "%s/who/%s", SECSTORE_DIR, id);
  27. b = Bopen(fn, mode);
  28. free(fn);
  29. return b;
  30. }
  31. static uint32_t
  32. mtimePW(char *id)
  33. {
  34. uint32_t mt;
  35. char *fn;
  36. Dir *d;
  37. fn = smprint("%s/who/%s", SECSTORE_DIR, id);
  38. d = dirstat(fn);
  39. mt = (d? d->mtime: 0);
  40. free(d);
  41. free(fn);
  42. return mt;
  43. }
  44. PW *
  45. getPW(char *id, int dead_or_alive)
  46. {
  47. uint32_t now = time(0);
  48. char *f1, *f2, *oid; /* fields 1, 2 = attribute, value */
  49. Biobuf *bin;
  50. PW *pw;
  51. oid = id;
  52. if((bin = openPW(id, OREAD)) == 0){
  53. id = "FICTITIOUS";
  54. if((bin = openPW(id, OREAD)) == 0){
  55. werrstr("accounts %s and FICTITIOUS do not exist", oid);
  56. return nil;
  57. }
  58. }
  59. pw = emalloc(sizeof *pw);
  60. pw->id = estrdup(id);
  61. pw->status |= Enabled;
  62. while( (f1 = Brdline(bin, '\n')) != 0){
  63. f1[Blinelen(bin)-1] = 0;
  64. for(f2 = f1; *f2 && *f2 != ' ' && *f2 != '\t'; f2++)
  65. ;
  66. if(*f2)
  67. for(*f2++ = 0; *f2 && (*f2==' ' || *f2=='\t'); f2++)
  68. ;
  69. if(strcmp(f1, "exp") == 0)
  70. pw->expire = strtoul(f2, 0, 10);
  71. else if(strcmp(f1, "DISABLED") == 0)
  72. pw->status &= ~Enabled;
  73. else if(strcmp(f1, "STA") == 0)
  74. pw->status |= STA;
  75. else if(strcmp(f1, "failed") == 0)
  76. pw->failed = strtoul(f2, 0, 10);
  77. else if(strcmp(f1, "other") == 0)
  78. pw->other = estrdup(f2);
  79. else if(strcmp(f1, "PAK-Hi") == 0)
  80. pw->Hi = strtomp(f2, nil, 64, nil);
  81. }
  82. Bterm(bin);
  83. if(pw->Hi == nil){
  84. werrstr("corrupted account file for %s", pw->id);
  85. freePW(pw);
  86. return nil;
  87. }
  88. if(dead_or_alive)
  89. return pw; /* return for editing, whether valid now or not */
  90. if(pw->expire != 0 && pw->expire <= now){
  91. /* %.28s excludes ctime's newline */
  92. werrstr("account %s expired at %.28s", pw->id,
  93. ctime(pw->expire));
  94. freePW(pw);
  95. return nil;
  96. }
  97. if((pw->status & Enabled) == 0){
  98. werrstr("account %s disabled", pw->id);
  99. freePW(pw);
  100. return nil;
  101. }
  102. if(pw->failed < 10)
  103. return pw; /* success */
  104. if(now < mtimePW(id)+300){
  105. werrstr("too many failures; try again in five minutes");
  106. freePW(pw);
  107. return nil;
  108. }
  109. pw->failed = 0;
  110. putPW(pw); /* reset failed-login-counter after five minutes */
  111. return pw;
  112. }
  113. int
  114. putPW(PW *pw)
  115. {
  116. Biobuf *bout;
  117. char *hexHi;
  118. if((bout = openPW(pw->id, OWRITE|OTRUNC)) ==0){
  119. werrstr("can't open PW file for %s", pw->id);
  120. return -1;
  121. }
  122. Bprint(bout, "exp %lud\n", pw->expire);
  123. if(!(pw->status & Enabled))
  124. Bprint(bout, "DISABLED\n");
  125. if(pw->status & STA)
  126. Bprint(bout, "STA\n");
  127. if(pw->failed)
  128. Bprint(bout, "failed\t%d\n", pw->failed);
  129. if(pw->other)
  130. Bprint(bout,"other\t%s\n", pw->other);
  131. hexHi = mptoa(pw->Hi, 64, nil, 0);
  132. Bprint(bout, "PAK-Hi\t%s\n", hexHi);
  133. free(hexHi);
  134. return 0;
  135. }
  136. void
  137. freePW(PW *pw)
  138. {
  139. if(pw == nil)
  140. return;
  141. free(pw->id);
  142. free(pw->other);
  143. mpfree(pw->Hi);
  144. free(pw);
  145. }