check_ssh.c 7.5 KB

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