r5037: got rid of all of the TALLOC_DEPRECATED stuff. My apologies for the
[ira/wip.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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "dlinklist.h"
25 #include "rpc_server/dcerpc_server.h"
26
27 /*
28   destroy a rpc handle
29 */
30 static int dcesrv_handle_destructor(void *ptr)
31 {
32         struct dcesrv_handle *h = ptr;
33         if (h->destroy) {
34                 h->destroy(h->context, h);
35         }
36         DLIST_REMOVE(h->context->handles, h);
37         talloc_free(h);
38         return 0;
39 }
40
41
42 /*
43   allocate a new rpc handle
44 */
45 struct dcesrv_handle *dcesrv_handle_new(struct dcesrv_connection_context *context, 
46                                         uint8_t handle_type)
47 {
48         struct dcesrv_handle *h;
49
50         h = talloc(context, struct dcesrv_handle);
51         if (!h) {
52                 return NULL;
53         }
54         h->data = NULL;
55         h->destroy = NULL;
56         h->context = context;
57
58         h->wire_handle.handle_type = handle_type;
59         h->wire_handle.uuid = GUID_random();
60         
61         DLIST_ADD(context->handles, h);
62
63         talloc_set_destructor(h, dcesrv_handle_destructor);
64
65         return h;
66 }
67
68 /*
69   find an internal handle given a wire handle. If the wire handle is NULL then
70   allocate a new handle
71 */
72 struct dcesrv_handle *dcesrv_handle_fetch(struct dcesrv_connection_context *context, 
73                                           struct policy_handle *p,
74                                           uint8_t handle_type)
75 {
76         struct dcesrv_handle *h;
77
78         if (policy_handle_empty(p)) {
79                 return dcesrv_handle_new(context, handle_type);
80         }
81
82         for (h=context->handles; h; h=h->next) {
83                 if (h->wire_handle.handle_type == p->handle_type &&
84                     GUID_equal(&p->uuid, &h->wire_handle.uuid)) {
85                         if (handle_type != DCESRV_HANDLE_ANY &&
86                             p->handle_type != handle_type) {
87                                 DEBUG(0,("client gave us the wrong handle type (%d should be %d)\n",
88                                          p->handle_type, handle_type));
89                                 return NULL;
90                         }
91                         return h;
92                 }
93         }
94
95         return NULL;
96 }