print-sunrpc.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Copyright (c) 1992, 1993, 1994, 1995, 1996
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that: (1) source code distributions
  7. * retain the above copyright notice and this paragraph in its entirety, (2)
  8. * distributions including binary code include the above copyright notice and
  9. * this paragraph in its entirety in the documentation or other materials
  10. * provided with the distribution, and (3) all advertising materials mentioning
  11. * features or use of this software display the following acknowledgement:
  12. * ``This product includes software developed by the University of California,
  13. * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  14. * the University nor the names of its contributors may be used to endorse
  15. * or promote products derived from this software without specific prior
  16. * written permission.
  17. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  18. * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20. */
  21. /* \summary: Sun Remote Procedure Call printer */
  22. #ifdef HAVE_CONFIG_H
  23. #include "config.h"
  24. #endif
  25. /*
  26. * At least on HP-UX:
  27. *
  28. * 1) getrpcbynumber() is declared in <netdb.h>, not any of the RPC
  29. * header files
  30. *
  31. * and
  32. *
  33. * 2) if _XOPEN_SOURCE_EXTENDED is defined, <netdb.h> doesn't declare
  34. * it
  35. *
  36. * so we undefine it.
  37. */
  38. #undef _XOPEN_SOURCE_EXTENDED
  39. #include <netdissect-stdinc.h>
  40. #if defined(HAVE_GETRPCBYNUMBER) && defined(HAVE_RPC_RPC_H)
  41. #include <rpc/rpc.h>
  42. #ifdef HAVE_RPC_RPCENT_H
  43. #include <rpc/rpcent.h>
  44. #endif /* HAVE_RPC_RPCENT_H */
  45. #endif /* defined(HAVE_GETRPCBYNUMBER) && defined(HAVE_RPC_RPC_H) */
  46. #include <stdio.h>
  47. #include <string.h>
  48. #include "netdissect.h"
  49. #include "addrtoname.h"
  50. #include "extract.h"
  51. #include "ip.h"
  52. #include "ip6.h"
  53. #include "rpc_auth.h"
  54. #include "rpc_msg.h"
  55. /*
  56. * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  57. * unrestricted use provided that this legend is included on all tape
  58. * media and as a part of the software program in whole or part. Users
  59. * may copy or modify Sun RPC without charge, but are not authorized
  60. * to license or distribute it to anyone else except as part of a product or
  61. * program developed by the user.
  62. *
  63. * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  64. * WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  65. * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  66. *
  67. * Sun RPC is provided with no support and without any obligation on the
  68. * part of Sun Microsystems, Inc. to assist in its use, correction,
  69. * modification or enhancement.
  70. *
  71. * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  72. * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  73. * OR ANY PART THEREOF.
  74. *
  75. * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  76. * or profits or other special, indirect and consequential damages, even if
  77. * Sun has been advised of the possibility of such damages.
  78. *
  79. * Sun Microsystems, Inc.
  80. * 2550 Garcia Avenue
  81. * Mountain View, California 94043
  82. *
  83. * from: @(#)pmap_prot.h 1.14 88/02/08 SMI
  84. * from: @(#)pmap_prot.h 2.1 88/07/29 4.0 RPCSRC
  85. * $FreeBSD: src/include/rpc/pmap_prot.h,v 1.9.2.1 1999/08/29 14:39:05 peter Exp $
  86. */
  87. /*
  88. * pmap_prot.h
  89. * Protocol for the local binder service, or pmap.
  90. *
  91. * Copyright (C) 1984, Sun Microsystems, Inc.
  92. *
  93. * The following procedures are supported by the protocol:
  94. *
  95. * PMAPPROC_NULL() returns ()
  96. * takes nothing, returns nothing
  97. *
  98. * PMAPPROC_SET(struct pmap) returns (bool_t)
  99. * TRUE is success, FALSE is failure. Registers the tuple
  100. * [prog, vers, prot, port].
  101. *
  102. * PMAPPROC_UNSET(struct pmap) returns (bool_t)
  103. * TRUE is success, FALSE is failure. Un-registers pair
  104. * [prog, vers]. prot and port are ignored.
  105. *
  106. * PMAPPROC_GETPORT(struct pmap) returns (long unsigned).
  107. * 0 is failure. Otherwise returns the port number where the pair
  108. * [prog, vers] is registered. It may lie!
  109. *
  110. * PMAPPROC_DUMP() RETURNS (struct pmaplist *)
  111. *
  112. * PMAPPROC_CALLIT(unsigned, unsigned, unsigned, string<>)
  113. * RETURNS (port, string<>);
  114. * usage: encapsulatedresults = PMAPPROC_CALLIT(prog, vers, proc, encapsulatedargs);
  115. * Calls the procedure on the local machine. If it is not registered,
  116. * this procedure is quite; ie it does not return error information!!!
  117. * This procedure only is supported on rpc/udp and calls via
  118. * rpc/udp. This routine only passes null authentication parameters.
  119. * This file has no interface to xdr routines for PMAPPROC_CALLIT.
  120. *
  121. * The service supports remote procedure calls on udp/ip or tcp/ip socket 111.
  122. */
  123. #define SUNRPC_PMAPPORT ((uint16_t)111)
  124. #define SUNRPC_PMAPPROG ((uint32_t)100000)
  125. #define SUNRPC_PMAPVERS ((uint32_t)2)
  126. #define SUNRPC_PMAPVERS_PROTO ((uint32_t)2)
  127. #define SUNRPC_PMAPVERS_ORIG ((uint32_t)1)
  128. #define SUNRPC_PMAPPROC_NULL ((uint32_t)0)
  129. #define SUNRPC_PMAPPROC_SET ((uint32_t)1)
  130. #define SUNRPC_PMAPPROC_UNSET ((uint32_t)2)
  131. #define SUNRPC_PMAPPROC_GETPORT ((uint32_t)3)
  132. #define SUNRPC_PMAPPROC_DUMP ((uint32_t)4)
  133. #define SUNRPC_PMAPPROC_CALLIT ((uint32_t)5)
  134. struct sunrpc_pmap {
  135. uint32_t pm_prog;
  136. uint32_t pm_vers;
  137. uint32_t pm_prot;
  138. uint32_t pm_port;
  139. };
  140. static const struct tok proc2str[] = {
  141. { SUNRPC_PMAPPROC_NULL, "null" },
  142. { SUNRPC_PMAPPROC_SET, "set" },
  143. { SUNRPC_PMAPPROC_UNSET, "unset" },
  144. { SUNRPC_PMAPPROC_GETPORT, "getport" },
  145. { SUNRPC_PMAPPROC_DUMP, "dump" },
  146. { SUNRPC_PMAPPROC_CALLIT, "call" },
  147. { 0, NULL }
  148. };
  149. /* Forwards */
  150. static char *progstr(uint32_t);
  151. void
  152. sunrpcrequest_print(netdissect_options *ndo, register const u_char *bp,
  153. register u_int length, register const u_char *bp2)
  154. {
  155. register const struct sunrpc_msg *rp;
  156. register const struct ip *ip;
  157. register const struct ip6_hdr *ip6;
  158. uint32_t x;
  159. char srcid[20], dstid[20]; /*fits 32bit*/
  160. rp = (const struct sunrpc_msg *)bp;
  161. if (!ndo->ndo_nflag) {
  162. snprintf(srcid, sizeof(srcid), "0x%x",
  163. EXTRACT_32BITS(&rp->rm_xid));
  164. strlcpy(dstid, "sunrpc", sizeof(dstid));
  165. } else {
  166. snprintf(srcid, sizeof(srcid), "0x%x",
  167. EXTRACT_32BITS(&rp->rm_xid));
  168. snprintf(dstid, sizeof(dstid), "0x%x", SUNRPC_PMAPPORT);
  169. }
  170. switch (IP_V((const struct ip *)bp2)) {
  171. case 4:
  172. ip = (const struct ip *)bp2;
  173. ND_PRINT((ndo, "%s.%s > %s.%s: %d",
  174. ipaddr_string(ndo, &ip->ip_src), srcid,
  175. ipaddr_string(ndo, &ip->ip_dst), dstid, length));
  176. break;
  177. case 6:
  178. ip6 = (const struct ip6_hdr *)bp2;
  179. ND_PRINT((ndo, "%s.%s > %s.%s: %d",
  180. ip6addr_string(ndo, &ip6->ip6_src), srcid,
  181. ip6addr_string(ndo, &ip6->ip6_dst), dstid, length));
  182. break;
  183. default:
  184. ND_PRINT((ndo, "%s.%s > %s.%s: %d", "?", srcid, "?", dstid, length));
  185. break;
  186. }
  187. ND_PRINT((ndo, " %s", tok2str(proc2str, " proc #%u",
  188. EXTRACT_32BITS(&rp->rm_call.cb_proc))));
  189. x = EXTRACT_32BITS(&rp->rm_call.cb_rpcvers);
  190. if (x != 2)
  191. ND_PRINT((ndo, " [rpcver %u]", x));
  192. switch (EXTRACT_32BITS(&rp->rm_call.cb_proc)) {
  193. case SUNRPC_PMAPPROC_SET:
  194. case SUNRPC_PMAPPROC_UNSET:
  195. case SUNRPC_PMAPPROC_GETPORT:
  196. case SUNRPC_PMAPPROC_CALLIT:
  197. x = EXTRACT_32BITS(&rp->rm_call.cb_prog);
  198. if (!ndo->ndo_nflag)
  199. ND_PRINT((ndo, " %s", progstr(x)));
  200. else
  201. ND_PRINT((ndo, " %u", x));
  202. ND_PRINT((ndo, ".%u", EXTRACT_32BITS(&rp->rm_call.cb_vers)));
  203. break;
  204. }
  205. }
  206. static char *
  207. progstr(uint32_t prog)
  208. {
  209. #if defined(HAVE_GETRPCBYNUMBER) && defined(HAVE_RPC_RPC_H)
  210. register struct rpcent *rp;
  211. #endif
  212. static char buf[32];
  213. static uint32_t lastprog = 0;
  214. if (lastprog != 0 && prog == lastprog)
  215. return (buf);
  216. #if defined(HAVE_GETRPCBYNUMBER) && defined(HAVE_RPC_RPC_H)
  217. rp = getrpcbynumber(prog);
  218. if (rp == NULL)
  219. #endif
  220. (void) snprintf(buf, sizeof(buf), "#%u", prog);
  221. #if defined(HAVE_GETRPCBYNUMBER) && defined(HAVE_RPC_RPC_H)
  222. else
  223. strlcpy(buf, rp->r_name, sizeof(buf));
  224. #endif
  225. return (buf);
  226. }