xref: /dragonfly/lib/libc/rpc/rpc_callmsg.c (revision 65ebff40bad065cba7ef5daf1c3c241e91d349af)
1 /*-
2  * Copyright (c) 2009, Sun Microsystems, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * - Redistributions of source code must retain the above copyright notice,
8  *   this list of conditions and the following disclaimer.
9  * - Redistributions in binary form must reproduce the above copyright notice,
10  *   this list of conditions and the following disclaimer in the documentation
11  *   and/or other materials provided with the distribution.
12  * - Neither the name of Sun Microsystems, Inc. nor the names of its
13  *   contributors may be used to endorse or promote products derived
14  *   from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  *
28  * @(#)rpc_callmsg.c 1.4 87/08/11 Copyr 1984 Sun Micro
29  * @(#)rpc_callmsg.c          2.1 88/07/29 4.0 RPCSRC
30  * $NetBSD: rpc_callmsg.c,v 1.16 2000/07/14 08:40:42 fvdl Exp $
31  * $FreeBSD: src/lib/libc/rpc/rpc_callmsg.c,v 1.13 2007/11/20 01:51:20 jb Exp $
32  */
33 
34 /*
35  * rpc_callmsg.c
36  *
37  * Copyright (C) 1984, Sun Microsystems, Inc.
38  *
39  */
40 
41 #include "namespace.h"
42 #include <assert.h>
43 #include <stdlib.h>
44 #include <string.h>
45 
46 #include <rpc/rpc.h>
47 #include "un-namespace.h"
48 
49 /*
50  * XDR a call message
51  */
52 bool_t
xdr_callmsg(XDR * xdrs,struct rpc_msg * cmsg)53 xdr_callmsg(XDR *xdrs, struct rpc_msg *cmsg)
54 {
55           enum msg_type *prm_direction;
56           int32_t *buf;
57           struct opaque_auth *oa;
58 
59           assert(xdrs != NULL);
60           assert(cmsg != NULL);
61 
62           if (xdrs->x_op == XDR_ENCODE) {
63                     if (cmsg->rm_call.cb_cred.oa_length > MAX_AUTH_BYTES) {
64                               return (FALSE);
65                     }
66                     if (cmsg->rm_call.cb_verf.oa_length > MAX_AUTH_BYTES) {
67                               return (FALSE);
68                     }
69                     buf = XDR_INLINE(xdrs, 8 * BYTES_PER_XDR_UNIT
70                               + RNDUP(cmsg->rm_call.cb_cred.oa_length)
71                               + 2 * BYTES_PER_XDR_UNIT
72                               + RNDUP(cmsg->rm_call.cb_verf.oa_length));
73                     if (buf != NULL) {
74                               IXDR_PUT_INT32(buf, cmsg->rm_xid);
75                               IXDR_PUT_ENUM(buf, cmsg->rm_direction);
76                               if (cmsg->rm_direction != CALL) {
77                                         return (FALSE);
78                               }
79                               IXDR_PUT_INT32(buf, cmsg->rm_call.cb_rpcvers);
80                               if (cmsg->rm_call.cb_rpcvers != RPC_MSG_VERSION) {
81                                         return (FALSE);
82                               }
83                               IXDR_PUT_INT32(buf, cmsg->rm_call.cb_prog);
84                               IXDR_PUT_INT32(buf, cmsg->rm_call.cb_vers);
85                               IXDR_PUT_INT32(buf, cmsg->rm_call.cb_proc);
86                               oa = &cmsg->rm_call.cb_cred;
87                               IXDR_PUT_ENUM(buf, oa->oa_flavor);
88                               IXDR_PUT_INT32(buf, oa->oa_length);
89                               if (oa->oa_length) {
90                                         memmove(buf, oa->oa_base, oa->oa_length);
91                                         buf += RNDUP(oa->oa_length) / sizeof (int32_t);
92                               }
93                               oa = &cmsg->rm_call.cb_verf;
94                               IXDR_PUT_ENUM(buf, oa->oa_flavor);
95                               IXDR_PUT_INT32(buf, oa->oa_length);
96                               if (oa->oa_length) {
97                                         memmove(buf, oa->oa_base, oa->oa_length);
98                                         /* no real need....
99                                         buf += RNDUP(oa->oa_length) / sizeof (int32_t);
100                                         */
101                               }
102                               return (TRUE);
103                     }
104           }
105           if (xdrs->x_op == XDR_DECODE) {
106                     buf = XDR_INLINE(xdrs, 8 * BYTES_PER_XDR_UNIT);
107                     if (buf != NULL) {
108                               cmsg->rm_xid = IXDR_GET_U_INT32(buf);
109                               cmsg->rm_direction = IXDR_GET_ENUM(buf, enum msg_type);
110                               if (cmsg->rm_direction != CALL) {
111                                         return (FALSE);
112                               }
113                               cmsg->rm_call.cb_rpcvers = IXDR_GET_U_INT32(buf);
114                               if (cmsg->rm_call.cb_rpcvers != RPC_MSG_VERSION) {
115                                         return (FALSE);
116                               }
117                               cmsg->rm_call.cb_prog = IXDR_GET_U_INT32(buf);
118                               cmsg->rm_call.cb_vers = IXDR_GET_U_INT32(buf);
119                               cmsg->rm_call.cb_proc = IXDR_GET_U_INT32(buf);
120                               oa = &cmsg->rm_call.cb_cred;
121                               oa->oa_flavor = IXDR_GET_ENUM(buf, enum_t);
122                               oa->oa_length = (u_int)IXDR_GET_U_INT32(buf);
123                               if (oa->oa_length) {
124                                         if (oa->oa_length > MAX_AUTH_BYTES) {
125                                                   return (FALSE);
126                                         }
127                                         if (oa->oa_base == NULL) {
128                                                   oa->oa_base = (caddr_t)
129                                                       mem_alloc(oa->oa_length);
130                                                   if (oa->oa_base == NULL)
131                                                             return (FALSE);
132                                         }
133                                         buf = XDR_INLINE(xdrs, RNDUP(oa->oa_length));
134                                         if (buf == NULL) {
135                                                   if (xdr_opaque(xdrs, oa->oa_base,
136                                                       oa->oa_length) == FALSE) {
137                                                             return (FALSE);
138                                                   }
139                                         } else {
140                                                   memmove(oa->oa_base, buf,
141                                                       oa->oa_length);
142                                                   /* no real need....
143                                                   buf += RNDUP(oa->oa_length) /
144                                                             sizeof (int32_t);
145                                                   */
146                                         }
147                               }
148                               oa = &cmsg->rm_call.cb_verf;
149                               buf = XDR_INLINE(xdrs, 2 * BYTES_PER_XDR_UNIT);
150                               if (buf == NULL) {
151                                         if (xdr_enum(xdrs, &oa->oa_flavor) == FALSE ||
152                                             xdr_u_int(xdrs, &oa->oa_length) == FALSE) {
153                                                   return (FALSE);
154                                         }
155                               } else {
156                                         oa->oa_flavor = IXDR_GET_ENUM(buf, enum_t);
157                                         oa->oa_length = (u_int)IXDR_GET_U_INT32(buf);
158                               }
159                               if (oa->oa_length) {
160                                         if (oa->oa_length > MAX_AUTH_BYTES) {
161                                                   return (FALSE);
162                                         }
163                                         if (oa->oa_base == NULL) {
164                                                   oa->oa_base = (caddr_t)
165                                                       mem_alloc(oa->oa_length);
166                                                   if (oa->oa_base == NULL)
167                                                             return (FALSE);
168                                         }
169                                         buf = XDR_INLINE(xdrs, RNDUP(oa->oa_length));
170                                         if (buf == NULL) {
171                                                   if (xdr_opaque(xdrs, oa->oa_base,
172                                                       oa->oa_length) == FALSE) {
173                                                             return (FALSE);
174                                                   }
175                                         } else {
176                                                   memmove(oa->oa_base, buf,
177                                                       oa->oa_length);
178                                                   /* no real need...
179                                                   buf += RNDUP(oa->oa_length) /
180                                                             sizeof (int32_t);
181                                                   */
182                                         }
183                               }
184                               return (TRUE);
185                     }
186           }
187           prm_direction = &cmsg->rm_direction;
188           if (
189               xdr_u_int32_t(xdrs, &(cmsg->rm_xid)) &&
190               xdr_enum(xdrs, (enum_t *) prm_direction) &&
191               (cmsg->rm_direction == CALL) &&
192               xdr_rpcvers(xdrs, &(cmsg->rm_call.cb_rpcvers)) &&
193               (cmsg->rm_call.cb_rpcvers == RPC_MSG_VERSION) &&
194               xdr_rpcprog(xdrs, &(cmsg->rm_call.cb_prog)) &&
195               xdr_rpcvers(xdrs, &(cmsg->rm_call.cb_vers)) &&
196               xdr_rpcproc(xdrs, &(cmsg->rm_call.cb_proc)) &&
197               xdr_opaque_auth(xdrs, &(cmsg->rm_call.cb_cred)) )
198                     return (xdr_opaque_auth(xdrs, &(cmsg->rm_call.cb_verf)));
199           return (FALSE);
200 }
201