1/*
2 * Copyright (c) 2010 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1.  Redistributions of source code must retain the above copyright
11 *     notice, this list of conditions and the following disclaimer.
12 * 2.  Redistributions in binary form must reproduce the above copyright
13 *     notice, this list of conditions and the following disclaimer in the
14 *     documentation and/or other materials provided with the distribution.
15 * 3.  Neither the name of Apple Inc. ("Apple") nor the names of its
16 *     contributors may be used to endorse or promote products derived from
17 *     this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 * Portions of this software have been released under the following terms:
31 *
32 * (c) Copyright 1989-1993 OPEN SOFTWARE FOUNDATION, INC.
33 * (c) Copyright 1989-1993 HEWLETT-PACKARD COMPANY
34 * (c) Copyright 1989-1993 DIGITAL EQUIPMENT CORPORATION
35 *
36 * To anyone who acknowledges that this file is provided "AS IS"
37 * without any express or implied warranty:
38 * permission to use, copy, modify, and distribute this file for any
39 * purpose is hereby granted without fee, provided that the above
40 * copyright notices and this notice appears in all source code copies,
41 * and that none of the names of Open Software Foundation, Inc., Hewlett-
42 * Packard Company or Digital Equipment Corporation be used
43 * in advertising or publicity pertaining to distribution of the software
44 * without specific, written prior permission.  Neither Open Software
45 * Foundation, Inc., Hewlett-Packard Company nor Digital
46 * Equipment Corporation makes any representations about the suitability
47 * of this software for any purpose.
48 *
49 * Copyright (c) 2007, Novell, Inc. All rights reserved.
50 * Redistribution and use in source and binary forms, with or without
51 * modification, are permitted provided that the following conditions
52 * are met:
53 *
54 * 1.  Redistributions of source code must retain the above copyright
55 *     notice, this list of conditions and the following disclaimer.
56 * 2.  Redistributions in binary form must reproduce the above copyright
57 *     notice, this list of conditions and the following disclaimer in the
58 *     documentation and/or other materials provided with the distribution.
59 * 3.  Neither the name of Novell Inc. nor the names of its contributors
60 *     may be used to endorse or promote products derived from this
61 *     this software without specific prior written permission.
62 *
63 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
64 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
65 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
66 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY
67 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
68 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
69 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
70 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
71 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
72 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
73 *
74 * @APPLE_LICENSE_HEADER_END@
75 */
76
77/*
78**
79**  NAME
80**
81**      comp.h
82**
83**  FACILITY:
84**
85**      Remote Procedure Call (RPC)
86**
87**  ABSTRACT:
88**
89**  Definitions of types/constants internal to the Common Communications
90**  Service component of the RPC runtime.
91**
92**
93*/
94
95#ifndef _COMP_H
96#define _COMP_H
97
98/***********************************************************************/
99#include <comprot.h>    /* Externals for Protocol Services sub-component*/
100#include <comnaf.h>     /* Externals for NAF Services sub-component     */
101#include <comauth.h>    /* Externals for Auth. Services sub-component   */
102
103/*
104 * Accessor macros for the RPC Protocol Sequence ID table.
105 */
106
107#define RPC_PROTSEQ_INQ_SUPPORTED(id) \
108       (boolean)rpc_g_protseq_id[id].supported
109#define RPC_PROTSEQ_INQ_PROTSEQ_ID(id)      rpc_g_protseq_id[id].rpc_protseq_id
110#define RPC_PROTSEQ_INQ_PROT_ID(id)         rpc_g_protseq_id[id].rpc_protocol_id
111#define RPC_PROTSEQ_INQ_NAF_ID(id)          rpc_g_protseq_id[id].naf_id
112#define RPC_PROTSEQ_INQ_PROTSEQ(id)         rpc_g_protseq_id[id].rpc_protseq
113#define RPC_PROTSEQ_INQ_NET_IF_ID(id)       rpc_g_protseq_id[id].network_if_id
114
115
116/***********************************************************************/
117/*
118 * R P C _ P R O T O C O L _ I D _ E L T _ T
119 *
120 * The RPC Protocol ID table element structure.  An element describes
121 * a single RPC Protocol.
122 *
123 * The fields are:
124 *
125 *      prot_init       The address of an initialization routine in the
126 *                      Protocol Service that will be called by rpc__init.
127 *
128 *      prot_fork_handler  The address of a routine to call to handle
129 *                      protocol specific, fork-related processing.
130 *
131 *      rpc_protocol_id A constant identifier for this RPC Protocol.
132 *
133 *      call_epv        An entry point vector for the Call Services in
134 *                      the Protocol Service.
135 *
136 *      mgmt_epv        An entry point vector for the Management Services in
137 *                      the Protocol Service.
138 *
139 *      binding_epv     An entry point vector for the Binding Services
140 *                      in the Protocol Service.
141 *
142 *      network_epv     An entry point vector for the Network Services
143 *                      in the Protocol Service.
144 */
145typedef struct
146{
147    rpc_prot_init_fn_t          prot_init;
148    rpc_prot_fork_handler_fn_t  prot_fork_handler;
149    rpc_protocol_id_t           rpc_protocol_id;
150    rpc_prot_call_epv_t         *call_epv;
151    rpc_prot_mgmt_epv_t         *mgmt_epv;
152    rpc_prot_binding_epv_t      *binding_epv;
153    rpc_prot_network_epv_t      *network_epv;
154} rpc_protocol_id_elt_t, *rpc_protocol_id_elt_p_t;
155
156/*
157 * Accessor macros for the RPC Protocol ID table.
158 */
159#define RPC_PROTOCOL_INQ_SUPPORTED(id) \
160      (rpc_g_protocol_id[id].prot_init != NULL)
161#define RPC_PROTOCOL_INQ_CALL_EPV(id)       rpc_g_protocol_id[id].call_epv
162#define RPC_PROTOCOL_INQ_MGMT_EPV(id)       rpc_g_protocol_id[id].mgmt_epv
163#define RPC_PROTOCOL_INQ_BINDING_EPV(id)    rpc_g_protocol_id[id].binding_epv
164#define RPC_PROTOCOL_INQ_NETWORK_EPV(id)    rpc_g_protocol_id[id].network_epv
165
166/***********************************************************************/
167/*
168 * R P C _ G _ P R O T O C O L _ I D
169 *
170 * The RPC Protocol ID table.  Each RPC Protocol has an entry in this
171 * table.  This table is index by RPC Protocol ID.
172 *
173 * Note that the ".rpc_protocol_id" field of i'th element in the table
174 * is always "i".  While redundant, this is useful so that you can pass
175 * pointers to individual table elements.
176 */
177EXTERNAL rpc_protocol_id_elt_t   rpc_g_protocol_id[];
178
179/*
180 * Protocol Sequence ID Table
181 *
182 * This table contains the valid combination of protocol ids
183 * for upper floor 3 and the lower tower floors.
184 * This table maps each combination to the appropriate
185 * RPC protocol id sequence.
186 *
187 * The field num_floors provides for the number of significant
188 * floors comprising the RPC protocol sequence.
189 * This table is used only by comtwrref.c to validate towers, but we need
190 * it accessible by the dynamic loading code so that new modules can register
191 * with the runtime.
192 * We allocate extra space, since there may be mulitple entries: eg
193 * rpc_c_protseq_id_ncacn_osi_dna
194 * */
195typedef struct
196{
197    unsigned8               prefix;
198    idl_uuid_t                  uuid;
199} rpc_flr_prot_id_t, *rpc_flr_prot_id_p_t;
200
201typedef struct
202{
203    rpc_protseq_id_t        rpc_protseq_id;
204    unsigned8               num_floors;
205    rpc_flr_prot_id_t       floor_prot_ids[RPC_C_MAX_NUM_NETWORK_FLOORS + 1];
206} rpc_tower_prot_ids_t, *rpc_tower_prot_ids_p_t;
207EXTERNAL rpc_tower_prot_ids_t rpc_g_tower_prot_ids[RPC_C_PROTSEQ_ID_MAX*2];
208EXTERNAL unsigned32 rpc_g_tower_prot_id_number;	/* number of elts in rpc_g_tower_prot_ids */
209
210
211/***********************************************************************/
212/*
213 * R P C _ N A F _ I D _ E L T _ T
214 *
215 * The Network Address Family ID table element structure.  An element
216 * describes a single Network Address Family Extension.
217 *
218 * The fields are:
219 *
220 *      naf_init        The address of an initialization routine in the
221 *                      NAF Service that will be called by rpc__init
222 *
223 *      naf_id          A constant identifier for this NAF.
224 *
225 *      net_if_id       A constant identifier for the network interface
226 *                      type used in the NAF initialization routine (when
227 *                      determining if this NAF is supported).
228 *
229 *      naf_epv         An entry point vector for the NAF Service.
230 */
231typedef struct
232{
233    rpc_naf_init_fn_t        naf_init;
234    rpc_naf_id_t             naf_id;
235    rpc_network_if_id_t      network_if_id;
236    rpc_naf_epv_t            *epv;
237} rpc_naf_id_elt_t, *rpc_naf_id_elt_p_t;
238
239/*
240 * Accessor macros for the Network Address Family ID table.
241 */
242#define RPC_NAF_INQ_SUPPORTED(id)       (rpc_g_naf_id[id].naf_init != NULL)
243#define RPC_NAF_INQ_EPV(id)             rpc_g_naf_id[id].epv
244
245/***********************************************************************/
246/*
247 * R P C _ G _ N A F _ I D
248 *
249 * The Network Address Family ID table.  This table is indexed by a NAF
250 * ID.
251 *
252 * Each Network Address Family Extension has an entry in this table.
253 * Note that this is a sparse table because it uses the Unix Address
254 * Family ID's as NAF ID's.
255 *
256 * Note that the ".naf_id" field of i'th element in the table is always
257 * "i".  While redundant, this is useful so that you can pass pointers
258 * to individual table elements.
259 */
260EXTERNAL rpc_naf_id_elt_t   rpc_g_naf_id[RPC_C_NAF_ID_MAX];
261
262/***********************************************************************/
263/*
264 * R P C _ A U T H N _ P R O T O C O L _ I D _ E L T _ T
265 *
266 * The RPC Authentication Protocol ID table element structure.
267 *
268 * The fields are:
269 *
270 *      auth_init           The address of an initialization routine in the
271 *                          Authentication Service that will be called by
272 *                          rpc__init.
273 *
274 *      authn_protocol_id   A constant identifier for this Authentication Service.
275 *
276 *      dce_rpc_authn_protocol_id_t
277 *                          The value that goes into RPC protocol messages to
278 *                          identify which authentication protocol is in use.
279 *
280 *      epv                 An entry point vector for the Authentication Service
281 *                          functions.
282 *      rpc_prot_epv_tbl    A table, indexed by RPC protocol ID,
283 *                          containing that RPC protocol's specific
284 *                          interface to the Authentication Service.
285 *
286 * Note that the ".auth_protocol_id" contains API values (see
287 * "rpc_c_authn_..." constants in "rpc.idl").
288 * "dce_rpc_authn_protocol_id_t" contains architectural values that appear
289 * in network messages (see "dce_c_rpc_authn_protocol_..." constants in
290 * "nbase.idl").
291 */
292typedef struct
293{
294    rpc_auth_init_fn_t      auth_init;
295    rpc_authn_protocol_id_t authn_protocol_id;
296    dce_rpc_authn_protocol_id_t
297                            dce_rpc_authn_protocol_id;
298    rpc_auth_epv_t          *epv;
299    rpc_auth_rpc_prot_epv_tbl_t
300                            rpc_prot_epv_tbl;
301} rpc_authn_protocol_id_elt_t, *rpc_authn_protocol_id_elt_p_t;
302
303
304/***********************************************************************/
305/*
306 * R P C _ G _ A U T H N _ P R O T O C O L _ I D
307 *
308 * The RPC Authentication Protocol ID table.  This table is indexed by
309 * auth protocol ID.
310 *
311 * Note that the ".auth_protocol_id" field of i'th element in the table
312 * is always "i".  While redundant, this is useful so that you can pass
313 * pointers to individual table elements.
314 */
315EXTERNAL rpc_authn_protocol_id_elt_t  rpc_g_authn_protocol_id[RPC_C_AUTHN_PROTOCOL_ID_MAX];
316
317/*
318 * Accessor macros for the Auth Protocol ID table.
319 */
320#define RPC_AUTHN_INQ_SUPPORTED(id)             (rpc_g_authn_protocol_id[id].auth_init != NULL)
321#define RPC_AUTHN_INQ_EPV(id)                   rpc_g_authn_protocol_id[id].epv
322#define RPC_AUTHN_INQ_RPC_PROT_EPV_TBL(id)      rpc_g_authn_protocol_id[id].rpc_prot_epv_tbl
323#define RPC_AUTHN_INQ_RPC_PROT_EPV(id, rpc_id)  (RPC_AUTHN_INQ_RPC_PROT_EPV_TBL(id))[rpc_id]
324#define RPC_AUTHN_INQ_SUPPORTED_RPC_PROT(id, rpc_id) (RPC_AUTHN_INQ_RPC_PROT_EPV(id, rpc_id) != NULL)
325
326/*
327 * Make sure the specified authentication protocol is valid.  If we're
328 * asked for the "default" authentication protocol, use DCE private
329 * authentication.  (Obviously this will have to be generalized in the
330 * future.)
331 */
332
333static inline int RPC_AUTHN_IN_RANGE(unsigned32 id)
334{
335	return (id > 0) ? (id < RPC_C_AUTHN_PROTOCOL_ID_MAX) : 0;
336}
337
338#define RPC_AUTHN_CHECK_SUPPORTED(id, st) \
339{ \
340    if ((id) == (typeof(id))(rpc_c_authn_default)) \
341    { \
342        id = rpc_c_authn_dce_private; \
343    } \
344    else if (! RPC_AUTHN_IN_RANGE(id) || ! RPC_AUTHN_INQ_SUPPORTED(id)) \
345    { \
346        *(st) = rpc_s_unknown_auth_protocol; \
347        return; \
348    } \
349}
350
351#define RPC_AUTHN_CHECK_SUPPORTED_RPC_PROT(id, rpc_id, st) \
352{ \
353    RPC_AUTHN_CHECK_SUPPORTED(id, st); \
354    if (! RPC_AUTHN_INQ_SUPPORTED_RPC_PROT(id, rpc_id)) \
355    { \
356        *(st) = rpc_s_proto_unsupp_by_auth; \
357        return; \
358    } \
359}
360
361
362/***********************************************************************/
363/*
364 * R P C _ G _ S E R V E R _ P T H R E A D _ A T T R
365 *
366 * A dcethread attribute for server thread creation.
367 * Initialized by rpc_init().
368 */
369EXTERNAL dcethread_attr     rpc_g_server_dcethread_attr;
370
371
372/***********************************************************************/
373/*
374 * Macros for checking the validity of bindings.  Note that these live
375 * here instead of "com.h" because they use RPC_PROTOCOL_INQ_SUPPORTED
376 * which is defined here.
377 */
378
379/*
380 * The following macro is used to determine the validity of a binding
381 * handle passed into the runtime.  Along with the normal sanity checks,
382 * it will also check whether the handle is being used for the first
383 * time in the child of a fork.  In the case of a handle crossing a fork,
384 * a protocol specific routine is called to clean up any dangling state
385 * that might have been carried across the fork.  Any other failed checks
386 * result in a bad status being set.
387 */
388
389#define RPC_BINDING_VALIDATE(binding_rep, st) \
390{ \
391    if ((binding_rep) == NULL || \
392        (binding_rep)->protocol_id >= RPC_C_PROTOCOL_ID_MAX || \
393        ! RPC_PROTOCOL_INQ_SUPPORTED((binding_rep)->protocol_id)) \
394    { \
395        *(st) = rpc_s_invalid_binding; \
396    } \
397    else if ((binding_rep)->fork_count != rpc_g_fork_count) \
398    { \
399        rpc__binding_cross_fork(binding_rep, st); \
400    } \
401    else \
402        *(st) = rpc_s_ok; \
403}
404
405/*
406 * The following macros are for use by callers that want to verify
407 * that, along with being valid, the handle in question is of the
408 * right type.  Note that the check for the correct type is only
409 * done if the sanity checks carried out by the VALIDATE macro pass.
410 */
411#define RPC_BINDING_VALIDATE_SERVER(binding_rep, st) \
412{ \
413    RPC_BINDING_VALIDATE(binding_rep, st) \
414    if (*(st) == rpc_s_ok && RPC_BINDING_IS_CLIENT(binding_rep)) \
415        *(st) = rpc_s_wrong_kind_of_binding; \
416}
417
418#define RPC_BINDING_VALIDATE_CLIENT(binding_rep, st) \
419{ \
420    RPC_BINDING_VALIDATE(binding_rep, st) \
421    if (*(st) == rpc_s_ok && RPC_BINDING_IS_SERVER(binding_rep)) \
422        *(st) = rpc_s_wrong_kind_of_binding; \
423}
424
425/***********************************************************************/
426/*
427 * Prototypes for Common Communications Services routines that are used
428 * across sub-components of the service.
429 */
430
431#ifdef __cplusplus
432extern "C" {
433#endif
434
435PRIVATE void rpc__if_inq_endpoint (
436        rpc_if_rep_p_t          /*ifspec*/,
437        rpc_protseq_id_t        /*protseq_id*/,
438        unsigned_char_t         ** /*endpoint*/,
439        unsigned32              * /*status*/
440    );
441
442#include <comimage.h>
443
444#ifdef __cplusplus
445}
446#endif
447
448
449#endif /* _COMP_H */
450