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