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