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

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