Blame SOURCES/0087-RHBZ-1110013-config-error-checking.patch

f20720
---
f20720
 libmultipath/parser.c |  154 ++++++++++++++++++++++++++++++++++++++++----------
f20720
 1 file changed, 126 insertions(+), 28 deletions(-)
f20720
f20720
Index: multipath-tools-130222/libmultipath/parser.c
f20720
===================================================================
f20720
--- multipath-tools-130222.orig/libmultipath/parser.c
f20720
+++ multipath-tools-130222/libmultipath/parser.c
f20720
@@ -395,36 +395,57 @@ set_value(vector strvec)
f20720
 	char *alloc = NULL;
f20720
 	char *tmp;
f20720
 
f20720
-	if (!str)
f20720
+	if (!str) {
f20720
+		condlog(0, "option '%s' missing value",
f20720
+			(char *)VECTOR_SLOT(strvec, 0));
f20720
 		return NULL;
f20720
-
f20720
+	}
f20720
 	size = strlen(str);
f20720
-	if (size == 0)
f20720
+	if (size == 0) {
f20720
+		condlog(0, "option '%s' has empty value",
f20720
+			(char *)VECTOR_SLOT(strvec, 0));
f20720
 		return NULL;
f20720
-
f20720
-	if (*str == '"') {
f20720
-		for (i = 2; i < VECTOR_SIZE(strvec); i++) {
f20720
-			str = VECTOR_SLOT(strvec, i);
f20720
-			len += strlen(str);
f20720
-			if (!alloc)
f20720
-				alloc =
f20720
-				    (char *) MALLOC(sizeof (char *) *
f20720
-						    (len + 1));
f20720
-			else {
f20720
-				alloc =
f20720
-				    REALLOC(alloc, sizeof (char *) * (len + 1));
f20720
-				tmp = VECTOR_SLOT(strvec, i-1);
f20720
-				if (alloc && *str != '"' && *tmp != '"')
f20720
-					strncat(alloc, " ", 1);
f20720
-			}
f20720
-
f20720
-			if (alloc && i != VECTOR_SIZE(strvec)-1)
f20720
-				strncat(alloc, str, strlen(str));
f20720
-		}
f20720
-	} else {
f20720
-		alloc = MALLOC(sizeof (char *) * (size + 1));
f20720
+	}
f20720
+	if (*str != '"') {
f20720
+		alloc = MALLOC(sizeof (char) * (size + 1));
f20720
 		if (alloc)
f20720
 			memcpy(alloc, str, size);
f20720
+		else
f20720
+			condlog(0, "can't allocate memeory for option '%s'",
f20720
+				(char *)VECTOR_SLOT(strvec, 0));
f20720
+		return alloc;
f20720
+	}
f20720
+	/* Even empty quotes counts as a value (An empty string) */
f20720
+	alloc = (char *) MALLOC(sizeof (char));
f20720
+	if (!alloc) {
f20720
+		condlog(0, "can't allocate memeory for option '%s'",
f20720
+			(char *)VECTOR_SLOT(strvec, 0));
f20720
+		return NULL;
f20720
+	}
f20720
+	for (i = 2; i < VECTOR_SIZE(strvec); i++) {
f20720
+		str = VECTOR_SLOT(strvec, i);
f20720
+		if (!str) {
f20720
+			free(alloc);
f20720
+			condlog(0, "parse error for option '%s'",
f20720
+				(char *)VECTOR_SLOT(strvec, 0));
f20720
+			return NULL;
f20720
+		}
f20720
+		if (*str == '"')
f20720
+			break;
f20720
+		tmp = alloc;
f20720
+		/* The first +1 is for the NULL byte. The rest are for the
f20720
+		 * spaces between words */
f20720
+		len += strlen(str) + 1;
f20720
+		alloc = REALLOC(alloc, sizeof (char) * len);
f20720
+		if (!alloc) {
f20720
+			FREE(tmp);
f20720
+			condlog(0, "can't allocate memeory for option '%s'",
f20720
+				(char *)VECTOR_SLOT(strvec, 0));
f20720
+			return NULL;
f20720
+		}
f20720
+		if (*alloc != '\0')
f20720
+			strncat(alloc, " ", 1);
f20720
+		strncat(alloc, str, strlen(str));
f20720
 	}
f20720
 	return alloc;
f20720
 }
f20720
@@ -465,6 +486,74 @@ void free_uniques(vector uniques)
f20720
 }
f20720
 
f20720
 int
