9auth.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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(t->afid == NOFID){
  58. /*
  59. * If no authentication is asked for, allow
  60. * "none" provided the connection has already
  61. * been authenticatated.
  62. *
  63. * The console is allowed to attach without
  64. * authentication.
  65. */
  66. if(!fid->con->isconsole &&
  67. (strcmp(fid->uname, unamenone) != 0 || !fid->con->aok)){
  68. consPrint("attach %s as %s: connection not authenticated, not console\n", fsysGetName(fsys), fid->uname);
  69. return 0;
  70. }
  71. if((fid->uid = uidByUname(fid->uname)) == nil){
  72. consPrint("attach %s as %s: unknown uname\n", fsysGetName(fsys), fid->uname);
  73. return 0;
  74. }
  75. return 1;
  76. }
  77. if((afid = fidGet(fid->con, t->afid, 0)) == nil){
  78. consPrint("attach %s as %s: bad afid\n", fsysGetName(fsys), fid->uname);
  79. return 0;
  80. }
  81. /*
  82. * Check valid afid;
  83. * check uname and aname match.
  84. */
  85. if(!(afid->qid.type & QTAUTH)){
  86. consPrint("attach %s as %s: afid not an auth file\n", fsysGetName(fsys), fid->uname);
  87. fidPut(afid);
  88. return 0;
  89. }
  90. if(strcmp(afid->uname, fid->uname) != 0 || afid->fsys != fsys){
  91. consPrint("attach %s as %s: afid is for %s as %s\n", fsysGetName(fsys), fid->uname, fsysGetName(afid->fsys), afid->uname);
  92. fidPut(afid);
  93. return 0;
  94. }
  95. vtLock(afid->alock);
  96. if(afid->cuname == nil){
  97. if(authRead(afid, buf, 0) != 0 || afid->cuname == nil){
  98. vtUnlock(afid->alock);
  99. consPrint("attach %s as %s: auth protocol not finished\n", fsysGetName(fsys), fid->uname);
  100. fidPut(afid);
  101. return 0;
  102. }
  103. }
  104. vtUnlock(afid->alock);
  105. assert(fid->uid == nil);
  106. if((fid->uid = uidByUname(afid->cuname)) == nil){
  107. consPrint("attach %s as %s: unknown cuname %s\n", fsysGetName(fsys), fid->uname, afid->cuname);
  108. fidPut(afid);
  109. return 0;
  110. }
  111. vtMemFree(fid->uname);
  112. fid->uname = vtStrDup(afid->cuname);
  113. fidPut(afid);
  114. /*
  115. * Allow "none" once the connection has been authenticated.
  116. */
  117. fid->con->aok = 1;
  118. return 1;
  119. }