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_connection *con;
56 void (*free)(struct cli_connection*);
61 struct policy *next, *prev;
68 struct samr_info *samr;
76 static struct bitmap *bmap;
79 /****************************************************************************
80 create a unique policy handle
81 ****************************************************************************/
82 static void create_pol_hnd(POLICY_HND *hnd)
84 static uint32 pol_hnd_low = 0;
85 static uint32 pol_hnd_high = 0;
87 if (hnd == NULL) return;
89 /* i severely doubt that pol_hnd_high will ever be non-zero... */
91 if (pol_hnd_low == 0) pol_hnd_high++;
93 SIVAL(hnd->data, 0 , 0x0); /* first bit must be null */
94 SIVAL(hnd->data, 4 , pol_hnd_low ); /* second bit is incrementing */
95 SIVAL(hnd->data, 8 , pol_hnd_high); /* second bit is incrementing */
96 SIVAL(hnd->data, 12, time(NULL)); /* something random */
97 SIVAL(hnd->data, 16, getpid()); /* something more random */
100 /****************************************************************************
101 initialise policy handle states...
102 ****************************************************************************/
103 BOOL init_policy_hnd(int num_pol_hnds)
105 bmap = bitmap_allocate(num_pol_hnds);
110 /****************************************************************************
111 find first available policy slot. creates a policy handle for you.
112 ****************************************************************************/
113 BOOL register_policy_hnd(POLICY_HND *hnd)
118 i = bitmap_find(bmap, 1);
121 DEBUG(0,("ERROR: out of Policy Handles!\n"));
125 p = (struct policy *)malloc(sizeof(*p));
127 DEBUG(0,("ERROR: out of memory!\n"));
135 p->type = POL_NO_INFO;
137 memcpy(&p->pol_hnd, hnd, sizeof(*hnd));
141 DLIST_ADD(Policy, p);
143 DEBUG(4,("Opened policy hnd[%x] ", i));
144 dump_data(4, (char *)hnd->data, sizeof(hnd->data));
149 /****************************************************************************
150 find first available policy slot. creates a policy handle for you.
151 ****************************************************************************/
152 BOOL open_policy_hnd(POLICY_HND *hnd)
155 return register_policy_hnd(hnd);
158 /****************************************************************************
159 find policy by handle
160 ****************************************************************************/
161 static struct policy *find_policy(const POLICY_HND *hnd)
165 for (p=Policy;p;p=p->next) {
166 if (memcmp(&p->pol_hnd, hnd, sizeof(*hnd)) == 0) {
167 DEBUG(4,("Found policy hnd[%x] ", p->pnum));
168 dump_data(4, (const char *)hnd->data,
174 DEBUG(4,("Policy not found: "));
175 dump_data(4, (const char *)hnd->data, sizeof(hnd->data));
180 /****************************************************************************
181 find policy index by handle
182 ****************************************************************************/
183 int find_policy_by_hnd(const POLICY_HND *hnd)
185 struct policy *p = find_policy(hnd);
190 /****************************************************************************
192 ****************************************************************************/
193 BOOL set_policy_samr_rid(POLICY_HND *hnd, uint32 rid)
195 struct policy *p = find_policy(hnd);
199 DEBUG(3,("Setting policy device rid=%x pnum=%x\n",
202 if (p->dev.samr == NULL)
204 p->dev.samr = (struct samr_info*)malloc(sizeof(*p->dev.samr));
206 if (p->dev.samr == NULL)
210 p->dev.samr->rid = rid;
214 DEBUG(3,("Error setting policy rid=%x\n",rid));
219 /****************************************************************************
220 set samr pol status. absolutely no idea what this is.
221 ****************************************************************************/
222 BOOL set_policy_samr_pol_status(POLICY_HND *hnd, uint32 pol_status)
224 struct policy *p = find_policy(hnd);
228 DEBUG(3,("Setting policy status=%x pnum=%x\n",
229 pol_status, p->pnum));
231 if (p->dev.samr == NULL)
233 p->type = POL_SAMR_INFO;
234 p->dev.samr = (struct samr_info*)malloc(sizeof(*p->dev.samr));
236 if (p->dev.samr == NULL)
240 p->dev.samr->status = pol_status;
244 DEBUG(3,("Error setting policy status=%x\n",
249 /****************************************************************************
251 ****************************************************************************/
252 BOOL set_policy_samr_sid(POLICY_HND *hnd, DOM_SID *sid)
255 struct policy *p = find_policy(hnd);
258 DEBUG(3,("Setting policy sid=%s pnum=%x\n",
259 sid_to_string(sidstr, sid), p->pnum));
261 if (p->dev.samr == NULL)
263 p->type = POL_SAMR_INFO;
264 p->dev.samr = (struct samr_info*)malloc(sizeof(*p->dev.samr));
266 if (p->dev.samr == NULL)
270 memcpy(&p->dev.samr->sid, sid, sizeof(*sid));
274 DEBUG(3,("Error setting policy sid=%s\n",
275 sid_to_string(sidstr, sid)));
279 /****************************************************************************
281 ****************************************************************************/
282 BOOL get_policy_samr_sid(POLICY_HND *hnd, DOM_SID *sid)
284 struct policy *p = find_policy(hnd);
286 if (p != NULL && p->open)
289 memcpy(sid, &p->dev.samr->sid, sizeof(*sid));
290 DEBUG(3,("Getting policy sid=%s pnum=%x\n",
291 sid_to_string(sidstr, sid), p->pnum));
296 DEBUG(3,("Error getting policy\n"));
300 /****************************************************************************
302 ****************************************************************************/
303 uint32 get_policy_samr_rid(POLICY_HND *hnd)
305 struct policy *p = find_policy(hnd);
308 uint32 rid = p->dev.samr->rid;
309 DEBUG(3,("Getting policy device rid=%x pnum=%x\n",
315 DEBUG(3,("Error getting policy\n"));
319 /****************************************************************************
321 ****************************************************************************/
322 BOOL set_policy_reg_name(POLICY_HND *hnd, fstring name)
324 struct policy *p = find_policy(hnd);
328 DEBUG(3,("Getting policy pnum=%x\n",
331 if (p->dev.reg == NULL)
333 p->type = POL_REG_INFO;
334 p->dev.reg = (struct reg_info*)malloc(sizeof(*p->dev.reg));
336 if (p->dev.reg == NULL)
340 fstrcpy(p->dev.reg->name, name);
344 DEBUG(3,("Error setting policy name=%s\n", name));
348 /****************************************************************************
350 ****************************************************************************/
351 BOOL get_policy_reg_name(POLICY_HND *hnd, fstring name)
353 struct policy *p = find_policy(hnd);
357 DEBUG(3,("Setting policy pnum=%x name=%s\n",
360 fstrcpy(name, p->dev.reg->name);
361 DEBUG(5,("getting policy reg name=%s\n", name));
365 DEBUG(3,("Error getting policy reg name\n"));
369 /****************************************************************************
371 ****************************************************************************/
372 BOOL set_policy_con(POLICY_HND *hnd, struct cli_connection *con,
373 void (*free_fn)(struct cli_connection *))
375 struct policy *p = find_policy(hnd);
379 DEBUG(3,("Setting policy con state pnum=%x\n", p->pnum));
381 if (p->dev.con == NULL)
383 p->type = POL_CLI_INFO;
384 p->dev.con = (struct con_info*)malloc(sizeof(*p->dev.con));
386 if (p->dev.con == NULL)
390 p->dev.con->con = con;
391 p->dev.con->free = free_fn;
395 DEBUG(3,("Error setting policy con state\n"));
400 /****************************************************************************
402 ****************************************************************************/
403 BOOL get_policy_con(const POLICY_HND *hnd, struct cli_connection **con)
405 struct policy *p = find_policy(hnd);
407 if (p != NULL && p->open)
409 DEBUG(3,("Getting con state pnum=%x\n", p->pnum));
413 (*con ) = p->dev.con->con;
419 DEBUG(3,("Error getting policy\n"));
423 /****************************************************************************
425 ****************************************************************************/
426 BOOL close_policy_hnd(POLICY_HND *hnd)
428 struct policy *p = find_policy(hnd);
432 DEBUG(3,("Error closing policy\n"));
436 DEBUG(3,("Closed policy name pnum=%x\n", p->pnum));
438 DLIST_REMOVE(Policy, p);
440 bitmap_clear(bmap, p->pnum);
459 if (p->dev.con->free != NULL)
461 p->dev.con->free(p->dev.con->con);