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