~zulcss/ubuntu/lucid/likewise-open/likewise-open-sru

« back to all changes in this revision

Viewing changes to samba/source/libads/ldap.c

  • Committer: Bazaar Package Importer
  • Author(s): Rick Clark
  • Date: 2008-08-27 08:56:20 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20080827085620-5q0f58b9qtog9myq
Tags: 4.1.0.2956-0ubuntu1
* missing-likewise-logo.diff: removed
* fixed copyright notice
* updated Standards-Version to 3.8.0
* removed path from command in prerm
* removed stop in S runlevel

Show diffs side-by-side

added added

removed removed

Lines of Context:
376
376
        return NT_STATUS_NO_LOGON_SERVERS;
377
377
}
378
378
 
 
379
/*********************************************************************
 
380
 *********************************************************************/
 
381
 
 
382
NTSTATUS ads_lookup_site(void)
 
383
{
 
384
        ADS_STRUCT *ads = NULL;
 
385
        ADS_STATUS ads_status;
 
386
        NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
 
387
        struct cldap_netlogon_reply cldap_reply;
 
388
        
 
389
        ZERO_STRUCT(cldap_reply);
 
390
        
 
391
        ads = ads_init(lp_realm(), NULL, NULL);
 
392
        if (!ads) {             
 
393
                return NT_STATUS_NO_MEMORY;
 
394
        }
 
395
 
 
396
        /* The NO_BIND here will find a DC and set the client site
 
397
           but not establish the TCP connection */
 
398
 
 
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)));
 
404
        }
 
405
        nt_status = ads_ntstatus(ads_status);
 
406
        
 
407
        if (ads) {
 
408
                ads_destroy(&ads);
 
409
        }
 
410
        
 
411
        return nt_status;
 
412
}
 
413
 
 
414
/*********************************************************************
 
415
 *********************************************************************/
 
416
 
 
417
const char* host_dns_domain(const char *fqdn)
 
418
{
 
419
        const char *p = fqdn;
 
420
        
 
421
        while (p && *p != '.') {                
 
422
                p++;
 
423
        }
 
424
 
 
425
        /* go to next char following '.' */
 
426
 
 
427
        if (p)
 
428
                p++;
 
429
        
 
430
        return p;       
 
431
}
 
432
 
 
433
 
 
434
/*
 
435
 * Sorting function for figuring out which GC servers which 
 
436
 * should prefer.
 
437
 */
 
438
 
 
439
static int gc_weight_cmp(void *a, void *b)
 
440
{
 
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;
 
445
        
 
446
        /* Get the trivial cases out of the way */
 
447
 
 
448
        if (a == b) return 0;   
 
449
        if (!a) return -1;
 
450
        if (!b) return 1;
 
451
 
 
452
        domain1 = host_dns_domain(rec1->hostname);
 
453
        domain2 = host_dns_domain(rec2->hostname);
 
454
 
 
455
        /* Prefer fully qualified hosts */
 
456
 
 
457
        if (!domain1) return 1;
 
458
        if (!domain2) return -1;
 
459
        
 
460
        if (strequal(domain1, domain2)) {
 
461
                return 0;
 
462
        }
 
463
 
 
464
        /* Prefer hosts in our realm */
 
465
 
 
466
        if (strequal(domain1, lp_realm())) {
 
467
                return -1;
 
468
        }
 
469
        if (strequal(domain2, lp_realm())) {
 
470
                return 1;
 
471
        }
 
472
 
 
473
        /* This should never happen */
 
474
 
 
475
        return 0;       
 
476
}
 
477
 
379
478
/**
380
479
 * Connect to the Global Catalog server
381
480
 * @param ads Pointer to an existing ADS_STRUCT
400
499
        if (!realm)
401
500
                realm = lp_realm();
402
501
 
403
 
        sitename = sitename_fetch(realm);
404
 
 
 
502
        if ((sitename = sitename_fetch(realm)) == NULL) {
 
503
                ads_lookup_site();
 
504
                sitename = sitename_fetch(realm);               
 
505
        }
 
506
 
405
507
        do {
406
508
                /* We try once with a sitename and once without
407
509
                   (unless we don't have a sitename and then we're
420
522
                        goto done;
421
523
                }
422
524
 
 
525
                /* Sort the list so that GCs closest to us or in our own domain
 
526
                   are selected first */
 
527
 
 
528
                qsort(gcs_list, 
 
529
                      num_gcs, 
 
530
                      sizeof(struct dns_rr_srv),
 
531
                      QSORT_CAST gc_weight_cmp);
 
532
 
 
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));
 
537
                        }
 
538
                }
 
539
                
 
540
 
423
541
                /* Loop until we get a successful connection or have gone
424
 
                   through them all */
 
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 */
425
544
 
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)) {
431
551
                                goto done;
432
552
                        }
433
553
                        SAFE_FREE(ads->server.ldap_server);
 
554
                        SAFE_FREE(ads->server.realm);                   
434
555
                }
435
556
 
436
557
                TALLOC_FREE(gcs_list);