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