auth.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include "lib9.h"
  2. #include "auth.h"
  3. #include "authlocal.h"
  4. static char *badreq = "bad ticket request";
  5. static char *ccmsg = "can't connect to AS";
  6. static char *srmsg = "server refused authentication";
  7. static char *sgmsg = "server gave up";
  8. int
  9. auth(int fd, uchar *secret)
  10. {
  11. int n, afd;
  12. int rv;
  13. char trbuf[TICKREQLEN];
  14. char tbuf[2*TICKETLEN+AUTHENTLEN];
  15. char ebuf[ERRLEN];
  16. Ticketreq tr;
  17. ebuf[0] = 0;
  18. errstr(ebuf);
  19. /* add uid and local hostid to ticket request */
  20. if(_asreadn(fd, trbuf, TICKREQLEN) < 0){
  21. werrstr(badreq);
  22. return -1;
  23. }
  24. convM2TR(trbuf, &tr);
  25. if(tr.type != AuthTreq){
  26. werrstr(badreq);
  27. return -1;
  28. }
  29. memset(tr.uid, 0, sizeof(tr.uid));
  30. strcpy(tr.uid, getuser());
  31. memset(tr.hostid, 0, sizeof(tr.hostid));
  32. _asrdfile("/dev/hostowner", tr.hostid, NAMELEN);
  33. convTR2M(&tr, trbuf);
  34. /* get ticket */
  35. afd = authdial();
  36. if(afd < 0){
  37. werrstr(ccmsg);
  38. return -1;
  39. }
  40. rv = _asgetticket(afd, trbuf, tbuf);
  41. close(afd);
  42. if(rv)
  43. return -1;
  44. ebuf[0] = 0;
  45. errstr(ebuf);
  46. /* get authenticator */
  47. afd = open("/dev/authenticator", ORDWR);
  48. if(afd < 0){
  49. werrstr("/dev/authenticator: %r");
  50. return -1;
  51. }
  52. ebuf[0] = 0;
  53. errstr(ebuf);
  54. if(write(afd, tbuf, TICKETLEN) < 0){
  55. werrstr("writing /dev/authenticator: %r");
  56. return -1;
  57. }
  58. ebuf[0] = 0;
  59. errstr(ebuf);
  60. if(read(afd, tbuf+2*TICKETLEN, AUTHENTLEN) < 0){
  61. werrstr("reading /dev/authenticator: %r");
  62. return -1;
  63. }
  64. ebuf[0] = 0;
  65. errstr(ebuf);
  66. /* write server ticket to server */
  67. if(write(fd, tbuf+TICKETLEN, TICKETLEN+AUTHENTLEN) < 0){
  68. werrstr("%s:%r", srmsg);
  69. return -1;
  70. }
  71. ebuf[0] = 0;
  72. errstr(ebuf);
  73. /* get authenticator from server and check */
  74. if(_asreadn(fd, tbuf+TICKETLEN, AUTHENTLEN) < 0){
  75. werrstr(sgmsg);
  76. return -1;
  77. }
  78. ebuf[0] = 0;
  79. errstr(ebuf);
  80. afd = open("/dev/authcheck", ORDWR);
  81. if(afd < 0){
  82. werrstr("authcheck: %r");
  83. return -1;
  84. }
  85. n = write(afd, tbuf, TICKETLEN+AUTHENTLEN);
  86. close(afd);
  87. if(n < 0){
  88. memset(tbuf, 0, AUTHENTLEN);
  89. if(memcmp(tbuf, tbuf+TICKETLEN, AUTHENTLEN) == 0)
  90. werrstr("refused by server");
  91. else
  92. werrstr("server lies");
  93. return -1;
  94. }
  95. if (secret){
  96. decrypt(secret, tbuf, TICKETLEN);
  97. des56to64((uchar*)(tbuf+TICKETLEN-DESKEYLEN), secret);
  98. }
  99. return 0;
  100. }