65f43c82e2d35e6173cb963e0f72fd0ccb073ce6
[samba.git] / source3 / rpc_server / srv_lsa_hnd.c
1 /* 
2  *  Unix SMB/Netbios implementation.
3  *  Version 1.9.
4  *  RPC Pipe client / server routines
5  *  Copyright (C) Andrew Tridgell              1992-1997,
6  *  Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
7  *  Copyright (C) Jeremy Allison                           2001.
8  *  
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *  
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *  
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #include "includes.h"
25
26 /* This is the max handles across all instances of a pipe name. */
27 #ifndef MAX_OPEN_POLS
28 #define MAX_OPEN_POLS 1024
29 #endif
30
31 /****************************************************************************
32  Initialise a policy handle list on a pipe. Handle list is shared between all
33  pipes of the same name.
34 ****************************************************************************/
35
36 BOOL init_pipe_handle_list(pipes_struct *p, char *pipe_name)
37 {
38         pipes_struct *plist = get_first_internal_pipe();
39         struct handle_list *hl = NULL;
40
41         for (plist = get_first_internal_pipe(); plist; plist = get_next_internal_pipe(plist)) {
42                 if (strequal( plist->name, pipe_name)) {
43                         if (!plist->pipe_handles) {
44                                 pstring msg;
45                                 slprintf(msg, sizeof(msg)-1, "init_pipe_handles: NULL pipe_handle pointer in pipe %s",
46                                                 pipe_name );
47                                 smb_panic(msg);
48                         }
49                         hl = plist->pipe_handles;
50                         break;
51                 }
52         }
53
54         if (!hl) {
55                 /*
56                  * No handle list for this pipe (first open of pipe).
57                  * Create list.
58                  */
59
60                 if ((hl = (struct handle_list *)malloc(sizeof(struct handle_list))) == NULL)
61                         return False;
62                 ZERO_STRUCTP(hl);
63
64                 DEBUG(10,("init_pipe_handles: created handle list for pipe %s\n", pipe_name ));
65         }
66
67         /*
68          * One more pipe is using this list.
69          */
70
71         hl->pipe_ref_count++;
72
73         /*
74          * Point this pipe at this list.
75          */
76
77         p->pipe_handles = hl;
78
79         DEBUG(10,("init_pipe_handles: pipe_handles ref count = %u for pipe %s\n",
80                         p->pipe_handles->pipe_ref_count, pipe_name ));
81
82         return True;
83 }
84
85 /****************************************************************************
86   find first available policy slot.  creates a policy handle for you.
87 ****************************************************************************/
88
89 BOOL create_policy_hnd(pipes_struct *p, POLICY_HND *hnd, void (*free_fn)(void *), void *data_ptr)
90 {
91         static uint32 pol_hnd_low  = 0;
92         static uint32 pol_hnd_high = 0;
93
94         struct policy *pol;
95
96         if (p->pipe_handles->count > MAX_OPEN_POLS) {
97                 DEBUG(0,("create_policy_hnd: ERROR: too many handles (%d) on this pipe.\n",
98                                 (int)p->pipe_handles->count));
99                 return False;
100         }
101
102         pol = (struct policy *)malloc(sizeof(*p));
103         if (!pol) {
104                 DEBUG(0,("create_policy_hnd: ERROR: out of memory!\n"));
105                 return False;
106         }
107
108         ZERO_STRUCTP(pol);
109
110         pol->data_ptr = data_ptr;
111         pol->free_fn = free_fn;
112
113         pol_hnd_low++;
114         if (pol_hnd_low == 0)
115                 (pol_hnd_high)++;
116
117         SIVAL(&pol->pol_hnd.data1, 0 , 0);  /* first bit must be null */
118         SIVAL(&pol->pol_hnd.data2, 0 , pol_hnd_low ); /* second bit is incrementing */
119         SSVAL(&pol->pol_hnd.data3, 0 , pol_hnd_high); /* second bit is incrementing */
120         SSVAL(&pol->pol_hnd.data4, 0 , (pol_hnd_high>>16)); /* second bit is incrementing */
121         SIVAL(pol->pol_hnd.data5, 0, time(NULL)); /* something random */
122         SIVAL(pol->pol_hnd.data5, 4, sys_getpid()); /* something more random */
123
124         DLIST_ADD(p->pipe_handles->Policy, pol);
125         p->pipe_handles->count++;
126
127         *hnd = pol->pol_hnd;
128         
129         DEBUG(4,("Opened policy hnd[%d] ", (int)p->pipe_handles->count));
130         dump_data(4, (char *)hnd, sizeof(*hnd));
131
132         return True;
133 }
134
135 /****************************************************************************
136   find policy by handle - internal version.
137 ****************************************************************************/
138
139 static struct policy *find_policy_by_hnd_internal(pipes_struct *p, POLICY_HND *hnd, void **data_p)
140 {
141         struct policy *pol;
142         size_t i;
143
144         if (data_p)
145                 *data_p = NULL;
146
147         for (i = 0, pol=p->pipe_handles->Policy;pol;pol=pol->next, i++) {
148                 if (memcmp(&pol->pol_hnd, hnd, sizeof(*hnd)) == 0) {
149                         DEBUG(4,("Found policy hnd[%d] ", (int)i));
150                         dump_data(4, (char *)hnd, sizeof(*hnd));
151                         if (data_p)
152                                 *data_p = pol->data_ptr;
153                         return pol;
154                 }
155         }
156
157         DEBUG(4,("Policy not found: "));
158         dump_data(4, (char *)hnd, sizeof(*hnd));
159
160         p->bad_handle_fault_state = True;
161
162         return NULL;
163 }
164
165 /****************************************************************************
166   find policy by handle
167 ****************************************************************************/
168
169 BOOL find_policy_by_hnd(pipes_struct *p, POLICY_HND *hnd, void **data_p)
170 {
171         return find_policy_by_hnd_internal(p, hnd, data_p) == NULL ? False : True;
172 }
173
174 /****************************************************************************
175   Close a policy.
176 ****************************************************************************/
177
178 BOOL close_policy_hnd(pipes_struct *p, POLICY_HND *hnd)
179 {
180         struct policy *pol = find_policy_by_hnd_internal(p, hnd, NULL);
181
182         if (!pol) {
183                 DEBUG(3,("Error closing policy\n"));
184                 return False;
185         }
186
187         DEBUG(3,("Closed policy\n"));
188
189         if (pol->free_fn && pol->data_ptr)
190                 (*pol->free_fn)(pol->data_ptr);
191
192         p->pipe_handles->count--;
193
194         DLIST_REMOVE(p->pipe_handles->Policy, pol);
195
196         ZERO_STRUCTP(pol);
197
198         SAFE_FREE(pol);
199
200         return True;
201 }
202
203 /****************************************************************************
204  Close a pipe - free the handle list if it was the last pipe reference.
205 ****************************************************************************/
206
207 void close_policy_by_pipe(pipes_struct *p)
208 {
209         p->pipe_handles->pipe_ref_count--;
210
211         if (p->pipe_handles->pipe_ref_count == 0) {
212                 /*
213                  * Last pipe open on this list - free the list.
214                  */
215                 while (p->pipe_handles->Policy)
216                         close_policy_hnd(p, &p->pipe_handles->Policy->pol_hnd);
217
218                 p->pipe_handles->Policy = NULL;
219                 p->pipe_handles->count = 0;
220
221                 SAFE_FREE(p->pipe_handles);
222                 DEBUG(10,("close_policy_by_pipe: deleted handle list for pipe %s\n", p->name ));
223         }
224 }