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