--- nagios-plugins-1.4.15/plugins/check_snmp.c.conf 2011-07-22 09:23:17.000000000 -0400 +++ nagios-plugins-1.4.15/plugins/check_snmp.c 2011-07-26 15:41:43.000000000 -0400 @@ -259,26 +259,29 @@ asprintf (&command_line[4], "%d", retries); command_line[5] = strdup ("-m"); command_line[6] = strdup (miblist); - command_line[7] = "-v"; - command_line[8] = strdup (proto); for (i = 0; i < numauthpriv; i++) { - command_line[9 + i] = authpriv[i]; + command_line[7 + i] = authpriv[i]; } - asprintf (&command_line[9 + numauthpriv], "%s:%s", server_address, port); + asprintf (&command_line[7 + numauthpriv], "%s:%s", server_address, port); /* This is just for display purposes, so it can remain a string */ - asprintf(&cl_hidden_auth, "%s -t %d -r %d -m %s -v %s %s %s:%s", - snmpcmd, timeout_interval, retries, strlen(miblist) ? miblist : "''", proto, "[authpriv]", - server_address, port); + if (proto != NULL) + asprintf(&cl_hidden_auth, "%s -t %d -r %d -m %s -v %s %s %s:%s", + snmpcmd, timeout_interval, retries, strlen(miblist) ? miblist : "''", proto, "[authpriv]", + server_address, port); + else + asprintf(&cl_hidden_auth, "%s -t %d -r %d -m %s %s %s:%s", + snmpcmd, timeout_interval, retries, strlen(miblist) ? miblist : "''", "[authpriv]", + server_address, port); for (i = 0; i < numoids; i++) { - command_line[9 + numauthpriv + 1 + i] = oids[i]; + command_line[7 + numauthpriv + 1 + i] = oids[i]; asprintf(&cl_hidden_auth, "%s %s", cl_hidden_auth, oids[i]); } - command_line[9 + numauthpriv + 1 + numoids] = NULL; + command_line[7 + numauthpriv + 1 + numoids] = NULL; if (verbose) printf ("%s\n", cl_hidden_auth); @@ -835,9 +838,36 @@ +void addParam(char *flag, char*val) { + // We are lazy. Verify if the value is null and, if not, add it + if (flag != NULL && val != NULL) { + // Raise the amount of space required for our array + numauthpriv+=2; + + if (authpriv == NULL) + authpriv = calloc(numauthpriv, sizeof(char *)); + else + authpriv = realloc(authpriv, numauthpriv * sizeof(char *)); + + authpriv[numauthpriv-2] = strdup(flag); + authpriv[numauthpriv-1] = strdup(val); + } +} + +/* +Protocol Security Params Reason +NULL NULL -c,-a,-u,-A,-x,-X We don't know what the user wants. We overwrite anything provided. +1 * -v,-c SNMP v1 supports only community +2c * -v,-c SNMP v2c supports only community +3 NULL -v,-u,-a,-A,-x,-X We don't know the security required in snmp.conf. We provide anything +3 authPriv -v,-l,-u,-a,-A,-x,-X We overwrite any Authentication of privacy setting of snmp.conf by command line +3 noAuthNoPriv -v,-l,-u We provide version, security level and security name +3 authNoPriv -v,-l,-u,-a,-A We provide version, security level, security name and password +*/ int validate_arguments () { + numauthpriv=0; /* check whether to load locally installed MIBS (CPU/disk intensive) */ if (miblist == NULL) { if ( needmibs == TRUE ) { @@ -855,75 +885,38 @@ if (numoids == 0) die(STATE_UNKNOWN, _("No OIDs specified\n")); - if (proto == NULL) - asprintf(&proto, DEFAULT_PROTOCOL); - if ((strcmp(proto,"1") == 0) || (strcmp(proto, "2c")==0)) { /* snmpv1 or snmpv2c */ - numauthpriv = 2; - authpriv = calloc (numauthpriv, sizeof (char *)); - authpriv[0] = strdup ("-c"); - authpriv[1] = strdup (community); - } - else if ( strcmp (proto, "3") == 0 ) { /* snmpv3 args */ - if (seclevel == NULL) - asprintf(&seclevel, "noAuthNoPriv"); - - if (strcmp(seclevel, "noAuthNoPriv") == 0) { - numauthpriv = 2; - authpriv = calloc (numauthpriv, sizeof (char *)); - authpriv[0] = strdup ("-l"); - authpriv[1] = strdup ("noAuthNoPriv"); + if (proto == NULL || strcmp (proto, "3") == 0) { + // No protocol will pass any valid parameter. See note before function for more explanation + if (proto == NULL) { + addParam("-c",community); } else { - if (! ( (strcmp(seclevel, "authNoPriv")==0) || (strcmp(seclevel, "authPriv")==0) ) ) { - usage2 (_("Invalid seclevel"), seclevel); - } - - if (authproto == NULL ) - asprintf(&authproto, DEFAULT_AUTH_PROTOCOL); - - if (secname == NULL) - die(STATE_UNKNOWN, _("Required parameter: %s\n"), "secname"); - - if (authpasswd == NULL) - die(STATE_UNKNOWN, _("Required parameter: %s\n"), "authpasswd"); - - if ( strcmp(seclevel, "authNoPriv") == 0 ) { - numauthpriv = 8; - authpriv = calloc (numauthpriv, sizeof (char *)); - authpriv[0] = strdup ("-l"); - authpriv[1] = strdup ("authNoPriv"); - authpriv[2] = strdup ("-a"); - authpriv[3] = strdup (authproto); - authpriv[4] = strdup ("-u"); - authpriv[5] = strdup (secname); - authpriv[6] = strdup ("-A"); - authpriv[7] = strdup (authpasswd); - } else if ( strcmp(seclevel, "authPriv") == 0 ) { - if (privproto == NULL ) - asprintf(&privproto, DEFAULT_PRIV_PROTOCOL); - - if (privpasswd == NULL) - die(STATE_UNKNOWN, _("Required parameter: %s\n"), "privpasswd"); - - numauthpriv = 12; - authpriv = calloc (numauthpriv, sizeof (char *)); - authpriv[0] = strdup ("-l"); - authpriv[1] = strdup ("authPriv"); - authpriv[2] = strdup ("-a"); - authpriv[3] = strdup (authproto); - authpriv[4] = strdup ("-u"); - authpriv[5] = strdup (secname); - authpriv[6] = strdup ("-A"); - authpriv[7] = strdup (authpasswd); - authpriv[8] = strdup ("-x"); - authpriv[9] = strdup (privproto); - authpriv[10] = strdup ("-X"); - authpriv[11] = strdup (privpasswd); - } + addParam("-v",proto); } + addParam("-u",secname); - } - else { + if (seclevel == NULL || strcmp(seclevel, "authPriv")==0) { + addParam("-l",seclevel); + addParam("-a",authproto); + addParam("-A",authpasswd); + addParam("-x",privproto); + addParam("-X",privpasswd); + } else if (strcmp(seclevel, "noAuthNoPriv") == 0) { + addParam("-v",proto); + addParam("-l",seclevel); + } else if (strcmp(seclevel, "authNoPriv")==0) { + addParam("-v",proto); + addParam("-l",seclevel); + addParam("-a",authproto); + addParam("-A",authpasswd); + } else { + usage2 (_("Invalid seclevel"), seclevel); + } + } else if ((strcmp(proto,"1") == 0) || (strcmp(proto, "2c")==0)) { /* snmpv1 or snmpv2c */ + // If we specified protocol v2, the only param that is usefull is community string (if specified). + addParam("-v",proto); + addParam("-c",community); + } else { usage2 (_("Invalid SNMP version"), proto); } @@ -1011,12 +1004,11 @@ printf (" %s\n", "-a, --authproto=[MD5|SHA]"); printf (" %s\n", _("SNMPv3 auth proto")); printf (" %s\n", "-x, --privproto=[DES|AES]"); - printf (" %s\n", _("SNMPv3 priv proto (default DES)")); + printf (" %s\n", _("SNMPv3 priv proto")); /* Authentication Tokens*/ printf (" %s\n", "-C, --community=STRING"); printf (" %s ", _("Optional community string for SNMP communication")); - printf ("(%s \"%s\")\n", _("default is") ,DEFAULT_COMMUNITY); printf (" %s\n", "-U, --secname=USERNAME"); printf (" %s\n", _("SNMPv3 username")); printf (" %s\n", "-A, --authpassword=PASSWORD"); @@ -1076,6 +1068,8 @@ printf ("\n"); printf ("%s\n", _("Notes:")); + printf (" %s\n", _("- Default configurations are retrieved from /etc/snmp/snmp.conf. See man page of")); + printf (" %s\n", _("snmp.conf for more information.")); printf (" %s\n", _("- Multiple OIDs may be indicated by a comma or space-delimited list (lists with")); printf (" %s %i %s\n", _("internal spaces must be quoted). Maximum:"), MAX_OIDS, _("OIDs."));