s4:rpc_server/handles: make use dcesrv_call_session_info()
[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->assoc_group = context->conn->assoc_group;
69         h->iface = context->iface;
70         h->wire_handle.handle_type = handle_type;
71         h->wire_handle.uuid = GUID_random();
72         
73         DLIST_ADD(context->conn->assoc_group->handles, h);
74
75         talloc_set_destructor(h, dcesrv_handle_destructor);
76
77         return h;
78 }
79
80 /**
81   find an internal handle given a wire handle. If the wire handle is NULL then
82   allocate a new handle
83 */
84
85 _PUBLIC_
86 struct dcesrv_handle *dcesrv_handle_lookup(struct dcesrv_call_state *call,
87                                            const struct policy_handle *p,
88                                            uint8_t handle_type)
89 {
90         struct dcesrv_connection_context *context = call->context;
91         struct auth_session_info *session_info =
92                 dcesrv_call_session_info(call);
93         struct dcesrv_handle *h;
94         struct dom_sid *sid;
95
96         /*
97          * For simplicty, ensure we abort here for an interface that has no handles (programmer error)
98          */
99         SMB_ASSERT((context->iface->flags & DCESRV_INTERFACE_FLAGS_HANDLES_NOT_USED) == 0);
100
101         sid = &session_info->security_token->sids[PRIMARY_USER_SID_INDEX];
102
103         if (ndr_policy_handle_empty(p)) {
104                 /* TODO: we should probably return a NULL handle here */
105                 return dcesrv_handle_create(call, handle_type);
106         }
107
108         for (h=context->conn->assoc_group->handles; h; h=h->next) {
109                 if (h->wire_handle.handle_type == p->handle_type &&
110                     GUID_equal(&p->uuid, &h->wire_handle.uuid)) {
111                         if (handle_type != DCESRV_HANDLE_ANY &&
112                             p->handle_type != handle_type) {
113                                 DEBUG(0,("client gave us the wrong handle type (%d should be %d)\n",
114                                          p->handle_type, handle_type));
115                                 return NULL;
116                         }
117                         if (!dom_sid_equal(h->sid, sid)) {
118                                 DEBUG(0,(__location__ ": Attempt to use invalid sid %s - %s\n",
119                                          dom_sid_string(context, h->sid),
120                                          dom_sid_string(context, sid)));
121                                 return NULL;
122                         }
123                         if (h->iface != context->iface) {
124                                 DEBUG(0,(__location__ ": Attempt to use invalid iface\n"));
125                                 return NULL;
126                         }
127                         return h;
128                 }
129         }
130
131         return NULL;
132 }