passwd.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <authsrv.h>
  4. #include <bio.h>
  5. #include "authcmdlib.h"
  6. static char *pbmsg = "AS protocol botch";
  7. int
  8. asrdresp(int fd, char *buf, int len)
  9. {
  10. char error[AERRLEN];
  11. if(read(fd, buf, 1) != 1){
  12. werrstr(pbmsg);
  13. return -1;
  14. }
  15. switch(buf[0]){
  16. case AuthOK:
  17. if(readn(fd, buf, len) < 0){
  18. werrstr(pbmsg);
  19. return -1;
  20. }
  21. break;
  22. case AuthErr:
  23. if(readn(fd, error, AERRLEN) < 0){
  24. werrstr(pbmsg);
  25. return -1;
  26. }
  27. error[AERRLEN-1] = 0;
  28. werrstr(error);
  29. return -1;
  30. default:
  31. werrstr(pbmsg);
  32. return -1;
  33. }
  34. return 0;
  35. }
  36. void
  37. main(int argc, char **argv)
  38. {
  39. int fd;
  40. Ticketreq tr;
  41. Ticket t;
  42. Passwordreq pr;
  43. char tbuf[TICKETLEN];
  44. char key[DESKEYLEN];
  45. char buf[512];
  46. char *s, *user;
  47. user = getuser();
  48. ARGBEGIN{
  49. }ARGEND
  50. s = nil;
  51. if(argc > 0){
  52. user = argv[0];
  53. s = strchr(user, '@');
  54. if(s != nil)
  55. *s++ = 0;
  56. if(*user == 0)
  57. user = getuser();
  58. }
  59. fd = authdial(nil, s);
  60. if(fd < 0)
  61. error("protocol botch: %r");
  62. /* send ticket request to AS */
  63. memset(&tr, 0, sizeof(tr));
  64. strcpy(tr.uid, user);
  65. tr.type = AuthPass;
  66. convTR2M(&tr, buf);
  67. if(write(fd, buf, TICKREQLEN) != TICKREQLEN)
  68. error("protocol botch: %r");
  69. if(asrdresp(fd, buf, TICKETLEN) < 0)
  70. error("%r");
  71. memmove(tbuf, buf, TICKETLEN);
  72. /*
  73. * get a password from the user and try to decrypt the
  74. * ticket. If it doesn't work we've got a bad password,
  75. * give up.
  76. */
  77. readln("Plan 9 Password: ", pr.old, sizeof pr.old, 1);
  78. passtokey(key, pr.old);
  79. convM2T(tbuf, &t, key);
  80. if(t.num != AuthTp || strcmp(t.cuid, tr.uid))
  81. error("bad password");
  82. /* loop trying new passwords */
  83. for(;;){
  84. pr.changesecret = 0;
  85. *pr.new = 0;
  86. readln("change Plan 9 Password? (y/n) ", buf, sizeof buf, 0);
  87. if(*buf == 'y' || *buf == 'Y'){
  88. readln("Password(8 to 31 characters): ", pr.new,
  89. sizeof pr.new, 1);
  90. readln("Confirm: ", buf, sizeof buf, 1);
  91. if(strcmp(pr.new, buf)){
  92. print("!mismatch\n");
  93. continue;
  94. }
  95. }
  96. readln("change Inferno/POP password? (y/n) ", buf, sizeof buf, 0);
  97. if(*buf == 'y' || *buf == 'Y'){
  98. pr.changesecret = 1;
  99. readln("make it the same as your plan 9 password? (y/n) ",
  100. buf, sizeof buf, 0);
  101. if(*buf == 'y' || *buf == 'Y'){
  102. if(*pr.new == 0)
  103. strcpy(pr.secret, pr.old);
  104. else
  105. strcpy(pr.secret, pr.new);
  106. } else {
  107. readln("Secret(0 to 256 characters): ", pr.secret,
  108. sizeof pr.secret, 1);
  109. readln("Confirm: ", buf, sizeof buf, 1);
  110. if(strcmp(pr.secret, buf)){
  111. print("!mismatch\n");
  112. continue;
  113. }
  114. }
  115. }
  116. pr.num = AuthPass;
  117. convPR2M(&pr, buf, t.key);
  118. if(write(fd, buf, PASSREQLEN) != PASSREQLEN)
  119. error("AS protocol botch: %r");
  120. if(asrdresp(fd, buf, 0) == 0)
  121. break;
  122. fprint(2, "passwd: refused: %r\n");
  123. }
  124. close(fd);
  125. exits(0);
  126. }