s3-utils/net_rpc_printer.c: print more info on write error
[Samba/gebeck_regimport.git] / source3 / rpc_server / rpc_handles.c
blobc40029bd0889f986adf63144773e8fc9c7cf142d
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"
24 #include "../librpc/gen_ndr/ndr_samr.h"
25 #include "auth.h"
26 #include "rpc_server/rpc_pipes.h"
27 #include "../libcli/security/security.h"
28 #include "lib/tsocket/tsocket.h"
30 #undef DBGC_CLASS
31 #define DBGC_CLASS DBGC_RPC_SRV
33 static struct pipes_struct *InternalPipes;
35 /* TODO
36 * the following prototypes are declared here to avoid
37 * code being moved about too much for a patch to be
38 * disrupted / less obvious.
40 * these functions, and associated functions that they
41 * call, should be moved behind a .so module-loading
42 * system _anyway_. so that's the next step...
45 int make_base_pipes_struct(TALLOC_CTX *mem_ctx,
46 struct messaging_context *msg_ctx,
47 const char *pipe_name,
48 enum dcerpc_transport_t transport,
49 bool endian, bool ncalrpc_as_system,
50 const struct tsocket_address *remote_address,
51 const struct tsocket_address *local_address,
52 struct pipes_struct **_p)
54 struct pipes_struct *p;
56 p = talloc_zero(mem_ctx, struct pipes_struct);
57 if (!p) {
58 return ENOMEM;
61 p->mem_ctx = talloc_named(p, 0, "pipe %s %p", pipe_name, p);
62 if (!p->mem_ctx) {
63 talloc_free(p);
64 return ENOMEM;
67 p->msg_ctx = msg_ctx;
68 p->transport = transport;
69 p->endian = endian;
70 p->ncalrpc_as_system = ncalrpc_as_system;
72 p->remote_address = tsocket_address_copy(remote_address, p);
73 if (p->remote_address == NULL) {
74 talloc_free(p);
75 return ENOMEM;
78 if (local_address) {
79 p->local_address = tsocket_address_copy(remote_address, p);
80 if (p->local_address == NULL) {
81 talloc_free(p);
82 return ENOMEM;
86 DLIST_ADD(InternalPipes, p);
87 talloc_set_destructor(p, close_internal_rpc_pipe_hnd);
89 *_p = p;
90 return 0;
94 bool check_open_pipes(void)
96 struct pipes_struct *p;
98 for (p = InternalPipes; p != NULL; p = p->next) {
99 if (num_pipe_handles(p) != 0) {
100 return true;
103 return false;
106 /****************************************************************************
107 Close an rpc pipe.
108 ****************************************************************************/
110 static void free_pipe_rpc_context_internal(struct pipe_rpc_fns *list)
112 struct pipe_rpc_fns *tmp = list;
113 struct pipe_rpc_fns *tmp2;
115 while (tmp) {
116 tmp2 = tmp->next;
117 SAFE_FREE(tmp);
118 tmp = tmp2;
121 return;
124 int close_internal_rpc_pipe_hnd(struct pipes_struct *p)
126 if (!p) {
127 DEBUG(0,("Invalid pipe in close_internal_rpc_pipe_hnd\n"));
128 return False;
131 TALLOC_FREE(p->auth.auth_ctx);
133 /* Free the handles database. */
134 close_policy_by_pipe(p);
136 free_pipe_rpc_context_internal( p->contexts );
138 DLIST_REMOVE(InternalPipes, p);
140 ZERO_STRUCTP(p);
142 return 0;
146 * Handle database - stored per pipe.
149 struct dcesrv_handle {
150 struct dcesrv_handle *prev, *next;
151 struct policy_handle wire_handle;
152 uint32_t access_granted;
153 void *data;
156 struct handle_list {
157 struct dcesrv_handle *handles; /* List of pipe handles. */
158 size_t count; /* Current number of handles. */
159 size_t pipe_ref_count; /* Number of pipe handles referring
160 * to this tree. */
163 /* This is the max handles across all instances of a pipe name. */
164 #ifndef MAX_OPEN_POLS
165 #define MAX_OPEN_POLS 2048
166 #endif
168 /****************************************************************************
169 Hack as handles need to be persisant over lsa pipe closes so long as a samr
170 pipe is open. JRA.
171 ****************************************************************************/
173 static bool is_samr_lsa_pipe(const struct ndr_syntax_id *syntax)
175 return (ndr_syntax_id_equal(syntax, &ndr_table_samr.syntax_id)
176 || ndr_syntax_id_equal(syntax, &ndr_table_lsarpc.syntax_id));
179 size_t num_pipe_handles(struct pipes_struct *p)
181 if (p->pipe_handles == NULL) {
182 return 0;
184 return p->pipe_handles->count;
187 /****************************************************************************
188 Initialise a policy handle list on a pipe. Handle list is shared between all
189 pipes of the same name.
190 ****************************************************************************/
192 bool init_pipe_handles(struct pipes_struct *p, const struct ndr_syntax_id *syntax)
194 struct pipes_struct *plist;
195 struct handle_list *hl;
197 for (plist = InternalPipes; plist; plist = plist->next) {
198 struct pipe_rpc_fns *p_ctx;
199 bool stop = false;
201 for (p_ctx = plist->contexts;
202 p_ctx != NULL;
203 p_ctx = p_ctx->next) {
204 if (ndr_syntax_id_equal(syntax, &p_ctx->syntax)) {
205 stop = true;
206 break;
208 if (is_samr_lsa_pipe(&p_ctx->syntax)
209 && is_samr_lsa_pipe(syntax)) {
211 * samr and lsa share a handle space (same process
212 * under Windows?)
214 stop = true;
215 break;
219 if (stop) {
220 break;
224 if (plist != NULL) {
225 hl = plist->pipe_handles;
226 if (hl == NULL) {
227 return false;
229 } else {
231 * First open, we have to create the handle list
233 hl = talloc_zero(NULL, struct handle_list);
234 if (hl == NULL) {
235 return false;
238 DEBUG(10,("init_pipe_handle_list: created handle list for "
239 "pipe %s\n",
240 get_pipe_name_from_syntax(talloc_tos(), syntax)));
244 * One more pipe is using this list.
247 hl->pipe_ref_count++;
250 * Point this pipe at this list.
253 p->pipe_handles = hl;
255 DEBUG(10,("init_pipe_handle_list: pipe_handles ref count = %lu for "
256 "pipe %s\n", (unsigned long)p->pipe_handles->pipe_ref_count,
257 get_pipe_name_from_syntax(talloc_tos(), syntax)));
259 return True;
262 /****************************************************************************
263 find first available policy slot. creates a policy handle for you.
265 If "data_ptr" is given, this must be a talloc'ed object, create_policy_hnd
266 talloc_moves this into the handle. If the policy_hnd is closed,
267 data_ptr is TALLOC_FREE()'ed
268 ****************************************************************************/
270 static struct dcesrv_handle *create_rpc_handle_internal(struct pipes_struct *p,
271 struct policy_handle *hnd, void *data_ptr)
273 struct dcesrv_handle *rpc_hnd;
274 static uint32 pol_hnd_low = 0;
275 static uint32 pol_hnd_high = 0;
276 time_t t = time(NULL);
278 if (p->pipe_handles->count > MAX_OPEN_POLS) {
279 DEBUG(0,("create_policy_hnd: ERROR: too many handles (%d) on this pipe.\n",
280 (int)p->pipe_handles->count));
281 return NULL;
284 rpc_hnd = talloc_zero(p->pipe_handles, struct dcesrv_handle);
285 if (!rpc_hnd) {
286 DEBUG(0,("create_policy_hnd: ERROR: out of memory!\n"));
287 return NULL;
290 if (data_ptr != NULL) {
291 rpc_hnd->data = talloc_move(rpc_hnd, &data_ptr);
294 pol_hnd_low++;
295 if (pol_hnd_low == 0) {
296 pol_hnd_high++;
299 /* first bit must be null */
300 SIVAL(&rpc_hnd->wire_handle.handle_type, 0 , 0);
302 /* second bit is incrementing */
303 SIVAL(&rpc_hnd->wire_handle.uuid.time_low, 0 , pol_hnd_low);
304 SSVAL(&rpc_hnd->wire_handle.uuid.time_mid, 0 , pol_hnd_high);
305 SSVAL(&rpc_hnd->wire_handle.uuid.time_hi_and_version, 0, (pol_hnd_high >> 16));
307 /* split the current time into two 16 bit values */
309 /* something random */
310 SSVAL(rpc_hnd->wire_handle.uuid.clock_seq, 0, (t >> 16));
311 /* something random */
312 SSVAL(rpc_hnd->wire_handle.uuid.node, 0, t);
313 /* something more random */
314 SIVAL(rpc_hnd->wire_handle.uuid.node, 2, sys_getpid());
316 DLIST_ADD(p->pipe_handles->handles, rpc_hnd);
317 p->pipe_handles->count++;
319 *hnd = rpc_hnd->wire_handle;
321 DEBUG(4, ("Opened policy hnd[%d] ", (int)p->pipe_handles->count));
322 dump_data(4, (uint8_t *)hnd, sizeof(*hnd));
324 return rpc_hnd;
327 bool create_policy_hnd(struct pipes_struct *p, struct policy_handle *hnd,
328 void *data_ptr)
330 struct dcesrv_handle *rpc_hnd;
332 rpc_hnd = create_rpc_handle_internal(p, hnd, data_ptr);
333 if (rpc_hnd == NULL) {
334 return false;
336 return true;
339 /****************************************************************************
340 find policy by handle - internal version.
341 ****************************************************************************/
343 static struct dcesrv_handle *find_policy_by_hnd_internal(struct pipes_struct *p,
344 const struct policy_handle *hnd, void **data_p)
346 struct dcesrv_handle *h;
347 unsigned int count;
349 if (data_p) {
350 *data_p = NULL;
353 count = 0;
354 for (h = p->pipe_handles->handles; h != NULL; h = h->next) {
355 if (memcmp(&h->wire_handle, hnd, sizeof(*hnd)) == 0) {
356 DEBUG(4,("Found policy hnd[%u] ", count));
357 dump_data(4, (const uint8 *)hnd, sizeof(*hnd));
358 if (data_p) {
359 *data_p = h->data;
361 return h;
363 count++;
366 DEBUG(4,("Policy not found: "));
367 dump_data(4, (const uint8_t *)hnd, sizeof(*hnd));
369 p->bad_handle_fault_state = true;
371 return NULL;
374 /****************************************************************************
375 find policy by handle
376 ****************************************************************************/
378 bool find_policy_by_hnd(struct pipes_struct *p, const struct policy_handle *hnd,
379 void **data_p)
381 struct dcesrv_handle *rpc_hnd;
383 rpc_hnd = find_policy_by_hnd_internal(p, hnd, data_p);
384 if (rpc_hnd == NULL) {
385 return false;
387 return true;
390 /****************************************************************************
391 Close a policy.
392 ****************************************************************************/
394 bool close_policy_hnd(struct pipes_struct *p, struct policy_handle *hnd)
396 struct dcesrv_handle *rpc_hnd;
398 rpc_hnd = find_policy_by_hnd_internal(p, hnd, NULL);
400 if (rpc_hnd == NULL) {
401 DEBUG(3, ("Error closing policy (policy not found)\n"));
402 return false;
405 DEBUG(3,("Closed policy\n"));
407 p->pipe_handles->count--;
409 DLIST_REMOVE(p->pipe_handles->handles, rpc_hnd);
410 TALLOC_FREE(rpc_hnd);
412 return true;
415 /****************************************************************************
416 Close a pipe - free the handle set if it was the last pipe reference.
417 ****************************************************************************/
419 void close_policy_by_pipe(struct pipes_struct *p)
421 if (p->pipe_handles == NULL) {
422 return;
425 p->pipe_handles->pipe_ref_count--;
427 if (p->pipe_handles->pipe_ref_count == 0) {
429 * Last pipe open on this list - free the list.
431 TALLOC_FREE(p->pipe_handles);
433 DEBUG(10,("Deleted handle list for RPC connection %s\n",
434 get_pipe_name_from_syntax(talloc_tos(),
435 &p->contexts->syntax)));
439 /*******************************************************************
440 Shall we allow access to this rpc? Currently this function
441 implements the 'restrict anonymous' setting by denying access to
442 anonymous users if the restrict anonymous level is > 0. Further work
443 will be checking a security descriptor to determine whether a user
444 token has enough access to access the pipe.
445 ********************************************************************/
447 bool pipe_access_check(struct pipes_struct *p)
449 /* Don't let anonymous users access this RPC if restrict
450 anonymous > 0 */
452 if (lp_restrict_anonymous() > 0) {
454 /* schannel, so we must be ok */
455 if (p->pipe_bound &&
456 (p->auth.auth_type == DCERPC_AUTH_TYPE_SCHANNEL)) {
457 return True;
460 if (security_session_user_level(p->session_info, NULL) < SECURITY_USER) {
461 return False;
465 return True;
468 void *_policy_handle_create(struct pipes_struct *p, struct policy_handle *hnd,
469 uint32_t access_granted, size_t data_size,
470 const char *type, NTSTATUS *pstatus)
472 struct dcesrv_handle *rpc_hnd;
473 void *data;
475 if (p->pipe_handles->count > MAX_OPEN_POLS) {
476 DEBUG(0, ("ERROR: Too many handles (%d) for RPC connection %s\n",
477 (int) p->pipe_handles->count,
478 get_pipe_name_from_syntax(talloc_tos(),
479 &p->contexts->syntax)));
480 *pstatus = NT_STATUS_INSUFFICIENT_RESOURCES;
481 return NULL;
484 data = talloc_size(talloc_tos(), data_size);
485 if (data == NULL) {
486 *pstatus = NT_STATUS_NO_MEMORY;
487 return NULL;
489 talloc_set_name_const(data, type);
491 rpc_hnd = create_rpc_handle_internal(p, hnd, data);
492 if (rpc_hnd == NULL) {
493 TALLOC_FREE(data);
494 *pstatus = NT_STATUS_NO_MEMORY;
495 return NULL;
497 rpc_hnd->access_granted = access_granted;
498 *pstatus = NT_STATUS_OK;
499 return data;
502 void *_policy_handle_find(struct pipes_struct *p,
503 const struct policy_handle *hnd,
504 uint32_t access_required,
505 uint32_t *paccess_granted,
506 const char *name, const char *location,
507 NTSTATUS *pstatus)
509 struct dcesrv_handle *rpc_hnd;
510 void *data;
512 rpc_hnd = find_policy_by_hnd_internal(p, hnd, &data);
513 if (rpc_hnd == NULL) {
514 *pstatus = NT_STATUS_INVALID_HANDLE;
515 return NULL;
517 if (strcmp(name, talloc_get_name(data)) != 0) {
518 DEBUG(10, ("expected %s, got %s\n", name,
519 talloc_get_name(data)));
520 *pstatus = NT_STATUS_INVALID_HANDLE;
521 return NULL;
523 if ((access_required & rpc_hnd->access_granted) != access_required) {
524 if (geteuid() == sec_initial_uid()) {
525 DEBUG(4, ("%s: ACCESS should be DENIED (granted: "
526 "%#010x; required: %#010x)\n", location,
527 rpc_hnd->access_granted, access_required));
528 DEBUGADD(4,("but overwritten by euid == 0\n"));
529 goto okay;
531 DEBUG(2,("%s: ACCESS DENIED (granted: %#010x; required: "
532 "%#010x)\n", location, rpc_hnd->access_granted,
533 access_required));
534 *pstatus = NT_STATUS_ACCESS_DENIED;
535 return NULL;
538 okay:
539 DEBUG(10, ("found handle of type %s\n", talloc_get_name(data)));
540 if (paccess_granted != NULL) {
541 *paccess_granted = rpc_hnd->access_granted;
543 *pstatus = NT_STATUS_OK;
544 return data;