check_ssh.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. #ifdef HAVE_GETOPT_H
  52. int option_index = 0;
  53. static struct option long_options[] = {
  54. {"version", no_argument, 0, 'V'},
  55. {"help", no_argument, 0, 'h'},
  56. {"verbose", no_argument, 0, 'v'},
  57. {"timeout", required_argument, 0, 't'},
  58. {"host", required_argument, 0, 'H'},
  59. {0, 0, 0, 0}
  60. };
  61. #endif
  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. #ifdef HAVE_GETOPT_H
  69. c = getopt_long (argc, argv, "+Vhvt:H:p:", long_options, &option_index);
  70. #else
  71. c = getopt (argc, argv, "+Vhvt:H:p:");
  72. #endif
  73. if (c == -1 || c == EOF)
  74. break;
  75. switch (c) {
  76. case '?': /* help */
  77. usage ("");
  78. case 'V': /* version */
  79. print_revision (progname, REVISION);
  80. exit (STATE_OK);
  81. case 'h': /* help */
  82. print_help ();
  83. exit (STATE_OK);
  84. case 'v': /* verose */
  85. verbose = TRUE;
  86. break;
  87. case 't': /* timeout period */
  88. if (!is_integer (optarg))
  89. usage ("Timeout Interval must be an integer!\n\n");
  90. socket_timeout = atoi (optarg);
  91. break;
  92. case 'H': /* host */
  93. if (is_host (optarg) == FALSE)
  94. usage ("Invalid hostname/address\n");
  95. server_name = optarg;
  96. break;
  97. case 'p': /* port */
  98. if (is_intpos (optarg)) {
  99. port = atoi (optarg);
  100. }
  101. else {
  102. printf ("Port number nust be a positive integer: %s\n", optarg);
  103. usage ("");
  104. }
  105. }
  106. }
  107. c = optind;
  108. if (server_name == NULL && c < argc) {
  109. if (is_host (argv[c])) {
  110. server_name = argv[c++];
  111. }
  112. }
  113. if (port == -1 && c < argc) {
  114. if (is_intpos (argv[c])) {
  115. port = atoi (argv[c++]);
  116. }
  117. else {
  118. print_usage ();
  119. exit (STATE_UNKNOWN);
  120. }
  121. }
  122. return validate_arguments ();
  123. }
  124. int
  125. validate_arguments (void)
  126. {
  127. if (server_name == NULL)
  128. return ERROR;
  129. if (port == -1) /* funky, but allows -p to override stray integer in args */
  130. port = SSH_DFL_PORT;
  131. return OK;
  132. }
  133. /************************************************************************
  134. *
  135. * Try to connect to SSH server at specified server and port
  136. *
  137. *-----------------------------------------------------------------------*/
  138. int
  139. ssh_connect (char *haddr, short hport)
  140. {
  141. int sd;
  142. int result;
  143. char *output = NULL;
  144. char *buffer = NULL;
  145. char *ssh_proto = NULL;
  146. char *ssh_server = NULL;
  147. char revision[20];
  148. sscanf ("$Revision$", "$Revision: %[0123456789.]", revision);
  149. result = my_tcp_connect (haddr, hport, &sd);
  150. if (result != STATE_OK)
  151. return result;
  152. output = (char *) malloc (BUFF_SZ + 1);
  153. memset (output, 0, BUFF_SZ + 1);
  154. recv (sd, output, BUFF_SZ, 0);
  155. if (strncmp (output, "SSH", 3)) {
  156. printf ("Server answer: %s", output);
  157. exit (STATE_CRITICAL);
  158. }
  159. else {
  160. strip (output);
  161. if (verbose)
  162. printf ("%s\n", output);
  163. ssh_proto = output + 4;
  164. ssh_server = ssh_proto + strspn (ssh_proto, "-0123456789. ");
  165. ssh_proto[strspn (ssh_proto, "0123456789. ")] = 0;
  166. printf
  167. ("SSH OK - %s (protocol %s)\n",
  168. ssh_server, ssh_proto);
  169. asprintf (&buffer, "SSH-%s-check_ssh_%s\r\n", ssh_proto, revision);
  170. send (sd, buffer, strlen (buffer), MSG_DONTWAIT);
  171. if (verbose)
  172. printf ("%s\n", buffer);
  173. exit (STATE_OK);
  174. }
  175. }
  176. void
  177. print_help (void)
  178. {
  179. print_revision (progname, REVISION);
  180. printf ("Copyright (c) 1999 Remi Paulmier (remi@sinfomic.fr)\n\n");
  181. print_usage ();
  182. printf ("by default, port is %d\n", SSH_DFL_PORT);
  183. }
  184. void
  185. print_usage (void)
  186. {
  187. printf
  188. ("Usage:\n"
  189. " %s -t [timeout] -p [port] <host>\n"
  190. " %s -V prints version info\n"
  191. " %s -h prints more detailed help\n", progname, progname, progname);
  192. }
  193. /* end of check_ssh.c */