s4:rpc_server/drsuapi: make use of dcesrv_handle_create()
[samba.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_ struct dcesrv_handle *dcesrv_handle_new(struct dcesrv_connection_context *context, 
42                                                  uint8_t handle_type)
43 {
44         struct dcesrv_handle *h;
45         struct dom_sid *sid;
46
47         /*
48          * For simplicty, ensure we abort here for an interface that has no handles (programmer error)
49          */
50         SMB_ASSERT((context->iface->flags & DCESRV_INTERFACE_FLAGS_HANDLES_NOT_USED) == 0);
51
52         sid = &context->conn->auth_state.session_info->security_token->sids[PRIMARY_USER_SID_INDEX];
53
54         h = talloc_zero(context->conn->assoc_group, struct dcesrv_handle);
55         if (!h) {
56                 return NULL;
57         }
58         h->data = NULL;
59         h->sid = dom_sid_dup(h, sid);
60         if (h->sid == NULL) {
61                 talloc_free(h);
62                 return NULL;
63         }
64         h->assoc_group = context->conn->assoc_group;
65         h->iface = context->iface;
66         h->wire_handle.handle_type = handle_type;
67         h->wire_handle.uuid = GUID_random();
68         
69         DLIST_ADD(context->conn->assoc_group->handles, h);
70
71         talloc_set_destructor(h, dcesrv_handle_destructor);
72
73         return h;
74 }
75
76 _PUBLIC_
77 struct dcesrv_handle *dcesrv_handle_create(struct dcesrv_call_state *call,
78                                            uint8_t handle_type)
79 {
80         return dcesrv_handle_new(call->context, handle_type);
81 }
82
83 /**
84   find an internal handle given a wire handle. If the wire handle is NULL then
85   allocate a new handle
86 */
87 _PUBLIC_ struct dcesrv_handle *dcesrv_handle_fetch(
88                                           struct dcesrv_connection_context *context, 
89                                           const struct policy_handle *p,
90                                           uint8_t handle_type)
91 {
92         struct dcesrv_handle *h;
93         struct dom_sid *sid;
94
95         /*
96          * For simplicty, ensure we abort here for an interface that has no handles (programmer error)
97          */
98         SMB_ASSERT((context->iface->flags & DCESRV_INTERFACE_FLAGS_HANDLES_NOT_USED) == 0);
99
100         sid = &context->conn->auth_state.session_info->security_token->sids[PRIMARY_USER_SID_INDEX];
101
102         if (ndr_policy_handle_empty(p)) {
103                 /* TODO: we should probably return a NULL handle here */
104                 return dcesrv_handle_new(context, handle_type);
105         }
106
107         for (h=context->conn->assoc_group->handles; h; h=h->next) {
108                 if (h->wire_handle.handle_type == p->handle_type &&
109                     GUID_equal(&p->uuid, &h->wire_handle.uuid)) {
110                         if (handle_type != DCESRV_HANDLE_ANY &&
111                             p->handle_type != handle_type) {
112                                 DEBUG(0,("client gave us the wrong handle type (%d should be %d)\n",
113                                          p->handle_type, handle_type));
114                                 return NULL;
115                         }
116                         if (!dom_sid_equal(h->sid, sid)) {
117                                 DEBUG(0,(__location__ ": Attempt to use invalid sid %s - %s\n",
118                                          dom_sid_string(context, h->sid),
119                                          dom_sid_string(context, sid)));
120                                 return NULL;
121                         }
122                         if (h->iface != context->iface) {
123                                 DEBUG(0,(__location__ ": Attempt to use invalid iface\n"));
124                                 return NULL;
125                         }
126                         return h;
127                 }
128         }
129
130         return NULL;
131 }
132
133 _PUBLIC_
134 struct dcesrv_handle *dcesrv_handle_lookup(struct dcesrv_call_state *call,
135                                            const struct policy_handle *p,
136                                            uint8_t handle_type)
137 {
138         return dcesrv_handle_fetch(call->context, p, handle_type);
139 }