s3: Next step to fix MIT trusts
[obnox/samba-ctdb.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 "libcli/security/dom_sid.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         extra_data = talloc_strdup(state->mem_ctx, "");
106         if (extra_data == NULL) {
107                 request_error(state);
108                 goto done;
109         }
110
111         for ( i = 0; i < num_domains; i++ ) {
112                 struct winbindd_domain *domain;
113                 bool is_online = true;          
114
115                 d = &dom_list[i];
116                 domain = find_domain_from_name_noinit(d->domain_name);
117                 if (domain) {
118                         is_online = domain->online;
119                 }
120                 extra_data = talloc_asprintf_append_buffer(
121                         extra_data,
122                         "%s\\%s\\%s\\%s\\%s\\%s\\%s\\%s\n",
123                         d->domain_name,
124                         d->dns_name ? d->dns_name : d->domain_name,
125                         sid_string_talloc(state->mem_ctx, &d->sid),
126                         get_trust_type_string(d),
127                         trust_is_transitive(d) ? "Yes" : "No",
128                         trust_is_inbound(d) ? "Yes" : "No",
129                         trust_is_outbound(d) ? "Yes" : "No",
130                         is_online ? "Online" : "Offline" );
131         }
132
133         state->response->data.num_entries = num_domains;
134
135         extra_data_len = strlen(extra_data);
136         if (extra_data_len > 0) {
137
138                 /* Strip the last \n */
139                 extra_data[extra_data_len-1] = '\0';
140
141                 state->response->extra_data.data = extra_data;
142                 state->response->length += extra_data_len;
143         }
144
145         request_ok(state);      
146 done:
147         TALLOC_FREE( dom_list );
148 }
149
150 enum winbindd_result winbindd_dual_list_trusted_domains(struct winbindd_domain *domain,
151                                                         struct winbindd_cli_state *state)
152 {
153         int i;
154         int extra_data_len = 0;
155         char *extra_data;
156         NTSTATUS result;
157         bool have_own_domain = False;
158         struct netr_DomainTrustList trusts;
159
160         DEBUG(3, ("[%5lu]: list trusted domains\n",
161                   (unsigned long)state->pid));
162
163         result = domain->methods->trusted_domains(domain, state->mem_ctx,
164                                                   &trusts);
165
166         if (!NT_STATUS_IS_OK(result)) {
167                 DEBUG(3, ("winbindd_dual_list_trusted_domains: trusted_domains returned %s\n",
168                         nt_errstr(result) ));
169                 return WINBINDD_ERROR;
170         }
171
172         extra_data = talloc_strdup(state->mem_ctx, "");
173
174         for (i=0; i<trusts.count; i++) {
175
176                 if (trusts.array[i].sid == NULL) {
177                         continue;
178                 }
179                 if (dom_sid_equal(trusts.array[i].sid, &global_sid_NULL)) {
180                         continue;
181                 }
182
183                 extra_data = talloc_asprintf_append_buffer(
184                         extra_data, "%s\\%s\\%s\n",
185                         trusts.array[i].netbios_name,
186                         trusts.array[i].dns_name,
187                         sid_string_talloc(state->mem_ctx,
188                                           trusts.array[i].sid));
189         }
190
191         /* add our primary domain */
192
193         for (i=0; i<trusts.count; i++) {
194                 if (strequal(trusts.array[i].netbios_name, domain->name)) {
195                         have_own_domain = True;
196                         break;
197                 }
198         }
199
200         if (state->request->data.list_all_domains && !have_own_domain) {
201                 extra_data = talloc_asprintf_append_buffer(
202                         extra_data, "%s\\%s\\%s\n", domain->name,
203                         domain->alt_name ? domain->alt_name : domain->name,
204                         sid_string_talloc(state->mem_ctx, &domain->sid));
205         }
206
207         extra_data_len = strlen(extra_data);
208         if (extra_data_len > 0) {
209
210                 /* Strip the last \n */
211                 extra_data[extra_data_len-1] = '\0';
212
213                 state->response->extra_data.data = extra_data;
214                 state->response->length += extra_data_len+1;
215         }
216
217         return WINBINDD_OK;
218 }
219
220 struct domain_info_state {
221         struct winbindd_domain *domain;
222         struct winbindd_cli_state *cli;
223         struct winbindd_request ping_request;
224 };
225
226 static void domain_info_done(struct tevent_req *req);
227
228 void winbindd_domain_info(struct winbindd_cli_state *cli)
229 {
230         struct domain_info_state *state;
231         struct winbindd_domain *domain;
232         struct tevent_req *req;
233
234         DEBUG(3, ("[%5lu]: domain_info [%s]\n", (unsigned long)cli->pid,
235                   cli->request->domain_name));
236
237         domain = find_domain_from_name_noinit(cli->request->domain_name);
238
239         if (domain == NULL) {
240                 DEBUG(3, ("Did not find domain [%s]\n",
241                           cli->request->domain_name));
242                 request_error(cli);
243                 return;
244         }
245
246         if (domain->initialized) {
247                 fstrcpy(cli->response->data.domain_info.name,
248                         domain->name);
249                 fstrcpy(cli->response->data.domain_info.alt_name,
250                         domain->alt_name);
251                 sid_to_fstring(cli->response->data.domain_info.sid,
252                                &domain->sid);
253                 cli->response->data.domain_info.native_mode =
254                         domain->native_mode;
255                 cli->response->data.domain_info.active_directory =
256                         domain->active_directory;
257                 cli->response->data.domain_info.primary =
258                         domain->primary;
259                 request_ok(cli);
260                 return;
261         }
262
263         state = talloc_zero(cli->mem_ctx, struct domain_info_state);
264         if (state == NULL) {
265                 DEBUG(0, ("talloc failed\n"));
266                 request_error(cli);
267                 return;
268         }
269
270         state->cli = cli;
271         state->domain = domain;
272         state->ping_request.cmd = WINBINDD_PING;
273
274         /*
275          * Send a ping down. This implicitly initializes the domain.
276          */
277
278         req = wb_domain_request_send(state, winbind_event_context(),
279                                      domain, &state->ping_request);
280         if (req == NULL) {
281                 DEBUG(3, ("wb_domain_request_send failed\n"));
282                 request_error(cli);
283                 return;
284         }
285         tevent_req_set_callback(req, domain_info_done, state);
286 }
287
288 static void domain_info_done(struct tevent_req *req)
289 {
290         struct domain_info_state *state = tevent_req_callback_data(
291                 req, struct domain_info_state);
292         struct winbindd_response *response;
293         int ret, err;
294
295         ret = wb_domain_request_recv(req, req, &response, &err);
296         TALLOC_FREE(req);
297         if (ret == -1) {
298                 DEBUG(10, ("wb_domain_request failed: %s\n", strerror(errno)));
299                 request_error(state->cli);
300                 return;
301         }
302         if (!state->domain->initialized) {
303                 DEBUG(5, ("wb_domain_request did not initialize domain %s\n",
304                           state->domain->name));
305                 request_error(state->cli);
306                 return;
307         }
308
309         fstrcpy(state->cli->response->data.domain_info.name,
310                 state->domain->name);
311         fstrcpy(state->cli->response->data.domain_info.alt_name,
312                 state->domain->alt_name);
313         sid_to_fstring(state->cli->response->data.domain_info.sid,
314                        &state->domain->sid);
315
316         state->cli->response->data.domain_info.native_mode =
317                 state->domain->native_mode;
318         state->cli->response->data.domain_info.active_directory =
319                 state->domain->active_directory;
320         state->cli->response->data.domain_info.primary =
321                 state->domain->primary;
322
323         request_ok(state->cli);
324 }
325
326 void winbindd_dc_info(struct winbindd_cli_state *cli)
327 {
328         struct winbindd_domain *domain;
329         char *dc_name, *dc_ip;
330
331         cli->request->domain_name[sizeof(cli->request->domain_name)-1] = '\0';
332
333         DEBUG(3, ("[%5lu]: domain_info [%s]\n", (unsigned long)cli->pid,
334                   cli->request->domain_name));
335
336         if (cli->request->domain_name[0] != '\0') {
337                 domain = find_domain_from_name_noinit(
338                         cli->request->domain_name);
339                 DEBUG(10, ("Could not find domain %s\n",
340                            cli->request->domain_name));
341                 if (domain == NULL) {
342                         request_error(cli);
343                         return;
344                 }
345         } else {
346                 domain = find_our_domain();
347         }
348
349         if (!fetch_current_dc_from_gencache(
350                     talloc_tos(), domain->name, &dc_name, &dc_ip)) {
351                 DEBUG(10, ("fetch_current_dc_from_gencache(%s) failed\n",
352                            domain->name));
353                 request_error(cli);
354                 return;
355         }
356
357         cli->response->data.num_entries = 1;
358         cli->response->extra_data.data = talloc_asprintf(
359                 cli->mem_ctx, "%s\n%s\n", dc_name, dc_ip);
360
361         TALLOC_FREE(dc_name);
362         TALLOC_FREE(dc_ip);
363
364         if (cli->response->extra_data.data == NULL) {
365                 request_error(cli);
366                 return;
367         }
368
369         /* must add one to length to copy the 0 for string termination */
370         cli->response->length +=
371                 strlen((char *)cli->response->extra_data.data) + 1;
372
373         request_ok(cli);
374 }
375
376 /* List various tidbits of information */
377
378 void winbindd_info(struct winbindd_cli_state *state)
379 {
380
381         DEBUG(3, ("[%5lu]: request misc info\n", (unsigned long)state->pid));
382
383         state->response->data.info.winbind_separator = *lp_winbind_separator();
384         fstrcpy(state->response->data.info.samba_version, samba_version_string());
385         request_ok(state);
386 }
387
388 /* Tell the client the current interface version */
389
390 void winbindd_interface_version(struct winbindd_cli_state *state)
391 {
392         DEBUG(3, ("[%5lu]: request interface version\n",
393                   (unsigned long)state->pid));
394
395         state->response->data.interface_version = WINBIND_INTERFACE_VERSION;
396         request_ok(state);
397 }
398
399 /* What domain are we a member of? */
400
401 void winbindd_domain_name(struct winbindd_cli_state *state)
402 {
403         DEBUG(3, ("[%5lu]: request domain name\n", (unsigned long)state->pid));
404
405         fstrcpy(state->response->data.domain_name, lp_workgroup());
406         request_ok(state);
407 }
408
409 /* What's my name again? */
410
411 void winbindd_netbios_name(struct winbindd_cli_state *state)
412 {
413         DEBUG(3, ("[%5lu]: request netbios name\n",
414                   (unsigned long)state->pid));
415
416         fstrcpy(state->response->data.netbios_name, global_myname());
417         request_ok(state);
418 }
419
420 /* Where can I find the privileged pipe? */
421
422 void winbindd_priv_pipe_dir(struct winbindd_cli_state *state)
423 {
424         char *priv_dir;
425         DEBUG(3, ("[%5lu]: request location of privileged pipe\n",
426                   (unsigned long)state->pid));
427
428         priv_dir = get_winbind_priv_pipe_dir();
429         state->response->extra_data.data = talloc_move(state->mem_ctx,
430                                                       &priv_dir);
431
432         /* must add one to length to copy the 0 for string termination */
433         state->response->length +=
434                 strlen((char *)state->response->extra_data.data) + 1;
435
436         request_ok(state);
437 }
438