check_ssh.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /******************************************************************************
  2. *
  3. * Nagios check_ssh plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 1999-2006 nagios-plugins team
  7. *
  8. * Last Modified: $Date$
  9. *
  10. * Description:
  11. *
  12. * This file contains the check_ssh plugin
  13. *
  14. *
  15. * Try to connect to an SSH server at specified server and port
  16. *
  17. *
  18. * License Information:
  19. *
  20. * This program is free software; you can redistribute it and/or modify
  21. * it under the terms of the GNU General Public License as published by
  22. * the Free Software Foundation; either version 2 of the License, or
  23. * (at your option) any later version.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU General Public License
  31. * along with this program; if not, write to the Free Software
  32. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  33. *
  34. * $Id$
  35. *
  36. ******************************************************************************/
  37. const char *progname = "check_ssh";
  38. const char *revision = "$Revision$";
  39. const char *copyright = "2000-2006";
  40. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  41. #include "common.h"
  42. #include "netutils.h"
  43. #include "utils.h"
  44. #ifndef MSG_DONTWAIT
  45. #define MSG_DONTWAIT 0
  46. #endif
  47. #define SSH_DFL_PORT 22
  48. #define BUFF_SZ 256
  49. int port = -1;
  50. char *server_name = NULL;
  51. char *remote_version = NULL;
  52. int verbose = FALSE;
  53. int process_arguments (int, char **);
  54. int validate_arguments (void);
  55. void print_help (void);
  56. void print_usage (void);
  57. int ssh_connect (char *haddr, int hport, char *remote_version);
  58. int
  59. main (int argc, char **argv)
  60. {
  61. int result = STATE_UNKNOWN;
  62. setlocale (LC_ALL, "");
  63. bindtextdomain (PACKAGE, LOCALEDIR);
  64. textdomain (PACKAGE);
  65. if (process_arguments (argc, argv) == ERROR)
  66. usage4 (_("Could not parse arguments"));
  67. /* initialize alarm signal handling */
  68. signal (SIGALRM, socket_timeout_alarm_handler);
  69. alarm (socket_timeout);
  70. /* ssh_connect exits if error is found */
  71. result = ssh_connect (server_name, port, remote_version);
  72. alarm (0);
  73. return (result);
  74. }
  75. /* process command-line arguments */
  76. int
  77. process_arguments (int argc, char **argv)
  78. {
  79. int c;
  80. int option = 0;
  81. static struct option longopts[] = {
  82. {"help", no_argument, 0, 'h'},
  83. {"version", no_argument, 0, 'V'},
  84. {"host", required_argument, 0, 'H'}, /* backward compatibility */
  85. {"hostname", required_argument, 0, 'H'},
  86. {"port", required_argument, 0, 'p'},
  87. {"use-ipv4", no_argument, 0, '4'},
  88. {"use-ipv6", no_argument, 0, '6'},
  89. {"timeout", required_argument, 0, 't'},
  90. {"verbose", no_argument, 0, 'v'},
  91. {"remote-version", required_argument, 0, 'r'},
  92. {0, 0, 0, 0}
  93. };
  94. if (argc < 2)
  95. return ERROR;
  96. for (c = 1; c < argc; c++)
  97. if (strcmp ("-to", argv[c]) == 0)
  98. strcpy (argv[c], "-t");
  99. while (1) {
  100. c = getopt_long (argc, argv, "+Vhv46t:r:H:p:", longopts, &option);
  101. if (c == -1 || c == EOF)
  102. break;
  103. switch (c) {
  104. case '?': /* help */
  105. usage5 ();
  106. case 'V': /* version */
  107. print_revision (progname, revision);
  108. exit (STATE_OK);
  109. case 'h': /* help */
  110. print_help ();
  111. exit (STATE_OK);
  112. case 'v': /* verbose */
  113. verbose = TRUE;
  114. break;
  115. case 't': /* timeout period */
  116. if (!is_integer (optarg))
  117. usage2 (_("Timeout interval must be a positive integer"), optarg);
  118. else
  119. socket_timeout = atoi (optarg);
  120. break;
  121. case '4':
  122. address_family = AF_INET;
  123. break;
  124. case '6':
  125. #ifdef USE_IPV6
  126. address_family = AF_INET6;
  127. #else
  128. usage4 (_("IPv6 support not available"));
  129. #endif
  130. break;
  131. case 'r': /* remote version */
  132. remote_version = optarg;
  133. break;
  134. case 'H': /* host */
  135. if (is_host (optarg) == FALSE)
  136. usage2 (_("Invalid hostname/address"), optarg);
  137. server_name = optarg;
  138. break;
  139. case 'p': /* port */
  140. if (is_intpos (optarg)) {
  141. port = atoi (optarg);
  142. }
  143. else {
  144. usage2 (_("Port number must be a positive integer"), optarg);
  145. }
  146. }
  147. }
  148. c = optind;
  149. if (server_name == NULL && c < argc) {
  150. if (is_host (argv[c])) {
  151. server_name = argv[c++];
  152. }
  153. }
  154. if (port == -1 && c < argc) {
  155. if (is_intpos (argv[c])) {
  156. port = atoi (argv[c++]);
  157. }
  158. else {
  159. print_usage ();
  160. exit (STATE_UNKNOWN);
  161. }
  162. }
  163. return validate_arguments ();
  164. }
  165. int
  166. validate_arguments (void)
  167. {
  168. if (server_name == NULL)
  169. return ERROR;
  170. if (port == -1) /* funky, but allows -p to override stray integer in args */
  171. port = SSH_DFL_PORT;
  172. return OK;
  173. }
  174. /************************************************************************
  175. *
  176. * Try to connect to SSH server at specified server and port
  177. *
  178. *-----------------------------------------------------------------------*/
  179. int
  180. ssh_connect (char *haddr, int hport, char *remote_version)
  181. {
  182. int sd;
  183. int result;
  184. char *output = NULL;
  185. char *buffer = NULL;
  186. char *ssh_proto = NULL;
  187. char *ssh_server = NULL;
  188. char rev_no[20];
  189. sscanf ("$Revision$", "$Revision: %[0123456789.]", rev_no);
  190. result = my_tcp_connect (haddr, hport, &sd);
  191. if (result != STATE_OK)
  192. return result;
  193. output = (char *) malloc (BUFF_SZ + 1);
  194. memset (output, 0, BUFF_SZ + 1);
  195. recv (sd, output, BUFF_SZ, 0);
  196. if (strncmp (output, "SSH", 3)) {
  197. printf (_("Server answer: %s"), output);
  198. close(sd);
  199. exit (STATE_CRITICAL);
  200. }
  201. else {
  202. strip (output);
  203. if (verbose)
  204. printf ("%s\n", output);
  205. ssh_proto = output + 4;
  206. ssh_server = ssh_proto + strspn (ssh_proto, "-0123456789. ");
  207. ssh_proto[strspn (ssh_proto, "0123456789. ")] = 0;
  208. asprintf (&buffer, "SSH-%s-check_ssh_%s\r\n", ssh_proto, rev_no);
  209. send (sd, buffer, strlen (buffer), MSG_DONTWAIT);
  210. if (verbose)
  211. printf ("%s\n", buffer);
  212. if (remote_version && strcmp(remote_version, ssh_server)) {
  213. printf
  214. (_("SSH WARNING - %s (protocol %s) version mismatch, expected '%s'\n"),
  215. ssh_server, ssh_proto, remote_version);
  216. close(sd);
  217. exit (STATE_WARNING);
  218. }
  219. printf
  220. (_("SSH OK - %s (protocol %s)\n"),
  221. ssh_server, ssh_proto);
  222. close(sd);
  223. exit (STATE_OK);
  224. }
  225. }
  226. void
  227. print_help (void)
  228. {
  229. char *myport;
  230. asprintf (&myport, "%d", SSH_DFL_PORT);
  231. print_revision (progname, revision);
  232. printf ("Copyright (c) 1999 Remi Paulmier <remi@sinfomic.fr>\n");
  233. printf (COPYRIGHT, copyright, email);
  234. printf ("%s\n", _("Try to connect to an SSH server at specified server and port"));
  235. printf ("\n\n");
  236. print_usage ();
  237. printf (_(UT_HELP_VRSN));
  238. printf (_(UT_HOST_PORT), 'p', myport);
  239. printf (_(UT_IPv46));
  240. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  241. printf (" %s\n", "-r, --remote-version=STRING");
  242. printf (" %s\n", _("Warn if string doesn't match expected server version (ex: OpenSSH_3.9p1)"));
  243. printf (_(UT_VERBOSE));
  244. printf (_(UT_SUPPORT));
  245. }
  246. void
  247. print_usage (void)
  248. {
  249. printf (_("Usage:"));
  250. printf ("%s [-46] [-t <timeout>] [-r <remote version>] [-p <port>] <host>\n", progname);
  251. }