xref: /openbsd-src/usr.sbin/amd/amd/misc_rpc.c (revision 26d0c865b2e28f6e7d4c71090534c37cab976ea0)
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  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	from: @(#)misc_rpc.c	8.1 (Berkeley) 6/6/93
35  *	$Id: misc_rpc.c,v 1.8 2014/10/26 03:28:41 guenther Exp $
36  */
37 
38 /*
39  * Additions to Sun RPC.
40  */
41 
42 #include "am.h"
43 
44 void	rpc_msg_init(struct rpc_msg *, u_long, u_long, u_long);
45 int	pickup_rpc_reply(void *, int, void *, xdrproc_t);
46 int	make_rpc_packet(char *, int, unsigned long, struct rpc_msg *,
47     void *, xdrproc_t, AUTH *);
48 
49 void
rpc_msg_init(struct rpc_msg * mp,unsigned long prog,unsigned long vers,unsigned long proc)50 rpc_msg_init(struct rpc_msg *mp, unsigned long prog,
51     unsigned long vers, unsigned long proc)
52 {
53 	/*
54 	 * Initialise the message
55 	 */
56 	bzero(mp, sizeof(*mp));
57 	mp->rm_xid = 0;
58 	mp->rm_direction = CALL;
59 	mp->rm_call.cb_rpcvers = RPC_MSG_VERSION;
60 	mp->rm_call.cb_prog = prog;
61 	mp->rm_call.cb_vers = vers;
62 	mp->rm_call.cb_proc = proc;
63 }
64 
65 /*
66  * Field reply to call to mountd
67  */
68 int
pickup_rpc_reply(void * pkt,int len,void * where,xdrproc_t where_xdr)69 pickup_rpc_reply(void *pkt, int len, void *where, xdrproc_t where_xdr)
70 {
71 	XDR reply_xdr;
72 	int ok;
73 	struct rpc_err err;
74 	struct rpc_msg reply_msg;
75 	int error = 0;
76 
77 	/*bzero(&err, sizeof(err));*/
78 	bzero(&reply_msg, sizeof(reply_msg));
79 
80 	reply_msg.acpted_rply.ar_results.where = (caddr_t) where;
81 	reply_msg.acpted_rply.ar_results.proc = where_xdr;
82 
83 	xdrmem_create(&reply_xdr, pkt, len, XDR_DECODE);
84 
85 	ok = xdr_replymsg(&reply_xdr, &reply_msg);
86 	if (!ok) {
87 		error = EIO;
88 		goto drop;
89 	}
90 	_seterr_reply(&reply_msg, &err);
91 	if (err.re_status != RPC_SUCCESS) {
92 		error = EIO;
93 		goto drop;
94 	}
95 
96 drop:
97 	if (reply_msg.rm_reply.rp_stat == MSG_ACCEPTED &&
98 			reply_msg.acpted_rply.ar_verf.oa_base) {
99 		reply_xdr.x_op = XDR_FREE;
100 		(void)xdr_opaque_auth(&reply_xdr,
101 			&reply_msg.acpted_rply.ar_verf);
102 	}
103 	xdr_destroy(&reply_xdr);
104 
105 	return error;
106 }
107 
108 int
make_rpc_packet(char * buf,int buflen,unsigned long proc,struct rpc_msg * mp,void * arg,xdrproc_t arg_xdr,AUTH * auth)109 make_rpc_packet(char *buf, int buflen, unsigned long proc,
110     struct rpc_msg *mp, void *arg, xdrproc_t arg_xdr, AUTH *auth)
111 {
112 	XDR msg_xdr;
113 	int len;
114 
115 	xdrmem_create(&msg_xdr, buf, buflen, XDR_ENCODE);
116 	/*
117 	 * Basic protocol header
118 	 */
119 	if (!xdr_callhdr(&msg_xdr, mp))
120 		return -EIO;
121 	/*
122 	 * Called procedure number
123 	 */
124 	if (!xdr_u_long(&msg_xdr, &proc))
125 		return -EIO;
126 	/*
127 	 * Authorization
128 	 */
129 	if (!AUTH_MARSHALL(auth, &msg_xdr))
130 		return -EIO;
131 	/*
132 	 * Arguments
133 	 */
134 	if (!(*arg_xdr)(&msg_xdr, arg))
135 		return -EIO;
136 	/*
137 	 * Determine length
138 	 */
139 	len = xdr_getpos(&msg_xdr);
140 	/*
141 	 * Throw away xdr
142 	 */
143 	xdr_destroy(&msg_xdr);
144 	return len;
145 }
146