format-subunit: Hide reason if it is None.
[Samba/eduardoll.git] / source3 / rpc_server / srv_lsa_hnd.c
blob4348fb8d4a7b115b15af60ec40a919120c90b887
1 /*
2 * Unix SMB/CIFS implementation.
3 * RPC Pipe client / server routines
4 * Copyright (C) Andrew Tridgell 1992-1997,
5 * Copyright (C) Luke Kenneth Casson Leighton 1996-1997,
6 * Copyright (C) Jeremy Allison 2001.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
24 #undef DBGC_CLASS
25 #define DBGC_CLASS DBGC_RPC_SRV
28 * Handle database - stored per pipe.
31 struct policy {
32 struct policy *next, *prev;
34 struct policy_handle pol_hnd;
36 uint32_t access_granted;
38 void *data_ptr;
41 struct handle_list {
42 struct policy *Policy; /* List of policies. */
43 size_t count; /* Current number of handles. */
44 size_t pipe_ref_count; /* Number of pipe handles referring to this list. */
47 /* This is the max handles across all instances of a pipe name. */
48 #ifndef MAX_OPEN_POLS
49 #define MAX_OPEN_POLS 2048
50 #endif
52 /****************************************************************************
53 Hack as handles need to be persisant over lsa pipe closes so long as a samr
54 pipe is open. JRA.
55 ****************************************************************************/
57 static bool is_samr_lsa_pipe(const struct ndr_syntax_id *syntax)
59 return (ndr_syntax_id_equal(syntax, &ndr_table_samr.syntax_id)
60 || ndr_syntax_id_equal(syntax, &ndr_table_lsarpc.syntax_id));
63 size_t num_pipe_handles(struct handle_list *list)
65 if (list == NULL) {
66 return 0;
68 return list->count;
71 /****************************************************************************
72 Initialise a policy handle list on a pipe. Handle list is shared between all
73 pipes of the same name.
74 ****************************************************************************/
76 bool init_pipe_handle_list(pipes_struct *p, const struct ndr_syntax_id *syntax)
78 pipes_struct *plist;
79 struct handle_list *hl;
81 for (plist = get_first_internal_pipe();
82 plist;
83 plist = get_next_internal_pipe(plist)) {
84 if (ndr_syntax_id_equal(syntax, &plist->syntax)) {
85 break;
87 if (is_samr_lsa_pipe(&plist->syntax)
88 && is_samr_lsa_pipe(syntax)) {
90 * samr and lsa share a handle space (same process
91 * under Windows?)
93 break;
97 if (plist != NULL) {
98 hl = plist->pipe_handles;
99 if (hl == NULL) {
100 return false;
102 } else {
104 * First open, we have to create the handle list
106 hl = SMB_MALLOC_P(struct handle_list);
107 if (hl == NULL) {
108 return false;
110 ZERO_STRUCTP(hl);
112 DEBUG(10,("init_pipe_handle_list: created handle list for "
113 "pipe %s\n",
114 get_pipe_name_from_syntax(talloc_tos(), syntax)));
118 * One more pipe is using this list.
121 hl->pipe_ref_count++;
124 * Point this pipe at this list.
127 p->pipe_handles = hl;
129 DEBUG(10,("init_pipe_handle_list: pipe_handles ref count = %lu for "
130 "pipe %s\n", (unsigned long)p->pipe_handles->pipe_ref_count,
131 get_pipe_name_from_syntax(talloc_tos(), syntax)));
133 return True;
136 /****************************************************************************
137 find first available policy slot. creates a policy handle for you.
139 If "data_ptr" is given, this must be a talloc'ed object, create_policy_hnd
140 talloc_moves this into the handle. If the policy_hnd is closed,
141 data_ptr is TALLOC_FREE()'ed
142 ****************************************************************************/
144 static struct policy *create_policy_hnd_internal(pipes_struct *p,
145 struct policy_handle *hnd,
146 void *data_ptr)
148 static uint32 pol_hnd_low = 0;
149 static uint32 pol_hnd_high = 0;
150 time_t t = time(NULL);
152 struct policy *pol;
154 if (p->pipe_handles->count > MAX_OPEN_POLS) {
155 DEBUG(0,("create_policy_hnd: ERROR: too many handles (%d) on this pipe.\n",
156 (int)p->pipe_handles->count));
157 return NULL;
160 pol = TALLOC_ZERO_P(NULL, struct policy);
161 if (!pol) {
162 DEBUG(0,("create_policy_hnd: ERROR: out of memory!\n"));
163 return NULL;
166 if (data_ptr != NULL) {
167 pol->data_ptr = talloc_move(pol, &data_ptr);
170 pol_hnd_low++;
171 if (pol_hnd_low == 0)
172 (pol_hnd_high)++;
174 SIVAL(&pol->pol_hnd.handle_type, 0 , 0); /* first bit must be null */
175 SIVAL(&pol->pol_hnd.uuid.time_low, 0 , pol_hnd_low ); /* second bit is incrementing */
176 SSVAL(&pol->pol_hnd.uuid.time_mid, 0 , pol_hnd_high); /* second bit is incrementing */
177 SSVAL(&pol->pol_hnd.uuid.time_hi_and_version, 0 , (pol_hnd_high>>16)); /* second bit is incrementing */
179 /* split the current time into two 16 bit values */
181 SSVAL(pol->pol_hnd.uuid.clock_seq, 0, (t>>16)); /* something random */
182 SSVAL(pol->pol_hnd.uuid.node, 0, t); /* something random */
184 SIVAL(pol->pol_hnd.uuid.node, 2, sys_getpid()); /* something more random */
186 DLIST_ADD(p->pipe_handles->Policy, pol);
187 p->pipe_handles->count++;
189 *hnd = pol->pol_hnd;
191 DEBUG(4,("Opened policy hnd[%d] ", (int)p->pipe_handles->count));
192 dump_data(4, (uint8 *)hnd, sizeof(*hnd));
194 return pol;
197 bool create_policy_hnd(pipes_struct *p, struct policy_handle *hnd,
198 void *data_ptr)
200 return create_policy_hnd_internal(p, hnd, data_ptr) != NULL;
203 /****************************************************************************
204 find policy by handle - internal version.
205 ****************************************************************************/
207 static struct policy *find_policy_by_hnd_internal(pipes_struct *p,
208 const struct policy_handle *hnd,
209 void **data_p)
211 struct policy *pol;
212 size_t i;
214 if (data_p)
215 *data_p = NULL;
217 for (i = 0, pol=p->pipe_handles->Policy;pol;pol=pol->next, i++) {
218 if (memcmp(&pol->pol_hnd, hnd, sizeof(*hnd)) == 0) {
219 DEBUG(4,("Found policy hnd[%d] ", (int)i));
220 dump_data(4, (uint8 *)hnd, sizeof(*hnd));
221 if (data_p)
222 *data_p = pol->data_ptr;
223 return pol;
227 DEBUG(4,("Policy not found: "));
228 dump_data(4, (uint8 *)hnd, sizeof(*hnd));
230 p->bad_handle_fault_state = True;
232 return NULL;
235 /****************************************************************************
236 find policy by handle
237 ****************************************************************************/
239 bool find_policy_by_hnd(pipes_struct *p, const struct policy_handle *hnd,
240 void **data_p)
242 return find_policy_by_hnd_internal(p, hnd, data_p) == NULL ? False : True;
245 /****************************************************************************
246 Close a policy.
247 ****************************************************************************/
249 bool close_policy_hnd(pipes_struct *p, struct policy_handle *hnd)
251 struct policy *pol = find_policy_by_hnd_internal(p, hnd, NULL);
253 if (!pol) {
254 DEBUG(3,("Error closing policy\n"));
255 return False;
258 DEBUG(3,("Closed policy\n"));
260 p->pipe_handles->count--;
262 DLIST_REMOVE(p->pipe_handles->Policy, pol);
264 TALLOC_FREE(pol);
266 return True;
269 /****************************************************************************
270 Close a pipe - free the handle list if it was the last pipe reference.
271 ****************************************************************************/
273 void close_policy_by_pipe(pipes_struct *p)
275 p->pipe_handles->pipe_ref_count--;
277 if (p->pipe_handles->pipe_ref_count == 0) {
279 * Last pipe open on this list - free the list.
281 while (p->pipe_handles->Policy)
282 close_policy_hnd(p, &p->pipe_handles->Policy->pol_hnd);
284 p->pipe_handles->Policy = NULL;
285 p->pipe_handles->count = 0;
287 SAFE_FREE(p->pipe_handles);
288 DEBUG(10,("close_policy_by_pipe: deleted handle list for "
289 "pipe %s\n",
290 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
294 /*******************************************************************
295 Shall we allow access to this rpc? Currently this function
296 implements the 'restrict anonymous' setting by denying access to
297 anonymous users if the restrict anonymous level is > 0. Further work
298 will be checking a security descriptor to determine whether a user
299 token has enough access to access the pipe.
300 ********************************************************************/
302 bool pipe_access_check(pipes_struct *p)
304 /* Don't let anonymous users access this RPC if restrict
305 anonymous > 0 */
307 if (lp_restrict_anonymous() > 0) {
309 /* schannel, so we must be ok */
310 if (p->pipe_bound && (p->auth.auth_type == PIPE_AUTH_TYPE_SCHANNEL)) {
311 return True;
314 if (p->server_info->guest) {
315 return False;
319 return True;
322 void *_policy_handle_create(struct pipes_struct *p, struct policy_handle *hnd,
323 uint32_t access_granted, size_t data_size,
324 const char *type, NTSTATUS *pstatus)
326 struct policy *pol;
327 void *data;
329 if (p->pipe_handles->count > MAX_OPEN_POLS) {
330 DEBUG(0, ("policy_handle_create: ERROR: too many handles (%d) "
331 "on pipe %s.\n", (int)p->pipe_handles->count,
332 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
333 *pstatus = NT_STATUS_INSUFFICIENT_RESOURCES;
334 return NULL;
337 data = talloc_size(talloc_tos(), data_size);
338 if (data == NULL) {
339 *pstatus = NT_STATUS_NO_MEMORY;
340 return NULL;
342 talloc_set_name_const(data, type);
344 pol = create_policy_hnd_internal(p, hnd, data);
345 if (pol == NULL) {
346 TALLOC_FREE(data);
347 *pstatus = NT_STATUS_NO_MEMORY;
348 return NULL;
350 pol->access_granted = access_granted;
351 *pstatus = NT_STATUS_OK;
352 return data;
355 void *_policy_handle_find(struct pipes_struct *p,
356 const struct policy_handle *hnd,
357 uint32_t access_required,
358 uint32_t *paccess_granted,
359 const char *name, const char *location,
360 NTSTATUS *pstatus)
362 struct policy *pol;
363 void *data;
365 pol = find_policy_by_hnd_internal(p, hnd, &data);
366 if (pol == NULL) {
367 *pstatus = NT_STATUS_INVALID_HANDLE;
368 return NULL;
370 if (strcmp(name, talloc_get_name(data)) != 0) {
371 DEBUG(10, ("expected %s, got %s\n", name,
372 talloc_get_name(data)));
373 *pstatus = NT_STATUS_INVALID_HANDLE;
374 return NULL;
376 if ((access_required & pol->access_granted) != access_required) {
377 if (geteuid() == sec_initial_uid()) {
378 DEBUG(4, ("%s: ACCESS should be DENIED (granted: "
379 "%#010x; required: %#010x)\n", location,
380 pol->access_granted, access_required));
381 DEBUGADD(4,("but overwritten by euid == 0\n"));
382 goto okay;
384 DEBUG(2,("%s: ACCESS DENIED (granted: %#010x; required: "
385 "%#010x)\n", location, pol->access_granted,
386 access_required));
387 *pstatus = NT_STATUS_ACCESS_DENIED;
388 return NULL;
391 okay:
392 DEBUG(10, ("found handle of type %s\n", talloc_get_name(data)));
393 if (paccess_granted != NULL) {
394 *paccess_granted = pol->access_granted;
396 *pstatus = NT_STATUS_OK;
397 return data;