1 /******************************************************************************
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2 of the License, or
6 (at your option) any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 $Id$
19 ******************************************************************************/
21 const char *progname = "check_ssh";
22 const char *revision = "$Revision$";
23 const char *copyright = "2000-2004";
24 const char *email = "nagiosplug-devel@lists.sourceforge.net";
26 #include "common.h"
27 #include "netutils.h"
28 #include "utils.h"
30 #ifndef MSG_DONTWAIT
31 #define MSG_DONTWAIT 0
32 #endif
34 #define SSH_DFL_PORT 22
35 #define BUFF_SZ 256
37 int port = -1;
38 char *server_name = NULL;
39 char *remote_version = NULL;
40 int verbose = FALSE;
42 int process_arguments (int, char **);
43 int validate_arguments (void);
44 void print_help (void);
45 void print_usage (void);
47 int ssh_connect (char *haddr, int hport, char *remote_version);
51 int
52 main (int argc, char **argv)
53 {
54 int result = STATE_UNKNOWN;
56 setlocale (LC_ALL, "");
57 bindtextdomain (PACKAGE, LOCALEDIR);
58 textdomain (PACKAGE);
60 if (process_arguments (argc, argv) == ERROR)
61 usage4 (_("Could not parse arguments"));
63 /* initialize alarm signal handling */
64 signal (SIGALRM, socket_timeout_alarm_handler);
66 alarm (socket_timeout);
68 /* ssh_connect exits if error is found */
69 result = ssh_connect (server_name, port, remote_version);
71 alarm (0);
73 return (result);
74 }
78 /* process command-line arguments */
79 int
80 process_arguments (int argc, char **argv)
81 {
82 int c;
84 int option = 0;
85 static struct option longopts[] = {
86 {"help", no_argument, 0, 'h'},
87 {"version", no_argument, 0, 'V'},
88 {"host", required_argument, 0, 'H'},
89 {"port", required_argument, 0, 'p'},
90 {"use-ipv4", no_argument, 0, '4'},
91 {"use-ipv6", no_argument, 0, '6'},
92 {"timeout", required_argument, 0, 't'},
93 {"verbose", no_argument, 0, 'v'},
94 {"remote-version", required_argument, 0, 'r'},
95 {0, 0, 0, 0}
96 };
98 if (argc < 2)
99 return ERROR;
101 for (c = 1; c < argc; c++)
102 if (strcmp ("-to", argv[c]) == 0)
103 strcpy (argv[c], "-t");
105 while (1) {
106 c = getopt_long (argc, argv, "+Vhv46t:r:H:p:", longopts, &option);
108 if (c == -1 || c == EOF)
109 break;
111 switch (c) {
112 case '?': /* help */
113 printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
114 print_usage ();
115 exit (STATE_UNKNOWN);
116 case 'V': /* version */
117 print_revision (progname, revision);
118 exit (STATE_OK);
119 case 'h': /* help */
120 print_help ();
121 exit (STATE_OK);
122 case 'v': /* verbose */
123 verbose = TRUE;
124 break;
125 case 't': /* timeout period */
126 if (!is_integer (optarg))
127 usage2 (_("Timeout interval must be a positive integer"), optarg);
128 else
129 socket_timeout = atoi (optarg);
130 break;
131 case '4':
132 address_family = AF_INET;
133 break;
134 case '6':
135 #ifdef USE_IPV6
136 address_family = AF_INET6;
137 #else
138 usage (_("IPv6 support not available\n"));
139 #endif
140 break;
141 case 'r': /* remote version */
142 remote_version = optarg;
143 break;
144 case 'H': /* host */
145 if (is_host (optarg) == FALSE)
146 usage2 (_("Invalid hostname/address"), optarg);
147 server_name = optarg;
148 break;
149 case 'p': /* port */
150 if (is_intpos (optarg)) {
151 port = atoi (optarg);
152 }
153 else {
154 printf ("Port number nust be a positive integer: %s\n", optarg);
155 usage ("");
156 }
157 }
159 }
161 c = optind;
162 if (server_name == NULL && c < argc) {
163 if (is_host (argv[c])) {
164 server_name = argv[c++];
165 }
166 }
168 if (port == -1 && c < argc) {
169 if (is_intpos (argv[c])) {
170 port = atoi (argv[c++]);
171 }
172 else {
173 print_usage ();
174 exit (STATE_UNKNOWN);
175 }
176 }
178 return validate_arguments ();
179 }
181 int
182 validate_arguments (void)
183 {
184 if (server_name == NULL)
185 return ERROR;
186 if (port == -1) /* funky, but allows -p to override stray integer in args */
187 port = SSH_DFL_PORT;
188 return OK;
189 }
192 /************************************************************************
193 *
194 * Try to connect to SSH server at specified server and port
195 *
196 *-----------------------------------------------------------------------*/
199 int
200 ssh_connect (char *haddr, int hport, char *remote_version)
201 {
202 int sd;
203 int result;
204 char *output = NULL;
205 char *buffer = NULL;
206 char *ssh_proto = NULL;
207 char *ssh_server = NULL;
208 char rev_no[20];
210 sscanf ("$Revision$", "$Revision: %[0123456789.]", rev_no);
212 result = my_tcp_connect (haddr, hport, &sd);
214 if (result != STATE_OK)
215 return result;
217 output = (char *) malloc (BUFF_SZ + 1);
218 memset (output, 0, BUFF_SZ + 1);
219 recv (sd, output, BUFF_SZ, 0);
220 if (strncmp (output, "SSH", 3)) {
221 printf (_("Server answer: %s"), output);
222 exit (STATE_CRITICAL);
223 }
224 else {
225 strip (output);
226 if (verbose)
227 printf ("%s\n", output);
228 ssh_proto = output + 4;
229 ssh_server = ssh_proto + strspn (ssh_proto, "-0123456789. ");
230 ssh_proto[strspn (ssh_proto, "0123456789. ")] = 0;
232 asprintf (&buffer, "SSH-%s-check_ssh_%s\r\n", ssh_proto, rev_no);
233 send (sd, buffer, strlen (buffer), MSG_DONTWAIT);
234 if (verbose)
235 printf ("%s\n", buffer);
237 if (remote_version && strcmp(remote_version, ssh_server)) {
238 printf
239 (_("SSH WARNING - %s (protocol %s) version mismatch, expected '%s'\n"),
240 ssh_server, ssh_proto, remote_version);
241 exit (STATE_WARNING);
242 }
244 printf
245 (_("SSH OK - %s (protocol %s)\n"),
246 ssh_server, ssh_proto);
247 exit (STATE_OK);
248 }
249 }
253 void
254 print_help (void)
255 {
256 char *myport;
257 asprintf (&myport, "%d", SSH_DFL_PORT);
259 print_revision (progname, revision);
261 printf ("Copyright (c) 1999 Remi Paulmier <remi@sinfomic.fr>\n");
262 printf (COPYRIGHT, copyright, email);
264 printf (_("Try to connect to an SSH server at specified server and port\n\n"));
266 print_usage ();
268 printf (_(UT_HELP_VRSN));
270 printf (_(UT_HOST_PORT), 'p', myport);
272 printf (_(UT_IPv46));
274 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
276 printf (_("\
277 -r, --remote-version=STRING\n\
278 Warn if string doesn't match expected server version (ex: OpenSSH_3.9p1)\n"));
280 printf (_(UT_VERBOSE));
282 printf (_(UT_SUPPORT));
283 }
287 void
288 print_usage (void)
289 {
290 printf ("\
291 Usage: %s [-46] [-t <timeout>] [-r <remote version>] [-p <port>] <host>\n", progname);
292 }