Another large patch for the passdb rewrite.
[Samba.git] / source / rpc_server / srv_lsa_hnd.c
blob0782c8c4b280313de5230251e632bf4f22f66be9
1 #define OLD_NTDOMAIN 1
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.
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.
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.
25 #include "includes.h"
27 extern int DEBUGLEVEL;
29 #ifndef MAX_OPEN_POLS
30 #define MAX_OPEN_POLS 64
31 #endif
33 struct reg_info
35 /* for use by \PIPE\winreg */
36 fstring name; /* name of registry key */
39 struct samr_info
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 */
46 static struct policy
48 struct policy *next, *prev;
49 int pnum;
50 BOOL open;
51 POLICY_HND pol_hnd;
53 union {
54 struct samr_info samr;
55 struct reg_info reg;
56 } dev;
57 } *Policy;
59 static struct bitmap *bmap;
62 /****************************************************************************
63 create a unique policy handle
64 ****************************************************************************/
65 static void create_pol_hnd(POLICY_HND *hnd)
67 static uint32 pol_hnd_low = 0;
68 static uint32 pol_hnd_high = 0;
70 if (hnd == NULL) return;
72 /* i severely doubt that pol_hnd_high will ever be non-zero... */
73 pol_hnd_low++;
74 if (pol_hnd_low == 0) pol_hnd_high++;
76 SIVAL(hnd->data, 0 , 0x0); /* first bit must be null */
77 SIVAL(hnd->data, 4 , pol_hnd_low ); /* second bit is incrementing */
78 SIVAL(hnd->data, 8 , pol_hnd_high); /* second bit is incrementing */
79 SIVAL(hnd->data, 12, time(NULL)); /* something random */
80 SIVAL(hnd->data, 16, sys_getpid()); /* something more random */
83 /****************************************************************************
84 initialise policy handle states...
85 ****************************************************************************/
86 void init_lsa_policy_hnd(void)
88 bmap = bitmap_allocate(MAX_OPEN_POLS);
89 if (!bmap) {
90 exit_server("out of memory in init_lsa_policy_hnd\n");
94 /****************************************************************************
95 find first available policy slot. creates a policy handle for you.
96 ****************************************************************************/
97 BOOL open_lsa_policy_hnd(POLICY_HND *hnd)
99 int i;
100 struct policy *p;
102 i = bitmap_find(bmap, 1);
104 if (i == -1) {
105 DEBUG(0,("ERROR: out of Policy Handles!\n"));
106 return False;
109 p = (struct policy *)malloc(sizeof(*p));
110 if (!p) {
111 DEBUG(0,("ERROR: out of memory!\n"));
112 return False;
115 ZERO_STRUCTP(p);
117 p->open = True;
118 p->pnum = i;
120 create_pol_hnd(hnd);
121 memcpy(&p->pol_hnd, hnd, sizeof(*hnd));
123 bitmap_set(bmap, i);
125 DLIST_ADD(Policy, p);
127 DEBUG(4,("Opened policy hnd[%x] ", i));
128 dump_data(4, (char *)hnd->data, sizeof(hnd->data));
130 return True;
133 /****************************************************************************
134 find policy by handle
135 ****************************************************************************/
136 static struct policy *find_lsa_policy(POLICY_HND *hnd)
138 struct policy *p;
140 for (p=Policy;p;p=p->next) {
141 if (memcmp(&p->pol_hnd, hnd, sizeof(*hnd)) == 0) {
142 DEBUG(4,("Found policy hnd[%x] ", p->pnum));
143 dump_data(4, (char *)hnd->data, sizeof(hnd->data));
144 return p;
148 DEBUG(4,("Policy not found: "));
149 dump_data(4, (char *)hnd->data, sizeof(hnd->data));
151 return NULL;
154 /****************************************************************************
155 find policy index by handle
156 ****************************************************************************/
157 int find_lsa_policy_by_hnd(POLICY_HND *hnd)
159 struct policy *p = find_lsa_policy(hnd);
161 return p?p->pnum:-1;
165 /****************************************************************************
166 set samr pol status. absolutely no idea what this is.
167 ****************************************************************************/
168 BOOL set_lsa_policy_samr_pol_status(POLICY_HND *hnd, uint32 pol_status)
170 struct policy *p = find_lsa_policy(hnd);
172 if (p && p->open) {
173 DEBUG(3,("Setting policy status=%x pnum=%x\n",
174 pol_status, p->pnum));
176 p->dev.samr.status = pol_status;
177 return True;
180 DEBUG(3,("Error setting policy status=%x\n",
181 pol_status));
182 return False;
185 /****************************************************************************
186 set samr sid
187 ****************************************************************************/
188 BOOL set_lsa_policy_samr_sid(POLICY_HND *hnd, DOM_SID *sid)
190 fstring sidstr;
191 struct policy *p = find_lsa_policy(hnd);
193 if (p && p->open) {
194 DEBUG(3,("Setting policy sid=%s pnum=%x\n",
195 sid_to_string(sidstr, sid), p->pnum));
197 memcpy(&p->dev.samr.sid, sid, sizeof(*sid));
198 return True;
201 DEBUG(3,("Error setting policy sid=%s\n",
202 sid_to_string(sidstr, sid)));
203 return False;
206 /****************************************************************************
207 get samr sid
208 ****************************************************************************/
209 BOOL get_lsa_policy_samr_sid(POLICY_HND *hnd, DOM_SID *sid)
211 struct policy *p = find_lsa_policy(hnd);
213 if (p != NULL && p->open) {
214 fstring sidstr;
215 memcpy(sid, &p->dev.samr.sid, sizeof(*sid));
216 DEBUG(3,("Getting policy sid=%s pnum=%x\n",
217 sid_to_string(sidstr, sid), p->pnum));
219 return True;
222 DEBUG(3,("Error getting policy\n"));
223 return False;
226 /****************************************************************************
227 get samr rid
228 ****************************************************************************/
229 uint32 get_lsa_policy_samr_rid(POLICY_HND *hnd)
231 struct policy *p = find_lsa_policy(hnd);
233 if (p && p->open) {
234 uint32 rid = p->dev.samr.sid.sub_auths[p->dev.samr.sid.num_auths-1];
235 DEBUG(3,("Getting policy device rid=%x pnum=%x\n",
236 rid, p->pnum));
238 return rid;
241 DEBUG(3,("Error getting policy\n"));
242 return 0xffffffff;
245 /****************************************************************************
246 set reg name
247 ****************************************************************************/
248 BOOL set_lsa_policy_reg_name(POLICY_HND *hnd, fstring name)
250 struct policy *p = find_lsa_policy(hnd);
252 if (p && p->open) {
253 DEBUG(3,("Setting policy pnum=%x name=%s\n",
254 p->pnum, name));
256 fstrcpy(p->dev.reg.name, name);
257 return True;
260 DEBUG(3,("Error setting policy name=%s\n", name));
261 return False;
264 /****************************************************************************
265 close an lsa policy
266 ****************************************************************************/
267 BOOL close_lsa_policy_hnd(POLICY_HND *hnd)
269 struct policy *p = find_lsa_policy(hnd);
271 if (!p) {
272 DEBUG(3,("Error closing policy\n"));
273 return False;
276 DEBUG(3,("Closed policy name pnum=%x\n", p->pnum));
278 DLIST_REMOVE(Policy, p);
280 bitmap_clear(bmap, p->pnum);
282 ZERO_STRUCTP(p);
284 free(p);
286 return True;
289 #undef OLD_NTDOMAIN