nfsmount.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /* Copyright © 2003 Russ Cox, MIT; see /sys/src/libsunrpc/COPYING */
  2. #include <u.h>
  3. #include <libc.h>
  4. #include <thread.h>
  5. #include <sunrpc.h>
  6. #include <nfs3.h>
  7. int chatty;
  8. SunClient *client;
  9. void
  10. usage(void)
  11. {
  12. fprint(2, "usage: nfsmount address [cmd]\n"
  13. "cmd is one of:\n"
  14. "\tnull\n"
  15. "\tmnt path\n"
  16. "\tdump\n"
  17. "\tumnt path\n"
  18. "\tumntall\n"
  19. "\texport (default)\n");
  20. threadexitsall("usage");
  21. }
  22. void
  23. portCall(SunCall *c, PortCallType type)
  24. {
  25. c->rpc.prog = PortProgram;
  26. c->rpc.vers = PortVersion;
  27. c->rpc.proc = type>>1;
  28. c->rpc.iscall = !(type&1);
  29. c->type = type;
  30. }
  31. int
  32. getport(SunClient *client, uint prog, uint vers, uint prot, uint *port)
  33. {
  34. PortTGetport tx;
  35. PortRGetport rx;
  36. memset(&tx, 0, sizeof tx);
  37. portCall(&tx.call, PortCallTGetport);
  38. tx.map.prog = prog;
  39. tx.map.vers = vers;
  40. tx.map.prot = prot;
  41. memset(&rx, 0, sizeof rx);
  42. portCall(&rx.call, PortCallRGetport);
  43. if(sunClientRpc(client, 0, &tx.call, &rx.call, nil) < 0)
  44. return -1;
  45. *port = rx.port;
  46. return 0;
  47. }
  48. uchar unixauth[] = {
  49. 0x12, 0x23, 0x34, 0x45, /* stamp */
  50. 0x00, 0x00, 0x00, 0x04, /* gnot */
  51. 0x67, 0x6e, 0x6f, 0x74,
  52. 0x00, 0x00, 0x03, 0xE9, /* 1001 */
  53. 0x00, 0x00, 0x03, 0xE9, /* 1001 */
  54. 0x00, 0x00, 0x00, 0x00, /* gid list */
  55. };
  56. void
  57. mountCall(SunCall *c, NfsMount3CallType type)
  58. {
  59. c->rpc.prog = NfsMount3Program;
  60. c->rpc.vers = NfsMount3Version;
  61. c->rpc.proc = type>>1;
  62. c->rpc.iscall = !(type&1);
  63. if(c->rpc.iscall){
  64. c->rpc.cred.flavor = SunAuthSys;
  65. c->rpc.cred.data = unixauth;
  66. c->rpc.cred.ndata = sizeof unixauth;
  67. }
  68. c->type = type;
  69. }
  70. void
  71. tnull(char **argv)
  72. {
  73. NfsMount3TNull tx;
  74. NfsMount3RNull rx;
  75. USED(argv);
  76. memset(&tx, 0, sizeof tx);
  77. mountCall(&tx.call, NfsMount3CallTNull);
  78. memset(&rx, 0, sizeof rx);
  79. mountCall(&rx.call, NfsMount3CallRNull);
  80. if(sunClientRpc(client, 0, &tx.call, &rx.call, nil) < 0)
  81. sysfatal("rpc: %r");
  82. }
  83. void
  84. tmnt(char **argv)
  85. {
  86. int i;
  87. NfsMount3TMnt tx;
  88. NfsMount3RMnt rx;
  89. memset(&tx, 0, sizeof tx);
  90. mountCall(&tx.call, NfsMount3CallTMnt);
  91. tx.path = argv[0];
  92. memset(&rx, 0, sizeof rx);
  93. mountCall(&rx.call, NfsMount3CallRMnt);
  94. if(sunClientRpc(client, 0, &tx.call, &rx.call, nil) < 0)
  95. sysfatal("rpc: %r");
  96. if(rx.status != 0){
  97. nfs3Errstr(rx.status);
  98. sysfatal("mnt: %r");
  99. }
  100. print("handle %.*H\n", rx.len, rx.handle);
  101. print("auth:");
  102. for(i=0; i<rx.nauth; i++)
  103. print(" %ud", (uint)rx.auth[i]);
  104. print("\n");
  105. }
  106. void
  107. tdump(char **argv)
  108. {
  109. uchar *p, *ep;
  110. NfsMount3TDump tx;
  111. NfsMount3RDump rx;
  112. NfsMount3Entry e;
  113. memset(&tx, 0, sizeof tx);
  114. mountCall(&tx.call, NfsMount3CallTDump);
  115. USED(argv);
  116. memset(&rx, 0, sizeof rx);
  117. mountCall(&rx.call, NfsMount3CallRDump);
  118. if(sunClientRpc(client, 0, &tx.call, &rx.call, nil) < 0)
  119. sysfatal("rpc: %r");
  120. p = rx.data;
  121. ep = p+rx.count;
  122. while(p < ep){
  123. if(nfsMount3EntryUnpack(p, ep, &p, &e) < 0)
  124. sysfatal("unpack entry structure failed");
  125. print("%s %s\n", e.host, e.path);
  126. }
  127. }
  128. void
  129. tumnt(char **argv)
  130. {
  131. NfsMount3TUmnt tx;
  132. NfsMount3RUmnt rx;
  133. memset(&tx, 0, sizeof tx);
  134. mountCall(&tx.call, NfsMount3CallTUmnt);
  135. tx.path = argv[0];
  136. memset(&rx, 0, sizeof rx);
  137. mountCall(&rx.call, NfsMount3CallRUmnt);
  138. if(sunClientRpc(client, 0, &tx.call, &rx.call, nil) < 0)
  139. sysfatal("rpc: %r");
  140. print("\n");
  141. }
  142. void
  143. tumntall(char **argv)
  144. {
  145. NfsMount3TUmntall tx;
  146. NfsMount3RUmntall rx;
  147. memset(&tx, 0, sizeof tx);
  148. mountCall(&tx.call, NfsMount3CallTUmntall);
  149. USED(argv);
  150. memset(&rx, 0, sizeof rx);
  151. mountCall(&rx.call, NfsMount3CallRUmntall);
  152. if(sunClientRpc(client, 0, &tx.call, &rx.call, nil) < 0)
  153. sysfatal("rpc: %r");
  154. print("\n");
  155. }
  156. void
  157. texport(char **argv)
  158. {
  159. uchar *p, *ep, *tofree;
  160. char **g, **gg;
  161. int ng, i, n;
  162. NfsMount3TDump tx;
  163. NfsMount3RDump rx;
  164. NfsMount3Export e;
  165. memset(&tx, 0, sizeof tx);
  166. mountCall(&tx.call, NfsMount3CallTExport);
  167. USED(argv);
  168. memset(&rx, 0, sizeof rx);
  169. mountCall(&rx.call, NfsMount3CallRExport);
  170. if(sunClientRpc(client, 0, &tx.call, &rx.call, &tofree) < 0)
  171. sysfatal("rpc: %r");
  172. p = rx.data;
  173. ep = p+rx.count;
  174. g = nil;
  175. ng = 0;
  176. while(p < ep){
  177. n = nfsMount3ExportGroupSize(p);
  178. if(n > ng){
  179. ng = n;
  180. g = erealloc(g, sizeof(g[0])*ng);
  181. }
  182. if(nfsMount3ExportUnpack(p, ep, &p, g, &gg, &e) < 0)
  183. sysfatal("unpack export structure failed");
  184. print("%s", e.path);
  185. for(i=0; i<e.ng; i++)
  186. print(" %s", e.g[i]);
  187. print("\n");
  188. }
  189. free(tofree);
  190. }
  191. static struct {
  192. char *cmd;
  193. int narg;
  194. void (*fn)(char**);
  195. } tab[] = {
  196. "null", 0, tnull,
  197. "mnt", 1, tmnt,
  198. "dump", 0, tdump,
  199. "umnt", 1, tumnt,
  200. "umntall", 1, tumntall,
  201. "export", 0, texport,
  202. };
  203. char*
  204. netchangeport(char *addr, char *port)
  205. {
  206. static char buf[256];
  207. char *r;
  208. strecpy(buf, buf+sizeof buf, addr);
  209. r = strrchr(buf, '!');
  210. if(r == nil)
  211. return nil;
  212. r++;
  213. strecpy(r, buf+sizeof buf, port);
  214. return buf;
  215. }
  216. void
  217. threadmain(int argc, char **argv)
  218. {
  219. char *dflt[] = { "export", };
  220. char *addr, *cmd;
  221. int i, proto;
  222. uint port;
  223. char buf[32];
  224. int mapit;
  225. mapit = 1;
  226. ARGBEGIN{
  227. case 'R':
  228. chatty++;
  229. break;
  230. case 'm':
  231. mapit = 0;
  232. break;
  233. }ARGEND
  234. if(argc < 1)
  235. usage();
  236. fmtinstall('B', sunRpcFmt);
  237. fmtinstall('C', sunCallFmt);
  238. fmtinstall('H', encodefmt);
  239. sunFmtInstall(&portProg);
  240. sunFmtInstall(&nfsMount3Prog);
  241. addr = netmkaddr(argv[0], "udp", "portmap");
  242. if(mapit){
  243. /* translate with port mapper */
  244. fprint(2, "connecting to %s\n", addr);
  245. if((client = sunDial(addr)) == nil)
  246. sysfatal("dial %s: %r", addr);
  247. client->chatty = chatty;
  248. sunClientProg(client, &portProg);
  249. if(strstr(addr, "udp!"))
  250. proto = PortProtoUdp;
  251. else
  252. proto = PortProtoTcp;
  253. if(getport(client, NfsMount3Program, NfsMount3Version, proto, &port) < 0)
  254. sysfatal("getport: %r");
  255. snprint(buf, sizeof buf, "%ud!r", port);
  256. addr = netchangeport(addr, buf);
  257. sunClientClose(client);
  258. }
  259. fprint(2, "connecting to %s\n", addr);
  260. if((client = sunDial(addr)) == nil)
  261. sysfatal("dial %s: %r", addr);
  262. client->chatty = chatty;
  263. sunClientProg(client, &nfsMount3Prog);
  264. argv++;
  265. argc--;
  266. if(argc == 0){
  267. argc = 1;
  268. argv = dflt;
  269. }
  270. cmd = argv[0];
  271. argv++;
  272. argc--;
  273. for(i=0; i<nelem(tab); i++){
  274. if(strcmp(tab[i].cmd, cmd) == 0){
  275. if(tab[i].narg != argc)
  276. usage();
  277. (*tab[i].fn)(argv);
  278. threadexitsall(nil);
  279. }
  280. }
  281. usage();
  282. }