s4:rpc_server/netlogon: make use of dcesrv_iface_state_{create,find}_conn()
[garming/samba-autobuild/.git] / source4 / rpc_server / handles.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    server side dcerpc handle code
5
6    Copyright (C) Andrew Tridgell 2003
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "../lib/util/dlinklist.h"
24 #include "rpc_server/dcerpc_server.h"
25 #include "libcli/security/security.h"
26 #include "auth/session.h"
27
28 /*
29   destroy a rpc handle
30 */
31 static int dcesrv_handle_destructor(struct dcesrv_handle *h)
32 {
33         DLIST_REMOVE(h->assoc_group->handles, h);
34         return 0;
35 }
36
37
38 /*
39   allocate a new rpc handle
40 */
41 _PUBLIC_
42 struct dcesrv_handle *dcesrv_handle_create(struct dcesrv_call_state *call,
43                                            uint8_t handle_type)
44 {
45         struct dcesrv_connection_context *context = call->context;
46         struct auth_session_info *session_info =
47                 dcesrv_call_session_info(call);
48         struct dcesrv_handle *h;
49         struct dom_sid *sid;
50
51         /*
52          * For simplicty, ensure we abort here for an interface that has no handles (programmer error)
53          */
54         SMB_ASSERT((context->iface->flags & DCESRV_INTERFACE_FLAGS_HANDLES_NOT_USED) == 0);
55
56         sid = &session_info->security_token->sids[PRIMARY_USER_SID_INDEX];
57
58         h = talloc_zero(context->conn->assoc_group, struct dcesrv_handle);
59         if (!h) {
60                 return NULL;
61         }
62         h->data = NULL;
63         h->sid = dom_sid_dup(h, sid);
64         if (h->sid == NULL) {
65                 talloc_free(h);
66                 return NULL;
67         }
68         h->min_auth_level = call->auth_state->auth_level;
69         h->assoc_group = context->conn->assoc_group;
70         h->iface = context->iface;
71         h->wire_handle.handle_type = handle_type;
72         h->wire_handle.uuid = GUID_random();
73         
74         DLIST_ADD(context->conn->assoc_group->handles, h);
75
76         talloc_set_destructor(h, dcesrv_handle_destructor);
77
78         return h;
79 }
80
81 /**
82   find an internal handle given a wire handle. If the wire handle is NULL then
83   allocate a new handle
84 */
85
86 _PUBLIC_
87 struct dcesrv_handle *dcesrv_handle_lookup(struct dcesrv_call_state *call,
88                                            const struct policy_handle *p,
89                                            uint8_t handle_type)
90 {
91         struct dcesrv_connection_context *context = call->context;
92         struct auth_session_info *session_info =
93                 dcesrv_call_session_info(call);
94         struct dcesrv_handle *h;
95         struct dom_sid *sid;
96
97         /*
98          * For simplicty, ensure we abort here for an interface that has no handles (programmer error)
99          */
100         SMB_ASSERT((context->iface->flags & DCESRV_INTERFACE_FLAGS_HANDLES_NOT_USED) == 0);
101
102         sid = &session_info->security_token->sids[PRIMARY_USER_SID_INDEX];
103
104         if (ndr_policy_handle_empty(p)) {
105                 /* TODO: we should probably return a NULL handle here */
106                 return dcesrv_handle_create(call, handle_type);
107         }
108
109         for (h=context->conn->assoc_group->handles; h; h=h->next) {
110                 if (h->wire_handle.handle_type == p->handle_type &&
111                     GUID_equal(&p->uuid, &h->wire_handle.uuid)) {
112                         if (handle_type != DCESRV_HANDLE_ANY &&
113                             p->handle_type != handle_type) {
114                                 DEBUG(0,("client gave us the wrong handle type (%d should be %d)\n",
115                                          p->handle_type, handle_type));
116                                 return NULL;
117                         }
118                         if (!dom_sid_equal(h->sid, sid)) {
119                                 DEBUG(0,(__location__ ": Attempt to use invalid sid %s - %s\n",
120                                          dom_sid_string(context, h->sid),
121                                          dom_sid_string(context, sid)));
122                                 return NULL;
123                         }
124                         if (call->auth_state->auth_level < h->min_auth_level) {
125                                 DEBUG(0,(__location__ ": Attempt to use invalid auth_level %u < %u\n",
126                                          call->auth_state->auth_level,
127                                          h->min_auth_level));
128                                 return NULL;
129                         }
130                         if (h->iface != context->iface) {
131                                 DEBUG(0,(__location__ ": Attempt to use invalid iface\n"));
132                                 return NULL;
133                         }
134                         return h;
135                 }
136         }
137
138         return NULL;
139 }
140
141 struct dcesrv_iface_state {
142         struct dcesrv_iface_state *prev, *next;
143         struct dcesrv_assoc_group *assoc;
144         const struct dcesrv_interface *iface;
145         struct dom_sid owner;
146         const struct dcesrv_connection *conn;
147         const struct dcesrv_auth *auth;
148         const struct dcesrv_connection_context *pres;
149         uint64_t magic;
150         void *ptr;
151         const char *location;
152 };
153
154 static int dcesrv_iface_state_destructor(struct dcesrv_iface_state *istate)
155 {
156         DLIST_REMOVE(istate->assoc->iface_states, istate);
157         return 0;
158 }
159
160 static void *dcesrv_iface_state_find(struct dcesrv_assoc_group *assoc,
161                         const struct dcesrv_interface *iface,
162                         const struct dom_sid *owner,
163                         const struct dcesrv_connection *conn,
164                         const struct dcesrv_auth *auth,
165                         const struct dcesrv_connection_context *pres,
166                         uint64_t magic,
167                         const void *ptr)
168 {
169         struct dcesrv_iface_state *cur = NULL;
170
171         for (cur = assoc->iface_states; cur != NULL; cur = cur->next) {
172                 bool match;
173
174                 SMB_ASSERT(cur->assoc == assoc);
175
176                 if (cur->ptr == ptr) {
177                         return cur->ptr;
178                 }
179
180                 if (cur->iface != iface) {
181                         continue;
182                 }
183
184                 match = dom_sid_equal(&cur->owner, owner);
185                 if (!match) {
186                         continue;
187                 }
188
189                 if (cur->conn != conn) {
190                         continue;
191                 }
192
193                 if (cur->auth != auth) {
194                         continue;
195                 }
196
197                 if (cur->pres != pres) {
198                         continue;
199                 }
200
201                 if (cur->magic != magic) {
202                         continue;
203                 }
204
205                 return cur->ptr;
206         }
207
208         return NULL;
209 }
210
211 static NTSTATUS dcesrv_iface_state_store(struct dcesrv_assoc_group *assoc,
212                                 const struct dcesrv_interface *iface,
213                                 const struct dom_sid *owner,
214                                 const struct dcesrv_connection *conn,
215                                 const struct dcesrv_auth *auth,
216                                 const struct dcesrv_connection_context *pres,
217                                 uint64_t magic,
218                                 TALLOC_CTX *mem_ctx,
219                                 void *ptr,
220                                 const char *location)
221 {
222         struct dcesrv_iface_state *istate = NULL;
223         void *optr = NULL;
224
225         optr = dcesrv_iface_state_find(assoc,
226                                        iface,
227                                        owner,
228                                        conn,
229                                        auth,
230                                        pres,
231                                        magic,
232                                        ptr);
233         if (optr != NULL) {
234                 return NT_STATUS_OBJECTID_EXISTS;
235         }
236
237         istate = talloc_zero(ptr, struct dcesrv_iface_state);
238         if (istate == NULL) {
239                 return NT_STATUS_NO_MEMORY;
240         }
241
242         *istate = (struct dcesrv_iface_state) {
243                 .assoc = assoc,
244                 .iface = iface,
245                 .owner = *owner,
246                 .conn  = conn,
247                 .auth  = auth,
248                 .pres  = pres,
249                 .magic = magic,
250                 .location = location,
251         };
252
253         istate->ptr = talloc_steal(mem_ctx, ptr);
254
255         talloc_set_destructor(istate, dcesrv_iface_state_destructor);
256
257         DLIST_ADD_END(assoc->iface_states, istate);
258
259         return NT_STATUS_OK;
260 }
261
262 NTSTATUS _dcesrv_iface_state_store_assoc(struct dcesrv_call_state *call,
263                                 uint64_t magic,
264                                 void *ptr,
265                                 const char *location)
266 {
267         struct auth_session_info *session_info =
268                 dcesrv_call_session_info(call);
269         const struct dom_sid *owner =
270                 &session_info->security_token->sids[0];
271         NTSTATUS status;
272
273         status = dcesrv_iface_state_store(call->conn->assoc_group,
274                                           call->context->iface,
275                                           owner,
276                                           NULL, /* conn */
277                                           NULL, /* auth */
278                                           NULL, /* pres */
279                                           magic,
280                                           call->conn->assoc_group, /* mem_ctx */
281                                           ptr,
282                                           location);
283         if (!NT_STATUS_IS_OK(status)) {
284                 return status;
285         }
286
287         return NT_STATUS_OK;
288 }
289
290 void *_dcesrv_iface_state_find_assoc(struct dcesrv_call_state *call, uint64_t magic)
291 {
292         struct auth_session_info *session_info =
293                 dcesrv_call_session_info(call);
294         const struct dom_sid *owner =
295                 &session_info->security_token->sids[0];
296         void *ptr = NULL;
297
298         ptr = dcesrv_iface_state_find(call->conn->assoc_group,
299                                       call->context->iface,
300                                       owner,
301                                       NULL, /* conn */
302                                       NULL, /* auth */
303                                       NULL, /* pres */
304                                       magic,
305                                       NULL); /* ptr */
306         if (ptr == NULL) {
307                 return NULL;
308         }
309
310         return ptr;
311 }
312
313 NTSTATUS _dcesrv_iface_state_store_conn(struct dcesrv_call_state *call,
314                                         uint64_t magic,
315                                         void *ptr,
316                                         const char *location)
317 {
318         struct auth_session_info *session_info =
319                 dcesrv_call_session_info(call);
320         const struct dom_sid *owner =
321                 &session_info->security_token->sids[0];
322         NTSTATUS status;
323
324         status = dcesrv_iface_state_store(call->conn->assoc_group,
325                                           call->context->iface,
326                                           owner,
327                                           call->conn,
328                                           call->auth_state,
329                                           call->context,
330                                           magic,
331                                           call->conn, /* mem_ctx */
332                                           ptr,
333                                           location);
334         if (!NT_STATUS_IS_OK(status)) {
335                 return status;
336         }
337
338         return NT_STATUS_OK;
339 }
340
341 void *_dcesrv_iface_state_find_conn(struct dcesrv_call_state *call, uint64_t magic)
342 {
343         struct auth_session_info *session_info =
344                 dcesrv_call_session_info(call);
345         const struct dom_sid *owner =
346                 &session_info->security_token->sids[0];
347         void *ptr = NULL;
348
349         ptr = dcesrv_iface_state_find(call->conn->assoc_group,
350                                       call->context->iface,
351                                       owner,
352                                       call->conn,
353                                       call->auth_state,
354                                       call->context,
355                                       magic,
356                                       NULL); /* ptr */
357         if (ptr == NULL) {
358                 return NULL;
359         }
360
361         return ptr;
362 }