summaryrefslogtreecommitdiffstats
path: root/plugins/check_ssh.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/check_ssh.c')
-rw-r--r--plugins/check_ssh.c62
1 files changed, 16 insertions, 46 deletions
diff --git a/plugins/check_ssh.c b/plugins/check_ssh.c
index faaead6a..3efc2e62 100644
--- a/plugins/check_ssh.c
+++ b/plugins/check_ssh.c
@@ -34,7 +34,6 @@ int validate_arguments (void);
34void print_help (void); 34void print_help (void);
35void print_usage (void); 35void print_usage (void);
36 36
37char *ssh_resolve (char *hostname);
38int ssh_connect (char *haddr, short hport); 37int ssh_connect (char *haddr, short hport);
39 38
40int 39int
@@ -49,7 +48,7 @@ main (int argc, char **argv)
49 alarm (socket_timeout); 48 alarm (socket_timeout);
50 49
51 /* ssh_connect exits if error is found */ 50 /* ssh_connect exits if error is found */
52 ssh_connect (ssh_resolve (server_name), port); 51 ssh_connect (server_name, port);
53 52
54 alarm (0); 53 alarm (0);
55 54
@@ -62,6 +61,7 @@ int
62process_arguments (int argc, char **argv) 61process_arguments (int argc, char **argv)
63{ 62{
64 int c; 63 int c;
64 char *tmp = NULL;
65 65
66#ifdef HAVE_GETOPT_H 66#ifdef HAVE_GETOPT_H
67 int option_index = 0; 67 int option_index = 0;
@@ -109,6 +109,8 @@ process_arguments (int argc, char **argv)
109 socket_timeout = atoi (optarg); 109 socket_timeout = atoi (optarg);
110 break; 110 break;
111 case 'H': /* host */ 111 case 'H': /* host */
112 if (is_host (optarg) == FALSE)
113 usage ("Invalid hostname/address\n");
112 server_name = optarg; 114 server_name = optarg;
113 break; 115 break;
114 case 'p': /* port */ 116 case 'p': /* port */
@@ -125,9 +127,12 @@ process_arguments (int argc, char **argv)
125 127
126 c = optind; 128 c = optind;
127 if (server_name == NULL && argv[c]) { 129 if (server_name == NULL && argv[c]) {
128 server_name = argv[c++]; 130 if (is_host (argv[c])) {
131 server_name = argv[c++];
132 }
129 } 133 }
130 else if (port == -1 && argv[c]) { 134
135 if (port == -1 && argv[c]) {
131 if (is_intpos (argv[c])) { 136 if (is_intpos (argv[c])) {
132 port = atoi (argv[c++]); 137 port = atoi (argv[c++]);
133 } 138 }
@@ -153,26 +158,6 @@ validate_arguments (void)
153 158
154/************************************************************************ 159/************************************************************************
155* 160*
156* Resolve hostname into IP address
157*
158*-----------------------------------------------------------------------*/
159
160char *
161ssh_resolve (char *hostname)
162{
163 struct hostent *host;
164
165 host = gethostbyname (hostname);
166 if (!host) {
167 herror (hostname);
168 exit (STATE_CRITICAL);
169 }
170 return (host->h_addr);
171}
172
173
174/************************************************************************
175*
176* Try to connect to SSH server at specified server and port 161* Try to connect to SSH server at specified server and port
177* 162*
178*-----------------------------------------------------------------------*/ 163*-----------------------------------------------------------------------*/
@@ -180,10 +165,8 @@ ssh_resolve (char *hostname)
180int 165int
181ssh_connect (char *haddr, short hport) 166ssh_connect (char *haddr, short hport)
182{ 167{
183 int s; 168 int sd;
184 struct sockaddr_in addr; 169 int result;
185 int addrlen;
186 int len;
187 char *output = NULL; 170 char *output = NULL;
188 char *buffer = NULL; 171 char *buffer = NULL;
189 char *ssh_proto = NULL; 172 char *ssh_proto = NULL;
@@ -192,27 +175,14 @@ ssh_connect (char *haddr, short hport)
192 175
193 sscanf ("$Revision$", "$Revision: %[0123456789.]", revision); 176 sscanf ("$Revision$", "$Revision: %[0123456789.]", revision);
194 177
195 addrlen = sizeof (addr); 178 result = my_tcp_connect (haddr, hport, &sd);
196 memset (&addr, 0, addrlen);
197 addr.sin_port = htons (hport);
198 addr.sin_family = AF_INET;
199 bcopy (haddr, (void *) &addr.sin_addr.s_addr, 4);
200 179
201 s = socket (AF_INET, SOCK_STREAM, 0); 180 if (result != STATE_OK)
202 if (!s) { 181 return result;
203 printf ("socket(): %s for %s:%d\n", strerror (errno), server_name, hport);
204 exit (STATE_CRITICAL);
205 }
206
207 if (connect (s, (struct sockaddr *) &addr, addrlen)) {
208 printf ("connect(): %s for %s:%d\n", strerror (errno), server_name,
209 hport);
210 exit (STATE_CRITICAL);
211 }
212 182
213 output = (char *) malloc (BUFF_SZ + 1); 183 output = (char *) malloc (BUFF_SZ + 1);
214 memset (output, 0, BUFF_SZ + 1); 184 memset (output, 0, BUFF_SZ + 1);
215 recv (s, output, BUFF_SZ, 0); 185 recv (sd, output, BUFF_SZ, 0);
216 if (strncmp (output, "SSH", 3)) { 186 if (strncmp (output, "SSH", 3)) {
217 printf ("Server answer: %s", output); 187 printf ("Server answer: %s", output);
218 exit (STATE_CRITICAL); 188 exit (STATE_CRITICAL);
@@ -228,7 +198,7 @@ ssh_connect (char *haddr, short hport)
228 ("SSH ok - %s (protocol %s)\n", 198 ("SSH ok - %s (protocol %s)\n",
229 ssh_server, ssh_proto); 199 ssh_server, ssh_proto);
230 asprintf (&buffer, "SSH-%s-check_ssh_%s\r\n", ssh_proto, revision); 200 asprintf (&buffer, "SSH-%s-check_ssh_%s\r\n", ssh_proto, revision);
231 send (s, buffer, strlen (buffer), MSG_DONTWAIT); 201 send (sd, buffer, strlen (buffer), MSG_DONTWAIT);
232 if (verbose) 202 if (verbose)
233 printf ("%s\n", buffer); 203 printf ("%s\n", buffer);
234 exit (STATE_OK); 204 exit (STATE_OK);