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