4
0

check_ssh.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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) == ERROR)
  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. usage2 (_("Unknown argument"), optarg);
  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. usage4 (_("IPv6 support not available"));
  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. usage2 (_("Port number must be a positive integer"), optarg);
  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. close(sd);
  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. }