Move to talloced profile so that we can free ressources more easily
authorMatthieu Patou <mat@matws.net>
Mon, 19 Mar 2012 19:10:46 +0000 (19:10 +0000)
committerMatthieu Patou <mat@matws.net>
Mon, 19 Mar 2012 19:10:46 +0000 (19:10 +0000)
libmapi/IProfAdmin.c
utils/mapiprofile.c

index 75dfc6b9e646872134024ffe168655fa6b799433..627d22f6a882a8135ed5c006e2b2a6a8ca76ca69 100644 (file)
@@ -57,30 +57,39 @@ static enum MAPISTATUS ldb_load_profile(TALLOC_CTX *mem_ctx,
        if (ret != LDB_SUCCESS) return MAPI_E_NOT_FOUND;
 
        /* profile not found */
-       if (!res->count) return MAPI_E_NOT_FOUND;
+       if (!res->count) {
+               talloc_free(res);
+               return MAPI_E_NOT_FOUND;
+       }
+
        /* more than one profile */
-       if (res->count > 1) return MAPI_E_COLLISION;
+       if (res->count > 1) {
+               talloc_free(res);
+               return MAPI_E_COLLISION;
+       }
 
        /* fills in profile with query result */
        msg = res->msgs[0];
 
-       profile->username = ldb_msg_find_attr_as_string(msg, "username", NULL);
-       profile->password = password ? password : ldb_msg_find_attr_as_string(msg, "password", NULL);
-       profile->workstation = ldb_msg_find_attr_as_string(msg, "workstation", NULL);
-       profile->realm = ldb_msg_find_attr_as_string(msg, "realm", NULL);
-       profile->domain = ldb_msg_find_attr_as_string(msg, "domain", NULL);
-       profile->mailbox = ldb_msg_find_attr_as_string(msg, "EmailAddress", NULL);
-       profile->homemdb = ldb_msg_find_attr_as_string(msg, "HomeMDB", NULL);
-       profile->localaddr = ldb_msg_find_attr_as_string(msg, "localaddress", NULL);
-       profile->server = ldb_msg_find_attr_as_string(msg, "binding", NULL);
+       profile->username = talloc_steal(profile, ldb_msg_find_attr_as_string(msg, "username", NULL));
+       profile->password = password ? password : talloc_steal(profile, ldb_msg_find_attr_as_string(msg, "password", NULL));
+       profile->workstation = talloc_steal(profile, ldb_msg_find_attr_as_string(msg, "workstation", NULL));
+       profile->realm = talloc_steal(profile, ldb_msg_find_attr_as_string(msg, "realm", NULL));
+       profile->domain = talloc_steal(profile, ldb_msg_find_attr_as_string(msg, "domain", NULL));
+       profile->mailbox = talloc_steal(profile, ldb_msg_find_attr_as_string(msg, "EmailAddress", NULL));
+       profile->homemdb = talloc_steal(profile, ldb_msg_find_attr_as_string(msg, "HomeMDB", NULL));
+       profile->localaddr = talloc_steal(profile, ldb_msg_find_attr_as_string(msg, "localaddress", NULL));
+       profile->server = talloc_steal(profile, ldb_msg_find_attr_as_string(msg, "binding", NULL));
        profile->seal = ldb_msg_find_attr_as_bool(msg, "seal", false);
-       profile->org = ldb_msg_find_attr_as_string(msg, "Organization", NULL);
-       profile->ou = ldb_msg_find_attr_as_string(msg, "OrganizationUnit", NULL);
+       profile->org = talloc_steal(profile, ldb_msg_find_attr_as_string(msg, "Organization", NULL));
+       profile->ou = talloc_steal(profile, ldb_msg_find_attr_as_string(msg, "OrganizationUnit", NULL));
        profile->codepage = ldb_msg_find_attr_as_int(msg, "codepage", 0);
        profile->language = ldb_msg_find_attr_as_int(msg, "language", 0);
        profile->method = ldb_msg_find_attr_as_int(msg, "method", 0);
        profile->exchange_version = ldb_msg_find_attr_as_int(msg, "exchange_version", 0);
-       profile->kerberos = ldb_msg_find_attr_as_string(msg, "kerberos", NULL);
+       profile->kerberos = talloc_steal(profile, ldb_msg_find_attr_as_string(msg, "kerberos", NULL));
+
+       talloc_free(res);
 
        return MAPI_E_SUCCESS;
 }
