dns: Use new DNS debugclass in DNS server
[kai/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         sid = &context->conn->auth_state.session_info->security_token->sids[PRIMARY_USER_SID_INDEX];
48
49         h = talloc(context->assoc_group, struct dcesrv_handle);
50         if (!h) {
51                 return NULL;
52         }
53         h->data = NULL;
54         h->sid = dom_sid_dup(h, sid);
55         if (h->sid == NULL) {
56                 talloc_free(h);
57                 return NULL;
58         }
59         h->assoc_group = context->assoc_group;
60         h->iface = context->iface;
61         h->wire_handle.handle_type = handle_type;
62         h->wire_handle.uuid = GUID_random();
63         
64         DLIST_ADD(context->assoc_group->handles, h);
65
66         talloc_set_destructor(h, dcesrv_handle_destructor);
67
68         return h;
69 }
70
71 /**
72   find an internal handle given a wire handle. If the wire handle is NULL then
73   allocate a new handle
74 */
75 _PUBLIC_ struct dcesrv_handle *dcesrv_handle_fetch(
76                                           struct dcesrv_connection_context *context, 
77                                           struct policy_handle *p,
78                                           uint8_t handle_type)
79 {
80         struct dcesrv_handle *h;
81         struct dom_sid *sid;
82
83         sid = &context->conn->auth_state.session_info->security_token->sids[PRIMARY_USER_SID_INDEX];
84
85         if (ndr_policy_handle_empty(p)) {
86                 /* TODO: we should probably return a NULL handle here */
87                 return dcesrv_handle_new(context, handle_type);
88         }
89
90         for (h=context->assoc_group->handles; h; h=h->next) {
91                 if (h->wire_handle.handle_type == p->handle_type &&
92                     GUID_equal(&p->uuid, &h->wire_handle.uuid)) {
93                         if (handle_type != DCESRV_HANDLE_ANY &&
94                             p->handle_type != handle_type) {
95                                 DEBUG(0,("client gave us the wrong handle type (%d should be %d)\n",
96                                          p->handle_type, handle_type));
97                                 return NULL;
98                         }
99                         if (!dom_sid_equal(h->sid, sid)) {
100                                 DEBUG(0,(__location__ ": Attempt to use invalid sid %s - %s\n",
101                                          dom_sid_string(context, h->sid),
102                                          dom_sid_string(context, sid)));
103                                 return NULL;
104                         }
105                         if (h->iface != context->iface) {
106                                 DEBUG(0,(__location__ ": Attempt to use invalid iface\n"));
107                                 return NULL;
108                         }
109                         return h;
110                 }
111         }
112
113         return NULL;
114 }