index 72cec63923995dcfe89ad3381205550c44384914..e8325bc82392c0d39e55ca4fe1b7102aff741512 100644 (file)
--- a/plugins/check_ntp_peer.c
+++ b/plugins/check_ntp_peer.c
* Copyright (c) 2006 Sean Finney <seanius@seanius.net>
* Copyright (c) 2006-2008 Nagios Plugins Development Team
*
-* Last Modified: $Date$
-*
* Description:
*
* This file contains the check_ntp_peer plugin
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
-* $Id$
*
*****************************************************************************/
const char *progname = "check_ntp_peer";
-const char *revision = "$Revision$";
const char *copyright = "2006-2008";
const char *email = "nagiosplug-devel@lists.sourceforge.net";
/* NTP control message header is 12 bytes, plus any data in the data
* field, plus null padding to the nearest 32-bit boundary per rfc.
*/
-#define SIZEOF_NTPCM(m) (12+ntohs(m.count)+((m.count)?4-(ntohs(m.count)%4):0))
+#define SIZEOF_NTPCM(m) (12+ntohs(m.count)+((ntohs(m.count)%4)?4-(ntohs(m.count)%4):0))
/* finally, a little helper or two for debugging: */
#define DBG(x) do{if(verbose>1){ x; }}while(0);
if(p->op&REM_RESP && p->op&OP_READSTAT){
peer=(ntp_assoc_status_pair*)p->data;
for(i=0;i<numpeers;i++){
- printf("\tpeer id %.2x status %.2x",
+ printf("\tpeer id %.2x status %.2x",
ntohs(peer[i].assoc), ntohs(peer[i].status));
if (PEER_SEL(peer[i].status) >= PEER_INCLUDED){
if(PEER_SEL(peer[i].status) >= PEER_SYNCSOURCE){
}
}
-/*
- * Extract the value from NTP key/value pairs, or return NULL.
- * The value returned can be free()ed.
- */
-char *extract_value(const char *varlist, const char *name){
- char *tmp=NULL, *value=NULL;
- int i;
-
- while (1) {
- /* Strip any leading space */
- for (varlist; isspace(varlist[0]); varlist++);
-
- if (strncmp(name, varlist, strlen(name)) == 0) {
- varlist += strlen(name);
- /* strip trailing spaces */
- for (varlist; isspace(varlist[0]); varlist++);
-
- if (varlist[0] == '=') {
- /* We matched the key, go past the = sign */
- varlist++;
- /* strip leading spaces */
- for (varlist; isspace(varlist[0]); varlist++);
-
- if (tmp = index(varlist, ',')) {
- /* Value is delimited by a comma */
- if (tmp-varlist == 0) continue;
- value = (char *)malloc(tmp-varlist+1);
- strncpy(value, varlist, tmp-varlist);
- value[tmp-varlist] = '\0';
- } else {
- /* Value is delimited by a \0 */
- if (strlen(varlist) == 0) continue;
- value = (char *)malloc(strlen(varlist) + 1);
- strncpy(value, varlist, strlen(varlist));
- value[strlen(varlist)] = '\0';
- }
- break;
- }
- }
- if (tmp = index(varlist, ',')) {
- /* More keys, keep going... */
- varlist = tmp + 1;
- } else {
- /* We're done */
- break;
- }
- }
-
- /* Clean-up trailing spaces/newlines */
- if (value) for (i=strlen(value)-1; isspace(value[i]); i--) value[i] = '\0';
-
- return value;
-}
-
void
setup_control_request(ntp_control_message *p, uint8_t opcode, uint16_t seq){
memset(p, 0, sizeof(ntp_control_message));
@@ -299,6 +241,9 @@ int ntp_request(const char *host, double *offset, int *offset_result, double *ji
if(read(conn, &req, SIZEOF_NTPCM(req)) == -1)
die(STATE_CRITICAL, "NTP CRITICAL: No response from NTP server\n");
DBG(print_ntp_control_message(&req));
+ /* discard obviously invalid packets */
+ if (ntohs(req.count) > MAX_CM_SIZE)
+ die(STATE_CRITICAL, "NTP CRITICAL: Invalid packet received from NTP server\n");
if (LI(req.flags) == LI_ALARM) li_alarm = 1;
/* Each peer identifier is 4 bytes in the data section, which
* we represent as a ntp_assoc_status_pair datatype.
@@ -388,7 +333,7 @@ int ntp_request(const char *host, double *offset, int *offset_result, double *ji
if(verbose)
printf("parsing offset from peer %.2x: ", ntohs(peers[i].assoc));
- value = extract_value(data, "offset");
+ value = np_extract_ntpvar(data, "offset");
nptr=NULL;
/* Convert the value if we have one */
if(value != NULL)
@@ -412,7 +357,7 @@ int ntp_request(const char *host, double *offset, int *offset_result, double *ji
if(verbose) {
printf("parsing %s from peer %.2x: ", strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter", ntohs(peers[i].assoc));
}
- value = extract_value(data, strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter");
+ value = np_extract_ntpvar(data, strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter");
nptr=NULL;
/* Convert the value if we have one */
if(value != NULL)
@@ -431,7 +376,7 @@ int ntp_request(const char *host, double *offset, int *offset_result, double *ji
if(verbose) {
printf("parsing stratum from peer %.2x: ", ntohs(peers[i].assoc));
}
- value = extract_value(data, "stratum");
+ value = np_extract_ntpvar(data, "stratum");
nptr=NULL;
/* Convert the value if we have one */
if(value != NULL)
{0, 0, 0, 0}
};
-
+
if (argc < 2)
usage ("\n");
exit(STATE_OK);
break;
case 'V':
- print_revision(progname, revision);
+ print_revision(progname, NP_VERSION);
exit(STATE_OK);
break;
case 'v':
void print_help(void){
- print_revision(progname, revision);
+ print_revision(progname, NP_VERSION);
printf ("Copyright (c) 2006 Sean Finney\n");
printf (COPYRIGHT, copyright, email);