pyldb: avoid segfault when adding an element with no name
[nivanova/samba-autobuild/.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 static char *get_trust_type_string(TALLOC_CTX *mem_ctx,
31                                    struct winbindd_tdc_domain *tdc,
32                                    struct winbindd_domain *domain)
33 {
34         enum netr_SchannelType secure_channel_type = SEC_CHAN_NULL;
35         char *s = NULL;
36
37         if (domain != NULL) {
38                 secure_channel_type = domain->secure_channel_type;
39         }
40
41         switch (secure_channel_type) {
42         case SEC_CHAN_NULL: {
43                 if (domain == NULL) {
44                         DBG_ERR("Missing domain [%s]\n",
45                                 tdc->domain_name);
46                         return NULL;
47                 }
48                 if (domain->routing_domain == NULL) {
49                         DBG_ERR("Missing routing for domain [%s]\n",
50                                 tdc->domain_name);
51                         return NULL;
52                 }
53                 s = talloc_asprintf(mem_ctx, "Routed (via %s)",
54                                     domain->routing_domain->name);
55                 if (s == NULL) {
56                         return NULL;
57                 }
58                 break;
59         }
60
61         case SEC_CHAN_LOCAL:
62                 s = talloc_strdup(mem_ctx, "Local");
63                 if (s == NULL) {
64                         return NULL;
65                 }
66                 break;
67
68         case SEC_CHAN_WKSTA:
69                 s = talloc_strdup(mem_ctx, "Workstation");
70                 if (s == NULL) {
71                         return NULL;
72                 }
73                 break;
74
75         case SEC_CHAN_BDC: {
76                 int role = lp_server_role();
77
78                 if (role == ROLE_DOMAIN_PDC) {
79                         s = talloc_strdup(mem_ctx, "PDC");
80                         if (s == NULL) {
81                                 return NULL;
82                         }
83                         break;
84                 }
85
86                 if (role == ROLE_DOMAIN_BDC) {
87                         s = talloc_strdup(mem_ctx, "BDC");
88                         if (s == NULL) {
89                                 return NULL;
90                         }
91                         break;
92                 }
93
94                 s = talloc_strdup(mem_ctx, "RWDC");
95                 if (s == NULL) {
96                         return NULL;
97                 }
98                 break;
99         }
100
101         case SEC_CHAN_RODC:
102                 s = talloc_strdup(mem_ctx, "RODC");
103                 if (s == NULL) {
104                         return NULL;
105                 }
106                 break;
107
108         case SEC_CHAN_DNS_DOMAIN:
109                 if (tdc->trust_attribs & LSA_TRUST_ATTRIBUTE_QUARANTINED_DOMAIN) {
110                         s = talloc_strdup(mem_ctx, "External");
111                         if (s == NULL) {
112                                 return NULL;
113                         }
114                         break;
115                 }
116                 if (tdc->trust_attribs & LSA_TRUST_ATTRIBUTE_WITHIN_FOREST) {
117                         s = talloc_strdup(mem_ctx, "In Forest");
118                         if (s == NULL) {
119                                 return NULL;
120                         }
121                         break;
122                 }
123                 if (tdc->trust_attribs & LSA_TRUST_ATTRIBUTE_TREAT_AS_EXTERNAL) {
124                         s = talloc_strdup(mem_ctx, "External");
125                         if (s == NULL) {
126                                 return NULL;
127                         }
128                         break;
129                 }
130                 if (tdc->trust_attribs & LSA_TRUST_ATTRIBUTE_FOREST_TRANSITIVE) {
131                         s = talloc_strdup(mem_ctx, "Forest");
132                         if (s == NULL) {
133                                 return NULL;
134                         }
135                         break;
136                 }
137                 s = talloc_strdup(mem_ctx, "External");
138                 if (s == NULL) {
139                         return NULL;
140                 }
141                 break;
142
143         case SEC_CHAN_DOMAIN:
144                 s = talloc_strdup(mem_ctx, "External");
145                 if (s == NULL) {
146                         return NULL;
147                 }
148                 break;
149
150         default:
151                 DBG_ERR("Unhandled secure_channel_type %d for domain[%s]\n",
152                         secure_channel_type, tdc->domain_name);
153                 return NULL;
154         }
155
156         return s;
157 }
158
159 static bool trust_is_inbound(struct winbindd_tdc_domain *domain)
160 {
161         if (domain->trust_flags & NETR_TRUST_FLAG_INBOUND) {
162                 return true;
163         }
164         return false;
165 }
166
167 static bool trust_is_outbound(struct winbindd_tdc_domain *domain)
168 {
169         if (domain->trust_flags & NETR_TRUST_FLAG_OUTBOUND) {
170                 return true;
171         }
172         return false;
173 }
174
175 static bool trust_is_transitive(struct winbindd_tdc_domain *domain)
176 {
177         bool transitive = false;
178
179         /*
180          * Beware: order matters
181          */
182
183         if (domain->trust_attribs & LSA_TRUST_ATTRIBUTE_WITHIN_FOREST) {
184                 transitive = true;
185         }
186
187         if (domain->trust_attribs & LSA_TRUST_ATTRIBUTE_FOREST_TRANSITIVE) {
188                 transitive = true;
189         }
190
191         if (domain->trust_attribs & LSA_TRUST_ATTRIBUTE_NON_TRANSITIVE) {
192                 transitive = false;
193         }
194
195         if (domain->trust_attribs & LSA_TRUST_ATTRIBUTE_QUARANTINED_DOMAIN) {
196                 transitive = false;
197         }
198
199         if (domain->trust_flags & NETR_TRUST_FLAG_PRIMARY) {
200                 transitive = true;
201         }
202
203         return transitive;
204 }
205
206 bool winbindd_list_trusted_domains(struct winbindd_cli_state *state)
207 {
208         struct winbindd_tdc_domain *dom_list = NULL;
209         size_t num_domains = 0;
210         int extra_data_len = 0;
211         char *extra_data = NULL;
212         size_t i = 0;
213         bool ret = false;
214
215         DBG_NOTICE("[%s (%u)]: list trusted domains\n",
216                    state->client_name,
217                    (unsigned int)state->pid);
218
219         if( !wcache_tdc_fetch_list( &dom_list, &num_domains )) {
220                 goto done;
221         }
222
223         extra_data = talloc_strdup(state->mem_ctx, "");
224         if (extra_data == NULL) {
225                 goto done;
226         }
227
228         for ( i = 0; i < num_domains; i++ ) {
229                 struct winbindd_domain *domain;
230                 bool is_online = true;          
231                 struct winbindd_tdc_domain *d = NULL;
232                 char *trust_type = NULL;
233                 struct dom_sid_buf buf;
234
235                 d = &dom_list[i];
236                 domain = find_domain_from_name_noinit(d->domain_name);
237                 if (domain) {
238                         is_online = domain->online;
239                 }
240
241                 trust_type = get_trust_type_string(talloc_tos(), d, domain);
242                 if (trust_type == NULL) {
243                         continue;
244                 }
245
246                 extra_data = talloc_asprintf_append_buffer(
247                         extra_data,
248                         "%s\\%s\\%s\\%s\\%s\\%s\\%s\\%s\n",
249                         d->domain_name,
250                         d->dns_name ? d->dns_name : "",
251                         dom_sid_str_buf(&d->sid, &buf),
252                         trust_type,
253                         trust_is_transitive(d) ? "Yes" : "No",
254                         trust_is_inbound(d) ? "Yes" : "No",
255                         trust_is_outbound(d) ? "Yes" : "No",
256                         is_online ? "Online" : "Offline" );
257
258                 TALLOC_FREE(trust_type);
259         }
260
261         state->response->data.num_entries = num_domains;
262
263         extra_data_len = strlen(extra_data);
264         if (extra_data_len > 0) {
265
266                 /* Strip the last \n */
267                 extra_data[extra_data_len-1] = '\0';
268
269                 state->response->extra_data.data = extra_data;
270                 state->response->length += extra_data_len;
271         }
272
273         ret = true;
274 done:
275         TALLOC_FREE( dom_list );
276         return ret;
277 }
278
279 enum winbindd_result winbindd_dual_list_trusted_domains(struct winbindd_domain *domain,
280                                                         struct winbindd_cli_state *state)
281 {
282         uint32_t i;
283         int extra_data_len = 0;
284         char *extra_data;
285         NTSTATUS result;
286         bool have_own_domain = False;
287         struct netr_DomainTrustList trusts;
288
289         DBG_NOTICE("[%s %u]: list trusted domains\n",
290                    state->client_name,
291                    (unsigned int)state->pid);
292
293         result = wb_cache_trusted_domains(domain, state->mem_ctx, &trusts);
294
295         if (!NT_STATUS_IS_OK(result)) {
296                 DEBUG(3, ("winbindd_dual_list_trusted_domains: trusted_domains returned %s\n",
297                         nt_errstr(result) ));
298                 return WINBINDD_ERROR;
299         }
300
301         extra_data = talloc_strdup(state->mem_ctx, "");
302
303         for (i=0; i<trusts.count; i++) {
304                 struct dom_sid_buf buf;
305
306                 if (trusts.array[i].sid == NULL) {
307                         continue;
308                 }
309                 if (dom_sid_equal(trusts.array[i].sid, &global_sid_NULL)) {
310                         continue;
311                 }
312
313                 extra_data = talloc_asprintf_append_buffer(
314                     extra_data, "%s\\%s\\%s\\%u\\%u\\%u\n",
315                     trusts.array[i].netbios_name, trusts.array[i].dns_name,
316                     dom_sid_str_buf(trusts.array[i].sid, &buf),
317                     trusts.array[i].trust_flags,
318                     (uint32_t)trusts.array[i].trust_type,
319                     trusts.array[i].trust_attributes);
320         }
321
322         /* add our primary domain */
323
324         for (i=0; i<trusts.count; i++) {
325                 if (strequal(trusts.array[i].netbios_name, domain->name)) {
326                         have_own_domain = True;
327                         break;
328                 }
329         }
330
331         if (state->request->data.list_all_domains && !have_own_domain) {
332                 struct dom_sid_buf buf;
333                 extra_data = talloc_asprintf_append_buffer(
334                         extra_data, "%s\\%s\\%s\n", domain->name,
335                         domain->alt_name != NULL ?
336                                 domain->alt_name :
337                                 domain->name,
338                         dom_sid_str_buf(&domain->sid, &buf));
339         }
340
341         extra_data_len = strlen(extra_data);
342         if (extra_data_len > 0) {
343
344                 /* Strip the last \n */
345                 extra_data[extra_data_len-1] = '\0';
346
347                 state->response->extra_data.data = extra_data;
348                 state->response->length += extra_data_len;
349         }
350
351         return WINBINDD_OK;
352 }
353
354 bool winbindd_dc_info(struct winbindd_cli_state *cli)
355 {
356         struct winbindd_domain *domain;
357         char *dc_name, *dc_ip;
358
359         cli->request->domain_name[sizeof(cli->request->domain_name)-1] = '\0';
360
361         DBG_NOTICE("[%s (%u)]: domain_info [%s]\n",
362                    cli->client_name,
363                    (unsigned int)cli->pid,
364                    cli->request->domain_name);
365
366         if (cli->request->domain_name[0] != '\0') {
367                 domain = find_trust_from_name_noinit(
368                         cli->request->domain_name);
369                 if (domain == NULL) {
370                         DEBUG(10, ("Could not find domain %s\n",
371                                    cli->request->domain_name));
372                         return false;
373                 }
374         } else {
375                 domain = find_our_domain();
376         }
377
378         if (!fetch_current_dc_from_gencache(
379                     talloc_tos(), domain->name, &dc_name, &dc_ip)) {
380                 DEBUG(10, ("fetch_current_dc_from_gencache(%s) failed\n",
381                            domain->name));
382                 return false;
383         }
384
385         cli->response->data.num_entries = 1;
386         cli->response->extra_data.data = talloc_asprintf(
387                 cli->mem_ctx, "%s\n%s\n", dc_name, dc_ip);
388
389         TALLOC_FREE(dc_name);
390         TALLOC_FREE(dc_ip);
391
392         if (cli->response->extra_data.data == NULL) {
393                 return false;
394         }
395
396         /* must add one to length to copy the 0 for string termination */
397         cli->response->length +=
398                 strlen((char *)cli->response->extra_data.data) + 1;
399
400         return true;
401 }
402
403 bool winbindd_ping(struct winbindd_cli_state *state)
404 {
405         DBG_NOTICE("[%s (%u)]: ping\n",
406                    state->client_name,
407                    (unsigned int)state->pid);
408         return true;
409 }
410
411 /* List various tidbits of information */
412
413 bool winbindd_info(struct winbindd_cli_state *state)
414 {
415
416         DBG_NOTICE("[%s (%u)]: request misc info\n",
417                    state->client_name,
418                    (unsigned int)state->pid);
419
420         state->response->data.info.winbind_separator = *lp_winbind_separator();
421         fstrcpy(state->response->data.info.samba_version, samba_version_string());
422         return true;
423 }
424
425 /* Tell the client the current interface version */
426
427 bool winbindd_interface_version(struct winbindd_cli_state *state)
428 {
429         DBG_NOTICE("[%s (%u)]: request interface version (version = %d)\n",
430                    state->client_name,
431                    (unsigned int)state->pid,
432                    WINBIND_INTERFACE_VERSION);
433
434         state->response->data.interface_version = WINBIND_INTERFACE_VERSION;
435         return true;
436 }
437
438 /* What domain are we a member of? */
439
440 bool winbindd_domain_name(struct winbindd_cli_state *state)
441 {
442         DBG_NOTICE("[%s (%u)]: request domain name\n",
443                    state->client_name,
444                    (unsigned int)state->pid);
445
446         fstrcpy(state->response->data.domain_name, lp_workgroup());
447         return true;
448 }
449
450 /* What's my name again? */
451
452 bool winbindd_netbios_name(struct winbindd_cli_state *state)
453 {
454         DBG_NOTICE("[%s (%u)]: request netbios name\n",
455                    state->client_name,
456                    (unsigned int)state->pid);
457
458         fstrcpy(state->response->data.netbios_name, lp_netbios_name());
459         return true;
460 }
461
462 /* Where can I find the privileged pipe? */
463
464 bool winbindd_priv_pipe_dir(struct winbindd_cli_state *state)
465 {
466         char *priv_dir;
467
468         DBG_NOTICE("[%s (%u)]: request location of privileged pipe\n",
469                    state->client_name,
470                    (unsigned int)state->pid);
471
472         priv_dir = get_winbind_priv_pipe_dir();
473         state->response->extra_data.data = talloc_move(state->mem_ctx,
474                                                       &priv_dir);
475
476         /* must add one to length to copy the 0 for string termination */
477         state->response->length +=
478                 strlen((char *)state->response->extra_data.data) + 1;
479
480         return true;
481 }