check_ssh.c 5.8 KB

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