netutils.c 10 KB

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