r18019: Fix a C++ warnings: Don't use void * in libads/ for LDAPMessage anymore.
[sfrench/samba-autobuild/.git] / source3 / libads / ldap.c
index 5ff1ce435d64502c49b8cf9db6f84b0bc0653c06..970311d5ca43d10d3a7fe0052b0cdfb308e186b0 100644 (file)
@@ -65,6 +65,11 @@ static void gotalarm_sig(void)
 
        ldp = ldap_open(server, port);
 
+       if (ldp == NULL) {
+               DEBUG(2,("Could not open LDAP connection to %s:%d: %s\n",
+                        server, port, strerror(errno)));
+       }
+
        /* Teardown timeout. */
        CatchSignal(SIGALRM, SIGNAL_CAST SIG_IGN);
        alarm(0);
@@ -110,6 +115,32 @@ static int ldap_search_with_timeout(LDAP *ld,
        return result;
 }
 
+#ifdef HAVE_KRB5
+/**********************************************
+ Do client and server sitename match ?
+**********************************************/
+
+BOOL ads_sitename_match(ADS_STRUCT *ads)
+{
+       if (ads->config.server_site_name == NULL &&
+           ads->config.client_site_name == NULL ) {
+               DEBUG(10,("ads_sitename_match: both null\n"));
+               return True;
+       }
+       if (ads->config.server_site_name &&
+           ads->config.client_site_name &&
+           strequal(ads->config.server_site_name,
+                    ads->config.client_site_name)) {
+               DEBUG(10,("ads_sitename_match: name %s match\n", ads->config.server_site_name));
+               return True;
+       }
+       DEBUG(10,("ads_sitename_match: no match %s %s\n",
+               ads->config.server_site_name ? ads->config.server_site_name : "NULL",
+               ads->config.client_site_name ? ads->config.client_site_name : "NULL"));
+       return False;
+}
+#endif
+
 /*
   try a connection to a given ldap server, returning True and setting the servers IP
   in the ads struct if successful
@@ -152,21 +183,32 @@ BOOL ads_try_connect(ADS_STRUCT *ads, const char *server )
        SAFE_FREE(ads->config.realm);
        SAFE_FREE(ads->config.bind_path);
        SAFE_FREE(ads->config.ldap_server_name);
+       SAFE_FREE(ads->config.server_site_name);
+       SAFE_FREE(ads->config.client_site_name);
        SAFE_FREE(ads->server.workgroup);
 
+       ads->config.flags              = cldap_reply.flags;
        ads->config.ldap_server_name   = SMB_STRDUP(cldap_reply.hostname);
        strupper_m(cldap_reply.domain);
        ads->config.realm              = SMB_STRDUP(cldap_reply.domain);
        ads->config.bind_path          = ads_build_dn(ads->config.realm);
+       if (*cldap_reply.server_site_name) {
+               ads->config.server_site_name =
+                       SMB_STRDUP(cldap_reply.server_site_name);
+       }
+       if (*cldap_reply.client_site_name) {
+               ads->config.client_site_name =
+                       SMB_STRDUP(cldap_reply.client_site_name);
+       }
+               
        ads->server.workgroup          = SMB_STRDUP(cldap_reply.netbios_domain);
 
        ads->ldap_port = LDAP_PORT;
        ads->ldap_ip = *interpret_addr2(srv);
        SAFE_FREE(srv);
        
-       /* cache the successful connection */
-
-       saf_store( ads->server.workgroup, server );
+       /* Store our site name. */
+       sitename_store( cldap_reply.client_site_name );
 
        return True;
 }
@@ -177,7 +219,7 @@ BOOL ads_try_connect(ADS_STRUCT *ads, const char *server )
  disabled
 **********************************************************************/
 
-static BOOL ads_find_dc(ADS_STRUCT *ads)
+static NTSTATUS ads_find_dc(ADS_STRUCT *ads)
 {
        const char *c_realm;
        int count, i=0;
@@ -185,6 +227,7 @@ static BOOL ads_find_dc(ADS_STRUCT *ads)
        pstring realm;
        BOOL got_realm = False;
        BOOL use_own_domain = False;
+       NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
 
        /* if the realm and workgroup are both empty, assume they are ours */
 
@@ -215,7 +258,7 @@ again:
                
                if ( !c_realm || !*c_realm ) {
                        DEBUG(0,("ads_find_dc: no realm or workgroup!  Don't know what to do\n"));
-                       return False;
+                       return NT_STATUS_INVALID_PARAMETER; /* rather need MISSING_PARAMETER ... */
                }
        }
        
