nfsmount.c 6.3 KB

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