@@ -687,7 +696,9 @@ _PUBLIC_ enum MAPISTATUS CreateProfileStore(const char *profiledb, const char *l
 
    \return MAPI_E_SUCCESS on success, otherwise MAPI error.
 
-   \note Developers may also call GetLastError() to retrieve the last
+   \note profile must be talloced because it used as talloc subcontext for
+         data members.
+   Developers may also call GetLastError() to retrieve the last
    MAPI error code. Possible MAPI error codes are:
    - MAPI_E_NOT_ENOUGH_RESOURCES: ldb subsystem initialization failed
    - MAPI_E_NOT_FOUND: the profile was not found in the profile
@@ -1022,7 +1033,7 @@ _PUBLIC_ enum MAPISTATUS DuplicateProfile(struct mapi_context *mapi_ctx,
        char                    *username_src = NULL;
        char                    *ProxyAddress = NULL;
        char                    *oldEmailAddress = NULL;
-       struct mapi_profile     profile;
+       struct mapi_profile     *profile;
        char                    **attr_tmp = NULL;
        char                    *tmp = NULL;
        char                    *attr;
@@ -1034,27 +1045,29 @@ _PUBLIC_ enum MAPISTATUS DuplicateProfile(struct mapi_context *mapi_ctx,
        OPENCHANGE_RETVAL_IF(!profile_dst, MAPI_E_INVALID_PARAMETER, NULL);
        OPENCHANGE_RETVAL_IF(!username, MAPI_E_INVALID_PARAMETER, NULL);
 
+       mem_ctx = talloc_named(mapi_ctx->mem_ctx, 0, "DuplicateProfile");
+       profile = talloc(mem_ctx, struct mapi_profile);
+
        /* Step 1. Copy the profile */
        retval = CopyProfile(mapi_ctx, profile_src, profile_dst);
        OPENCHANGE_RETVAL_IF(retval, retval, NULL);
 
        /* retrieve username, EmailAddress and ProxyAddress */
-       retval = OpenProfile(mapi_ctx, &profile, profile_src, NULL);
+       retval = OpenProfile(mapi_ctx, profile, profile_src, NULL);
        OPENCHANGE_RETVAL_IF(retval, MAPI_E_NOT_FOUND, NULL);
 
-       mem_ctx = talloc_named(mapi_ctx->mem_ctx, 0, "DuplicateProfile");
 
-       retval = GetProfileAttr(&profile, "username", &count, &attr_tmp);
+       retval = GetProfileAttr(profile, "username", &count, &attr_tmp);
        OPENCHANGE_RETVAL_IF(retval || !count, MAPI_E_NOT_FOUND, mem_ctx);
        username_src = talloc_strdup(mem_ctx, attr_tmp[0]);
        talloc_free(attr_tmp[0]);
 
-       retval = GetProfileAttr(&profile, "EmailAddress", &count, &attr_tmp);
+       retval = GetProfileAttr(profile, "EmailAddress", &count, &attr_tmp);
        OPENCHANGE_RETVAL_IF(retval, MAPI_E_NOT_FOUND, mem_ctx);
        oldEmailAddress = talloc_strdup(mem_ctx, attr_tmp[0]);
        talloc_free(attr_tmp[0]);
 
-       retval = GetProfileAttr(&profile, "ProxyAddress", &count, &attr_tmp);
+       retval = GetProfileAttr(profile, "ProxyAddress", &count, &attr_tmp);
        OPENCHANGE_RETVAL_IF(retval, MAPI_E_NOT_FOUND, mem_ctx);
        ProxyAddress = talloc_strdup(mem_ctx, attr_tmp[0]);
        talloc_free(attr_tmp[0]);
@@ -1072,7 +1085,7 @@ _PUBLIC_ enum MAPISTATUS DuplicateProfile(struct mapi_context *mapi_ctx,
                char                            *password;
                struct mapi_session     *session = NULL;
 
-               retval = GetProfileAttr(&profile, "password", &count, &attr_tmp);
+               retval = GetProfileAttr(profile, "password", &count, &attr_tmp);
                OPENCHANGE_RETVAL_IF(retval || !count, MAPI_E_NOT_FOUND, mem_ctx);
                password = talloc_strdup(mem_ctx, attr_tmp[0]);
                talloc_free(attr_tmp[0]);
@@ -1159,6 +1172,7 @@ _PUBLIC_ enum MAPISTATUS DuplicateProfile(struct mapi_context *mapi_ctx,
        mapi_profile_modify_string_attr(mapi_ctx, profile_dst, "DisplayName", username);
        mapi_profile_modify_string_attr(mapi_ctx, profile_dst, "Account", username);
 
+       talloc_free(profile);
        talloc_free(mem_ctx);
 
        return MAPI_E_SUCCESS;
index 5c2f5fb45213a2fc6ab8ee41e7e33ff301746e5e..4773a3c25751365404e7661e11c1acc23dc913af 100644 (file)
@@ -402,11 +402,12 @@ static void mapiprofile_dump(struct mapi_context *mapi_ctx, const char *profdb,
 {
        TALLOC_CTX              *mem_ctx;
        enum MAPISTATUS         retval;
-       struct mapi_profile     profile;
+       struct mapi_profile     *profile;
        char                    *profname;
        char                    *exchange_version = NULL;
 
        mem_ctx = talloc_named(mapi_ctx->mem_ctx, 0, "mapiprofile_dump");
+       profile = talloc(mem_ctx, struct mapi_profile);
 
        if (!opt_profname) {
                if ((retval = GetDefaultProfile(mapi_ctx, &profname)) != MAPI_E_SUCCESS) {
@@ -418,7 +419,7 @@ static void mapiprofile_dump(struct mapi_context *mapi_ctx, const char *profdb,
                profname = talloc_strdup(mem_ctx, (const char *)opt_profname);
        }
 
-       retval = OpenProfile(mapi_ctx, &profile, profname, NULL);
+       retval = OpenProfile(mapi_ctx, profile, profname, NULL);
        talloc_free(profname);
 
        if (retval && (retval != MAPI_E_INVALID_PARAMETER)) {
@@ -427,7 +428,7 @@ static void mapiprofile_dump(struct mapi_context *mapi_ctx, const char *profdb,
                exit (1);
        }
 
-       switch (profile.exchange_version) {
+       switch (profile->exchange_version) {
        case 0x0:
                exchange_version = talloc_strdup(mem_ctx, "exchange 2000");
                break;
@@ -442,15 +443,15 @@ static void mapiprofile_dump(struct mapi_context *mapi_ctx, const char *profdb,
                goto end;
        }
 
-       printf("Profile: %s\n", profile.profname);
+       printf("Profile: %s\n", profile->profname);
        printf("\texchange server == %s\n", exchange_version);
-       printf("\tencryption      == %s\n", (profile.seal == true) ? "yes" : "no");
-       printf("\tusername        == %s\n", profile.username);
-       printf("\tpassword        == %s\n", profile.password);
-       printf("\tmailbox         == %s\n", profile.mailbox);
-       printf("\tworkstation     == %s\n", profile.workstation);
-       printf("\tdomain          == %s\n", profile.domain);
-       printf("\tserver          == %s\n", profile.server);
+       printf("\tencryption      == %s\n", (profile->seal == true) ? "yes" : "no");
+       printf("\tusername        == %s\n", profile->username);
+       printf("\tpassword        == %s\n", profile->password);
+       printf("\tmailbox         == %s\n", profile->mailbox);
+       printf("\tworkstation     == %s\n", profile->workstation);
+       printf("\tdomain          == %s\n", profile->domain);
+       printf("\tserver          == %s\n", profile->server);
 
 end:
        talloc_free(mem_ctx);
@@ -461,13 +462,14 @@ static void mapiprofile_attribute(struct mapi_context *mapi_ctx, const char *pro
 {
        TALLOC_CTX              *mem_ctx;
        enum MAPISTATUS         retval;
-       struct mapi_profile     profile;
+       struct mapi_profile     *profile;
        char                    *profname = NULL;
        char                    **value = NULL;
        unsigned int            count = 0;
        unsigned int            i;
 
        mem_ctx = talloc_named(mapi_ctx->mem_ctx, 0, "mapiprofile_attribute");
+       profile = talloc(mem_ctx, struct mapi_profile);
 
        if (!opt_profname) {
                if ((retval = GetDefaultProfile(mapi_ctx, &profname)) != MAPI_E_SUCCESS) {
@@ -478,7 +480,7 @@ static void mapiprofile_attribute(struct mapi_context *mapi_ctx, const char *pro
                profname = talloc_strdup(mem_ctx, (const char *)opt_profname);
        }
 
-       retval = OpenProfile(mapi_ctx, &profile, profname, NULL);
+       retval = OpenProfile(mapi_ctx, profile, profname, NULL);
        if (retval && (retval != MAPI_E_INVALID_PARAMETER)) {
                mapi_errstr("OpenProfile", retval);
                talloc_free(profname);
@@ -486,7 +488,7 @@ static void mapiprofile_attribute(struct mapi_context *mapi_ctx, const char *pro
                exit (1);
        }
 
-       if ((retval = GetProfileAttr(&profile, attribute, &count, &value))) {
+       if ((retval = GetProfileAttr(profile, attribute, &count, &value))) {
                mapi_errstr("ProfileGetAttr", retval);
                talloc_free(profname);
                talloc_free(mem_ctx);