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