Code

properly call close() on the ssh connection before exiting.
[nagiosplug.git] / plugins / check_ssh.c
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$
18  
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);
65         
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                         usage2 (_("Unknown argument"), optarg);
114                 case 'V':                                                                       /* version */
115                         print_revision (progname, revision);
116                         exit (STATE_OK);
117                 case 'h':                                                                       /* help */
118                         print_help ();
119                         exit (STATE_OK);
120                 case 'v':                                                                       /* verbose */
121                         verbose = TRUE;
122                         break;
123                 case 't':                                                                       /* timeout period */
124                         if (!is_integer (optarg))
125                                 usage2 (_("Timeout interval must be a positive integer"), optarg);
126                         else
127                                 socket_timeout = atoi (optarg);
128                         break;
129                 case '4':
130                         address_family = AF_INET;
131                         break;
132                 case '6':
133 #ifdef USE_IPV6
134                         address_family = AF_INET6;
135 #else
136                         usage4 (_("IPv6 support not available"));
137 #endif
138                         break;
139                 case 'r':                                                                       /* remote version */
140                         remote_version = optarg;
141                         break;
142                 case 'H':                                                                       /* host */
143                         if (is_host (optarg) == FALSE)
144                                 usage2 (_("Invalid hostname/address"), optarg);
145                         server_name = optarg;
146                         break;
147                 case 'p':                                                                       /* port */
148                         if (is_intpos (optarg)) {
149                                 port = atoi (optarg);
150                         }
151                         else {
152                                 usage2 (_("Port number must be a positive integer"), optarg);
153                         }
154                 }
155         }
157         c = optind;
158         if (server_name == NULL && c < argc) {
159                 if (is_host (argv[c])) {
160                         server_name = argv[c++];
161                 }
162         }
164         if (port == -1 && c < argc) {
165                 if (is_intpos (argv[c])) {
166                         port = atoi (argv[c++]);
167                 }
168                 else {
169                         print_usage ();
170                         exit (STATE_UNKNOWN);
171                 }
172         }
174         return validate_arguments ();
177 int
178 validate_arguments (void)
180         if (server_name == NULL)
181                 return ERROR;
182         if (port == -1)                                                         /* funky, but allows -p to override stray integer in args */
183                 port = SSH_DFL_PORT;
184         return OK;
188 /************************************************************************
190 * Try to connect to SSH server at specified server and port
192 *-----------------------------------------------------------------------*/
195 int
196 ssh_connect (char *haddr, int hport, char *remote_version)
198         int sd;
199         int result;
200         char *output = NULL;
201         char *buffer = NULL;
202         char *ssh_proto = NULL;
203         char *ssh_server = NULL;
204         char rev_no[20];
206         sscanf ("$Revision$", "$Revision: %[0123456789.]", rev_no);
208         result = my_tcp_connect (haddr, hport, &sd);
210         if (result != STATE_OK)
211                 return result;
213         output = (char *) malloc (BUFF_SZ + 1);
214         memset (output, 0, BUFF_SZ + 1);
215         recv (sd, output, BUFF_SZ, 0);
216         if (strncmp (output, "SSH", 3)) {
217                 printf (_("Server answer: %s"), output);
218                 exit (STATE_CRITICAL);
219         }
220         else {
221                 strip (output);
222                 if (verbose)
223                         printf ("%s\n", output);
224                 ssh_proto = output + 4;
225                 ssh_server = ssh_proto + strspn (ssh_proto, "-0123456789. ");
226                 ssh_proto[strspn (ssh_proto, "0123456789. ")] = 0;
228                 asprintf (&buffer, "SSH-%s-check_ssh_%s\r\n", ssh_proto, rev_no);
229                 send (sd, buffer, strlen (buffer), MSG_DONTWAIT);
230                 if (verbose)
231                         printf ("%s\n", buffer);
233                 if (remote_version && strcmp(remote_version, ssh_server)) {
234                         printf
235                                 (_("SSH WARNING - %s (protocol %s) version mismatch, expected '%s'\n"),
236                                  ssh_server, ssh_proto, remote_version);
237                         exit (STATE_WARNING);
238                 }
239                 
240                 printf
241                         (_("SSH OK - %s (protocol %s)\n"),
242                          ssh_server, ssh_proto);
243                 close(sd);
244                 exit (STATE_OK);
245         }
250 void
251 print_help (void)
253         char *myport;
254         asprintf (&myport, "%d", SSH_DFL_PORT);
256         print_revision (progname, revision);
258         printf ("Copyright (c) 1999 Remi Paulmier <remi@sinfomic.fr>\n");
259         printf (COPYRIGHT, copyright, email);
261         printf (_("Try to connect to an SSH server at specified server and port\n\n"));
263         print_usage ();
265         printf (_(UT_HELP_VRSN));
267         printf (_(UT_HOST_PORT), 'p', myport);
269         printf (_(UT_IPv46));
271         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
273         printf (_("\
274  -r, --remote-version=STRING\n\
275     Warn if string doesn't match expected server version (ex: OpenSSH_3.9p1)\n"));
276         
277         printf (_(UT_VERBOSE));
279         printf (_(UT_SUPPORT));
284 void
285 print_usage (void)
287         printf ("\
288 Usage: %s [-46] [-t <timeout>] [-r <remote version>] [-p <port>] <host>\n", progname);