passwd.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 = getenv("service");
  50. if(s && strcmp(s, "cpu") == 0){
  51. fprint(2, "passwd must not be run on the cpu server\n");
  52. exits("boofhead");
  53. }
  54. s = nil;
  55. if(argc > 0){
  56. user = argv[0];
  57. s = strchr(user, '@');
  58. if(s != nil)
  59. *s++ = 0;
  60. if(*user == 0)
  61. user = getuser();
  62. }
  63. fd = authdial(nil, s);
  64. if(fd < 0)
  65. error("protocol botch: %r");
  66. /* send ticket request to AS */
  67. memset(&tr, 0, sizeof(tr));
  68. strcpy(tr.uid, user);
  69. tr.type = AuthPass;
  70. convTR2M(&tr, buf);
  71. if(write(fd, buf, TICKREQLEN) != TICKREQLEN)
  72. error("protocol botch: %r");
  73. if(asrdresp(fd, buf, TICKETLEN) < 0)
  74. error("%r");
  75. memmove(tbuf, buf, TICKETLEN);
  76. /*
  77. * get a password from the user and try to decrypt the
  78. * ticket. If it doesn't work we've got a bad password,
  79. * give up.
  80. */
  81. readln("Plan 9 Password: ", pr.old, sizeof pr.old, 1);
  82. passtokey(key, pr.old);
  83. convM2T(tbuf, &t, key);
  84. if(t.num != AuthTp || strcmp(t.cuid, tr.uid))
  85. error("bad password");
  86. /* loop trying new passwords */
  87. for(;;){
  88. pr.changesecret = 0;
  89. *pr.new = 0;
  90. readln("change Plan 9 Password? (y/n) ", buf, sizeof buf, 0);
  91. if(*buf == 'y' || *buf == 'Y'){
  92. readln("Password(8 to 31 characters): ", pr.new,
  93. sizeof pr.new, 1);
  94. readln("Confirm: ", buf, sizeof buf, 1);
  95. if(strcmp(pr.new, buf)){
  96. print("!mismatch\n");
  97. continue;
  98. }
  99. }
  100. readln("change Inferno/POP password? (y/n) ", buf, sizeof buf, 0);
  101. if(*buf == 'y' || *buf == 'Y'){
  102. pr.changesecret = 1;
  103. readln("make it the same as your plan 9 password? (y/n) ",
  104. buf, sizeof buf, 0);
  105. if(*buf == 'y' || *buf == 'Y'){
  106. if(*pr.new == 0)
  107. strcpy(pr.secret, pr.old);
  108. else
  109. strcpy(pr.secret, pr.new);
  110. } else {
  111. readln("Secret(0 to 256 characters): ", pr.secret,
  112. sizeof pr.secret, 1);
  113. readln("Confirm: ", buf, sizeof buf, 1);
  114. if(strcmp(pr.secret, buf)){
  115. print("!mismatch\n");
  116. continue;
  117. }
  118. }
  119. }
  120. pr.num = AuthPass;
  121. convPR2M(&pr, buf, t.key);
  122. if(write(fd, buf, PASSREQLEN) != PASSREQLEN)
  123. error("AS protocol botch: %r");
  124. if(asrdresp(fd, buf, 0) == 0)
  125. break;
  126. fprint(2, "refused: %r\n");
  127. }
  128. close(fd);
  129. exits(0);
  130. }