s3:libads/ldap.c: if the client belongs to no site at all any dc is the closest
[kai/samba.git] / source3 / libads / ldap.c
index 7c64082ab46e8c98507cd080f6fc14b859f6dba2..c0bb9c7e2df85346b4bbe80eb3e2062cc3d36da9 100644 (file)
@@ -162,6 +162,11 @@ bool ads_closest_dc(ADS_STRUCT *ads)
                return True;
        }
 
+       if (ads->config.client_site_name == NULL) {
+               DEBUG(10,("ads_closest_dc: client belongs to no site\n"));
+               return True;
+       }
+
        DEBUG(10,("ads_closest_dc: %s is not the closest DC\n", 
                ads->config.ldap_server_name));
 
@@ -173,10 +178,10 @@ bool ads_closest_dc(ADS_STRUCT *ads)
   try a connection to a given ldap server, returning True and setting the servers IP
   in the ads struct if successful
  */
-bool ads_try_connect(ADS_STRUCT *ads, const char *server )
+static bool ads_try_connect(ADS_STRUCT *ads, const char *server, bool gc)
 {
        char *srv;
-       struct nbt_cldap_netlogon_5 cldap_reply;
+       struct NETLOGON_SAM_LOGON_RESPONSE_EX cldap_reply;
        TALLOC_CTX *mem_ctx = NULL;
        bool ret = false;
 
@@ -238,7 +243,7 @@ bool ads_try_connect(ADS_STRUCT *ads, const char *server )
        }
        ads->server.workgroup          = SMB_STRDUP(cldap_reply.domain);
 
