s3: Fix Coverity ID 2332: MISSING_BREAK
[samba.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 #include "auth.h"
26 #include "ntdomain.h"
27
28 #undef DBGC_CLASS
29 #define DBGC_CLASS DBGC_RPC_SRV
30
31 /*
32  * Handle database - stored per pipe.
33  */
34
35 struct dcesrv_handle {
36         struct dcesrv_handle *prev, *next;
37         struct policy_handle wire_handle;
38         uint32_t access_granted;
39         void *data;
40 };
41
42 struct handle_list {
43         struct dcesrv_handle *handles;  /* List of pipe handles. */
44         size_t count;                   /* Current number of handles. */
45         size_t pipe_ref_count;          /* Number of pipe handles referring
46                                          * to this tree. */
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(struct 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_handles(struct pipes_struct *p, const struct ndr_syntax_id *syntax)
79 {
80         struct 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 = talloc_zero(NULL, struct handle_list);
109                 if (hl == NULL) {
110                         return false;
111                 }
112
113                 DEBUG(10,("init_pipe_handle_list: created handle list for "
114                           "pipe %s\n",
115                           get_pipe_name_from_syntax(talloc_tos(), syntax)));
116         }
117
118         /*
119          * One more pipe is using this list.
120          */
121
122         hl->pipe_ref_count++;
123
124         /*
125          * Point this pipe at this list.
126          */
127
128         p->pipe_handles = hl;
129
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)));
133
134         return True;
135 }
136
137 /****************************************************************************
138   find first available policy slot.  creates a policy handle for you.
139
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 ****************************************************************************/
144
145 static struct dcesrv_handle *create_rpc_handle_internal(struct pipes_struct *p,
146                                 struct policy_handle *hnd, void *data_ptr)
147 {
148         struct dcesrv_handle *rpc_hnd;
149         static uint32 pol_hnd_low  = 0;
150         static uint32 pol_hnd_high = 0;
151         time_t t = time(NULL);
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         rpc_hnd = talloc_zero(p->pipe_handles, struct dcesrv_handle);
160         if (!rpc_hnd) {
161                 DEBUG(0,("create_policy_hnd: ERROR: out of memory!\n"));
162                 return NULL;
163         }
164
165         if (data_ptr != NULL) {
166                 rpc_hnd->data = talloc_move(rpc_hnd, &data_ptr);
167         }
168
169         pol_hnd_low++;
170         if (pol_hnd_low == 0) {
171                 pol_hnd_high++;
172         }
173
174         /* first bit must be null */
175         SIVAL(&rpc_hnd->wire_handle.handle_type, 0 , 0);
176
177         /* second bit is incrementing */
178         SIVAL(&rpc_hnd->wire_handle.uuid.time_low, 0 , pol_hnd_low);
179         SSVAL(&rpc_hnd->wire_handle.uuid.time_mid, 0 , pol_hnd_high);
180         SSVAL(&rpc_hnd->wire_handle.uuid.time_hi_and_version, 0, (pol_hnd_high >> 16));
181
182         /* split the current time into two 16 bit values */
183
184         /* something random */
185         SSVAL(rpc_hnd->wire_handle.uuid.clock_seq, 0, (t >> 16));
186         /* something random */
187         SSVAL(rpc_hnd->wire_handle.uuid.node, 0, t);
188         /* something more random */
189         SIVAL(rpc_hnd->wire_handle.uuid.node, 2, sys_getpid());
190
191         DLIST_ADD(p->pipe_handles->handles, rpc_hnd);
192         p->pipe_handles->count++;
193
194         *hnd = rpc_hnd->wire_handle;
195
196         DEBUG(4, ("Opened policy hnd[%d] ", (int)p->pipe_handles->count));
197         dump_data(4, (uint8_t *)hnd, sizeof(*hnd));
198
199         return rpc_hnd;
200 }
201
202 bool create_policy_hnd(struct pipes_struct *p, struct policy_handle *hnd,
203                        void *data_ptr)
204 {
205         struct dcesrv_handle *rpc_hnd;
206
207         rpc_hnd = create_rpc_handle_internal(p, hnd, data_ptr);
208         if (rpc_hnd == NULL) {
209                 return false;
210         }
211         return true;
212 }
213
214 /****************************************************************************
215   find policy by handle - internal version.
216 ****************************************************************************/
217
218 static struct dcesrv_handle *find_policy_by_hnd_internal(struct pipes_struct *p,
219                                 const struct policy_handle *hnd, void **data_p)
220 {
221         struct dcesrv_handle *h;
222         unsigned int count;
223
224         if (data_p) {
225                 *data_p = NULL;
226         }
227
228         count = 0;
229         for (h = p->pipe_handles->handles; h != NULL; h = h->next) {
230                 if (memcmp(&h->wire_handle, hnd, sizeof(*hnd)) == 0) {
231                         DEBUG(4,("Found policy hnd[%u] ", count));
232                         dump_data(4, (uint8 *)hnd, sizeof(*hnd));
233                         if (data_p) {
234                                 *data_p = h->data;
235                         }
236                         return h;
237                 }
238                 count++;
239         }
240
241         DEBUG(4,("Policy not found: "));
242         dump_data(4, (uint8_t *)hnd, sizeof(*hnd));
243
244         p->bad_handle_fault_state = true;
245
246         return NULL;
247 }
248
249 /****************************************************************************
250   find policy by handle
251 ****************************************************************************/
252
253 bool find_policy_by_hnd(struct pipes_struct *p, const struct policy_handle *hnd,
254                         void **data_p)
255 {
256         struct dcesrv_handle *rpc_hnd;
257
258         rpc_hnd = find_policy_by_hnd_internal(p, hnd, data_p);
259         if (rpc_hnd == NULL) {
260                 return false;
261         }
262         return true;
263 }
264
265 /****************************************************************************
266   Close a policy.
267 ****************************************************************************/
268
269 bool close_policy_hnd(struct pipes_struct *p, struct policy_handle *hnd)
270 {
271         struct dcesrv_handle *rpc_hnd;
272
273         rpc_hnd = find_policy_by_hnd_internal(p, hnd, NULL);
274
275         if (rpc_hnd == NULL) {
276                 DEBUG(3, ("Error closing policy (policy not found)\n"));
277                 return false;
278         }
279
280         DEBUG(3,("Closed policy\n"));
281
282         p->pipe_handles->count--;
283
284         DLIST_REMOVE(p->pipe_handles->handles, rpc_hnd);
285         TALLOC_FREE(rpc_hnd);
286
287         return true;
288 }
289
290 /****************************************************************************
291  Close a pipe - free the handle set if it was the last pipe reference.
292 ****************************************************************************/
293
294 void close_policy_by_pipe(struct pipes_struct *p)
295 {
296         p->pipe_handles->pipe_ref_count--;
297
298         if (p->pipe_handles->pipe_ref_count == 0) {
299                 /*
300                  * Last pipe open on this list - free the list.
301                  */
302                 TALLOC_FREE(p->pipe_handles);
303
304                 DEBUG(10,("close_policy_by_pipe: deleted handle list for "
305                           "pipe %s\n",
306                           get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
307         }
308 }
309
310 /*******************************************************************
311 Shall we allow access to this rpc?  Currently this function
312 implements the 'restrict anonymous' setting by denying access to
313 anonymous users if the restrict anonymous level is > 0.  Further work
314 will be checking a security descriptor to determine whether a user
315 token has enough access to access the pipe.
316 ********************************************************************/
317
318 bool pipe_access_check(struct pipes_struct *p)
319 {
320         /* Don't let anonymous users access this RPC if restrict
321            anonymous > 0 */
322
323         if (lp_restrict_anonymous() > 0) {
324
325                 /* schannel, so we must be ok */
326                 if (p->pipe_bound &&
327                     (p->auth.auth_type == DCERPC_AUTH_TYPE_SCHANNEL)) {
328                         return True;
329                 }
330
331                 if (p->session_info->guest) {
332                         return False;
333                 }
334         }
335
336         return True;
337 }
338
339 void *_policy_handle_create(struct pipes_struct *p, struct policy_handle *hnd,
340                             uint32_t access_granted, size_t data_size,
341                             const char *type, NTSTATUS *pstatus)
342 {
343         struct dcesrv_handle *rpc_hnd;
344         void *data;
345
346         if (p->pipe_handles->count > MAX_OPEN_POLS) {
347                 DEBUG(0, ("policy_handle_create: ERROR: too many handles (%d) "
348                           "on pipe %s.\n", (int)p->pipe_handles->count,
349                           get_pipe_name_from_syntax(talloc_tos(), &p->syntax)));
350                 *pstatus = NT_STATUS_INSUFFICIENT_RESOURCES;
351                 return NULL;
352         }
353
354         data = talloc_size(talloc_tos(), data_size);
355         if (data == NULL) {
356                 *pstatus = NT_STATUS_NO_MEMORY;
357                 return NULL;
358         }
359         talloc_set_name_const(data, type);
360
361         rpc_hnd = create_rpc_handle_internal(p, hnd, data);
362         if (rpc_hnd == NULL) {
363                 TALLOC_FREE(data);
364                 *pstatus = NT_STATUS_NO_MEMORY;
365                 return NULL;
366         }
367         rpc_hnd->access_granted = access_granted;
368         *pstatus = NT_STATUS_OK;
369         return data;
370 }
371
372 void *_policy_handle_find(struct pipes_struct *p,
373                           const struct policy_handle *hnd,
374                           uint32_t access_required,
375                           uint32_t *paccess_granted,
376                           const char *name, const char *location,
377                           NTSTATUS *pstatus)
378 {
379         struct dcesrv_handle *rpc_hnd;
380         void *data;
381
382         rpc_hnd = find_policy_by_hnd_internal(p, hnd, &data);
383         if (rpc_hnd == NULL) {
384                 *pstatus = NT_STATUS_INVALID_HANDLE;
385                 return NULL;
386         }
387         if (strcmp(name, talloc_get_name(data)) != 0) {
388                 DEBUG(10, ("expected %s, got %s\n", name,
389                            talloc_get_name(data)));
390                 *pstatus = NT_STATUS_INVALID_HANDLE;
391                 return NULL;
392         }
393         if ((access_required & rpc_hnd->access_granted) != access_required) {
394                 if (geteuid() == sec_initial_uid()) {
395                         DEBUG(4, ("%s: ACCESS should be DENIED (granted: "
396                                   "%#010x; required: %#010x)\n", location,
397                                   rpc_hnd->access_granted, access_required));
398                         DEBUGADD(4,("but overwritten by euid == 0\n"));
399                         goto okay;
400                 }
401                 DEBUG(2,("%s: ACCESS DENIED (granted: %#010x; required: "
402                          "%#010x)\n", location, rpc_hnd->access_granted,
403                          access_required));
404                 *pstatus = NT_STATUS_ACCESS_DENIED;
405                 return NULL;
406         }
407
408  okay:
409         DEBUG(10, ("found handle of type %s\n", talloc_get_name(data)));
410         if (paccess_granted != NULL) {
411                 *paccess_granted = rpc_hnd->access_granted;
412         }
413         *pstatus = NT_STATUS_OK;
414         return data;
415 }