mport.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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[33], *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. sprint(name, "%s/remote", dir);
  47. fd = open(name, OREAD);
  48. if(fd < 0){
  49. fprint(2, "can't open %s: %r\n", name);
  50. exits("remote");
  51. }
  52. n = read(fd, buf, sizeof buf-1);
  53. if(n < 0){
  54. fprint(2, "can't read %s: %r\n", name);
  55. exits("remote");
  56. }
  57. close(fd);
  58. buf[n] = 0;
  59. p = buf;
  60. r.cmd.host = 0;
  61. for(i=0; i<4; i++, p++)
  62. r.cmd.host = (r.cmd.host<<8)|strtol(p, &p, 10);
  63. r.cmd.port = strtol(p, 0, 10);
  64. fprint(2, "host=%ld.%ld.%ld.%ld, port=%lud\n",
  65. (r.cmd.host>>24)&0xff, (r.cmd.host>>16)&0xff,
  66. (r.cmd.host>>8)&0xff, r.cmd.host&0xff, r.cmd.port);
  67. fprint(r.ctl, "disconnect");
  68. r.cmd.xid = time(0);
  69. r.cmd.mtype = CALL;
  70. r.cmd.rpcvers = 2;
  71. r.cmd.args = r.argbuf;
  72. if(mach)
  73. putauth(mach, &r.cmd.cred);
  74. r.cmd.prog = 100000; /* portmapper */
  75. r.cmd.vers = 2; /* vers */
  76. r.cmd.proc = 3; /* getport */
  77. dataptr = r.cmd.args;
  78. PLONG(100005); /* mount */
  79. PLONG(1); /* vers */
  80. PLONG(IPPROTO_UDP);
  81. PLONG(0);
  82. i = rpccall(&r, dataptr-(uchar*)r.cmd.args);
  83. if(i != 4)
  84. exits("trouble");
  85. argptr = r.reply.results;
  86. remport = GLONG();
  87. fprint(2, "remote port = %d\n", remport);
  88. r.cmd.port = remport;
  89. r.cmd.prog = 100005; /* mount */
  90. r.cmd.vers = 1; /* vers */
  91. r.cmd.proc = 0; /* null */
  92. dataptr = r.cmd.args;
  93. i = rpccall(&r, dataptr-(uchar*)r.cmd.args);
  94. if(i != 0)
  95. exits("trouble");
  96. fprint(2, "OK ping mount\n");
  97. r.cmd.prog = 100005; /* mount */
  98. r.cmd.vers = 1; /* vers */
  99. r.cmd.proc = 5; /* export */
  100. dataptr = r.cmd.args;
  101. i = rpccall(&r, dataptr-(uchar*)r.cmd.args);
  102. fprint(2, "export: %d bytes returned\n", i);
  103. argptr = r.reply.results;
  104. while (GLONG() != 0) {
  105. n = GLONG();
  106. p = GPTR(n);
  107. print("%.*s\n", utfnlen(p, n), p);
  108. while (GLONG() != 0) {
  109. n = GLONG();
  110. p = GPTR(n);
  111. print("\t%.*s\n", utfnlen(p, n), p);
  112. }
  113. }
  114. exits(0);
  115. }
  116. void
  117. putauth(char *mach, Auth *a)
  118. {
  119. uchar *dataptr;
  120. long stamp = time(0);
  121. int n = strlen(mach);
  122. dataptr = realloc(a->data, 2*4+ROUNDUP(n)+4*4);
  123. a->data = dataptr;
  124. a->flavor = AUTH_UNIX;
  125. PLONG(stamp);
  126. PLONG(n);
  127. PPTR(mach, n);
  128. PLONG(0);
  129. PLONG(1);
  130. PLONG(1);
  131. PLONG(0);
  132. a->count = dataptr - (uchar*)a->data;
  133. }
  134. int
  135. rpccall(Rpcconn *r, int narg)
  136. {
  137. int n;
  138. r->cmd.xid++;
  139. n = rpcS2M(&r->cmd, narg, r->rpcbuf);
  140. if(rpcdebug)
  141. rpcprint(2, &r->cmd);
  142. if(write(r->data, r->rpcbuf, n) < 0){
  143. fprint(2, "rpc write: %r\n");
  144. exits("rpc");
  145. }
  146. n = read(r->data, r->rpcbuf, sizeof r->rpcbuf);
  147. if(n < 0){
  148. fprint(2, "rpc read: %r\n");
  149. exits("rpc");
  150. }
  151. if(rpcM2S(r->rpcbuf, &r->reply, n) != 0){
  152. fprint(2, "rpc reply format error\n");
  153. exits("rpc");
  154. }
  155. if(rpcdebug)
  156. rpcprint(2, &r->reply);
  157. if(r->reply.mtype != REPLY || r->reply.stat != MSG_ACCEPTED ||
  158. r->reply.astat != SUCCESS){
  159. fprint(2, "rpc mtype, stat, astat = %ld, %ld, %ld\n",
  160. r->reply.mtype, r->reply.stat, r->reply.astat);
  161. exits("rpc");
  162. }
  163. return n - (((uchar *)r->reply.results) - r->rpcbuf);
  164. }