auth_getuserpasswd.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <auth.h>
  4. #include "authlocal.h"
  5. enum {
  6. ARgiveup = 100,
  7. };
  8. static int
  9. dorpc(AuthRpc *rpc, char *verb, char *val, int len, AuthGetkey *getkey)
  10. {
  11. int ret;
  12. for(;;){
  13. if((ret = auth_rpc(rpc, verb, val, len)) != ARneedkey && ret != ARbadkey)
  14. return ret;
  15. if(getkey == nil)
  16. return ARgiveup; /* don't know how */
  17. if((*getkey)(rpc->arg) < 0)
  18. return ARgiveup; /* user punted */
  19. }
  20. }
  21. UserPasswd*
  22. auth_getuserpasswd(AuthGetkey *getkey, char *fmt, ...)
  23. {
  24. AuthRpc *rpc;
  25. char *f[3], *p, *params;
  26. int fd;
  27. va_list arg;
  28. UserPasswd *up;
  29. up = nil;
  30. rpc = nil;
  31. params = nil;
  32. fd = open("/mnt/factotum/rpc", ORDWR);
  33. if(fd < 0)
  34. goto out;
  35. rpc = auth_allocrpc(fd);
  36. if(rpc == nil)
  37. goto out;
  38. quotefmtinstall(); /* just in case */
  39. va_start(arg, fmt);
  40. params = vsmprint(fmt, arg);
  41. va_end(arg);
  42. if(params == nil)
  43. goto out;
  44. if(dorpc(rpc, "start", params, strlen(params), getkey) != ARok
  45. || dorpc(rpc, "read", nil, 0, getkey) != ARok)
  46. goto out;
  47. rpc->arg[rpc->narg] = '\0';
  48. if(tokenize(rpc->arg, f, 2) != 2){
  49. werrstr("bad answer from factotum");
  50. goto out;
  51. }
  52. up = malloc(sizeof(*up)+rpc->narg+1);
  53. if(up == nil)
  54. goto out;
  55. p = (char*)&up[1];
  56. strcpy(p, f[0]);
  57. up->user = p;
  58. p += strlen(p)+1;
  59. strcpy(p, f[1]);
  60. up->passwd = p;
  61. out:
  62. free(params);
  63. auth_freerpc(rpc);
  64. close(fd);
  65. return up;
  66. }