portmapper.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /*
  10. * sunrpc portmapper
  11. */
  12. #include "all.h"
  13. typedef struct Portmap Portmap;
  14. struct Portmap
  15. {
  16. int prog;
  17. int vers;
  18. int protocol;
  19. int port;
  20. };
  21. Portmap map[] = {
  22. 100003, 2, IPPROTO_UDP, 2049, /* nfs v2 */
  23. // 100003, 3, IPPROTO_UDP, 2049, /* nfs v3 */
  24. 100005, 1, IPPROTO_UDP, 2049, /* mount */
  25. 150001, 2, IPPROTO_UDP, 1111, /* pcnfsd v2 */
  26. 150001, 1, IPPROTO_UDP, 1111, /* pcnfsd v1 */
  27. 0, 0, 0, 0,
  28. };
  29. static void pmapinit(int, char**);
  30. static int pmapnull(int, Rpccall*, Rpccall*);
  31. static int pmapset(int, Rpccall*, Rpccall*);
  32. static int pmapunset(int, Rpccall*, Rpccall*);
  33. static int pmapgetport(int, Rpccall*, Rpccall*);
  34. static int pmapdump(int, Rpccall*, Rpccall*);
  35. static int pmapcallit(int, Rpccall*, Rpccall*);
  36. static Procmap pmapproc[] = {
  37. 0, pmapnull,
  38. 1, pmapset,
  39. 2, pmapunset,
  40. 3, pmapgetport,
  41. 4, pmapdump,
  42. 5, pmapcallit,
  43. 0, 0
  44. };
  45. int myport = 111;
  46. Progmap progmap[] = {
  47. 100000, 2, pmapinit, pmapproc,
  48. 0, 0, 0,
  49. };
  50. void
  51. main(int argc, char *argv[])
  52. {
  53. server(argc, argv, myport, progmap);
  54. }
  55. static
  56. void
  57. pmapinit(int argc, char **argv)
  58. {
  59. ARGBEGIN{
  60. default:
  61. if(argopt(ARGC()) < 0)
  62. sysfatal("usage: %s %s", argv0, commonopts);
  63. break;
  64. }ARGEND;
  65. clog("portmapper init\n");
  66. }
  67. static int
  68. pmapnull(int n, Rpccall *cmd, Rpccall *reply)
  69. {
  70. USED(n); USED(cmd); USED(reply);
  71. return 0;
  72. }
  73. static int
  74. pmapset(int n, Rpccall *cmd, Rpccall *reply)
  75. {
  76. uint8_t *dataptr = reply->results;
  77. if(n != 16)
  78. return garbage(reply, "bad count");
  79. USED(cmd);
  80. PLONG(FALSE);
  81. return dataptr - (uint8_t *)reply->results;
  82. }
  83. static int
  84. pmapunset(int n, Rpccall *cmd, Rpccall *reply)
  85. {
  86. uint8_t *dataptr = reply->results;
  87. if(n != 16)
  88. return garbage(reply, "bad count");
  89. USED(cmd);
  90. PLONG(TRUE);
  91. return dataptr - (uint8_t *)reply->results;
  92. }
  93. static int
  94. pmapgetport(int n, Rpccall *cmd, Rpccall *reply)
  95. {
  96. int prog, vers, prot;
  97. uint8_t *argptr = cmd->args;
  98. uint8_t *dataptr = reply->results;
  99. Portmap *mp;
  100. clog("get port\n");
  101. if(n != 16)
  102. return garbage(reply, "bad count");
  103. prog = GLONG();
  104. vers = GLONG();
  105. prot = GLONG();
  106. chat("host=%I, port=%ld: ", cmd->host, cmd->port);
  107. chat("getport: %d, %d, %d...", prog, vers, prot);
  108. for(mp=map; mp->prog>0; mp++)
  109. if(prog == mp->prog && vers == mp->vers &&
  110. prot == mp->protocol)
  111. break;
  112. chat("%d\n", mp->port);
  113. PLONG(mp->port);
  114. return dataptr - (uint8_t *)reply->results;
  115. }
  116. static int
  117. pmapdump(int n, Rpccall *cmd, Rpccall *reply)
  118. {
  119. uint8_t *dataptr = reply->results;
  120. Portmap *mp;
  121. if(n != 0)
  122. return garbage(reply, "bad count");
  123. USED(cmd);
  124. for(mp=map; mp->prog>0; mp++){
  125. PLONG(1);
  126. PLONG(mp->prog);
  127. PLONG(mp->vers);
  128. PLONG(mp->protocol);
  129. PLONG(mp->port);
  130. }
  131. PLONG(0);
  132. return dataptr - (uint8_t *)reply->results;
  133. }
  134. static int
  135. pmapcallit(int n, Rpccall *cmd, Rpccall *reply)
  136. {
  137. int prog, vers, proc;
  138. uint8_t *argptr = cmd->args;
  139. uint8_t *dataptr = reply->results;
  140. Portmap *mp;
  141. if(n < 12)
  142. return garbage(reply, "bad count");
  143. prog = GLONG();
  144. vers = GLONG();
  145. proc = GLONG();
  146. chat("host=%I, port=%ld: ", cmd->host, cmd->port);
  147. chat("callit: %d, %d, %d...", prog, vers, proc);
  148. for(mp=map; mp->prog>0; mp++)
  149. if(prog == mp->prog && vers == mp->vers &&
  150. proc == 0)
  151. break;
  152. if(mp->port == 0){
  153. chat("ignored\n");
  154. return -1;
  155. }
  156. chat("%d\n", mp->port);
  157. PLONG(mp->port);
  158. PLONG(0);
  159. return dataptr - (uint8_t *)reply->results;
  160. }