root/src/modules/ssh2.c

Revision b9a4230bc7f65c1268a1569d08ffc92d1074bc68, 10.2 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 9 years ago)

first whack at using fcntl everywhere. Heads up, a lot could break. resf #167.

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2007, OmniTI Computer Consulting, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  *       notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  *       copyright notice, this list of conditions and the following
13  *       disclaimer in the documentation and/or other materials provided
14  *       with the distribution.
15  *     * Neither the name OmniTI Computer Consulting, Inc. nor the names
16  *       of its contributors may be used to endorse or promote products
17  *       derived from this software without specific prior written
18  *       permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include "noit_defines.h"
34
35 #include <stdio.h>
36 #include <unistd.h>
37 #include <errno.h>
38 #include <assert.h>
39 #include <math.h>
40 #ifdef HAVE_SYS_FILIO_H
41 #include <sys/filio.h>
42 #endif
43
44 #include "noit_module.h"
45 #include "noit_check.h"
46 #include "noit_check_tools.h"
47 #include "utils/noit_log.h"
48 #include "utils/noit_hash.h"
49
50 #include <libssh2.h>
51
52 #define DEFAULT_SSH_PORT 22
53
54 typedef struct {
55   noit_module_t *self;
56   noit_check_t *check;
57   enum {
58     WANT_CONNECT = 0,
59     WANT_CLOSE = 1
60   } state;
61   LIBSSH2_SESSION *session;
62   int available;
63   int timed_out;
64   char *error;
65   char fingerprint[33]; /* 32 hex characters */
66   eventer_t synch_fd_event;
67   eventer_t timeout_event; /* Only used for connect() */
68 } ssh2_check_info_t;
69
70 static noit_log_stream_t nlerr = NULL;
71 static noit_log_stream_t nldeb = NULL;
72
73 static void ssh2_cleanup(noit_module_t *self, noit_check_t *check) {
74   ssh2_check_info_t *ci = check->closure;
75   if(ci) {
76     if(ci->timeout_event) {
77       eventer_remove(ci->timeout_event);
78       eventer_free(ci->timeout_event);
79     }
80     if(ci->session) {
81       libssh2_session_disconnect(ci->session, "Bye!");
82       libssh2_session_free(ci->session);
83     }
84     if(ci->error) free(ci->error);
85     memset(ci, 0, sizeof(*ci));
86   }
87 }
88 static int ssh2_init(noit_module_t *self) {
89   return 0;
90 }
91 static int ssh2_config(noit_module_t *self, noit_hash_table *options) {
92   return 0;
93 }
94 static void ssh2_log_results(noit_module_t *self, noit_check_t *check) {
95   stats_t current;
96   struct timeval duration;
97   ssh2_check_info_t *ci = check->closure;
98
99   noit_check_stats_clear(&current);
100
101   gettimeofday(&current.whence, NULL);
102   sub_timeval(current.whence, check->last_fire_time, &duration);
103   current.duration = duration.tv_sec * 1000 + duration.tv_usec / 1000;
104   current.available = ci->available ? NP_AVAILABLE : NP_UNAVAILABLE;
105   current.state = ci->fingerprint[0] ? NP_GOOD : NP_BAD;
106
107   if(ci->error) current.status = ci->error;
108   else if(ci->timed_out) current.status = "timeout";
109   else if(ci->fingerprint[0]) current.status = ci->fingerprint;
110   else current.status = "internal error";
111
112   if(ci->fingerprint[0])
113     noit_stats_set_metric(&current, "fingerprint", METRIC_STRING,
114                           ci->fingerprint);
115   noit_check_set_stats(self, check, &current);
116 }
117 static int ssh2_drive_session(eventer_t e, int mask, void *closure,
118                               struct timeval *now) {
119   int i;
120   const char *fingerprint;
121   ssh2_check_info_t *ci = closure;
122   if(ci->state == WANT_CLOSE) {
123     noit_check_t *check = ci->check;
124     ssh2_log_results(ci->self, ci->check);
125     ssh2_cleanup(ci->self, ci->check);
126     eventer_remove_fd(e->fd);
127     e->opset->close(e->fd, &mask, e);
128     check->flags &= ~NP_RUNNING;
129     return 0;
130   }
131   switch(mask) {
132     case EVENTER_ASYNCH_WORK:
133       if(eventer_set_fd_blocking(e->fd)) {
134         ci->timed_out = 0;
135         ci->error = strdup("socket error");
136         return 0;
137       }
138       ci->session = libssh2_session_init();
139       if (libssh2_session_startup(ci->session, e->fd)) {
140         ci->timed_out = 0;
141         ci->error = strdup("ssh session startup failed");
142         return 0;
143       }
144       fingerprint = libssh2_hostkey_hash(ci->session, LIBSSH2_HOSTKEY_HASH_MD5);
145       for(i=0;i<16;i++) {
146         snprintf(ci->fingerprint + (i*2), 3, "%02x",
147                  (unsigned char)fingerprint[i]);
148       }
149       ci->fingerprint[32] = '\0';
150       ci->timed_out = 0;
151       return 0;
152       break;
153     case EVENTER_ASYNCH_CLEANUP:
154       if(ci->session) {
155         libssh2_session_disconnect(ci->session, "Bye!");
156         libssh2_session_free(ci->session);
157         ci->session = NULL;
158       }
159       ci->state = WANT_CLOSE;
160       break;
161     default:
162       abort();
163   }
164   return 0;
165 }
166 static int ssh2_connect_complete(eventer_t e, int mask, void *closure,
167                                  struct timeval *now) {
168   ssh2_check_info_t *ci = closure;
169   eventer_t asynch_e;
170
171   if(mask & EVENTER_EXCEPTION) {
172     noit_check_t *check = ci->check;
173     ci->timed_out = 0;
174     ci->error = strdup("ssh connection failed");
175     ssh2_log_results(ci->self, ci->check);
176     ssh2_cleanup(ci->self, ci->check);
177     eventer_remove_fd(e->fd);
178     e->opset->close(e->fd, &mask, e);
179     check->flags &= ~NP_RUNNING;
180     return 0;
181   }
182
183   ci->available = 1;
184
185   /* We steal the timeout_event as it has the exact timeout we want. */
186   assert(ci->timeout_event);
187   asynch_e = eventer_remove(ci->timeout_event);
188   assert(asynch_e);
189   ci->timeout_event = NULL;
190
191   ci->synch_fd_event = NULL;
192   asynch_e->fd = e->fd;
193   asynch_e->callback = ssh2_drive_session;
194   asynch_e->closure = closure;
195   asynch_e->mask = EVENTER_ASYNCH;
196   eventer_add(asynch_e);
197
198   eventer_remove_fd(e->fd);
199   return 0;
200 }
201 static int ssh2_connect_timeout(eventer_t e, int mask, void *closure,
202                                 struct timeval *now) {
203   eventer_t fde;
204   ssh2_check_info_t *ci = closure;
205   noit_check_t *check = ci->check;
206  
207   ci->timeout_event = NULL; /* This is us, return 0 will free this */
208   ci->error = strdup("ssh connect timeout");
209   if(ci->synch_fd_event) {
210     fde = ci->synch_fd_event;
211     eventer_remove_fd(fde->fd);
212     fde->opset->close(fde->fd, &mask, fde);
213     eventer_free(fde);
214      ci->synch_fd_event = NULL;
215   }
216   ssh2_log_results(ci->self, ci->check);
217   ssh2_cleanup(ci->self, ci->check);
218   check->flags &= ~NP_RUNNING;
219   return 0;
220 }
221 static int ssh2_initiate(noit_module_t *self, noit_check_t *check) {
222   ssh2_check_info_t *ci = check->closure;
223   struct timeval p_int, __now;
224   int fd, rv;
225   eventer_t e;
226   union {
227     struct sockaddr_in sin;
228     struct sockaddr_in6 sin6;
229   } sockaddr;
230   socklen_t sockaddr_len;
231   unsigned short ssh_port = DEFAULT_SSH_PORT;
232   const char *port_str = NULL;
233
234   /* We cannot be running */
235   assert(!(check->flags & NP_RUNNING));
236   check->flags |= NP_RUNNING;
237
238   ci->self = self;
239   ci->check = check;
240
241   ci->timed_out = 1;
242   if(ci->timeout_event) {
243     eventer_remove(ci->timeout_event);
244     free(ci->timeout_event->closure);
245     eventer_free(ci->timeout_event);
246     ci->timeout_event = NULL;
247   }
248   gettimeofday(&__now, NULL);
249   memcpy(&check->last_fire_time, &__now, sizeof(__now));
250
251   /* Open a socket */
252   fd = socket(check->target_family, SOCK_STREAM, 0);
253   if(fd < 0) goto fail;
254
255   /* Make it non-blocking */
256   if(eventer_set_fd_nonblocking(fd)) goto fail;
257
258   if(noit_hash_retr_str(check->config, "port", strlen("port"),
259                         &port_str)) {
260     ssh_port = (unsigned short)atoi(port_str);
261   }
262   memset(&sockaddr, 0, sizeof(sockaddr));
263   sockaddr.sin6.sin6_family = check->target_family;
264   if(check->target_family == AF_INET) {
265     memcpy(&sockaddr.sin.sin_addr,
266            &check->target_addr.addr, sizeof(sockaddr.sin.sin_addr));
267     sockaddr.sin.sin_port = htons(ssh_port);
268     sockaddr_len = sizeof(sockaddr.sin);
269   }
270   else {
271     memcpy(&sockaddr.sin6.sin6_addr,
272            &check->target_addr.addr6, sizeof(sockaddr.sin6.sin6_addr));
273     sockaddr.sin6.sin6_port = htons(ssh_port);
274     sockaddr_len = sizeof(sockaddr.sin6);
275   }
276
277   /* Initiate a connection */
278   rv = connect(fd, (struct sockaddr *)&sockaddr, sockaddr_len);
279   if(rv == -1 && errno != EINPROGRESS) goto fail;
280
281   /* Register a handler for connection completion */
282   e = eventer_alloc();
283   e->fd = fd;
284   e->mask = EVENTER_READ | EVENTER_WRITE | EVENTER_EXCEPTION;
285   e->callback = ssh2_connect_complete;
286   e->closure =  ci;
287   ci->synch_fd_event = e;
288   eventer_add(e);
289
290   e = eventer_alloc();
291   e->mask = EVENTER_TIMER;
292   e->callback = ssh2_connect_timeout;
293   e->closure = ci;
294   memcpy(&e->whence, &__now, sizeof(__now));
295   p_int.tv_sec = check->timeout / 1000;
296   p_int.tv_usec = (check->timeout % 1000) * 1000;
297   add_timeval(e->whence, p_int, &e->whence);
298   ci->timeout_event = e;
299   eventer_add(e);
300   return 0;
301
302  fail:
303   if(fd >= 0) close(fd);
304   ssh2_log_results(ci->self, ci->check);
305   ssh2_cleanup(ci->self, ci->check);
306   check->flags &= ~NP_RUNNING;
307   return -1;
308 }
309
310 static int ssh2_initiate_check(noit_module_t *self, noit_check_t *check,
311                                int once, noit_check_t *parent) {
312   if(!check->closure) check->closure = calloc(1, sizeof(ssh2_check_info_t));
313   INITIATE_CHECK(ssh2_initiate, self, check);
314   return 0;
315 }
316
317 static int ssh2_onload(noit_image_t *self) {
318   nlerr = noit_log_stream_find("error/ssh2");
319   nldeb = noit_log_stream_find("debug/ssh2");
320   if(!nlerr) nlerr = noit_stderr;
321   if(!nldeb) nldeb = noit_debug;
322
323   eventer_name_callback("http/ssh2_connect_complete", ssh2_connect_complete);
324   eventer_name_callback("http/ssh2_drive_session", ssh2_drive_session);
325   return 0;
326 }
327
328 #include "ssh2.xmlh"
329 noit_module_t ssh2 = {
330   {
331     NOIT_MODULE_MAGIC,
332     NOIT_MODULE_ABI_VERSION,
333     "ssh2",
334     "Secure Shell version 2 checker",
335     ssh2_xml_description,
336     ssh2_onload
337   },
338   ssh2_config,
339   ssh2_init,
340   ssh2_initiate_check,
341   ssh2_cleanup
342 };
343
Note: See TracBrowser for help on using the browser.