Do not vasprint stuff where not necessary
[kai/samba-autobuild/.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 /*
28  * Handle database - stored per pipe.
29  */
30
31 struct policy {
32         struct policy *next, *prev;
33
34         struct policy_handle pol_hnd;
35
36         uint32_t access_granted;
37
38         void *data_ptr;
39 };
40
41 struct handle_list {
42         struct policy *Policy;  /* List of policies. */
43         size_t count;                   /* Current number of handles. */
44         size_t pipe_ref_count;  /* Number of pipe handles referring to this list. */
45 };
46
47 /* This is the max handles across all instances of a pipe name. */
48 #ifndef MAX_OPEN_POLS
49 #define MAX_OPEN_POLS 1024
50 #endif
51
52 /****************************************************************************
53  Hack as handles need to be persisant over lsa pipe closes so long as a samr
54  pipe is open. JRA.
55 ****************************************************************************/
56
57 static bool is_samr_lsa_pipe(const struct ndr_syntax_id *syntax)
58 {
59         return (ndr_syntax_id_equal(syntax, &ndr_table_samr.syntax_id)
60                 || ndr_syntax_id_equal(syntax, &ndr_table_lsarpc.syntax_id));
61 }
62
63 size_t num_pipe_handles(struct handle_list *list)
64 {
65         if (list == NULL) {
66                 return 0;
67         }
68         return list->count;
69 }
70
71 /****************************************************************************
72  Initialise a policy handle list on a pipe. Handle list is shared between all
73  pipes of the same name.
74 ****************************************************************************/
75
76 bool init_pipe_handle_list(pipes_struct *p, const struct ndr_syntax_id *syntax)
77 {
78         pipes_struct *plist;
79         struct handle_list *hl;
80
81         for (plist = get_first_internal_pipe();
82              plist;
83              plist = get_next_internal_pipe(plist)) {
84                 if (ndr_syntax_id_equal(syntax, &plist->syntax)) {
85                         break;
86                 }
87                 if (is_samr_lsa_pipe(&plist->syntax)
88                     && is_samr_lsa_pipe(syntax)) {
89                         /*
90                          * samr and lsa share a handle space (same process
91                          * under Windows?)
92                          */
93                         break;
94                 }
95         }
96
97         if (plist != NULL) {
98                 hl = plist->pipe_handles;
99                 if (hl == NULL) {
100                         return false;
101                 }
102         } else {
103                 /*
104                  * First open, we have to create the handle list
105                  */
106                 hl = SMB_MALLOC_P(struct handle_list);
107                 if (hl == NULL) {
108                         return false;
109                 }
110                 ZERO_STRUCTP(hl);
111
112                 DEBUG(10,("init_pipe_handles: created handle list for "
113                           "pipe %s\n", get_pipe_name_from_iface(syntax)));
114         }
115
116         /*
117          * One more pipe is using this list.
118          */
119
120         hl->pipe_ref_count++;
121
122         /*
123          * Point this pipe at this list.
124          */
125
126         p->pipe_handles = hl;
127
128         DEBUG(10,("init_pipe_handles: pipe_handles ref count = %lu for pipe %s\n",
129                   (unsigned long)p->pipe_handles->pipe_ref_count,
130                   get_pipe_name_from_iface(syntax)));
131
132         return True;
133 }
134
135 /****************************************************************************
136   find first available policy slot.  creates a policy handle for you.
137
138   If "data_ptr" is given, this must be a talloc'ed object, create_policy_hnd
139   talloc_moves this into the handle. If the policy_hnd is closed,
140   data_ptr is TALLOC_FREE()'ed
141 ****************************************************************************/
142
143 static struct policy *create_policy_hnd_internal(pipes_struct *p,
144                                                  struct policy_handle *hnd,
145                                                  void *data_ptr)
146 {
147         static uint32 pol_hnd_low  = 0;
148         static uint32 pol_hnd_high = 0;
149         time_t t = time(NULL);
150
151         struct policy *pol;
152
153         if (p->pipe_handles->count > MAX_OPEN_POLS) {
154                 DEBUG(0,("create_policy_hnd: ERROR: too many handles (%d) on this pipe.\n",
155                                 (int)p->pipe_handles->count));
156                 return NULL;
157         }
158
159         pol = TALLOC_ZERO_P(NULL, struct policy);
160         if (!pol) {
161                 DEBUG(0,("create_policy_hnd: ERROR: out of memory!\n"));
162                 return NULL;
163         }
164
165         if (data_ptr != NULL) {
166                 pol->data_ptr = talloc_move(pol, &data_ptr);
167         }
168
169         pol_hnd_low++;
170         if (pol_hnd_low == 0)
171                 (pol_hnd_high)++;
172
173         SIVAL(&pol->pol_hnd.handle_type, 0 , 0);  /* first bit must be null */
174         SIVAL(&pol->pol_hnd.uuid.time_low, 0 , pol_hnd_low ); /* second bit is incrementing */
175         SSVAL(&pol->pol_hnd.uuid.time_mid, 0 , pol_hnd_high); /* second bit is incrementing */
176         SSVAL(&pol->pol_hnd.uuid.time_hi_and_version, 0 , (pol_hnd_high>>16)); /* second bit is incrementing */
177
178         /* split the current time into two 16 bit values */
179
180         SSVAL(pol->pol_hnd.uuid.clock_seq, 0, (t>>16)); /* something random */
181         SSVAL(pol->pol_hnd.uuid.node, 0, t); /* something random */
182
183         SIVAL(pol->pol_hnd.uuid.node, 2, sys_getpid()); /* something more random */
184
185         DLIST_ADD(p->pipe_handles->Policy, pol);
186         p->pipe_handles->count++;
187
188         *hnd = pol->pol_hnd;
189         
190         DEBUG(4,("Opened policy hnd[%d] ", (int)p->pipe_handles->count));
191         dump_data(4, (uint8 *)hnd, sizeof(*hnd));
192
193         return pol;
194 }
195
196 bool create_policy_hnd(pipes_struct *p, struct policy_handle *hnd,
197                        void *data_ptr)
198 {
199         return create_policy_hnd_internal(p, hnd, data_ptr) != NULL;
200 }
201
202 /****************************************************************************
203   find policy by handle - internal version.
204 ****************************************************************************/
205
206 static struct policy *find_policy_by_hnd_internal(pipes_struct *p,
207                                                   const struct policy_handle *hnd,
208                                                   void **data_p)
209 {
210         struct policy *pol;
211         size_t i;
212
213         if (data_p)
214                 *data_p = NULL;
215
216         for (i = 0, pol=p->pipe_handles->Policy;pol;pol=pol->next, i++) {
217                 if (memcmp(&pol->pol_hnd, hnd, sizeof(*hnd)) == 0) {
218                         DEBUG(4,("Found policy hnd[%d] ", (int)i));
219                         dump_data(4, (uint8 *)hnd, sizeof(*hnd));
220                         if (data_p)
221                                 *data_p = pol->data_ptr;
222                         return pol;
223                 }
224         }
225
226         DEBUG(4,("Policy not found: "));
227         dump_data(4, (uint8 *)hnd, sizeof(*hnd));
228
229         p->bad_handle_fault_state = True;
230
231         return NULL;
232 }
233
234 /****************************************************************************
235   find policy by handle
236 ****************************************************************************/
237
238 bool find_policy_by_hnd(pipes_struct *p, const struct policy_handle *hnd,
239                         void **data_p)
240 {
241         return find_policy_by_hnd_internal(p, hnd, data_p) == NULL ? False : True;
242 }
243
244 /****************************************************************************
245   Close a policy.
246 ****************************************************************************/
247
248 bool close_policy_hnd(pipes_struct *p, struct policy_handle *hnd)
249 {
250         struct policy *pol = find_policy_by_hnd_internal(p, hnd, NULL);
251
252         if (!pol) {
253                 DEBUG(3,("Error closing policy\n"));
254                 return False;
255         }
256
257         DEBUG(3,("Closed policy\n"));
258
259         p->pipe_handles->count--;
260
261         DLIST_REMOVE(p->pipe_handles->Policy, pol);
262
263         TALLOC_FREE(pol);
264
265         return True;
266 }
267
268 /****************************************************************************
269  Close a pipe - free the handle list if it was the last pipe reference.
270 ****************************************************************************/
271
272 void close_policy_by_pipe(pipes_struct *p)
273 {
274         p->pipe_handles->pipe_ref_count--;
275
276         if (p->pipe_handles->pipe_ref_count == 0) {
277                 /*
278                  * Last pipe open on this list - free the list.
279                  */
280                 while (p->pipe_handles->Policy)
281                         close_policy_hnd(p, &p->pipe_handles->Policy->pol_hnd);
282
283                 p->pipe_handles->Policy = NULL;
284                 p->pipe_handles->count = 0;
285
286                 SAFE_FREE(p->pipe_handles);
287                 DEBUG(10,("close_policy_by_pipe: deleted handle list for "
288                           "pipe %s\n", get_pipe_name_from_iface(&p->syntax)));
289         }
290 }
291
292 /*******************************************************************
293 Shall we allow access to this rpc?  Currently this function
294 implements the 'restrict anonymous' setting by denying access to
295 anonymous users if the restrict anonymous level is > 0.  Further work
296 will be checking a security descriptor to determine whether a user
297 token has enough access to access the pipe.
298 ********************************************************************/
299
300 bool pipe_access_check(pipes_struct *p)
301 {
302         /* Don't let anonymous users access this RPC if restrict
303            anonymous > 0 */
304
305         if (lp_restrict_anonymous() > 0) {
306
307                 /* schannel, so we must be ok */
308                 if (p->pipe_bound && (p->auth.auth_type == PIPE_AUTH_TYPE_SCHANNEL)) {
309                         return True;
310                 }
311
312                 if (p->server_info->guest) {
313                         return False;
314                 }
315         }
316
317         return True;
318 }
319
320 void *_policy_handle_create(struct pipes_struct *p, struct policy_handle *hnd,
321                             uint32_t access_granted, size_t data_size,
322                             const char *type, NTSTATUS *pstatus)
323 {
324         struct policy *pol;
325         void *data;
326
327         if (p->pipe_handles->count > MAX_OPEN_POLS) {
328                 DEBUG(0, ("policy_handle_create: ERROR: too many handles (%d) "
329                           "on pipe %s.\n", (int)p->pipe_handles->count,
330                           get_pipe_name_from_iface(&p->syntax)));
331                 *pstatus = NT_STATUS_INSUFFICIENT_RESOURCES;
332                 return NULL;
333         }
334
335         data = talloc_size(talloc_tos(), data_size);
336         if (data == NULL) {
337                 *pstatus = NT_STATUS_NO_MEMORY;
338                 return NULL;
339         }
340         talloc_set_name_const(data, type);
341
342         pol = create_policy_hnd_internal(p, hnd, data);
343         if (pol == NULL) {
344                 TALLOC_FREE(data);
345                 *pstatus = NT_STATUS_NO_MEMORY;
346                 return NULL;
347         }
348         pol->access_granted = access_granted;
349         *pstatus = NT_STATUS_OK;
350         return data;
351 }
352
353 void *_policy_handle_find(struct pipes_struct *p,
354                           const struct policy_handle *hnd,
355                           uint32_t access_required,
356                           uint32_t *paccess_granted,
357                           const char *name, const char *location,
358                           NTSTATUS *pstatus)
359 {
360         struct policy *pol;
361         void *data;
362
363         pol = find_policy_by_hnd_internal(p, hnd, &data);
364         if (pol == NULL) {
365                 *pstatus = NT_STATUS_INVALID_HANDLE;
366                 return NULL;
367         }
368         if (strcmp(name, talloc_get_name(data)) != 0) {
369                 DEBUG(10, ("expected %s, got %s\n", name,
370                            talloc_get_name(data)));
371                 *pstatus = NT_STATUS_INVALID_HANDLE;
372                 return NULL;
373         }
374         if ((access_required & pol->access_granted) != access_required) {
375                 if (geteuid() == sec_initial_uid()) {
376                         DEBUG(4, ("%s: ACCESS should be DENIED (granted: "
377                                   "%#010x; required: %#010x)\n", location,
378                                   pol->access_granted, access_required));
379                         DEBUGADD(4,("but overwritten by euid == 0\n"));
380                         goto okay;
381                 }
382                 DEBUG(2,("%s: ACCESS DENIED (granted: %#010x; required: "
383                          "%#010x)\n", location, pol->access_granted,
384                          access_required));
385                 *pstatus = NT_STATUS_ACCESS_DENIED;
386                 return NULL;
387         }
388
389  okay:
390         DEBUG(10, ("found handle of type %s\n", talloc_get_name(data)));
391         if (paccess_granted != NULL) {
392                 *paccess_granted = pol->access_granted;
393         }
394         *pstatus = NT_STATUS_OK;
395         return data;
396 }