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