[asterisk-commits] gtjoseph: branch 1.8 r422900 - /branches/1.8/main/config.c

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Sep 10 10:58:53 CDT 2014


Author: gtjoseph
Date: Wed Sep 10 10:58:45 2014
New Revision: 422900

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=422900
Log:
config: bug: fix truncation of included config files on permissions error

ast_config_text_file_save() currently truncates include files as they
are processed.  If a subsequent include file or the main config file has
a permissions error that prevents writing, earlier include files are left
truncated resulting in a frantic search for backups.

This patch causes ast_config_text_file_save to check for write access
on all files before it truncates any of them.

Will be applied 1.8 > trunk.

Tested by: George Joseph
Review: https://reviewboard.asterisk.org/r/3986/

Modified:
    branches/1.8/main/config.c

Modified: branches/1.8/main/config.c
URL: http://svnview.digium.com/svn/asterisk/branches/1.8/main/config.c?view=diff&rev=422900&r1=422899&r2=422900
==============================================================================
--- branches/1.8/main/config.c (original)
+++ branches/1.8/main/config.c Wed Sep 10 10:58:45 2014
@@ -1859,21 +1859,27 @@
 	ast_free(o->fname);
 }
 
-
-static struct inclfile *set_fn(char *fn, int fn_size, const char *file, const char *configfile, struct ao2_container *fileset)
+static void make_fn(char *fn, size_t fn_size, const char *file, const char *configfile)
+{
+	if (ast_strlen_zero(file)) {
+		if (configfile[0] == '/') {
+			ast_copy_string(fn, configfile, fn_size);
+		} else {
+			snprintf(fn, fn_size, "%s/%s", ast_config_AST_CONFIG_DIR, configfile);
+		}
+	} else if (file[0] == '/') {
+		ast_copy_string(fn, file, fn_size);
+	} else {
+		snprintf(fn, fn_size, "%s/%s", ast_config_AST_CONFIG_DIR, file);
+	}
+}
+
+static struct inclfile *set_fn(char *fn, size_t fn_size, const char *file, const char *configfile, struct ao2_container *fileset)
 {
 	struct inclfile lookup;
 	struct inclfile *fi;
 
-	if (ast_strlen_zero(file)) {
-		if (configfile[0] == '/')
-			ast_copy_string(fn, configfile, fn_size);
-		else
-			snprintf(fn, fn_size, "%s/%s", ast_config_AST_CONFIG_DIR, configfile);
-	} else if (file[0] == '/')
-		ast_copy_string(fn, file, fn_size);
-	else
-		snprintf(fn, fn_size, "%s/%s", ast_config_AST_CONFIG_DIR, file);
+	make_fn(fn, fn_size, file, configfile);
 	lookup.fname = fn;
 	fi = ao2_find(fileset, &lookup, OBJ_POINTER);
 	if (fi) {
@@ -1982,10 +1988,28 @@
 		return -1;
 	}
 
-	/* reset all the output flags, in case this isn't our first time saving this data */
+	/* Check all the files for write access before attempting to modify any of them */
 	for (incl = cfg->includes; incl; incl = incl->next) {
+		/* reset all the output flags in case this isn't our first time saving this data */
 		incl->output = 0;
-	}
+		/* now make sure we have write access */
+		if (!incl->exec) {
+			make_fn(fn, sizeof(fn), incl->included_file, configfile);
+			if (access(fn, R_OK | W_OK)) {
+				ast_log(LOG_ERROR, "Unable to write %s (%s)\n", fn, strerror(errno));
+				return -1;
+			}
+		}
+	}
+
+	/* now make sure we have write access to the main config file */
+	make_fn(fn, sizeof(fn), 0, configfile);
+	if (access(fn, R_OK | W_OK)) {
+		ast_log(LOG_ERROR, "Unable to write %s (%s)\n", fn, strerror(errno));
+		return -1;
+	}
+
+	/* Now that we know we have write access to all files, it's safe to start truncating them */
 
 	/* go thru all the inclusions and make sure all the files involved (configfile plus all its inclusions)
 	   are all truncated to zero bytes and have that nice header*/
@@ -1998,8 +2022,7 @@
 				gen_header(f, configfile, fn, generator);
 				fclose(f); /* this should zero out the file */
 			} else {
-				ast_debug(1, "Unable to open for writing: %s\n", fn);
-				ast_verb(2, "Unable to write %s (%s)", fn, strerror(errno));
+				ast_log(LOG_ERROR, "Unable to write %s (%s)\n", fn, strerror(errno));
 			}
 			if (fi) {
 				ao2_ref(fi, -1);
@@ -2032,8 +2055,7 @@
 			fi = set_fn(fn, sizeof(fn), cat->file, configfile, fileset);
 			f = fopen(fn, "a");
 			if (!f) {
-				ast_debug(1, "Unable to open for writing: %s\n", fn);
-				ast_verb(2, "Unable to write %s (%s)", fn, strerror(errno));
+				ast_log(LOG_ERROR, "Unable to write %s (%s)\n", fn, strerror(errno));
 				if (fi) {
 					ao2_ref(fi, -1);
 				}




More information about the asterisk-commits mailing list