Code

test GNU_SOURCE and include features.h if present to clear warning about asprintf...
[nagiosplug.git] / plugins / check_by_ssh.c
index 97c8672fa668135f854243c68b01cb4528e08e1a..7328f5ac66d0016cabae9b4c8adc78abdb021c17 100644 (file)
@@ -22,7 +22,7 @@
  *
  *****************************************************************************/
  
-#define PROGRAM check_by_ssh
+const char *progname = "check_by_ssh";
 #define DESCRIPTION "Run checks on a remote system using ssh, wrapping the proper timeout around the ssh invocation."
 #define AUTHOR "Karl DeBisschop"
 #define EMAIL "karl@debisschop.net"
 #include "utils.h"
 #include <time.h>
 
-#define PROGNAME "check_by_ssh"
-
 int process_arguments (int, char **);
-int call_getopt (int, char **);
 int validate_arguments (void);
-void print_help (char *command_name);
+void print_help (const char *command_name);
 void print_usage (void);
 
 
@@ -177,7 +174,6 @@ process_arguments (int argc, char **argv)
        char *p1, *p2;
        size_t len;
 
-#ifdef HAVE_GETOPT_H
        int option_index = 0;
        static struct option long_options[] = {
                {"version", no_argument, 0, 'V'},
@@ -194,11 +190,12 @@ process_arguments (int argc, char **argv)
                {"user", required_argument, 0, 'u'},
                {"logname", required_argument, 0, 'l'},
                {"command", required_argument, 0, 'C'},
+               {"proto1", no_argument, 0, '1'},
+               {"proto2", no_argument, 0, '2'},
                {"use-ipv4", no_argument, 0, '4'},
                {"use-ipv6", no_argument, 0, '6'},
                {0, 0, 0, 0}
        };
-#endif
 
        if (argc < 2)
                return ERROR;
@@ -208,13 +205,8 @@ process_arguments (int argc, char **argv)
                        strcpy (argv[c], "-t");
 
        while (1) {
-#ifdef HAVE_GETOPT_H
-               c =
-                       getopt_long (argc, argv, "+?Vvhft46H:O:p:i:u:l:C:n:s:", long_options,
+               c = getopt_long (argc, argv, "Vvh1246ft:H:O:p:i:u:l:C:n:s:", long_options,
                                                                         &option_index);
-#else
-               c = getopt (argc, argv, "+?Vvhft46H:O:p:i:u:l:C:n:s:");
-#endif
 
                if (c == -1 || c == EOF)
                        break;
@@ -224,10 +216,10 @@ process_arguments (int argc, char **argv)
                        print_usage ();
                        exit (STATE_UNKNOWN);
                case 'V':                                                                       /* version */
-                       print_revision (PROGNAME, "$Revision$");
+                       print_revision (progname, "$Revision$");
                        exit (STATE_OK);
                case 'h':                                                                       /* help */
-                       print_help (PROGNAME);
+                       print_help (progname);
                        exit (STATE_OK);
                case 'v':                                                                       /* help */
                        verbose = TRUE;
@@ -252,12 +244,12 @@ process_arguments (int argc, char **argv)
                        passive = TRUE;
                        break;
                case 's':                                                                       /* description of service to check */
-                       service = realloc (service, ++services);
+                       service = realloc (service, (++services) * sizeof(char *));
                        p1 = optarg;
                        while (p2 = index (p1, ':')) {
                                *p2 = '\0';
                                asprintf (&service[services-1], "%s", p1);
-                               service = realloc (service, ++services);
+                               service = realloc (service, (++services) * sizeof(char *));
                                p1 = p2 + 1;
                        }
                        asprintf (&service[services-1], "%s", p1);
@@ -271,8 +263,10 @@ process_arguments (int argc, char **argv)
                case 'i':                                                                       /* identity */
                        asprintf (&comm, "%s -%c %s", comm, c, optarg);
                        break;
-               case '4':                                                                       /* Pass these switches directly to ssh */
-               case '6':                                                               /* -4 for IPv4, -6 for IPv6 */
+               case '1':                                                                       /* Pass these switches directly to ssh */
+               case '2':                                                                       /* 1 to force version 1, 2 to force version 2 */
+               case '4':                                                                       /* -4 for IPv4 */
+               case '6':                                                               /* -6 for IPv6 */
                case 'f':                                                                       /* fork to background */
                        asprintf (&comm, "%s -%c", comm, c);
                        break;
@@ -286,14 +280,19 @@ process_arguments (int argc, char **argv)
 
        c = optind;
        if (hostname == NULL) {
-               if (!is_host (argv[c]))
-                       terminate (STATE_UNKNOWN, "%s: Invalid host name %s\n", PROGNAME, argv[c]);
+               if (c <= argc) {
+                       terminate (STATE_UNKNOWN, "%s: You must provide a host name\n", progname);
+               } else if (!is_host (argv[c]))
+                       terminate (STATE_UNKNOWN, "%s: Invalid host name %s\n", progname, argv[c]);
                hostname = argv[c++];
        }
 
        if (strlen(remotecmd) == 0) {
                for (; c < argc; c++)
-                       asprintf (&remotecmd, "%s %s", remotecmd, argv[c]);
+                       if (strlen(remotecmd) > 0)
+                               asprintf (&remotecmd, "%s %s", remotecmd, argv[c]);
+                       else
+                               asprintf (&remotecmd, "%s", argv[c]);
        }
 
        if (commands > 1)
@@ -318,10 +317,10 @@ validate_arguments (void)
                return ERROR;
 
        if (passive && commands != services)
-               terminate (STATE_UNKNOWN, "%s: In passive mode, you must provide a service name for each command.\n", PROGNAME);
+               terminate (STATE_UNKNOWN, "%s: In passive mode, you must provide a service name for each command.\n", progname);
 
        if (passive && host_shortname == NULL)
-               terminate (STATE_UNKNOWN, "%s: In passive mode, you must provide the host short name from the nagios configs.\n", PROGNAME);
+               terminate (STATE_UNKNOWN, "%s: In passive mode, you must provide the host short name from the nagios configs.\n", progname);
 
        return OK;
 }
@@ -331,7 +330,7 @@ validate_arguments (void)
 
 
 void
-print_help (char *cmd)
+print_help (const char *cmd)
 {
        print_revision (cmd, "$Revision$");
 
@@ -362,6 +361,10 @@ print_help (char *cmd)
                 "   list of nagios service names, separated by ':' [optional]\n"
                 "-n, --name=NAME\n"
                 "   short name of host in nagios configuration [optional]\n"
+                "-1, --proto1\n"
+                "   tell ssh to use Protocol 1\n"
+                "-2, --proto2\n"
+                "   tell ssh to use Protocol 2\n"
                 "-4, --use-ipv4\n"
                 "   tell ssh to use IPv4\n"
                 "-6, --use-ipv6\n"