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"
36 #include "lib/util/smb_strtox.h"
37 #include "lib/util/string_wrappers.h"
38 #include "lib/global_contexts.h"
39 #include "librpc/gen_ndr/ndr_winbind_c.h"
42 #define DBGC_CLASS DBGC_WINBIND
45 * @file winbindd_util.c
47 * Winbind daemon for NT domain authentication nss module.
50 /* The list of trusted domains. Note that the list can be deleted and
51 recreated using the init_domain_list() function so pointers to
52 individual winbindd_domain structures cannot be made. Keep a copy of
53 the domain name instead. */
55 static struct winbindd_domain
*_domain_list
= NULL
;
57 struct winbindd_domain
*domain_list(void)
61 if ((!_domain_list
) && (!init_domain_list())) {
62 smb_panic("Init_domain_list failed");
68 /* Free all entries in the trusted domain list */
70 static void free_domain_list(void)
72 struct winbindd_domain
*domain
= _domain_list
;
75 struct winbindd_domain
*next
= domain
->next
;
77 DLIST_REMOVE(_domain_list
, domain
);
84 * Iterator for winbindd's domain list.
85 * To be used (e.g.) in tevent based loops.
87 struct winbindd_domain
*wb_next_domain(struct winbindd_domain
*domain
)
90 domain
= domain_list();
92 domain
= domain
->next
;
95 if ((domain
!= NULL
) &&
96 (lp_server_role() != ROLE_ACTIVE_DIRECTORY_DC
) &&
97 sid_check_is_our_sam(&domain
->sid
))
99 domain
= domain
->next
;
105 static bool is_internal_domain(const struct dom_sid
*sid
)
110 return (sid_check_is_our_sam(sid
) || sid_check_is_builtin(sid
));
113 /* Add a trusted domain to our list of domains.
114 If the domain already exists in the list,
115 return it and don't re-initialize. */
117 static NTSTATUS
add_trusted_domain(const char *domain_name
,
118 const char *dns_name
,
119 const struct dom_sid
*sid
,
121 uint32_t trust_flags
,
122 uint32_t trust_attribs
,
123 enum netr_SchannelType secure_channel_type
,
124 struct winbindd_domain
*routing_domain
,
125 struct winbindd_domain
**_d
)
127 struct winbindd_domain
*domain
= NULL
;
128 int role
= lp_server_role();
129 struct dom_sid_buf buf
;
131 if (is_null_sid(sid
)) {
132 DBG_ERR("Got null SID for domain [%s]\n", domain_name
);
133 return NT_STATUS_INVALID_PARAMETER
;
136 if (secure_channel_type
== SEC_CHAN_NULL
&& !is_allowed_domain(domain_name
)) {
137 return NT_STATUS_NO_SUCH_DOMAIN
;
141 * We can't call domain_list() as this function is called from
142 * init_domain_list() and we'll get stuck in a loop.
144 for (domain
= _domain_list
; domain
; domain
= domain
->next
) {
145 if (strequal(domain_name
, domain
->name
)) {
150 if (domain
!= NULL
) {
151 struct winbindd_domain
*check_domain
= NULL
;
153 for (check_domain
= _domain_list
;
154 check_domain
!= NULL
;
155 check_domain
= check_domain
->next
)
157 if (check_domain
== domain
) {
161 if (dom_sid_equal(&check_domain
->sid
, sid
)) {
166 if (check_domain
!= NULL
) {
167 DBG_ERR("SID [%s] already used by domain [%s], "
169 dom_sid_str_buf(sid
, &buf
),
172 return NT_STATUS_INVALID_PARAMETER
;
176 if ((domain
!= NULL
) && (dns_name
!= NULL
)) {
177 struct winbindd_domain
*check_domain
= NULL
;
179 for (check_domain
= _domain_list
;
180 check_domain
!= NULL
;
181 check_domain
= check_domain
->next
)
183 if (check_domain
== domain
) {
187 if (strequal(check_domain
->alt_name
, dns_name
)) {
192 if (check_domain
!= NULL
) {
193 DBG_ERR("DNS name [%s] used by domain [%s], "
195 dns_name
, check_domain
->name
,
197 return NT_STATUS_INVALID_PARAMETER
;
201 if (domain
!= NULL
) {
206 /* Create new domain entry */
207 domain
= talloc_zero(NULL
, struct winbindd_domain
);
208 if (domain
== NULL
) {
209 return NT_STATUS_NO_MEMORY
;
212 domain
->children
= talloc_zero_array(domain
,
213 struct winbindd_child
,
214 lp_winbind_max_domain_connections());
215 if (domain
->children
== NULL
) {
217 return NT_STATUS_NO_MEMORY
;
220 domain
->queue
= tevent_queue_create(domain
, "winbind_domain");
221 if (domain
->queue
== NULL
) {
223 return NT_STATUS_NO_MEMORY
;
226 domain
->binding_handle
= wbint_binding_handle(domain
, domain
, NULL
);
227 if (domain
->binding_handle
== NULL
) {
229 return NT_STATUS_NO_MEMORY
;
232 domain
->name
= talloc_strdup(domain
, domain_name
);
233 if (domain
->name
== NULL
) {
235 return NT_STATUS_NO_MEMORY
;
238 if (dns_name
!= NULL
) {
239 domain
->alt_name
= talloc_strdup(domain
, dns_name
);
240 if (domain
->alt_name
== NULL
) {
242 return NT_STATUS_NO_MEMORY
;
246 domain
->backend
= NULL
;
247 domain
->internal
= is_internal_domain(sid
);
248 domain
->secure_channel_type
= secure_channel_type
;
249 domain
->sequence_number
= DOM_SEQUENCE_NONE
;
250 domain
->last_seq_check
= 0;
251 domain
->initialized
= false;
252 domain
->online
= is_internal_domain(sid
);
253 domain
->domain_flags
= trust_flags
;
254 domain
->domain_type
= trust_type
;
255 domain
->domain_trust_attribs
= trust_attribs
;
256 domain
->routing_domain
= routing_domain
;
257 sid_copy(&domain
->sid
, sid
);
259 /* Is this our primary domain ? */
260 if (role
== ROLE_DOMAIN_MEMBER
) {
261 domain
->primary
= strequal(domain_name
, lp_workgroup());
263 domain
->primary
= strequal(domain_name
, get_global_sam_name());
266 if (domain
->primary
) {
267 if (role
== ROLE_ACTIVE_DIRECTORY_DC
) {
268 domain
->active_directory
= true;
270 if (lp_security() == SEC_ADS
) {
271 domain
->active_directory
= true;
273 } else if (!domain
->internal
) {
274 if (domain
->domain_type
== LSA_TRUST_TYPE_UPLEVEL
) {
275 domain
->active_directory
= true;
279 domain
->can_do_ncacn_ip_tcp
= domain
->active_directory
;
281 if (secure_channel_type
!= SEC_CHAN_NULL
) {
283 * If we loaded the domain from
284 * our config it is initialized
287 domain
->initialized
= true;
290 /* Link to domain list */
291 DLIST_ADD_END(_domain_list
, domain
);
293 wcache_tdc_add_domain( domain
);
295 setup_domain_child(domain
);
297 DBG_NOTICE("Added domain [%s] [%s] [%s]\n",
298 domain
->name
, domain
->alt_name
,
299 dom_sid_str_buf(&domain
->sid
, &buf
));
305 bool set_routing_domain(struct winbindd_domain
*domain
,
306 struct winbindd_domain
*routing_domain
)
308 if (domain
->routing_domain
== NULL
) {
309 domain
->routing_domain
= routing_domain
;
312 if (domain
->routing_domain
!= routing_domain
) {
318 bool add_trusted_domain_from_auth(uint16_t validation_level
,
319 struct info3_text
*info3
,
320 struct info6_text
*info6
)
322 struct winbindd_domain
*domain
= NULL
;
323 struct dom_sid domain_sid
;
324 const char *dns_domainname
= NULL
;
329 * We got a successful auth from a domain that might not yet be in our
330 * domain list. If we're a member we trust our DC who authenticated the
331 * user from that domain and add the domain to our list on-the-fly. If
332 * we're a DC we rely on configured trusts and don't add on-the-fly.
339 ok
= dom_sid_parse(info3
->dom_sid
, &domain_sid
);
341 DBG_NOTICE("dom_sid_parse [%s] failed\n", info3
->dom_sid
);
345 if (validation_level
== 6) {
346 if (!strequal(info6
->dns_domainname
, "")) {
347 dns_domainname
= info6
->dns_domainname
;
351 status
= add_trusted_domain(info3
->logon_dom
,
355 NETR_TRUST_FLAG_OUTBOUND
,
358 find_default_route_domain(),
360 if (!NT_STATUS_IS_OK(status
) &&
361 !NT_STATUS_EQUAL(status
, NT_STATUS_NO_SUCH_DOMAIN
))
363 DBG_DEBUG("Adding domain [%s] with sid [%s] failed\n",
364 info3
->logon_dom
, info3
->dom_sid
);
371 bool domain_is_forest_root(const struct winbindd_domain
*domain
)
373 const uint32_t fr_flags
=
374 (NETR_TRUST_FLAG_TREEROOT
|NETR_TRUST_FLAG_IN_FOREST
);
376 return ((domain
->domain_flags
& fr_flags
) == fr_flags
);
379 /********************************************************************
380 rescan our domains looking for new trusted domains
381 ********************************************************************/
383 struct trustdom_state
{
384 struct winbindd_domain
*domain
;
385 struct netr_DomainTrustList trusts
;
388 static void trustdom_list_done(struct tevent_req
*req
);
389 static void rescan_forest_root_trusts( void );
390 static void rescan_forest_trusts( void );
392 static void add_trusted_domains( struct winbindd_domain
*domain
)
394 struct tevent_context
*ev
= global_event_context();
395 struct trustdom_state
*state
;
396 struct tevent_req
*req
;
397 const char *client_name
= NULL
;
400 state
= talloc_zero(NULL
, struct trustdom_state
);
402 DEBUG(0, ("talloc failed\n"));
405 state
->domain
= domain
;
407 /* Called from timer, not from a real client */
408 client_name
= getprogname();
409 client_pid
= getpid();
411 req
= dcerpc_wbint_ListTrustedDomains_send(state
,
413 dom_child_handle(domain
),
418 DBG_ERR("dcerpc_wbint_ListTrustedDomains_send failed\n");
422 tevent_req_set_callback(req
, trustdom_list_done
, state
);
425 static void trustdom_list_done(struct tevent_req
*req
)
427 struct trustdom_state
*state
= tevent_req_callback_data(
428 req
, struct trustdom_state
);
429 bool within_forest
= false;
430 NTSTATUS status
, result
;
434 * Only when we enumerate our primary domain
435 * or our forest root domain, we should keep
436 * the NETR_TRUST_FLAG_IN_FOREST flag, in
437 * all other cases we need to clear it as the domain
438 * is not part of our forest.
440 if (state
->domain
->primary
) {
441 within_forest
= true;
442 } else if (domain_is_forest_root(state
->domain
)) {
443 within_forest
= true;
446 status
= dcerpc_wbint_ListTrustedDomains_recv(req
, state
, &result
);
447 if (any_nt_status_not_ok(status
, result
, &status
)) {
448 DBG_WARNING("Could not receive trusts for domain %s: %s-%s\n",
449 state
->domain
->name
, nt_errstr(status
),
455 for (i
=0; i
<state
->trusts
.count
; i
++) {
456 struct netr_DomainTrust
*trust
= &state
->trusts
.array
[i
];
457 struct winbindd_domain
*domain
= NULL
;
459 if (!within_forest
) {
460 trust
->trust_flags
&= ~NETR_TRUST_FLAG_IN_FOREST
;
463 if (!state
->domain
->primary
) {
464 trust
->trust_flags
&= ~NETR_TRUST_FLAG_PRIMARY
;
468 * We always call add_trusted_domain() cause on an existing
469 * domain structure, it will update the SID if necessary.
470 * This is important because we need the SID for sibling
473 status
= add_trusted_domain(trust
->netbios_name
,
478 trust
->trust_attributes
,
480 find_default_route_domain(),
482 if (!NT_STATUS_IS_OK(status
) &&
483 !NT_STATUS_EQUAL(status
, NT_STATUS_NO_SUCH_DOMAIN
))
485 DBG_NOTICE("add_trusted_domain returned %s\n",
492 Cases to consider when scanning trusts:
493 (a) we are calling from a child domain (primary && !forest_root)
494 (b) we are calling from the root of the forest (primary && forest_root)
495 (c) we are calling from a trusted forest domain (!primary
499 if (state
->domain
->primary
) {
500 /* If this is our primary domain and we are not in the
501 forest root, we have to scan the root trusts first */
503 if (!domain_is_forest_root(state
->domain
))
504 rescan_forest_root_trusts();
506 rescan_forest_trusts();
508 } else if (domain_is_forest_root(state
->domain
)) {
509 /* Once we have done root forest trust search, we can
510 go on to search the trusted forests */
512 rescan_forest_trusts();
520 /********************************************************************
521 Scan the trusts of our forest root
522 ********************************************************************/
524 static void rescan_forest_root_trusts( void )
526 struct winbindd_tdc_domain
*dom_list
= NULL
;
527 size_t num_trusts
= 0;
531 /* The only transitive trusts supported by Windows 2003 AD are
532 (a) Parent-Child, (b) Tree-Root, and (c) Forest. The
533 first two are handled in forest and listed by
534 DsEnumerateDomainTrusts(). Forest trusts are not so we
535 have to do that ourselves. */
537 if ( !wcache_tdc_fetch_list( &dom_list
, &num_trusts
) )
540 for ( i
=0; i
<num_trusts
; i
++ ) {
541 struct winbindd_domain
*d
= NULL
;
543 /* Find the forest root. Don't necessarily trust
544 the domain_list() as our primary domain may not
545 have been initialized. */
547 if ( !(dom_list
[i
].trust_flags
& NETR_TRUST_FLAG_TREEROOT
) ) {
551 /* Here's the forest root */
553 d
= find_domain_from_name_noinit( dom_list
[i
].domain_name
);
555 status
= add_trusted_domain(dom_list
[i
].domain_name
,
556 dom_list
[i
].dns_name
,
558 dom_list
[i
].trust_type
,
559 dom_list
[i
].trust_flags
,
560 dom_list
[i
].trust_attribs
,
562 find_default_route_domain(),
565 if (!NT_STATUS_IS_OK(status
) &&
566 NT_STATUS_EQUAL(status
, NT_STATUS_NO_SUCH_DOMAIN
))
568 DBG_ERR("add_trusted_domain returned %s\n",
577 DEBUG(10,("rescan_forest_root_trusts: Following trust path "
578 "for domain tree root %s (%s)\n",
579 d
->name
, d
->alt_name
));
581 d
->domain_flags
= dom_list
[i
].trust_flags
;
582 d
->domain_type
= dom_list
[i
].trust_type
;
583 d
->domain_trust_attribs
= dom_list
[i
].trust_attribs
;
585 add_trusted_domains( d
);
590 TALLOC_FREE( dom_list
);
595 /********************************************************************
596 scan the transitive forest trusts (not our own)
597 ********************************************************************/
600 static void rescan_forest_trusts( void )
602 struct winbindd_domain
*d
= NULL
;
603 struct winbindd_tdc_domain
*dom_list
= NULL
;
604 size_t num_trusts
= 0;
608 /* The only transitive trusts supported by Windows 2003 AD are
609 (a) Parent-Child, (b) Tree-Root, and (c) Forest. The
610 first two are handled in forest and listed by
611 DsEnumerateDomainTrusts(). Forest trusts are not so we
612 have to do that ourselves. */
614 if ( !wcache_tdc_fetch_list( &dom_list
, &num_trusts
) )
617 for ( i
=0; i
<num_trusts
; i
++ ) {
618 uint32_t flags
= dom_list
[i
].trust_flags
;
619 uint32_t type
= dom_list
[i
].trust_type
;
620 uint32_t attribs
= dom_list
[i
].trust_attribs
;
622 d
= find_domain_from_name_noinit( dom_list
[i
].domain_name
);
624 /* ignore our primary and internal domains */
626 if ( d
&& (d
->internal
|| d
->primary
) )
629 if ( (flags
& NETR_TRUST_FLAG_INBOUND
) &&
630 (type
== LSA_TRUST_TYPE_UPLEVEL
) &&
631 (attribs
& LSA_TRUST_ATTRIBUTE_FOREST_TRANSITIVE
) )
633 /* add the trusted domain if we don't know
637 status
= add_trusted_domain(
638 dom_list
[i
].domain_name
,
639 dom_list
[i
].dns_name
,
645 find_default_route_domain(),
647 if (!NT_STATUS_IS_OK(status
) &&
648 NT_STATUS_EQUAL(status
,
649 NT_STATUS_NO_SUCH_DOMAIN
))
651 DBG_ERR("add_trusted_domain: %s\n",
661 DEBUG(10,("Following trust path for domain %s (%s)\n",
662 d
->name
, d
->alt_name
));
663 add_trusted_domains( d
);
667 TALLOC_FREE( dom_list
);
672 /*********************************************************************
673 The process of updating the trusted domain list is a three step
676 (b) ask the root domain in our forest
677 (c) ask a DC in any Win2003 trusted forests
678 *********************************************************************/
680 void rescan_trusted_domains(struct tevent_context
*ev
, struct tevent_timer
*te
,
681 struct timeval now
, void *private_data
)
685 /* I used to clear the cache here and start over but that
686 caused problems in child processes that needed the
687 trust dom list early on. Removing it means we
688 could have some trusted domains listed that have been
689 removed from our primary domain's DC until a full
690 restart. This should be ok since I think this is what
691 Windows does as well. */
693 /* this will only add new domains we didn't already know about
694 in the domain_list()*/
696 add_trusted_domains( find_our_domain() );
698 te
= tevent_add_timer(
699 ev
, NULL
, timeval_current_ofs(WINBINDD_RESCAN_FREQ
, 0),
700 rescan_trusted_domains
, NULL
);
702 * If te == NULL, there's not much we can do here. Don't fail, the
703 * only thing we miss is new trusted domains.
709 static void wbd_ping_dc_done(struct tevent_req
*subreq
);
711 void winbindd_ping_offline_domains(struct tevent_context
*ev
,
712 struct tevent_timer
*te
,
716 struct winbindd_domain
*domain
= NULL
;
720 for (domain
= domain_list(); domain
!= NULL
; domain
= domain
->next
) {
721 DBG_DEBUG("Domain %s is %s\n",
723 domain
->online
? "online" : "offline");
725 if (get_global_winbindd_state_offline()) {
726 DBG_DEBUG("We are globally offline, do nothing.\n");
730 if (domain
->online
||
731 domain
->check_online_event
!= NULL
||
732 domain
->secure_channel_type
== SEC_CHAN_NULL
) {
736 winbindd_flush_negative_conn_cache(domain
);
738 domain
->check_online_event
=
739 dcerpc_wbint_PingDc_send(domain
,
741 dom_child_handle(domain
),
742 &domain
->ping_dcname
);
743 if (domain
->check_online_event
== NULL
) {
744 DBG_WARNING("Failed to schedule ping, no-memory\n");
748 tevent_req_set_callback(domain
->check_online_event
,
749 wbd_ping_dc_done
, domain
);
752 te
= tevent_add_timer(ev
,
754 timeval_current_ofs(lp_winbind_reconnect_delay(),
756 winbindd_ping_offline_domains
,
759 DBG_ERR("Failed to schedule winbindd_ping_offline_domains()\n");
765 static void wbd_ping_dc_done(struct tevent_req
*subreq
)
767 struct winbindd_domain
*domain
=
768 tevent_req_callback_data(subreq
,
769 struct winbindd_domain
);
770 NTSTATUS status
, result
;
772 SMB_ASSERT(subreq
== domain
->check_online_event
);
773 domain
->check_online_event
= NULL
;
775 status
= dcerpc_wbint_PingDc_recv(subreq
, domain
, &result
);
777 if (any_nt_status_not_ok(status
, result
, &status
)) {
778 DBG_WARNING("dcerpc_wbint_PingDc_recv failed for domain: "
785 DBG_DEBUG("dcerpc_wbint_PingDc_recv() succeeded, "
786 "domain: %s, dc-name: %s\n",
788 domain
->ping_dcname
);
790 talloc_free(discard_const(domain
->ping_dcname
));
791 domain
->ping_dcname
= NULL
;
796 static void wb_imsg_new_trusted_domain(struct imessaging_context
*msg
,
799 struct server_id server_id
,
807 DBG_WARNING("Received %zu fds, ignoring message\n", num_fds
);
811 DBG_NOTICE("Rescanning trusted domains\n");
813 ok
= add_trusted_domains_dc();
815 DBG_ERR("Failed to reload trusted domains\n");
820 * We did not get the secret when we queried secrets.tdb, so read it
821 * from secrets.tdb and re-sync the databases
823 static bool migrate_secrets_tdb_to_ldb(struct winbindd_domain
*domain
)
826 struct cli_credentials
*creds
;
827 NTSTATUS can_migrate
= pdb_get_trust_credentials(domain
->name
,
828 NULL
, domain
, &creds
);
829 if (!NT_STATUS_IS_OK(can_migrate
)) {
830 DEBUG(0, ("Failed to fetch our own local AD domain join "
831 "password for winbindd's internal use, both from "
832 "secrets.tdb and secrets.ldb: %s\n",
833 nt_errstr(can_migrate
)));
838 * NOTE: It is very unlikely we end up here if there is an
839 * oldpass, because a new password is created at
840 * classicupgrade, so this is not a concern.
842 ok
= secrets_store_machine_pw_sync(cli_credentials_get_password(creds
),
844 cli_credentials_get_domain(creds
),
845 cli_credentials_get_realm(creds
),
846 cli_credentials_get_salt_principal(creds
, creds
),
847 0, /* Supported enc types, unused */
849 cli_credentials_get_password_last_changed_time(creds
),
850 cli_credentials_get_secure_channel_type(creds
),
851 false /* do_delete: Do not delete */);
854 DEBUG(0, ("Failed to write our own "
855 "local AD domain join password for "
856 "winbindd's internal use into secrets.tdb\n"));
862 bool add_trusted_domains_dc(void)
864 struct winbindd_domain
*domain
= NULL
;
865 struct pdb_trusted_domain
**domains
= NULL
;
866 uint32_t num_domains
= 0;
870 if (!(pdb_capabilities() & PDB_CAP_TRUSTED_DOMAINS_EX
)) {
871 struct trustdom_info
**ti
= NULL
;
873 status
= pdb_enum_trusteddoms(talloc_tos(), &num_domains
, &ti
);
874 if (!NT_STATUS_IS_OK(status
)) {
875 DBG_ERR("pdb_enum_trusteddoms() failed - %s\n",
880 for (i
= 0; i
< num_domains
; i
++) {
881 status
= add_trusted_domain(ti
[i
]->name
,
884 LSA_TRUST_TYPE_DOWNLEVEL
,
885 NETR_TRUST_FLAG_OUTBOUND
,
890 if (!NT_STATUS_IS_OK(status
)) {
891 DBG_NOTICE("add_trusted_domain returned %s\n",
900 status
= pdb_enum_trusted_domains(talloc_tos(), &num_domains
, &domains
);
901 if (!NT_STATUS_IS_OK(status
)) {
902 DBG_ERR("pdb_enum_trusted_domains() failed - %s\n",
907 for (i
= 0; i
< num_domains
; i
++) {
908 enum netr_SchannelType sec_chan_type
= SEC_CHAN_DOMAIN
;
909 uint32_t trust_flags
= 0;
911 if (domains
[i
]->trust_type
== LSA_TRUST_TYPE_UPLEVEL
) {
912 sec_chan_type
= SEC_CHAN_DNS_DOMAIN
;
915 if (!(domains
[i
]->trust_direction
& LSA_TRUST_DIRECTION_OUTBOUND
)) {
916 sec_chan_type
= SEC_CHAN_NULL
;
919 if (domains
[i
]->trust_direction
& LSA_TRUST_DIRECTION_INBOUND
) {
920 trust_flags
|= NETR_TRUST_FLAG_INBOUND
;
922 if (domains
[i
]->trust_direction
& LSA_TRUST_DIRECTION_OUTBOUND
) {
923 trust_flags
|= NETR_TRUST_FLAG_OUTBOUND
;
925 if (domains
[i
]->trust_attributes
& LSA_TRUST_ATTRIBUTE_WITHIN_FOREST
) {
926 trust_flags
|= NETR_TRUST_FLAG_IN_FOREST
;
929 if (domains
[i
]->trust_attributes
& LSA_TRUST_ATTRIBUTE_CROSS_ORGANIZATION
) {
931 * We don't support selective authentication yet.
933 DBG_WARNING("Ignoring CROSS_ORGANIZATION trust to "
935 domains
[i
]->netbios_name
,
936 domains
[i
]->domain_name
);
940 status
= add_trusted_domain(domains
[i
]->netbios_name
,
941 domains
[i
]->domain_name
,
942 &domains
[i
]->security_identifier
,
943 domains
[i
]->trust_type
,
945 domains
[i
]->trust_attributes
,
949 if (!NT_STATUS_IS_OK(status
)) {
950 DBG_NOTICE("add_trusted_domain returned %s\n",
956 for (i
= 0; i
< num_domains
; i
++) {
957 struct ForestTrustInfo fti
;
959 enum ndr_err_code ndr_err
;
960 struct winbindd_domain
*routing_domain
= NULL
;
962 if (domains
[i
]->trust_type
!= LSA_TRUST_TYPE_UPLEVEL
) {
966 if (!(domains
[i
]->trust_attributes
& LSA_TRUST_ATTRIBUTE_FOREST_TRANSITIVE
)) {
970 if (domains
[i
]->trust_forest_trust_info
.length
== 0) {
974 routing_domain
= find_domain_from_name_noinit(
975 domains
[i
]->netbios_name
);
976 if (routing_domain
== NULL
) {
977 DBG_ERR("Can't find winbindd domain [%s]\n",
978 domains
[i
]->netbios_name
);
982 ndr_err
= ndr_pull_struct_blob_all(
983 &domains
[i
]->trust_forest_trust_info
,
985 (ndr_pull_flags_fn_t
)ndr_pull_ForestTrustInfo
);
986 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
987 DBG_ERR("ndr_pull_ForestTrustInfo(%s) - %s\n",
988 domains
[i
]->netbios_name
,
989 ndr_map_error2string(ndr_err
));
993 for (fi
= 0; fi
< fti
.count
; fi
++) {
994 struct ForestTrustInfoRecord
*rec
=
995 &fti
.records
[fi
].record
;
996 struct ForestTrustDataDomainInfo
*drec
= NULL
;
998 if (rec
->type
!= FOREST_TRUST_DOMAIN_INFO
) {
1001 drec
= &rec
->data
.info
;
1003 if (rec
->flags
& LSA_NB_DISABLED_MASK
) {
1007 if (rec
->flags
& LSA_SID_DISABLED_MASK
) {
1013 * also try to find a matching
1014 * LSA_TLN_DISABLED_MASK ???
1017 domain
= find_domain_from_name_noinit(drec
->netbios_name
.string
);
1018 if (domain
!= NULL
) {
1022 status
= add_trusted_domain(drec
->netbios_name
.string
,
1023 drec
->dns_name
.string
,
1025 LSA_TRUST_TYPE_UPLEVEL
,
1026 NETR_TRUST_FLAG_OUTBOUND
,
1031 if (!NT_STATUS_IS_OK(status
)) {
1032 DBG_NOTICE("add_trusted_domain returned %s\n",
1036 if (domain
== NULL
) {
1046 /* Look up global info for the winbind daemon */
1047 bool init_domain_list(void)
1049 int role
= lp_server_role();
1050 struct pdb_domain_info
*pdb_domain_info
= NULL
;
1051 struct winbindd_domain
*domain
= NULL
;
1055 /* Free existing list */
1058 /* BUILTIN domain */
1060 status
= add_trusted_domain("BUILTIN",
1062 &global_sid_Builtin
,
1063 LSA_TRUST_TYPE_DOWNLEVEL
,
1064 0, /* trust_flags */
1065 0, /* trust_attribs */
1069 if (!NT_STATUS_IS_OK(status
)) {
1070 DBG_ERR("add_trusted_domain BUILTIN returned %s\n",
1078 * In case the passdb backend is passdb_dsdb the domain SID comes from
1079 * dsdb, not from secrets.tdb. As we use the domain SID in various
1080 * places, we must ensure the domain SID is migrated from dsdb to
1081 * secrets.tdb before get_global_sam_sid() is called the first time.
1083 * The migration is done as part of the passdb_dsdb initialisation,
1084 * calling pdb_get_domain_info() triggers it.
1086 pdb_domain_info
= pdb_get_domain_info(talloc_tos());
1088 if ( role
== ROLE_ACTIVE_DIRECTORY_DC
) {
1089 uint32_t trust_flags
;
1091 enum netr_SchannelType sec_chan_type
;
1092 const char *account_name
;
1093 struct samr_Password current_nt_hash
;
1095 if (pdb_domain_info
== NULL
) {
1096 DEBUG(0, ("Failed to fetch our own local AD "
1097 "domain info from sam.ldb\n"));
1101 trust_flags
= NETR_TRUST_FLAG_PRIMARY
;
1102 trust_flags
|= NETR_TRUST_FLAG_IN_FOREST
;
1103 trust_flags
|= NETR_TRUST_FLAG_NATIVE
;
1104 trust_flags
|= NETR_TRUST_FLAG_OUTBOUND
;
1106 is_root
= strequal(pdb_domain_info
->dns_domain
,
1107 pdb_domain_info
->dns_forest
);
1109 trust_flags
|= NETR_TRUST_FLAG_TREEROOT
;
1112 status
= add_trusted_domain(pdb_domain_info
->name
,
1113 pdb_domain_info
->dns_domain
,
1114 &pdb_domain_info
->sid
,
1115 LSA_TRUST_TYPE_UPLEVEL
,
1117 LSA_TRUST_ATTRIBUTE_WITHIN_FOREST
,
1121 TALLOC_FREE(pdb_domain_info
);
1122 if (!NT_STATUS_IS_OK(status
)) {
1123 DBG_ERR("Failed to add our own local AD "
1124 "domain to winbindd's internal list\n");
1129 * We need to call this to find out if we are an RODC
1131 ok
= get_trust_pw_hash(domain
->name
,
1132 current_nt_hash
.hash
,
1137 * If get_trust_pw_hash() fails, then try and
1138 * fetch the password from the more recent of
1139 * secrets.{ldb,tdb} using the
1140 * pdb_get_trust_credentials()
1142 ok
= migrate_secrets_tdb_to_ldb(domain
);
1145 DEBUG(0, ("Failed to migrate our own "
1146 "local AD domain join password for "
1147 "winbindd's internal use into "
1151 ok
= get_trust_pw_hash(domain
->name
,
1152 current_nt_hash
.hash
,
1156 DEBUG(0, ("Failed to find our own just "
1157 "written local AD domain join "
1158 "password for winbindd's internal "
1159 "use in secrets.tdb\n"));
1164 domain
->secure_channel_type
= sec_chan_type
;
1165 if (sec_chan_type
== SEC_CHAN_RODC
) {
1166 domain
->rodc
= true;
1170 uint32_t trust_flags
;
1171 enum netr_SchannelType secure_channel_type
;
1173 trust_flags
= NETR_TRUST_FLAG_OUTBOUND
;
1174 if (role
!= ROLE_DOMAIN_MEMBER
) {
1175 trust_flags
|= NETR_TRUST_FLAG_PRIMARY
;
1178 if (role
> ROLE_DOMAIN_MEMBER
) {
1179 secure_channel_type
= SEC_CHAN_BDC
;
1181 secure_channel_type
= SEC_CHAN_LOCAL
;
1184 if ((pdb_domain_info
!= NULL
) && (role
== ROLE_IPA_DC
)) {
1185 /* This is IPA DC that presents itself as
1186 * an Active Directory domain controller to trusted AD
1187 * forests but in fact is a classic domain controller.
1189 trust_flags
= NETR_TRUST_FLAG_PRIMARY
;
1190 trust_flags
|= NETR_TRUST_FLAG_IN_FOREST
;
1191 trust_flags
|= NETR_TRUST_FLAG_NATIVE
;
1192 trust_flags
|= NETR_TRUST_FLAG_OUTBOUND
;
1193 trust_flags
|= NETR_TRUST_FLAG_TREEROOT
;
1194 status
= add_trusted_domain(pdb_domain_info
->name
,
1195 pdb_domain_info
->dns_domain
,
1196 &pdb_domain_info
->sid
,
1197 LSA_TRUST_TYPE_UPLEVEL
,
1199 LSA_TRUST_ATTRIBUTE_WITHIN_FOREST
,
1200 secure_channel_type
,
1203 TALLOC_FREE(pdb_domain_info
);
1205 status
= add_trusted_domain(get_global_sam_name(),
1207 get_global_sam_sid(),
1208 LSA_TRUST_TYPE_DOWNLEVEL
,
1210 0, /* trust_attribs */
1211 secure_channel_type
,
1215 if (!NT_STATUS_IS_OK(status
)) {
1216 DBG_ERR("Failed to add local SAM to "
1217 "domain to winbindd's internal list\n");
1223 ok
= add_trusted_domains_dc();
1225 DBG_ERR("init_domain_list_dc failed\n");
1230 if ( role
== ROLE_DOMAIN_MEMBER
) {
1231 struct dom_sid our_sid
;
1232 uint32_t trust_type
;
1234 if (!secrets_fetch_domain_sid(lp_workgroup(), &our_sid
)) {
1235 DEBUG(0, ("Could not fetch our SID - did we join?\n"));
1239 if (lp_realm() != NULL
) {
1240 trust_type
= LSA_TRUST_TYPE_UPLEVEL
;
1242 trust_type
= LSA_TRUST_TYPE_DOWNLEVEL
;
1245 status
= add_trusted_domain(lp_workgroup(),
1249 NETR_TRUST_FLAG_PRIMARY
|
1250 NETR_TRUST_FLAG_OUTBOUND
,
1251 0, /* trust_attribs */
1255 if (!NT_STATUS_IS_OK(status
)) {
1256 DBG_ERR("Failed to add local SAM to "
1257 "domain to winbindd's internal list\n");
1262 status
= imessaging_register(winbind_imessaging_context(), NULL
,
1263 MSG_WINBIND_RELOAD_TRUSTED_DOMAINS
,
1264 wb_imsg_new_trusted_domain
);
1265 if (!NT_STATUS_IS_OK(status
)) {
1266 DBG_ERR("imessaging_register failed %s\n", nt_errstr(status
));
1274 * Given a domain name, return the struct winbindd domain info for it
1276 * @note Do *not* pass lp_workgroup() to this function. domain_list
1277 * may modify it's value, and free that pointer. Instead, our local
1278 * domain may be found by calling find_our_domain().
1282 * @return The domain structure for the named domain, if it is working.
1285 struct winbindd_domain
*find_domain_from_name_noinit(const char *domain_name
)
1287 struct winbindd_domain
*domain
;
1289 /* Search through list */
1291 for (domain
= domain_list(); domain
!= NULL
; domain
= domain
->next
) {
1292 if (strequal(domain_name
, domain
->name
)) {
1295 if (domain
->alt_name
== NULL
) {
1298 if (strequal(domain_name
, domain
->alt_name
)) {
1309 * Given a domain name, return the struct winbindd domain if it's a direct
1312 * @return The domain structure for the named domain, if it is a direct outgoing trust
1314 struct winbindd_domain
*find_trust_from_name_noinit(const char *domain_name
)
1316 struct winbindd_domain
*domain
= NULL
;
1318 domain
= find_domain_from_name_noinit(domain_name
);
1319 if (domain
== NULL
) {
1323 if (domain
->secure_channel_type
!= SEC_CHAN_NULL
) {
1330 struct winbindd_domain
*find_domain_from_name(const char *domain_name
)
1332 struct winbindd_domain
*domain
;
1334 domain
= find_domain_from_name_noinit(domain_name
);
1339 if (!domain
->initialized
)
1340 init_dc_connection(domain
, false);
1345 /* Given a domain sid, return the struct winbindd domain info for it */
1347 struct winbindd_domain
*find_domain_from_sid_noinit(const struct dom_sid
*sid
)
1349 struct winbindd_domain
*domain
;
1351 /* Search through list */
1353 for (domain
= domain_list(); domain
!= NULL
; domain
= domain
->next
) {
1354 if (dom_sid_compare_domain(sid
, &domain
->sid
) == 0)
1364 * Given a domain sid, return the struct winbindd domain if it's a direct
1367 * @return The domain structure for the specified domain, if it is a direct outgoing trust
1369 struct winbindd_domain
*find_trust_from_sid_noinit(const struct dom_sid
*sid
)
1371 struct winbindd_domain
*domain
= NULL
;
1373 domain
= find_domain_from_sid_noinit(sid
);
1374 if (domain
== NULL
) {
1378 if (domain
->secure_channel_type
!= SEC_CHAN_NULL
) {
1385 /* Given a domain sid, return the struct winbindd domain info for it */
1387 struct winbindd_domain
*find_domain_from_sid(const struct dom_sid
*sid
)
1389 struct winbindd_domain
*domain
;
1391 domain
= find_domain_from_sid_noinit(sid
);
1396 if (!domain
->initialized
)
1397 init_dc_connection(domain
, false);
1402 struct winbindd_domain
*find_our_domain(void)
1404 struct winbindd_domain
*domain
;
1406 /* Search through list */
1408 for (domain
= domain_list(); domain
!= NULL
; domain
= domain
->next
) {
1409 if (domain
->primary
)
1413 smb_panic("Could not find our domain");
1417 struct winbindd_domain
*find_default_route_domain(void)
1420 return find_our_domain();
1422 DBG_DEBUG("Routing logic not yet implemented on a DC\n");
1426 /* Find the appropriate domain to lookup a name or SID */
1428 struct winbindd_domain
*find_lookup_domain_from_sid(const struct dom_sid
*sid
)
1430 struct dom_sid_buf buf
;
1432 DBG_DEBUG("SID [%s]\n", dom_sid_str_buf(sid
, &buf
));
1435 * SIDs in the S-1-22-{1,2} domain and well-known SIDs should be handled
1439 if ( sid_check_is_in_unix_groups(sid
) ||
1440 sid_check_is_unix_groups(sid
) ||
1441 sid_check_is_in_unix_users(sid
) ||
1442 sid_check_is_unix_users(sid
) ||
1443 sid_check_is_our_sam(sid
) ||
1444 sid_check_is_in_our_sam(sid
) )
1446 return find_domain_from_sid(get_global_sam_sid());
1449 if ( sid_check_is_builtin(sid
) ||
1450 sid_check_is_in_builtin(sid
) ||
1451 sid_check_is_wellknown_domain(sid
, NULL
) ||
1452 sid_check_is_in_wellknown_domain(sid
) )
1454 return find_domain_from_sid(&global_sid_Builtin
);
1458 struct winbindd_domain
*domain
= NULL
;
1460 domain
= find_domain_from_sid_noinit(sid
);
1461 if (domain
== NULL
) {
1465 if (domain
->secure_channel_type
!= SEC_CHAN_NULL
) {
1469 return domain
->routing_domain
;
1472 /* On a member server a query for SID or name can always go to our
1475 DEBUG(10, ("calling find_our_domain\n"));
1476 return find_our_domain();
1479 struct winbindd_domain
*find_lookup_domain_from_name(const char *domain_name
)
1483 if ( strequal(domain_name
, unix_users_domain_name() ) ||
1484 strequal(domain_name
, unix_groups_domain_name() ) )
1487 * The "Unix User" and "Unix Group" domain are handled by
1490 return find_domain_from_name_noinit( get_global_sam_name() );
1493 if (strequal(domain_name
, "BUILTIN") ||
1494 strequal(domain_name
, get_global_sam_name())) {
1495 return find_domain_from_name_noinit(domain_name
);
1498 predefined
= dom_sid_lookup_is_predefined_domain(domain_name
);
1500 return find_domain_from_name_noinit(builtin_domain_name());
1504 struct winbindd_domain
*domain
= NULL
;
1506 domain
= find_domain_from_name_noinit(domain_name
);
1507 if (domain
== NULL
) {
1511 if (domain
->secure_channel_type
!= SEC_CHAN_NULL
) {
1515 return domain
->routing_domain
;
1518 return find_our_domain();
1521 /* Is this a domain which we may assume no DOMAIN\ prefix? */
1523 static bool assume_domain(const char *domain
)
1525 /* never assume the domain on a standalone server */
1527 if ( lp_server_role() == ROLE_STANDALONE
)
1530 /* domain member servers may possibly assume for the domain name */
1532 if ( lp_server_role() == ROLE_DOMAIN_MEMBER
) {
1533 if ( !strequal(lp_workgroup(), domain
) )
1536 if ( lp_winbind_use_default_domain() )
1540 /* only left with a domain controller */
1542 if ( strequal(get_global_sam_name(), domain
) ) {
1549 /* Parse a DOMAIN\user or UPN string into a domain, namespace and a user */
1550 bool parse_domain_user(TALLOC_CTX
*ctx
,
1551 const char *domuser
,
1557 char *namespace = NULL
;
1558 char *domain
= NULL
;
1561 if (strlen(domuser
) == 0) {
1565 p
= strchr(domuser
, *lp_winbind_separator());
1567 user
= talloc_strdup(ctx
, p
+ 1);
1571 domain
= talloc_strdup(ctx
,
1573 if (domain
== NULL
) {
1576 domain
[PTR_DIFF(p
, domuser
)] = '\0';
1577 namespace = talloc_strdup(ctx
, domain
);
1578 if (namespace == NULL
) {
1582 user
= talloc_strdup(ctx
, domuser
);
1586 p
= strchr(domuser
, '@');
1589 namespace = talloc_strdup(ctx
, p
+ 1);
1590 if (namespace == NULL
) {
1593 domain
= talloc_strdup(ctx
, "");
1594 if (domain
== NULL
) {
1598 } else if (assume_domain(lp_workgroup())) {
1599 domain
= talloc_strdup(ctx
, lp_workgroup());
1600 if (domain
== NULL
) {
1603 namespace = talloc_strdup(ctx
, domain
);
1604 if (namespace == NULL
) {
1608 namespace = talloc_strdup(ctx
, lp_netbios_name());
1609 if (namespace == NULL
) {
1612 domain
= talloc_strdup(ctx
, "");
1613 if (domain
== NULL
) {
1619 if (!strupper_m(domain
)) {
1623 *pnamespace
= namespace;
1629 TALLOC_FREE(domain
);
1630 TALLOC_FREE(namespace);
1634 bool canonicalize_username(TALLOC_CTX
*mem_ctx
,
1635 char **pusername_inout
,
1641 char *namespace = NULL
;
1642 char *domain
= NULL
;
1644 char *username_inout
= NULL
;
1646 ok
= parse_domain_user(mem_ctx
,
1648 &namespace, &domain
, &user
);
1654 username_inout
= talloc_asprintf(mem_ctx
, "%s%c%s",
1655 domain
, *lp_winbind_separator(),
1658 if (username_inout
== NULL
) {
1662 *pnamespace
= namespace;
1665 *pusername_inout
= username_inout
;
1668 TALLOC_FREE(username_inout
);
1669 TALLOC_FREE(namespace);
1670 TALLOC_FREE(domain
);
1676 Fill DOMAIN\\USERNAME entry accounting 'winbind use default domain' and
1677 'winbind separator' options.
1679 - omit DOMAIN when 'winbind use default domain = true' and DOMAIN is
1682 If we are a PDC or BDC, and this is for our domain, do likewise.
1684 On an AD DC we always fill DOMAIN\\USERNAME.
1686 We always canonicalize as UPPERCASE DOMAIN, lowercase username.
1689 * talloc version of fill_domain_username()
1690 * return NULL on talloc failure.
1692 char *fill_domain_username_talloc(TALLOC_CTX
*mem_ctx
,
1697 char *tmp_user
, *name
;
1699 if (lp_server_role() == ROLE_ACTIVE_DIRECTORY_DC
) {
1707 tmp_user
= talloc_strdup(mem_ctx
, user
);
1708 if (tmp_user
== NULL
) {
1711 if (!strlower_m(tmp_user
)) {
1712 TALLOC_FREE(tmp_user
);
1716 if (can_assume
&& assume_domain(domain
)) {
1719 name
= talloc_asprintf(mem_ctx
, "%s%c%s",
1721 *lp_winbind_separator(),
1723 TALLOC_FREE(tmp_user
);
1730 * Client list accessor functions
1733 static struct winbindd_cli_state
*_client_list
;
1734 static int _num_clients
;
1736 /* Return list of all connected clients */
1738 struct winbindd_cli_state
*winbindd_client_list(void)
1740 return _client_list
;
1743 /* Return list-tail of all connected clients */
1745 struct winbindd_cli_state
*winbindd_client_list_tail(void)
1747 return DLIST_TAIL(_client_list
);
1750 /* Return previous (read:newer) client in list */
1752 struct winbindd_cli_state
*
1753 winbindd_client_list_prev(struct winbindd_cli_state
*cli
)
1755 return DLIST_PREV(cli
);
1758 /* Add a connection to the list */
1760 void winbindd_add_client(struct winbindd_cli_state
*cli
)
1762 cli
->last_access
= time(NULL
);
1763 DLIST_ADD(_client_list
, cli
);
1767 /* Remove a client from the list */
1769 void winbindd_remove_client(struct winbindd_cli_state
*cli
)
1771 DLIST_REMOVE(_client_list
, cli
);
1775 /* Move a client to head or list */
1777 void winbindd_promote_client(struct winbindd_cli_state
*cli
)
1779 cli
->last_access
= time(NULL
);
1780 DLIST_PROMOTE(_client_list
, cli
);
1783 /* Return number of open clients */
1785 int winbindd_num_clients(void)
1787 return _num_clients
;
1790 NTSTATUS
lookup_usergroups_cached(TALLOC_CTX
*mem_ctx
,
1791 const struct dom_sid
*user_sid
,
1792 uint32_t *p_num_groups
, struct dom_sid
**user_sids
)
1794 struct netr_SamInfo3
*info3
= NULL
;
1795 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
1796 uint32_t num_groups
= 0;
1798 DEBUG(3,(": lookup_usergroups_cached\n"));
1803 info3
= netsamlogon_cache_get(mem_ctx
, user_sid
);
1805 if (info3
== NULL
) {
1806 return NT_STATUS_OBJECT_NAME_NOT_FOUND
;
1810 * Before bug #7843 the "Domain Local" groups were added with a
1811 * lookupuseraliases call, but this isn't done anymore for our domain
1812 * so we need to resolve resource groups here.
1814 * When to use Resource Groups:
1815 * http://technet.microsoft.com/en-us/library/cc753670%28v=WS.10%29.aspx
1817 status
= sid_array_from_info3(mem_ctx
, info3
,
1822 if (!NT_STATUS_IS_OK(status
)) {
1828 *p_num_groups
= num_groups
;
1829 status
= (user_sids
!= NULL
) ? NT_STATUS_OK
: NT_STATUS_NO_MEMORY
;
1831 DEBUG(3,(": lookup_usergroups_cached succeeded\n"));
1836 /*********************************************************************
1837 We use this to remove spaces from user and group names
1838 ********************************************************************/
1840 NTSTATUS
normalize_name_map(TALLOC_CTX
*mem_ctx
,
1841 const char *domain_name
,
1845 struct winbindd_domain
*domain
= NULL
;
1848 if (!name
|| !normalized
) {
1849 return NT_STATUS_INVALID_PARAMETER
;
1852 if (!lp_winbind_normalize_names()) {
1853 return NT_STATUS_PROCEDURE_NOT_FOUND
;
1856 domain
= find_domain_from_name_noinit(domain_name
);
1857 if (domain
== NULL
) {
1858 DBG_ERR("Failed to find domain '%s'\n", domain_name
);
1859 return NT_STATUS_NO_SUCH_DOMAIN
;
1862 /* Alias support and whitespace replacement are mutually
1865 nt_status
= resolve_username_to_alias(mem_ctx
, domain
,
1867 if (NT_STATUS_IS_OK(nt_status
)) {
1868 /* special return code to let the caller know we
1869 mapped to an alias */
1870 return NT_STATUS_FILE_RENAMED
;
1873 /* check for an unreachable domain */
1875 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
)) {
1876 DEBUG(5,("normalize_name_map: Setting domain %s offline\n",
1878 set_domain_offline(domain
);
1882 /* deal with whitespace */
1884 *normalized
= talloc_strdup(mem_ctx
, name
);
1885 if (!(*normalized
)) {
1886 return NT_STATUS_NO_MEMORY
;
1889 all_string_sub( *normalized
, " ", "_", 0 );
1891 return NT_STATUS_OK
;
1894 /*********************************************************************
1895 We use this to do the inverse of normalize_name_map()
1896 ********************************************************************/
1898 NTSTATUS
normalize_name_unmap(TALLOC_CTX
*mem_ctx
,
1903 struct winbindd_domain
*domain
= find_our_domain();
1905 if (!name
|| !normalized
) {
1906 return NT_STATUS_INVALID_PARAMETER
;
1909 if (!lp_winbind_normalize_names()) {
1910 return NT_STATUS_PROCEDURE_NOT_FOUND
;
1913 /* Alias support and whitespace replacement are mutally
1916 /* When mapping from an alias to a username, we don't know the
1917 domain. But we only need a domain structure to cache
1918 a successful lookup , so just our own domain structure for
1921 nt_status
= resolve_alias_to_username(mem_ctx
, domain
,
1923 if (NT_STATUS_IS_OK(nt_status
)) {
1924 /* Special return code to let the caller know we mapped
1926 return NT_STATUS_FILE_RENAMED
;
1929 /* check for an unreachable domain */
1931 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND
)) {
1932 DEBUG(5,("normalize_name_unmap: Setting domain %s offline\n",
1934 set_domain_offline(domain
);
1938 /* deal with whitespace */
1940 *normalized
= talloc_strdup(mem_ctx
, name
);
1941 if (!(*normalized
)) {
1942 return NT_STATUS_NO_MEMORY
;
1945 all_string_sub(*normalized
, "_", " ", 0);
1947 return NT_STATUS_OK
;
1950 /*********************************************************************
1951 ********************************************************************/
1953 bool winbindd_can_contact_domain(struct winbindd_domain
*domain
)
1955 struct winbindd_tdc_domain
*tdc
= NULL
;
1956 TALLOC_CTX
*frame
= talloc_stackframe();
1959 /* We can contact the domain if it is our primary domain */
1961 if (domain
->primary
) {
1966 /* Trust the TDC cache and not the winbindd_domain flags */
1968 if ((tdc
= wcache_tdc_fetch_domain(frame
, domain
->name
)) == NULL
) {
1969 DEBUG(10,("winbindd_can_contact_domain: %s not found in cache\n",
1975 /* Can always contact a domain that is in out forest */
1977 if (tdc
->trust_flags
& NETR_TRUST_FLAG_IN_FOREST
) {
1983 * On a _member_ server, we cannot contact the domain if it
1984 * is running AD and we have no inbound trust.
1988 domain
->active_directory
&&
1989 ((tdc
->trust_flags
& NETR_TRUST_FLAG_INBOUND
) != NETR_TRUST_FLAG_INBOUND
))
1991 DEBUG(10, ("winbindd_can_contact_domain: %s is an AD domain "
1992 "and we have no inbound trust.\n", domain
->name
));
1996 /* Assume everything else is ok (probably not true but what
2002 talloc_destroy(frame
);
2007 #ifdef HAVE_KRB5_LOCATE_PLUGIN_H
2009 /*********************************************************************
2010 ********************************************************************/
2012 static void winbindd_set_locator_kdc_env(const struct winbindd_domain
*domain
)
2015 char addr
[INET6_ADDRSTRLEN
];
2016 const char *kdc
= NULL
;
2019 if (!domain
|| !domain
->alt_name
|| !*domain
->alt_name
) {
2023 if (domain
->initialized
&& !domain
->active_directory
) {
2024 DEBUG(lvl
,("winbindd_set_locator_kdc_env: %s not AD\n",
2029 print_sockaddr(addr
, sizeof(addr
), &domain
->dcaddr
);
2032 DEBUG(lvl
,("winbindd_set_locator_kdc_env: %s no DC IP\n",
2034 kdc
= domain
->dcname
;
2037 if (!kdc
|| !*kdc
) {
2038 DEBUG(lvl
,("winbindd_set_locator_kdc_env: %s no DC at all\n",
2043 var
= talloc_asprintf_strupper_m(
2046 WINBINDD_LOCATOR_KDC_ADDRESS
,
2052 DEBUG(lvl
,("winbindd_set_locator_kdc_env: setting var: %s to: %s\n",
2055 setenv(var
, kdc
, 1);
2059 /*********************************************************************
2060 ********************************************************************/
2062 void winbindd_set_locator_kdc_envs(const struct winbindd_domain
*domain
)
2064 struct winbindd_domain
*our_dom
= find_our_domain();
2066 winbindd_set_locator_kdc_env(domain
);
2068 if (domain
!= our_dom
) {
2069 winbindd_set_locator_kdc_env(our_dom
);
2073 /*********************************************************************
2074 ********************************************************************/
2076 void winbindd_unset_locator_kdc_env(const struct winbindd_domain
*domain
)
2080 if (!domain
|| !domain
->alt_name
|| !*domain
->alt_name
) {
2084 var
= talloc_asprintf_strupper_m(
2087 WINBINDD_LOCATOR_KDC_ADDRESS
,
2098 void winbindd_set_locator_kdc_envs(const struct winbindd_domain
*domain
)
2103 void winbindd_unset_locator_kdc_env(const struct winbindd_domain
*domain
)
2108 #endif /* HAVE_KRB5_LOCATE_PLUGIN_H */
2110 void set_auth_errors(struct winbindd_response
*resp
, NTSTATUS result
)
2113 * Make sure we start with authoritative=true,
2114 * it will only set to false if we don't know the
2117 resp
->data
.auth
.authoritative
= true;
2119 resp
->data
.auth
.nt_status
= NT_STATUS_V(result
);
2120 fstrcpy(resp
->data
.auth
.nt_status_string
, nt_errstr(result
));
2122 /* we might have given a more useful error above */
2123 if (*resp
->data
.auth
.error_string
== '\0')
2124 fstrcpy(resp
->data
.auth
.error_string
,
2125 get_friendly_nt_error_msg(result
));
2126 resp
->data
.auth
.pam_error
= nt_status_to_pam(result
);
2129 bool is_domain_offline(const struct winbindd_domain
*domain
)
2131 if (get_global_winbindd_state_offline()) {
2134 return !domain
->online
;
2137 bool is_domain_online(const struct winbindd_domain
*domain
)
2139 return !is_domain_offline(domain
);
2143 * Parse an char array into a list of sids.
2145 * The input sidstr should consist of 0-terminated strings
2146 * representing sids, separated by newline characters '\n'.
2147 * The list is terminated by an empty string, i.e.
2148 * character '\0' directly following a character '\n'
2149 * (or '\0' right at the start of sidstr).
2151 bool parse_sidlist(TALLOC_CTX
*mem_ctx
, const char *sidstr
,
2152 struct dom_sid
**sids
, uint32_t *num_sids
)
2160 while (p
[0] != '\0') {
2162 const char *q
= NULL
;
2164 if (!dom_sid_parse_endp(p
, &sid
, &q
)) {
2165 DEBUG(1, ("Could not parse sid %s\n", p
));
2169 DEBUG(1, ("Got invalid sidstr: %s\n", p
));
2172 if (!NT_STATUS_IS_OK(add_sid_to_array(mem_ctx
, &sid
, sids
,
2182 bool parse_xidlist(TALLOC_CTX
*mem_ctx
, const char *xidstr
,
2183 struct unixid
**pxids
, uint32_t *pnum_xids
)
2186 struct unixid
*xids
= NULL
;
2187 uint32_t num_xids
= 0;
2194 while (p
[0] != '\0') {
2197 unsigned long long id
;
2203 xid
= (struct unixid
) { .type
= ID_TYPE_UID
};
2206 xid
= (struct unixid
) { .type
= ID_TYPE_GID
};
2214 id
= smb_strtoull(p
, &endp
, 10, &error
, SMB_STR_STANDARD
);
2218 if (*endp
!= '\n') {
2224 if ((unsigned long long)xid
.id
!= id
) {
2228 tmp
= talloc_realloc(mem_ctx
, xids
, struct unixid
, num_xids
+1);
2234 xids
[num_xids
] = xid
;
2239 *pnum_xids
= num_xids
;