95d47ef2e9a0e50661adb81d5c3f15ff0af6f900
[ira/wip.git] / source3 / winbindd / winbindd_misc.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Winbind daemon - miscellaneous other functions
5
6    Copyright (C) Tim Potter      2000
7    Copyright (C) Andrew Bartlett 2002
8    
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "winbindd.h"
25 #include "../librpc/gen_ndr/cli_netlogon.h"
26
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_WINBIND
29
30 /* Constants and helper functions for determining domain trust types */
31
32 enum trust_type {
33         EXTERNAL = 0,
34         FOREST,
35         IN_FOREST,
36         NONE,
37 };
38
39 const char *trust_type_strings[] = {"External", 
40                                     "Forest", 
41                                     "In Forest",
42                                     "None"};
43
44 static enum trust_type get_trust_type(struct winbindd_tdc_domain *domain)
45 {
46         if (domain->trust_attribs == NETR_TRUST_ATTRIBUTE_QUARANTINED_DOMAIN)   
47                 return EXTERNAL;
48         else if (domain->trust_attribs == NETR_TRUST_ATTRIBUTE_FOREST_TRANSITIVE)
49                 return FOREST;
50         else if (((domain->trust_flags & NETR_TRUST_FLAG_IN_FOREST) == NETR_TRUST_FLAG_IN_FOREST) &&
51             ((domain->trust_flags & NETR_TRUST_FLAG_PRIMARY) == 0x0))
52                 return IN_FOREST;
53         return NONE;    
54 }
55
56 static const char *get_trust_type_string(struct winbindd_tdc_domain *domain)
57 {
58         return trust_type_strings[get_trust_type(domain)];
59 }
60
61 static bool trust_is_inbound(struct winbindd_tdc_domain *domain)
62 {
63         return (domain->trust_flags == 0x0) ||
64             ((domain->trust_flags & NETR_TRUST_FLAG_IN_FOREST) ==
65             NETR_TRUST_FLAG_IN_FOREST) ||                       
66             ((domain->trust_flags & NETR_TRUST_FLAG_INBOUND) ==
67             NETR_TRUST_FLAG_INBOUND);           
68 }
69
70 static bool trust_is_outbound(struct winbindd_tdc_domain *domain)
71 {
72         return (domain->trust_flags == 0x0) ||
73             ((domain->trust_flags & NETR_TRUST_FLAG_IN_FOREST) ==
74             NETR_TRUST_FLAG_IN_FOREST) ||                       
75             ((domain->trust_flags & NETR_TRUST_FLAG_OUTBOUND) ==
76             NETR_TRUST_FLAG_OUTBOUND);          
77 }
78
79 static bool trust_is_transitive(struct winbindd_tdc_domain *domain)
80 {
81         if ((domain->trust_attribs == NETR_TRUST_ATTRIBUTE_NON_TRANSITIVE) ||         
82             (domain->trust_attribs == NETR_TRUST_ATTRIBUTE_QUARANTINED_DOMAIN) ||
83             (domain->trust_attribs == NETR_TRUST_ATTRIBUTE_TREAT_AS_EXTERNAL))
84                 return False;
85         return True;
86 }
87
88 void winbindd_list_trusted_domains(struct winbindd_cli_state *state)
89 {
90         struct winbindd_tdc_domain *dom_list = NULL;
91         struct winbindd_tdc_domain *d = NULL;
92         size_t num_domains = 0;
93         int extra_data_len = 0;
94         char *extra_data = NULL;
95         int i = 0;
96         
97         DEBUG(3, ("[%5lu]: list trusted domains\n",
98                   (unsigned long)state->pid));
99
100         if( !wcache_tdc_fetch_list( &dom_list, &num_domains )) {
101                 request_error(state);   
102                 goto done;
103         }
104
105         for ( i = 0; i < num_domains; i++ ) {
106                 struct winbindd_domain *domain;
107                 bool is_online = true;          
108
109                 d = &dom_list[i];
110                 domain = find_domain_from_name_noinit(d->domain_name);
111                 if (domain) {
112                         is_online = domain->online;
113                 }
114
115                 if ( !extra_data ) {
116                         extra_data = talloc_asprintf(state->mem_ctx, 
117                                                      "%s\\%s\\%s\\%s\\%s\\%s\\%s\\%s",
118                                                      d->domain_name,
119                                                      d->dns_name ? d->dns_name : d->domain_name,
120                                                      sid_string_talloc(state->mem_ctx, &d->sid),
121                                                      get_trust_type_string(d),
122                                                      trust_is_transitive(d) ? "Yes" : "No",
123                                                      trust_is_inbound(d) ? "Yes" : "No",
124                                                      trust_is_outbound(d) ? "Yes" : "No",
125                                                      is_online ? "Online" : "Offline" );
126                 } else {
127                         extra_data = talloc_asprintf(state->mem_ctx, 
128                                                      "%s\n%s\\%s\\%s\\%s\\%s\\%s\\%s\\%s",
129                                                      extra_data,
130                                                      d->domain_name,
131                                                      d->dns_name ? d->dns_name : d->domain_name,
132                                                      sid_string_talloc(state->mem_ctx, &d->sid),
133                                                      get_trust_type_string(d),
134                                                      trust_is_transitive(d) ? "Yes" : "No",
135                                                      trust_is_inbound(d) ? "Yes" : "No",
136                                                      trust_is_outbound(d) ? "Yes" : "No",
137                                                      is_online ? "Online" : "Offline" );
138                 }
139         }
140         
141         extra_data_len = 0;
142         if (extra_data != NULL) {
143                 extra_data_len = strlen(extra_data);
144         }
145
146         if (extra_data_len > 0) {
147                 state->response->extra_data.data = extra_data;
148                 state->response->length += extra_data_len+1;
149         }
150
151         request_ok(state);      
152 done:
153         TALLOC_FREE( dom_list );
154 }
155
156 enum winbindd_result winbindd_dual_list_trusted_domains(struct winbindd_domain *domain,
157                                                         struct winbindd_cli_state *state)
158 {
159         uint32 i, num_domains;
160         char **names, **alt_names;
161         DOM_SID *sids;
162         int extra_data_len = 0;
163         char *extra_data;
164         NTSTATUS result;
165         bool have_own_domain = False;
166
167         DEBUG(3, ("[%5lu]: list trusted domains\n",
168                   (unsigned long)state->pid));
169
170         result = domain->methods->trusted_domains(domain, state->mem_ctx,
171                                                   &num_domains, &names,
172                                                   &alt_names, &sids);
173
174         if (!NT_STATUS_IS_OK(result)) {
175                 DEBUG(3, ("winbindd_dual_list_trusted_domains: trusted_domains returned %s\n",
176                         nt_errstr(result) ));
177                 return WINBINDD_ERROR;
178         }
179
180         extra_data = talloc_strdup(state->mem_ctx, "");
181
182         if (num_domains > 0)
183                 extra_data = talloc_asprintf(
184                         state->mem_ctx, "%s\\%s\\%s",
185                         names[0], alt_names[0] ? alt_names[0] : names[0],
186                         sid_string_talloc(state->mem_ctx, &sids[0]));
187
188         for (i=1; i<num_domains; i++)
189                 extra_data = talloc_asprintf(
190                         state->mem_ctx, "%s\n%s\\%s\\%s",
191                         extra_data, names[i],
192                         alt_names[i] ? alt_names[i] : names[i],
193                         sid_string_talloc(state->mem_ctx, &sids[i]));
194
195         /* add our primary domain */
196         
197         for (i=0; i<num_domains; i++) {
198                 if (strequal(names[i], domain->name)) {
199                         have_own_domain = True;
200                         break;
201                 }
202         }
203
204         if (state->request->data.list_all_domains && !have_own_domain) {
205                 extra_data = talloc_asprintf(
206                         state->mem_ctx, "%s\n%s\\%s\\%s",
207                         extra_data, domain->name,
208                         domain->alt_name ? domain->alt_name : domain->name,
209                         sid_string_talloc(state->mem_ctx, &domain->sid));
210         }
211
212         /* This is a bit excessive, but the extra data sooner or later will be
213            talloc'ed */
214
215         extra_data_len = 0;
216         if (extra_data != NULL) {
217                 extra_data_len = strlen(extra_data);
218         }
219
220         if (extra_data_len > 0) {
221                 state->response->extra_data.data = extra_data;
222                 state->response->length += extra_data_len+1;
223         }
224
225         return WINBINDD_OK;
226 }
227
228 /* This is the child-only version of --sequence. It only allows for a single
229  * domain (ie "our" one) to be displayed. */
230
231 enum winbindd_result winbindd_dual_show_sequence(struct winbindd_domain *domain,
232                                                  struct winbindd_cli_state *state)
233 {
234         DEBUG(3, ("[%5lu]: show sequence\n", (unsigned long)state->pid));
235
236         /* Ensure null termination */
237         state->request->domain_name[sizeof(state->request->domain_name)-1]='\0';
238
239         domain->methods->sequence_number(domain, &domain->sequence_number);
240
241         state->response->data.sequence_number =
242                 domain->sequence_number;
243
244         return WINBINDD_OK;
245 }
246
247 struct domain_info_state {
248         struct winbindd_domain *domain;
249         struct winbindd_cli_state *cli;
250         struct winbindd_request ping_request;
251 };
252
253 static void domain_info_done(struct tevent_req *req);
254
255 void winbindd_domain_info(struct winbindd_cli_state *cli)
256 {
257         struct domain_info_state *state;
258         struct winbindd_domain *domain;
259         struct tevent_req *req;
260
261         DEBUG(3, ("[%5lu]: domain_info [%s]\n", (unsigned long)cli->pid,
262                   cli->request->domain_name));
263
264         domain = find_domain_from_name_noinit(cli->request->domain_name);
265
266         if (domain == NULL) {
267                 DEBUG(3, ("Did not find domain [%s]\n",
268                           cli->request->domain_name));
269                 request_error(cli);
270                 return;
271         }
272
273         if (domain->initialized) {
274                 fstrcpy(cli->response->data.domain_info.name,
275                         domain->name);
276                 fstrcpy(cli->response->data.domain_info.alt_name,
277                         domain->alt_name);
278                 sid_to_fstring(cli->response->data.domain_info.sid,
279                                &domain->sid);
280                 cli->response->data.domain_info.native_mode =
281                         domain->native_mode;
282                 cli->response->data.domain_info.active_directory =
283                         domain->active_directory;
284                 cli->response->data.domain_info.primary =
285                         domain->primary;
286                 request_ok(cli);
287                 return;
288         }
289
290         state = talloc_zero(cli->mem_ctx, struct domain_info_state);
291         if (state == NULL) {
292                 DEBUG(0, ("talloc failed\n"));
293                 request_error(cli);
294                 return;
295         }
296
297         state->cli = cli;
298         state->domain = domain;
299         state->ping_request.cmd = WINBINDD_PING;
300
301         /*
302          * Send a ping down. This implicitly initializes the domain.
303          */
304
305         req = wb_domain_request_send(state, winbind_event_context(),
306                                      domain, &state->ping_request);
307         if (req == NULL) {
308                 DEBUG(3, ("wb_domain_request_send failed\n"));
309                 request_error(cli);
310                 return;
311         }
312         tevent_req_set_callback(req, domain_info_done, state);
313 }
314
315 static void domain_info_done(struct tevent_req *req)
316 {
317         struct domain_info_state *state = tevent_req_callback_data(
318                 req, struct domain_info_state);
319         struct winbindd_response *response;
320         int ret, err;
321
322         ret = wb_domain_request_recv(req, req, &response, &err);
323         TALLOC_FREE(req);
324         if (ret == -1) {
325                 DEBUG(10, ("wb_domain_request failed: %s\n", strerror(errno)));
326                 request_error(state->cli);
327                 return;
328         }
329         if (!state->domain->initialized) {
330                 DEBUG(5, ("wb_domain_request did not initialize domain %s\n",
331                           state->domain->name));
332                 request_error(state->cli);
333                 return;
334         }
335
336         fstrcpy(state->cli->response->data.domain_info.name,
337                 state->domain->name);
338         fstrcpy(state->cli->response->data.domain_info.alt_name,
339                 state->domain->alt_name);
340         sid_to_fstring(state->cli->response->data.domain_info.sid,
341                        &state->domain->sid);
342
343         state->cli->response->data.domain_info.native_mode =
344                 state->domain->native_mode;
345         state->cli->response->data.domain_info.active_directory =
346                 state->domain->active_directory;
347         state->cli->response->data.domain_info.primary =
348                 state->domain->primary;
349
350         request_ok(state->cli);
351 }
352
353 /* List various tidbits of information */
354
355 void winbindd_info(struct winbindd_cli_state *state)
356 {
357
358         DEBUG(3, ("[%5lu]: request misc info\n", (unsigned long)state->pid));
359
360         state->response->data.info.winbind_separator = *lp_winbind_separator();
361         fstrcpy(state->response->data.info.samba_version, samba_version_string());
362         request_ok(state);
363 }
364
365 /* Tell the client the current interface version */
366
367 void winbindd_interface_version(struct winbindd_cli_state *state)
368 {
369         DEBUG(3, ("[%5lu]: request interface version\n",
370                   (unsigned long)state->pid));
371         
372         state->response->data.interface_version = WINBIND_INTERFACE_VERSION;
373         request_ok(state);
374 }
375
376 /* What domain are we a member of? */
377
378 void winbindd_domain_name(struct winbindd_cli_state *state)
379 {
380         DEBUG(3, ("[%5lu]: request domain name\n", (unsigned long)state->pid));
381         
382         fstrcpy(state->response->data.domain_name, lp_workgroup());
383         request_ok(state);
384 }
385
386 /* What's my name again? */
387
388 void winbindd_netbios_name(struct winbindd_cli_state *state)
389 {
390         DEBUG(3, ("[%5lu]: request netbios name\n",
391                   (unsigned long)state->pid));
392         
393         fstrcpy(state->response->data.netbios_name, global_myname());
394         request_ok(state);
395 }
396
397 /* Where can I find the privileged pipe? */
398
399 void winbindd_priv_pipe_dir(struct winbindd_cli_state *state)
400 {
401         char *priv_dir;
402         DEBUG(3, ("[%5lu]: request location of privileged pipe\n",
403                   (unsigned long)state->pid));
404         
405         priv_dir = get_winbind_priv_pipe_dir();
406         state->response->extra_data.data = talloc_move(state->mem_ctx,
407                                                       &priv_dir);
408
409         /* must add one to length to copy the 0 for string termination */
410         state->response->length +=
411                 strlen((char *)state->response->extra_data.data) + 1;
412
413         request_ok(state);
414 }
415