netutils.c 9.8 KB

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