Back to index

nagios-plugins  1.4.16
check_ssh.c
Go to the documentation of this file.
00001 /*****************************************************************************
00002 * 
00003 * Nagios check_ssh plugin
00004 * 
00005 * License: GPL
00006 * Copyright (c) 2000-2007 Nagios Plugins Development Team
00007 * 
00008 * Description:
00009 * 
00010 * This file contains the check_ssh plugin
00011 * 
00012 * Try to connect to an SSH server at specified server and port
00013 * 
00014 * 
00015 * This program is free software: you can redistribute it and/or modify
00016 * it under the terms of the GNU General Public License as published by
00017 * the Free Software Foundation, either version 3 of the License, or
00018 * (at your option) any later version.
00019 * 
00020 * This program is distributed in the hope that it will be useful,
00021 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023 * GNU General Public License for more details.
00024 * 
00025 * You should have received a copy of the GNU General Public License
00026 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00027 * 
00028 * 
00029 *****************************************************************************/
00030 
00031 const char *progname = "check_ssh";
00032 const char *copyright = "2000-2007";
00033 const char *email = "nagiosplug-devel@lists.sourceforge.net";
00034 
00035 #include "common.h"
00036 #include "netutils.h"
00037 #include "utils.h"
00038 
00039 #ifndef MSG_DONTWAIT
00040 #define MSG_DONTWAIT 0
00041 #endif
00042 
00043 #define SSH_DFL_PORT    22
00044 #define BUFF_SZ         256
00045 
00046 int port = -1;
00047 char *server_name = NULL;
00048 char *remote_version = NULL;
00049 int verbose = FALSE;
00050 
00051 int process_arguments (int, char **);
00052 int validate_arguments (void);
00053 void print_help (void);
00054 void print_usage (void);
00055 
00056 int ssh_connect (char *haddr, int hport, char *remote_version);
00057 
00058 
00059 
00060 int
00061 main (int argc, char **argv)
00062 {
00063        int result = STATE_UNKNOWN;
00064 
00065        setlocale (LC_ALL, "");
00066        bindtextdomain (PACKAGE, LOCALEDIR);
00067        textdomain (PACKAGE);
00068 
00069        /* Parse extra opts if any */
00070        argv=np_extra_opts (&argc, argv, progname);
00071 
00072        if (process_arguments (argc, argv) == ERROR)
00073               usage4 (_("Could not parse arguments"));
00074 
00075        /* initialize alarm signal handling */
00076        signal (SIGALRM, socket_timeout_alarm_handler);
00077 
00078        alarm (socket_timeout);
00079 
00080        /* ssh_connect exits if error is found */
00081        result = ssh_connect (server_name, port, remote_version);
00082 
00083        alarm (0);
00084 
00085        return (result);
00086 }
00087 
00088 
00089 
00090 /* process command-line arguments */
00091 int
00092 process_arguments (int argc, char **argv)
00093 {
00094        int c;
00095 
00096        int option = 0;
00097        static struct option longopts[] = {
00098               {"help", no_argument, 0, 'h'},
00099               {"version", no_argument, 0, 'V'},
00100               {"host", required_argument, 0, 'H'},      /* backward compatibility */
00101               {"hostname", required_argument, 0, 'H'},
00102               {"port", required_argument, 0, 'p'},
00103               {"use-ipv4", no_argument, 0, '4'},
00104               {"use-ipv6", no_argument, 0, '6'},
00105               {"timeout", required_argument, 0, 't'},
00106               {"verbose", no_argument, 0, 'v'},
00107               {"remote-version", required_argument, 0, 'r'},
00108               {0, 0, 0, 0}
00109        };
00110 
00111        if (argc < 2)
00112               return ERROR;
00113 
00114        for (c = 1; c < argc; c++)
00115               if (strcmp ("-to", argv[c]) == 0)
00116                      strcpy (argv[c], "-t");
00117 
00118        while (1) {
00119               c = getopt_long (argc, argv, "+Vhv46t:r:H:p:", longopts, &option);
00120 
00121               if (c == -1 || c == EOF)
00122                      break;
00123 
00124               switch (c) {
00125               case '?':                                                             /* help */
00126                      usage5 ();
00127               case 'V':                                                             /* version */
00128                      print_revision (progname, NP_VERSION);
00129                      exit (STATE_OK);
00130               case 'h':                                                             /* help */
00131                      print_help ();
00132                      exit (STATE_OK);
00133               case 'v':                                                             /* verbose */
00134                      verbose = TRUE;
00135                      break;
00136               case 't':                                                             /* timeout period */
00137                      if (!is_integer (optarg))
00138                             usage2 (_("Timeout interval must be a positive integer"), optarg);
00139                      else
00140                             socket_timeout = atoi (optarg);
00141                      break;
00142               case '4':
00143                      address_family = AF_INET;
00144                      break;
00145               case '6':
00146 #ifdef USE_IPV6
00147                      address_family = AF_INET6;
00148 #else
00149                      usage4 (_("IPv6 support not available"));
00150 #endif
00151                      break;
00152               case 'r':                                                             /* remote version */
00153                      remote_version = optarg;
00154                      break;
00155               case 'H':                                                             /* host */
00156                      if (is_host (optarg) == FALSE)
00157                             usage2 (_("Invalid hostname/address"), optarg);
00158                      server_name = optarg;
00159                      break;
00160               case 'p':                                                             /* port */
00161                      if (is_intpos (optarg)) {
00162                             port = atoi (optarg);
00163                      }
00164                      else {
00165                             usage2 (_("Port number must be a positive integer"), optarg);
00166                      }
00167               }
00168        }
00169 
00170        c = optind;
00171        if (server_name == NULL && c < argc) {
00172               if (is_host (argv[c])) {
00173                      server_name = argv[c++];
00174               }
00175        }
00176 
00177        if (port == -1 && c < argc) {
00178               if (is_intpos (argv[c])) {
00179                      port = atoi (argv[c++]);
00180               }
00181               else {
00182                      print_usage ();
00183                      exit (STATE_UNKNOWN);
00184               }
00185        }
00186 
00187        return validate_arguments ();
00188 }
00189 
00190 int
00191 validate_arguments (void)
00192 {
00193        if (server_name == NULL)
00194               return ERROR;
00195        if (port == -1)                                                       /* funky, but allows -p to override stray integer in args */
00196               port = SSH_DFL_PORT;
00197        return OK;
00198 }
00199 
00200 
00201 /************************************************************************
00202 *
00203 * Try to connect to SSH server at specified server and port
00204 *
00205 *-----------------------------------------------------------------------*/
00206 
00207 
00208 int
00209 ssh_connect (char *haddr, int hport, char *remote_version)
00210 {
00211        int sd;
00212        int result;
00213        char *output = NULL;
00214        char *buffer = NULL;
00215        char *ssh_proto = NULL;
00216        char *ssh_server = NULL;
00217        static char *rev_no = VERSION;
00218        struct timeval tv;
00219        double elapsed_time;
00220 
00221        gettimeofday(&tv, NULL);
00222 
00223        result = my_tcp_connect (haddr, hport, &sd);
00224 
00225        if (result != STATE_OK)
00226               return result;
00227 
00228        output = (char *) malloc (BUFF_SZ + 1);
00229        memset (output, 0, BUFF_SZ + 1);
00230        recv (sd, output, BUFF_SZ, 0);
00231        if (strncmp (output, "SSH", 3)) {
00232               printf (_("Server answer: %s"), output);
00233               close(sd);
00234               exit (STATE_CRITICAL);
00235        }
00236        else {
00237               strip (output);
00238               if (verbose)
00239                      printf ("%s\n", output);
00240               ssh_proto = output + 4;
00241               ssh_server = ssh_proto + strspn (ssh_proto, "-0123456789. ");
00242               ssh_proto[strspn (ssh_proto, "0123456789. ")] = 0;
00243 
00244               asprintf (&buffer, "SSH-%s-check_ssh_%s\r\n", ssh_proto, rev_no);
00245               send (sd, buffer, strlen (buffer), MSG_DONTWAIT);
00246               if (verbose)
00247                      printf ("%s\n", buffer);
00248 
00249               if (remote_version && strcmp(remote_version, ssh_server)) {
00250                      printf
00251                             (_("SSH WARNING - %s (protocol %s) version mismatch, expected '%s'\n"),
00252                              ssh_server, ssh_proto, remote_version);
00253                      close(sd);
00254                      exit (STATE_WARNING);
00255               }
00256 
00257               elapsed_time = (double)deltime(tv) / 1.0e6;
00258 
00259               printf
00260                      (_("SSH OK - %s (protocol %s) | %s\n"),
00261                       ssh_server, ssh_proto, fperfdata("time", elapsed_time, "s",
00262                       FALSE, 0, FALSE, 0, TRUE, 0, TRUE, (int)socket_timeout));
00263               close(sd);
00264               exit (STATE_OK);
00265        }
00266 }
00267 
00268 
00269 
00270 void
00271 print_help (void)
00272 {
00273        char *myport;
00274        asprintf (&myport, "%d", SSH_DFL_PORT);
00275 
00276        print_revision (progname, NP_VERSION);
00277 
00278        printf ("Copyright (c) 1999 Remi Paulmier <remi@sinfomic.fr>\n");
00279        printf (COPYRIGHT, copyright, email);
00280 
00281        printf ("%s\n", _("Try to connect to an SSH server at specified server and port"));
00282 
00283   printf ("\n\n");
00284 
00285        print_usage ();
00286 
00287        printf (UT_HELP_VRSN);
00288        printf (UT_EXTRA_OPTS);
00289 
00290        printf (UT_HOST_PORT, 'p', myport);
00291 
00292        printf (UT_IPv46);
00293 
00294        printf (UT_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
00295 
00296        printf (" %s\n", "-r, --remote-version=STRING");
00297   printf ("    %s\n", _("Warn if string doesn't match expected server version (ex: OpenSSH_3.9p1)"));
00298 
00299        printf (UT_VERBOSE);
00300 
00301        printf (UT_SUPPORT);
00302 }
00303 
00304 
00305 
00306 void
00307 print_usage (void)
00308 {
00309   printf ("%s\n", _("Usage:"));
00310        printf ("%s [-46] [-t <timeout>] [-r <remote version>] [-p <port>] <host>\n", progname);
00311 }
00312