diff options
author | Thomas Guyot-Sionnest <dermoth@users.sourceforge.net> | 2008-08-21 04:17:35 +0000 |
---|---|---|
committer | Thomas Guyot-Sionnest <dermoth@users.sourceforge.net> | 2008-08-21 04:17:35 +0000 |
commit | 49bc8237c09e3fc0963ec18cedc549cb5c7373df (patch) | |
tree | 635b00bfff38862df8b975f94121ee8bc87a698b /plugins | |
parent | 77362a18bac7e73a4f824736212acb7fd6f7dd1e (diff) | |
download | monitoring-plugins-49bc8237c09e3fc0963ec18cedc549cb5c7373df.tar.gz |
Complete rewrite of the extract_value function
The original one was flawed (easy to trigger segfaults) and did not allow some whitespaces as permitted by rfc1305. This one has been troughfully tested with tap (the testing code might get included later)
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@2036 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/check_ntp_peer.c | 59 |
1 files changed, 46 insertions, 13 deletions
diff --git a/plugins/check_ntp_peer.c b/plugins/check_ntp_peer.c index 67ee0a77..73ba8990 100644 --- a/plugins/check_ntp_peer.c +++ b/plugins/check_ntp_peer.c | |||
@@ -175,23 +175,56 @@ void print_ntp_control_message(const ntp_control_message *p){ | |||
175 | } | 175 | } |
176 | } | 176 | } |
177 | 177 | ||
178 | /* | ||
179 | * Extract the value from NTP key/value pairs, or return NULL. | ||
180 | * The value returned can be free()ed. | ||
181 | */ | ||
178 | char *extract_value(const char *varlist, const char *name){ | 182 | char *extract_value(const char *varlist, const char *name){ |
179 | char *tmpvarlist=NULL, *tmpkey=NULL, *value=NULL; | 183 | char *tmp=NULL, *value=NULL; |
180 | int last=0; | 184 | int i; |
181 | |||
182 | /* The following code require a non-empty varlist */ | ||
183 | if(strlen(varlist) == 0) | ||
184 | return NULL; | ||
185 | 185 | ||
186 | tmpvarlist = strdup(varlist); | 186 | /* Strip any leading space */ |
187 | tmpkey = strtok(tmpvarlist, "="); | ||
188 | 187 | ||
189 | do { | 188 | while (1) { |
190 | if(strstr(tmpkey, name) != NULL) { | 189 | for (varlist; isspace(varlist[0]); varlist++); |
191 | value = strtok(NULL, ","); | 190 | if (strncmp(name, varlist, strlen(name)) == 0) { |
192 | last = 1; | 191 | varlist += strlen(name); |
192 | /* strip trailing spaces */ | ||
193 | for (varlist; isspace(varlist[0]); varlist++); | ||
194 | |||
195 | if (varlist[0] == '=') { | ||
196 | /* We matched the key, go past the = sign */ | ||
197 | varlist++; | ||
198 | /* strip leading spaces */ | ||
199 | for (varlist; isspace(varlist[0]); varlist++); | ||
200 | |||
201 | if (tmp = index(varlist, ',')) { | ||
202 | /* Value is delimited by a comma */ | ||
203 | if (tmp-varlist == 0) continue; | ||
204 | value = (char *)malloc(tmp-varlist+1); | ||
205 | strncpy(value, varlist, tmp-varlist); | ||
206 | value[tmp-varlist] = '\0'; | ||
207 | } else { | ||
208 | /* Value is delimited by a \0 */ | ||
209 | if (strlen(varlist) == 0) continue; | ||
210 | value = (char *)malloc(strlen(varlist) + 1); | ||
211 | strncpy(value, varlist, strlen(varlist)); | ||
212 | value[strlen(varlist)] = '\0'; | ||
213 | } | ||
214 | break; | ||
215 | } | ||
216 | } | ||
217 | if (tmp = index(varlist, ',')) { | ||
218 | /* More keys, keep going... */ | ||
219 | varlist = tmp + 1; | ||
220 | } else { | ||
221 | /* We're done */ | ||
222 | break; | ||
193 | } | 223 | } |
194 | } while (last == 0 && (tmpkey = strtok(NULL, "="))); | 224 | } |
225 | |||
226 | /* Clean-up trailing spaces/newlines */ | ||
227 | if (value) for (i=strlen(value)-1; isspace(value[i]); i--) value[i] = '\0'; | ||
195 | 228 | ||
196 | return value; | 229 | return value; |
197 | } | 230 | } |