auth_respond.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <auth.h>
  4. #include <authsrv.h>
  5. #include "authlocal.h"
  6. enum {
  7. ARgiveup = 100,
  8. };
  9. static int
  10. dorpc(AuthRpc *rpc, char *verb, char *val, int len, AuthGetkey *getkey)
  11. {
  12. int ret;
  13. for(;;){
  14. if((ret = auth_rpc(rpc, verb, val, len)) != ARneedkey && ret != ARbadkey)
  15. return ret;
  16. if(getkey == nil)
  17. return ARgiveup; /* don't know how */
  18. if((*getkey)(rpc->arg) < 0)
  19. return ARgiveup; /* user punted */
  20. }
  21. }
  22. int
  23. auth_respond(void *chal, uint nchal, char *user, uint nuser, void *resp, uint nresp, AuthGetkey *getkey, char *fmt, ...)
  24. {
  25. char *p, *s;
  26. va_list arg;
  27. int afd;
  28. AuthRpc *rpc;
  29. Attr *a;
  30. if((afd = open("/mnt/factotum/rpc", ORDWR)) < 0)
  31. return -1;
  32. if((rpc = auth_allocrpc(afd)) == nil){
  33. close(afd);
  34. return -1;
  35. }
  36. quotefmtinstall(); /* just in case */
  37. va_start(arg, fmt);
  38. p = vsmprint(fmt, arg);
  39. va_end(arg);
  40. if(p==nil
  41. || dorpc(rpc, "start", p, strlen(p), getkey) != ARok
  42. || dorpc(rpc, "write", chal, nchal, getkey) != ARok
  43. || dorpc(rpc, "read", nil, 0, getkey) != ARok){
  44. free(p);
  45. close(afd);
  46. auth_freerpc(rpc);
  47. return -1;
  48. }
  49. free(p);
  50. if(rpc->narg < nresp)
  51. nresp = rpc->narg;
  52. memmove(resp, rpc->arg, nresp);
  53. if((a = auth_attr(rpc)) != nil
  54. && (s = _strfindattr(a, "user")) != nil && strlen(s) < nuser)
  55. strcpy(user, s);
  56. else if(nuser > 0)
  57. user[0] = '\0';
  58. _freeattr(a);
  59. close(afd);
  60. auth_freerpc(rpc);
  61. return nresp;
  62. }