r19528: Fix container handling for "net ads user" and "net ads group" functions
authorGünther Deschner <gd@samba.org>
Wed, 1 Nov 2006 11:19:33 +0000 (11:19 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 17:15:41 +0000 (12:15 -0500)
along with some memleaks.

Guenther
(This used to be commit 4bad52c5b3a983418d4216a2c3f5e04926e37e94)

source3/libads/ldap.c
source3/utils/net_ads.c

index 6fbae51179fcd113a7966b9871915b6876e22ad4..1e726f5e5523e0a94f9be16ce190a89b7ea9c807 100644 (file)
@@ -1299,6 +1299,7 @@ char *ads_default_ou_string(ADS_STRUCT *ads, const char *wknguid)
        SAFE_FREE(base);
 
        if (ads_count_replies(ads, res) != 1) {
+               ads_msgfree(ads, res);
                return NULL;
        }
 
@@ -1314,7 +1315,7 @@ char *ads_default_ou_string(ADS_STRUCT *ads, const char *wknguid)
 
        new_ln = wkn_ln - bind_ln;
 
-       ret = wkn_dn_exp[0];
+       ret = SMB_STRDUP(wkn_dn_exp[0]);
 
        for (i=1; i < new_ln; i++) {
                char *s;
@@ -1323,6 +1324,7 @@ char *ads_default_ou_string(ADS_STRUCT *ads, const char *wknguid)
                free(s);
        }
 
+       ads_msgfree(ads, res);
        ads_memfree(ads, wkn_dn);
        ldap_value_free(wkn_dn_exp);
        ldap_value_free(bind_dn_exp);
index 060114f548e047b409d6c062091558d2e699d54f..377bfa22b70d78dcbcde337e531f15643dab127b 100644 (file)
@@ -446,6 +446,7 @@ static int ads_user_add(int argc, const char **argv)
        char *upn, *userdn;
        LDAPMessage *res=NULL;
        int rc = -1;
+       char *ou_str = NULL;
 
        if (argc < 1) return net_ads_user_usage(argc, argv);
        
@@ -465,11 +466,13 @@ static int ads_user_add(int argc, const char **argv)
                goto done;
        }
 
-       if (opt_container == NULL) {
-               opt_container = ads_default_ou_string(ads, WELL_KNOWN_GUID_USERS);
+       if (opt_container) {
+               ou_str = SMB_STRDUP(opt_container);
+       } else {
+               ou_str = ads_default_ou_string(ads, WELL_KNOWN_GUID_USERS);
        }
 
-       status = ads_add_user_acct(ads, argv[0], opt_container, opt_comment);
+       status = ads_add_user_acct(ads, argv[0], ou_str, opt_comment);
 
        if (!ADS_ERR_OK(status)) {
                d_fprintf(stderr, "Could not add user %s: %s\n", argv[0],
@@ -510,6 +513,7 @@ static int ads_user_add(int argc, const char **argv)
        if (res)
                ads_msgfree(ads, res);
        ads_destroy(&ads);
+       SAFE_FREE(ou_str);
        return rc;
 }
 
@@ -654,6 +658,7 @@ static int ads_group_add(int argc, const char **argv)
        ADS_STATUS status;
        LDAPMessage *res=NULL;
        int rc = -1;
+       char *ou_str = NULL;
 
        if (argc < 1) {
                return net_ads_group_usage(argc, argv);
@@ -675,11 +680,13 @@ static int ads_group_add(int argc, const char **argv)
                goto done;
        }
 
-       if (opt_container == NULL) {
-               opt_container = ads_default_ou_string(ads, WELL_KNOWN_GUID_USERS);
+       if (opt_container) {
+               ou_str = SMB_STRDUP(opt_container);
+       } else {
+               ou_str = ads_default_ou_string(ads, WELL_KNOWN_GUID_USERS);
        }
 
-       status = ads_add_group_acct(ads, argv[0], opt_container, opt_comment);
+       status = ads_add_group_acct(ads, argv[0], ou_str, opt_comment);
 
        if (ADS_ERR_OK(status)) {
                d_printf("Group %s added\n", argv[0]);
@@ -693,6 +700,7 @@ static int ads_group_add(int argc, const char **argv)
        if (res)
                ads_msgfree(ads, res);
        ads_destroy(&ads);
+       SAFE_FREE(ou_str);
        return rc;
 }
 
@@ -1123,8 +1131,10 @@ static ADS_STATUS net_precreate_machine_acct( ADS_STRUCT *ads, const char *ou )
        LDAPMessage *res = NULL;
 
        ou_str = ads_ou_string(ads, ou);
-       asprintf(&dn, "%s,%s", ou_str, ads->config.bind_path);
-       free(ou_str);
+       if ((asprintf(&dn, "%s,%s", ou_str, ads->config.bind_path)) == -1) {
+               SAFE_FREE(ou_str);
+               return ADS_ERROR(LDAP_NO_MEMORY);
+       }
 
        rc = ads_search_dn(ads, &res, dn, NULL);
        ads_msgfree(ads, res);
@@ -1139,6 +1149,7 @@ static ADS_STATUS net_precreate_machine_acct( ADS_STRUCT *ads, const char *ou )
                }
        }
 
+       SAFE_FREE( ou_str );
        SAFE_FREE( dn );
 
        return rc;