auth.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 <fcall.h>
  13. #include <thread.h>
  14. #include <9p.h>
  15. typedef struct Afid Afid;
  16. struct Afid
  17. {
  18. AuthRpc *rpc;
  19. char *uname;
  20. char *aname;
  21. int authok;
  22. int afd;
  23. };
  24. static uint64_t authgen = 1ULL<<63;
  25. void
  26. auth9p(Req *r)
  27. {
  28. char *spec;
  29. Afid *afid;
  30. afid = emalloc9p(sizeof(Afid));
  31. afid->afd = open("/mnt/factotum/rpc", ORDWR);
  32. if(afid->afd < 0)
  33. goto error;
  34. if((afid->rpc = auth_allocrpc(afid->afd)) == nil)
  35. goto error;
  36. if(r->ifcall.uname[0] == 0)
  37. goto error;
  38. afid->uname = estrdup9p(r->ifcall.uname);
  39. afid->aname = estrdup9p(r->ifcall.aname);
  40. spec = r->srv->keyspec;
  41. if(spec == nil)
  42. spec = "proto=p9any role=server";
  43. if(auth_rpc(afid->rpc, "start", spec, strlen(spec)) != ARok)
  44. goto error;
  45. r->afid->qid.type = QTAUTH;
  46. r->afid->qid.path = ++authgen;
  47. r->afid->qid.vers = 0;
  48. r->afid->omode = ORDWR;
  49. r->ofcall.qid = r->afid->qid;
  50. r->afid->aux = afid;
  51. respond(r, nil);
  52. return;
  53. error:
  54. if(afid->rpc)
  55. auth_freerpc(afid->rpc);
  56. if(afid->uname)
  57. free(afid->uname);
  58. if(afid->aname)
  59. free(afid->aname);
  60. if(afid->afd >= 0)
  61. close(afid->afd);
  62. free(afid);
  63. responderror(r);
  64. }
  65. static int
  66. _authread(Afid *afid, void *data, int count)
  67. {
  68. AuthInfo *ai;
  69. switch(auth_rpc(afid->rpc, "read", nil, 0)){
  70. case ARdone:
  71. ai = auth_getinfo(afid->rpc);
  72. if(ai == nil)
  73. return -1;
  74. auth_freeAI(ai);
  75. if(chatty9p)
  76. fprint(2, "authenticate %s/%s: ok\n", afid->uname, afid->aname);
  77. afid->authok = 1;
  78. return 0;
  79. case ARok:
  80. if(count < afid->rpc->narg){
  81. werrstr("authread count too small");
  82. return -1;
  83. }
  84. count = afid->rpc->narg;
  85. memmove(data, afid->rpc->arg, count);
  86. return count;
  87. case ARphase:
  88. default:
  89. werrstr("authrpc botch");
  90. return -1;
  91. }
  92. }
  93. void
  94. authread(Req *r)
  95. {
  96. int n;
  97. Afid *afid;
  98. Fid *fid;
  99. fid = r->fid;
  100. afid = fid->aux;
  101. if(afid == nil || r->fid->qid.type != QTAUTH){
  102. respond(r, "not an auth fid");
  103. return;
  104. }
  105. n = _authread(afid, r->ofcall.data, r->ifcall.count);
  106. if(n < 0){
  107. responderror(r);
  108. return;
  109. }
  110. r->ofcall.count = n;
  111. respond(r, nil);
  112. }
  113. void
  114. authwrite(Req *r)
  115. {
  116. Afid *afid;
  117. Fid *fid;
  118. fid = r->fid;
  119. afid = fid->aux;
  120. if(afid == nil || r->fid->qid.type != QTAUTH){
  121. respond(r, "not an auth fid");
  122. return;
  123. }
  124. if(auth_rpc(afid->rpc, "write", r->ifcall.data, r->ifcall.count) != ARok){
  125. responderror(r);
  126. return;
  127. }
  128. r->ofcall.count = r->ifcall.count;
  129. respond(r, nil);
  130. }
  131. void
  132. authdestroy(Fid *fid)
  133. {
  134. Afid *afid;
  135. if((fid->qid.type & QTAUTH) && (afid = fid->aux) != nil){
  136. if(afid->rpc)
  137. auth_freerpc(afid->rpc);
  138. close(afid->afd);
  139. free(afid->uname);
  140. free(afid->aname);
  141. free(afid);
  142. fid->aux = nil;
  143. }
  144. }
  145. int
  146. authattach(Req *r)
  147. {
  148. Afid *afid;
  149. char buf[ERRMAX];
  150. if(r->afid == nil){
  151. respond(r, "not authenticated");
  152. return -1;
  153. }
  154. afid = r->afid->aux;
  155. if((r->afid->qid.type&QTAUTH) == 0 || afid == nil){
  156. respond(r, "not an auth fid");
  157. return -1;
  158. }
  159. if(!afid->authok){
  160. if(_authread(afid, buf, 0) < 0){
  161. responderror(r);
  162. return -1;
  163. }
  164. }
  165. if(strcmp(afid->uname, r->ifcall.uname) != 0){
  166. snprint(buf, sizeof buf, "auth uname mismatch: %s vs %s",
  167. afid->uname, r->ifcall.uname);
  168. respond(r, buf);
  169. return -1;
  170. }
  171. if(strcmp(afid->aname, r->ifcall.aname) != 0){
  172. snprint(buf, sizeof buf, "auth aname mismatch: %s vs %s",
  173. afid->aname, r->ifcall.aname);
  174. respond(r, buf);
  175. return -1;
  176. }
  177. return 0;
  178. }