Sync up handle creation with 2.2 branch. We can now join AS/U domains and
[ira/wip.git] / source3 / 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 2048
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 status; /* some sort of flag.  best to record it.  comes from opnum 0x39 */
44 };
45
46 static struct policy
47 {
48         struct policy *next, *prev;
49         int pnum;
50         BOOL open;
51         POLICY_HND pol_hnd;
52
53         union {
54                 struct samr_info samr;
55                 struct reg_info reg;
56         } dev;
57 } *Policy;
58
59 static struct bitmap *bmap;
60
61 static uint32 pol_hnd_low  = 0;
62 static uint32 pol_hnd_high = 0;
63
64 /*******************************************************************
65  Creates a POLICY_HND structure.
66 ********************************************************************/
67
68 void create_policy_handle(POLICY_HND *hnd, uint32 *hnd_low, uint32 *hnd_high)
69 {
70         if (hnd == NULL) return;
71
72     (*hnd_low)++;
73     if (*hnd_low == 0) (*hnd_high)++;
74
75     ZERO_STRUCTP(hnd);
76
77     SIVAL(&hnd->data1, 0 , 0);  /* first bit must be null */
78     SIVAL(&hnd->data2, 0 , *hnd_low ); /* second bit is incrementing */
79     SSVAL(&hnd->data3, 0 , *hnd_high); /* second bit is incrementing */
80     SSVAL(&hnd->data4, 0 , (*hnd_high>>16)); /* second bit is incrementing */
81     SIVAL(hnd->data5, 0, time(NULL)); /* something random */
82     SIVAL(hnd->data5, 4, sys_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_policy_handle(hnd, &pol_hnd_low, &pol_hnd_high);
123         p->pol_hnd = *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, sizeof(hnd));
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, sizeof(hnd));
146                         return p;
147                 }
148         }
149
150         DEBUG(4,("Policy not found: "));
151         dump_data(4, (char *)hnd, sizeof(hnd));
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 /****************************************************************************
168   set samr pol status.  absolutely no idea what this is.
169 ****************************************************************************/
170 BOOL set_lsa_policy_samr_pol_status(POLICY_HND *hnd, uint32 pol_status)
171 {
172         struct policy *p = find_lsa_policy(hnd);
173
174         if (p && p->open) {
175                 DEBUG(3,("Setting policy status=%x pnum=%x\n",
176                           pol_status, p->pnum));
177
178                 p->dev.samr.status = pol_status;
179                 return True;
180         } 
181
182         DEBUG(3,("Error setting policy status=%x\n",
183                  pol_status));
184         return False;
185 }
186
187 /****************************************************************************
188   set samr sid
189 ****************************************************************************/
190 BOOL set_lsa_policy_samr_sid(POLICY_HND *hnd, DOM_SID *sid)
191 {
192         fstring sidstr;
193         struct policy *p = find_lsa_policy(hnd);
194
195         if (p && p->open) {
196                 DEBUG(3,("Setting policy sid=%s pnum=%x\n",
197                          sid_to_string(sidstr, sid), p->pnum));
198
199                 memcpy(&p->dev.samr.sid, sid, sizeof(*sid));
200                 return True;
201         }
202
203         DEBUG(3,("Error setting policy sid=%s\n",
204                   sid_to_string(sidstr, sid)));
205         return False;
206 }
207
208 /****************************************************************************
209   get samr sid
210 ****************************************************************************/
211 BOOL get_lsa_policy_samr_sid(POLICY_HND *hnd, DOM_SID *sid)
212 {
213         struct policy *p = find_lsa_policy(hnd);
214
215         if (p != NULL && p->open) {
216                 fstring sidstr;
217                 memcpy(sid, &p->dev.samr.sid, sizeof(*sid));
218                 DEBUG(3,("Getting policy sid=%s pnum=%x\n",
219                          sid_to_string(sidstr, sid), p->pnum));
220
221                 return True;
222         }
223
224         DEBUG(3,("Error getting policy\n"));
225         return False;
226 }
227
228 /****************************************************************************
229   get samr rid
230 ****************************************************************************/
231 uint32 get_lsa_policy_samr_rid(POLICY_HND *hnd)
232 {
233         struct policy *p = find_lsa_policy(hnd);
234
235         if (p && p->open) {
236                 uint32 rid = p->dev.samr.sid.sub_auths[p->dev.samr.sid.num_auths-1];
237                 DEBUG(3,("Getting policy device rid=%x pnum=%x\n",
238                           rid, p->pnum));
239
240                 return rid;
241         }
242
243         DEBUG(3,("Error getting policy\n"));
244         return 0xffffffff;
245 }
246
247 /****************************************************************************
248   set reg name 
249 ****************************************************************************/
250 BOOL set_lsa_policy_reg_name(POLICY_HND *hnd, fstring name)
251 {
252         struct policy *p = find_lsa_policy(hnd);
253
254         if (p && p->open) {
255                 DEBUG(3,("Setting policy pnum=%x name=%s\n",
256                          p->pnum, name));
257
258                 fstrcpy(p->dev.reg.name, name);
259                 return True;
260         }
261
262         DEBUG(3,("Error setting policy name=%s\n", name));
263         return False;
264 }
265
266 /****************************************************************************
267   close an lsa policy
268 ****************************************************************************/
269 BOOL close_lsa_policy_hnd(POLICY_HND *hnd)
270 {
271         struct policy *p = find_lsa_policy(hnd);
272
273         if (!p) {
274                 DEBUG(3,("Error closing policy\n"));
275                 return False;
276         }
277
278         DEBUG(3,("Closed policy name pnum=%x\n",  p->pnum));
279
280         DLIST_REMOVE(Policy, p);
281
282         bitmap_clear(bmap, p->pnum);
283
284         ZERO_STRUCTP(p);
285
286         free(p);
287
288         return True;
289 }
290
291 #undef OLD_NTDOMAIN