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.
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/>.
23 #include "system/passwd.h" /* uid_wrapper */
24 #include "../librpc/gen_ndr/ndr_lsa.h"
25 #include "../librpc/gen_ndr/ndr_samr.h"
27 #include "rpc_server/rpc_pipes.h"
28 #include "../libcli/security/security.h"
29 #include "lib/tsocket/tsocket.h"
32 #define DBGC_CLASS DBGC_RPC_SRV
34 static struct pipes_struct
*InternalPipes
;
37 * the following prototypes are declared here to avoid
38 * code being moved about too much for a patch to be
39 * disrupted / less obvious.
41 * these functions, and associated functions that they
42 * call, should be moved behind a .so module-loading
43 * system _anyway_. so that's the next step...
46 int make_base_pipes_struct(TALLOC_CTX
*mem_ctx
,
47 struct messaging_context
*msg_ctx
,
48 const char *pipe_name
,
49 enum dcerpc_transport_t transport
,
50 bool endian
, bool ncalrpc_as_system
,
51 const struct tsocket_address
*remote_address
,
52 const struct tsocket_address
*local_address
,
53 struct pipes_struct
**_p
)
55 struct pipes_struct
*p
;
57 p
= talloc_zero(mem_ctx
, struct pipes_struct
);
62 p
->mem_ctx
= talloc_named(p
, 0, "pipe %s %p", pipe_name
, p
);
69 p
->transport
= transport
;
71 p
->ncalrpc_as_system
= ncalrpc_as_system
;
73 p
->remote_address
= tsocket_address_copy(remote_address
, p
);
74 if (p
->remote_address
== NULL
) {
80 p
->local_address
= tsocket_address_copy(local_address
, p
);
81 if (p
->local_address
== NULL
) {
87 DLIST_ADD(InternalPipes
, p
);
88 talloc_set_destructor(p
, close_internal_rpc_pipe_hnd
);
95 bool check_open_pipes(void)
97 struct pipes_struct
*p
;
99 for (p
= InternalPipes
; p
!= NULL
; p
= p
->next
) {
100 if (num_pipe_handles(p
) != 0) {
107 /****************************************************************************
109 ****************************************************************************/
111 int close_internal_rpc_pipe_hnd(struct pipes_struct
*p
)
114 DEBUG(0,("Invalid pipe in close_internal_rpc_pipe_hnd\n"));
118 /* Free the handles database. */
119 close_policy_by_pipe(p
);
121 DLIST_REMOVE(InternalPipes
, p
);
127 * Handle database - stored per pipe.
130 struct dcesrv_handle
{
131 struct dcesrv_handle
*prev
, *next
;
132 struct policy_handle wire_handle
;
133 uint32_t access_granted
;
138 struct dcesrv_handle
*handles
; /* List of pipe handles. */
139 size_t count
; /* Current number of handles. */
140 size_t pipe_ref_count
; /* Number of pipe handles referring
144 /* This is the max handles across all instances of a pipe name. */
145 #ifndef MAX_OPEN_POLS
146 #define MAX_OPEN_POLS 2048
149 /****************************************************************************
150 Hack as handles need to be persisant over lsa pipe closes so long as a samr
152 ****************************************************************************/
154 static bool is_samr_lsa_pipe(const struct ndr_syntax_id
*syntax
)
156 return (ndr_syntax_id_equal(syntax
, &ndr_table_samr
.syntax_id
)
157 || ndr_syntax_id_equal(syntax
, &ndr_table_lsarpc
.syntax_id
));
160 size_t num_pipe_handles(struct pipes_struct
*p
)
162 if (p
->pipe_handles
== NULL
) {
165 return p
->pipe_handles
->count
;
168 /****************************************************************************
169 Initialise a policy handle list on a pipe. Handle list is shared between all
170 pipes of the same name.
171 ****************************************************************************/
173 bool init_pipe_handles(struct pipes_struct
*p
, const struct ndr_syntax_id
*syntax
)
175 struct pipes_struct
*plist
;
176 struct handle_list
*hl
;
178 for (plist
= InternalPipes
; plist
; plist
= plist
->next
) {
179 struct pipe_rpc_fns
*p_ctx
;
182 for (p_ctx
= plist
->contexts
;
184 p_ctx
= p_ctx
->next
) {
185 if (ndr_syntax_id_equal(syntax
, &p_ctx
->syntax
)) {
189 if (is_samr_lsa_pipe(&p_ctx
->syntax
)
190 && is_samr_lsa_pipe(syntax
)) {
192 * samr and lsa share a handle space (same process
206 hl
= plist
->pipe_handles
;
212 * First open, we have to create the handle list
214 hl
= talloc_zero(NULL
, struct handle_list
);
219 DEBUG(10,("init_pipe_handle_list: created handle list for "
221 get_pipe_name_from_syntax(talloc_tos(), syntax
)));
225 * One more pipe is using this list.
228 hl
->pipe_ref_count
++;
231 * Point this pipe at this list.
234 p
->pipe_handles
= hl
;
236 DEBUG(10,("init_pipe_handle_list: pipe_handles ref count = %lu for "
237 "pipe %s\n", (unsigned long)p
->pipe_handles
->pipe_ref_count
,
238 get_pipe_name_from_syntax(talloc_tos(), syntax
)));
243 /****************************************************************************
244 find first available policy slot. creates a policy handle for you.
246 If "data_ptr" is given, this must be a talloc'ed object, create_policy_hnd
247 talloc_moves this into the handle. If the policy_hnd is closed,
248 data_ptr is TALLOC_FREE()'ed
249 ****************************************************************************/
251 static struct dcesrv_handle
*create_rpc_handle_internal(struct pipes_struct
*p
,
252 struct policy_handle
*hnd
, void *data_ptr
)
254 struct dcesrv_handle
*rpc_hnd
;
255 static uint32 pol_hnd_low
= 0;
256 static uint32 pol_hnd_high
= 0;
257 time_t t
= time(NULL
);
259 if (p
->pipe_handles
->count
> MAX_OPEN_POLS
) {
260 DEBUG(0,("create_policy_hnd: ERROR: too many handles (%d) on this pipe.\n",
261 (int)p
->pipe_handles
->count
));
265 rpc_hnd
= talloc_zero(p
->pipe_handles
, struct dcesrv_handle
);
267 DEBUG(0,("create_policy_hnd: ERROR: out of memory!\n"));
271 if (data_ptr
!= NULL
) {
272 rpc_hnd
->data
= talloc_move(rpc_hnd
, &data_ptr
);
276 if (pol_hnd_low
== 0) {
280 /* first bit must be null */
281 SIVAL(&rpc_hnd
->wire_handle
.handle_type
, 0 , 0);
283 /* second bit is incrementing */
284 SIVAL(&rpc_hnd
->wire_handle
.uuid
.time_low
, 0 , pol_hnd_low
);
285 SSVAL(&rpc_hnd
->wire_handle
.uuid
.time_mid
, 0 , pol_hnd_high
);
286 SSVAL(&rpc_hnd
->wire_handle
.uuid
.time_hi_and_version
, 0, (pol_hnd_high
>> 16));
288 /* split the current time into two 16 bit values */
290 /* something random */
291 SSVAL(rpc_hnd
->wire_handle
.uuid
.clock_seq
, 0, (t
>> 16));
292 /* something random */
293 SSVAL(rpc_hnd
->wire_handle
.uuid
.node
, 0, t
);
294 /* something more random */
295 SIVAL(rpc_hnd
->wire_handle
.uuid
.node
, 2, sys_getpid());
297 DLIST_ADD(p
->pipe_handles
->handles
, rpc_hnd
);
298 p
->pipe_handles
->count
++;
300 *hnd
= rpc_hnd
->wire_handle
;
302 DEBUG(4, ("Opened policy hnd[%d] ", (int)p
->pipe_handles
->count
));
303 dump_data(4, (uint8_t *)hnd
, sizeof(*hnd
));
308 bool create_policy_hnd(struct pipes_struct
*p
, struct policy_handle
*hnd
,
311 struct dcesrv_handle
*rpc_hnd
;
313 rpc_hnd
= create_rpc_handle_internal(p
, hnd
, data_ptr
);
314 if (rpc_hnd
== NULL
) {
320 /****************************************************************************
321 find policy by handle - internal version.
322 ****************************************************************************/
324 static struct dcesrv_handle
*find_policy_by_hnd_internal(struct pipes_struct
*p
,
325 const struct policy_handle
*hnd
, void **data_p
)
327 struct dcesrv_handle
*h
;
335 for (h
= p
->pipe_handles
->handles
; h
!= NULL
; h
= h
->next
) {
336 if (memcmp(&h
->wire_handle
, hnd
, sizeof(*hnd
)) == 0) {
337 DEBUG(4,("Found policy hnd[%u] ", count
));
338 dump_data(4, (const uint8
*)hnd
, sizeof(*hnd
));
347 DEBUG(4,("Policy not found: "));
348 dump_data(4, (const uint8_t *)hnd
, sizeof(*hnd
));
350 p
->bad_handle_fault_state
= true;
355 /****************************************************************************
356 find policy by handle
357 ****************************************************************************/
359 bool find_policy_by_hnd(struct pipes_struct
*p
, const struct policy_handle
*hnd
,
362 struct dcesrv_handle
*rpc_hnd
;
364 rpc_hnd
= find_policy_by_hnd_internal(p
, hnd
, data_p
);
365 if (rpc_hnd
== NULL
) {
371 /****************************************************************************
373 ****************************************************************************/
375 bool close_policy_hnd(struct pipes_struct
*p
, struct policy_handle
*hnd
)
377 struct dcesrv_handle
*rpc_hnd
;
379 rpc_hnd
= find_policy_by_hnd_internal(p
, hnd
, NULL
);
381 if (rpc_hnd
== NULL
) {
382 DEBUG(3, ("Error closing policy (policy not found)\n"));
386 DEBUG(3,("Closed policy\n"));
388 p
->pipe_handles
->count
--;
390 DLIST_REMOVE(p
->pipe_handles
->handles
, rpc_hnd
);
391 TALLOC_FREE(rpc_hnd
);
396 /****************************************************************************
397 Close a pipe - free the handle set if it was the last pipe reference.
398 ****************************************************************************/
400 void close_policy_by_pipe(struct pipes_struct
*p
)
402 if (p
->pipe_handles
== NULL
) {
406 p
->pipe_handles
->pipe_ref_count
--;
408 if (p
->pipe_handles
->pipe_ref_count
== 0) {
410 * Last pipe open on this list - free the list.
412 TALLOC_FREE(p
->pipe_handles
);
414 DEBUG(10,("Deleted handle list for RPC connection %s\n",
415 get_pipe_name_from_syntax(talloc_tos(),
416 &p
->contexts
->syntax
)));
420 /*******************************************************************
421 Shall we allow access to this rpc? Currently this function
422 implements the 'restrict anonymous' setting by denying access to
423 anonymous users if the restrict anonymous level is > 0. Further work
424 will be checking a security descriptor to determine whether a user
425 token has enough access to access the pipe.
426 ********************************************************************/
428 bool pipe_access_check(struct pipes_struct
*p
)
430 /* Don't let anonymous users access this RPC if restrict
433 if (lp_restrict_anonymous() > 0) {
435 /* schannel, so we must be ok */
437 (p
->auth
.auth_type
== DCERPC_AUTH_TYPE_SCHANNEL
)) {
441 if (security_session_user_level(p
->session_info
, NULL
) < SECURITY_USER
) {
449 void *_policy_handle_create(struct pipes_struct
*p
, struct policy_handle
*hnd
,
450 uint32_t access_granted
, size_t data_size
,
451 const char *type
, NTSTATUS
*pstatus
)
453 struct dcesrv_handle
*rpc_hnd
;
456 if (p
->pipe_handles
->count
> MAX_OPEN_POLS
) {
457 DEBUG(0, ("ERROR: Too many handles (%d) for RPC connection %s\n",
458 (int) p
->pipe_handles
->count
,
459 get_pipe_name_from_syntax(talloc_tos(),
460 &p
->contexts
->syntax
)));
461 *pstatus
= NT_STATUS_INSUFFICIENT_RESOURCES
;
465 data
= talloc_size(talloc_tos(), data_size
);
467 *pstatus
= NT_STATUS_NO_MEMORY
;
470 talloc_set_name_const(data
, type
);
472 rpc_hnd
= create_rpc_handle_internal(p
, hnd
, data
);
473 if (rpc_hnd
== NULL
) {
475 *pstatus
= NT_STATUS_NO_MEMORY
;
478 rpc_hnd
->access_granted
= access_granted
;
479 *pstatus
= NT_STATUS_OK
;
483 void *_policy_handle_find(struct pipes_struct
*p
,
484 const struct policy_handle
*hnd
,
485 uint32_t access_required
,
486 uint32_t *paccess_granted
,
487 const char *name
, const char *location
,
490 struct dcesrv_handle
*rpc_hnd
;
493 rpc_hnd
= find_policy_by_hnd_internal(p
, hnd
, &data
);
494 if (rpc_hnd
== NULL
) {
495 *pstatus
= NT_STATUS_INVALID_HANDLE
;
498 if (strcmp(name
, talloc_get_name(data
)) != 0) {
499 DEBUG(10, ("expected %s, got %s\n", name
,
500 talloc_get_name(data
)));
501 *pstatus
= NT_STATUS_INVALID_HANDLE
;
504 if ((access_required
& rpc_hnd
->access_granted
) != access_required
) {
505 if (geteuid() == sec_initial_uid()) {
506 DEBUG(4, ("%s: ACCESS should be DENIED (granted: "
507 "%#010x; required: %#010x)\n", location
,
508 rpc_hnd
->access_granted
, access_required
));
509 DEBUGADD(4,("but overwritten by euid == 0\n"));
512 DEBUG(2,("%s: ACCESS DENIED (granted: %#010x; required: "
513 "%#010x)\n", location
, rpc_hnd
->access_granted
,
515 *pstatus
= NT_STATUS_ACCESS_DENIED
;
520 DEBUG(10, ("found handle of type %s\n", talloc_get_name(data
)));
521 if (paccess_granted
!= NULL
) {
522 *paccess_granted
= rpc_hnd
->access_granted
;
524 *pstatus
= NT_STATUS_OK
;