netutils.c 8.8 KB

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