Code

Fixed reading too many argv parameters and changed to asprintf
[nagiosplug.git] / plugins / check_by_ssh.c
index 5c5053092541f0c5f98644254cacdfcdc855a6c8..866044bd3b9e62975e538c908a5d149d77f19110 100644 (file)
@@ -174,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'},
@@ -195,7 +194,6 @@ process_arguments (int argc, char **argv)
                {"use-ipv6", no_argument, 0, '6'},
                {0, 0, 0, 0}
        };
-#endif
 
        if (argc < 2)
                return ERROR;
@@ -205,13 +203,8 @@ process_arguments (int argc, char **argv)
                        strcpy (argv[c], "-t");
 
        while (1) {
-#ifdef HAVE_GETOPT_H
-               c =
-                       getopt_long (argc, argv, "Vvh46ft:H:O:p:i:u:l:C:n:s:", long_options,
+               c = getopt_long (argc, argv, "Vvh46ft:H:O:p:i:u:l:C:n:s:", long_options,
                                                                         &option_index);
-#else
-               c = getopt (argc, argv, "Vvh46ft:H:O:p:i:u:l:C:n:s:");
-#endif
 
                if (c == -1 || c == EOF)
                        break;
@@ -249,12 +242,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);
@@ -283,14 +276,19 @@ process_arguments (int argc, char **argv)
 
        c = optind;
        if (hostname == NULL) {
-               if (!is_host (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)