1 /*
2  * rpc_callmsg.c
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 <string.h>
35 #include <sys/param.h>
36 #include <rpc/rpc.h>
37 #include <shlib-compat.h>
38 
39 /*
40  * XDR a call message
41  */
42 bool_t
xdr_callmsg(XDR * xdrs,struct rpc_msg * cmsg)43 xdr_callmsg (XDR *xdrs, struct rpc_msg *cmsg)
44 {
45   int32_t *buf;
46   struct opaque_auth *oa;
47 
48   if (xdrs->x_op == XDR_ENCODE)
49     {
50       if (cmsg->rm_call.cb_cred.oa_length > MAX_AUTH_BYTES)
51 	{
52 	  return (FALSE);
53 	}
54       if (cmsg->rm_call.cb_verf.oa_length > MAX_AUTH_BYTES)
55 	{
56 	  return (FALSE);
57 	}
58       buf = XDR_INLINE (xdrs, 8 * BYTES_PER_XDR_UNIT
59 			+ RNDUP (cmsg->rm_call.cb_cred.oa_length)
60 			+ 2 * BYTES_PER_XDR_UNIT
61 			+ RNDUP (cmsg->rm_call.cb_verf.oa_length));
62       if (buf != NULL)
63 	{
64 	  (void) IXDR_PUT_LONG (buf, cmsg->rm_xid);
65 	  (void) IXDR_PUT_ENUM (buf, cmsg->rm_direction);
66 	  if (cmsg->rm_direction != CALL)
67 	    return FALSE;
68 	  (void) IXDR_PUT_LONG (buf, cmsg->rm_call.cb_rpcvers);
69 	  if (cmsg->rm_call.cb_rpcvers != RPC_MSG_VERSION)
70 	    return FALSE;
71 	  (void) IXDR_PUT_LONG (buf, cmsg->rm_call.cb_prog);
72 	  (void) IXDR_PUT_LONG (buf, cmsg->rm_call.cb_vers);
73 	  (void) IXDR_PUT_LONG (buf, cmsg->rm_call.cb_proc);
74 	  oa = &cmsg->rm_call.cb_cred;
75 	  (void) IXDR_PUT_ENUM (buf, oa->oa_flavor);
76 	  (void) IXDR_PUT_INT32 (buf, oa->oa_length);
77 	  if (oa->oa_length)
78 	    {
79 	      memcpy ((caddr_t) buf, oa->oa_base, oa->oa_length);
80 	      buf = (int32_t *) ((char *) buf + RNDUP (oa->oa_length));
81 	    }
82 	  oa = &cmsg->rm_call.cb_verf;
83 	  (void) IXDR_PUT_ENUM (buf, oa->oa_flavor);
84 	  (void) IXDR_PUT_INT32 (buf, oa->oa_length);
85 	  if (oa->oa_length)
86 	    {
87 	      memcpy ((caddr_t) buf, oa->oa_base, oa->oa_length);
88 	      /* no real need....
89 		 buf = (long *) ((char *) buf + RNDUP(oa->oa_length));
90 	       */
91 	    }
92 	  return TRUE;
93 	}
94     }
95   if (xdrs->x_op == XDR_DECODE)
96     {
97       buf = XDR_INLINE (xdrs, 8 * BYTES_PER_XDR_UNIT);
98       if (buf != NULL)
99 	{
100 	  cmsg->rm_xid = IXDR_GET_LONG (buf);
101 	  cmsg->rm_direction = IXDR_GET_ENUM (buf, enum msg_type);
102 	  if (cmsg->rm_direction != CALL)
103 	    {
104 	      return FALSE;
105 	    }
106 	  cmsg->rm_call.cb_rpcvers = IXDR_GET_LONG (buf);
107 	  if (cmsg->rm_call.cb_rpcvers != RPC_MSG_VERSION)
108 	    {
109 	      return FALSE;
110 	    }
111 	  cmsg->rm_call.cb_prog = IXDR_GET_LONG (buf);
112 	  cmsg->rm_call.cb_vers = IXDR_GET_LONG (buf);
113 	  cmsg->rm_call.cb_proc = IXDR_GET_LONG (buf);
114 	  oa = &cmsg->rm_call.cb_cred;
115 	  oa->oa_flavor = IXDR_GET_ENUM (buf, enum_t);
116 	  oa->oa_length = IXDR_GET_INT32 (buf);
117 	  if (oa->oa_length)
118 	    {
119 	      if (oa->oa_length > MAX_AUTH_BYTES)
120 		return FALSE;
121 	      if (oa->oa_base == NULL)
122 		{
123 		  oa->oa_base = (caddr_t)
124 		    mem_alloc (oa->oa_length);
125 		}
126 	      buf = XDR_INLINE (xdrs, RNDUP (oa->oa_length));
127 	      if (buf == NULL)
128 		{
129 		  if (xdr_opaque (xdrs, oa->oa_base,
130 				  oa->oa_length) == FALSE)
131 		    return FALSE;
132 		}
133 	      else
134 		{
135 		  memcpy (oa->oa_base, (caddr_t) buf, oa->oa_length);
136 		  /* no real need....
137 		     buf = (long *) ((char *) buf
138 		     + RNDUP(oa->oa_length));
139 		   */
140 		}
141 	    }
142 	  oa = &cmsg->rm_call.cb_verf;
143 	  buf = XDR_INLINE (xdrs, 2 * BYTES_PER_XDR_UNIT);
144 	  if (buf == NULL)
145 	    {
146 	      if (xdr_enum (xdrs, &oa->oa_flavor) == FALSE ||
147 		  xdr_u_int (xdrs, &oa->oa_length) == FALSE)
148 		{
149 		  return FALSE;
150 		}
151 	    }
152 	  else
153 	    {
154 	      oa->oa_flavor = IXDR_GET_ENUM (buf, enum_t);
155 	      oa->oa_length = IXDR_GET_INT32 (buf);
156 	    }
157 	  if (oa->oa_length)
158 	    {
159 	      if (oa->oa_length > MAX_AUTH_BYTES)
160 		return FALSE;
161 	      if (oa->oa_base == NULL)
162 		{
163 		  oa->oa_base = (caddr_t)
164 		    mem_alloc (oa->oa_length);
165 		}
166 	      buf = XDR_INLINE (xdrs, RNDUP (oa->oa_length));
167 	      if (buf == NULL)
168 		{
169 		  if (xdr_opaque (xdrs, oa->oa_base,
170 				  oa->oa_length) == FALSE)
171 		    return FALSE;
172 		}
173 	      else
174 		{
175 		  memcpy (oa->oa_base, (caddr_t) buf, oa->oa_length);
176 		  /* no real need...
177 		     buf = (long *) ((char *) buf
178 		     + RNDUP(oa->oa_length));
179 		   */
180 		}
181 	    }
182 	  return TRUE;
183 	}
184     }
185   if (
186        xdr_u_long (xdrs, &(cmsg->rm_xid)) &&
187        xdr_enum (xdrs, (enum_t *) & (cmsg->rm_direction)) &&
188        (cmsg->rm_direction == CALL) &&
189        xdr_u_long (xdrs, &(cmsg->rm_call.cb_rpcvers)) &&
190        (cmsg->rm_call.cb_rpcvers == RPC_MSG_VERSION) &&
191        xdr_u_long (xdrs, &(cmsg->rm_call.cb_prog)) &&
192        xdr_u_long (xdrs, &(cmsg->rm_call.cb_vers)) &&
193        xdr_u_long (xdrs, &(cmsg->rm_call.cb_proc)) &&
194        xdr_opaque_auth (xdrs, &(cmsg->rm_call.cb_cred)))
195     return xdr_opaque_auth (xdrs, &(cmsg->rm_call.cb_verf));
196   return FALSE;
197 }
198 libc_hidden_nolink_sunrpc (xdr_callmsg, GLIBC_2_0)
199