376
376
return NT_STATUS_NO_LOGON_SERVERS;
379
/*********************************************************************
380
*********************************************************************/
382
NTSTATUS ads_lookup_site(void)
384
ADS_STRUCT *ads = NULL;
385
ADS_STATUS ads_status;
386
NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
387
struct cldap_netlogon_reply cldap_reply;
389
ZERO_STRUCT(cldap_reply);
391
ads = ads_init(lp_realm(), NULL, NULL);
393
return NT_STATUS_NO_MEMORY;
396
/* The NO_BIND here will find a DC and set the client site
397
but not establish the TCP connection */
399
ads->auth.flags = ADS_AUTH_NO_BIND;
400
ads_status = ads_connect(ads);
401
if (!ADS_ERR_OK(ads_status)) {
402
DEBUG(4, ("ads_lookup_site: ads_connect to our realm failed! (%s)\n",
403
ads_errstr(ads_status)));
405
nt_status = ads_ntstatus(ads_status);
414
/*********************************************************************
415
*********************************************************************/
417
const char* host_dns_domain(const char *fqdn)
419
const char *p = fqdn;
421
while (p && *p != '.') {
425
/* go to next char following '.' */
435
* Sorting function for figuring out which GC servers which
439
static int gc_weight_cmp(void *a, void *b)
441
struct dns_rr_srv *rec1 = (struct dns_rr_srv*)a;
442
struct dns_rr_srv *rec2 = (struct dns_rr_srv*)b;
443
const char *domain1 = NULL;
444
const char *domain2 = NULL;
446
/* Get the trivial cases out of the way */
448
if (a == b) return 0;
452
domain1 = host_dns_domain(rec1->hostname);
453
domain2 = host_dns_domain(rec2->hostname);
455
/* Prefer fully qualified hosts */
457
if (!domain1) return 1;
458
if (!domain2) return -1;
460
if (strequal(domain1, domain2)) {
464
/* Prefer hosts in our realm */
466
if (strequal(domain1, lp_realm())) {
469
if (strequal(domain2, lp_realm())) {
473
/* This should never happen */
380
479
* Connect to the Global Catalog server
381
480
* @param ads Pointer to an existing ADS_STRUCT
401
500
realm = lp_realm();
403
sitename = sitename_fetch(realm);
502
if ((sitename = sitename_fetch(realm)) == NULL) {
504
sitename = sitename_fetch(realm);
406
508
/* We try once with a sitename and once without
407
509
(unless we don't have a sitename and then we're
525
/* Sort the list so that GCs closest to us or in our own domain
526
are selected first */
530
sizeof(struct dns_rr_srv),
531
QSORT_CAST gc_weight_cmp);
533
if (DEBUGLEVEL >= 10) {
534
DEBUG(10,("ads_connect_gc: Sorted GC list:\n"));
535
for (i=0; i<num_gcs; i++) {
536
DEBUGADD(10,(" %s\n", gcs_list[i].hostname));
423
541
/* Loop until we get a successful connection or have gone
542
through them all. When connecting a GC server, make sure that
543
the realm is the server's DNS name and not the forest root */
426
545
for (i=0; i<num_gcs; i++) {
427
546
ads->server.gc = true;
428
547
ads->server.ldap_server = SMB_STRDUP(gcs_list[i].hostname);
548
ads->server.realm = SMB_STRDUP(host_dns_domain(ads->server.ldap_server));
429
549
ads_status = ads_connect(ads);
430
550
if (ADS_ERR_OK(ads_status)) {
433
553
SAFE_FREE(ads->server.ldap_server);
554
SAFE_FREE(ads->server.realm);
436
557
TALLOC_FREE(gcs_list);