@@ -224,16 +267,17 @@ again:
        DEBUG(6,("ads_find_dc: looking for %s '%s'\n", 
                (got_realm ? "realm" : "domain"), realm));
 
-       if ( !get_sorted_dc_list(realm, &ip_list, &count, got_realm) ) {
+       status = get_sorted_dc_list(realm, &ip_list, &count, got_realm);
+       if (!NT_STATUS_IS_OK(status)) {
                /* fall back to netbios if we can */
                if ( got_realm && !lp_disable_netbios() ) {
                        got_realm = False;
                        goto again;
                }
                
-               return False;
+               return status;
        }
-                       
+
        /* if we fail this loop, then giveup since all the IP addresses returned were dead */
        for ( i=0; i<count; i++ ) {
                fstring server;
@@ -242,10 +286,30 @@ again:
                
                if ( !NT_STATUS_IS_OK(check_negative_conn_cache(realm, server)) )
                        continue;
+
+               if (!got_realm) {
+                       /* realm in this case is a workgroup name. We need
+                          to ignore any IP addresses in the negative connection
+                          cache that match ip addresses returned in the ad realm
+                          case. It sucks that I have to reproduce the logic above... */
+                       c_realm = ads->server.realm;
+                       if ( !c_realm || !*c_realm ) {
+                               if ( !ads->server.workgroup || !*ads->server.workgroup ) {
+                                       c_realm = lp_realm();
+                               }
+                       }
+                       if (c_realm && *c_realm &&
+                                       !NT_STATUS_IS_OK(check_negative_conn_cache(c_realm, server))) {
+                               /* Ensure we add the workgroup name for this
+                                  IP address as negative too. */
+                               add_failed_connection_entry( realm, server, NT_STATUS_UNSUCCESSFUL );
+                               continue;
+                       }
+               }
                        
                if ( ads_try_connect(ads, server) ) {
                        SAFE_FREE(ip_list);
-                       return True;
+                       return NT_STATUS_OK;
                }
                
                /* keep track of failures */
@@ -254,7 +318,7 @@ again:
 
        SAFE_FREE(ip_list);
        
-       return False;
+       return NT_STATUS_NO_LOGON_SERVERS;
 }
 
 
@@ -267,6 +331,7 @@ ADS_STATUS ads_connect(ADS_STRUCT *ads)
 {
        int version = LDAP_VERSION3;
        ADS_STATUS status;
+       NTSTATUS ntstatus;
 
        ads->last_attempt = time(NULL);
        ads->ld = NULL;
@@ -278,11 +343,12 @@ ADS_STATUS ads_connect(ADS_STRUCT *ads)
                goto got_connection;
        }
 
-       if (ads_find_dc(ads)) {
+       ntstatus = ads_find_dc(ads);
+       if (NT_STATUS_IS_OK(ntstatus)) {
                goto got_connection;
        }
 
-       return ADS_ERROR_SYSTEM(errno?errno:ENOENT);
+       return ADS_ERROR_NT(ntstatus);
 
 got_connection:
        DEBUG(3,("Connected to LDAP server %s\n", inet_ntoa(ads->ldap_ip)));
@@ -326,6 +392,10 @@ got_connection:
        {
                return ADS_ERROR(LDAP_OPERATIONS_ERROR);
        }
+
+       /* cache the successful connection */
+       saf_store( ads->server.workgroup, inet_ntoa(ads->ldap_ip));
+
        ldap_set_option(ads->ld, LDAP_OPT_PROTOCOL_VERSION, &version);
 
        status = ADS_ERROR(smb_ldap_start_tls(ads->ld, version));
@@ -368,7 +438,8 @@ static struct berval *dup_berval(TALLOC_CTX *ctx, const struct berval *in_val)
        if (in_val->bv_len == 0) return value;
 
        value->bv_len = in_val->bv_len;
-       value->bv_val = TALLOC_MEMDUP(ctx, in_val->bv_val, in_val->bv_len);
+       value->bv_val = (char *)TALLOC_MEMDUP(ctx, in_val->bv_val,
+                                             in_val->bv_len);
        return value;
 }
 
@@ -447,10 +518,12 @@ static char **ads_pull_strvals(TALLOC_CTX *ctx, const char **in_vals)
  * @param cookie The paged results cookie to be returned on subsequent calls
  * @return status of search
  **/
-ADS_STATUS ads_do_paged_search_args(ADS_STRUCT *ads, const char *bind_path,
-                                   int scope, const char *expr,
-                                   const char **attrs, void *args, void **res, 
-                                   int *count, void **cookie)
+static ADS_STATUS ads_do_paged_search_args(ADS_STRUCT *ads,
+                                          const char *bind_path,
+                                          int scope, const char *expr,
+                                          const char **attrs, void *args,
+                                          LDAPMessage **res, 
+                                          int *count, struct berval **cookie)
 {
        int rc, i, version;
        char *utf8_expr, *utf8_path, **search_attrs;
@@ -497,7 +570,7 @@ ADS_STATUS ads_do_paged_search_args(ADS_STRUCT *ads, const char *bind_path,
        }
 
        cookie_be = ber_alloc_t(LBER_USE_DER);
-       if (cookie && *cookie) {
+       if (*cookie) {
                ber_printf(cookie_be, "{iO}", (ber_int_t) 1000, *cookie);
                ber_bvfree(*cookie); /* don't need it from last time */
                *cookie = NULL;
@@ -623,10 +696,10 @@ done:
        return ADS_ERROR(rc);
 }
 
-ADS_STATUS ads_do_paged_search(ADS_STRUCT *ads, const char *bind_path,
-                              int scope, const char *expr,
-                              const char **attrs, void **res, 
-                              int *count, void **cookie)
+static ADS_STATUS ads_do_paged_search(ADS_STRUCT *ads, const char *bind_path,
+                                     int scope, const char *expr,
+                                     const char **attrs, LDAPMessage **res, 
+                                     int *count, struct berval **cookie)
 {
        return ads_do_paged_search_args(ads, bind_path, scope, expr, attrs, NULL, res, count, cookie);
 }
@@ -643,11 +716,12 @@ ADS_STATUS ads_do_paged_search(ADS_STRUCT *ads, const char *bind_path,
  * @param res ** which will contain results - free res* with ads_msgfree()
  * @return status of search
  **/
-ADS_STATUS ads_do_search_all_args(ADS_STRUCT *ads, const char *bind_path,
-                                 int scope, const char *expr,
-                                 const char **attrs, void *args, void **res)
+ ADS_STATUS ads_do_search_all_args(ADS_STRUCT *ads, const char *bind_path,
+                                  int scope, const char *expr,
+                                  const char **attrs, void *args,
+                                  LDAPMessage **res)
 {
-       void *cookie = NULL;
+       struct berval *cookie = NULL;
        int count = 0;
        ADS_STATUS status;
 
@@ -660,7 +734,7 @@ ADS_STATUS ads_do_search_all_args(ADS_STRUCT *ads, const char *bind_path,
 
 #ifdef HAVE_LDAP_ADD_RESULT_ENTRY
        while (cookie) {
-               void *res2 = NULL;
+               LDAPMessage *res2 = NULL;
                ADS_STATUS status2;
                LDAPMessage *msg, *next;
 
@@ -686,9 +760,9 @@ ADS_STATUS ads_do_search_all_args(ADS_STRUCT *ads, const char *bind_path,
        return status;
 }
 
-ADS_STATUS ads_do_search_all(ADS_STRUCT *ads, const char *bind_path,
-                            int scope, const char *expr,
-                            const char **attrs, void **res)
+ ADS_STATUS ads_do_search_all(ADS_STRUCT *ads, const char *bind_path,
+                             int scope, const char *expr,
+                             const char **attrs, LDAPMessage **res)
 {
        return ads_do_search_all_args(ads, bind_path, scope, expr, attrs, NULL, res);
 }
@@ -710,10 +784,10 @@ ADS_STATUS ads_do_search_all_fn(ADS_STRUCT *ads, const char *bind_path,
                                BOOL(*fn)(char *, void **, void *), 
                                void *data_area)
 {
-       void *cookie = NULL;
+       struct berval *cookie = NULL;
        int count = 0;
        ADS_STATUS status;
-       void *res;
+       LDAPMessage *res;
 
        status = ads_do_paged_search(ads, bind_path, scope, expr, attrs, &res,
                                     &count, &cookie);
@@ -746,9 +820,9 @@ ADS_STATUS ads_do_search_all_fn(ADS_STRUCT *ads, const char *bind_path,
  * @param res ** which will contain results - free res* with ads_msgfree()
  * @return status of search
  **/
-ADS_STATUS ads_do_search(ADS_STRUCT *ads, const char *bind_path, int scope, 
-                        const char *expr,
-                        const char **attrs, void **res)
+ ADS_STATUS ads_do_search(ADS_STRUCT *ads, const char *bind_path, int scope, 
+                         const char *expr,
+                         const char **attrs, LDAPMessage **res)
 {
        int rc;
        char *utf8_expr, *utf8_path, **search_attrs = NULL;
@@ -810,9 +884,8 @@ ADS_STATUS ads_do_search(ADS_STRUCT *ads, const char *bind_path, int scope,
  * @param attrs Attributes to retrieve
  * @return status of search
  **/
-ADS_STATUS ads_search(ADS_STRUCT *ads, void **res, 
-                     const char *expr, 
-                     const char **attrs)
+ ADS_STATUS ads_search(ADS_STRUCT *ads, LDAPMessage **res, 
+                      const char *expr, const char **attrs)
 {
        return ads_do_search(ads, ads->config.bind_path, LDAP_SCOPE_SUBTREE, 
                             expr, attrs, res);
@@ -826,12 +899,11 @@ ADS_STATUS ads_search(ADS_STRUCT *ads, void **res,
  * @param attrs Attributes to retrieve
  * @return status of search
  **/
-ADS_STATUS ads_search_dn(ADS_STRUCT *ads, void *_res, 
-                        const char *dn, 
-                        const char **attrs)
+ ADS_STATUS ads_search_dn(ADS_STRUCT *ads, LDAPMessage **res, 
+                         const char *dn, const char **attrs)
 {
-       void **res = (void **)_res;
-       return ads_do_search(ads, dn, LDAP_SCOPE_BASE, "(objectclass=*)", attrs, res);
+       return ads_do_search(ads, dn, LDAP_SCOPE_BASE, "(objectclass=*)",
+                            attrs, res);
 }
 
 /**
@@ -839,7 +911,7 @@ ADS_STATUS ads_search_dn(ADS_STRUCT *ads, void *_res,
  * @param ads connection to ads server
  * @param msg Search results to free
  **/
-void ads_msgfree(ADS_STRUCT *ads, void *msg)
+ void ads_msgfree(ADS_STRUCT *ads, LDAPMessage *msg)
 {
        if (!msg) return;
        ldap_msgfree(msg);
@@ -861,7 +933,7 @@ void ads_memfree(ADS_STRUCT *ads, void *mem)
  * @param msg Search result
  * @return dn string
  **/
-char *ads_get_dn(ADS_STRUCT *ads, void *msg)
+ char *ads_get_dn(ADS_STRUCT *ads, LDAPMessage *msg)
 {
        char *utf8_dn, *unix_dn;
 
@@ -887,7 +959,7 @@ char *ads_get_dn(ADS_STRUCT *ads, void *msg)
  * @param msg Search result
  * @return dn string
  **/
-char *ads_get_dn_canonical(ADS_STRUCT *ads, void *msg)
+ char *ads_get_dn_canonical(ADS_STRUCT *ads, LDAPMessage *msg)
 {
 #ifdef HAVE_LDAP_DN2AD_CANONICAL
        return ldap_dn2ad_canonical(ads_get_dn(ads, msg));
@@ -925,7 +997,8 @@ char *ads_parent_dn(const char *dn)
  * @param host Hostname to search for
  * @return status of search
  **/
-ADS_STATUS ads_find_machine_acct(ADS_STRUCT *ads, void **res, const char *machine)
+ ADS_STATUS ads_find_machine_acct(ADS_STRUCT *ads, LDAPMessage **res,
+                                 const char *machine)
 {
        ADS_STATUS status;
        char *expr;
@@ -1201,7 +1274,7 @@ char *ads_ou_string(ADS_STRUCT *ads, const char *org_unit)
 char *ads_default_ou_string(ADS_STRUCT *ads, const char *wknguid)
 {
        ADS_STATUS status;
-       void *res;
+       LDAPMessage *res;
        char *base, *wkn_dn, *ret, **wkn_dn_exp, **bind_dn_exp;
        const char *attrs[] = {"distinguishedName", NULL};
        int new_ln, wkn_ln, bind_ln, i;
@@ -1268,7 +1341,8 @@ char *ads_default_ou_string(ADS_STRUCT *ads, const char *wknguid)
 ADS_STATUS ads_add_strlist(TALLOC_CTX *ctx, ADS_MODLIST *mods,
                                const char *name, const char **vals)
 {
-       return ads_modlist_add(ctx, mods, LDAP_MOD_ADD, name, (const void **) vals);
+       return ads_modlist_add(ctx, mods, LDAP_MOD_ADD, name,
+                              (const void *) vals);
 }
 
 /**
@@ -1291,7 +1365,7 @@ uint32 ads_get_kvno(ADS_STRUCT *ads, const char *machine_name)
        if (asprintf(&filter, "(samAccountName=%s$)", machine_name) == -1) {
                return kvno;
        }
-       ret = ads_search(ads, (void**)(void *)&res, filter, attrs);
+       ret = ads_search(ads, &res, filter, attrs);
        SAFE_FREE(filter);
        if (!ADS_ERR_OK(ret) && ads_count_replies(ads, res)) {
                DEBUG(1,("ads_get_kvno: Computer Account For %s not found.\n", machine_name));
@@ -1345,7 +1419,7 @@ ADS_STATUS ads_clear_service_principal_names(ADS_STRUCT *ads, const char *machin
        ADS_STATUS ret = ADS_ERROR(LDAP_SUCCESS);
        char *dn_string = NULL;
 
-       ret = ads_find_machine_acct(ads, (void **)(void *)&res, machine_name);
+       ret = ads_find_machine_acct(ads, &res, machine_name);
        if (!ADS_ERR_OK(ret) || ads_count_replies(ads, res) != 1) {
                DEBUG(5,("ads_clear_service_principal_names: WARNING: Host Account for %s not found... skipping operation.\n", machine_name));
                DEBUG(5,("ads_clear_service_principal_names: WARNING: Service Principals for %s have NOT been cleared.\n", machine_name));
@@ -1414,7 +1488,7 @@ ADS_STATUS ads_add_service_principal_name(ADS_STRUCT *ads, const char *machine_n
        char *dn_string = NULL;
        const char *servicePrincipalName[3] = {NULL, NULL, NULL};
 
-       ret = ads_find_machine_acct(ads, (void **)(void *)&res, machine_name);
+       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));
@@ -1681,7 +1755,7 @@ static BOOL ads_dump_field(char *field, void **values, void *data_area)
  * @param res Results to dump
  **/
 
-void ads_dump(ADS_STRUCT *ads, void *res)
+ void ads_dump(ADS_STRUCT *ads, LDAPMessage *res)
 {
        ads_process_results(ads, res, ads_dump_field, NULL);
 }
@@ -1697,11 +1771,11 @@ void ads_dump(ADS_STRUCT *ads, void *res)
  * @param fn Function for processing each result
  * @param data_area user-defined area to pass to function
  **/
-void ads_process_results(ADS_STRUCT *ads, void *res,
-                        BOOL(*fn)(char *, void **, void *),
-                        void *data_area)
+ void ads_process_results(ADS_STRUCT *ads, LDAPMessage *res,
+                         BOOL(*fn)(char *, void **, void *),
+                         void *data_area)
 {
-       void *msg;
+       LDAPMessage *msg;
        TALLOC_CTX *ctx;
 
        if (!(ctx = talloc_init("ads_process_results")))
@@ -1766,9 +1840,9 @@ int ads_count_replies(ADS_STRUCT *ads, void *res)
  * @param res Results of search
  * @return first entry from result
  **/
-void *ads_first_entry(ADS_STRUCT *ads, void *res)
+ LDAPMessage *ads_first_entry(ADS_STRUCT *ads, LDAPMessage *res)
 {
-       return (void *)ldap_first_entry(ads->ld, (LDAPMessage *)res);
+       return ldap_first_entry(ads->ld, res);
 }
 
 /**
@@ -1777,9 +1851,9 @@ void *ads_first_entry(ADS_STRUCT *ads, void *res)
  * @param res Results of search
  * @return next entry from result
  **/
-void *ads_next_entry(ADS_STRUCT *ads, void *res)
+ LDAPMessage *ads_next_entry(ADS_STRUCT *ads, LDAPMessage *res)
 {
-       return (void *)ldap_next_entry(ads->ld, (LDAPMessage *)res);
+       return ldap_next_entry(ads->ld, res);
 }
 
 /**
@@ -1790,8 +1864,8 @@ void *ads_next_entry(ADS_STRUCT *ads, void *res)
  * @param field Attribute to retrieve
  * @return Result string in talloc context
  **/
-char *ads_pull_string(ADS_STRUCT *ads, 
-                     TALLOC_CTX *mem_ctx, void *msg, const char *field)
+ char *ads_pull_string(ADS_STRUCT *ads, TALLOC_CTX *mem_ctx, LDAPMessage *msg,
+                      const char *field)
 {
        char **values;
        char *ret = NULL;
@@ -1821,9 +1895,9 @@ char *ads_pull_string(ADS_STRUCT *ads,
  * @param field Attribute to retrieve
  * @return Result strings in talloc context
  **/
-char **ads_pull_strings(ADS_STRUCT *ads, 
-                       TALLOC_CTX *mem_ctx, void *msg, const char *field,
-                       size_t *num_values)
+ char **ads_pull_strings(ADS_STRUCT *ads, TALLOC_CTX *mem_ctx,
+                        LDAPMessage *msg, const char *field,
+                        size_t *num_values)
 {
        char **values;
        char **ret = NULL;
@@ -1866,13 +1940,13 @@ char **ads_pull_strings(ADS_STRUCT *ads,
  * @param more_values Are there more values to get?
  * @return Result strings in talloc context
  **/
-char **ads_pull_strings_range(ADS_STRUCT *ads, 
-                             TALLOC_CTX *mem_ctx,
-                             void *msg, const char *field,
-                             char **current_strings,
-                             const char **next_attribute,
-                             size_t *num_strings,
-                             BOOL *more_strings)
+ char **ads_pull_strings_range(ADS_STRUCT *ads, 
+                              TALLOC_CTX *mem_ctx,
+                              LDAPMessage *msg, const char *field,
+                              char **current_strings,
+                              const char **next_attribute,
+                              size_t *num_strings,
+                              BOOL *more_strings)
 {
        char *attr;
        char *expected_range_attrib, *range_attr;
@@ -1989,8 +2063,8 @@ char **ads_pull_strings_range(ADS_STRUCT *ads,
  * @param v Pointer to int to store result
  * @return boolean inidicating success
 */
-BOOL ads_pull_uint32(ADS_STRUCT *ads, 
-                    void *msg, const char *field, uint32 *v)
+ BOOL ads_pull_uint32(ADS_STRUCT *ads, LDAPMessage *msg, const char *field,
+                     uint32 *v)
 {
        char **values;
 
@@ -2014,8 +2088,7 @@ BOOL ads_pull_uint32(ADS_STRUCT *ads,
  * @param guid 37-byte area to receive text guid
  * @return boolean indicating success
  **/
-BOOL ads_pull_guid(ADS_STRUCT *ads,
-                  void *msg, struct uuid *guid)
+ BOOL ads_pull_guid(ADS_STRUCT *ads, LDAPMessage *msg, struct uuid *guid)
 {
        char **values;
        UUID_FLAT flat_guid;
@@ -2044,8 +2117,8 @@ BOOL ads_pull_guid(ADS_STRUCT *ads,
  * @param sid Pointer to sid to store result
  * @return boolean inidicating success
 */
-BOOL ads_pull_sid(ADS_STRUCT *ads, 
-                 void *msg, const char *field, DOM_SID *sid)
+ BOOL ads_pull_sid(ADS_STRUCT *ads, LDAPMessage *msg, const char *field,
+                  DOM_SID *sid)
 {
        struct berval **values;
        BOOL ret = False;
@@ -2071,8 +2144,8 @@ BOOL ads_pull_sid(ADS_STRUCT *ads,
  * @param sids pointer to sid array to allocate
  * @return the count of SIDs pulled
  **/
-int ads_pull_sids(ADS_STRUCT *ads, TALLOC_CTX *mem_ctx,
-                 void *msg, const char *field, DOM_SID **sids)
+ int ads_pull_sids(ADS_STRUCT *ads, TALLOC_CTX *mem_ctx,
+                  LDAPMessage *msg, const char *field, DOM_SID **sids)
 {
        struct berval **values;
        BOOL ret;
@@ -2115,8 +2188,8 @@ int ads_pull_sids(ADS_STRUCT *ads, TALLOC_CTX *mem_ctx,
  * @param sd Pointer to *SEC_DESC to store result (talloc()ed)
  * @return boolean inidicating success
 */
-BOOL ads_pull_sd(ADS_STRUCT *ads, TALLOC_CTX *mem_ctx,
-                 void *msg, const char *field, SEC_DESC **sd)
+ BOOL ads_pull_sd(ADS_STRUCT *ads, TALLOC_CTX *mem_ctx,
+                 LDAPMessage *msg, const char *field, SEC_DESC **sd)
 {
        struct berval **values;
        prs_struct      ps;
@@ -2148,7 +2221,8 @@ BOOL ads_pull_sd(ADS_STRUCT *ads, TALLOC_CTX *mem_ctx,
  * @param msg Results of search
  * @return the username
  */
-char *ads_pull_username(ADS_STRUCT *ads, TALLOC_CTX *mem_ctx, void *msg)
+ char *ads_pull_username(ADS_STRUCT *ads, TALLOC_CTX *mem_ctx,
+                        LDAPMessage *msg)
 {
 #if 0  /* JERRY */
        char *ret, *p;
@@ -2178,7 +2252,7 @@ ADS_STATUS ads_USN(ADS_STRUCT *ads, uint32 *usn)
 {
        const char *attrs[] = {"highestCommittedUSN", NULL};
        ADS_STATUS status;
-       void *res;
+       LDAPMessage *res;
 
        status = ads_do_search_retry(ads, "", LDAP_SCOPE_BASE, "(objectclass=*)", attrs, &res);
        if (!ADS_ERR_OK(status)) 
@@ -2220,7 +2294,7 @@ ADS_STATUS ads_current_time(ADS_STRUCT *ads)
 {
        const char *attrs[] = {"currentTime", NULL};
        ADS_STATUS status;
-       void *res;
+       LDAPMessage *res;
        char *timestr;
        TALLOC_CTX *ctx;
        ADS_STRUCT *ads_s = ads;
@@ -2285,7 +2359,7 @@ ADS_STATUS ads_domain_func_level(ADS_STRUCT *ads, uint32 *val)
 {
        const char *attrs[] = {"domainFunctionality", NULL};
        ADS_STATUS status;
-       void *res;
+       LDAPMessage *res;
        ADS_STRUCT *ads_s = ads;
        
        *val = DS_DOMAIN_FUNCTION_2000;
@@ -2341,7 +2415,7 @@ done:
 ADS_STATUS ads_domain_sid(ADS_STRUCT *ads, DOM_SID *sid)
 {
        const char *attrs[] = {"objectSid", NULL};
-       void *res;
+       LDAPMessage *res;
        ADS_STATUS rc;
 
        rc = ads_do_search_retry(ads, ads->config.bind_path, LDAP_SCOPE_BASE, "(objectclass=*)", 
@@ -2366,7 +2440,7 @@ ADS_STATUS ads_domain_sid(ADS_STRUCT *ads, DOM_SID *sid)
 ADS_STATUS ads_site_dn(ADS_STRUCT *ads, TALLOC_CTX *mem_ctx, const char **site_name)
 {
        ADS_STATUS status;
-       void *res;
+       LDAPMessage *res;
        const char *dn, *service_name;
        const char *attrs[] = { "dsServiceName", NULL };
 
@@ -2410,7 +2484,7 @@ ADS_STATUS ads_site_dn(ADS_STRUCT *ads, TALLOC_CTX *mem_ctx, const char **site_n
 ADS_STATUS ads_site_dn_for_machine(ADS_STRUCT *ads, TALLOC_CTX *mem_ctx, const char *computer_name, const char **site_dn)
 {
        ADS_STATUS status;
-       void *res;
+       LDAPMessage *res;
        const char *parent, *config_context, *filter;
        const char *attrs[] = { "configurationNamingContext", NULL };
        char *dn;
@@ -2479,7 +2553,7 @@ ADS_STATUS ads_site_dn_for_machine(ADS_STRUCT *ads, TALLOC_CTX *mem_ctx, const c
 ADS_STATUS ads_upn_suffixes(ADS_STRUCT *ads, TALLOC_CTX *mem_ctx, char **suffixes, size_t *num_suffixes)
 {
        ADS_STATUS status;
-       void *res;
+       LDAPMessage *res;
        const char *config_context, *base;
        const char *attrs[] = { "configurationNamingContext", NULL };
        const char *attrs2[] = { "uPNSuffixes", NULL };
@@ -2508,7 +2582,7 @@ ADS_STATUS ads_upn_suffixes(ADS_STRUCT *ads, TALLOC_CTX *mem_ctx, char **suffixe
                return ADS_ERROR(LDAP_NO_SUCH_OBJECT);
        }
 
-       suffixes = ads_pull_strings(ads, mem_ctx, &res, "uPNSuffixes", num_suffixes);
+       suffixes = ads_pull_strings(ads, mem_ctx, res, "uPNSuffixes", num_suffixes);
        if (suffixes == NULL) {
                ads_msgfree(ads, res);
                return ADS_ERROR(LDAP_NO_MEMORY);
@@ -2605,12 +2679,12 @@ 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, 
-                                 void *msg, 
-                                 const char *field,
-                                 enum ads_extended_dn_flags flags,
-                                 DOM_SID **sids)
+ 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;
@@ -2652,7 +2726,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, (void **)(void *)&res, global_myname());
+       status = ads_find_machine_acct(ads, &res, global_myname());
        if (!ADS_ERR_OK(status)) {
                DEBUG(0,("ads_get_dnshostname: Failed to find account for %s\n",
                        global_myname()));
@@ -2684,20 +2758,20 @@ char* ads_get_upn( ADS_STRUCT *ads, TALLOC_CTX *ctx, const char *machine_name )
        int count = 0;
        char *name = NULL;
        
-       status = ads_find_machine_acct(ads, (void **)(void *)&res, global_myname());
+       status = ads_find_machine_acct(ads, &res, global_myname());
        if (!ADS_ERR_OK(status)) {
-               DEBUG(0,("ads_get_dnshostname: Failed to find account for %s\n",
+               DEBUG(0,("ads_get_upn: Failed to find account for %s\n",
                        global_myname()));
                goto out;
        }
                
        if ( (count = ads_count_replies(ads, res)) != 1 ) {
-               DEBUG(1,("ads_get_dnshostname: %d entries returned!\n", count));
+               DEBUG(1,("ads_get_upn: %d entries returned!\n", count));
                goto out;
        }
                
        if ( (name = ads_pull_string(ads, ctx, res, "userPrincipalName")) == NULL ) {
-               DEBUG(0,("ads_get_dnshostname: No userPrincipalName attribute!\n"));
+               DEBUG(2,("ads_get_upn: No userPrincipalName attribute!\n"));
        }
 
 out:
@@ -2716,7 +2790,7 @@ char* ads_get_samaccountname( ADS_STRUCT *ads, TALLOC_CTX *ctx, const char *mach
        int count = 0;
        char *name = NULL;
        
-       status = ads_find_machine_acct(ads, (void **)(void *)&res, global_myname());
+       status = ads_find_machine_acct(ads, &res, global_myname());
        if (!ADS_ERR_OK(status)) {
                DEBUG(0,("ads_get_dnshostname: Failed to find account for %s\n",
                        global_myname()));