check_ssh.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*****************************************************************************
  2. *
  3. * Nagios check_ssh plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 2000-2007 Nagios Plugins Development Team
  7. *
  8. * Last Modified: $Date$
  9. *
  10. * Description:
  11. *
  12. * This file contains the check_ssh plugin
  13. *
  14. * Try to connect to an SSH server at specified server and port
  15. *
  16. *
  17. * This program is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License as published by
  19. * the Free Software Foundation, either version 3 of the License, or
  20. * (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  29. *
  30. * $Id$
  31. *
  32. *****************************************************************************/
  33. const char *progname = "check_ssh";
  34. const char *revision = "$Revision$";
  35. const char *copyright = "2000-2007";
  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. /* Parse extra opts if any */
  62. argv=np_extra_opts (&argc, argv, progname);
  63. if (process_arguments (argc, argv) == ERROR)
  64. usage4 (_("Could not parse arguments"));
  65. /* initialize alarm signal handling */
  66. signal (SIGALRM, socket_timeout_alarm_handler);
  67. alarm (socket_timeout);
  68. /* ssh_connect exits if error is found */
  69. result = ssh_connect (server_name, port, remote_version);
  70. alarm (0);
  71. return (result);
  72. }
  73. /* process command-line arguments */
  74. int
  75. process_arguments (int argc, char **argv)
  76. {
  77. int c;
  78. int option = 0;
  79. static struct option longopts[] = {
  80. {"help", no_argument, 0, 'h'},
  81. {"version", no_argument, 0, 'V'},
  82. {"host", required_argument, 0, 'H'}, /* backward compatibility */
  83. {"hostname", required_argument, 0, 'H'},
  84. {"port", required_argument, 0, 'p'},
  85. {"use-ipv4", no_argument, 0, '4'},
  86. {"use-ipv6", no_argument, 0, '6'},
  87. {"timeout", required_argument, 0, 't'},
  88. {"verbose", no_argument, 0, 'v'},
  89. {"remote-version", required_argument, 0, 'r'},
  90. {0, 0, 0, 0}
  91. };
  92. if (argc < 2)
  93. return ERROR;
  94. for (c = 1; c < argc; c++)
  95. if (strcmp ("-to", argv[c]) == 0)
  96. strcpy (argv[c], "-t");
  97. while (1) {
  98. c = getopt_long (argc, argv, "+Vhv46t:r:H:p:", longopts, &option);
  99. if (c == -1 || c == EOF)
  100. break;
  101. switch (c) {
  102. case '?': /* help */
  103. usage5 ();
  104. case 'V': /* version */
  105. print_revision (progname, revision);
  106. exit (STATE_OK);
  107. case 'h': /* help */
  108. print_help ();
  109. exit (STATE_OK);
  110. case 'v': /* verbose */
  111. verbose = TRUE;
  112. break;
  113. case 't': /* timeout period */
  114. if (!is_integer (optarg))
  115. usage2 (_("Timeout interval must be a positive integer"), optarg);
  116. else
  117. socket_timeout = atoi (optarg);
  118. break;
  119. case '4':
  120. address_family = AF_INET;
  121. break;
  122. case '6':
  123. #ifdef USE_IPV6
  124. address_family = AF_INET6;
  125. #else
  126. usage4 (_("IPv6 support not available"));
  127. #endif
  128. break;
  129. case 'r': /* remote version */
  130. remote_version = optarg;
  131. break;
  132. case 'H': /* host */
  133. if (is_host (optarg) == FALSE)
  134. usage2 (_("Invalid hostname/address"), optarg);
  135. server_name = optarg;
  136. break;
  137. case 'p': /* port */
  138. if (is_intpos (optarg)) {
  139. port = atoi (optarg);
  140. }
  141. else {
  142. usage2 (_("Port number must be a positive integer"), optarg);
  143. }
  144. }
  145. }
  146. c = optind;
  147. if (server_name == NULL && c < argc) {
  148. if (is_host (argv[c])) {
  149. server_name = argv[c++];
  150. }
  151. }
  152. if (port == -1 && c < argc) {
  153. if (is_intpos (argv[c])) {
  154. port = atoi (argv[c++]);
  155. }
  156. else {
  157. print_usage ();
  158. exit (STATE_UNKNOWN);
  159. }
  160. }
  161. return validate_arguments ();
  162. }
  163. int
  164. validate_arguments (void)
  165. {
  166. if (server_name == NULL)
  167. return ERROR;
  168. if (port == -1) /* funky, but allows -p to override stray integer in args */
  169. port = SSH_DFL_PORT;
  170. return OK;
  171. }
  172. /************************************************************************
  173. *
  174. * Try to connect to SSH server at specified server and port
  175. *
  176. *-----------------------------------------------------------------------*/
  177. int
  178. ssh_connect (char *haddr, int hport, char *remote_version)
  179. {
  180. int sd;
  181. int result;
  182. char *output = NULL;
  183. char *buffer = NULL;
  184. char *ssh_proto = NULL;
  185. char *ssh_server = NULL;
  186. char rev_no[20];
  187. sscanf ("$Revision$", "$Revision: %[0123456789.]", rev_no);
  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. asprintf (&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 WARNING - %s (protocol %s) version mismatch, expected '%s'\n"),
  213. ssh_server, ssh_proto, remote_version);
  214. close(sd);
  215. exit (STATE_WARNING);
  216. }
  217. printf
  218. (_("SSH OK - %s (protocol %s)\n"),
  219. ssh_server, ssh_proto);
  220. close(sd);
  221. exit (STATE_OK);
  222. }
  223. }
  224. void
  225. print_help (void)
  226. {
  227. char *myport;
  228. asprintf (&myport, "%d", SSH_DFL_PORT);
  229. print_revision (progname, revision);
  230. printf ("Copyright (c) 1999 Remi Paulmier <remi@sinfomic.fr>\n");
  231. printf (COPYRIGHT, copyright, email);
  232. printf ("%s\n", _("Try to connect to an SSH server at specified server and port"));
  233. printf ("\n\n");
  234. print_usage ();
  235. printf (_(UT_HELP_VRSN));
  236. printf (_(UT_EXTRA_OPTS));
  237. printf (_(UT_HOST_PORT), 'p', myport);
  238. printf (_(UT_IPv46));
  239. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  240. printf (" %s\n", "-r, --remote-version=STRING");
  241. printf (" %s\n", _("Warn if string doesn't match expected server version (ex: OpenSSH_3.9p1)"));
  242. printf (_(UT_VERBOSE));
  243. #ifdef NP_EXTRA_OPTS
  244. printf ("\n");
  245. printf ("%s\n", _("Notes:"));
  246. printf (_(UT_EXTRA_OPTS_NOTES));
  247. #endif
  248. printf (_(UT_SUPPORT));
  249. }
  250. void
  251. print_usage (void)
  252. {
  253. printf (_("Usage:"));
  254. printf ("%s [-46] [-t <timeout>] [-r <remote version>] [-p <port>] <host>\n", progname);
  255. }