netutils.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. #define LOCAL_TIMEOUT_ALARM_HANDLER
  34. #include "common.h"
  35. #include "netutils.h"
  36. unsigned int socket_timeout = DEFAULT_SOCKET_TIMEOUT;
  37. int econn_refuse_state = STATE_CRITICAL;
  38. int was_refused = FALSE;
  39. #if USE_IPV6
  40. int address_family = AF_UNSPEC;
  41. #else
  42. int address_family = AF_INET;
  43. #endif
  44. /* handles socket timeouts */
  45. void
  46. socket_timeout_alarm_handler (int sig)
  47. {
  48. if (sig == SIGALRM)
  49. printf (_("CRITICAL - Socket timeout after %d seconds\n"), socket_timeout);
  50. else
  51. printf (_("CRITICAL - Abnormal timeout after %d seconds\n"), socket_timeout);
  52. exit (STATE_CRITICAL);
  53. }
  54. /* connects to a host on a specified tcp port, sends a string, and gets a
  55. response. loops on select-recv until timeout or eof to get all of a
  56. multi-packet answer */
  57. int
  58. process_tcp_request2 (const char *server_address, int server_port,
  59. const char *send_buffer, char *recv_buffer, int recv_size)
  60. {
  61. int result;
  62. int send_result;
  63. int recv_result;
  64. int sd;
  65. struct timeval tv;
  66. fd_set readfds;
  67. int recv_length = 0;
  68. result = np_net_connect (server_address, server_port, &sd, IPPROTO_TCP);
  69. if (result != STATE_OK)
  70. return STATE_CRITICAL;
  71. send_result = send (sd, send_buffer, strlen (send_buffer), 0);
  72. if (send_result<0 || (size_t)send_result!=strlen(send_buffer)) {
  73. printf ("%s\n", _("Send failed"));
  74. result = STATE_WARNING;
  75. }
  76. while (1) {
  77. /* wait up to the number of seconds for socket timeout
  78. minus one for data from the host */
  79. tv.tv_sec = socket_timeout - 1;
  80. tv.tv_usec = 0;
  81. FD_ZERO (&readfds);
  82. FD_SET (sd, &readfds);
  83. select (sd + 1, &readfds, NULL, NULL, &tv);
  84. /* make sure some data has arrived */
  85. if (!FD_ISSET (sd, &readfds)) { /* it hasn't */
  86. if (!recv_length) {
  87. strcpy (recv_buffer, "");
  88. printf ("%s\n", _("No data was received from host!"));
  89. result = STATE_WARNING;
  90. }
  91. else { /* this one failed, but previous ones worked */
  92. recv_buffer[recv_length] = 0;
  93. }
  94. break;
  95. }
  96. else { /* it has */
  97. recv_result =
  98. recv (sd, recv_buffer + recv_length,
  99. (size_t)recv_size - recv_length - 1, 0);
  100. if (recv_result == -1) {
  101. /* recv failed, bail out */
  102. strcpy (recv_buffer + recv_length, "");
  103. result = STATE_WARNING;
  104. break;
  105. }
  106. else if (recv_result == 0) {
  107. /* end of file ? */
  108. recv_buffer[recv_length] = 0;
  109. break;
  110. }
  111. else { /* we got data! */
  112. recv_length += recv_result;
  113. if (recv_length >= recv_size - 1) {
  114. /* buffer full, we're done */
  115. recv_buffer[recv_size - 1] = 0;
  116. break;
  117. }
  118. }
  119. }
  120. /* end if(!FD_ISSET(sd,&readfds)) */
  121. }
  122. /* end while(1) */
  123. close (sd);
  124. return result;
  125. }
  126. /* connects to a host on a specified port, sends a string, and gets a
  127. response */
  128. int
  129. process_request (const char *server_address, int server_port, int proto,
  130. const char *send_buffer, char *recv_buffer, int recv_size)
  131. {
  132. int result;
  133. int sd;
  134. result = STATE_OK;
  135. result = np_net_connect (server_address, server_port, &sd, proto);
  136. if (result != STATE_OK)
  137. return STATE_CRITICAL;
  138. result = send_request (sd, proto, send_buffer, recv_buffer, recv_size);
  139. close (sd);
  140. return result;
  141. }
  142. /* opens a tcp or udp connection to a remote host or local socket */
  143. int
  144. np_net_connect (const char *host_name, int port, int *sd, int proto)
  145. {
  146. struct addrinfo hints;
  147. struct addrinfo *r, *res;
  148. struct sockaddr_un su;
  149. char port_str[6], host[MAX_HOST_ADDRESS_LENGTH];
  150. size_t len;
  151. int socktype, result;
  152. socktype = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
  153. /* as long as it doesn't start with a '/', it's assumed a host or ip */
  154. if(host_name[0] != '/'){
  155. memset (&hints, 0, sizeof (hints));
  156. hints.ai_family = address_family;
  157. hints.ai_protocol = proto;
  158. hints.ai_socktype = socktype;
  159. len = strlen (host_name);
  160. /* check for an [IPv6] address (and strip the brackets) */
  161. if (len >= 2 && host_name[0] == '[' && host_name[len - 1] == ']') {
  162. host_name++;
  163. len -= 2;
  164. }
  165. if (len >= sizeof(host))
  166. return STATE_UNKNOWN;
  167. memcpy (host, host_name, len);
  168. host[len] = '\0';
  169. snprintf (port_str, sizeof (port_str), "%d", port);
  170. result = getaddrinfo (host, port_str, &hints, &res);
  171. if (result != 0) {
  172. printf ("%s\n", gai_strerror (result));
  173. return STATE_UNKNOWN;
  174. }
  175. r = res;
  176. while (r) {
  177. /* attempt to create a socket */
  178. *sd = socket (r->ai_family, socktype, r->ai_protocol);
  179. if (*sd < 0) {
  180. printf ("%s\n", _("Socket creation failed"));
  181. freeaddrinfo (r);
  182. return STATE_UNKNOWN;
  183. }
  184. /* attempt to open a connection */
  185. result = connect (*sd, r->ai_addr, r->ai_addrlen);
  186. if (result == 0) {
  187. was_refused = FALSE;
  188. break;
  189. }
  190. if (result < 0) {
  191. switch (errno) {
  192. case ECONNREFUSED:
  193. was_refused = TRUE;
  194. break;
  195. }
  196. }
  197. close (*sd);
  198. r = r->ai_next;
  199. }
  200. freeaddrinfo (res);
  201. }
  202. /* else the hostname is interpreted as a path to a unix socket */
  203. else {
  204. if(strlen(host_name) >= UNIX_PATH_MAX){
  205. die(STATE_UNKNOWN, _("Supplied path too long unix domain socket"));
  206. }
  207. memset(&su, 0, sizeof(su));
  208. su.sun_family = AF_UNIX;
  209. strncpy(su.sun_path, host_name, UNIX_PATH_MAX);
  210. *sd = socket(PF_UNIX, SOCK_STREAM, 0);
  211. if(*sd < 0){
  212. die(STATE_UNKNOWN, _("Socket creation failed"));
  213. }
  214. result = connect(*sd, (struct sockaddr *)&su, sizeof(su));
  215. if (result < 0 && errno == ECONNREFUSED)
  216. was_refused = TRUE;
  217. }
  218. if (result == 0)
  219. return STATE_OK;
  220. else if (was_refused) {
  221. switch (econn_refuse_state) { /* a user-defined expected outcome */
  222. case STATE_OK:
  223. case STATE_WARNING: /* user wants WARN or OK on refusal */
  224. return econn_refuse_state;
  225. break;
  226. case STATE_CRITICAL: /* user did not set econn_refuse_state */
  227. printf ("%s\n", strerror(errno));
  228. return econn_refuse_state;
  229. break;
  230. default: /* it's a logic error if we do not end up in STATE_(OK|WARNING|CRITICAL) */
  231. return STATE_UNKNOWN;
  232. break;
  233. }
  234. }
  235. else {
  236. printf ("%s\n", strerror(errno));
  237. return STATE_CRITICAL;
  238. }
  239. }
  240. int
  241. send_request (int sd, int proto, const char *send_buffer, char *recv_buffer, int recv_size)
  242. {
  243. int result = STATE_OK;
  244. int send_result;
  245. int recv_result;
  246. struct timeval tv;
  247. fd_set readfds;
  248. send_result = send (sd, send_buffer, strlen (send_buffer), 0);
  249. if (send_result<0 || (size_t)send_result!=strlen(send_buffer)) {
  250. printf ("%s\n", _("Send failed"));
  251. result = STATE_WARNING;
  252. }
  253. /* wait up to the number of seconds for socket timeout minus one
  254. for data from the host */
  255. tv.tv_sec = socket_timeout - 1;
  256. tv.tv_usec = 0;
  257. FD_ZERO (&readfds);
  258. FD_SET (sd, &readfds);
  259. select (sd + 1, &readfds, NULL, NULL, &tv);
  260. /* make sure some data has arrived */
  261. if (!FD_ISSET (sd, &readfds)) {
  262. strcpy (recv_buffer, "");
  263. printf ("%s\n", _("No data was received from host!"));
  264. result = STATE_WARNING;
  265. }
  266. else {
  267. recv_result = recv (sd, recv_buffer, (size_t)recv_size - 1, 0);
  268. if (recv_result == -1) {
  269. strcpy (recv_buffer, "");
  270. if (proto != IPPROTO_TCP)
  271. printf ("%s\n", _("Receive failed"));
  272. result = STATE_WARNING;
  273. }
  274. else
  275. recv_buffer[recv_result] = 0;
  276. /* die returned string */
  277. recv_buffer[recv_size - 1] = 0;
  278. }
  279. return result;
  280. }
  281. int
  282. is_host (const char *address)
  283. {
  284. if (is_addr (address) || is_hostname (address))
  285. return (TRUE);
  286. return (FALSE);
  287. }
  288. void
  289. host_or_die(const char *str)
  290. {
  291. if(!str || (!is_addr(str) && !is_hostname(str)))
  292. usage_va(_("Invalid hostname/address - %s"), str);
  293. }
  294. int
  295. is_addr (const char *address)
  296. {
  297. #ifdef USE_IPV6
  298. if (address_family == AF_INET && is_inet_addr (address))
  299. return TRUE;
  300. else if (address_family == AF_INET6 && is_inet6_addr (address))
  301. return TRUE;
  302. #else
  303. if (is_inet_addr (address))
  304. return (TRUE);
  305. #endif
  306. return (FALSE);
  307. }
  308. int
  309. resolve_host_or_addr (const char *address, int family)
  310. {
  311. struct addrinfo hints;
  312. struct addrinfo *res;
  313. int retval;
  314. memset (&hints, 0, sizeof (hints));
  315. hints.ai_family = family;
  316. retval = getaddrinfo (address, NULL, &hints, &res);
  317. if (retval != 0)
  318. return FALSE;
  319. else {
  320. freeaddrinfo (res);
  321. return TRUE;
  322. }
  323. }