summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--configure.in71
-rw-r--r--lib/Makefile.am3
-rw-r--r--lib/getaddrinfo.c312
-rw-r--r--lib/getaddrinfo.h67
-rw-r--r--lib/gethostbyname.c228
-rw-r--r--lib/gethostbyname.h103
-rw-r--r--plugins/check_pgsql.c1
-rw-r--r--plugins/common.h.in7
-rw-r--r--plugins/netutils.c2
-rw-r--r--plugins/netutils.h.in3
-rwxr-xr-xtools/setup2
11 files changed, 783 insertions, 16 deletions
diff --git a/configure.in b/configure.in
index ef0d06b6..7fccb98d 100644
--- a/configure.in
+++ b/configure.in
@@ -11,6 +11,7 @@ PACKAGE_RELEASE="${REL}"
11AC_SUBST(PACKAGE_RELEASE) 11AC_SUBST(PACKAGE_RELEASE)
12AM_INIT_AUTOMAKE(nagios-plugins,${PACKAGE_VERSION}) 12AM_INIT_AUTOMAKE(nagios-plugins,${PACKAGE_VERSION})
13AM_CONFIG_HEADER(plugins/config.h plugins/common.h plugins/version.h plugins/netutils.h plugins/utils.h plugins/popen.h) 13AM_CONFIG_HEADER(plugins/config.h plugins/common.h plugins/version.h plugins/netutils.h plugins/utils.h plugins/popen.h)
14AC_CANONICAL_HOST
14 15
15AC_PREFIX_DEFAULT(/usr/local/nagios) 16AC_PREFIX_DEFAULT(/usr/local/nagios)
16 17
@@ -52,18 +53,38 @@ SUPPORT="Send email to nagios-users@lists.sourceforge.net if you have questions\
52AC_SUBST(SUPPORT) 53AC_SUBST(SUPPORT)
53 54
54dnl CGIURL has changed for Nagios with 1.0 beta 55dnl CGIURL has changed for Nagios with 1.0 beta
55AC_ARG_WITH(cgiurl,--with-cgiurl=<dir> sets URL for cgi programs,cgiurl=$withval,cgiurl=/nagios/cgi-bin) 56dnl AC_ARG_WITH(cgiurl,--with-cgiurl=<dir> sets URL for cgi programs,cgiurl=$withval,cgiurl=/nagios/cgi-bin)
57AC_ARG_WITH(cgiurl,
58 ACX_HELP_STRING([--with-cgiurl=DIR],
59 [sets URL for cgi programs]),
60 cgiurl=$withval,
61 cgiurl=/nagios/cgi-bin)
56CGIURL="$cgiurl" 62CGIURL="$cgiurl"
57AC_DEFINE_UNQUOTED(CGIURL,"$CGIURL",[URL of CGI programs]) 63AC_DEFINE_UNQUOTED(CGIURL,"$CGIURL",[URL of CGI programs])
58 64
59AC_ARG_WITH(nagios_user,--with-nagios-user=<user> sets user name to run nagios,nagios_usr=$withval,nagios_usr=nagios) 65dnl AC_ARG_WITH(nagios_user,--with-nagios-user=<user> sets user name to run nagios,nagios_usr=$withval,nagios_usr=nagios)
60AC_ARG_WITH(nagios_group,--with-nagios-group=<group> sets group name to run nagios,nagios_grp=$withval,nagios_grp=nagios) 66AC_ARG_WITH(nagios_user,
67 ACX_HELP_STRING([--with-nagios-user=USER],
68 [set user name to run nagios]),
69 nagios_usr=$withval,
70 nagios_usr=nagios)
71dnl AC_ARG_WITH(nagios_group,--with-nagios-group=<group> sets group name to run nagios,nagios_grp=$withval,nagios_grp=nagios)
72AC_ARG_WITH(nagios_group,
73 ACX_HELP_STRING([--with-nagios-group=GROUP],
74 [set group name to run nagios]),
75 nagios_grp=$withval,
76 nagios_grp=nagios)
61AC_SUBST(nagios_usr) 77AC_SUBST(nagios_usr)
62AC_SUBST(nagios_grp) 78AC_SUBST(nagios_grp)
63INSTALL_OPTS="-o $nagios_usr -g $nagios_grp" 79INSTALL_OPTS="-o $nagios_usr -g $nagios_grp"
64AC_SUBST(INSTALL_OPTS) 80AC_SUBST(INSTALL_OPTS)
65 81
66AC_ARG_WITH(trusted_path,--with-trusted-path=/bin:/sbin:/usr/bin:/usr/sbin sets trusted path for executables called by scripts,trusted_path=$withval,trusted_path=/bin:/sbin:/usr/bin:/usr/sbin) 82dnl AC_ARG_WITH(trusted_path,--with-trusted-path=/bin:/sbin:/usr/bin:/usr/sbin sets trusted path for executables called by scripts,trusted_path=$withval,trusted_path=/bin:/sbin:/usr/bin:/usr/sbin)
83AC_ARG_WITH(trusted_path,
84 ACX_HELP_STRING([--with-trusted-path=PATH],
85 [sets trusted path for executables called by scripts (default=/bin:/sbin:/usr/bin:/usr/sbin)]),
86 trusted_path=$withval,
87 trusted_path=/bin:/sbin:/usr/bin:/usr/sbin)
67AC_SUBST(trusted_path) 88AC_SUBST(trusted_path)
68 89
69EXTRAS= 90EXTRAS=
@@ -84,6 +105,18 @@ AC_PATH_PROG(SH,sh)
84AC_PATH_PROG(HOSTNAME,hostname) 105AC_PATH_PROG(HOSTNAME,hostname)
85AC_PATH_PROG(BASENAME,basename) 106AC_PATH_PROG(BASENAME,basename)
86 107
108dnl
109dnl Check for miscellaneous stuff
110dnl
111
112case $host_vender-$host_os in
113sun*)
114 AC_DEFINE(__EXTENSIONS__,1,[Sun's netdb.h needs this for getaddrinfo])
115 ;;
116osf*)
117 AC_DEFINE(_OSF_SOURCE,1,[OSF needs this for getaddrinfo])
118 ;;
119esac
87 120
88dnl 121dnl
89dnl Checks for libraries. 122dnl Checks for libraries.
@@ -98,7 +131,11 @@ AC_SUBST(SOCKETLIBS)
98dnl Check for PostgreSQL libraries 131dnl Check for PostgreSQL libraries
99_SAVEDLIBS="$LIBS" 132_SAVEDLIBS="$LIBS"
100_SAVEDCPPFLAGS="$CPPFLAGS" 133_SAVEDCPPFLAGS="$CPPFLAGS"
101AC_ARG_WITH(pgsql,--with-pgsql=<dir> sets path to pgsql installation,[PGSQL=$withval]) 134dnl AC_ARG_WITH(pgsql,--with-pgsql=<dir> sets path to pgsql installation,[PGSQL=$withval])
135AC_ARG_WITH(pgsql,
136 ACX_HELP_STRING([--with-pgsql=DIR],
137 [sets path to pgsql installation]),
138 PGSQL=$withval,)
102AC_CHECK_LIB(crypt,main) 139AC_CHECK_LIB(crypt,main)
103if test "$ac_cv_lib_crypt_main" = "yes"; then 140if test "$ac_cv_lib_crypt_main" = "yes"; then
104 if test -n "$PGSQL"; then 141 if test -n "$PGSQL"; then
@@ -175,7 +212,11 @@ LIBS="$_SAVEDLIBS"
175dnl Check for mysql libraries 212dnl Check for mysql libraries
176_SAVEDLIBS="$LIBS" 213_SAVEDLIBS="$LIBS"
177_SAVEDCPPFLAGS="$CPPFLAGS" 214_SAVEDCPPFLAGS="$CPPFLAGS"
178AC_ARG_WITH(mysql,--with-mysql=<dir> sets path to mysql installation (assumes lib/mysql and include subdirs),[MYSQL=$withval]) 215dnl AC_ARG_WITH(mysql,--with-mysql=<dir> sets path to mysql installation (assumes lib/mysql and include subdirs),[MYSQL=$withval])
216AC_ARG_WITH(mysql,
217 ACX_HELP_STRING([--with-mysql=DIR],
218 [sets path to mysql installation (assumes lib/mysql and include subdirs]),
219 MYSQL=$withval,)
179if test -n "$MYSQL"; then 220if test -n "$MYSQL"; then
180 MYSQLLIBDIR=$MYSQL/lib/mysql 221 MYSQLLIBDIR=$MYSQL/lib/mysql
181 CPPFLAGS="-I$MYSQL/include" 222 CPPFLAGS="-I$MYSQL/include"
@@ -229,7 +270,11 @@ elif test "$OPENSSL" = "/usr/local/bin/openssl"; then
229elif test "$OPENSSL" = "/usr/local/ssl/bin/openssl"; then 270elif test "$OPENSSL" = "/usr/local/ssl/bin/openssl"; then
230 OPENSSL=/usr/local/ssl 271 OPENSSL=/usr/local/ssl
231fi 272fi
232AC_ARG_WITH(openssl,--with-openssl=<dir> sets path to openssl installation,[OPENSSL=$withval]) 273dnl AC_ARG_WITH(openssl,--with-openssl=<dir> sets path to openssl installation,[OPENSSL=$withval])
274AC_ARG_WITH(openssl,
275 ACX_HELP_STRING([--with-openssl=DIR],
276 [sets path to openssl installation]),
277 OPENSSL=$withval,)
233 278
234dnl ######################################################################### 279dnl #########################################################################
235dnl Check if Posix getaddrinfo() is available. It is also possible to use 280dnl Check if Posix getaddrinfo() is available. It is also possible to use
@@ -785,7 +830,11 @@ else
785 echo "** Unable to find usable ps syntax" 830 echo "** Unable to find usable ps syntax"
786fi 831fi
787 832
788AC_ARG_WITH(df_command,--with-df-command=<syntax> sets syntax for df,DF_COMMAND=$withval) 833dnl AC_ARG_WITH(df_command,--with-df-command=<syntax>,sets syntax for df,DF_COMMAND=$withval)
834AC_ARG_WITH(df_command,
835 ACX_HELP_STRING([--with-df-command=SYNTAX],
836 [sets syntax for df]),
837 DF_COMMAND=$withval,)
789if test -n "$DF_COMMAND" 838if test -n "$DF_COMMAND"
790then 839then
791 AC_DEFINE_UNQUOTED(DF_COMMAND,"$DF_COMMAND",[path and args for df command]) 840 AC_DEFINE_UNQUOTED(DF_COMMAND,"$DF_COMMAND",[path and args for df command])
@@ -815,7 +864,11 @@ fi
815 864
816AC_PATH_PROG(PATH_TO_PING,ping) 865AC_PATH_PROG(PATH_TO_PING,ping)
817 866
818AC_ARG_WITH(ping_command,--with-ping-command=<syntax> sets syntax for ping,PING_COMMAND=$withval) 867dnl AC_ARG_WITH(ping_command,--with-ping-command=<syntax> sets syntax for ping,PING_COMMAND=$withval)
868AC_ARG_WITH(ping_command,
869 ACX_HELP_STRING([--with-ping-command=SYNTAX],
870 [sets syntax for ping]),
871 PING_COMMAND=$withval,)
819if test -n "$PING_COMMAND" 872if test -n "$PING_COMMAND"
820then 873then
821 echo " ping syntax... (command-line) $PING_COMMAND" 874 echo " ping syntax... (command-line) $PING_COMMAND"
diff --git a/lib/Makefile.am b/lib/Makefile.am
index d82f138b..7826a286 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -2,9 +2,10 @@
2 2
3noinst_LIBRARIES = libnagiosplug.a 3noinst_LIBRARIES = libnagiosplug.a
4 4
5noinst_HEADERS = getopt.h 5noinst_HEADERS = getopt.h getaddrinfo.h gethostbyname.h
6 6
7libnagiosplug_a_SOURCES = getopt.c getopt1.c getloadavg.c 7libnagiosplug_a_SOURCES = getopt.c getopt1.c getloadavg.c
8 8
9INCLUDES = -I$(srcdir) 9INCLUDES = -I$(srcdir)
10 10
11EXTRA_DIST = getaddrinfo.c gethostbyname.c
diff --git a/lib/getaddrinfo.c b/lib/getaddrinfo.c
new file mode 100644
index 00000000..c958da6c
--- /dev/null
+++ b/lib/getaddrinfo.c
@@ -0,0 +1,312 @@
1/*
2 * This file is part of libESMTP, a library for submission of RFC 2822
3 * formatted electronic mail messages using the SMTP protocol described
4 * in RFC 2821.
5 *
6 * Copyright (C) 2001,2002 Brian Stafford <brian@stafford.uklinux.net>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23/* An emulation of the RFC 2553 / Posix getaddrinfo resolver interface.
24 */
25
26#ifdef HAVE_CONFIG_H
27#include <config.h>
28#endif
29
30/* Need to turn off Posix features in glibc to build this */
31#undef _POSIX_C_SOURCE
32#undef _XOPEN_SOURCE
33
34#include <stdlib.h>
35#include <string.h>
36#include <ctype.h>
37#include <errno.h>
38
39#include <sys/socket.h>
40#include <netinet/in.h>
41#include <arpa/inet.h>
42
43#include <netdb.h>
44
45#include "gethostbyname.h"
46#include "getaddrinfo.h"
47
48static struct addrinfo *
49dup_addrinfo (struct addrinfo *info, void *addr, size_t addrlen)
50{
51 struct addrinfo *ret;
52
53 ret = malloc (sizeof (struct addrinfo));
54 if (ret == NULL)
55 return NULL;
56 memcpy (ret, info, sizeof (struct addrinfo));
57 ret->ai_addr = malloc (addrlen);
58 if (ret->ai_addr == NULL)
59 {
60 free (ret);
61 return NULL;
62 }
63 memcpy (ret->ai_addr, addr, addrlen);
64 ret->ai_addrlen = addrlen;
65 return ret;
66}
67
68int
69getaddrinfo (const char *nodename, const char *servname,
70 const struct addrinfo *hints, struct addrinfo **res)
71{
72 struct hostent *hp;
73 struct servent *servent;
74 const char *socktype;
75 int port;
76 struct addrinfo hint, result;
77 struct addrinfo *ai, *sai, *eai;
78 struct ghbnctx ghbnctx;
79 char **addrs;
80 int code;
81
82 memset (&result, 0, sizeof result);
83
84 /* default for hints */
85 if (hints == NULL)
86 {
87 memset (&hint, 0, sizeof hint);
88 hint.ai_family = PF_UNSPEC;
89 hints = &hint;
90 }
91
92 /* servname must not be NULL in this implementation */
93 if (servname == NULL)
94 return EAI_NONAME;
95
96 /* check for tcp or udp sockets only */
97 if (hints->ai_socktype == SOCK_STREAM)
98 socktype = "tcp";
99 else if (hints->ai_socktype == SOCK_DGRAM)
100 socktype = "udp";
101 else
102 return EAI_SERVICE;
103 result.ai_socktype = hints->ai_socktype;
104
105 /* Note: maintain port in host byte order to make debugging easier */
106 if (isdigit (*servname))
107 port = strtol (servname, NULL, 10);
108 else if ((servent = getservbyname (servname, socktype)) != NULL)
109 port = ntohs (servent->s_port);
110 else
111 return EAI_NONAME;
112
113 /* if nodename == NULL refer to the local host for a client or any
114 for a server */
115 if (nodename == NULL)
116 {
117 struct sockaddr_in sin;
118
119 /* check protocol family is PF_UNSPEC or PF_INET - could try harder
120 for IPv6 but that's more code than I'm prepared to write */
121 if (hints->ai_family == PF_UNSPEC || hints->ai_family == PF_INET)
122 result.ai_family = AF_INET;
123 else
124 return EAI_FAMILY;
125
126 sin.sin_family = result.ai_family;
127 sin.sin_port = htons (port);
128 if (hints->ai_flags & AI_PASSIVE)
129 sin.sin_addr.s_addr = htonl (INADDR_ANY);
130 else
131 sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
132 /* Duplicate result and addr and return */
133 *res = dup_addrinfo (&result, &sin, sizeof sin);
134 return (*res == NULL) ? EAI_MEMORY : 0;
135 }
136
137 /* If AI_NUMERIC is specified, use inet_addr to translate numbers and
138 dots notation. */
139 if (hints->ai_flags & AI_NUMERICHOST)
140 {
141 struct sockaddr_in sin;
142
143 /* check protocol family is PF_UNSPEC or PF_INET */
144 if (hints->ai_family == PF_UNSPEC || hints->ai_family == PF_INET)
145 result.ai_family = AF_INET;
146 else
147 return EAI_FAMILY;
148
149 sin.sin_family = result.ai_family;
150 sin.sin_port = htons (port);
151 sin.sin_addr.s_addr = inet_addr (nodename);
152 /* Duplicate result and addr and return */
153 *res = dup_addrinfo (&result, &sin, sizeof sin);
154 return (*res == NULL) ? EAI_MEMORY : 0;
155 }
156
157 errno = 0;
158 hp = gethostbyname_ctx (nodename, &ghbnctx);
159 if (hp == NULL)
160 {
161 if (errno != 0)
162 {
163 free_ghbnctx (&ghbnctx);
164 return EAI_SYSTEM;
165 }
166 code = h_error_ctx (&ghbnctx);
167 switch (code)
168 {
169 case HOST_NOT_FOUND: code = EAI_NODATA; break;
170 case NO_DATA: code = EAI_NODATA; break;
171#if defined(NO_ADDRESS) && NO_ADDRESS != NO_DATA
172 case NO_ADDRESS: code = EAI_NODATA; break;
173#endif
174 case NO_RECOVERY: code = EAI_FAIL; break;
175 case TRY_AGAIN: code = EAI_AGAIN; break;
176 default: code = EAI_FAIL; break;
177 }
178 free_ghbnctx (&ghbnctx);
179 return code;
180 }
181
182 /* Check that the address family is acceptable.
183 */
184 switch (hp->h_addrtype)
185 {
186 case AF_INET:
187 if (!(hints->ai_family == PF_UNSPEC || hints->ai_family == PF_INET))
188 goto eai_family;
189 break;
190#ifdef USE_IPV6
191 case AF_INET6:
192 if (!(hints->ai_family == PF_UNSPEC || hints->ai_family == PF_INET6))
193 goto eai_family;
194 break;
195#endif
196 default:
197 eai_family:
198 free_ghbnctx (&ghbnctx);
199 return EAI_FAMILY;
200 }
201
202 /* For each element pointed to by hp, create an element in the
203 result linked list. */
204 sai = eai = NULL;
205 for (addrs = hp->h_addr_list; *addrs != NULL; addrs++)
206 {
207 struct sockaddr sa;
208 size_t addrlen;
209
210 sa.sa_family = hp->h_addrtype;
211 switch (hp->h_addrtype)
212 {
213 case AF_INET:
214 ((struct sockaddr_in *) &sa)->sin_port = htons (port);
215 memcpy (&((struct sockaddr_in *) &sa)->sin_addr,
216 *addrs, hp->h_length);
217 addrlen = sizeof (struct sockaddr_in);
218 break;
219#ifdef USE_IPV6
220 case AF_INET6:
221# if SIN6_LEN
222 ((struct sockaddr_in6 *) &sa)->sin6_len = hp->h_length;
223# endif
224 ((struct sockaddr_in6 *) &sa)->sin6_port = htons (port);
225 memcpy (&((struct sockaddr_in6 *) &sa)->sin6_addr,
226 *addrs, hp->h_length);
227 addrlen = sizeof (struct sockaddr_in6);
228 break;
229#endif
230 default:
231 continue;
232 }
233
234 result.ai_family = hp->h_addrtype;
235 ai = dup_addrinfo (&result, &sa, addrlen);
236 if (ai == NULL)
237 {
238 free_ghbnctx (&ghbnctx);
239 freeaddrinfo (sai);
240 return EAI_MEMORY;
241 }
242 if (sai == NULL)
243 sai = ai;
244 else
245 eai->ai_next = ai;
246 eai = ai;
247 }
248
249 if (sai == NULL)
250 {
251 free_ghbnctx (&ghbnctx);
252 return EAI_NODATA;
253 }
254
255 if (hints->ai_flags & AI_CANONNAME)
256 {
257 sai->ai_canonname = malloc (strlen (hp->h_name) + 1);
258 if (sai->ai_canonname == NULL)
259 {
260 free_ghbnctx (&ghbnctx);
261 freeaddrinfo (sai);
262 return EAI_MEMORY;
263 }
264 strcpy (sai->ai_canonname, hp->h_name);
265 }
266
267 free_ghbnctx (&ghbnctx);
268 *res = sai;
269 return 0;
270}
271
272void
273freeaddrinfo (struct addrinfo *ai)
274{
275 struct addrinfo *next;
276
277 while (ai != NULL)
278 {
279 next = ai->ai_next;
280 if (ai->ai_canonname != NULL)
281 free (ai->ai_canonname);
282 if (ai->ai_addr != NULL)
283 free (ai->ai_addr);
284 free (ai);
285 ai = next;
286 }
287}
288
289const char *
290gai_strerror (int ecode)
291{
292 static const char *eai_descr[] =
293 {
294 "no error",
295 "address family for nodename not supported", /* EAI_ADDRFAMILY */
296 "temporary failure in name resolution", /* EAI_AGAIN */
297 "invalid value for ai_flags", /* EAI_BADFLAGS */
298 "non-recoverable failure in name resolution", /* EAI_FAIL */
299 "ai_family not supported", /* EAI_FAMILY */
300 "memory allocation failure", /* EAI_MEMORY */
301 "no address associated with nodename", /* EAI_NODATA */
302 "nodename nor servname provided, or not known", /* EAI_NONAME */
303 "servname not supported for ai_socktype", /* EAI_SERVICE */
304 "ai_socktype not supported", /* EAI_SOCKTYPE */
305 "system error returned in errno", /* EAI_SYSTEM */
306 };
307
308 if (ecode < 0 || ecode > (int) (sizeof eai_descr/ sizeof eai_descr[0]))
309 return "unknown error";
310 return eai_descr[ecode];
311}
312
diff --git a/lib/getaddrinfo.h b/lib/getaddrinfo.h
new file mode 100644
index 00000000..c474d3bc
--- /dev/null
+++ b/lib/getaddrinfo.h
@@ -0,0 +1,67 @@
1#ifndef _getaddrinfo_h
2#define _getaddrinfo_h
3/*
4 * This file is part of libESMTP, a library for submission of RFC 2822
5 * formatted electronic mail messages using the SMTP protocol described
6 * in RFC 2821.
7 *
8 * Copyright (C) 2001,2002 Brian Stafford <brian@stafford.uklinux.net>
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24
25/* Structure and prototypes aken from RFC 2553 */
26
27struct addrinfo
28 {
29 int ai_flags; /* AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST */
30 int ai_family; /* PF_xxx */
31 int ai_socktype; /* SOCK_xxx */
32 int ai_protocol; /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
33 size_t ai_addrlen; /* length of ai_addr */
34 char *ai_canonname; /* canonical name for nodename */
35 struct sockaddr *ai_addr; /* binary address */
36 struct addrinfo *ai_next; /* next structure in linked list */
37 };
38
39/* Supposed to be defined in <netdb.h> */
40#define AI_PASSIVE 1 /* Socket address is intended for `bind'. */
41#define AI_CANONNAME 2 /* Request for canonical name. */
42#define AI_NUMERICHOST 4 /* Don't use name resolution. */
43
44/* Supposed to be defined in <netdb.h> */
45#define EAI_ADDRFAMILY 1 /* address family for nodename not supported */
46#define EAI_AGAIN 2 /* temporary failure in name resolution */
47#define EAI_BADFLAGS 3 /* invalid value for ai_flags */
48#define EAI_FAIL 4 /* non-recoverable failure in name resolution */
49#define EAI_FAMILY 5 /* ai_family not supported */
50#define EAI_MEMORY 6 /* memory allocation failure */
51#define EAI_NODATA 7 /* no address associated with nodename */
52#define EAI_NONAME 8 /* nodename nor servname provided, or not known */
53#define EAI_SERVICE 9 /* servname not supported for ai_socktype */
54#define EAI_SOCKTYPE 10 /* ai_socktype not supported */
55#define EAI_SYSTEM 11 /* system error returned in errno */
56
57/* RFC 2553 / Posix resolver */
58int getaddrinfo (const char *nodename, const char *servname,
59 const struct addrinfo *hints, struct addrinfo **res);
60
61/* Free addrinfo structure and associated storage */
62void freeaddrinfo (struct addrinfo *ai);
63
64/* Convert error return from getaddrinfo() to string */
65const char *gai_strerror (int code);
66
67#endif
diff --git a/lib/gethostbyname.c b/lib/gethostbyname.c
new file mode 100644
index 00000000..d151606d
--- /dev/null
+++ b/lib/gethostbyname.c
@@ -0,0 +1,228 @@
1/*
2 * This file is a ghastly hack because nobody can agree on
3 * gethostbyname_r()'s prototype.
4 *
5 * Copyright (C) 2001,2002 Brian Stafford <brian@stafford.uklinux.net>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22#ifdef HAVE_CONFIG_H
23#include <config.h>
24#endif
25
26#define _SVID_SOURCE 1 /* Need this to get gethostbyname_r() */
27
28#include <assert.h>
29
30#include <stdlib.h>
31#include <string.h>
32#include <netdb.h>
33#include <errno.h>
34
35#include "gethostbyname.h"
36
37#if HAVE_GETIPNODEBYNAME
38
39void
40free_ghbnctx (struct ghbnctx *ctx)
41{
42 assert (ctx != NULL);
43
44 if (ctx->hostent != NULL)
45 freehostent (ctx->hostent);
46}
47
48struct hostent *
49gethostbyname_ctx (const char *host, struct ghbnctx *ctx)
50{
51 assert (ctx != NULL);
52
53 memset (ctx, 0, sizeof (struct ghbnctx));
54 ctx->hostent = getipnodebyname (host, AF_UNSPEC, AI_ADDRCONFIG, &ctx->h_err);
55 return ctx->hostent;
56}
57
58int
59h_error_ctx (struct ghbnctx *ctx)
60{
61 assert (ctx != NULL);
62
63 return ctx->h_err;
64}
65
66#elif HAVE_GETHOSTBYNAME_R == 6
67
68void
69free_ghbnctx (struct ghbnctx *ctx)
70{
71 assert (ctx != NULL);
72
73 if (ctx->hostbuf != NULL)
74 free (ctx->hostbuf);
75}
76
77struct hostent *
78gethostbyname_ctx (const char *host, struct ghbnctx *ctx)
79{
80 struct hostent *hp;
81 char *tmp;
82 int err;
83
84 assert (ctx != NULL);
85
86 memset (ctx, 0, sizeof (struct ghbnctx));
87 ctx->hostbuf_len = 2048;
88 if ((ctx->hostbuf = malloc (ctx->hostbuf_len)) == NULL)
89 {
90 errno = ENOMEM;
91 return NULL;
92 }
93 while ((err = gethostbyname_r (host,
94 &ctx->hostent, ctx->hostbuf, ctx->hostbuf_len,
95 &hp, &ctx->h_err)) == ERANGE)
96 {
97 ctx->hostbuf_len += 1024;
98 if ((tmp = realloc (ctx->hostbuf, ctx->hostbuf_len)) == NULL)
99 {
100 errno = ENOMEM;
101 return NULL;
102 }
103 ctx->hostbuf = tmp;
104 }
105 if (err != 0)
106 {
107 errno = err;
108 return NULL;
109 }
110 return hp;
111}
112
113int
114h_error_ctx (struct ghbnctx *ctx)
115{
116 assert (ctx != NULL);
117
118 return ctx->h_err;
119}
120
121#elif HAVE_GETHOSTBYNAME_R == 5
122
123void
124free_ghbnctx (struct ghbnctx *ctx)
125{
126 assert (ctx != NULL);
127
128 if (ctx->hostbuf != NULL)
129 free (ctx->hostbuf);
130}
131
132struct hostent *
133gethostbyname_ctx (const char *host, struct ghbnctx *ctx)
134{
135 struct hostent *hp;
136 char *tmp;
137
138 assert (ctx != NULL);
139
140 memset (ctx, 0, sizeof (struct ghbnctx));
141 ctx->hostbuf_len = 2048;
142 if ((ctx->hostbuf = malloc (ctx->hostbuf_len)) == NULL)
143 {
144 errno = ENOMEM;
145 return NULL;
146 }
147 while ((hp = gethostbyname_r (host, &ctx->hostent,
148 ctx->hostbuf, ctx->hostbuf_len,
149 &ctx->h_err)) == NULL && errno == ERANGE)
150 {
151 ctx->hostbuf_len += 1024;
152 if ((tmp = realloc (ctx->hostbuf, ctx->hostbuf_len)) == NULL)
153 {
154 errno = ENOMEM;
155 return NULL;
156 }
157 ctx->hostbuf = tmp;
158 }
159 return hp;
160}
161
162int
163h_error_ctx (struct ghbnctx *ctx)
164{
165 assert (ctx != NULL);
166
167 return ctx->h_err;
168}
169
170#elif HAVE_GETHOSTBYNAME_R == 3
171
172void
173free_ghbnctx (struct ghbnctx *ctx)
174{
175 assert (ctx != NULL);
176
177 /* FIXME: does this need to do anything? */
178}
179
180struct hostent *
181gethostbyname_ctx (const char *host, struct ghbnctx *ctx)
182{
183 assert (ctx != NULL);
184
185 if (!gethostbyname_r (host, &ctx->hostent, &ctx->hostent_data))
186 {
187 ctx->h_err = h_errno; /* FIXME: is this correct? */
188 return NULL;
189 }
190 return &ctx->hostent;
191}
192
193int
194h_error_ctx (struct ghbnctx *ctx)
195{
196 assert (ctx != NULL);
197
198 return ctx->h_err;
199}
200
201#else
202
203void
204free_ghbnctx (struct ghbnctx *ctx __attribute__ ((unused)))
205{
206 assert (ctx != NULL);
207}
208
209struct hostent *
210gethostbyname_ctx (const char *host, struct ghbnctx *ctx)
211{
212 struct hostent *hp;
213
214 hp = gethostbyname (host);
215 if (hp == NULL)
216 ctx->h_err = h_errno;
217 return hp;
218}
219
220int
221h_error_ctx (struct ghbnctx *ctx)
222{
223 assert (ctx != NULL);
224
225 return ctx->h_err;
226}
227
228#endif
diff --git a/lib/gethostbyname.h b/lib/gethostbyname.h
new file mode 100644
index 00000000..2b963997
--- /dev/null
+++ b/lib/gethostbyname.h
@@ -0,0 +1,103 @@
1/*
2 * This file is a ghastly hack because nobody can agree on
3 * gethostbyname_r()'s prototype.
4 *
5 * Copyright (C) 2001,2002 Brian Stafford <brian@stafford.uklinux.net>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22/*************************************************************************
23 Usage:
24
25 #include <errno.h>
26 #include "gethostbyname.h"
27
28 f ()
29 {
30 struct ghbnctx ctx;
31
32 errno = 0;
33 hp = gethostbyname_ctx (host, &ctx);
34 if (hp == NULL)
35 {
36 if (errno != 0)
37 handle_value_of_errno (errno);
38 else
39 handle_value_of_h_errno (h_error_ctx (&ctx));
40 }
41 else
42 {
43 ...
44 }
45 free_ghbnctx (&ctx);
46 }
47 *************************************************************************/
48
49#ifndef _gethostbyname_h
50#define _gethostbyname_h
51
52#if HAVE_GETIPNODEBYNAME
53
54struct ghbnctx
55 {
56 int h_err;
57 struct hostent *hostent;
58 };
59
60#elif HAVE_GETHOSTBYNAME_R == 6
61
62struct ghbnctx
63 {
64 int h_err;
65 struct hostent hostent;
66 char *hostbuf;
67 size_t hostbuf_len;
68 };
69
70#elif HAVE_GETHOSTBYNAME_R == 5
71
72struct ghbnctx
73 {
74 int h_err;
75 struct hostent hostent;
76 char *hostbuf;
77 int hostbuf_len;
78 };
79
80#elif HAVE_GETHOSTBYNAME_R == 3
81
82struct ghbnctx
83 {
84 int h_err;
85 struct hostent_data hostent_data;
86 struct hostent hostent;
87 };
88
89#else
90
91struct ghbnctx
92 {
93 int h_err;
94 };
95
96#endif
97
98struct hostent *gethostbyname_ctx (const char *host, struct ghbnctx *ctx);
99int h_error_ctx (struct ghbnctx *ctx);
100void free_ghbnctx (struct ghbnctx *ctx);
101
102#endif
103
diff --git a/plugins/check_pgsql.c b/plugins/check_pgsql.c
index f8419dca..2033b8c1 100644
--- a/plugins/check_pgsql.c
+++ b/plugins/check_pgsql.c
@@ -79,7 +79,6 @@ enum {
79#include "config.h" 79#include "config.h"
80#include "common.h" 80#include "common.h"
81#include "utils.h" 81#include "utils.h"
82#include <netdb.h>
83#include <libpq-fe.h> 82#include <libpq-fe.h>
84 83
85int process_arguments (int, char **); 84int process_arguments (int, char **);
diff --git a/plugins/common.h.in b/plugins/common.h.in
index 48e7f6f4..09df00dd 100644
--- a/plugins/common.h.in
+++ b/plugins/common.h.in
@@ -72,6 +72,13 @@
72 72
73#include <ctype.h> 73#include <ctype.h>
74 74
75#if HAVE_LWRES_NETDB_H
76#include <lwres/netdb.h>
77#elif !HAVE_GETADDRINFO
78#include "getaddrinfo.h"
79#else
80#include <netdb.h>
81#endif
75 82
76/* 83/*
77 * 84 *
diff --git a/plugins/netutils.c b/plugins/netutils.c
index e5d35281..fc85a246 100644
--- a/plugins/netutils.c
+++ b/plugins/netutils.c
@@ -31,10 +31,8 @@
31 31
32#include "config.h" 32#include "config.h"
33#include "common.h" 33#include "common.h"
34#include <sys/socket.h>
35#include <netinet/in.h> 34#include <netinet/in.h>
36#include <arpa/inet.h> 35#include <arpa/inet.h>
37#include <netdb.h>
38 36
39extern int socket_timeout; 37extern int socket_timeout;
40RETSIGTYPE socket_timeout_alarm_handler (int); 38RETSIGTYPE socket_timeout_alarm_handler (int);
diff --git a/plugins/netutils.h.in b/plugins/netutils.h.in
index be4ae241..1907ab7e 100644
--- a/plugins/netutils.h.in
+++ b/plugins/netutils.h.in
@@ -34,10 +34,9 @@
34#undef HAVE_STRINGS_H 34#undef HAVE_STRINGS_H
35#undef HAVE_STRING_H 35#undef HAVE_STRING_H
36 36
37#include <sys/socket.h> 37#include "common.h"
38#include <netinet/in.h> 38#include <netinet/in.h>
39#include <arpa/inet.h> 39#include <arpa/inet.h>
40#include <netdb.h>
41 40
42void socket_timeout_alarm_handler (int); 41void socket_timeout_alarm_handler (int);
43 42
diff --git a/tools/setup b/tools/setup
index 18281a31..b825a6b2 100755
--- a/tools/setup
+++ b/tools/setup
@@ -3,7 +3,7 @@
3aclocal -I lib 3aclocal -I lib
4autoconf 4autoconf
5autoheader 5autoheader
6automake 6automake --add-missing --copy
7 7
8if [ -f debian/rules ] ; then 8if [ -f debian/rules ] ; then
9 chmod +x debian/rules 9 chmod +x debian/rules