r5351: Add wrapper for nt_errstr and array functions for samr_ConnectInfo.
[samba.git] / source4 / libads / ldap.c
index 9161c356923b5b27af0db27cec8af661ed69eeaf..f760843b59698769da51e76fd936423d63560a5e 100644 (file)
@@ -21,6 +21,7 @@
 */
 
 #include "includes.h"
+#include "version.h"
 
 #ifdef HAVE_LDAP
 
@@ -45,7 +46,7 @@
   TODO : add a negative connection cache in here leveraged off of the one
   found in the rpc code.  --jerry
  */
-static BOOL ads_try_connect(ADS_STRUCT *ads, const char *server, unsigned port)
+static BOOL ads_try_connect(ADS_STRUCT *ads, const char *server, uint_t port)
 {
        char *srv;
 
@@ -64,7 +65,7 @@ static BOOL ads_try_connect(ADS_STRUCT *ads, const char *server, unsigned port)
                return False;
        }
        ads->ldap_port = port;
-       ads->ldap_ip = *interpret_addr2(srv);
+       ads->ldap_ip = interpret_addr2(srv);
        free(srv);
 
        return True;
@@ -228,7 +229,7 @@ got_connection:
        if (!ads->auth.user_name) {
                /* by default use the machine account */
                fstring myname;
-               fstrcpy(myname, global_myname());
+               fstrcpy(myname, lp_netbios_name());
                strlower_m(myname);
                asprintf(&ads->auth.user_name, "HOST/%s", myname);
        }
@@ -276,7 +277,7 @@ static struct berval *dup_berval(TALLOC_CTX *ctx, const struct berval *in_val)
 
        if (!in_val) return NULL;
 
-       value = talloc_zero(ctx, sizeof(struct berval));
+       value = talloc_zero(ctx, struct berval);
        if (value == NULL)
                return NULL;
        if (in_val->bv_len == 0) return value;
@@ -317,7 +318,7 @@ static char **ads_push_strvals(TALLOC_CTX *ctx, const char **in_vals)
        
        if (!in_vals) return NULL;
        for (i=0; in_vals[i]; i++); /* count values */
