mport.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #include "all.h"
  2. typedef struct Rpcconn Rpcconn;
  3. struct Rpcconn
  4. {
  5. int data;
  6. int ctl;
  7. Rpccall cmd;
  8. Rpccall reply;
  9. uchar rpcbuf[8192];
  10. uchar argbuf[8192];
  11. };
  12. void putauth(char*, Auth*);
  13. int rpccall(Rpcconn*, int);
  14. int rpcdebug;
  15. Rpcconn r;
  16. char * mach;
  17. void
  18. main(int argc, char **argv)
  19. {
  20. char addr[64], dir[64], name[128];
  21. char buf[32], *p;
  22. uchar *dataptr, *argptr;
  23. int i, fd, n, remport;
  24. ARGBEGIN{
  25. case 'm':
  26. mach = ARGF();
  27. break;
  28. case 'D':
  29. ++rpcdebug;
  30. break;
  31. }ARGEND
  32. if(argc != 1)
  33. exits("usage");
  34. snprint(addr, sizeof addr, "udp!%s!111", argv[0]);
  35. r.data = dial(addr, 0, dir, &r.ctl);
  36. if(r.data < 0){
  37. fprint(2, "dial %s: %r\n", addr);
  38. exits("dial");
  39. }
  40. if(rpcdebug)
  41. fprint(2, "dial %s: dir=%s\n", addr, dir);
  42. if(fprint(r.ctl, "headers") < 0){
  43. fprint(2, "can't set header mode: %r\n");
  44. exits("headers");
  45. }
  46. fprint(r.ctl, "oldheaders");
  47. sprint(name, "%s/remote", dir);
  48. fd = open(name, OREAD);
  49. if(fd < 0){
  50. fprint(2, "can't open %s: %r\n", name);
  51. exits("remote");
  52. }
  53. n = read(fd, buf, sizeof buf);
  54. if(n < 0){
  55. fprint(2, "can't read %s: %r\n", name);
  56. exits("remote");
  57. }
  58. close(fd);
  59. buf[n] = 0;
  60. p = buf;
  61. r.cmd.host = 0;
  62. for(i=0; i<4; i++, p++)
  63. r.cmd.host = (r.cmd.host<<8)|strtol(p, &p, 10);
  64. r.cmd.port = strtol(p, 0, 10);
  65. fprint(2, "host=%ld.%ld.%ld.%ld, port=%lud\n",
  66. (r.cmd.host>>24)&0xff, (r.cmd.host>>16)&0xff,
  67. (r.cmd.host>>8)&0xff, r.cmd.host&0xff, r.cmd.port);
  68. fprint(r.ctl, "disconnect");
  69. r.cmd.xid = time(0);
  70. r.cmd.mtype = CALL;
  71. r.cmd.rpcvers = 2;
  72. r.cmd.args = r.argbuf;
  73. if(mach)
  74. putauth(mach, &r.cmd.cred);
  75. r.cmd.prog = 100000; /* portmapper */
  76. r.cmd.vers = 2; /* vers */
  77. r.cmd.proc = 3; /* getport */
  78. dataptr = r.cmd.args;
  79. PLONG(100005); /* mount */
  80. PLONG(1); /* vers */
  81. PLONG(IPPROTO_UDP);
  82. PLONG(0);
  83. i = rpccall(&r, dataptr-(uchar*)r.cmd.args);
  84. if(i != 4)
  85. exits("trouble");
  86. argptr = r.reply.results;
  87. remport = GLONG();
  88. fprint(2, "remote port = %d\n", remport);
  89. r.cmd.port = remport;
  90. r.cmd.prog = 100005; /* mount */
  91. r.cmd.vers = 1; /* vers */
  92. r.cmd.proc = 0; /* null */
  93. dataptr = r.cmd.args;
  94. i = rpccall(&r, dataptr-(uchar*)r.cmd.args);
  95. if(i != 0)
  96. exits("trouble");
  97. fprint(2, "OK ping mount\n");
  98. r.cmd.prog = 100005; /* mount */
  99. r.cmd.vers = 1; /* vers */
  100. r.cmd.proc = 5; /* export */
  101. dataptr = r.cmd.args;
  102. i = rpccall(&r, dataptr-(uchar*)r.cmd.args);
  103. fprint(2, "export: %d bytes returned\n", i);
  104. argptr = r.reply.results;
  105. for(;;){
  106. if(GLONG() == 0)
  107. break;
  108. n = GLONG();
  109. p = GPTR(n);
  110. print("%.*s\n", utfnlen(p, n), p);
  111. for(;;){
  112. if(GLONG() == 0)
  113. break;
  114. n = GLONG();
  115. p = GPTR(n);
  116. print("\t%.*s\n", utfnlen(p, n), p);
  117. }
  118. }
  119. exits(0);
  120. }
  121. void
  122. putauth(char *mach, Auth *a)
  123. {
  124. uchar *dataptr;
  125. long stamp = time(0);
  126. int n = strlen(mach);
  127. dataptr = realloc(a->data, 2*4+ROUNDUP(n)+4*4);
  128. a->data = dataptr;
  129. a->flavor = AUTH_UNIX;
  130. PLONG(stamp);
  131. PLONG(n);
  132. PPTR(mach, n);
  133. PLONG(0);
  134. PLONG(1);
  135. PLONG(1);
  136. PLONG(0);
  137. a->count = dataptr - (uchar*)a->data;
  138. }
  139. int
  140. rpccall(Rpcconn *r, int narg)
  141. {
  142. int n;
  143. r->cmd.xid++;
  144. n = rpcS2M(&r->cmd, narg, r->rpcbuf);
  145. if(rpcdebug)
  146. rpcprint(2, &r->cmd);
  147. if(write(r->data, r->rpcbuf, n) < 0){
  148. fprint(2, "rpc write: %r\n");
  149. exits("rpc");
  150. }
  151. n = read(r->data, r->rpcbuf, sizeof r->rpcbuf);
  152. if(n < 0){
  153. fprint(2, "rpc read: %r\n");
  154. exits("rpc");
  155. }
  156. if(rpcM2S(r->rpcbuf, &r->reply, n) != 0){
  157. fprint(2, "rpc reply format error\n");
  158. exits("rpc");
  159. }
  160. if(rpcdebug)
  161. rpcprint(2, &r->reply);
  162. if(r->reply.mtype != REPLY || r->reply.stat != MSG_ACCEPTED ||
  163. r->reply.astat != SUCCESS){
  164. fprint(2, "rpc mtype, stat, astat = %ld, %ld, %ld\n",
  165. r->reply.mtype, r->reply.stat, r->reply.astat);
  166. exits("rpc");
  167. }
  168. return n - (((uchar *)r->reply.results) - r->rpcbuf);
  169. }