r2627: use the new talloc capabilities in a bunch more places in the rpc
[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 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
25 /*
26   allocate a new rpc handle
27 */
28 struct dcesrv_handle *dcesrv_handle_new(struct dcesrv_connection *dce_conn, 
29                                         uint8_t handle_type)
30 {
31         struct dcesrv_handle *h;
32
33         h = talloc_p(dce_conn, struct dcesrv_handle);
34         if (!h) {
35                 return NULL;
36         }
37         h->data = NULL;
38         h->destroy = NULL;
39
40         h->wire_handle.handle_type = handle_type;
41         uuid_generate_random(&h->wire_handle.uuid);
42         
43         DLIST_ADD(dce_conn->handles, h);
44
45         return h;
46 }
47
48 /*
49   destroy a rpc handle
50 */
51 void dcesrv_handle_destroy(struct dcesrv_connection *dce_conn, 
52                            struct dcesrv_handle *h)
53 {
54         if (h->destroy) {
55                 h->destroy(dce_conn, h);
56         }
57         DLIST_REMOVE(dce_conn->handles, h);
58         talloc_free(h);
59 }
60
61
62 /*
63   find an internal handle given a wire handle. If the wire handle is NULL then
64   allocate a new handle
65 */
66 struct dcesrv_handle *dcesrv_handle_fetch(struct dcesrv_connection *dce_conn, 
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(dce_conn, handle_type);
74         }
75
76         for (h=dce_conn->handles; h; h=h->next) {
77                 if (h->wire_handle.handle_type == p->handle_type &&
78                     uuid_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 }