-       values = (char ** ) talloc_zero(ctx, (i+1)*sizeof(char *));
+       values = talloc_zero_array_p(ctx, char *, i+1);
        if (!values) return NULL;
 
        for (i=0; in_vals[i]; i++) {
@@ -336,7 +337,7 @@ static char **ads_pull_strvals(TALLOC_CTX *ctx, const char **in_vals)
        
        if (!in_vals) return NULL;
        for (i=0; in_vals[i]; i++); /* count values */
-       values = (char **) talloc_zero(ctx, (i+1)*sizeof(char *));
+       values = talloc_zero_array_p(ctx, char *, i+1);
        if (!values) return NULL;
 
        for (i=0; in_vals[i]; i++) {
@@ -390,7 +391,8 @@ ADS_STATUS ads_do_paged_search(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(&search_attrs, attrs))) {
+               search_attrs = str_list_copy(ctx, attrs);
+               if (search_attrs == NULL) {
                        rc = LDAP_NO_MEMORY;
                        goto done;
                }
@@ -478,7 +480,7 @@ ADS_STATUS ads_do_paged_search(ADS_STRUCT *ads, const char *bind_path,
        ldap_controls_free(rcontrols);
 
 done:
-       talloc_destroy(ctx);
+       talloc_free(ctx);
        /* if/when we decide to utf8-encode attrs, take out this next line */
        str_list_free(&search_attrs);
 
@@ -615,8 +617,8 @@ ADS_STATUS ads_do_search(ADS_STRUCT *ads, const char *bind_path, int scope,
        else {
                /* This would be the utf8-encoded version...*/
                /* if (!(search_attrs = ads_push_strvals(ctx, attrs)))  */
-               if (!(str_list_copy(&search_attrs, attrs)))
-               {
+               search_attrs = str_list_copy(ctx, attrs);
+               if (search_attrs == NULL) {
                        DEBUG(1,("ads_do_search: str_list_copy() failed!"));
                        rc = LDAP_NO_MEMORY;
                        goto done;
@@ -640,7 +642,7 @@ ADS_STATUS ads_do_search(ADS_STRUCT *ads, const char *bind_path, int scope,
        }
 
  done:
-       talloc_destroy(ctx);
+       talloc_free(ctx);
        /* if/when we decide to utf8-encode attrs, take out this next line */
        str_list_free(&search_attrs);
        return ADS_ERROR(rc);
@@ -758,11 +760,11 @@ ADS_MODLIST ads_init_mods(TALLOC_CTX *ctx)
 #define ADS_MODLIST_ALLOC_SIZE 10
        LDAPMod **mods;
        
-       if ((mods = (LDAPMod **) talloc_zero(ctx, sizeof(LDAPMod *) * 
-                                            (ADS_MODLIST_ALLOC_SIZE + 1))))
+       if ((mods = talloc_zero_array_p(ctx, LDAPMod *, ADS_MODLIST_ALLOC_SIZE + 1))) {
                /* -1 is safety to make sure we don't go over the end.
                   need to reset it to NULL before doing ldap modify */
                mods[ADS_MODLIST_ALLOC_SIZE] = (LDAPMod *) -1;
+       }
        
        return mods;
 }
@@ -795,7 +797,7 @@ static ADS_STATUS ads_modlist_add(TALLOC_CTX *ctx, ADS_MODLIST *mods,
        for (curmod=0; modlist[curmod] && modlist[curmod] != (LDAPMod *) -1;
             curmod++);
        if (modlist[curmod] == (LDAPMod *) -1) {
-               if (!(modlist = talloc_realloc(ctx, modlist, 
+               if (!(modlist = talloc_realloc(modlist, 
                        (curmod+ADS_MODLIST_ALLOC_SIZE+1)*sizeof(LDAPMod *))))
                        return ADS_ERROR(LDAP_NO_MEMORY);
                memset(&modlist[curmod], 0, 
@@ -804,7 +806,7 @@ static ADS_STATUS ads_modlist_add(TALLOC_CTX *ctx, ADS_MODLIST *mods,
                *mods = modlist;
        }
                
-       if (!(modlist[curmod] = talloc_zero(ctx, sizeof(LDAPMod))))
+       if (!(modlist[curmod] = talloc_zero(ctx, LDAPMod)))
                return ADS_ERROR(LDAP_NO_MEMORY);
        modlist[curmod]->mod_type = talloc_strdup(ctx, name);
        if (mod_op & LDAP_MOD_BVALUES) {
@@ -997,8 +999,8 @@ static ADS_STATUS ads_add_machine_acct(ADS_STRUCT *ads, const char *hostname,
                                     "user", "computer", NULL};
        const char *servicePrincipalName[5] = {NULL, NULL, NULL, NULL, NULL};
        char *psp, *psp2;
-       unsigned acct_control;
-       unsigned exists=0;
+       uint_t acct_control;
+       uint_t exists=0;
        LDAPMessage *res;
 
        status = ads_find_machine_acct(ads, (void **)&res, hostname);
@@ -1087,7 +1089,7 @@ static ADS_STATUS ads_add_machine_acct(ADS_STRUCT *ads, const char *hostname,
                }
        }
 done:
-       talloc_destroy(ctx);
+       talloc_free(ctx);
        return ret;
 }
 
@@ -1100,7 +1102,7 @@ static void dump_binary(const char *field, struct berval **values)
        for (i=0; values[i]; i++) {
                printf("%s: ", field);
                for (j=0; j<values[i]->bv_len; j++) {
-                       printf("%02X", (unsigned char)values[i]->bv_val[j]);
+                       printf("%02X", (uint8_t)values[i]->bv_val[j]);
                }
                printf("\n");
        }
@@ -1108,9 +1110,9 @@ static void dump_binary(const char *field, struct berval **values)
 
 struct uuid {
         uint32_t   i1;
-        uint16   i2;
-        uint16   i3;
-        uint8    s[8];
+        uint16_t   i2;
+        uint16_t   i3;
+        uint8_t    s[8];
 };
 
 static void dump_guid(const char *field, struct berval **values)
@@ -1157,13 +1159,13 @@ static void dump_sd(const char *filed, struct berval **values)
        /* parse secdesc */
        if (!sec_io_desc("sd", &psd, &ps, 1)) {
                prs_mem_free(&ps);
-               talloc_destroy(ctx);
+               talloc_free(ctx);
                return;
        }
        if (psd) ads_disp_sd(psd);
 
        prs_mem_free(&ps);
-       talloc_destroy(ctx);
+       talloc_free(ctx);
 }
 
 /*
@@ -1291,7 +1293,7 @@ void ads_process_results(ADS_STRUCT *ads, void *res,
                fn(NULL, NULL, data_area); /* completed an entry */
 
        }
-       talloc_destroy(ctx);
+       talloc_free(ctx);
 }
 
 /**
@@ -1513,7 +1515,7 @@ ADS_STATUS ads_set_machine_sd(ADS_STRUCT *ads, const char *hostname, char *dn)
 ads_set_sd_error:
        ads_msgfree(ads, res);
        prs_mem_free(&ps_wire);
-       talloc_destroy(ctx);
+       talloc_free(ctx);
        return ret;
 }
 
@@ -1592,7 +1594,7 @@ char **ads_pull_strings(ADS_STRUCT *ads,
 
        *num_values = ldap_count_values(values);
 
-       ret = talloc(mem_ctx, sizeof(char *) * (*num_values+1));
+       ret = talloc_array(mem_ctx, char *, *num_values+1);
        if (!ret) {
                ldap_value_free(values);
                return NULL;
@@ -1653,7 +1655,7 @@ char **ads_pull_strings_range(ADS_STRUCT *ads,
             attr; 
             attr = ldap_next_attribute(ads->ld, (LDAPMessage *)msg, ptr)) {
                /* we ignore the fact that this is utf8, as all attributes are ascii... */
-               if (strnequal(attr, expected_range_attrib, strlen(expected_range_attrib))) {
+               if (strncasecmp(attr, expected_range_attrib, strlen(expected_range_attrib)) == 0) {
                        range_attr = attr;
                        break;
                }
@@ -1661,7 +1663,7 @@ char **ads_pull_strings_range(ADS_STRUCT *ads,
        }
        if (!attr) {
                ber_free(ptr, 0);
-               /* nothing here - this feild is just empty */
+               /* nothing here - this field is just empty */
                *more_strings = False;
                return NULL;
        }
@@ -1703,7 +1705,7 @@ char **ads_pull_strings_range(ADS_STRUCT *ads,
                return NULL;
        }
 
-       strings = talloc_realloc(mem_ctx, current_strings,
+       strings = talloc_realloc(current_strings,
                                 sizeof(*current_strings) *
                                 (*num_strings + num_new_strings));
        
@@ -1839,7 +1841,7 @@ int ads_pull_sids(ADS_STRUCT *ads, TALLOC_CTX *mem_ctx,
        for (i=0; values[i]; i++)
                /* nop */ ;
 
-       (*sids) = talloc(mem_ctx, sizeof(DOM_SID) * i);
+       (*sids) = talloc_array(mem_ctx, DOM_SID, i);
        if (!(*sids)) {
                ldap_value_free_len(values);
                return 0;
@@ -1999,7 +2001,7 @@ ADS_STATUS ads_server_info(ADS_STRUCT *ads)
 
        p = strchr(value, ':');
        if (!p) {
-               talloc_destroy(ctx);
+               talloc_free(ctx);
                DEBUG(1, ("ads_server_info: returned ldap server name did not contain a ':' "
                          "so was deemed invalid\n"));
                return ADS_ERROR(LDAP_DECODING_ERROR);
@@ -2010,7 +2012,7 @@ ADS_STATUS ads_server_info(ADS_STRUCT *ads)
        ads->config.ldap_server_name = strdup(p+1);
        p = strchr(ads->config.ldap_server_name, '$');
        if (!p || p[1] != '@') {
-               talloc_destroy(ctx);
+               talloc_free(ctx);
                DEBUG(1, ("ads_server_info: returned ldap server name (%s) does not contain '$@'"
                          " so was deemed invalid\n", ads->config.ldap_server_name));
                SAFE_FREE(ads->config.ldap_server_name);
@@ -2036,7 +2038,7 @@ ADS_STATUS ads_server_info(ADS_STRUCT *ads)
                DEBUG(4,("time offset is %d seconds\n", ads->auth.time_offset));
        }
 
-       talloc_destroy(ctx);
+       talloc_free(ctx);
 
        return ADS_SUCCESS;
 }