netutils.c 9.1 KB

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