pass.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. /*
  10. * This is just a repository for a password.
  11. * We don't want to encourage this, there's
  12. * no server side.
  13. */
  14. #include "dat.h"
  15. typedef struct State State;
  16. struct State
  17. {
  18. Key *key;
  19. };
  20. enum
  21. {
  22. HavePass,
  23. Maxphase,
  24. };
  25. static int
  26. passinit(Proto *p, Fsstate *fss)
  27. {
  28. int ret;
  29. Key *k;
  30. Keyinfo ki;
  31. State *s;
  32. ret = findkey(&k, mkkeyinfo(&ki, fss, nil), "%s", p->keyprompt);
  33. if(ret != RpcOk)
  34. return ret;
  35. setattrs(fss->attr, k->attr);
  36. s = emalloc(sizeof(*s));
  37. s->key = k;
  38. fss->ps = s;
  39. fss->phase = HavePass;
  40. return RpcOk;
  41. }
  42. static void
  43. passclose(Fsstate *fss)
  44. {
  45. State *s;
  46. s = fss->ps;
  47. if(s->key)
  48. closekey(s->key);
  49. free(s);
  50. }
  51. static int
  52. passread(Fsstate *fss, void *va, uint *n)
  53. {
  54. int m;
  55. char buf[500];
  56. char *pass, *user;
  57. State *s;
  58. s = fss->ps;
  59. switch(fss->phase){
  60. default:
  61. return phaseerror(fss, "read");
  62. case HavePass:
  63. user = _strfindattr(s->key->attr, "user");
  64. pass = _strfindattr(s->key->privattr, "!password");
  65. if(user==nil || pass==nil)
  66. return failure(fss, "passread cannot happen");
  67. snprint(buf, sizeof buf, "%q %q", user, pass);
  68. m = strlen(buf);
  69. if(m > *n)
  70. return toosmall(fss, m);
  71. *n = m;
  72. memmove(va, buf, m);
  73. return RpcOk;
  74. }
  75. }
  76. static int
  77. passwrite(Fsstate *fss, void* v, uint u)
  78. {
  79. return phaseerror(fss, "write");
  80. }
  81. Proto pass =
  82. {
  83. .name= "pass",
  84. .init= passinit,
  85. .write= passwrite,
  86. .read= passread,
  87. .close= passclose,
  88. .addkey= replacekey,
  89. .keyprompt= "user? !password?",
  90. };