r17943: The horror, the horror. Add KDC site support by
[tprouty/samba.git] / source / libsmb / namequery_dc.c
index a596f00ddb9b0ab8c896426d6a26e39a152c0761..4099cc9dd8fb05838f3196d077d0e679cbade368 100644 (file)
 #include "includes.h"
 
 /**************************************************************************
- Find the name and IP address for a server in he realm/domain
+ Find the name and IP address for a server in the realm/domain
  *************************************************************************/
  
-static BOOL ads_dc_name(const char *domain, struct in_addr *dc_ip, fstring srv_name)
+static BOOL ads_dc_name(const char *domain,
+                       const char *realm,
+                       struct in_addr *dc_ip,
+                       fstring srv_name)
 {
        ADS_STRUCT *ads;
-       const char *realm = domain;
+       char *sitename = sitename_fetch();
+       int i;
 
-       if (strcasecmp(realm, lp_workgroup()) == 0)
+       if (!realm && strequal(domain, lp_workgroup())) {
                realm = lp_realm();
+       }
 
-       ads = ads_init(realm, domain, NULL);
-       if (!ads)
-               return False;
-
-       /* we don't need to bind, just connect */
-       ads->auth.flags |= ADS_AUTH_NO_BIND;
+       /* Try this 3 times then give up. */
+       for( i =0 ; i < 3; i++) {
+               ads = ads_init(realm, domain, NULL);
+               if (!ads) {
+                       SAFE_FREE(sitename);
+                       return False;
+               }
 
-       DEBUG(4,("ads_dc_name: domain=%s\n", domain));
+               DEBUG(4,("ads_dc_name: domain=%s\n", domain));
 
 #ifdef HAVE_ADS
-       /* a full ads_connect() is actually overkill, as we don't srictly need
-          to do the SASL auth in order to get the info we need, but libads
-          doesn't offer a better way right now */
-       ads_connect(ads);
+               /* we don't need to bind, just connect */
+               ads->auth.flags |= ADS_AUTH_NO_BIND;
+               ads_connect(ads);
 #endif
 
-       if (!ads->config.realm)
+               if (!ads->config.realm) {
+                       SAFE_FREE(sitename);
+                       ads_destroy(&ads);
+                       return False;
+               }
+
+               /* Now we've found a server, see if our sitename
+                  has changed. If so, we need to re-do the DNS query
+                  to ensure we only find servers in our site. */
+
+               if (sitename_changed(sitename)) {
+                       SAFE_FREE(sitename);
+                       sitename = sitename_fetch();
+                       ads_destroy(&ads);
+                       continue;
+               }
+
+#ifdef HAVE_KRB5
+               if ((ads->config.flags & ADS_KDC) && sitename) {
+                       /* We're going to use this KDC for this realm/domain.
+                          If we are using sites, then force the krb5 libs
+                          to use this KDC. */
+
+                       create_local_private_krb5_conf_for_domain(realm,
+                                                               domain,
+                                                               ads->ldap_ip);
+               }
+#endif
+               break;
+       }
+
+       if (i == 3) {
+               DEBUG(1,("ads_dc_name: sitename (now \"%s\") keeps changing ???\n",
+                       sitename ? sitename : ""));
+               SAFE_FREE(sitename);
                return False;
+       }
+
+       SAFE_FREE(sitename);
 
        fstrcpy(srv_name, ads->config.ldap_server_name);
        strupper_m(srv_name);
@@ -77,47 +119,20 @@ static BOOL rpc_dc_name(const char *domain, fstring srv_name, struct in_addr *ip
        struct ip_service *ip_list = NULL;
        struct in_addr dc_ip, exclude_ip;
        int count, i;
-       BOOL use_pdc_only;
        NTSTATUS result;
        
        zero_ip(&exclude_ip);
 
-       use_pdc_only = must_use_pdc(domain);
-       
-       /* Lookup domain controller name */
-          
-       if ( use_pdc_only && get_pdc_ip(domain, &dc_ip) ) 
-       {
-               DEBUG(10,("rpc_dc_name: Atempting to lookup PDC to avoid sam sync delays\n"));
-               
-               /* check the connection cache and perform the node status 
-                  lookup only if the IP is not found to be bad */
-
-               if (name_status_find(domain, 0x1b, 0x20, dc_ip, srv_name) ) {
-                       result = check_negative_conn_cache( domain, srv_name );
-                       if ( NT_STATUS_IS_OK(result) )
-                               goto done;
-               }
-               /* Didn't get name, remember not to talk to this DC. */
-               exclude_ip = dc_ip;
-       }
-
        /* get a list of all domain controllers */
        
-       if ( !get_sorted_dc_list(domain, &ip_list, &count, False) ) {
+       if (!NT_STATUS_IS_OK(get_sorted_dc_list(domain, &ip_list, &count,
+                                               False))) {
                DEBUG(3, ("Could not look up dc's for domain %s\n", domain));
                return False;
        }
 
        /* Remove the entry we've already failed with (should be the PDC). */
 
-       if ( use_pdc_only ) {
-               for (i = 0; i < count; i++) {   
-                       if (ip_equal( exclude_ip, ip_list[i].ip))
-                               zero_ip(&ip_list[i].ip);
-               }
-       }
-
        for (i = 0; i < count; i++) {
                if (is_zero_ip(ip_list[i].ip))
                        continue;
@@ -157,7 +172,7 @@ static BOOL rpc_dc_name(const char *domain, fstring srv_name, struct in_addr *ip
  wrapper around ads and rpc methods of finds DC's
 **********************************************************************/
 
-BOOL get_dc_name(const char *domain, fstring srv_name, struct in_addr *ip_out)
+BOOL get_dc_name(const char *domain, const char *realm, fstring srv_name, struct in_addr *ip_out)
 {
        struct in_addr dc_ip;
        BOOL ret;
@@ -167,15 +182,14 @@ BOOL get_dc_name(const char *domain, fstring srv_name, struct in_addr *ip_out)
 
        ret = False;
        
-       if ( strequal(lp_workgroup(), domain) || strequal(lp_realm(), domain) )
+       if ( strequal(lp_workgroup(), domain) || strequal(lp_realm(), realm) )
                our_domain = True;
        
-       /* always try to obey what the admin specified in smb.conf.
-          If it is not our domain, assume that domain names with periods 
-          in them are realm names */
+       /* always try to obey what the admin specified in smb.conf 
+          (for the local domain) */
        
-       if ( (our_domain && lp_security()==SEC_ADS) || strchr_m(domain, '.') ) {
-               ret = ads_dc_name(domain, &dc_ip, srv_name);
+       if ( (our_domain && lp_security()==SEC_ADS) || realm ) {
+               ret = ads_dc_name(domain, realm, &dc_ip, srv_name);
        }
        
        if (!ret) {
@@ -187,4 +201,3 @@ BOOL get_dc_name(const char *domain, fstring srv_name, struct in_addr *ip_out)
 
        return ret;
 }
-