auth_respond.c 1.8 KB

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