auth_challenge.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <auth.h>
  4. #include <authsrv.h>
  5. #include "authlocal.h"
  6. Chalstate*
  7. auth_challenge(char *fmt, ...)
  8. {
  9. char *p;
  10. va_list arg;
  11. Chalstate *c;
  12. quotefmtinstall(); /* just in case */
  13. va_start(arg, fmt);
  14. p = vsmprint(fmt, arg);
  15. va_end(arg);
  16. if(p == nil)
  17. return nil;
  18. c = mallocz(sizeof(*c), 1);
  19. if(c == nil){
  20. free(p);
  21. return nil;
  22. }
  23. if((c->afd = open("/mnt/factotum/rpc", ORDWR)) < 0){
  24. Error:
  25. auth_freechal(c);
  26. free(p);
  27. return nil;
  28. }
  29. if((c->rpc=auth_allocrpc(c->afd)) == nil
  30. || auth_rpc(c->rpc, "start", p, strlen(p)) != ARok
  31. || auth_rpc(c->rpc, "read", nil, 0) != ARok)
  32. goto Error;
  33. if(c->rpc->narg > sizeof(c->chal)-1){
  34. werrstr("buffer too small for challenge");
  35. goto Error;
  36. }
  37. memmove(c->chal, c->rpc->arg, c->rpc->narg);
  38. c->nchal = c->rpc->narg;
  39. free(p);
  40. return c;
  41. }
  42. AuthInfo*
  43. auth_response(Chalstate *c)
  44. {
  45. int ret;
  46. AuthInfo *ai;
  47. ai = nil;
  48. if(c->afd < 0){
  49. werrstr("auth_response: connection not open");
  50. return nil;
  51. }
  52. if(c->resp == nil){
  53. werrstr("auth_response: nil response");
  54. return nil;
  55. }
  56. if(c->nresp == 0){
  57. werrstr("auth_response: unspecified response length");
  58. return nil;
  59. }
  60. if(c->user){
  61. if(auth_rpc(c->rpc, "write", c->user, strlen(c->user)) != ARok){
  62. /*
  63. * if this fails we're out of phase with factotum.
  64. * give up.
  65. */
  66. goto Out;
  67. }
  68. }
  69. if(auth_rpc(c->rpc, "write", c->resp, c->nresp) != ARok){
  70. /*
  71. * don't close the connection -- maybe we'll try again.
  72. */
  73. return nil;
  74. }
  75. switch(ret = auth_rpc(c->rpc, "read", nil, 0)){
  76. case ARok:
  77. default:
  78. werrstr("factotum protocol botch %d %s", ret, c->rpc->ibuf);
  79. break;
  80. case ARdone:
  81. ai = auth_getinfo(c->rpc);
  82. break;
  83. }
  84. Out:
  85. close(c->afd);
  86. auth_freerpc(c->rpc);
  87. c->afd = -1;
  88. c->rpc = nil;
  89. return ai;
  90. }
  91. void
  92. auth_freechal(Chalstate *c)
  93. {
  94. if(c == nil)
  95. return;
  96. if(c->afd >= 0)
  97. close(c->afd);
  98. if(c->rpc != nil)
  99. auth_freerpc(c->rpc);
  100. memset(c, 0xBB, sizeof(*c));
  101. free(c);
  102. }