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 "../librpc/gen_ndr/ndr_lsa.h"
26 #define DBGC_CLASS DBGC_RPC_SRV
29 * Handle database - stored per pipe.
33 struct policy
*next
, *prev
;
35 struct policy_handle pol_hnd
;
37 uint32_t access_granted
;
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. */
50 #define MAX_OPEN_POLS 2048
53 /****************************************************************************
54 Hack as handles need to be persisant over lsa pipe closes so long as a samr
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
)
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
)
80 struct handle_list
*hl
;
82 for (plist
= get_first_internal_pipe();
84 plist
= get_next_internal_pipe(plist
)) {
85 if (ndr_syntax_id_equal(syntax
, &plist
->syntax
)) {
88 if (is_samr_lsa_pipe(&plist
->syntax
)
89 && is_samr_lsa_pipe(syntax
)) {
91 * samr and lsa share a handle space (same process
99 hl
= plist
->pipe_handles
;
105 * First open, we have to create the handle list
107 hl
= SMB_MALLOC_P(struct handle_list
);
113 DEBUG(10,("init_pipe_handle_list: created handle list for "
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
)));
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
,
149 static uint32 pol_hnd_low
= 0;
150 static uint32 pol_hnd_high
= 0;
151 time_t t
= time(NULL
);
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
));
161 pol
= TALLOC_ZERO_P(NULL
, struct policy
);
163 DEBUG(0,("create_policy_hnd: ERROR: out of memory!\n"));
167 if (data_ptr
!= NULL
) {
168 pol
->data_ptr
= talloc_move(pol
, &data_ptr
);
172 if (pol_hnd_low
== 0)
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
++;
192 DEBUG(4,("Opened policy hnd[%d] ", (int)p
->pipe_handles
->count
));
193 dump_data(4, (uint8
*)hnd
, sizeof(*hnd
));
198 bool create_policy_hnd(pipes_struct
*p
, struct policy_handle
*hnd
,
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
,
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
));
223 *data_p
= pol
->data_ptr
;
228 DEBUG(4,("Policy not found: "));
229 dump_data(4, (uint8
*)hnd
, sizeof(*hnd
));
231 p
->bad_handle_fault_state
= True
;
236 /****************************************************************************
237 find policy by handle
238 ****************************************************************************/
240 bool find_policy_by_hnd(pipes_struct
*p
, const struct policy_handle
*hnd
,
243 return find_policy_by_hnd_internal(p
, hnd
, data_p
) == NULL
? False
: True
;
246 /****************************************************************************
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
);
255 DEBUG(3,("Error closing policy\n"));
259 DEBUG(3,("Closed policy\n"));
261 p
->pipe_handles
->count
--;
263 DLIST_REMOVE(p
->pipe_handles
->Policy
, pol
);
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 "
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
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
)) {
315 if (p
->server_info
->guest
) {
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
)
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
;
338 data
= talloc_size(talloc_tos(), data_size
);
340 *pstatus
= NT_STATUS_NO_MEMORY
;
343 talloc_set_name_const(data
, type
);
345 pol
= create_policy_hnd_internal(p
, hnd
, data
);
348 *pstatus
= NT_STATUS_NO_MEMORY
;
351 pol
->access_granted
= access_granted
;
352 *pstatus
= NT_STATUS_OK
;
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
,
366 pol
= find_policy_by_hnd_internal(p
, hnd
, &data
);
368 *pstatus
= NT_STATUS_INVALID_HANDLE
;
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
;
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"));
385 DEBUG(2,("%s: ACCESS DENIED (granted: %#010x; required: "
386 "%#010x)\n", location
, pol
->access_granted
,
388 *pstatus
= NT_STATUS_ACCESS_DENIED
;
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
;