check_ssh.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 (_("Unknow argument\n"));
  82. break;
  83. case 'V': /* version */
  84. print_revision (progname, revision);
  85. exit (STATE_OK);
  86. case 'h': /* help */
  87. print_help ();
  88. exit (STATE_OK);
  89. case 'v': /* verbose */
  90. verbose = TRUE;
  91. break;
  92. case 't': /* timeout period */
  93. if (!is_integer (optarg))
  94. usage2 (_("Timeout interval must be a positive integer"), optarg);
  95. else
  96. socket_timeout = atoi (optarg);
  97. break;
  98. case '4':
  99. address_family = AF_INET;
  100. break;
  101. case '6':
  102. #ifdef USE_IPV6
  103. address_family = AF_INET6;
  104. #else
  105. usage (_("IPv6 support not available\n"));
  106. #endif
  107. break;
  108. case 'r': /* remote version */
  109. remote_version = optarg;
  110. break;
  111. case 'H': /* host */
  112. if (is_host (optarg) == FALSE)
  113. usage2 (_("Invalid hostname/address"), optarg);
  114. server_name = optarg;
  115. break;
  116. case 'p': /* port */
  117. if (is_intpos (optarg)) {
  118. port = atoi (optarg);
  119. }
  120. else {
  121. printf ("Port number nust be a positive integer: %s\n", optarg);
  122. usage ("");
  123. }
  124. }
  125. }
  126. c = optind;
  127. if (server_name == NULL && c < argc) {
  128. if (is_host (argv[c])) {
  129. server_name = argv[c++];
  130. }
  131. }
  132. if (port == -1 && c < argc) {
  133. if (is_intpos (argv[c])) {
  134. port = atoi (argv[c++]);
  135. }
  136. else {
  137. print_usage ();
  138. exit (STATE_UNKNOWN);
  139. }
  140. }
  141. return validate_arguments ();
  142. }
  143. int
  144. validate_arguments (void)
  145. {
  146. if (server_name == NULL)
  147. return ERROR;
  148. if (port == -1) /* funky, but allows -p to override stray integer in args */
  149. port = SSH_DFL_PORT;
  150. return OK;
  151. }
  152. /************************************************************************
  153. *
  154. * Try to connect to SSH server at specified server and port
  155. *
  156. *-----------------------------------------------------------------------*/
  157. int
  158. ssh_connect (char *haddr, int hport, char *remote_version)
  159. {
  160. int sd;
  161. int result;
  162. char *output = NULL;
  163. char *buffer = NULL;
  164. char *ssh_proto = NULL;
  165. char *ssh_server = NULL;
  166. char rev_no[20];
  167. sscanf ("$Revision$", "$Revision: %[0123456789.]", rev_no);
  168. result = my_tcp_connect (haddr, hport, &sd);
  169. if (result != STATE_OK)
  170. return result;
  171. output = (char *) malloc (BUFF_SZ + 1);
  172. memset (output, 0, BUFF_SZ + 1);
  173. recv (sd, output, BUFF_SZ, 0);
  174. if (strncmp (output, "SSH", 3)) {
  175. printf (_("Server answer: %s"), output);
  176. exit (STATE_CRITICAL);
  177. }
  178. else {
  179. strip (output);
  180. if (verbose)
  181. printf ("%s\n", output);
  182. ssh_proto = output + 4;
  183. ssh_server = ssh_proto + strspn (ssh_proto, "-0123456789. ");
  184. ssh_proto[strspn (ssh_proto, "0123456789. ")] = 0;
  185. asprintf (&buffer, "SSH-%s-check_ssh_%s\r\n", ssh_proto, rev_no);
  186. send (sd, buffer, strlen (buffer), MSG_DONTWAIT);
  187. if (verbose)
  188. printf ("%s\n", buffer);
  189. if (remote_version && strcmp(remote_version, ssh_server)) {
  190. printf
  191. (_("SSH WARNING - %s (protocol %s) version mismatch, expected '%s'\n"),
  192. ssh_server, ssh_proto, remote_version);
  193. exit (STATE_WARNING);
  194. }
  195. printf
  196. (_("SSH OK - %s (protocol %s)\n"),
  197. ssh_server, ssh_proto);
  198. exit (STATE_OK);
  199. }
  200. }
  201. void
  202. print_help (void)
  203. {
  204. char *myport;
  205. asprintf (&myport, "%d", SSH_DFL_PORT);
  206. print_revision (progname, revision);
  207. printf ("Copyright (c) 1999 Remi Paulmier <remi@sinfomic.fr>\n");
  208. printf (COPYRIGHT, copyright, email);
  209. printf (_("Try to connect to an SSH server at specified server and port\n\n"));
  210. print_usage ();
  211. printf (_(UT_HELP_VRSN));
  212. printf (_(UT_HOST_PORT), 'p', myport);
  213. printf (_(UT_IPv46));
  214. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  215. printf (_("\
  216. -r, --remote-version=STRING\n\
  217. Warn if string doesn't match expected server version (ex: OpenSSH_3.9p1)\n"));
  218. printf (_(UT_VERBOSE));
  219. printf (_(UT_SUPPORT));
  220. }
  221. void
  222. print_usage (void)
  223. {
  224. printf (_("\
  225. Usage: %s [-46] [-t <timeout>] [-r <remote version>] [-p <port>] <host>\n"), progname);
  226. printf (_(UT_HLP_VRS), progname, progname);
  227. }
  228. /* end of check_ssh.c */