xref: /original-bsd/usr.sbin/amd/amd/misc_rpc.c (revision a6d4d8bb)
1 /*
2  * Copyright (c) 1990 Jan-Simon Pendry
3  * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
4  * Copyright (c) 1990 The Regents of the University of California.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Jan-Simon Pendry at Imperial College, London.
9  *
10  * %sccs.include.redist.c%
11  *
12  *	@(#)misc_rpc.c	5.3 (Berkeley) 05/12/91
13  *
14  * $Id: misc_rpc.c,v 5.2.1.3 91/05/07 22:18:10 jsp Alpha $
15  *
16  */
17 
18 /*
19  * Additions to Sun RPC.
20  */
21 
22 #include "am.h"
23 
24 void rpc_msg_init P((struct rpc_msg *mp, u_long prog, u_long vers, u_long proc));
25 void rpc_msg_init(mp, prog, vers, proc)
26 struct rpc_msg *mp;
27 unsigned long prog, vers, proc;
28 {
29 	/*
30 	 * Initialise the message
31 	 */
32 	bzero((voidp) mp, sizeof(*mp));
33 	mp->rm_xid = 0;
34 	mp->rm_direction = CALL;
35 	mp->rm_call.cb_rpcvers = RPC_MSG_VERSION;
36 	mp->rm_call.cb_prog = prog;
37 	mp->rm_call.cb_vers = vers;
38 	mp->rm_call.cb_proc = proc;
39 }
40 
41 /*
42  * Field reply to call to mountd
43  */
44 int pickup_rpc_reply P((voidp pkt, int len, voidp where, xdrproc_t where_xdr));
45 int pickup_rpc_reply(pkt, len, where, where_xdr)
46 voidp pkt;
47 int len;
48 voidp where;
49 xdrproc_t where_xdr;
50 {
51 	XDR reply_xdr;
52 	int ok;
53 	struct rpc_err err;
54 	struct rpc_msg reply_msg;
55 	int error = 0;
56 
57 	/*bzero((voidp) &err, sizeof(err));*/
58 	bzero((voidp) &reply_msg, sizeof(reply_msg));
59 
60 	reply_msg.acpted_rply.ar_results.where = (caddr_t) where;
61 	reply_msg.acpted_rply.ar_results.proc = where_xdr;
62 
63 	xdrmem_create(&reply_xdr, pkt, len, XDR_DECODE);
64 
65 	ok = xdr_replymsg(&reply_xdr, &reply_msg);
66 	if (!ok) {
67 		error = EIO;
68 		goto drop;
69 	}
70 	_seterr_reply(&reply_msg, &err);
71 	if (err.re_status != RPC_SUCCESS) {
72 		error = EIO;
73 		goto drop;
74 	}
75 
76 drop:
77 	if (reply_msg.acpted_rply.ar_verf.oa_base) {
78 		reply_xdr.x_op = XDR_FREE;
79 		(void)xdr_opaque_auth(&reply_xdr,
80 			&reply_msg.acpted_rply.ar_verf);
81 	}
82 	xdr_destroy(&reply_xdr);
83 
84 	return error;
85 }
86 
87 int make_rpc_packet P((char *buf, int buflen, unsigned long proc,
88 			struct rpc_msg *mp, voidp arg, xdrproc_t arg_xdr, AUTH *auth));
89 int make_rpc_packet(buf, buflen, proc, mp, arg, arg_xdr, auth)
90 char *buf;
91 int buflen;
92 unsigned long proc;
93 struct rpc_msg *mp;
94 voidp arg;
95 xdrproc_t arg_xdr;
96 AUTH *auth;
97 {
98 	XDR msg_xdr;
99 	int len;
100 
101 	xdrmem_create(&msg_xdr, buf, buflen, XDR_ENCODE);
102 	/*
103 	 * Basic protocol header
104 	 */
105 	if (!xdr_callhdr(&msg_xdr, mp))
106 		return -EIO;
107 	/*
108 	 * Called procedure number
109 	 */
110 	if (!xdr_enum(&msg_xdr, &proc))
111 		return -EIO;
112 	/*
113 	 * Authorization
114 	 */
115 	if (!AUTH_MARSHALL(auth, &msg_xdr))
116 		return -EIO;
117 	/*
118 	 * Arguments
119 	 */
120 	if (!(*arg_xdr)(&msg_xdr, arg))
121 		return -EIO;
122 	/*
123 	 * Determine length
124 	 */
125 	len = xdr_getpos(&msg_xdr);
126 	/*
127 	 * Throw away xdr
128 	 */
129 	xdr_destroy(&msg_xdr);
130 	return len;
131 }
132 
133 
134 /*
135  * Early RPC seems to be missing these..
136  * Extracted from the RPC 3.9 sources as indicated
137  */
138 
139 #ifdef NEED_XDR_POINTER
140 /* @(#)xdr_reference.c	1.1 87/11/04 3.9 RPCSRC */
141 /*
142  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
143  * unrestricted use provided that this legend is included on all tape
144  * media and as a part of the software program in whole or part.  Users
145  * may copy or modify Sun RPC without charge, but are not authorized
146  * to license or distribute it to anyone else except as part of a product or
147  * program developed by the user.
148  *
149  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
150  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
151  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
152  *
153  * Sun RPC is provided with no support and without any obligation on the
154  * part of Sun Microsystems, Inc. to assist in its use, correction,
155  * modification or enhancement.
156  *
157  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
158  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
159  * OR ANY PART THEREOF.
160  *
161  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
162  * or profits or other special, indirect and consequential damages, even if
163  * Sun has been advised of the possibility of such damages.
164  *
165  * Sun Microsystems, Inc.
166  * 2550 Garcia Avenue
167  * Mountain View, California  94043
168  */
169 
170 
171 /*
172  * xdr_pointer():
173  *
174  * XDR a pointer to a possibly recursive data structure. This
175  * differs with xdr_reference in that it can serialize/deserialiaze
176  * trees correctly.
177  *
178  *  What's sent is actually a union:
179  *
180  *  union object_pointer switch (boolean b) {
181  *  case TRUE: object_data data;
182  *  case FALSE: void nothing;
183  *  }
184  *
185  * > objpp: Pointer to the pointer to the object.
186  * > obj_size: size of the object.
187  * > xdr_obj: routine to XDR an object.
188  *
189  */
190 bool_t
191 xdr_pointer(xdrs,objpp,obj_size,xdr_obj)
192 	register XDR *xdrs;
193 	char **objpp;
194 	u_int obj_size;
195 	xdrproc_t xdr_obj;
196 {
197 
198 	bool_t more_data;
199 
200 	more_data = (*objpp != NULL);
201 	if (! xdr_bool(xdrs,&more_data)) {
202 		return (FALSE);
203 	}
204 	if (! more_data) {
205 		*objpp = NULL;
206 		return (TRUE);
207 	}
208 	return (xdr_reference(xdrs,objpp,obj_size,xdr_obj));
209 }
210 #endif /* NEED_XDR_POINTER */
211 
212 #ifdef NEED_CLNT_SPERRNO
213 /* @(#)clnt_perror.c	1.1 87/11/04 3.9 RPCSRC */
214 /*
215  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
216  * unrestricted use provided that this legend is included on all tape
217  * media and as a part of the software program in whole or part.  Users
218  * may copy or modify Sun RPC without charge, but are not authorized
219  * to license or distribute it to anyone else except as part of a product or
220  * program developed by the user.
221  *
222  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
223  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
224  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
225  *
226  * Sun RPC is provided with no support and without any obligation on the
227  * part of Sun Microsystems, Inc. to assist in its use, correction,
228  * modification or enhancement.
229  *
230  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
231  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
232  * OR ANY PART THEREOF.
233  *
234  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
235  * or profits or other special, indirect and consequential damages, even if
236  * Sun has been advised of the possibility of such damages.
237  *
238  * Sun Microsystems, Inc.
239  * 2550 Garcia Avenue
240  * Mountain View, California  94043
241  */
242 
243 struct rpc_errtab {
244 	enum clnt_stat status;
245 	char *message;
246 };
247 
248 static struct rpc_errtab  rpc_errlist[] = {
249 	{ RPC_SUCCESS,
250 		"RPC: Success" },
251 	{ RPC_CANTENCODEARGS,
252 		"RPC: Can't encode arguments" },
253 	{ RPC_CANTDECODERES,
254 		"RPC: Can't decode result" },
255 	{ RPC_CANTSEND,
256 		"RPC: Unable to send" },
257 	{ RPC_CANTRECV,
258 		"RPC: Unable to receive" },
259 	{ RPC_TIMEDOUT,
260 		"RPC: Timed out" },
261 	{ RPC_VERSMISMATCH,
262 		"RPC: Incompatible versions of RPC" },
263 	{ RPC_AUTHERROR,
264 		"RPC: Authentication error" },
265 	{ RPC_PROGUNAVAIL,
266 		"RPC: Program unavailable" },
267 	{ RPC_PROGVERSMISMATCH,
268 		"RPC: Program/version mismatch" },
269 	{ RPC_PROCUNAVAIL,
270 		"RPC: Procedure unavailable" },
271 	{ RPC_CANTDECODEARGS,
272 		"RPC: Server can't decode arguments" },
273 	{ RPC_SYSTEMERROR,
274 		"RPC: Remote system error" },
275 	{ RPC_UNKNOWNHOST,
276 		"RPC: Unknown host" },
277 /*	{ RPC_UNKNOWNPROTO,
278 		"RPC: Unknown protocol" },*/
279 	{ RPC_PMAPFAILURE,
280 		"RPC: Port mapper failure" },
281 	{ RPC_PROGNOTREGISTERED,
282 		"RPC: Program not registered"},
283 	{ RPC_FAILED,
284 		"RPC: Failed (unspecified error)"}
285 };
286 
287 
288 /*
289  * This interface for use by clntrpc
290  */
291 char *
292 clnt_sperrno(stat)
293 	enum clnt_stat stat;
294 {
295 	int i;
296 
297 	for (i = 0; i < sizeof(rpc_errlist)/sizeof(struct rpc_errtab); i++) {
298 		if (rpc_errlist[i].status == stat) {
299 			return (rpc_errlist[i].message);
300 		}
301 	}
302 	return ("RPC: (unknown error code)");
303 }
304 
305 #endif /* NEED_CLNT_SPERRNO */
306 
307