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