summaryrefslogtreecommitdiffstats
path: root/0087-RHBZ-1110013-config-error-checking.patch
blob: c774bc5f0d6dc3f000312f7e3511b13aa511750c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
---
 libmultipath/parser.c |  154 ++++++++++++++++++++++++++++++++++++++++----------
 1 file changed, 126 insertions(+), 28 deletions(-)

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