f20720
+is_sublevel_keyword(char *str)
f20720
+{
f20720
+	return (strcmp(str, "defaults") == 0 || strcmp(str, "blacklist") == 0 ||
f20720
+		strcmp(str, "blacklist_exceptions") == 0 ||
f20720
+		strcmp(str, "devices") == 0 || strcmp(str, "devices") == 0 ||
f20720
+		strcmp(str, "device") == 0 || strcmp(str, "multipaths") == 0 ||
f20720
+		strcmp(str, "multipath") == 0);
f20720
+}
f20720
+
f20720
+int
f20720
+validate_config_strvec(vector strvec)
f20720
+{
f20720
+	char *str;
f20720
+	int i;
f20720
+
f20720
+	str = VECTOR_SLOT(strvec, 0);
f20720
+	if (str == NULL) {
f20720
+		condlog(0, "can't parse option on line %d of config file",
f20720
+			line_nr);
f20720
+	return -1;
f20720
+	}
f20720
+	if (*str == '}') {
f20720
+		if (VECTOR_SIZE(strvec) > 1)
f20720
+			condlog(0, "ignoring extra data starting with '%s' on line %d of config file", (char *)VECTOR_SLOT(strvec, 1), line_nr);
f20720
+		return 0;
f20720
+	}
f20720
+	if (*str == '{') {
f20720
+		condlog(0, "invalid keyword '%s' on line %d of config file", str, line_nr);
f20720
+		return -1;
f20720
+	}
f20720
+	if (is_sublevel_keyword(str)) {
f20720
+		str = VECTOR_SLOT(strvec, 1);
f20720
+		if (str == NULL)
f20720
+			condlog(0, "missing '{' on line %d of config file", line_nr);
f20720
+		else if (*str != '{')
f20720
+			condlog(0, "expecting '{' on line %d of config file. found '%s'", line_nr, str);
f20720
+		else if (VECTOR_SIZE(strvec) > 2)
f20720
+			condlog(0, "ignoring extra data starting with '%s' on line %d of config file", (char *)VECTOR_SLOT(strvec, 2), line_nr);
f20720
+		return 0;
f20720
+	}
f20720
+	str = VECTOR_SLOT(strvec, 1);
f20720
+	if (str == NULL) {
f20720
+		condlog(0, "missing value for option '%s' on line %d of config file", (char *)VECTOR_SLOT(strvec, 0), line_nr);
f20720
+		return -1;
f20720
+	}
f20720
+	if (*str != '"') {
f20720
+		if (VECTOR_SIZE(strvec) > 2)
f20720
+			condlog(0, "ignoring extra data starting with '%s' on line %d of config file", (char *)VECTOR_SLOT(strvec, 2), line_nr);
f20720
+		return 0;
f20720
+	}
f20720
+	for (i = 2; i < VECTOR_SIZE(strvec); i++) {
f20720
+		str = VECTOR_SLOT(strvec, i);
f20720
+		if (str == NULL) {
f20720
+			condlog(0, "can't parse value on line %d of config file", line_nr);
f20720
+			return -1;
f20720
+		}
f20720
+		if (*str == '"') {
f20720
+			if (VECTOR_SIZE(strvec) > i + 1)
f20720
+				condlog(0, "ignoring extra data starting with '%s' on line %d of config file", (char *)VECTOR_SLOT(strvec, (i + 1)), line_nr);
f20720
+			return 0;
f20720
+		}
f20720
+	}
f20720
+	condlog(0, "missing closing quotes on line %d of config file",
f20720
+		line_nr);
f20720
+	return 0;
f20720
+}
f20720
+
f20720
+int
f20720
 process_stream(vector keywords)
f20720
 {
f20720
 	int i;
f20720
@@ -494,11 +583,20 @@ process_stream(vector keywords)
f20720
 		if (!strvec)
f20720
 			continue;
f20720
 
f20720
+		if (validate_config_strvec(strvec) != 0) {
f20720
+			free_strvec(strvec);
f20720
+			continue;
f20720
+		}
f20720
+
f20720
 		str = VECTOR_SLOT(strvec, 0);
f20720
 
f20720
-		if (!strcmp(str, EOB) && kw_level > 0) {
f20720
-			free_strvec(strvec);
f20720
-			break;
f20720
+		if (!strcmp(str, EOB)) {
f20720
+			if (kw_level > 0) {
f20720
+				free_strvec(strvec);
f20720
+				break;
f20720
+			}
f20720
+			condlog(0, "unmatched '%s' at line %d of config file",
f20720
+				EOB, line_nr);
f20720
 		}
f20720
 
f20720
 		for (i = 0; i < VECTOR_SIZE(keywords); i++) {