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