s3-rpc_server: Created a per connection spoolss pipe.
[Samba/ekacnet.git] / source3 / rpc_server / srv_lsa_hnd.c
blobf1ab77485905e1d18afbcc5d87c8d5fb9fa20842
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"
23 #include "../librpc/gen_ndr/ndr_lsa.h"
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_RPC_SRV
29 * Handle database - stored per pipe.
32 struct policy {
33 struct policy *next, *prev;
35 struct policy_handle pol_hnd;
37 uint32_t access_granted;
39 void *data_ptr;
42 struct handle_list {
43 struct policy *Policy; /* List of policies. */
44 size_t count; /* Current number of handles. */
45 size_t pipe_ref_count; /* Number of pipe handles referring to this list. */
48 /* This is the max handles across all instances of a pipe name. */
49 #ifndef MAX_OPEN_POLS
50 #define MAX_OPEN_POLS 2048
51 #endif
53 /****************************************************************************
54 Hack as handles need to be persisant over lsa pipe closes so long as a samr
55 pipe is open. JRA.
56 ****************************************************************************/
58 static bool is_samr_lsa_pipe(const struct ndr_syntax_id *syntax)
60 return (ndr_syntax_id_equal(syntax, &ndr_table_samr.syntax_id)
61 || ndr_syntax_id_equal(syntax, &ndr_table_lsarpc.syntax_id));
64 size_t num_pipe_handles(struct handle_list *list)
66 if (list == NULL) {
67 return 0;
69 return list->count;
72 /****************************************************************************
73 Initialise a policy handle list on a pipe. Handle list is shared between all
74 pipes of the same name.
75 ****************************************************************************/
77 bool init_pipe_handle_list(pipes_struct *p, const struct ndr_syntax_id *syntax)
79 pipes_struct *plist;
80 struct handle_list *hl;
82 for (plist = get_first_internal_pipe();
83 plist;
84 plist = get_next_internal_pipe(plist)) {
85 if (ndr_syntax_id_equal(syntax, &plist->syntax)) {
86 break;
88 if (is_samr_lsa_pipe(&plist->syntax)
89 && is_samr_lsa_pipe(syntax)) {
91 * samr and lsa share a handle space (same process
92 * under Windows?)
94 break;
98 if (plist != NULL) {
99 hl = plist->pipe_handles;
100 if (hl == NULL) {
101 return false;
103 } else {
105 * First open, we have to create the handle list
107 hl = SMB_MALLOC_P(struct handle_list);
108 if (hl == NULL) {
109 return false;
111 ZERO_STRUCTP(hl);
113 DEBUG(10,("init_pipe_handle_list: created handle list for "
114 "pipe %s\n",
115 get_pipe_name_from_syntax(talloc_tos(), syntax)));
119 * One more pipe is using this list.
122 hl->pipe_ref_count++;
125 * Point this pipe at this list.
128 p->pipe_handles = hl;
130 DEBUG(10,("init_pipe_handle_list: pipe_handles ref count = %lu for "
131 "pipe %s\n", (unsigned long)p->pipe_handles->pipe_ref_count,
132 get_pipe_name_from_syntax(talloc_tos(), syntax)));
134 return True;
137 /****************************************************************************
138 find first available policy slot. creates a policy handle for you.
140 If "data_ptr" is given, this must be a talloc'ed object, create_policy_hnd
141 talloc_moves this into the handle. If the policy_hnd is closed,
142 data_ptr is TALLOC_FREE()'ed
143 ****************************************************************************/
145 static struct policy *create_policy_hnd_internal(pipes_struct *p,
146 struct policy_handle *hnd,
147 void *data_ptr)
149 static uint32 pol_hnd_low = 0;
150 static uint32 pol_hnd_high = 0;
151 time_t t = time(NULL);
153 struct policy *pol;
155 if (p->pipe_handles->count > MAX_OPEN_POLS) {
156 DEBUG(0,("create_policy_hnd: ERROR: too many handles (%d) on this pipe.\n",
157 (int)p->pipe_handles->count));
158 return NULL;
161 pol = TALLOC_ZERO_P(NULL, struct policy);
162 if (!pol) {
163 DEBUG(0,("create_policy_hnd: ERROR: out of memory!\n"));
164 return NULL;
167 if (data_ptr != NULL) {
168 pol->data_ptr = talloc_move(pol, &data_ptr);
171 pol_hnd_low++;
172 if (pol_hnd_low == 0)
173 (pol_hnd_high)++;
175 SIVAL(&pol->pol_hnd.handle_type, 0 , 0); /* first bit must be null */
176 SIVAL(&pol->pol_hnd.uuid.time_low, 0 , pol_hnd_low ); /* second bit is incrementing */
177 SSVAL(&pol->pol_hnd.uuid.time_mid, 0 , pol_hnd_high); /* second bit is incrementing */
178 SSVAL(&pol->pol_hnd.uuid.time_hi_and_version, 0 , (pol_hnd_high>>16)); /* second bit is incrementing */
180 /* split the current time into two 16 bit values */
182 SSVAL(pol->pol_hnd.uuid.clock_seq, 0, (t>>16)); /* something random */
183 SSVAL(pol->pol_hnd.uuid.node, 0, t); /* something random */
185 SIVAL(pol->pol_hnd.uuid.node, 2, sys_getpid()); /* something more random */
187 DLIST_ADD(p->pipe_handles->Policy, pol);
188 p->pipe_handles->count++;
190 *hnd = pol->pol_hnd;
192 DEBUG(4,("Opened policy hnd[%d] ", (int)p->pipe_handles->count));
193 dump_data(4, (uint8 *)hnd, sizeof(*hnd));
195 return pol;
198 bool create_policy_hnd(pipes_struct *p, struct policy_handle *hnd,
199 void *data_ptr)
201 return create_policy_hnd_internal(p, hnd, data_ptr) != NULL;
204 /****************************************************************************
205 find policy by handle - internal version.
206 ****************************************************************************/
208 static struct policy *find_policy_by_hnd_internal(pipes_struct *p,
209 const struct policy_handle *hnd,
210 void **data_p)
212 struct policy *pol;
213 size_t i;
215 if (data_p)
216 *data_p = NULL;
218 for (i = 0, pol=p->pipe_handles->Policy;pol;pol=pol->next, i++) {
219 if (memcmp(&pol->pol_hnd, hnd, sizeof(*hnd)) == 0) {
220 DEBUG(4,("Found policy hnd[%d] ", (int)i));
221 dump_data(4, (uint8 *)hnd, sizeof(*hnd));
222 if (data_p)
223 *data_p = pol->data_ptr;
224 return pol;
228 DEBUG(4,("Policy not found: "));
229 dump_data(4, (uint8 *)hnd, sizeof(*hnd));
231 p->bad_handle_fault_state = True;
233 return NULL;
236 /****************************************************************************
237 find policy by handle
238 ****************************************************************************/
240 bool find_policy_by_hnd(pipes_struct *p, const struct policy_handle *hnd,
241 void **data_p)
243 return find_policy_by_hnd_internal(p, hnd, data_p) == NULL ? False : True;
246 /****************************************************************************
247 Close a policy.
248 ****************************************************************************/
250 bool close_policy_hnd(pipes_struct *p, struct policy_handle *hnd)
252 struct policy *pol = find_policy_by_hnd_internal(p, hnd, NULL);
254 if (!pol) {
255 DEBUG(3,("Error closing policy\n"));
256 return False;
259 DEBUG(3,("Closed policy\n"));
261 p->pipe_handles->count--;
263 DLIST_REMOVE(p->pipe_handles->Policy, pol);
265 TALLOC_FREE(pol);
267 return True;
270 /****************************************************************************
271 Close a pipe - free the handle list if it was the last pipe reference.
272 ****************************************************************************/
274 void close_policy_by_pipe(pipes_struct *p)
276 p->pipe_handles->pipe_ref_count--;
278 if (p->pipe_handles->pipe_ref_count == 0) {
280 * Last pipe open on this list - free the list.
282 while (p->pipe_handles->Policy)
283 close_policy_hnd(p, &p->pipe_handles->Policy->pol_hnd);
285 p->pipe_handles->Policy = NULL;
286 p->pipe_handles->count = 0;
288 SAFE_FREE(p->pipe_handles);
289 DEBUG(10,("close_policy_by_pipe: deleted handle list for "
290 "pipe %s\n",
291 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
295 /*******************************************************************
296 Shall we allow access to this rpc? Currently this function
297 implements the 'restrict anonymous' setting by denying access to
298 anonymous users if the restrict anonymous level is > 0. Further work
299 will be checking a security descriptor to determine whether a user
300 token has enough access to access the pipe.
301 ********************************************************************/
303 bool pipe_access_check(pipes_struct *p)
305 /* Don't let anonymous users access this RPC if restrict
306 anonymous > 0 */
308 if (lp_restrict_anonymous() > 0) {
310 /* schannel, so we must be ok */
311 if (p->pipe_bound && (p->auth.auth_type == PIPE_AUTH_TYPE_SCHANNEL)) {
312 return True;
315 if (p->server_info->guest) {
316 return False;
320 return True;
323 void *_policy_handle_create(struct pipes_struct *p, struct policy_handle *hnd,
324 uint32_t access_granted, size_t data_size,
325 const char *type, NTSTATUS *pstatus)
327 struct policy *pol;
328 void *data;
330 if (p->pipe_handles->count > MAX_OPEN_POLS) {
331 DEBUG(0, ("policy_handle_create: ERROR: too many handles (%d) "
332 "on pipe %s.\n", (int)p->pipe_handles->count,
333 get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
334 *pstatus = NT_STATUS_INSUFFICIENT_RESOURCES;
335 return NULL;
338 data = talloc_size(talloc_tos(), data_size);
339 if (data == NULL) {
340 *pstatus = NT_STATUS_NO_MEMORY;
341 return NULL;
343 talloc_set_name_const(data, type);
345 pol = create_policy_hnd_internal(p, hnd, data);
346 if (pol == NULL) {
347 TALLOC_FREE(data);
348 *pstatus = NT_STATUS_NO_MEMORY;
349 return NULL;
351 pol->access_granted = access_granted;
352 *pstatus = NT_STATUS_OK;
353 return data;
356 void *_policy_handle_find(struct pipes_struct *p,
357 const struct policy_handle *hnd,
358 uint32_t access_required,
359 uint32_t *paccess_granted,
360 const char *name, const char *location,
361 NTSTATUS *pstatus)
363 struct policy *pol;
364 void *data;
366 pol = find_policy_by_hnd_internal(p, hnd, &data);
367 if (pol == NULL) {
368 *pstatus = NT_STATUS_INVALID_HANDLE;
369 return NULL;
371 if (strcmp(name, talloc_get_name(data)) != 0) {
372 DEBUG(10, ("expected %s, got %s\n", name,
373 talloc_get_name(data)));
374 *pstatus = NT_STATUS_INVALID_HANDLE;
375 return NULL;
377 if ((access_required & pol->access_granted) != access_required) {
378 if (geteuid() == sec_initial_uid()) {
379 DEBUG(4, ("%s: ACCESS should be DENIED (granted: "
380 "%#010x; required: %#010x)\n", location,
381 pol->access_granted, access_required));
382 DEBUGADD(4,("but overwritten by euid == 0\n"));
383 goto okay;
385 DEBUG(2,("%s: ACCESS DENIED (granted: %#010x; required: "
386 "%#010x)\n", location, pol->access_granted,
387 access_required));
388 *pstatus = NT_STATUS_ACCESS_DENIED;
389 return NULL;
392 okay:
393 DEBUG(10, ("found handle of type %s\n", talloc_get_name(data)));
394 if (paccess_granted != NULL) {
395 *paccess_granted = pol->access_granted;
397 *pstatus = NT_STATUS_OK;
398 return data;