Code

79e17017ba24755631d0891df01336fc81acd13c
[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 ******************************************************************************/
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 int 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, int hport);
46 int
47 main (int argc, char **argv)
48 {
49         int result;
51         setlocale (LC_ALL, "");
52         bindtextdomain (PACKAGE, LOCALEDIR);
53         textdomain (PACKAGE);
55         if (process_arguments (argc, argv) == ERROR)
56                 usage (_("Could not parse arguments\n"));
58         /* initialize alarm signal handling */
59         signal (SIGALRM, socket_timeout_alarm_handler);
60         alarm (socket_timeout);
62         /* ssh_connect exits if error is found */
63         result = ssh_connect (server_name, port);
65         alarm (0);
67         return (result);
68 }
71 /* process command-line arguments */
72 int
73 process_arguments (int argc, char **argv)
74 {
75         int c;
77         int option = 0;
78         static struct option longopts[] = {
79                 {"help", no_argument, 0, 'h'},
80                 {"version", no_argument, 0, 'V'},
81                 {"host", required_argument, 0, 'H'},
82                 {"port", required_argument, 0, 'p'},
83                 {"use-ipv4", no_argument, 0, '4'},
84                 {"use-ipv6", no_argument, 0, '6'},
85                 {"timeout", required_argument, 0, 't'},
86                 {"verbose", no_argument, 0, 'v'},
87                 {0, 0, 0, 0}
88         };
90         if (argc < 2)
91                 return ERROR;
93         for (c = 1; c < argc; c++)
94                 if (strcmp ("-to", argv[c]) == 0)
95                         strcpy (argv[c], "-t");
97         while (1) {
98                 c = getopt_long (argc, argv, "+Vhv46t:H:p:", longopts, &option);
100                 if (c == -1 || c == EOF)
101                         break;
103                 switch (c) {
104                 case '?':                                                                       /* help */
105                         usage ("");
106                 case 'V':                                                                       /* version */
107                         print_revision (progname, revision);
108                         exit (STATE_OK);
109                 case 'h':                                                                       /* help */
110                         print_help ();
111                         exit (STATE_OK);
112                 case 'v':                                                                       /* verose */
113                         verbose = TRUE;
114                         break;
115                 case 't':                                                                       /* timeout period */
116                         if (!is_integer (optarg))
117                                 usage (_("Timeout Interval must be an integer!\n\n"));
118                         else
119                                 socket_timeout = atoi (optarg);
120                         break;
121                 case '4':
122                         address_family = AF_INET;
123                         break;
124                 case '6':
125 #ifdef USE_IPV6
126                         address_family = AF_INET6;
127 #else
128                         usage (_("IPv6 support not available\n"));
129 #endif
130                         break;
131                 case 'H':                                                                       /* host */
132                         if (is_host (optarg) == FALSE)
133                                 usage ("Invalid hostname/address\n");
134                         server_name = optarg;
135                         break;
136                 case 'p':                                                                       /* port */
137                         if (is_intpos (optarg)) {
138                                 port = atoi (optarg);
139                         }
140                         else {
141                                 printf ("Port number nust be a positive integer: %s\n", optarg);
142                                 usage ("");
143                         }
144                 }
146         }
148         c = optind;
149         if (server_name == NULL && c < argc) {
150                 if (is_host (argv[c])) {
151                         server_name = argv[c++];
152                 }
153         }
155         if (port == -1 && c < argc) {
156                 if (is_intpos (argv[c])) {
157                         port = atoi (argv[c++]);
158                 }
159                 else {
160                         print_usage ();
161                         exit (STATE_UNKNOWN);
162                 }
163         }
165         return validate_arguments ();
168 int
169 validate_arguments (void)
171         if (server_name == NULL)
172                 return ERROR;
173         if (port == -1)                                                         /* funky, but allows -p to override stray integer in args */
174                 port = SSH_DFL_PORT;
175         return OK;
179 /************************************************************************
181 * Try to connect to SSH server at specified server and port
183 *-----------------------------------------------------------------------*/
185 int
186 ssh_connect (char *haddr, int hport)
188         int sd;
189         int result;
190         char *output = NULL;
191         char *buffer = NULL;
192         char *ssh_proto = NULL;
193         char *ssh_server = NULL;
194         char rev_no[20];
196         sscanf ("$Revision$", "$Revision: %[0123456789.]", rev_no);
198         result = my_tcp_connect (haddr, hport, &sd);
200         if (result != STATE_OK)
201                 return result;
203         output = (char *) malloc (BUFF_SZ + 1);
204         memset (output, 0, BUFF_SZ + 1);
205         recv (sd, output, BUFF_SZ, 0);
206         if (strncmp (output, "SSH", 3)) {
207                 printf (_("Server answer: %s"), output);
208                 exit (STATE_CRITICAL);
209         }
210         else {
211                 strip (output);
212                 if (verbose)
213                         printf ("%s\n", output);
214                 ssh_proto = output + 4;
215                 ssh_server = ssh_proto + strspn (ssh_proto, "-0123456789. ");
216                 ssh_proto[strspn (ssh_proto, "0123456789. ")] = 0;
217                 printf
218                         (_("SSH OK - %s (protocol %s)\n"),
219                          ssh_server, ssh_proto);
220                 asprintf (&buffer, "SSH-%s-check_ssh_%s\r\n", ssh_proto, rev_no);
221                 send (sd, buffer, strlen (buffer), MSG_DONTWAIT);
222                 if (verbose)
223                         printf ("%s\n", buffer);
224                 exit (STATE_OK);
225         }
228 void
229 print_help (void)
231         char *myport;
232         asprintf (&myport, "%d", SSH_DFL_PORT);
234         print_revision (progname, revision);
236         printf (_("Copyright (c) 1999 Remi Paulmier <remi@sinfomic.fr>\n"));
237         printf (_(COPYRIGHT), copyright, email);
239         printf (_("Try to connect to SSH server at specified server and port\n\n"));
241         print_usage ();
243         printf (_(UT_HELP_VRSN));
245         printf (_(UT_HOST_PORT), 'p', myport);
247         printf (_(UT_IPv46));
249         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
251         printf (_(UT_VERBOSE));
253         printf (_(UT_SUPPORT));
256 void
257 print_usage (void)
259         printf (_("\
260 Usage: %s [-46] [-t <timeout>] [-p <port>] <host>\n"), progname);
261         printf (_(UT_HLP_VRS), progname, progname);
264 /* end of check_ssh.c */