netutils.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /****************************************************************************
  2. *
  3. * Nagios plugins network utilities
  4. *
  5. * License: GPL
  6. * Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)
  7. *
  8. * Last Modified: $Date$
  9. *
  10. * Description:
  11. *
  12. * This file contains commons functions used in many of the plugins.
  13. *
  14. * License Information:
  15. *
  16. * This program is free software; you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation; either version 2 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  29. *
  30. * $Id$
  31. *
  32. ****************************************************************************/
  33. #include "common.h"
  34. #include "netutils.h"
  35. unsigned int socket_timeout = DEFAULT_SOCKET_TIMEOUT;
  36. int econn_refuse_state = STATE_CRITICAL;
  37. int was_refused = FALSE;
  38. int address_family = AF_UNSPEC;
  39. /* handles socket timeouts */
  40. void
  41. socket_timeout_alarm_handler (int sig)
  42. {
  43. if (sig == SIGALRM)
  44. printf (_("CRITICAL - Socket timeout after %d seconds\n"), socket_timeout);
  45. else
  46. printf (_("CRITICAL - Abnormal timeout after %d seconds\n"), socket_timeout);
  47. exit (STATE_CRITICAL);
  48. }
  49. /* connects to a host on a specified tcp port, sends a string, and gets a
  50. response. loops on select-recv until timeout or eof to get all of a
  51. multi-packet answer */
  52. int
  53. process_tcp_request2 (const char *server_address, int server_port,
  54. const char *send_buffer, char *recv_buffer, int recv_size)
  55. {
  56. int result;
  57. int send_result;
  58. int recv_result;
  59. int sd;
  60. struct timeval tv;
  61. fd_set readfds;
  62. int recv_length = 0;
  63. result = np_net_connect (server_address, server_port, &sd, IPPROTO_TCP);
  64. if (result != STATE_OK)
  65. return STATE_CRITICAL;
  66. send_result = send (sd, send_buffer, strlen (send_buffer), 0);
  67. if (send_result<0 || (size_t)send_result!=strlen(send_buffer)) {
  68. printf (_("Send failed\n"));
  69. result = STATE_WARNING;
  70. }
  71. while (1) {
  72. /* wait up to the number of seconds for socket timeout
  73. minus one for data from the host */
  74. tv.tv_sec = socket_timeout - 1;
  75. tv.tv_usec = 0;
  76. FD_ZERO (&readfds);
  77. FD_SET (sd, &readfds);
  78. select (sd + 1, &readfds, NULL, NULL, &tv);
  79. /* make sure some data has arrived */
  80. if (!FD_ISSET (sd, &readfds)) { /* it hasn't */
  81. if (!recv_length) {
  82. strcpy (recv_buffer, "");
  83. printf (_("No data was received from host!\n"));
  84. result = STATE_WARNING;
  85. }
  86. else { /* this one failed, but previous ones worked */
  87. recv_buffer[recv_length] = 0;
  88. }
  89. break;
  90. }
  91. else { /* it has */
  92. recv_result =
  93. recv (sd, recv_buffer + recv_length,
  94. (size_t)recv_size - recv_length - 1, 0);
  95. if (recv_result == -1) {
  96. /* recv failed, bail out */
  97. strcpy (recv_buffer + recv_length, "");
  98. result = STATE_WARNING;
  99. break;
  100. }
  101. else if (recv_result == 0) {
  102. /* end of file ? */
  103. recv_buffer[recv_length] = 0;
  104. break;
  105. }
  106. else { /* we got data! */
  107. recv_length += recv_result;
  108. if (recv_length >= recv_size - 1) {
  109. /* buffer full, we're done */
  110. recv_buffer[recv_size - 1] = 0;
  111. break;
  112. }
  113. }
  114. }
  115. /* end if(!FD_ISSET(sd,&readfds)) */
  116. }
  117. /* end while(1) */
  118. close (sd);
  119. return result;
  120. }
  121. /* connects to a host on a specified port, sends a string, and gets a
  122. response */
  123. int
  124. process_request (const char *server_address, int server_port, int proto,
  125. const char *send_buffer, char *recv_buffer, int recv_size)
  126. {
  127. int result;
  128. int sd;
  129. result = STATE_OK;
  130. result = np_net_connect (server_address, server_port, &sd, proto);
  131. if (result != STATE_OK)
  132. return STATE_CRITICAL;
  133. result = send_request (sd, proto, send_buffer, recv_buffer, recv_size);
  134. close (sd);
  135. return result;
  136. }
  137. /* opens a tcp or udp connection to a remote host */
  138. int
  139. np_net_connect (const char *host_name, int port, int *sd, int proto)
  140. {
  141. struct addrinfo hints;
  142. struct addrinfo *res, *res0;
  143. char port_str[6];
  144. int result;
  145. memset (&hints, 0, sizeof (hints));
  146. hints.ai_family = address_family;
  147. hints.ai_protocol = proto;
  148. hints.ai_socktype = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
  149. snprintf (port_str, sizeof (port_str), "%d", port);
  150. result = getaddrinfo (host_name, port_str, &hints, &res0);
  151. if (result != 0) {
  152. printf ("%s\n", gai_strerror (result));
  153. return STATE_UNKNOWN;
  154. }
  155. else {
  156. res = res0;
  157. while (res) {
  158. /* attempt to create a socket */
  159. *sd = socket (res->ai_family, (proto == IPPROTO_UDP) ?
  160. SOCK_DGRAM : SOCK_STREAM, res->ai_protocol);
  161. if (*sd < 0) {
  162. printf (_("Socket creation failed\n"));
  163. freeaddrinfo (res);
  164. return STATE_UNKNOWN;
  165. }
  166. /* attempt to open a connection */
  167. result = connect (*sd, res->ai_addr, res->ai_addrlen);
  168. if (result == 0) {
  169. was_refused = FALSE;
  170. break;
  171. }
  172. if (result < 0) {
  173. switch (errno) {
  174. case ECONNREFUSED:
  175. was_refused = TRUE;
  176. break;
  177. }
  178. }
  179. close (*sd);
  180. res = res->ai_next;
  181. }
  182. freeaddrinfo (res0);
  183. }
  184. if (result == 0)
  185. return STATE_OK;
  186. else if (was_refused) {
  187. switch (econn_refuse_state) { /* a user-defined expected outcome */
  188. case STATE_OK:
  189. case STATE_WARNING: /* user wants WARN or OK on refusal */
  190. return econn_refuse_state;
  191. break;
  192. case STATE_CRITICAL: /* user did not set econn_refuse_state */
  193. printf ("%s\n", strerror(errno));
  194. return econn_refuse_state;
  195. break;
  196. default: /* it's a logic error if we do not end up in STATE_(OK|WARNING|CRITICAL) */
  197. return STATE_UNKNOWN;
  198. break;
  199. }
  200. }
  201. else {
  202. printf ("%s\n", strerror(errno));
  203. return STATE_CRITICAL;
  204. }
  205. }
  206. #ifdef HAVE_SSL
  207. static SSL_CTX *c=NULL;
  208. static SSL *s=NULL;
  209. int np_net_ssl_init (int sd){
  210. SSL_METHOD *m=NULL;
  211. /* Initialize SSL context */
  212. SSLeay_add_ssl_algorithms ();
  213. m = SSLv23_client_method ();
  214. SSL_load_error_strings ();
  215. OpenSSL_add_all_algorithms();
  216. if ((c = SSL_CTX_new (m)) == NULL) {
  217. printf (_("CRITICAL - Cannot create SSL context.\n"));
  218. return STATE_CRITICAL;
  219. }
  220. if ((s = SSL_new (c)) != NULL){
  221. SSL_set_fd (s, sd);
  222. if (SSL_connect(s) == 1){
  223. return OK;
  224. } else {
  225. printf (_("CRITICAL - Cannot make SSL connection "));
  226. #ifdef USE_OPENSSL /* XXX look into ERR_error_string */
  227. ERR_print_errors_fp (stdout);
  228. #endif /* USE_OPENSSL */
  229. }
  230. } else {
  231. printf (_("CRITICAL - Cannot initiate SSL handshake.\n"));
  232. }
  233. return STATE_CRITICAL;
  234. }
  235. void np_net_ssl_cleanup (){
  236. if(s){
  237. SSL_shutdown (s);
  238. SSL_free (s);
  239. if(c) SSL_CTX_free (c);
  240. }
  241. }
  242. int np_net_ssl_write(const void *buf, int num){
  243. return SSL_write(s, buf, num);
  244. }
  245. int np_net_ssl_read(void *buf, int num){
  246. return SSL_read(s, buf, num);
  247. }
  248. #endif /* HAVE_SSL */
  249. int
  250. send_request (int sd, int proto, const char *send_buffer, char *recv_buffer, int recv_size)
  251. {
  252. int result = STATE_OK;
  253. int send_result;
  254. int recv_result;
  255. struct timeval tv;
  256. fd_set readfds;
  257. send_result = send (sd, send_buffer, strlen (send_buffer), 0);
  258. if (send_result<0 || (size_t)send_result!=strlen(send_buffer)) {
  259. printf (_("Send failed\n"));
  260. result = STATE_WARNING;
  261. }
  262. /* wait up to the number of seconds for socket timeout minus one
  263. for data from the host */
  264. tv.tv_sec = socket_timeout - 1;
  265. tv.tv_usec = 0;
  266. FD_ZERO (&readfds);
  267. FD_SET (sd, &readfds);
  268. select (sd + 1, &readfds, NULL, NULL, &tv);
  269. /* make sure some data has arrived */
  270. if (!FD_ISSET (sd, &readfds)) {
  271. strcpy (recv_buffer, "");
  272. printf (_("No data was received from host!\n"));
  273. result = STATE_WARNING;
  274. }
  275. else {
  276. recv_result = recv (sd, recv_buffer, (size_t)recv_size - 1, 0);
  277. if (recv_result == -1) {
  278. strcpy (recv_buffer, "");
  279. if (proto != IPPROTO_TCP)
  280. printf (_("Receive failed\n"));
  281. result = STATE_WARNING;
  282. }
  283. else
  284. recv_buffer[recv_result] = 0;
  285. /* die returned string */
  286. recv_buffer[recv_size - 1] = 0;
  287. }
  288. return result;
  289. }
  290. int
  291. is_host (const char *address)
  292. {
  293. if (is_addr (address) || is_hostname (address))
  294. return (TRUE);
  295. return (FALSE);
  296. }
  297. int
  298. is_addr (const char *address)
  299. {
  300. #ifdef USE_IPV6
  301. if (is_inet_addr (address) && address_family != AF_INET6)
  302. #else
  303. if (is_inet_addr (address))
  304. #endif
  305. return (TRUE);
  306. #ifdef USE_IPV6
  307. if (is_inet6_addr (address) && address_family != AF_INET)
  308. return (TRUE);
  309. #endif
  310. return (FALSE);
  311. }
  312. int
  313. resolve_host_or_addr (const char *address, int family)
  314. {
  315. struct addrinfo hints;
  316. struct addrinfo *res;
  317. int retval;
  318. memset (&hints, 0, sizeof (hints));
  319. hints.ai_family = family;
  320. retval = getaddrinfo (address, NULL, &hints, &res);
  321. if (retval != 0)
  322. return FALSE;
  323. else {
  324. freeaddrinfo (res);
  325. return TRUE;
  326. }
  327. }