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 ******************************************************************************/
19 #include "common.h"
20 #include "netutils.h"
21 #include "utils.h"
23 const char *progname = "check_ssh";
24 const char *revision = "$Revision$";
25 const char *copyright = "2000-2003";
26 const char *email = "nagiosplug-devel@lists.sourceforge.net";
28 #ifndef MSG_DONTWAIT
29 #define MSG_DONTWAIT 0
30 #endif
32 #define SSH_DFL_PORT 22
33 #define BUFF_SZ 256
35 short port = -1;
36 char *server_name = NULL;
37 int verbose = FALSE;
39 int process_arguments (int, char **);
40 int validate_arguments (void);
41 void print_help (void);
42 void print_usage (void);
44 int ssh_connect (char *haddr, short hport);
46 int
47 main (int argc, char **argv)
48 {
49 int result;
51 if (process_arguments (argc, argv) == ERROR)
52 usage (_("Could not parse arguments\n"));
54 /* initialize alarm signal handling */
55 signal (SIGALRM, socket_timeout_alarm_handler);
56 alarm (socket_timeout);
58 /* ssh_connect exits if error is found */
59 result = ssh_connect (server_name, port);
61 alarm (0);
63 return (result);
64 }
67 /* process command-line arguments */
68 int
69 process_arguments (int argc, char **argv)
70 {
71 int c;
73 int option_index = 0;
74 static struct option long_options[] = {
75 {"help", no_argument, 0, 'h'},
76 {"version", no_argument, 0, 'V'},
77 {"host", required_argument, 0, 'H'},
78 {"port", required_argument, 0, 'p'},
79 {"use-ipv4", no_argument, 0, '4'},
80 {"use-ipv6", no_argument, 0, '6'},
81 {"timeout", required_argument, 0, 't'},
82 {"verbose", no_argument, 0, 'v'},
83 {0, 0, 0, 0}
84 };
86 if (argc < 2)
87 return ERROR;
89 for (c = 1; c < argc; c++)
90 if (strcmp ("-to", argv[c]) == 0)
91 strcpy (argv[c], "-t");
93 while (1) {
94 c = getopt_long (argc, argv, "+Vhv46t:H:p:", long_options, &option_index);
96 if (c == -1 || c == EOF)
97 break;
99 switch (c) {
100 case '?': /* help */
101 usage ("");
102 case 'V': /* version */
103 print_revision (progname, revision);
104 exit (STATE_OK);
105 case 'h': /* help */
106 print_help ();
107 exit (STATE_OK);
108 case 'v': /* verose */
109 verbose = TRUE;
110 break;
111 case 't': /* timeout period */
112 if (!is_integer (optarg))
113 usage (_("Timeout Interval must be an integer!\n\n"));
114 socket_timeout = atoi (optarg);
115 break;
116 case '4':
117 address_family = AF_INET;
118 break;
119 case '6':
120 #ifdef USE_IPV6
121 address_family = AF_INET6;
122 #else
123 usage (_("IPv6 support not available\n"));
124 #endif
125 break;
126 case 'H': /* host */
127 if (is_host (optarg) == FALSE)
128 usage ("Invalid hostname/address\n");
129 server_name = optarg;
130 break;
131 case 'p': /* port */
132 if (is_intpos (optarg)) {
133 port = atoi (optarg);
134 }
135 else {
136 printf ("Port number nust be a positive integer: %s\n", optarg);
137 usage ("");
138 }
139 }
141 }
143 c = optind;
144 if (server_name == NULL && c < argc) {
145 if (is_host (argv[c])) {
146 server_name = argv[c++];
147 }
148 }
150 if (port == -1 && c < argc) {
151 if (is_intpos (argv[c])) {
152 port = atoi (argv[c++]);
153 }
154 else {
155 print_usage ();
156 exit (STATE_UNKNOWN);
157 }
158 }
160 return validate_arguments ();
161 }
163 int
164 validate_arguments (void)
165 {
166 if (server_name == NULL)
167 return ERROR;
168 if (port == -1) /* funky, but allows -p to override stray integer in args */
169 port = SSH_DFL_PORT;
170 return OK;
171 }
174 /************************************************************************
175 *
176 * Try to connect to SSH server at specified server and port
177 *
178 *-----------------------------------------------------------------------*/
180 int
181 ssh_connect (char *haddr, short hport)
182 {
183 int sd;
184 int result;
185 char *output = NULL;
186 char *buffer = NULL;
187 char *ssh_proto = NULL;
188 char *ssh_server = NULL;
189 char rev_no[20];
191 sscanf ("$Revision$", "$Revision: %[0123456789.]", rev_no);
193 result = my_tcp_connect (haddr, hport, &sd);
195 if (result != STATE_OK)
196 return result;
198 output = (char *) malloc (BUFF_SZ + 1);
199 memset (output, 0, BUFF_SZ + 1);
200 recv (sd, output, BUFF_SZ, 0);
201 if (strncmp (output, "SSH", 3)) {
202 printf (_("Server answer: %s"), output);
203 exit (STATE_CRITICAL);
204 }
205 else {
206 strip (output);
207 if (verbose)
208 printf ("%s\n", output);
209 ssh_proto = output + 4;
210 ssh_server = ssh_proto + strspn (ssh_proto, "-0123456789. ");
211 ssh_proto[strspn (ssh_proto, "0123456789. ")] = 0;
212 printf
213 (_("SSH OK - %s (protocol %s)\n"),
214 ssh_server, ssh_proto);
215 asprintf (&buffer, "SSH-%s-check_ssh_%s\r\n", ssh_proto, rev_no);
216 send (sd, buffer, strlen (buffer), MSG_DONTWAIT);
217 if (verbose)
218 printf ("%s\n", buffer);
219 exit (STATE_OK);
220 }
221 }
223 void
224 print_help (void)
225 {
226 char *myport;
227 asprintf (&myport, "%d", SSH_DFL_PORT);
229 print_revision (progname, revision);
231 printf (_("Copyright (c) 1999 Remi Paulmier <remi@sinfomic.fr>\n"));
232 printf (_(COPYRIGHT), copyright, email);
234 printf (_("Try to connect to SSH server at specified server and port\n\n"));
236 print_usage ();
238 printf (_(UT_HELP_VRSN));
240 printf (_(UT_HOST_PORT), 'p', myport);
242 printf (_(UT_IPv46));
244 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
246 printf (_(UT_VERBOSE));
248 printf (_(UT_SUPPORT));
249 }
251 void
252 print_usage (void)
253 {
254 printf (_("\
255 Usage: %s [-46] [-t <timeout>] [-p <port>] <host>\n"), progname);
256 printf (_(UT_HLP_VRS), progname, progname);
257 }
259 /* end of check_ssh.c */