xref: /illumos-gate/usr/src/lib/libnsl/rpc/rpc_prot.c (revision b8ccc4133d4adaea81b30537c9a156ae726b2146)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
29 /* All Rights Reserved */
30 /*
31  * Portions of this source code were derived from Berkeley
32  * 4.3 BSD under license from the Regents of the University of
33  * California.
34  */
35 
36 /*
37  * This set of routines implements the rpc message definition,
38  * its serializer and some common rpc utility routines.
39  * The routines are meant for various implementations of rpc -
40  * they are NOT for the rpc client or rpc service implementations!
41  * Because authentication stuff is easy and is part of rpc, the opaque
42  * routines are also in this program.
43  */
44 
45 #include "mt.h"
46 #include <sys/param.h>
47 #include <syslog.h>
48 #include <rpc/rpc.h>
49 #include <malloc.h>
50 
51 /* * * * * * * * * * * * * * XDR Authentication * * * * * * * * * * * */
52 
53 /*
54  * XDR an opaque authentication struct
55  * (see auth.h)
56  */
57 bool_t
58 xdr_opaque_auth(XDR *xdrs, struct opaque_auth *ap)
59 {
60 	if (xdr_enum(xdrs, &(ap->oa_flavor)))
61 		return (xdr_bytes(xdrs, &ap->oa_base,
62 		    &ap->oa_length, MAX_AUTH_BYTES));
63 	return (FALSE);
64 }
65 
66 /*
67  * XDR a DES block
68  */
69 bool_t
70 xdr_des_block(XDR *xdrs, des_block *blkp)
71 {
72 	return (xdr_opaque(xdrs, (caddr_t)blkp, sizeof (des_block)));
73 }
74 
75 /* * * * * * * * * * * * * * XDR RPC MESSAGE * * * * * * * * * * * * * * * */
76 
77 /*
78  * XDR the MSG_ACCEPTED part of a reply message union
79  */
80 bool_t
81 xdr_accepted_reply(XDR *xdrs, struct accepted_reply *ar)
82 {
83 	/* personalized union, rather than calling xdr_union */
84 	if (!xdr_opaque_auth(xdrs, &(ar->ar_verf)))
85 		return (FALSE);
86 	if (!xdr_enum(xdrs, (enum_t *)&(ar->ar_stat)))
87 		return (FALSE);
88 
89 	switch (ar->ar_stat) {
90 	case SUCCESS:
91 		return ((*(ar->ar_results.proc))(xdrs, ar->ar_results.where));
92 	case PROG_MISMATCH:
93 		if (!xdr_u_int(xdrs, (uint_t *)&(ar->ar_vers.low)))
94 			return (FALSE);
95 		return (xdr_u_int(xdrs, (uint_t *)&(ar->ar_vers.high)));
96 	}
97 	return (TRUE);  /* TRUE => open ended set of problems */
98 }
99 
100 /*
101  * XDR the MSG_DENIED part of a reply message union
102  */
103 bool_t
104 xdr_rejected_reply(XDR *xdrs, struct rejected_reply *rr)
105 {
106 	/* personalized union, rather than calling xdr_union */
107 	if (!xdr_enum(xdrs, (enum_t *)&(rr->rj_stat)))
108 		return (FALSE);
109 	switch (rr->rj_stat) {
110 	case RPC_MISMATCH:
111 		if (!xdr_u_int(xdrs, (uint_t *)&(rr->rj_vers.low)))
112 			return (FALSE);
113 		return (xdr_u_int(xdrs, (uint_t *)&(rr->rj_vers.high)));
114 	case AUTH_ERROR:
115 		return (xdr_enum(xdrs, (enum_t *)&(rr->rj_why)));
116 	}
117 	return (FALSE);
118 }
119 
120 /*
121  * XDR a reply message
122  */
123 bool_t
124 xdr_replymsg(XDR *xdrs, struct rpc_msg *rmsg)
125 {
126 	struct xdr_discrim reply_dscrm[3];
127 	rpc_inline_t *buf;
128 	struct accepted_reply *ar;
129 	struct opaque_auth *oa;
130 	uint_t rndup;
131 
132 	if (xdrs->x_op == XDR_ENCODE &&
133 	    rmsg->rm_reply.rp_stat == MSG_ACCEPTED &&
134 	    rmsg->rm_direction == REPLY &&
135 	    (buf = XDR_INLINE(xdrs, 6 * BYTES_PER_XDR_UNIT + (rndup =
136 	    RNDUP(rmsg->rm_reply.rp_acpt.ar_verf.oa_length)))) != NULL) {
137 		IXDR_PUT_INT32(buf, rmsg->rm_xid);
138 		IXDR_PUT_ENUM(buf, rmsg->rm_direction);
139 		IXDR_PUT_ENUM(buf, rmsg->rm_reply.rp_stat);
140 		ar = &rmsg->rm_reply.rp_acpt;
141 		oa = &ar->ar_verf;
142 		IXDR_PUT_ENUM(buf, oa->oa_flavor);
143 		IXDR_PUT_INT32(buf, oa->oa_length);
144 		if (oa->oa_length) {
145 			(void) memcpy(buf, oa->oa_base, oa->oa_length);
146 /* LINTED pointer alignment */
147 			buf = (rpc_inline_t *)(((caddr_t)buf) + oa->oa_length);
148 		}
149 		if ((rndup = (rndup - oa->oa_length)) > 0) {
150 			(void) memset((caddr_t)buf, 0, rndup);
151 /* LINTED pointer alignment */
152 			buf = (rpc_inline_t *)(((caddr_t)buf) + rndup);
153 		}
154 		/*
155 		 * stat and rest of reply, copied from xdr_accepted_reply
156 		 */
157 		IXDR_PUT_ENUM(buf, ar->ar_stat);
158 		switch (ar->ar_stat) {
159 		case SUCCESS:
160 			return ((*(ar->ar_results.proc))
161 			    (xdrs, ar->ar_results.where));
162 		case PROG_MISMATCH:
163 			if (!xdr_u_int(xdrs, (uint_t *)&(ar->ar_vers.low)))
164 				return (FALSE);
165 			return (xdr_u_int(xdrs, (uint_t *)&(ar->ar_vers.high)));
166 		}
167 		return (TRUE);
168 	}
169 	if (xdrs->x_op == XDR_DECODE &&
170 	    (buf = XDR_INLINE(xdrs, 3 * BYTES_PER_XDR_UNIT)) != NULL) {
171 		rmsg->rm_xid = IXDR_GET_INT32(buf);
172 		rmsg->rm_direction = IXDR_GET_ENUM(buf, enum msg_type);
173 		if (rmsg->rm_direction != REPLY)
174 			return (FALSE);
175 		rmsg->rm_reply.rp_stat = IXDR_GET_ENUM(buf, enum reply_stat);
176 		if (rmsg->rm_reply.rp_stat != MSG_ACCEPTED) {
177 			if (rmsg->rm_reply.rp_stat == MSG_DENIED)
178 				return (xdr_rejected_reply(xdrs,
179 				    &rmsg->rm_reply.rp_rjct));
180 			return (FALSE);
181 		}
182 		ar = &rmsg->rm_reply.rp_acpt;
183 		oa = &ar->ar_verf;
184 		buf = XDR_INLINE(xdrs, 2 * BYTES_PER_XDR_UNIT);
185 		if (buf != NULL) {
186 			oa->oa_flavor = IXDR_GET_ENUM(buf, enum_t);
187 			oa->oa_length = IXDR_GET_INT32(buf);
188 		} else {
189 			if (xdr_enum(xdrs, &oa->oa_flavor) == FALSE ||
190 			    xdr_u_int(xdrs, &oa->oa_length) == FALSE)
191 				return (FALSE);
192 		}
193 		if (oa->oa_length) {
194 			if (oa->oa_length > MAX_AUTH_BYTES)
195 				return (FALSE);
196 			if (oa->oa_base == NULL) {
197 				oa->oa_base = malloc(oa->oa_length);
198 				if (oa->oa_base == NULL) {
199 					syslog(LOG_ERR,
200 					    "xdr_replymsg : out of memory.");
201 					rpc_callerr.re_status = RPC_SYSTEMERROR;
202 					return (FALSE);
203 				}
204 			}
205 			buf = XDR_INLINE(xdrs, RNDUP(oa->oa_length));
206 			if (buf == NULL) {
207 				if (xdr_opaque(xdrs, oa->oa_base,
208 				    oa->oa_length) == FALSE)
209 					return (FALSE);
210 			} else {
211 				(void) memcpy(oa->oa_base, buf, oa->oa_length);
212 			}
213 		}
214 		/*
215 		 * stat and rest of reply, copied from
216 		 * xdr_accepted_reply
217 		 */
218 		if (!xdr_enum(xdrs, (enum_t *)&ar->ar_stat))
219 			return (FALSE);
220 		switch (ar->ar_stat) {
221 		case SUCCESS:
222 			return ((*(ar->ar_results.proc))
223 			    (xdrs, ar->ar_results.where));
224 		case PROG_MISMATCH:
225 			if (!xdr_u_int(xdrs, (uint_t *)&(ar->ar_vers.low)))
226 				return (FALSE);
227 			return (xdr_u_int(xdrs, (uint_t *)&(ar->ar_vers.high)));
228 		}
229 		return (TRUE);
230 	}
231 
232 	reply_dscrm[0].value = (int)MSG_ACCEPTED;
233 	reply_dscrm[0].proc = (xdrproc_t)xdr_accepted_reply;
234 	reply_dscrm[1].value = (int)MSG_DENIED;
235 	reply_dscrm[1].proc =  (xdrproc_t)xdr_rejected_reply;
236 	reply_dscrm[2].value = __dontcare__;
237 	reply_dscrm[2].proc = NULL_xdrproc_t;
238 	if (xdr_u_int(xdrs, &(rmsg->rm_xid)) &&
239 	    xdr_enum(xdrs, (enum_t *)&(rmsg->rm_direction)) &&
240 	    (rmsg->rm_direction == REPLY))
241 		return (xdr_union(xdrs, (enum_t *)&(rmsg->rm_reply.rp_stat),
242 		    (caddr_t)&(rmsg->rm_reply.ru),
243 		    reply_dscrm, NULL_xdrproc_t));
244 	return (FALSE);
245 }
246 
247 /*
248  * Serializes the "static part" of a call message header.
249  * The fields include: rm_xid, rm_direction, rpcvers, prog, and vers.
250  * The rm_xid is not really static, but the user can easily munge on the fly.
251  */
252 bool_t
253 xdr_callhdr(XDR *xdrs, struct rpc_msg *cmsg)
254 {
255 	cmsg->rm_direction = CALL;
256 	cmsg->rm_call.cb_rpcvers = RPC_MSG_VERSION;
257 	if (xdrs->x_op == XDR_ENCODE &&
258 	    xdr_u_int(xdrs, &(cmsg->rm_xid)) &&
259 	    xdr_enum(xdrs, (enum_t *)&(cmsg->rm_direction)) &&
260 	    xdr_u_int(xdrs, (uint_t *)&(cmsg->rm_call.cb_rpcvers)) &&
261 	    xdr_u_int(xdrs, (uint_t *)&(cmsg->rm_call.cb_prog))) {
262 		return (xdr_u_int(xdrs, (uint_t *)&(cmsg->rm_call.cb_vers)));
263 	}
264 	return (FALSE);
265 }
266 
267 /* ************************** Client utility routine ************* */
268 
269 static void
270 accepted(enum accept_stat acpt_stat, struct rpc_err *error)
271 {
272 	switch (acpt_stat) {
273 
274 	case PROG_UNAVAIL:
275 		error->re_status = RPC_PROGUNAVAIL;
276 		return;
277 
278 	case PROG_MISMATCH:
279 		error->re_status = RPC_PROGVERSMISMATCH;
280 		return;
281 
282 	case PROC_UNAVAIL:
283 		error->re_status = RPC_PROCUNAVAIL;
284 		return;
285 
286 	case GARBAGE_ARGS:
287 		error->re_status = RPC_CANTDECODEARGS;
288 		return;
289 
290 	case SYSTEM_ERR:
291 		error->re_status = RPC_SYSTEMERROR;
292 		return;
293 
294 	case SUCCESS:
295 		error->re_status = RPC_SUCCESS;
296 		return;
297 	}
298 	/* something's wrong, but we don't know what ... */
299 	error->re_status = RPC_FAILED;
300 	error->re_lb.s1 = (int32_t)MSG_ACCEPTED;
301 	error->re_lb.s2 = (int32_t)acpt_stat;
302 }
303 
304 static void
305 rejected(enum reject_stat rjct_stat, struct rpc_err *error)
306 {
307 	switch (rjct_stat) {
308 	case RPC_MISMATCH:
309 		error->re_status = RPC_VERSMISMATCH;
310 		return;
311 
312 	case AUTH_ERROR:
313 		error->re_status = RPC_AUTHERROR;
314 		return;
315 	}
316 	/* something's wrong, but we don't know what ... */
317 	error->re_status = RPC_FAILED;
318 	error->re_lb.s1 = (int32_t)MSG_DENIED;
319 	error->re_lb.s2 = (int32_t)rjct_stat;
320 }
321 
322 /*
323  * given a reply message, fills in the error
324  */
325 void
326 __seterr_reply(struct rpc_msg *msg, struct rpc_err *error)
327 {
328 	/* optimized for normal, SUCCESSful case */
329 	switch (msg->rm_reply.rp_stat) {
330 	case MSG_ACCEPTED:
331 		if (msg->acpted_rply.ar_stat == SUCCESS) {
332 			error->re_status = RPC_SUCCESS;
333 			return;
334 		};
335 		accepted(msg->acpted_rply.ar_stat, error);
336 		break;
337 
338 	case MSG_DENIED:
339 		rejected(msg->rjcted_rply.rj_stat, error);
340 		break;
341 
342 	default:
343 		error->re_status = RPC_FAILED;
344 		error->re_lb.s1 = (int32_t)(msg->rm_reply.rp_stat);
345 		break;
346 	}
347 
348 	switch (error->re_status) {
349 	case RPC_VERSMISMATCH:
350 		error->re_vers.low = msg->rjcted_rply.rj_vers.low;
351 		error->re_vers.high = msg->rjcted_rply.rj_vers.high;
352 		break;
353 
354 	case RPC_AUTHERROR:
355 		error->re_why = msg->rjcted_rply.rj_why;
356 		break;
357 
358 	case RPC_PROGVERSMISMATCH:
359 		error->re_vers.low = msg->acpted_rply.ar_vers.low;
360 		error->re_vers.high = msg->acpted_rply.ar_vers.high;
361 		break;
362 	}
363 }
364