nfsmount.c 6.1 KB

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