check_ssh.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*****************************************************************************
  2. *
  3. * Nagios check_ssh plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 2000-2007 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-2007";
  32. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  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. int verbose = FALSE;
  45. int process_arguments (int, char **);
  46. int validate_arguments (void);
  47. void print_help (void);
  48. void print_usage (void);
  49. int ssh_connect (char *haddr, int hport, char *remote_version);
  50. int
  51. main (int argc, char **argv)
  52. {
  53. int result = STATE_UNKNOWN;
  54. setlocale (LC_ALL, "");
  55. bindtextdomain (PACKAGE, LOCALEDIR);
  56. textdomain (PACKAGE);
  57. /* Parse extra opts if any */
  58. argv=np_extra_opts (&argc, argv, progname);
  59. if (process_arguments (argc, argv) == ERROR)
  60. usage4 (_("Could not parse arguments"));
  61. /* initialize alarm signal handling */
  62. signal (SIGALRM, socket_timeout_alarm_handler);
  63. alarm (socket_timeout);
  64. /* ssh_connect exits if error is found */
  65. result = ssh_connect (server_name, port, remote_version);
  66. alarm (0);
  67. return (result);
  68. }
  69. /* process command-line arguments */
  70. int
  71. process_arguments (int argc, char **argv)
  72. {
  73. int c;
  74. int option = 0;
  75. static struct option longopts[] = {
  76. {"help", no_argument, 0, 'h'},
  77. {"version", no_argument, 0, 'V'},
  78. {"host", required_argument, 0, 'H'}, /* backward compatibility */
  79. {"hostname", required_argument, 0, 'H'},
  80. {"port", required_argument, 0, 'p'},
  81. {"use-ipv4", no_argument, 0, '4'},
  82. {"use-ipv6", no_argument, 0, '6'},
  83. {"timeout", required_argument, 0, 't'},
  84. {"verbose", no_argument, 0, 'v'},
  85. {"remote-version", required_argument, 0, 'r'},
  86. {0, 0, 0, 0}
  87. };
  88. if (argc < 2)
  89. return ERROR;
  90. for (c = 1; c < argc; c++)
  91. if (strcmp ("-to", argv[c]) == 0)
  92. strcpy (argv[c], "-t");
  93. while (1) {
  94. c = getopt_long (argc, argv, "+Vhv46t:r:H:p:", longopts, &option);
  95. if (c == -1 || c == EOF)
  96. break;
  97. switch (c) {
  98. case '?': /* help */
  99. usage5 ();
  100. case 'V': /* version */
  101. print_revision (progname, NP_VERSION);
  102. exit (STATE_OK);
  103. case 'h': /* help */
  104. print_help ();
  105. exit (STATE_OK);
  106. case 'v': /* verbose */
  107. verbose = TRUE;
  108. break;
  109. case 't': /* timeout period */
  110. if (!is_integer (optarg))
  111. usage2 (_("Timeout interval must be a positive integer"), optarg);
  112. else
  113. socket_timeout = atoi (optarg);
  114. break;
  115. case '4':
  116. address_family = AF_INET;
  117. break;
  118. case '6':
  119. #ifdef USE_IPV6
  120. address_family = AF_INET6;
  121. #else
  122. usage4 (_("IPv6 support not available"));
  123. #endif
  124. break;
  125. case 'r': /* remote version */
  126. remote_version = optarg;
  127. break;
  128. case 'H': /* host */
  129. if (is_host (optarg) == FALSE)
  130. usage2 (_("Invalid hostname/address"), optarg);
  131. server_name = optarg;
  132. break;
  133. case 'p': /* port */
  134. if (is_intpos (optarg)) {
  135. port = atoi (optarg);
  136. }
  137. else {
  138. usage2 (_("Port number must be a positive integer"), optarg);
  139. }
  140. }
  141. }
  142. c = optind;
  143. if (server_name == NULL && c < argc) {
  144. if (is_host (argv[c])) {
  145. server_name = argv[c++];
  146. }
  147. }
  148. if (port == -1 && c < argc) {
  149. if (is_intpos (argv[c])) {
  150. port = atoi (argv[c++]);
  151. }
  152. else {
  153. print_usage ();
  154. exit (STATE_UNKNOWN);
  155. }
  156. }
  157. return validate_arguments ();
  158. }
  159. int
  160. validate_arguments (void)
  161. {
  162. if (server_name == NULL)
  163. return ERROR;
  164. if (port == -1) /* funky, but allows -p to override stray integer in args */
  165. port = SSH_DFL_PORT;
  166. return OK;
  167. }
  168. /************************************************************************
  169. *
  170. * Try to connect to SSH server at specified server and port
  171. *
  172. *-----------------------------------------------------------------------*/
  173. int
  174. ssh_connect (char *haddr, int hport, char *remote_version)
  175. {
  176. int sd;
  177. int result;
  178. char *output = NULL;
  179. char *buffer = NULL;
  180. char *ssh_proto = NULL;
  181. char *ssh_server = NULL;
  182. static char *rev_no = VERSION;
  183. struct timeval tv;
  184. double elapsed_time;
  185. gettimeofday(&tv, NULL);
  186. result = my_tcp_connect (haddr, hport, &sd);
  187. if (result != STATE_OK)
  188. return result;
  189. output = (char *) malloc (BUFF_SZ + 1);
  190. memset (output, 0, BUFF_SZ + 1);
  191. recv (sd, output, BUFF_SZ, 0);
  192. if (strncmp (output, "SSH", 3)) {
  193. printf (_("Server answer: %s"), output);
  194. close(sd);
  195. exit (STATE_CRITICAL);
  196. }
  197. else {
  198. strip (output);
  199. if (verbose)
  200. printf ("%s\n", output);
  201. ssh_proto = output + 4;
  202. ssh_server = ssh_proto + strspn (ssh_proto, "-0123456789. ");
  203. ssh_proto[strspn (ssh_proto, "0123456789. ")] = 0;
  204. xasprintf (&buffer, "SSH-%s-check_ssh_%s\r\n", ssh_proto, rev_no);
  205. send (sd, buffer, strlen (buffer), MSG_DONTWAIT);
  206. if (verbose)
  207. printf ("%s\n", buffer);
  208. if (remote_version && strcmp(remote_version, ssh_server)) {
  209. printf
  210. (_("SSH WARNING - %s (protocol %s) version mismatch, expected '%s'\n"),
  211. ssh_server, ssh_proto, remote_version);
  212. close(sd);
  213. exit (STATE_WARNING);
  214. }
  215. elapsed_time = (double)deltime(tv) / 1.0e6;
  216. printf
  217. (_("SSH OK - %s (protocol %s) | %s\n"),
  218. ssh_server, ssh_proto, fperfdata("time", elapsed_time, "s",
  219. FALSE, 0, FALSE, 0, TRUE, 0, TRUE, (int)socket_timeout));
  220. close(sd);
  221. exit (STATE_OK);
  222. }
  223. }
  224. void
  225. print_help (void)
  226. {
  227. char *myport;
  228. xasprintf (&myport, "%d", SSH_DFL_PORT);
  229. print_revision (progname, NP_VERSION);
  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. printf (UT_SUPPORT);
  244. }
  245. void
  246. print_usage (void)
  247. {
  248. printf ("%s\n", _("Usage:"));
  249. printf ("%s [-4|-6] [-t <timeout>] [-r <remote version>] [-p <port>] <host>\n", progname);
  250. }