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 */
13 #include "config.h"
14 #include "common.h"
15 #include "netutils.h"
16 #include "utils.h"
18 const char *progname = "check_ssh";
19 #define REVISION "$Revision$"
21 #ifndef MSG_DONTWAIT
22 #define MSG_DONTWAIT 0
23 #endif
25 #define SSH_DFL_PORT 22
26 #define BUFF_SZ 256
28 short port = -1;
29 char *server_name = NULL;
30 int verbose = FALSE;
32 int process_arguments (int, char **);
33 int validate_arguments (void);
34 void print_help (void);
35 void print_usage (void);
37 int ssh_connect (char *haddr, short hport);
39 int
40 main (int argc, char **argv)
41 {
42 int result;
44 if (process_arguments (argc, argv) == ERROR)
45 usage ("Could not parse arguments\n");
47 /* initialize alarm signal handling */
48 signal (SIGALRM, socket_timeout_alarm_handler);
49 alarm (socket_timeout);
51 /* ssh_connect exits if error is found */
52 result = ssh_connect (server_name, port);
54 alarm (0);
56 return (result);
57 }
60 /* process command-line arguments */
61 int
62 process_arguments (int argc, char **argv)
63 {
64 int c;
65 char *tmp = NULL;
67 int option_index = 0;
68 static struct option long_options[] = {
69 {"version", no_argument, 0, 'V'},
70 {"help", no_argument, 0, 'h'},
71 {"verbose", no_argument, 0, 'v'},
72 {"timeout", required_argument, 0, 't'},
73 {"host", required_argument, 0, 'H'},
74 {0, 0, 0, 0}
75 };
77 if (argc < 2)
78 return ERROR;
80 for (c = 1; c < argc; c++)
81 if (strcmp ("-to", argv[c]) == 0)
82 strcpy (argv[c], "-t");
84 while (1) {
85 c = getopt_long (argc, argv, "+Vhvt:H:p:", long_options, &option_index);
87 if (c == -1 || c == EOF)
88 break;
90 switch (c) {
91 case '?': /* help */
92 usage ("");
93 case 'V': /* version */
94 print_revision (progname, REVISION);
95 exit (STATE_OK);
96 case 'h': /* help */
97 print_help ();
98 exit (STATE_OK);
99 case 'v': /* verose */
100 verbose = TRUE;
101 break;
102 case 't': /* timeout period */
103 if (!is_integer (optarg))
104 usage ("Timeout Interval must be an integer!\n\n");
105 socket_timeout = atoi (optarg);
106 break;
107 case 'H': /* host */
108 if (is_host (optarg) == FALSE)
109 usage ("Invalid hostname/address\n");
110 server_name = optarg;
111 break;
112 case 'p': /* port */
113 if (is_intpos (optarg)) {
114 port = atoi (optarg);
115 }
116 else {
117 printf ("Port number nust be a positive integer: %s\n", optarg);
118 usage ("");
119 }
120 }
122 }
124 c = optind;
125 if (server_name == NULL && c < argc) {
126 if (is_host (argv[c])) {
127 server_name = argv[c++];
128 }
129 }
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 }
141 return validate_arguments ();
142 }
144 int
145 validate_arguments (void)
146 {
147 if (server_name == NULL)
148 return ERROR;
149 if (port == -1) /* funky, but allows -p to override stray integer in args */
150 port = SSH_DFL_PORT;
151 return OK;
152 }
155 /************************************************************************
156 *
157 * Try to connect to SSH server at specified server and port
158 *
159 *-----------------------------------------------------------------------*/
161 int
162 ssh_connect (char *haddr, short hport)
163 {
164 int sd;
165 int result;
166 char *output = NULL;
167 char *buffer = NULL;
168 char *ssh_proto = NULL;
169 char *ssh_server = NULL;
170 char revision[20];
172 sscanf ("$Revision$", "$Revision: %[0123456789.]", revision);
174 result = my_tcp_connect (haddr, hport, &sd);
176 if (result != STATE_OK)
177 return result;
179 output = (char *) malloc (BUFF_SZ + 1);
180 memset (output, 0, BUFF_SZ + 1);
181 recv (sd, output, BUFF_SZ, 0);
182 if (strncmp (output, "SSH", 3)) {
183 printf ("Server answer: %s", output);
184 exit (STATE_CRITICAL);
185 }
186 else {
187 strip (output);
188 if (verbose)
189 printf ("%s\n", output);
190 ssh_proto = output + 4;
191 ssh_server = ssh_proto + strspn (ssh_proto, "-0123456789. ");
192 ssh_proto[strspn (ssh_proto, "0123456789. ")] = 0;
193 printf
194 ("SSH OK - %s (protocol %s)\n",
195 ssh_server, ssh_proto);
196 asprintf (&buffer, "SSH-%s-check_ssh_%s\r\n", ssh_proto, revision);
197 send (sd, buffer, strlen (buffer), MSG_DONTWAIT);
198 if (verbose)
199 printf ("%s\n", buffer);
200 exit (STATE_OK);
201 }
202 }
204 void
205 print_help (void)
206 {
207 print_revision (progname, REVISION);
208 printf ("Copyright (c) 1999 Remi Paulmier (remi@sinfomic.fr)\n\n");
209 print_usage ();
210 printf ("by default, port is %d\n", SSH_DFL_PORT);
211 }
213 void
214 print_usage (void)
215 {
216 printf
217 ("Usage:\n"
218 " %s -t [timeout] -p [port] <host>\n"
219 " %s -V prints version info\n"
220 " %s -h prints more detailed help\n", progname, progname, progname);
221 }
223 /* end of check_ssh.c */