9auth.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include "stdinc.h"
  2. #include "9.h"
  3. int
  4. authRead(Fid* afid, void* data, int count)
  5. {
  6. AuthInfo *ai;
  7. AuthRpc *rpc;
  8. if((rpc = afid->rpc) == nil)
  9. return -1;
  10. switch(auth_rpc(rpc, "read", nil, 0)){
  11. default:
  12. return -1;
  13. case ARdone:
  14. if((ai = auth_getinfo(rpc)) == nil)
  15. break;
  16. if(ai->cuid == nil || *ai->cuid == '\0'){
  17. auth_freeAI(ai);
  18. break;
  19. }
  20. assert(afid->cuname == nil);
  21. afid->cuname = vtStrDup(ai->cuid);
  22. auth_freeAI(ai);
  23. if(Dflag)
  24. fprint(2, "authRead cuname %s\n", afid->cuname);
  25. assert(afid->uid == nil);
  26. if((afid->uid = uidByUname(afid->cuname)) == nil)
  27. break;
  28. return 0;
  29. case ARok:
  30. if(count < rpc->narg)
  31. break;
  32. memmove(data, rpc->arg, rpc->narg);
  33. return rpc->narg;
  34. case ARphase:
  35. break;
  36. }
  37. return -1;
  38. }
  39. int
  40. authWrite(Fid* afid, void* data, int count)
  41. {
  42. assert(afid->rpc != nil);
  43. if(auth_rpc(afid->rpc, "write", data, count) != ARok)
  44. return -1;
  45. return count;
  46. }
  47. int
  48. authCheck(Fcall* t, Fid* fid, Fs* fsys)
  49. {
  50. Fid *afid;
  51. uchar buf[1];
  52. /*
  53. * Can't lookup with FidWlock here as there may be
  54. * protocol to do. Use a separate lock to protect altering
  55. * the auth information inside afid.
  56. */
  57. if((afid = fidGet(fid->con, t->afid, 0)) == nil){
  58. /*
  59. * If no authentication is asked for, allow
  60. * "none" provided the connection has already
  61. * been authenticatated.
  62. */
  63. if(strcmp(fid->uname, unamenone) == 0 && fid->con->aok){
  64. if((fid->uid = uidByUname(fid->uname)) == nil)
  65. return 0;
  66. return 1;
  67. }
  68. /*
  69. * The console is allowed to attach without
  70. * authentication.
  71. */
  72. if(!fid->con->isconsole)
  73. return 0;
  74. if((fid->uid = uidByUname(fid->uname)) == nil)
  75. return 0;
  76. return 1;
  77. }
  78. /*
  79. * Check valid afid;
  80. * check uname and aname match.
  81. */
  82. if(!(afid->qid.type & QTAUTH)){
  83. fidPut(afid);
  84. return 0;
  85. }
  86. if(strcmp(afid->uname, fid->uname) != 0 || afid->fsys != fsys){
  87. fidPut(afid);
  88. return 0;
  89. }
  90. vtLock(afid->alock);
  91. if(afid->cuname == nil){
  92. if(authRead(afid, buf, 0) != 0 || afid->cuname == nil){
  93. vtUnlock(afid->alock);
  94. fidPut(afid);
  95. return 0;
  96. }
  97. }
  98. vtUnlock(afid->alock);
  99. assert(fid->uid == nil);
  100. if((fid->uid = uidByUname(afid->cuname)) == nil){
  101. fidPut(afid);
  102. return 0;
  103. }
  104. vtMemFree(fid->uname);
  105. fid->uname = vtStrDup(afid->cuname);
  106. fidPut(afid);
  107. /*
  108. * Allow "none" once the connection has been authenticated.
  109. */
  110. fid->con->aok = 1;
  111. return 1;
  112. }