check_ssh.c 4.8 KB

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