1/* $NetBSD: rpc_prot.c,v 1.18 2003/05/29 18:15:25 christos Exp $ */ 2 3/* 4 * Copyright (c) 2010, Oracle America, Inc. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are 8 * met: 9 * 10 * * Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * * Redistributions in binary form must reproduce the above 13 * copyright notice, this list of conditions and the following 14 * disclaimer in the documentation and/or other materials 15 * provided with the distribution. 16 * * Neither the name of the "Oracle America, Inc." nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34#include <sys/cdefs.h> 35#if defined(LIBC_SCCS) && !defined(lint) 36#if 0 37static char *sccsid = "@(#)rpc_prot.c 1.36 87/08/11 Copyr 1984 Sun Micro"; 38static char *sccsid = "@(#)rpc_prot.c 2.3 88/08/07 4.0 RPCSRC"; 39#else 40__RCSID("$NetBSD: rpc_prot.c,v 1.18 2003/05/29 18:15:25 christos Exp $"); 41#endif 42#endif 43 44/* 45 * rpc_prot.c 46 * 47 * Copyright (C) 1984, Sun Microsystems, Inc. 48 * 49 * This set of routines implements the rpc message definition, 50 * its serializer and some common rpc utility routines. 51 * The routines are meant for various implementations of rpc - 52 * they are NOT for the rpc client or rpc service implementations! 53 * Because authentication stuff is easy and is part of rpc, the opaque 54 * routines are also in this program. 55 */ 56 57#include "namespace.h" 58 59#include <sys/param.h> 60 61#include <assert.h> 62 63#include <rpc/rpc.h> 64 65#ifdef __weak_alias 66__weak_alias(xdr_accepted_reply,_xdr_accepted_reply) 67__weak_alias(xdr_callhdr,_xdr_callhdr) 68__weak_alias(xdr_des_block,_xdr_des_block) 69__weak_alias(xdr_opaque_auth,_xdr_opaque_auth) 70__weak_alias(xdr_rejected_reply,_xdr_rejected_reply) 71__weak_alias(xdr_replymsg,_xdr_replymsg) 72#endif 73 74static void accepted __P((enum accept_stat, struct rpc_err *)); 75static void rejected __P((enum reject_stat, struct rpc_err *)); 76 77/* * * * * * * * * * * * * * XDR Authentication * * * * * * * * * * * */ 78 79/* 80 * XDR an opaque authentication struct 81 * (see auth.h) 82 */ 83bool_t 84xdr_opaque_auth(xdrs, ap) 85 XDR *xdrs; 86 struct opaque_auth *ap; 87{ 88 89 _DIAGASSERT(xdrs != NULL); 90 _DIAGASSERT(ap != NULL); 91 92 if (xdr_enum(xdrs, &(ap->oa_flavor))) 93 return (xdr_bytes(xdrs, &ap->oa_base, 94 &ap->oa_length, MAX_AUTH_BYTES)); 95 return (FALSE); 96} 97 98/* 99 * XDR a DES block 100 */ 101bool_t 102xdr_des_block(xdrs, blkp) 103 XDR *xdrs; 104 des_block *blkp; 105{ 106 107 _DIAGASSERT(xdrs != NULL); 108 _DIAGASSERT(blkp != NULL); 109 110 return (xdr_opaque(xdrs, (caddr_t)(void *)blkp, sizeof(des_block))); 111} 112 113/* * * * * * * * * * * * * * XDR RPC MESSAGE * * * * * * * * * * * * * * * */ 114 115/* 116 * XDR the MSG_ACCEPTED part of a reply message union 117 */ 118bool_t 119xdr_accepted_reply(xdrs, ar) 120 XDR *xdrs; 121 struct accepted_reply *ar; 122{ 123 124 _DIAGASSERT(xdrs != NULL); 125 _DIAGASSERT(ar != NULL); 126 127 /* personalized union, rather than calling xdr_union */ 128 if (! xdr_opaque_auth(xdrs, &(ar->ar_verf))) 129 return (FALSE); 130 if (! xdr_enum(xdrs, (enum_t *)(void *)&(ar->ar_stat))) 131 return (FALSE); 132 switch (ar->ar_stat) { 133 134 case SUCCESS: 135 return ((*(ar->ar_results.proc))(xdrs, ar->ar_results.where)); 136 137 case PROG_MISMATCH: 138 if (! xdr_u_int32_t(xdrs, &(ar->ar_vers.low))) 139 return (FALSE); 140 return (xdr_u_int32_t(xdrs, &(ar->ar_vers.high))); 141 142 case GARBAGE_ARGS: 143 case SYSTEM_ERR: 144 case PROC_UNAVAIL: 145 case PROG_UNAVAIL: 146 break; 147 } 148 return (TRUE); /* TRUE => open ended set of problems */ 149} 150 151/* 152 * XDR the MSG_DENIED part of a reply message union 153 */ 154bool_t 155xdr_rejected_reply(xdrs, rr) 156 XDR *xdrs; 157 struct rejected_reply *rr; 158{ 159 160 _DIAGASSERT(xdrs != NULL); 161 _DIAGASSERT(rr != NULL); 162 163 /* personalized union, rather than calling xdr_union */ 164 if (! xdr_enum(xdrs, (enum_t *)(void *)&(rr->rj_stat))) 165 return (FALSE); 166 switch (rr->rj_stat) { 167 168 case RPC_MISMATCH: 169 if (! xdr_u_int32_t(xdrs, &(rr->rj_vers.low))) 170 return (FALSE); 171 return (xdr_u_int32_t(xdrs, &(rr->rj_vers.high))); 172 173 case AUTH_ERROR: 174 return (xdr_enum(xdrs, (enum_t *)(void *)&(rr->rj_why))); 175 } 176 /* NOTREACHED */ 177 return (FALSE); 178} 179 180static const struct xdr_discrim reply_dscrm[3] = { 181 { (int)MSG_ACCEPTED, (xdrproc_t)xdr_accepted_reply }, 182 { (int)MSG_DENIED, (xdrproc_t)xdr_rejected_reply }, 183 { __dontcare__, NULL_xdrproc_t } }; 184 185/* 186 * XDR a reply message 187 */ 188bool_t 189xdr_replymsg(xdrs, rmsg) 190 XDR *xdrs; 191 struct rpc_msg *rmsg; 192{ 193 _DIAGASSERT(xdrs != NULL); 194 _DIAGASSERT(rmsg != NULL); 195 196 if ( 197 xdr_u_int32_t(xdrs, &(rmsg->rm_xid)) && 198 xdr_enum(xdrs, (enum_t *)(void *)&(rmsg->rm_direction)) && 199 (rmsg->rm_direction == REPLY) ) 200 return (xdr_union(xdrs, (enum_t *)(void *)&(rmsg->rm_reply.rp_stat), 201 (caddr_t)(void *)&(rmsg->rm_reply.ru), reply_dscrm, 202 NULL_xdrproc_t)); 203 return (FALSE); 204} 205 206 207/* 208 * Serializes the "static part" of a call message header. 209 * The fields include: rm_xid, rm_direction, rpcvers, prog, and vers. 210 * The rm_xid is not really static, but the user can easily munge on the fly. 211 */ 212bool_t 213xdr_callhdr(xdrs, cmsg) 214 XDR *xdrs; 215 struct rpc_msg *cmsg; 216{ 217 218 _DIAGASSERT(xdrs != NULL); 219 _DIAGASSERT(cmsg != NULL); 220 221 cmsg->rm_direction = CALL; 222 cmsg->rm_call.cb_rpcvers = RPC_MSG_VERSION; 223 if ( 224 (xdrs->x_op == XDR_ENCODE) && 225 xdr_u_int32_t(xdrs, &(cmsg->rm_xid)) && 226 xdr_enum(xdrs, (enum_t *)(void *)&(cmsg->rm_direction)) && 227 xdr_u_int32_t(xdrs, &(cmsg->rm_call.cb_rpcvers)) && 228 xdr_u_int32_t(xdrs, &(cmsg->rm_call.cb_prog)) ) 229 return (xdr_u_int32_t(xdrs, &(cmsg->rm_call.cb_vers))); 230 return (FALSE); 231} 232 233/* ************************** Client utility routine ************* */ 234 235static void 236accepted(acpt_stat, error) 237 enum accept_stat acpt_stat; 238 struct rpc_err *error; 239{ 240 241 _DIAGASSERT(error != NULL); 242 243 switch (acpt_stat) { 244 245 case PROG_UNAVAIL: 246 error->re_status = RPC_PROGUNAVAIL; 247 return; 248 249 case PROG_MISMATCH: 250 error->re_status = RPC_PROGVERSMISMATCH; 251 return; 252 253 case PROC_UNAVAIL: 254 error->re_status = RPC_PROCUNAVAIL; 255 return; 256 257 case GARBAGE_ARGS: 258 error->re_status = RPC_CANTDECODEARGS; 259 return; 260 261 case SYSTEM_ERR: 262 error->re_status = RPC_SYSTEMERROR; 263 return; 264 265 case SUCCESS: 266 error->re_status = RPC_SUCCESS; 267 return; 268 } 269 /* NOTREACHED */ 270 /* something's wrong, but we don't know what ... */ 271 error->re_status = RPC_FAILED; 272 error->re_lb.s1 = (int32_t)MSG_ACCEPTED; 273 error->re_lb.s2 = (int32_t)acpt_stat; 274} 275 276static void 277rejected(rjct_stat, error) 278 enum reject_stat rjct_stat; 279 struct rpc_err *error; 280{ 281 282 _DIAGASSERT(error != NULL); 283 284 switch (rjct_stat) { 285 case RPC_MISMATCH: 286 error->re_status = RPC_VERSMISMATCH; 287 return; 288 289 case AUTH_ERROR: 290 error->re_status = RPC_AUTHERROR; 291 return; 292 } 293 /* something's wrong, but we don't know what ... */ 294 /* NOTREACHED */ 295 error->re_status = RPC_FAILED; 296 error->re_lb.s1 = (int32_t)MSG_DENIED; 297 error->re_lb.s2 = (int32_t)rjct_stat; 298} 299 300/* 301 * given a reply message, fills in the error 302 */ 303void 304_seterr_reply(msg, error) 305 struct rpc_msg *msg; 306 struct rpc_err *error; 307{ 308 309 _DIAGASSERT(msg != NULL); 310 _DIAGASSERT(error != NULL); 311 312 /* optimized for normal, SUCCESSful case */ 313 switch (msg->rm_reply.rp_stat) { 314 315 case MSG_ACCEPTED: 316 if (msg->acpted_rply.ar_stat == SUCCESS) { 317 error->re_status = RPC_SUCCESS; 318 return; 319 } 320 accepted(msg->acpted_rply.ar_stat, error); 321 break; 322 323 case MSG_DENIED: 324 rejected(msg->rjcted_rply.rj_stat, error); 325 break; 326 327 default: 328 error->re_status = RPC_FAILED; 329 error->re_lb.s1 = (int32_t)(msg->rm_reply.rp_stat); 330 break; 331 } 332 switch (error->re_status) { 333 334 case RPC_VERSMISMATCH: 335 error->re_vers.low = msg->rjcted_rply.rj_vers.low; 336 error->re_vers.high = msg->rjcted_rply.rj_vers.high; 337 break; 338 339 case RPC_AUTHERROR: 340 error->re_why = msg->rjcted_rply.rj_why; 341 break; 342 343 case RPC_PROGVERSMISMATCH: 344 error->re_vers.low = msg->acpted_rply.ar_vers.low; 345 error->re_vers.high = msg->acpted_rply.ar_vers.high; 346 break; 347 348 case RPC_FAILED: 349 case RPC_SUCCESS: 350 case RPC_PROGNOTREGISTERED: 351 case RPC_PMAPFAILURE: 352 case RPC_UNKNOWNPROTO: 353 case RPC_UNKNOWNHOST: 354 case RPC_SYSTEMERROR: 355 case RPC_CANTDECODEARGS: 356 case RPC_PROCUNAVAIL: 357 case RPC_PROGUNAVAIL: 358 case RPC_TIMEDOUT: 359 case RPC_CANTRECV: 360 case RPC_CANTSEND: 361 case RPC_CANTDECODERES: 362 case RPC_CANTENCODEARGS: 363 default: 364 break; 365 } 366} 367