smbd: Reduce the complexity of open_file_ntcreate
[mat/samba.git] / source3 / rpc_server / rpc_pipes.h
1 /*
2    Unix SMB/Netbios implementation.
3    RPC Server Headers
4    Copyright (C) Andrew Tridgell 1992-1997
5    Copyright (C) Luke Kenneth Casson Leighton 1996-1997
6    Copyright (C) Paul Ashton 1997
7    Copyright (C) Jeremy Allison 2000-2004
8    Copyright (C) Simo Sorce 2010-2011
9
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #ifndef _RPC_PIPES_H_
25 #define _RPC_PIPES_H_
26
27 #include "librpc/rpc/dcerpc.h"
28
29 typedef struct _output_data {
30         /*
31          * Raw RPC output data. This does not include RPC headers or footers.
32          */
33         DATA_BLOB rdata;
34
35         /* The amount of data sent from the current rdata struct. */
36         uint32 data_sent_length;
37
38         /*
39          * The current fragment being returned. This inclues
40          * headers, data and authentication footer.
41          */
42         DATA_BLOB frag;
43
44         /* The amount of data sent from the current PDU. */
45         uint32 current_pdu_sent;
46 } output_data;
47
48 typedef struct _input_data {
49         /*
50          * This is the current incoming pdu. The data here
51          * is collected via multiple writes until a complete
52          * pdu is seen, then the data is copied into the in_data
53          * structure. The maximum size of this is 0x1630 (RPC_MAX_PDU_FRAG_LEN).
54          * If length is zero, then we are at the start of a new
55          * pdu.
56          */
57         DATA_BLOB pdu;
58
59         /*
60          * The amount of data needed to complete the in_pdu.
61          * If this is zero, then we are at the start of a new
62          * pdu.
63          */
64         uint32 pdu_needed_len;
65
66         /*
67          * This is the collection of input data with all
68          * the rpc headers and auth footers removed.
69          * The maximum length of this (1Mb) is strictly enforced.
70          */
71         DATA_BLOB data;
72
73 } input_data;
74
75 struct dcesrv_ep_entry_list;
76 struct tsocket_address;
77 struct handle_list;
78 struct pipes_struct;
79
80 struct api_struct {
81         const char *name;
82         uint8 opnum;
83         bool (*fn) (struct pipes_struct *);
84 };
85
86 struct pipe_rpc_fns {
87
88         struct pipe_rpc_fns *next, *prev;
89
90         /* RPC function table associated with the current rpc_bind (associated by context) */
91
92         const struct api_struct *cmds;
93         int n_cmds;
94         uint32_t context_id;
95         struct ndr_syntax_id syntax;
96
97 };
98
99 /*
100  * DCE/RPC-specific samba-internal-specific handling of data on
101  * NamedPipes.
102  */
103 struct pipes_struct {
104         struct pipes_struct *next, *prev;
105
106         const struct tsocket_address *local_address;
107         const struct tsocket_address *remote_address;
108
109         enum dcerpc_transport_t transport;
110
111         struct auth_session_info *session_info;
112         struct messaging_context *msg_ctx;
113
114         struct dcesrv_ep_entry_list *ep_entries;
115
116         /* linked list of rpc dispatch tables associated 
117            with the open rpc contexts */
118
119         struct pipe_rpc_fns *contexts;
120
121         struct pipe_auth_data auth;
122
123         bool ncalrpc_as_system;
124
125         /*
126          * Set to true when an RPC bind has been done on this pipe.
127          */
128
129         bool pipe_bound;
130
131         /*
132          * Set the DCERPC_FAULT to return.
133          */
134         int fault_state;
135
136         /*
137          * Set to RPC_BIG_ENDIAN when dealing with big-endian PDU's
138          */
139
140         bool endian;
141
142         /*
143          * Struct to deal with multiple pdu inputs.
144          */
145
146         input_data in_data;
147
148         /*
149          * Struct to deal with multiple pdu outputs.
150          */
151
152         output_data out_data;
153
154         /* This context is used for PDU data and is freed between each pdu.
155                 Don't use for pipe state storage. */
156         TALLOC_CTX *mem_ctx;
157
158         /* handle database to use on this pipe. */
159         struct handle_list *pipe_handles;
160
161         /* call id retrieved from the pdu header */
162         uint32_t call_id;
163
164         /* operation number retrieved from the rpc header */
165         uint16_t opnum;
166
167         /* private data for the interface implementation */
168         void *private_data;
169
170 };
171
172 int make_base_pipes_struct(TALLOC_CTX *mem_ctx,
173                            struct messaging_context *msg_ctx,
174                            const char *pipe_name,
175                            enum dcerpc_transport_t transport,
176                            bool endian, bool ncalrpc_as_system,
177                            const struct tsocket_address *remote_address,
178                            const struct tsocket_address *local_address,
179                            struct pipes_struct **_p);
180 bool check_open_pipes(void);
181 int close_internal_rpc_pipe_hnd(struct pipes_struct *p);
182
183 size_t num_pipe_handles(struct pipes_struct *p);
184 bool init_pipe_handles(struct pipes_struct *p, const struct ndr_syntax_id *syntax);
185 bool create_policy_hnd(struct pipes_struct *p, struct policy_handle *hnd, void *data_ptr);
186 bool find_policy_by_hnd(struct pipes_struct *p, const struct policy_handle *hnd,
187                         void **data_p);
188 bool close_policy_hnd(struct pipes_struct *p, struct policy_handle *hnd);
189 void close_policy_by_pipe(struct pipes_struct *p);
190 bool pipe_access_check(struct pipes_struct *p);
191
192 void *_policy_handle_create(struct pipes_struct *p, struct policy_handle *hnd,
193                             uint32_t access_granted, size_t data_size,
194                             const char *type, NTSTATUS *pstatus);
195 #define policy_handle_create(_p, _hnd, _access, _type, _pstatus) \
196         (_type *)_policy_handle_create((_p), (_hnd), (_access), sizeof(_type), #_type, \
197                                        (_pstatus))
198
199 void *_policy_handle_find(struct pipes_struct *p,
200                           const struct policy_handle *hnd,
201                           uint32_t access_required, uint32_t *paccess_granted,
202                           const char *name, const char *location,
203                           NTSTATUS *pstatus);
204 #define policy_handle_find(_p, _hnd, _access_required, _access_granted, _type, _pstatus) \
205         (_type *)_policy_handle_find((_p), (_hnd), (_access_required), \
206                                      (_access_granted), #_type, __location__, (_pstatus))
207
208 #endif /* _RPC_PIPES_H_ */