2 Unix SMB/CIFS implementation.
4 Winbind daemon for ntdom nss module
6 Copyright (C) Tim Potter 2000-2001
7 Copyright (C) 2001 by Martin Pool <mbp@samba.org>
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.
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.
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/>.
25 #include "lib/util_unixsids.h"
27 #include "../libcli/security/security.h"
28 #include "../libcli/auth/pam_errors.h"
29 #include "passdb/machine_sid.h"
31 #include "source4/lib/messaging/messaging.h"
32 #include "librpc/gen_ndr/ndr_lsa.h"
33 #include "librpc/gen_ndr/ndr_drsblobs.h"
34 #include "auth/credentials/credentials.h"
35 #include "libsmb/samlogon_cache.h"
38 #define DBGC_CLASS DBGC_WINBIND
41 * @file winbindd_util.c
43 * Winbind daemon for NT domain authentication nss module.
47 /* The list of trusted domains. Note that the list can be deleted and
48 recreated using the init_domain_list() function so pointers to
49 individual winbindd_domain structures cannot be made. Keep a copy of
50 the domain name instead. */
52 static struct winbindd_domain *_domain_list = NULL;
54 struct winbindd_domain *domain_list(void)
58 if ((!_domain_list) && (!init_domain_list())) {
59 smb_panic("Init_domain_list failed");
65 /* Free all entries in the trusted domain list */
67 static void free_domain_list(void)
69 struct winbindd_domain *domain = _domain_list;
72 struct winbindd_domain *next = domain->next;
74 DLIST_REMOVE(_domain_list, domain);
81 * Iterator for winbindd's domain list.
82 * To be used (e.g.) in tevent based loops.
84 struct winbindd_domain *wb_next_domain(struct winbindd_domain *domain)
87 domain = domain_list();
89 domain = domain->next;
92 if ((domain != NULL) &&
93 (lp_server_role() != ROLE_ACTIVE_DIRECTORY_DC) &&
94 sid_check_is_our_sam(&domain->sid))
96 domain = domain->next;
102 static bool is_internal_domain(const struct dom_sid *sid)
107 return (sid_check_is_our_sam(sid) || sid_check_is_builtin(sid));
110 static bool is_in_internal_domain(const struct dom_sid *sid)
115 return (sid_check_is_in_our_sam(sid) || sid_check_is_in_builtin(sid));
119 /* Add a trusted domain to our list of domains.
120 If the domain already exists in the list,
121 return it and don't re-initialize. */
123 static NTSTATUS add_trusted_domain(const char *domain_name,
124 const char *dns_name,
125 const struct dom_sid *sid,
127 uint32_t trust_flags,
128 uint32_t trust_attribs,
129 enum netr_SchannelType secure_channel_type,
130 struct winbindd_domain *routing_domain,
131 struct winbindd_domain **_d)
133 struct winbindd_domain *domain = NULL;
134 const char **ignored_domains = NULL;
135 const char **dom = NULL;
136 int role = lp_server_role();
138 if (is_null_sid(sid)) {
139 DBG_ERR("Got null SID for domain [%s]\n", domain_name);
140 return NT_STATUS_INVALID_PARAMETER;
143 ignored_domains = lp_parm_string_list(-1, "winbind", "ignore domains", NULL);
144 for (dom=ignored_domains; dom && *dom; dom++) {
145 if (gen_fnmatch(*dom, domain_name) == 0) {
146 DEBUG(2,("Ignoring domain '%s'\n", domain_name));
147 return NT_STATUS_NO_SUCH_DOMAIN;
152 * We can't call domain_list() as this function is called from
153 * init_domain_list() and we'll get stuck in a loop.
155 for (domain = _domain_list; domain; domain = domain->next) {
156 if (strequal(domain_name, domain->name)) {
161 if (domain != NULL) {
162 struct winbindd_domain *check_domain = NULL;
164 for (check_domain = _domain_list;
165 check_domain != NULL;
166 check_domain = check_domain->next)
168 if (check_domain == domain) {
172 if (dom_sid_equal(&check_domain->sid, sid)) {
177 if (check_domain != NULL) {
178 DBG_ERR("SID [%s] already used by domain [%s], "
180 sid_string_dbg(sid), check_domain->name,
182 return NT_STATUS_INVALID_PARAMETER;
186 if ((domain != NULL) && (dns_name != NULL)) {
187 struct winbindd_domain *check_domain = NULL;
189 for (check_domain = _domain_list;
190 check_domain != NULL;
191 check_domain = check_domain->next)
193 if (check_domain == domain) {
197 if (strequal(check_domain->alt_name, dns_name)) {
202 if (check_domain != NULL) {
203 DBG_ERR("DNS name [%s] used by domain [%s], "
205 dns_name, check_domain->name,
207 return NT_STATUS_INVALID_PARAMETER;
211 if (domain != NULL) {
216 /* Create new domain entry */
217 domain = talloc_zero(NULL, struct winbindd_domain);
218 if (domain == NULL) {
219 return NT_STATUS_NO_MEMORY;
222 domain->children = talloc_zero_array(domain,
223 struct winbindd_child,
224 lp_winbind_max_domain_connections());
225 if (domain->children == NULL) {
227 return NT_STATUS_NO_MEMORY;
230 domain->name = talloc_strdup(domain, domain_name);
231 if (domain->name == NULL) {
233 return NT_STATUS_NO_MEMORY;
236 if (dns_name != NULL) {
237 domain->alt_name = talloc_strdup(domain, dns_name);
238 if (domain->alt_name == NULL) {
240 return NT_STATUS_NO_MEMORY;
244 domain->backend = NULL;
245 domain->internal = is_internal_domain(sid);
246 domain->secure_channel_type = secure_channel_type;
247 domain->sequence_number = DOM_SEQUENCE_NONE;
248 domain->last_seq_check = 0;
249 domain->initialized = false;
250 domain->online = is_internal_domain(sid);
251 domain->check_online_timeout = 0;
252 domain->dc_probe_pid = (pid_t)-1;
253 domain->domain_flags = trust_flags;
254 domain->domain_type = trust_type;
255 domain->domain_trust_attribs = trust_attribs;
256 domain->secure_channel_type = secure_channel_type;
257 domain->routing_domain = routing_domain;
258 sid_copy(&domain->sid, sid);
260 /* Is this our primary domain ? */
261 if (role == ROLE_DOMAIN_MEMBER) {
262 domain->primary = strequal(domain_name, lp_workgroup());
264 domain->primary = strequal(domain_name, get_global_sam_name());
267 if (domain->primary) {
268 if (role == ROLE_ACTIVE_DIRECTORY_DC) {
269 domain->active_directory = true;
271 if (lp_security() == SEC_ADS) {
272 domain->active_directory = true;
274 } else if (!domain->internal) {
275 if (domain->domain_type == LSA_TRUST_TYPE_UPLEVEL) {
276 domain->active_directory = true;
280 domain->can_do_ncacn_ip_tcp = domain->active_directory;
282 /* Link to domain list */
283 DLIST_ADD_END(_domain_list, domain);
285 wcache_tdc_add_domain( domain );
287 setup_domain_child(domain);
289 DBG_NOTICE("Added domain [%s] [%s] [%s]\n",
290 domain->name, domain->alt_name,
291 sid_string_dbg(&domain->sid));
297 bool set_routing_domain(struct winbindd_domain *domain,
298 struct winbindd_domain *routing_domain)
300 if (domain->routing_domain == NULL) {
301 domain->routing_domain = routing_domain;
304 if (domain->routing_domain != routing_domain) {
310 bool add_trusted_domain_from_auth(uint16_t validation_level,
311 struct info3_text *info3,
312 struct info6_text *info6)
314 struct winbindd_domain *domain = NULL;
315 struct dom_sid domain_sid;
316 const char *dns_domainname = NULL;
321 * We got a successfull auth from a domain that might not yet be in our
322 * domain list. If we're a member we trust our DC who authenticated the
323 * user from that domain and add the domain to our list on-the-fly. If
324 * we're a DC we rely on configured trusts and don't add on-the-fly.
331 ok = dom_sid_parse(info3->dom_sid, &domain_sid);
333 DBG_NOTICE("dom_sid_parse [%s] failed\n", info3->dom_sid);
337 if (validation_level == 6) {
338 dns_domainname = &info6->dns_domainname[0];
341 status = add_trusted_domain(info3->logon_dom,
345 NETR_TRUST_FLAG_OUTBOUND,
348 find_default_route_domain(),
350 if (!NT_STATUS_IS_OK(status) &&
351 !NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_DOMAIN))
353 DBG_DEBUG("Adding domain [%s] with sid [%s] failed\n",
354 info3->logon_dom, info3->dom_sid);
361 bool domain_is_forest_root(const struct winbindd_domain *domain)
363 const uint32_t fr_flags =
364 (NETR_TRUST_FLAG_TREEROOT|NETR_TRUST_FLAG_IN_FOREST);
366 return ((domain->domain_flags & fr_flags) == fr_flags);
369 /********************************************************************
370 rescan our domains looking for new trusted domains
371 ********************************************************************/
373 struct trustdom_state {
374 struct winbindd_domain *domain;
375 struct winbindd_request request;
378 static void trustdom_list_done(struct tevent_req *req);
379 static void rescan_forest_root_trusts( void );
380 static void rescan_forest_trusts( void );
382 static void add_trusted_domains( struct winbindd_domain *domain )
384 struct trustdom_state *state;
385 struct tevent_req *req;
387 state = talloc_zero(NULL, struct trustdom_state);
389 DEBUG(0, ("talloc failed\n"));
392 state->domain = domain;
394 state->request.length = sizeof(state->request);
395 state->request.cmd = WINBINDD_LIST_TRUSTDOM;
397 req = wb_domain_request_send(state, server_event_context(),
398 domain, &state->request);
400 DEBUG(1, ("wb_domain_request_send failed\n"));
404 tevent_req_set_callback(req, trustdom_list_done, state);
407 static void trustdom_list_done(struct tevent_req *req)
409 struct trustdom_state *state = tevent_req_callback_data(
410 req, struct trustdom_state);
411 struct winbindd_response *response;
415 bool within_forest = false;
419 * Only when we enumerate our primary domain
420 * or our forest root domain, we should keep
421 * the NETR_TRUST_FLAG_IN_FOREST flag, in
422 * all other cases we need to clear it as the domain
423 * is not part of our forest.
425 if (state->domain->primary) {
426 within_forest = true;
427 } else if (domain_is_forest_root(state->domain)) {
428 within_forest = true;
431 res = wb_domain_request_recv(req, state, &response, &err);
432 if ((res == -1) || (response->result != WINBINDD_OK)) {
433 DBG_WARNING("Could not receive trusts for domain %s\n",
434 state->domain->name);
439 if (response->length < sizeof(struct winbindd_response)) {
440 DBG_ERR("ill-formed trustdom response - short length\n");
445 extra_len = response->length - sizeof(struct winbindd_response);
447 p = (char *)response->extra_data.data;
449 while ((p - (char *)response->extra_data.data) < extra_len) {
450 struct winbindd_domain *domain = NULL;
451 char *name, *q, *sidstr, *alt_name;
454 uint32_t trust_attribs;
455 uint32_t trust_flags;
457 DBG_DEBUG("parsing response line '%s'\n", p);
461 alt_name = strchr(p, '\\');
462 if (alt_name == NULL) {
463 DBG_ERR("Got invalid trustdom response\n");
470 sidstr = strchr(alt_name, '\\');
471 if (sidstr == NULL) {
472 DBG_ERR("Got invalid trustdom response\n");
479 /* use the real alt_name if we have one, else pass in NULL */
480 if (strequal(alt_name, "(null)")) {
484 q = strtok(sidstr, "\\");
486 DBG_ERR("Got invalid trustdom response\n");
490 if (!string_to_sid(&sid, sidstr)) {
491 DEBUG(0, ("Got invalid trustdom response\n"));
495 q = strtok(NULL, "\\");
497 DBG_ERR("Got invalid trustdom response\n");
501 trust_flags = (uint32_t)strtoul(q, NULL, 10);
503 q = strtok(NULL, "\\");
505 DBG_ERR("Got invalid trustdom response\n");
509 trust_type = (uint32_t)strtoul(q, NULL, 10);
511 q = strtok(NULL, "\n");
513 DBG_ERR("Got invalid trustdom response\n");
517 trust_attribs = (uint32_t)strtoul(q, NULL, 10);
519 if (!within_forest) {
520 trust_flags &= ~NETR_TRUST_FLAG_IN_FOREST;
523 if (!state->domain->primary) {
524 trust_flags &= ~NETR_TRUST_FLAG_PRIMARY;
528 * We always call add_trusted_domain() cause on an existing
529 * domain structure, it will update the SID if necessary.
530 * This is important because we need the SID for sibling
533 status = add_trusted_domain(name,
540 find_default_route_domain(),
542 if (!NT_STATUS_IS_OK(status) &&
543 !NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_DOMAIN))
545 DBG_NOTICE("add_trusted_domain returned %s\n",
550 p = q + strlen(q) + 1;
554 Cases to consider when scanning trusts:
555 (a) we are calling from a child domain (primary && !forest_root)
556 (b) we are calling from the root of the forest (primary && forest_root)
557 (c) we are calling from a trusted forest domain (!primary
561 if (state->domain->primary) {
562 /* If this is our primary domain and we are not in the
563 forest root, we have to scan the root trusts first */
565 if (!domain_is_forest_root(state->domain))
566 rescan_forest_root_trusts();
568 rescan_forest_trusts();
570 } else if (domain_is_forest_root(state->domain)) {
571 /* Once we have done root forest trust search, we can
572 go on to search the trusted forests */
574 rescan_forest_trusts();
582 /********************************************************************
583 Scan the trusts of our forest root
584 ********************************************************************/
586 static void rescan_forest_root_trusts( void )
588 struct winbindd_tdc_domain *dom_list = NULL;
589 size_t num_trusts = 0;
593 /* The only transitive trusts supported by Windows 2003 AD are
594 (a) Parent-Child, (b) Tree-Root, and (c) Forest. The
595 first two are handled in forest and listed by
596 DsEnumerateDomainTrusts(). Forest trusts are not so we
597 have to do that ourselves. */
599 if ( !wcache_tdc_fetch_list( &dom_list, &num_trusts ) )
602 for ( i=0; i<num_trusts; i++ ) {
603 struct winbindd_domain *d = NULL;
605 /* Find the forest root. Don't necessarily trust
606 the domain_list() as our primary domain may not
607 have been initialized. */
609 if ( !(dom_list[i].trust_flags & NETR_TRUST_FLAG_TREEROOT) ) {
613 /* Here's the forest root */
615 d = find_domain_from_name_noinit( dom_list[i].domain_name );
617 status = add_trusted_domain(dom_list[i].domain_name,
618 dom_list[i].dns_name,
620 dom_list[i].trust_type,
621 dom_list[i].trust_flags,
622 dom_list[i].trust_attribs,
624 find_default_route_domain(),
627 if (!NT_STATUS_IS_OK(status) &&
628 NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_DOMAIN))
630 DBG_ERR("add_trusted_domain returned %s\n",
639 DEBUG(10,("rescan_forest_root_trusts: Following trust path "
640 "for domain tree root %s (%s)\n",
641 d->name, d->alt_name ));
643 d->domain_flags = dom_list[i].trust_flags;
644 d->domain_type = dom_list[i].trust_type;
645 d->domain_trust_attribs = dom_list[i].trust_attribs;
647 add_trusted_domains( d );
652 TALLOC_FREE( dom_list );
657 /********************************************************************
658 scan the transitive forest trusts (not our own)
659 ********************************************************************/
662 static void rescan_forest_trusts( void )
664 struct winbindd_domain *d = NULL;
665 struct winbindd_tdc_domain *dom_list = NULL;
666 size_t num_trusts = 0;
670 /* The only transitive trusts supported by Windows 2003 AD are
671 (a) Parent-Child, (b) Tree-Root, and (c) Forest. The
672 first two are handled in forest and listed by
673 DsEnumerateDomainTrusts(). Forest trusts are not so we
674 have to do that ourselves. */
676 if ( !wcache_tdc_fetch_list( &dom_list, &num_trusts ) )
679 for ( i=0; i<num_trusts; i++ ) {
680 uint32_t flags = dom_list[i].trust_flags;
681 uint32_t type = dom_list[i].trust_type;
682 uint32_t attribs = dom_list[i].trust_attribs;
684 d = find_domain_from_name_noinit( dom_list[i].domain_name );
686 /* ignore our primary and internal domains */
688 if ( d && (d->internal || d->primary ) )
691 if ( (flags & NETR_TRUST_FLAG_INBOUND) &&
692 (type == LSA_TRUST_TYPE_UPLEVEL) &&
693 (attribs == LSA_TRUST_ATTRIBUTE_FOREST_TRANSITIVE) )
695 /* add the trusted domain if we don't know
699 status = add_trusted_domain(
700 dom_list[i].domain_name,
701 dom_list[i].dns_name,
707 find_default_route_domain(),
709 if (!NT_STATUS_IS_OK(status) &&
710 NT_STATUS_EQUAL(status,
711 NT_STATUS_NO_SUCH_DOMAIN))
713 DBG_ERR("add_trusted_domain: %s\n",
723 DEBUG(10,("Following trust path for domain %s (%s)\n",
724 d->name, d->alt_name ));
725 add_trusted_domains( d );
729 TALLOC_FREE( dom_list );
734 /*********************************************************************
735 The process of updating the trusted domain list is a three step
738 (b) ask the root domain in our forest
739 (c) ask the a DC in any Win2003 trusted forests
740 *********************************************************************/
742 void rescan_trusted_domains(struct tevent_context *ev, struct tevent_timer *te,
743 struct timeval now, void *private_data)
747 /* I use to clear the cache here and start over but that
748 caused problems in child processes that needed the
749 trust dom list early on. Removing it means we
750 could have some trusted domains listed that have been
751 removed from our primary domain's DC until a full
752 restart. This should be ok since I think this is what
753 Windows does as well. */
755 /* this will only add new domains we didn't already know about
756 in the domain_list()*/
758 add_trusted_domains( find_our_domain() );
760 te = tevent_add_timer(
761 ev, NULL, timeval_current_ofs(WINBINDD_RESCAN_FREQ, 0),
762 rescan_trusted_domains, NULL);
764 * If te == NULL, there's not much we can do here. Don't fail, the
765 * only thing we miss is new trusted domains.
771 enum winbindd_result winbindd_dual_init_connection(struct winbindd_domain *domain,
772 struct winbindd_cli_state *state)
774 /* Ensure null termination */
775 state->request->domain_name
776 [sizeof(state->request->domain_name)-1]='\0';
777 state->request->data.init_conn.dcname
778 [sizeof(state->request->data.init_conn.dcname)-1]='\0';
780 if (strlen(state->request->data.init_conn.dcname) > 0) {
781 fstrcpy(domain->dcname, state->request->data.init_conn.dcname);
784 init_dc_connection(domain, false);
786 if (!domain->initialized) {
787 /* If we return error here we can't do any cached authentication,
788 but we may be in disconnected mode and can't initialize correctly.
789 Do what the previous code did and just return without initialization,
790 once we go online we'll re-initialize.
792 DEBUG(5, ("winbindd_dual_init_connection: %s returning without initialization "
793 "online = %d\n", domain->name, (int)domain->online ));
796 fstrcpy(state->response->data.domain_info.name, domain->name);
797 fstrcpy(state->response->data.domain_info.alt_name, domain->alt_name);
798 sid_to_fstring(state->response->data.domain_info.sid, &domain->sid);
800 state->response->data.domain_info.native_mode
801 = domain->native_mode;
802 state->response->data.domain_info.active_directory
803 = domain->active_directory;
804 state->response->data.domain_info.primary
810 static void wb_imsg_new_trusted_domain(struct imessaging_context *msg,
813 struct server_id server_id,
816 TALLOC_CTX *frame = talloc_stackframe();
817 enum netr_SchannelType secure_channel_type = SEC_CHAN_DOMAIN;
818 struct lsa_TrustDomainInfoInfoEx info;
819 enum ndr_err_code ndr_err;
820 struct winbindd_domain *d = NULL;
821 uint32_t trust_flags = 0;
824 DEBUG(5, ("wb_imsg_new_trusted_domain\n"));
831 ndr_err = ndr_pull_struct_blob_all(data, frame, &info,
832 (ndr_pull_flags_fn_t)ndr_pull_lsa_TrustDomainInfoInfoEx);
833 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
838 d = find_domain_from_name_noinit(info.netbios_name.string);
844 if (info.trust_type == LSA_TRUST_TYPE_UPLEVEL) {
845 secure_channel_type = SEC_CHAN_DNS_DOMAIN;
847 if (info.trust_direction & LSA_TRUST_DIRECTION_INBOUND) {
848 trust_flags |= NETR_TRUST_FLAG_INBOUND;
850 if (info.trust_direction & LSA_TRUST_DIRECTION_OUTBOUND) {
851 trust_flags |= NETR_TRUST_FLAG_OUTBOUND;
853 if (info.trust_attributes & LSA_TRUST_ATTRIBUTE_WITHIN_FOREST) {
854 trust_flags |= NETR_TRUST_FLAG_IN_FOREST;
857 status = add_trusted_domain(info.netbios_name.string,
858 info.domain_name.string,
862 info.trust_attributes,
864 find_default_route_domain(),
866 if (!NT_STATUS_IS_OK(status) &&
867 !NT_STATUS_EQUAL(status, NT_STATUS_NO_SUCH_DOMAIN))
869 DBG_NOTICE("add_trusted_domain returned %s\n",
879 * We did not get the secret when we queried secrets.tdb, so read it
880 * from secrets.tdb and re-sync the databases
882 static bool migrate_secrets_tdb_to_ldb(struct winbindd_domain *domain)
885 struct cli_credentials *creds;
886 NTSTATUS can_migrate = pdb_get_trust_credentials(domain->name,
887 NULL, domain, &creds);
888 if (!NT_STATUS_IS_OK(can_migrate)) {
889 DEBUG(0, ("Failed to fetch our own, local AD domain join "
890 "password for winbindd's internal use, both from "
891 "secrets.tdb and secrets.ldb: %s\n",
892 nt_errstr(can_migrate)));
897 * NOTE: It is very unlikely we end up here if there is an
898 * oldpass, because a new password is created at
899 * classicupgrade, so this is not a concern.
901 ok = secrets_store_machine_pw_sync(cli_credentials_get_password(creds),
903 cli_credentials_get_domain(creds),
904 cli_credentials_get_realm(creds),
905 cli_credentials_get_salt_principal(creds),
906 0, /* Supported enc types, unused */
908 cli_credentials_get_password_last_changed_time(creds),
909 cli_credentials_get_secure_channel_type(creds),
910 false /* do_delete: Do not delete */);
913 DEBUG(0, ("Failed to write our our own, "
914 "local AD domain join password for "
915 "winbindd's internal use into secrets.tdb\n"));
921 /* Look up global info for the winbind daemon */
922 bool init_domain_list(void)
924 int role = lp_server_role();
925 struct pdb_domain_info *pdb_domain_info = NULL;
926 struct winbindd_domain *domain = NULL;
929 /* Free existing list */
934 status = add_trusted_domain("BUILTIN",
937 LSA_TRUST_TYPE_DOWNLEVEL,
939 0, /* trust_attribs */
943 if (!NT_STATUS_IS_OK(status)) {
944 DBG_ERR("add_trusted_domain BUILTIN returned %s\n",
952 * In case the passdb backend is passdb_dsdb the domain SID comes from
953 * dsdb, not from secrets.tdb. As we use the domain SID in various
954 * places, we must ensure the domain SID is migrated from dsdb to
955 * secrets.tdb before get_global_sam_sid() is called the first time.
957 * The migration is done as part of the passdb_dsdb initialisation,
958 * calling pdb_get_domain_info() triggers it.
960 pdb_domain_info = pdb_get_domain_info(talloc_tos());
962 if ( role == ROLE_ACTIVE_DIRECTORY_DC ) {
963 uint32_t trust_flags;
965 enum netr_SchannelType sec_chan_type;
966 const char *account_name;
967 struct samr_Password current_nt_hash;
970 if (pdb_domain_info == NULL) {
971 DEBUG(0, ("Failed to fetch our own, local AD "
972 "domain info from sam.ldb\n"));
976 trust_flags = NETR_TRUST_FLAG_PRIMARY;
977 trust_flags |= NETR_TRUST_FLAG_IN_FOREST;
978 trust_flags |= NETR_TRUST_FLAG_NATIVE;
979 trust_flags |= NETR_TRUST_FLAG_OUTBOUND;
981 is_root = strequal(pdb_domain_info->dns_domain,
982 pdb_domain_info->dns_forest);
984 trust_flags |= NETR_TRUST_FLAG_TREEROOT;
987 status = add_trusted_domain(pdb_domain_info->name,
988 pdb_domain_info->dns_domain,
989 &pdb_domain_info->sid,
990 LSA_TRUST_TYPE_UPLEVEL,
992 LSA_TRUST_ATTRIBUTE_WITHIN_FOREST,
996 TALLOC_FREE(pdb_domain_info);
997 if (!NT_STATUS_IS_OK(status)) {
998 DBG_ERR("Failed to add our own, local AD "
999 "domain to winbindd's internal list\n");
1004 * We need to call this to find out if we are an RODC
1006 ok = get_trust_pw_hash(domain->name,
1007 current_nt_hash.hash,
1012 * If get_trust_pw_hash() fails, then try and
1013 * fetch the password from the more recent of
1014 * secrets.{ldb,tdb} using the
1015 * pdb_get_trust_credentials()
1017 ok = migrate_secrets_tdb_to_ldb(domain);
1020 DEBUG(0, ("Failed to migrate our own, "
1021 "local AD domain join password for "
1022 "winbindd's internal use into "
1026 ok = get_trust_pw_hash(domain->name,
1027 current_nt_hash.hash,
1031 DEBUG(0, ("Failed to find our our own, just "
1032 "written local AD domain join "
1033 "password for winbindd's internal "
1034 "use in secrets.tdb\n"));
1039 domain->secure_channel_type = sec_chan_type;
1040 if (sec_chan_type == SEC_CHAN_RODC) {
1041 domain->rodc = true;
1045 uint32_t trust_flags;
1046 enum netr_SchannelType secure_channel_type;
1048 trust_flags = NETR_TRUST_FLAG_OUTBOUND;
1049 if (role != ROLE_DOMAIN_MEMBER) {
1050 trust_flags |= NETR_TRUST_FLAG_PRIMARY;
1053 if (role > ROLE_DOMAIN_MEMBER) {
1054 secure_channel_type = SEC_CHAN_BDC;
1056 secure_channel_type = SEC_CHAN_LOCAL;
1059 status = add_trusted_domain(get_global_sam_name(),
1061 get_global_sam_sid(),
1062 LSA_TRUST_TYPE_DOWNLEVEL,
1064 0, /* trust_attribs */
1065 secure_channel_type,
1068 if (!NT_STATUS_IS_OK(status)) {
1069 DBG_ERR("Failed to add local SAM to "
1070 "domain to winbindd's internal list\n");
1074 /* Add ourselves as the first entry. */
1076 if ( role == ROLE_DOMAIN_MEMBER ) {
1077 struct dom_sid our_sid;
1078 uint32_t trust_type;
1080 if (!secrets_fetch_domain_sid(lp_workgroup(), &our_sid)) {
1081 DEBUG(0, ("Could not fetch our SID - did we join?\n"));
1085 if (lp_realm() != NULL) {
1086 trust_type = LSA_TRUST_TYPE_UPLEVEL;
1088 trust_type = LSA_TRUST_TYPE_DOWNLEVEL;
1091 status = add_trusted_domain(lp_workgroup(),
1095 NETR_TRUST_FLAG_PRIMARY|
1096 NETR_TRUST_FLAG_OUTBOUND,
1097 0, /* trust_attribs */
1101 if (!NT_STATUS_IS_OK(status)) {
1102 DBG_ERR("Failed to add local SAM to "
1103 "domain to winbindd's internal list\n");
1106 /* Even in the parent winbindd we'll need to
1107 talk to the DC, so try and see if we can
1108 contact it. Theoretically this isn't neccessary
1109 as the init_dc_connection() in init_child_recv()
1110 will do this, but we can start detecting the DC
1112 set_domain_online_request(domain);
1116 if (IS_DC && (pdb_capabilities() & PDB_CAP_TRUSTED_DOMAINS_EX)) {
1117 uint32_t num_domains = 0;
1118 struct pdb_trusted_domain **domains = NULL;
1121 status = pdb_enum_trusted_domains(talloc_tos(), &num_domains, &domains);
1122 if (!NT_STATUS_IS_OK(status)) {
1123 DBG_ERR("pdb_enum_trusted_domains() failed - %s\n",
1128 for (i = 0; i < num_domains; i++) {
1129 enum netr_SchannelType sec_chan_type = SEC_CHAN_DOMAIN;
1130 uint32_t trust_flags = 0;
1132 if (domains[i]->trust_type == LSA_TRUST_TYPE_UPLEVEL) {
1133 sec_chan_type = SEC_CHAN_DNS_DOMAIN;
1136 if (!(domains[i]->trust_direction & LSA_TRUST_DIRECTION_OUTBOUND)) {
1137 sec_chan_type = SEC_CHAN_NULL;
1140 if (domains[i]->trust_direction & LSA_TRUST_DIRECTION_INBOUND) {
1141 trust_flags |= NETR_TRUST_FLAG_INBOUND;
1143 if (domains[i]->trust_direction & LSA_TRUST_DIRECTION_OUTBOUND) {
1144 trust_flags |= NETR_TRUST_FLAG_OUTBOUND;
1146 if (domains[i]->trust_attributes & LSA_TRUST_ATTRIBUTE_WITHIN_FOREST) {
1147 trust_flags |= NETR_TRUST_FLAG_IN_FOREST;
1150 status = add_trusted_domain(domains[i]->netbios_name,
1151 domains[i]->domain_name,
1152 &domains[i]->security_identifier,
1153 domains[i]->trust_type,
1155 domains[i]->trust_attributes,
1159 if (!NT_STATUS_IS_OK(status)) {
1160 DBG_NOTICE("add_trusted_domain returned %s\n",
1165 if (domains[i]->trust_type == LSA_TRUST_TYPE_UPLEVEL) {
1166 domain->active_directory = true;
1168 domain->domain_type = domains[i]->trust_type;
1169 domain->domain_trust_attribs = domains[i]->trust_attributes;
1171 if (sec_chan_type != SEC_CHAN_NULL) {
1172 /* Even in the parent winbindd we'll need to
1173 talk to the DC, so try and see if we can
1174 contact it. Theoretically this isn't neccessary
1175 as the init_dc_connection() in init_child_recv()
1176 will do this, but we can start detecting the DC
1178 set_domain_online_request(domain);
1182 for (i = 0; i < num_domains; i++) {
1183 struct ForestTrustInfo fti;
1185 enum ndr_err_code ndr_err;
1186 struct winbindd_domain *routing_domain = NULL;
1188 if (domains[i]->trust_type != LSA_TRUST_TYPE_UPLEVEL) {
1192 if (!(domains[i]->trust_attributes & LSA_TRUST_ATTRIBUTE_FOREST_TRANSITIVE)) {
1196 if (domains[i]->trust_forest_trust_info.length == 0) {
1200 routing_domain = find_domain_from_name_noinit(
1201 domains[i]->netbios_name);
1202 if (routing_domain == NULL) {
1203 DBG_ERR("Can't find winbindd domain [%s]\n",
1204 domains[i]->netbios_name);
1208 ndr_err = ndr_pull_struct_blob_all(
1209 &domains[i]->trust_forest_trust_info,
1211 (ndr_pull_flags_fn_t)ndr_pull_ForestTrustInfo);
1212 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1213 DBG_ERR("ndr_pull_ForestTrustInfo(%s) - %s\n",
1214 domains[i]->netbios_name,
1215 ndr_map_error2string(ndr_err));
1219 for (fi = 0; fi < fti.count; fi++) {
1220 struct ForestTrustInfoRecord *rec =
1221 &fti.records[fi].record;
1222 struct ForestTrustDataDomainInfo *drec = NULL;
1224 if (rec->type != FOREST_TRUST_DOMAIN_INFO) {
1227 drec = &rec->data.info;
1229 if (rec->flags & LSA_NB_DISABLED_MASK) {
1233 if (rec->flags & LSA_SID_DISABLED_MASK) {
1239 * also try to find a matching
1240 * LSA_TLN_DISABLED_MASK ???
1243 domain = find_domain_from_name_noinit(drec->netbios_name.string);
1244 if (domain != NULL) {
1248 status = add_trusted_domain(drec->netbios_name.string,
1249 drec->dns_name.string,
1251 LSA_TRUST_TYPE_UPLEVEL,
1252 NETR_TRUST_FLAG_OUTBOUND,
1257 if (!NT_STATUS_IS_OK(status)) {
1258 DBG_NOTICE("add_trusted_domain returned %s\n",
1262 if (domain == NULL) {
1268 uint32_t num_domains = 0;
1269 struct trustdom_info **domains = NULL;
1272 status = pdb_enum_trusteddoms(talloc_tos(), &num_domains, &domains);
1273 if (!NT_STATUS_IS_OK(status)) {
1274 DBG_ERR("pdb_enum_trusteddoms() failed - %s\n",
1279 for (i = 0; i < num_domains; i++) {
1280 status = add_trusted_domain(domains[i]->name,
1283 LSA_TRUST_TYPE_DOWNLEVEL,
1284 NETR_TRUST_FLAG_OUTBOUND,
1289 if (!NT_STATUS_IS_OK(status)) {
1290 DBG_NOTICE("add_trusted_domain returned %s\n",
1295 /* Even in the parent winbindd we'll need to
1296 talk to the DC, so try and see if we can
1297 contact it. Theoretically this isn't neccessary
1298 as the init_dc_connection() in init_child_recv()
1299 will do this, but we can start detecting the DC
1301 set_domain_online_request(domain);
1305 status = imessaging_register(winbind_imessaging_context(), NULL,
1306 MSG_WINBIND_NEW_TRUSTED_DOMAIN,
1307 wb_imsg_new_trusted_domain);
1308 if (!NT_STATUS_IS_OK(status)) {
1309 DEBUG(0, ("imessaging_register(MSG_WINBIND_NEW_TRUSTED_DOMAIN) - %s\n",
1310 nt_errstr(status)));
1318 * Given a domain name, return the struct winbindd domain info for it
1320 * @note Do *not* pass lp_workgroup() to this function. domain_list
1321 * may modify it's value, and free that pointer. Instead, our local
1322 * domain may be found by calling find_our_domain().
1326 * @return The domain structure for the named domain, if it is working.
1329 struct winbindd_domain *find_domain_from_name_noinit(const char *domain_name)
1331 struct winbindd_domain *domain;
1333 /* Search through list */
1335 for (domain = domain_list(); domain != NULL; domain = domain->next) {
1336 if (strequal(domain_name, domain->name)) {
1339 if (domain->alt_name == NULL) {
1342 if (strequal(domain_name, domain->alt_name)) {
1353 * Given a domain name, return the struct winbindd domain if it's a direct
1356 * @return The domain structure for the named domain, if it is a direct outgoing trust
1358 struct winbindd_domain *find_trust_from_name_noinit(const char *domain_name)
1360 struct winbindd_domain *domain = NULL;
1362 domain = find_domain_from_name_noinit(domain_name);
1363 if (domain == NULL) {
1367 if (domain->secure_channel_type != SEC_CHAN_NULL) {
1374 struct winbindd_domain *find_domain_from_name(const char *domain_name)
1376 struct winbindd_domain *domain;
1378 domain = find_domain_from_name_noinit(domain_name);
1383 if (!domain->initialized)
1384 init_dc_connection(domain, false);
1389 /* Given a domain sid, return the struct winbindd domain info for it */
1391 struct winbindd_domain *find_domain_from_sid_noinit(const struct dom_sid *sid)
1393 struct winbindd_domain *domain;
1395 /* Search through list */
1397 for (domain = domain_list(); domain != NULL; domain = domain->next) {
1398 if (dom_sid_compare_domain(sid, &domain->sid) == 0)
1408 * Given a domain sid, return the struct winbindd domain if it's a direct
1411 * @return The domain structure for the specified domain, if it is a direct outgoing trust
1413 struct winbindd_domain *find_trust_from_sid_noinit(const struct dom_sid *sid)
1415 struct winbindd_domain *domain = NULL;
1417 domain = find_domain_from_sid_noinit(sid);
1418 if (domain == NULL) {
1422 if (domain->secure_channel_type != SEC_CHAN_NULL) {
1429 /* Given a domain sid, return the struct winbindd domain info for it */
1431 struct winbindd_domain *find_domain_from_sid(const struct dom_sid *sid)
1433 struct winbindd_domain *domain;
1435 domain = find_domain_from_sid_noinit(sid);
1440 if (!domain->initialized)
1441 init_dc_connection(domain, false);
1446 struct winbindd_domain *find_our_domain(void)
1448 struct winbindd_domain *domain;
1450 /* Search through list */
1452 for (domain = domain_list(); domain != NULL; domain = domain->next) {
1453 if (domain->primary)
1457 smb_panic("Could not find our domain");
1461 struct winbindd_domain *find_default_route_domain(void)
1464 return find_our_domain();
1466 DBG_ERR("Routing logic not yet implemented on a DC");
1470 /* Find the appropriate domain to lookup a name or SID */
1472 struct winbindd_domain *find_lookup_domain_from_sid(const struct dom_sid *sid)
1474 DBG_DEBUG("SID [%s]\n", sid_string_dbg(sid));
1477 * SIDs in the S-1-22-{1,2} domain and well-known SIDs should be handled
1481 if ( sid_check_is_in_unix_groups(sid) ||
1482 sid_check_is_unix_groups(sid) ||
1483 sid_check_is_in_unix_users(sid) ||
1484 sid_check_is_unix_users(sid) ||
1485 sid_check_is_wellknown_domain(sid, NULL) ||
1486 sid_check_is_in_wellknown_domain(sid) )
1488 return find_domain_from_sid(get_global_sam_sid());
1492 * On member servers the internal domains are different: These are part
1496 if (is_internal_domain(sid) || is_in_internal_domain(sid)) {
1497 DEBUG(10, ("calling find_domain_from_sid\n"));
1498 return find_domain_from_sid(sid);
1502 struct winbindd_domain *domain = NULL;
1504 domain = find_domain_from_sid_noinit(sid);
1505 if (domain == NULL) {
1509 if (domain->secure_channel_type != SEC_CHAN_NULL) {
1513 return domain->routing_domain;
1516 /* On a member server a query for SID or name can always go to our
1519 DEBUG(10, ("calling find_our_domain\n"));
1520 return find_our_domain();
1523 struct winbindd_domain *find_lookup_domain_from_name(const char *domain_name)
1525 if ( strequal(domain_name, unix_users_domain_name() ) ||
1526 strequal(domain_name, unix_groups_domain_name() ) )
1529 * The "Unix User" and "Unix Group" domain our handled by
1532 return find_domain_from_name_noinit( get_global_sam_name() );
1535 if (strequal(domain_name, "BUILTIN") ||
1536 strequal(domain_name, get_global_sam_name()))
1537 return find_domain_from_name_noinit(domain_name);
1540 struct winbindd_domain *domain = NULL;
1542 domain = find_domain_from_name_noinit(domain_name);
1543 if (domain == NULL) {
1547 if (domain->secure_channel_type != SEC_CHAN_NULL) {
1551 return domain->routing_domain;
1554 return find_our_domain();
1557 /* Is this a domain which we may assume no DOMAIN\ prefix? */
1559 static bool assume_domain(const char *domain)
1561 /* never assume the domain on a standalone server */
1563 if ( lp_server_role() == ROLE_STANDALONE )
1566 /* domain member servers may possibly assume for the domain name */
1568 if ( lp_server_role() == ROLE_DOMAIN_MEMBER ) {
1569 if ( !strequal(lp_workgroup(), domain) )
1572 if ( lp_winbind_use_default_domain() )
1576 /* only left with a domain controller */
1578 if ( strequal(get_global_sam_name(), domain) ) {
1585 /* Parse a string of the form DOMAIN\user into a domain and a user */
1587 bool parse_domain_user(const char *domuser, fstring domain, fstring user)
1589 char *p = strchr(domuser,*lp_winbind_separator());
1592 fstrcpy(user, domuser);
1593 p = strchr(domuser, '@');
1595 if ( assume_domain(lp_workgroup()) && p == NULL) {
1596 fstrcpy(domain, lp_workgroup());
1597 } else if (p != NULL) {
1598 fstrcpy(domain, p + 1);
1599 user[PTR_DIFF(p, domuser)] = 0;
1605 fstrcpy(domain, domuser);
1606 domain[PTR_DIFF(p, domuser)] = 0;
1609 return strupper_m(domain);
1612 bool parse_domain_user_talloc(TALLOC_CTX *mem_ctx, const char *domuser,
1613 char **domain, char **user)
1615 fstring fstr_domain, fstr_user;
1616 if (!parse_domain_user(domuser, fstr_domain, fstr_user)) {
1619 *domain = talloc_strdup(mem_ctx, fstr_domain);
1620 *user = talloc_strdup(mem_ctx, fstr_user);
1621 return ((*domain != NULL) && (*user != NULL));
1624 /* Ensure an incoming username from NSS is fully qualified. Replace the
1625 incoming fstring with DOMAIN <separator> user. Returns the same
1626 values as parse_domain_user() but also replaces the incoming username.
1627 Used to ensure all names are fully qualified within winbindd.
1628 Used by the NSS protocols of auth, chauthtok, logoff and ccache_ntlm_auth.
1629 The protocol definitions of auth_crap, chng_pswd_auth_crap
1630 really should be changed to use this instead of doing things
1633 bool canonicalize_username(fstring username_inout, fstring domain, fstring user)
1635 if (!parse_domain_user(username_inout, domain, user)) {
1638 slprintf(username_inout, sizeof(fstring) - 1, "%s%c%s",
1639 domain, *lp_winbind_separator(),
1645 Fill DOMAIN\\USERNAME entry accounting 'winbind use default domain' and
1646 'winbind separator' options.
1648 - omit DOMAIN when 'winbind use default domain = true' and DOMAIN is
1651 If we are a PDC or BDC, and this is for our domain, do likewise.
1653 On an AD DC we always fill DOMAIN\\USERNAME.
1655 We always canonicalize as UPPERCASE DOMAIN, lowercase username.
1657 void fill_domain_username(fstring name, const char *domain, const char *user, bool can_assume)
1661 if (lp_server_role() == ROLE_ACTIVE_DIRECTORY_DC) {
1665 fstrcpy(tmp_user, user);
1666 (void)strlower_m(tmp_user);
1668 if (can_assume && assume_domain(domain)) {
1669 strlcpy(name, tmp_user, sizeof(fstring));
1671 slprintf(name, sizeof(fstring) - 1, "%s%c%s",
1672 domain, *lp_winbind_separator(),
1678 * talloc version of fill_domain_username()
1679 * return NULL on talloc failure.
1681 char *fill_domain_username_talloc(TALLOC_CTX *mem_ctx,
1686 char *tmp_user, *name;
1688 if (lp_server_role() == ROLE_ACTIVE_DIRECTORY_DC) {
1692 tmp_user = talloc_strdup(mem_ctx, user);
1693 if (!strlower_m(tmp_user)) {
1694 TALLOC_FREE(tmp_user);
1698 if (can_assume && assume_domain(domain)) {
1701 name = talloc_asprintf(mem_ctx, "%s%c%s",
1703 *lp_winbind_separator(),
1705 TALLOC_FREE(tmp_user);
1712 * Client list accessor functions
1715 static struct winbindd_cli_state *_client_list;
1716 static int _num_clients;
1718 /* Return list of all connected clients */
1720 struct winbindd_cli_state *winbindd_client_list(void)
1722 return _client_list;
1725 /* Return list-tail of all connected clients */
1727 struct winbindd_cli_state *winbindd_client_list_tail(void)
1729 return DLIST_TAIL(_client_list);
1732 /* Return previous (read:newer) client in list */
1734 struct winbindd_cli_state *
1735 winbindd_client_list_prev(struct winbindd_cli_state *cli)
1737 return DLIST_PREV(cli);
1740 /* Add a connection to the list */
1742 void winbindd_add_client(struct winbindd_cli_state *cli)
1744 cli->last_access = time(NULL);
1745 DLIST_ADD(_client_list, cli);
1749 /* Remove a client from the list */
1751 void winbindd_remove_client(struct winbindd_cli_state *cli)
1753 DLIST_REMOVE(_client_list, cli);
1757 /* Move a client to head or list */
1759 void winbindd_promote_client(struct winbindd_cli_state *cli)
1761 cli->last_access = time(NULL);
1762 DLIST_PROMOTE(_client_list, cli);
1765 /* Return number of open clients */
1767 int winbindd_num_clients(void)
1769 return _num_clients;
1772 NTSTATUS lookup_usergroups_cached(TALLOC_CTX *mem_ctx,
1773 const struct dom_sid *user_sid,
1774 uint32_t *p_num_groups, struct dom_sid **user_sids)
1776 struct netr_SamInfo3 *info3 = NULL;
1777 NTSTATUS status = NT_STATUS_NO_MEMORY;
1778 uint32_t num_groups = 0;
1780 DEBUG(3,(": lookup_usergroups_cached\n"));
1785 info3 = netsamlogon_cache_get(mem_ctx, user_sid);
1787 if (info3 == NULL) {
1788 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
1792 * Before bug #7843 the "Domain Local" groups were added with a
1793 * lookupuseraliases call, but this isn't done anymore for our domain
1794 * so we need to resolve resource groups here.
1796 * When to use Resource Groups:
1797 * http://technet.microsoft.com/en-us/library/cc753670%28v=WS.10%29.aspx
1799 status = sid_array_from_info3(mem_ctx, info3,
1804 if (!NT_STATUS_IS_OK(status)) {
1810 *p_num_groups = num_groups;
1811 status = (user_sids != NULL) ? NT_STATUS_OK : NT_STATUS_NO_MEMORY;
1813 DEBUG(3,(": lookup_usergroups_cached succeeded\n"));
1818 /*********************************************************************
1819 We use this to remove spaces from user and group names
1820 ********************************************************************/
1822 NTSTATUS normalize_name_map(TALLOC_CTX *mem_ctx,
1823 const char *domain_name,
1827 struct winbindd_domain *domain = NULL;
1830 if (!name || !normalized) {
1831 return NT_STATUS_INVALID_PARAMETER;
1834 if (!lp_winbind_normalize_names()) {
1835 return NT_STATUS_PROCEDURE_NOT_FOUND;
1838 domain = find_domain_from_name_noinit(domain_name);
1839 if (domain == NULL) {
1840 DBG_ERR("Failed to find domain '%s'\n", domain_name);
1841 return NT_STATUS_NO_SUCH_DOMAIN;
1844 /* Alias support and whitespace replacement are mutually
1847 nt_status = resolve_username_to_alias(mem_ctx, domain,
1849 if (NT_STATUS_IS_OK(nt_status)) {
1850 /* special return code to let the caller know we
1851 mapped to an alias */
1852 return NT_STATUS_FILE_RENAMED;
1855 /* check for an unreachable domain */
1857 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND)) {
1858 DEBUG(5,("normalize_name_map: Setting domain %s offline\n",
1860 set_domain_offline(domain);
1864 /* deal with whitespace */
1866 *normalized = talloc_strdup(mem_ctx, name);
1867 if (!(*normalized)) {
1868 return NT_STATUS_NO_MEMORY;
1871 all_string_sub( *normalized, " ", "_", 0 );
1873 return NT_STATUS_OK;
1876 /*********************************************************************
1877 We use this to do the inverse of normalize_name_map()
1878 ********************************************************************/
1880 NTSTATUS normalize_name_unmap(TALLOC_CTX *mem_ctx,
1885 struct winbindd_domain *domain = find_our_domain();
1887 if (!name || !normalized) {
1888 return NT_STATUS_INVALID_PARAMETER;
1891 if (!lp_winbind_normalize_names()) {
1892 return NT_STATUS_PROCEDURE_NOT_FOUND;
1895 /* Alias support and whitespace replacement are mutally
1898 /* When mapping from an alias to a username, we don't know the
1899 domain. But we only need a domain structure to cache
1900 a successful lookup , so just our own domain structure for
1903 nt_status = resolve_alias_to_username(mem_ctx, domain,
1905 if (NT_STATUS_IS_OK(nt_status)) {
1906 /* Special return code to let the caller know we mapped
1908 return NT_STATUS_FILE_RENAMED;
1911 /* check for an unreachable domain */
1913 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND)) {
1914 DEBUG(5,("normalize_name_unmap: Setting domain %s offline\n",
1916 set_domain_offline(domain);
1920 /* deal with whitespace */
1922 *normalized = talloc_strdup(mem_ctx, name);
1923 if (!(*normalized)) {
1924 return NT_STATUS_NO_MEMORY;
1927 all_string_sub(*normalized, "_", " ", 0);
1929 return NT_STATUS_OK;
1932 /*********************************************************************
1933 ********************************************************************/
1935 bool winbindd_can_contact_domain(struct winbindd_domain *domain)
1937 struct winbindd_tdc_domain *tdc = NULL;
1938 TALLOC_CTX *frame = talloc_stackframe();
1941 /* We can contact the domain if it is our primary domain */
1943 if (domain->primary) {
1948 /* Trust the TDC cache and not the winbindd_domain flags */
1950 if ((tdc = wcache_tdc_fetch_domain(frame, domain->name)) == NULL) {
1951 DEBUG(10,("winbindd_can_contact_domain: %s not found in cache\n",
1957 /* Can always contact a domain that is in out forest */
1959 if (tdc->trust_flags & NETR_TRUST_FLAG_IN_FOREST) {
1965 * On a _member_ server, we cannot contact the domain if it
1966 * is running AD and we have no inbound trust.
1970 domain->active_directory &&
1971 ((tdc->trust_flags & NETR_TRUST_FLAG_INBOUND) != NETR_TRUST_FLAG_INBOUND))
1973 DEBUG(10, ("winbindd_can_contact_domain: %s is an AD domain "
1974 "and we have no inbound trust.\n", domain->name));
1978 /* Assume everything else is ok (probably not true but what
1984 talloc_destroy(frame);
1989 /*********************************************************************
1990 ********************************************************************/
1992 bool winbindd_internal_child(struct winbindd_child *child)
1994 if ((child == idmap_child()) || (child == locator_child())) {
2001 #ifdef HAVE_KRB5_LOCATE_PLUGIN_H
2003 /*********************************************************************
2004 ********************************************************************/
2006 static void winbindd_set_locator_kdc_env(const struct winbindd_domain *domain)
2009 char addr[INET6_ADDRSTRLEN];
2010 const char *kdc = NULL;
2013 if (!domain || !domain->alt_name || !*domain->alt_name) {
2017 if (domain->initialized && !domain->active_directory) {
2018 DEBUG(lvl,("winbindd_set_locator_kdc_env: %s not AD\n",
2023 print_sockaddr(addr, sizeof(addr), &domain->dcaddr);
2026 DEBUG(lvl,("winbindd_set_locator_kdc_env: %s no DC IP\n",
2028 kdc = domain->dcname;
2031 if (!kdc || !*kdc) {
2032 DEBUG(lvl,("winbindd_set_locator_kdc_env: %s no DC at all\n",
2037 if (asprintf_strupper_m(&var, "%s_%s", WINBINDD_LOCATOR_KDC_ADDRESS,
2038 domain->alt_name) == -1) {
2042 DEBUG(lvl,("winbindd_set_locator_kdc_env: setting var: %s to: %s\n",
2045 setenv(var, kdc, 1);
2049 /*********************************************************************
2050 ********************************************************************/
2052 void winbindd_set_locator_kdc_envs(const struct winbindd_domain *domain)
2054 struct winbindd_domain *our_dom = find_our_domain();
2056 winbindd_set_locator_kdc_env(domain);
2058 if (domain != our_dom) {
2059 winbindd_set_locator_kdc_env(our_dom);
2063 /*********************************************************************
2064 ********************************************************************/
2066 void winbindd_unset_locator_kdc_env(const struct winbindd_domain *domain)
2070 if (!domain || !domain->alt_name || !*domain->alt_name) {
2074 if (asprintf_strupper_m(&var, "%s_%s", WINBINDD_LOCATOR_KDC_ADDRESS,
2075 domain->alt_name) == -1) {
2084 void winbindd_set_locator_kdc_envs(const struct winbindd_domain *domain)
2089 void winbindd_unset_locator_kdc_env(const struct winbindd_domain *domain)
2094 #endif /* HAVE_KRB5_LOCATE_PLUGIN_H */
2096 void set_auth_errors(struct winbindd_response *resp, NTSTATUS result)
2098 resp->data.auth.nt_status = NT_STATUS_V(result);
2099 fstrcpy(resp->data.auth.nt_status_string, nt_errstr(result));
2101 /* we might have given a more useful error above */
2102 if (*resp->data.auth.error_string == '\0')
2103 fstrcpy(resp->data.auth.error_string,
2104 get_friendly_nt_error_msg(result));
2105 resp->data.auth.pam_error = nt_status_to_pam(result);
2108 bool is_domain_offline(const struct winbindd_domain *domain)
2110 if (get_global_winbindd_state_offline()) {
2113 return !domain->online;
2116 bool is_domain_online(const struct winbindd_domain *domain)
2118 return !is_domain_offline(domain);
2122 * Parse an char array into a list of sids.
2124 * The input sidstr should consist of 0-terminated strings
2125 * representing sids, separated by newline characters '\n'.
2126 * The list is terminated by an empty string, i.e.
2127 * character '\0' directly following a character '\n'
2128 * (or '\0' right at the start of sidstr).
2130 bool parse_sidlist(TALLOC_CTX *mem_ctx, const char *sidstr,
2131 struct dom_sid **sids, uint32_t *num_sids)
2139 while (p[0] != '\0') {
2141 const char *q = NULL;
2143 if (!dom_sid_parse_endp(p, &sid, &q)) {
2144 DEBUG(1, ("Could not parse sid %s\n", p));
2148 DEBUG(1, ("Got invalid sidstr: %s\n", p));
2151 if (!NT_STATUS_IS_OK(add_sid_to_array(mem_ctx, &sid, sids,
2161 bool parse_xidlist(TALLOC_CTX *mem_ctx, const char *xidstr,
2162 struct unixid **pxids, uint32_t *pnum_xids)
2165 struct unixid *xids = NULL;
2166 uint32_t num_xids = 0;
2173 while (p[0] != '\0') {
2176 unsigned long long id;
2181 xid = (struct unixid) { .type = ID_TYPE_UID };
2184 xid = (struct unixid) { .type = ID_TYPE_GID };
2192 id = strtoull(p, &endp, 10);
2193 if ((id == ULLONG_MAX) && (errno == ERANGE)) {
2196 if (*endp != '\n') {
2202 if ((unsigned long long)xid.id != id) {
2206 tmp = talloc_realloc(mem_ctx, xids, struct unixid, num_xids+1);
2212 xids[num_xids] = xid;
2217 *pnum_xids = num_xids;