passwd.c 3.1 KB

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