s3: RPC: Don't crash on trying to talloc_free(-1) if smb_iconv_open_ex() fails.
[Samba.git] / source3 / rpc_server / rpc_handles.c
blob08ba7da36cb538bc79c4574b2dae5fe197b55a57
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 "system/passwd.h" /* uid_wrapper */
24 #include "../librpc/gen_ndr/ndr_lsa.h"
25 #include "../librpc/gen_ndr/ndr_samr.h"
26 #include "auth.h"
27 #include "rpc_server/rpc_pipes.h"
28 #include "../libcli/security/security.h"
29 #include "lib/tsocket/tsocket.h"
30 #include "librpc/ndr/ndr_table.h"
32 #undef DBGC_CLASS
33 #define DBGC_CLASS DBGC_RPC_SRV
35 static struct pipes_struct *InternalPipes;
37 /* TODO
38 * the following prototypes are declared here to avoid
39 * code being moved about too much for a patch to be
40 * disrupted / less obvious.
42 * these functions, and associated functions that they
43 * call, should be moved behind a .so module-loading
44 * system _anyway_. so that's the next step...
47 int make_base_pipes_struct(TALLOC_CTX *mem_ctx,
48 struct messaging_context *msg_ctx,
49 const char *pipe_name,
50 enum dcerpc_transport_t transport,
51 bool endian,
52 const struct tsocket_address *remote_address,
53 const struct tsocket_address *local_address,
54 struct pipes_struct **_p)
56 struct pipes_struct *p;
58 p = talloc_zero(mem_ctx, struct pipes_struct);
59 if (!p) {
60 return ENOMEM;
63 p->mem_ctx = talloc_named(p, 0, "pipe %s %p", pipe_name, p);
64 if (!p->mem_ctx) {
65 talloc_free(p);
66 return ENOMEM;
69 p->msg_ctx = msg_ctx;
70 p->transport = transport;
71 p->endian = endian;
72 p->allow_bind = true;
74 p->remote_address = tsocket_address_copy(remote_address, p);
75 if (p->remote_address == NULL) {
76 talloc_free(p);
77 return ENOMEM;
80 if (local_address) {
81 p->local_address = tsocket_address_copy(local_address, p);
82 if (p->local_address == NULL) {
83 talloc_free(p);
84 return ENOMEM;
88 DLIST_ADD(InternalPipes, p);
89 talloc_set_destructor(p, close_internal_rpc_pipe_hnd);
91 *_p = p;
92 return 0;
96 bool check_open_pipes(void)
98 struct pipes_struct *p;
100 for (p = InternalPipes; p != NULL; p = p->next) {
101 if (num_pipe_handles(p) != 0) {
102 return true;
105 return false;
108 /****************************************************************************
109 Close an rpc pipe.
110 ****************************************************************************/
112 int close_internal_rpc_pipe_hnd(struct pipes_struct *p)
114 if (!p) {
115 DEBUG(0,("Invalid pipe in close_internal_rpc_pipe_hnd\n"));
116 return False;
119 /* Free the handles database. */
120 close_policy_by_pipe(p);
122 DLIST_REMOVE(InternalPipes, p);
124 return 0;
128 * Handle database - stored per pipe.
131 struct dcesrv_handle_old {
132 struct dcesrv_handle_old *prev, *next;
133 struct policy_handle wire_handle;
134 uint32_t access_granted;
135 void *data;
138 struct handle_list {
139 struct dcesrv_handle_old *handles; /* List of pipe handles. */
140 size_t count; /* Current number of handles. */
141 size_t pipe_ref_count; /* Number of pipe handles referring
142 * to this tree. */
145 /* This is the max handles across all instances of a pipe name. */
146 #ifndef MAX_OPEN_POLS
147 #define MAX_OPEN_POLS 2048
148 #endif
150 /****************************************************************************
151 Hack as handles need to be persistent over lsa pipe closes so long as a samr
152 pipe is open. JRA.
153 ****************************************************************************/
155 static bool is_samr_lsa_pipe(const struct ndr_syntax_id *syntax)
157 return (ndr_syntax_id_equal(syntax, &ndr_table_samr.syntax_id)
158 || ndr_syntax_id_equal(syntax, &ndr_table_lsarpc.syntax_id));
161 size_t num_pipe_handles(struct pipes_struct *p)
163 if (p->pipe_handles == NULL) {
164 return 0;
166 return p->pipe_handles->count;
169 /****************************************************************************
170 Initialise a policy handle list on a pipe. Handle list is shared between all
171 pipes of the same name.
172 ****************************************************************************/
174 bool init_pipe_handles(struct pipes_struct *p, const struct ndr_syntax_id *syntax)
176 struct pipes_struct *plist;
177 struct handle_list *hl;
179 for (plist = InternalPipes; plist; plist = plist->next) {
180 struct pipe_rpc_fns *p_ctx;
181 bool stop = false;
183 for (p_ctx = plist->contexts;
184 p_ctx != NULL;
185 p_ctx = p_ctx->next) {
186 if (ndr_syntax_id_equal(syntax, &p_ctx->syntax)) {
187 stop = true;
188 break;
190 if (is_samr_lsa_pipe(&p_ctx->syntax)
191 && is_samr_lsa_pipe(syntax)) {
193 * samr and lsa share a handle space (same process
194 * under Windows?)
196 stop = true;
197 break;
201 if (stop) {
202 break;
206 if (plist != NULL) {
207 hl = plist->pipe_handles;
208 if (hl == NULL) {
209 return false;
211 } else {
213 * First open, we have to create the handle list
215 hl = talloc_zero(NULL, struct handle_list);
216 if (hl == NULL) {
217 return false;
220 DEBUG(10,("init_pipe_handle_list: created handle list for "
221 "pipe %s\n",
222 ndr_interface_name(&syntax->uuid,
223 syntax->if_version)));
227 * One more pipe is using this list.
230 hl->pipe_ref_count++;
233 * Point this pipe at this list.
236 p->pipe_handles = hl;
238 DEBUG(10,("init_pipe_handle_list: pipe_handles ref count = %lu for "
239 "pipe %s\n", (unsigned long)p->pipe_handles->pipe_ref_count,
240 ndr_interface_name(&syntax->uuid, syntax->if_version)));
242 return True;
245 /****************************************************************************
246 find first available policy slot. creates a policy handle for you.
248 If "data_ptr" is given, this must be a talloc'ed object, create_policy_hnd
249 talloc_moves this into the handle. If the policy_hnd is closed,
250 data_ptr is TALLOC_FREE()'ed
251 ****************************************************************************/
253 static struct dcesrv_handle_old *create_rpc_handle_internal(
254 struct pipes_struct *p,
255 struct policy_handle *hnd,
256 uint8_t handle_type,
257 void *data_ptr)
259 struct dcesrv_handle_old *rpc_hnd = NULL;
260 static uint32_t pol_hnd_low = 0;
261 static uint32_t pol_hnd_high = 0;
262 time_t t = time(NULL);
264 if (p->pipe_handles->count > MAX_OPEN_POLS) {
265 DEBUG(0,("create_policy_hnd: ERROR: too many handles (%d) on this pipe.\n",
266 (int)p->pipe_handles->count));
267 return NULL;
270 rpc_hnd = talloc_zero(p->pipe_handles, struct dcesrv_handle_old);
271 if (!rpc_hnd) {
272 DEBUG(0,("create_policy_hnd: ERROR: out of memory!\n"));
273 return NULL;
276 if (data_ptr != NULL) {
277 rpc_hnd->data = talloc_move(rpc_hnd, &data_ptr);
280 pol_hnd_low++;
281 if (pol_hnd_low == 0) {
282 pol_hnd_high++;
285 rpc_hnd->wire_handle.handle_type = handle_type;
287 /* second bit is incrementing */
288 SIVAL(&rpc_hnd->wire_handle.uuid.time_low, 0 , pol_hnd_low);
289 SSVAL(&rpc_hnd->wire_handle.uuid.time_mid, 0 , pol_hnd_high);
290 SSVAL(&rpc_hnd->wire_handle.uuid.time_hi_and_version, 0, (pol_hnd_high >> 16));
292 /* split the current time into two 16 bit values */
294 /* something random */
295 SSVAL(rpc_hnd->wire_handle.uuid.clock_seq, 0, (t >> 16));
296 /* something random */
297 SSVAL(rpc_hnd->wire_handle.uuid.node, 0, t);
298 /* something more random */
299 SIVAL(rpc_hnd->wire_handle.uuid.node, 2, getpid());
301 DLIST_ADD(p->pipe_handles->handles, rpc_hnd);
302 p->pipe_handles->count++;
304 *hnd = rpc_hnd->wire_handle;
306 DEBUG(6, ("Opened policy hnd[%d] ", (int)p->pipe_handles->count));
307 dump_data(6, (uint8_t *)hnd, sizeof(*hnd));
309 return rpc_hnd;
312 bool create_policy_hnd(struct pipes_struct *p,
313 struct policy_handle *hnd,
314 uint8_t handle_type,
315 void *data_ptr)
317 struct dcesrv_handle_old *rpc_hnd = NULL;
319 rpc_hnd = create_rpc_handle_internal(p, hnd, handle_type, data_ptr);
320 if (rpc_hnd == NULL) {
321 return false;
323 return true;
326 /****************************************************************************
327 find policy by handle - internal version.
328 ****************************************************************************/
330 static struct dcesrv_handle_old *find_policy_by_hnd_internal(struct pipes_struct *p,
331 const struct policy_handle *hnd, void **data_p)
333 struct dcesrv_handle_old *h = NULL;
334 unsigned int count;
336 if (data_p) {
337 *data_p = NULL;
340 count = 0;
341 for (h = p->pipe_handles->handles; h != NULL; h = h->next) {
342 if (memcmp(&h->wire_handle, hnd, sizeof(*hnd)) == 0) {
343 DEBUG(6,("Found policy hnd[%u] ", count));
344 dump_data(6, (const uint8_t *)hnd, sizeof(*hnd));
345 if (data_p) {
346 *data_p = h->data;
348 return h;
350 count++;
353 DEBUG(4,("Policy not found: "));
354 dump_data(4, (const uint8_t *)hnd, sizeof(*hnd));
356 p->fault_state = DCERPC_FAULT_CONTEXT_MISMATCH;
358 return NULL;
361 /****************************************************************************
362 find policy by handle
363 ****************************************************************************/
365 bool find_policy_by_hnd(struct pipes_struct *p, const struct policy_handle *hnd,
366 void **data_p)
368 struct dcesrv_handle_old *rpc_hnd = NULL;
370 rpc_hnd = find_policy_by_hnd_internal(p, hnd, data_p);
371 if (rpc_hnd == NULL) {
372 return false;
374 return true;
377 /****************************************************************************
378 Close a policy.
379 ****************************************************************************/
381 bool close_policy_hnd(struct pipes_struct *p, struct policy_handle *hnd)
383 struct dcesrv_handle_old *rpc_hnd = NULL;
385 rpc_hnd = find_policy_by_hnd_internal(p, hnd, NULL);
387 if (rpc_hnd == NULL) {
388 DEBUG(3, ("Error closing policy (policy not found)\n"));
389 return false;
392 DEBUG(6,("Closed policy\n"));
394 p->pipe_handles->count--;
396 DLIST_REMOVE(p->pipe_handles->handles, rpc_hnd);
397 TALLOC_FREE(rpc_hnd);
399 return true;
402 /****************************************************************************
403 Close a pipe - free the handle set if it was the last pipe reference.
404 ****************************************************************************/
406 void close_policy_by_pipe(struct pipes_struct *p)
408 if (p->pipe_handles == NULL) {
409 return;
412 p->pipe_handles->pipe_ref_count--;
414 if (p->pipe_handles->pipe_ref_count == 0) {
416 * Last pipe open on this list - free the list.
418 TALLOC_FREE(p->pipe_handles);
420 DBG_DEBUG("Deleted handle list for RPC connection %s\n",
421 p->contexts ?
422 ndr_interface_name(&p->contexts->syntax.uuid,
423 p->contexts->syntax.if_version) :
424 "Unknown");
428 /*******************************************************************
429 Shall we allow access to this rpc? Currently this function
430 implements the 'restrict anonymous' setting by denying access to
431 anonymous users if the restrict anonymous level is > 0. Further work
432 will be checking a security descriptor to determine whether a user
433 token has enough access to access the pipe.
434 ********************************************************************/
436 bool pipe_access_check(struct pipes_struct *p)
438 /* Don't let anonymous users access this RPC if restrict
439 anonymous > 0 */
441 if (lp_restrict_anonymous() > 0) {
443 /* schannel, so we must be ok */
444 if (p->pipe_bound &&
445 (p->auth.auth_type == DCERPC_AUTH_TYPE_SCHANNEL)) {
446 return True;
449 if (security_session_user_level(p->session_info, NULL) < SECURITY_USER) {
450 return False;
454 return True;
457 void *_policy_handle_create(struct pipes_struct *p,
458 struct policy_handle *hnd,
459 uint8_t handle_type,
460 uint32_t access_granted,
461 size_t data_size,
462 const char *type,
463 NTSTATUS *pstatus)
465 struct dcesrv_handle_old *rpc_hnd = NULL;
466 void *data;
468 if (p->pipe_handles->count > MAX_OPEN_POLS) {
469 DEBUG(0, ("ERROR: Too many handles (%d) for RPC connection %s\n",
470 (int) p->pipe_handles->count,
471 ndr_interface_name(&p->contexts->syntax.uuid,
472 p->contexts->syntax.if_version)));
474 *pstatus = NT_STATUS_INSUFFICIENT_RESOURCES;
475 return NULL;
478 data = talloc_size(talloc_tos(), data_size);
479 if (data == NULL) {
480 *pstatus = NT_STATUS_NO_MEMORY;
481 return NULL;
483 talloc_set_name_const(data, type);
485 rpc_hnd = create_rpc_handle_internal(p, hnd, handle_type, data);
486 if (rpc_hnd == NULL) {
487 TALLOC_FREE(data);
488 *pstatus = NT_STATUS_NO_MEMORY;
489 return NULL;
491 rpc_hnd->access_granted = access_granted;
492 *pstatus = NT_STATUS_OK;
493 return data;
496 void *_policy_handle_find(struct pipes_struct *p,
497 const struct policy_handle *hnd,
498 uint32_t access_required,
499 uint32_t *paccess_granted,
500 const char *name, const char *location,
501 NTSTATUS *pstatus)
503 struct dcesrv_handle_old *rpc_hnd = NULL;
504 void *data;
506 rpc_hnd = find_policy_by_hnd_internal(p, hnd, &data);
507 if (rpc_hnd == NULL) {
508 *pstatus = NT_STATUS_INVALID_HANDLE;
509 return NULL;
511 if (strcmp(name, talloc_get_name(data)) != 0) {
512 DEBUG(10, ("expected %s, got %s\n", name,
513 talloc_get_name(data)));
514 *pstatus = NT_STATUS_INVALID_HANDLE;
515 return NULL;
517 if ((access_required & rpc_hnd->access_granted) != access_required) {
518 if (root_mode()) {
519 DEBUG(4, ("%s: ACCESS should be DENIED (granted: "
520 "%#010x; required: %#010x)\n", location,
521 rpc_hnd->access_granted, access_required));
522 DEBUGADD(4,("but overwritten by euid == 0\n"));
523 goto okay;
525 DEBUG(2,("%s: ACCESS DENIED (granted: %#010x; required: "
526 "%#010x)\n", location, rpc_hnd->access_granted,
527 access_required));
528 *pstatus = NT_STATUS_ACCESS_DENIED;
529 return NULL;
532 okay:
533 DEBUG(10, ("found handle of type %s\n", talloc_get_name(data)));
534 if (paccess_granted != NULL) {
535 *paccess_granted = rpc_hnd->access_granted;
537 *pstatus = NT_STATUS_OK;
538 return data;