doauthenticate.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. int32_t
  16. readn(int fd, void *buf, int32_t 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. }
  60. else {
  61. error[ERRMAX-1] = 0;
  62. msg = error;
  63. }
  64. break;
  65. default:
  66. msg = pbmsg;
  67. break;
  68. }
  69. close(afd);
  70. return msg;
  71. }
  72. void
  73. doauthenticate(int fd, Method *mp)
  74. {
  75. char *msg;
  76. char trbuf[TICKREQLEN];
  77. char tbuf[2*TICKETLEN];
  78. print("session...");
  79. if(fsession(fd, trbuf, sizeof trbuf) < 0)
  80. fatal("session command failed");
  81. /* no authentication required? */
  82. memset(tbuf, 0, 2*TICKETLEN);
  83. if(trbuf[0] == 0)
  84. return;
  85. /* try getting to an auth server */
  86. print("getting ticket...");
  87. msg = fromauth(mp, trbuf, tbuf);
  88. print("authenticating...");
  89. if(msg == 0)
  90. if(fauth(fd, tbuf) >= 0)
  91. return;
  92. /* didn't work, go for the security hole */
  93. fprint(2, "no authentication server (%s), using your key as server key\n", msg);
  94. }
  95. char*
  96. checkkey(Method *mp, char *name, char *key)
  97. {
  98. char *msg;
  99. Ticketreq tr;
  100. Ticket t;
  101. char trbuf[TICKREQLEN];
  102. char tbuf[TICKETLEN];
  103. memset(&tr, 0, sizeof tr);
  104. tr.type = AuthTreq;
  105. strcpy(tr.authid, name);
  106. strcpy(tr.hostid, name);
  107. strcpy(tr.uid, name);
  108. convTR2M(&tr, trbuf);
  109. msg = fromauth(mp, trbuf, tbuf);
  110. if(msg == ccmsg){
  111. fprint(2, "boot: can't contact auth server, passwd unchecked\n");
  112. return 0;
  113. }
  114. if(msg)
  115. return msg;
  116. convM2T(tbuf, &t, key);
  117. if(t.num == AuthTc && strcmp(name, t.cuid)==0)
  118. return 0;
  119. return "no match";
  120. }