check_ssh.c 5.8 KB

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