1*8bae5d40Schristos /* $NetBSD: misc_rpc.c,v 1.1.1.3 2015/01/17 16:34:18 christos Exp $ */
2a53f50b9Schristos
3a53f50b9Schristos /*
4*8bae5d40Schristos * Copyright (c) 1997-2014 Erez Zadok
5a53f50b9Schristos * Copyright (c) 1990 Jan-Simon Pendry
6a53f50b9Schristos * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
7a53f50b9Schristos * Copyright (c) 1990 The Regents of the University of California.
8a53f50b9Schristos * All rights reserved.
9a53f50b9Schristos *
10a53f50b9Schristos * This code is derived from software contributed to Berkeley by
11a53f50b9Schristos * Jan-Simon Pendry at Imperial College, London.
12a53f50b9Schristos *
13a53f50b9Schristos * Redistribution and use in source and binary forms, with or without
14a53f50b9Schristos * modification, are permitted provided that the following conditions
15a53f50b9Schristos * are met:
16a53f50b9Schristos * 1. Redistributions of source code must retain the above copyright
17a53f50b9Schristos * notice, this list of conditions and the following disclaimer.
18a53f50b9Schristos * 2. Redistributions in binary form must reproduce the above copyright
19a53f50b9Schristos * notice, this list of conditions and the following disclaimer in the
20a53f50b9Schristos * documentation and/or other materials provided with the distribution.
21*8bae5d40Schristos * 3. Neither the name of the University nor the names of its contributors
22a53f50b9Schristos * may be used to endorse or promote products derived from this software
23a53f50b9Schristos * without specific prior written permission.
24a53f50b9Schristos *
25a53f50b9Schristos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26a53f50b9Schristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27a53f50b9Schristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28a53f50b9Schristos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29a53f50b9Schristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30a53f50b9Schristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31a53f50b9Schristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32a53f50b9Schristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33a53f50b9Schristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34a53f50b9Schristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35a53f50b9Schristos * SUCH DAMAGE.
36a53f50b9Schristos *
37a53f50b9Schristos *
38a53f50b9Schristos * File: am-utils/libamu/misc_rpc.c
39a53f50b9Schristos *
40a53f50b9Schristos */
41a53f50b9Schristos
42a53f50b9Schristos /*
43a53f50b9Schristos * Additions to Sun RPC.
44a53f50b9Schristos */
45a53f50b9Schristos
46a53f50b9Schristos #ifdef HAVE_CONFIG_H
47a53f50b9Schristos # include <config.h>
48a53f50b9Schristos #endif /* HAVE_CONFIG_H */
49a53f50b9Schristos #include <am_defs.h>
50a53f50b9Schristos #include <amu.h>
51a53f50b9Schristos
52a53f50b9Schristos /*
53a53f50b9Schristos * Some systems renamed _seterr_reply to __seterr_reply (with two
54a53f50b9Schristos * leading underscores)
55a53f50b9Schristos */
56a53f50b9Schristos #if !defined(HAVE__SETERR_REPLY) && defined(HAVE___SETERR_REPLY)
57a53f50b9Schristos # define _seterr_reply __seterr_reply
58a53f50b9Schristos #endif /* !defined(HAVE__SETERR_REPLY) && defined(HAVE___SETERR_REPLY) */
59a53f50b9Schristos
60a53f50b9Schristos
61a53f50b9Schristos void
rpc_msg_init(struct rpc_msg * mp,u_long prog,u_long vers,u_long proc)62a53f50b9Schristos rpc_msg_init(struct rpc_msg *mp, u_long prog, u_long vers, u_long proc)
63a53f50b9Schristos {
64a53f50b9Schristos /*
65a53f50b9Schristos * Initialize the message
66a53f50b9Schristos */
67a53f50b9Schristos memset((voidp) mp, 0, sizeof(*mp));
68a53f50b9Schristos mp->rm_xid = 0;
69a53f50b9Schristos mp->rm_direction = CALL;
70a53f50b9Schristos mp->rm_call.cb_rpcvers = RPC_MSG_VERSION;
71a53f50b9Schristos mp->rm_call.cb_prog = prog;
72a53f50b9Schristos mp->rm_call.cb_vers = vers;
73a53f50b9Schristos mp->rm_call.cb_proc = proc;
74a53f50b9Schristos }
75a53f50b9Schristos
76a53f50b9Schristos
77a53f50b9Schristos /*
78a53f50b9Schristos * Field reply to call to mountd
79a53f50b9Schristos */
80a53f50b9Schristos int
pickup_rpc_reply(voidp pkt,int len,voidp where,XDRPROC_T_TYPE where_xdr)81a53f50b9Schristos pickup_rpc_reply(voidp pkt, int len, voidp where, XDRPROC_T_TYPE where_xdr)
82a53f50b9Schristos {
83a53f50b9Schristos XDR reply_xdr;
84a53f50b9Schristos int ok;
85a53f50b9Schristos struct rpc_err err;
86a53f50b9Schristos struct rpc_msg reply_msg;
87a53f50b9Schristos int error = 0;
88a53f50b9Schristos
89a53f50b9Schristos /* memset((voidp) &err, 0, sizeof(err)); */
90a53f50b9Schristos memset((voidp) &reply_msg, 0, sizeof(reply_msg));
91a53f50b9Schristos memset((voidp) &reply_xdr, 0, sizeof(reply_xdr));
92a53f50b9Schristos
93a53f50b9Schristos reply_msg.acpted_rply.ar_results.where = where;
94a53f50b9Schristos reply_msg.acpted_rply.ar_results.proc = where_xdr;
95a53f50b9Schristos
96a53f50b9Schristos xdrmem_create(&reply_xdr, pkt, len, XDR_DECODE);
97a53f50b9Schristos
98a53f50b9Schristos ok = xdr_replymsg(&reply_xdr, &reply_msg);
99a53f50b9Schristos if (!ok) {
100a53f50b9Schristos error = EIO;
101a53f50b9Schristos goto drop;
102a53f50b9Schristos }
103a53f50b9Schristos _seterr_reply(&reply_msg, &err);
104a53f50b9Schristos if (err.re_status != RPC_SUCCESS) {
105a53f50b9Schristos error = EIO;
106a53f50b9Schristos goto drop;
107a53f50b9Schristos }
108a53f50b9Schristos
109a53f50b9Schristos drop:
110a53f50b9Schristos if (reply_msg.rm_reply.rp_stat == MSG_ACCEPTED &&
111a53f50b9Schristos reply_msg.acpted_rply.ar_verf.oa_base) {
112a53f50b9Schristos reply_xdr.x_op = XDR_FREE;
113a53f50b9Schristos (void) xdr_opaque_auth(&reply_xdr,
114a53f50b9Schristos &reply_msg.acpted_rply.ar_verf);
115a53f50b9Schristos }
116a53f50b9Schristos xdr_destroy(&reply_xdr);
117a53f50b9Schristos
118a53f50b9Schristos return error;
119a53f50b9Schristos }
120a53f50b9Schristos
121a53f50b9Schristos
122a53f50b9Schristos int
make_rpc_packet(char * buf,int buflen,u_long proc,struct rpc_msg * mp,voidp arg,XDRPROC_T_TYPE arg_xdr,AUTH * auth)123a53f50b9Schristos make_rpc_packet(char *buf, int buflen, u_long proc, struct rpc_msg *mp, voidp arg, XDRPROC_T_TYPE arg_xdr, AUTH *auth)
124a53f50b9Schristos {
125a53f50b9Schristos XDR msg_xdr;
126a53f50b9Schristos int len;
127a53f50b9Schristos /*
128a53f50b9Schristos * Never cast pointers between different integer types, it breaks badly
129a53f50b9Schristos * on big-endian platforms if those types have different sizes.
130a53f50b9Schristos *
131a53f50b9Schristos * Cast to a local variable instead, and use that variable's address.
132a53f50b9Schristos */
133a53f50b9Schristos enum_t local_proc = (enum_t) proc;
134a53f50b9Schristos
135a53f50b9Schristos xdrmem_create(&msg_xdr, buf, buflen, XDR_ENCODE);
136a53f50b9Schristos
137a53f50b9Schristos /*
138a53f50b9Schristos * Basic protocol header
139a53f50b9Schristos */
140a53f50b9Schristos if (!xdr_callhdr(&msg_xdr, mp))
141a53f50b9Schristos return -EIO;
142a53f50b9Schristos
143a53f50b9Schristos /*
144a53f50b9Schristos * Called procedure number
145a53f50b9Schristos */
146a53f50b9Schristos if (!xdr_enum(&msg_xdr, &local_proc))
147a53f50b9Schristos return -EIO;
148a53f50b9Schristos
149a53f50b9Schristos /*
150a53f50b9Schristos * Authorization
151a53f50b9Schristos */
152a53f50b9Schristos if (!AUTH_MARSHALL(auth, &msg_xdr))
153a53f50b9Schristos return -EIO;
154a53f50b9Schristos
155a53f50b9Schristos /*
156a53f50b9Schristos * Arguments
157a53f50b9Schristos */
158a53f50b9Schristos if (!(*arg_xdr) (&msg_xdr, arg))
159a53f50b9Schristos return -EIO;
160a53f50b9Schristos
161a53f50b9Schristos /*
162a53f50b9Schristos * Determine length
163a53f50b9Schristos */
164a53f50b9Schristos len = xdr_getpos(&msg_xdr);
165a53f50b9Schristos
166a53f50b9Schristos /*
167a53f50b9Schristos * Throw away xdr
168a53f50b9Schristos */
169a53f50b9Schristos xdr_destroy(&msg_xdr);
170a53f50b9Schristos
171a53f50b9Schristos return len;
172a53f50b9Schristos }
173a53f50b9Schristos
174a53f50b9Schristos
175a53f50b9Schristos /* get uid/gid from RPC credentials */
176a53f50b9Schristos int
getcreds(struct svc_req * rp,uid_t * u,gid_t * g,SVCXPRT * nfsxprt)177a53f50b9Schristos getcreds(struct svc_req *rp, uid_t *u, gid_t *g, SVCXPRT *nfsxprt)
178a53f50b9Schristos {
179a53f50b9Schristos struct authunix_parms *aup = (struct authunix_parms *) NULL;
180a53f50b9Schristos #ifdef HAVE_RPC_AUTH_DES_H
181a53f50b9Schristos struct authdes_cred *adp;
182a53f50b9Schristos #endif /* HAVE_RPC_AUTH_DES_H */
183a53f50b9Schristos
184a53f50b9Schristos switch (rp->rq_cred.oa_flavor) {
185a53f50b9Schristos
186a53f50b9Schristos case AUTH_UNIX:
187a53f50b9Schristos aup = (struct authunix_parms *) rp->rq_clntcred;
188a53f50b9Schristos *u = aup->aup_uid;
189a53f50b9Schristos *g = aup->aup_gid;
190a53f50b9Schristos break;
191a53f50b9Schristos
192a53f50b9Schristos #ifdef HAVE_RPC_AUTH_DES_H
193a53f50b9Schristos case AUTH_DES:
194a53f50b9Schristos adp = (struct authdes_cred *) rp->rq_clntcred;
195a53f50b9Schristos *g = INVALIDID; /* some unknown group id */
196a53f50b9Schristos if (sscanf(adp->adc_fullname.name, "unix.%lu@", (u_long *) u) == 1)
197a53f50b9Schristos break;
198a53f50b9Schristos /* fall through */
199a53f50b9Schristos #endif /* HAVE_RPC_AUTH_DES_H */
200a53f50b9Schristos
201a53f50b9Schristos default:
202a53f50b9Schristos *u = *g = INVALIDID; /* just in case */
203a53f50b9Schristos svcerr_weakauth(nfsxprt);
204a53f50b9Schristos return -1;
205a53f50b9Schristos }
206a53f50b9Schristos
207a53f50b9Schristos return 0; /* everything is ok */
208a53f50b9Schristos }
209