-       ads->ldap.port = LDAP_PORT;
+       ads->ldap.port = gc ? LDAP_GC_PORT : LDAP_PORT;
        if (!interpret_string_addr(&ads->ldap.ss, srv, 0)) {
                DEBUG(1,("ads_try_connect: unable to convert %s "
                        "to an address\n",
@@ -267,10 +272,12 @@ bool ads_try_connect(ADS_STRUCT *ads, const char *server )
 
 static NTSTATUS ads_find_dc(ADS_STRUCT *ads)
 {
+       const char *c_domain;
        const char *c_realm;
        int count, i=0;
        struct ip_service *ip_list;
        const char *realm;
+       const char *domain;
        bool got_realm = False;
        bool use_own_domain = False;
        char *sitename;
@@ -301,20 +308,51 @@ static NTSTATUS ads_find_dc(ADS_STRUCT *ads)
                        if ( use_own_domain )
                                c_realm = lp_workgroup();
                }
+       }
 
-               if ( !c_realm || !*c_realm ) {
-                       DEBUG(0,("ads_find_dc: no realm or workgroup!  Don't know what to do\n"));
-                       return NT_STATUS_INVALID_PARAMETER; /* rather need MISSING_PARAMETER ... */
-               }
+       if ( !c_realm || !*c_realm ) {
+               DEBUG(0,("ads_find_dc: no realm or workgroup!  Don't know what to do\n"));
+               return NT_STATUS_INVALID_PARAMETER; /* rather need MISSING_PARAMETER ... */
+       }
+
+       if ( use_own_domain ) {
+               c_domain = lp_workgroup();
+       } else {
+               c_domain = ads->server.workgroup;
        }
 
        realm = c_realm;
+       domain = c_domain;
+
+       /*
+        * In case of LDAP we use get_dc_name() as that
+        * creates the custom krb5.conf file
+        */
+       if (!(ads->auth.flags & ADS_AUTH_NO_BIND)) {
+               fstring srv_name;
+               struct sockaddr_storage ip_out;
+
+               DEBUG(6,("ads_find_dc: (ldap) looking for %s '%s'\n",
+                       (got_realm ? "realm" : "domain"), realm));
+
+               if (get_dc_name(domain, realm, srv_name, &ip_out)) {
+                       /*
+                        * we call ads_try_connect() to fill in the
+                        * ads->config details
+                        */
+                       if (ads_try_connect(ads, srv_name, false)) {
+                               return NT_STATUS_OK;
+                       }
+               }
+
+               return NT_STATUS_NO_LOGON_SERVERS;
+       }
 
        sitename = sitename_fetch(realm);
 
  again:
 
-       DEBUG(6,("ads_find_dc: looking for %s '%s'\n",
+       DEBUG(6,("ads_find_dc: (cldap) looking for %s '%s'\n",
                (got_realm ? "realm" : "domain"), realm));
 
        status = get_sorted_dc_list(realm, sitename, &ip_list, &count, got_realm);
@@ -358,7 +396,7 @@ static NTSTATUS ads_find_dc(ADS_STRUCT *ads)
                        }
                }
 
-               if ( ads_try_connect(ads, server) ) {
+               if ( ads_try_connect(ads, server, false) ) {
                        SAFE_FREE(ip_list);
                        SAFE_FREE(sitename);
                        return NT_STATUS_OK;
@@ -385,6 +423,138 @@ static NTSTATUS ads_find_dc(ADS_STRUCT *ads)
        return NT_STATUS_NO_LOGON_SERVERS;
 }
 
+/*********************************************************************
+ *********************************************************************/
+
+static NTSTATUS ads_lookup_site(void)
+{
+       ADS_STRUCT *ads = NULL;
+       ADS_STATUS ads_status;
+       NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
+
+       ads = ads_init(lp_realm(), NULL, NULL);
+       if (!ads) {
+               return NT_STATUS_NO_MEMORY;
+       }
+
+       /* The NO_BIND here will find a DC and set the client site
+          but not establish the TCP connection */
+
+       ads->auth.flags = ADS_AUTH_NO_BIND;
+       ads_status = ads_connect(ads);
+       if (!ADS_ERR_OK(ads_status)) {
+               DEBUG(4, ("ads_lookup_site: ads_connect to our realm failed! (%s)\n",
+                         ads_errstr(ads_status)));
+       }
+       nt_status = ads_ntstatus(ads_status);
+
+       if (ads) {
+               ads_destroy(&ads);
+       }
+
+       return nt_status;
+}
+
+/*********************************************************************
+ *********************************************************************/
+
+static const char* host_dns_domain(const char *fqdn)
+{
+       const char *p = fqdn;
+
+       /* go to next char following '.' */
+
+       if ((p = strchr_m(fqdn, '.')) != NULL) {
+               p++;
+       }
+
+       return p;
+}
+
+
+/**
+ * Connect to the Global Catalog server
+ * @param ads Pointer to an existing ADS_STRUCT
+ * @return status of connection
+ *
+ * Simple wrapper around ads_connect() that fills in the
+ * GC ldap server information
+ **/
+
+ADS_STATUS ads_connect_gc(ADS_STRUCT *ads)
+{
+       TALLOC_CTX *frame = talloc_stackframe();
+       struct dns_rr_srv *gcs_list;
+       int num_gcs;
+       char *realm = ads->server.realm;
+       NTSTATUS nt_status = NT_STATUS_UNSUCCESSFUL;
+       ADS_STATUS ads_status = ADS_ERROR_NT(NT_STATUS_UNSUCCESSFUL);
+       int i;
+       bool done = false;
+       char *sitename = NULL;
+
+       if (!realm)
+               realm = lp_realm();
+
+       if ((sitename = sitename_fetch(realm)) == NULL) {
+               ads_lookup_site();
+               sitename = sitename_fetch(realm);
+       }
+
+       do {
+               /* We try once with a sitename and once without
+                  (unless we don't have a sitename and then we're
+                  done */
+
+               if (sitename == NULL)
+                       done = true;
+
+               nt_status = ads_dns_query_gcs(frame, realm, sitename,
+                                             &gcs_list, &num_gcs);
+
+               SAFE_FREE(sitename);
+
+               if (!NT_STATUS_IS_OK(nt_status)) {
+                       ads_status = ADS_ERROR_NT(nt_status);
+                       goto done;
+               }
+
+               /* Loop until we get a successful connection or have gone
+                  through them all.  When connecting a GC server, make sure that
+                  the realm is the server's DNS name and not the forest root */
+
+               for (i=0; i<num_gcs; i++) {
+                       ads->server.gc = true;
+                       ads->server.ldap_server = SMB_STRDUP(gcs_list[i].hostname);
+                       ads->server.realm = SMB_STRDUP(host_dns_domain(ads->server.ldap_server));
+                       ads_status = ads_connect(ads);
+                       if (ADS_ERR_OK(ads_status)) {
+                               /* Reset the bind_dn to "".  A Global Catalog server
+                                  may host  multiple domain trees in a forest.
+                                  Windows 2003 GC server will accept "" as the search
+                                  path to imply search all domain trees in the forest */
+
+                               SAFE_FREE(ads->config.bind_path);
+                               ads->config.bind_path = SMB_STRDUP("");
+
+
+                               goto done;
+                       }
+                       SAFE_FREE(ads->server.ldap_server);
+                       SAFE_FREE(ads->server.realm);
+               }
+
+               TALLOC_FREE(gcs_list);
+               num_gcs = 0;
+       } while (!done);
+
+done:
+       SAFE_FREE(sitename);
+       talloc_destroy(frame);
+
+       return ads_status;
+}
+
 
 /**
  * Connect to the LDAP server
@@ -412,7 +582,7 @@ ADS_STATUS ads_connect(ADS_STRUCT *ads)
        }
 
        if (ads->server.ldap_server &&
-           ads_try_connect(ads, ads->server.ldap_server)) {
+           ads_try_connect(ads, ads->server.ldap_server, ads->server.gc)) {
                goto got_connection;
        }
 
@@ -472,7 +642,7 @@ got_connection:
        /* Otherwise setup the TCP LDAP session */
 
        ads->ldap.ld = ldap_open_with_timeout(ads->config.ldap_server_name,
-                                             LDAP_PORT, lp_ldap_timeout());
+                                             ads->ldap.port, lp_ldap_timeout());
        if (ads->ldap.ld == NULL) {
                status = ADS_ERROR(LDAP_OPERATIONS_ERROR);
                goto out;
@@ -669,7 +839,7 @@ static ADS_STATUS ads_do_paged_search_args(ADS_STRUCT *ads,
                                           int *count, struct berval **cookie)
 {
        int rc, i, version;
-       char *utf8_expr, *utf8_path, **search_attrs;
+       char *utf8_expr, *utf8_path, **search_attrs = NULL;
        size_t converted_size;
        LDAPControl PagedResults, NoReferrals, ExternalCtrl, *controls[4], **rcontrols;
        BerElement *cookie_be = NULL;
@@ -700,7 +870,7 @@ static ADS_STATUS ads_do_paged_search_args(ADS_STRUCT *ads,
        else {
                /* This would be the utf8-encoded version...*/
                /* if (!(search_attrs = ads_push_strvals(ctx, attrs))) */
-               if (!(str_list_copy(talloc_tos(), &search_attrs, attrs))) {
+               if (!(search_attrs = str_list_copy(talloc_tos(), attrs))) {
                        rc = LDAP_NO_MEMORY;
                        goto done;
                }
@@ -1012,7 +1182,7 @@ ADS_STATUS ads_do_search_all_fn(ADS_STRUCT *ads, const char *bind_path,
        else {
                /* This would be the utf8-encoded version...*/
                /* if (!(search_attrs = ads_push_strvals(ctx, attrs)))  */
-               if (!(str_list_copy(talloc_tos(), &search_attrs, attrs)))
+               if (!(search_attrs = str_list_copy(talloc_tos(), attrs)))
                {
                        DEBUG(1,("ads_do_search: str_list_copy() failed!"));
                        rc = LDAP_NO_MEMORY;
@@ -1930,7 +2100,7 @@ static void dump_guid(ADS_STRUCT *ads, const char *field, struct berval **values
 
                memcpy(guid.info, values[i]->bv_val, sizeof(guid.info));
                smb_uuid_unpack(guid, &tmp);
-               printf("%s: %s\n", field, smb_uuid_string(talloc_tos(), tmp));
+               printf("%s: %s\n", field, GUID_string(talloc_tos(), &tmp));
        }
 }
 
@@ -2696,6 +2866,7 @@ ADS_STATUS ads_domain_func_level(ADS_STRUCT *ads, uint32 *val)
                if ( (ads_s = ads_init( ads->server.realm, ads->server.workgroup, 
                        ads->server.ldap_server )) == NULL )
                {
+                       status = ADS_ERROR_NT(NT_STATUS_NO_MEMORY);
                        goto done;
                }
                ads_s->auth.flags = ADS_AUTH_ANON_BIND;
@@ -2978,60 +3149,66 @@ ADS_STATUS ads_get_joinable_ous(ADS_STRUCT *ads,
 
 /**
  * pull a DOM_SID from an extended dn string
- * @param mem_ctx TALLOC_CTX 
+ * @param mem_ctx TALLOC_CTX
  * @param extended_dn string
  * @param flags string type of extended_dn
  * @param sid pointer to a DOM_SID
- * @return boolean inidicating success
+ * @return NT_STATUS_OK on success,
+ *        NT_INVALID_PARAMETER on error,
+ *        NT_STATUS_NOT_FOUND if no SID present
  **/
-bool ads_get_sid_from_extended_dn(TALLOC_CTX *mem_ctx, 
-                                 const char *extended_dn, 
-                                 enum ads_extended_dn_flags flags, 
-                                 DOM_SID *sid)
+ADS_STATUS ads_get_sid_from_extended_dn(TALLOC_CTX *mem_ctx,
+                                       const char *extended_dn,
+                                       enum ads_extended_dn_flags flags,
+                                       DOM_SID *sid)
 {
        char *p, *q, *dn;
 
        if (!extended_dn) {
-               return False;
+               return ADS_ERROR_NT(NT_STATUS_INVALID_PARAMETER);
        }
 
        /* otherwise extended_dn gets stripped off */
        if ((dn = talloc_strdup(mem_ctx, extended_dn)) == NULL) {
-               return False;
+               return ADS_ERROR_NT(NT_STATUS_INVALID_PARAMETER);
        }
-       /* 
+       /*
         * ADS_EXTENDED_DN_HEX_STRING:
         * <GUID=238e1963cb390f4bb032ba0105525a29>;<SID=010500000000000515000000bb68c8fd6b61b427572eb04556040000>;CN=gd,OU=berlin,OU=suse,DC=ber,DC=suse,DC=de
         *
         * ADS_EXTENDED_DN_STRING (only with w2k3):
-       <GUID=63198e23-39cb-4b0f-b032-ba0105525a29>;<SID=S-1-5-21-4257769659-666132843-1169174103-1110>;CN=gd,OU=berlin,OU=suse,DC=ber,DC=suse,DC=de
+        * <GUID=63198e23-39cb-4b0f-b032-ba0105525a29>;<SID=S-1-5-21-4257769659-666132843-1169174103-1110>;CN=gd,OU=berlin,OU=suse,DC=ber,DC=suse,DC=de
+        *
+        * Object with no SID, such as an Exchange Public Folder
+        * <GUID=28907fb4bdf6854993e7f0a10b504e7c>;CN=public,CN=Microsoft Exchange System Objects,DC=sd2k3ms,DC=west,DC=isilon,DC=com
         */
 
        p = strchr(dn, ';');
        if (!p) {
-               return False;
+               return ADS_ERROR_NT(NT_STATUS_INVALID_PARAMETER);
        }
 
        if (strncmp(p, ";<SID=", strlen(";<SID=")) != 0) {
-               return False;
+               DEBUG(5,("No SID present in extended dn\n"));
+               return ADS_ERROR_NT(NT_STATUS_NOT_FOUND);
        }
 
        p += strlen(";<SID=");
 
        q = strchr(p, '>');
        if (!q) {
-               return False;
+               return ADS_ERROR_NT(NT_STATUS_INVALID_PARAMETER);
        }
-       
+
        *q = '\0';
 
        DEBUG(100,("ads_get_sid_from_extended_dn: sid string is %s\n", p));
 
        switch (flags) {
-       
+
        case ADS_EXTENDED_DN_STRING:
                if (!string_to_sid(sid, p)) {
-                       return False;
+                       return ADS_ERROR_NT(NT_STATUS_INVALID_PARAMETER);
                }
                break;
        case ADS_EXTENDED_DN_HEX_STRING: {
@@ -3040,21 +3217,21 @@ bool ads_get_sid_from_extended_dn(TALLOC_CTX *mem_ctx,
 
                buf_len = strhex_to_str(buf, sizeof(buf), p, strlen(p));
                if (buf_len == 0) {
-                       return False;
+                       return ADS_ERROR_NT(NT_STATUS_INVALID_PARAMETER);
                }
 
                if (!sid_parse(buf, buf_len, sid)) {
                        DEBUG(10,("failed to parse sid\n"));
-                       return False;
+                       return ADS_ERROR_NT(NT_STATUS_INVALID_PARAMETER);
                }
                break;
                }
        default:
                DEBUG(10,("unknown extended dn format\n"));
-               return False;
+               return ADS_ERROR_NT(NT_STATUS_INVALID_PARAMETER);
        }
 
-       return True;
+       return ADS_ERROR_NT(NT_STATUS_OK);
 }
 
 /**
@@ -3067,18 +3244,19 @@ bool ads_get_sid_from_extended_dn(TALLOC_CTX *mem_ctx,
  * @param sids pointer to sid array to allocate
  * @return the count of SIDs pulled
  **/
- int ads_pull_sids_from_extendeddn(ADS_STRUCT *ads, 
-                                  TALLOC_CTX *mem_ctx, 
-                                  LDAPMessage *msg, 
+ int ads_pull_sids_from_extendeddn(ADS_STRUCT *ads,
+                                  TALLOC_CTX *mem_ctx,
+                                  LDAPMessage *msg,
                                   const char *field,
                                   enum ads_extended_dn_flags flags,
                                   DOM_SID **sids)
 {
        int i;
-       size_t dn_count;
+       ADS_STATUS rc;
+       size_t dn_count, ret_count = 0;
        char **dn_strings;
 
-       if ((dn_strings = ads_pull_strings(ads, mem_ctx, msg, field, 
+       if ((dn_strings = ads_pull_strings(ads, mem_ctx, msg, field,
                                           &dn_count)) == NULL) {
                return 0;
        }
@@ -3090,18 +3268,25 @@ bool ads_get_sid_from_extended_dn(TALLOC_CTX *mem_ctx,
        }
 
        for (i=0; i<dn_count; i++) {
-
-               if (!ads_get_sid_from_extended_dn(mem_ctx, dn_strings[i], 
-                                                 flags, &(*sids)[i])) {
-                       TALLOC_FREE(*sids);
-                       TALLOC_FREE(dn_strings);
-                       return 0;
+               rc = ads_get_sid_from_extended_dn(mem_ctx, dn_strings[i],
+                                                 flags, &(*sids)[i]);
+               if (!ADS_ERR_OK(rc)) {
+                       if (NT_STATUS_EQUAL(ads_ntstatus(rc),
+                           NT_STATUS_NOT_FOUND)) {
+                               continue;
+                       }
+                       else {
+                               TALLOC_FREE(*sids);
+                               TALLOC_FREE(dn_strings);
+                               return 0;
+                       }
                }
+               ret_count++;
        }
 
        TALLOC_FREE(dn_strings);
 
-       return dn_count;
+       return ret_count;
 }
 
 /********************************************************************
@@ -3615,7 +3800,7 @@ const char *ads_get_extended_right_name_by_guid(ADS_STRUCT *ads,
        }
 
        expr = talloc_asprintf(mem_ctx, "(rightsGuid=%s)", 
-                              smb_uuid_string(mem_ctx, *rights_guid));
+                              GUID_string(mem_ctx, rights_guid));
        if (!expr) {
                goto done;
        }