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