selftest: abstract Samba3 provision a bit
[metze/samba/wip.git] / source3 / rpc_server / srv_lsa_hnd.c
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.
12  *  
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.
17  *  
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/>.
20  */
21
22 #include "includes.h"
23
24 #undef DBGC_CLASS
25 #define DBGC_CLASS DBGC_RPC_SRV
26
27 /* This is the max handles across all instances of a pipe name. */
28 #ifndef MAX_OPEN_POLS
29 #define MAX_OPEN_POLS 1024
30 #endif
31
32 /****************************************************************************
33  Hack as handles need to be persisant over lsa pipe closes so long as a samr
34  pipe is open. JRA.
35 ****************************************************************************/
36
37 static bool is_samr_lsa_pipe(const char *pipe_name)
38 {
39         return (strstr(pipe_name, "samr") || strstr(pipe_name, "lsa"));
40 }
41
42 /****************************************************************************
43  Initialise a policy handle list on a pipe. Handle list is shared between all
44  pipes of the same name.
45 ****************************************************************************/
46
47 bool init_pipe_handle_list(pipes_struct *p, const char *pipe_name)
48 {
49         pipes_struct *plist;
50         struct handle_list *hl;
51
52         for (plist = get_first_internal_pipe();
53              plist;
54              plist = get_next_internal_pipe(plist)) {
55                 if (strequal(plist->name, pipe_name)) {
56                         break;
57                 }
58                 if (is_samr_lsa_pipe(plist->name)
59                     && is_samr_lsa_pipe(pipe_name)) {
60                         /*
61                          * samr and lsa share a handle space (same process
62                          * under Windows?)
63                          */
64                         break;
65                 }
66         }
67
68         if (plist != NULL) {
69                 hl = plist->pipe_handles;
70                 if (hl == NULL) {
71                         return false;
72                 }
73         } else {
74                 /*
75                  * First open, we have to create the handle list
76                  */
77                 hl = SMB_MALLOC_P(struct handle_list);
78                 if (hl == NULL) {
79                         return false;
80                 }
81                 ZERO_STRUCTP(hl);
82
83                 DEBUG(10,("init_pipe_handles: created handle list for pipe %s\n", pipe_name ));
84         }
85
86         /*
87          * One more pipe is using this list.
88          */
89
90         hl->pipe_ref_count++;
91
92         /*
93          * Point this pipe at this list.
94          */
95
96         p->pipe_handles = hl;
97
98         DEBUG(10,("init_pipe_handles: pipe_handles ref count = %lu for pipe %s\n",
99                   (unsigned long)p->pipe_handles->pipe_ref_count, pipe_name ));
100
101         return True;
102 }
103
104 /****************************************************************************
105   find first available policy slot.  creates a policy handle for you.
106
107   If "data_ptr" is given, this must be a talloc'ed object, create_policy_hnd
108   talloc_moves this into the handle. If the policy_hnd is closed,
109   data_ptr is TALLOC_FREE()'ed
110 ****************************************************************************/
111
112 bool create_policy_hnd(pipes_struct *p, POLICY_HND *hnd, void *data_ptr)
113 {
114         static uint32 pol_hnd_low  = 0;
115         static uint32 pol_hnd_high = 0;
116         time_t t = time(NULL);
117
118         struct policy *pol;
119
120         if (p->pipe_handles->count > MAX_OPEN_POLS) {
121                 DEBUG(0,("create_policy_hnd: ERROR: too many handles (%d) on this pipe.\n",
122                                 (int)p->pipe_handles->count));
123                 return False;
124         }
125
126         pol = TALLOC_ZERO_P(NULL, struct policy);
127         if (!pol) {
128                 DEBUG(0,("create_policy_hnd: ERROR: out of memory!\n"));
129                 return False;
130         }
131
132         if (data_ptr != NULL) {
133                 pol->data_ptr = talloc_move(pol, &data_ptr);
134         }
135
136         pol_hnd_low++;
137         if (pol_hnd_low == 0)
138                 (pol_hnd_high)++;
139
140         SIVAL(&pol->pol_hnd.handle_type, 0 , 0);  /* first bit must be null */
141         SIVAL(&pol->pol_hnd.uuid.time_low, 0 , pol_hnd_low ); /* second bit is incrementing */
142         SSVAL(&pol->pol_hnd.uuid.time_mid, 0 , pol_hnd_high); /* second bit is incrementing */
143         SSVAL(&pol->pol_hnd.uuid.time_hi_and_version, 0 , (pol_hnd_high>>16)); /* second bit is incrementing */
144
145         /* split the current time into two 16 bit values */
146
147         SSVAL(pol->pol_hnd.uuid.clock_seq, 0, (t>>16)); /* something random */
148         SSVAL(pol->pol_hnd.uuid.node, 0, t); /* something random */
149
150         SIVAL(pol->pol_hnd.uuid.node, 2, sys_getpid()); /* something more random */
151
152         DLIST_ADD(p->pipe_handles->Policy, pol);
153         p->pipe_handles->count++;
154
155         *hnd = pol->pol_hnd;
156         
157         DEBUG(4,("Opened policy hnd[%d] ", (int)p->pipe_handles->count));
158         dump_data(4, (uint8 *)hnd, sizeof(*hnd));
159
160         return True;
161 }
162
163 /****************************************************************************
164   find policy by handle - internal version.
165 ****************************************************************************/
166
167 static struct policy *find_policy_by_hnd_internal(pipes_struct *p, POLICY_HND *hnd, void **data_p)
168 {
169         struct policy *pol;
170         size_t i;
171
172         if (data_p)
173                 *data_p = NULL;
174
175         for (i = 0, pol=p->pipe_handles->Policy;pol;pol=pol->next, i++) {
176                 if (memcmp(&pol->pol_hnd, hnd, sizeof(*hnd)) == 0) {
177                         DEBUG(4,("Found policy hnd[%d] ", (int)i));
178                         dump_data(4, (uint8 *)hnd, sizeof(*hnd));
179                         if (data_p)
180                                 *data_p = pol->data_ptr;
181                         return pol;
182                 }
183         }
184
185         DEBUG(4,("Policy not found: "));
186         dump_data(4, (uint8 *)hnd, sizeof(*hnd));
187
188         p->bad_handle_fault_state = True;
189
190         return NULL;
191 }
192
193 /****************************************************************************
194   find policy by handle
195 ****************************************************************************/
196
197 bool find_policy_by_hnd(pipes_struct *p, POLICY_HND *hnd, void **data_p)
198 {
199         return find_policy_by_hnd_internal(p, hnd, data_p) == NULL ? False : True;
200 }
201
202 /****************************************************************************
203   Close a policy.
204 ****************************************************************************/
205
206 bool close_policy_hnd(pipes_struct *p, POLICY_HND *hnd)
207 {
208         struct policy *pol = find_policy_by_hnd_internal(p, hnd, NULL);
209
210         if (!pol) {
211                 DEBUG(3,("Error closing policy\n"));
212                 return False;
213         }
214
215         DEBUG(3,("Closed policy\n"));
216
217         p->pipe_handles->count--;
218
219         DLIST_REMOVE(p->pipe_handles->Policy, pol);
220
221         TALLOC_FREE(pol);
222
223         return True;
224 }
225
226 /****************************************************************************
227  Close a pipe - free the handle list if it was the last pipe reference.
228 ****************************************************************************/
229
230 void close_policy_by_pipe(pipes_struct *p)
231 {
232         p->pipe_handles->pipe_ref_count--;
233
234         if (p->pipe_handles->pipe_ref_count == 0) {
235                 /*
236                  * Last pipe open on this list - free the list.
237                  */
238                 while (p->pipe_handles->Policy)
239                         close_policy_hnd(p, &p->pipe_handles->Policy->pol_hnd);
240
241                 p->pipe_handles->Policy = NULL;
242                 p->pipe_handles->count = 0;
243
244                 SAFE_FREE(p->pipe_handles);
245                 DEBUG(10,("close_policy_by_pipe: deleted handle list for pipe %s\n", p->name ));
246         }
247 }
248
249 /*******************************************************************
250 Shall we allow access to this rpc?  Currently this function
251 implements the 'restrict anonymous' setting by denying access to
252 anonymous users if the restrict anonymous level is > 0.  Further work
253 will be checking a security descriptor to determine whether a user
254 token has enough access to access the pipe.
255 ********************************************************************/
256
257 bool pipe_access_check(pipes_struct *p)
258 {
259         /* Don't let anonymous users access this RPC if restrict
260            anonymous > 0 */
261
262         if (lp_restrict_anonymous() > 0) {
263
264                 /* schannel, so we must be ok */
265                 if (p->pipe_bound && (p->auth.auth_type == PIPE_AUTH_TYPE_SCHANNEL)) {
266                         return True;
267                 }
268
269                 if (p->server_info->guest) {
270                         return False;
271                 }
272         }
273
274         return True;
275 }