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