check_ssh.c 6.5 KB

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