doauthenticate.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 <auth.h>
  12. #include "../boot/boot.h"
  13. static char *pbmsg = "AS protocol botch";
  14. static char *ccmsg = "can't connect to AS";
  15. i32
  16. readn(int fd, void *buf, i32 len)
  17. {
  18. int m, n;
  19. char *p;
  20. p = buf;
  21. for(n = 0; n < len; n += m){
  22. m = read(fd, p + n, len - n);
  23. if(m <= 0)
  24. return -1;
  25. }
  26. return n;
  27. }
  28. static char *
  29. fromauth(Method *mp, char *trbuf, char *tbuf)
  30. {
  31. int afd;
  32. char t;
  33. char *msg;
  34. static char error[2 * ERRMAX];
  35. if(mp->auth == 0)
  36. fatal("no method for accessing auth server");
  37. afd = (*mp->auth)();
  38. if(afd < 0){
  39. sprint(error, "%s: %r", ccmsg);
  40. return error;
  41. }
  42. if(write(afd, trbuf, TICKREQLEN) < 0 || read(afd, &t, 1) != 1){
  43. close(afd);
  44. sprint(error, "%s: %r", pbmsg);
  45. return error;
  46. }
  47. switch(t){
  48. case AuthOK:
  49. msg = 0;
  50. if(readn(afd, tbuf, 2 * TICKETLEN) < 0){
  51. sprint(error, "%s: %r", pbmsg);
  52. msg = error;
  53. }
  54. break;
  55. case AuthErr:
  56. if(readn(afd, error, ERRMAX) < 0){
  57. sprint(error, "%s: %r", pbmsg);
  58. msg = error;
  59. } else {
  60. error[ERRMAX - 1] = 0;
  61. msg = error;
  62. }
  63. break;
  64. default:
  65. msg = pbmsg;
  66. break;
  67. }
  68. close(afd);
  69. return msg;
  70. }
  71. void
  72. doauthenticate(int fd, Method *mp)
  73. {
  74. char *msg;
  75. char trbuf[TICKREQLEN];
  76. char tbuf[2 * TICKETLEN];
  77. print("session...");
  78. if(fsession(fd, trbuf, sizeof trbuf) < 0)
  79. fatal("session command failed");
  80. /* no authentication required? */
  81. memset(tbuf, 0, 2 * TICKETLEN);
  82. if(trbuf[0] == 0)
  83. return;
  84. /* try getting to an auth server */
  85. print("getting ticket...");
  86. msg = fromauth(mp, trbuf, tbuf);
  87. print("authenticating...");
  88. if(msg == 0)
  89. if(fauth(fd, tbuf) >= 0)
  90. return;
  91. /* didn't work, go for the security hole */
  92. fprint(2, "no authentication server (%s), using your key as server key\n", msg);
  93. }
  94. char *
  95. checkkey(Method *mp, char *name, char *key)
  96. {
  97. char *msg;
  98. Ticketreq tr;
  99. Ticket t;
  100. char trbuf[TICKREQLEN];
  101. char tbuf[TICKETLEN];
  102. memset(&tr, 0, sizeof tr);
  103. tr.type = AuthTreq;
  104. strcpy(tr.authid, name);
  105. strcpy(tr.hostid, name);
  106. strcpy(tr.uid, name);
  107. convTR2M(&tr, trbuf);
  108. msg = fromauth(mp, trbuf, tbuf);
  109. if(msg == ccmsg){
  110. fprint(2, "boot: can't contact auth server, passwd unchecked\n");
  111. return 0;
  112. }
  113. if(msg)
  114. return msg;
  115. convM2T(tbuf, &t, key);
  116. if(t.num == AuthTc && strcmp(name, t.cuid) == 0)
  117. return 0;
  118. return "no match";
  119. }