secuser.c 5.0 KB

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