3 * Unix SMB/Netbios implementation.
5 * RPC Pipe client / server routines
6 * Copyright (C) Andrew Tridgell 1992-1997,
7 * Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
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.
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.
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.
28 extern int DEBUGLEVEL;
31 #define MAX_OPEN_POLS 64
35 #define POL_REG_INFO 1
36 #define POL_SAMR_INFO 2
37 #define POL_CLI_INFO 3
41 /* for use by \PIPE\winreg */
42 fstring name; /* name of registry key */
47 /* for use by the \PIPE\samr policy */
49 uint32 rid; /* relative id associated with the pol_hnd */
50 uint32 status; /* some sort of flag. best to record it. comes from opnum 0x39 */
55 struct cli_state *cli;
57 void (*free)(struct cli_state*, uint16 fnum);
62 struct policy *next, *prev;
69 struct samr_info *samr;
77 static struct bitmap *bmap;
80 /****************************************************************************
81 create a unique policy handle
82 ****************************************************************************/
83 static void create_pol_hnd(POLICY_HND *hnd)
85 static uint32 pol_hnd_low = 0;
86 static uint32 pol_hnd_high = 0;
88 if (hnd == NULL) return;
90 /* i severely doubt that pol_hnd_high will ever be non-zero... */
92 if (pol_hnd_low == 0) pol_hnd_high++;
94 SIVAL(hnd->data, 0 , 0x0); /* first bit must be null */
95 SIVAL(hnd->data, 4 , pol_hnd_low ); /* second bit is incrementing */
96 SIVAL(hnd->data, 8 , pol_hnd_high); /* second bit is incrementing */
97 SIVAL(hnd->data, 12, time(NULL)); /* something random */
98 SIVAL(hnd->data, 16, getpid()); /* something more random */
101 /****************************************************************************
102 initialise policy handle states...
103 ****************************************************************************/
104 BOOL init_policy_hnd(int num_pol_hnds)
106 bmap = bitmap_allocate(num_pol_hnds);
111 /****************************************************************************
112 find first available policy slot. creates a policy handle for you.
113 ****************************************************************************/
114 BOOL register_policy_hnd(POLICY_HND *hnd)
119 i = bitmap_find(bmap, 1);
122 DEBUG(0,("ERROR: out of Policy Handles!\n"));
126 p = (struct policy *)malloc(sizeof(*p));
128 DEBUG(0,("ERROR: out of memory!\n"));
136 p->type = POL_NO_INFO;
138 memcpy(&p->pol_hnd, hnd, sizeof(*hnd));
142 DLIST_ADD(Policy, p);
144 DEBUG(4,("Opened policy hnd[%x] ", i));
145 dump_data(4, (char *)hnd->data, sizeof(hnd->data));
150 /****************************************************************************
151 find first available policy slot. creates a policy handle for you.
152 ****************************************************************************/
153 BOOL open_policy_hnd(POLICY_HND *hnd)
156 return register_policy_hnd(hnd);
159 /****************************************************************************
160 find policy by handle
161 ****************************************************************************/
162 static struct policy *find_policy(const POLICY_HND *hnd)
166 for (p=Policy;p;p=p->next) {
167 if (memcmp(&p->pol_hnd, hnd, sizeof(*hnd)) == 0) {
168 DEBUG(4,("Found policy hnd[%x] ", p->pnum));
169 dump_data(4, (const char *)hnd->data,
175 DEBUG(4,("Policy not found: "));
176 dump_data(4, (const char *)hnd->data, sizeof(hnd->data));
181 /****************************************************************************
182 find policy index by handle
183 ****************************************************************************/
184 int find_policy_by_hnd(const POLICY_HND *hnd)
186 struct policy *p = find_policy(hnd);
191 /****************************************************************************
193 ****************************************************************************/
194 BOOL set_policy_samr_rid(POLICY_HND *hnd, uint32 rid)
196 struct policy *p = find_policy(hnd);
200 DEBUG(3,("Setting policy device rid=%x pnum=%x\n",
203 if (p->dev.samr == NULL)
205 p->dev.samr = (struct samr_info*)malloc(sizeof(*p->dev.samr));
207 if (p->dev.samr == NULL)
211 p->dev.samr->rid = rid;
215 DEBUG(3,("Error setting policy rid=%x\n",rid));
220 /****************************************************************************
221 set samr pol status. absolutely no idea what this is.
222 ****************************************************************************/
223 BOOL set_policy_samr_pol_status(POLICY_HND *hnd, uint32 pol_status)
225 struct policy *p = find_policy(hnd);
229 DEBUG(3,("Setting policy status=%x pnum=%x\n",
230 pol_status, p->pnum));
232 if (p->dev.samr == NULL)
234 p->type = POL_SAMR_INFO;
235 p->dev.samr = (struct samr_info*)malloc(sizeof(*p->dev.samr));
237 if (p->dev.samr == NULL)
241 p->dev.samr->status = pol_status;
245 DEBUG(3,("Error setting policy status=%x\n",
250 /****************************************************************************
252 ****************************************************************************/
253 BOOL set_policy_samr_sid(POLICY_HND *hnd, DOM_SID *sid)
256 struct policy *p = find_policy(hnd);
259 DEBUG(3,("Setting policy sid=%s pnum=%x\n",
260 sid_to_string(sidstr, sid), p->pnum));
262 if (p->dev.samr == NULL)
264 p->type = POL_SAMR_INFO;
265 p->dev.samr = (struct samr_info*)malloc(sizeof(*p->dev.samr));
267 if (p->dev.samr == NULL)
271 memcpy(&p->dev.samr->sid, sid, sizeof(*sid));
275 DEBUG(3,("Error setting policy sid=%s\n",
276 sid_to_string(sidstr, sid)));
280 /****************************************************************************
282 ****************************************************************************/
283 BOOL get_policy_samr_sid(POLICY_HND *hnd, DOM_SID *sid)
285 struct policy *p = find_policy(hnd);
287 if (p != NULL && p->open)
290 memcpy(sid, &p->dev.samr->sid, sizeof(*sid));
291 DEBUG(3,("Getting policy sid=%s pnum=%x\n",
292 sid_to_string(sidstr, sid), p->pnum));
297 DEBUG(3,("Error getting policy\n"));
301 /****************************************************************************
303 ****************************************************************************/
304 uint32 get_policy_samr_rid(POLICY_HND *hnd)
306 struct policy *p = find_policy(hnd);
309 uint32 rid = p->dev.samr->rid;
310 DEBUG(3,("Getting policy device rid=%x pnum=%x\n",
316 DEBUG(3,("Error getting policy\n"));
320 /****************************************************************************
322 ****************************************************************************/
323 BOOL set_policy_reg_name(POLICY_HND *hnd, fstring name)
325 struct policy *p = find_policy(hnd);
329 DEBUG(3,("Getting policy pnum=%x\n",
332 if (p->dev.reg == NULL)
334 p->type = POL_REG_INFO;
335 p->dev.reg = (struct reg_info*)malloc(sizeof(*p->dev.reg));
337 if (p->dev.reg == NULL)
341 fstrcpy(p->dev.reg->name, name);
345 DEBUG(3,("Error setting policy name=%s\n", name));
349 /****************************************************************************
351 ****************************************************************************/
352 BOOL get_policy_reg_name(POLICY_HND *hnd, fstring name)
354 struct policy *p = find_policy(hnd);
358 DEBUG(3,("Setting policy pnum=%x name=%s\n",
361 fstrcpy(name, p->dev.reg->name);
362 DEBUG(5,("getting policy reg name=%s\n", name));
366 DEBUG(3,("Error getting policy reg name\n"));
370 /****************************************************************************
372 ****************************************************************************/
373 BOOL set_policy_cli_state(POLICY_HND *hnd, struct cli_state *cli, uint16 fnum,
374 void (*free_fn)(struct cli_state *, uint16))
376 struct policy *p = find_policy(hnd);
380 DEBUG(3,("Setting policy cli state pnum=%x\n", p->pnum));
382 if (p->dev.cli == NULL)
384 p->type = POL_CLI_INFO;
385 p->dev.cli = (struct cli_info*)malloc(sizeof(*p->dev.cli));
387 if (p->dev.cli == NULL)
391 p->dev.cli->cli = cli;
392 p->dev.cli->free = free_fn;
393 p->dev.cli->fnum = fnum;
397 DEBUG(3,("Error setting policy cli state\n"));
402 /****************************************************************************
404 ****************************************************************************/
405 BOOL get_policy_cli_state(const POLICY_HND *hnd, struct cli_state **cli,
408 struct policy *p = find_policy(hnd);
410 if (p != NULL && p->open)
412 DEBUG(3,("Getting cli state pnum=%x\n", p->pnum));
414 (*cli ) = p->dev.cli->cli;
415 (*fnum) = p->dev.cli->fnum;
420 DEBUG(3,("Error getting policy\n"));
424 /****************************************************************************
426 ****************************************************************************/
427 BOOL close_policy_hnd(POLICY_HND *hnd)
429 struct policy *p = find_policy(hnd);
433 DEBUG(3,("Error closing policy\n"));
437 DEBUG(3,("Closed policy name pnum=%x\n", p->pnum));
439 DLIST_REMOVE(Policy, p);
441 bitmap_clear(bmap, p->pnum);
460 if (p->dev.cli->free != NULL)
462 p->dev.cli->free(p->dev.cli->cli,