check_ssh.c 4.5 KB

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