3 * Unix SMB/Netbios implementation.
5 * RPC Pipe client / server routines
6 * Copyright (C) Andrew Tridgell 1992-2000,
7 * Copyright (C) Luke Kenneth Casson Leighton 1996-2000,
8 * Copyright (C) Elrond 2000
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.
29 extern int DEBUGLEVEL
;
34 struct policy
*next
, *prev
;
44 void (*free_fn
) (void *);
48 /****************************************************************************
49 i hate this. a global policy handle cache. yuk.
50 ****************************************************************************/
51 struct policy_cache
*get_global_hnd_cache(void)
53 static struct policy_cache
*cache
= NULL
;
57 cache
= init_policy_cache(1024);
62 /****************************************************************************
63 create a unique policy handle
64 ****************************************************************************/
65 static void create_pol_hnd(POLICY_HND
*hnd
)
67 static uint32 pol_hnd_low
= 0;
77 unix_to_nt_time(&ntt
, time(NULL
));
80 hnd
->uuid
.time_low
= ntt
.low
;
81 hnd
->uuid
.time_mid
= (ntt
.high
& 0xffff);
82 hnd
->uuid
.time_hi_and_version
= ((ntt
.high
>> 16) & 0xffff);
83 SIVAL(hnd
->uuid
.remaining
, 0, getpid());
84 SIVAL(hnd
->uuid
.remaining
, 4, pol_hnd_low
);
87 /****************************************************************************
88 initialise policy handle states...
89 ****************************************************************************/
90 struct policy_cache
*init_policy_cache(int num_pol_hnds
)
92 struct policy_cache
*cache
= malloc(sizeof(struct policy_cache
));
101 /****************************************************************************
102 free policy handle states...
103 ****************************************************************************/
104 void free_policy_cache(struct policy_cache
*cache
)
109 /****************************************************************************
110 find policy by handle
111 ****************************************************************************/
112 static struct policy
*find_policy(struct policy_cache
*cache
,
113 const POLICY_HND
*hnd
)
119 DEBUG(0, ("find_policy: NULL cache\n"));
125 DEBUG(0, ("find_policy: NULL handle\n"));
130 for (p
= cache
->Policy
; p
; p
= p
->next
)
132 DEBUG(10, ("Compare policy hnd[%x] ", p
->pnum
));
133 dump_data(10, (const char *)hnd
, sizeof(*hnd
));
134 if (memcmp(&p
->pol_hnd
, hnd
, sizeof(*hnd
)) == 0)
136 DEBUG(4, ("Found policy hnd[%x] ", p
->pnum
));
137 dump_data(4, (const char *)hnd
, sizeof(*hnd
));
142 DEBUG(4, ("cache->Policy not found: "));
143 dump_data(4, (const char *)hnd
, sizeof(*hnd
));
148 /****************************************************************************
149 set the name of a POLICY_HND
150 ****************************************************************************/
151 BOOL
policy_hnd_set_name(struct policy_cache
*cache
,
152 POLICY_HND
*hnd
, const char *name
)
154 struct policy
*p
= find_policy(cache
, hnd
);
157 DEBUG(3, ("Error setting name for policy\n"));
163 DEBUG(4, ("policy pnum=%x setting name to %s\n",
165 p
->name
= strdup(name
);
166 return (p
->name
!= NULL
);
170 DEBUG(4, ("policy pnum=%x setting name to %s\n",
177 /****************************************************************************
178 get the name of a POLICY_HND
179 ****************************************************************************/
180 static const char *pol_get_name(const struct policy
*p
)
193 /****************************************************************************
194 get the name of a POLICY_HND, public interface
195 ****************************************************************************/
196 const char *policy_hnd_get_name(struct policy_cache
*cache
,
197 const POLICY_HND
*hnd
)
200 struct policy
*p
= find_policy(cache
, hnd
);
204 DEBUG(3, ("Error getting name for policy\n"));
205 return "(invalid POLICY_HND)";
207 name
= pol_get_name(p
);
208 DEBUG(4, ("policy(pnum=%x %s): getting name\n", p
->pnum
, name
));
213 /****************************************************************************
214 find first available policy slot. copies a policy handle for you.
215 ****************************************************************************/
216 BOOL
dup_policy_hnd(struct policy_cache
*cache
,
217 POLICY_HND
*hnd
, const POLICY_HND
*from
)
219 struct policy
*p
= find_policy(cache
, from
);
225 DEBUG(3, ("Duplicating policy state pnum=%x\n", p
->pnum
));
226 return register_policy_hnd(cache
, &p
->key
, hnd
, p
->access_mask
);
229 /****************************************************************************
230 find first available policy slot. creates a policy handle for you.
231 ****************************************************************************/
232 BOOL
register_policy_hnd(struct policy_cache
*cache
,
233 const vuser_key
* key
,
234 POLICY_HND
*hnd
, uint32 access_mask
)
237 static int count
= 1;
239 p
= (struct policy
*)malloc(sizeof(*p
));
242 DEBUG(0, ("ERROR: out of memory!\n"));
250 p
->access_mask
= access_mask
;
257 p
->key
.vuid
= UID_FIELD_INVALID
;
258 p
->key
.pid
= getpid();
262 DLIST_ADD(cache
->Policy
, p
);
264 DEBUG(4, ("Opened policy hnd[%x] ", p
->pnum
));
265 DEBUG(10, ("register_policy_hnd: vuser [%d, %x]\n",
266 p
->key
.pid
, p
->key
.vuid
));
268 memcpy(&p
->pol_hnd
, hnd
, sizeof(*hnd
));
269 dump_data(4, (char *)hnd
, sizeof(*hnd
));
274 /****************************************************************************
275 find first available policy slot. creates a policy handle for you.
276 ****************************************************************************/
277 BOOL
open_policy_hnd(struct policy_cache
*cache
,
278 const vuser_key
* key
,
279 POLICY_HND
*hnd
, uint32 access_mask
)
282 return register_policy_hnd(cache
, key
, hnd
, access_mask
);
285 /****************************************************************************
286 find first available policy slot. creates a policy handle for you.
287 ****************************************************************************/
288 BOOL
open_policy_hnd_link(struct policy_cache
*cache
,
289 const POLICY_HND
*parent_hnd
,
290 POLICY_HND
*hnd
, uint32 access_mask
)
292 const vuser_key
*key
= get_policy_vuser_key(cache
, parent_hnd
);
298 return register_policy_hnd(cache
, key
, hnd
, access_mask
);
301 /****************************************************************************
302 find policy index by handle
303 ****************************************************************************/
304 int find_policy_by_hnd(struct policy_cache
*cache
, const POLICY_HND
*hnd
)
306 struct policy
*p
= find_policy(cache
, hnd
);
308 return p
? p
->pnum
: -1;
312 /****************************************************************************
314 ****************************************************************************/
315 BOOL
set_policy_state(struct policy_cache
*cache
, POLICY_HND
*hnd
,
316 void (*fn
) (void *), void *dev
)
318 struct policy
*p
= find_policy(cache
, hnd
);
322 DEBUG(3, ("policy(pnum=%x %s): Setting policy state\n",
323 p
->pnum
, pol_get_name(p
)));
330 DEBUG(3, ("Error setting policy state\n"));
335 /****************************************************************************
337 ****************************************************************************/
338 void *get_policy_state_info(struct policy_cache
*cache
, const POLICY_HND
*hnd
)
340 struct policy
*p
= find_policy(cache
, hnd
);
342 if (p
!= NULL
&& p
->open
)
344 DEBUG(3, ("policy(pnum=%x %s): Getting policy state\n",
345 p
->pnum
, pol_get_name(p
)));
349 DEBUG(3, ("Error getting policy state\n"));
353 /****************************************************************************
354 set the type of the state of a POLICY_HND
355 ****************************************************************************/
356 BOOL
policy_hnd_set_state_type(struct policy_cache
*cache
,
357 POLICY_HND
*hnd
, int type
)
359 struct policy
*p
= find_policy(cache
, hnd
);
363 DEBUG(3, ("Error setting type for policy state\n"));
366 DEBUG(4, ("policy(pnum=%x %s): setting type to %d\n",
367 p
->pnum
, pol_get_name(p
), type
));
372 /****************************************************************************
373 get the type of the state of a POLICY_HND
374 ****************************************************************************/
375 int policy_hnd_get_state_type(struct policy_cache
*cache
,
376 const POLICY_HND
*hnd
)
378 struct policy
*p
= find_policy(cache
, hnd
);
382 DEBUG(3, ("Error getting type for policy state\n"));
385 DEBUG(4, ("policy(pnum=%x %s): getting type %d\n",
386 p
->pnum
, pol_get_name(p
), p
->type
));
391 /****************************************************************************
392 check the type of the state of a POLICY_HND
393 ****************************************************************************/
394 BOOL
policy_hnd_check_state_type(struct policy_cache
*cache
,
395 const POLICY_HND
*hnd
, int type
)
397 struct policy
*p
= find_policy(cache
, hnd
);
402 DEBUG(3, ("Error checking type for policy state\n"));
406 ret
= (p
->type
== type
);
410 DEBUG(4, ("policy(pnum=%x %s): checking if type %d is %d\n",
411 p
->pnum
, pol_get_name(p
), p
->type
, type
));
415 DEBUG(3, ("policy(pnum=%x %s): type %d is not %d\n",
416 p
->pnum
, pol_get_name(p
), p
->type
, type
));
422 /****************************************************************************
424 ****************************************************************************/
425 BOOL
close_policy_hnd(struct policy_cache
*cache
, POLICY_HND
*hnd
)
427 struct policy
*p
= find_policy(cache
, hnd
);
431 DEBUG(3, ("Error closing policy\n"));
435 DEBUG(3, ("policy(pnum=%x %s): Closing\n", p
->pnum
, pol_get_name(p
)));
437 DLIST_REMOVE(cache
->Policy
, p
);
441 if (p
->free_fn
!= NULL
)
453 DEBUG(10, ("policy closed\n"));
458 /****************************************************************************
460 ****************************************************************************/
461 BOOL
policy_link_key(struct policy_cache
*cache
, const POLICY_HND
*hnd
,
464 struct policy
*p
= find_policy(cache
, hnd
);
465 struct policy
*pto
= find_policy(cache
, to
);
467 if (p
!= NULL
&& p
->open
&& pto
!= NULL
&& pto
->open
)
469 DEBUG(3, ("Linking policy key pnum=%x pid=%d vuid=%x\n",
470 p
->key
.pid
, p
->key
.vuid
, p
->pnum
));
475 DEBUG(3, ("Error getting policy link states\n"));
479 /****************************************************************************
481 ****************************************************************************/
482 const vuser_key
*get_policy_vuser_key(struct policy_cache
*cache
,
483 const POLICY_HND
*hnd
)
485 struct policy
*p
= find_policy(cache
, hnd
);
487 if (p
!= NULL
&& p
->open
)
489 DEBUG(3, ("Getting policy vuser_key pnum=%x pid=%d vuid=%x\n",
490 p
->pnum
, p
->key
.pid
, p
->key
.vuid
));
494 DEBUG(3, ("Error getting policy state\n"));
498 /****************************************************************************
499 get user session key.
500 ****************************************************************************/
501 BOOL
pol_get_usr_sesskey(struct policy_cache
*cache
, const POLICY_HND
*hnd
,
502 uchar usr_sess_key
[16])
504 const vuser_key
*key
= get_policy_vuser_key(cache
, hnd
);
507 if (key
== NULL
|| key
->vuid
== UID_FIELD_INVALID
)
509 memset(usr_sess_key
, 0, 16);
512 vuser
= get_valid_user_struct(key
);
515 DEBUG(10, ("pol_get_usr_sesskey: no vuser struct\n"));
518 memcpy(usr_sess_key
, vuser
->usr
.user_sess_key
, 16);
519 vuid_free_user_struct(vuser
);