Code

changed Error: by CRITICAL -
[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 #include "common.h"
22 #include "netutils.h"
23 #include "utils.h"
25 const char *progname = "check_ssh";
26 const char *revision = "$Revision$";
27 const char *copyright = "2000-2003";
28 const char *email = "nagiosplug-devel@lists.sourceforge.net";
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;
56         setlocale (LC_ALL, "");
57         bindtextdomain (PACKAGE, LOCALEDIR);
58         textdomain (PACKAGE);
60         if (process_arguments (argc, argv) != OK)
61                 usage (_("check_ssh: could not parse arguments\n"));
63         /* initialize alarm signal handling */
64         signal (SIGALRM, socket_timeout_alarm_handler);
65         alarm (socket_timeout);
67         /* ssh_connect exits if error is found */
68         result = ssh_connect (server_name, port, remote_version);
70         alarm (0);
72         return (result);
73 }
77 /* process command-line arguments */
78 int
79 process_arguments (int argc, char **argv)
80 {
81         int c;
83         int option = 0;
84         static struct option longopts[] = {
85                 {"help", no_argument, 0, 'h'},
86                 {"version", no_argument, 0, 'V'},
87                 {"host", required_argument, 0, 'H'},
88                 {"port", required_argument, 0, 'p'},
89                 {"use-ipv4", no_argument, 0, '4'},
90                 {"use-ipv6", no_argument, 0, '6'},
91                 {"timeout", required_argument, 0, 't'},
92                 {"verbose", no_argument, 0, 'v'},
93                 {"remote-version", required_argument, 0, 'r'},
94                 {0, 0, 0, 0}
95         };
97         if (argc < 2)
98                 return ERROR;
100         for (c = 1; c < argc; c++)
101                 if (strcmp ("-to", argv[c]) == 0)
102                         strcpy (argv[c], "-t");
104         while (1) {
105                 c = getopt_long (argc, argv, "+Vhv46t:r:H:p:", longopts, &option);
107                 if (c == -1 || c == EOF)
108                         break;
110                 switch (c) {
111                 case '?':                                                                       /* help */
112                         printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
113                         print_usage ();
114                         exit (STATE_UNKNOWN);
115                 case 'V':                                                                       /* version */
116                         print_revision (progname, revision);
117                         exit (STATE_OK);
118                 case 'h':                                                                       /* help */
119                         print_help ();
120                         exit (STATE_OK);
121                 case 'v':                                                                       /* verbose */
122                         verbose = TRUE;
123                         break;
124                 case 't':                                                                       /* timeout period */
125                         if (!is_integer (optarg))
126                                 usage2 (_("Timeout interval must be a positive integer"), optarg);
127                         else
128                                 socket_timeout = atoi (optarg);
129                         break;
130                 case '4':
131                         address_family = AF_INET;
132                         break;
133                 case '6':
134 #ifdef USE_IPV6
135                         address_family = AF_INET6;
136 #else
137                         usage (_("IPv6 support not available\n"));
138 #endif
139                         break;
140                 case 'r':                                                                       /* remote version */
141                         remote_version = optarg;
142                         break;
143                 case 'H':                                                                       /* host */
144                         if (is_host (optarg) == FALSE)
145                                 usage2 (_("Invalid hostname/address"), optarg);
146                         server_name = optarg;
147                         break;
148                 case 'p':                                                                       /* port */
149                         if (is_intpos (optarg)) {
150                                 port = atoi (optarg);
151                         }
152                         else {
153                                 printf ("Port number nust be a positive integer: %s\n", optarg);
154                                 usage ("");
155                         }
156                 }
158         }
160         c = optind;
161         if (server_name == NULL && c < argc) {
162                 if (is_host (argv[c])) {
163                         server_name = argv[c++];
164                 }
165         }
167         if (port == -1 && c < argc) {
168                 if (is_intpos (argv[c])) {
169                         port = atoi (argv[c++]);
170                 }
171                 else {
172                         print_usage ();
173                         exit (STATE_UNKNOWN);
174                 }
175         }
177         return validate_arguments ();
180 int
181 validate_arguments (void)
183         if (server_name == NULL)
184                 return ERROR;
185         if (port == -1)                                                         /* funky, but allows -p to override stray integer in args */
186                 port = SSH_DFL_PORT;
187         return OK;
191 /************************************************************************
193 * Try to connect to SSH server at specified server and port
195 *-----------------------------------------------------------------------*/
198 int
199 ssh_connect (char *haddr, int hport, char *remote_version)
201         int sd;
202         int result;
203         char *output = NULL;
204         char *buffer = NULL;
205         char *ssh_proto = NULL;
206         char *ssh_server = NULL;
207         char rev_no[20];
209         sscanf ("$Revision$", "$Revision: %[0123456789.]", rev_no);
211         result = my_tcp_connect (haddr, hport, &sd);
213         if (result != STATE_OK)
214                 return result;
216         output = (char *) malloc (BUFF_SZ + 1);
217         memset (output, 0, BUFF_SZ + 1);
218         recv (sd, output, BUFF_SZ, 0);
219         if (strncmp (output, "SSH", 3)) {
220                 printf (_("Server answer: %s"), output);
221                 exit (STATE_CRITICAL);
222         }
223         else {
224                 strip (output);
225                 if (verbose)
226                         printf ("%s\n", output);
227                 ssh_proto = output + 4;
228                 ssh_server = ssh_proto + strspn (ssh_proto, "-0123456789. ");
229                 ssh_proto[strspn (ssh_proto, "0123456789. ")] = 0;
231                 asprintf (&buffer, "SSH-%s-check_ssh_%s\r\n", ssh_proto, rev_no);
232                 send (sd, buffer, strlen (buffer), MSG_DONTWAIT);
233                 if (verbose)
234                         printf ("%s\n", buffer);
236                 if (remote_version && strcmp(remote_version, ssh_server)) {
237                         printf
238                                 (_("SSH WARNING - %s (protocol %s) version mismatch, expected '%s'\n"),
239                                  ssh_server, ssh_proto, remote_version);
240                         exit (STATE_WARNING);
241                 }
242                 
243                 printf
244                         (_("SSH OK - %s (protocol %s)\n"),
245                          ssh_server, ssh_proto);
246                 exit (STATE_OK);
247         }
252 void
253 print_help (void)
255         char *myport;
256         asprintf (&myport, "%d", SSH_DFL_PORT);
258         print_revision (progname, revision);
260         printf ("Copyright (c) 1999 Remi Paulmier <remi@sinfomic.fr>\n");
261         printf (COPYRIGHT, copyright, email);
263         printf (_("Try to connect to an SSH server at specified server and port\n\n"));
265         print_usage ();
267         printf (_(UT_HELP_VRSN));
269         printf (_(UT_HOST_PORT), 'p', myport);
271         printf (_(UT_IPv46));
273         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
275         printf (_("\
276  -r, --remote-version=STRING\n\
277     Warn if string doesn't match expected server version (ex: OpenSSH_3.9p1)\n"));
278         
279         printf (_(UT_VERBOSE));
281         printf (_(UT_SUPPORT));
286 void
287 print_usage (void)
289         printf (_("\
290 Usage: %s [-46] [-t <timeout>] [-r <remote version>] [-p <port>] <host>\n"), progname);
291         printf (_(UT_HLP_VRS), progname, progname);
294 /* end of check_ssh.c */