4
0

netutils.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*****************************************************************************
  2. *
  3. * Nagios plugins network utilities
  4. *
  5. * License: GPL
  6. * Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)
  7. * Copyright (c) 2003-2008 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. #define LOCAL_TIMEOUT_ALARM_HANDLER
  30. #include "common.h"
  31. #include "netutils.h"
  32. int econn_refuse_state = STATE_CRITICAL;
  33. int was_refused = FALSE;
  34. #if USE_IPV6
  35. int address_family = AF_UNSPEC;
  36. #else
  37. int address_family = AF_INET;
  38. #endif
  39. /* handles socket timeouts */
  40. void
  41. socket_timeout_alarm_handler (int sig)
  42. {
  43. if (sig == SIGALRM)
  44. printf (_("%s - Socket timeout after %d seconds\n"), state_text(socket_timeout_state), socket_timeout);
  45. else
  46. printf (_("%s - Abnormal timeout after %d seconds\n"), state_text(socket_timeout_state), socket_timeout);
  47. exit (socket_timeout_state);
  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 ("%s\n", _("Send failed"));
  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 ("%s\n", _("No data was received from host!"));
  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 or local socket */
  138. int
  139. np_net_connect (const char *host_name, int port, int *sd, int proto)
  140. {
  141. struct addrinfo hints;
  142. struct addrinfo *r, *res;
  143. struct sockaddr_un su;
  144. char port_str[6], host[MAX_HOST_ADDRESS_LENGTH];
  145. size_t len;
  146. int socktype, result;
  147. socktype = (proto == IPPROTO_UDP) ? SOCK_DGRAM : SOCK_STREAM;
  148. /* as long as it doesn't start with a '/', it's assumed a host or ip */
  149. if(host_name[0] != '/'){
  150. memset (&hints, 0, sizeof (hints));
  151. hints.ai_family = address_family;
  152. hints.ai_protocol = proto;
  153. hints.ai_socktype = socktype;
  154. len = strlen (host_name);
  155. /* check for an [IPv6] address (and strip the brackets) */
  156. if (len >= 2 && host_name[0] == '[' && host_name[len - 1] == ']') {
  157. host_name++;
  158. len -= 2;
  159. }
  160. if (len >= sizeof(host))
  161. return STATE_UNKNOWN;
  162. memcpy (host, host_name, len);
  163. host[len] = '\0';
  164. snprintf (port_str, sizeof (port_str), "%d", port);
  165. result = getaddrinfo (host, port_str, &hints, &res);
  166. if (result != 0) {
  167. printf ("%s\n", gai_strerror (result));
  168. return STATE_UNKNOWN;
  169. }
  170. r = res;
  171. while (r) {
  172. /* attempt to create a socket */
  173. *sd = socket (r->ai_family, socktype, r->ai_protocol);
  174. if (*sd < 0) {
  175. printf ("%s\n", _("Socket creation failed"));
  176. freeaddrinfo (r);
  177. return STATE_UNKNOWN;
  178. }
  179. /* attempt to open a connection */
  180. result = connect (*sd, r->ai_addr, r->ai_addrlen);
  181. if (result == 0) {
  182. was_refused = FALSE;
  183. break;
  184. }
  185. if (result < 0) {
  186. switch (errno) {
  187. case ECONNREFUSED:
  188. was_refused = TRUE;
  189. break;
  190. }
  191. }
  192. close (*sd);
  193. r = r->ai_next;
  194. }
  195. freeaddrinfo (res);
  196. }
  197. /* else the hostname is interpreted as a path to a unix socket */
  198. else {
  199. if(strlen(host_name) >= UNIX_PATH_MAX){
  200. die(STATE_UNKNOWN, _("Supplied path too long unix domain socket"));
  201. }
  202. memset(&su, 0, sizeof(su));
  203. su.sun_family = AF_UNIX;
  204. strncpy(su.sun_path, host_name, UNIX_PATH_MAX);
  205. *sd = socket(PF_UNIX, SOCK_STREAM, 0);
  206. if(*sd < 0){
  207. die(STATE_UNKNOWN, _("Socket creation failed"));
  208. }
  209. result = connect(*sd, (struct sockaddr *)&su, sizeof(su));
  210. if (result < 0 && errno == ECONNREFUSED)
  211. was_refused = TRUE;
  212. }
  213. if (result == 0)
  214. return STATE_OK;
  215. else if (was_refused) {
  216. switch (econn_refuse_state) { /* a user-defined expected outcome */
  217. case STATE_OK:
  218. case STATE_WARNING: /* user wants WARN or OK on refusal */
  219. return econn_refuse_state;
  220. break;
  221. case STATE_CRITICAL: /* user did not set econn_refuse_state */
  222. printf ("%s\n", strerror(errno));
  223. return econn_refuse_state;
  224. break;
  225. default: /* it's a logic error if we do not end up in STATE_(OK|WARNING|CRITICAL) */
  226. return STATE_UNKNOWN;
  227. break;
  228. }
  229. }
  230. else {
  231. printf ("%s\n", strerror(errno));
  232. return STATE_CRITICAL;
  233. }
  234. }
  235. int
  236. send_request (int sd, int proto, const char *send_buffer, char *recv_buffer, int recv_size)
  237. {
  238. int result = STATE_OK;
  239. int send_result;
  240. int recv_result;
  241. struct timeval tv;
  242. fd_set readfds;
  243. send_result = send (sd, send_buffer, strlen (send_buffer), 0);
  244. if (send_result<0 || (size_t)send_result!=strlen(send_buffer)) {
  245. printf ("%s\n", _("Send failed"));
  246. result = STATE_WARNING;
  247. }
  248. /* wait up to the number of seconds for socket timeout minus one
  249. for data from the host */
  250. tv.tv_sec = socket_timeout - 1;
  251. tv.tv_usec = 0;
  252. FD_ZERO (&readfds);
  253. FD_SET (sd, &readfds);
  254. select (sd + 1, &readfds, NULL, NULL, &tv);
  255. /* make sure some data has arrived */
  256. if (!FD_ISSET (sd, &readfds)) {
  257. strcpy (recv_buffer, "");
  258. printf ("%s\n", _("No data was received from host!"));
  259. result = STATE_WARNING;
  260. }
  261. else {
  262. recv_result = recv (sd, recv_buffer, (size_t)recv_size - 1, 0);
  263. if (recv_result == -1) {
  264. strcpy (recv_buffer, "");
  265. if (proto != IPPROTO_TCP)
  266. printf ("%s\n", _("Receive failed"));
  267. result = STATE_WARNING;
  268. }
  269. else
  270. recv_buffer[recv_result] = 0;
  271. /* die returned string */
  272. recv_buffer[recv_size - 1] = 0;
  273. }
  274. return result;
  275. }
  276. int
  277. is_host (const char *address)
  278. {
  279. if (is_addr (address) || is_hostname (address))
  280. return (TRUE);
  281. return (FALSE);
  282. }
  283. void
  284. host_or_die(const char *str)
  285. {
  286. if(!str || (!is_addr(str) && !is_hostname(str)))
  287. usage_va(_("Invalid hostname/address - %s"), str);
  288. }
  289. int
  290. is_addr (const char *address)
  291. {
  292. #ifdef USE_IPV6
  293. if (address_family == AF_INET && is_inet_addr (address))
  294. return TRUE;
  295. else if (address_family == AF_INET6 && is_inet6_addr (address))
  296. return TRUE;
  297. #else
  298. if (is_inet_addr (address))
  299. return (TRUE);
  300. #endif
  301. return (FALSE);
  302. }
  303. int
  304. resolve_host_or_addr (const char *address, int family)
  305. {
  306. struct addrinfo hints;
  307. struct addrinfo *res;
  308. int retval;
  309. memset (&hints, 0, sizeof (hints));
  310. hints.ai_family = family;
  311. retval = getaddrinfo (address, NULL, &hints, &res);
  312. if (retval != 0)
  313. return FALSE;
  314. else {
  315. freeaddrinfo (res);
  316. return TRUE;
  317. }
  318. }