Code

test GNU_SOURCE and include features.h if present to clear warning about asprintf...
[nagiosplug.git] / plugins / check_ssh.c
1 /*
2 * check_ssh.c
3
4 * Made by (Remi PAULMIER)
5 * Login   <remi@sinfomic.fr>
6
7 * Started on  Fri Jul  9 09:18:23 1999 Remi PAULMIER
8 * Update Thu Jul 22 12:50:04 1999 remi paulmier
9 * $Id$
10 *
11 */
13 #include "config.h"
14 #include "common.h"
15 #include "netutils.h"
16 #include "utils.h"
18 const char *progname = "check_ssh";
19 #define REVISION "$Revision$"
21 #ifndef MSG_DONTWAIT
22 #define MSG_DONTWAIT 0
23 #endif
25 #define SSH_DFL_PORT    22
26 #define BUFF_SZ         256
28 short port = -1;
29 char *server_name = NULL;
30 int verbose = FALSE;
32 int process_arguments (int, char **);
33 int validate_arguments (void);
34 void print_help (void);
35 void print_usage (void);
37 int ssh_connect (char *haddr, short hport);
39 int
40 main (int argc, char **argv)
41 {
42         int result;
44         if (process_arguments (argc, argv) == ERROR)
45                 usage ("Could not parse arguments\n");
47         /* initialize alarm signal handling */
48         signal (SIGALRM, socket_timeout_alarm_handler);
49         alarm (socket_timeout);
51         /* ssh_connect exits if error is found */
52         result = ssh_connect (server_name, port);
54         alarm (0);
56         return (result);
57 }
60 /* process command-line arguments */
61 int
62 process_arguments (int argc, char **argv)
63 {
64         int c;
65         char *tmp = NULL;
67         int option_index = 0;
68         static struct option long_options[] = {
69                 {"version", no_argument, 0, 'V'},
70                 {"help", no_argument, 0, 'h'},
71                 {"use-ipv4", no_argument, 0, '4'},
72                 {"use-ipv6", no_argument, 0, '6'},
73                 {"verbose", no_argument, 0, 'v'},
74                 {"timeout", required_argument, 0, 't'},
75                 {"host", required_argument, 0, 'H'},
76                 {0, 0, 0, 0}
77         };
79         if (argc < 2)
80                 return ERROR;
82         for (c = 1; c < argc; c++)
83                 if (strcmp ("-to", argv[c]) == 0)
84                         strcpy (argv[c], "-t");
86         while (1) {
87                 c = getopt_long (argc, argv, "+Vhv46t:H:p:", long_options, &option_index);
89                 if (c == -1 || c == EOF)
90                         break;
92                 switch (c) {
93                 case '?':                                                                       /* help */
94                         usage ("");
95                 case 'V':                                                                       /* version */
96                         print_revision (progname, REVISION);
97                         exit (STATE_OK);
98                 case 'h':                                                                       /* help */
99                         print_help ();
100                         exit (STATE_OK);
101                 case 'v':                                                                       /* verose */
102                         verbose = TRUE;
103                         break;
104                 case 't':                                                                       /* timeout period */
105                         if (!is_integer (optarg))
106                                 usage ("Timeout Interval must be an integer!\n\n");
107                         socket_timeout = atoi (optarg);
108                         break;
109                 case '4':
110                         address_family = AF_INET;
111                         break;
112                 case '6':
113 #ifdef USE_IPV6
114                         address_family = AF_INET6;
115 #else
116                         usage ("IPv6 support not available\n");
117 #endif
118                         break;
119                 case 'H':                                                                       /* host */
120                         if (is_host (optarg) == FALSE)
121                                 usage ("Invalid hostname/address\n");
122                         server_name = optarg;
123                         break;
124                 case 'p':                                                                       /* port */
125                         if (is_intpos (optarg)) {
126                                 port = atoi (optarg);
127                         }
128                         else {
129                                 printf ("Port number nust be a positive integer: %s\n", optarg);
130                                 usage ("");
131                         }
132                 }
134         }
136         c = optind;
137         if (server_name == NULL && c < argc) {
138                 if (is_host (argv[c])) {
139                         server_name = argv[c++];
140                 }
141         }
143         if (port == -1 && c < argc) {
144                 if (is_intpos (argv[c])) {
145                         port = atoi (argv[c++]);
146                 }
147                 else {
148                         print_usage ();
149                         exit (STATE_UNKNOWN);
150                 }
151         }
153         return validate_arguments ();
156 int
157 validate_arguments (void)
159         if (server_name == NULL)
160                 return ERROR;
161         if (port == -1)                                                         /* funky, but allows -p to override stray integer in args */
162                 port = SSH_DFL_PORT;
163         return OK;
167 /************************************************************************
169 * Try to connect to SSH server at specified server and port
171 *-----------------------------------------------------------------------*/
173 int
174 ssh_connect (char *haddr, short hport)
176         int sd;
177         int result;
178         char *output = NULL;
179         char *buffer = NULL;
180         char *ssh_proto = NULL;
181         char *ssh_server = NULL;
182         char revision[20];
184         sscanf ("$Revision$", "$Revision: %[0123456789.]", revision);
186         result = my_tcp_connect (haddr, hport, &sd);
188         if (result != STATE_OK)
189                 return result;
191         output = (char *) malloc (BUFF_SZ + 1);
192         memset (output, 0, BUFF_SZ + 1);
193         recv (sd, output, BUFF_SZ, 0);
194         if (strncmp (output, "SSH", 3)) {
195                 printf ("Server answer: %s", output);
196                 exit (STATE_CRITICAL);
197         }
198         else {
199                 strip (output);
200                 if (verbose)
201                         printf ("%s\n", output);
202                 ssh_proto = output + 4;
203                 ssh_server = ssh_proto + strspn (ssh_proto, "-0123456789. ");
204                 ssh_proto[strspn (ssh_proto, "0123456789. ")] = 0;
205                 printf
206                         ("SSH OK - %s (protocol %s)\n",
207                          ssh_server, ssh_proto);
208                 asprintf (&buffer, "SSH-%s-check_ssh_%s\r\n", ssh_proto, revision);
209                 send (sd, buffer, strlen (buffer), MSG_DONTWAIT);
210                 if (verbose)
211                         printf ("%s\n", buffer);
212                 exit (STATE_OK);
213         }
216 void
217 print_help (void)
219         print_revision (progname, REVISION);
220         printf ("Copyright (c) 1999 Remi Paulmier (remi@sinfomic.fr)\n\n");
221         print_usage ();
222         printf ("by default, port is %d\n", SSH_DFL_PORT);
225 void
226 print_usage (void)
228         printf
229                 ("Usage:\n"
230                  " %s -t [timeout] -p [port] <host>\n"
231                  " %s -V prints version info\n"
232                  " %s -4 use IPv4 connection\n"
233                  " %s -6 use IPv6 connection\n"
234                  " %s -h prints more detailed help\n", 
235                  progname, progname, progname, progname, progname);
238 /* end of check_ssh.c */