nfsmount.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. #include "all.h"
  2. /*
  3. * Cf. /lib/rfc/rfc1094
  4. */
  5. static int mntnull(int, Rpccall*, Rpccall*);
  6. static int mntmnt(int, Rpccall*, Rpccall*);
  7. static int mntdump(int, Rpccall*, Rpccall*);
  8. static int mntumnt(int, Rpccall*, Rpccall*);
  9. static int mntumntall(int, Rpccall*, Rpccall*);
  10. static int mntexport(int, Rpccall*, Rpccall*);
  11. Procmap mntproc[] = {
  12. 0, mntnull,
  13. 1, mntmnt,
  14. 2, mntdump,
  15. 3, mntumnt,
  16. 4, mntumntall,
  17. 5, mntexport,
  18. 0, 0
  19. };
  20. long starttime;
  21. static int noauth;
  22. char * config;
  23. Session * head;
  24. Session * tail;
  25. int staletime = 10*60;
  26. void
  27. mnttimer(long now)
  28. {
  29. Session *s;
  30. for(s=head; s; s=s->next)
  31. fidtimer(s, now);
  32. }
  33. void
  34. mntinit(int argc, char **argv)
  35. {
  36. char *argv0 = 0;
  37. int tries;
  38. config = "config";
  39. starttime = time(0);
  40. clog("nfs mount server init, starttime = %lud\n", starttime);
  41. tries = 0;
  42. ARGBEGIN{
  43. case 'a':
  44. ++tries;
  45. srvinit(-1, 0, ARGF());
  46. break;
  47. case 'f':
  48. ++tries;
  49. srvinit(-1, ARGF(), 0);
  50. break;
  51. case 's':
  52. ++tries;
  53. srvinit(1, 0, 0);
  54. break;
  55. case 'n':
  56. ++noauth;
  57. break;
  58. case 'c':
  59. config = ARGF();
  60. break;
  61. case 'T':
  62. staletime = atoi(ARGF());
  63. break;
  64. }ARGEND
  65. noauth=1; /* ZZZ */
  66. USED(argv0);
  67. if(tries == 0 && head == 0)
  68. srvinit(-1, 0, "il!bootes");
  69. if(head == 0)
  70. panic("can't initialize services");
  71. readunixidmaps(config);
  72. }
  73. void
  74. srvinit(int fd, char *file, char *addr)
  75. {
  76. char fdservice[16], *naddr;
  77. Session *s;
  78. Xfile *xp;
  79. Xfid *xf;
  80. Fid *f;
  81. s = calloc(1, sizeof(Session));
  82. s->spec = "";
  83. s->fd = -1;
  84. if(fd >= 0){
  85. s->fd = fd;
  86. sprint(fdservice, "/fd/%d", s->fd);
  87. s->service = strstore(fdservice);
  88. chat("fd = %d\n", s->fd);
  89. }else if(file){
  90. chat("file = \"%s\"\n", file);
  91. s->service = file;
  92. s->fd = open(file, ORDWR);
  93. if(s->fd < 0){
  94. clog("can't open %s: %r\n", file);
  95. goto error;
  96. }
  97. }else if(addr){
  98. chat("addr = \"%s\"\n", addr);
  99. naddr = netmkaddr(addr, 0, "9fs");
  100. s->service = addr;
  101. s->fd = dial(naddr, 0, 0, 0);
  102. if(s->fd < 0){
  103. clog("can't dial %s: %r\n", naddr);
  104. goto error;
  105. }
  106. }
  107. chat("version...");
  108. s->tag = NOTAG-1;
  109. s->f.msize = Maxfdata+IOHDRSZ;
  110. s->f.version = "9P2000";
  111. xmesg(s, Tversion);
  112. messagesize = IOHDRSZ+s->f.msize;
  113. chat("version spec %s size %d\n", s->f.version, s->f.msize);
  114. /*
  115. chat("session...");
  116. s->tag = NOTAG-1;
  117. srand(starttime);
  118. for(i = 0; i < CHALLEN; i++)
  119. s->cchal[i] = nrand(256);
  120. memmove(s->f.chal, s->cchal, CHALLEN);
  121. if(xmesg(s, Tsession)){
  122. clog("session failed\n");
  123. goto error;
  124. }
  125. memmove(s->schal, s->f.chal, CHALLEN);
  126. memmove(s->authid, s->f.authid, NAMELEN);
  127. memmove(s->authdom, s->f.authdom, DOMLEN);
  128. for(i = 0; i < CHALLEN; i++)
  129. if(s->schal[i])
  130. break;
  131. if(i >= CHALLEN || noauth){
  132. chat("no schal...");
  133. ++s->noauth;
  134. }
  135. */
  136. chat("attach...");
  137. s->tag = 0;
  138. f = newfid(s);
  139. s->f.fid = f - s->fids;
  140. s->f.afid = ~0x0UL;
  141. s->f.uname = "none";
  142. s->f.aname = s->spec;
  143. if(xmesg(s, Tattach)){
  144. clog("attach failed\n");
  145. goto error;
  146. }
  147. s->count = 1;
  148. xp = xfile(&s->f.qid, s, 1);
  149. s->root = xp;
  150. xp->parent = xp;
  151. xp->name = "/";
  152. xf = xfid("none", xp, 1);
  153. xf->urfid = f;
  154. clog("service=%s uid=%s fid=%ld\n",
  155. s->service, xf->uid, xf->urfid-s->fids);
  156. if(tail)
  157. tail->next = s;
  158. else
  159. head = s;
  160. tail = s;
  161. return;
  162. error:
  163. if(s->fd >= 0)
  164. close(s->fd);
  165. free(s);
  166. }
  167. static int
  168. mntnull(int n, Rpccall *cmd, Rpccall *reply)
  169. {
  170. USED(n, cmd, reply);
  171. chat("mntnull\n");
  172. return 0;
  173. }
  174. static char*
  175. Str2str(String s, char *buf, int nbuf)
  176. {
  177. int i;
  178. i = s.n;
  179. if(i >= nbuf)
  180. i = nbuf-1;
  181. memmove(buf, s.s, i);
  182. buf[i] = 0;
  183. return buf;
  184. }
  185. static int
  186. mntmnt(int n, Rpccall *cmd, Rpccall *reply)
  187. {
  188. int i;
  189. char dom[64];
  190. uchar *argptr = cmd->args;
  191. uchar *dataptr = reply->results;
  192. Authunix au;
  193. Xfile *xp;
  194. String root;
  195. chat("mntmnt...\n");
  196. if(n < 8)
  197. return garbage(reply, "n too small");
  198. argptr += string2S(argptr, &root);
  199. if(argptr != &((uchar *)cmd->args)[n])
  200. return garbage(reply, "bad count");
  201. clog("host=%I, port=%ld, root=\"%.*s\"...",
  202. cmd->host, cmd->port, utfnlen(root.s, root.n), root.s);
  203. if(auth2unix(&cmd->cred, &au) != 0){
  204. chat("auth flavor=%ld, count=%ld\n",
  205. cmd->cred.flavor, cmd->cred.count);
  206. for(i=0; i<cmd->cred.count; i++)
  207. chat(" %.2ux", ((uchar *)cmd->cred.data)[i]);
  208. chat("\n");
  209. clog("auth: bad credentials");
  210. return error(reply, 1);
  211. }
  212. clog("auth: %ld %.*s u=%ld g=%ld",
  213. au.stamp, utfnlen(au.mach.s, au.mach.n), au.mach.s, au.uid, au.gid);
  214. for(i=0; i<au.gidlen; i++)
  215. chat(", %ld", au.gids[i]);
  216. chat("...");
  217. if(getdom(cmd->host, dom, sizeof(dom))<0){
  218. clog("auth: unknown ip address");
  219. return error(reply, 1);
  220. }
  221. chat("dom=%s...", dom);
  222. xp = xfroot(root.s, root.n);
  223. if(xp == 0){
  224. chat("xp=0...");
  225. clog("mntmnt: no fs");
  226. return error(reply, 3);
  227. }
  228. PLONG(0);
  229. dataptr += xp2fhandle(xp, dataptr);
  230. chat("OK\n");
  231. return dataptr - (uchar *)reply->results;
  232. }
  233. static int
  234. mntdump(int n, Rpccall *cmd, Rpccall *reply)
  235. {
  236. if(n != 0)
  237. return garbage(reply, "mntdump");
  238. USED(cmd);
  239. chat("mntdump...");
  240. return error(reply, FALSE);
  241. }
  242. static int
  243. mntumnt(int n, Rpccall *cmd, Rpccall *reply)
  244. {
  245. if(n <= 0)
  246. return garbage(reply, "mntumnt");
  247. USED(cmd);
  248. chat("mntumnt\n");
  249. return 0;
  250. }
  251. static int
  252. mntumntall(int n, Rpccall *cmd, Rpccall *reply)
  253. {
  254. if(n != 0)
  255. return garbage(reply, "mntumntall");
  256. USED(cmd);
  257. chat("mntumntall\n");
  258. return 0;
  259. }
  260. static int
  261. mntexport(int n, Rpccall *cmd, Rpccall *reply)
  262. {
  263. uchar *dataptr = reply->results;
  264. Authunix au;
  265. int i;
  266. chat("mntexport...");
  267. if(n != 0)
  268. return garbage(reply, "mntexport");
  269. if(auth2unix(&cmd->cred, &au) != 0){
  270. chat("auth flavor=%ld, count=%ld\n",
  271. cmd->cred.flavor, cmd->cred.count);
  272. for(i=0; i<cmd->cred.count; i++)
  273. chat(" %.2ux", ((uchar *)cmd->cred.data)[i]);
  274. chat("...");
  275. au.mach.n = 0;
  276. }else
  277. chat("%ld@%.*s...", au.uid, utfnlen(au.mach.s, au.mach.n), au.mach.s);
  278. PLONG(TRUE);
  279. PLONG(1);
  280. PPTR("/", 1);
  281. if(au.mach.n > 0){
  282. PLONG(TRUE);
  283. PLONG(au.mach.n);
  284. PPTR(au.mach.s, au.mach.n);
  285. }
  286. PLONG(FALSE);
  287. PLONG(FALSE);
  288. chat("OK\n");
  289. return dataptr - (uchar *)reply->results;
  290. }
  291. Xfile *
  292. xfroot(char *name, int n)
  293. {
  294. Session *s;
  295. char *p;
  296. if(n <= 0)
  297. n = strlen(name);
  298. chat("xfroot: %.*s...", utfnlen(name, n), name);
  299. if(n == 1 && name[0] == '/')
  300. return head->root;
  301. for(s=head; s; s=s->next){
  302. if(strncmp(name, s->service, n) == 0)
  303. return s->root;
  304. p = strrchr(s->service, '!'); /* for -a il!foo */
  305. if(p && strncmp(name, p+1, n) == 0)
  306. return s->root;
  307. p = strrchr(s->service, '/'); /* for -a /srv/foo */
  308. if(p && strncmp(name, p+1, n) == 0)
  309. return s->root;
  310. }
  311. return 0;
  312. }