auth_getuserpasswd.c 1.7 KB

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