4831fb063da91551f847ac2af496fce4fe034f41
[kai/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
26 /*
27   destroy a rpc handle
28 */
29 static int dcesrv_handle_destructor(struct dcesrv_handle *h)
30 {
31         DLIST_REMOVE(h->context->handles, h);
32         return 0;
33 }
34
35
36 /*
37   allocate a new rpc handle
38 */
39 _PUBLIC_ struct dcesrv_handle *dcesrv_handle_new(struct dcesrv_connection_context *context, 
40                                         uint8_t handle_type)
41 {
42         struct dcesrv_handle *h;
43
44         h = talloc(context, struct dcesrv_handle);
45         if (!h) {
46                 return NULL;
47         }
48         h->data = NULL;
49         h->context = context;
50
51         h->wire_handle.handle_type = handle_type;
52         h->wire_handle.uuid = GUID_random();
53         
54         DLIST_ADD(context->handles, h);
55
56         talloc_set_destructor(h, dcesrv_handle_destructor);
57
58         return h;
59 }
60
61 /**
62   find an internal handle given a wire handle. If the wire handle is NULL then
63   allocate a new handle
64 */
65 _PUBLIC_ struct dcesrv_handle *dcesrv_handle_fetch(
66                                           struct dcesrv_connection_context *context, 
67                                           struct policy_handle *p,
68                                           uint8_t handle_type)
69 {
70         struct dcesrv_handle *h;
71
72         if (policy_handle_empty(p)) {
73                 return dcesrv_handle_new(context, handle_type);
74         }
75
76         for (h=context->handles; h; h=h->next) {
77                 if (h->wire_handle.handle_type == p->handle_type &&
78                     GUID_equal(&p->uuid, &h->wire_handle.uuid)) {
79                         if (handle_type != DCESRV_HANDLE_ANY &&
80                             p->handle_type != handle_type) {
81                                 DEBUG(0,("client gave us the wrong handle type (%d should be %d)\n",
82                                          p->handle_type, handle_type));
83                                 return NULL;
84                         }
85                         return h;
86                 }
87         }
88
89         return NULL;
90 }