passwd.c 2.7 KB

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