Code

the last round of pedantic compiler warnings
[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         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 = 0;
74         static struct option longopts[] = {
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:", longopts, &option);
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                         else
115                                 socket_timeout = atoi (optarg);
116                         break;
117                 case '4':
118                         address_family = AF_INET;
119                         break;
120                 case '6':
121 #ifdef USE_IPV6
122                         address_family = AF_INET6;
123 #else
124                         usage (_("IPv6 support not available\n"));
125 #endif
126                         break;
127                 case 'H':                                                                       /* host */
128                         if (is_host (optarg) == FALSE)
129                                 usage ("Invalid hostname/address\n");
130                         server_name = optarg;
131                         break;
132                 case 'p':                                                                       /* port */
133                         if (is_intpos (optarg)) {
134                                 port = atoi (optarg);
135                         }
136                         else {
137                                 printf ("Port number nust be a positive integer: %s\n", optarg);
138                                 usage ("");
139                         }
140                 }
142         }
144         c = optind;
145         if (server_name == NULL && c < argc) {
146                 if (is_host (argv[c])) {
147                         server_name = argv[c++];
148                 }
149         }
151         if (port == -1 && c < argc) {
152                 if (is_intpos (argv[c])) {
153                         port = atoi (argv[c++]);
154                 }
155                 else {
156                         print_usage ();
157                         exit (STATE_UNKNOWN);
158                 }
159         }
161         return validate_arguments ();
164 int
165 validate_arguments (void)
167         if (server_name == NULL)
168                 return ERROR;
169         if (port == -1)                                                         /* funky, but allows -p to override stray integer in args */
170                 port = SSH_DFL_PORT;
171         return OK;
175 /************************************************************************
177 * Try to connect to SSH server at specified server and port
179 *-----------------------------------------------------------------------*/
181 int
182 ssh_connect (char *haddr, int hport)
184         int sd;
185         int result;
186         char *output = NULL;
187         char *buffer = NULL;
188         char *ssh_proto = NULL;
189         char *ssh_server = NULL;
190         char rev_no[20];
192         sscanf ("$Revision$", "$Revision: %[0123456789.]", rev_no);
194         result = my_tcp_connect (haddr, hport, &sd);
196         if (result != STATE_OK)
197                 return result;
199         output = (char *) malloc (BUFF_SZ + 1);
200         memset (output, 0, BUFF_SZ + 1);
201         recv (sd, output, BUFF_SZ, 0);
202         if (strncmp (output, "SSH", 3)) {
203                 printf (_("Server answer: %s"), output);
204                 exit (STATE_CRITICAL);
205         }
206         else {
207                 strip (output);
208                 if (verbose)
209                         printf ("%s\n", output);
210                 ssh_proto = output + 4;
211                 ssh_server = ssh_proto + strspn (ssh_proto, "-0123456789. ");
212                 ssh_proto[strspn (ssh_proto, "0123456789. ")] = 0;
213                 printf
214                         (_("SSH OK - %s (protocol %s)\n"),
215                          ssh_server, ssh_proto);
216                 asprintf (&buffer, "SSH-%s-check_ssh_%s\r\n", ssh_proto, rev_no);
217                 send (sd, buffer, strlen (buffer), MSG_DONTWAIT);
218                 if (verbose)
219                         printf ("%s\n", buffer);
220                 exit (STATE_OK);
221         }
224 void
225 print_help (void)
227         char *myport;
228         asprintf (&myport, "%d", SSH_DFL_PORT);
230         print_revision (progname, revision);
232         printf (_("Copyright (c) 1999 Remi Paulmier <remi@sinfomic.fr>\n"));
233         printf (_(COPYRIGHT), copyright, email);
235         printf (_("Try to connect to SSH server at specified server and port\n\n"));
237         print_usage ();
239         printf (_(UT_HELP_VRSN));
241         printf (_(UT_HOST_PORT), 'p', myport);
243         printf (_(UT_IPv46));
245         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
247         printf (_(UT_VERBOSE));
249         printf (_(UT_SUPPORT));
252 void
253 print_usage (void)
255         printf (_("\
256 Usage: %s [-46] [-t <timeout>] [-p <port>] <host>\n"), progname);
257         printf (_(UT_HLP_VRS), progname, progname);
260 /* end of check_ssh.c */