X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=plugins%2Fcheck_ssh.c;h=7fcb81198ca046b05f7284b63f65203be618b2fd;hb=eb47d0870a7901a5e6304686e0defee4287d66bc;hp=d78189fc793100e146b2f0da3fe79ec8a66609b4;hpb=44a321cb8a42d6c0ea2d96a1086a17f2134c89cc;p=nagiosplug.git diff --git a/plugins/check_ssh.c b/plugins/check_ssh.c index d78189f..7fcb811 100644 --- a/plugins/check_ssh.c +++ b/plugins/check_ssh.c @@ -1,21 +1,29 @@ -/* -* check_ssh.c -* -* Made by (Remi PAULMIER) -* Login -* -* Started on Fri Jul 9 09:18:23 1999 Remi PAULMIER -* Update Thu Jul 22 12:50:04 1999 remi paulmier -* $Id$ -* -*/ +/****************************************************************************** + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +******************************************************************************/ -#include "config.h" #include "common.h" #include "netutils.h" #include "utils.h" -#define PROGNAME "check_ssh" +const char *progname = "check_ssh"; +const char *revision = "$Revision$"; +const char *copyright = "2000-2003"; +const char *email = "nagiosplug-devel@lists.sourceforge.net"; #ifndef MSG_DONTWAIT #define MSG_DONTWAIT 0 @@ -29,31 +37,30 @@ char *server_name = NULL; int verbose = FALSE; int process_arguments (int, char **); -int call_getopt (int, char **); 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"); + usage (_("Could not parse arguments\n")); /* initialize alarm signal handling */ signal (SIGALRM, socket_timeout_alarm_handler); 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); } @@ -63,81 +70,37 @@ process_arguments (int argc, char **argv) { int c; - if (argc < 2) - return ERROR; - - for (c = 1; c < argc; c++) - if (strcmp ("-to", argv[c]) == 0) - strcpy (argv[c], "-t"); - - c = 0; - while (c += (call_getopt (argc - c, &argv[c]))) { - if (argc <= c) - break; - if (server_name == NULL) { - server_name = argv[c]; - } - else if (port == -1) { - if (is_intpos (argv[c])) { - port = atoi (argv[c]); - } - else { - print_usage (); - exit (STATE_UNKNOWN); - } - } - } - - return validate_arguments (); -} - - -/************************************************************************ -* -* Run the getopt until we encounter a non-option entry in the arglist -* -*-----------------------------------------------------------------------*/ - -int -call_getopt (int argc, char **argv) -{ - int c, i = 1; - -#ifdef HAVE_GETOPT_H int option_index = 0; static struct option long_options[] = { - {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, - {"verbose", no_argument, 0, 'v'}, - {"timeout", required_argument, 0, 't'}, + {"version", no_argument, 0, 'V'}, {"host", required_argument, 0, 'H'}, + {"port", required_argument, 0, 'p'}, + {"use-ipv4", no_argument, 0, '4'}, + {"use-ipv6", no_argument, 0, '6'}, + {"timeout", required_argument, 0, 't'}, + {"verbose", no_argument, 0, 'v'}, {0, 0, 0, 0} }; -#endif + + if (argc < 2) + return ERROR; + + for (c = 1; c < argc; c++) + if (strcmp ("-to", argv[c]) == 0) + 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 + c = getopt_long (argc, argv, "+Vhv46t:H:p:", long_options, &option_index); if (c == -1 || c == EOF) break; - i++; - switch (c) { - case 't': - case 'H': - case 'p': - i++; - } - switch (c) { case '?': /* help */ usage (""); case 'V': /* version */ - print_revision (my_basename (argv[0]), "$Revision$"); + print_revision (progname, revision); exit (STATE_OK); case 'h': /* help */ print_help (); @@ -147,10 +110,22 @@ call_getopt (int argc, char **argv) break; case 't': /* timeout period */ if (!is_integer (optarg)) - usage ("Timeout Interval must be an integer!\n\n"); + usage (_("Timeout Interval must be an integer!\n\n")); socket_timeout = atoi (optarg); break; + case '4': + address_family = AF_INET; + break; + case '6': +#ifdef USE_IPV6 + address_family = AF_INET6; +#else + usage (_("IPv6 support not available\n")); +#endif + break; case 'H': /* host */ + if (is_host (optarg) == FALSE) + usage ("Invalid hostname/address\n"); server_name = optarg; break; case 'p': /* port */ @@ -164,7 +139,25 @@ call_getopt (int argc, char **argv) } } - return i; + + c = optind; + if (server_name == NULL && c < argc) { + if (is_host (argv[c])) { + server_name = argv[c++]; + } + } + + if (port == -1 && c < argc) { + if (is_intpos (argv[c])) { + port = atoi (argv[c++]); + } + else { + print_usage (); + exit (STATE_UNKNOWN); + } + } + + return validate_arguments (); } int @@ -178,26 +171,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 @@ -207,41 +180,26 @@ 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; char *ssh_server = NULL; - char revision[20]; + char rev_no[20]; - sscanf ("$Revision$", "$Revision: %[0123456789.]", revision); + sscanf ("$Revision$", "$Revision: %[0123456789.]", rev_no); - 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); + printf (_("Server answer: %s"), output); exit (STATE_CRITICAL); } else { @@ -249,14 +207,13 @@ ssh_connect (char *haddr, short hport) if (verbose) printf ("%s\n", output); ssh_proto = output + 4; - ssh_server = ssh_proto + strspn (ssh_proto, "0123456789-. "); - ssh_proto[strspn (ssh_proto, "0123456789-. ")] = 0; + ssh_server = ssh_proto + strspn (ssh_proto, "-0123456789. "); + ssh_proto[strspn (ssh_proto, "0123456789. ")] = 0; printf - ("SSH ok - protocol version %s - server version %s\n", - ssh_proto, ssh_server); - buffer = - ssprintf (buffer, "SSH-%s-check_ssh_%s\r\n", ssh_proto, revision); - send (s, buffer, strlen (buffer), MSG_DONTWAIT); + (_("SSH OK - %s (protocol %s)\n"), + ssh_server, ssh_proto); + asprintf (&buffer, "SSH-%s-check_ssh_%s\r\n", ssh_proto, rev_no); + send (sd, buffer, strlen (buffer), MSG_DONTWAIT); if (verbose) printf ("%s\n", buffer); exit (STATE_OK); @@ -266,20 +223,37 @@ ssh_connect (char *haddr, short hport) void print_help (void) { - print_revision (PROGNAME, "$Revision$"); - printf ("Copyright (c) 1999 Remi Paulmier (remi@sinfomic.fr)\n\n"); + char *myport; + asprintf (&myport, "%d", SSH_DFL_PORT); + + print_revision (progname, revision); + + printf (_("Copyright (c) 1999 Remi Paulmier \n")); + printf (_(COPYRIGHT), copyright, email); + + printf (_("Try to connect to SSH server at specified server and port\n\n")); + print_usage (); - printf ("by default, port is %d\n", SSH_DFL_PORT); + + printf (_(UT_HELP_VRSN)); + + printf (_(UT_HOST_PORT), 'p', myport); + + printf (_(UT_IPv46)); + + printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT); + + printf (_(UT_VERBOSE)); + + printf (_(UT_SUPPORT)); } void print_usage (void) { - printf - ("Usage:\n" - " %s -t [timeout] -p [port] \n" - " %s -V prints version info\n" - " %s -h prints more detailed help\n", PROGNAME, PROGNAME, PROGNAME); + printf (_("\ +Usage: %s [-46] [-t ] [-p ] \n"), progname); + printf (_(UT_HLP_VRS), progname, progname); } /* end of check_ssh.c */