Assorted small fixes and leak plugs
authorJakub Hrozek <jakub.hrozek@posteo.se>
Wed, 28 Oct 2015 13:27:15 +0000 (14:27 +0100)
committerAndreas Schneider <asn@samba.org>
Thu, 10 Dec 2015 12:31:19 +0000 (13:31 +0100)
src/libpamtest.c
src/pam_wrapper.c
tests/test_pam_wrapper.c

index 20a2b28c704be65649a7924e4ad1f0594fba338f..771786cf81a2d50dda2dee306a9da5f4d9101994 100644 (file)
@@ -186,10 +186,12 @@ static int pamtest_simple_conv(int num_msg,
                return PAM_CONV_ERR;
        }
 
-       reply = (struct pam_response *) calloc(num_msg,
-                                              sizeof(struct pam_response));
-       if (reply == NULL) {
-               return PAM_CONV_ERR;
+       if (response) {
+               reply = (struct pam_response *) calloc(num_msg,
+                                               sizeof(struct pam_response));
+               if (reply == NULL) {
+                       return PAM_CONV_ERR;
+               }
        }
 
        for (i=0; i < num_msg; i++) {
@@ -201,10 +203,12 @@ static int pamtest_simple_conv(int num_msg,
                                return PAM_CONV_ERR;
                        }
 
-                       ret = add_to_reply(&reply[i], prompt);
-                       if (ret != PAM_SUCCESS) {
-                               /* FIXME - free data? */
-                               return ret;
+                       if (reply != NULL) {
+                               ret = add_to_reply(&reply[i], prompt);
+                               if (ret != PAM_SUCCESS) {
+                                       /* FIXME - free data? */
+                                       return ret;
+                               }
                        }
 
                        cctx->echo_off_idx++;
@@ -216,10 +220,12 @@ static int pamtest_simple_conv(int num_msg,
                                return PAM_CONV_ERR;
                        }
 
-                       ret = add_to_reply(&reply[i], prompt);
-                       if (ret != PAM_SUCCESS) {
-                               /* FIXME - free data? */
-                               return ret;
+                       if (reply != NULL) {
+                               ret = add_to_reply(&reply[i], prompt);
+                               if (ret != PAM_SUCCESS) {
+                                       /* FIXME - free data? */
+                                       return ret;
+                               }
                        }
 
                        cctx->echo_on_idx++;
index bc3116544ff39d15813b3571808ab34a051410be..abe6bbf4fbd633baaa28ecf06352e76c9f10dee3 100644 (file)
@@ -282,10 +282,10 @@ static void *pwrap_load_lib_handle(enum pwrap_lib lib)
 
                        handle = dlopen(libpam_path, flags);
                        if (handle != NULL) {
+                               pwrap.libpam.handle = handle;
                                break;
-               }
+                       }
 
-                       pwrap.libpam.handle = handle;
                }
                break;
        }
@@ -983,53 +983,53 @@ static int pwrap_pam_get_item(const pam_handle_t *pamh,
                switch(item_type) {
                case PAM_USER:
                        PWRAP_LOG(PWRAP_LOG_TRACE,
-                                 "pwrap_set_item PAM_USER=%s",
-                                 (char *)item);
+                                 "pwrap_get_item PAM_USER=%s",
+                                 (char *) *item);
                        break;
                case PAM_SERVICE:
                        PWRAP_LOG(PWRAP_LOG_TRACE,
-                                 "pwrap_set_item PAM_SERVICE=%s",
-                                 (char *)item);
+                                 "pwrap_get_item PAM_SERVICE=%s",
+                                 (char *) *item);
                        break;
                case PAM_USER_PROMPT:
                        PWRAP_LOG(PWRAP_LOG_TRACE,
-                                 "pwrap_set_item PAM_USER_PROMPT=%s",
-                                 (char *)item);
+                                 "pwrap_get_item PAM_USER_PROMPT=%s",
+                                 (char *) *item);
                        break;
                case PAM_TTY:
                        PWRAP_LOG(PWRAP_LOG_TRACE,
-                                 "pwrap_set_item PAM_TTY=%s",
-                                 (char *)item);
+                                 "pwrap_get_item PAM_TTY=%s",
+                                 (char *) *item);
                        break;
                case PAM_RUSER:
                        PWRAP_LOG(PWRAP_LOG_TRACE,
-                                 "pwrap_set_item PAM_RUSER=%s",
-                                 (char *)item);
+                                 "pwrap_get_item PAM_RUSER=%s",
+                                 (char *) *item);
                        break;
                case PAM_RHOST:
                        PWRAP_LOG(PWRAP_LOG_TRACE,
-                                 "pwrap_set_item PAM_RHOST=%s",
-                                 (char *)item);
+                                 "pwrap_get_item PAM_RHOST=%s",
+                                 (char *) *item);
                        break;
                case PAM_AUTHTOK:
                        PWRAP_LOG(PWRAP_LOG_TRACE,
-                                 "pwrap_set_item PAM_AUTHTOK=%s",
-                                 (char *)item);
+                                 "pwrap_get_item PAM_AUTHTOK=%s",
+                                 (char *) *item);
                        break;
                case PAM_OLDAUTHTOK:
                        PWRAP_LOG(PWRAP_LOG_TRACE,
-                                 "pwrap_set_item PAM_OLDAUTHTOK=%s",
-                                 (char *)item);
+                                 "pwrap_get_item PAM_OLDAUTHTOK=%s",
+                                 (char *) *item);
                        break;
                case PAM_CONV:
                        PWRAP_LOG(PWRAP_LOG_TRACE,
-                                 "pwrap_set_item PAM_CONV=%p",
-                                 (void *) item);
+                                 "pwrap_get_item PAM_CONV=%p",
+                                 (void *) *item);
                        break;
                default:
                        PWRAP_LOG(PWRAP_LOG_TRACE,
-                                 "pwrap_set_item item_type=%d item=%p",
-                                 item_type, (void *) item);
+                                 "pwrap_get_item item_type=%d item=%p",
+                                 item_type, (void *) *item);
                        break;
                }
        } else {
@@ -1246,8 +1246,6 @@ static int p_rmdirs(const char *path) {
        struct stat sb;
        char *fname;
 
-       return 0;
-
        if ((d = opendir(path)) != NULL) {
                while(stat(path, &sb) == 0) {
                        /* if we can remove the directory we're done */
@@ -1316,10 +1314,17 @@ void pwrap_destructor(void)
 {
        const char *env;
 
+       PWRAP_LOG(PWRAP_LOG_TRACE, "entering pwrap_destructor");
+
        if (pwrap.libpam.handle != NULL) {
                dlclose(pwrap.libpam.handle);
        }
 
+       if (pwrap.pam_library != NULL) {
+               free(pwrap.pam_library);
+               pwrap.pam_library = NULL;
+       }
+
        if (!pwrap.initialised) {
                return;
        }
@@ -1331,4 +1336,9 @@ void pwrap_destructor(void)
        if (env == NULL || env[0] != '1') {
                p_rmdirs(pwrap.config_dir);
        }
+
+       if (pwrap.config_dir != NULL) {
+               free(pwrap.config_dir);
+               pwrap.config_dir = NULL;
+       }
 }
index 1315e1d1a47e32c312ee395c7200a24a40e04c92..2d5c01735cb4d0550a3a9c960793ff7823016e2d 100644 (file)
@@ -695,12 +695,8 @@ static void test_pam_authenticate_db_opt_err(void **state)
 static void test_pam_vsyslog(void **state)
 {
        struct pwrap_test_ctx *test_ctx;
-       int rv;
 
        test_ctx = (struct pwrap_test_ctx *) *state;
-       rv = pam_start("matrix", "trinity",
-                      &test_ctx->conv, &test_ctx->ph);
-       assert_int_equal(rv, PAM_SUCCESS);
 
        pam_syslog(test_ctx->ph, LOG_INFO, "This is pam_wrapper test\n");
 }
@@ -755,6 +751,8 @@ static void test_get_set(void **state)
        test_getenv(tests[1].case_out.envlist, "PAM_OLDAUTHTOK");
        test_getenv(tests[1].case_out.envlist, "PAM_XDISPLAY");
        test_getenv(tests[1].case_out.envlist, "PAM_AUTHTOK_TYPE");
+
+       pamtest_free_env(tests[1].case_out.envlist);
 }
 
 static void test_libpamtest_keepopen(void **state)
@@ -818,7 +816,7 @@ int main(void) {
        const struct CMUnitTest init_tests[] = {
                cmocka_unit_test(test_env),
                cmocka_unit_test_setup_teardown(test_pam_start,
-                                               setup_noconv,
+                                               setup_ctx_only,
                                                teardown_simple),
                cmocka_unit_test_setup_teardown(test_pam_authenticate,
                                                setup_passdb,