check_ssh.c 6.5 KB

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