From 73635bb1b32450a86c78866ed8c485cc1ce3a1de Mon Sep 17 00:00:00 2001 From: Sumit Bose Date: Mon, 8 Dec 2008 09:19:13 +0100 Subject: some memory handling fixes --- worker/ipaaction.c | 68 ++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 46 insertions(+), 22 deletions(-) (limited to 'worker/ipaaction.c') diff --git a/worker/ipaaction.c b/worker/ipaaction.c index de01d94..f175a49 100644 --- a/worker/ipaaction.c +++ b/worker/ipaaction.c @@ -77,7 +77,6 @@ int check_ipaaction_condition(const xmlDocPtr doc, const xmlChar *default_namesp ret=exec_command(condition, user, group, arguments, NULL); - free(arguments); free(group); free(user); free(condition); @@ -86,15 +85,15 @@ int check_ipaaction_condition(const xmlDocPtr doc, const xmlChar *default_namesp } int ipaaction_file(const xmlDocPtr doc, const xmlChar *default_namespace) { - char *url; - char *data; - char *path; - char *owner; - char *group; - char *access; - char *selinux_context; + char *url=NULL; + char *data=NULL; + char *path=NULL; + char *owner=NULL; + char *group=NULL; + char *access=NULL; + char *selinux_context=NULL; //char **acl; - char *cleanup; + char *cleanup=NULL; CURL *curl_context; CURLcode curl_result; char *tmp_file_name; @@ -113,14 +112,14 @@ int ipaaction_file(const xmlDocPtr doc, const xmlChar *default_namespace) { } if (url!=NULL && data!=NULL) { DEBUG(0,("Only url or data element are allowed for ipaaction file, not both. This should never happen.\n")); - return -1; + goto failed; } path = find_value(doc, XPATH_IPAACTION_FILE_PATH, NULL); - CHECK(path, NULL, ("Path for ipaaction file not found.\n"), return -1); + CHECK(path, NULL, ("Path for ipaaction file not found.\n"), goto failed); DEBUG(3, ("Found path for ipaaction file: %s\n", path)); ret=stat(path, &stat_buffer); - CHECK(ret, 0, ("Destination file %s alread exists.\n", path), return -1); + CHECK(ret, 0, ("Destination file %s alread exists.\n", path), goto failed); owner = find_value(doc, XPATH_IPAACTION_FILE_OWNER, "root"); DEBUG(3, ("Found owner for ipaaction file: %s\n", owner)); @@ -139,16 +138,16 @@ int ipaaction_file(const xmlDocPtr doc, const xmlChar *default_namespace) { tmp_file_name=(char *) malloc(strlen(path)+7); - CHECK(tmp_file_name,NULL, ("malloc failed."), return -1); + CHECK(tmp_file_name,NULL, ("malloc failed."), goto failed); strcpy(tmp_file_name, path); strcat(tmp_file_name, ".XXXXXX"); fd=open_temporary_file(tmp_file_name, access, owner, group, selinux_context); - CHECK(fd, -1, ("Failed to open temporary file.\n"), return -1); + CHECK(fd, -1, ("Failed to open temporary file.\n"), goto failed); output_file=fdopen(fd,"w"); - CHECK(output_file, NULL, ("fdopen failed: %s\n", strerror(errno)), return -1); + CHECK(output_file, NULL, ("fdopen failed: %s\n", strerror(errno)), goto failed); if (url!=NULL) { curl_context=curl_easy_init(); - CHECK(curl_context, NULL, ("curl_easy_init failed.\n"), return -1); + CHECK(curl_context, NULL, ("curl_easy_init failed.\n"), goto failed); curl_result=curl_easy_setopt(curl_context, CURLOPT_URL, url); DEBUG(3,("curl result: %d\n",curl_result)); curl_result=curl_easy_setopt(curl_context, CURLOPT_WRITEDATA, output_file); @@ -162,10 +161,30 @@ int ipaaction_file(const xmlDocPtr doc, const xmlChar *default_namespace) { fclose(output_file); /* this should close fd, too */ ret=rename(tmp_file_name, path); - CHECK_MINUS_ONE_RETURN(ret, ("Cannot rename %s to %s: %s\n", tmp_file_name, path, strerror(errno) )); - free(tmp_file_name); + CHECK(ret, -1, ("Cannot rename %s to %s: %s\n", tmp_file_name, path, strerror(errno) ), goto failed); + free(tmp_file_name); + free(url); + free(data); + free(path); + free(owner); + free(group); + free(access); + free(selinux_context); + free(cleanup); return 0; + +failed: + free(tmp_file_name); + free(url); + free(data); + free(path); + free(owner); + free(group); + free(access); + free(selinux_context); + free(cleanup); + return -1; } int ipaaction_run(const xmlDocPtr doc, const xmlChar *default_namespace) { @@ -194,7 +213,6 @@ int ipaaction_run(const xmlDocPtr doc, const xmlChar *default_namespace) { ret=exec_command(command, user, group, arguments, NULL); - free(arguments); free(group); free(user); free(command); @@ -214,22 +232,28 @@ int handle_ipaaction(const char *policy_name, const xmlChar *default_namespace) ret=check_ipaaction_condition(doc, default_namespace); if (ret!=0) { DEBUG(0,("IPA action condition failed\n")); - return -1; + goto failed; } ret=ipaaction_file(doc, default_namespace); if (ret!=0) { DEBUG(0,("IPA action file failed\n")); - return -1; + goto failed; } ret=ipaaction_run(doc, default_namespace); if (ret!=0) { DEBUG(0,("IPA action run failed\n")); - return -1; + goto failed; } xmlFreeDoc(doc); + xmlCleanupParser(); return 0; + +failed: + xmlFreeDoc(doc); + xmlCleanupParser(); + return -1; } -- cgit