some cleanups to use ZERO_STRUCT() and friends
[kai/samba-autobuild/.git] / source3 / rpc_server / srv_lsa_hnd.c
1
2 /* 
3  *  Unix SMB/Netbios implementation.
4  *  Version 1.9.
5  *  RPC Pipe client / server routines
6  *  Copyright (C) Andrew Tridgell              1992-1997,
7  *  Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
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
25 #include "includes.h"
26
27
28 extern int DEBUGLEVEL;
29
30 #ifndef MAX_OPEN_POLS
31 #define MAX_OPEN_POLS 64
32 #endif
33
34 struct reg_info
35 {
36     /* for use by \PIPE\winreg */
37         fstring name; /* name of registry key */
38 };
39
40 struct samr_info
41 {
42     /* for use by the \PIPE\samr policy */
43         DOM_SID sid;
44     uint32 rid; /* relative id associated with the pol_hnd */
45     uint32 status; /* some sort of flag.  best to record it.  comes from opnum 0x39 */
46 };
47
48 static struct policy
49 {
50         struct policy *next, *prev;
51         int pnum;
52         BOOL open;
53         POLICY_HND pol_hnd;
54
55         union {
56                 struct samr_info samr;
57                 struct reg_info reg;
58         } dev;
59 } *Policy;
60
61 static struct bitmap *bmap;
62
63
64 /****************************************************************************
65   create a unique policy handle
66 ****************************************************************************/
67 static void create_pol_hnd(POLICY_HND *hnd)
68 {
69         static uint32 pol_hnd_low  = 0;
70         static uint32 pol_hnd_high = 0;
71
72         if (hnd == NULL) return;
73
74         /* i severely doubt that pol_hnd_high will ever be non-zero... */
75         pol_hnd_low++;
76         if (pol_hnd_low == 0) pol_hnd_high++;
77
78         SIVAL(hnd->data, 0 , 0x0);  /* first bit must be null */
79         SIVAL(hnd->data, 4 , pol_hnd_low ); /* second bit is incrementing */
80         SIVAL(hnd->data, 8 , pol_hnd_high); /* second bit is incrementing */
81         SIVAL(hnd->data, 12, time(NULL)); /* something random */
82         SIVAL(hnd->data, 16, getpid()); /* something more random */
83 }
84
85 /****************************************************************************
86   initialise policy handle states...
87 ****************************************************************************/
88 void init_lsa_policy_hnd(void)
89 {
90         bmap = bitmap_allocate(MAX_OPEN_POLS);
91         if (!bmap) {
92                 exit_server("out of memory in init_lsa_policy_hnd\n");
93         }
94 }
95
96 /****************************************************************************
97   find first available policy slot.  creates a policy handle for you.
98 ****************************************************************************/
99 BOOL open_lsa_policy_hnd(POLICY_HND *hnd)
100 {
101         int i;
102         struct policy *p;
103
104         i = bitmap_find(bmap, 1);
105
106         if (i == -1) {
107                 DEBUG(0,("ERROR: out of Policy Handles!\n"));
108                 return False;
109         }
110
111         p = (struct policy *)malloc(sizeof(*p));
112         if (!p) {
113                 DEBUG(0,("ERROR: out of memory!\n"));
114                 return False;
115         }
116
117         ZERO_STRUCTP(p);
118
119         p->open = True;                         
120         p->pnum = i;
121
122         create_pol_hnd(hnd);
123         memcpy(&p->pol_hnd, hnd, sizeof(*hnd));
124
125         bitmap_set(bmap, i);
126
127         DLIST_ADD(Policy, p);
128         
129         DEBUG(4,("Opened policy hnd[%x] ", i));
130         dump_data(4, (char *)hnd->data, sizeof(hnd->data));
131
132         return True;
133 }
134
135 /****************************************************************************
136   find policy by handle
137 ****************************************************************************/
138 static struct policy *find_lsa_policy(POLICY_HND *hnd)
139 {
140         struct policy *p;
141
142         for (p=Policy;p;p=p->next) {
143                 if (memcmp(&p->pol_hnd, hnd, sizeof(*hnd)) == 0) {
144                         DEBUG(4,("Found policy hnd[%x] ", p->pnum));
145                         dump_data(4, (char *)hnd->data, sizeof(hnd->data));
146                         return p;
147                 }
148         }
149
150         DEBUG(4,("Policy not found: "));
151         dump_data(4, (char *)hnd->data, sizeof(hnd->data));
152
153         return NULL;
154 }
155
156 /****************************************************************************
157   find policy index by handle
158 ****************************************************************************/
159 int find_lsa_policy_by_hnd(POLICY_HND *hnd)
160 {
161         struct policy *p = find_lsa_policy(hnd);
162
163         return p?p->pnum:-1;
164 }
165
166 /****************************************************************************
167   set samr rid
168 ****************************************************************************/
169 BOOL set_lsa_policy_samr_rid(POLICY_HND *hnd, uint32 rid)
170 {
171         struct policy *p = find_lsa_policy(hnd);
172
173         if (p && p->open) {
174                 DEBUG(3,("Setting policy device rid=%x pnum=%x\n",
175                          rid, p->pnum));
176
177                 p->dev.samr.rid = rid;
178                 return True;
179         }
180
181         DEBUG(3,("Error setting policy rid=%x\n",rid));
182         return False;
183 }
184
185
186 /****************************************************************************
187   set samr pol status.  absolutely no idea what this is.
188 ****************************************************************************/
189 BOOL set_lsa_policy_samr_pol_status(POLICY_HND *hnd, uint32 pol_status)
190 {
191         struct policy *p = find_lsa_policy(hnd);
192
193         if (p && p->open) {
194                 DEBUG(3,("Setting policy status=%x pnum=%x\n",
195                           pol_status, p->pnum));
196
197                 p->dev.samr.status = pol_status;
198                 return True;
199         } 
200
201         DEBUG(3,("Error setting policy status=%x\n",
202                  pol_status));
203         return False;
204 }
205
206 /****************************************************************************
207   set samr sid
208 ****************************************************************************/
209 BOOL set_lsa_policy_samr_sid(POLICY_HND *hnd, DOM_SID *sid)
210 {
211         pstring sidstr;
212         struct policy *p = find_lsa_policy(hnd);
213
214         if (p && p->open) {
215                 DEBUG(3,("Setting policy sid=%s pnum=%x\n",
216                          sid_to_string(sidstr, sid), p->pnum));
217
218                 memcpy(&p->dev.samr.sid, sid, sizeof(*sid));
219                 return True;
220         }
221
222         DEBUG(3,("Error setting policy sid=%s\n",
223                   sid_to_string(sidstr, sid)));
224         return False;
225 }
226
227 /****************************************************************************
228   set samr rid
229 ****************************************************************************/
230 uint32 get_lsa_policy_samr_rid(POLICY_HND *hnd)
231 {
232         struct policy *p = find_lsa_policy(hnd);
233
234         if (p && p->open) {
235                 uint32 rid = p->dev.samr.rid;
236                 DEBUG(3,("Getting policy device rid=%x pnum=%x\n",
237                           rid, p->pnum));
238
239                 return rid;
240         }
241
242         DEBUG(3,("Error getting policy\n"));
243         return 0xffffffff;
244 }
245
246 /****************************************************************************
247   set reg name 
248 ****************************************************************************/
249 BOOL set_lsa_policy_reg_name(POLICY_HND *hnd, fstring name)
250 {
251         struct policy *p = find_lsa_policy(hnd);
252
253         if (p && p->open) {
254                 DEBUG(3,("Setting policy pnum=%x name=%s\n",
255                          p->pnum, name));
256
257                 fstrcpy(p->dev.reg.name, name);
258                 return True;
259         }
260
261         DEBUG(3,("Error setting policy name=%s\n", name));
262         return False;
263 }
264
265 /****************************************************************************
266   close an lsa policy
267 ****************************************************************************/
268 BOOL close_lsa_policy_hnd(POLICY_HND *hnd)
269 {
270         struct policy *p = find_lsa_policy(hnd);
271
272         if (!p) {
273                 DEBUG(3,("Error closing policy\n"));
274                 return False;
275         }
276
277         DEBUG(3,("Closed policy name pnum=%x\n",  p->pnum));
278
279         DLIST_REMOVE(Policy, p);
280
281         bitmap_clear(bmap, p->pnum);
282
283         ZERO_STRUCTP(p);
284
285         free(p);
286
287         return True;
288 }
289