check_ssh.c 6.7 KB

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