secuser.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <mp.h>
  4. #include <libsec.h>
  5. #include "SConn.h"
  6. #include "secstore.h"
  7. int verbose;
  8. static void userinput(char *, int);
  9. void
  10. main(int argc, char **argv)
  11. {
  12. int isnew;
  13. char *id, buf[Maxmsg], passck[Maxmsg], home[Maxmsg], prompt[100], *hexHi;
  14. long expsecs;
  15. mpint *H = mpnew(0), *Hi = mpnew(0);
  16. PW *pw;
  17. Tm *tm;
  18. ARGBEGIN{
  19. case 'v':
  20. verbose++;
  21. break;
  22. }ARGEND;
  23. if(argc!=1){
  24. print("usage: secuser <user>\n");
  25. exits("usage");
  26. }
  27. id = argv[0];
  28. if(verbose)
  29. fprint(2,"secuser %s\n", id);
  30. if((pw = getPW(id,1)) == nil){
  31. isnew = 1;
  32. print("new account (because %s/%s %r)\n", SECSTORE_DIR, id);
  33. pw = emalloc(sizeof(*pw));
  34. pw->id = estrdup(id);
  35. snprint(home, sizeof(home), "%s/store/%s", SECSTORE_DIR, id);
  36. if(access(home, AEXIST) == 0){
  37. print("new user, but directory %s already exists\n", home);
  38. exits(home);
  39. }
  40. if(create(home, OREAD, DMDIR | 0775L) < 0){
  41. print("unable to create %s: %r\n", home);
  42. exits(home);
  43. }
  44. }else
  45. isnew = 0;
  46. /* get main password for id */
  47. for(;;){
  48. if(isnew)
  49. snprint(prompt, sizeof(prompt), "%s password: ", id);
  50. else
  51. snprint(prompt, sizeof(prompt), "%s password [default = don't change]: ", id);
  52. if(getpasswd(prompt, buf, sizeof(buf)) < 0){
  53. print("getpass failed\n");
  54. exits("getpass failed");
  55. }
  56. if(verbose)
  57. print("%ld characters\n", strlen(buf));
  58. if(buf[0] == '\0' && isnew == 0)
  59. break;
  60. if(strlen(buf) >= 7)
  61. break;
  62. print("password must be at least 7 characters\n");
  63. }
  64. if(buf[0] != '\0'){
  65. snprint(prompt, sizeof(prompt), "retype password: ");
  66. if(verbose)
  67. print("confirming...\n");
  68. if(getpasswd(prompt, passck, sizeof(passck)) < 0){
  69. print("getpass failed\n");
  70. exits("getpass failed");
  71. }
  72. if(strcmp(buf, passck) != 0){
  73. print("passwords didn't match\n");
  74. exits("no match");
  75. }
  76. hexHi = PAK_Hi(id, buf, H, Hi);
  77. free(hexHi);
  78. mpfree(H);
  79. pw->Hi = Hi;
  80. }
  81. /* get expiration time (midnight of date specified) */
  82. if(isnew)
  83. expsecs = time(0) + 365*24*60*60;
  84. else
  85. expsecs = pw->expire;
  86. for(;;){
  87. tm = localtime(expsecs);
  88. print("expires [DDMMYYYY, default = %2.2d%2.2d%4.4d]: ",
  89. tm->mday, tm->mon, tm->year+1900);
  90. userinput(buf, sizeof(buf));
  91. if(strlen(buf) == 0)
  92. break;
  93. if(strlen(buf) != 8){
  94. print("!bad date format: %s\n", buf);
  95. continue;
  96. }
  97. tm->mday = (buf[0]-'0')*10 + (buf[1]-'0');
  98. if(tm->mday > 31 || tm->mday < 1){
  99. print("!bad day of month: %d\n", tm->mday);
  100. continue;
  101. }
  102. tm->mon = (buf[2]-'0')*10 + (buf[3]-'0') - 1;
  103. if(tm->mon > 11 || tm->mday < 0){
  104. print("!bad month: %d\n", tm->mon + 1);
  105. continue;
  106. }
  107. tm->year = atoi(buf+4) - 1900;
  108. if(tm->year < 70){
  109. print("!bad year: %d\n", tm->year + 1900);
  110. continue;
  111. }
  112. tm->sec = 59;
  113. tm->min = 59;
  114. tm->hour = 23;
  115. expsecs = tm2sec(tm);
  116. break;
  117. }
  118. pw->expire = expsecs;
  119. /* failed logins */
  120. if(pw->failed != 0 )
  121. print("clearing %d failed login attempts\n", pw->failed);
  122. pw->failed = 0;
  123. /* status bits */
  124. if(isnew)
  125. pw->status = Enabled;
  126. for(;;){
  127. print("Enabled or Disabled [default %s]: ",
  128. (pw->status & Enabled) ? "Enabled" : "Disabled" );
  129. userinput(buf, sizeof(buf));
  130. if(strlen(buf) == 0)
  131. break;
  132. if(buf[0]=='E' || buf[0]=='e'){
  133. pw->status |= Enabled;
  134. break;
  135. }
  136. if(buf[0]=='D' || buf[0]=='d'){
  137. pw->status = pw->status & ~Enabled;
  138. break;
  139. }
  140. }
  141. for(;;){
  142. print("require STA? [default %s]: ",
  143. (pw->status & STA) ? "yes" : "no" );
  144. userinput(buf, sizeof(buf));
  145. if(strlen(buf) == 0)
  146. break;
  147. if(buf[0]=='Y' || buf[0]=='y'){
  148. pw->status |= STA;
  149. break;
  150. }
  151. if(buf[0]=='N' || buf[0]=='n'){
  152. pw->status = pw->status & ~STA;
  153. break;
  154. }
  155. }
  156. /* free form field */
  157. if(isnew)
  158. pw->other = nil;
  159. print("comments [default = %s]: ", (pw->other == nil) ? "" : pw->other);
  160. userinput(buf, 72); /* 72 comes from password.h */
  161. if(buf[0])
  162. if((pw->other = strdup(buf)) == nil)
  163. sysfatal("strdup");
  164. syslog(0, LOG, "CHANGELOGIN for '%s'", pw->id);
  165. if(putPW(pw) < 0)
  166. print("error writing entry: %r\n");
  167. else
  168. print("change written\n");
  169. exits("");
  170. }
  171. static void
  172. userinput(char *buf, int blen)
  173. {
  174. int n;
  175. while(1){
  176. n = read(0, buf, blen);
  177. if(n<=0)
  178. exits("read error");
  179. if(buf[n-1]=='\n'){
  180. buf[n-1] = '\0';
  181. return;
  182. }
  183. buf += n; blen -= n;
  184. if(blen<=0)
  185. exits("input too large");
  186. }
  187. }