1957ae7c3dffe7f75be2374cd3bc77cee5809697
[openafs.git] / src / WINNT / afsd / msrpc.h
1 /*
2  * Copyright (c) 2009 Secure Endpoints Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person
5  * obtaining a copy of this software and associated documentation
6  * files (the "Software"), to deal in the Software without
7  * restriction, including without limitation the rights to use, copy,
8  * modify, merge, publish, distribute, sublicense, and/or sell copies
9  * of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24
25 #pragma once
26
27 #include <Rpc.h>
28 #include <RpcdceP.h>
29 #include <RpcNdr.h>
30
31 #define I_RpcGetBuffer MSRPC_I_GetBuffer
32
33 #define I_RpcFreeBuffer MSRPC_I_FreeBuffer
34
35 #define NdrServerInitializeNew MSRPC_NdrServerInitializeNew
36
37 /*! \brief Maximum size of an accepted message
38
39    This value is arbitrary.  The chosen value is larger than the
40    maximum fragment size used by the SMB RPC transport and is only
41    meant to mitigate the damage that could be done from a malformed
42    packet.
43  */
44 #define MAX_RPC_MSG_SIZE 102400
45
46 /*! \brief Default message size */
47 #define DEF_RPC_MSG_SIZE 2048
48
49 /*! \brief Status of a msrpc_call object */
50 typedef enum msrpc_call_status {
51     MSRPC_CALL_NONE = 0,
52     MSRPC_CALL_MSGRECEIVED,
53     MSRPC_CALL_DISPATCHED,
54     MSRPC_CALL_MSGSENDING,
55     MSRPC_CALL_MSGSENT,
56     MSRPC_CALL_SKIP,
57 } MSRPC_CALL_STATUS;
58
59 typedef enum PDU_type {
60     PDU_TYPE_REQUEST = 0,
61     PDU_TYPE_PING = 1,
62     PDU_TYPE_RESPONSE = 2,
63     PDU_TYPE_FAULT = 3,
64     PDU_TYPE_WORKING = 4,
65     PDU_TYPE_NOCALL = 5,
66     PDU_TYPE_REJECT = 6,
67     PDU_TYPE_ACK = 7,
68     PDU_TYPE_CL_CANCEL = 8,
69     PDU_TYPE_FACK = 9,
70     PDU_TYPE_CANCEL_ACK = 10,
71     PDU_TYPE_BIND = 11,
72     PDU_TYPE_BIND_ACK = 12,
73     PDU_TYPE_BIND_NAK = 13,
74     PDU_TYPE_ALTER_CONTEXT = 14,
75     PDU_TYPE_ALTER_CONTEXT_RESP = 15,
76     PDU_TYPE_SHUTDOWN = 16,
77     PDU_TYPE_CO_CANCEL = 18,
78     PDU_TYPE_ORPHANED = 19
79 } PDU_TYPE;
80
81 typedef enum msrpc_bind_reject_reason {
82     BIND_REJ_REASON_NOT_SPECIFIED = 0,
83     BIND_REJ_TEMPORARY_CONGESTION = 1,
84     BIND_REJ_LOCAL_LIMIT_EXCEEDED = 2,
85     BIND_REJ_CALLED_PADDR_UNKNOWN = 3, /* not used */
86     BIND_REJ_PROTOCOL_VERSION_NOT_SUPPORTED = 4,
87     BIND_REJ_DEFAULT_CONTEXT_NOT_SUPPORTED = 5, /* not used */
88     BIND_REJ_USER_DATA_NOT_READABLE = 6, /* not used */
89     BIND_REJ_NO_PSAP_AVAILABLE = 7 /* not used */
90 } BIND_REJECT_REASON;
91
92 typedef unsigned __int8     byte;
93 typedef unsigned __int8     u_int8;
94 typedef unsigned __int16    u_int16;
95 typedef unsigned __int32    u_int32;
96 typedef unsigned __int64    u_int64;
97
98 /*! \brief Common RPC envelope header */
99 typedef
100 struct E_CommonHeader {
101     u_int8  rpc_vers;           /*!< 00:01 RPC version == 5 */
102     u_int8  rpc_vers_minor;     /*!< 01:01 minor version */
103     u_int8  PTYPE;              /*!< 02:01 packet type */
104     u_int8  pfc_flags;          /*!< 03:01 flags (see PFC_... ) */
105
106 #define PFC_FIRST_FRAG           0x01 /* First fragment */
107 #define PFC_LAST_FRAG            0x02 /* Last fragment */
108 #define PFC_PENDING_CANCEL       0x04 /* Cancel was pending at sender */
109 #define PFC_RESERVED_1           0x08
110 #define PFC_CONC_MPX             0x10 /* supports concurrent multiplexing
111                                        * of a single connection. */
112 #define PFC_DID_NOT_EXECUTE      0x20 /* only meaningful on `fault' packet;
113                                        * if true, guaranteed call did not
114                                        * execute. */
115 #define PFC_MAYBE                0x40 /* `maybe' call semantics requested */
116 #define PFC_OBJECT_UUID          0x80 /* if true, a non-nil object UUID
117                                        * was specified in the handle, and
118                                        * is present in the optional object
119                                        * field. If false, the object field
120                                        * is omitted. */
121
122     byte    packed_drep[4]; /*!< 04:04 NDR data representation format label */
123     u_int16 frag_length;    /*!< 08:02 total length of fragment */
124     u_int16 auth_length;    /*!< 10:02 length of auth_value */
125     u_int32 call_id;        /*!< 12:04 call identifier */
126 } E_CommonHeader;
127
128 typedef   u_int16   p_context_id_t; /* local context identifier */
129
130 /*! \brief RPC message buffer
131  */
132 typedef struct msrpc_buffer {
133     BYTE * buf_data;
134     unsigned int buf_length;
135     unsigned int buf_alloc;
136     unsigned int buf_pos;
137
138     PDU_TYPE pdu_type;
139 } msrpc_buffer;
140
141 typedef struct cm_user cm_user_t;
142
143 /*! \brief RPC call
144
145    A single RPC call.  This might even be a single fragment of a
146    message.
147  */
148 typedef struct msrpc_call {
149     struct msrpc_call * next;
150     struct msrpc_call * prev;
151
152     MSRPC_CALL_STATUS status;
153
154     u_int32 call_id;           /*!< Call ID */
155     p_context_id_t context_id;  /*!< presentation context ID (last seen) */
156
157     E_CommonHeader * in_header; /*!< Pointer to the header of the
158                                   input packet.  Only valid during a
159                                   request dispatch. */
160
161     msrpc_buffer in;            /*!< Input buffer */
162
163     msrpc_buffer out;           /*!< Output buffer */
164
165     RPC_MESSAGE msg;
166
167     cm_user_t *cm_userp;        /*!< Held reference to the cm_user_t
168                                    associated with this call. */
169 } msrpc_call;
170
171 /*! \brief RPC connection structure
172
173    Represents a single RPC connection.  Can support multiple queued
174    RPCs.
175 */
176 typedef struct _msrpc_conn {
177     unsigned int max_xmit_frag; /*!< Maximum size of fragment
178                                    client->server */
179     unsigned int max_recv_frag; /*!< Maximum size of fragment
180                                    server->client */
181     u_int32 assoc_group_id;     /*!< Association group ID for the
182                                    connection. */
183
184     unsigned int rpc_vers;      /*!< RPC Protocol version. Always 5 */
185     unsigned int rpc_vers_minor; /*!< Minor version number.  0 or 1 */
186
187     char * secondary_address;   /*!< Secondary address for the
188                                    connection. */
189
190     RPC_SERVER_INTERFACE * interface;
191                                 /*!< Selected server interface.  Only
192                                    valid after a successful bind. */
193
194     msrpc_call * head;          /*!< Queue of msrpc_call objects for
195                                    the connection. */
196     msrpc_call * tail;
197 } msrpc_conn;
198
199
200 void
201 MSRPC_Init(void);
202
203 void
204 MSRPC_Shutdown(void);
205
206 int
207 MSRPC_InitConn(msrpc_conn * conn, const char * secondary_address);
208
209 int
210 MSRPC_FreeConn(msrpc_conn * conn);
211
212 int
213 MSRPC_WriteMessage(msrpc_conn * conn, BYTE * buffer, unsigned int len,
214                    cm_user_t * userp);
215
216 int
217 MSRPC_PrepareRead(msrpc_conn * conn);
218
219 int
220 MSRPC_ReadMessageLength(msrpc_conn * conn, unsigned int max_len);
221
222 int
223 MSRPC_ReadMessage(msrpc_conn * conn, BYTE *buffer, unsigned int len);
224
225 RPC_STATUS
226 RPC_ENTRY
227 I_RpcGetBuffer (IN OUT RPC_MESSAGE __RPC_FAR * Message);
228
229 RPC_STATUS
230 RPC_ENTRY
231 I_RpcFreeBuffer (IN OUT RPC_MESSAGE __RPC_FAR * Message);
232
233 unsigned char  *
234 RPC_ENTRY
235 NdrServerInitializeNew(
236     PRPC_MESSAGE            pRpcMsg,
237     PMIDL_STUB_MESSAGE      pStubMsg,
238     PMIDL_STUB_DESC         pStubDescriptor
239     );
240
241 extern cm_user_t *
242 MSRPC_GetCmUser(void);
243
244 extern void RPC_SRVSVC_Init(void);
245
246 extern void RPC_SRVSVC_Shutdown(void);
247
248 extern int
249 MSRPC_IsWellKnownService(const clientchar_t * lastNamep);