Code

Fixed tests (Mathew Ericson - 738608)
[nagiosplug.git] / plugins / check_ssh.c
index faaead6a36725ef8e27a286067b567a169ec07de..f3a351e6800d82dd24b608705c76bc4501cf7936 100644 (file)
@@ -34,12 +34,12 @@ int validate_arguments (void);
 void print_help (void);
 void print_usage (void);
 
-char *ssh_resolve (char *hostname);
 int ssh_connect (char *haddr, short hport);
 
 int
 main (int argc, char **argv)
 {
+       int result;
 
        if (process_arguments (argc, argv) == ERROR)
                usage ("Could not parse arguments\n");
@@ -49,11 +49,11 @@ main (int argc, char **argv)
        alarm (socket_timeout);
 
        /* ssh_connect exits if error is found */
-       ssh_connect (ssh_resolve (server_name), port);
+       result = ssh_connect (server_name, port);
 
        alarm (0);
 
-       return (STATE_OK);
+       return (result);
 }
 
 
@@ -62,8 +62,8 @@ int
 process_arguments (int argc, char **argv)
 {
        int c;
+       char *tmp = NULL;
 
-#ifdef HAVE_GETOPT_H
        int option_index = 0;
        static struct option long_options[] = {
                {"version", no_argument, 0, 'V'},
@@ -73,7 +73,6 @@ process_arguments (int argc, char **argv)
                {"host", required_argument, 0, 'H'},
                {0, 0, 0, 0}
        };
-#endif
 
        if (argc < 2)
                return ERROR;
@@ -83,11 +82,8 @@ process_arguments (int argc, char **argv)
                        strcpy (argv[c], "-t");
 
        while (1) {
-#ifdef HAVE_GETOPT_H
                c = getopt_long (argc, argv, "+Vhvt:H:p:", long_options, &option_index);
-#else
-               c = getopt (argc, argv, "+Vhvt:H:p:");
-#endif
+
                if (c == -1 || c == EOF)
                        break;
 
@@ -109,6 +105,8 @@ process_arguments (int argc, char **argv)
                        socket_timeout = atoi (optarg);
                        break;
                case 'H':                                                                       /* host */
+                       if (is_host (optarg) == FALSE)
+                               usage ("Invalid hostname/address\n");
                        server_name = optarg;
                        break;
                case 'p':                                                                       /* port */
@@ -124,10 +122,13 @@ process_arguments (int argc, char **argv)
        }
 
        c = optind;
-       if (server_name == NULL && argv[c]) {
-               server_name = argv[c++];
+       if (server_name == NULL && c < argc) {
+               if (is_host (argv[c])) {
+                       server_name = argv[c++];
+               }
        }
-       else if (port == -1 && argv[c]) {
+
+       if (port == -1 && c < argc) {
                if (is_intpos (argv[c])) {
                        port = atoi (argv[c++]);
                }
@@ -151,26 +152,6 @@ validate_arguments (void)
 }
 
 
-/************************************************************************
-*
-* Resolve hostname into IP address
-*
-*-----------------------------------------------------------------------*/
-
-char *
-ssh_resolve (char *hostname)
-{
-       struct hostent *host;
-
-       host = gethostbyname (hostname);
-       if (!host) {
-               herror (hostname);
-               exit (STATE_CRITICAL);
-       }
-       return (host->h_addr);
-}
-
-
 /************************************************************************
 *
 * Try to connect to SSH server at specified server and port
@@ -180,10 +161,8 @@ ssh_resolve (char *hostname)
 int
 ssh_connect (char *haddr, short hport)
 {
-       int s;
-       struct sockaddr_in addr;
-       int addrlen;
-       int len;
+       int sd;
+       int result;
        char *output = NULL;
        char *buffer = NULL;
        char *ssh_proto = NULL;
@@ -192,27 +171,14 @@ ssh_connect (char *haddr, short hport)
 
        sscanf ("$Revision$", "$Revision: %[0123456789.]", revision);
 
-       addrlen = sizeof (addr);
-       memset (&addr, 0, addrlen);
-       addr.sin_port = htons (hport);
-       addr.sin_family = AF_INET;
-       bcopy (haddr, (void *) &addr.sin_addr.s_addr, 4);
+       result = my_tcp_connect (haddr, hport, &sd);
 
-       s = socket (AF_INET, SOCK_STREAM, 0);
-       if (!s) {
-               printf ("socket(): %s for %s:%d\n", strerror (errno), server_name, hport);
-               exit (STATE_CRITICAL);
-       }
-
-       if (connect (s, (struct sockaddr *) &addr, addrlen)) {
-               printf ("connect(): %s for %s:%d\n", strerror (errno), server_name,
-                                               hport);
-               exit (STATE_CRITICAL);
-       }
+       if (result != STATE_OK)
+               return result;
 
        output = (char *) malloc (BUFF_SZ + 1);
        memset (output, 0, BUFF_SZ + 1);
-       recv (s, output, BUFF_SZ, 0);
+       recv (sd, output, BUFF_SZ, 0);
        if (strncmp (output, "SSH", 3)) {
                printf ("Server answer: %s", output);
                exit (STATE_CRITICAL);
@@ -225,10 +191,10 @@ ssh_connect (char *haddr, short hport)
                ssh_server = ssh_proto + strspn (ssh_proto, "-0123456789. ");
                ssh_proto[strspn (ssh_proto, "0123456789. ")] = 0;
                printf
-                       ("SSH ok - %s (protocol %s)\n",
+                       ("SSH OK - %s (protocol %s)\n",
                         ssh_server, ssh_proto);
                asprintf (&buffer, "SSH-%s-check_ssh_%s\r\n", ssh_proto, revision);
-               send (s, buffer, strlen (buffer), MSG_DONTWAIT);
+               send (sd, buffer, strlen (buffer), MSG_DONTWAIT);
                if (verbose)
                        printf ("%s\n", buffer);
                exit (STATE_OK);