3 things:
[Samba/gebeck_regimport.git] / source / rpc_server / srv_lsa_hnd.c
blob814fa60aaba4c8b897b2ed696781bb4fd4cdcfc1
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 2 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, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "includes.h"
25 #undef DBGC_CLASS
26 #define DBGC_CLASS DBGC_RPC_SRV
28 /* This is the max handles across all instances of a pipe name. */
29 #ifndef MAX_OPEN_POLS
30 #define MAX_OPEN_POLS 1024
31 #endif
33 /****************************************************************************
34 Hack as handles need to be persisant over lsa pipe closes so long as a samr
35 pipe is open. JRA.
36 ****************************************************************************/
38 static BOOL is_samr_lsa_pipe(const char *pipe_name)
40 return (strstr(pipe_name, "samr") || strstr(pipe_name, "lsa"));
43 /****************************************************************************
44 Initialise a policy handle list on a pipe. Handle list is shared between all
45 pipes of the same name.
46 ****************************************************************************/
48 BOOL init_pipe_handle_list(pipes_struct *p, char *pipe_name)
50 pipes_struct *plist = get_first_internal_pipe();
51 struct handle_list *hl = NULL;
53 for (plist = get_first_internal_pipe(); plist; plist = get_next_internal_pipe(plist)) {
54 if (strequal( plist->name, pipe_name) ||
55 (is_samr_lsa_pipe(plist->name) && is_samr_lsa_pipe(pipe_name))) {
56 if (!plist->pipe_handles) {
57 pstring msg;
58 slprintf(msg, sizeof(msg)-1, "init_pipe_handles: NULL pipe_handle pointer in pipe %s",
59 pipe_name );
60 smb_panic(msg);
62 hl = plist->pipe_handles;
63 break;
67 if (!hl) {
69 * No handle list for this pipe (first open of pipe).
70 * Create list.
73 if ((hl = (struct handle_list *)malloc(sizeof(struct handle_list))) == NULL)
74 return False;
75 ZERO_STRUCTP(hl);
77 DEBUG(10,("init_pipe_handles: created handle list for pipe %s\n", pipe_name ));
81 * One more pipe is using this list.
84 hl->pipe_ref_count++;
87 * Point this pipe at this list.
90 p->pipe_handles = hl;
92 DEBUG(10,("init_pipe_handles: pipe_handles ref count = %u for pipe %s\n",
93 p->pipe_handles->pipe_ref_count, pipe_name ));
95 return True;
98 /****************************************************************************
99 find first available policy slot. creates a policy handle for you.
100 ****************************************************************************/
102 BOOL create_policy_hnd(pipes_struct *p, POLICY_HND *hnd, void (*free_fn)(void *), void *data_ptr)
104 static uint32 pol_hnd_low = 0;
105 static uint32 pol_hnd_high = 0;
107 struct policy *pol;
109 if (p->pipe_handles->count > MAX_OPEN_POLS) {
110 DEBUG(0,("create_policy_hnd: ERROR: too many handles (%d) on this pipe.\n",
111 (int)p->pipe_handles->count));
112 return False;
115 pol = (struct policy *)malloc(sizeof(*p));
116 if (!pol) {
117 DEBUG(0,("create_policy_hnd: ERROR: out of memory!\n"));
118 return False;
121 ZERO_STRUCTP(pol);
123 pol->data_ptr = data_ptr;
124 pol->free_fn = free_fn;
126 pol_hnd_low++;
127 if (pol_hnd_low == 0)
128 (pol_hnd_high)++;
130 SIVAL(&pol->pol_hnd.data1, 0 , 0); /* first bit must be null */
131 SIVAL(&pol->pol_hnd.data2, 0 , pol_hnd_low ); /* second bit is incrementing */
132 SSVAL(&pol->pol_hnd.data3, 0 , pol_hnd_high); /* second bit is incrementing */
133 SSVAL(&pol->pol_hnd.data4, 0 , (pol_hnd_high>>16)); /* second bit is incrementing */
134 SIVAL(pol->pol_hnd.data5, 0, time(NULL)); /* something random */
135 SIVAL(pol->pol_hnd.data5, 4, sys_getpid()); /* something more random */
137 DLIST_ADD(p->pipe_handles->Policy, pol);
138 p->pipe_handles->count++;
140 *hnd = pol->pol_hnd;
142 DEBUG(4,("Opened policy hnd[%d] ", (int)p->pipe_handles->count));
143 dump_data(4, (char *)hnd, sizeof(*hnd));
145 return True;
148 /****************************************************************************
149 find policy by handle - internal version.
150 ****************************************************************************/
152 static struct policy *find_policy_by_hnd_internal(pipes_struct *p, POLICY_HND *hnd, void **data_p)
154 struct policy *pol;
155 size_t i;
157 if (data_p)
158 *data_p = NULL;
160 for (i = 0, pol=p->pipe_handles->Policy;pol;pol=pol->next, i++) {
161 if (memcmp(&pol->pol_hnd, hnd, sizeof(*hnd)) == 0) {
162 DEBUG(4,("Found policy hnd[%d] ", (int)i));
163 dump_data(4, (char *)hnd, sizeof(*hnd));
164 if (data_p)
165 *data_p = pol->data_ptr;
166 return pol;
170 DEBUG(4,("Policy not found: "));
171 dump_data(4, (char *)hnd, sizeof(*hnd));
173 p->bad_handle_fault_state = True;
175 return NULL;
178 /****************************************************************************
179 find policy by handle
180 ****************************************************************************/
182 BOOL find_policy_by_hnd(pipes_struct *p, POLICY_HND *hnd, void **data_p)
184 return find_policy_by_hnd_internal(p, hnd, data_p) == NULL ? False : True;
187 /****************************************************************************
188 Close a policy.
189 ****************************************************************************/
191 BOOL close_policy_hnd(pipes_struct *p, POLICY_HND *hnd)
193 struct policy *pol = find_policy_by_hnd_internal(p, hnd, NULL);
195 if (!pol) {
196 DEBUG(3,("Error closing policy\n"));
197 return False;
200 DEBUG(3,("Closed policy\n"));
202 if (pol->free_fn && pol->data_ptr)
203 (*pol->free_fn)(pol->data_ptr);
205 p->pipe_handles->count--;
207 DLIST_REMOVE(p->pipe_handles->Policy, pol);
209 ZERO_STRUCTP(pol);
211 SAFE_FREE(pol);
213 return True;
216 /****************************************************************************
217 Close a pipe - free the handle list if it was the last pipe reference.
218 ****************************************************************************/
220 void close_policy_by_pipe(pipes_struct *p)
222 p->pipe_handles->pipe_ref_count--;
224 if (p->pipe_handles->pipe_ref_count == 0) {
226 * Last pipe open on this list - free the list.
228 while (p->pipe_handles->Policy)
229 close_policy_hnd(p, &p->pipe_handles->Policy->pol_hnd);
231 p->pipe_handles->Policy = NULL;
232 p->pipe_handles->count = 0;
234 SAFE_FREE(p->pipe_handles);
235 DEBUG(10,("close_policy_by_pipe: deleted handle list for pipe %s\n", p->name ));
239 /*******************************************************************
240 Shall we allow access to this rpc? Currently this function
241 implements the 'restrict anonymous' setting by denying access to
242 anonymous users if the restrict anonymous level is > 0. Further work
243 will be checking a security descriptor to determine whether a user
244 token has enough access to access the pipe.
245 ********************************************************************/
247 BOOL pipe_access_check(pipes_struct *p)
249 /* Don't let anonymous users access this RPC if restrict
250 anonymous > 0 */
252 if (lp_restrict_anonymous() > 0) {
253 user_struct *user = get_valid_user_struct(p->vuid);
255 if (!user) {
256 DEBUG(3, ("invalid vuid %d\n", p->vuid));
257 return False;
260 if (user->guest)
261 return False;
264 return True;