lib: Make sid_parse return the parsed length
[samba.git] / source3 / libads / ldap.c
index c70cdeb5163a129b5cddb9f756472b2514fca1e6..8e0ecb87569ef6780ca8ea0a6f2d26a3472a4b19 100644 (file)
@@ -31,6 +31,7 @@
 #include "../libcli/security/security.h"
 #include "../librpc/gen_ndr/netlogon.h"
 #include "lib/param/loadparm.h"
+#include "libsmb/namequery.h"
 
 #ifdef HAVE_LDAP
 
@@ -279,7 +280,12 @@ static bool ads_try_connect(ADS_STRUCT *ads, bool gc,
        SAFE_FREE(ads->config.client_site_name);
        SAFE_FREE(ads->server.workgroup);
 
-       ads->config.flags              = cldap_reply.server_type;
+       if (!check_cldap_reply_required_flags(cldap_reply.server_type,
+                                             ads->config.flags)) {
+               ret = false;
+               goto out;
+       }
+
        ads->config.ldap_server_name   = SMB_STRDUP(cldap_reply.pdc_dns_name);
        ads->config.realm              = SMB_STRDUP(cldap_reply.dns_domain);
        if (!strupper_m(ads->config.realm)) {
@@ -305,6 +311,9 @@ static bool ads_try_connect(ADS_STRUCT *ads, bool gc,
        sitename_store( cldap_reply.domain_name, cldap_reply.client_site);
        sitename_store( cldap_reply.dns_domain, cldap_reply.client_site);
 
+       /* Leave this until last so that the flags are not clobbered */
+       ads->config.flags              = cldap_reply.server_type;
+
        ret = true;
 
  out:
@@ -334,7 +343,9 @@ static NTSTATUS cldap_ping_list(ADS_STRUCT *ads,const char *domain,
                        check_negative_conn_cache(domain, server)))
                        continue;
 
+               /* Returns ok only if it matches the correct server type */
                ok = ads_try_connect(ads, false, &ip_list[i].ss);
+
                if (ok) {
                        return NT_STATUS_OK;
                }
@@ -402,7 +413,7 @@ static NTSTATUS resolve_and_ping_dns(ADS_STRUCT *ads, const char *sitename,
                                     const char *realm)
 {
        int count;
-       struct ip_service *ip_list;
+       struct ip_service *ip_list = NULL;
        NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
 
        DEBUG(6, ("resolve_and_ping_dns: (cldap) looking for realm '%s'\n",
@@ -411,6 +422,7 @@ static NTSTATUS resolve_and_ping_dns(ADS_STRUCT *ads, const char *sitename,
        status = get_sorted_dc_list(realm, sitename, &ip_list, &count,
                                    true);
        if (!NT_STATUS_IS_OK(status)) {
+               SAFE_FREE(ip_list);
                return status;
        }
 
@@ -566,8 +578,9 @@ ADS_STATUS ads_connect(ADS_STRUCT *ads)
        char addr[INET6_ADDRSTRLEN];
 
        ZERO_STRUCT(ads->ldap);
+       ZERO_STRUCT(ads->ldap_wrap_data);
        ads->ldap.last_attempt  = time_mono(NULL);
-       ads->ldap.wrap_type     = ADS_SASLWRAP_TYPE_PLAIN;
+       ads->ldap_wrap_data.wrap_type   = ADS_SASLWRAP_TYPE_PLAIN;
 
        /* try with a user specified server */
 
@@ -602,6 +615,11 @@ ADS_STATUS ads_connect(ADS_STRUCT *ads)
                if (ads->server.gc == true) {
                        return ADS_ERROR(LDAP_OPERATIONS_ERROR);
                }
+
+               if (ads->server.no_fallback) {
+                       status = ADS_ERROR_NT(NT_STATUS_NOT_FOUND);
+                       goto out;
+               }
        }
 
        ntstatus = ads_find_dc(ads);
@@ -643,8 +661,8 @@ got_connection:
                goto out;
        }
 
-       ads->ldap.mem_ctx = talloc_init("ads LDAP connection memory");
-       if (!ads->ldap.mem_ctx) {
+       ads->ldap_wrap_data.mem_ctx = talloc_init("ads LDAP connection memory");
+       if (!ads->ldap_wrap_data.mem_ctx) {
                status = ADS_ERROR_NT(NT_STATUS_NO_MEMORY);
                goto out;
        }
@@ -730,13 +748,15 @@ void ads_disconnect(ADS_STRUCT *ads)
                ldap_unbind(ads->ldap.ld);
                ads->ldap.ld = NULL;
        }
-       if (ads->ldap.wrap_ops && ads->ldap.wrap_ops->disconnect) {
-               ads->ldap.wrap_ops->disconnect(ads);
+       if (ads->ldap_wrap_data.wrap_ops &&
+               ads->ldap_wrap_data.wrap_ops->disconnect) {
+               ads->ldap_wrap_data.wrap_ops->disconnect(&ads->ldap_wrap_data);
        }
-       if (ads->ldap.mem_ctx) {
-               talloc_free(ads->ldap.mem_ctx);
+       if (ads->ldap_wrap_data.mem_ctx) {
+               talloc_free(ads->ldap_wrap_data.mem_ctx);
        }
        ZERO_STRUCT(ads->ldap);
+       ZERO_STRUCT(ads->ldap_wrap_data);
 }
 
 /*
@@ -1978,28 +1998,27 @@ done:
  * (found by hostname) in AD.
  * @param ads An initialized ADS_STRUCT
  * @param machine_name the NetBIOS name of the computer, which is used to identify the computer account.
- * @param my_fqdn The fully qualified DNS name of the machine
- * @param spn A string of the service principal to add, i.e. 'host'
+ * @param spns An array or strings for the service principals to add,
+ *        i.e. 'cifs/machine_name', 'http/machine.full.domain.com' etc.
  * @return 0 upon sucess, or non-zero if a failure occurs
  **/
 
-ADS_STATUS ads_add_service_principal_name(ADS_STRUCT *ads, const char *machine_name, 
-                                          const char *my_fqdn, const char *spn)
+ADS_STATUS ads_add_service_principal_names(ADS_STRUCT *ads,
+                                          const char *machine_name,
+                                           const char **spns)
 {
        ADS_STATUS ret;
        TALLOC_CTX *ctx;
        LDAPMessage *res = NULL;
-       char *psp1, *psp2;
        ADS_MODLIST mods;
        char *dn_string = NULL;
-       const char *servicePrincipalName[3] = {NULL, NULL, NULL};
+       const char **servicePrincipalName = spns;
 
        ret = ads_find_machine_acct(ads, &res, machine_name);
        if (!ADS_ERR_OK(ret) || ads_count_replies(ads, res) != 1) {
                DEBUG(1,("ads_add_service_principal_name: WARNING: Host Account for %s not found... skipping operation.\n",
                        machine_name));
-               DEBUG(1,("ads_add_service_principal_name: WARNING: Service Principal '%s/%s@%s' has NOT been added.\n",
-                       spn, machine_name, ads->config.realm));
+               DEBUG(1,("ads_add_service_principal_name: WARNING: Service Principals have NOT been added.\n"));
                ads_msgfree(ads, res);
                return ADS_ERROR(LDAP_NO_SUCH_OBJECT);
        }
@@ -2010,44 +2029,24 @@ ADS_STATUS ads_add_service_principal_name(ADS_STRUCT *ads, const char *machine_n
                return ADS_ERROR(LDAP_NO_MEMORY);
        }
 
-       /* add short name spn */
-
-       if ( (psp1 = talloc_asprintf(ctx, "%s/%s", spn, machine_name)) == NULL ) {
-               talloc_destroy(ctx);
-               ads_msgfree(ads, res);
-               return ADS_ERROR(LDAP_NO_MEMORY);
-       }
-       if (!strlower_m(&psp1[strlen(spn) + 1])) {
-               ret = ADS_ERROR(LDAP_NO_MEMORY);
-               goto out;
-       }
-       servicePrincipalName[0] = psp1;
+       DEBUG(5,("ads_add_service_principal_name: INFO: "
+               "Adding %s to host %s\n",
+               spns[0] ? "N/A" : spns[0], machine_name));
 
-       DEBUG(5,("ads_add_service_principal_name: INFO: Adding %s to host %s\n", 
-               psp1, machine_name));
 
-
-       /* add fully qualified spn */
-
-       if ( (psp2 = talloc_asprintf(ctx, "%s/%s", spn, my_fqdn)) == NULL ) {
-               ret = ADS_ERROR(LDAP_NO_MEMORY);
-               goto out;
-       }
-       if (!strlower_m(&psp2[strlen(spn) + 1])) {
-               ret = ADS_ERROR(LDAP_NO_MEMORY);
-               goto out;
-       }
-       servicePrincipalName[1] = psp2;
-
-       DEBUG(5,("ads_add_service_principal_name: INFO: Adding %s to host %s\n", 
-               psp2, machine_name));
+       DEBUG(5,("ads_add_service_principal_name: INFO: "
+               "Adding %s to host %s\n",
+               spns[1] ? "N/A" : spns[1], machine_name));
 
        if ( (mods = ads_init_mods(ctx)) == NULL ) {
                ret = ADS_ERROR(LDAP_NO_MEMORY);
                goto out;
        }
 
-       ret = ads_add_strlist(ctx, &mods, "servicePrincipalName", servicePrincipalName);
+       ret = ads_add_strlist(ctx,
+                             &mods,
+                             "servicePrincipalName",
+                             servicePrincipalName);
        if (!ADS_ERR_OK(ret)) {
                DEBUG(1,("ads_add_service_principal_name: Error: Updating Service Principals in LDAP\n"));
                goto out;
@@ -2230,8 +2229,9 @@ done:
 */
 static void dump_binary(ADS_STRUCT *ads, const char *field, struct berval **values)
 {
-       int i, j;
+       size_t i;
        for (i=0; values[i]; i++) {
+               ber_len_t j;
                printf("%s: ", field);
                for (j=0; j<values[i]->bv_len; j++) {
                        printf("%02X", (unsigned char)values[i]->bv_val[j]);
@@ -2264,13 +2264,15 @@ static void dump_sid(ADS_STRUCT *ads, const char *field, struct berval **values)
 {
        int i;
        for (i=0; values[i]; i++) {
+               struct sid_parse_ret ret;
                struct dom_sid sid;
-               fstring tmp;
-               if (!sid_parse((const uint8_t *)values[i]->bv_val,
-                              values[i]->bv_len, &sid)) {
+               struct dom_sid_buf tmp;
+               ret = sid_parse((const uint8_t *)values[i]->bv_val,
+                               values[i]->bv_len, &sid);
+               if (ret.len == -1) {
                        return;
                }
-               printf("%s: %s\n", field, sid_to_fstring(tmp, &sid));
+               printf("%s: %s\n", field, dom_sid_str_buf(&sid, &tmp));
        }
 }
 
@@ -2541,8 +2543,7 @@ int ads_count_replies(ADS_STRUCT *ads, void *res)
 {
        char **values;
        char **ret = NULL;
-       int i;
-       size_t converted_size;
+       size_t i, converted_size;
 
        values = ldap_get_values(ads->ldap.ld, msg, field);
        if (!values)
@@ -2774,7 +2775,6 @@ int ads_count_replies(ADS_STRUCT *ads, void *res)
                   LDAPMessage *msg, const char *field, struct dom_sid **sids)
 {
        struct berval **values;
-       bool ret;
        int count, i;
 
        values = ldap_get_values_len(ads->ldap.ld, msg, field);
@@ -2797,11 +2797,13 @@ int ads_count_replies(ADS_STRUCT *ads, void *res)
 
        count = 0;
        for (i=0; values[i]; i++) {
+               struct sid_parse_ret ret;
                ret = sid_parse((const uint8_t *)values[i]->bv_val,
                                values[i]->bv_len, &(*sids)[count]);
-               if (ret) {
-                       DEBUG(10, ("pulling SID: %s\n",
-                                  sid_string_dbg(&(*sids)[count])));
+               if (ret.len != -1) {
+                       struct dom_sid_buf buf;
+                       DBG_DEBUG("pulling SID: %s\n",
+                                 dom_sid_str_buf(&(*sids)[count], &buf));
                        count++;
                }
        }
@@ -3356,6 +3358,7 @@ ADS_STATUS ads_get_sid_from_extended_dn(TALLOC_CTX *mem_ctx,
                }
                break;
        case ADS_EXTENDED_DN_HEX_STRING: {
+               struct sid_parse_ret ret;
                fstring buf;
                size_t buf_len;
 
@@ -3364,7 +3367,8 @@ ADS_STATUS ads_get_sid_from_extended_dn(TALLOC_CTX *mem_ctx,
                        return ADS_ERROR_NT(NT_STATUS_INVALID_PARAMETER);
                }
 
-               if (!sid_parse((const uint8_t *)buf, buf_len, sid)) {
+               ret = sid_parse((const uint8_t *)buf, buf_len, sid);
+               if (ret.len == -1) {
                        DEBUG(10,("failed to parse sid\n"));
                        return ADS_ERROR_NT(NT_STATUS_INVALID_PARAMETER);
                }
@@ -3388,7 +3392,7 @@ char* ads_get_dnshostname( ADS_STRUCT *ads, TALLOC_CTX *ctx, const char *machine
        int count = 0;
        char *name = NULL;
 
-       status = ads_find_machine_acct(ads, &res, lp_netbios_name());
+       status = ads_find_machine_acct(ads, &res, machine_name);
        if (!ADS_ERR_OK(status)) {
                DEBUG(0,("ads_get_dnshostname: Failed to find account for %s\n",
                        lp_netbios_name()));
@@ -3445,33 +3449,37 @@ out:
 /********************************************************************
 ********************************************************************/
 
-char* ads_get_samaccountname( ADS_STRUCT *ads, TALLOC_CTX *ctx, const char *machine_name )
+bool ads_has_samaccountname( ADS_STRUCT *ads, TALLOC_CTX *ctx, const char *machine_name )
 {
        LDAPMessage *res = NULL;
        ADS_STATUS status;
        int count = 0;
        char *name = NULL;
+       bool ok = false;
 
-       status = ads_find_machine_acct(ads, &res, lp_netbios_name());
+       status = ads_find_machine_acct(ads, &res, machine_name);
        if (!ADS_ERR_OK(status)) {
-               DEBUG(0,("ads_get_dnshostname: Failed to find account for %s\n",
+               DEBUG(0,("ads_has_samaccountname: Failed to find account for %s\n",
                        lp_netbios_name()));
                goto out;
        }
 
        if ( (count = ads_count_replies(ads, res)) != 1 ) {
-               DEBUG(1,("ads_get_dnshostname: %d entries returned!\n", count));
+               DEBUG(1,("ads_has_samaccountname: %d entries returned!\n", count));
                goto out;
        }
 
        if ( (name = ads_pull_string(ads, ctx, res, "sAMAccountName")) == NULL ) {
-               DEBUG(0,("ads_get_dnshostname: No sAMAccountName attribute!\n"));
+               DEBUG(0,("ads_has_samaccountname: No sAMAccountName attribute!\n"));
        }
 
 out:
        ads_msgfree(ads, res);
-
-       return name;
+       if (name != NULL) {
+               ok = (strlen(name) > 0);
+       }
+       TALLOC_FREE(name);
+       return ok;
 }
 
 #if 0