diff -pruN /home/reed/src/isc/libbind/libbind/irs/Lint_htonl.c /usr/src/lib/libc/net/Lint_htonl.c --- /home/reed/src/isc/libbind/libbind/irs/Lint_htonl.c 1969-12-31 18:00:00.000000000 -0600 +++ /usr/src/lib/libc/net/Lint_htonl.c 2001-08-22 02:42:08.000000000 -0500 @@ -0,0 +1,17 @@ +/* $NetBSD: Lint_htonl.c,v 1.4 2001/08/22 07:42:08 itojun Exp $ */ + +/* + * This file placed in the public domain. + * Chris Demetriou, November 5, 1997. + */ + +#include +#undef htonl + +/*ARGSUSED*/ +uint32_t +htonl(host32) + uint32_t host32; +{ + return (0); +} diff -pruN /home/reed/src/isc/libbind/libbind/irs/Lint_htons.c /usr/src/lib/libc/net/Lint_htons.c --- /home/reed/src/isc/libbind/libbind/irs/Lint_htons.c 1969-12-31 18:00:00.000000000 -0600 +++ /usr/src/lib/libc/net/Lint_htons.c 2001-08-22 02:42:09.000000000 -0500 @@ -0,0 +1,17 @@ +/* $NetBSD: Lint_htons.c,v 1.4 2001/08/22 07:42:09 itojun Exp $ */ + +/* + * This file placed in the public domain. + * Chris Demetriou, November 5, 1997. + */ + +#include +#undef htons + +/*ARGSUSED*//*NOSTRICT*/ +uint16_t +htons(host16) + uint16_t host16; +{ + return (0); +} diff -pruN /home/reed/src/isc/libbind/libbind/irs/Lint_ntohl.c /usr/src/lib/libc/net/Lint_ntohl.c --- /home/reed/src/isc/libbind/libbind/irs/Lint_ntohl.c 1969-12-31 18:00:00.000000000 -0600 +++ /usr/src/lib/libc/net/Lint_ntohl.c 2001-08-22 02:42:09.000000000 -0500 @@ -0,0 +1,17 @@ +/* $NetBSD: Lint_ntohl.c,v 1.4 2001/08/22 07:42:09 itojun Exp $ */ + +/* + * This file placed in the public domain. + * Chris Demetriou, November 5, 1997. + */ + +#include +#undef ntohl + +/*ARGSUSED*/ +uint32_t +ntohl(net32) + uint32_t net32; +{ + return (0); +} diff -pruN /home/reed/src/isc/libbind/libbind/irs/Lint_ntohs.c /usr/src/lib/libc/net/Lint_ntohs.c --- /home/reed/src/isc/libbind/libbind/irs/Lint_ntohs.c 1969-12-31 18:00:00.000000000 -0600 +++ /usr/src/lib/libc/net/Lint_ntohs.c 2001-08-22 02:42:09.000000000 -0500 @@ -0,0 +1,17 @@ +/* $NetBSD: Lint_ntohs.c,v 1.4 2001/08/22 07:42:09 itojun Exp $ */ + +/* + * This file placed in the public domain. + * Chris Demetriou, November 5, 1997. + */ + +#include +#undef ntohs + +/*ARGSUSED*//*NOSTRICT*/ +uint16_t +ntohs(net16) + uint16_t net16; +{ + return (0); +} diff -pruN /home/reed/src/isc/libbind/libbind/irs/base64.c /usr/src/lib/libc/net/base64.c --- /home/reed/src/isc/libbind/libbind/irs/base64.c 1969-12-31 18:00:00.000000000 -0600 +++ /usr/src/lib/libc/net/base64.c 2013-06-05 09:26:12.000000000 -0500 @@ -0,0 +1,346 @@ +/* $NetBSD: base64.c,v 1.14 2012/06/25 22:32:44 abs Exp $ */ + +/* + * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") + * Copyright (c) 1996-1999 by Internet Software Consortium. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT + * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +/* + * Portions Copyright (c) 1995 by International Business Machines, Inc. + * + * International Business Machines, Inc. (hereinafter called IBM) grants + * permission under its copyrights to use, copy, modify, and distribute this + * Software with or without fee, provided that the above copyright notice and + * all paragraphs of this notice appear in all copies, and that the name of IBM + * not be used in connection with the marketing of any product incorporating + * the Software or modifications thereof, without specific, written prior + * permission. + * + * To the extent it has a right to do so, IBM grants an immunity from suit + * under its patents, if any, for the use, sale or manufacture of products to + * the extent that such products are used for performing Domain Name System + * dynamic updates in TCP/IP networks by means of the Software. No immunity is + * granted for any product per se or for any other function of any product. + * + * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES, + * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A + * PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL, + * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING + * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN + * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static const char rcsid[] = "Id: base64.c,v 1.4 2005/04/27 04:56:34 sra Exp"; +#else +__RCSID("$NetBSD: base64.c,v 1.14 2012/06/25 22:32:44 abs Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "port_before.h" + +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include "port_after.h" + +#define Assert(Cond) if (!(Cond)) abort() + +static const char Base64[] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; +static const char Pad64 = '='; + +/* (From RFC1521 and draft-ietf-dnssec-secext-03.txt) + The following encoding technique is taken from RFC1521 by Borenstein + and Freed. It is reproduced here in a slightly edited form for + convenience. + + A 65-character subset of US-ASCII is used, enabling 6 bits to be + represented per printable character. (The extra 65th character, "=", + is used to signify a special processing function.) + + The encoding process represents 24-bit groups of input bits as output + strings of 4 encoded characters. Proceeding from left to right, a + 24-bit input group is formed by concatenating 3 8-bit input groups. + These 24 bits are then treated as 4 concatenated 6-bit groups, each + of which is translated into a single digit in the base64 alphabet. + + Each 6-bit group is used as an index into an array of 64 printable + characters. The character referenced by the index is placed in the + output string. + + Table 1: The Base64 Alphabet + + Value Encoding Value Encoding Value Encoding Value Encoding + 0 A 17 R 34 i 51 z + 1 B 18 S 35 j 52 0 + 2 C 19 T 36 k 53 1 + 3 D 20 U 37 l 54 2 + 4 E 21 V 38 m 55 3 + 5 F 22 W 39 n 56 4 + 6 G 23 X 40 o 57 5 + 7 H 24 Y 41 p 58 6 + 8 I 25 Z 42 q 59 7 + 9 J 26 a 43 r 60 8 + 10 K 27 b 44 s 61 9 + 11 L 28 c 45 t 62 + + 12 M 29 d 46 u 63 / + 13 N 30 e 47 v + 14 O 31 f 48 w (pad) = + 15 P 32 g 49 x + 16 Q 33 h 50 y + + Special processing is performed if fewer than 24 bits are available + at the end of the data being encoded. A full encoding quantum is + always completed at the end of a quantity. When fewer than 24 input + bits are available in an input group, zero bits are added (on the + right) to form an integral number of 6-bit groups. Padding at the + end of the data is performed using the '=' character. + + Since all base64 input is an integral number of octets, only the + ------------------------------------------------- + following cases can arise: + + (1) the final quantum of encoding input is an integral + multiple of 24 bits; here, the final unit of encoded + output will be an integral multiple of 4 characters + with no "=" padding, + (2) the final quantum of encoding input is exactly 8 bits; + here, the final unit of encoded output will be two + characters followed by two "=" padding characters, or + (3) the final quantum of encoding input is exactly 16 bits; + here, the final unit of encoded output will be three + characters followed by one "=" padding character. + */ + +int +b64_ntop(u_char const *src, size_t srclength, char *target, size_t targsize) { + size_t datalength = 0; + u_char input[3]; + u_char output[4]; + size_t i; + + _DIAGASSERT(src != NULL); + _DIAGASSERT(target != NULL); + + while (2U < srclength) { + input[0] = *src++; + input[1] = *src++; + input[2] = *src++; + srclength -= 3; + + output[0] = (u_int32_t)input[0] >> 2; + output[1] = ((u_int32_t)(input[0] & 0x03) << 4) + + ((u_int32_t)input[1] >> 4); + output[2] = ((u_int32_t)(input[1] & 0x0f) << 2) + + ((u_int32_t)input[2] >> 6); + output[3] = input[2] & 0x3f; + Assert(output[0] < 64); + Assert(output[1] < 64); + Assert(output[2] < 64); + Assert(output[3] < 64); + + if (datalength + 4 > targsize) + return (-1); + target[datalength++] = Base64[output[0]]; + target[datalength++] = Base64[output[1]]; + target[datalength++] = Base64[output[2]]; + target[datalength++] = Base64[output[3]]; + } + + /* Now we worry about padding. */ + if (0U != srclength) { + /* Get what's left. */ + input[0] = input[1] = input[2] = '\0'; + for (i = 0; i < srclength; i++) + input[i] = *src++; + + output[0] = (u_int32_t)input[0] >> 2; + output[1] = ((u_int32_t)(input[0] & 0x03) << 4) + + ((u_int32_t)input[1] >> 4); + output[2] = ((u_int32_t)(input[1] & 0x0f) << 2) + + ((u_int32_t)input[2] >> 6); + Assert(output[0] < 64); + Assert(output[1] < 64); + Assert(output[2] < 64); + + if (datalength + 4 > targsize) + return (-1); + target[datalength++] = Base64[output[0]]; + target[datalength++] = Base64[output[1]]; + if (srclength == 1U) + target[datalength++] = Pad64; + else + target[datalength++] = Base64[output[2]]; + target[datalength++] = Pad64; + } + if (datalength >= targsize) + return (-1); + target[datalength] = '\0'; /*%< Returned value doesn't count \\0. */ + _DIAGASSERT(__type_fit(int, datalength)); + return (int)datalength; +} + +/* skips all whitespace anywhere. + converts characters, four at a time, starting at (or after) + src from base - 64 numbers into three 8 bit bytes in the target area. + it returns the number of data bytes stored at the target, or -1 on error. + */ + +int +b64_pton(char const *src, u_char *target, size_t targsize) +{ + size_t tarindex; + int state, ch; + char *pos; + + _DIAGASSERT(src != NULL); + _DIAGASSERT(target != NULL); + + state = 0; + tarindex = 0; + + while ((ch = (u_char) *src++) != '\0') { + if (isspace(ch)) /*%< Skip whitespace anywhere. */ + continue; + + if (ch == Pad64) + break; + + pos = strchr(Base64, ch); + if (pos == 0) /*%< A non-base64 character. */ + return (-1); + + switch (state) { + case 0: + if (target) { + if ((size_t)tarindex >= targsize) + return (-1); + target[tarindex] = + (unsigned char)(pos - Base64) << 2; + } + state = 1; + break; + case 1: + if (target) { + if ((size_t)tarindex + 1 >= targsize) + return (-1); + target[tarindex] |= + (u_int32_t)(pos - Base64) >> 4; + target[tarindex+1] = + (unsigned char) + (((pos - Base64) & 0x0f) << 4); + } + tarindex++; + state = 2; + break; + case 2: + if (target) { + if ((size_t)tarindex + 1 >= targsize) + return (-1); + target[tarindex] |= + (u_int32_t)(pos - Base64) >> 2; + target[tarindex+1] = + (unsigned char) + (((pos - Base64) & 0x03) << 6); + } + tarindex++; + state = 3; + break; + case 3: + if (target) { + if ((size_t)tarindex >= targsize) + return (-1); + target[tarindex] |= + (unsigned char)(pos - Base64); + } + tarindex++; + state = 0; + break; + default: + abort(); + } + } + + /* + * We are done decoding Base-64 chars. Let's see if we ended + * on a byte boundary, and/or with erroneous trailing characters. + */ + + if (ch == Pad64) { /*%< We got a pad char. */ + ch = *src++; /*%< Skip it, get next. */ + switch (state) { + case 0: /*%< Invalid = in first position */ + case 1: /*%< Invalid = in second position */ + return (-1); + + case 2: /*%< Valid, means one byte of info */ + /* Skip any number of spaces. */ + for (; ch != '\0'; ch = (u_char) *src++) + if (!isspace(ch)) + break; + /* Make sure there is another trailing = sign. */ + if (ch != Pad64) + return (-1); + ch = *src++; /*%< Skip the = */ + /* Fall through to "single trailing =" case. */ + /* FALLTHROUGH */ + + case 3: /*%< Valid, means two bytes of info */ + /* + * We know this char is an =. Is there anything but + * whitespace after it? + */ + for (; ch != '\0'; ch = (u_char) *src++) + if (!isspace(ch)) + return (-1); + + /* + * Now make sure for cases 2 and 3 that the "extra" + * bits that slopped past the last full byte were + * zeros. If we don't check them, they become a + * subliminal channel. + */ + if (target && target[tarindex] != 0) + return (-1); + } + } else { + /* + * We ended by seeing the end of the string. Make sure we + * have no partial bytes lying around. + */ + if (state != 0) + return (-1); + } + + _DIAGASSERT(__type_fit(int, tarindex)); + return (int)tarindex; +} + +/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/dns.c /usr/src/lib/libc/net/dns.c --- /home/reed/src/isc/libbind/libbind/irs/dns.c 2006-03-09 17:57:56.000000000 -0600 +++ /usr/src/lib/libc/net/dns.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,154 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1996-1999 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#if defined(LIBC_SCCS) && !defined(lint) -static const char rcsid[] = "$Id: dns.c,v 1.5 2006/03/09 23:57:56 marka Exp $"; -#endif - -/*! \file - * \brief - * dns.c --- this is the top-level accessor function for the dns - */ - -#include "port_before.h" - -#include -#include -#include - -#include -#include -#include -#include - -#include - -#include -#include - -#include "port_after.h" - -#include "irs_p.h" -#include "hesiod.h" -#include "dns_p.h" - -/* forward */ - -static void dns_close(struct irs_acc *); -static struct __res_state * dns_res_get(struct irs_acc *); -static void dns_res_set(struct irs_acc *, struct __res_state *, - void (*)(void *)); - -/* public */ - -struct irs_acc * -irs_dns_acc(const char *options) { - struct irs_acc *acc; - struct dns_p *dns; - - UNUSED(options); - - if (!(acc = memget(sizeof *acc))) { - errno = ENOMEM; - return (NULL); - } - memset(acc, 0x5e, sizeof *acc); - if (!(dns = memget(sizeof *dns))) { - errno = ENOMEM; - memput(acc, sizeof *acc); - return (NULL); - } - memset(dns, 0x5e, sizeof *dns); - dns->res = NULL; - dns->free_res = NULL; - if (hesiod_init(&dns->hes_ctx) < 0) { - /* - * We allow the dns accessor class to initialize - * despite hesiod failing to initialize correctly, - * since dns host queries don't depend on hesiod. - */ - dns->hes_ctx = NULL; - } - acc->private = dns; -#ifdef WANT_IRS_GR - acc->gr_map = irs_dns_gr; -#else - acc->gr_map = NULL; -#endif -#ifdef WANT_IRS_PW - acc->pw_map = irs_dns_pw; -#else - acc->pw_map = NULL; -#endif - acc->sv_map = irs_dns_sv; - acc->pr_map = irs_dns_pr; - acc->ho_map = irs_dns_ho; - acc->nw_map = irs_dns_nw; - acc->ng_map = irs_nul_ng; - acc->res_get = dns_res_get; - acc->res_set = dns_res_set; - acc->close = dns_close; - return (acc); -} - -/* methods */ -static struct __res_state * -dns_res_get(struct irs_acc *this) { - struct dns_p *dns = (struct dns_p *)this->private; - - if (dns->res == NULL) { - struct __res_state *res; - res = (struct __res_state *)malloc(sizeof *res); - if (res == NULL) - return (NULL); - memset(res, 0, sizeof *res); - dns_res_set(this, res, free); - } - - if ((dns->res->options & RES_INIT) == 0U && - res_ninit(dns->res) < 0) - return (NULL); - - return (dns->res); -} - -static void -dns_res_set(struct irs_acc *this, struct __res_state *res, - void (*free_res)(void *)) { - struct dns_p *dns = (struct dns_p *)this->private; - - if (dns->res && dns->free_res) { - res_nclose(dns->res); - (*dns->free_res)(dns->res); - } - dns->res = res; - dns->free_res = free_res; -} - -static void -dns_close(struct irs_acc *this) { - struct dns_p *dns; - - dns = (struct dns_p *)this->private; - if (dns->res && dns->free_res) - (*dns->free_res)(dns->res); - if (dns->hes_ctx) - hesiod_end(dns->hes_ctx); - memput(dns, sizeof *dns); - memput(this, sizeof *this); -} - diff -pruN /home/reed/src/isc/libbind/libbind/irs/dns_gr.c /usr/src/lib/libc/net/dns_gr.c --- /home/reed/src/isc/libbind/libbind/irs/dns_gr.c 2005-04-26 23:56:21.000000000 -0500 +++ /usr/src/lib/libc/net/dns_gr.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,294 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1996-1999 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#if defined(LIBC_SCCS) && !defined(lint) -static const char rcsid[] = "$Id: dns_gr.c,v 1.4 2005/04/27 04:56:21 sra Exp $"; -#endif - -/*! \file - * \brief - * dns_gr.c --- this file contains the functions for accessing - * group information from Hesiod. - */ - -#include "port_before.h" - -#ifndef WANT_IRS_GR -static int __bind_irs_gr_unneeded; -#else - -#include -#include - -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -#include - -#include - -#include "port_after.h" - -#include "irs_p.h" -#include "hesiod.h" -#include "dns_p.h" - -/* Types. */ - -struct pvt { - /* - * This is our private accessor data. It has a shared hesiod context. - */ - struct dns_p * dns; - /* - * Need space to store the entries read from the group file. - * The members list also needs space per member, and the - * strings making up the user names must be allocated - * somewhere. Rather than doing lots of small allocations, - * we keep one buffer and resize it as needed. - */ - struct group group; - size_t nmemb; /*%< Malloc'd max index of gr_mem[]. */ - char * membuf; - size_t membufsize; -}; - -/* Forward. */ - -static struct group * gr_next(struct irs_gr *); -static struct group * gr_byname(struct irs_gr *, const char *); -static struct group * gr_bygid(struct irs_gr *, gid_t); -static void gr_rewind(struct irs_gr *); -static void gr_close(struct irs_gr *); -static int gr_list(struct irs_gr *, const char *, - gid_t, gid_t *, int *); -static void gr_minimize(struct irs_gr *); -static struct __res_state * gr_res_get(struct irs_gr *); -static void gr_res_set(struct irs_gr *, - struct __res_state *, - void (*)(void *)); - -static struct group * get_hes_group(struct irs_gr *this, - const char *name, - const char *type); - -/* Public. */ - -struct irs_gr * -irs_dns_gr(struct irs_acc *this) { - struct dns_p *dns = (struct dns_p *)this->private; - struct irs_gr *gr; - struct pvt *pvt; - - if (!dns || !dns->hes_ctx) { - errno = ENODEV; - return (NULL); - } - if (!(pvt = memget(sizeof *pvt))) { - errno = ENOMEM; - return (NULL); - } - memset(pvt, 0, sizeof *pvt); - pvt->dns = dns; - if (!(gr = memget(sizeof *gr))) { - memput(pvt, sizeof *pvt); - errno = ENOMEM; - return (NULL); - } - memset(gr, 0x5e, sizeof *gr); - gr->private = pvt; - gr->next = gr_next; - gr->byname = gr_byname; - gr->bygid = gr_bygid; - gr->rewind = gr_rewind; - gr->close = gr_close; - gr->list = gr_list; - gr->minimize = gr_minimize; - gr->res_get = gr_res_get; - gr->res_set = gr_res_set; - return (gr); -} - -/* methods */ - -static void -gr_close(struct irs_gr *this) { - struct pvt *pvt = (struct pvt *)this->private; - - if (pvt->group.gr_mem) - free(pvt->group.gr_mem); - if (pvt->membuf) - free(pvt->membuf); - memput(pvt, sizeof *pvt); - memput(this, sizeof *this); -} - -static struct group * -gr_next(struct irs_gr *this) { - - UNUSED(this); - - return (NULL); -} - -static struct group * -gr_byname(struct irs_gr *this, const char *name) { - return (get_hes_group(this, name, "group")); -} - -static struct group * -gr_bygid(struct irs_gr *this, gid_t gid) { - char name[32]; - - sprintf(name, "%ld", (long)gid); - return (get_hes_group(this, name, "gid")); -} - -static void -gr_rewind(struct irs_gr *this) { - - UNUSED(this); - - /* NOOP */ -} - -static int -gr_list(struct irs_gr *this, const char *name, - gid_t basegid, gid_t *groups, int *ngroups) -{ - UNUSED(this); - UNUSED(name); - UNUSED(basegid); - UNUSED(groups); - - *ngroups = 0; - /* There's some way to do this in Hesiod. */ - return (-1); -} - -static void -gr_minimize(struct irs_gr *this) { - - UNUSED(this); - /* NOOP */ -} - -/* Private. */ - -static struct group * -get_hes_group(struct irs_gr *this, const char *name, const char *type) { - struct pvt *pvt = (struct pvt *)this->private; - char **hes_list, *cp, **new; - size_t num_members = 0; - u_long t; - - hes_list = hesiod_resolve(pvt->dns->hes_ctx, name, type); - if (!hes_list) - return (NULL); - - /* - * Copy the returned hesiod string into storage space. - */ - if (pvt->membuf) - free(pvt->membuf); - pvt->membuf = strdup(*hes_list); - hesiod_free_list(pvt->dns->hes_ctx, hes_list); - - cp = pvt->membuf; - pvt->group.gr_name = cp; - if (!(cp = strchr(cp, ':'))) - goto cleanup; - *cp++ = '\0'; - - pvt->group.gr_passwd = cp; - if (!(cp = strchr(cp, ':'))) - goto cleanup; - *cp++ = '\0'; - - errno = 0; - t = strtoul(cp, NULL, 10); - if (errno == ERANGE) - goto cleanup; - pvt->group.gr_gid = (gid_t) t; - if (!(cp = strchr(cp, ':'))) - goto cleanup; - cp++; - - /* - * Parse the members out. - */ - while (*cp) { - if (num_members+1 >= pvt->nmemb || pvt->group.gr_mem == NULL) { - pvt->nmemb += 10; - new = realloc(pvt->group.gr_mem, - pvt->nmemb * sizeof(char *)); - if (new == NULL) - goto cleanup; - pvt->group.gr_mem = new; - } - pvt->group.gr_mem[num_members++] = cp; - if (!(cp = strchr(cp, ','))) - break; - *cp++ = '\0'; - } - if (!pvt->group.gr_mem) { - pvt->group.gr_mem = malloc(sizeof(char*)); - if (!pvt->group.gr_mem) - goto cleanup; - } - pvt->group.gr_mem[num_members] = NULL; - - return (&pvt->group); - - cleanup: - if (pvt->group.gr_mem) { - free(pvt->group.gr_mem); - pvt->group.gr_mem = NULL; - } - if (pvt->membuf) { - free(pvt->membuf); - pvt->membuf = NULL; - } - return (NULL); -} - -static struct __res_state * -gr_res_get(struct irs_gr *this) { - struct pvt *pvt = (struct pvt *)this->private; - struct dns_p *dns = pvt->dns; - - return (__hesiod_res_get(dns->hes_ctx)); -} - -static void -gr_res_set(struct irs_gr *this, struct __res_state * res, - void (*free_res)(void *)) { - struct pvt *pvt = (struct pvt *)this->private; - struct dns_p *dns = pvt->dns; - - __hesiod_res_set(dns->hes_ctx, res, free_res); -} - -#endif /* WANT_IRS_GR */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/dns_ho.c /usr/src/lib/libc/net/dns_ho.c --- /home/reed/src/isc/libbind/libbind/irs/dns_ho.c 2013-06-05 09:33:54.000000000 -0500 +++ /usr/src/lib/libc/net/dns_ho.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,1139 +0,0 @@ -/* - * Portions Copyright (C) 2004-2006, 2008 Internet Systems Consortium, Inc. ("ISC") - * Portions Copyright (C) 1996-2003 Internet Software Consortium. - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH - * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY - * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, - * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM - * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE - * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR - * PERFORMANCE OF THIS SOFTWARE. - */ - -/* - * Copyright (c) 1985, 1988, 1993 - * The Regents of the University of California. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -/* from gethostnamadr.c 8.1 (Berkeley) 6/4/93 */ -/* BIND Id: gethnamaddr.c,v 8.15 1996/05/22 04:56:30 vixie Exp $ */ - -#if defined(LIBC_SCCS) && !defined(lint) -static const char rcsid[] = "$Id: dns_ho.c,v 1.24 2013-01-06 23:14:51 marka Exp $"; -#endif /* LIBC_SCCS and not lint */ - -/* Imports. */ - -#include "port_before.h" - -#include -#include -#include - -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include "port_after.h" - -#include "irs_p.h" -#include "dns_p.h" - -#ifdef SPRINTF_CHAR -# define SPRINTF(x) strlen(sprintf/**/x) -#else -# define SPRINTF(x) sprintf x -#endif - -/* Definitions. */ - -#define MAXALIASES 35 -#define MAXADDRS 35 - -#define MAXPACKET (65535) /*%< Maximum TCP message size */ -#define BOUNDS_CHECK(ptr, count) \ - if ((ptr) + (count) > eom) { \ - had_error++; \ - continue; \ - } else (void)0 - -typedef union { - HEADER hdr; - u_char buf[MAXPACKET]; -} querybuf; - -struct dns_res_target { - struct dns_res_target *next; - querybuf qbuf; /*%< query buffer */ - u_char *answer; /*%< buffer to put answer */ - int anslen; /*%< size of answer buffer */ - int qclass, qtype; /*%< class and type of query */ - int action; /*%< condition whether query is really issued */ - char qname[MAXDNAME +1]; /*%< domain name */ -#if 0 - int n; /*%< result length */ -#endif -}; -enum {RESTGT_DOALWAYS, RESTGT_AFTERFAILURE, RESTGT_IGNORE}; -enum {RESQRY_SUCCESS, RESQRY_FAIL}; - -struct pvt { - struct hostent host; - char * h_addr_ptrs[MAXADDRS + 1]; - char * host_aliases[MAXALIASES]; - char hostbuf[8*1024]; - u_char host_addr[16]; /*%< IPv4 or IPv6 */ - struct __res_state *res; - void (*free_res)(void *); -}; - -typedef union { - int32_t al; - char ac; -} align; - -static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff }; -static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 }; -/* Note: the IPv6 loopback address is in the "tunnel" space */ -static const u_char v6local[] = { 0,0, 0,1 }; /*%< last 4 bytes of IPv6 addr */ -/* Forwards. */ - -static void ho_close(struct irs_ho *this); -static struct hostent * ho_byname(struct irs_ho *this, const char *name); -static struct hostent * ho_byname2(struct irs_ho *this, const char *name, - int af); -static struct hostent * ho_byaddr(struct irs_ho *this, const void *addr, - int len, int af); -static struct hostent * ho_next(struct irs_ho *this); -static void ho_rewind(struct irs_ho *this); -static void ho_minimize(struct irs_ho *this); -static struct __res_state * ho_res_get(struct irs_ho *this); -static void ho_res_set(struct irs_ho *this, - struct __res_state *res, - void (*free_res)(void *)); -static struct addrinfo * ho_addrinfo(struct irs_ho *this, const char *name, - const struct addrinfo *pai); - -static void map_v4v6_hostent(struct hostent *hp, char **bp, - char *ep); -static void addrsort(res_state, char **, int); -static struct hostent * gethostans(struct irs_ho *this, - const u_char *ansbuf, int anslen, - const char *qname, int qtype, - int af, int size, - struct addrinfo **ret_aip, - const struct addrinfo *pai); -static int add_hostent(struct pvt *pvt, char *bp, char **hap, - struct addrinfo *ai); -static int init(struct irs_ho *this); - -/* Exports. */ - -struct irs_ho * -irs_dns_ho(struct irs_acc *this) { - struct irs_ho *ho; - struct pvt *pvt; - - UNUSED(this); - - if (!(pvt = memget(sizeof *pvt))) { - errno = ENOMEM; - return (NULL); - } - memset(pvt, 0, sizeof *pvt); - - if (!(ho = memget(sizeof *ho))) { - memput(pvt, sizeof *pvt); - errno = ENOMEM; - return (NULL); - } - memset(ho, 0x5e, sizeof *ho); - ho->private = pvt; - ho->close = ho_close; - ho->byname = ho_byname; - ho->byname2 = ho_byname2; - ho->byaddr = ho_byaddr; - ho->next = ho_next; - ho->rewind = ho_rewind; - ho->minimize = ho_minimize; - ho->res_get = ho_res_get; - ho->res_set = ho_res_set; - ho->addrinfo = ho_addrinfo; - return (ho); -} - -/* Methods. */ - -static void -ho_close(struct irs_ho *this) { - struct pvt *pvt = (struct pvt *)this->private; - - ho_minimize(this); - if (pvt->res && pvt->free_res) - (*pvt->free_res)(pvt->res); - memput(pvt, sizeof *pvt); - memput(this, sizeof *this); -} - -static struct hostent * -ho_byname(struct irs_ho *this, const char *name) { - struct pvt *pvt = (struct pvt *)this->private; - struct hostent *hp; - - if (init(this) == -1) - return (NULL); - - if (pvt->res->options & RES_USE_INET6) { - hp = ho_byname2(this, name, AF_INET6); - if (hp) - return (hp); - } - return (ho_byname2(this, name, AF_INET)); -} - -static struct hostent * -ho_byname2(struct irs_ho *this, const char *name, int af) -{ - struct pvt *pvt = (struct pvt *)this->private; - struct hostent *hp = NULL; - int n, size; - char tmp[NS_MAXDNAME]; - const char *cp; - struct addrinfo ai; - struct dns_res_target *q, *p; - int querystate = RESQRY_FAIL; - - if (init(this) == -1) - return (NULL); - - q = memget(sizeof(*q)); - if (q == NULL) { - RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL); - errno = ENOMEM; - goto cleanup; - } - memset(q, 0, sizeof(*q)); - - switch (af) { - case AF_INET: - size = INADDRSZ; - q->qclass = C_IN; - q->qtype = T_A; - q->answer = q->qbuf.buf; - q->anslen = sizeof(q->qbuf); - q->action = RESTGT_DOALWAYS; - break; - case AF_INET6: - size = IN6ADDRSZ; - q->qclass = C_IN; - q->qtype = T_AAAA; - q->answer = q->qbuf.buf; - q->anslen = sizeof(q->qbuf); - q->action = RESTGT_DOALWAYS; - break; - default: - RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL); - errno = EAFNOSUPPORT; - hp = NULL; - goto cleanup; - } - - /* - * if there aren't any dots, it could be a user-level alias. - * this is also done in res_nquery() since we are not the only - * function that looks up host names. - */ - if (!strchr(name, '.') && (cp = res_hostalias(pvt->res, name, - tmp, sizeof tmp))) - name = cp; - - for (p = q; p; p = p->next) { - switch(p->action) { - case RESTGT_DOALWAYS: - break; - case RESTGT_AFTERFAILURE: - if (querystate == RESQRY_SUCCESS) - continue; - break; - case RESTGT_IGNORE: - continue; - } - - if ((n = res_nsearch(pvt->res, name, p->qclass, p->qtype, - p->answer, p->anslen)) < 0) { - querystate = RESQRY_FAIL; - continue; - } - - memset(&ai, 0, sizeof(ai)); - ai.ai_family = af; - if ((hp = gethostans(this, p->answer, n, name, p->qtype, - af, size, NULL, - (const struct addrinfo *)&ai)) != NULL) - goto cleanup; /*%< no more loop is necessary */ - querystate = RESQRY_FAIL; - continue; - } - - cleanup: - if (q != NULL) - memput(q, sizeof(*q)); - return(hp); -} - -static struct hostent * -ho_byaddr(struct irs_ho *this, const void *addr, int len, int af) -{ - struct pvt *pvt = (struct pvt *)this->private; - const u_char *uaddr = addr; - char *qp; - struct hostent *hp = NULL; - struct addrinfo ai; - struct dns_res_target *q, *q2, *p; - int n, size, i; - int querystate = RESQRY_FAIL; - - if (init(this) == -1) - return (NULL); - - q = memget(sizeof(*q)); - q2 = memget(sizeof(*q2)); - if (q == NULL || q2 == NULL) { - RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL); - errno = ENOMEM; - goto cleanup; - } - memset(q, 0, sizeof(*q)); - memset(q2, 0, sizeof(*q2)); - - if (af == AF_INET6 && len == IN6ADDRSZ && - (!memcmp(uaddr, mapped, sizeof mapped) || - (!memcmp(uaddr, tunnelled, sizeof tunnelled) && - memcmp(&uaddr[sizeof tunnelled], v6local, sizeof(v6local))))) { - /* Unmap. */ - addr = (const char *)addr + sizeof mapped; - uaddr += sizeof mapped; - af = AF_INET; - len = INADDRSZ; - } - switch (af) { - case AF_INET: - size = INADDRSZ; - q->qclass = C_IN; - q->qtype = T_PTR; - q->answer = q->qbuf.buf; - q->anslen = sizeof(q->qbuf); - q->action = RESTGT_DOALWAYS; - break; - case AF_INET6: - size = IN6ADDRSZ; - q->qclass = C_IN; - q->qtype = T_PTR; - q->answer = q->qbuf.buf; - q->anslen = sizeof(q->qbuf); - q->next = q2; - q->action = RESTGT_DOALWAYS; - q2->qclass = C_IN; - q2->qtype = T_PTR; - q2->answer = q2->qbuf.buf; - q2->anslen = sizeof(q2->qbuf); - if ((pvt->res->options & RES_NO_NIBBLE2) != 0U) - q2->action = RESTGT_IGNORE; - else - q2->action = RESTGT_AFTERFAILURE; - break; - default: - errno = EAFNOSUPPORT; - RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL); - hp = NULL; - goto cleanup; - } - if (size > len) { - errno = EINVAL; - RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL); - hp = NULL; - goto cleanup; - } - switch (af) { - case AF_INET: - qp = q->qname; - (void) sprintf(qp, "%u.%u.%u.%u.in-addr.arpa", - (uaddr[3] & 0xff), - (uaddr[2] & 0xff), - (uaddr[1] & 0xff), - (uaddr[0] & 0xff)); - break; - case AF_INET6: - if (q->action != RESTGT_IGNORE) { - const char *nibsuff = res_get_nibblesuffix(pvt->res); - qp = q->qname; - for (n = IN6ADDRSZ - 1; n >= 0; n--) { - i = SPRINTF((qp, "%x.%x.", - uaddr[n] & 0xf, - (uaddr[n] >> 4) & 0xf)); - if (i != 4) - abort(); - qp += i; - } - if (strlen(q->qname) + strlen(nibsuff) + 1 > - sizeof q->qname) { - errno = ENAMETOOLONG; - RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL); - hp = NULL; - goto cleanup; - } - strcpy(qp, nibsuff); /* (checked) */ - } - if (q2->action != RESTGT_IGNORE) { - const char *nibsuff2 = res_get_nibblesuffix2(pvt->res); - qp = q2->qname; - for (n = IN6ADDRSZ - 1; n >= 0; n--) { - i = SPRINTF((qp, "%x.%x.", - uaddr[n] & 0xf, - (uaddr[n] >> 4) & 0xf)); - if (i != 4) - abort(); - qp += i; - } - if (strlen(q2->qname) + strlen(nibsuff2) + 1 > - sizeof q2->qname) { - errno = ENAMETOOLONG; - RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL); - hp = NULL; - goto cleanup; - } - strcpy(qp, nibsuff2); /* (checked) */ - } - break; - default: - abort(); - } - - for (p = q; p; p = p->next) { - switch(p->action) { - case RESTGT_DOALWAYS: - break; - case RESTGT_AFTERFAILURE: - if (querystate == RESQRY_SUCCESS) - continue; - break; - case RESTGT_IGNORE: - continue; - } - - if ((n = res_nquery(pvt->res, p->qname, p->qclass, p->qtype, - p->answer, p->anslen)) < 0) { - querystate = RESQRY_FAIL; - continue; - } - - memset(&ai, 0, sizeof(ai)); - ai.ai_family = af; - hp = gethostans(this, p->answer, n, p->qname, T_PTR, af, size, - NULL, (const struct addrinfo *)&ai); - if (!hp) { - querystate = RESQRY_FAIL; - continue; - } - - memcpy(pvt->host_addr, addr, len); - pvt->h_addr_ptrs[0] = (char *)pvt->host_addr; - pvt->h_addr_ptrs[1] = NULL; - if (af == AF_INET && (pvt->res->options & RES_USE_INET6)) { - map_v4v6_address((char*)pvt->host_addr, - (char*)pvt->host_addr); - pvt->host.h_addrtype = AF_INET6; - pvt->host.h_length = IN6ADDRSZ; - } - - RES_SET_H_ERRNO(pvt->res, NETDB_SUCCESS); - goto cleanup; /*%< no more loop is necessary. */ - } - hp = NULL; /*%< H_ERRNO was set by subroutines */ - cleanup: - if (q != NULL) - memput(q, sizeof(*q)); - if (q2 != NULL) - memput(q2, sizeof(*q2)); - return(hp); -} - -static struct hostent * -ho_next(struct irs_ho *this) { - - UNUSED(this); - - return (NULL); -} - -static void -ho_rewind(struct irs_ho *this) { - - UNUSED(this); - - /* NOOP */ -} - -static void -ho_minimize(struct irs_ho *this) { - struct pvt *pvt = (struct pvt *)this->private; - - if (pvt->res) - res_nclose(pvt->res); -} - -static struct __res_state * -ho_res_get(struct irs_ho *this) { - struct pvt *pvt = (struct pvt *)this->private; - - if (!pvt->res) { - struct __res_state *res; - res = (struct __res_state *)malloc(sizeof *res); - if (!res) { - errno = ENOMEM; - return (NULL); - } - memset(res, 0, sizeof *res); - ho_res_set(this, res, free); - } - - return (pvt->res); -} - -/* XXX */ -extern struct addrinfo *addr2addrinfo __P((const struct addrinfo *, - const char *)); - -static struct addrinfo * -ho_addrinfo(struct irs_ho *this, const char *name, const struct addrinfo *pai) -{ - struct pvt *pvt = (struct pvt *)this->private; - int n; - char tmp[NS_MAXDNAME]; - const char *cp; - struct dns_res_target *q, *q2, *p; - struct addrinfo sentinel, *cur; - int querystate = RESQRY_FAIL; - - if (init(this) == -1) - return (NULL); - - memset(&sentinel, 0, sizeof(sentinel)); - cur = &sentinel; - - q = memget(sizeof(*q)); - q2 = memget(sizeof(*q2)); - if (q == NULL || q2 == NULL) { - RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL); - errno = ENOMEM; - goto cleanup; - } - memset(q, 0, sizeof(*q2)); - memset(q2, 0, sizeof(*q2)); - - switch (pai->ai_family) { - case AF_UNSPEC: - /* prefer IPv6 */ - q->qclass = C_IN; - q->qtype = T_AAAA; - q->answer = q->qbuf.buf; - q->anslen = sizeof(q->qbuf); - q->next = q2; - q->action = RESTGT_DOALWAYS; - q2->qclass = C_IN; - q2->qtype = T_A; - q2->answer = q2->qbuf.buf; - q2->anslen = sizeof(q2->qbuf); - q2->action = RESTGT_DOALWAYS; - break; - case AF_INET: - q->qclass = C_IN; - q->qtype = T_A; - q->answer = q->qbuf.buf; - q->anslen = sizeof(q->qbuf); - q->action = RESTGT_DOALWAYS; - break; - case AF_INET6: - q->qclass = C_IN; - q->qtype = T_AAAA; - q->answer = q->qbuf.buf; - q->anslen = sizeof(q->qbuf); - q->action = RESTGT_DOALWAYS; - break; - default: - RES_SET_H_ERRNO(pvt->res, NO_RECOVERY); /*%< better error? */ - goto cleanup; - } - - /* - * if there aren't any dots, it could be a user-level alias. - * this is also done in res_nquery() since we are not the only - * function that looks up host names. - */ - if (!strchr(name, '.') && (cp = res_hostalias(pvt->res, name, - tmp, sizeof tmp))) - name = cp; - - for (p = q; p; p = p->next) { - struct addrinfo *ai; - - switch(p->action) { - case RESTGT_DOALWAYS: - break; - case RESTGT_AFTERFAILURE: - if (querystate == RESQRY_SUCCESS) - continue; - break; - case RESTGT_IGNORE: - continue; - } - - if ((n = res_nsearch(pvt->res, name, p->qclass, p->qtype, - p->answer, p->anslen)) < 0) { - querystate = RESQRY_FAIL; - continue; - } - (void)gethostans(this, p->answer, n, name, p->qtype, - pai->ai_family, /*%< XXX: meaningless */ - 0, &ai, pai); - if (ai) { - querystate = RESQRY_SUCCESS; - cur->ai_next = ai; - while (cur->ai_next) - cur = cur->ai_next; - } else - querystate = RESQRY_FAIL; - } - - cleanup: - if (q != NULL) - memput(q, sizeof(*q)); - if (q2 != NULL) - memput(q2, sizeof(*q2)); - return(sentinel.ai_next); -} - -static void -ho_res_set(struct irs_ho *this, struct __res_state *res, - void (*free_res)(void *)) { - struct pvt *pvt = (struct pvt *)this->private; - - if (pvt->res && pvt->free_res) { - res_nclose(pvt->res); - (*pvt->free_res)(pvt->res); - } - - pvt->res = res; - pvt->free_res = free_res; -} - -/* Private. */ - -static struct hostent * -gethostans(struct irs_ho *this, - const u_char *ansbuf, int anslen, const char *qname, int qtype, - int af, int size, /*!< meaningless for addrinfo cases */ - struct addrinfo **ret_aip, const struct addrinfo *pai) -{ - struct pvt *pvt = (struct pvt *)this->private; - int type, class, ancount, qdcount, n, haveanswer, had_error; - int error = NETDB_SUCCESS; - int (*name_ok)(const char *); - const HEADER *hp; - const u_char *eom; - const u_char *eor; - const u_char *cp; - const char *tname; - const char *hname; - char *bp, *ep, **ap, **hap; - char tbuf[MAXDNAME+1]; - struct addrinfo sentinel, *cur, ai; - - if (pai == NULL) abort(); - if (ret_aip != NULL) - *ret_aip = NULL; - memset(&sentinel, 0, sizeof(sentinel)); - cur = &sentinel; - - tname = qname; - eom = ansbuf + anslen; - switch (qtype) { - case T_A: - case T_AAAA: - case T_ANY: /*%< use T_ANY only for T_A/T_AAAA lookup */ - name_ok = res_hnok; - break; - case T_PTR: - name_ok = res_dnok; - break; - default: - abort(); - } - - pvt->host.h_addrtype = af; - pvt->host.h_length = size; - hname = pvt->host.h_name = NULL; - - /* - * Find first satisfactory answer. - */ - if (ansbuf + HFIXEDSZ > eom) { - RES_SET_H_ERRNO(pvt->res, NO_RECOVERY); - return (NULL); - } - hp = (const HEADER *)ansbuf; - ancount = ntohs(hp->ancount); - qdcount = ntohs(hp->qdcount); - bp = pvt->hostbuf; - ep = pvt->hostbuf + sizeof(pvt->hostbuf); - cp = ansbuf + HFIXEDSZ; - if (qdcount != 1) { - RES_SET_H_ERRNO(pvt->res, NO_RECOVERY); - return (NULL); - } - n = dn_expand(ansbuf, eom, cp, bp, ep - bp); - if (n < 0 || !maybe_ok(pvt->res, bp, name_ok)) { - RES_SET_H_ERRNO(pvt->res, NO_RECOVERY); - return (NULL); - } - cp += n + QFIXEDSZ; - if (cp > eom) { - RES_SET_H_ERRNO(pvt->res, NO_RECOVERY); - return (NULL); - } - if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) { - /* res_nsend() has already verified that the query name is the - * same as the one we sent; this just gets the expanded name - * (i.e., with the succeeding search-domain tacked on). - */ - n = strlen(bp) + 1; /*%< for the \\0 */ - if (n > MAXHOSTNAMELEN) { - RES_SET_H_ERRNO(pvt->res, NO_RECOVERY); - return (NULL); - } - pvt->host.h_name = bp; - hname = bp; - bp += n; - /* The qname can be abbreviated, but hname is now absolute. */ - qname = pvt->host.h_name; - } - ap = pvt->host_aliases; - *ap = NULL; - pvt->host.h_aliases = pvt->host_aliases; - hap = pvt->h_addr_ptrs; - *hap = NULL; - pvt->host.h_addr_list = pvt->h_addr_ptrs; - haveanswer = 0; - had_error = 0; - while (ancount-- > 0 && cp < eom && !had_error) { - n = dn_expand(ansbuf, eom, cp, bp, ep - bp); - if (n < 0 || !maybe_ok(pvt->res, bp, name_ok)) { - had_error++; - continue; - } - cp += n; /*%< name */ - BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ); - type = ns_get16(cp); - cp += INT16SZ; /*%< type */ - class = ns_get16(cp); - cp += INT16SZ + INT32SZ; /*%< class, TTL */ - n = ns_get16(cp); - cp += INT16SZ; /*%< len */ - BOUNDS_CHECK(cp, n); - if (class != C_IN) { - cp += n; - continue; - } - eor = cp + n; - if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) && - type == T_CNAME) { - if (haveanswer) { - int level = LOG_CRIT; -#ifdef LOG_SECURITY - level |= LOG_SECURITY; -#endif - syslog(level, - "gethostans: possible attempt to exploit buffer overflow while looking up %s", - *qname ? qname : "."); - } - n = dn_expand(ansbuf, eor, cp, tbuf, sizeof tbuf); - if (n < 0 || !maybe_ok(pvt->res, tbuf, name_ok)) { - had_error++; - continue; - } - cp += n; - /* Store alias. */ - if (ap >= &pvt->host_aliases[MAXALIASES-1]) - continue; - *ap++ = bp; - n = strlen(bp) + 1; /*%< for the \\0 */ - bp += n; - /* Get canonical name. */ - n = strlen(tbuf) + 1; /*%< for the \\0 */ - if (n > (ep - bp) || n > MAXHOSTNAMELEN) { - had_error++; - continue; - } - strcpy(bp, tbuf); /* (checked) */ - pvt->host.h_name = bp; - hname = bp; - bp += n; - continue; - } - if (qtype == T_PTR && type == T_CNAME) { - n = dn_expand(ansbuf, eor, cp, tbuf, sizeof tbuf); - if (n < 0 || !maybe_dnok(pvt->res, tbuf)) { - had_error++; - continue; - } - cp += n; -#ifdef RES_USE_DNAME - if ((pvt->res->options & RES_USE_DNAME) != 0U) -#endif - { - /* - * We may be able to check this regardless - * of the USE_DNAME bit, but we add the check - * for now since the DNAME support is - * experimental. - */ - if (ns_samename(tname, bp) != 1) - continue; - } - /* Get canonical name. */ - n = strlen(tbuf) + 1; /*%< for the \\0 */ - if (n > (ep - bp)) { - had_error++; - continue; - } - strcpy(bp, tbuf); /* (checked) */ - tname = bp; - bp += n; - continue; - } - if (qtype == T_ANY) { - if (!(type == T_A || type == T_AAAA)) { - cp += n; - continue; - } - } else if (type != qtype) { - cp += n; - continue; - } - switch (type) { - case T_PTR: - if (ret_aip != NULL) { - /* addrinfo never needs T_PTR */ - cp += n; - continue; - } - if (ns_samename(tname, bp) != 1) { - cp += n; - continue; - } - n = dn_expand(ansbuf, eor, cp, bp, ep - bp); - if (n < 0 || !maybe_hnok(pvt->res, bp) || - n >= MAXHOSTNAMELEN) { - had_error++; - break; - } - cp += n; - if (!haveanswer) { - pvt->host.h_name = bp; - hname = bp; - } - else if (ap < &pvt->host_aliases[MAXALIASES-1]) - *ap++ = bp; - else - n = -1; - if (n != -1) { - n = strlen(bp) + 1; /*%< for the \\0 */ - bp += n; - } - break; - case T_A: - case T_AAAA: - if (ns_samename(hname, bp) != 1) { - cp += n; - continue; - } - if (type == T_A && n != INADDRSZ) { - cp += n; - continue; - } - if (type == T_AAAA && n != IN6ADDRSZ) { - cp += n; - continue; - } - - /* make addrinfo. don't overwrite constant PAI */ - ai = *pai; - ai.ai_family = (type == T_AAAA) ? AF_INET6 : AF_INET; - cur->ai_next = addr2addrinfo( - (const struct addrinfo *)&ai, - (const char *)cp); - if (cur->ai_next == NULL) - had_error++; - - if (!haveanswer) { - int nn; - - nn = strlen(bp) + 1; /*%< for the \\0 */ - if (nn >= MAXHOSTNAMELEN) { - cp += n; - had_error++; - continue; - } - pvt->host.h_name = bp; - hname = bp; - bp += nn; - } - /* Ensure alignment. */ - bp = (char *)(((u_long)bp + (sizeof(align) - 1)) & - ~(sizeof(align) - 1)); - /* Avoid overflows. */ - if (bp + n > &pvt->hostbuf[sizeof(pvt->hostbuf) - 1]) { - had_error++; - continue; - } - if (ret_aip) { /*%< need addrinfo. keep it. */ - while (cur->ai_next) - cur = cur->ai_next; - } else if (cur->ai_next) { /*%< need hostent */ - struct addrinfo *aip = cur->ai_next; - - for (aip = cur->ai_next; aip; - aip = aip->ai_next) { - int m; - - m = add_hostent(pvt, bp, hap, aip); - if (m < 0) { - had_error++; - break; - } - if (m == 0) - continue; - if (hap < &pvt->h_addr_ptrs[MAXADDRS]) - hap++; - *hap = NULL; - bp += m; - } - - freeaddrinfo(cur->ai_next); - cur->ai_next = NULL; - } - cp += n; - break; - default: - abort(); - } - if (!had_error) - haveanswer++; - } - if (haveanswer) { - if (ret_aip == NULL) { - *ap = NULL; - *hap = NULL; - - if (pvt->res->nsort && hap != pvt->h_addr_ptrs && - qtype == T_A) - addrsort(pvt->res, pvt->h_addr_ptrs, - hap - pvt->h_addr_ptrs); - if (pvt->host.h_name == NULL) { - n = strlen(qname) + 1; /*%< for the \\0 */ - if (n > (ep - bp) || n >= MAXHOSTNAMELEN) - goto no_recovery; - strcpy(bp, qname); /* (checked) */ - pvt->host.h_name = bp; - bp += n; - } - if (pvt->res->options & RES_USE_INET6) - map_v4v6_hostent(&pvt->host, &bp, ep); - RES_SET_H_ERRNO(pvt->res, NETDB_SUCCESS); - return (&pvt->host); - } else { - if ((pai->ai_flags & AI_CANONNAME) != 0) { - if (pvt->host.h_name == NULL) { - sentinel.ai_next->ai_canonname = - strdup(qname); - } - else { - sentinel.ai_next->ai_canonname = - strdup(pvt->host.h_name); - } - } - *ret_aip = sentinel.ai_next; - return(NULL); - } - } - no_recovery: - if (sentinel.ai_next) { - /* this should be impossible, but check it for safety */ - freeaddrinfo(sentinel.ai_next); - } - if (error == NETDB_SUCCESS) - RES_SET_H_ERRNO(pvt->res, NO_RECOVERY); - else - RES_SET_H_ERRNO(pvt->res, error); - return(NULL); -} - -static int -add_hostent(struct pvt *pvt, char *bp, char **hap, struct addrinfo *ai) -{ - int addrlen; - char *addrp; - const char **tap; - char *obp = bp; - - switch(ai->ai_addr->sa_family) { - case AF_INET6: - addrlen = IN6ADDRSZ; - addrp = (char *)&((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr; - break; - case AF_INET: - addrlen = INADDRSZ; - addrp = (char *)&((struct sockaddr_in *)ai->ai_addr)->sin_addr; - break; - default: - return(-1); /*%< abort? */ - } - - /* Ensure alignment. */ - bp = (char *)(((u_long)bp + (sizeof(align) - 1)) & - ~(sizeof(align) - 1)); - /* Avoid overflows. */ - if (bp + addrlen > &pvt->hostbuf[sizeof(pvt->hostbuf) - 1]) - return(-1); - if (hap >= &pvt->h_addr_ptrs[MAXADDRS]) - return(0); /*%< fail, but not treat it as an error. */ - /* Suppress duplicates. */ - for (tap = (const char **)pvt->h_addr_ptrs; - *tap != NULL; - tap++) - if (memcmp(*tap, addrp, addrlen) == 0) - break; - if (*tap != NULL) - return (0); - - memcpy(*hap = bp, addrp, addrlen); - return((bp + addrlen) - obp); -} - -static void -map_v4v6_hostent(struct hostent *hp, char **bpp, char *ep) { - char **ap; - - if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ) - return; - hp->h_addrtype = AF_INET6; - hp->h_length = IN6ADDRSZ; - for (ap = hp->h_addr_list; *ap; ap++) { - int i = (u_long)*bpp % sizeof(align); - - if (i != 0) - i = sizeof(align) - i; - - if ((ep - *bpp) < (i + IN6ADDRSZ)) { - /* Out of memory. Truncate address list here. */ - *ap = NULL; - return; - } - *bpp += i; - map_v4v6_address(*ap, *bpp); - *ap = *bpp; - *bpp += IN6ADDRSZ; - } -} - -static void -addrsort(res_state statp, char **ap, int num) { - int i, j, needsort = 0, aval[MAXADDRS]; - char **p; - - p = ap; - for (i = 0; i < num; i++, p++) { - for (j = 0 ; (unsigned)j < statp->nsort; j++) - if (statp->sort_list[j].addr.s_addr == - (((struct in_addr *)(*p))->s_addr & - statp->sort_list[j].mask)) - break; - aval[i] = j; - if (needsort == 0 && i > 0 && j < aval[i-1]) - needsort = i; - } - if (!needsort) - return; - - while (needsort < num) { - for (j = needsort - 1; j >= 0; j--) { - if (aval[j] > aval[j+1]) { - char *hp; - - i = aval[j]; - aval[j] = aval[j+1]; - aval[j+1] = i; - - hp = ap[j]; - ap[j] = ap[j+1]; - ap[j+1] = hp; - - } else - break; - } - needsort++; - } -} - -static int -init(struct irs_ho *this) { - struct pvt *pvt = (struct pvt *)this->private; - - if (!pvt->res && !ho_res_get(this)) - return (-1); - if (((pvt->res->options & RES_INIT) == 0U) && - res_ninit(pvt->res) == -1) - return (-1); - return (0); -} diff -pruN /home/reed/src/isc/libbind/libbind/irs/dns_nw.c /usr/src/lib/libc/net/dns_nw.c --- /home/reed/src/isc/libbind/libbind/irs/dns_nw.c 2005-04-26 23:56:22.000000000 -0500 +++ /usr/src/lib/libc/net/dns_nw.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,591 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1996-1999 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#if defined(LIBC_SCCS) && !defined(lint) -static const char rcsid[] = "$Id: dns_nw.c,v 1.12 2005/04/27 04:56:22 sra Exp $"; -#endif /* LIBC_SCCS and not lint */ - -/* Imports. */ - -#include "port_before.h" - -#include -#include - -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include "port_after.h" - -#include "irs_p.h" -#include "dns_p.h" - -#ifdef SPRINTF_CHAR -# define SPRINTF(x) strlen(sprintf/**/x) -#else -# define SPRINTF(x) sprintf x -#endif - -/* Definitions. */ - -#define MAXALIASES 35 - -#define MAXPACKET (64*1024) - -struct pvt { - struct nwent net; - char * ali[MAXALIASES]; - char buf[BUFSIZ+1]; - struct __res_state * res; - void (*free_res)(void *); -}; - -typedef union { - long al; - char ac; -} align; - -enum by_what { by_addr, by_name }; - -/* Forwards. */ - -static void nw_close(struct irs_nw *); -static struct nwent * nw_byname(struct irs_nw *, const char *, int); -static struct nwent * nw_byaddr(struct irs_nw *, void *, int, int); -static struct nwent * nw_next(struct irs_nw *); -static void nw_rewind(struct irs_nw *); -static void nw_minimize(struct irs_nw *); -static struct __res_state * nw_res_get(struct irs_nw *this); -static void nw_res_set(struct irs_nw *this, - struct __res_state *res, - void (*free_res)(void *)); - -static struct nwent * get1101byaddr(struct irs_nw *, u_char *, int); -static struct nwent * get1101byname(struct irs_nw *, const char *); -static struct nwent * get1101answer(struct irs_nw *, - u_char *ansbuf, int anslen, - enum by_what by_what, - int af, const char *name, - const u_char *addr, int addrlen); -static struct nwent * get1101mask(struct irs_nw *this, struct nwent *); -static int make1101inaddr(const u_char *, int, char *, int); -static void normalize_name(char *name); -static int init(struct irs_nw *this); - -/* Exports. */ - -struct irs_nw * -irs_dns_nw(struct irs_acc *this) { - struct irs_nw *nw; - struct pvt *pvt; - - UNUSED(this); - - if (!(pvt = memget(sizeof *pvt))) { - errno = ENOMEM; - return (NULL); - } - memset(pvt, 0, sizeof *pvt); - if (!(nw = memget(sizeof *nw))) { - memput(pvt, sizeof *pvt); - errno = ENOMEM; - return (NULL); - } - memset(nw, 0x5e, sizeof *nw); - nw->private = pvt; - nw->close = nw_close; - nw->byname = nw_byname; - nw->byaddr = nw_byaddr; - nw->next = nw_next; - nw->rewind = nw_rewind; - nw->minimize = nw_minimize; - nw->res_get = nw_res_get; - nw->res_set = nw_res_set; - return (nw); -} - -/* Methods. */ - -static void -nw_close(struct irs_nw *this) { - struct pvt *pvt = (struct pvt *)this->private; - - nw_minimize(this); - - if (pvt->res && pvt->free_res) - (*pvt->free_res)(pvt->res); - - memput(pvt, sizeof *pvt); - memput(this, sizeof *this); -} - -static struct nwent * -nw_byname(struct irs_nw *this, const char *name, int af) { - struct pvt *pvt = (struct pvt *)this->private; - - if (init(this) == -1) - return (NULL); - - switch (af) { - case AF_INET: - return (get1101byname(this, name)); - default: - (void)NULL; - } - RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL); - errno = EAFNOSUPPORT; - return (NULL); -} - -static struct nwent * -nw_byaddr(struct irs_nw *this, void *net, int len, int af) { - struct pvt *pvt = (struct pvt *)this->private; - - if (init(this) == -1) - return (NULL); - - switch (af) { - case AF_INET: - return (get1101byaddr(this, net, len)); - default: - (void)NULL; - } - RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL); - errno = EAFNOSUPPORT; - return (NULL); -} - -static struct nwent * -nw_next(struct irs_nw *this) { - - UNUSED(this); - - return (NULL); -} - -static void -nw_rewind(struct irs_nw *this) { - UNUSED(this); - /* NOOP */ -} - -static void -nw_minimize(struct irs_nw *this) { - struct pvt *pvt = (struct pvt *)this->private; - - if (pvt->res) - res_nclose(pvt->res); -} - -static struct __res_state * -nw_res_get(struct irs_nw *this) { - struct pvt *pvt = (struct pvt *)this->private; - - if (!pvt->res) { - struct __res_state *res; - res = (struct __res_state *)malloc(sizeof *res); - if (!res) { - errno = ENOMEM; - return (NULL); - } - memset(res, 0, sizeof *res); - nw_res_set(this, res, free); - } - - return (pvt->res); -} - -static void -nw_res_set(struct irs_nw *this, struct __res_state *res, - void (*free_res)(void *)) { - struct pvt *pvt = (struct pvt *)this->private; - - if (pvt->res && pvt->free_res) { - res_nclose(pvt->res); - (*pvt->free_res)(pvt->res); - } - - pvt->res = res; - pvt->free_res = free_res; -} - -/* Private. */ - -static struct nwent * -get1101byname(struct irs_nw *this, const char *name) { - struct pvt *pvt = (struct pvt *)this->private; - u_char *ansbuf; - int anslen; - struct nwent *result; - - ansbuf = memget(MAXPACKET); - if (ansbuf == NULL) { - errno = ENOMEM; - RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL); - return (NULL); - } - anslen = res_nsearch(pvt->res, name, C_IN, T_PTR, ansbuf, MAXPACKET); - if (anslen < 0) { - memput(ansbuf, MAXPACKET); - return (NULL); - } - result = get1101mask(this, get1101answer(this, ansbuf, anslen, by_name, - AF_INET, name, NULL, 0)); - memput(ansbuf, MAXPACKET); - return (result); -} - -static struct nwent * -get1101byaddr(struct irs_nw *this, u_char *net, int len) { - struct pvt *pvt = (struct pvt *)this->private; - char qbuf[sizeof "255.255.255.255.in-addr.arpa"]; - struct nwent *result; - u_char *ansbuf; - int anslen; - - if (len < 1 || len > 32) { - errno = EINVAL; - RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL); - return (NULL); - } - if (make1101inaddr(net, len, qbuf, sizeof qbuf) < 0) - return (NULL); - ansbuf = memget(MAXPACKET); - if (ansbuf == NULL) { - errno = ENOMEM; - RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL); - return (NULL); - } - anslen = res_nquery(pvt->res, qbuf, C_IN, T_PTR, ansbuf, MAXPACKET); - if (anslen < 0) { - memput(ansbuf, MAXPACKET); - return (NULL); - } - result = get1101mask(this, get1101answer(this, ansbuf, anslen, by_addr, - AF_INET, NULL, net, len)); - memput(ansbuf, MAXPACKET); - return (result); -} - -static struct nwent * -get1101answer(struct irs_nw *this, - u_char *ansbuf, int anslen, enum by_what by_what, - int af, const char *name, const u_char *addr, int addrlen) -{ - struct pvt *pvt = (struct pvt *)this->private; - int type, class, ancount, qdcount, haveanswer; - char *bp, *ep, **ap; - u_char *cp, *eom; - HEADER *hp; - - /* Initialize, and parse header. */ - eom = ansbuf + anslen; - if (ansbuf + HFIXEDSZ > eom) { - RES_SET_H_ERRNO(pvt->res, NO_RECOVERY); - return (NULL); - } - hp = (HEADER *)ansbuf; - cp = ansbuf + HFIXEDSZ; - qdcount = ntohs(hp->qdcount); - while (qdcount-- > 0) { - int n = dn_skipname(cp, eom); - cp += n + QFIXEDSZ; - if (n < 0 || cp > eom) { - RES_SET_H_ERRNO(pvt->res, NO_RECOVERY); - return (NULL); - } - } - ancount = ntohs(hp->ancount); - if (!ancount) { - if (hp->aa) - RES_SET_H_ERRNO(pvt->res, HOST_NOT_FOUND); - else - RES_SET_H_ERRNO(pvt->res, TRY_AGAIN); - return (NULL); - } - - /* Prepare a return structure. */ - bp = pvt->buf; - ep = pvt->buf + sizeof(pvt->buf); - pvt->net.n_name = NULL; - pvt->net.n_aliases = pvt->ali; - pvt->net.n_addrtype = af; - pvt->net.n_addr = NULL; - pvt->net.n_length = addrlen; - - /* Save input key if given. */ - switch (by_what) { - case by_name: - if (name != NULL) { - int n = strlen(name) + 1; - - if (n > (ep - bp)) { - RES_SET_H_ERRNO(pvt->res, NO_RECOVERY); - return (NULL); - } - pvt->net.n_name = strcpy(bp, name); /* (checked) */ - bp += n; - } - break; - case by_addr: - if (addr != NULL && addrlen != 0) { - int n = addrlen / 8 + ((addrlen % 8) != 0); - - if (INADDRSZ > (ep - bp)) { - RES_SET_H_ERRNO(pvt->res, NO_RECOVERY); - return (NULL); - } - memset(bp, 0, INADDRSZ); - memcpy(bp, addr, n); - pvt->net.n_addr = bp; - bp += INADDRSZ; - } - break; - default: - abort(); - } - - /* Parse the answer, collect aliases. */ - ap = pvt->ali; - haveanswer = 0; - while (--ancount >= 0 && cp < eom) { - int n = dn_expand(ansbuf, eom, cp, bp, ep - bp); - - cp += n; /*%< Owner */ - if (n < 0 || !maybe_dnok(pvt->res, bp) || - cp + 3 * INT16SZ + INT32SZ > eom) { - RES_SET_H_ERRNO(pvt->res, NO_RECOVERY); - return (NULL); - } - GETSHORT(type, cp); /*%< Type */ - GETSHORT(class, cp); /*%< Class */ - cp += INT32SZ; /*%< TTL */ - GETSHORT(n, cp); /*%< RDLENGTH */ - if (class == C_IN && type == T_PTR) { - int nn; - - nn = dn_expand(ansbuf, eom, cp, bp, ep - bp); - if (nn < 0 || !maybe_hnok(pvt->res, bp) || nn != n) { - RES_SET_H_ERRNO(pvt->res, NO_RECOVERY); - return (NULL); - } - normalize_name(bp); - switch (by_what) { - case by_addr: { - if (pvt->net.n_name == NULL) - pvt->net.n_name = bp; - else if (ns_samename(pvt->net.n_name, bp) == 1) - break; - else - *ap++ = bp; - nn = strlen(bp) + 1; - bp += nn; - haveanswer++; - break; - } - case by_name: { - u_int b1, b2, b3, b4; - - if (pvt->net.n_addr != NULL || - sscanf(bp, "%u.%u.%u.%u.in-addr.arpa", - &b1, &b2, &b3, &b4) != 4) - break; - if ((ep - bp) < INADDRSZ) { - RES_SET_H_ERRNO(pvt->res, NO_RECOVERY); - return (NULL); - } - pvt->net.n_addr = bp; - *bp++ = b4; - *bp++ = b3; - *bp++ = b2; - *bp++ = b1; - pvt->net.n_length = INADDRSZ * 8; - haveanswer++; - } - } - } - cp += n; /*%< RDATA */ - } - if (!haveanswer) { - RES_SET_H_ERRNO(pvt->res, TRY_AGAIN); - return (NULL); - } - *ap = NULL; - - return (&pvt->net); -} - -static struct nwent * -get1101mask(struct irs_nw *this, struct nwent *nwent) { - struct pvt *pvt = (struct pvt *)this->private; - char qbuf[sizeof "255.255.255.255.in-addr.arpa"], owner[MAXDNAME]; - int anslen, type, class, ancount, qdcount; - u_char *ansbuf, *cp, *eom; - HEADER *hp; - - if (!nwent) - return (NULL); - if (make1101inaddr(nwent->n_addr, nwent->n_length, qbuf, sizeof qbuf) - < 0) { - /* "First, do no harm." */ - return (nwent); - } - - ansbuf = memget(MAXPACKET); - if (ansbuf == NULL) { - errno = ENOMEM; - RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL); - return (NULL); - } - /* Query for the A RR that would hold this network's mask. */ - anslen = res_nquery(pvt->res, qbuf, C_IN, T_A, ansbuf, MAXPACKET); - if (anslen < HFIXEDSZ) { - memput(ansbuf, MAXPACKET); - return (nwent); - } - - /* Initialize, and parse header. */ - hp = (HEADER *)ansbuf; - cp = ansbuf + HFIXEDSZ; - eom = ansbuf + anslen; - qdcount = ntohs(hp->qdcount); - while (qdcount-- > 0) { - int n = dn_skipname(cp, eom); - cp += n + QFIXEDSZ; - if (n < 0 || cp > eom) { - memput(ansbuf, MAXPACKET); - return (nwent); - } - } - ancount = ntohs(hp->ancount); - - /* Parse the answer, collect aliases. */ - while (--ancount >= 0 && cp < eom) { - int n = dn_expand(ansbuf, eom, cp, owner, sizeof owner); - - if (n < 0 || !maybe_dnok(pvt->res, owner)) - break; - cp += n; /*%< Owner */ - if (cp + 3 * INT16SZ + INT32SZ > eom) - break; - GETSHORT(type, cp); /*%< Type */ - GETSHORT(class, cp); /*%< Class */ - cp += INT32SZ; /*%< TTL */ - GETSHORT(n, cp); /*%< RDLENGTH */ - if (cp + n > eom) - break; - if (n == INADDRSZ && class == C_IN && type == T_A && - ns_samename(qbuf, owner) == 1) { - /* This A RR indicates the actual netmask. */ - int nn, mm; - - nwent->n_length = 0; - for (nn = 0; nn < INADDRSZ; nn++) - for (mm = 7; mm >= 0; mm--) - if (cp[nn] & (1 << mm)) - nwent->n_length++; - else - break; - } - cp += n; /*%< RDATA */ - } - memput(ansbuf, MAXPACKET); - return (nwent); -} - -static int -make1101inaddr(const u_char *net, int bits, char *name, int size) { - int n, m; - char *ep; - - ep = name + size; - - /* Zero fill any whole bytes left out of the prefix. */ - for (n = (32 - bits) / 8; n > 0; n--) { - if (ep - name < (int)(sizeof "0.")) - goto emsgsize; - m = SPRINTF((name, "0.")); - name += m; - } - - /* Format the partial byte, if any, within the prefix. */ - if ((n = bits % 8) != 0) { - if (ep - name < (int)(sizeof "255.")) - goto emsgsize; - m = SPRINTF((name, "%u.", - net[bits / 8] & ~((1 << (8 - n)) - 1))); - name += m; - } - - /* Format the whole bytes within the prefix. */ - for (n = bits / 8; n > 0; n--) { - if (ep - name < (int)(sizeof "255.")) - goto emsgsize; - m = SPRINTF((name, "%u.", net[n - 1])); - name += m; - } - - /* Add the static text. */ - if (ep - name < (int)(sizeof "in-addr.arpa")) - goto emsgsize; - (void) SPRINTF((name, "in-addr.arpa")); - return (0); - - emsgsize: - errno = EMSGSIZE; - return (-1); -} - -static void -normalize_name(char *name) { - char *t; - - /* Make lower case. */ - for (t = name; *t; t++) - if (isascii((unsigned char)*t) && isupper((unsigned char)*t)) - *t = tolower((*t)&0xff); - - /* Remove trailing dots. */ - while (t > name && t[-1] == '.') - *--t = '\0'; -} - -static int -init(struct irs_nw *this) { - struct pvt *pvt = (struct pvt *)this->private; - - if (!pvt->res && !nw_res_get(this)) - return (-1); - if (((pvt->res->options & RES_INIT) == 0U) && - res_ninit(pvt->res) == -1) - return (-1); - return (0); -} - -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/dns_p.h /usr/src/lib/libc/net/dns_p.h --- /home/reed/src/isc/libbind/libbind/irs/dns_p.h 2005-04-26 23:56:22.000000000 -0500 +++ /usr/src/lib/libc/net/dns_p.h 1969-12-31 18:00:00.000000000 -0600 @@ -1,52 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1996-1999 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -/* - * $Id: dns_p.h,v 1.4 2005/04/27 04:56:22 sra Exp $ - */ - -#ifndef _DNS_P_H_INCLUDED -#define _DNS_P_H_INCLUDED - -#define maybe_ok(res, nm, ok) (((res)->options & RES_NOCHECKNAME) != 0U || \ - (ok)(nm) != 0) -#define maybe_hnok(res, hn) maybe_ok((res), (hn), res_hnok) -#define maybe_dnok(res, dn) maybe_ok((res), (dn), res_dnok) - -/*% - * Object state. - */ -struct dns_p { - void *hes_ctx; - struct __res_state *res; - void (*free_res) __P((void *)); -}; - -/* - * Methods. - */ - -extern struct irs_gr * irs_dns_gr __P((struct irs_acc *)); -extern struct irs_pw * irs_dns_pw __P((struct irs_acc *)); -extern struct irs_sv * irs_dns_sv __P((struct irs_acc *)); -extern struct irs_pr * irs_dns_pr __P((struct irs_acc *)); -extern struct irs_ho * irs_dns_ho __P((struct irs_acc *)); -extern struct irs_nw * irs_dns_nw __P((struct irs_acc *)); - -#endif /*_DNS_P_H_INCLUDED*/ - -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/dns_pr.c /usr/src/lib/libc/net/dns_pr.c --- /home/reed/src/isc/libbind/libbind/irs/dns_pr.c 2005-04-26 23:56:22.000000000 -0500 +++ /usr/src/lib/libc/net/dns_pr.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,268 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1996,1999 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#if defined(LIBC_SCCS) && !defined(lint) -static const char rcsid[] = "$Id: dns_pr.c,v 1.5 2005/04/27 04:56:22 sra Exp $"; -#endif - -/* Imports */ - -#include "port_before.h" - -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include - -#include -#include - -#include "port_after.h" - -#include "irs_p.h" -#include "hesiod.h" -#include "dns_p.h" - -/* Types. */ - -struct pvt { - struct dns_p * dns; - struct protoent proto; - char * prbuf; -}; - -/* Forward. */ - -static void pr_close(struct irs_pr *); -static struct protoent * pr_byname(struct irs_pr *, const char *); -static struct protoent * pr_bynumber(struct irs_pr *, int); -static struct protoent * pr_next(struct irs_pr *); -static void pr_rewind(struct irs_pr *); -static void pr_minimize(struct irs_pr *); -static struct __res_state * pr_res_get(struct irs_pr *); -static void pr_res_set(struct irs_pr *, - struct __res_state *, - void (*)(void *)); - -static struct protoent * parse_hes_list(struct irs_pr *, char **); - -/* Public. */ - -struct irs_pr * -irs_dns_pr(struct irs_acc *this) { - struct dns_p *dns = (struct dns_p *)this->private; - struct pvt *pvt; - struct irs_pr *pr; - - if (!dns->hes_ctx) { - errno = ENODEV; - return (NULL); - } - if (!(pvt = memget(sizeof *pvt))) { - errno = ENOMEM; - return (NULL); - } - memset(pvt, 0, sizeof *pvt); - if (!(pr = memget(sizeof *pr))) { - memput(pvt, sizeof *pvt); - errno = ENOMEM; - return (NULL); - } - memset(pr, 0x5e, sizeof *pr); - pvt->dns = dns; - pr->private = pvt; - pr->byname = pr_byname; - pr->bynumber = pr_bynumber; - pr->next = pr_next; - pr->rewind = pr_rewind; - pr->close = pr_close; - pr->minimize = pr_minimize; - pr->res_get = pr_res_get; - pr->res_set = pr_res_set; - return (pr); -} - -/* Methods. */ - -static void -pr_close(struct irs_pr *this) { - struct pvt *pvt = (struct pvt *)this->private; - - if (pvt->proto.p_aliases) - free(pvt->proto.p_aliases); - if (pvt->prbuf) - free(pvt->prbuf); - - memput(pvt, sizeof *pvt); - memput(this, sizeof *this); -} - -static struct protoent * -pr_byname(struct irs_pr *this, const char *name) { - struct pvt *pvt = (struct pvt *)this->private; - struct dns_p *dns = pvt->dns; - struct protoent *proto; - char **hes_list; - - if (!(hes_list = hesiod_resolve(dns->hes_ctx, name, "protocol"))) - return (NULL); - - proto = parse_hes_list(this, hes_list); - hesiod_free_list(dns->hes_ctx, hes_list); - return (proto); -} - -static struct protoent * -pr_bynumber(struct irs_pr *this, int num) { - struct pvt *pvt = (struct pvt *)this->private; - struct dns_p *dns = pvt->dns; - struct protoent *proto; - char numstr[16]; - char **hes_list; - - sprintf(numstr, "%d", num); - if (!(hes_list = hesiod_resolve(dns->hes_ctx, numstr, "protonum"))) - return (NULL); - - proto = parse_hes_list(this, hes_list); - hesiod_free_list(dns->hes_ctx, hes_list); - return (proto); -} - -static struct protoent * -pr_next(struct irs_pr *this) { - UNUSED(this); - errno = ENODEV; - return (NULL); -} - -static void -pr_rewind(struct irs_pr *this) { - UNUSED(this); - /* NOOP */ -} - -static void -pr_minimize(struct irs_pr *this) { - UNUSED(this); - /* NOOP */ -} - -static struct __res_state * -pr_res_get(struct irs_pr *this) { - struct pvt *pvt = (struct pvt *)this->private; - struct dns_p *dns = pvt->dns; - - return (__hesiod_res_get(dns->hes_ctx)); -} - -static void -pr_res_set(struct irs_pr *this, struct __res_state * res, - void (*free_res)(void *)) { - struct pvt *pvt = (struct pvt *)this->private; - struct dns_p *dns = pvt->dns; - - __hesiod_res_set(dns->hes_ctx, res, free_res); -} - -/* Private. */ - -static struct protoent * -parse_hes_list(struct irs_pr *this, char **hes_list) { - struct pvt *pvt = (struct pvt *)this->private; - char *p, *cp, **cpp, **new; - int num = 0; - int max = 0; - - for (cpp = hes_list; *cpp; cpp++) { - cp = *cpp; - - /* Strip away comments, if any. */ - if ((p = strchr(cp, '#'))) - *p = 0; - - /* Skip blank lines. */ - p = cp; - while (*p && !isspace((unsigned char)*p)) - p++; - if (!*p) - continue; - - /* OK, we've got a live one. Let's parse it for real. */ - if (pvt->prbuf) - free(pvt->prbuf); - pvt->prbuf = strdup(cp); - - p = pvt->prbuf; - pvt->proto.p_name = p; - while (*p && !isspace((unsigned char)*p)) - p++; - if (!*p) - continue; - *p++ = '\0'; - - pvt->proto.p_proto = atoi(p); - while (*p && !isspace((unsigned char)*p)) - p++; - if (*p) - *p++ = '\0'; - - while (*p) { - if ((num + 1) >= max || !pvt->proto.p_aliases) { - max += 10; - new = realloc(pvt->proto.p_aliases, - max * sizeof(char *)); - if (!new) { - errno = ENOMEM; - goto cleanup; - } - pvt->proto.p_aliases = new; - } - pvt->proto.p_aliases[num++] = p; - while (*p && !isspace((unsigned char)*p)) - p++; - if (*p) - *p++ = '\0'; - } - if (!pvt->proto.p_aliases) - pvt->proto.p_aliases = malloc(sizeof(char *)); - if (!pvt->proto.p_aliases) - goto cleanup; - pvt->proto.p_aliases[num] = NULL; - return (&pvt->proto); - } - - cleanup: - if (pvt->proto.p_aliases) { - free(pvt->proto.p_aliases); - pvt->proto.p_aliases = NULL; - } - if (pvt->prbuf) { - free(pvt->prbuf); - pvt->prbuf = NULL; - } - return (NULL); -} - -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/dns_pw.c /usr/src/lib/libc/net/dns_pw.c --- /home/reed/src/isc/libbind/libbind/irs/dns_pw.c 2005-04-26 23:56:22.000000000 -0500 +++ /usr/src/lib/libc/net/dns_pw.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,232 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1996,1999 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#if defined(LIBC_SCCS) && !defined(lint) -static const char rcsid[] = "$Id: dns_pw.c,v 1.3 2005/04/27 04:56:22 sra Exp $"; -#endif - -#include "port_before.h" - -#ifndef WANT_IRS_PW -static int __bind_irs_pw_unneeded; -#else - -#include -#include -#include -#include - -#include -#include -#include -#include - -#include - -#include - -#include "port_after.h" - -#include "irs_p.h" -#include "hesiod.h" -#include "dns_p.h" - -/* Types. */ - -struct pvt { - struct dns_p * dns; - struct passwd passwd; - char * pwbuf; -}; - -/* Forward. */ - -static void pw_close(struct irs_pw *); -static struct passwd * pw_byname(struct irs_pw *, const char *); -static struct passwd * pw_byuid(struct irs_pw *, uid_t); -static struct passwd * pw_next(struct irs_pw *); -static void pw_rewind(struct irs_pw *); -static void pw_minimize(struct irs_pw *); -static struct __res_state * pw_res_get(struct irs_pw *); -static void pw_res_set(struct irs_pw *, - struct __res_state *, - void (*)(void *)); - -static struct passwd * getpwcommon(struct irs_pw *, const char *, - const char *); - -/* Public. */ - -struct irs_pw * -irs_dns_pw(struct irs_acc *this) { - struct dns_p *dns = (struct dns_p *)this->private; - struct irs_pw *pw; - struct pvt *pvt; - - if (!dns || !dns->hes_ctx) { - errno = ENODEV; - return (NULL); - } - if (!(pvt = memget(sizeof *pvt))) { - errno = ENOMEM; - return (NULL); - } - memset(pvt, 0, sizeof *pvt); - pvt->dns = dns; - if (!(pw = memget(sizeof *pw))) { - memput(pvt, sizeof *pvt); - errno = ENOMEM; - return (NULL); - } - memset(pw, 0x5e, sizeof *pw); - pw->private = pvt; - pw->close = pw_close; - pw->byname = pw_byname; - pw->byuid = pw_byuid; - pw->next = pw_next; - pw->rewind = pw_rewind; - pw->minimize = pw_minimize; - pw->res_get = pw_res_get; - pw->res_set = pw_res_set; - return (pw); -} - -/* Methods. */ - -static void -pw_close(struct irs_pw *this) { - struct pvt *pvt = (struct pvt *)this->private; - - if (pvt->pwbuf) - free(pvt->pwbuf); - - memput(pvt, sizeof *pvt); - memput(this, sizeof *this); -} - -static struct passwd * -pw_byname(struct irs_pw *this, const char *nam) { - return (getpwcommon(this, nam, "passwd")); -} - -static struct passwd * -pw_byuid(struct irs_pw *this, uid_t uid) { - char uidstr[16]; - - sprintf(uidstr, "%lu", (u_long)uid); - return (getpwcommon(this, uidstr, "uid")); -} - -static struct passwd * -pw_next(struct irs_pw *this) { - UNUSED(this); - errno = ENODEV; - return (NULL); -} - -static void -pw_rewind(struct irs_pw *this) { - UNUSED(this); - /* NOOP */ -} - -static void -pw_minimize(struct irs_pw *this) { - UNUSED(this); - /* NOOP */ -} - -static struct __res_state * -pw_res_get(struct irs_pw *this) { - struct pvt *pvt = (struct pvt *)this->private; - struct dns_p *dns = pvt->dns; - - return (__hesiod_res_get(dns->hes_ctx)); -} - -static void -pw_res_set(struct irs_pw *this, struct __res_state * res, - void (*free_res)(void *)) { - struct pvt *pvt = (struct pvt *)this->private; - struct dns_p *dns = pvt->dns; - - __hesiod_res_set(dns->hes_ctx, res, free_res); -} - -/* Private. */ - -static struct passwd * -getpwcommon(struct irs_pw *this, const char *arg, const char *type) { - struct pvt *pvt = (struct pvt *)this->private; - char **hes_list, *cp; - - if (!(hes_list = hesiod_resolve(pvt->dns->hes_ctx, arg, type))) - return (NULL); - if (!*hes_list) { - hesiod_free_list(pvt->dns->hes_ctx, hes_list); - errno = ENOENT; - return (NULL); - } - - memset(&pvt->passwd, 0, sizeof pvt->passwd); - if (pvt->pwbuf) - free(pvt->pwbuf); - pvt->pwbuf = strdup(*hes_list); - hesiod_free_list(pvt->dns->hes_ctx, hes_list); - - cp = pvt->pwbuf; - pvt->passwd.pw_name = cp; - if (!(cp = strchr(cp, ':'))) - goto cleanup; - *cp++ = '\0'; - - pvt->passwd.pw_passwd = cp; - if (!(cp = strchr(cp, ':'))) - goto cleanup; - *cp++ = '\0'; - - pvt->passwd.pw_uid = atoi(cp); - if (!(cp = strchr(cp, ':'))) - goto cleanup; - *cp++ = '\0'; - - pvt->passwd.pw_gid = atoi(cp); - if (!(cp = strchr(cp, ':'))) - goto cleanup; - *cp++ = '\0'; - - pvt->passwd.pw_gecos = cp; - if (!(cp = strchr(cp, ':'))) - goto cleanup; - *cp++ = '\0'; - - pvt->passwd.pw_dir = cp; - if (!(cp = strchr(cp, ':'))) - goto cleanup; - *cp++ = '\0'; - - pvt->passwd.pw_shell = cp; - return (&pvt->passwd); - - cleanup: - free(pvt->pwbuf); - pvt->pwbuf = NULL; - return (NULL); -} - -#endif /* WANT_IRS_PW */ -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/dns_sv.c /usr/src/lib/libc/net/dns_sv.c --- /home/reed/src/isc/libbind/libbind/irs/dns_sv.c 2005-04-26 23:56:23.000000000 -0500 +++ /usr/src/lib/libc/net/dns_sv.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,300 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1996,1999 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#if defined(LIBC_SCCS) && !defined(lint) -static const char rcsid[] = "$Id: dns_sv.c,v 1.5 2005/04/27 04:56:23 sra Exp $"; -#endif - -/* Imports */ - -#include "port_before.h" - -#include -#include - -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -#include -#include - -#include "port_after.h" - -#include "irs_p.h" -#include "hesiod.h" -#include "dns_p.h" - -/* Definitions */ - -struct pvt { - struct dns_p * dns; - struct servent serv; - char * svbuf; - struct __res_state * res; - void (*free_res)(void *); -}; - -/* Forward. */ - -static void sv_close(struct irs_sv *); -static struct servent * sv_byname(struct irs_sv *, - const char *, const char *); -static struct servent * sv_byport(struct irs_sv *, int, const char *); -static struct servent * sv_next(struct irs_sv *); -static void sv_rewind(struct irs_sv *); -static void sv_minimize(struct irs_sv *); -#ifdef SV_RES_SETGET -static struct __res_state * sv_res_get(struct irs_sv *); -static void sv_res_set(struct irs_sv *, - struct __res_state *, - void (*)(void *)); -#endif - -static struct servent * parse_hes_list(struct irs_sv *, - char **, const char *); - -/* Public */ - -struct irs_sv * -irs_dns_sv(struct irs_acc *this) { - struct dns_p *dns = (struct dns_p *)this->private; - struct irs_sv *sv; - struct pvt *pvt; - - if (!dns || !dns->hes_ctx) { - errno = ENODEV; - return (NULL); - } - if (!(pvt = memget(sizeof *pvt))) { - errno = ENOMEM; - return (NULL); - } - memset(pvt, 0, sizeof *pvt); - pvt->dns = dns; - if (!(sv = memget(sizeof *sv))) { - memput(pvt, sizeof *pvt); - errno = ENOMEM; - return (NULL); - } - memset(sv, 0x5e, sizeof *sv); - sv->private = pvt; - sv->byname = sv_byname; - sv->byport = sv_byport; - sv->next = sv_next; - sv->rewind = sv_rewind; - sv->close = sv_close; - sv->minimize = sv_minimize; -#ifdef SV_RES_SETGET - sv->res_get = sv_res_get; - sv->res_set = sv_res_set; -#else - sv->res_get = NULL; /*%< sv_res_get; */ - sv->res_set = NULL; /*%< sv_res_set; */ -#endif - return (sv); -} - -/* Methods */ - -static void -sv_close(struct irs_sv *this) { - struct pvt *pvt = (struct pvt *)this->private; - - if (pvt->serv.s_aliases) - free(pvt->serv.s_aliases); - if (pvt->svbuf) - free(pvt->svbuf); - - if (pvt->res && pvt->free_res) - (*pvt->free_res)(pvt->res); - memput(pvt, sizeof *pvt); - memput(this, sizeof *this); -} - -static struct servent * -sv_byname(struct irs_sv *this, const char *name, const char *proto) { - struct pvt *pvt = (struct pvt *)this->private; - struct dns_p *dns = pvt->dns; - struct servent *s; - char **hes_list; - - if (!(hes_list = hesiod_resolve(dns->hes_ctx, name, "service"))) - return (NULL); - - s = parse_hes_list(this, hes_list, proto); - hesiod_free_list(dns->hes_ctx, hes_list); - return (s); -} - -static struct servent * -sv_byport(struct irs_sv *this, int port, const char *proto) { - struct pvt *pvt = (struct pvt *)this->private; - struct dns_p *dns = pvt->dns; - struct servent *s; - char portstr[16]; - char **hes_list; - - sprintf(portstr, "%d", ntohs(port)); - if (!(hes_list = hesiod_resolve(dns->hes_ctx, portstr, "port"))) - return (NULL); - - s = parse_hes_list(this, hes_list, proto); - hesiod_free_list(dns->hes_ctx, hes_list); - return (s); -} - -static struct servent * -sv_next(struct irs_sv *this) { - UNUSED(this); - errno = ENODEV; - return (NULL); -} - -static void -sv_rewind(struct irs_sv *this) { - UNUSED(this); - /* NOOP */ -} - -/* Private */ - -static struct servent * -parse_hes_list(struct irs_sv *this, char **hes_list, const char *proto) { - struct pvt *pvt = (struct pvt *)this->private; - char *p, *cp, **cpp, **new; - int proto_len; - int num = 0; - int max = 0; - - for (cpp = hes_list; *cpp; cpp++) { - cp = *cpp; - - /* Strip away comments, if any. */ - if ((p = strchr(cp, '#'))) - *p = 0; - - /* Check to make sure the protocol matches. */ - p = cp; - while (*p && !isspace((unsigned char)*p)) - p++; - if (!*p) - continue; - if (proto) { - proto_len = strlen(proto); - if (strncasecmp(++p, proto, proto_len) != 0) - continue; - if (p[proto_len] && !isspace(p[proto_len]&0xff)) - continue; - } - /* OK, we've got a live one. Let's parse it for real. */ - if (pvt->svbuf) - free(pvt->svbuf); - pvt->svbuf = strdup(cp); - - p = pvt->svbuf; - pvt->serv.s_name = p; - while (*p && !isspace(*p&0xff)) - p++; - if (!*p) - continue; - *p++ = '\0'; - - pvt->serv.s_proto = p; - while (*p && !isspace(*p&0xff)) - p++; - if (!*p) - continue; - *p++ = '\0'; - - pvt->serv.s_port = htons((u_short) atoi(p)); - while (*p && !isspace(*p&0xff)) - p++; - if (*p) - *p++ = '\0'; - - while (*p) { - if ((num + 1) >= max || !pvt->serv.s_aliases) { - max += 10; - new = realloc(pvt->serv.s_aliases, - max * sizeof(char *)); - if (!new) { - errno = ENOMEM; - goto cleanup; - } - pvt->serv.s_aliases = new; - } - pvt->serv.s_aliases[num++] = p; - while (*p && !isspace(*p&0xff)) - p++; - if (*p) - *p++ = '\0'; - } - if (!pvt->serv.s_aliases) - pvt->serv.s_aliases = malloc(sizeof(char *)); - if (!pvt->serv.s_aliases) - goto cleanup; - pvt->serv.s_aliases[num] = NULL; - return (&pvt->serv); - } - - cleanup: - if (pvt->serv.s_aliases) { - free(pvt->serv.s_aliases); - pvt->serv.s_aliases = NULL; - } - if (pvt->svbuf) { - free(pvt->svbuf); - pvt->svbuf = NULL; - } - return (NULL); -} - -static void -sv_minimize(struct irs_sv *this) { - UNUSED(this); - /* NOOP */ -} - -#ifdef SV_RES_SETGET -static struct __res_state * -sv_res_get(struct irs_sv *this) { - struct pvt *pvt = (struct pvt *)this->private; - struct dns_p *dns = pvt->dns; - - return (__hesiod_res_get(dns->hes_ctx)); -} - -static void -sv_res_set(struct irs_sv *this, struct __res_state * res, - void (*free_res)(void *)) { - struct pvt *pvt = (struct pvt *)this->private; - struct dns_p *dns = pvt->dns; - - __hesiod_res_set(dns->hes_ctx, res, free_res); -} -#endif - -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/ethers.c /usr/src/lib/libc/net/ethers.c --- /home/reed/src/isc/libbind/libbind/irs/ethers.c 1969-12-31 18:00:00.000000000 -0600 +++ /usr/src/lib/libc/net/ethers.c 2013-06-05 09:26:12.000000000 -0500 @@ -0,0 +1,221 @@ +/* $NetBSD: ethers.c,v 1.23 2012/03/20 17:44:18 matt Exp $ */ + +/* + * ethers(3N) a la Sun. + * + * Written by Roland McGrath 10/14/93. + * Public domain. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: ethers.c,v 1.23 2012/03/20 17:44:18 matt Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#ifdef YP +#include +#endif + +#ifdef __weak_alias +__weak_alias(ether_aton,_ether_aton) +__weak_alias(ether_hostton,_ether_hostton) +__weak_alias(ether_line,_ether_line) +__weak_alias(ether_ntoa,_ether_ntoa) +__weak_alias(ether_ntohost,_ether_ntohost) +#endif + +#ifndef _PATH_ETHERS +#define _PATH_ETHERS "/etc/ethers" +#endif + +char * +ether_ntoa(const struct ether_addr *e) +{ + static char a[18]; + + _DIAGASSERT(e != NULL); + + (void) snprintf(a, sizeof a, "%02x:%02x:%02x:%02x:%02x:%02x", + e->ether_addr_octet[0], e->ether_addr_octet[1], + e->ether_addr_octet[2], e->ether_addr_octet[3], + e->ether_addr_octet[4], e->ether_addr_octet[5]); + return a; +} + +struct ether_addr * +ether_aton(const char *s) +{ + static struct ether_addr n; + u_int i[6]; + + _DIAGASSERT(s != NULL); + + if (sscanf(s, " %x:%x:%x:%x:%x:%x ", &i[0], &i[1], + &i[2], &i[3], &i[4], &i[5]) == 6) { + n.ether_addr_octet[0] = (u_char)i[0]; + n.ether_addr_octet[1] = (u_char)i[1]; + n.ether_addr_octet[2] = (u_char)i[2]; + n.ether_addr_octet[3] = (u_char)i[3]; + n.ether_addr_octet[4] = (u_char)i[4]; + n.ether_addr_octet[5] = (u_char)i[5]; + return &n; + } + return NULL; +} + +int +ether_ntohost(char *hostname, const struct ether_addr *e) +{ + FILE *f; + char *p; + size_t len; + struct ether_addr try; +#ifdef YP + char trybuf[sizeof "xx:xx:xx:xx:xx:xx"]; + int trylen; +#endif + + _DIAGASSERT(hostname != NULL); + _DIAGASSERT(e != NULL); + +#ifdef YP + trylen = snprintf(trybuf, sizeof trybuf, "%x:%x:%x:%x:%x:%x", + e->ether_addr_octet[0], e->ether_addr_octet[1], + e->ether_addr_octet[2], e->ether_addr_octet[3], + e->ether_addr_octet[4], e->ether_addr_octet[5]); +#endif + + f = fopen(_PATH_ETHERS, "r"); + if (f == NULL) + return -1; + while ((p = fgetln(f, &len)) != NULL) { + if (p[len - 1] != '\n') + continue; /* skip lines w/o \n */ + p[--len] = '\0'; +#ifdef YP + /* A + in the file means try YP now. */ + if (len == 1 && *p == '+') { + char *ypbuf, *ypdom; + int ypbuflen; + + if (yp_get_default_domain(&ypdom)) + continue; + if (yp_match(ypdom, "ethers.byaddr", trybuf, + trylen, &ypbuf, &ypbuflen)) + continue; + if (ether_line(ypbuf, &try, hostname) == 0) { + free(ypbuf); + (void)fclose(f); + return 0; + } + free(ypbuf); + continue; + } +#endif + if (ether_line(p, &try, hostname) == 0 && + memcmp(&try, e, sizeof try) == 0) { + (void)fclose(f); + return 0; + } + } + (void)fclose(f); + errno = ENOENT; + return -1; +} + +int +ether_hostton(const char *hostname, struct ether_addr *e) +{ + FILE *f; + char *p; + size_t len; + char try[MAXHOSTNAMELEN + 1]; +#ifdef YP + int hostlen = (int)strlen(hostname); +#endif + + _DIAGASSERT(hostname != NULL); + _DIAGASSERT(e != NULL); + + f = fopen(_PATH_ETHERS, "r"); + if (f==NULL) + return -1; + + while ((p = fgetln(f, &len)) != NULL) { + if (p[len - 1] != '\n') + continue; /* skip lines w/o \n */ + p[--len] = '\0'; +#ifdef YP + /* A + in the file means try YP now. */ + if (len == 1 && *p == '+') { + char *ypbuf, *ypdom; + int ypbuflen; + + if (yp_get_default_domain(&ypdom)) + continue; + if (yp_match(ypdom, "ethers.byname", hostname, hostlen, + &ypbuf, &ypbuflen)) + continue; + if (ether_line(ypbuf, e, try) == 0) { + free(ypbuf); + (void)fclose(f); + return 0; + } + free(ypbuf); + continue; + } +#endif + if (ether_line(p, e, try) == 0 && strcmp(hostname, try) == 0) { + (void)fclose(f); + return 0; + } + } + (void)fclose(f); + errno = ENOENT; + return -1; +} + +int +ether_line(const char *l, struct ether_addr *e, char *hostname) +{ + u_int i[6]; + +#define S2(arg) #arg +#define S1(arg) S2(arg) + static const char fmt[] = " %x:%x:%x:%x:%x:%x" + " %" S1(MAXHOSTNAMELEN) "s\n"; +#undef S2 +#undef S1 + + _DIAGASSERT(l != NULL); + _DIAGASSERT(e != NULL); + _DIAGASSERT(hostname != NULL); + + if (sscanf(l, fmt, + &i[0], &i[1], &i[2], &i[3], &i[4], &i[5], hostname) == 7) { + e->ether_addr_octet[0] = (u_char)i[0]; + e->ether_addr_octet[1] = (u_char)i[1]; + e->ether_addr_octet[2] = (u_char)i[2]; + e->ether_addr_octet[3] = (u_char)i[3]; + e->ether_addr_octet[4] = (u_char)i[4]; + e->ether_addr_octet[5] = (u_char)i[5]; + return 0; + } + errno = EINVAL; + return -1; +} diff -pruN /home/reed/src/isc/libbind/libbind/irs/gai_strerror.c /usr/src/lib/libc/net/gai_strerror.c --- /home/reed/src/isc/libbind/libbind/irs/gai_strerror.c 2007-08-26 22:32:26.000000000 -0500 +++ /usr/src/lib/libc/net/gai_strerror.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,105 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 2001 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#include -#include -#include - -#ifdef DO_PTHREADS -#include -#include -#endif - -static const char *gai_errlist[] = { - "no error", - "address family not supported for name",/*%< EAI_ADDRFAMILY */ - "temporary failure", /*%< EAI_AGAIN */ - "invalid flags", /*%< EAI_BADFLAGS */ - "permanent failure", /*%< EAI_FAIL */ - "address family not supported", /*%< EAI_FAMILY */ - "memory failure", /*%< EAI_MEMORY */ - "no address", /*%< EAI_NODATA */ - "unknown name or service", /*%< EAI_NONAME */ - "service not supported for socktype", /*%< EAI_SERVICE */ - "socktype not supported", /*%< EAI_SOCKTYPE */ - "system failure", /*%< EAI_SYSTEM */ - "bad hints", /*%< EAI_BADHINTS */ - "bad protocol", /*%< EAI_PROTOCOL */ - "unknown error" /*%< Must be last. */ -}; - -static const int gai_nerr = (sizeof(gai_errlist)/sizeof(*gai_errlist)); - -#define EAI_BUFSIZE 128 - -const char * -gai_strerror(int ecode) { -#ifndef DO_PTHREADS - static char buf[EAI_BUFSIZE]; -#else /* DO_PTHREADS */ -#ifndef LIBBIND_MUTEX_INITIALIZER -#define LIBBIND_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER -#endif - static pthread_mutex_t lock = LIBBIND_MUTEX_INITIALIZER; - static pthread_key_t key; - static int once = 0; - char *buf; -#endif - - if (ecode >= 0 && ecode < (gai_nerr - 1)) - return (gai_errlist[ecode]); - -#ifdef DO_PTHREADS - if (!once) { - if (pthread_mutex_lock(&lock) != 0) - goto unknown; - if (!once) { - if (pthread_key_create(&key, free) != 0) { - (void)pthread_mutex_unlock(&lock); - goto unknown; - } - once = 1; - } - if (pthread_mutex_unlock(&lock) != 0) - goto unknown; - } - - buf = pthread_getspecific(key); - if (buf == NULL) { - buf = malloc(EAI_BUFSIZE); - if (buf == NULL) - goto unknown; - if (pthread_setspecific(key, buf) != 0) { - free(buf); - goto unknown; - } - } -#endif - /* - * XXX This really should be snprintf(buf, EAI_BUFSIZE, ...). - * It is safe until message catalogs are used. - */ - sprintf(buf, "%s: %d", gai_errlist[gai_nerr - 1], ecode); - return (buf); - -#ifdef DO_PTHREADS - unknown: - return ("unknown error"); -#endif -} - -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/gen.c /usr/src/lib/libc/net/gen.c --- /home/reed/src/isc/libbind/libbind/irs/gen.c 2005-04-26 23:56:23.000000000 -0500 +++ /usr/src/lib/libc/net/gen.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,433 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1996-1999 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#if !defined(LINT) && !defined(CODECENTER) -static const char rcsid[] = "$Id: gen.c,v 1.7 2005/04/27 04:56:23 sra Exp $"; -#endif - -/*! \file - * \brief - * this is the top level dispatcher - * - * The dispatcher is implemented as an accessor class; it is an - * accessor class that calls other accessor classes, as controlled by a - * configuration file. - * - * A big difference between this accessor class and others is that the - * map class initializers are NULL, and the map classes are already - * filled in with method functions that will do the right thing. - */ - -/* Imports */ - -#include "port_before.h" - -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -#include -#include - -#include "port_after.h" - -#include "irs_p.h" -#include "gen_p.h" - -/* Definitions */ - -struct nameval { - const char * name; - int val; -}; - -static const struct nameval acc_names[irs_nacc+1] = { - { "local", irs_lcl }, - { "dns", irs_dns }, - { "nis", irs_nis }, - { "irp", irs_irp }, - { NULL, irs_nacc } -}; - -typedef struct irs_acc *(*accinit) __P((const char *options)); - -static const accinit accs[irs_nacc+1] = { - irs_lcl_acc, - irs_dns_acc, -#ifdef WANT_IRS_NIS - irs_nis_acc, -#else - NULL, -#endif - irs_irp_acc, - NULL -}; - -static const struct nameval map_names[irs_nmap+1] = { - { "group", irs_gr }, - { "passwd", irs_pw }, - { "services", irs_sv }, - { "protocols", irs_pr }, - { "hosts", irs_ho }, - { "networks", irs_nw }, - { "netgroup", irs_ng }, - { NULL, irs_nmap } -}; - -static const struct nameval option_names[] = { - { "merge", IRS_MERGE }, - { "continue", IRS_CONTINUE }, - { NULL, 0 } -}; - -/* Forward */ - -static void gen_close(struct irs_acc *); -static struct __res_state * gen_res_get(struct irs_acc *); -static void gen_res_set(struct irs_acc *, struct __res_state *, - void (*)(void *)); -static int find_name(const char *, const struct nameval nv[]); -static void init_map_rules(struct gen_p *, const char *conf_file); -static struct irs_rule *release_rule(struct irs_rule *); -static int add_rule(struct gen_p *, - enum irs_map_id, enum irs_acc_id, - const char *); - -/* Public */ - -struct irs_acc * -irs_gen_acc(const char *options, const char *conf_file) { - struct irs_acc *acc; - struct gen_p *irs; - - if (!(acc = memget(sizeof *acc))) { - errno = ENOMEM; - return (NULL); - } - memset(acc, 0x5e, sizeof *acc); - if (!(irs = memget(sizeof *irs))) { - errno = ENOMEM; - memput(acc, sizeof *acc); - return (NULL); - } - memset(irs, 0x5e, sizeof *irs); - irs->options = strdup(options); - irs->res = NULL; - irs->free_res = NULL; - memset(irs->accessors, 0, sizeof irs->accessors); - memset(irs->map_rules, 0, sizeof irs->map_rules); - init_map_rules(irs, conf_file); - acc->private = irs; -#ifdef WANT_IRS_GR - acc->gr_map = irs_gen_gr; -#else - acc->gr_map = NULL; -#endif -#ifdef WANT_IRS_PW - acc->pw_map = irs_gen_pw; -#else - acc->pw_map = NULL; -#endif - acc->sv_map = irs_gen_sv; - acc->pr_map = irs_gen_pr; - acc->ho_map = irs_gen_ho; - acc->nw_map = irs_gen_nw; - acc->ng_map = irs_gen_ng; - acc->res_get = gen_res_get; - acc->res_set = gen_res_set; - acc->close = gen_close; - return (acc); -} - -/* Methods */ - -static struct __res_state * -gen_res_get(struct irs_acc *this) { - struct gen_p *irs = (struct gen_p *)this->private; - - if (irs->res == NULL) { - struct __res_state *res; - res = (struct __res_state *)malloc(sizeof *res); - if (res == NULL) - return (NULL); - memset(res, 0, sizeof *res); - gen_res_set(this, res, free); - } - - if (((irs->res->options & RES_INIT) == 0U) && res_ninit(irs->res) < 0) - return (NULL); - - return (irs->res); -} - -static void -gen_res_set(struct irs_acc *this, struct __res_state *res, - void (*free_res)(void *)) { - struct gen_p *irs = (struct gen_p *)this->private; -#if 0 - struct irs_rule *rule; - struct irs_ho *ho; - struct irs_nw *nw; -#endif - - if (irs->res && irs->free_res) { - res_nclose(irs->res); - (*irs->free_res)(irs->res); - } - - irs->res = res; - irs->free_res = free_res; - -#if 0 - for (rule = irs->map_rules[irs_ho]; rule; rule = rule->next) { - ho = rule->inst->ho; - - (*ho->res_set)(ho, res, NULL); - } - for (rule = irs->map_rules[irs_nw]; rule; rule = rule->next) { - nw = rule->inst->nw; - - (*nw->res_set)(nw, res, NULL); - } -#endif -} - -static void -gen_close(struct irs_acc *this) { - struct gen_p *irs = (struct gen_p *)this->private; - int n; - - /* Search rules. */ - for (n = 0; n < irs_nmap; n++) - while (irs->map_rules[n] != NULL) - irs->map_rules[n] = release_rule(irs->map_rules[n]); - - /* Access methods. */ - for (n = 0; n < irs_nacc; n++) { - /* Map objects. */ - if (irs->accessors[n].gr != NULL) - (*irs->accessors[n].gr->close)(irs->accessors[n].gr); - if (irs->accessors[n].pw != NULL) - (*irs->accessors[n].pw->close)(irs->accessors[n].pw); - if (irs->accessors[n].sv != NULL) - (*irs->accessors[n].sv->close)(irs->accessors[n].sv); - if (irs->accessors[n].pr != NULL) - (*irs->accessors[n].pr->close)(irs->accessors[n].pr); - if (irs->accessors[n].ho != NULL) - (*irs->accessors[n].ho->close)(irs->accessors[n].ho); - if (irs->accessors[n].nw != NULL) - (*irs->accessors[n].nw->close)(irs->accessors[n].nw); - if (irs->accessors[n].ng != NULL) - (*irs->accessors[n].ng->close)(irs->accessors[n].ng); - /* Enclosing accessor. */ - if (irs->accessors[n].acc != NULL) - (*irs->accessors[n].acc->close)(irs->accessors[n].acc); - } - - /* The options string was strdup'd. */ - free((void*)irs->options); - - if (irs->res && irs->free_res) - (*irs->free_res)(irs->res); - - /* The private data container. */ - memput(irs, sizeof *irs); - - /* The object. */ - memput(this, sizeof *this); -} - -/* Private */ - -static int -find_name(const char *name, const struct nameval names[]) { - int n; - - for (n = 0; names[n].name != NULL; n++) - if (strcmp(name, names[n].name) == 0) - return (names[n].val); - return (-1); -} - -static struct irs_rule * -release_rule(struct irs_rule *rule) { - struct irs_rule *next = rule->next; - - memput(rule, sizeof *rule); - return (next); -} - -static int -add_rule(struct gen_p *irs, - enum irs_map_id map, enum irs_acc_id acc, - const char *options) -{ - struct irs_rule **rules, *last, *tmp, *new; - struct irs_inst *inst; - const char *cp; - int n; - -#ifndef WANT_IRS_GR - if (map == irs_gr) - return (-1); -#endif -#ifndef WANT_IRS_PW - if (map == irs_pw) - return (-1); -#endif -#ifndef WANT_IRS_NIS - if (acc == irs_nis) - return (-1); -#endif - new = memget(sizeof *new); - if (new == NULL) - return (-1); - memset(new, 0x5e, sizeof *new); - new->next = NULL; - - new->inst = &irs->accessors[acc]; - - new->flags = 0; - cp = options; - while (cp && *cp) { - char option[50], *next; - - next = strchr(cp, ','); - if (next) - n = next++ - cp; - else - n = strlen(cp); - if ((size_t)n > sizeof option - 1) - n = sizeof option - 1; - strncpy(option, cp, n); - option[n] = '\0'; - - n = find_name(option, option_names); - if (n >= 0) - new->flags |= n; - - cp = next; - } - - rules = &irs->map_rules[map]; - for (last = NULL, tmp = *rules; - tmp != NULL; - last = tmp, tmp = tmp->next) - (void)NULL; - if (last == NULL) - *rules = new; - else - last->next = new; - - /* Try to instantiate map accessors for this if necessary & approp. */ - inst = &irs->accessors[acc]; - if (inst->acc == NULL && accs[acc] != NULL) - inst->acc = (*accs[acc])(irs->options); - if (inst->acc != NULL) { - if (inst->gr == NULL && inst->acc->gr_map != NULL) - inst->gr = (*inst->acc->gr_map)(inst->acc); - if (inst->pw == NULL && inst->acc->pw_map != NULL) - inst->pw = (*inst->acc->pw_map)(inst->acc); - if (inst->sv == NULL && inst->acc->sv_map != NULL) - inst->sv = (*inst->acc->sv_map)(inst->acc); - if (inst->pr == NULL && inst->acc->pr_map != NULL) - inst->pr = (*inst->acc->pr_map)(inst->acc); - if (inst->ho == NULL && inst->acc->ho_map != NULL) - inst->ho = (*inst->acc->ho_map)(inst->acc); - if (inst->nw == NULL && inst->acc->nw_map != NULL) - inst->nw = (*inst->acc->nw_map)(inst->acc); - if (inst->ng == NULL && inst->acc->ng_map != NULL) - inst->ng = (*inst->acc->ng_map)(inst->acc); - } - - return (0); -} - -static void -default_map_rules(struct gen_p *irs) { - /* Install time honoured and proved BSD style rules as default. */ - add_rule(irs, irs_gr, irs_lcl, ""); - add_rule(irs, irs_pw, irs_lcl, ""); - add_rule(irs, irs_sv, irs_lcl, ""); - add_rule(irs, irs_pr, irs_lcl, ""); - add_rule(irs, irs_ho, irs_dns, "continue"); - add_rule(irs, irs_ho, irs_lcl, ""); - add_rule(irs, irs_nw, irs_dns, "continue"); - add_rule(irs, irs_nw, irs_lcl, ""); - add_rule(irs, irs_ng, irs_lcl, ""); -} - -static void -init_map_rules(struct gen_p *irs, const char *conf_file) { - char line[1024], pattern[40], mapname[20], accname[20], options[100]; - FILE *conf; - - if (conf_file == NULL) - conf_file = _PATH_IRS_CONF ; - - /* A conf file of "" means compiled in defaults. Irpd wants this */ - if (conf_file[0] == '\0' || (conf = fopen(conf_file, "r")) == NULL) { - default_map_rules(irs); - return; - } - (void) sprintf(pattern, "%%%lus %%%lus %%%lus\n", - (unsigned long)sizeof mapname, - (unsigned long)sizeof accname, - (unsigned long)sizeof options); - while (fgets(line, sizeof line, conf)) { - enum irs_map_id map; - enum irs_acc_id acc; - char *tmp; - int n; - - for (tmp = line; - isascii((unsigned char)*tmp) && - isspace((unsigned char)*tmp); - tmp++) - (void)NULL; - if (*tmp == '#' || *tmp == '\n' || *tmp == '\0') - continue; - n = sscanf(tmp, pattern, mapname, accname, options); - if (n < 2) - continue; - if (n < 3) - options[0] = '\0'; - - n = find_name(mapname, map_names); - INSIST(n < irs_nmap); - if (n < 0) - continue; - map = (enum irs_map_id) n; - - n = find_name(accname, acc_names); - INSIST(n < irs_nacc); - if (n < 0) - continue; - acc = (enum irs_acc_id) n; - - add_rule(irs, map, acc, options); - } - fclose(conf); -} diff -pruN /home/reed/src/isc/libbind/libbind/irs/gen_gr.c /usr/src/lib/libc/net/gen_gr.c --- /home/reed/src/isc/libbind/libbind/irs/gen_gr.c 2005-04-26 23:56:23.000000000 -0500 +++ /usr/src/lib/libc/net/gen_gr.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,493 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1996-1999 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#if !defined(LINT) && !defined(CODECENTER) -static const char rcsid[] = "$Id: gen_gr.c,v 1.8 2005/04/27 04:56:23 sra Exp $"; -#endif - -/* Imports */ - -#include "port_before.h" - -#ifndef WANT_IRS_GR -static int __bind_irs_gr_unneeded; -#else - -#include - -#include -#include -#include -#include -#include - -#include -#include -#include - -#include -#include - -#include "port_after.h" - -#include "irs_p.h" -#include "gen_p.h" - -/* Definitions */ - -struct pvt { - struct irs_rule * rules; - struct irs_rule * rule; - struct irs_gr * gr; - /* - * Need space to store the entries read from the group file. - * The members list also needs space per member, and the - * strings making up the user names must be allocated - * somewhere. Rather than doing lots of small allocations, - * we keep one buffer and resize it as needed. - */ - struct group group; - size_t nmemb; /*%< Malloc'd max index of gr_mem[]. */ - char * membuf; - size_t membufsize; - struct __res_state * res; - void (*free_res)(void *); -}; - -/* Forward */ - -static void gr_close(struct irs_gr *); -static struct group * gr_next(struct irs_gr *); -static struct group * gr_byname(struct irs_gr *, const char *); -static struct group * gr_bygid(struct irs_gr *, gid_t); -static void gr_rewind(struct irs_gr *); -static int gr_list(struct irs_gr *, const char *, - gid_t, gid_t *, int *); -static void gr_minimize(struct irs_gr *); -static struct __res_state * gr_res_get(struct irs_gr *); -static void gr_res_set(struct irs_gr *, - struct __res_state *, - void (*)(void *)); - -static int grmerge(struct irs_gr *gr, const struct group *src, - int preserve); - -static int countvec(char **vec); -static int isnew(char **old, char *new); -static int countnew(char **old, char **new); -static size_t sizenew(char **old, char **new); -static int newgid(int, gid_t *, gid_t); - -/* Macros */ - -#define FREE_IF(x) do { if ((x) != NULL) { free(x); (x) = NULL; } } while (0) - -/* Public */ - -struct irs_gr * -irs_gen_gr(struct irs_acc *this) { - struct gen_p *accpvt = (struct gen_p *)this->private; - struct irs_gr *gr; - struct pvt *pvt; - - if (!(gr = memget(sizeof *gr))) { - errno = ENOMEM; - return (NULL); - } - memset(gr, 0x5e, sizeof *gr); - if (!(pvt = memget(sizeof *pvt))) { - memput(gr, sizeof *gr); - errno = ENOMEM; - return (NULL); - } - memset(pvt, 0, sizeof *pvt); - pvt->rules = accpvt->map_rules[irs_gr]; - pvt->rule = pvt->rules; - gr->private = pvt; - gr->close = gr_close; - gr->next = gr_next; - gr->byname = gr_byname; - gr->bygid = gr_bygid; - gr->rewind = gr_rewind; - gr->list = gr_list; - gr->minimize = gr_minimize; - gr->res_get = gr_res_get; - gr->res_set = gr_res_set; - return (gr); -} - -/* Methods. */ - -static void -gr_close(struct irs_gr *this) { - struct pvt *pvt = (struct pvt *)this->private; - - memput(pvt, sizeof *pvt); - memput(this, sizeof *this); -} - -static struct group * -gr_next(struct irs_gr *this) { - struct pvt *pvt = (struct pvt *)this->private; - struct group *rval; - struct irs_gr *gr; - - while (pvt->rule) { - gr = pvt->rule->inst->gr; - rval = (*gr->next)(gr); - if (rval) - return (rval); - if (!(pvt->rule->flags & IRS_CONTINUE)) - break; - pvt->rule = pvt->rule->next; - if (pvt->rule) { - gr = pvt->rule->inst->gr; - (*gr->rewind)(gr); - } - } - return (NULL); -} - -static struct group * -gr_byname(struct irs_gr *this, const char *name) { - struct pvt *pvt = (struct pvt *)this->private; - struct irs_rule *rule; - struct group *tval; - struct irs_gr *gr; - int dirty; - - dirty = 0; - for (rule = pvt->rules; rule; rule = rule->next) { - gr = rule->inst->gr; - tval = (*gr->byname)(gr, name); - if (tval) { - if (!grmerge(this, tval, dirty++)) - return (NULL); - if (!(rule->flags & IRS_MERGE)) - break; - } else { - if (!(rule->flags & IRS_CONTINUE)) - break; - } - } - if (dirty) - return (&pvt->group); - return (NULL); -} - -static struct group * -gr_bygid(struct irs_gr *this, gid_t gid) { - struct pvt *pvt = (struct pvt *)this->private; - struct irs_rule *rule; - struct group *tval; - struct irs_gr *gr; - int dirty; - - dirty = 0; - for (rule = pvt->rules; rule; rule = rule->next) { - gr = rule->inst->gr; - tval = (*gr->bygid)(gr, gid); - if (tval) { - if (!grmerge(this, tval, dirty++)) - return (NULL); - if (!(rule->flags & IRS_MERGE)) - break; - } else { - if (!(rule->flags & IRS_CONTINUE)) - break; - } - } - if (dirty) - return (&pvt->group); - return (NULL); -} - -static void -gr_rewind(struct irs_gr *this) { - struct pvt *pvt = (struct pvt *)this->private; - struct irs_gr *gr; - - pvt->rule = pvt->rules; - if (pvt->rule) { - gr = pvt->rule->inst->gr; - (*gr->rewind)(gr); - } -} - -static int -gr_list(struct irs_gr *this, const char *name, - gid_t basegid, gid_t *groups, int *ngroups) -{ - struct pvt *pvt = (struct pvt *)this->private; - struct irs_rule *rule; - struct irs_gr *gr; - int t_ngroups, maxgroups; - gid_t *t_groups; - int n, t, rval = 0; - - maxgroups = *ngroups; - *ngroups = 0; - t_groups = (gid_t *)malloc(maxgroups * sizeof(gid_t)); - if (!t_groups) { - errno = ENOMEM; - return (-1); - } - - for (rule = pvt->rules; rule; rule = rule->next) { - t_ngroups = maxgroups; - gr = rule->inst->gr; - t = (*gr->list)(gr, name, basegid, t_groups, &t_ngroups); - for (n = 0; n < t_ngroups; n++) { - if (newgid(*ngroups, groups, t_groups[n])) { - if (*ngroups == maxgroups) { - rval = -1; - goto done; - } - groups[(*ngroups)++] = t_groups[n]; - } - } - if (t == 0) { - if (!(rule->flags & IRS_MERGE)) - break; - } else { - if (!(rule->flags & IRS_CONTINUE)) - break; - } - } - done: - free(t_groups); - return (rval); -} - -static void -gr_minimize(struct irs_gr *this) { - struct pvt *pvt = (struct pvt *)this->private; - struct irs_rule *rule; - - for (rule = pvt->rules; rule != NULL; rule = rule->next) { - struct irs_gr *gr = rule->inst->gr; - - (*gr->minimize)(gr); - } -} - -static struct __res_state * -gr_res_get(struct irs_gr *this) { - struct pvt *pvt = (struct pvt *)this->private; - - if (!pvt->res) { - struct __res_state *res; - res = (struct __res_state *)malloc(sizeof *res); - if (!res) { - errno = ENOMEM; - return (NULL); - } - memset(res, 0, sizeof *res); - gr_res_set(this, res, free); - } - - return (pvt->res); -} - -static void -gr_res_set(struct irs_gr *this, struct __res_state *res, - void (*free_res)(void *)) { - struct pvt *pvt = (struct pvt *)this->private; - struct irs_rule *rule; - - if (pvt->res && pvt->free_res) { - res_nclose(pvt->res); - (*pvt->free_res)(pvt->res); - } - - pvt->res = res; - pvt->free_res = free_res; - - for (rule = pvt->rules; rule != NULL; rule = rule->next) { - struct irs_gr *gr = rule->inst->gr; - - if (gr->res_set) - (*gr->res_set)(gr, pvt->res, NULL); - } -} - -/* Private. */ - -static int -grmerge(struct irs_gr *this, const struct group *src, int preserve) { - struct pvt *pvt = (struct pvt *)this->private; - char *cp, **m, **p, *oldmembuf, *ep; - int n, ndst, nnew; - size_t used; - - if (!preserve) { - pvt->group.gr_gid = src->gr_gid; - if (pvt->nmemb < 1) { - m = malloc(sizeof *m); - if (m == NULL) { - /* No harm done, no work done. */ - return (0); - } - pvt->group.gr_mem = m; - pvt->nmemb = 1; - } - pvt->group.gr_mem[0] = NULL; - } - ndst = countvec(pvt->group.gr_mem); - nnew = countnew(pvt->group.gr_mem, src->gr_mem); - - /* - * Make sure destination member array is large enough. - * p points to new portion. - */ - n = ndst + nnew + 1; - if ((size_t)n > pvt->nmemb) { - m = realloc(pvt->group.gr_mem, n * sizeof *m); - if (m == NULL) { - /* No harm done, no work done. */ - return (0); - } - pvt->group.gr_mem = m; - pvt->nmemb = n; - } - p = pvt->group.gr_mem + ndst; - - /* - * Enlarge destination membuf; cp points at new portion. - */ - n = sizenew(pvt->group.gr_mem, src->gr_mem); - INSIST((nnew == 0) == (n == 0)); - if (!preserve) { - n += strlen(src->gr_name) + 1; - n += strlen(src->gr_passwd) + 1; - } - if (n == 0) { - /* No work to do. */ - return (1); - } - used = preserve ? pvt->membufsize : 0; - cp = malloc(used + n); - if (cp == NULL) { - /* No harm done, no work done. */ - return (0); - } - ep = cp + used + n; - if (used != 0) - memcpy(cp, pvt->membuf, used); - oldmembuf = pvt->membuf; - pvt->membuf = cp; - pvt->membufsize = used + n; - cp += used; - - /* - * Adjust group.gr_mem. - */ - if (pvt->membuf != oldmembuf) - for (m = pvt->group.gr_mem; *m; m++) - *m = pvt->membuf + (*m - oldmembuf); - - /* - * Add new elements. - */ - for (m = src->gr_mem; *m; m++) - if (isnew(pvt->group.gr_mem, *m)) { - *p++ = cp; - *p = NULL; - n = strlen(*m) + 1; - if (n > ep - cp) { - FREE_IF(oldmembuf); - return (0); - } - strcpy(cp, *m); /* (checked) */ - cp += n; - } - if (preserve) { - pvt->group.gr_name = pvt->membuf + - (pvt->group.gr_name - oldmembuf); - pvt->group.gr_passwd = pvt->membuf + - (pvt->group.gr_passwd - oldmembuf); - } else { - pvt->group.gr_name = cp; - n = strlen(src->gr_name) + 1; - if (n > ep - cp) { - FREE_IF(oldmembuf); - return (0); - } - strcpy(cp, src->gr_name); /* (checked) */ - cp += n; - - pvt->group.gr_passwd = cp; - n = strlen(src->gr_passwd) + 1; - if (n > ep - cp) { - FREE_IF(oldmembuf); - return (0); - } - strcpy(cp, src->gr_passwd); /* (checked) */ - cp += n; - } - FREE_IF(oldmembuf); - INSIST(cp >= pvt->membuf && cp <= &pvt->membuf[pvt->membufsize]); - return (1); -} - -static int -countvec(char **vec) { - int n = 0; - - while (*vec++) - n++; - return (n); -} - -static int -isnew(char **old, char *new) { - for (; *old; old++) - if (strcmp(*old, new) == 0) - return (0); - return (1); -} - -static int -countnew(char **old, char **new) { - int n = 0; - - for (; *new; new++) - n += isnew(old, *new); - return (n); -} - -static size_t -sizenew(char **old, char **new) { - size_t n = 0; - - for (; *new; new++) - if (isnew(old, *new)) - n += strlen(*new) + 1; - return (n); -} - -static int -newgid(int ngroups, gid_t *groups, gid_t group) { - ngroups--, groups++; - for (; ngroups-- > 0; groups++) - if (*groups == group) - return (0); - return (1); -} - -#endif /* WANT_IRS_GR */ -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/gen_ho.c /usr/src/lib/libc/net/gen_ho.c --- /home/reed/src/isc/libbind/libbind/irs/gen_ho.c 2006-03-09 17:57:56.000000000 -0600 +++ /usr/src/lib/libc/net/gen_ho.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,391 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1996,1999 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#if defined(LIBC_SCCS) && !defined(lint) -static const char rcsid[] = "$Id: gen_ho.c,v 1.5 2006/03/09 23:57:56 marka Exp $"; -#endif /* LIBC_SCCS and not lint */ - -/* Imports */ - -#include "port_before.h" - -#include - -#include -#include - -#include -#include -#include -#include -#include -#include - -#include -#include - -#include "port_after.h" - -#include "irs_p.h" -#include "gen_p.h" - -/* Definitions */ - -struct pvt { - struct irs_rule * rules; - struct irs_rule * rule; - struct irs_ho * ho; - struct __res_state * res; - void (*free_res)(void *); -}; - -/* Forwards */ - -static void ho_close(struct irs_ho *this); -static struct hostent * ho_byname(struct irs_ho *this, const char *name); -static struct hostent * ho_byname2(struct irs_ho *this, const char *name, - int af); -static struct hostent * ho_byaddr(struct irs_ho *this, const void *addr, - int len, int af); -static struct hostent * ho_next(struct irs_ho *this); -static void ho_rewind(struct irs_ho *this); -static void ho_minimize(struct irs_ho *this); -static struct __res_state * ho_res_get(struct irs_ho *this); -static void ho_res_set(struct irs_ho *this, - struct __res_state *res, - void (*free_res)(void *)); -static struct addrinfo * ho_addrinfo(struct irs_ho *this, const char *name, - const struct addrinfo *pai); - -static int init(struct irs_ho *this); - -/* Exports */ - -struct irs_ho * -irs_gen_ho(struct irs_acc *this) { - struct gen_p *accpvt = (struct gen_p *)this->private; - struct irs_ho *ho; - struct pvt *pvt; - - if (!(pvt = memget(sizeof *pvt))) { - errno = ENOMEM; - return (NULL); - } - memset(pvt, 0, sizeof *pvt); - if (!(ho = memget(sizeof *ho))) { - memput(pvt, sizeof *pvt); - errno = ENOMEM; - return (NULL); - } - memset(ho, 0x5e, sizeof *ho); - pvt->rules = accpvt->map_rules[irs_ho]; - pvt->rule = pvt->rules; - ho->private = pvt; - ho->close = ho_close; - ho->byname = ho_byname; - ho->byname2 = ho_byname2; - ho->byaddr = ho_byaddr; - ho->next = ho_next; - ho->rewind = ho_rewind; - ho->minimize = ho_minimize; - ho->res_get = ho_res_get; - ho->res_set = ho_res_set; - ho->addrinfo = ho_addrinfo; - return (ho); -} - -/* Methods. */ - -static void -ho_close(struct irs_ho *this) { - struct pvt *pvt = (struct pvt *)this->private; - - ho_minimize(this); - if (pvt->res && pvt->free_res) - (*pvt->free_res)(pvt->res); - memput(pvt, sizeof *pvt); - memput(this, sizeof *this); -} - -static struct hostent * -ho_byname(struct irs_ho *this, const char *name) { - struct pvt *pvt = (struct pvt *)this->private; - struct irs_rule *rule; - struct hostent *rval; - struct irs_ho *ho; - int therrno = NETDB_INTERNAL; - int softerror = 0; - - if (init(this) == -1) - return (NULL); - - for (rule = pvt->rules; rule; rule = rule->next) { - ho = rule->inst->ho; - RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL); - errno = 0; - rval = (*ho->byname)(ho, name); - if (rval != NULL) - return (rval); - if (softerror == 0 && - pvt->res->res_h_errno != HOST_NOT_FOUND && - pvt->res->res_h_errno != NETDB_INTERNAL) { - softerror = 1; - therrno = pvt->res->res_h_errno; - } - if (rule->flags & IRS_CONTINUE) - continue; - /* - * The value TRY_AGAIN can mean that the service - * is not available, or just that this particular name - * cannot be resolved now. We use the errno ECONNREFUSED - * to distinguish. If a lookup sets that errno when - * H_ERRNO is TRY_AGAIN, we continue to try other lookup - * functions, otherwise we return the TRY_AGAIN error. - */ - if (pvt->res->res_h_errno != TRY_AGAIN || errno != ECONNREFUSED) - break; - } - if (softerror != 0 && pvt->res->res_h_errno == HOST_NOT_FOUND) - RES_SET_H_ERRNO(pvt->res, therrno); - return (NULL); -} - -static struct hostent * -ho_byname2(struct irs_ho *this, const char *name, int af) { - struct pvt *pvt = (struct pvt *)this->private; - struct irs_rule *rule; - struct hostent *rval; - struct irs_ho *ho; - int therrno = NETDB_INTERNAL; - int softerror = 0; - - if (init(this) == -1) - return (NULL); - - for (rule = pvt->rules; rule; rule = rule->next) { - ho = rule->inst->ho; - RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL); - errno = 0; - rval = (*ho->byname2)(ho, name, af); - if (rval != NULL) - return (rval); - if (softerror == 0 && - pvt->res->res_h_errno != HOST_NOT_FOUND && - pvt->res->res_h_errno != NETDB_INTERNAL) { - softerror = 1; - therrno = pvt->res->res_h_errno; - } - if (rule->flags & IRS_CONTINUE) - continue; - /* - * See the comments in ho_byname() explaining - * the interpretation of TRY_AGAIN and ECONNREFUSED. - */ - if (pvt->res->res_h_errno != TRY_AGAIN || errno != ECONNREFUSED) - break; - } - if (softerror != 0 && pvt->res->res_h_errno == HOST_NOT_FOUND) - RES_SET_H_ERRNO(pvt->res, therrno); - return (NULL); -} - -static struct hostent * -ho_byaddr(struct irs_ho *this, const void *addr, int len, int af) { - struct pvt *pvt = (struct pvt *)this->private; - struct irs_rule *rule; - struct hostent *rval; - struct irs_ho *ho; - int therrno = NETDB_INTERNAL; - int softerror = 0; - - - if (init(this) == -1) - return (NULL); - - for (rule = pvt->rules; rule; rule = rule->next) { - ho = rule->inst->ho; - RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL); - errno = 0; - rval = (*ho->byaddr)(ho, addr, len, af); - if (rval != NULL) - return (rval); - if (softerror == 0 && - pvt->res->res_h_errno != HOST_NOT_FOUND && - pvt->res->res_h_errno != NETDB_INTERNAL) { - softerror = 1; - therrno = pvt->res->res_h_errno; - } - - if (rule->flags & IRS_CONTINUE) - continue; - /* - * See the comments in ho_byname() explaining - * the interpretation of TRY_AGAIN and ECONNREFUSED. - */ - if (pvt->res->res_h_errno != TRY_AGAIN || errno != ECONNREFUSED) - break; - } - if (softerror != 0 && pvt->res->res_h_errno == HOST_NOT_FOUND) - RES_SET_H_ERRNO(pvt->res, therrno); - return (NULL); -} - -static struct hostent * -ho_next(struct irs_ho *this) { - struct pvt *pvt = (struct pvt *)this->private; - struct hostent *rval; - struct irs_ho *ho; - - while (pvt->rule) { - ho = pvt->rule->inst->ho; - rval = (*ho->next)(ho); - if (rval) - return (rval); - if (!(pvt->rule->flags & IRS_CONTINUE)) - break; - pvt->rule = pvt->rule->next; - if (pvt->rule) { - ho = pvt->rule->inst->ho; - (*ho->rewind)(ho); - } - } - return (NULL); -} - -static void -ho_rewind(struct irs_ho *this) { - struct pvt *pvt = (struct pvt *)this->private; - struct irs_ho *ho; - - pvt->rule = pvt->rules; - if (pvt->rule) { - ho = pvt->rule->inst->ho; - (*ho->rewind)(ho); - } -} - -static void -ho_minimize(struct irs_ho *this) { - struct pvt *pvt = (struct pvt *)this->private; - struct irs_rule *rule; - - if (pvt->res) - res_nclose(pvt->res); - for (rule = pvt->rules; rule != NULL; rule = rule->next) { - struct irs_ho *ho = rule->inst->ho; - - (*ho->minimize)(ho); - } -} - -static struct __res_state * -ho_res_get(struct irs_ho *this) { - struct pvt *pvt = (struct pvt *)this->private; - - if (!pvt->res) { - struct __res_state *res; - res = (struct __res_state *)malloc(sizeof *res); - if (!res) { - errno = ENOMEM; - return (NULL); - } - memset(res, 0, sizeof *res); - ho_res_set(this, res, free); - } - - return (pvt->res); -} - -static void -ho_res_set(struct irs_ho *this, struct __res_state *res, - void (*free_res)(void *)) { - struct pvt *pvt = (struct pvt *)this->private; - struct irs_rule *rule; - - if (pvt->res && pvt->free_res) { - res_nclose(pvt->res); - (*pvt->free_res)(pvt->res); - } - - pvt->res = res; - pvt->free_res = free_res; - - for (rule = pvt->rules; rule != NULL; rule = rule->next) { - struct irs_ho *ho = rule->inst->ho; - - (*ho->res_set)(ho, pvt->res, NULL); - } -} - -static struct addrinfo * -ho_addrinfo(struct irs_ho *this, const char *name, const struct addrinfo *pai) -{ - struct pvt *pvt = (struct pvt *)this->private; - struct irs_rule *rule; - struct addrinfo *rval = NULL; - struct irs_ho *ho; - int therrno = NETDB_INTERNAL; - int softerror = 0; - - if (init(this) == -1) - return (NULL); - - for (rule = pvt->rules; rule; rule = rule->next) { - ho = rule->inst->ho; - RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL); - errno = 0; - if (ho->addrinfo == NULL) /*%< for safety */ - continue; - rval = (*ho->addrinfo)(ho, name, pai); - if (rval != NULL) - return (rval); - if (softerror == 0 && - pvt->res->res_h_errno != HOST_NOT_FOUND && - pvt->res->res_h_errno != NETDB_INTERNAL) { - softerror = 1; - therrno = pvt->res->res_h_errno; - } - if (rule->flags & IRS_CONTINUE) - continue; - /* - * See the comments in ho_byname() explaining - * the interpretation of TRY_AGAIN and ECONNREFUSED. - */ - if (pvt->res->res_h_errno != TRY_AGAIN || - errno != ECONNREFUSED) - break; - } - if (softerror != 0 && pvt->res->res_h_errno == HOST_NOT_FOUND) - RES_SET_H_ERRNO(pvt->res, therrno); - return (NULL); -} - -static int -init(struct irs_ho *this) { - struct pvt *pvt = (struct pvt *)this->private; - - if (!pvt->res && !ho_res_get(this)) - return (-1); - - if (((pvt->res->options & RES_INIT) == 0U) && - (res_ninit(pvt->res) == -1)) - return (-1); - - return (0); -} - -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/gen_ng.c /usr/src/lib/libc/net/gen_ng.c --- /home/reed/src/isc/libbind/libbind/irs/gen_ng.c 2005-04-26 23:56:23.000000000 -0500 +++ /usr/src/lib/libc/net/gen_ng.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,174 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1996,1999 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#if !defined(LINT) && !defined(CODECENTER) -static const char rcsid[] = "$Id: gen_ng.c,v 1.3 2005/04/27 04:56:23 sra Exp $"; -#endif - -/* Imports */ - -#include "port_before.h" - -#include - -#include -#include -#include - -#include -#include -#include - -#include -#include - -#include "port_after.h" - -#include "irs_p.h" -#include "gen_p.h" - -/* Types */ - -struct pvt { - struct irs_rule * rules; - struct irs_rule * rule; - char * curgroup; -}; - -/* Forward */ - -static void ng_close(struct irs_ng *); -static int ng_next(struct irs_ng *, const char **, - const char **, const char **); -static int ng_test(struct irs_ng *, const char *, - const char *, const char *, - const char *); -static void ng_rewind(struct irs_ng *, const char *); -static void ng_minimize(struct irs_ng *); - -/* Public */ - -struct irs_ng * -irs_gen_ng(struct irs_acc *this) { - struct gen_p *accpvt = (struct gen_p *)this->private; - struct irs_ng *ng; - struct pvt *pvt; - - if (!(ng = memget(sizeof *ng))) { - errno = ENOMEM; - return (NULL); - } - memset(ng, 0x5e, sizeof *ng); - if (!(pvt = memget(sizeof *pvt))) { - memput(ng, sizeof *ng); - errno = ENOMEM; - return (NULL); - } - memset(pvt, 0, sizeof *pvt); - pvt->rules = accpvt->map_rules[irs_ng]; - pvt->rule = pvt->rules; - ng->private = pvt; - ng->close = ng_close; - ng->next = ng_next; - ng->test = ng_test; - ng->rewind = ng_rewind; - ng->minimize = ng_minimize; - return (ng); -} - -/* Methods */ - -static void -ng_close(struct irs_ng *this) { - struct pvt *pvt = (struct pvt *)this->private; - - ng_minimize(this); - if (pvt->curgroup) - free(pvt->curgroup); - memput(pvt, sizeof *pvt); - memput(this, sizeof *this); -} - -static int -ng_next(struct irs_ng *this, const char **host, const char **user, - const char **domain) -{ - struct pvt *pvt = (struct pvt *)this->private; - struct irs_ng *ng; - - while (pvt->rule) { - ng = pvt->rule->inst->ng; - if ((*ng->next)(ng, host, user, domain) == 1) - return (1); - if (!(pvt->rule->flags & IRS_CONTINUE)) - break; - pvt->rule = pvt->rule->next; - if (pvt->rule) { - ng = pvt->rule->inst->ng; - (*ng->rewind)(ng, pvt->curgroup); - } - } - return (0); -} - -static int -ng_test(struct irs_ng *this, const char *name, - const char *user, const char *host, const char *domain) -{ - struct pvt *pvt = (struct pvt *)this->private; - struct irs_rule *rule; - struct irs_ng *ng; - int rval; - - rval = 0; - for (rule = pvt->rules; rule; rule = rule->next) { - ng = rule->inst->ng; - rval = (*ng->test)(ng, name, user, host, domain); - if (rval || !(rule->flags & IRS_CONTINUE)) - break; - } - return (rval); -} - -static void -ng_rewind(struct irs_ng *this, const char *group) { - struct pvt *pvt = (struct pvt *)this->private; - struct irs_ng *ng; - - pvt->rule = pvt->rules; - if (pvt->rule) { - if (pvt->curgroup) - free(pvt->curgroup); - pvt->curgroup = strdup(group); - ng = pvt->rule->inst->ng; - (*ng->rewind)(ng, pvt->curgroup); - } -} - -static void -ng_minimize(struct irs_ng *this) { - struct pvt *pvt = (struct pvt *)this->private; - struct irs_rule *rule; - - for (rule = pvt->rules; rule != NULL; rule = rule->next) { - struct irs_ng *ng = rule->inst->ng; - - (*ng->minimize)(ng); - } -} - -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/gen_nw.c /usr/src/lib/libc/net/gen_nw.c --- /home/reed/src/isc/libbind/libbind/irs/gen_nw.c 2005-04-26 23:56:23.000000000 -0500 +++ /usr/src/lib/libc/net/gen_nw.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,264 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1996,1999 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#if !defined(LINT) && !defined(CODECENTER) -static const char rcsid[] = "$Id: gen_nw.c,v 1.4 2005/04/27 04:56:23 sra Exp $"; -#endif - -/* Imports */ - -#include "port_before.h" - -#include - -#include -#include - -#include -#include -#include -#include - -#include -#include - -#include "port_after.h" - -#include "irs_p.h" -#include "gen_p.h" - -/* Types */ - -struct pvt { - struct irs_rule * rules; - struct irs_rule * rule; - struct __res_state * res; - void (*free_res)(void *); -}; - -/* Forward */ - -static void nw_close(struct irs_nw*); -static struct nwent * nw_next(struct irs_nw *); -static struct nwent * nw_byname(struct irs_nw *, const char *, int); -static struct nwent * nw_byaddr(struct irs_nw *, void *, int, int); -static void nw_rewind(struct irs_nw *); -static void nw_minimize(struct irs_nw *); -static struct __res_state * nw_res_get(struct irs_nw *this); -static void nw_res_set(struct irs_nw *this, - struct __res_state *res, - void (*free_res)(void *)); - -static int init(struct irs_nw *this); - -/* Public */ - -struct irs_nw * -irs_gen_nw(struct irs_acc *this) { - struct gen_p *accpvt = (struct gen_p *)this->private; - struct irs_nw *nw; - struct pvt *pvt; - - if (!(pvt = memget(sizeof *pvt))) { - errno = ENOMEM; - return (NULL); - } - memset(pvt, 0, sizeof *pvt); - if (!(nw = memget(sizeof *nw))) { - memput(pvt, sizeof *pvt); - errno = ENOMEM; - return (NULL); - } - memset(nw, 0x5e, sizeof *nw); - pvt->rules = accpvt->map_rules[irs_nw]; - pvt->rule = pvt->rules; - nw->private = pvt; - nw->close = nw_close; - nw->next = nw_next; - nw->byname = nw_byname; - nw->byaddr = nw_byaddr; - nw->rewind = nw_rewind; - nw->minimize = nw_minimize; - nw->res_get = nw_res_get; - nw->res_set = nw_res_set; - return (nw); -} - -/* Methods */ - -static void -nw_close(struct irs_nw *this) { - struct pvt *pvt = (struct pvt *)this->private; - - nw_minimize(this); - - if (pvt->res && pvt->free_res) - (*pvt->free_res)(pvt->res); - - memput(pvt, sizeof *pvt); - memput(this, sizeof *this); -} - -static struct nwent * -nw_next(struct irs_nw *this) { - struct pvt *pvt = (struct pvt *)this->private; - struct nwent *rval; - struct irs_nw *nw; - - if (init(this) == -1) - return(NULL); - - while (pvt->rule) { - nw = pvt->rule->inst->nw; - rval = (*nw->next)(nw); - if (rval) - return (rval); - if (!(pvt->rules->flags & IRS_CONTINUE)) - break; - pvt->rule = pvt->rule->next; - if (pvt->rule) { - nw = pvt->rule->inst->nw; - (*nw->rewind)(nw); - } - } - return (NULL); -} - -static struct nwent * -nw_byname(struct irs_nw *this, const char *name, int type) { - struct pvt *pvt = (struct pvt *)this->private; - struct irs_rule *rule; - struct nwent *rval; - struct irs_nw *nw; - - if (init(this) == -1) - return(NULL); - - for (rule = pvt->rules; rule; rule = rule->next) { - nw = rule->inst->nw; - RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL); - rval = (*nw->byname)(nw, name, type); - if (rval != NULL) - return (rval); - if (pvt->res->res_h_errno != TRY_AGAIN && - !(rule->flags & IRS_CONTINUE)) - break; - } - return (NULL); -} - -static struct nwent * -nw_byaddr(struct irs_nw *this, void *net, int length, int type) { - struct pvt *pvt = (struct pvt *)this->private; - struct irs_rule *rule; - struct nwent *rval; - struct irs_nw *nw; - - if (init(this) == -1) - return(NULL); - - for (rule = pvt->rules; rule; rule = rule->next) { - nw = rule->inst->nw; - RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL); - rval = (*nw->byaddr)(nw, net, length, type); - if (rval != NULL) - return (rval); - if (pvt->res->res_h_errno != TRY_AGAIN && - !(rule->flags & IRS_CONTINUE)) - break; - } - return (NULL); -} - -static void -nw_rewind(struct irs_nw *this) { - struct pvt *pvt = (struct pvt *)this->private; - struct irs_nw *nw; - - pvt->rule = pvt->rules; - if (pvt->rule) { - nw = pvt->rule->inst->nw; - (*nw->rewind)(nw); - } -} - -static void -nw_minimize(struct irs_nw *this) { - struct pvt *pvt = (struct pvt *)this->private; - struct irs_rule *rule; - - if (pvt->res) - res_nclose(pvt->res); - for (rule = pvt->rules; rule != NULL; rule = rule->next) { - struct irs_nw *nw = rule->inst->nw; - - (*nw->minimize)(nw); - } -} - -static struct __res_state * -nw_res_get(struct irs_nw *this) { - struct pvt *pvt = (struct pvt *)this->private; - - if (!pvt->res) { - struct __res_state *res; - res = (struct __res_state *)malloc(sizeof *res); - if (!res) { - errno = ENOMEM; - return (NULL); - } - memset(res, 0, sizeof *res); - nw_res_set(this, res, free); - } - - return (pvt->res); -} - -static void -nw_res_set(struct irs_nw *this, struct __res_state *res, - void (*free_res)(void *)) { - struct pvt *pvt = (struct pvt *)this->private; - struct irs_rule *rule; - - if (pvt->res && pvt->free_res) { - res_nclose(pvt->res); - (*pvt->free_res)(pvt->res); - } - - pvt->res = res; - pvt->free_res = free_res; - - for (rule = pvt->rules; rule != NULL; rule = rule->next) { - struct irs_nw *nw = rule->inst->nw; - - (*nw->res_set)(nw, pvt->res, NULL); - } -} - -static int -init(struct irs_nw *this) { - struct pvt *pvt = (struct pvt *)this->private; - - if (!pvt->res && !nw_res_get(this)) - return (-1); - if (((pvt->res->options & RES_INIT) == 0U) && - res_ninit(pvt->res) == -1) - return (-1); - return (0); -} - -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/gen_p.h /usr/src/lib/libc/net/gen_p.h --- /home/reed/src/isc/libbind/libbind/irs/gen_p.h 2005-04-26 23:56:23.000000000 -0500 +++ /usr/src/lib/libc/net/gen_p.h 1969-12-31 18:00:00.000000000 -0600 @@ -1,113 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1996,1999 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -/* - * $Id: gen_p.h,v 1.3 2005/04/27 04:56:23 sra Exp $ - */ - -/*! \file - * Notes: - * We hope to create a complete set of thread-safe entry points someday, - * which will mean a set of getXbyY() functions that take as an argument - * a pointer to the map class, which will have a pointer to the private - * data, which will be used preferentially to the static variables that - * are necessary to support the "classic" interface. This "classic" - * interface will then be reimplemented as stubs on top of the thread - * safe modules, and will keep the map class pointers as their only - * static data. HOWEVER, we are not there yet. So while we will call - * the just-barely-converted map class methods with map class pointers, - * right now they probably all still use statics. We're not fooling - * anybody, and we're not trying to (yet). - */ - -#ifndef _GEN_P_H_INCLUDED -#define _GEN_P_H_INCLUDED - -/*% - * These are the access methods. - */ -enum irs_acc_id { - irs_lcl, /*%< Local. */ - irs_dns, /*%< DNS or Hesiod. */ - irs_nis, /*%< Sun NIS ("YP"). */ - irs_irp, /*%< IR protocol. */ - irs_nacc -}; - -/*% - * These are the map types. - */ -enum irs_map_id { - irs_gr, /*%< "group" */ - irs_pw, /*%< "passwd" */ - irs_sv, /*%< "services" */ - irs_pr, /*%< "protocols" */ - irs_ho, /*%< "hosts" */ - irs_nw, /*%< "networks" */ - irs_ng, /*%< "netgroup" */ - irs_nmap -}; - -/*% - * This is an accessor instance. - */ -struct irs_inst { - struct irs_acc *acc; - struct irs_gr * gr; - struct irs_pw * pw; - struct irs_sv * sv; - struct irs_pr * pr; - struct irs_ho * ho; - struct irs_nw * nw; - struct irs_ng * ng; -}; - -/*% - * This is a search rule for some map type. - */ -struct irs_rule { - struct irs_rule * next; - struct irs_inst * inst; - int flags; -}; -#define IRS_MERGE 0x0001 /*%< Don't stop if acc. has data? */ -#define IRS_CONTINUE 0x0002 /*%< Don't stop if acc. has no data? */ -/* - * This is the private data for a search access class. - */ -struct gen_p { - char * options; - struct irs_rule * map_rules[(int)irs_nmap]; - struct irs_inst accessors[(int)irs_nacc]; - struct __res_state * res; - void (*free_res) __P((void *)); -}; - -/* - * Externs. - */ - -extern struct irs_acc * irs_gen_acc __P((const char *, const char *conf_file)); -extern struct irs_gr * irs_gen_gr __P((struct irs_acc *)); -extern struct irs_pw * irs_gen_pw __P((struct irs_acc *)); -extern struct irs_sv * irs_gen_sv __P((struct irs_acc *)); -extern struct irs_pr * irs_gen_pr __P((struct irs_acc *)); -extern struct irs_ho * irs_gen_ho __P((struct irs_acc *)); -extern struct irs_nw * irs_gen_nw __P((struct irs_acc *)); -extern struct irs_ng * irs_gen_ng __P((struct irs_acc *)); - -#endif /*_IRS_P_H_INCLUDED*/ diff -pruN /home/reed/src/isc/libbind/libbind/irs/gen_pr.c /usr/src/lib/libc/net/gen_pr.c --- /home/reed/src/isc/libbind/libbind/irs/gen_pr.c 2005-04-26 23:56:24.000000000 -0500 +++ /usr/src/lib/libc/net/gen_pr.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,228 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1996,1999 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#if !defined(LINT) && !defined(CODECENTER) -static const char rcsid[] = "$Id: gen_pr.c,v 1.3 2005/04/27 04:56:24 sra Exp $"; -#endif - -/* Imports */ - -#include "port_before.h" - -#include -#include -#include - -#include -#include -#include -#include - -#include -#include - -#include "port_after.h" - -#include "irs_p.h" -#include "gen_p.h" - -/* Types */ - -struct pvt { - struct irs_rule * rules; - struct irs_rule * rule; - struct __res_state * res; - void (*free_res)(void *); -}; - -/* Forward */ - -static void pr_close(struct irs_pr*); -static struct protoent * pr_next(struct irs_pr *); -static struct protoent * pr_byname(struct irs_pr *, const char *); -static struct protoent * pr_bynumber(struct irs_pr *, int); -static void pr_rewind(struct irs_pr *); -static void pr_minimize(struct irs_pr *); -static struct __res_state * pr_res_get(struct irs_pr *); -static void pr_res_set(struct irs_pr *, - struct __res_state *, - void (*)(void *)); - -/* Public */ - -struct irs_pr * -irs_gen_pr(struct irs_acc *this) { - struct gen_p *accpvt = (struct gen_p *)this->private; - struct irs_pr *pr; - struct pvt *pvt; - - if (!(pr = memget(sizeof *pr))) { - errno = ENOMEM; - return (NULL); - } - memset(pr, 0x5e, sizeof *pr); - if (!(pvt = memget(sizeof *pvt))) { - memput(pr, sizeof *pr); - errno = ENOMEM; - return (NULL); - } - memset(pvt, 0, sizeof *pvt); - pvt->rules = accpvt->map_rules[irs_pr]; - pvt->rule = pvt->rules; - pr->private = pvt; - pr->close = pr_close; - pr->next = pr_next; - pr->byname = pr_byname; - pr->bynumber = pr_bynumber; - pr->rewind = pr_rewind; - pr->minimize = pr_minimize; - pr->res_get = pr_res_get; - pr->res_set = pr_res_set; - return (pr); -} - -/* Methods */ - -static void -pr_close(struct irs_pr *this) { - struct pvt *pvt = (struct pvt *)this->private; - - memput(pvt, sizeof *pvt); - memput(this, sizeof *this); -} - -static struct protoent * -pr_next(struct irs_pr *this) { - struct pvt *pvt = (struct pvt *)this->private; - struct protoent *rval; - struct irs_pr *pr; - - while (pvt->rule) { - pr = pvt->rule->inst->pr; - rval = (*pr->next)(pr); - if (rval) - return (rval); - if (!(pvt->rules->flags & IRS_CONTINUE)) - break; - pvt->rule = pvt->rule->next; - if (pvt->rule) { - pr = pvt->rule->inst->pr; - (*pr->rewind)(pr); - } - } - return (NULL); -} - -static struct protoent * -pr_byname(struct irs_pr *this, const char *name) { - struct pvt *pvt = (struct pvt *)this->private; - struct irs_rule *rule; - struct protoent *rval; - struct irs_pr *pr; - - rval = NULL; - for (rule = pvt->rules; rule; rule = rule->next) { - pr = rule->inst->pr; - rval = (*pr->byname)(pr, name); - if (rval || !(rule->flags & IRS_CONTINUE)) - break; - } - return (rval); -} - -static struct protoent * -pr_bynumber(struct irs_pr *this, int proto) { - struct pvt *pvt = (struct pvt *)this->private; - struct irs_rule *rule; - struct protoent *rval; - struct irs_pr *pr; - - rval = NULL; - for (rule = pvt->rules; rule; rule = rule->next) { - pr = rule->inst->pr; - rval = (*pr->bynumber)(pr, proto); - if (rval || !(rule->flags & IRS_CONTINUE)) - break; - } - return (rval); -} - -static void -pr_rewind(struct irs_pr *this) { - struct pvt *pvt = (struct pvt *)this->private; - struct irs_pr *pr; - - pvt->rule = pvt->rules; - if (pvt->rule) { - pr = pvt->rule->inst->pr; - (*pr->rewind)(pr); - } -} - -static void -pr_minimize(struct irs_pr *this) { - struct pvt *pvt = (struct pvt *)this->private; - struct irs_rule *rule; - - for (rule = pvt->rules; rule != NULL; rule = rule->next) { - struct irs_pr *pr = rule->inst->pr; - - (*pr->minimize)(pr); - } -} - -static struct __res_state * -pr_res_get(struct irs_pr *this) { - struct pvt *pvt = (struct pvt *)this->private; - - if (!pvt->res) { - struct __res_state *res; - res = (struct __res_state *)malloc(sizeof *res); - if (!res) { - errno = ENOMEM; - return (NULL); - } - memset(res, 0, sizeof *res); - pr_res_set(this, res, free); - } - - return (pvt->res); -} - -static void -pr_res_set(struct irs_pr *this, struct __res_state *res, - void (*free_res)(void *)) { - struct pvt *pvt = (struct pvt *)this->private; - struct irs_rule *rule; - - if (pvt->res && pvt->free_res) { - res_nclose(pvt->res); - (*pvt->free_res)(pvt->res); - } - - pvt->res = res; - pvt->free_res = free_res; - - for (rule = pvt->rules; rule != NULL; rule = rule->next) { - struct irs_pr *pr = rule->inst->pr; - - if (pr->res_set) - (*pr->res_set)(pr, pvt->res, NULL); - } -} - -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/gen_pw.c /usr/src/lib/libc/net/gen_pw.c --- /home/reed/src/isc/libbind/libbind/irs/gen_pw.c 2005-04-26 23:56:24.000000000 -0500 +++ /usr/src/lib/libc/net/gen_pw.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,234 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1996,1999 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#if !defined(LINT) && !defined(CODECENTER) -static const char rcsid[] = "$Id: gen_pw.c,v 1.3 2005/04/27 04:56:24 sra Exp $"; -#endif - -/* Imports */ - -#include "port_before.h" - -#ifndef WANT_IRS_PW -static int __bind_irs_pw_unneeded; -#else - -#include -#include -#include -#include - -#include -#include -#include -#include - -#include -#include - -#include "port_after.h" - -#include "irs_p.h" -#include "gen_p.h" - -/* Types */ - -struct pvt { - struct irs_rule * rules; - struct irs_rule * rule; - struct __res_state * res; - void (*free_res)(void *); -}; - -/* Forward */ - -static void pw_close(struct irs_pw *); -static struct passwd * pw_next(struct irs_pw *); -static struct passwd * pw_byname(struct irs_pw *, const char *); -static struct passwd * pw_byuid(struct irs_pw *, uid_t); -static void pw_rewind(struct irs_pw *); -static void pw_minimize(struct irs_pw *); -static struct __res_state * pw_res_get(struct irs_pw *); -static void pw_res_set(struct irs_pw *, - struct __res_state *, - void (*)(void *)); - -/* Public */ - -struct irs_pw * -irs_gen_pw(struct irs_acc *this) { - struct gen_p *accpvt = (struct gen_p *)this->private; - struct irs_pw *pw; - struct pvt *pvt; - - if (!(pw = memget(sizeof *pw))) { - errno = ENOMEM; - return (NULL); - } - memset(pw, 0x5e, sizeof *pw); - if (!(pvt = memget(sizeof *pvt))) { - memput(pw, sizeof *pvt); - errno = ENOMEM; - return (NULL); - } - memset(pvt, 0, sizeof *pvt); - pvt->rules = accpvt->map_rules[irs_pw]; - pvt->rule = pvt->rules; - pw->private = pvt; - pw->close = pw_close; - pw->next = pw_next; - pw->byname = pw_byname; - pw->byuid = pw_byuid; - pw->rewind = pw_rewind; - pw->minimize = pw_minimize; - pw->res_get = pw_res_get; - pw->res_set = pw_res_set; - return (pw); -} - -/* Methods */ - -static void -pw_close(struct irs_pw *this) { - struct pvt *pvt = (struct pvt *)this->private; - - memput(pvt, sizeof *pvt); - memput(this, sizeof *this); -} - -static struct passwd * -pw_next(struct irs_pw *this) { - struct pvt *pvt = (struct pvt *)this->private; - struct passwd *rval; - struct irs_pw *pw; - - while (pvt->rule) { - pw = pvt->rule->inst->pw; - rval = (*pw->next)(pw); - if (rval) - return (rval); - if (!(pvt->rule->flags & IRS_CONTINUE)) - break; - pvt->rule = pvt->rule->next; - if (pvt->rule) { - pw = pvt->rule->inst->pw; - (*pw->rewind)(pw); - } - } - return (NULL); -} - -static void -pw_rewind(struct irs_pw *this) { - struct pvt *pvt = (struct pvt *)this->private; - struct irs_pw *pw; - - pvt->rule = pvt->rules; - if (pvt->rule) { - pw = pvt->rule->inst->pw; - (*pw->rewind)(pw); - } -} - -static struct passwd * -pw_byname(struct irs_pw *this, const char *name) { - struct pvt *pvt = (struct pvt *)this->private; - struct irs_rule *rule; - struct passwd *rval; - struct irs_pw *pw; - - rval = NULL; - for (rule = pvt->rules; rule; rule = rule->next) { - pw = rule->inst->pw; - rval = (*pw->byname)(pw, name); - if (rval || !(rule->flags & IRS_CONTINUE)) - break; - } - return (rval); -} - -static struct passwd * -pw_byuid(struct irs_pw *this, uid_t uid) { - struct pvt *pvt = (struct pvt *)this->private; - struct irs_rule *rule; - struct passwd *rval; - struct irs_pw *pw; - - rval = NULL; - for (rule = pvt->rules; rule; rule = rule->next) { - pw = rule->inst->pw; - rval = (*pw->byuid)(pw, uid); - if (rval || !(rule->flags & IRS_CONTINUE)) - break; - } - return (rval); -} - -static void -pw_minimize(struct irs_pw *this) { - struct pvt *pvt = (struct pvt *)this->private; - struct irs_rule *rule; - - for (rule = pvt->rules; rule != NULL; rule = rule->next) { - struct irs_pw *pw = rule->inst->pw; - - (*pw->minimize)(pw); - } -} - -static struct __res_state * -pw_res_get(struct irs_pw *this) { - struct pvt *pvt = (struct pvt *)this->private; - - if (!pvt->res) { - struct __res_state *res; - res = (struct __res_state *)malloc(sizeof *res); - if (!res) { - errno = ENOMEM; - return (NULL); - } - memset(res, 0, sizeof *res); - pw_res_set(this, res, free); - } - - return (pvt->res); -} - -static void -pw_res_set(struct irs_pw *this, struct __res_state *res, - void (*free_res)(void *)) { - struct pvt *pvt = (struct pvt *)this->private; - struct irs_rule *rule; - - if (pvt->res && pvt->free_res) { - res_nclose(pvt->res); - (*pvt->free_res)(pvt->res); - } - - pvt->res = res; - pvt->free_res = free_res; - - for (rule = pvt->rules; rule != NULL; rule = rule->next) { - struct irs_pw *pw = rule->inst->pw; - - if (pw->res_set) - (*pw->res_set)(pw, pvt->res, NULL); - } -} - -#endif /* WANT_IRS_PW */ -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/gen_sv.c /usr/src/lib/libc/net/gen_sv.c --- /home/reed/src/isc/libbind/libbind/irs/gen_sv.c 2005-04-26 23:56:24.000000000 -0500 +++ /usr/src/lib/libc/net/gen_sv.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,229 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1996,1999 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#if !defined(LINT) && !defined(CODECENTER) -static const char rcsid[] = "$Id: gen_sv.c,v 1.3 2005/04/27 04:56:24 sra Exp $"; -#endif - -/* Imports */ - -#include "port_before.h" - -#include -#include -#include -#include - -#include -#include -#include - -#include -#include - -#include "port_after.h" - -#include "irs_p.h" -#include "gen_p.h" - -/* Types */ - -struct pvt { - struct irs_rule * rules; - struct irs_rule * rule; - struct __res_state * res; - void (*free_res)(void *); -}; - -/* Forward */ - -static void sv_close(struct irs_sv*); -static struct servent * sv_next(struct irs_sv *); -static struct servent * sv_byname(struct irs_sv *, const char *, - const char *); -static struct servent * sv_byport(struct irs_sv *, int, const char *); -static void sv_rewind(struct irs_sv *); -static void sv_minimize(struct irs_sv *); -static struct __res_state * sv_res_get(struct irs_sv *); -static void sv_res_set(struct irs_sv *, - struct __res_state *, - void (*)(void *)); - -/* Public */ - -struct irs_sv * -irs_gen_sv(struct irs_acc *this) { - struct gen_p *accpvt = (struct gen_p *)this->private; - struct irs_sv *sv; - struct pvt *pvt; - - if (!(sv = memget(sizeof *sv))) { - errno = ENOMEM; - return (NULL); - } - memset(sv, 0x5e, sizeof *sv); - if (!(pvt = memget(sizeof *pvt))) { - memput(sv, sizeof *sv); - errno = ENOMEM; - return (NULL); - } - memset(pvt, 0, sizeof *pvt); - pvt->rules = accpvt->map_rules[irs_sv]; - pvt->rule = pvt->rules; - sv->private = pvt; - sv->close = sv_close; - sv->next = sv_next; - sv->byname = sv_byname; - sv->byport = sv_byport; - sv->rewind = sv_rewind; - sv->minimize = sv_minimize; - sv->res_get = sv_res_get; - sv->res_set = sv_res_set; - return (sv); -} - -/* Methods */ - -static void -sv_close(struct irs_sv *this) { - struct pvt *pvt = (struct pvt *)this->private; - - memput(pvt, sizeof *pvt); - memput(this, sizeof *this); -} - -static struct servent * -sv_next(struct irs_sv *this) { - struct pvt *pvt = (struct pvt *)this->private; - struct servent *rval; - struct irs_sv *sv; - - while (pvt->rule) { - sv = pvt->rule->inst->sv; - rval = (*sv->next)(sv); - if (rval) - return (rval); - if (!(pvt->rule->flags & IRS_CONTINUE)) - break; - pvt->rule = pvt->rule->next; - if (pvt->rule) { - sv = pvt->rule->inst->sv; - (*sv->rewind)(sv); - } - } - return (NULL); -} - -static struct servent * -sv_byname(struct irs_sv *this, const char *name, const char *proto) { - struct pvt *pvt = (struct pvt *)this->private; - struct irs_rule *rule; - struct servent *rval; - struct irs_sv *sv; - - rval = NULL; - for (rule = pvt->rules; rule; rule = rule->next) { - sv = rule->inst->sv; - rval = (*sv->byname)(sv, name, proto); - if (rval || !(rule->flags & IRS_CONTINUE)) - break; - } - return (rval); -} - -static struct servent * -sv_byport(struct irs_sv *this, int port, const char *proto) { - struct pvt *pvt = (struct pvt *)this->private; - struct irs_rule *rule; - struct servent *rval; - struct irs_sv *sv; - - rval = NULL; - for (rule = pvt->rules; rule; rule = rule->next) { - sv = rule->inst->sv; - rval = (*sv->byport)(sv, port, proto); - if (rval || !(rule->flags & IRS_CONTINUE)) - break; - } - return (rval); -} - -static void -sv_rewind(struct irs_sv *this) { - struct pvt *pvt = (struct pvt *)this->private; - struct irs_sv *sv; - - pvt->rule = pvt->rules; - if (pvt->rule) { - sv = pvt->rule->inst->sv; - (*sv->rewind)(sv); - } -} - -static void -sv_minimize(struct irs_sv *this) { - struct pvt *pvt = (struct pvt *)this->private; - struct irs_rule *rule; - - for (rule = pvt->rules; rule != NULL; rule = rule->next) { - struct irs_sv *sv = rule->inst->sv; - - (*sv->minimize)(sv); - } -} - -static struct __res_state * -sv_res_get(struct irs_sv *this) { - struct pvt *pvt = (struct pvt *)this->private; - - if (!pvt->res) { - struct __res_state *res; - res = (struct __res_state *)malloc(sizeof *res); - if (!res) { - errno = ENOMEM; - return (NULL); - } - memset(res, 0, sizeof *res); - sv_res_set(this, res, free); - } - - return (pvt->res); -} - -static void -sv_res_set(struct irs_sv *this, struct __res_state *res, - void (*free_res)(void *)) { - struct pvt *pvt = (struct pvt *)this->private; - struct irs_rule *rule; - - if (pvt->res && pvt->free_res) { - res_nclose(pvt->res); - (*pvt->free_res)(pvt->res); - } - - pvt->res = res; - pvt->free_res = free_res; - - for (rule = pvt->rules; rule != NULL; rule = rule->next) { - struct irs_sv *sv = rule->inst->sv; - - if (sv->res_set) - (*sv->res_set)(sv, pvt->res, NULL); - } -} - -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/getaddrinfo.c /usr/src/lib/libc/net/getaddrinfo.c --- /home/reed/src/isc/libbind/libbind/irs/getaddrinfo.c 2006-11-12 23:00:56.000000000 -0600 +++ /usr/src/lib/libc/net/getaddrinfo.c 2013-06-05 09:26:12.000000000 -0500 @@ -1,4 +1,5 @@ -/* $KAME: getaddrinfo.c,v 1.14 2001/01/06 09:41:15 jinmei Exp $ */ +/* $NetBSD: getaddrinfo.c,v 1.105 2013/05/13 17:54:55 christos Exp $ */ +/* $KAME: getaddrinfo.c,v 1.29 2000/08/31 17:26:57 itojun Exp $ */ /* * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. @@ -29,87 +30,71 @@ * SUCH DAMAGE. */ -/*! \file +/* * Issues to be discussed: - *\li Thread safe-ness must be checked. - *\li Return values. There are nonstandard return values defined and used + * - Return values. There are nonstandard return values defined and used * in the source code. This is because RFC2553 is silent about which error * code must be returned for which situation. - *\li IPv4 classful (shortened) form. RFC2553 is silent about it. XNET 5.2 - * says to use inet_aton() to convert IPv4 numeric to binary (allows + * - IPv4 classful (shortened) form. RFC2553 is silent about it. XNET 5.2 + * says to use inet_aton() to convert IPv4 numeric to binary (alows * classful form as a result). * current code - disallow classful form for IPv4 (due to use of inet_pton). - *\li freeaddrinfo(NULL). RFC2553 is silent about it. XNET 5.2 says it is + * - freeaddrinfo(NULL). RFC2553 is silent about it. XNET 5.2 says it is * invalid. * current code - SEGV on freeaddrinfo(NULL) * Note: - *\li We use getipnodebyname() just for thread-safeness. There's no intent - * to let it do PF_UNSPEC (actually we never pass PF_UNSPEC to - * getipnodebyname(). - *\li The code filters out AFs that are not supported by the kernel, + * - The code filters out AFs that are not supported by the kernel, * when globbing NULL hostname (to loopback, or wildcard). Is it the right * thing to do? What is the relationship with post-RFC2553 AI_ADDRCONFIG * in ai_flags? - *\li (post-2553) semantics of AI_ADDRCONFIG itself is too vague. + * - (post-2553) semantics of AI_ADDRCONFIG itself is too vague. * (1) what should we do against numeric hostname (2) what should we do * against NULL hostname (3) what is AI_ADDRCONFIG itself. AF not ready? * non-loopback address configured? global address configured? - * \par Additional Issue: - * To avoid search order issue, we have a big amount of code duplicate - * from gethnamaddr.c and some other places. The issues that there's no - * lower layer function to lookup "IPv4 or IPv6" record. Calling - * gethostbyname2 from getaddrinfo will end up in wrong search order, as - * follows: - * \li The code makes use of following calls when asked to resolver with - * ai_family = PF_UNSPEC: - *\code getipnodebyname(host, AF_INET6); - * getipnodebyname(host, AF_INET); - *\endcode - * \li This will result in the following queries if the node is configure to - * prefer /etc/hosts than DNS: - *\code - * lookup /etc/hosts for IPv6 address - * lookup DNS for IPv6 address - * lookup /etc/hosts for IPv4 address - * lookup DNS for IPv4 address - *\endcode - * which may not meet people's requirement. - * \li The right thing to happen is to have underlying layer which does - * PF_UNSPEC lookup (lookup both) and return chain of addrinfos. - * This would result in a bit of code duplicate with _dns_ghbyname() and - * friends. */ -#include "port_before.h" +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: getaddrinfo.c,v 1.105 2013/05/13 17:54:55 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ +#include "namespace.h" #include #include #include - #include #include - #include #include - +#include +#include +#include #include #include -#include -#include #include -#include -#include #include -#include +#include +#include +#include +#include +#include #include +#include -#include -#include +#ifdef YP +#include +#include +#include +#endif -#include "port_after.h" +#include "servent.h" -#include "irs_data.h" +#ifdef __weak_alias +__weak_alias(getaddrinfo,_getaddrinfo) +__weak_alias(freeaddrinfo,_freeaddrinfo) +__weak_alias(gai_strerror,_gai_strerror) +#endif #define SUCCESS 0 #define ANY 0 @@ -117,13 +102,15 @@ #define NO 0 static const char in_addrany[] = { 0, 0, 0, 0 }; +static const char in_loopback[] = { 127, 0, 0, 1 }; +#ifdef INET6 static const char in6_addrany[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; -static const char in_loopback[] = { 127, 0, 0, 1 }; static const char in6_loopback[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }; +#endif static const struct afd { int a_af; @@ -134,10 +121,12 @@ static const struct afd { const char *a_loopback; int a_scoped; } afdl [] = { +#ifdef INET6 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6), offsetof(struct sockaddr_in6, sin6_addr), in6_addrany, in6_loopback, 1}, +#endif {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in), offsetof(struct sockaddr_in, sin_addr), @@ -160,135 +149,179 @@ static const struct explore explore[] = #if 0 { PF_LOCAL, 0, ANY, ANY, NULL, 0x01 }, #endif +#ifdef INET6 { PF_INET6, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 }, { PF_INET6, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 }, { PF_INET6, SOCK_RAW, ANY, NULL, 0x05 }, +#endif { PF_INET, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 }, { PF_INET, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 }, { PF_INET, SOCK_RAW, ANY, NULL, 0x05 }, + { PF_UNSPEC, SOCK_DGRAM, IPPROTO_UDP, "udp", 0x07 }, + { PF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, "tcp", 0x07 }, + { PF_UNSPEC, SOCK_RAW, ANY, NULL, 0x05 }, { -1, 0, 0, NULL, 0 }, }; +#ifdef INET6 #define PTON_MAX 16 +#else +#define PTON_MAX 4 +#endif + +static const ns_src default_dns_files[] = { + { NSSRC_FILES, NS_SUCCESS }, + { NSSRC_DNS, NS_SUCCESS }, + { 0, 0 } +}; -static int str_isnumber __P((const char *)); -static int explore_fqdn __P((const struct addrinfo *, const char *, - const char *, struct addrinfo **)); -static int explore_copy __P((const struct addrinfo *, const struct addrinfo *, - struct addrinfo **)); -static int explore_null __P((const struct addrinfo *, - const char *, struct addrinfo **)); -static int explore_numeric __P((const struct addrinfo *, const char *, - const char *, struct addrinfo **)); -static int explore_numeric_scope __P((const struct addrinfo *, const char *, - const char *, struct addrinfo **)); -static int get_canonname __P((const struct addrinfo *, - struct addrinfo *, const char *)); -static struct addrinfo *get_ai __P((const struct addrinfo *, - const struct afd *, const char *)); -static struct addrinfo *copy_ai __P((const struct addrinfo *)); -static int get_portmatch __P((const struct addrinfo *, const char *)); -static int get_port __P((const struct addrinfo *, const char *, int)); -static const struct afd *find_afd __P((int)); -static int addrconfig __P((int)); -static int ip6_str2scopeid __P((char *, struct sockaddr_in6 *, - u_int32_t *scopeidp)); -static struct net_data *init __P((void)); - -struct addrinfo *hostent2addrinfo __P((struct hostent *, - const struct addrinfo *)); -struct addrinfo *addr2addrinfo __P((const struct addrinfo *, - const char *)); +#define MAXPACKET (64*1024) -#if 0 -static const char *ai_errlist[] = { - "Success", - "Address family for hostname not supported", /*%< EAI_ADDRFAMILY */ - "Temporary failure in name resolution", /*%< EAI_AGAIN */ - "Invalid value for ai_flags", /*%< EAI_BADFLAGS */ - "Non-recoverable failure in name resolution", /*%< EAI_FAIL */ - "ai_family not supported", /*%< EAI_FAMILY */ - "Memory allocation failure", /*%< EAI_MEMORY */ - "No address associated with hostname", /*%< EAI_NODATA */ - "hostname nor servname provided, or not known", /*%< EAI_NONAME */ - "servname not supported for ai_socktype", /*%< EAI_SERVICE */ - "ai_socktype not supported", /*%< EAI_SOCKTYPE */ - "System error returned in errno", /*%< EAI_SYSTEM */ - "Invalid value for hints", /*%< EAI_BADHINTS */ - "Resolved protocol is unknown", /*%< EAI_PROTOCOL */ - "Unknown error", /*%< EAI_MAX */ +typedef union { + HEADER hdr; + u_char buf[MAXPACKET]; +} querybuf; + +struct res_target { + struct res_target *next; + const char *name; /* domain name */ + int qclass, qtype; /* class and type of query */ + u_char *answer; /* buffer to put answer */ + int anslen; /* size of answer buffer */ + int n; /* result length */ +}; + +struct srvinfo { + struct srvinfo *next; + char name[MAXDNAME]; + int port, pri, weight; }; + +static int gai_srvok(const char *); +static int str2number(const char *); +static int explore_fqdn(const struct addrinfo *, const char *, + const char *, struct addrinfo **, struct servent_data *); +static int explore_null(const struct addrinfo *, + const char *, struct addrinfo **, struct servent_data *); +static int explore_numeric(const struct addrinfo *, const char *, + const char *, struct addrinfo **, const char *, struct servent_data *); +static int explore_numeric_scope(const struct addrinfo *, const char *, + const char *, struct addrinfo **, struct servent_data *); +static int get_canonname(const struct addrinfo *, + struct addrinfo *, const char *); +static struct addrinfo *get_ai(const struct addrinfo *, + const struct afd *, const char *); +static int get_portmatch(const struct addrinfo *, const char *, + struct servent_data *); +static int get_port(const struct addrinfo *, const char *, int, + struct servent_data *); +static const struct afd *find_afd(int); +static int addrconfig(uint64_t *); +#ifdef INET6 +static int ip6_str2scopeid(char *, struct sockaddr_in6 *, u_int32_t *); +#endif + +static struct addrinfo *getanswer(const querybuf *, int, const char *, int, + const struct addrinfo *); +static void aisort(struct addrinfo *s, res_state res); +static struct addrinfo * _dns_query(struct res_target *, + const struct addrinfo *, res_state, int); +static struct addrinfo * _dns_srv_lookup(const char *, const char *, + const struct addrinfo *); +static struct addrinfo * _dns_host_lookup(const char *, + const struct addrinfo *); +static int _dns_getaddrinfo(void *, void *, va_list); +static void _sethtent(FILE **); +static void _endhtent(FILE **); +static struct addrinfo *_gethtent(FILE **, const char *, + const struct addrinfo *); +static int _files_getaddrinfo(void *, void *, va_list); +#ifdef YP +static struct addrinfo *_yphostent(char *, const struct addrinfo *); +static int _yp_getaddrinfo(void *, void *, va_list); #endif +static int res_queryN(const char *, struct res_target *, res_state); +static int res_searchN(const char *, struct res_target *, res_state); +static int res_querydomainN(const char *, const char *, + struct res_target *, res_state); + +static const char * const ai_errlist[] = { + "Success", + "Address family for hostname not supported", /* EAI_ADDRFAMILY */ + "Temporary failure in name resolution", /* EAI_AGAIN */ + "Invalid value for ai_flags", /* EAI_BADFLAGS */ + "Non-recoverable failure in name resolution", /* EAI_FAIL */ + "ai_family not supported", /* EAI_FAMILY */ + "Memory allocation failure", /* EAI_MEMORY */ + "No address associated with hostname", /* EAI_NODATA */ + "hostname nor servname provided, or not known", /* EAI_NONAME */ + "servname not supported for ai_socktype", /* EAI_SERVICE */ + "ai_socktype not supported", /* EAI_SOCKTYPE */ + "System error returned in errno", /* EAI_SYSTEM */ + "Invalid value for hints", /* EAI_BADHINTS */ + "Resolved protocol is unknown", /* EAI_PROTOCOL */ + "Argument buffer overflow", /* EAI_OVERFLOW */ + "Unknown error", /* EAI_MAX */ +}; + /* XXX macros that make external reference is BAD. */ -#define GET_AI(ai, afd, addr) \ -do { \ - /* external reference: pai, error, and label free */ \ - (ai) = get_ai(pai, (afd), (addr)); \ - if ((ai) == NULL) { \ - error = EAI_MEMORY; \ - goto free; \ - } \ +#define GET_AI(ai, afd, addr) \ +do { \ + /* external reference: pai, error, and label free */ \ + (ai) = get_ai(pai, (afd), (addr)); \ + if ((ai) == NULL) { \ + error = EAI_MEMORY; \ + goto free; \ + } \ } while (/*CONSTCOND*/0) -#define GET_PORT(ai, serv) \ -do { \ - /* external reference: error and label free */ \ - error = get_port((ai), (serv), 0); \ - if (error != 0) \ - goto free; \ +#define GET_PORT(ai, serv, svd) \ +do { \ + /* external reference: error and label free */ \ + error = get_port((ai), (serv), 0, (svd)); \ + if (error != 0) \ + goto free; \ } while (/*CONSTCOND*/0) -#define GET_CANONNAME(ai, str) \ -do { \ - /* external reference: pai, error and label free */ \ - error = get_canonname(pai, (ai), (str)); \ - if (error != 0) \ - goto free; \ +#define GET_CANONNAME(ai, str) \ +do { \ + /* external reference: pai, error and label free */ \ + error = get_canonname(pai, (ai), (str)); \ + if (error != 0) \ + goto free; \ } while (/*CONSTCOND*/0) -#ifndef SOLARIS2 -#define SETERROR(err) \ -do { \ - /* external reference: error, and label bad */ \ - error = (err); \ - goto bad; \ - /*NOTREACHED*/ \ +#define ERR(err) \ +do { \ + /* external reference: error, and label bad */ \ + error = (err); \ + goto bad; \ + /*NOTREACHED*/ \ } while (/*CONSTCOND*/0) -#else -#define SETERROR(err) \ -do { \ - /* external reference: error, and label bad */ \ - error = (err); \ - if (error == error) \ - goto bad; \ -} while (/*CONSTCOND*/0) -#endif - -#define MATCH_FAMILY(x, y, w) \ - ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == PF_UNSPEC || (y) == PF_UNSPEC))) -#define MATCH(x, y, w) \ +#define MATCH_FAMILY(x, y, w) \ + ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == PF_UNSPEC || \ + (y) == PF_UNSPEC))) +#define MATCH(x, y, w) \ ((x) == (y) || (/*CONSTCOND*/(w) && ((x) == ANY || (y) == ANY))) -#if 0 /*%< bind8 has its own version */ -char * -gai_strerror(ecode) - int ecode; +const char * +gai_strerror(int ecode) { if (ecode < 0 || ecode > EAI_MAX) ecode = EAI_MAX; return ai_errlist[ecode]; } -#endif void -freeaddrinfo(ai) - struct addrinfo *ai; +freeaddrinfo(struct addrinfo *ai) { struct addrinfo *next; + _DIAGASSERT(ai != NULL); + do { next = ai->ai_next; if (ai->ai_canonname) @@ -299,52 +332,105 @@ freeaddrinfo(ai) } while (ai); } +/* + * We don't want localization to affect us + */ +#define PERIOD '.' +#define hyphenchar(c) ((c) == '-') +#define periodchar(c) ((c) == PERIOD) +#define underschar(c) ((c) == '_') +#define alphachar(c) (((c) >= 'a' && (c) <= 'z') || ((c) >= 'A' && (c) <= 'Z')) +#define digitchar(c) ((c) >= '0' && (c) <= '9') + +#define firstchar(c) (alphachar(c) || digitchar(c) || underschar(c)) +#define lastchar(c) (alphachar(c) || digitchar(c)) +#define middlechar(c) (lastchar(c) || hyphenchar(c)) + +static int +gai_srvok(const char *dn) +{ + int nch, pch, ch; + + for (pch = PERIOD, nch = ch = *dn++; ch != '\0'; pch = ch, ch = nch) { + if (periodchar(ch)) + continue; + if (periodchar(pch)) { + if (!firstchar(ch)) + return 0; + } else if (periodchar(nch) || nch == '\0') { + if (!lastchar(ch)) + return 0; + } else if (!middlechar(ch)) + return 0; + } + return 1; +} + +static in_port_t * +getport(struct addrinfo *ai) { + static in_port_t p; + + switch (ai->ai_family) { + case AF_INET: + return &((struct sockaddr_in *)(void *)ai->ai_addr)->sin_port; +#ifdef INET6 + case AF_INET6: + return &((struct sockaddr_in6 *)(void *)ai->ai_addr)->sin6_port; +#endif + default: + p = 0; + /* XXX: abort()? */ + return &p; + } +} + static int -str_isnumber(p) - const char *p; +str2number(const char *p) { char *ep; + unsigned long v; + + _DIAGASSERT(p != NULL); if (*p == '\0') - return NO; + return -1; ep = NULL; errno = 0; - (void)strtoul(p, &ep, 10); - if (errno == 0 && ep && *ep == '\0') - return YES; + v = strtoul(p, &ep, 10); + if (errno == 0 && ep && *ep == '\0' && v <= INT_MAX) + return (int)v; else - return NO; + return -1; } int -getaddrinfo(hostname, servname, hints, res) - const char *hostname, *servname; - const struct addrinfo *hints; - struct addrinfo **res; +getaddrinfo(const char *hostname, const char *servname, + const struct addrinfo *hints, struct addrinfo **res) { struct addrinfo sentinel; struct addrinfo *cur; int error = 0; - struct addrinfo ai, ai0, *afai = NULL; + struct addrinfo ai; + struct addrinfo ai0; struct addrinfo *pai; const struct explore *ex; + struct servent_data svd; + uint64_t mask = (uint64_t)~0ULL; + + /* hostname is allowed to be NULL */ + /* servname is allowed to be NULL */ + /* hints is allowed to be NULL */ + _DIAGASSERT(res != NULL); + (void)memset(&svd, 0, sizeof(svd)); memset(&sentinel, 0, sizeof(sentinel)); cur = &sentinel; + memset(&ai, 0, sizeof(ai)); pai = &ai; pai->ai_flags = 0; pai->ai_family = PF_UNSPEC; pai->ai_socktype = ANY; pai->ai_protocol = ANY; -#if defined(sun) && defined(_SOCKLEN_T) && defined(__sparcv9) - /* - * clear _ai_pad to preserve binary - * compatibility with previously compiled 64-bit - * applications in a pre-SUSv3 environment by - * guaranteeing the upper 32-bits are empty. - */ - pai->_ai_pad = 0; -#endif pai->ai_addrlen = 0; pai->ai_canonname = NULL; pai->ai_addr = NULL; @@ -356,26 +442,21 @@ getaddrinfo(hostname, servname, hints, r /* error check for hints */ if (hints->ai_addrlen || hints->ai_canonname || hints->ai_addr || hints->ai_next) - SETERROR(EAI_BADHINTS); /*%< xxx */ + ERR(EAI_BADHINTS); /* xxx */ if (hints->ai_flags & ~AI_MASK) - SETERROR(EAI_BADFLAGS); + ERR(EAI_BADFLAGS); switch (hints->ai_family) { case PF_UNSPEC: case PF_INET: +#ifdef INET6 case PF_INET6: +#endif break; default: - SETERROR(EAI_FAMILY); + ERR(EAI_FAMILY); } memcpy(pai, hints, sizeof(*pai)); -#if defined(sun) && defined(_SOCKLEN_T) && defined(__sparcv9) - /* - * We need to clear _ai_pad to preserve binary - * compatibility. See prior comment. - */ - pai->_ai_pad = 0; -#endif /* * if both socktype/protocol are specified, check if they * are meaningful combination. @@ -388,34 +469,16 @@ getaddrinfo(hostname, servname, hints, r continue; if (ex->e_protocol == ANY) continue; - if (pai->ai_socktype == ex->e_socktype && - pai->ai_protocol != ex->e_protocol) { - SETERROR(EAI_BADHINTS); + if (pai->ai_socktype == ex->e_socktype + && pai->ai_protocol != ex->e_protocol) { + ERR(EAI_BADHINTS); } } } } - /* - * post-2553: AI_ALL and AI_V4MAPPED are effective only against - * AF_INET6 query. They needs to be ignored if specified in other - * occassions. - */ - switch (pai->ai_flags & (AI_ALL | AI_V4MAPPED)) { - case AI_V4MAPPED: - case AI_ALL | AI_V4MAPPED: - if (pai->ai_family != AF_INET6) - pai->ai_flags &= ~(AI_ALL | AI_V4MAPPED); - break; - case AI_ALL: -#if 1 - /* illegal */ - SETERROR(EAI_BADFLAGS); -#else - pai->ai_flags &= ~(AI_ALL | AI_V4MAPPED); - break; -#endif - } + if ((pai->ai_flags & AI_ADDRCONFIG) != 0 && addrconfig(&mask) == -1) + ERR(EAI_FAIL); /* * check for special cases. (1) numeric servname is disallowed if @@ -436,9 +499,9 @@ getaddrinfo(hostname, servname, hints, r pai->ai_family = PF_INET; #endif } - error = get_portmatch(pai, servname); + error = get_portmatch(pai, servname, &svd); if (error) - SETERROR(error); + goto bad; *pai = ai0; } @@ -449,13 +512,20 @@ getaddrinfo(hostname, servname, hints, r for (ex = explore; ex->e_af >= 0; ex++) { *pai = ai0; + /* ADDRCONFIG check */ + if ((((uint64_t)1 << ex->e_af) & mask) == 0) + continue; + + /* PF_UNSPEC entries are prepared for DNS queries only */ + if (ex->e_af == PF_UNSPEC) + continue; + if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex))) continue; if (!MATCH(pai->ai_socktype, ex->e_socktype, WILD_SOCKTYPE(ex))) continue; if (!MATCH(pai->ai_protocol, ex->e_protocol, WILD_PROTOCOL(ex))) continue; - if (pai->ai_family == PF_UNSPEC) pai->ai_family = ex->e_af; if (pai->ai_socktype == ANY && ex->e_socktype != ANY) @@ -463,223 +533,152 @@ getaddrinfo(hostname, servname, hints, r if (pai->ai_protocol == ANY && ex->e_protocol != ANY) pai->ai_protocol = ex->e_protocol; - /* - * if the servname does not match socktype/protocol, ignore it. - */ - if (get_portmatch(pai, servname) != 0) - continue; - - if (hostname == NULL) { - /* - * filter out AFs that are not supported by the kernel - * XXX errno? - */ - if (!addrconfig(pai->ai_family)) - continue; - error = explore_null(pai, servname, &cur->ai_next); - } else + if (hostname == NULL) + error = explore_null(pai, servname, &cur->ai_next, + &svd); + else error = explore_numeric_scope(pai, hostname, servname, - &cur->ai_next); + &cur->ai_next, &svd); if (error) goto free; - while (cur && cur->ai_next) + while (cur->ai_next) cur = cur->ai_next; } /* * XXX - * If numreic representation of AF1 can be interpreted as FQDN + * If numeric representation of AF1 can be interpreted as FQDN * representation of AF2, we need to think again about the code below. */ if (sentinel.ai_next) goto good; - if (pai->ai_flags & AI_NUMERICHOST) - SETERROR(EAI_NONAME); if (hostname == NULL) - SETERROR(EAI_NONAME); + ERR(EAI_NODATA); + if (pai->ai_flags & AI_NUMERICHOST) + ERR(EAI_NONAME); /* * hostname as alphabetical name. - * We'll make sure that - * - if returning addrinfo list is empty, return non-zero error - * value (already known one or EAI_NONAME). - * - otherwise, - * + if we haven't had any errors, return 0 (i.e. success). - * + if we've had an error, free the list and return the error. - * without any assumption on the behavior of explore_fqdn(). - */ - - /* first, try to query DNS for all possible address families. */ - *pai = ai0; - error = explore_fqdn(pai, hostname, servname, &afai); - if (error) { - if (afai != NULL) - freeaddrinfo(afai); - goto free; - } - if (afai == NULL) { - error = EAI_NONAME; /*%< we've had no errors. */ - goto free; - } - - /* - * we would like to prefer AF_INET6 than AF_INET, so we'll make an + * we would like to prefer AF_INET6 than AF_INET, so we'll make a * outer loop by AFs. */ for (ex = explore; ex->e_af >= 0; ex++) { *pai = ai0; - if (pai->ai_family == PF_UNSPEC) - pai->ai_family = ex->e_af; - if (!MATCH_FAMILY(pai->ai_family, ex->e_af, WILD_AF(ex))) + /* ADDRCONFIG check */ + /* PF_UNSPEC entries are prepared for DNS queries only */ + if (ex->e_af != PF_UNSPEC && + (((uint64_t)1 << ex->e_af) & mask) == 0) + continue; + + /* require exact match for family field */ + if (pai->ai_family != ex->e_af) continue; + if (!MATCH(pai->ai_socktype, ex->e_socktype, - WILD_SOCKTYPE(ex))) { + WILD_SOCKTYPE(ex))) { continue; } if (!MATCH(pai->ai_protocol, ex->e_protocol, - WILD_PROTOCOL(ex))) { + WILD_PROTOCOL(ex))) { continue; } -#ifdef AI_ADDRCONFIG - /* - * If AI_ADDRCONFIG is specified, check if we are - * expected to return the address family or not. - */ - if ((pai->ai_flags & AI_ADDRCONFIG) != 0 && - !addrconfig(pai->ai_family)) - continue; -#endif - - if (pai->ai_family == PF_UNSPEC) - pai->ai_family = ex->e_af; if (pai->ai_socktype == ANY && ex->e_socktype != ANY) pai->ai_socktype = ex->e_socktype; if (pai->ai_protocol == ANY && ex->e_protocol != ANY) pai->ai_protocol = ex->e_protocol; - /* - * if the servname does not match socktype/protocol, ignore it. - */ - if (get_portmatch(pai, servname) != 0) - continue; - - if ((error = explore_copy(pai, afai, &cur->ai_next)) != 0) { - freeaddrinfo(afai); - goto free; - } + error = explore_fqdn(pai, hostname, servname, &cur->ai_next, + &svd); while (cur && cur->ai_next) cur = cur->ai_next; } - freeaddrinfo(afai); /*%< afai must not be NULL at this point. */ + /* XXX */ + if (sentinel.ai_next) + error = 0; + + if (error) + goto free; if (sentinel.ai_next) { -good: + good: + endservent_r(&svd); *res = sentinel.ai_next; - return(SUCCESS); - } else { - /* - * All the process succeeded, but we've had an empty list. - * This can happen if the given hints do not match our - * candidates. - */ - error = EAI_NONAME; - } - -free: -bad: + return SUCCESS; + } else + error = EAI_FAIL; + free: + bad: + endservent_r(&svd); if (sentinel.ai_next) freeaddrinfo(sentinel.ai_next); *res = NULL; - return(error); + return error; } -/*% +/* * FQDN hostname, DNS lookup */ static int -explore_fqdn(pai, hostname, servname, res) - const struct addrinfo *pai; - const char *hostname; - const char *servname; - struct addrinfo **res; +explore_fqdn(const struct addrinfo *pai, const char *hostname, + const char *servname, struct addrinfo **res, struct servent_data *svd) { struct addrinfo *result; struct addrinfo *cur; - struct net_data *net_data = init(); - struct irs_ho *ho; int error = 0; - char tmp[NS_MAXDNAME]; - const char *cp; + static const ns_dtab dtab[] = { + NS_FILES_CB(_files_getaddrinfo, NULL) + { NSSRC_DNS, _dns_getaddrinfo, NULL }, /* force -DHESIOD */ + NS_NIS_CB(_yp_getaddrinfo, NULL) + NS_NULL_CB + }; + + _DIAGASSERT(pai != NULL); + /* hostname may be NULL */ + /* servname may be NULL */ + _DIAGASSERT(res != NULL); - INSIST(res != NULL && *res == NULL); + result = NULL; /* * if the servname does not match socktype/protocol, ignore it. */ - if (get_portmatch(pai, servname) != 0) - return(0); + if (get_portmatch(pai, servname, svd) != 0) + return 0; - if (!net_data || !(ho = net_data->ho)) - return(0); -#if 0 /*%< XXX (notyet) */ - if (net_data->ho_stayopen && net_data->ho_last && - net_data->ho_last->h_addrtype == af) { - if (ns_samename(name, net_data->ho_last->h_name) == 1) - return (net_data->ho_last); - for (hap = net_data->ho_last->h_aliases; hap && *hap; hap++) - if (ns_samename(name, *hap) == 1) - return (net_data->ho_last); - } -#endif - if (!strchr(hostname, '.') && - (cp = res_hostalias(net_data->res, hostname, - tmp, sizeof(tmp)))) - hostname = cp; - result = (*ho->addrinfo)(ho, hostname, pai); - if (!net_data->ho_stayopen) { - (*ho->minimize)(ho); - } - if (result == NULL) { - int e = h_errno; - - switch(e) { - case NETDB_INTERNAL: - error = EAI_SYSTEM; - break; - case TRY_AGAIN: - error = EAI_AGAIN; - break; - case NO_RECOVERY: - error = EAI_FAIL; - break; - case HOST_NOT_FOUND: - case NO_DATA: - error = EAI_NONAME; - break; - default: - case NETDB_SUCCESS: /*%< should be impossible... */ - error = EAI_NONAME; - break; - } + switch (nsdispatch(&result, dtab, NSDB_HOSTS, "getaddrinfo", + default_dns_files, hostname, pai, servname)) { + case NS_TRYAGAIN: + error = EAI_AGAIN; goto free; - } - - for (cur = result; cur; cur = cur->ai_next) { - GET_PORT(cur, servname); /*%< XXX: redundant lookups... */ - /* canonname should already be filled. */ + case NS_UNAVAIL: + error = EAI_FAIL; + goto free; + case NS_NOTFOUND: + error = EAI_NODATA; + goto free; + case NS_SUCCESS: + error = 0; + for (cur = result; cur; cur = cur->ai_next) { + /* Check for already filled port. */ + if (*getport(cur)) + continue; + GET_PORT(cur, servname, svd); + /* canonname should be filled already */ + } + break; } *res = result; - return(0); + return 0; free: if (result) @@ -687,63 +686,46 @@ free: return error; } -static int -explore_copy(pai, src0, res) - const struct addrinfo *pai; /*%< seed */ - const struct addrinfo *src0; /*%< source */ - struct addrinfo **res; -{ - int error; - struct addrinfo sentinel, *cur; - const struct addrinfo *src; - - error = 0; - sentinel.ai_next = NULL; - cur = &sentinel; - - for (src = src0; src != NULL; src = src->ai_next) { - if (src->ai_family != pai->ai_family) - continue; - - cur->ai_next = copy_ai(src); - if (!cur->ai_next) { - error = EAI_MEMORY; - goto fail; - } - - cur->ai_next->ai_socktype = pai->ai_socktype; - cur->ai_next->ai_protocol = pai->ai_protocol; - cur = cur->ai_next; - } - - *res = sentinel.ai_next; - return 0; - -fail: - freeaddrinfo(sentinel.ai_next); - return error; -} - -/*% +/* * hostname == NULL. * passive socket -> anyaddr (0.0.0.0 or ::) * non-passive socket -> localhost (127.0.0.1 or ::1) */ static int -explore_null(pai, servname, res) - const struct addrinfo *pai; - const char *servname; - struct addrinfo **res; +explore_null(const struct addrinfo *pai, const char *servname, + struct addrinfo **res, struct servent_data *svd) { + int s; const struct afd *afd; struct addrinfo *cur; struct addrinfo sentinel; int error; + _DIAGASSERT(pai != NULL); + /* servname may be NULL */ + _DIAGASSERT(res != NULL); + *res = NULL; sentinel.ai_next = NULL; cur = &sentinel; + /* + * filter out AFs that are not supported by the kernel + * XXX errno? + */ + s = socket(pai->ai_family, SOCK_DGRAM, 0); + if (s < 0) { + if (errno != EMFILE) + return 0; + } else + close(s); + + /* + * if the servname does not match socktype/protocol, ignore it. + */ + if (get_portmatch(pai, servname, svd) != 0) + return 0; + afd = find_afd(pai->ai_family); if (afd == NULL) return 0; @@ -753,13 +735,13 @@ explore_null(pai, servname, res) /* xxx meaningless? * GET_CANONNAME(cur->ai_next, "anyaddr"); */ - GET_PORT(cur->ai_next, servname); + GET_PORT(cur->ai_next, servname, svd); } else { GET_AI(cur->ai_next, afd, afd->a_loopback); /* xxx meaningless? * GET_CANONNAME(cur->ai_next, "localhost"); */ - GET_PORT(cur->ai_next, servname); + GET_PORT(cur->ai_next, servname, svd); } cur = cur->ai_next; @@ -772,15 +754,13 @@ free: return error; } -/*% +/* * numeric hostname */ static int -explore_numeric(pai, hostname, servname, res) - const struct addrinfo *pai; - const char *hostname; - const char *servname; - struct addrinfo **res; +explore_numeric(const struct addrinfo *pai, const char *hostname, + const char *servname, struct addrinfo **res, const char *canonname, + struct servent_data *svd) { const struct afd *afd; struct addrinfo *cur; @@ -788,10 +768,21 @@ explore_numeric(pai, hostname, servname, int error; char pton[PTON_MAX]; + _DIAGASSERT(pai != NULL); + /* hostname may be NULL */ + /* servname may be NULL */ + _DIAGASSERT(res != NULL); + *res = NULL; sentinel.ai_next = NULL; cur = &sentinel; + /* + * if the servname does not match socktype/protocol, ignore it. + */ + if (get_portmatch(pai, servname, svd) != 0) + return 0; + afd = find_afd(pai->ai_family); if (afd == NULL) return 0; @@ -803,11 +794,19 @@ explore_numeric(pai, hostname, servname, if (pai->ai_family == afd->a_af || pai->ai_family == PF_UNSPEC /*?*/) { GET_AI(cur->ai_next, afd, pton); - GET_PORT(cur->ai_next, servname); - while (cur->ai_next) + GET_PORT(cur->ai_next, servname, svd); + if ((pai->ai_flags & AI_CANONNAME)) { + /* + * Set the numeric address itself as + * the canonical name, based on a + * clarification in rfc2553bis-03. + */ + GET_CANONNAME(cur->ai_next, canonname); + } + while (cur && cur->ai_next) cur = cur->ai_next; } else - SETERROR(EAI_FAMILY); /*xxx*/ + ERR(EAI_FAMILY); /*xxx*/ } break; #endif @@ -816,11 +815,19 @@ explore_numeric(pai, hostname, servname, if (pai->ai_family == afd->a_af || pai->ai_family == PF_UNSPEC /*?*/) { GET_AI(cur->ai_next, afd, pton); - GET_PORT(cur->ai_next, servname); + GET_PORT(cur->ai_next, servname, svd); + if ((pai->ai_flags & AI_CANONNAME)) { + /* + * Set the numeric address itself as + * the canonical name, based on a + * clarification in rfc2553bis-03. + */ + GET_CANONNAME(cur->ai_next, canonname); + } while (cur->ai_next) cur = cur->ai_next; } else - SETERROR(EAI_FAMILY); /*xxx*/ + ERR(EAI_FAMILY); /*xxx*/ } break; } @@ -835,18 +842,15 @@ bad: return error; } -/*% +/* * numeric hostname with scope */ static int -explore_numeric_scope(pai, hostname, servname, res) - const struct addrinfo *pai; - const char *hostname; - const char *servname; - struct addrinfo **res; +explore_numeric_scope(const struct addrinfo *pai, const char *hostname, + const char *servname, struct addrinfo **res, struct servent_data *svd) { -#ifndef SCOPE_DELIMITER - return explore_numeric(pai, hostname, servname, res); +#if !defined(SCOPE_DELIMITER) || !defined(INET6) + return explore_numeric(pai, hostname, servname, res, hostname, svd); #else const struct afd *afd; struct addrinfo *cur; @@ -854,16 +858,29 @@ explore_numeric_scope(pai, hostname, ser char *cp, *hostname2 = NULL, *scope, *addr; struct sockaddr_in6 *sin6; + _DIAGASSERT(pai != NULL); + /* hostname may be NULL */ + /* servname may be NULL */ + _DIAGASSERT(res != NULL); + + /* + * if the servname does not match socktype/protocol, ignore it. + */ + if (get_portmatch(pai, servname, svd) != 0) + return 0; + afd = find_afd(pai->ai_family); if (afd == NULL) return 0; if (!afd->a_scoped) - return explore_numeric(pai, hostname, servname, res); + return explore_numeric(pai, hostname, servname, res, hostname, + svd); cp = strchr(hostname, SCOPE_DELIMITER); if (cp == NULL) - return explore_numeric(pai, hostname, servname, res); + return explore_numeric(pai, hostname, servname, res, hostname, + svd); /* * Handle special case of @@ -876,21 +893,19 @@ explore_numeric_scope(pai, hostname, ser addr = hostname2; scope = cp + 1; - error = explore_numeric(pai, addr, servname, res); + error = explore_numeric(pai, addr, servname, res, hostname, svd); if (error == 0) { - u_int32_t scopeid = 0; + u_int32_t scopeid; for (cur = *res; cur; cur = cur->ai_next) { if (cur->ai_family != AF_INET6) continue; sin6 = (struct sockaddr_in6 *)(void *)cur->ai_addr; - if (!ip6_str2scopeid(scope, sin6, &scopeid)) { + if (ip6_str2scopeid(scope, sin6, &scopeid) == -1) { free(hostname2); - return(EAI_NONAME); /*%< XXX: is return OK? */ + return(EAI_NODATA); /* XXX: is return OK? */ } -#ifdef HAVE_SIN6_SCOPE_ID sin6->sin6_scope_id = scopeid; -#endif } } @@ -901,95 +916,87 @@ explore_numeric_scope(pai, hostname, ser } static int -get_canonname(pai, ai, str) - const struct addrinfo *pai; - struct addrinfo *ai; - const char *str; +get_canonname(const struct addrinfo *pai, struct addrinfo *ai, const char *str) { + + _DIAGASSERT(pai != NULL); + _DIAGASSERT(ai != NULL); + _DIAGASSERT(str != NULL); + if ((pai->ai_flags & AI_CANONNAME) != 0) { - ai->ai_canonname = (char *)malloc(strlen(str) + 1); + ai->ai_canonname = strdup(str); if (ai->ai_canonname == NULL) return EAI_MEMORY; - strcpy(ai->ai_canonname, str); } return 0; } -static struct addrinfo * -get_ai(pai, afd, addr) - const struct addrinfo *pai; - const struct afd *afd; - const char *addr; +struct addrinfo * +allocaddrinfo(socklen_t addrlen) { - char *p; struct addrinfo *ai; - ai = (struct addrinfo *)malloc(sizeof(struct addrinfo) - + (afd->a_socklen)); - if (ai == NULL) - return NULL; + ai = calloc(sizeof(struct addrinfo) + addrlen, 1); + if (ai) { + ai->ai_addr = (void *)(ai+1); + ai->ai_addrlen = ai->ai_addr->sa_len = addrlen; + } - memcpy(ai, pai, sizeof(struct addrinfo)); - ai->ai_addr = (struct sockaddr *)(void *)(ai + 1); - memset(ai->ai_addr, 0, (size_t)afd->a_socklen); -#ifdef HAVE_SA_LEN - ai->ai_addr->sa_len = afd->a_socklen; -#endif - ai->ai_addrlen = afd->a_socklen; - ai->ai_addr->sa_family = ai->ai_family = afd->a_af; - p = (char *)(void *)(ai->ai_addr); - memcpy(p + afd->a_off, addr, (size_t)afd->a_addrlen); return ai; } -/* XXX need to malloc() the same way we do from other functions! */ static struct addrinfo * -copy_ai(pai) - const struct addrinfo *pai; +get_ai(const struct addrinfo *pai, const struct afd *afd, const char *addr) { + char *p; struct addrinfo *ai; - size_t l; + struct sockaddr *save; - l = sizeof(*ai) + pai->ai_addrlen; - if ((ai = (struct addrinfo *)malloc(l)) == NULL) + _DIAGASSERT(pai != NULL); + _DIAGASSERT(afd != NULL); + _DIAGASSERT(addr != NULL); + + ai = allocaddrinfo((socklen_t)afd->a_socklen); + if (ai == NULL) return NULL; - memset(ai, 0, l); - memcpy(ai, pai, sizeof(*ai)); - ai->ai_addr = (struct sockaddr *)(void *)(ai + 1); - memcpy(ai->ai_addr, pai->ai_addr, pai->ai_addrlen); - - if (pai->ai_canonname) { - l = strlen(pai->ai_canonname) + 1; - if ((ai->ai_canonname = malloc(l)) == NULL) { - free(ai); - return NULL; - } - strcpy(ai->ai_canonname, pai->ai_canonname); /* (checked) */ - } else { - /* just to make sure */ - ai->ai_canonname = NULL; - } - ai->ai_next = NULL; + save = ai->ai_addr; + memcpy(ai, pai, sizeof(struct addrinfo)); + + /* since we just overwrote all of ai, we have + to restore ai_addr and ai_addrlen */ + ai->ai_addr = save; + ai->ai_addrlen = (socklen_t)afd->a_socklen; + ai->ai_addr->sa_family = ai->ai_family = afd->a_af; + p = (char *)(void *)(ai->ai_addr); + memcpy(p + afd->a_off, addr, (size_t)afd->a_addrlen); return ai; } static int -get_portmatch(const struct addrinfo *ai, const char *servname) { +get_portmatch(const struct addrinfo *ai, const char *servname, + struct servent_data *svd) +{ - /* get_port does not touch first argument. when matchonly == 1. */ - /* LINTED const cast */ - return get_port((const struct addrinfo *)ai, servname, 1); + _DIAGASSERT(ai != NULL); + /* servname may be NULL */ + + return get_port(ai, servname, 1, svd); } static int -get_port(const struct addrinfo *ai, const char *servname, int matchonly) { +get_port(const struct addrinfo *ai, const char *servname, int matchonly, + struct servent_data *svd) +{ const char *proto; struct servent *sp; int port; int allownumeric; + _DIAGASSERT(ai != NULL); + /* servname may be NULL */ + if (servname == NULL) return 0; switch (ai->ai_family) { @@ -1010,30 +1017,29 @@ get_port(const struct addrinfo *ai, cons allownumeric = 1; break; case ANY: - switch (ai->ai_family) { - case AF_INET: -#ifdef AF_INET6 - case AF_INET6: -#endif - allownumeric = 1; - break; - default: - allownumeric = 0; - break; - } + /* + * This was 0. It is now 1 so that queries specifying + * a NULL hint, or hint without socktype (but, hopefully, + * with protocol) and numeric address actually work. + */ + allownumeric = 1; break; default: return EAI_SOCKTYPE; } - if (str_isnumber(servname)) { + port = str2number(servname); + if (port >= 0) { if (!allownumeric) return EAI_SERVICE; - port = atoi(servname); if (port < 0 || port > 65535) return EAI_SERVICE; port = htons(port); } else { + struct servent sv; + if (ai->ai_flags & AI_NUMERICSERV) + return EAI_NONAME; + switch (ai->ai_socktype) { case SOCK_DGRAM: proto = "udp"; @@ -1046,30 +1052,19 @@ get_port(const struct addrinfo *ai, cons break; } - if ((sp = getservbyname(servname, proto)) == NULL) + sp = getservbyname_r(servname, proto, &sv, svd); + if (sp == NULL) return EAI_SERVICE; port = sp->s_port; } - if (!matchonly) { - switch (ai->ai_family) { - case AF_INET: - ((struct sockaddr_in *)(void *) - ai->ai_addr)->sin_port = port; - break; - case AF_INET6: - ((struct sockaddr_in6 *)(void *) - ai->ai_addr)->sin6_port = port; - break; - } - } - + if (!matchonly) + *getport(__UNCONST(ai)) = port; return 0; } static const struct afd * -find_afd(af) - int af; +find_afd(int af) { const struct afd *afd; @@ -1082,57 +1077,60 @@ find_afd(af) return NULL; } -/*% - * post-2553: AI_ADDRCONFIG check. if we use getipnodeby* as backend, backend - * will take care of it. - * the semantics of AI_ADDRCONFIG is not defined well. we are not sure - * if the code is right or not. +/* + * AI_ADDRCONFIG check: Build a mask containing a bit set for each address + * family configured in the system. + * */ static int -addrconfig(af) - int af; +addrconfig(uint64_t *mask) { - int s; + struct ifaddrs *ifaddrs, *ifa; - /* XXX errno */ - s = socket(af, SOCK_DGRAM, 0); - if (s < 0) { - if (errno != EMFILE) - return 0; - } else - close(s); - return 1; + if (getifaddrs(&ifaddrs) == -1) + return -1; + + *mask = 0; + for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) + if (ifa->ifa_addr && (ifa->ifa_flags & IFF_UP)) { + _DIAGASSERT(ifa->ifa_addr->sa_family < 64); + *mask |= (uint64_t)1 << ifa->ifa_addr->sa_family; + } + + freeifaddrs(ifaddrs); + return 0; } +#ifdef INET6 /* convert a string to a scope identifier. XXX: IPv6 specific */ static int -ip6_str2scopeid(char *scope, struct sockaddr_in6 *sin6, - u_int32_t *scopeidp) +ip6_str2scopeid(char *scope, struct sockaddr_in6 *sin6, u_int32_t *scopeid) { - u_int32_t scopeid; u_long lscopeid; - struct in6_addr *a6 = &sin6->sin6_addr; + struct in6_addr *a6; char *ep; - + + _DIAGASSERT(scope != NULL); + _DIAGASSERT(sin6 != NULL); + _DIAGASSERT(scopeid != NULL); + + a6 = &sin6->sin6_addr; + /* empty scopeid portion is invalid */ if (*scope == '\0') - return (0); + return -1; -#ifdef USE_IFNAMELINKID - if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6) || - IN6_IS_ADDR_MC_NODELOCAL(a6)) { + if (IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) { /* - * Using interface names as link indices can be allowed - * only when we can assume a one-to-one mappings between - * links and interfaces. See comments in getnameinfo.c. + * We currently assume a one-to-one mapping between links + * and interfaces, so we simply use interface indices for + * like-local scopes. */ - scopeid = if_nametoindex(scope); - if (scopeid == 0) + *scopeid = if_nametoindex(scope); + if (*scopeid == 0) goto trynumeric; - *scopeidp = scopeid; - return (1); + return 0; } -#endif /* still unclear about literal, allow numeric only - placeholder */ if (IN6_IS_ADDR_SITELOCAL(a6) || IN6_IS_ADDR_MC_SITELOCAL(a6)) @@ -1140,114 +1138,1191 @@ ip6_str2scopeid(char *scope, struct sock if (IN6_IS_ADDR_MC_ORGLOCAL(a6)) goto trynumeric; else - goto trynumeric; /*%< global */ + goto trynumeric; /* global */ + /* try to convert to a numeric id as a last resort */ -trynumeric: + trynumeric: errno = 0; lscopeid = strtoul(scope, &ep, 10); - scopeid = lscopeid & 0xffffffff; - if (errno == 0 && ep && *ep == '\0' && scopeid == lscopeid) { - *scopeidp = scopeid; - return (1); - } else - return (0); + *scopeid = (u_int32_t)(lscopeid & 0xffffffffUL); + if (errno == 0 && ep && *ep == '\0' && *scopeid == lscopeid) + return 0; + else + return -1; } +#endif -struct addrinfo * -hostent2addrinfo(hp, pai) - struct hostent *hp; - const struct addrinfo *pai; +/* code duplicate with gethnamaddr.c */ + +static const char AskedForGot[] = + "gethostby*.getanswer: asked for \"%s\", got \"%s\""; + +static struct addrinfo * +getanswer(const querybuf *answer, int anslen, const char *qname, int qtype, + const struct addrinfo *pai) { - int i, af, error = 0; - char **aplist = NULL, *ap; struct addrinfo sentinel, *cur; + struct addrinfo ai, *aip; const struct afd *afd; - - af = hp->h_addrtype; - if (pai->ai_family != AF_UNSPEC && af != pai->ai_family) - return(NULL); - - afd = find_afd(af); - if (afd == NULL) - return(NULL); - - aplist = hp->h_addr_list; + char *canonname; + const HEADER *hp; + const u_char *cp; + int n; + const u_char *eom; + char *bp, *ep; + int type, class, ancount, qdcount; + int haveanswer, had_error; + char tbuf[MAXDNAME]; + int (*name_ok) (const char *); + char hostbuf[8*1024]; + int port, pri, weight; + struct srvinfo *srvlist, *srv, *csrv; + + _DIAGASSERT(answer != NULL); + _DIAGASSERT(qname != NULL); + _DIAGASSERT(pai != NULL); memset(&sentinel, 0, sizeof(sentinel)); cur = &sentinel; - for (i = 0; (ap = aplist[i]) != NULL; i++) { -#if 0 /*%< the trick seems too much */ - af = hp->h_addr_list; - if (af == AF_INET6 && - IN6_IS_ADDR_V4MAPPED((struct in6_addr *)ap)) { - af = AF_INET; - ap = ap + sizeof(struct in6_addr) - - sizeof(struct in_addr); + canonname = NULL; + eom = answer->buf + anslen; + switch (qtype) { + case T_A: + case T_AAAA: + case T_ANY: /*use T_ANY only for T_A/T_AAAA lookup*/ + name_ok = res_hnok; + break; + case T_SRV: + name_ok = gai_srvok; + break; + default: + return NULL; /* XXX should be abort(); */ + } + /* + * find first satisfactory answer + */ + hp = &answer->hdr; + ancount = ntohs(hp->ancount); + qdcount = ntohs(hp->qdcount); + bp = hostbuf; + ep = hostbuf + sizeof hostbuf; + cp = answer->buf + HFIXEDSZ; + if (qdcount != 1) { + h_errno = NO_RECOVERY; + return (NULL); + } + n = dn_expand(answer->buf, eom, cp, bp, (int)(ep - bp)); + if ((n < 0) || !(*name_ok)(bp)) { + h_errno = NO_RECOVERY; + return (NULL); + } + cp += n + QFIXEDSZ; + if (qtype == T_A || qtype == T_AAAA || qtype == T_ANY) { + /* res_send() has already verified that the query name is the + * same as the one we sent; this just gets the expanded name + * (i.e., with the succeeding search-domain tacked on). + */ + n = (int)strlen(bp) + 1; /* for the \0 */ + if (n >= MAXHOSTNAMELEN) { + h_errno = NO_RECOVERY; + return (NULL); } - afd = find_afd(af); - if (afd == NULL) + canonname = bp; + bp += n; + /* The qname can be abbreviated, but h_name is now absolute. */ + qname = canonname; + } + haveanswer = 0; + had_error = 0; + srvlist = NULL; + while (ancount-- > 0 && cp < eom && !had_error) { + n = dn_expand(answer->buf, eom, cp, bp, (int)(ep - bp)); + if ((n < 0) || !(*name_ok)(bp)) { + had_error++; + continue; + } + cp += n; /* name */ + type = _getshort(cp); + cp += INT16SZ; /* type */ + class = _getshort(cp); + cp += INT16SZ + INT32SZ; /* class, TTL */ + n = _getshort(cp); + cp += INT16SZ; /* len */ + if (class != C_IN) { + /* XXX - debug? syslog? */ + cp += n; + continue; /* XXX - had_error++ ? */ + } + if ((qtype == T_A || qtype == T_AAAA || qtype == T_ANY) && + type == T_CNAME) { + n = dn_expand(answer->buf, eom, cp, tbuf, (int)sizeof tbuf); + if ((n < 0) || !(*name_ok)(tbuf)) { + had_error++; + continue; + } + cp += n; + /* Get canonical name. */ + n = (int)strlen(tbuf) + 1; /* for the \0 */ + if (n > ep - bp || n >= MAXHOSTNAMELEN) { + had_error++; + continue; + } + strlcpy(bp, tbuf, (size_t)(ep - bp)); + canonname = bp; + bp += n; continue; -#endif /* 0 */ + } + if (qtype == T_ANY) { + if (!(type == T_A || type == T_AAAA)) { + cp += n; + continue; + } + } else if (type != qtype) { + if (type != T_KEY && type != T_SIG) { + struct syslog_data sd = SYSLOG_DATA_INIT; + syslog_r(LOG_NOTICE|LOG_AUTH, &sd, + "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", + qname, p_class(C_IN), p_type(qtype), + p_type(type)); + } + cp += n; + continue; /* XXX - had_error++ ? */ + } + switch (type) { + case T_A: + case T_AAAA: + if (strcasecmp(canonname, bp) != 0) { + struct syslog_data sd = SYSLOG_DATA_INIT; + syslog_r(LOG_NOTICE|LOG_AUTH, &sd, + AskedForGot, canonname, bp); + cp += n; + continue; /* XXX - had_error++ ? */ + } + if (type == T_A && n != INADDRSZ) { + cp += n; + continue; + } + if (type == T_AAAA && n != IN6ADDRSZ) { + cp += n; + continue; + } + if (type == T_AAAA) { + struct in6_addr in6; + memcpy(&in6, cp, IN6ADDRSZ); + if (IN6_IS_ADDR_V4MAPPED(&in6)) { + cp += n; + continue; + } + } + if (!haveanswer) { + int nn; - GET_AI(cur->ai_next, afd, ap); + canonname = bp; + nn = (int)strlen(bp) + 1; /* for the \0 */ + bp += nn; + } + + /* don't overwrite pai */ + ai = *pai; + ai.ai_family = (type == T_A) ? AF_INET : AF_INET6; + afd = find_afd(ai.ai_family); + if (afd == NULL) { + cp += n; + continue; + } + cur->ai_next = get_ai(&ai, afd, (const char *)cp); + if (cur->ai_next == NULL) + had_error++; + while (cur && cur->ai_next) + cur = cur->ai_next; + cp += n; + break; + case T_SRV: + /* Add to SRV list. Insertion sort on priority. */ + pri = _getshort(cp); + cp += INT16SZ; + weight = _getshort(cp); + cp += INT16SZ; + port = _getshort(cp); + cp += INT16SZ; + n = dn_expand(answer->buf, eom, cp, tbuf, + (int)sizeof(tbuf)); + if ((n < 0) || !res_hnok(tbuf)) { + had_error++; + continue; + } + cp += n; + if (strlen(tbuf) + 1 >= MAXDNAME) { + had_error++; + continue; + } + srv = malloc(sizeof(*srv)); + if (!srv) { + had_error++; + continue; + } + strlcpy(srv->name, tbuf, sizeof(srv->name)); + srv->pri = pri; + srv->weight = weight; + srv->port = port; + /* Weight 0 is sorted before other weights. */ + if (!srvlist + || srv->pri < srvlist->pri + || (srv->pri == srvlist->pri && + (!srv->weight || srvlist->weight))) { + srv->next = srvlist; + srvlist = srv; + } else { + for (csrv = srvlist; + csrv->next && csrv->next->pri <= srv->pri; + csrv = csrv->next) { + if (csrv->next->pri == srv->pri + && (!srv->weight || + csrv->next->weight)) + break; + } + srv->next = csrv->next; + csrv->next = srv; + } + continue; /* Don't add to haveanswer yet. */ + default: + abort(); + } + if (!had_error) + haveanswer++; + } + + if (srvlist) { + res_state res; + /* + * Check for explicit rejection. + */ + if (!srvlist->next && !srvlist->name[0]) { + free(srvlist); + h_errno = HOST_NOT_FOUND; + return NULL; + } + res = __res_get_state(); + if (res == NULL) { + while (srvlist != NULL) { + srv = srvlist; + srvlist = srvlist->next; + free(srv); + } + h_errno = NETDB_INTERNAL; + return NULL; + } + + while (srvlist) { + struct res_target q, q2; + + srv = srvlist; + srvlist = srvlist->next; - /* GET_PORT(cur->ai_next, servname); */ - if ((pai->ai_flags & AI_CANONNAME) != 0) { /* - * RFC2553 says that ai_canonname will be set only for - * the first element. we do it for all the elements, - * just for convenience. + * Since res_* doesn't give the additional + * section, we always look up. */ - GET_CANONNAME(cur->ai_next, hp->h_name); + memset(&q, 0, sizeof(q)); + memset(&q2, 0, sizeof(q2)); + + q.name = srv->name; + q.qclass = C_IN; + q.qtype = T_AAAA; + q.next = &q2; + q2.name = srv->name; + q2.qclass = C_IN; + q2.qtype = T_A; + + aip = _dns_query(&q, pai, res, 0); + + if (aip != NULL) { + cur->ai_next = aip; + while (cur && cur->ai_next) { + cur = cur->ai_next; + *getport(cur) = htons(srv->port); + haveanswer++; + } + } + free(srv); } - while (cur->ai_next) /*%< no need to loop, actually. */ + __res_put_state(res); + } + if (haveanswer) { + if (!sentinel.ai_next->ai_canonname) + (void)get_canonname(pai, sentinel.ai_next, + canonname ? canonname : qname); + h_errno = NETDB_SUCCESS; + return sentinel.ai_next; + } + + h_errno = NO_RECOVERY; + return NULL; +} + +#define SORTEDADDR(p) (((struct sockaddr_in *)(void *)(p->ai_next->ai_addr))->sin_addr.s_addr) +#define SORTMATCH(p, s) ((SORTEDADDR(p) & (s).mask) == (s).addr.s_addr) + +static void +aisort(struct addrinfo *s, res_state res) +{ + struct addrinfo head, *t, *p; + int i; + + head.ai_next = NULL; + t = &head; + + for (i = 0; i < res->nsort; i++) { + p = s; + while (p->ai_next) { + if ((p->ai_next->ai_family != AF_INET) + || SORTMATCH(p, res->sort_list[i])) { + t->ai_next = p->ai_next; + t = t->ai_next; + p->ai_next = p->ai_next->ai_next; + } else { + p = p->ai_next; + } + } + } + + /* add rest of list and reset s to the new list*/ + t->ai_next = s->ai_next; + s->ai_next = head.ai_next; +} + +static struct addrinfo * +_dns_query(struct res_target *q, const struct addrinfo *pai, + res_state res, int dosearch) +{ + struct res_target *q2 = q->next; + querybuf *buf, *buf2; + struct addrinfo sentinel, *cur, *ai; + +#ifdef DNS_DEBUG + struct res_target *iter; + for (iter = q; iter; iter = iter->next) + printf("Query type %d for %s\n", iter->qtype, iter->name); +#endif + + buf = malloc(sizeof(*buf)); + if (buf == NULL) { + h_errno = NETDB_INTERNAL; + return NULL; + } + buf2 = malloc(sizeof(*buf2)); + if (buf2 == NULL) { + free(buf); + h_errno = NETDB_INTERNAL; + return NULL; + } + + memset(&sentinel, 0, sizeof(sentinel)); + cur = &sentinel; + + q->answer = buf->buf; + q->anslen = sizeof(buf->buf); + if (q2) { + q2->answer = buf2->buf; + q2->anslen = sizeof(buf2->buf); + } + + if (dosearch) { + if (res_searchN(q->name, q, res) < 0) + goto out; + } else { + if (res_queryN(q->name, q, res) < 0) + goto out; + } + + ai = getanswer(buf, q->n, q->name, q->qtype, pai); + if (ai) { + cur->ai_next = ai; + while (cur && cur->ai_next) cur = cur->ai_next; - continue; + } + if (q2) { + ai = getanswer(buf2, q2->n, q2->name, q2->qtype, pai); + if (ai) + cur->ai_next = ai; + } + free(buf); + free(buf2); + return sentinel.ai_next; +out: + free(buf); + free(buf2); + return NULL; +} + +/*ARGSUSED*/ +static struct addrinfo * +_dns_srv_lookup(const char *name, const char *servname, + const struct addrinfo *pai) +{ + static const char * const srvprotos[] = { "tcp", "udp" }; + static const int srvnottype[] = { SOCK_DGRAM, SOCK_STREAM }; + static const int nsrvprotos = 2; + struct addrinfo sentinel, *cur, *ai; + struct servent *serv, sv; + struct servent_data svd; + struct res_target q; + res_state res; + char *tname; + int i; - free: - if (cur->ai_next) - freeaddrinfo(cur->ai_next); - cur->ai_next = NULL; - /* continue, without tht pointer CUR advanced. */ + res = __res_get_state(); + if (res == NULL) + return NULL; + + memset(&svd, 0, sizeof(svd)); + memset(&sentinel, 0, sizeof(sentinel)); + cur = &sentinel; + + /* + * Iterate over supported SRV protocols. + * (currently UDP and TCP only) + */ + for (i = 0; i < nsrvprotos; i++) { + /* + * Check that the caller didn't specify a hint + * which precludes this protocol. + */ + if (pai->ai_socktype == srvnottype[i]) + continue; + /* + * If the caller specified a port, + * then lookup the database for the + * official service name. + */ + serv = getservbyname_r(servname, srvprotos[i], &sv, &svd); + if (serv == NULL) + continue; + + /* + * Construct service DNS name. + */ + if (asprintf(&tname, "_%s._%s.%s", serv->s_name, serv->s_proto, + name) < 0) + continue; + + memset(&q, 0, sizeof(q)); + q.name = tname; + q.qclass = C_IN; + q.qtype = T_SRV; + + /* + * Do SRV query. + */ + ai = _dns_query(&q, pai, res, 1); + if (ai) { + cur->ai_next = ai; + while (cur && cur->ai_next) + cur = cur->ai_next; + } + free(tname); } - return(sentinel.ai_next); + if (res->nsort) + aisort(&sentinel, res); + + __res_put_state(res); + + return sentinel.ai_next; } -struct addrinfo * -addr2addrinfo(pai, cp) +/*ARGSUSED*/ +static struct addrinfo * +_dns_host_lookup(const char *name, const struct addrinfo *pai) +{ + struct res_target q, q2; + struct addrinfo sentinel, *ai; + res_state res; + + res = __res_get_state(); + if (res == NULL) + return NULL; + + memset(&q, 0, sizeof(q2)); + memset(&q2, 0, sizeof(q2)); + + switch (pai->ai_family) { + case AF_UNSPEC: + /* prefer IPv6 */ + q.name = name; + q.qclass = C_IN; + q.qtype = T_AAAA; + q.next = &q2; + q2.name = name; + q2.qclass = C_IN; + q2.qtype = T_A; + break; + case AF_INET: + q.name = name; + q.qclass = C_IN; + q.qtype = T_A; + break; + case AF_INET6: + q.name = name; + q.qclass = C_IN; + q.qtype = T_AAAA; + break; + default: + __res_put_state(res); + h_errno = NETDB_INTERNAL; + return NULL; + } + + ai = _dns_query(&q, pai, res, 1); + + memset(&sentinel, 0, sizeof(sentinel)); + sentinel.ai_next = ai; + + if (ai != NULL && res->nsort) + aisort(&sentinel, res); + + __res_put_state(res); + + return sentinel.ai_next; +} + +/*ARGSUSED*/ +static int +_dns_getaddrinfo(void *rv, void *cb_data, va_list ap) +{ + struct addrinfo *ai = NULL; + const char *name, *servname; const struct addrinfo *pai; - const char *cp; + + name = va_arg(ap, char *); + pai = va_arg(ap, const struct addrinfo *); + servname = va_arg(ap, char *); + + /* + * Try doing SRV lookup on service first. + */ + if (servname +#ifdef AI_SRV + && (pai->ai_flags & AI_SRV) +#endif + && !(pai->ai_flags & AI_NUMERICSERV) + && str2number(servname) == -1) { + +#ifdef DNS_DEBUG + printf("%s: try SRV lookup\n", __func__); +#endif + ai = _dns_srv_lookup(name, servname, pai); + } + + /* + * Do lookup on name. + */ + if (ai == NULL) { + +#ifdef DNS_DEBUG + printf("%s: try HOST lookup\n", __func__); +#endif + ai = _dns_host_lookup(name, pai); + + if (ai == NULL) { + switch (h_errno) { + case HOST_NOT_FOUND: + return NS_NOTFOUND; + case TRY_AGAIN: + return NS_TRYAGAIN; + default: + return NS_UNAVAIL; + } + } + } + + *((struct addrinfo **)rv) = ai; + return NS_SUCCESS; +} + +static void +_sethtent(FILE **hostf) { - const struct afd *afd; - afd = find_afd(pai->ai_family); - if (afd == NULL) - return(NULL); + if (!*hostf) + *hostf = fopen(_PATH_HOSTS, "re"); + else + rewind(*hostf); +} + +static void +_endhtent(FILE **hostf) +{ - return(get_ai(pai, afd, cp)); + if (*hostf) { + (void) fclose(*hostf); + *hostf = NULL; + } } -static struct net_data * -init() +static struct addrinfo * +_gethtent(FILE **hostf, const char *name, const struct addrinfo *pai) { - struct net_data *net_data; + char *p; + char *cp, *tname, *cname; + struct addrinfo hints, *res0, *res; + int error; + const char *addr; + char hostbuf[8*1024]; - if (!(net_data = net_data_init(NULL))) - goto error; - if (!net_data->ho) { - net_data->ho = (*net_data->irs->ho_map)(net_data->irs); - if (!net_data->ho || !net_data->res) { -error: - errno = EIO; - if (net_data && net_data->res) - RES_SET_H_ERRNO(net_data->res, NETDB_INTERNAL); + _DIAGASSERT(name != NULL); + _DIAGASSERT(pai != NULL); + + if (!*hostf && !(*hostf = fopen(_PATH_HOSTS, "re"))) + return (NULL); + again: + if (!(p = fgets(hostbuf, (int)sizeof hostbuf, *hostf))) + return (NULL); + if (*p == '#') + goto again; + if (!(cp = strpbrk(p, "#\n"))) + goto again; + *cp = '\0'; + if (!(cp = strpbrk(p, " \t"))) + goto again; + *cp++ = '\0'; + addr = p; + /* if this is not something we're looking for, skip it. */ + cname = NULL; + while (cp && *cp) { + if (*cp == ' ' || *cp == '\t') { + cp++; + continue; + } + if (!cname) + cname = cp; + tname = cp; + if ((cp = strpbrk(cp, " \t")) != NULL) + *cp++ = '\0'; + if (strcasecmp(name, tname) == 0) + goto found; + } + goto again; + +found: + hints = *pai; + hints.ai_flags = AI_NUMERICHOST; + error = getaddrinfo(addr, NULL, &hints, &res0); + if (error) + goto again; + for (res = res0; res; res = res->ai_next) { + /* cover it up */ + res->ai_flags = pai->ai_flags; + + if (pai->ai_flags & AI_CANONNAME) { + if (get_canonname(pai, res, cname) != 0) { + freeaddrinfo(res0); + goto again; + } + } + } + return res0; +} + +/*ARGSUSED*/ +static int +_files_getaddrinfo(void *rv, void *cb_data, va_list ap) +{ + const char *name; + const struct addrinfo *pai; + struct addrinfo sentinel, *cur; + struct addrinfo *p; +#ifndef _REENTRANT + static +#endif + FILE *hostf = NULL; + + name = va_arg(ap, char *); + pai = va_arg(ap, const struct addrinfo *); + + memset(&sentinel, 0, sizeof(sentinel)); + cur = &sentinel; + + _sethtent(&hostf); + while ((p = _gethtent(&hostf, name, pai)) != NULL) { + cur->ai_next = p; + while (cur && cur->ai_next) + cur = cur->ai_next; + } + _endhtent(&hostf); + + *((struct addrinfo **)rv) = sentinel.ai_next; + if (sentinel.ai_next == NULL) + return NS_NOTFOUND; + return NS_SUCCESS; +} + +#ifdef YP +/*ARGSUSED*/ +static struct addrinfo * +_yphostent(char *line, const struct addrinfo *pai) +{ + struct addrinfo sentinel, *cur; + struct addrinfo hints, *res, *res0; + int error; + char *p; + const char *addr, *canonname; + char *nextline; + char *cp; + + _DIAGASSERT(line != NULL); + _DIAGASSERT(pai != NULL); + + p = line; + addr = canonname = NULL; + + memset(&sentinel, 0, sizeof(sentinel)); + cur = &sentinel; + +nextline: + /* terminate line */ + cp = strchr(p, '\n'); + if (cp) { + *cp++ = '\0'; + nextline = cp; + } else + nextline = NULL; + + cp = strpbrk(p, " \t"); + if (cp == NULL) { + if (canonname == NULL) return (NULL); + else + goto done; + } + *cp++ = '\0'; + + addr = p; + + while (cp && *cp) { + if (*cp == ' ' || *cp == '\t') { + cp++; + continue; + } + if (!canonname) + canonname = cp; + if ((cp = strpbrk(cp, " \t")) != NULL) + *cp++ = '\0'; + } + + hints = *pai; + hints.ai_flags = AI_NUMERICHOST; + error = getaddrinfo(addr, NULL, &hints, &res0); + if (error == 0) { + for (res = res0; res; res = res->ai_next) { + /* cover it up */ + res->ai_flags = pai->ai_flags; + + if (pai->ai_flags & AI_CANONNAME) + (void)get_canonname(pai, res, canonname); + } + } else + res0 = NULL; + if (res0) { + cur->ai_next = res0; + while (cur->ai_next) + cur = cur->ai_next; + } + + if (nextline) { + p = nextline; + goto nextline; + } + +done: + return sentinel.ai_next; +} + +/*ARGSUSED*/ +static int +_yp_getaddrinfo(void *rv, void *cb_data, va_list ap) +{ + struct addrinfo sentinel, *cur; + struct addrinfo *ai = NULL; + char *ypbuf; + int ypbuflen, r; + const char *name; + const struct addrinfo *pai; + char *ypdomain; + + if (_yp_check(&ypdomain) == 0) + return NS_UNAVAIL; + + name = va_arg(ap, char *); + pai = va_arg(ap, const struct addrinfo *); + + memset(&sentinel, 0, sizeof(sentinel)); + cur = &sentinel; + + /* hosts.byname is only for IPv4 (Solaris8) */ + if (pai->ai_family == PF_UNSPEC || pai->ai_family == PF_INET) { + r = yp_match(ypdomain, "hosts.byname", name, + (int)strlen(name), &ypbuf, &ypbuflen); + if (r == 0) { + struct addrinfo ai4; + + ai4 = *pai; + ai4.ai_family = AF_INET; + ai = _yphostent(ypbuf, &ai4); + if (ai) { + cur->ai_next = ai; + while (cur && cur->ai_next) + cur = cur->ai_next; + } + } + free(ypbuf); + } + + /* ipnodes.byname can hold both IPv4/v6 */ + r = yp_match(ypdomain, "ipnodes.byname", name, + (int)strlen(name), &ypbuf, &ypbuflen); + if (r == 0) { + ai = _yphostent(ypbuf, pai); + if (ai) + cur->ai_next = ai; + free(ypbuf); + } + + if (sentinel.ai_next == NULL) { + h_errno = HOST_NOT_FOUND; + return NS_NOTFOUND; + } + *((struct addrinfo **)rv) = sentinel.ai_next; + return NS_SUCCESS; +} +#endif + +/* resolver logic */ + +/* + * Formulate a normal query, send, and await answer. + * Returned answer is placed in supplied buffer "answer". + * Perform preliminary check of answer, returning success only + * if no error is indicated and the answer count is nonzero. + * Return the size of the response on success, -1 on error. + * Error number is left in h_errno. + * + * Caller must parse answer and determine whether it answers the question. + */ +static int +res_queryN(const char *name, /* domain name */ struct res_target *target, + res_state res) +{ + u_char buf[MAXPACKET]; + HEADER *hp; + int n; + struct res_target *t; + int rcode; + int ancount; + + _DIAGASSERT(name != NULL); + /* XXX: target may be NULL??? */ + + rcode = NOERROR; + ancount = 0; + + for (t = target; t; t = t->next) { + int class, type; + u_char *answer; + int anslen; + + hp = (HEADER *)(void *)t->answer; + hp->rcode = NOERROR; /* default */ + + /* make it easier... */ + class = t->qclass; + type = t->qtype; + answer = t->answer; + anslen = t->anslen; +#ifdef DEBUG + if (res->options & RES_DEBUG) + printf(";; res_nquery(%s, %d, %d)\n", name, class, type); +#endif + + n = res_nmkquery(res, QUERY, name, class, type, NULL, 0, NULL, + buf, (int)sizeof(buf)); +#ifdef RES_USE_EDNS0 + if (n > 0 && (res->options & RES_USE_EDNS0) != 0) + n = res_nopt(res, n, buf, (int)sizeof(buf), anslen); +#endif + if (n <= 0) { +#ifdef DEBUG + if (res->options & RES_DEBUG) + printf(";; res_nquery: mkquery failed\n"); +#endif + h_errno = NO_RECOVERY; + return n; + } + n = res_nsend(res, buf, n, answer, anslen); +#if 0 + if (n < 0) { +#ifdef DEBUG + if (res->options & RES_DEBUG) + printf(";; res_query: send error\n"); +#endif + h_errno = TRY_AGAIN; + return n; } +#endif - (*net_data->ho->res_set)(net_data->ho, net_data->res, NULL); + if (n < 0 || hp->rcode != NOERROR || ntohs(hp->ancount) == 0) { + rcode = hp->rcode; /* record most recent error */ +#ifdef DEBUG + if (res->options & RES_DEBUG) + printf(";; rcode = %u, ancount=%u\n", hp->rcode, + ntohs(hp->ancount)); +#endif + continue; + } + + ancount += ntohs(hp->ancount); + + t->n = n; } - return (net_data); + if (ancount == 0) { + switch (rcode) { + case NXDOMAIN: + h_errno = HOST_NOT_FOUND; + break; + case SERVFAIL: + h_errno = TRY_AGAIN; + break; + case NOERROR: + h_errno = NO_DATA; + break; + case FORMERR: + case NOTIMP: + case REFUSED: + default: + h_errno = NO_RECOVERY; + break; + } + return -1; + } + return ancount; } + +/* + * Formulate a normal query, send, and retrieve answer in supplied buffer. + * Return the size of the response on success, -1 on error. + * If enabled, implement search rules until answer or unrecoverable failure + * is detected. Error code, if any, is left in h_errno. + */ +static int +res_searchN(const char *name, struct res_target *target, res_state res) +{ + const char *cp, * const *domain; + HEADER *hp; + u_int dots; + int trailing_dot, ret, saved_herrno; + int got_nodata = 0, got_servfail = 0, tried_as_is = 0; + + _DIAGASSERT(name != NULL); + _DIAGASSERT(target != NULL); + + hp = (HEADER *)(void *)target->answer; /*XXX*/ + + errno = 0; + h_errno = HOST_NOT_FOUND; /* default, if we never query */ + dots = 0; + for (cp = name; *cp; cp++) + dots += (*cp == '.'); + trailing_dot = 0; + if (cp > name && *--cp == '.') + trailing_dot++; + + /* + * if there aren't any dots, it could be a user-level alias + */ + if (!dots && (cp = __hostalias(name)) != NULL) { + ret = res_queryN(cp, target, res); + return ret; + } + + /* + * If there are dots in the name already, let's just give it a try + * 'as is'. The threshold can be set with the "ndots" option. + */ + saved_herrno = -1; + if (dots >= res->ndots) { + ret = res_querydomainN(name, NULL, target, res); + if (ret > 0) + return (ret); + saved_herrno = h_errno; + tried_as_is++; + } + + /* + * We do at least one level of search if + * - there is no dot and RES_DEFNAME is set, or + * - there is at least one dot, there is no trailing dot, + * and RES_DNSRCH is set. + */ + if ((!dots && (res->options & RES_DEFNAMES)) || + (dots && !trailing_dot && (res->options & RES_DNSRCH))) { + int done = 0; + + for (domain = (const char * const *)res->dnsrch; + *domain && !done; + domain++) { + + ret = res_querydomainN(name, *domain, target, res); + if (ret > 0) + return ret; + + /* + * If no server present, give up. + * If name isn't found in this domain, + * keep trying higher domains in the search list + * (if that's enabled). + * On a NO_DATA error, keep trying, otherwise + * a wildcard entry of another type could keep us + * from finding this entry higher in the domain. + * If we get some other error (negative answer or + * server failure), then stop searching up, + * but try the input name below in case it's + * fully-qualified. + */ + if (errno == ECONNREFUSED) { + h_errno = TRY_AGAIN; + return -1; + } + + switch (h_errno) { + case NO_DATA: + got_nodata++; + /* FALLTHROUGH */ + case HOST_NOT_FOUND: + /* keep trying */ + break; + case TRY_AGAIN: + if (hp->rcode == SERVFAIL) { + /* try next search element, if any */ + got_servfail++; + break; + } + /* FALLTHROUGH */ + default: + /* anything else implies that we're done */ + done++; + } + /* + * if we got here for some reason other than DNSRCH, + * we only wanted one iteration of the loop, so stop. + */ + if (!(res->options & RES_DNSRCH)) + done++; + } + } + + /* + * if we have not already tried the name "as is", do that now. + * note that we do this regardless of how many dots were in the + * name or whether it ends with a dot. + */ + if (!tried_as_is) { + ret = res_querydomainN(name, NULL, target, res); + if (ret > 0) + return ret; + } + + /* + * if we got here, we didn't satisfy the search. + * if we did an initial full query, return that query's h_errno + * (note that we wouldn't be here if that query had succeeded). + * else if we ever got a nodata, send that back as the reason. + * else send back meaningless h_errno, that being the one from + * the last DNSRCH we did. + */ + if (saved_herrno != -1) + h_errno = saved_herrno; + else if (got_nodata) + h_errno = NO_DATA; + else if (got_servfail) + h_errno = TRY_AGAIN; + return -1; +} + +/* + * Perform a call on res_query on the concatenation of name and domain, + * removing a trailing dot from name if domain is NULL. + */ +static int +res_querydomainN(const char *name, const char *domain, + struct res_target *target, res_state res) +{ + char nbuf[MAXDNAME]; + const char *longname = nbuf; + size_t n, d; + + _DIAGASSERT(name != NULL); + /* XXX: target may be NULL??? */ + +#ifdef DEBUG + if (res->options & RES_DEBUG) + printf(";; res_querydomain(%s, %s)\n", + name, domain?domain:""); +#endif + if (domain == NULL) { + /* + * Check for trailing '.'; + * copy without '.' if present. + */ + n = strlen(name); + if (n + 1 > sizeof(nbuf)) { + h_errno = NO_RECOVERY; + return -1; + } + if (n > 0 && name[--n] == '.') { + strncpy(nbuf, name, n); + nbuf[n] = '\0'; + } else + longname = name; + } else { + n = strlen(name); + d = strlen(domain); + if (n + 1 + d + 1 > sizeof(nbuf)) { + h_errno = NO_RECOVERY; + return -1; + } + snprintf(nbuf, sizeof(nbuf), "%s.%s", name, domain); + } + return res_queryN(longname, target, res); +} + +#ifdef TEST +int +main(int argc, char *argv[]) { + struct addrinfo *ai, *sai; + int i, e; + char buf[1024]; + + for (i = 1; i < argc; i++) { + if ((e = getaddrinfo(argv[i], NULL, NULL, &sai)) != 0) + warnx("%s: %s", argv[i], gai_strerror(e)); + for (ai = sai; ai; ai = ai->ai_next) { + sockaddr_snprintf(buf, sizeof(buf), "%a", ai->ai_addr); + printf("flags=0x%x family=%d socktype=%d protocol=%d " + "addrlen=%zu addr=%s canonname=%s next=%p\n", + ai->ai_flags, + ai->ai_family, + ai->ai_socktype, + ai->ai_protocol, + (size_t)ai->ai_addrlen, + buf, + ai->ai_canonname, + ai->ai_next); + } + if (sai) + freeaddrinfo(sai); + } + return 0; +} +#endif diff -pruN /home/reed/src/isc/libbind/libbind/irs/getgrent.c /usr/src/lib/libc/net/getgrent.c --- /home/reed/src/isc/libbind/libbind/irs/getgrent.c 2005-04-26 23:56:24.000000000 -0500 +++ /usr/src/lib/libc/net/getgrent.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,224 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1996-1999 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#if !defined(LINT) && !defined(CODECENTER) -static const char rcsid[] = "$Id: getgrent.c,v 1.5 2005/04/27 04:56:24 sra Exp $"; -#endif - -/* Imports */ - -#include "port_before.h" - -#if !defined(WANT_IRS_GR) || defined(__BIND_NOSTATIC) -static int __bind_irs_gr_unneeded; -#else - -#include - -#include -#include - -#include -#include -#include -#include -#include -#include - -#include - -#include "port_after.h" - -#include "irs_data.h" - -/* Forward */ - -static struct net_data *init(void); -void endgrent(void); - -/* Public */ - -struct group * -getgrent() { - struct net_data *net_data = init(); - - return (getgrent_p(net_data)); -} - -struct group * -getgrnam(const char *name) { - struct net_data *net_data = init(); - - return (getgrnam_p(name, net_data)); -} - -struct group * -getgrgid(gid_t gid) { - struct net_data *net_data = init(); - - return (getgrgid_p(gid, net_data)); -} - -int -setgroupent(int stayopen) { - struct net_data *net_data = init(); - - return (setgroupent_p(stayopen, net_data)); -} - -#ifdef SETGRENT_VOID -void -setgrent(void) { - struct net_data *net_data = init(); - - setgrent_p(net_data); -} -#else -int -setgrent(void) { - struct net_data *net_data = init(); - - return (setgrent_p(net_data)); -} -#endif /* SETGRENT_VOID */ - -void -endgrent() { - struct net_data *net_data = init(); - - endgrent_p(net_data); -} - -int -getgrouplist(GETGROUPLIST_ARGS) { - struct net_data *net_data = init(); - - return (getgrouplist_p(name, basegid, groups, ngroups, net_data)); -} - -/* Shared private. */ - -struct group * -getgrent_p(struct net_data *net_data) { - struct irs_gr *gr; - - if (!net_data || !(gr = net_data->gr)) - return (NULL); - net_data->gr_last = (*gr->next)(gr); - return (net_data->gr_last); -} - -struct group * -getgrnam_p(const char *name, struct net_data *net_data) { - struct irs_gr *gr; - - if (!net_data || !(gr = net_data->gr)) - return (NULL); - if (net_data->gr_stayopen && net_data->gr_last && - !strcmp(net_data->gr_last->gr_name, name)) - return (net_data->gr_last); - net_data->gr_last = (*gr->byname)(gr, name); - if (!net_data->gr_stayopen) - endgrent(); - return (net_data->gr_last); -} - -struct group * -getgrgid_p(gid_t gid, struct net_data *net_data) { - struct irs_gr *gr; - - if (!net_data || !(gr = net_data->gr)) - return (NULL); - if (net_data->gr_stayopen && net_data->gr_last && - (gid_t)net_data->gr_last->gr_gid == gid) - return (net_data->gr_last); - net_data->gr_last = (*gr->bygid)(gr, gid); - if (!net_data->gr_stayopen) - endgrent(); - return (net_data->gr_last); -} - -int -setgroupent_p(int stayopen, struct net_data *net_data) { - struct irs_gr *gr; - - if (!net_data || !(gr = net_data->gr)) - return (0); - (*gr->rewind)(gr); - net_data->gr_stayopen = (stayopen != 0); - if (stayopen == 0) - net_data_minimize(net_data); - return (1); -} - -#ifdef SETGRENT_VOID -void -setgrent_p(struct net_data *net_data) { - (void)setgroupent_p(0, net_data); -} -#else -int -setgrent_p(struct net_data *net_data) { - return (setgroupent_p(0, net_data)); -} -#endif /* SETGRENT_VOID */ - -void -endgrent_p(struct net_data *net_data) { - struct irs_gr *gr; - - if ((net_data != NULL) && ((gr = net_data->gr) != NULL)) - (*gr->minimize)(gr); -} - -int -getgrouplist_p(const char *name, gid_t basegid, gid_t *groups, int *ngroups, - struct net_data *net_data) { - struct irs_gr *gr; - - if (!net_data || !(gr = net_data->gr)) { - *ngroups = 0; - return (-1); - } - return ((*gr->list)(gr, name, basegid, groups, ngroups)); -} - -/* Private */ - -static struct net_data * -init() { - struct net_data *net_data; - - if (!(net_data = net_data_init(NULL))) - goto error; - if (!net_data->gr) { - net_data->gr = (*net_data->irs->gr_map)(net_data->irs); - - if (!net_data->gr || !net_data->res) { - error: - errno = EIO; - return (NULL); - } - (*net_data->gr->res_set)(net_data->gr, net_data->res, - NULL); - } - - return (net_data); -} - -#endif /* WANT_IRS_GR */ -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/getgrent_r.c /usr/src/lib/libc/net/getgrent_r.c --- /home/reed/src/isc/libbind/libbind/irs/getgrent_r.c 2005-04-26 23:56:24.000000000 -0500 +++ /usr/src/lib/libc/net/getgrent_r.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,230 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1998-1999 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#if defined(LIBC_SCCS) && !defined(lint) -static const char rcsid[] = "$Id: getgrent_r.c,v 1.7 2005/04/27 04:56:24 sra Exp $"; -#endif /* LIBC_SCCS and not lint */ - -#include -#if !defined(_REENTRANT) || !defined(DO_PTHREADS) || !defined(WANT_IRS_PW) - static int getgrent_r_not_required = 0; -#else -#include -#include -#include -#include -#if (defined(POSIX_GETGRNAM_R) || defined(POSIX_GETGRGID_R)) && \ - defined(_POSIX_PTHREAD_SEMANTICS) - /* turn off solaris remapping in */ -#define _UNIX95 -#undef _POSIX_PTHREAD_SEMANTICS -#include -#define _POSIX_PTHREAD_SEMANTICS 1 -#else -#include -#endif -#include -#include - -#ifdef GROUP_R_RETURN - -static int -copy_group(struct group *, struct group *, char *buf, int buflen); - -/* POSIX 1003.1c */ -#ifdef POSIX_GETGRNAM_R -int -__posix_getgrnam_r(const char *name, struct group *gptr, - char *buf, int buflen, struct group **result) { -#else -int -getgrnam_r(const char *name, struct group *gptr, - char *buf, size_t buflen, struct group **result) { -#endif - struct group *ge = getgrnam(name); - int res; - - if (ge == NULL) { - *result = NULL; - return (0); - } - - res = copy_group(ge, gptr, buf, buflen); - *result = res ? NULL : gptr; - return (res); -} - -#ifdef POSIX_GETGRNAM_R -struct group * -getgrnam_r(const char *name, struct group *gptr, - char *buf, int buflen) { - struct group *ge = getgrnam(name); - int res; - - if (ge == NULL) - return (NULL); - res = copy_group(ge, gptr, buf, buflen); - return (res ? NULL : gptr); -} -#endif /* POSIX_GETGRNAM_R */ - -/* POSIX 1003.1c */ -#ifdef POSIX_GETGRGID_R -int -__posix_getgrgid_r(gid_t gid, struct group *gptr, - char *buf, int buflen, struct group **result) { -#else /* POSIX_GETGRGID_R */ -int -getgrgid_r(gid_t gid, struct group *gptr, - char *buf, size_t buflen, struct group **result) { -#endif /* POSIX_GETGRGID_R */ - struct group *ge = getgrgid(gid); - int res; - - if (ge == NULL) { - *result = NULL; - return (0); - } - - res = copy_group(ge, gptr, buf, buflen); - *result = res ? NULL : gptr; - return (res); -} - -#ifdef POSIX_GETGRGID_R -struct group * -getgrgid_r(gid_t gid, struct group *gptr, - char *buf, int buflen) { - struct group *ge = getgrgid(gid); - int res; - - if (ge == NULL) - return (NULL); - - res = copy_group(ge, gptr, buf, buflen); - return (res ? NULL : gptr); -} -#endif - -/*% - * These assume a single context is in operation per thread. - * If this is not the case we will need to call irs directly - * rather than through the base functions. - */ - -GROUP_R_RETURN -getgrent_r(struct group *gptr, GROUP_R_ARGS) { - struct group *ge = getgrent(); - int res; - - if (ge == NULL) { - return (GROUP_R_BAD); - } - - res = copy_group(ge, gptr, buf, buflen); - return (res ? GROUP_R_BAD : GROUP_R_OK); -} - -GROUP_R_SET_RETURN -setgrent_r(GROUP_R_ENT_ARGS) { - - setgrent(); -#ifdef GROUP_R_SET_RESULT - return (GROUP_R_SET_RESULT); -#endif -} - -GROUP_R_END_RETURN -endgrent_r(GROUP_R_ENT_ARGS) { - - endgrent(); - GROUP_R_END_RESULT(GROUP_R_OK); -} - - -#if 0 - /* XXX irs does not have a fgetgrent() */ -GROUP_R_RETURN -fgetgrent_r(FILE *f, struct group *gptr, GROUP_R_ARGS) { - struct group *ge = fgetgrent(f); - int res; - - if (ge == NULL) - return (GROUP_R_BAD); - - res = copy_group(ge, gptr, buf, buflen); - return (res ? GROUP_R_BAD : GROUP_R_OK); -} -#endif - -/* Private */ - -static int -copy_group(struct group *ge, struct group *gptr, char *buf, int buflen) { - char *cp; - int i, n; - int numptr, len; - - /* Find out the amount of space required to store the answer. */ - numptr = 1; /*%< NULL ptr */ - len = (char *)ALIGN(buf) - buf; - for (i = 0; ge->gr_mem[i]; i++, numptr++) { - len += strlen(ge->gr_mem[i]) + 1; - } - len += strlen(ge->gr_name) + 1; - len += strlen(ge->gr_passwd) + 1; - len += numptr * sizeof(char*); - - if (len > buflen) { - errno = ERANGE; - return (ERANGE); - } - - /* copy group id */ - gptr->gr_gid = ge->gr_gid; - - cp = (char *)ALIGN(buf) + numptr * sizeof(char *); - - /* copy official name */ - n = strlen(ge->gr_name) + 1; - strcpy(cp, ge->gr_name); - gptr->gr_name = cp; - cp += n; - - /* copy member list */ - gptr->gr_mem = (char **)ALIGN(buf); - for (i = 0 ; ge->gr_mem[i]; i++) { - n = strlen(ge->gr_mem[i]) + 1; - strcpy(cp, ge->gr_mem[i]); - gptr->gr_mem[i] = cp; - cp += n; - } - gptr->gr_mem[i] = NULL; - - /* copy password */ - n = strlen(ge->gr_passwd) + 1; - strcpy(cp, ge->gr_passwd); - gptr->gr_passwd = cp; - cp += n; - - return (0); -} -#else /* GROUP_R_RETURN */ - static int getgrent_r_unknown_system = 0; -#endif /* GROUP_R_RETURN */ -#endif /* !def(_REENTRANT) || !def(DO_PTHREADS) || !def(WANT_IRS_PW) */ -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/gethnamaddr.c /usr/src/lib/libc/net/gethnamaddr.c --- /home/reed/src/isc/libbind/libbind/irs/gethnamaddr.c 1969-12-31 18:00:00.000000000 -0600 +++ /usr/src/lib/libc/net/gethnamaddr.c 2013-06-05 09:26:12.000000000 -0500 @@ -0,0 +1,1448 @@ +/* $NetBSD: gethnamaddr.c,v 1.79 2012/09/09 16:42:23 christos Exp $ */ + +/* + * ++Copyright++ 1985, 1988, 1993 + * - + * Copyright (c) 1985, 1988, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * - + * Portions Copyright (c) 1993 by Digital Equipment Corporation. + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies, and that + * the name of Digital Equipment Corporation not be used in advertising or + * publicity pertaining to distribution of the document or software without + * specific, written prior permission. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL + * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT + * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL + * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR + * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS + * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS + * SOFTWARE. + * - + * --Copyright-- + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)gethostnamadr.c 8.1 (Berkeley) 6/4/93"; +static char rcsid[] = "Id: gethnamaddr.c,v 8.21 1997/06/01 20:34:37 vixie Exp "; +#else +__RCSID("$NetBSD: gethnamaddr.c,v 1.79 2012/09/09 16:42:23 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#if defined(_LIBC) +#include "namespace.h" +#endif +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#ifndef LOG_AUTH +# define LOG_AUTH 0 +#endif + +#define MULTI_PTRS_ARE_ALIASES 1 /* XXX - experimental */ + +#include +#include +#include + +#ifdef YP +#include +#include +#include +#endif + +#if defined(_LIBC) && defined(__weak_alias) +__weak_alias(gethostbyaddr,_gethostbyaddr) +__weak_alias(gethostbyname,_gethostbyname) +__weak_alias(gethostent,_gethostent) +#endif + +#define maybe_ok(res, nm, ok) (((res)->options & RES_NOCHECKNAME) != 0U || \ + (ok)(nm) != 0) +#define maybe_hnok(res, hn) maybe_ok((res), (hn), res_hnok) +#define maybe_dnok(res, dn) maybe_ok((res), (dn), res_dnok) + + +#define MAXALIASES 35 +#define MAXADDRS 35 + +static const char AskedForGot[] = + "gethostby*.getanswer: asked for \"%s\", got \"%s\""; + +static char *h_addr_ptrs[MAXADDRS + 1]; + +#ifdef YP +static char *__ypdomain; +#endif + +static struct hostent host; +static char *host_aliases[MAXALIASES]; +static char hostbuf[8*1024]; +static u_int32_t host_addr[16 / sizeof(u_int32_t)]; /* IPv4 or IPv6 */ +static FILE *hostf = NULL; +static int stayopen = 0; + +#define MAXPACKET (64*1024) + +typedef union { + HEADER hdr; + u_char buf[MAXPACKET]; +} querybuf; + +typedef union { + int32_t al; + char ac; +} align; + +#ifdef DEBUG +static void debugprintf(const char *, res_state, ...) + __attribute__((__format__(__printf__, 1, 3))); +#endif +static struct hostent *getanswer(const querybuf *, int, const char *, int, + res_state); +static void map_v4v6_address(const char *, char *); +static void map_v4v6_hostent(struct hostent *, char **, char *); +static void addrsort(char **, int, res_state); + +void _sethtent(int); +void _endhtent(void); +struct hostent *_gethtent(void); +void ht_sethostent(int); +void ht_endhostent(void); +struct hostent *ht_gethostbyname(char *); +struct hostent *ht_gethostbyaddr(const char *, int, int); +void dns_service(void); +#undef dn_skipname +int dn_skipname(const u_char *, const u_char *); +int _gethtbyaddr(void *, void *, va_list); +int _gethtbyname(void *, void *, va_list); +struct hostent *_gethtbyname2(const char *, int); +int _dns_gethtbyaddr(void *, void *, va_list); +int _dns_gethtbyname(void *, void *, va_list); +#ifdef YP +struct hostent *_yphostent(char *, int); +int _yp_gethtbyaddr(void *, void *, va_list); +int _yp_gethtbyname(void *, void *, va_list); +#endif + +static struct hostent *gethostbyname_internal(const char *, int, res_state); + +static const ns_src default_dns_files[] = { + { NSSRC_FILES, NS_SUCCESS }, + { NSSRC_DNS, NS_SUCCESS }, + { 0, 0 } +}; + + +#ifdef DEBUG +static void +debugprintf(const char *msg, res_state res, ...) +{ + _DIAGASSERT(msg != NULL); + + if (res->options & RES_DEBUG) { + int save = errno; + va_list ap; + + va_start (ap, res); + vprintf(msg, ap); + va_end (ap); + + errno = save; + } +} +#else +# define debugprintf(msg, res, num) /*nada*/ +#endif + +#define BOUNDED_INCR(x) \ + do { \ + cp += (x); \ + if (cp > eom) { \ + h_errno = NO_RECOVERY; \ + return NULL; \ + } \ + } while (/*CONSTCOND*/0) + +#define BOUNDS_CHECK(ptr, count) \ + do { \ + if ((ptr) + (count) > eom) { \ + h_errno = NO_RECOVERY; \ + return NULL; \ + } \ + } while (/*CONSTCOND*/0) + +static struct hostent * +getanswer(const querybuf *answer, int anslen, const char *qname, int qtype, + res_state res) +{ + const HEADER *hp; + const u_char *cp; + int n; + const u_char *eom, *erdata; + char *bp, **ap, **hap, *ep; + int type, class, ancount, qdcount; + int haveanswer, had_error; + int toobig = 0; + char tbuf[MAXDNAME]; + const char *tname; + int (*name_ok)(const char *); + + _DIAGASSERT(answer != NULL); + _DIAGASSERT(qname != NULL); + + tname = qname; + host.h_name = NULL; + eom = answer->buf + anslen; + switch (qtype) { + case T_A: + case T_AAAA: + name_ok = res_hnok; + break; + case T_PTR: + name_ok = res_dnok; + break; + default: + return NULL; /* XXX should be abort(); */ + } + /* + * find first satisfactory answer + */ + hp = &answer->hdr; + ancount = ntohs(hp->ancount); + qdcount = ntohs(hp->qdcount); + bp = hostbuf; + ep = hostbuf + sizeof hostbuf; + cp = answer->buf; + BOUNDED_INCR(HFIXEDSZ); + if (qdcount != 1) { + h_errno = NO_RECOVERY; + return NULL; + } + n = dn_expand(answer->buf, eom, cp, bp, (int)(ep - bp)); + if ((n < 0) || !maybe_ok(res, bp, name_ok)) { + h_errno = NO_RECOVERY; + return NULL; + } + BOUNDED_INCR(n + QFIXEDSZ); + if (qtype == T_A || qtype == T_AAAA) { + /* res_send() has already verified that the query name is the + * same as the one we sent; this just gets the expanded name + * (i.e., with the succeeding search-domain tacked on). + */ + n = (int)strlen(bp) + 1; /* for the \0 */ + if (n >= MAXHOSTNAMELEN) { + h_errno = NO_RECOVERY; + return NULL; + } + host.h_name = bp; + bp += n; + /* The qname can be abbreviated, but h_name is now absolute. */ + qname = host.h_name; + } + ap = host_aliases; + *ap = NULL; + host.h_aliases = host_aliases; + hap = h_addr_ptrs; + *hap = NULL; + host.h_addr_list = h_addr_ptrs; + haveanswer = 0; + had_error = 0; + while (ancount-- > 0 && cp < eom && !had_error) { + n = dn_expand(answer->buf, eom, cp, bp, (int)(ep - bp)); + if ((n < 0) || !maybe_ok(res, bp, name_ok)) { + had_error++; + continue; + } + cp += n; /* name */ + BOUNDS_CHECK(cp, 3 * INT16SZ + INT32SZ); + type = _getshort(cp); + cp += INT16SZ; /* type */ + class = _getshort(cp); + cp += INT16SZ + INT32SZ; /* class, TTL */ + n = _getshort(cp); + cp += INT16SZ; /* len */ + BOUNDS_CHECK(cp, n); + erdata = cp + n; + if (class != C_IN) { + /* XXX - debug? syslog? */ + cp += n; + continue; /* XXX - had_error++ ? */ + } + if ((qtype == T_A || qtype == T_AAAA) && type == T_CNAME) { + if (ap >= &host_aliases[MAXALIASES-1]) + continue; + n = dn_expand(answer->buf, eom, cp, tbuf, (int)sizeof tbuf); + if ((n < 0) || !maybe_ok(res, tbuf, name_ok)) { + had_error++; + continue; + } + cp += n; + if (cp != erdata) { + h_errno = NO_RECOVERY; + return NULL; + } + /* Store alias. */ + *ap++ = bp; + n = (int)strlen(bp) + 1; /* for the \0 */ + if (n >= MAXHOSTNAMELEN) { + had_error++; + continue; + } + bp += n; + /* Get canonical name. */ + n = (int)strlen(tbuf) + 1; /* for the \0 */ + if (n > ep - bp || n >= MAXHOSTNAMELEN) { + had_error++; + continue; + } + strlcpy(bp, tbuf, (size_t)(ep - bp)); + host.h_name = bp; + bp += n; + continue; + } + if (qtype == T_PTR && type == T_CNAME) { + n = dn_expand(answer->buf, eom, cp, tbuf, (int)sizeof tbuf); + if (n < 0 || !maybe_dnok(res, tbuf)) { + had_error++; + continue; + } + cp += n; + if (cp != erdata) { + h_errno = NO_RECOVERY; + return NULL; + } + /* Get canonical name. */ + n = (int)strlen(tbuf) + 1; /* for the \0 */ + if (n > ep - bp || n >= MAXHOSTNAMELEN) { + had_error++; + continue; + } + strlcpy(bp, tbuf, (size_t)(ep - bp)); + tname = bp; + bp += n; + continue; + } + if (type != qtype) { + if (type != T_KEY && type != T_SIG) + syslog(LOG_NOTICE|LOG_AUTH, + "gethostby*.getanswer: asked for \"%s %s %s\", got type \"%s\"", + qname, p_class(C_IN), p_type(qtype), + p_type(type)); + cp += n; + continue; /* XXX - had_error++ ? */ + } + switch (type) { + case T_PTR: + if (strcasecmp(tname, bp) != 0) { + syslog(LOG_NOTICE|LOG_AUTH, + AskedForGot, qname, bp); + cp += n; + continue; /* XXX - had_error++ ? */ + } + n = dn_expand(answer->buf, eom, cp, bp, (int)(ep - bp)); + if ((n < 0) || !maybe_hnok(res, bp)) { + had_error++; + break; + } +#if MULTI_PTRS_ARE_ALIASES + cp += n; + if (cp != erdata) { + h_errno = NO_RECOVERY; + return NULL; + } + if (!haveanswer) + host.h_name = bp; + else if (ap < &host_aliases[MAXALIASES-1]) + *ap++ = bp; + else + n = -1; + if (n != -1) { + n = (int)strlen(bp) + 1; /* for the \0 */ + if (n >= MAXHOSTNAMELEN) { + had_error++; + break; + } + bp += n; + } + break; +#else + host.h_name = bp; + if (res->options & RES_USE_INET6) { + n = strlen(bp) + 1; /* for the \0 */ + if (n >= MAXHOSTNAMELEN) { + had_error++; + break; + } + bp += n; + map_v4v6_hostent(&host, &bp, ep); + } + h_errno = NETDB_SUCCESS; + return &host; +#endif + case T_A: + case T_AAAA: + if (strcasecmp(host.h_name, bp) != 0) { + syslog(LOG_NOTICE|LOG_AUTH, + AskedForGot, host.h_name, bp); + cp += n; + continue; /* XXX - had_error++ ? */ + } + if (n != host.h_length) { + cp += n; + continue; + } + if (type == T_AAAA) { + struct in6_addr in6; + memcpy(&in6, cp, IN6ADDRSZ); + if (IN6_IS_ADDR_V4MAPPED(&in6)) { + cp += n; + continue; + } + } + if (!haveanswer) { + int nn; + + host.h_name = bp; + nn = (int)strlen(bp) + 1; /* for the \0 */ + bp += nn; + } + + bp += sizeof(align) - + (size_t)((u_long)bp % sizeof(align)); + + if (bp + n >= &hostbuf[sizeof hostbuf]) { + debugprintf("size (%d) too big\n", res, n); + had_error++; + continue; + } + if (hap >= &h_addr_ptrs[MAXADDRS-1]) { + if (!toobig++) { + debugprintf("Too many addresses (%d)\n", + res, MAXADDRS); + } + cp += n; + continue; + } + (void)memcpy(*hap++ = bp, cp, (size_t)n); + bp += n; + cp += n; + if (cp != erdata) { + h_errno = NO_RECOVERY; + return NULL; + } + break; + default: + abort(); + } + if (!had_error) + haveanswer++; + } + if (haveanswer) { + *ap = NULL; + *hap = NULL; + /* + * Note: we sort even if host can take only one address + * in its return structures - should give it the "best" + * address in that case, not some random one + */ + if (res->nsort && haveanswer > 1 && qtype == T_A) + addrsort(h_addr_ptrs, haveanswer, res); + if (!host.h_name) { + n = (int)strlen(qname) + 1; /* for the \0 */ + if (n > ep - bp || n >= MAXHOSTNAMELEN) + goto no_recovery; + strlcpy(bp, qname, (size_t)(ep - bp)); + host.h_name = bp; + bp += n; + } + if (res->options & RES_USE_INET6) + map_v4v6_hostent(&host, &bp, ep); + h_errno = NETDB_SUCCESS; + return &host; + } + no_recovery: + h_errno = NO_RECOVERY; + return NULL; +} + +struct hostent * +gethostbyname(const char *name) +{ + struct hostent *hp; + res_state res = __res_get_state(); + + if (res == NULL) + return NULL; + + _DIAGASSERT(name != NULL); + + if (res->options & RES_USE_INET6) { + hp = gethostbyname_internal(name, AF_INET6, res); + if (hp) { + __res_put_state(res); + return hp; + } + } + hp = gethostbyname_internal(name, AF_INET, res); + __res_put_state(res); + return hp; +} + +struct hostent * +gethostbyname2(const char *name, int af) +{ + struct hostent *hp; + res_state res = __res_get_state(); + + if (res == NULL) + return NULL; + hp = gethostbyname_internal(name, af, res); + __res_put_state(res); + return hp; +} + +static struct hostent * +gethostbyname_internal(const char *name, int af, res_state res) +{ + const char *cp; + char *bp, *ep; + int size; + struct hostent *hp; + static const ns_dtab dtab[] = { + NS_FILES_CB(_gethtbyname, NULL) + { NSSRC_DNS, _dns_gethtbyname, NULL }, /* force -DHESIOD */ + NS_NIS_CB(_yp_gethtbyname, NULL) + NS_NULL_CB + }; + + _DIAGASSERT(name != NULL); + + switch (af) { + case AF_INET: + size = INADDRSZ; + break; + case AF_INET6: + size = IN6ADDRSZ; + break; + default: + h_errno = NETDB_INTERNAL; + errno = EAFNOSUPPORT; + return NULL; + } + + host.h_addrtype = af; + host.h_length = size; + + /* + * if there aren't any dots, it could be a user-level alias. + * this is also done in res_nquery() since we are not the only + * function that looks up host names. + */ + if (!strchr(name, '.') && (cp = __hostalias(name))) + name = cp; + + /* + * disallow names consisting only of digits/dots, unless + * they end in a dot. + */ + if (isdigit((u_char) name[0])) + for (cp = name;; ++cp) { + if (!*cp) { + if (*--cp == '.') + break; + /* + * All-numeric, no dot at the end. + * Fake up a hostent as if we'd actually + * done a lookup. + */ + if (inet_pton(af, name, + (char *)(void *)host_addr) <= 0) { + h_errno = HOST_NOT_FOUND; + return NULL; + } + strncpy(hostbuf, name, MAXDNAME); + hostbuf[MAXDNAME] = '\0'; + bp = hostbuf + MAXDNAME; + ep = hostbuf + sizeof hostbuf; + host.h_name = hostbuf; + host.h_aliases = host_aliases; + host_aliases[0] = NULL; + h_addr_ptrs[0] = (char *)(void *)host_addr; + h_addr_ptrs[1] = NULL; + host.h_addr_list = h_addr_ptrs; + if (res->options & RES_USE_INET6) + map_v4v6_hostent(&host, &bp, ep); + h_errno = NETDB_SUCCESS; + return &host; + } + if (!isdigit((u_char) *cp) && *cp != '.') + break; + } + if ((isxdigit((u_char) name[0]) && strchr(name, ':') != NULL) || + name[0] == ':') + for (cp = name;; ++cp) { + if (!*cp) { + if (*--cp == '.') + break; + /* + * All-IPv6-legal, no dot at the end. + * Fake up a hostent as if we'd actually + * done a lookup. + */ + if (inet_pton(af, name, + (char *)(void *)host_addr) <= 0) { + h_errno = HOST_NOT_FOUND; + return NULL; + } + strncpy(hostbuf, name, MAXDNAME); + hostbuf[MAXDNAME] = '\0'; + bp = hostbuf + MAXDNAME; + ep = hostbuf + sizeof hostbuf; + host.h_name = hostbuf; + host.h_aliases = host_aliases; + host_aliases[0] = NULL; + h_addr_ptrs[0] = (char *)(void *)host_addr; + h_addr_ptrs[1] = NULL; + host.h_addr_list = h_addr_ptrs; + h_errno = NETDB_SUCCESS; + return &host; + } + if (!isxdigit((u_char) *cp) && *cp != ':' && *cp != '.') + break; + } + + hp = NULL; + h_errno = NETDB_INTERNAL; + if (nsdispatch(&hp, dtab, NSDB_HOSTS, "gethostbyname", + default_dns_files, name, strlen(name), af) != NS_SUCCESS) + return NULL; + h_errno = NETDB_SUCCESS; + return hp; +} + +struct hostent * +gethostbyaddr(const char *addr, /* XXX should have been def'd as u_char! */ + socklen_t len, int af) +{ + const u_char *uaddr = (const u_char *)addr; + socklen_t size; + struct hostent *hp; + static const ns_dtab dtab[] = { + NS_FILES_CB(_gethtbyaddr, NULL) + { NSSRC_DNS, _dns_gethtbyaddr, NULL }, /* force -DHESIOD */ + NS_NIS_CB(_yp_gethtbyaddr, NULL) + NS_NULL_CB + }; + + _DIAGASSERT(addr != NULL); + + if (af == AF_INET6 && len == IN6ADDRSZ && + (IN6_IS_ADDR_LINKLOCAL((const struct in6_addr *)(const void *)uaddr) || + IN6_IS_ADDR_SITELOCAL((const struct in6_addr *)(const void *)uaddr))) { + h_errno = HOST_NOT_FOUND; + return NULL; + } + if (af == AF_INET6 && len == IN6ADDRSZ && + (IN6_IS_ADDR_V4MAPPED((const struct in6_addr *)(const void *)uaddr) || + IN6_IS_ADDR_V4COMPAT((const struct in6_addr *)(const void *)uaddr))) { + /* Unmap. */ + addr += IN6ADDRSZ - INADDRSZ; + uaddr += IN6ADDRSZ - INADDRSZ; + af = AF_INET; + len = INADDRSZ; + } + switch (af) { + case AF_INET: + size = INADDRSZ; + break; + case AF_INET6: + size = IN6ADDRSZ; + break; + default: + errno = EAFNOSUPPORT; + h_errno = NETDB_INTERNAL; + return NULL; + } + if (size != len) { + errno = EINVAL; + h_errno = NETDB_INTERNAL; + return NULL; + } + hp = NULL; + h_errno = NETDB_INTERNAL; + if (nsdispatch(&hp, dtab, NSDB_HOSTS, "gethostbyaddr", + default_dns_files, uaddr, len, af) != NS_SUCCESS) + return NULL; + h_errno = NETDB_SUCCESS; + return hp; +} + +void +_sethtent(int f) +{ + if (!hostf) + hostf = fopen(_PATH_HOSTS, "re"); + else + rewind(hostf); + stayopen = f; +} + +void +_endhtent(void) +{ + if (hostf && !stayopen) { + (void) fclose(hostf); + hostf = NULL; + } +} + +struct hostent * +_gethtent(void) +{ + char *p; + char *cp, **q; + int af, len; + + if (!hostf && !(hostf = fopen(_PATH_HOSTS, "re"))) { + h_errno = NETDB_INTERNAL; + return NULL; + } + again: + if (!(p = fgets(hostbuf, (int)sizeof hostbuf, hostf))) { + h_errno = HOST_NOT_FOUND; + return NULL; + } + if (*p == '#') + goto again; + if (!(cp = strpbrk(p, "#\n"))) + goto again; + *cp = '\0'; + if (!(cp = strpbrk(p, " \t"))) + goto again; + *cp++ = '\0'; + if (inet_pton(AF_INET6, p, (char *)(void *)host_addr) > 0) { + af = AF_INET6; + len = IN6ADDRSZ; + } else if (inet_pton(AF_INET, p, (char *)(void *)host_addr) > 0) { + res_state res = __res_get_state(); + if (res == NULL) + return NULL; + if (res->options & RES_USE_INET6) { + map_v4v6_address((char *)(void *)host_addr, + (char *)(void *)host_addr); + af = AF_INET6; + len = IN6ADDRSZ; + } else { + af = AF_INET; + len = INADDRSZ; + } + __res_put_state(res); + } else { + goto again; + } + /* if this is not something we're looking for, skip it. */ + if (host.h_addrtype != 0 && host.h_addrtype != af) + goto again; + if (host.h_length != 0 && host.h_length != len) + goto again; + h_addr_ptrs[0] = (char *)(void *)host_addr; + h_addr_ptrs[1] = NULL; + host.h_addr_list = h_addr_ptrs; + host.h_length = len; + host.h_addrtype = af; + while (*cp == ' ' || *cp == '\t') + cp++; + host.h_name = cp; + q = host.h_aliases = host_aliases; + if ((cp = strpbrk(cp, " \t")) != NULL) + *cp++ = '\0'; + while (cp && *cp) { + if (*cp == ' ' || *cp == '\t') { + cp++; + continue; + } + if (q < &host_aliases[MAXALIASES - 1]) + *q++ = cp; + if ((cp = strpbrk(cp, " \t")) != NULL) + *cp++ = '\0'; + } + *q = NULL; + h_errno = NETDB_SUCCESS; + return &host; +} + +/*ARGSUSED*/ +int +_gethtbyname(void *rv, void *cb_data, va_list ap) +{ + struct hostent *hp; + const char *name; + int af; + + _DIAGASSERT(rv != NULL); + + name = va_arg(ap, char *); + /* NOSTRICT skip len */(void)va_arg(ap, int); + af = va_arg(ap, int); + + hp = NULL; +#if 0 + { + res_state res = __res_get_state(); + if (res == NULL) + return NS_NOTFOUND; + if (res->options & RES_USE_INET6) + hp = _gethtbyname2(name, AF_INET6); + if (hp==NULL) + hp = _gethtbyname2(name, AF_INET); + __res_put_state(res); + } +#else + hp = _gethtbyname2(name, af); +#endif + *((struct hostent **)rv) = hp; + if (hp == NULL) { + h_errno = HOST_NOT_FOUND; + return NS_NOTFOUND; + } + return NS_SUCCESS; +} + +struct hostent * +_gethtbyname2(const char *name, int af) +{ + struct hostent *p; + char *tmpbuf, *ptr, **cp; + int num; + size_t len; + + _DIAGASSERT(name != NULL); + + _sethtent(stayopen); + ptr = tmpbuf = NULL; + num = 0; + while ((p = _gethtent()) != NULL && num < MAXADDRS) { + if (p->h_addrtype != af) + continue; + if (strcasecmp(p->h_name, name) != 0) { + for (cp = p->h_aliases; *cp != NULL; cp++) + if (strcasecmp(*cp, name) == 0) + break; + if (*cp == NULL) continue; + } + + if (num == 0) { + size_t bufsize; + char *src; + + bufsize = strlen(p->h_name) + 2 + + MAXADDRS * p->h_length + + ALIGNBYTES; + for (cp = p->h_aliases; *cp != NULL; cp++) + bufsize += strlen(*cp) + 1; + + if ((tmpbuf = malloc(bufsize)) == NULL) { + h_errno = NETDB_INTERNAL; + return NULL; + } + + ptr = tmpbuf; + src = p->h_name; + while ((*ptr++ = *src++) != '\0'); + for (cp = p->h_aliases; *cp != NULL; cp++) { + src = *cp; + while ((*ptr++ = *src++) != '\0'); + } + *ptr++ = '\0'; + + ptr = (char *)(void *)ALIGN(ptr); + } + + (void)memcpy(ptr, p->h_addr_list[0], (size_t)p->h_length); + ptr += p->h_length; + num++; + } + _endhtent(); + if (num == 0) return NULL; + + len = ptr - tmpbuf; + if (len > (sizeof(hostbuf) - ALIGNBYTES)) { + free(tmpbuf); + errno = ENOSPC; + h_errno = NETDB_INTERNAL; + return NULL; + } + ptr = memcpy((void *)ALIGN(hostbuf), tmpbuf, len); + free(tmpbuf); + + host.h_name = ptr; + while (*ptr++); + + cp = host_aliases; + while (*ptr) { + *cp++ = ptr; + while (*ptr++); + } + ptr++; + *cp = NULL; + + ptr = (char *)(void *)ALIGN(ptr); + cp = h_addr_ptrs; + while (num--) { + *cp++ = ptr; + ptr += host.h_length; + } + *cp = NULL; + + return &host; +} + +/*ARGSUSED*/ +int +_gethtbyaddr(void *rv, void *cb_data, va_list ap) +{ + struct hostent *p; + const unsigned char *addr; + int len, af; + + _DIAGASSERT(rv != NULL); + + addr = va_arg(ap, unsigned char *); + len = va_arg(ap, int); + af = va_arg(ap, int); + + host.h_length = len; + host.h_addrtype = af; + + _sethtent(stayopen); + while ((p = _gethtent()) != NULL) + if (p->h_addrtype == af && !memcmp(p->h_addr, addr, + (size_t)len)) + break; + _endhtent(); + *((struct hostent **)rv) = p; + if (p==NULL) { + h_errno = HOST_NOT_FOUND; + return NS_NOTFOUND; + } + return NS_SUCCESS; +} + +static void +map_v4v6_address(const char *src, char *dst) +{ + u_char *p = (u_char *)dst; + char tmp[INADDRSZ]; + int i; + + _DIAGASSERT(src != NULL); + _DIAGASSERT(dst != NULL); + + /* Stash a temporary copy so our caller can update in place. */ + (void)memcpy(tmp, src, INADDRSZ); + /* Mark this ipv6 addr as a mapped ipv4. */ + for (i = 0; i < 10; i++) + *p++ = 0x00; + *p++ = 0xff; + *p++ = 0xff; + /* Retrieve the saved copy and we're done. */ + (void)memcpy((void *)p, tmp, INADDRSZ); +} + +static void +map_v4v6_hostent(struct hostent *hp, char **bpp, char *ep) +{ + char **ap; + + _DIAGASSERT(hp != NULL); + _DIAGASSERT(bpp != NULL); + _DIAGASSERT(ep != NULL); + + if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ) + return; + hp->h_addrtype = AF_INET6; + hp->h_length = IN6ADDRSZ; + for (ap = hp->h_addr_list; *ap; ap++) { + int i = (int)(sizeof(align) - + (size_t)((u_long)*bpp % sizeof(align))); + + if (ep - *bpp < (i + IN6ADDRSZ)) { + /* Out of memory. Truncate address list here. XXX */ + *ap = NULL; + return; + } + *bpp += i; + map_v4v6_address(*ap, *bpp); + *ap = *bpp; + *bpp += IN6ADDRSZ; + } +} + +static void +addrsort(char **ap, int num, res_state res) +{ + int i, j; + char **p; + short aval[MAXADDRS]; + int needsort = 0; + + _DIAGASSERT(ap != NULL); + + p = ap; + for (i = 0; i < num; i++, p++) { + for (j = 0 ; (unsigned)j < res->nsort; j++) + if (res->sort_list[j].addr.s_addr == + (((struct in_addr *)(void *)(*p))->s_addr & + res->sort_list[j].mask)) + break; + aval[i] = j; + if (needsort == 0 && i > 0 && j < aval[i-1]) + needsort = i; + } + if (!needsort) + return; + + while (needsort < num) { + for (j = needsort - 1; j >= 0; j--) { + if (aval[j] > aval[j+1]) { + char *hp; + + i = aval[j]; + aval[j] = aval[j+1]; + aval[j+1] = i; + + hp = ap[j]; + ap[j] = ap[j+1]; + ap[j+1] = hp; + } else + break; + } + needsort++; + } +} + +struct hostent * +gethostent(void) +{ + host.h_addrtype = 0; + host.h_length = 0; + return _gethtent(); +} + +/*ARGSUSED*/ +int +_dns_gethtbyname(void *rv, void *cb_data, va_list ap) +{ + querybuf *buf; + int n, type; + struct hostent *hp; + const char *name; + int af; + res_state res; + + _DIAGASSERT(rv != NULL); + + name = va_arg(ap, char *); + /* NOSTRICT skip len */(void)va_arg(ap, int); + af = va_arg(ap, int); + + switch (af) { + case AF_INET: + type = T_A; + break; + case AF_INET6: + type = T_AAAA; + break; + default: + return NS_UNAVAIL; + } + buf = malloc(sizeof(*buf)); + if (buf == NULL) { + h_errno = NETDB_INTERNAL; + return NS_NOTFOUND; + } + res = __res_get_state(); + if (res == NULL) { + free(buf); + return NS_NOTFOUND; + } + n = res_nsearch(res, name, C_IN, type, buf->buf, (int)sizeof(buf->buf)); + if (n < 0) { + free(buf); + debugprintf("res_nsearch failed (%d)\n", res, n); + __res_put_state(res); + return NS_NOTFOUND; + } + hp = getanswer(buf, n, name, type, res); + free(buf); + __res_put_state(res); + if (hp == NULL) + switch (h_errno) { + case HOST_NOT_FOUND: + return NS_NOTFOUND; + case TRY_AGAIN: + return NS_TRYAGAIN; + default: + return NS_UNAVAIL; + } + *((struct hostent **)rv) = hp; + return NS_SUCCESS; +} + +/*ARGSUSED*/ +int +_dns_gethtbyaddr(void *rv, void *cb_data, va_list ap) +{ + char qbuf[MAXDNAME + 1], *qp, *ep; + int n; + querybuf *buf; + struct hostent *hp; + const unsigned char *uaddr; + int len, af, advance; + res_state res; + + _DIAGASSERT(rv != NULL); + + uaddr = va_arg(ap, unsigned char *); + len = va_arg(ap, int); + af = va_arg(ap, int); + + switch (af) { + case AF_INET: + (void)snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa", + (uaddr[3] & 0xff), (uaddr[2] & 0xff), + (uaddr[1] & 0xff), (uaddr[0] & 0xff)); + break; + + case AF_INET6: + qp = qbuf; + ep = qbuf + sizeof(qbuf) - 1; + for (n = IN6ADDRSZ - 1; n >= 0; n--) { + advance = snprintf(qp, (size_t)(ep - qp), "%x.%x.", + uaddr[n] & 0xf, + ((unsigned int)uaddr[n] >> 4) & 0xf); + if (advance > 0 && qp + advance < ep) + qp += advance; + else { + h_errno = NETDB_INTERNAL; + return NS_NOTFOUND; + } + } + if (strlcat(qbuf, "ip6.arpa", sizeof(qbuf)) >= sizeof(qbuf)) { + h_errno = NETDB_INTERNAL; + return NS_NOTFOUND; + } + break; + default: + abort(); + } + + buf = malloc(sizeof(*buf)); + if (buf == NULL) { + h_errno = NETDB_INTERNAL; + return NS_NOTFOUND; + } + res = __res_get_state(); + if (res == NULL) { + free(buf); + return NS_NOTFOUND; + } + n = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf, (int)sizeof(buf->buf)); + if (n < 0) { + free(buf); + debugprintf("res_nquery failed (%d)\n", res, n); + __res_put_state(res); + return NS_NOTFOUND; + } + hp = getanswer(buf, n, qbuf, T_PTR, res); + free(buf); + if (hp == NULL) { + __res_put_state(res); + switch (h_errno) { + case HOST_NOT_FOUND: + return NS_NOTFOUND; + case TRY_AGAIN: + return NS_TRYAGAIN; + default: + return NS_UNAVAIL; + } + } + hp->h_addrtype = af; + hp->h_length = len; + (void)memcpy(host_addr, uaddr, (size_t)len); + h_addr_ptrs[0] = (char *)(void *)host_addr; + h_addr_ptrs[1] = NULL; + if (af == AF_INET && (res->options & RES_USE_INET6)) { + map_v4v6_address((char *)(void *)host_addr, + (char *)(void *)host_addr); + hp->h_addrtype = AF_INET6; + hp->h_length = IN6ADDRSZ; + } + + __res_put_state(res); + *((struct hostent **)rv) = hp; + h_errno = NETDB_SUCCESS; + return NS_SUCCESS; +} + +#ifdef YP +/*ARGSUSED*/ +struct hostent * +_yphostent(char *line, int af) +{ + static struct in_addr host_addrs[MAXADDRS]; + static struct in6_addr host6_addrs[MAXADDRS]; + char *p = line; + char *cp, **q; + char **hap; + int addrok; + int more; + size_t naddrs; + + _DIAGASSERT(line != NULL); + + host.h_name = NULL; + host.h_addr_list = h_addr_ptrs; + host.h_addrtype = af; + switch (af) { + case AF_INET: + host.h_length = INADDRSZ; + break; + case AF_INET6: + host.h_length = IN6ADDRSZ; + break; + default: + return NULL; + } + hap = h_addr_ptrs; + q = host.h_aliases = host_aliases; + naddrs = 0; + +nextline: + /* check for host_addrs overflow */ + if (naddrs >= sizeof(host_addrs) / sizeof(host_addrs[0])) + goto done; + if (naddrs >= sizeof(host6_addrs) / sizeof(host6_addrs[0])) + goto done; + + more = 0; + cp = strpbrk(p, " \t"); + if (cp == NULL) + goto done; + *cp++ = '\0'; + + /* p has should have an address */ + addrok = 0; + switch (af) { + case AF_INET: + addrok = inet_aton(p, &host_addrs[naddrs]); + break; + case AF_INET6: + addrok = inet_pton(af, p, &host6_addrs[naddrs]); + break; + } + if (addrok != 1) { + /* skip to the next line */ + while (cp && *cp) { + if (*cp == '\n') { + cp++; + goto nextline; + } + cp++; + } + + goto done; + } + + switch (af) { + case AF_INET: + *hap++ = (char *)(void *)&host_addrs[naddrs++]; + break; + case AF_INET6: + *hap++ = (char *)(void *)&host6_addrs[naddrs++]; + break; + } + + while (*cp == ' ' || *cp == '\t') + cp++; + p = cp; + cp = strpbrk(p, " \t\n"); + if (cp != NULL) { + if (*cp == '\n') + more = 1; + *cp++ = '\0'; + } + if (!host.h_name) + host.h_name = p; + else if (strcmp(host.h_name, p)==0) + ; + else if (q < &host_aliases[MAXALIASES - 1]) + *q++ = p; + p = cp; + if (more) + goto nextline; + + while (cp && *cp) { + if (*cp == ' ' || *cp == '\t') { + cp++; + continue; + } + if (*cp == '\n') { + cp++; + goto nextline; + } + if (q < &host_aliases[MAXALIASES - 1]) + *q++ = cp; + cp = strpbrk(cp, " \t"); + if (cp != NULL) + *cp++ = '\0'; + } + +done: + if (host.h_name == NULL) + return NULL; + *q = NULL; + *hap = NULL; + return &host; +} + +/*ARGSUSED*/ +int +_yp_gethtbyaddr(void *rv, void *cb_data, va_list ap) +{ + struct hostent *hp = NULL; + static char *__ypcurrent; + int __ypcurrentlen, r; + char name[INET6_ADDRSTRLEN]; /* XXX enough? */ + const unsigned char *uaddr; + int af; + const char *map; + + _DIAGASSERT(rv != NULL); + + uaddr = va_arg(ap, unsigned char *); + /* NOSTRICT skip len */(void)va_arg(ap, int); + af = va_arg(ap, int); + + if (!__ypdomain) { + if (_yp_check(&__ypdomain) == 0) + return NS_UNAVAIL; + } + /* + * XXX unfortunately, we cannot support IPv6 extended scoped address + * notation here. gethostbyaddr() is not scope-aware. too bad. + */ + if (inet_ntop(af, uaddr, name, (socklen_t)sizeof(name)) == NULL) + return NS_UNAVAIL; + if (__ypcurrent) + free(__ypcurrent); + __ypcurrent = NULL; + switch (af) { + case AF_INET: + map = "hosts.byaddr"; + break; + default: + map = "ipnodes.byaddr"; + break; + } + r = yp_match(__ypdomain, map, name, + (int)strlen(name), &__ypcurrent, &__ypcurrentlen); + if (r == 0) + hp = _yphostent(__ypcurrent, af); + if (hp == NULL) { + h_errno = HOST_NOT_FOUND; + return NS_NOTFOUND; + } + *((struct hostent **)rv) = hp; + return NS_SUCCESS; +} + +/*ARGSUSED*/ +int +_yp_gethtbyname(void *rv, void *cb_data, va_list ap) +{ + struct hostent *hp = NULL; + static char *__ypcurrent; + int __ypcurrentlen, r; + const char *name; + int af; + const char *map; + + _DIAGASSERT(rv != NULL); + + name = va_arg(ap, char *); + /* NOSTRICT skip len */(void)va_arg(ap, int); + af = va_arg(ap, int); + + if (!__ypdomain) { + if (_yp_check(&__ypdomain) == 0) + return NS_UNAVAIL; + } + if (__ypcurrent) + free(__ypcurrent); + __ypcurrent = NULL; + switch (af) { + case AF_INET: + map = "hosts.byname"; + break; + default: + map = "ipnodes.byname"; + break; + } + r = yp_match(__ypdomain, map, name, + (int)strlen(name), &__ypcurrent, &__ypcurrentlen); + if (r == 0) + hp = _yphostent(__ypcurrent, af); + if (hp == NULL) { + h_errno = HOST_NOT_FOUND; + return NS_NOTFOUND; + } + *((struct hostent **)rv) = hp; + return NS_SUCCESS; +} +#endif diff -pruN /home/reed/src/isc/libbind/libbind/irs/gethostent.c /usr/src/lib/libc/net/gethostent.c --- /home/reed/src/isc/libbind/libbind/irs/gethostent.c 2006-01-09 23:06:00.000000000 -0600 +++ /usr/src/lib/libc/net/gethostent.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,1070 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1996-1999 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#if !defined(LINT) && !defined(CODECENTER) -static const char rcsid[] = "$Id: gethostent.c,v 1.8 2006/01/10 05:06:00 marka Exp $"; -#endif - -/* Imports */ - -#include "port_before.h" - -#if !defined(__BIND_NOSTATIC) - -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include "port_after.h" - -#include "irs_p.h" -#include "irs_data.h" - -/* Definitions */ - -struct pvt { - char * aliases[1]; - char * addrs[2]; - char addr[NS_IN6ADDRSZ]; - char name[NS_MAXDNAME + 1]; - struct hostent host; -}; - -/* Forward */ - -static struct net_data *init(void); -static void freepvt(struct net_data *); -static struct hostent *fakeaddr(const char *, int, struct net_data *); - - -/* Public */ - -struct hostent * -gethostbyname(const char *name) { - struct net_data *net_data = init(); - - return (gethostbyname_p(name, net_data)); -} - -struct hostent * -gethostbyname2(const char *name, int af) { - struct net_data *net_data = init(); - - return (gethostbyname2_p(name, af, net_data)); -} - -struct hostent * -gethostbyaddr(const char *addr, int len, int af) { - struct net_data *net_data = init(); - - return (gethostbyaddr_p(addr, len, af, net_data)); -} - -struct hostent * -gethostent() { - struct net_data *net_data = init(); - - return (gethostent_p(net_data)); -} - -void -sethostent(int stayopen) { - struct net_data *net_data = init(); - sethostent_p(stayopen, net_data); -} - - -void -endhostent() { - struct net_data *net_data = init(); - endhostent_p(net_data); -} - -/* Shared private. */ - -struct hostent * -gethostbyname_p(const char *name, struct net_data *net_data) { - struct hostent *hp; - - if (!net_data) - return (NULL); - - if (net_data->res->options & RES_USE_INET6) { - hp = gethostbyname2_p(name, AF_INET6, net_data); - if (hp) - return (hp); - } - return (gethostbyname2_p(name, AF_INET, net_data)); -} - -struct hostent * -gethostbyname2_p(const char *name, int af, struct net_data *net_data) { - struct irs_ho *ho; - char tmp[NS_MAXDNAME]; - struct hostent *hp; - const char *cp; - char **hap; - - if (!net_data || !(ho = net_data->ho)) - return (NULL); - if (net_data->ho_stayopen && net_data->ho_last && - net_data->ho_last->h_addrtype == af) { - if (ns_samename(name, net_data->ho_last->h_name) == 1) - return (net_data->ho_last); - for (hap = net_data->ho_last->h_aliases; hap && *hap; hap++) - if (ns_samename(name, *hap) == 1) - return (net_data->ho_last); - } - if (!strchr(name, '.') && (cp = res_hostalias(net_data->res, name, - tmp, sizeof tmp))) - name = cp; - if ((hp = fakeaddr(name, af, net_data)) != NULL) - return (hp); - net_data->ho_last = (*ho->byname2)(ho, name, af); - if (!net_data->ho_stayopen) - endhostent(); - return (net_data->ho_last); -} - -struct hostent * -gethostbyaddr_p(const char *addr, int len, int af, struct net_data *net_data) { - struct irs_ho *ho; - char **hap; - - if (!net_data || !(ho = net_data->ho)) - return (NULL); - if (net_data->ho_stayopen && net_data->ho_last && - net_data->ho_last->h_length == len) - for (hap = net_data->ho_last->h_addr_list; - hap && *hap; - hap++) - if (!memcmp(addr, *hap, len)) - return (net_data->ho_last); - net_data->ho_last = (*ho->byaddr)(ho, addr, len, af); - if (!net_data->ho_stayopen) - endhostent(); - return (net_data->ho_last); -} - - -struct hostent * -gethostent_p(struct net_data *net_data) { - struct irs_ho *ho; - struct hostent *hp; - - if (!net_data || !(ho = net_data->ho)) - return (NULL); - while ((hp = (*ho->next)(ho)) != NULL && - hp->h_addrtype == AF_INET6 && - (net_data->res->options & RES_USE_INET6) == 0U) - continue; - net_data->ho_last = hp; - return (net_data->ho_last); -} - - -void -sethostent_p(int stayopen, struct net_data *net_data) { - struct irs_ho *ho; - - if (!net_data || !(ho = net_data->ho)) - return; - freepvt(net_data); - (*ho->rewind)(ho); - net_data->ho_stayopen = (stayopen != 0); - if (stayopen == 0) - net_data_minimize(net_data); -} - -void -endhostent_p(struct net_data *net_data) { - struct irs_ho *ho; - - if ((net_data != NULL) && ((ho = net_data->ho) != NULL)) - (*ho->minimize)(ho); -} - -#ifndef IN6_IS_ADDR_V4COMPAT -static const unsigned char in6addr_compat[12] = { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; -#define IN6_IS_ADDR_V4COMPAT(x) (!memcmp((x)->s6_addr, in6addr_compat, 12) && \ - ((x)->s6_addr[12] != 0 || \ - (x)->s6_addr[13] != 0 || \ - (x)->s6_addr[14] != 0 || \ - ((x)->s6_addr[15] != 0 && \ - (x)->s6_addr[15] != 1))) -#endif -#ifndef IN6_IS_ADDR_V4MAPPED -#define IN6_IS_ADDR_V4MAPPED(x) (!memcmp((x)->s6_addr, in6addr_mapped, 12)) -#endif - -static const unsigned char in6addr_mapped[12] = { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff }; - -static int scan_interfaces(int *, int *); -static struct hostent *copyandmerge(struct hostent *, struct hostent *, int, int *); - -/*% - * Public functions - */ - -/*% - * AI_V4MAPPED + AF_INET6 - * If no IPv6 address then a query for IPv4 and map returned values. - * - * AI_ALL + AI_V4MAPPED + AF_INET6 - * Return IPv6 and IPv4 mapped. - * - * AI_ADDRCONFIG - * Only return IPv6 / IPv4 address if there is an interface of that - * type active. - */ - -struct hostent * -getipnodebyname(const char *name, int af, int flags, int *error_num) { - int have_v4 = 1, have_v6 = 1; - struct in_addr in4; - struct in6_addr in6; - struct hostent he, *he1 = NULL, *he2 = NULL, *he3; - int v4 = 0, v6 = 0; - struct net_data *net_data = init(); - u_long options; - int tmp_err; - - if (net_data == NULL) { - *error_num = NO_RECOVERY; - return (NULL); - } - - /* If we care about active interfaces then check. */ - if ((flags & AI_ADDRCONFIG) != 0) - if (scan_interfaces(&have_v4, &have_v6) == -1) { - *error_num = NO_RECOVERY; - return (NULL); - } - - /* Check for literal address. */ - if ((v4 = inet_pton(AF_INET, name, &in4)) != 1) - v6 = inet_pton(AF_INET6, name, &in6); - - /* Impossible combination? */ - - if ((af == AF_INET6 && (flags & AI_V4MAPPED) == 0 && v4 == 1) || - (af == AF_INET && v6 == 1) || - (have_v4 == 0 && v4 == 1) || - (have_v6 == 0 && v6 == 1) || - (have_v4 == 0 && af == AF_INET) || - (have_v6 == 0 && af == AF_INET6)) { - *error_num = HOST_NOT_FOUND; - return (NULL); - } - - /* Literal address? */ - if (v4 == 1 || v6 == 1) { - char *addr_list[2]; - char *aliases[1]; - - DE_CONST(name, he.h_name); - he.h_addr_list = addr_list; - he.h_addr_list[0] = (v4 == 1) ? (char *)&in4 : (char *)&in6; - he.h_addr_list[1] = NULL; - he.h_aliases = aliases; - he.h_aliases[0] = NULL; - he.h_length = (v4 == 1) ? INADDRSZ : IN6ADDRSZ; - he.h_addrtype = (v4 == 1) ? AF_INET : AF_INET6; - return (copyandmerge(&he, NULL, af, error_num)); - } - - options = net_data->res->options; - net_data->res->options &= ~RES_USE_INET6; - - tmp_err = NO_RECOVERY; - if (have_v6 && af == AF_INET6) { - he2 = gethostbyname2_p(name, AF_INET6, net_data); - if (he2 != NULL) { - he1 = copyandmerge(he2, NULL, af, error_num); - if (he1 == NULL) - return (NULL); - he2 = NULL; - } else { - tmp_err = net_data->res->res_h_errno; - } - } - - if (have_v4 && - ((af == AF_INET) || - (af == AF_INET6 && (flags & AI_V4MAPPED) != 0 && - (he1 == NULL || (flags & AI_ALL) != 0)))) { - he2 = gethostbyname2_p(name, AF_INET, net_data); - if (he1 == NULL && he2 == NULL) { - *error_num = net_data->res->res_h_errno; - return (NULL); - } - } else - *error_num = tmp_err; - - net_data->res->options = options; - - he3 = copyandmerge(he1, he2, af, error_num); - - if (he1 != NULL) - freehostent(he1); - return (he3); -} - -struct hostent * -getipnodebyaddr(const void *src, size_t len, int af, int *error_num) { - struct hostent *he1, *he2; - struct net_data *net_data = init(); - - /* Sanity Checks. */ - if (src == NULL) { - *error_num = NO_RECOVERY; - return (NULL); - } - - switch (af) { - case AF_INET: - if (len != (size_t)INADDRSZ) { - *error_num = NO_RECOVERY; - return (NULL); - } - break; - case AF_INET6: - if (len != (size_t)IN6ADDRSZ) { - *error_num = NO_RECOVERY; - return (NULL); - } - break; - default: - *error_num = NO_RECOVERY; - return (NULL); - } - - /* - * Lookup IPv4 and IPv4 mapped/compatible addresses - */ - if ((af == AF_INET6 && - IN6_IS_ADDR_V4COMPAT((const struct in6_addr *)src)) || - (af == AF_INET6 && - IN6_IS_ADDR_V4MAPPED((const struct in6_addr *)src)) || - (af == AF_INET)) { - const char *cp = src; - - if (af == AF_INET6) - cp += 12; - he1 = gethostbyaddr_p(cp, 4, AF_INET, net_data); - if (he1 == NULL) { - *error_num = net_data->res->res_h_errno; - return (NULL); - } - he2 = copyandmerge(he1, NULL, af, error_num); - if (he2 == NULL) - return (NULL); - /* - * Restore original address if mapped/compatible. - */ - if (af == AF_INET6) - memcpy(he1->h_addr, src, len); - return (he2); - } - - /* - * Lookup IPv6 address. - */ - if (memcmp((const struct in6_addr *)src, &in6addr_any, 16) == 0) { - *error_num = HOST_NOT_FOUND; - return (NULL); - } - - he1 = gethostbyaddr_p(src, 16, AF_INET6, net_data); - if (he1 == NULL) { - *error_num = net_data->res->res_h_errno; - return (NULL); - } - return (copyandmerge(he1, NULL, af, error_num)); -} - -void -freehostent(struct hostent *he) { - char **cpp; - int names = 1; - int addresses = 1; - - memput(he->h_name, strlen(he->h_name) + 1); - - cpp = he->h_addr_list; - while (*cpp != NULL) { - memput(*cpp, (he->h_addrtype == AF_INET) ? - INADDRSZ : IN6ADDRSZ); - *cpp = NULL; - cpp++; - addresses++; - } - - cpp = he->h_aliases; - while (*cpp != NULL) { - memput(*cpp, strlen(*cpp) + 1); - cpp++; - names++; - } - - memput(he->h_aliases, sizeof(char *) * (names)); - memput(he->h_addr_list, sizeof(char *) * (addresses)); - memput(he, sizeof *he); -} - -/*% - * Private - */ - -/*% - * Scan the interface table and set have_v4 and have_v6 depending - * upon whether there are IPv4 and IPv6 interface addresses. - * - * Returns: - * 0 on success - * -1 on failure. - */ - -#if defined(SIOCGLIFCONF) && defined(SIOCGLIFADDR) && \ - !defined(IRIX_EMUL_IOCTL_SIOCGIFCONF) - -#ifdef __hpux -#define lifc_len iflc_len -#define lifc_buf iflc_buf -#define lifc_req iflc_req -#define LIFCONF if_laddrconf -#else -#define SETFAMILYFLAGS -#define LIFCONF lifconf -#endif - -#ifdef __hpux -#define lifr_addr iflr_addr -#define lifr_name iflr_name -#define lifr_dstaddr iflr_dstaddr -#define lifr_flags iflr_flags -#define ss_family sa_family -#define LIFREQ if_laddrreq -#else -#define LIFREQ lifreq -#endif - -static void -scan_interfaces6(int *have_v4, int *have_v6) { - struct LIFCONF lifc; - struct LIFREQ lifreq; - struct in_addr in4; - struct in6_addr in6; - char *buf = NULL, *cp, *cplim; - static unsigned int bufsiz = 4095; - int s, cpsize, n; - - /* Get interface list from system. */ - if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) == -1) - goto cleanup; - - /* - * Grow buffer until large enough to contain all interface - * descriptions. - */ - for (;;) { - buf = memget(bufsiz); - if (buf == NULL) - goto cleanup; -#ifdef SETFAMILYFLAGS - lifc.lifc_family = AF_UNSPEC; /*%< request all families */ - lifc.lifc_flags = 0; -#endif - lifc.lifc_len = bufsiz; - lifc.lifc_buf = buf; - if ((n = ioctl(s, SIOCGLIFCONF, (char *)&lifc)) != -1) { - /* - * Some OS's just return what will fit rather - * than set EINVAL if the buffer is too small - * to fit all the interfaces in. If - * lifc.lifc_len is too near to the end of the - * buffer we will grow it just in case and - * retry. - */ - if (lifc.lifc_len + 2 * sizeof(lifreq) < bufsiz) - break; - } - if ((n == -1) && errno != EINVAL) - goto cleanup; - - if (bufsiz > 1000000) - goto cleanup; - - memput(buf, bufsiz); - bufsiz += 4096; - } - - /* Parse system's interface list. */ - cplim = buf + lifc.lifc_len; /*%< skip over if's with big ifr_addr's */ - for (cp = buf; - (*have_v4 == 0 || *have_v6 == 0) && cp < cplim; - cp += cpsize) { - memcpy(&lifreq, cp, sizeof lifreq); -#ifdef HAVE_SA_LEN -#ifdef FIX_ZERO_SA_LEN - if (lifreq.lifr_addr.sa_len == 0) - lifreq.lifr_addr.sa_len = 16; -#endif -#ifdef HAVE_MINIMUM_IFREQ - cpsize = sizeof lifreq; - if (lifreq.lifr_addr.sa_len > sizeof (struct sockaddr)) - cpsize += (int)lifreq.lifr_addr.sa_len - - (int)(sizeof (struct sockaddr)); -#else - cpsize = sizeof lifreq.lifr_name + lifreq.lifr_addr.sa_len; -#endif /* HAVE_MINIMUM_IFREQ */ -#elif defined SIOCGIFCONF_ADDR - cpsize = sizeof lifreq; -#else - cpsize = sizeof lifreq.lifr_name; - /* XXX maybe this should be a hard error? */ - if (ioctl(s, SIOCGLIFADDR, (char *)&lifreq) < 0) - continue; -#endif - switch (lifreq.lifr_addr.ss_family) { - case AF_INET: - if (*have_v4 == 0) { - memcpy(&in4, - &((struct sockaddr_in *) - &lifreq.lifr_addr)->sin_addr, - sizeof in4); - if (in4.s_addr == INADDR_ANY) - break; - n = ioctl(s, SIOCGLIFFLAGS, (char *)&lifreq); - if (n < 0) - break; - if ((lifreq.lifr_flags & IFF_UP) == 0) - break; - *have_v4 = 1; - } - break; - case AF_INET6: - if (*have_v6 == 0) { - memcpy(&in6, - &((struct sockaddr_in6 *) - &lifreq.lifr_addr)->sin6_addr, sizeof in6); - if (memcmp(&in6, &in6addr_any, sizeof in6) == 0) - break; - n = ioctl(s, SIOCGLIFFLAGS, (char *)&lifreq); - if (n < 0) - break; - if ((lifreq.lifr_flags & IFF_UP) == 0) - break; - *have_v6 = 1; - } - break; - } - } - if (buf != NULL) - memput(buf, bufsiz); - close(s); - /* printf("scan interface -> 4=%d 6=%d\n", *have_v4, *have_v6); */ - return; - cleanup: - if (buf != NULL) - memput(buf, bufsiz); - if (s != -1) - close(s); - /* printf("scan interface -> 4=%d 6=%d\n", *have_v4, *have_v6); */ - return; -} -#endif - -#if ( defined(__linux__) || defined(__linux) || defined(LINUX) ) -#ifndef IF_NAMESIZE -# ifdef IFNAMSIZ -# define IF_NAMESIZE IFNAMSIZ -# else -# define IF_NAMESIZE 16 -# endif -#endif -static void -scan_linux6(int *have_v6) { - FILE *proc = NULL; - char address[33]; - char name[IF_NAMESIZE+1]; - int ifindex, prefix, flag3, flag4; - - proc = fopen("/proc/net/if_inet6", "r"); - if (proc == NULL) - return; - - if (fscanf(proc, "%32[a-f0-9] %x %x %x %x %16s\n", - address, &ifindex, &prefix, &flag3, &flag4, name) == 6) - *have_v6 = 1; - fclose(proc); - return; -} -#endif - -static int -scan_interfaces(int *have_v4, int *have_v6) { - struct ifconf ifc; - union { - char _pad[256]; /*%< leave space for IPv6 addresses */ - struct ifreq ifreq; - } u; - struct in_addr in4; - struct in6_addr in6; - char *buf = NULL, *cp, *cplim; - static unsigned int bufsiz = 4095; - int s, n; - size_t cpsize; - - /* Set to zero. Used as loop terminators below. */ - *have_v4 = *have_v6 = 0; - -#if defined(SIOCGLIFCONF) && defined(SIOCGLIFADDR) && \ - !defined(IRIX_EMUL_IOCTL_SIOCGIFCONF) - /* - * Try to scan the interfaces using IPv6 ioctls(). - */ - scan_interfaces6(have_v4, have_v6); - if (*have_v4 != 0 && *have_v6 != 0) - return (0); -#endif -#ifdef __linux - scan_linux6(have_v6); -#endif - - /* Get interface list from system. */ - if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) - goto err_ret; - - /* - * Grow buffer until large enough to contain all interface - * descriptions. - */ - for (;;) { - buf = memget(bufsiz); - if (buf == NULL) - goto err_ret; - ifc.ifc_len = bufsiz; - ifc.ifc_buf = buf; -#ifdef IRIX_EMUL_IOCTL_SIOCGIFCONF - /* - * This is a fix for IRIX OS in which the call to ioctl with - * the flag SIOCGIFCONF may not return an entry for all the - * interfaces like most flavors of Unix. - */ - if (emul_ioctl(&ifc) >= 0) - break; -#else - if ((n = ioctl(s, SIOCGIFCONF, (char *)&ifc)) != -1) { - /* - * Some OS's just return what will fit rather - * than set EINVAL if the buffer is too small - * to fit all the interfaces in. If - * ifc.ifc_len is too near to the end of the - * buffer we will grow it just in case and - * retry. - */ - if (ifc.ifc_len + 2 * sizeof(u.ifreq) < bufsiz) - break; - } -#endif - if ((n == -1) && errno != EINVAL) - goto err_ret; - - if (bufsiz > 1000000) - goto err_ret; - - memput(buf, bufsiz); - bufsiz += 4096; - } - - /* Parse system's interface list. */ - cplim = buf + ifc.ifc_len; /*%< skip over if's with big ifr_addr's */ - for (cp = buf; - (*have_v4 == 0 || *have_v6 == 0) && cp < cplim; - cp += cpsize) { - memcpy(&u.ifreq, cp, sizeof u.ifreq); -#ifdef HAVE_SA_LEN -#ifdef FIX_ZERO_SA_LEN - if (u.ifreq.ifr_addr.sa_len == 0) - u.ifreq.ifr_addr.sa_len = 16; -#endif -#ifdef HAVE_MINIMUM_IFREQ - cpsize = sizeof u.ifreq; - if (u.ifreq.ifr_addr.sa_len > sizeof (struct sockaddr)) - cpsize += (int)u.ifreq.ifr_addr.sa_len - - (int)(sizeof (struct sockaddr)); -#else - cpsize = sizeof u.ifreq.ifr_name + u.ifreq.ifr_addr.sa_len; -#endif /* HAVE_MINIMUM_IFREQ */ - if (cpsize > sizeof u.ifreq && cpsize <= sizeof u) - memcpy(&u.ifreq, cp, cpsize); -#elif defined SIOCGIFCONF_ADDR - cpsize = sizeof u.ifreq; -#else - cpsize = sizeof u.ifreq.ifr_name; - /* XXX maybe this should be a hard error? */ - if (ioctl(s, SIOCGIFADDR, (char *)&u.ifreq) < 0) - continue; -#endif - switch (u.ifreq.ifr_addr.sa_family) { - case AF_INET: - if (*have_v4 == 0) { - memcpy(&in4, - &((struct sockaddr_in *) - &u.ifreq.ifr_addr)->sin_addr, - sizeof in4); - if (in4.s_addr == INADDR_ANY) - break; - n = ioctl(s, SIOCGIFFLAGS, (char *)&u.ifreq); - if (n < 0) - break; - if ((u.ifreq.ifr_flags & IFF_UP) == 0) - break; - *have_v4 = 1; - } - break; - case AF_INET6: - if (*have_v6 == 0) { - memcpy(&in6, - &((struct sockaddr_in6 *) - &u.ifreq.ifr_addr)->sin6_addr, - sizeof in6); - if (memcmp(&in6, &in6addr_any, sizeof in6) == 0) - break; - n = ioctl(s, SIOCGIFFLAGS, (char *)&u.ifreq); - if (n < 0) - break; - if ((u.ifreq.ifr_flags & IFF_UP) == 0) - break; - *have_v6 = 1; - } - break; - } - } - if (buf != NULL) - memput(buf, bufsiz); - close(s); - /* printf("scan interface -> 4=%d 6=%d\n", *have_v4, *have_v6); */ - return (0); - err_ret: - if (buf != NULL) - memput(buf, bufsiz); - if (s != -1) - close(s); - /* printf("scan interface -> 4=%d 6=%d\n", *have_v4, *have_v6); */ - return (-1); -} - -static struct hostent * -copyandmerge(struct hostent *he1, struct hostent *he2, int af, int *error_num) { - struct hostent *he = NULL; - int addresses = 1; /*%< NULL terminator */ - int names = 1; /*%< NULL terminator */ - int len = 0; - char **cpp, **npp; - - /* - * Work out array sizes; - */ - if (he1 != NULL) { - cpp = he1->h_addr_list; - while (*cpp != NULL) { - addresses++; - cpp++; - } - cpp = he1->h_aliases; - while (*cpp != NULL) { - names++; - cpp++; - } - } - - if (he2 != NULL) { - cpp = he2->h_addr_list; - while (*cpp != NULL) { - addresses++; - cpp++; - } - if (he1 == NULL) { - cpp = he2->h_aliases; - while (*cpp != NULL) { - names++; - cpp++; - } - } - } - - if (addresses == 1) { - *error_num = NO_ADDRESS; - return (NULL); - } - - he = memget(sizeof *he); - if (he == NULL) - goto no_recovery; - - he->h_addr_list = memget(sizeof(char *) * (addresses)); - if (he->h_addr_list == NULL) - goto cleanup0; - memset(he->h_addr_list, 0, sizeof(char *) * (addresses)); - - /* copy addresses */ - npp = he->h_addr_list; - if (he1 != NULL) { - cpp = he1->h_addr_list; - while (*cpp != NULL) { - *npp = memget((af == AF_INET) ? INADDRSZ : IN6ADDRSZ); - if (*npp == NULL) - goto cleanup1; - /* convert to mapped if required */ - if (af == AF_INET6 && he1->h_addrtype == AF_INET) { - memcpy(*npp, in6addr_mapped, - sizeof in6addr_mapped); - memcpy(*npp + sizeof in6addr_mapped, *cpp, - INADDRSZ); - } else { - memcpy(*npp, *cpp, - (af == AF_INET) ? INADDRSZ : IN6ADDRSZ); - } - cpp++; - npp++; - } - } - - if (he2 != NULL) { - cpp = he2->h_addr_list; - while (*cpp != NULL) { - *npp = memget((af == AF_INET) ? INADDRSZ : IN6ADDRSZ); - if (*npp == NULL) - goto cleanup1; - /* convert to mapped if required */ - if (af == AF_INET6 && he2->h_addrtype == AF_INET) { - memcpy(*npp, in6addr_mapped, - sizeof in6addr_mapped); - memcpy(*npp + sizeof in6addr_mapped, *cpp, - INADDRSZ); - } else { - memcpy(*npp, *cpp, - (af == AF_INET) ? INADDRSZ : IN6ADDRSZ); - } - cpp++; - npp++; - } - } - - he->h_aliases = memget(sizeof(char *) * (names)); - if (he->h_aliases == NULL) - goto cleanup1; - memset(he->h_aliases, 0, sizeof(char *) * (names)); - - /* copy aliases */ - npp = he->h_aliases; - cpp = (he1 != NULL) ? he1->h_aliases : he2->h_aliases; - while (*cpp != NULL) { - len = strlen (*cpp) + 1; - *npp = memget(len); - if (*npp == NULL) - goto cleanup2; - strcpy(*npp, *cpp); - npp++; - cpp++; - } - - /* copy hostname */ - he->h_name = memget(strlen((he1 != NULL) ? - he1->h_name : he2->h_name) + 1); - if (he->h_name == NULL) - goto cleanup2; - strcpy(he->h_name, (he1 != NULL) ? he1->h_name : he2->h_name); - - /* set address type and length */ - he->h_addrtype = af; - he->h_length = (af == AF_INET) ? INADDRSZ : IN6ADDRSZ; - return(he); - - cleanup2: - cpp = he->h_aliases; - while (*cpp != NULL) { - memput(*cpp, strlen(*cpp) + 1); - cpp++; - } - memput(he->h_aliases, sizeof(char *) * (names)); - - cleanup1: - cpp = he->h_addr_list; - while (*cpp != NULL) { - memput(*cpp, (af == AF_INET) ? INADDRSZ : IN6ADDRSZ); - *cpp = NULL; - cpp++; - } - memput(he->h_addr_list, sizeof(char *) * (addresses)); - - cleanup0: - memput(he, sizeof *he); - - no_recovery: - *error_num = NO_RECOVERY; - return (NULL); -} - -static struct net_data * -init() { - struct net_data *net_data; - - if (!(net_data = net_data_init(NULL))) - goto error; - if (!net_data->ho) { - net_data->ho = (*net_data->irs->ho_map)(net_data->irs); - if (!net_data->ho || !net_data->res) { - error: - errno = EIO; - if (net_data && net_data->res) - RES_SET_H_ERRNO(net_data->res, NETDB_INTERNAL); - return (NULL); - } - - (*net_data->ho->res_set)(net_data->ho, net_data->res, NULL); - } - - return (net_data); -} - -static void -freepvt(struct net_data *net_data) { - if (net_data->ho_data) { - free(net_data->ho_data); - net_data->ho_data = NULL; - } -} - -static struct hostent * -fakeaddr(const char *name, int af, struct net_data *net_data) { - struct pvt *pvt; - - freepvt(net_data); - net_data->ho_data = malloc(sizeof (struct pvt)); - if (!net_data->ho_data) { - errno = ENOMEM; - RES_SET_H_ERRNO(net_data->res, NETDB_INTERNAL); - return (NULL); - } - pvt = net_data->ho_data; -#ifndef __bsdi__ - /* - * Unlike its forebear(inet_aton), our friendly inet_pton() is strict - * in its interpretation of its input, and it will only return "1" if - * the input string is a formally valid(and thus unambiguous with - * respect to host names) internet address specification for this AF. - * - * This means "telnet 0xdeadbeef" and "telnet 127.1" are dead now. - */ - if (inet_pton(af, name, pvt->addr) != 1) { -#else - /* BSDI XXX - * We put this back to inet_aton -- we really want the old behavior - * Long live 127.1... - */ - if ((af != AF_INET || - inet_aton(name, (struct in_addr *)pvt->addr) != 1) && - inet_pton(af, name, pvt->addr) != 1) { -#endif - RES_SET_H_ERRNO(net_data->res, HOST_NOT_FOUND); - return (NULL); - } - strncpy(pvt->name, name, NS_MAXDNAME); - pvt->name[NS_MAXDNAME] = '\0'; - if (af == AF_INET && (net_data->res->options & RES_USE_INET6) != 0U) { - map_v4v6_address(pvt->addr, pvt->addr); - af = AF_INET6; - } - pvt->host.h_addrtype = af; - switch(af) { - case AF_INET: - pvt->host.h_length = NS_INADDRSZ; - break; - case AF_INET6: - pvt->host.h_length = NS_IN6ADDRSZ; - break; - default: - errno = EAFNOSUPPORT; - RES_SET_H_ERRNO(net_data->res, NETDB_INTERNAL); - return (NULL); - } - pvt->host.h_name = pvt->name; - pvt->host.h_aliases = pvt->aliases; - pvt->aliases[0] = NULL; - pvt->addrs[0] = (char *)pvt->addr; - pvt->addrs[1] = NULL; - pvt->host.h_addr_list = pvt->addrs; - RES_SET_H_ERRNO(net_data->res, NETDB_SUCCESS); - return (&pvt->host); -} - -#ifdef grot /*%< for future use in gethostbyaddr(), for "SUNSECURITY" */ - struct hostent *rhp; - char **haddr; - u_long old_options; - char hname2[MAXDNAME+1]; - - if (af == AF_INET) { - /* - * turn off search as the name should be absolute, - * 'localhost' should be matched by defnames - */ - strncpy(hname2, hp->h_name, MAXDNAME); - hname2[MAXDNAME] = '\0'; - old_options = net_data->res->options; - net_data->res->options &= ~RES_DNSRCH; - net_data->res->options |= RES_DEFNAMES; - if (!(rhp = gethostbyname(hname2))) { - net_data->res->options = old_options; - RES_SET_H_ERRNO(net_data->res, HOST_NOT_FOUND); - return (NULL); - } - net_data->res->options = old_options; - for (haddr = rhp->h_addr_list; *haddr; haddr++) - if (!memcmp(*haddr, addr, INADDRSZ)) - break; - if (!*haddr) { - RES_SET_H_ERRNO(net_data->res, HOST_NOT_FOUND); - return (NULL); - } - } -#endif /* grot */ -#endif /*__BIND_NOSTATIC*/ - -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/gethostent_r.c /usr/src/lib/libc/net/gethostent_r.c --- /home/reed/src/isc/libbind/libbind/irs/gethostent_r.c 2005-09-03 07:41:37.000000000 -0500 +++ /usr/src/lib/libc/net/gethostent_r.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,275 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1998-1999 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#if defined(LIBC_SCCS) && !defined(lint) -static const char rcsid[] = "$Id: gethostent_r.c,v 1.9 2005/09/03 12:41:37 marka Exp $"; -#endif /* LIBC_SCCS and not lint */ - -#include -#if !defined(_REENTRANT) || !defined(DO_PTHREADS) - static int gethostent_r_not_required = 0; -#else -#include -#include -#include -#include -#include -#include -#include -#include - -#ifdef HOST_R_RETURN - -static HOST_R_RETURN -copy_hostent(struct hostent *, struct hostent *, HOST_R_COPY_ARGS); - -HOST_R_RETURN -gethostbyname_r(const char *name, struct hostent *hptr, HOST_R_ARGS) { - struct hostent *he = gethostbyname(name); -#ifdef HOST_R_SETANSWER - int n = 0; -#endif - -#ifdef HOST_R_ERRNO - HOST_R_ERRNO; -#endif - -#ifdef HOST_R_SETANSWER - if (he == NULL || (n = copy_hostent(he, hptr, HOST_R_COPY)) != 0) - *answerp = NULL; - else - *answerp = hptr; - - return (n); -#else - if (he == NULL) - return (HOST_R_BAD); - - return (copy_hostent(he, hptr, HOST_R_COPY)); -#endif -} - -HOST_R_RETURN -gethostbyaddr_r(const char *addr, int len, int type, - struct hostent *hptr, HOST_R_ARGS) { - struct hostent *he = gethostbyaddr(addr, len, type); -#ifdef HOST_R_SETANSWER - int n = 0; -#endif - -#ifdef HOST_R_ERRNO - HOST_R_ERRNO; -#endif - -#ifdef HOST_R_SETANSWER - if (he == NULL || (n = copy_hostent(he, hptr, HOST_R_COPY)) != 0) - *answerp = NULL; - else - *answerp = hptr; - - return (n); -#else - if (he == NULL) - return (HOST_R_BAD); - - return (copy_hostent(he, hptr, HOST_R_COPY)); -#endif -} - -/*% - * These assume a single context is in operation per thread. - * If this is not the case we will need to call irs directly - * rather than through the base functions. - */ - -HOST_R_RETURN -gethostent_r(struct hostent *hptr, HOST_R_ARGS) { - struct hostent *he = gethostent(); -#ifdef HOST_R_SETANSWER - int n = 0; -#endif - -#ifdef HOST_R_ERRNO - HOST_R_ERRNO; -#endif - -#ifdef HOST_R_SETANSWER - if (he == NULL || (n = copy_hostent(he, hptr, HOST_R_COPY)) != 0) - *answerp = NULL; - else - *answerp = hptr; - - return (n); -#else - if (he == NULL) - return (HOST_R_BAD); - - return (copy_hostent(he, hptr, HOST_R_COPY)); -#endif -} - -HOST_R_SET_RETURN -#ifdef HOST_R_ENT_ARGS -sethostent_r(int stay_open, HOST_R_ENT_ARGS) -#else -sethostent_r(int stay_open) -#endif -{ -#ifdef HOST_R_ENT_ARGS - UNUSED(hdptr); -#endif - sethostent(stay_open); -#ifdef HOST_R_SET_RESULT - return (HOST_R_SET_RESULT); -#endif -} - -HOST_R_END_RETURN -#ifdef HOST_R_ENT_ARGS -endhostent_r(HOST_R_ENT_ARGS) -#else -endhostent_r(void) -#endif -{ -#ifdef HOST_R_ENT_ARGS - UNUSED(hdptr); -#endif - endhostent(); - HOST_R_END_RESULT(HOST_R_OK); -} - -/* Private */ - -#ifndef HOSTENT_DATA -static HOST_R_RETURN -copy_hostent(struct hostent *he, struct hostent *hptr, HOST_R_COPY_ARGS) { - char *cp; - char **ptr; - int i, n; - int nptr, len; - - /* Find out the amount of space required to store the answer. */ - nptr = 2; /*%< NULL ptrs */ - len = (char *)ALIGN(buf) - buf; - for (i = 0; he->h_addr_list[i]; i++, nptr++) { - len += he->h_length; - } - for (i = 0; he->h_aliases[i]; i++, nptr++) { - len += strlen(he->h_aliases[i]) + 1; - } - len += strlen(he->h_name) + 1; - len += nptr * sizeof(char*); - - if (len > buflen) { - errno = ERANGE; - return (HOST_R_BAD); - } - - /* copy address size and type */ - hptr->h_addrtype = he->h_addrtype; - n = hptr->h_length = he->h_length; - - ptr = (char **)ALIGN(buf); - cp = (char *)ALIGN(buf) + nptr * sizeof(char *); - - /* copy address list */ - hptr->h_addr_list = ptr; - for (i = 0; he->h_addr_list[i]; i++ , ptr++) { - memcpy(cp, he->h_addr_list[i], n); - hptr->h_addr_list[i] = cp; - cp += n; - } - hptr->h_addr_list[i] = NULL; - ptr++; - - /* copy official name */ - n = strlen(he->h_name) + 1; - strcpy(cp, he->h_name); - hptr->h_name = cp; - cp += n; - - /* copy aliases */ - hptr->h_aliases = ptr; - for (i = 0 ; he->h_aliases[i]; i++) { - n = strlen(he->h_aliases[i]) + 1; - strcpy(cp, he->h_aliases[i]); - hptr->h_aliases[i] = cp; - cp += n; - } - hptr->h_aliases[i] = NULL; - - return (HOST_R_OK); -} -#else /* !HOSTENT_DATA */ -static int -copy_hostent(struct hostent *he, struct hostent *hptr, HOST_R_COPY_ARGS) { - char *cp, *eob; - int i, n; - - /* copy address size and type */ - hptr->h_addrtype = he->h_addrtype; - n = hptr->h_length = he->h_length; - - /* copy up to first 35 addresses */ - i = 0; - cp = hdptr->hostbuf; - eob = hdptr->hostbuf + sizeof(hdptr->hostbuf); - hptr->h_addr_list = hdptr->h_addr_ptrs; - while (he->h_addr_list[i] && i < (_MAXADDRS)) { - if (n < (eob - cp)) { - memcpy(cp, he->h_addr_list[i], n); - hptr->h_addr_list[i] = cp; - cp += n; - } else { - break; - } - i++; - } - hptr->h_addr_list[i] = NULL; - - /* copy official name */ - if ((n = strlen(he->h_name) + 1) < (eob - cp)) { - strcpy(cp, he->h_name); - hptr->h_name = cp; - cp += n; - } else { - return (-1); - } - - /* copy aliases */ - i = 0; - hptr->h_aliases = hdptr->host_aliases; - while (he->h_aliases[i] && i < (_MAXALIASES-1)) { - if ((n = strlen(he->h_aliases[i]) + 1) < (eob - cp)) { - strcpy(cp, he->h_aliases[i]); - hptr->h_aliases[i] = cp; - cp += n; - } else { - break; - } - i++; - } - hptr->h_aliases[i] = NULL; - - return (HOST_R_OK); -} -#endif /* !HOSTENT_DATA */ -#else /* HOST_R_RETURN */ - static int gethostent_r_unknown_system = 0; -#endif /* HOST_R_RETURN */ -#endif /* !defined(_REENTRANT) || !defined(DO_PTHREADS) */ -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/getifaddrs.c /usr/src/lib/libc/net/getifaddrs.c --- /home/reed/src/isc/libbind/libbind/irs/getifaddrs.c 1969-12-31 18:00:00.000000000 -0600 +++ /usr/src/lib/libc/net/getifaddrs.c 2013-06-05 09:26:12.000000000 -0500 @@ -0,0 +1,305 @@ +/* $NetBSD: getifaddrs.c,v 1.15 2012/03/13 21:13:40 christos Exp $ */ + +/* + * Copyright (c) 1995, 1999 + * Berkeley Software Design, Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * BSDI getifaddrs.c,v 2.12 2000/02/23 14:51:59 dab Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: getifaddrs.c,v 1.15 2012/03/13 21:13:40 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#ifndef RUMP_ACTION +#include "namespace.h" +#endif +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#if defined(__weak_alias) && !defined(RUMP_ACTION) +__weak_alias(getifaddrs,_getifaddrs) +__weak_alias(freeifaddrs,_freeifaddrs) +#endif + +#ifdef RUMP_ACTION +#include +#define sysctl(a,b,c,d,e,f) rump_sys___sysctl(a,b,c,d,e,f) +#endif + +#define SA_RLEN(sa) RT_ROUNDUP((sa)->sa_len) + +int +getifaddrs(struct ifaddrs **pif) +{ + size_t icnt = 1; + size_t dcnt = 0; + size_t ncnt = 0; + static const int mib[] = { + CTL_NET, + PF_ROUTE, + 0, /* protocol */ + 0, /* wildcard address family */ + NET_RT_IFLIST, + 0 /* no flags */ + }; + size_t needed; + char *buf; + char *next; + struct ifaddrs cif; + char *p, *p0; + struct rt_msghdr *rtm; + struct if_msghdr *ifm; + struct ifa_msghdr *ifam; + struct sockaddr *sa; + struct ifaddrs *ifa, *ift; + u_short idx = 0; + int i; + size_t len, alen; + char *data; + char *names; + + _DIAGASSERT(pif != NULL); + + if (sysctl(mib, (u_int)__arraycount(mib), NULL, &needed, NULL, 0) < 0) + return (-1); + if ((buf = malloc(needed)) == NULL) + return (-1); + if (sysctl(mib, (u_int)__arraycount(mib), buf, &needed, NULL, 0) < 0) { + free(buf); + return (-1); + } + + for (next = buf; next < buf + needed; next += rtm->rtm_msglen) { + rtm = (struct rt_msghdr *)(void *)next; + if (rtm->rtm_version != RTM_VERSION) + continue; + switch (rtm->rtm_type) { + case RTM_IFINFO: + ifm = (struct if_msghdr *)(void *)rtm; + if (ifm->ifm_addrs & RTA_IFP) { + const struct sockaddr_dl *dl; + + idx = ifm->ifm_index; + ++icnt; + dl = (struct sockaddr_dl *)(void *)(ifm + 1); + dcnt += SA_RLEN((const struct sockaddr *)(const void *)dl) + + ALIGNBYTES; + dcnt += sizeof(ifm->ifm_data); + ncnt += dl->sdl_nlen + 1; + } else + idx = 0; + break; + + case RTM_NEWADDR: + ifam = (struct ifa_msghdr *)(void *)rtm; + if (idx && ifam->ifam_index != idx) + abort(); /* this cannot happen */ + +#define RTA_MASKS (RTA_NETMASK | RTA_IFA | RTA_BRD) + if (idx == 0 || (ifam->ifam_addrs & RTA_MASKS) == 0) + break; + p = (char *)(void *)(ifam + 1); + ++icnt; + /* Scan to look for length of address */ + alen = 0; + for (p0 = p, i = 0; i < RTAX_MAX; i++) { + if ((RTA_MASKS & ifam->ifam_addrs & (1 << i)) + == 0) + continue; + sa = (struct sockaddr *)(void *)p; + len = SA_RLEN(sa); + if (i == RTAX_IFA) { + alen = len; + break; + } + p += len; + } + for (p = p0, i = 0; i < RTAX_MAX; i++) { + if ((RTA_MASKS & ifam->ifam_addrs & (1 << i)) + == 0) + continue; + sa = (struct sockaddr *)(void *)p; + len = SA_RLEN(sa); + if (i == RTAX_NETMASK && sa->sa_len == 0) + dcnt += alen; + else + dcnt += len; + p += len; + } + break; + } + } + + if (icnt + dcnt + ncnt == 1) { + *pif = NULL; + free(buf); + return (0); + } + data = malloc(sizeof(struct ifaddrs) * icnt + dcnt + ncnt); + if (data == NULL) { + free(buf); + return(-1); + } + + ifa = (struct ifaddrs *)(void *)data; + data += sizeof(struct ifaddrs) * icnt; + names = data + dcnt; + + memset(ifa, 0, sizeof(struct ifaddrs) * icnt); + ift = ifa; + + idx = 0; + for (next = buf; next < buf + needed; next += rtm->rtm_msglen) { + rtm = (struct rt_msghdr *)(void *)next; + if (rtm->rtm_version != RTM_VERSION) + continue; + switch (rtm->rtm_type) { + case RTM_IFINFO: + ifm = (struct if_msghdr *)(void *)rtm; + if (ifm->ifm_addrs & RTA_IFP) { + const struct sockaddr_dl *dl; + + idx = ifm->ifm_index; + dl = (struct sockaddr_dl *)(void *)(ifm + 1); + + memset(&cif, 0, sizeof(cif)); + + cif.ifa_name = names; + cif.ifa_flags = (int)ifm->ifm_flags; + memcpy(names, dl->sdl_data, + (size_t)dl->sdl_nlen); + names[dl->sdl_nlen] = 0; + names += dl->sdl_nlen + 1; + + cif.ifa_addr = (struct sockaddr *)(void *)data; + memcpy(data, dl, (size_t)dl->sdl_len); + data += SA_RLEN((const struct sockaddr *)(const void *)dl); + + /* ifm_data needs to be aligned */ + cif.ifa_data = data = (void *)ALIGN(data); + memcpy(data, &ifm->ifm_data, sizeof(ifm->ifm_data)); + data += sizeof(ifm->ifm_data); + } else + idx = 0; + break; + + case RTM_NEWADDR: + ifam = (struct ifa_msghdr *)(void *)rtm; + if (idx && ifam->ifam_index != idx) + abort(); /* this cannot happen */ + + if (idx == 0 || (ifam->ifam_addrs & RTA_MASKS) == 0) + break; + ift->ifa_name = cif.ifa_name; + ift->ifa_flags = cif.ifa_flags; + ift->ifa_data = NULL; + p = (char *)(void *)(ifam + 1); + /* Scan to look for length of address */ + alen = 0; + for (p0 = p, i = 0; i < RTAX_MAX; i++) { + if ((RTA_MASKS & ifam->ifam_addrs & (1 << i)) + == 0) + continue; + sa = (struct sockaddr *)(void *)p; + len = SA_RLEN(sa); + if (i == RTAX_IFA) { + alen = len; + break; + } + p += len; + } + for (p = p0, i = 0; i < RTAX_MAX; i++) { + if ((RTA_MASKS & ifam->ifam_addrs & (1 << i)) + == 0) + continue; + sa = (struct sockaddr *)(void *)p; + len = SA_RLEN(sa); + switch (i) { + case RTAX_IFA: + ift->ifa_addr = + (struct sockaddr *)(void *)data; + memcpy(data, p, len); + data += len; + if (ift->ifa_addr->sa_family == AF_LINK) + ift->ifa_data = cif.ifa_data; + break; + + case RTAX_NETMASK: + ift->ifa_netmask = + (struct sockaddr *)(void *)data; + if (sa->sa_len == 0) { + memset(data, 0, alen); + data += alen; + break; + } + memcpy(data, p, len); + data += len; + break; + + case RTAX_BRD: + ift->ifa_broadaddr = + (struct sockaddr *)(void *)data; + memcpy(data, p, len); + data += len; + break; + } + p += len; + } + + + ift = (ift->ifa_next = ift + 1); + break; + } + } + + free(buf); + if (--ift >= ifa) { + ift->ifa_next = NULL; + *pif = ifa; + } else { + *pif = NULL; + free(ifa); + } + return (0); +} + +void +freeifaddrs(struct ifaddrs *ifp) +{ + + _DIAGASSERT(ifp != NULL); + + free(ifp); +} diff -pruN /home/reed/src/isc/libbind/libbind/irs/getnameinfo.c /usr/src/lib/libc/net/getnameinfo.c --- /home/reed/src/isc/libbind/libbind/irs/getnameinfo.c 2006-01-09 23:06:00.000000000 -0600 +++ /usr/src/lib/libc/net/getnameinfo.c 2013-06-05 09:26:12.000000000 -0500 @@ -1,22 +1,11 @@ -/* - * Issues to be discussed: - * - Thread safe-ness must be checked - */ - -#if ( defined(__linux__) || defined(__linux) || defined(LINUX) ) -#ifndef IF_NAMESIZE -# ifdef IFNAMSIZ -# define IF_NAMESIZE IFNAMSIZ -# else -# define IF_NAMESIZE 16 -# endif -#endif -#endif +/* $NetBSD: getnameinfo.c,v 1.53 2012/09/26 23:13:00 christos Exp $ */ +/* $KAME: getnameinfo.c,v 1.45 2000/09/25 22:43:56 itojun Exp $ */ /* + * Copyright (c) 2000 Ben Harris. * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. * All rights reserved. - * + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -25,14 +14,10 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by WIDE Project and - * its contributors. - * 4. Neither the name of the project nor the names of its contributors + * 3. Neither the name of the project nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE @@ -46,88 +31,227 @@ * SUCH DAMAGE. */ -#include +/* + * Issues to be discussed: + * - Thread safe-ness must be checked + * - RFC2553 says that we should raise error on short buffer. X/Open says + * we need to truncate the result. We obey RFC2553 (and X/Open should be + * modified). ipngwg rough consensus seems to follow RFC2553. + * - What is "local" in NI_FQDN? + * - NI_NAMEREQD and NI_NUMERICHOST conflict with each other. + * - (KAME extension) always attach textual scopeid (fe80::1%lo0), if + * sin6_scope_id is filled - standardization status? + * XXX breaks backward compat for code that expects no scopeid. + * beware on merge. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: getnameinfo.c,v 1.53 2012/09/26 23:13:00 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ +#include "namespace.h" #include #include - +#include +#include +#include +#include +#include +#include #include -#include #include -#include - +#include +#include +#include #include #include -#include #include +#include -#include +#include "servent.h" -/*% - * Note that a_off will be dynamically adjusted so that to be consistent - * with the definition of sockaddr_in{,6}. - * The value presented below is just a guess. - */ -static struct afd { - int a_af; - int a_addrlen; - size_t a_socklen; - int a_off; +#ifdef __weak_alias +__weak_alias(getnameinfo,_getnameinfo) +#endif + +static const struct afd { + int a_af; + socklen_t a_addrlen; + socklen_t a_socklen; + int a_off; } afdl [] = { - /* first entry is linked last... */ - {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in), - offsetof(struct sockaddr_in, sin_addr)}, +#ifdef INET6 {PF_INET6, sizeof(struct in6_addr), sizeof(struct sockaddr_in6), - offsetof(struct sockaddr_in6, sin6_addr)}, + offsetof(struct sockaddr_in6, sin6_addr)}, +#endif + {PF_INET, sizeof(struct in_addr), sizeof(struct sockaddr_in), + offsetof(struct sockaddr_in, sin_addr)}, {0, 0, 0, 0}, }; struct sockinet { -#ifdef HAVE_SA_LEN u_char si_len; -#endif u_char si_family; u_short si_port; }; -static int ip6_parsenumeric __P((const struct sockaddr *, const char *, char *, - size_t, int)); -#ifdef HAVE_SIN6_SCOPE_ID -static int ip6_sa2str __P((const struct sockaddr_in6 *, char *, size_t, int)); -#endif +static int getnameinfo_inet(const struct sockaddr *, socklen_t, char *, + socklen_t, char *, socklen_t, int); +#ifdef INET6 +static int ip6_parsenumeric(const struct sockaddr *, const char *, char *, + socklen_t, int); +static int ip6_sa2str(const struct sockaddr_in6 *, char *, size_t, int); +#endif +static int getnameinfo_atalk(const struct sockaddr *, socklen_t, char *, + socklen_t, char *, socklen_t, int); +static int getnameinfo_local(const struct sockaddr *, socklen_t, char *, + socklen_t, char *, socklen_t, int); + +static int getnameinfo_link(const struct sockaddr *, socklen_t, char *, + socklen_t, char *, socklen_t, int); +static int hexname(const uint8_t *, size_t, char *, socklen_t); +/* + * Top-level getnameinfo() code. Look at the address family, and pick an + * appropriate function to call. + */ int -getnameinfo(sa, salen, host, hostlen, serv, servlen, flags) - const struct sockaddr *sa; - size_t salen; - char *host; - size_t hostlen; - char *serv; - size_t servlen; - int flags; +getnameinfo(const struct sockaddr *sa, socklen_t salen, + char *host, socklen_t hostlen, + char *serv, socklen_t servlen, + int flags) +{ + + switch (sa->sa_family) { + case AF_APPLETALK: + return getnameinfo_atalk(sa, salen, host, hostlen, + serv, servlen, flags); + case AF_INET: + case AF_INET6: + return getnameinfo_inet(sa, salen, host, hostlen, + serv, servlen, flags); + case AF_LINK: + return getnameinfo_link(sa, salen, host, hostlen, + serv, servlen, flags); + case AF_LOCAL: + return getnameinfo_local(sa, salen, host, hostlen, + serv, servlen, flags); + default: + return EAI_FAMILY; + } +} + +/* + * getnameinfo_atalk(): + * Format an AppleTalk address into a printable format. + */ +/* ARGSUSED */ +static int +getnameinfo_atalk(const struct sockaddr *sa, socklen_t salen, + char *host, socklen_t hostlen, char *serv, socklen_t servlen, + int flags) +{ + char numserv[8]; + int n, m=0; + + const struct sockaddr_at *sat = + (const struct sockaddr_at *)(const void *)sa; + + if (serv != NULL && servlen > 0) { + snprintf(numserv, sizeof(numserv), "%u", sat->sat_port); + if (strlen(numserv) + 1 > servlen) + return EAI_MEMORY; + strlcpy(serv, numserv, servlen); + } + + n = snprintf(host, hostlen, "%u.%u", + ntohs(sat->sat_addr.s_net), sat->sat_addr.s_node); + + if (n < 0 || (socklen_t)(m+n) >= hostlen) + goto errout; + + m += n; + + if (sat->sat_range.r_netrange.nr_phase) { + n = snprintf(host+m, hostlen-m, " phase %u", + sat->sat_range.r_netrange.nr_phase); + + if (n < 0 || (socklen_t)(m+n) >= hostlen) + goto errout; + + m += n; + } + if (sat->sat_range.r_netrange.nr_firstnet) { + n = snprintf(host+m, hostlen-m, " range %u - %u", + ntohs(sat->sat_range.r_netrange.nr_firstnet), + ntohs(sat->sat_range.r_netrange.nr_lastnet )); + + if (n < 0 || (socklen_t)(m+n) >= hostlen) + goto errout; + + m += n; + } + + return 0; + +errout: + if (host && hostlen>0) + host[m] = '\0'; /* XXX ??? */ + + return EAI_MEMORY; +} + +/* + * getnameinfo_local(): + * Format an local address into a printable format. + */ +/* ARGSUSED */ +static int +getnameinfo_local(const struct sockaddr *sa, socklen_t salen, + char *host, socklen_t hostlen, char *serv, socklen_t servlen, + int flags) +{ + const struct sockaddr_un *sun = + (const struct sockaddr_un *)(const void *)sa; + + if (serv != NULL && servlen > 0) + serv[0] = '\0'; + + if (host && hostlen > 0) + strlcpy(host, sun->sun_path, + MIN(sizeof(sun->sun_path) + 1, hostlen)); + + return 0; +} + +/* + * getnameinfo_inet(): + * Format an IPv4 or IPv6 sockaddr into a printable string. + */ +static int +getnameinfo_inet(const struct sockaddr *sa, socklen_t salen, + char *host, socklen_t hostlen, + char *serv, socklen_t servlen, + int flags) { - struct afd *afd; + const struct afd *afd; struct servent *sp; struct hostent *hp; u_short port; -#ifdef HAVE_SA_LEN - size_t len; -#endif int family, i; const char *addr; - char *p; + uint32_t v4a; char numserv[512]; char numaddr[512]; - const struct sockaddr_in6 *sin6; + + /* sa is checked below */ + /* host may be NULL */ + /* serv may be NULL */ if (sa == NULL) return EAI_FAIL; -#ifdef HAVE_SA_LEN - len = sa->sa_len; - if (len != salen) return EAI_FAIL; -#endif - family = sa->sa_family; for (i = 0; afdl[i].a_af; i++) if (afdl[i].a_af == family) { @@ -137,38 +261,62 @@ getnameinfo(sa, salen, host, hostlen, se return EAI_FAMILY; found: - if (salen != afd->a_socklen) return EAI_FAIL; + if (salen != afd->a_socklen) + return EAI_FAIL; - port = ((const struct sockinet *)sa)->si_port; /*%< network byte order */ - addr = (const char *)sa + afd->a_off; + /* network byte order */ + port = ((const struct sockinet *)(const void *)sa)->si_port; + addr = (const char *)(const void *)sa + afd->a_off; - if (serv == NULL || servlen == 0U) { + if (serv == NULL || servlen == 0) { /* - * rfc2553bis says that serv == NULL or servlen == 0 means that - * the caller does not want the result. + * do nothing in this case. + * in case you are wondering if "&&" is more correct than + * "||" here: rfc2553bis-03 says that serv == NULL OR + * servlen == 0 means that the caller does not want the result. */ - } else if (flags & NI_NUMERICSERV) { - sprintf(numserv, "%d", ntohs(port)); - if (strlen(numserv) > servlen) - return EAI_MEMORY; - strcpy(serv, numserv); } else { - sp = getservbyport(port, (flags & NI_DGRAM) ? "udp" : "tcp"); + struct servent_data svd; + struct servent sv; + + if (flags & NI_NUMERICSERV) + sp = NULL; + else { + (void)memset(&svd, 0, sizeof(svd)); + sp = getservbyport_r(port, + (flags & NI_DGRAM) ? "udp" : "tcp", &sv, &svd); + } if (sp) { - if (strlen(sp->s_name) + 1 > servlen) + if (strlen(sp->s_name) + 1 > servlen) { + endservent_r(&svd); return EAI_MEMORY; - strcpy(serv, sp->s_name); - } else - return EAI_NONAME; + } + strlcpy(serv, sp->s_name, servlen); + endservent_r(&svd); + } else { + snprintf(numserv, sizeof(numserv), "%u", ntohs(port)); + if (strlen(numserv) + 1 > servlen) + return EAI_MEMORY; + strlcpy(serv, numserv, servlen); + } } switch (sa->sa_family) { case AF_INET: - if (ntohl(*(const u_int32_t *)addr) >> IN_CLASSA_NSHIFT == 0) - flags |= NI_NUMERICHOST; + v4a = (uint32_t) + ntohl(((const struct sockaddr_in *) + (const void *)sa)->sin_addr.s_addr); + if (IN_MULTICAST(v4a) || IN_EXPERIMENTAL(v4a)) + flags |= NI_NUMERICHOST; + v4a >>= IN_CLASSA_NSHIFT; + if (v4a == 0) + flags |= NI_NUMERICHOST; break; +#ifdef INET6 case AF_INET6: - sin6 = (const struct sockaddr_in6 *)sa; + { + const struct sockaddr_in6 *sin6; + sin6 = (const struct sockaddr_in6 *)(const void *)sa; switch (sin6->sin6_addr.s6_addr[0]) { case 0x00: if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) @@ -179,37 +327,78 @@ getnameinfo(sa, salen, host, hostlen, se flags |= NI_NUMERICHOST; break; default: - if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) + if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) { flags |= NI_NUMERICHOST; + } else if (IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr)) flags |= NI_NUMERICHOST; break; } + } break; +#endif } - if (host == NULL || hostlen == 0U) { + if (host == NULL || hostlen == 0) { /* - * rfc2553bis says that host == NULL or hostlen == 0 means that - * the caller does not want the result. + * do nothing in this case. + * in case you are wondering if "&&" is more correct than + * "||" here: rfc2553bis-03 says that host == NULL or + * hostlen == 0 means that the caller does not want the result. */ } else if (flags & NI_NUMERICHOST) { - goto numeric; + size_t numaddrlen; + + /* NUMERICHOST and NAMEREQD conflicts with each other */ + if (flags & NI_NAMEREQD) + return EAI_NONAME; + + switch(afd->a_af) { +#ifdef INET6 + case AF_INET6: + { + int error; + + if ((error = ip6_parsenumeric(sa, addr, host, + hostlen, flags)) != 0) + return(error); + break; + } +#endif + default: + if (inet_ntop(afd->a_af, addr, numaddr, + (socklen_t)sizeof(numaddr)) == NULL) + return EAI_SYSTEM; + numaddrlen = strlen(numaddr); + if (numaddrlen + 1 > hostlen) /* don't forget terminator */ + return EAI_MEMORY; + strlcpy(host, numaddr, hostlen); + break; + } } else { hp = gethostbyaddr(addr, afd->a_addrlen, afd->a_af); if (hp) { +#if 0 + /* + * commented out, since "for local host" is not + * implemented here - see RFC2553 p30 + */ if (flags & NI_NOFQDN) { + char *p; p = strchr(hp->h_name, '.'); - if (p) *p = '\0'; + if (p) + *p = '\0'; } - if (strlen(hp->h_name) + 1 > hostlen) +#endif + if (strlen(hp->h_name) + 1 > hostlen) { return EAI_MEMORY; - strcpy(host, hp->h_name); + } + strlcpy(host, hp->h_name, hostlen); } else { if (flags & NI_NAMEREQD) return EAI_NONAME; - numeric: switch(afd->a_af) { +#ifdef INET6 case AF_INET6: { int error; @@ -220,115 +409,198 @@ getnameinfo(sa, salen, host, hostlen, se return(error); break; } - +#endif default: - if (inet_ntop(afd->a_af, addr, numaddr, - sizeof(numaddr)) == NULL) - return EAI_NONAME; - if (strlen(numaddr) + 1 > hostlen) - return EAI_MEMORY; - strcpy(host, numaddr); + if (inet_ntop(afd->a_af, addr, host, + hostlen) == NULL) + return EAI_SYSTEM; + break; } } } return(0); } +#ifdef INET6 static int ip6_parsenumeric(const struct sockaddr *sa, const char *addr, char *host, - size_t hostlen, int flags) + socklen_t hostlen, int flags) { size_t numaddrlen; char numaddr[512]; -#ifndef HAVE_SIN6_SCOPE_ID - UNUSED(sa); - UNUSED(flags); -#endif + _DIAGASSERT(sa != NULL); + _DIAGASSERT(addr != NULL); + _DIAGASSERT(host != NULL); - if (inet_ntop(AF_INET6, addr, numaddr, sizeof(numaddr)) + if (inet_ntop(AF_INET6, addr, numaddr, (socklen_t)sizeof(numaddr)) == NULL) return EAI_SYSTEM; numaddrlen = strlen(numaddr); - if (numaddrlen + 1 > hostlen) /*%< don't forget terminator */ - return EAI_MEMORY; - strcpy(host, numaddr); - -#ifdef HAVE_SIN6_SCOPE_ID - if (((const struct sockaddr_in6 *)sa)->sin6_scope_id) { - char scopebuf[MAXHOSTNAMELEN]; /*%< XXX */ - int scopelen; - - /* ip6_sa2str never fails */ - scopelen = ip6_sa2str((const struct sockaddr_in6 *)sa, - scopebuf, sizeof(scopebuf), flags); - - if (scopelen + 1 + numaddrlen + 1 > hostlen) - return EAI_MEMORY; - - /* construct */ - memcpy(host + numaddrlen + 1, scopebuf, - scopelen); + if (numaddrlen + 1 > hostlen) /* don't forget terminator */ + return EAI_OVERFLOW; + strlcpy(host, numaddr, hostlen); + + if (((const struct sockaddr_in6 *)(const void *)sa)->sin6_scope_id) { + char zonebuf[MAXHOSTNAMELEN]; + int zonelen; + + zonelen = ip6_sa2str( + (const struct sockaddr_in6 *)(const void *)sa, + zonebuf, sizeof(zonebuf), flags); + if (zonelen < 0) + return EAI_OVERFLOW; + if ((size_t) zonelen + 1 + numaddrlen + 1 > hostlen) + return EAI_OVERFLOW; + /* construct */ + memcpy(host + numaddrlen + 1, zonebuf, + (size_t)zonelen); host[numaddrlen] = SCOPE_DELIMITER; - host[numaddrlen + 1 + scopelen] = '\0'; + host[numaddrlen + 1 + zonelen] = '\0'; } -#endif return 0; } -#ifdef HAVE_SIN6_SCOPE_ID /* ARGSUSED */ static int -ip6_sa2str(const struct sockaddr_in6 *sa6, char *buf, - size_t bufsiz, int flags) +ip6_sa2str(const struct sockaddr_in6 *sa6, char *buf, size_t bufsiz, int flags) { -#ifdef USE_IFNAMELINKID - unsigned int ifindex = (unsigned int)sa6->sin6_scope_id; - const struct in6_addr *a6 = &sa6->sin6_addr; -#endif - char tmp[64]; + unsigned int ifindex; + const struct in6_addr *a6; + int n; + + _DIAGASSERT(sa6 != NULL); + _DIAGASSERT(buf != NULL); + + ifindex = (unsigned int)sa6->sin6_scope_id; + a6 = &sa6->sin6_addr; #ifdef NI_NUMERICSCOPE - if (flags & NI_NUMERICSCOPE) { - sprintf(tmp, "%u", sa6->sin6_scope_id); - if (bufsiz != 0U) { - strncpy(buf, tmp, bufsiz - 1); - buf[bufsiz - 1] = '\0'; - } - return(strlen(tmp)); + if ((flags & NI_NUMERICSCOPE) != 0) { + n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id); + if (n < 0 || (size_t)n >= bufsiz) + return -1; + else + return n; } #endif -#ifdef USE_IFNAMELINKID - /* - * For a link-local address, convert the index to an interface - * name, assuming a one-to-one mapping between links and interfaces. - * Note, however, that this assumption is stronger than the - * specification of the scoped address architecture; the - * specficication says that more than one interfaces can belong to - * a single link. - */ - /* if_indextoname() does not take buffer size. not a good api... */ if ((IN6_IS_ADDR_LINKLOCAL(a6) || IN6_IS_ADDR_MC_LINKLOCAL(a6)) && bufsiz >= IF_NAMESIZE) { char *p = if_indextoname(ifindex, buf); if (p) { - return(strlen(p)); + return (int)strlen(p); } } -#endif /* last resort */ - sprintf(tmp, "%u", sa6->sin6_scope_id); - if (bufsiz != 0U) { - strncpy(buf, tmp, bufsiz - 1); - buf[bufsiz - 1] = '\0'; - } - return(strlen(tmp)); + n = snprintf(buf, bufsiz, "%u", sa6->sin6_scope_id); + if (n < 0 || (size_t) n >= bufsiz) + return -1; + else + return n; } +#endif /* INET6 */ + + +/* + * getnameinfo_link(): + * Format a link-layer address into a printable format, paying attention to + * the interface type. + */ +/* ARGSUSED */ +static int +getnameinfo_link(const struct sockaddr *sa, socklen_t salen, + char *host, socklen_t hostlen, char *serv, socklen_t servlen, + int flags) +{ + const struct sockaddr_dl *sdl = + (const struct sockaddr_dl *)(const void *)sa; + const struct ieee1394_hwaddr *iha; + int n; + + if (serv != NULL && servlen > 0) + *serv = '\0'; + + if (sdl->sdl_nlen == 0 && sdl->sdl_alen == 0 && sdl->sdl_slen == 0) { + n = snprintf(host, hostlen, "link#%u", sdl->sdl_index); + if (n < 0 || (socklen_t) n > hostlen) { + *host = '\0'; + return EAI_MEMORY; + } + return 0; + } + + switch (sdl->sdl_type) { +#ifdef IFT_ECONET + case IFT_ECONET: + if (sdl->sdl_alen < 2) + return EAI_FAMILY; + if (CLLADDR(sdl)[1] == 0) + n = snprintf(host, hostlen, "%u", CLLADDR(sdl)[0]); + else + n = snprintf(host, hostlen, "%u.%u", + CLLADDR(sdl)[1], CLLADDR(sdl)[0]); + if (n < 0 || (socklen_t) n >= hostlen) { + *host = '\0'; + return EAI_MEMORY; + } else + return 0; #endif + case IFT_IEEE1394: + if (sdl->sdl_alen < sizeof(iha->iha_uid)) + return EAI_FAMILY; + iha = + (const struct ieee1394_hwaddr *)(const void *)CLLADDR(sdl); + return hexname(iha->iha_uid, sizeof(iha->iha_uid), + host, hostlen); + /* + * The following have zero-length addresses. + * IFT_ATM (net/if_atmsubr.c) + * IFT_FAITH (net/if_faith.c) + * IFT_GIF (net/if_gif.c) + * IFT_LOOP (net/if_loop.c) + * IFT_PPP (net/if_ppp.c, net/if_spppsubr.c) + * IFT_SLIP (net/if_sl.c, net/if_strip.c) + * IFT_STF (net/if_stf.c) + * IFT_L2VLAN (net/if_vlan.c) + * IFT_PROPVIRTUAL (net/if_bridge.h> + */ + /* + * The following use IPv4 addresses as link-layer addresses: + * IFT_OTHER (net/if_gre.c) + */ + case IFT_ARCNET: /* default below is believed correct for all these. */ + case IFT_ETHER: + case IFT_FDDI: + case IFT_HIPPI: + case IFT_ISO88025: + default: + return hexname((const uint8_t *)CLLADDR(sdl), + (size_t)sdl->sdl_alen, host, hostlen); + } +} -/*! \file */ +static int +hexname(const uint8_t *cp, size_t len, char *host, socklen_t hostlen) +{ + int n; + size_t i; + char *outp = host; + + *outp = '\0'; + for (i = 0; i < len; i++) { + n = snprintf(outp, hostlen, "%s%02x", + i ? ":" : "", cp[i]); + if (n < 0 || (socklen_t) n >= hostlen) { + *host = '\0'; + return EAI_MEMORY; + } + outp += n; + hostlen -= n; + } + return 0; +} diff -pruN /home/reed/src/isc/libbind/libbind/irs/getnetent.c /usr/src/lib/libc/net/getnetent.c --- /home/reed/src/isc/libbind/libbind/irs/getnetent.c 2005-04-26 23:56:25.000000000 -0500 +++ /usr/src/lib/libc/net/getnetent.c 2013-06-05 09:26:12.000000000 -0500 @@ -1,345 +1,168 @@ +/* $NetBSD: getnetent.c,v 1.21 2012/03/20 17:44:18 matt Exp $ */ + /* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1996,1999 by Internet Software Consortium. + * Copyright (c) 1983, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * Portions Copyright (c) 1993 Carlos Leandro and Rui Salgueiro + * Dep. Matematica Universidade de Coimbra, Portugal, Europe * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * from getnetent.c 1.1 (Coimbra) 93/06/02 */ -#if !defined(LINT) && !defined(CODECENTER) -static const char rcsid[] = "$Id: getnetent.c,v 1.7 2005/04/27 04:56:25 sra Exp $"; +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)getnetent.c 8.1 (Berkeley) 6/4/93"; +static char rcsid[] = "Id: getnetent.c,v 8.4 1997/06/01 20:34:37 vixie Exp "; +#else +__RCSID("$NetBSD: getnetent.c,v 1.21 2012/03/20 17:44:18 matt Exp $"); #endif +#endif /* LIBC_SCCS and not lint */ -/* Imports */ - -#include "port_before.h" - -#if !defined(__BIND_NOSTATIC) - +#include "namespace.h" #include #include - #include -#include #include - -#include -#include #include -#include -#include +#include #include -#include - -#include "port_after.h" - -#include "irs_p.h" -#include "irs_data.h" - -/* Definitions */ - -struct pvt { - struct netent netent; - char * aliases[1]; - char name[MAXDNAME + 1]; -}; - -/* Forward */ - -static struct net_data *init(void); -static struct netent *nw_to_net(struct nwent *, struct net_data *); -static void freepvt(struct net_data *); -static struct netent *fakeaddr(const char *, int af, struct net_data *); - -/* Portability */ - -#ifndef INADDR_NONE -# define INADDR_NONE 0xffffffff +#ifdef __weak_alias +__weak_alias(endnetent,_endnetent) +__weak_alias(getnetent,_getnetent) +__weak_alias(setnetent,_setnetent) #endif -/* Public */ - -struct netent * -getnetent() { - struct net_data *net_data = init(); - - return (getnetent_p(net_data)); -} - -struct netent * -getnetbyname(const char *name) { - struct net_data *net_data = init(); - - return (getnetbyname_p(name, net_data)); -} +#define MAXALIASES 35 -struct netent * -getnetbyaddr(unsigned long net, int type) { - struct net_data *net_data = init(); +static FILE *netf; +static char line[BUFSIZ+1]; +static struct netent net; +static char *net_aliases[MAXALIASES]; +int _net_stayopen; - return (getnetbyaddr_p(net, type, net_data)); -} +static void __setnetent(int); +static void __endnetent(void); void -setnetent(int stayopen) { - struct net_data *net_data = init(); +setnetent(int stayopen) +{ - setnetent_p(stayopen, net_data); + sethostent(stayopen); + __setnetent(stayopen); } - void -endnetent() { - struct net_data *net_data = init(); +endnetent(void) +{ - endnetent_p(net_data); + endhostent(); + __endnetent(); } -/* Shared private. */ - -struct netent * -getnetent_p(struct net_data *net_data) { - struct irs_nw *nw; +static void +__setnetent(int f) +{ - if (!net_data || !(nw = net_data->nw)) - return (NULL); - net_data->nww_last = (*nw->next)(nw); - net_data->nw_last = nw_to_net(net_data->nww_last, net_data); - return (net_data->nw_last); + if (netf == NULL) + netf = fopen(_PATH_NETWORKS, "re"); + else + rewind(netf); + _net_stayopen |= f; } -struct netent * -getnetbyname_p(const char *name, struct net_data *net_data) { - struct irs_nw *nw; - struct netent *np; - char **nap; +static void +__endnetent(void) +{ - if (!net_data || !(nw = net_data->nw)) - return (NULL); - if (net_data->nw_stayopen && net_data->nw_last) { - if (!strcmp(net_data->nw_last->n_name, name)) - return (net_data->nw_last); - for (nap = net_data->nw_last->n_aliases; nap && *nap; nap++) - if (!strcmp(name, *nap)) - return (net_data->nw_last); + if (netf) { + fclose(netf); + netf = NULL; } - if ((np = fakeaddr(name, AF_INET, net_data)) != NULL) - return (np); - net_data->nww_last = (*nw->byname)(nw, name, AF_INET); - net_data->nw_last = nw_to_net(net_data->nww_last, net_data); - if (!net_data->nw_stayopen) - endnetent(); - return (net_data->nw_last); + _net_stayopen = 0; } struct netent * -getnetbyaddr_p(unsigned long net, int type, struct net_data *net_data) { - struct irs_nw *nw; - u_char addr[4]; - int bits; +getnetent(void) +{ + char *p; + register char *cp, **q; - if (!net_data || !(nw = net_data->nw)) + if (netf == NULL && (netf = fopen(_PATH_NETWORKS, "re")) == NULL) return (NULL); - if (net_data->nw_stayopen && net_data->nw_last) - if (type == net_data->nw_last->n_addrtype && - net == net_data->nw_last->n_net) - return (net_data->nw_last); - - /* cannonize net(host order) */ - if (net < 256UL) { - net <<= 24; - bits = 8; - } else if (net < 65536UL) { - net <<= 16; - bits = 16; - } else if (net < 16777216UL) { - net <<= 8; - bits = 24; - } else - bits = 32; - - /* convert to net order */ - addr[0] = (0xFF000000 & net) >> 24; - addr[1] = (0x00FF0000 & net) >> 16; - addr[2] = (0x0000FF00 & net) >> 8; - addr[3] = (0x000000FF & net); - - /* reduce bits to as close to natural number as possible */ - if ((bits == 32) && (addr[0] < 224) && (addr[3] == 0)) { - if ((addr[0] < 192) && (addr[2] == 0)) { - if ((addr[0] < 128) && (addr[1] == 0)) - bits = 8; - else - bits = 16; - } else { - bits = 24; - } - } - - net_data->nww_last = (*nw->byaddr)(nw, addr, bits, AF_INET); - net_data->nw_last = nw_to_net(net_data->nww_last, net_data); - if (!net_data->nw_stayopen) - endnetent(); - return (net_data->nw_last); -} - - - - -void -setnetent_p(int stayopen, struct net_data *net_data) { - struct irs_nw *nw; - - if (!net_data || !(nw = net_data->nw)) - return; - freepvt(net_data); - (*nw->rewind)(nw); - net_data->nw_stayopen = (stayopen != 0); - if (stayopen == 0) - net_data_minimize(net_data); -} - -void -endnetent_p(struct net_data *net_data) { - struct irs_nw *nw; - - if ((net_data != NULL) && ((nw = net_data->nw) != NULL)) - (*nw->minimize)(nw); -} - -/* Private */ - -static struct net_data * -init() { - struct net_data *net_data; - - if (!(net_data = net_data_init(NULL))) - goto error; - if (!net_data->nw) { - net_data->nw = (*net_data->irs->nw_map)(net_data->irs); - - if (!net_data->nw || !net_data->res) { - error: - errno = EIO; - return (NULL); +#if (defined(__sparc__) && defined(_LP64)) || \ + defined(__alpha__) || \ + (defined(__i386__) && defined(_LP64)) || \ + (defined(__sh__) && defined(_LP64)) + net.__n_pad0 = 0; +#endif +again: + p = fgets(line, (int)sizeof line, netf); + if (p == NULL) + return (NULL); + if (*p == '#') + goto again; + cp = strpbrk(p, "#\n"); + if (cp == NULL) + goto again; + *cp = '\0'; + net.n_name = p; + cp = strpbrk(p, " \t"); + if (cp == NULL) + goto again; + *cp++ = '\0'; + while (*cp == ' ' || *cp == '\t') + cp++; + p = strpbrk(cp, " \t"); + if (p != NULL) + *p++ = '\0'; + net.n_net = inet_network(cp); + net.n_addrtype = AF_INET; + q = net.n_aliases = net_aliases; + if (p != NULL) { + cp = p; + while (cp && *cp) { + if (*cp == ' ' || *cp == '\t') { + cp++; + continue; + } + if (q < &net_aliases[MAXALIASES - 1]) + *q++ = cp; + cp = strpbrk(cp, " \t"); + if (cp != NULL) + *cp++ = '\0'; } - (*net_data->nw->res_set)(net_data->nw, net_data->res, NULL); - } - - return (net_data); -} - -static void -freepvt(struct net_data *net_data) { - if (net_data->nw_data) { - free(net_data->nw_data); - net_data->nw_data = NULL; - } -} - -static struct netent * -fakeaddr(const char *name, int af, struct net_data *net_data) { - struct pvt *pvt; - const char *cp; - u_long tmp; - - if (af != AF_INET) { - /* XXX should support IPv6 some day */ - errno = EAFNOSUPPORT; - RES_SET_H_ERRNO(net_data->res, NETDB_INTERNAL); - return (NULL); - } - if (!isascii((unsigned char)(name[0])) || - !isdigit((unsigned char)(name[0]))) - return (NULL); - for (cp = name; *cp; ++cp) - if (!isascii(*cp) || (!isdigit((unsigned char)*cp) && *cp != '.')) - return (NULL); - if (*--cp == '.') - return (NULL); - - /* All-numeric, no dot at the end. */ - - tmp = inet_network(name); - if (tmp == INADDR_NONE) { - RES_SET_H_ERRNO(net_data->res, HOST_NOT_FOUND); - return (NULL); } - - /* Valid network number specified. - * Fake up a netent as if we'd actually - * done a lookup. - */ - freepvt(net_data); - net_data->nw_data = malloc(sizeof (struct pvt)); - if (!net_data->nw_data) { - errno = ENOMEM; - RES_SET_H_ERRNO(net_data->res, NETDB_INTERNAL); - return (NULL); - } - pvt = net_data->nw_data; - - strncpy(pvt->name, name, MAXDNAME); - pvt->name[MAXDNAME] = '\0'; - pvt->netent.n_name = pvt->name; - pvt->netent.n_addrtype = AF_INET; - pvt->netent.n_aliases = pvt->aliases; - pvt->aliases[0] = NULL; - pvt->netent.n_net = tmp; - - return (&pvt->netent); + *q = NULL; + return (&net); } - -static struct netent * -nw_to_net(struct nwent *nwent, struct net_data *net_data) { - struct pvt *pvt; - u_long addr = 0; - int i; - int msbyte; - - if (!nwent || nwent->n_addrtype != AF_INET) - return (NULL); - freepvt(net_data); - net_data->nw_data = malloc(sizeof (struct pvt)); - if (!net_data->nw_data) { - errno = ENOMEM; - RES_SET_H_ERRNO(net_data->res, NETDB_INTERNAL); - return (NULL); - } - pvt = net_data->nw_data; - pvt->netent.n_name = nwent->n_name; - pvt->netent.n_aliases = nwent->n_aliases; - pvt->netent.n_addrtype = nwent->n_addrtype; - -/*% - * What this code does: Converts net addresses from network to host form. - * - * msbyte: the index of the most significant byte in the n_addr array. - * - * Shift bytes in significant order into addr. When all signicant - * bytes are in, zero out bits in the LSB that are not part of the network. - */ - msbyte = nwent->n_length / 8 + - ((nwent->n_length % 8) != 0 ? 1 : 0) - 1; - for (i = 0; i <= msbyte; i++) - addr = (addr << 8) | ((unsigned char *)nwent->n_addr)[i]; - i = (32 - nwent->n_length) % 8; - if (i != 0) - addr &= ~((1 << (i + 1)) - 1); - pvt->netent.n_net = addr; - return (&pvt->netent); -} - -#endif /*__BIND_NOSTATIC*/ - -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/getnetent_r.c /usr/src/lib/libc/net/getnetent_r.c --- /home/reed/src/isc/libbind/libbind/irs/getnetent_r.c 2005-09-03 07:41:38.000000000 -0500 +++ /usr/src/lib/libc/net/getnetent_r.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,234 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1998-1999 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#if defined(LIBC_SCCS) && !defined(lint) -static const char rcsid[] = "$Id: getnetent_r.c,v 1.6 2005/09/03 12:41:38 marka Exp $"; -#endif /* LIBC_SCCS and not lint */ - -#include -#if !defined(_REENTRANT) || !defined(DO_PTHREADS) - static int getnetent_r_not_required = 0; -#else -#include -#include -#include -#include -#include -#include -#include -#include - -#ifdef NET_R_RETURN - -static NET_R_RETURN -copy_netent(struct netent *, struct netent *, NET_R_COPY_ARGS); - -NET_R_RETURN -getnetbyname_r(const char *name, struct netent *nptr, NET_R_ARGS) { - struct netent *ne = getnetbyname(name); -#ifdef NET_R_SETANSWER - int n = 0; - - if (ne == NULL || (n = copy_netent(ne, nptr, NET_R_COPY)) != 0) - *answerp = NULL; - else - *answerp = ne; - if (ne == NULL) - *h_errnop = h_errno; - return (n); -#else - if (ne == NULL) - return (NET_R_BAD); - - return (copy_netent(ne, nptr, NET_R_COPY)); -#endif -} - -#ifndef GETNETBYADDR_ADDR_T -#define GETNETBYADDR_ADDR_T long -#endif -NET_R_RETURN -getnetbyaddr_r(GETNETBYADDR_ADDR_T addr, int type, struct netent *nptr, NET_R_ARGS) { - struct netent *ne = getnetbyaddr(addr, type); -#ifdef NET_R_SETANSWER - int n = 0; - - if (ne == NULL || (n = copy_netent(ne, nptr, NET_R_COPY)) != 0) - *answerp = NULL; - else - *answerp = ne; - if (ne == NULL) - *h_errnop = h_errno; - return (n); -#else - - if (ne == NULL) - return (NET_R_BAD); - - return (copy_netent(ne, nptr, NET_R_COPY)); -#endif -} - -/*% - * These assume a single context is in operation per thread. - * If this is not the case we will need to call irs directly - * rather than through the base functions. - */ - -NET_R_RETURN -getnetent_r(struct netent *nptr, NET_R_ARGS) { - struct netent *ne = getnetent(); -#ifdef NET_R_SETANSWER - int n = 0; - - if (ne == NULL || (n = copy_netent(ne, nptr, NET_R_COPY)) != 0) - *answerp = NULL; - else - *answerp = ne; - if (ne == NULL) - *h_errnop = h_errno; - return (n); -#else - - if (ne == NULL) - return (NET_R_BAD); - - return (copy_netent(ne, nptr, NET_R_COPY)); -#endif -} - -NET_R_SET_RETURN -#ifdef NET_R_ENT_ARGS -setnetent_r(int stay_open, NET_R_ENT_ARGS) -#else -setnetent_r(int stay_open) -#endif -{ -#ifdef NET_R_ENT_ARGS - UNUSED(ndptr); -#endif - setnetent(stay_open); -#ifdef NET_R_SET_RESULT - return (NET_R_SET_RESULT); -#endif -} - -NET_R_END_RETURN -#ifdef NET_R_ENT_ARGS -endnetent_r(NET_R_ENT_ARGS) -#else -endnetent_r() -#endif -{ -#ifdef NET_R_ENT_ARGS - UNUSED(ndptr); -#endif - endnetent(); - NET_R_END_RESULT(NET_R_OK); -} - -/* Private */ - -#ifndef NETENT_DATA -static NET_R_RETURN -copy_netent(struct netent *ne, struct netent *nptr, NET_R_COPY_ARGS) { - char *cp; - int i, n; - int numptr, len; - - /* Find out the amount of space required to store the answer. */ - numptr = 1; /*%< NULL ptr */ - len = (char *)ALIGN(buf) - buf; - for (i = 0; ne->n_aliases[i]; i++, numptr++) { - len += strlen(ne->n_aliases[i]) + 1; - } - len += strlen(ne->n_name) + 1; - len += numptr * sizeof(char*); - - if (len > (int)buflen) { - errno = ERANGE; - return (NET_R_BAD); - } - - /* copy net value and type */ - nptr->n_addrtype = ne->n_addrtype; - nptr->n_net = ne->n_net; - - cp = (char *)ALIGN(buf) + numptr * sizeof(char *); - - /* copy official name */ - n = strlen(ne->n_name) + 1; - strcpy(cp, ne->n_name); - nptr->n_name = cp; - cp += n; - - /* copy aliases */ - nptr->n_aliases = (char **)ALIGN(buf); - for (i = 0 ; ne->n_aliases[i]; i++) { - n = strlen(ne->n_aliases[i]) + 1; - strcpy(cp, ne->n_aliases[i]); - nptr->n_aliases[i] = cp; - cp += n; - } - nptr->n_aliases[i] = NULL; - - return (NET_R_OK); -} -#else /* !NETENT_DATA */ -static int -copy_netent(struct netent *ne, struct netent *nptr, NET_R_COPY_ARGS) { - char *cp, *eob; - int i, n; - - /* copy net value and type */ - nptr->n_addrtype = ne->n_addrtype; - nptr->n_net = ne->n_net; - - /* copy official name */ - cp = ndptr->line; - eob = ndptr->line + sizeof(ndptr->line); - if ((n = strlen(ne->n_name) + 1) < (eob - cp)) { - strcpy(cp, ne->n_name); - nptr->n_name = cp; - cp += n; - } else { - return (-1); - } - - /* copy aliases */ - i = 0; - nptr->n_aliases = ndptr->net_aliases; - while (ne->n_aliases[i] && i < (_MAXALIASES-1)) { - if ((n = strlen(ne->n_aliases[i]) + 1) < (eob - cp)) { - strcpy(cp, ne->n_aliases[i]); - nptr->n_aliases[i] = cp; - cp += n; - } else { - break; - } - i++; - } - nptr->n_aliases[i] = NULL; - - return (NET_R_OK); -} -#endif /* !NETENT_DATA */ -#else /* NET_R_RETURN */ - static int getnetent_r_unknown_system = 0; -#endif /* NET_R_RETURN */ -#endif /* !defined(_REENTRANT) || !defined(DO_PTHREADS) */ -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/getnetgrent.c /usr/src/lib/libc/net/getnetgrent.c --- /home/reed/src/isc/libbind/libbind/irs/getnetgrent.c 2008-11-13 20:36:51.000000000 -0600 +++ /usr/src/lib/libc/net/getnetgrent.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,160 +0,0 @@ -/* - * Copyright (C) 2004, 2005, 2008 Internet Systems Consortium, Inc. ("ISC") - * Copyright (C) 1996-1999, 2001, 2003 Internet Software Consortium. - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH - * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY - * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, - * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM - * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE - * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR - * PERFORMANCE OF THIS SOFTWARE. - */ - -#if defined(LIBC_SCCS) && !defined(lint) -static const char rcsid[] = "$Id: getnetgrent.c,v 1.6 2008/11/14 02:36:51 marka Exp $"; -#endif /* LIBC_SCCS and not lint */ - -/* Imports */ - -#include "port_before.h" - -#if !defined(__BIND_NOSTATIC) - -#include - -#include -#include - -#include -#include -#include - -#include - -#include "port_after.h" - -#include "irs_data.h" - -/* Forward */ - -static struct net_data *init(void); - - -/* Public */ - -#ifndef SETNETGRENT_ARGS -#define SETNETGRENT_ARGS const char *netgroup -#endif -void -setnetgrent(SETNETGRENT_ARGS) { - struct net_data *net_data = init(); - - setnetgrent_p(netgroup, net_data); -} - -void -endnetgrent(void) { - struct net_data *net_data = init(); - - endnetgrent_p(net_data); -} - -#ifndef INNETGR_ARGS -#define INNETGR_ARGS const char *netgroup, const char *host, \ - const char *user, const char *domain -#endif -int -innetgr(INNETGR_ARGS) { - struct net_data *net_data = init(); - - return (innetgr_p(netgroup, host, user, domain, net_data)); -} - -int -getnetgrent(NGR_R_CONST char **host, NGR_R_CONST char **user, - NGR_R_CONST char **domain) -{ - struct net_data *net_data = init(); - const char *ch, *cu, *cd; - int ret; - - ret = getnetgrent_p(&ch, &cu, &cd, net_data); - if (ret != 1) - return (ret); - - DE_CONST(ch, *host); - DE_CONST(cu, *user); - DE_CONST(cd, *domain); - return (ret); -} - -/* Shared private. */ - -void -setnetgrent_p(const char *netgroup, struct net_data *net_data) { - struct irs_ng *ng; - - if ((net_data != NULL) && ((ng = net_data->ng) != NULL)) - (*ng->rewind)(ng, netgroup); -} - -void -endnetgrent_p(struct net_data *net_data) { - struct irs_ng *ng; - - if (!net_data) - return; - if ((ng = net_data->ng) != NULL) - (*ng->close)(ng); - net_data->ng = NULL; -} - -int -innetgr_p(const char *netgroup, const char *host, - const char *user, const char *domain, - struct net_data *net_data) { - struct irs_ng *ng; - - if (!net_data || !(ng = net_data->ng)) - return (0); - return ((*ng->test)(ng, netgroup, host, user, domain)); -} - -int -getnetgrent_p(const char **host, const char **user, const char **domain, - struct net_data *net_data ) { - struct irs_ng *ng; - - if (!net_data || !(ng = net_data->ng)) - return (0); - return ((*ng->next)(ng, host, user, domain)); -} - -/* Private */ - -static struct net_data * -init(void) { - struct net_data *net_data; - - if (!(net_data = net_data_init(NULL))) - goto error; - if (!net_data->ng) { - net_data->ng = (*net_data->irs->ng_map)(net_data->irs); - if (!net_data->ng) { - error: - errno = EIO; - return (NULL); - } - } - - return (net_data); -} - -#endif /*__BIND_NOSTATIC*/ - -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/getnetgrent_r.c /usr/src/lib/libc/net/getnetgrent_r.c --- /home/reed/src/isc/libbind/libbind/irs/getnetgrent_r.c 2008-11-13 20:36:51.000000000 -0600 +++ /usr/src/lib/libc/net/getnetgrent_r.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,219 +0,0 @@ -/* - * Copyright (C) 2004, 2005, 2008 Internet Systems Consortium, Inc. ("ISC") - * Copyright (C) 1998, 1999, 2001, 2003 Internet Software Consortium. - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH - * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY - * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, - * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM - * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE - * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR - * PERFORMANCE OF THIS SOFTWARE. - */ - -#if defined(LIBC_SCCS) && !defined(lint) -static const char rcsid[] = "$Id: getnetgrent_r.c,v 1.14 2008/11/14 02:36:51 marka Exp $"; -#endif /* LIBC_SCCS and not lint */ - -#include -#if !defined(_REENTRANT) || !defined(DO_PTHREADS) - static int getnetgrent_r_not_required = 0; -#else -#include -#include -#include -#include -#include -#include -#include -#include - -#ifdef NGR_R_RETURN -#ifndef NGR_R_PRIVATE -#define NGR_R_PRIVATE 0 -#endif - -static NGR_R_RETURN -copy_protoent(NGR_R_CONST char **, NGR_R_CONST char **, NGR_R_CONST char **, - const char *, const char *, const char *, NGR_R_COPY_ARGS); - -NGR_R_RETURN -innetgr_r(const char *netgroup, const char *host, const char *user, - const char *domain) { - char *ng, *ho, *us, *dom; - - DE_CONST(netgroup, ng); - DE_CONST(host, ho); - DE_CONST(user, us); - DE_CONST(domain, dom); - - return (innetgr(ng, ho, us, dom)); -} - -/*% - * These assume a single context is in operation per thread. - * If this is not the case we will need to call irs directly - * rather than through the base functions. - */ - -NGR_R_RETURN -getnetgrent_r(NGR_R_CONST char **machinep, NGR_R_CONST char **userp, - NGR_R_CONST char **domainp, NGR_R_ARGS) -{ - NGR_R_CONST char *mp, *up, *dp; - int res = getnetgrent(&mp, &up, &dp); - - if (res != 1) - return (res); - - return (copy_protoent(machinep, userp, domainp, - mp, up, dp, NGR_R_COPY)); -} - -#if NGR_R_PRIVATE == 2 -struct private { - char *buf; -}; - -#endif -NGR_R_SET_RETURN -#ifdef NGR_R_SET_ARGS -setnetgrent_r(NGR_R_SET_CONST char *netgroup, NGR_R_SET_ARGS) -#else -setnetgrent_r(NGR_R_SET_CONST char *netgroup) -#endif -{ -#if NGR_R_PRIVATE == 2 - struct private *p; -#endif - char *tmp; -#if defined(NGR_R_SET_ARGS) && NGR_R_PRIVATE == 0 - UNUSED(buf); - UNUSED(buflen); -#endif - - DE_CONST(netgroup, tmp); - setnetgrent(tmp); - -#if NGR_R_PRIVATE == 1 - *buf = NULL; -#elif NGR_R_PRIVATE == 2 - *buf = p = malloc(sizeof(struct private)); - if (p == NULL) -#ifdef NGR_R_SET_RESULT - return (NGR_R_BAD); -#else - return; -#endif - p->buf = NULL; -#endif -#ifdef NGR_R_SET_RESULT - return (NGR_R_SET_RESULT); -#endif -} - -NGR_R_END_RETURN -#ifdef NGR_R_END_ARGS -endnetgrent_r(NGR_R_END_ARGS) -#else -endnetgrent_r(void) -#endif -{ -#if NGR_R_PRIVATE == 2 - struct private *p = buf; -#endif -#if defined(NGR_R_SET_ARGS) && NGR_R_PRIVATE == 0 - UNUSED(buf); - UNUSED(buflen); -#endif - - endnetgrent(); -#if NGR_R_PRIVATE == 1 - if (*buf != NULL) - free(*buf); - *buf = NULL; -#elif NGR_R_PRIVATE == 2 - if (p->buf != NULL) - free(p->buf); - free(p); -#endif - NGR_R_END_RESULT(NGR_R_OK); -} - -/* Private */ - -static int -copy_protoent(NGR_R_CONST char **machinep, NGR_R_CONST char **userp, - NGR_R_CONST char **domainp, const char *mp, const char *up, - const char *dp, NGR_R_COPY_ARGS) -{ -#if NGR_R_PRIVATE == 2 - struct private *p = buf; -#endif - char *cp; - int n; - int len; - - /* Find out the amount of space required to store the answer. */ - len = 0; - if (mp != NULL) len += strlen(mp) + 1; - if (up != NULL) len += strlen(up) + 1; - if (dp != NULL) len += strlen(dp) + 1; - -#if NGR_R_PRIVATE == 1 - if (*buf != NULL) - free(*buf); - *buf = malloc(len); - if (*buf == NULL) - return(NGR_R_BAD); - cp = *buf; -#elif NGR_R_PRIVATE == 2 - if (p->buf) - free(p->buf); - p->buf = malloc(len); - if (p->buf == NULL) - return(NGR_R_BAD); - cp = p->buf; -#else - if (len > (int)buflen) { - errno = ERANGE; - return (NGR_R_BAD); - } - cp = buf; -#endif - - if (mp != NULL) { - n = strlen(mp) + 1; - strcpy(cp, mp); - *machinep = cp; - cp += n; - } else - *machinep = NULL; - - if (up != NULL) { - n = strlen(up) + 1; - strcpy(cp, up); - *userp = cp; - cp += n; - } else - *userp = NULL; - - if (dp != NULL) { - n = strlen(dp) + 1; - strcpy(cp, dp); - *domainp = cp; - cp += n; - } else - *domainp = NULL; - - return (NGR_R_OK); -} -#else /* NGR_R_RETURN */ - static int getnetgrent_r_unknown_system = 0; -#endif /* NGR_R_RETURN */ -#endif /* !defined(_REENTRANT) || !defined(DO_PTHREADS) */ -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/getnetnamadr.c /usr/src/lib/libc/net/getnetnamadr.c --- /home/reed/src/isc/libbind/libbind/irs/getnetnamadr.c 1969-12-31 18:00:00.000000000 -0600 +++ /usr/src/lib/libc/net/getnetnamadr.c 2013-06-05 09:26:12.000000000 -0500 @@ -0,0 +1,648 @@ +/* $NetBSD: getnetnamadr.c,v 1.42 2012/03/13 21:13:41 christos Exp $ */ + +/* Copyright (c) 1993 Carlos Leandro and Rui Salgueiro + * Dep. Matematica Universidade de Coimbra, Portugal, Europe + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + */ +/* + * Copyright (c) 1983, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)getnetbyaddr.c 8.1 (Berkeley) 6/4/93"; +static char sccsid_[] = "from getnetnamadr.c 1.4 (Coimbra) 93/06/03"; +static char rcsid[] = "Id: getnetnamadr.c,v 8.8 1997/06/01 20:34:37 vixie Exp "; +#else +__RCSID("$NetBSD: getnetnamadr.c,v 1.42 2012/03/13 21:13:41 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef YP +#include +#include +#include +#endif + +#ifdef __weak_alias +__weak_alias(getnetbyaddr,_getnetbyaddr) +__weak_alias(getnetbyname,_getnetbyname) +#endif + +extern int _net_stayopen; + +#define BYADDR 0 +#define BYNAME 1 +#define MAXALIASES 35 + +#define MAXPACKET (64*1024) + +typedef union { + HEADER hdr; + u_char buf[MAXPACKET]; +} querybuf; + +typedef union { + long al; + char ac; +} align; + +#ifdef YP +static char *__ypdomain; +static char *__ypcurrent; +static int __ypcurrentlen; +#endif + +static struct netent net_entry; +static char *net_aliases[MAXALIASES]; + +static int parse_reversed_addr(const char *, in_addr_t *); +static struct netent *getnetanswer(querybuf *, int, int); +static int _files_getnetbyaddr(void *, void *, va_list); +static int _files_getnetbyname(void *, void *, va_list); +static int _dns_getnetbyaddr(void *, void *, va_list); +static int _dns_getnetbyname(void *, void *, va_list); +#ifdef YP +static int _yp_getnetbyaddr(void *, void *, va_list); +static int _yp_getnetbyname(void *, void *, va_list); +static struct netent *_ypnetent(char *); +#endif + +/* + * parse_reversed_addr -- + * parse str, which should be of the form 'd.c.b.a.IN-ADDR.ARPA' + * (a PTR as per RFC 1101) and convert into an in_addr_t of the + * address 'a.b.c.d'. + * returns 0 on success (storing in *result), or -1 on error. + */ +static int +parse_reversed_addr(const char *str, in_addr_t *result) +{ + unsigned long octet[4]; + const char *sp; + char *ep; + int octidx; + + sp = str; + /* find the four octets 'd.b.c.a.' */ + for (octidx = 0; octidx < 4; octidx++) { + /* ensure it's a number */ + if (!isdigit((unsigned char)*sp)) + return -1; + octet[octidx] = strtoul(sp, &ep, 10); + /* with a trailing '.' */ + if (*ep != '.') + return -1; + /* and is 0 <= octet <= 255 */ + if (octet[octidx] > 255) + return -1; + sp = ep + 1; + } + /* ensure trailer is correct */ + if (strcasecmp(sp, "IN-ADDR.ARPA") != 0) + return -1; + *result = 0; + /* build result from octets in reverse */ + for (octidx = 3; octidx >= 0; octidx--) { + *result <<= 8; + *result |= (in_addr_t)(octet[octidx] & 0xff); + } + return 0; +} + +static struct netent * +getnetanswer(querybuf *answer, int anslen, int net_i) +{ + static char n_name[MAXDNAME]; + static char netbuf[PACKETSZ]; + + HEADER *hp; + u_char *cp; + int n; + u_char *eom; + int type, class, ancount, qdcount, haveanswer; + char *in, *bp, **ap, *ep; + + _DIAGASSERT(answer != NULL); + + /* + * find first satisfactory answer + * + * answer --> +------------+ ( MESSAGE ) + * | Header | + * +------------+ + * | Question | the question for the name server + * +------------+ + * | Answer | RRs answering the question + * +------------+ + * | Authority | RRs pointing toward an authority + * | Additional | RRs holding additional information + * +------------+ + */ + eom = answer->buf + anslen; + hp = &answer->hdr; + ancount = ntohs(hp->ancount); /* #/records in the answer section */ + qdcount = ntohs(hp->qdcount); /* #/entries in the question section */ + bp = netbuf; + ep = netbuf + sizeof(netbuf); + cp = answer->buf + HFIXEDSZ; + if (!qdcount) { + if (hp->aa) + h_errno = HOST_NOT_FOUND; + else + h_errno = TRY_AGAIN; + return NULL; + } + while (qdcount-- > 0) { + n = __dn_skipname(cp, eom); + if (n < 0 || (cp + n + QFIXEDSZ) > eom) { + h_errno = NO_RECOVERY; + return(NULL); + } + cp += n + QFIXEDSZ; + } + ap = net_aliases; + *ap = NULL; + net_entry.n_aliases = net_aliases; + haveanswer = 0; + n_name[0] = '\0'; + while (--ancount >= 0 && cp < eom) { + n = dn_expand(answer->buf, eom, cp, bp, (int)(ep - bp)); + if ((n < 0) || !res_dnok(bp)) + break; + cp += n; + (void)strlcpy(n_name, bp, sizeof(n_name)); + GETSHORT(type, cp); + GETSHORT(class, cp); + cp += INT32SZ; /* TTL */ + GETSHORT(n, cp); + if (class == C_IN && type == T_PTR) { + n = dn_expand(answer->buf, eom, cp, bp, (int)(ep - bp)); + if ((n < 0) || !res_hnok(bp)) { + cp += n; + return NULL; + } + cp += n; + *ap++ = bp; + bp += strlen(bp) + 1; + net_entry.n_addrtype = + (class == C_IN) ? AF_INET : AF_UNSPEC; + haveanswer++; + } + } + if (haveanswer) { + *ap = NULL; + switch (net_i) { + case BYADDR: + net_entry.n_name = *net_entry.n_aliases; + net_entry.n_net = 0L; + break; + case BYNAME: + ap = net_entry.n_aliases; + next_alias: + in = *ap++; + if (in == NULL) { + h_errno = HOST_NOT_FOUND; + return NULL; + } + net_entry.n_name = n_name; + if (parse_reversed_addr(in, &net_entry.n_net) == -1) + goto next_alias; + break; + } + net_entry.n_aliases++; +#if (defined(__sparc__) && defined(_LP64)) || \ + defined(__alpha__) || \ + (defined(__i386__) && defined(_LP64)) || \ + (defined(__sh__) && defined(_LP64)) + net_entry.__n_pad0 = 0; +#endif + return &net_entry; + } + h_errno = TRY_AGAIN; + return NULL; +} + +/*ARGSUSED*/ +static int +_files_getnetbyaddr(void *cbrv, void *cbdata, va_list ap) +{ + struct netent **retval = va_arg(ap, struct netent **); + uint32_t net = va_arg(ap, uint32_t); + int type = va_arg(ap, int); + + struct netent *np; + + setnetent(_net_stayopen); + while ((np = getnetent()) != NULL) + if (np->n_addrtype == type && np->n_net == net) + break; + if (!_net_stayopen) + endnetent(); + + if (np != NULL) { + *retval = np; + return NS_SUCCESS; + } else { + h_errno = HOST_NOT_FOUND; + return NS_NOTFOUND; + } +} + +/*ARGSUSED*/ +static int +_dns_getnetbyaddr(void *cbrv, void *cbdata, va_list ap) +{ + struct netent **retval = va_arg(ap, struct netent **); + uint32_t net = va_arg(ap, uint32_t); + int type = va_arg(ap, int); + + unsigned int netbr[4]; + int nn, anslen; + querybuf *buf; + char qbuf[MAXDNAME]; + uint32_t net2; + struct netent *np; + res_state res; + + if (type != AF_INET) + return NS_UNAVAIL; + + for (nn = 4, net2 = net; net2; net2 >>= 8) + netbr[--nn] = (unsigned int)(net2 & 0xff); + switch (nn) { + default: + return NS_UNAVAIL; + case 3: /* Class A */ + snprintf(qbuf, sizeof(qbuf), "0.0.0.%u.in-addr.arpa", netbr[3]); + break; + case 2: /* Class B */ + snprintf(qbuf, sizeof(qbuf), "0.0.%u.%u.in-addr.arpa", + netbr[3], netbr[2]); + break; + case 1: /* Class C */ + snprintf(qbuf, sizeof(qbuf), "0.%u.%u.%u.in-addr.arpa", + netbr[3], netbr[2], netbr[1]); + break; + case 0: /* Class D - E */ + snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u.in-addr.arpa", + netbr[3], netbr[2], netbr[1], netbr[0]); + break; + } + buf = malloc(sizeof(*buf)); + if (buf == NULL) { + h_errno = NETDB_INTERNAL; + return NS_NOTFOUND; + } + res = __res_get_state(); + if (res == NULL) { + free(buf); + return NS_NOTFOUND; + } + anslen = res_nquery(res, qbuf, C_IN, T_PTR, buf->buf, + (int)sizeof(buf->buf)); + if (anslen < 0) { + free(buf); +#ifdef DEBUG + if (res->options & RES_DEBUG) + printf("res_query failed\n"); +#endif + __res_put_state(res); + return NS_NOTFOUND; + } + __res_put_state(res); + np = getnetanswer(buf, anslen, BYADDR); + free(buf); + if (np) { + /* maybe net should be unsigned? */ + uint32_t u_net = net; + + /* Strip trailing zeros */ + while ((u_net & 0xff) == 0 && u_net != 0) + u_net >>= 8; + np->n_net = u_net; + } + + if (np != NULL) { + *retval = np; + return NS_SUCCESS; + } else { + h_errno = HOST_NOT_FOUND; + return NS_NOTFOUND; + } +} + +struct netent * +getnetbyaddr(uint32_t net, int net_type) +{ + int rv; + struct netent *retval; + + static const ns_dtab dtab[] = { + NS_FILES_CB(_files_getnetbyaddr, NULL) + { NSSRC_DNS, _dns_getnetbyaddr, NULL }, /* force -DHESIOD */ + NS_NIS_CB(_yp_getnetbyaddr, NULL) + NS_NULL_CB + }; + + retval = NULL; + h_errno = NETDB_INTERNAL; + rv = nsdispatch(NULL, dtab, NSDB_NETWORKS, "getnetbyaddr", + __nsdefaultsrc, &retval, net, net_type); + if (rv == NS_SUCCESS) { + h_errno = NETDB_SUCCESS; + return retval; + } + return NULL; +} + +/*ARGSUSED*/ +static int +_files_getnetbyname(void *cbrv, void *cbdata, va_list ap) +{ + struct netent **retval = va_arg(ap, struct netent **); + const char *name = va_arg(ap, const char *); + + struct netent *np; + char **cp; + + setnetent(_net_stayopen); + while ((np = getnetent()) != NULL) { + if (strcasecmp(np->n_name, name) == 0) + break; + for (cp = np->n_aliases; *cp != 0; cp++) + if (strcasecmp(*cp, name) == 0) + goto found; + } +found: + if (!_net_stayopen) + endnetent(); + + if (np != NULL) { + *retval = np; + return NS_SUCCESS; + } else { + h_errno = HOST_NOT_FOUND; + return NS_NOTFOUND; + } +} + +/*ARGSUSED*/ +static int +_dns_getnetbyname(void *cbrv, void *cbdata, va_list ap) +{ + struct netent **retval = va_arg(ap, struct netent **); + const char *name = va_arg(ap, const char *); + + int anslen; + querybuf *buf; + char qbuf[MAXDNAME]; + struct netent *np; + res_state res; + + strlcpy(&qbuf[0], name, sizeof(qbuf)); + buf = malloc(sizeof(*buf)); + if (buf == NULL) { + h_errno = NETDB_INTERNAL; + return NS_NOTFOUND; + } + res = __res_get_state(); + if (res == NULL) { + free(buf); + return NS_NOTFOUND; + } + anslen = res_nsearch(res, qbuf, C_IN, T_PTR, buf->buf, + (int)sizeof(buf->buf)); + if (anslen < 0) { + free(buf); +#ifdef DEBUG + if (res->options & RES_DEBUG) + printf("res_search failed\n"); +#endif + __res_put_state(res); + return NS_NOTFOUND; + } + __res_put_state(res); + np = getnetanswer(buf, anslen, BYNAME); + free(buf); + + if (np != NULL) { + *retval = np; + return NS_SUCCESS; + } else { + h_errno = HOST_NOT_FOUND; + return NS_NOTFOUND; + } +} + +struct netent * +getnetbyname(const char *name) +{ + int rv; + struct netent *retval; + + static const ns_dtab dtab[] = { + NS_FILES_CB(_files_getnetbyname, NULL) + { NSSRC_DNS, _dns_getnetbyname, NULL }, /* force -DHESIOD */ + NS_NIS_CB(_yp_getnetbyname, NULL) + NS_NULL_CB + }; + + _DIAGASSERT(name != NULL); + + retval = NULL; + h_errno = NETDB_INTERNAL; + rv = nsdispatch(NULL, dtab, NSDB_NETWORKS, "getnetbyname", + __nsdefaultsrc, &retval, name); + if (rv == NS_SUCCESS) { + h_errno = NETDB_SUCCESS; + return retval; + } + return NULL; +} + +#ifdef YP +/*ARGSUSED*/ +static int +_yp_getnetbyaddr(void *cbrv, void *cb_data, va_list ap) +{ + struct netent **retval = va_arg(ap, struct netent **); + uint32_t net = va_arg(ap, uint32_t); + int type = va_arg(ap, int); + + struct netent *np; + char qbuf[MAXDNAME]; + unsigned int netbr[4]; + uint32_t net2; + int r; + + if (type != AF_INET) + return NS_UNAVAIL; + + if (!__ypdomain) { + if (_yp_check(&__ypdomain) == 0) + return NS_UNAVAIL; + } + np = NULL; + if (__ypcurrent) + free(__ypcurrent); + __ypcurrent = NULL; + for (r = 4, net2 = net; net2; net2 >>= 8) + netbr[--r] = (unsigned int)(net2 & 0xff); + switch (r) { + default: + return NS_UNAVAIL; + case 3: /* Class A */ + snprintf(qbuf, sizeof(qbuf), "%u", netbr[3]); + break; + case 2: /* Class B */ + snprintf(qbuf, sizeof(qbuf), "%u.%u", netbr[2], netbr[3]); + break; + case 1: /* Class C */ + snprintf(qbuf, sizeof(qbuf), "%u.%u.%u", netbr[1], netbr[2], + netbr[3]); + break; + case 0: /* Class D - E */ + snprintf(qbuf, sizeof(qbuf), "%u.%u.%u.%u", netbr[0], netbr[1], + netbr[2], netbr[3]); + break; + } + r = yp_match(__ypdomain, "networks.byaddr", qbuf, (int)strlen(qbuf), + &__ypcurrent, &__ypcurrentlen); + if (r == 0) + np = _ypnetent(__ypcurrent); + + if (np != NULL) { + *retval = np; + return NS_SUCCESS; + } else { + h_errno = HOST_NOT_FOUND; + return NS_NOTFOUND; + } +} + +/*ARGSUSED*/ +static int +_yp_getnetbyname(void *cbrv, void *cbdata, va_list ap) +{ + struct netent **retval = va_arg(ap, struct netent **); + const char *name = va_arg(ap, const char *); + + struct netent *np; + int r; + + if (!__ypdomain) { + if (_yp_check(&__ypdomain) == 0) + return NS_UNAVAIL; + } + np = NULL; + if (__ypcurrent) + free(__ypcurrent); + __ypcurrent = NULL; + r = yp_match(__ypdomain, "networks.byname", name, (int)strlen(name), + &__ypcurrent, &__ypcurrentlen); + if (r == 0) + np = _ypnetent(__ypcurrent); + + if (np != NULL) { + *retval = np; + return NS_SUCCESS; + } else { + h_errno = HOST_NOT_FOUND; + return NS_NOTFOUND; + } +} + +static struct netent * +_ypnetent(char *line) +{ + char *cp, *p, **q; + + _DIAGASSERT(line != NULL); + + net_entry.n_name = line; + cp = strpbrk(line, " \t"); + if (cp == NULL) + return NULL; + *cp++ = '\0'; + while (*cp == ' ' || *cp == '\t') + cp++; + p = strpbrk(cp, " \t"); + if (p != NULL) + *p++ = '\0'; + net_entry.n_net = inet_network(cp); +#if (defined(__sparc__) && defined(_LP64)) || \ + defined(__alpha__) || \ + (defined(__i386__) && defined(_LP64)) || \ + (defined(__sh__) && defined(_LP64)) + net_entry.__n_pad0 = 0; +#endif + net_entry.n_addrtype = AF_INET; + q = net_entry.n_aliases = net_aliases; + if (p != NULL) { + cp = p; + while (cp && *cp) { + if (*cp == ' ' || *cp == '\t') { + cp++; + continue; + } + if (q < &net_aliases[MAXALIASES - 1]) + *q++ = cp; + cp = strpbrk(cp, " \t"); + if (cp != NULL) + *cp++ = '\0'; + } + } + *q = NULL; + + return &net_entry; +} +#endif diff -pruN /home/reed/src/isc/libbind/libbind/irs/getpeereid.c /usr/src/lib/libc/net/getpeereid.c --- /home/reed/src/isc/libbind/libbind/irs/getpeereid.c 1969-12-31 18:00:00.000000000 -0600 +++ /usr/src/lib/libc/net/getpeereid.c 2008-04-29 01:53:01.000000000 -0500 @@ -0,0 +1,57 @@ +/* $NetBSD: getpeereid.c,v 1.2 2008/04/29 06:53:01 martin Exp $ */ + +/*- + * Copyright (c) 2007 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Arne H. Juul. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: getpeereid.c,v 1.2 2008/04/29 06:53:01 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include +#include + + +int +getpeereid(int s, uid_t *euid, gid_t *egid) +{ + struct unpcbid cred; + socklen_t len = sizeof(cred); + if (getsockopt(s, 0, LOCAL_PEEREID, &cred, &len) < 0) { + return -1; + } else { + if (euid != NULL) + *euid = cred.unp_euid; + if (egid != NULL) + *egid = cred.unp_egid; + return 0; + } +} diff -pruN /home/reed/src/isc/libbind/libbind/irs/getprotobyname.c /usr/src/lib/libc/net/getprotobyname.c --- /home/reed/src/isc/libbind/libbind/irs/getprotobyname.c 1969-12-31 18:00:00.000000000 -0600 +++ /usr/src/lib/libc/net/getprotobyname.c 2012-10-24 08:34:36.000000000 -0500 @@ -0,0 +1,61 @@ +/* $NetBSD: getprotobyname.c,v 1.4 2008/04/28 20:23:00 martin Exp $ */ + +/*- + * Copyright (c) 2004 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: getprotobyname.c,v 1.4 2008/04/28 20:23:00 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include "reentrant.h" + +#include + +#include "protoent.h" + +#ifdef __weak_alias +__weak_alias(getprotobyname,_getprotobyname) +#endif + +#ifdef _REENTRANT +extern mutex_t _protoent_mutex; +#endif +extern struct protoent_data _protoent_data; + +struct protoent * +getprotobyname(const char *name) +{ + struct protoent *p; + + mutex_lock(&_protoent_mutex); + p = getprotobyname_r(name, &_protoent_data.proto, &_protoent_data); + mutex_unlock(&_protoent_mutex); + return (p); +} diff -pruN /home/reed/src/isc/libbind/libbind/irs/getprotobyname_r.c /usr/src/lib/libc/net/getprotobyname_r.c --- /home/reed/src/isc/libbind/libbind/irs/getprotobyname_r.c 1969-12-31 18:00:00.000000000 -0600 +++ /usr/src/lib/libc/net/getprotobyname_r.c 2005-04-18 14:39:45.000000000 -0500 @@ -0,0 +1,77 @@ +/* $NetBSD: getprotobyname_r.c,v 1.3 2005/04/18 19:39:45 kleink Exp $ */ + +/* + * Copyright (c) 1983, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)getprotoname.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: getprotobyname_r.c,v 1.3 2005/04/18 19:39:45 kleink Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include +#include +#include + +#include "protoent.h" + +#ifdef __weak_alias +__weak_alias(getprotobyname_r,_getprotobyname_r) +#endif + +struct protoent * +getprotobyname_r(const char *name, struct protoent *pr, + struct protoent_data *pd) +{ + struct protoent *p; + char **cp; + + _DIAGASSERT(name != NULL); + + setprotoent_r(pd->stayopen, pd); + while ((p = getprotoent_r(pr, pd)) != NULL) { + if (strcmp(p->p_name, name) == 0) + break; + for (cp = p->p_aliases; *cp != NULL; cp++) + if (strcmp(*cp, name) == 0) + goto found; + } +found: + if (!pd->stayopen) + if (pd->fp != NULL) { + (void)fclose(pd->fp); + pd->fp = NULL; + } + return p; +} diff -pruN /home/reed/src/isc/libbind/libbind/irs/getprotobynumber.c /usr/src/lib/libc/net/getprotobynumber.c --- /home/reed/src/isc/libbind/libbind/irs/getprotobynumber.c 1969-12-31 18:00:00.000000000 -0600 +++ /usr/src/lib/libc/net/getprotobynumber.c 2012-10-24 08:34:36.000000000 -0500 @@ -0,0 +1,62 @@ +/* $NetBSD: getprotobynumber.c,v 1.4 2008/04/28 20:23:00 martin Exp $ */ + +/*- + * Copyright (c) 2004 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: getprotobynumber.c,v 1.4 2008/04/28 20:23:00 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include "reentrant.h" + +#include + +#include "protoent.h" + +#ifdef __weak_alias +__weak_alias(getprotobynumber,_getprotobynumber) +#endif + +#ifdef _REENTRANT +extern mutex_t _protoent_mutex; +#endif +extern struct protoent_data _protoent_data; + +struct protoent * +getprotobynumber(int proto) +{ + struct protoent *p; + + mutex_lock(&_protoent_mutex); + p = getprotobynumber_r(proto, &_protoent_data.proto, &_protoent_data); + mutex_unlock(&_protoent_mutex); + return (p); +} diff -pruN /home/reed/src/isc/libbind/libbind/irs/getprotobynumber_r.c /usr/src/lib/libc/net/getprotobynumber_r.c --- /home/reed/src/isc/libbind/libbind/irs/getprotobynumber_r.c 1969-12-31 18:00:00.000000000 -0600 +++ /usr/src/lib/libc/net/getprotobynumber_r.c 2005-04-18 14:39:45.000000000 -0500 @@ -0,0 +1,66 @@ +/* $NetBSD: getprotobynumber_r.c,v 1.3 2005/04/18 19:39:45 kleink Exp $ */ + +/* + * Copyright (c) 1983, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)getproto.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: getprotobynumber_r.c,v 1.3 2005/04/18 19:39:45 kleink Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include + +#include "protoent.h" + +#ifdef __weak_alias +__weak_alias(getprotobynumber_r,_getprotobynumber_r) +#endif + +struct protoent * +getprotobynumber_r(int proto, struct protoent *pr, struct protoent_data *pd) +{ + struct protoent *p; + + setprotoent_r(pd->stayopen, pd); + while ((p = getprotoent_r(pr, pd)) != NULL) + if (p->p_proto == proto) + break; + if (!pd->stayopen) + if (pd->fp != NULL) { + (void)fclose(pd->fp); + pd->fp = NULL; + } + return p; +} diff -pruN /home/reed/src/isc/libbind/libbind/irs/getprotoent.c /usr/src/lib/libc/net/getprotoent.c --- /home/reed/src/isc/libbind/libbind/irs/getprotoent.c 2005-04-26 23:56:26.000000000 -0500 +++ /usr/src/lib/libc/net/getprotoent.c 2012-10-24 08:34:36.000000000 -0500 @@ -1,176 +1,80 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1996,1999 by Internet Software Consortium. +/* $NetBSD: getprotoent.c,v 1.12 2008/04/28 20:23:00 martin Exp $ */ + +/*- + * Copyright (c) 2004 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. */ +#include -#if !defined(LINT) && !defined(CODECENTER) -static const char rcsid[] = "$Id: getprotoent.c,v 1.4 2005/04/27 04:56:26 sra Exp $"; -#endif - -/* Imports */ - -#include "port_before.h" - -#if !defined(__BIND_NOSTATIC) - -#include - -#include -#include - -#include -#include -#include -#include - -#include - -#include "port_after.h" - -#include "irs_data.h" +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: getprotoent.c,v 1.12 2008/04/28 20:23:00 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ -/* Forward */ +#include "namespace.h" +#include "reentrant.h" -static struct net_data *init(void); +#include -/* Public */ +#include "protoent.h" -struct protoent * -getprotoent() { - struct net_data *net_data = init(); - - return (getprotoent_p(net_data)); -} - -struct protoent * -getprotobyname(const char *name) { - struct net_data *net_data = init(); - - return (getprotobyname_p(name, net_data)); -} - -struct protoent * -getprotobynumber(int proto) { - struct net_data *net_data = init(); +#ifdef __weak_alias +__weak_alias(endprotoent,_endprotoent) +__weak_alias(getprotoent,_getprotoent) +__weak_alias(setprotoent,_setprotoent) +#endif - return (getprotobynumber_p(proto, net_data)); -} +#ifdef _REENTRANT +mutex_t _protoent_mutex = MUTEX_INITIALIZER; +#endif +struct protoent_data _protoent_data; void -setprotoent(int stayopen) { - struct net_data *net_data = init(); - - setprotoent_p(stayopen, net_data); +setprotoent(int f) +{ + mutex_lock(&_protoent_mutex); + setprotoent_r(f, &_protoent_data); + mutex_unlock(&_protoent_mutex); } void -endprotoent() { - struct net_data *net_data = init(); - - endprotoent_p(net_data); -} - -/* Shared private. */ - -struct protoent * -getprotoent_p(struct net_data *net_data) { - struct irs_pr *pr; - - if (!net_data || !(pr = net_data->pr)) - return (NULL); - net_data->pr_last = (*pr->next)(pr); - return (net_data->pr_last); -} - -struct protoent * -getprotobyname_p(const char *name, struct net_data *net_data) { - struct irs_pr *pr; - char **pap; - - if (!net_data || !(pr = net_data->pr)) - return (NULL); - if (net_data->pr_stayopen && net_data->pr_last) { - if (!strcmp(net_data->pr_last->p_name, name)) - return (net_data->pr_last); - for (pap = net_data->pr_last->p_aliases; pap && *pap; pap++) - if (!strcmp(name, *pap)) - return (net_data->pr_last); - } - net_data->pr_last = (*pr->byname)(pr, name); - if (!net_data->pr_stayopen) - endprotoent(); - return (net_data->pr_last); +endprotoent(void) +{ + mutex_lock(&_protoent_mutex); + endprotoent_r(&_protoent_data); + mutex_unlock(&_protoent_mutex); } struct protoent * -getprotobynumber_p(int proto, struct net_data *net_data) { - struct irs_pr *pr; - - if (!net_data || !(pr = net_data->pr)) - return (NULL); - if (net_data->pr_stayopen && net_data->pr_last) - if (net_data->pr_last->p_proto == proto) - return (net_data->pr_last); - net_data->pr_last = (*pr->bynumber)(pr, proto); - if (!net_data->pr_stayopen) - endprotoent(); - return (net_data->pr_last); -} - -void -setprotoent_p(int stayopen, struct net_data *net_data) { - struct irs_pr *pr; - - if (!net_data || !(pr = net_data->pr)) - return; - (*pr->rewind)(pr); - net_data->pr_stayopen = (stayopen != 0); - if (stayopen == 0) - net_data_minimize(net_data); -} - -void -endprotoent_p(struct net_data *net_data) { - struct irs_pr *pr; - - if ((net_data != NULL) && ((pr = net_data->pr) != NULL)) - (*pr->minimize)(pr); -} - -/* Private */ - -static struct net_data * -init() { - struct net_data *net_data; - - if (!(net_data = net_data_init(NULL))) - goto error; - if (!net_data->pr) { - net_data->pr = (*net_data->irs->pr_map)(net_data->irs); - - if (!net_data->pr || !net_data->res) { - error: - errno = EIO; - return (NULL); - } - (*net_data->pr->res_set)(net_data->pr, net_data->res, NULL); - } - - return (net_data); +getprotoent(void) +{ + struct protoent *p; + + mutex_lock(&_protoent_mutex); + p = getprotoent_r(&_protoent_data.proto, &_protoent_data); + mutex_unlock(&_protoent_mutex); + return (p); } - -#endif /*__BIND_NOSTATIC*/ - -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/getprotoent_r.c /usr/src/lib/libc/net/getprotoent_r.c --- /home/reed/src/isc/libbind/libbind/irs/getprotoent_r.c 2006-07-31 20:14:16.000000000 -0500 +++ /usr/src/lib/libc/net/getprotoent_r.c 2012-10-24 08:34:36.000000000 -0500 @@ -1,223 +1,153 @@ +/* $NetBSD: getprotoent_r.c,v 1.6 2011/10/15 23:00:02 christos Exp $ */ + /* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1998-1999 by Internet Software Consortium. + * Copyright (c) 1983, 1993 + * The Regents of the University of California. All rights reserved. * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. */ +#include #if defined(LIBC_SCCS) && !defined(lint) -static const char rcsid[] = "$Id: getprotoent_r.c,v 1.6 2006/08/01 01:14:16 marka Exp $"; +#if 0 +static char sccsid[] = "@(#)getprotoent.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: getprotoent_r.c,v 1.6 2011/10/15 23:00:02 christos Exp $"); +#endif #endif /* LIBC_SCCS and not lint */ -#include -#if !defined(_REENTRANT) || !defined(DO_PTHREADS) - static int getprotoent_r_not_required = 0; -#else +#include "namespace.h" +#include +#include #include +#include #include -#include -#include -#include -#include -#include - -#ifdef PROTO_R_RETURN - -static PROTO_R_RETURN -copy_protoent(struct protoent *, struct protoent *, PROTO_R_COPY_ARGS); -PROTO_R_RETURN -getprotobyname_r(const char *name, struct protoent *pptr, PROTO_R_ARGS) { - struct protoent *pe = getprotobyname(name); -#ifdef PROTO_R_SETANSWER - int n = 0; - - if (pe == NULL || (n = copy_protoent(pe, pptr, PROTO_R_COPY)) != 0) - *answerp = NULL; - else - *answerp = pptr; - - return (n); -#else - if (pe == NULL) - return (PROTO_R_BAD); +#include "protoent.h" - return (copy_protoent(pe, pptr, PROTO_R_COPY)); +#ifdef __weak_alias +__weak_alias(endprotoent_r,_endprotoent_r) +__weak_alias(getprotoent_r,_getprotoent_r) +__weak_alias(setprotoent_r,_setprotoent_r) #endif -} - -PROTO_R_RETURN -getprotobynumber_r(int proto, struct protoent *pptr, PROTO_R_ARGS) { - struct protoent *pe = getprotobynumber(proto); -#ifdef PROTO_R_SETANSWER - int n = 0; - - if (pe == NULL || (n = copy_protoent(pe, pptr, PROTO_R_COPY)) != 0) - *answerp = NULL; - else - *answerp = pptr; - - return (n); -#else - if (pe == NULL) - return (PROTO_R_BAD); - - return (copy_protoent(pe, pptr, PROTO_R_COPY)); -#endif -} - -/*% - * These assume a single context is in operation per thread. - * If this is not the case we will need to call irs directly - * rather than through the base functions. - */ - -PROTO_R_RETURN -getprotoent_r(struct protoent *pptr, PROTO_R_ARGS) { - struct protoent *pe = getprotoent(); -#ifdef PROTO_R_SETANSWER - int n = 0; - if (pe == NULL || (n = copy_protoent(pe, pptr, PROTO_R_COPY)) != 0) - *answerp = NULL; - else - *answerp = pptr; - - return (n); -#else - if (pe == NULL) - return (PROTO_R_BAD); - - return (copy_protoent(pe, pptr, PROTO_R_COPY)); -#endif -} - -PROTO_R_SET_RETURN -#ifdef PROTO_R_ENT_ARGS -setprotoent_r(int stay_open, PROTO_R_ENT_ARGS) -#else -setprotoent_r(int stay_open) -#endif +void +setprotoent_r(int f, struct protoent_data *pd) { -#ifdef PROTO_R_ENT_UNUSED - PROTO_R_ENT_UNUSED; -#endif - setprotoent(stay_open); -#ifdef PROTO_R_SET_RESULT - return (PROTO_R_SET_RESULT); -#endif + if (pd->fp == NULL) + pd->fp = fopen(_PATH_PROTOCOLS, "re"); + else + rewind(pd->fp); + pd->stayopen |= f; } -PROTO_R_END_RETURN -#ifdef PROTO_R_ENT_ARGS -endprotoent_r(PROTO_R_ENT_ARGS) -#else -endprotoent_r() -#endif +void +endprotoent_r(struct protoent_data *pd) { -#ifdef PROTO_R_ENT_UNUSED - PROTO_R_ENT_UNUSED; -#endif - endprotoent(); - PROTO_R_END_RESULT(PROTO_R_OK); -} - -/* Private */ - -#ifndef PROTOENT_DATA -static PROTO_R_RETURN -copy_protoent(struct protoent *pe, struct protoent *pptr, PROTO_R_COPY_ARGS) { - char *cp; - int i, n; - int numptr, len; - - /* Find out the amount of space required to store the answer. */ - numptr = 1; /*%< NULL ptr */ - len = (char *)ALIGN(buf) - buf; - for (i = 0; pe->p_aliases[i]; i++, numptr++) { - len += strlen(pe->p_aliases[i]) + 1; + if (pd->fp) { + (void)fclose(pd->fp); + pd->fp = NULL; } - len += strlen(pe->p_name) + 1; - len += numptr * sizeof(char*); - - if (len > (int)buflen) { - errno = ERANGE; - return (PROTO_R_BAD); + if (pd->aliases) { + free(pd->aliases); + pd->aliases = NULL; + pd->maxaliases = 0; } - - /* copy protocol value*/ - pptr->p_proto = pe->p_proto; - - cp = (char *)ALIGN(buf) + numptr * sizeof(char *); - - /* copy official name */ - n = strlen(pe->p_name) + 1; - strcpy(cp, pe->p_name); - pptr->p_name = cp; - cp += n; - - /* copy aliases */ - pptr->p_aliases = (char **)ALIGN(buf); - for (i = 0 ; pe->p_aliases[i]; i++) { - n = strlen(pe->p_aliases[i]) + 1; - strcpy(cp, pe->p_aliases[i]); - pptr->p_aliases[i] = cp; - cp += n; + if (pd->line) { + free(pd->line); + pd->line = NULL; } - pptr->p_aliases[i] = NULL; - - return (PROTO_R_OK); + pd->stayopen = 0; } -#else /* !PROTOENT_DATA */ -static int -copy_protoent(struct protoent *pe, struct protoent *pptr, PROTO_R_COPY_ARGS) { - char *cp, *eob; - int i, n; - - /* copy protocol value */ - pptr->p_proto = pe->p_proto; - - /* copy official name */ - cp = pdptr->line; - eob = pdptr->line + sizeof(pdptr->line); - if ((n = strlen(pe->p_name) + 1) < (eob - cp)) { - strcpy(cp, pe->p_name); - pptr->p_name = cp; - cp += n; - } else { - return (-1); - } - /* copy aliases */ - i = 0; - pptr->p_aliases = pdptr->proto_aliases; - while (pe->p_aliases[i] && i < (_MAXALIASES-1)) { - if ((n = strlen(pe->p_aliases[i]) + 1) < (eob - cp)) { - strcpy(cp, pe->p_aliases[i]); - pptr->p_aliases[i] = cp; - cp += n; - } else { - break; +struct protoent * +getprotoent_r(struct protoent *pr, struct protoent_data *pd) +{ + char *p, *cp, **q; + size_t i = 0; + int oerrno; + + if (pd->fp == NULL && (pd->fp = fopen(_PATH_PROTOCOLS, "re")) == NULL) + return NULL; + + for (;;) { + if (pd->line) + free(pd->line); + pd->line = fparseln(pd->fp, NULL, NULL, NULL, + FPARSELN_UNESCALL); + if (pd->line == NULL) + return NULL; + pr->p_name = p = pd->line; + cp = strpbrk(p, " \t"); + if (cp == NULL) + continue; + *cp++ = '\0'; + while (*cp == ' ' || *cp == '\t') + cp++; + p = strpbrk(cp, " \t"); + if (p != NULL) + *p++ = '\0'; + pr->p_proto = atoi(cp); + if (pd->aliases == NULL) { + pd->maxaliases = 10; + pd->aliases = malloc(pd->maxaliases * sizeof(char *)); + if (pd->aliases == NULL) { + oerrno = errno; + endprotoent_r(pd); + errno = oerrno; + return NULL; + } + } + q = pr->p_aliases = pd->aliases; + if (p != NULL) { + cp = p; + while (cp && *cp) { + if (*cp == ' ' || *cp == '\t') { + cp++; + continue; + } + if (i == pd->maxaliases - 2) { + pd->maxaliases *= 2; + q = realloc(q, + pd->maxaliases * sizeof(char *)); + if (q == NULL) { + oerrno = errno; + endprotoent_r(pd); + errno = oerrno; + return NULL; + } + pr->p_aliases = pd->aliases = q; + } + q[i++] = cp; + + cp = strpbrk(cp, " \t"); + if (cp != NULL) + *cp++ = '\0'; + } } - i++; + q[i] = NULL; + return pr; } - pptr->p_aliases[i] = NULL; - - return (PROTO_R_OK); } -#endif /* PROTOENT_DATA */ -#else /* PROTO_R_RETURN */ - static int getprotoent_r_unknown_system = 0; -#endif /* PROTO_R_RETURN */ -#endif /* !defined(_REENTRANT) || !defined(DO_PTHREADS) */ -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/getpwent.c /usr/src/lib/libc/net/getpwent.c --- /home/reed/src/isc/libbind/libbind/irs/getpwent.c 2005-04-26 23:56:26.000000000 -0500 +++ /usr/src/lib/libc/net/getpwent.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,201 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1996,1999 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#if !defined(LINT) && !defined(CODECENTER) -static const char rcsid[] = "$Id: getpwent.c,v 1.3 2005/04/27 04:56:26 sra Exp $"; -#endif - -/* Imports */ - -#include "port_before.h" - -#if !defined(WANT_IRS_PW) || defined(__BIND_NOSTATIC) -static int __bind_irs_pw_unneeded; -#else - -#include - -#include -#include - -#include -#include -#include -#include -#include - -#include - -#include "port_after.h" - -#include "irs_data.h" - -/* Forward */ - -static struct net_data * init(void); - -/* Public */ - -struct passwd * -getpwent(void) { - struct net_data *net_data = init(); - - return (getpwent_p(net_data)); -} - -struct passwd * -getpwnam(const char *name) { - struct net_data *net_data = init(); - - return (getpwnam_p(name, net_data)); -} - -struct passwd * -getpwuid(uid_t uid) { - struct net_data *net_data = init(); - - return (getpwuid_p(uid, net_data)); -} - -int -setpassent(int stayopen) { - struct net_data *net_data = init(); - - return (setpassent_p(stayopen, net_data)); -} - -#ifdef SETPWENT_VOID -void -setpwent() { - struct net_data *net_data = init(); - - setpwent_p(net_data); -} -#else -int -setpwent() { - struct net_data *net_data = init(); - - return (setpwent_p(net_data)); -} -#endif - -void -endpwent() { - struct net_data *net_data = init(); - - endpwent_p(net_data); -} - -/* Shared private. */ - -struct passwd * -getpwent_p(struct net_data *net_data) { - struct irs_pw *pw; - - if (!net_data || !(pw = net_data->pw)) - return (NULL); - net_data->pw_last = (*pw->next)(pw); - return (net_data->pw_last); -} - -struct passwd * -getpwnam_p(const char *name, struct net_data *net_data) { - struct irs_pw *pw; - - if (!net_data || !(pw = net_data->pw)) - return (NULL); - if (net_data->pw_stayopen && net_data->pw_last && - !strcmp(net_data->pw_last->pw_name, name)) - return (net_data->pw_last); - net_data->pw_last = (*pw->byname)(pw, name); - if (!net_data->pw_stayopen) - endpwent(); - return (net_data->pw_last); -} - -struct passwd * -getpwuid_p(uid_t uid, struct net_data *net_data) { - struct irs_pw *pw; - - if (!net_data || !(pw = net_data->pw)) - return (NULL); - if (net_data->pw_stayopen && net_data->pw_last && - net_data->pw_last->pw_uid == uid) - return (net_data->pw_last); - net_data->pw_last = (*pw->byuid)(pw, uid); - if (!net_data->pw_stayopen) - endpwent(); - return (net_data->pw_last); -} - -int -setpassent_p(int stayopen, struct net_data *net_data) { - struct irs_pw *pw; - - if (!net_data || !(pw = net_data->pw)) - return (0); - (*pw->rewind)(pw); - net_data->pw_stayopen = (stayopen != 0); - if (stayopen == 0) - net_data_minimize(net_data); - return (1); -} - -#ifdef SETPWENT_VOID -void -setpwent_p(struct net_data *net_data) { - (void) setpassent_p(0, net_data); -} -#else -int -setpwent_p(struct net_data *net_data) { - return (setpassent_p(0, net_data)); -} -#endif - -void -endpwent_p(struct net_data *net_data) { - struct irs_pw *pw; - - if ((net_data != NULL) && ((pw = net_data->pw) != NULL)) - (*pw->minimize)(pw); -} - -/* Private */ - -static struct net_data * -init() { - struct net_data *net_data; - if (!(net_data = net_data_init(NULL))) - goto error; - if (!net_data->pw) { - net_data->pw = (*net_data->irs->pw_map)(net_data->irs); - - if (!net_data->pw || !net_data->res) { - error: - errno = EIO; - return (NULL); - } - (*net_data->pw->res_set)(net_data->pw, net_data->res, NULL); - } - - return (net_data); -} - -#endif /* WANT_IRS_PW */ -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/getpwent_r.c /usr/src/lib/libc/net/getpwent_r.c --- /home/reed/src/isc/libbind/libbind/irs/getpwent_r.c 2005-04-26 23:56:26.000000000 -0500 +++ /usr/src/lib/libc/net/getpwent_r.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,276 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1998-1999 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#if defined(LIBC_SCCS) && !defined(lint) -static const char rcsid[] = "$Id: getpwent_r.c,v 1.8 2005/04/27 04:56:26 sra Exp $"; -#endif /* LIBC_SCCS and not lint */ - -#include -#if !defined(_REENTRANT) || !defined(DO_PTHREADS) || !defined(WANT_IRS_PW) - static int getpwent_r_not_required = 0; -#else -#include -#include -#include -#include -#if (defined(POSIX_GETPWNAM_R) || defined(POSIX_GETPWUID_R)) -#if defined(_POSIX_PTHREAD_SEMANTICS) - /* turn off solaris remapping in */ -#undef _POSIX_PTHREAD_SEMANTICS -#include -#define _POSIX_PTHREAD_SEMANTICS 1 -#else -#define _UNIX95 1 -#include -#endif -#else -#include -#endif -#include - -#ifdef PASS_R_RETURN - -static int -copy_passwd(struct passwd *, struct passwd *, char *buf, int buflen); - -/* POSIX 1003.1c */ -#ifdef POSIX_GETPWNAM_R -int -__posix_getpwnam_r(const char *login, struct passwd *pwptr, - char *buf, size_t buflen, struct passwd **result) { -#else -int -getpwnam_r(const char *login, struct passwd *pwptr, - char *buf, size_t buflen, struct passwd **result) { -#endif - struct passwd *pw = getpwnam(login); - int res; - - if (pw == NULL) { - *result = NULL; - return (0); - } - - res = copy_passwd(pw, pwptr, buf, buflen); - *result = res ? NULL : pwptr; - return (res); -} - -#ifdef POSIX_GETPWNAM_R -struct passwd * -getpwnam_r(const char *login, struct passwd *pwptr, char *buf, int buflen) { - struct passwd *pw = getpwnam(login); - int res; - - if (pw == NULL) - return (NULL); - - res = copy_passwd(pw, pwptr, buf, buflen); - return (res ? NULL : pwptr); -} -#endif - -/* POSIX 1003.1c */ -#ifdef POSIX_GETPWUID_R -int -__posix_getpwuid_r(uid_t uid, struct passwd *pwptr, - char *buf, int buflen, struct passwd **result) { -#else -int -getpwuid_r(uid_t uid, struct passwd *pwptr, - char *buf, size_t buflen, struct passwd **result) { -#endif - struct passwd *pw = getpwuid(uid); - int res; - - if (pw == NULL) { - *result = NULL; - return (0); - } - - res = copy_passwd(pw, pwptr, buf, buflen); - *result = res ? NULL : pwptr; - return (res); -} - -#ifdef POSIX_GETPWUID_R -struct passwd * -getpwuid_r(uid_t uid, struct passwd *pwptr, char *buf, int buflen) { - struct passwd *pw = getpwuid(uid); - int res; - - if (pw == NULL) - return (NULL); - - res = copy_passwd(pw, pwptr, buf, buflen); - return (res ? NULL : pwptr); -} -#endif - -/*% - * These assume a single context is in operation per thread. - * If this is not the case we will need to call irs directly - * rather than through the base functions. - */ - -PASS_R_RETURN -getpwent_r(struct passwd *pwptr, PASS_R_ARGS) { - struct passwd *pw = getpwent(); - int res = 0; - - if (pw == NULL) - return (PASS_R_BAD); - - res = copy_passwd(pw, pwptr, buf, buflen); - return (res ? PASS_R_BAD : PASS_R_OK); -} - -PASS_R_SET_RETURN -#ifdef PASS_R_ENT_ARGS -setpassent_r(int stayopen, PASS_R_ENT_ARGS) -#else -setpassent_r(int stayopen) -#endif -{ - - setpassent(stayopen); -#ifdef PASS_R_SET_RESULT - return (PASS_R_SET_RESULT); -#endif -} - -PASS_R_SET_RETURN -#ifdef PASS_R_ENT_ARGS -setpwent_r(PASS_R_ENT_ARGS) -#else -setpwent_r(void) -#endif -{ - - setpwent(); -#ifdef PASS_R_SET_RESULT - return (PASS_R_SET_RESULT); -#endif -} - -PASS_R_END_RETURN -#ifdef PASS_R_ENT_ARGS -endpwent_r(PASS_R_ENT_ARGS) -#else -endpwent_r(void) -#endif -{ - - endpwent(); - PASS_R_END_RESULT(PASS_R_OK); -} - - -#ifdef HAS_FGETPWENT -PASS_R_RETURN -fgetpwent_r(FILE *f, struct passwd *pwptr, PASS_R_COPY_ARGS) { - struct passwd *pw = fgetpwent(f); - int res = 0; - - if (pw == NULL) - return (PASS_R_BAD); - - res = copy_passwd(pw, pwptr, PASS_R_COPY); - return (res ? PASS_R_BAD : PASS_R_OK ); -} -#endif - -/* Private */ - -static int -copy_passwd(struct passwd *pw, struct passwd *pwptr, char *buf, int buflen) { - char *cp; - int n; - int len; - - /* Find out the amount of space required to store the answer. */ - len = strlen(pw->pw_name) + 1; - len += strlen(pw->pw_passwd) + 1; -#ifdef HAVE_PW_CLASS - len += strlen(pw->pw_class) + 1; -#endif - len += strlen(pw->pw_gecos) + 1; - len += strlen(pw->pw_dir) + 1; - len += strlen(pw->pw_shell) + 1; - - if (len > buflen) { - errno = ERANGE; - return (ERANGE); - } - - /* copy fixed atomic values*/ - pwptr->pw_uid = pw->pw_uid; - pwptr->pw_gid = pw->pw_gid; -#ifdef HAVE_PW_CHANGE - pwptr->pw_change = pw->pw_change; -#endif -#ifdef HAVE_PW_EXPIRE - pwptr->pw_expire = pw->pw_expire; -#endif - - cp = buf; - - /* copy official name */ - n = strlen(pw->pw_name) + 1; - strcpy(cp, pw->pw_name); - pwptr->pw_name = cp; - cp += n; - - /* copy password */ - n = strlen(pw->pw_passwd) + 1; - strcpy(cp, pw->pw_passwd); - pwptr->pw_passwd = cp; - cp += n; - -#ifdef HAVE_PW_CLASS - /* copy class */ - n = strlen(pw->pw_class) + 1; - strcpy(cp, pw->pw_class); - pwptr->pw_class = cp; - cp += n; -#endif - - /* copy gecos */ - n = strlen(pw->pw_gecos) + 1; - strcpy(cp, pw->pw_gecos); - pwptr->pw_gecos = cp; - cp += n; - - /* copy directory */ - n = strlen(pw->pw_dir) + 1; - strcpy(cp, pw->pw_dir); - pwptr->pw_dir = cp; - cp += n; - - /* copy login shell */ - n = strlen(pw->pw_shell) + 1; - strcpy(cp, pw->pw_shell); - pwptr->pw_shell = cp; - cp += n; - - return (0); -} -#else /* PASS_R_RETURN */ - static int getpwent_r_unknown_system = 0; -#endif /* PASS_R_RETURN */ -#endif /* !def(_REENTRANT) || !def(DO_PTHREADS) || !def(WANT_IRS_PW) */ -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/getservbyname.c /usr/src/lib/libc/net/getservbyname.c --- /home/reed/src/isc/libbind/libbind/irs/getservbyname.c 1969-12-31 18:00:00.000000000 -0600 +++ /usr/src/lib/libc/net/getservbyname.c 2012-10-24 08:34:36.000000000 -0500 @@ -0,0 +1,61 @@ +/* $NetBSD: getservbyname.c,v 1.14 2008/04/28 20:23:00 martin Exp $ */ + +/*- + * Copyright (c) 2004 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: getservbyname.c,v 1.14 2008/04/28 20:23:00 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include "reentrant.h" + +#include + +#include "servent.h" + +#ifdef __weak_alias +__weak_alias(getservbyname,_getservbyname) +#endif + +#ifdef _REENTRANT +extern mutex_t _servent_mutex; +#endif +extern struct servent_data _servent_data; + +struct servent * +getservbyname(const char *name, const char *proto) +{ + struct servent *s; + + mutex_lock(&_servent_mutex); + s = getservbyname_r(name, proto, &_servent_data.serv, &_servent_data); + mutex_unlock(&_servent_mutex); + return (s); +} diff -pruN /home/reed/src/isc/libbind/libbind/irs/getservbyname_r.c /usr/src/lib/libc/net/getservbyname_r.c --- /home/reed/src/isc/libbind/libbind/irs/getservbyname_r.c 1969-12-31 18:00:00.000000000 -0600 +++ /usr/src/lib/libc/net/getservbyname_r.c 2013-06-05 09:26:12.000000000 -0500 @@ -0,0 +1,149 @@ +/* $NetBSD: getservbyname_r.c,v 1.9 2012/03/13 21:13:41 christos Exp $ */ + +/* + * Copyright (c) 1983, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)getservbyname.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: getservbyname_r.c,v 1.9 2012/03/13 21:13:41 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include +#include + +#include "servent.h" + +#ifdef __weak_alias +__weak_alias(getservbyname_r,_getservbyname_r) +#endif + +static struct servent * +_servent_getbyname(struct servent_data *sd, struct servent *sp, + const char *name, const char *proto) +{ + + if ((sd->flags & (_SV_CDB | _SV_PLAINFILE)) == 0) + return NULL; + + if (sd->flags & _SV_CDB) { + uint8_t buf[255 * 2 + 2]; + size_t namelen, protolen; + const uint8_t *data, *data_end; + const void *data_ptr; + size_t datalen; + + namelen = strlen(name); + if (namelen == 0 || namelen > 255) + return NULL; + if (proto != NULL) { + protolen = strlen(proto); + if (protolen == 0 || protolen > 255) + return NULL; + } else + protolen = 0; + if (namelen + protolen > 255) + return NULL; + + buf[0] = (uint8_t)namelen; + buf[1] = (uint8_t)protolen; + memcpy(buf + 2, name, namelen); + memcpy(buf + 2 + namelen, proto, protolen); + + if (cdbr_find(sd->cdb, buf, 2 + namelen + protolen, + &data_ptr, &datalen)) + return NULL; + + if (datalen < namelen + protolen + 6) + return NULL; + + data = data_ptr; + data_end = data + datalen; + if (protolen) { + if (data[2] != protolen) + return NULL; + if (memcmp(data + 3, proto, protolen + 1)) + return NULL; + } + data += 3 + data[2] + 1; + if (data > data_end) + return NULL; + while (data != data_end) { + if (*data == '\0') + return NULL; + if (data + data[0] + 2 > data_end) + return NULL; + if (data[0] == namelen && + memcmp(data + 1, name, namelen + 1) == 0) + return _servent_parsedb(sd, sp, data_ptr, + datalen); + data += data[0] + 2; + } + return NULL; + } else { + while (_servent_getline(sd) != -1) { + char **cp; + if (_servent_parseline(sd, sp) == NULL) + continue; + + if (strcmp(name, sp->s_name) == 0) + goto gotname; + + for (cp = sp->s_aliases; *cp; cp++) + if (strcmp(name, *cp) == 0) + goto gotname; + continue; +gotname: + if (proto == NULL || strcmp(sp->s_proto, proto) == 0) + return sp; + } + return NULL; + } +} + +struct servent * +getservbyname_r(const char *name, const char *proto, struct servent *sp, + struct servent_data *sd) +{ + _DIAGASSERT(name != NULL); + /* proto may be NULL */ + + setservent_r(sd->flags & _SV_STAYOPEN, sd); + sp = _servent_getbyname(sd, sp, name, proto); + if (!(sd->flags & _SV_STAYOPEN)) + _servent_close(sd); + return sp; +} diff -pruN /home/reed/src/isc/libbind/libbind/irs/getservbyport.c /usr/src/lib/libc/net/getservbyport.c --- /home/reed/src/isc/libbind/libbind/irs/getservbyport.c 1969-12-31 18:00:00.000000000 -0600 +++ /usr/src/lib/libc/net/getservbyport.c 2012-10-24 08:34:36.000000000 -0500 @@ -0,0 +1,62 @@ +/* $NetBSD: getservbyport.c,v 1.12 2008/04/28 20:23:00 martin Exp $ */ + +/*- + * Copyright (c) 2004 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: getservbyport.c,v 1.12 2008/04/28 20:23:00 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include "reentrant.h" + +#include + +#include "servent.h" + +#ifdef __weak_alias +__weak_alias(getservbyport,_getservbyport) +#endif + +#ifdef _REENTRANT +extern mutex_t _servent_mutex; +#endif +extern struct servent_data _servent_data; + +struct servent * +getservbyport(int port, const char *proto) +{ + struct servent *s; + + mutex_lock(&_servent_mutex); + s = getservbyport_r(port, proto, &_servent_data.serv, &_servent_data); + mutex_unlock(&_servent_mutex); + return (s); +} diff -pruN /home/reed/src/isc/libbind/libbind/irs/getservbyport_r.c /usr/src/lib/libc/net/getservbyport_r.c --- /home/reed/src/isc/libbind/libbind/irs/getservbyport_r.c 1969-12-31 18:00:00.000000000 -0600 +++ /usr/src/lib/libc/net/getservbyport_r.c 2013-06-05 09:26:12.000000000 -0500 @@ -0,0 +1,123 @@ +/* $NetBSD: getservbyport_r.c,v 1.9 2012/03/13 21:13:41 christos Exp $ */ + +/* + * Copyright (c) 1983, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)getservbyport.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: getservbyport_r.c,v 1.9 2012/03/13 21:13:41 christos Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include + +#include "servent.h" + +#ifdef __weak_alias +__weak_alias(getservbyport_r,_getservbyport_r) +#endif + +static struct servent * +_servent_getbyport(struct servent_data *sd, struct servent *sp, int port, + const char *proto) +{ + + if ((sd->flags & (_SV_CDB | _SV_PLAINFILE)) == 0) + return NULL; + + if (sd->flags & _SV_CDB) { + uint8_t buf[255 + 4]; + size_t protolen; + const uint8_t *data; + const void *data_ptr; + size_t datalen; + + port = be16toh(port); + + if (proto != NULL) { + protolen = strlen(proto); + if (protolen == 0 || protolen > 255) + return NULL; + } else + protolen = 0; + if (port < 0 || port > 65536) + return NULL; + + buf[0] = 0; + buf[1] = (uint8_t)protolen; + be16enc(buf + 2, port); + memcpy(buf + 4, proto, protolen); + + if (cdbr_find(sd->cdb, buf, 4 + protolen, + &data_ptr, &datalen)) + return NULL; + + if (datalen < protolen + 4) + return NULL; + + data = data_ptr; + if (be16dec(data) != port) + return NULL; + if (protolen) { + if (data[2] != protolen) + return NULL; + if (memcmp(data + 3, proto, protolen + 1)) + return NULL; + } + return _servent_parsedb(sd, sp, data, datalen); + } else { + while (_servent_getline(sd) != -1) { + if (_servent_parseline(sd, sp) == NULL) + continue; + if (sp->s_port != port) + continue; + if (proto == NULL || strcmp(sp->s_proto, proto) == 0) + return sp; + } + return NULL; + } +} + +struct servent * +getservbyport_r(int port, const char *proto, struct servent *sp, + struct servent_data *sd) +{ + setservent_r(sd->flags & _SV_STAYOPEN, sd); + sp = _servent_getbyport(sd, sp, port, proto); + if (!(sd->flags & _SV_STAYOPEN)) + _servent_close(sd); + return sp; +} diff -pruN /home/reed/src/isc/libbind/libbind/irs/getservent.c /usr/src/lib/libc/net/getservent.c --- /home/reed/src/isc/libbind/libbind/irs/getservent.c 2005-04-26 23:56:26.000000000 -0500 +++ /usr/src/lib/libc/net/getservent.c 2012-10-24 08:34:36.000000000 -0500 @@ -1,179 +1,80 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1996,1999 by Internet Software Consortium. +/* $NetBSD: getservent.c,v 1.12 2008/04/28 20:23:00 martin Exp $ */ + +/*- + * Copyright (c) 2004 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. */ -#if !defined(LINT) && !defined(CODECENTER) -static const char rcsid[] = "$Id: getservent.c,v 1.4 2005/04/27 04:56:26 sra Exp $"; +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: getservent.c,v 1.12 2008/04/28 20:23:00 martin Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include "reentrant.h" + +#include + +#include "servent.h" + +#ifdef __weak_alias +__weak_alias(endservent,_endservent) +__weak_alias(getservent,_getservent) +__weak_alias(setservent,_setservent) #endif -/* Imports */ - -#include "port_before.h" - -#if !defined(__BIND_NOSTATIC) - -#include - -#include -#include - -#include -#include -#include -#include - -#include - -#include "port_after.h" - -#include "irs_data.h" - -/* Forward */ - -static struct net_data *init(void); - -/* Public */ - -struct servent * -getservent(void) { - struct net_data *net_data = init(); - - return (getservent_p(net_data)); -} - -struct servent * -getservbyname(const char *name, const char *proto) { - struct net_data *net_data = init(); - - return (getservbyname_p(name, proto, net_data)); -} - -struct servent * -getservbyport(int port, const char *proto) { - struct net_data *net_data = init(); - - return (getservbyport_p(port, proto, net_data)); -} +#ifdef _REENTRANT +mutex_t _servent_mutex = MUTEX_INITIALIZER; +#endif +struct servent_data _servent_data; void -setservent(int stayopen) { - struct net_data *net_data = init(); - - setservent_p(stayopen, net_data); +setservent(int f) +{ + mutex_lock(&_servent_mutex); + setservent_r(f, &_servent_data); + mutex_unlock(&_servent_mutex); } void -endservent() { - struct net_data *net_data = init(); - - endservent_p(net_data); +endservent(void) +{ + mutex_lock(&_servent_mutex); + endservent_r(&_servent_data); + mutex_unlock(&_servent_mutex); } -/* Shared private. */ - struct servent * -getservent_p(struct net_data *net_data) { - struct irs_sv *sv; - - if (!net_data || !(sv = net_data->sv)) - return (NULL); - net_data->sv_last = (*sv->next)(sv); - return (net_data->sv_last); +getservent(void) +{ + struct servent *s; + + mutex_lock(&_servent_mutex); + s = getservent_r(&_servent_data.serv, &_servent_data); + mutex_unlock(&_servent_mutex); + return (s); } - -struct servent * -getservbyname_p(const char *name, const char *proto, - struct net_data *net_data) { - struct irs_sv *sv; - char **sap; - - if (!net_data || !(sv = net_data->sv)) - return (NULL); - if (net_data->sv_stayopen && net_data->sv_last) - if (!proto || !strcmp(net_data->sv_last->s_proto, proto)) { - if (!strcmp(net_data->sv_last->s_name, name)) - return (net_data->sv_last); - for (sap = net_data->sv_last->s_aliases; - sap && *sap; sap++) - if (!strcmp(name, *sap)) - return (net_data->sv_last); - } - net_data->sv_last = (*sv->byname)(sv, name, proto); - if (!net_data->sv_stayopen) - endservent(); - return (net_data->sv_last); -} - -struct servent * -getservbyport_p(int port, const char *proto, struct net_data *net_data) { - struct irs_sv *sv; - - if (!net_data || !(sv = net_data->sv)) - return (NULL); - if (net_data->sv_stayopen && net_data->sv_last) - if (port == net_data->sv_last->s_port && - ( !proto || - !strcmp(net_data->sv_last->s_proto, proto))) - return (net_data->sv_last); - net_data->sv_last = (*sv->byport)(sv, port, proto); - return (net_data->sv_last); -} - -void -setservent_p(int stayopen, struct net_data *net_data) { - struct irs_sv *sv; - - if (!net_data || !(sv = net_data->sv)) - return; - (*sv->rewind)(sv); - net_data->sv_stayopen = (stayopen != 0); - if (stayopen == 0) - net_data_minimize(net_data); -} - -void -endservent_p(struct net_data *net_data) { - struct irs_sv *sv; - - if ((net_data != NULL) && ((sv = net_data->sv) != NULL)) - (*sv->minimize)(sv); -} - -/* Private */ - -static struct net_data * -init() { - struct net_data *net_data; - - if (!(net_data = net_data_init(NULL))) - goto error; - if (!net_data->sv) { - net_data->sv = (*net_data->irs->sv_map)(net_data->irs); - - if (!net_data->sv || !net_data->res) { - error: - errno = EIO; - return (NULL); - } - (*net_data->sv->res_set)(net_data->sv, net_data->res, NULL); - } - - return (net_data); -} - -#endif /*__BIND_NOSTATIC*/ - -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/getservent_r.c /usr/src/lib/libc/net/getservent_r.c --- /home/reed/src/isc/libbind/libbind/irs/getservent_r.c 2006-07-31 20:14:16.000000000 -0500 +++ /usr/src/lib/libc/net/getservent_r.c 2012-10-24 08:34:36.000000000 -0500 @@ -1,242 +1,323 @@ +/* $NetBSD: getservent_r.c,v 1.11 2011/10/15 23:00:02 christos Exp $ */ + /* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1998-1999 by Internet Software Consortium. + * Copyright (c) 1983, 1993 + * The Regents of the University of California. All rights reserved. * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. */ +#include #if defined(LIBC_SCCS) && !defined(lint) -static const char rcsid[] = "$Id: getservent_r.c,v 1.6 2006/08/01 01:14:16 marka Exp $"; +#if 0 +static char sccsid[] = "@(#)getservent.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: getservent_r.c,v 1.11 2011/10/15 23:00:02 christos Exp $"); +#endif #endif /* LIBC_SCCS and not lint */ -#include -#if !defined(_REENTRANT) || !defined(DO_PTHREADS) - static int getservent_r_not_required = 0; -#else +#include "namespace.h" +#include #include -#include -#include -#include -#include +#include #include -#include -#include +#include +#include +#include -#ifdef SERV_R_RETURN +#include "servent.h" -static SERV_R_RETURN -copy_servent(struct servent *, struct servent *, SERV_R_COPY_ARGS); +#ifdef __weak_alias +__weak_alias(endservent_r,_endservent_r) +__weak_alias(getservent_r,_getservent_r) +__weak_alias(setservent_r,_setservent_r) +#endif -SERV_R_RETURN -getservbyname_r(const char *name, const char *proto, - struct servent *sptr, SERV_R_ARGS) { - struct servent *se = getservbyname(name, proto); -#ifdef SERV_R_SETANSWER - int n = 0; - - if (se == NULL || (n = copy_servent(se, sptr, SERV_R_COPY)) != 0) - *answerp = NULL; - else - *answerp = sptr; +int +_servent_open(struct servent_data *sd) +{ + if (sd->flags & (_SV_CDB | _SV_PLAINFILE)) { + sd->flags |= _SV_FIRST; + return 0; + } - return (n); -#else - if (se == NULL) - return (SERV_R_BAD); + free(sd->line); + sd->line = NULL; + free(sd->cdb_buf); + sd->cdb_buf = NULL; + sd->cdb_buf_len = 0; + free(sd->aliases); + sd->aliases = NULL; + sd->maxaliases = 0; + sd->flags |= _SV_FIRST; + + sd->cdb = cdbr_open(_PATH_SERVICES_CDB, CDBR_DEFAULT); + if (sd->cdb != NULL) { + sd->flags |= _SV_CDB; + return 0; + } + + sd->plainfile = fopen(_PATH_SERVICES, "re"); + if (sd->plainfile != NULL) { + sd->flags |= _SV_PLAINFILE; + return 0; + } + return -1; +} - return (copy_servent(se, sptr, SERV_R_COPY)); -#endif +void +_servent_close(struct servent_data *sd) +{ + if (sd->flags & _SV_CDB) { + cdbr_close(sd->cdb); + sd->cdb = NULL; + sd->flags &= ~_SV_CDB; + } + + if (sd->flags & _SV_PLAINFILE) { + (void)fclose(sd->plainfile); + sd->plainfile = NULL; + sd->flags &= ~_SV_PLAINFILE; + } + sd->flags &= ~_SV_STAYOPEN; } -SERV_R_RETURN -getservbyport_r(int port, const char *proto, - struct servent *sptr, SERV_R_ARGS) { - struct servent *se = getservbyport(port, proto); -#ifdef SERV_R_SETANSWER - int n = 0; - - if (se == NULL || (n = copy_servent(se, sptr, SERV_R_COPY)) != 0) - *answerp = NULL; - else - *answerp = sptr; - return (n); -#else - if (se == NULL) - return (SERV_R_BAD); +int +_servent_getline(struct servent_data *sd) +{ - return (copy_servent(se, sptr, SERV_R_COPY)); -#endif -} + if (sd->flags & _SV_CDB) + return -1; -/*% - * These assume a single context is in operation per thread. - * If this is not the case we will need to call irs directly - * rather than through the base functions. - */ + if ((sd->flags & _SV_PLAINFILE) == 0) + return -1; -SERV_R_RETURN -getservent_r(struct servent *sptr, SERV_R_ARGS) { - struct servent *se = getservent(); -#ifdef SERV_R_SETANSWER - int n = 0; - - if (se == NULL || (n = copy_servent(se, sptr, SERV_R_COPY)) != 0) - *answerp = NULL; - else - *answerp = sptr; + free(sd->line); + sd->line = NULL; - return (n); -#else - if (se == NULL) - return (SERV_R_BAD); + if (sd->flags & _SV_FIRST) { + (void)rewind((FILE *)sd->plainfile); + sd->flags &= ~_SV_FIRST; + } + sd->line = fparseln(sd->plainfile, NULL, NULL, NULL, + FPARSELN_UNESCALL); + return sd->line == NULL ? -1 : 0; +} - return (copy_servent(se, sptr, SERV_R_COPY)); -#endif +struct servent * +_servent_parseline(struct servent_data *sd, struct servent *sp) +{ + size_t i = 0; + int oerrno; + char *p, *cp, **q; + + if (sd->line == NULL) + return NULL; + + sp->s_name = p = sd->line; + p = strpbrk(p, " \t"); + if (p == NULL) + return NULL; + *p++ = '\0'; + while (*p == ' ' || *p == '\t') + p++; + cp = strpbrk(p, ",/"); + if (cp == NULL) + return NULL; + *cp++ = '\0'; + sp->s_port = htons((u_short)atoi(p)); + sp->s_proto = cp; + if (sd->aliases == NULL) { + sd->maxaliases = 10; + sd->aliases = calloc(sd->maxaliases, sizeof(*sd->aliases)); + if (sd->aliases == NULL) { + oerrno = errno; + endservent_r(sd); + errno = oerrno; + return NULL; + } + } + sp->s_aliases = sd->aliases; + cp = strpbrk(cp, " \t"); + if (cp != NULL) + *cp++ = '\0'; + while (cp && *cp) { + if (*cp == ' ' || *cp == '\t') { + cp++; + continue; + } + if (i == sd->maxaliases - 2) { + sd->maxaliases *= 2; + q = realloc(sd->aliases, sd->maxaliases * sizeof(*q)); + if (q == NULL) { + oerrno = errno; + endservent_r(sd); + errno = oerrno; + return NULL; + } + sp->s_aliases = sd->aliases = q; + } + sp->s_aliases[i++] = cp; + cp = strpbrk(cp, " \t"); + if (cp != NULL) + *cp++ = '\0'; + } + sp->s_aliases[i] = NULL; + return sp; } -SERV_R_SET_RETURN -#ifdef SERV_R_ENT_ARGS -setservent_r(int stay_open, SERV_R_ENT_ARGS) -#else -setservent_r(int stay_open) -#endif +void +setservent_r(int f, struct servent_data *sd) { -#ifdef SERV_R_ENT_UNUSED - SERV_R_ENT_UNUSED; -#endif - setservent(stay_open); -#ifdef SERV_R_SET_RESULT - return (SERV_R_SET_RESULT); -#endif + (void)_servent_open(sd); + sd->flags |= f ? _SV_STAYOPEN : 0; } -SERV_R_END_RETURN -#ifdef SERV_R_ENT_ARGS -endservent_r(SERV_R_ENT_ARGS) -#else -endservent_r() -#endif +void +endservent_r(struct servent_data *sd) { -#ifdef SERV_R_ENT_UNUSED - SERV_R_ENT_UNUSED; -#endif - endservent(); - SERV_R_END_RESULT(SERV_R_OK); + _servent_close(sd); + free(sd->aliases); + sd->aliases = NULL; + sd->maxaliases = 0; + free(sd->line); + sd->line = NULL; + free(sd->cdb_buf); + sd->cdb_buf = NULL; + sd->cdb_buf_len = 0; } -/* Private */ +struct servent * +getservent_r(struct servent *sp, struct servent_data *sd) +{ -#ifndef SERVENT_DATA -static SERV_R_RETURN -copy_servent(struct servent *se, struct servent *sptr, SERV_R_COPY_ARGS) { - char *cp; - int i, n; - int numptr, len; - - /* Find out the amount of space required to store the answer. */ - numptr = 1; /*%< NULL ptr */ - len = (char *)ALIGN(buf) - buf; - for (i = 0; se->s_aliases[i]; i++, numptr++) { - len += strlen(se->s_aliases[i]) + 1; - } - len += strlen(se->s_name) + 1; - len += strlen(se->s_proto) + 1; - len += numptr * sizeof(char*); - - if (len > (int)buflen) { - errno = ERANGE; - return (SERV_R_BAD); - } - - /* copy port value */ - sptr->s_port = se->s_port; - - cp = (char *)ALIGN(buf) + numptr * sizeof(char *); - - /* copy official name */ - n = strlen(se->s_name) + 1; - strcpy(cp, se->s_name); - sptr->s_name = cp; - cp += n; - - /* copy aliases */ - sptr->s_aliases = (char **)ALIGN(buf); - for (i = 0 ; se->s_aliases[i]; i++) { - n = strlen(se->s_aliases[i]) + 1; - strcpy(cp, se->s_aliases[i]); - sptr->s_aliases[i] = cp; - cp += n; - } - sptr->s_aliases[i] = NULL; - - /* copy proto */ - n = strlen(se->s_proto) + 1; - strcpy(cp, se->s_proto); - sptr->s_proto = cp; - cp += n; - - return (SERV_R_OK); -} -#else /* !SERVENT_DATA */ -static int -copy_servent(struct servent *se, struct servent *sptr, SERV_R_COPY_ARGS) { - char *cp, *eob; - int i, n; - - /* copy port value */ - sptr->s_port = se->s_port; - - /* copy official name */ - cp = sdptr->line; - eob = sdptr->line + sizeof(sdptr->line); - if ((n = strlen(se->s_name) + 1) < (eob - cp)) { - strcpy(cp, se->s_name); - sptr->s_name = cp; - cp += n; - } else { - return (-1); + if ((sd->flags & (_SV_CDB | _SV_PLAINFILE)) == 0 && + _servent_open(sd) == -1) + return NULL; + + if (sd->flags & _SV_CDB) { + const void *data; + size_t len; + + if (sd->flags & _SV_FIRST) { + sd->cdb_index = 0; + sd->flags &= ~_SV_FIRST; + } + + if (cdbr_get(sd->cdb, sd->cdb_index, &data, &len)) + return NULL; + ++sd->cdb_index; + return _servent_parsedb(sd, sp, data, len); + } + if (sd->flags & _SV_PLAINFILE) { + for (;;) { + if (_servent_getline(sd) == -1) + return NULL; + if (_servent_parseline(sd, sp) == NULL) + continue; + return sp; + } + } + return NULL; +} + +struct servent * +_servent_parsedb(struct servent_data *sd, struct servent *sp, + const uint8_t *data, size_t len) +{ + char **q; + size_t i; + int oerrno; + + if ((sd->flags & _SV_STAYOPEN) == 0) { + if (len > sd->cdb_buf_len) { + void *tmp = realloc(sd->cdb_buf, len); + if (tmp == NULL) + goto fail; + sd->cdb_buf = tmp; + sd->cdb_buf_len = len; + } + memcpy(sd->cdb_buf, data, len); + data = sd->cdb_buf; } - /* copy aliases */ + if (len < 2) + goto fail; + sp->s_port = htobe16(be16dec(data)); + data += 2; + len -= 2; + + if (len == 0 || len < (size_t)data[0] + 2) + goto fail; + sp->s_proto = __UNCONST(data + 1); + + if (sp->s_proto[data[0]] != '\0') + goto fail; + + len -= 2 + data[0]; + data += 2 + data[0]; + + if (len == 0) + goto fail; + if (len < (size_t)data[0] + 2) + goto fail; + + sp->s_name = __UNCONST(data + 1); + len -= 2 + data[0]; + data += 2 + data[0]; + + if (sd->aliases == NULL) { + sd->maxaliases = 10; + sd->aliases = malloc(sd->maxaliases * sizeof(char *)); + if (sd->aliases == NULL) + goto fail; + } + sp->s_aliases = sd->aliases; i = 0; - sptr->s_aliases = sdptr->serv_aliases; - while (se->s_aliases[i] && i < (_MAXALIASES-1)) { - if ((n = strlen(se->s_aliases[i]) + 1) < (eob - cp)) { - strcpy(cp, se->s_aliases[i]); - sptr->s_aliases[i] = cp; - cp += n; - } else { - break; - } - i++; - } - sptr->s_aliases[i] = NULL; - - /* copy proto */ - if ((n = strlen(se->s_proto) + 1) < (eob - cp)) { - strcpy(cp, se->s_proto); - sptr->s_proto = cp; - cp += n; - } else { - return (-1); - } - - return (SERV_R_OK); -} -#endif /* !SERVENT_DATA */ -#else /*SERV_R_RETURN */ - static int getservent_r_unknown_system = 0; -#endif /*SERV_R_RETURN */ -#endif /* !defined(_REENTRANT) || !defined(DO_PTHREADS) */ -/*! \file */ + while (len) { + if (len < (size_t)data[0] + 2) + goto fail; + if (i == sd->maxaliases - 2) { + sd->maxaliases *= 2; + q = realloc(sd->aliases, sd->maxaliases * sizeof(*q)); + if (q == NULL) + goto fail; + sp->s_aliases = sd->aliases = q; + } + sp->s_aliases[i++] = __UNCONST(data + 1); + len -= 2 + data[0]; + data += 2 + data[0]; + } + sp->s_aliases[i] = NULL; + return sp; + +fail: + oerrno = errno; + endservent_r(sd); + errno = oerrno; + return NULL; +} + diff -pruN /home/reed/src/isc/libbind/libbind/irs/hesiod.c /usr/src/lib/libc/net/hesiod.c --- /home/reed/src/isc/libbind/libbind/irs/hesiod.c 2005-07-28 01:51:48.000000000 -0500 +++ /usr/src/lib/libc/net/hesiod.c 2013-06-05 09:26:12.000000000 -0500 @@ -1,505 +1,632 @@ -#if defined(LIBC_SCCS) && !defined(lint) -static const char rcsid[] = "$Id: hesiod.c,v 1.7 2005/07/28 06:51:48 marka Exp $"; -#endif +/* $NetBSD: hesiod.c,v 1.27 2012/03/20 17:44:18 matt Exp $ */ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1996,1999 by Internet Software Consortium. +/* Copyright (c) 1996 by Internet Software Consortium. * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS + * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE + * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL + * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR + * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS + * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS + * SOFTWARE. */ +/* Copyright 1996 by the Massachusetts Institute of Technology. + * + * Permission to use, copy, modify, and distribute this + * software and its documentation for any purpose and without + * fee is hereby granted, provided that the above copyright + * notice appear in all copies and that both that copyright + * notice and this permission notice appear in supporting + * documentation, and that the name of M.I.T. not be used in + * advertising or publicity pertaining to distribution of the + * software without specific, written prior permission. + * M.I.T. makes no representations about the suitability of + * this software for any purpose. It is provided "as is" + * without express or implied warranty. + */ -/*! \file - * \brief - * hesiod.c --- the core portion of the hesiod resolver. +/* This file is part of the hesiod library. It implements the core + * portion of the hesiod resolver. * - * This file is derived from the hesiod library from Project Athena; - * It has been extensively rewritten by Theodore Ts'o to have a more - * thread-safe interface. - * \author - * This file is primarily maintained by <tytso@mit.edu> and <ghudson@mit.edu>. + * This file is loosely based on an interim version of hesiod.c from + * the BIND IRS library, which was in turn based on an earlier version + * of this file. Extensive changes have been made on each step of the + * path. + * + * This implementation is thread-safe because it uses res_nsend(). */ -/* Imports */ +#include + +#if defined(LIBC_SCCS) && !defined(lint) +__IDSTRING(rcsid_hesiod_c, + "#Id: hesiod.c,v 1.18.2.1 1997/01/03 20:48:20 ghudson Exp #"); +__IDSTRING(rcsid_hesiod_p_h, + "#Id: hesiod_p.h,v 1.1 1996/12/08 21:39:37 ghudson Exp #"); +__IDSTRING(rcsid_hescompat_c, + "#Id: hescompat.c,v 1.1.2.1 1996/12/16 08:37:45 ghudson Exp #"); +__RCSID("$NetBSD: hesiod.c,v 1.27 2012/03/20 17:44:18 matt Exp $"); +#endif /* LIBC_SCCS and not lint */ -#include "port_before.h" +#include "namespace.h" #include +#include #include #include +#include +#include #include -#include +#include #include #include #include #include +#include -#include "port_after.h" +#ifdef __weak_alias +__weak_alias(hesiod_init,_hesiod_init) +__weak_alias(hesiod_end,_hesiod_end) +__weak_alias(hesiod_to_bind,_hesiod_to_bind) +__weak_alias(hesiod_resolve,_hesiod_resolve) +__weak_alias(hesiod_free_list,_hesiod_free_list) +__weak_alias(hes_init,_hes_init) +__weak_alias(hes_to_bind,_hes_to_bind) +__weak_alias(hes_resolve,_hes_resolve) +__weak_alias(hes_error,_hes_error) +__weak_alias(hes_free,_hes_free) +#endif -#include "pathnames.h" -#include "hesiod.h" -#include "hesiod_p.h" - -/* Forward */ - -int hesiod_init(void **context); -void hesiod_end(void *context); -char * hesiod_to_bind(void *context, const char *name, - const char *type); -char ** hesiod_resolve(void *context, const char *name, - const char *type); -void hesiod_free_list(void *context, char **list); - -static int parse_config_file(struct hesiod_p *ctx, const char *filename); -static char ** get_txt_records(struct hesiod_p *ctx, int class, - const char *name); -static int init(struct hesiod_p *ctx); +struct hesiod_p { + char *lhs; /* normally ".ns" */ + char *rhs; /* AKA the default hesiod domain */ + int classes[2]; /* The class search order. */ +}; + +#define MAX_HESRESP 1024 + +static int read_config_file(struct hesiod_p *, const char *); +static char **get_txt_records(int, const char *); +static int init_context(void); +static void translate_errors(void); -/* Public */ -/*% - * This function is called to initialize a hesiod_p. +/* + * hesiod_init -- + * initialize a hesiod_p. */ -int -hesiod_init(void **context) { - struct hesiod_p *ctx; - char *cp; - - ctx = malloc(sizeof(struct hesiod_p)); - if (ctx == 0) { - errno = ENOMEM; - return (-1); - } - - memset(ctx, 0, sizeof (*ctx)); - - if (parse_config_file(ctx, _PATH_HESIOD_CONF) < 0) { -#ifdef DEF_RHS +int +hesiod_init(void **context) +{ + struct hesiod_p *ctx; + const char *p, *configname; + int serrno; + + _DIAGASSERT(context != NULL); + + ctx = calloc(1, sizeof(struct hesiod_p)); + if (ctx) { + *context = ctx; /* - * Use compiled in defaults. + * don't permit overrides from environment + * for set.id programs */ - ctx->LHS = malloc(strlen(DEF_LHS) + 1); - ctx->RHS = malloc(strlen(DEF_RHS) + 1); - if (ctx->LHS == NULL || ctx->RHS == NULL) { - errno = ENOMEM; - goto cleanup; - } - strcpy(ctx->LHS, DEF_LHS); /* (checked) */ - strcpy(ctx->RHS, DEF_RHS); /* (checked) */ -#else - goto cleanup; -#endif - } - /* - * The default RHS can be overridden by an environment - * variable. - */ - if ((cp = getenv("HES_DOMAIN")) != NULL) { - size_t RHSlen = strlen(cp) + 2; - if (ctx->RHS) - free(ctx->RHS); - ctx->RHS = malloc(RHSlen); - if (!ctx->RHS) { - errno = ENOMEM; - goto cleanup; - } - if (cp[0] == '.') { - strcpy(ctx->RHS, cp); /* (checked) */ - } else { - strcpy(ctx->RHS, "."); /* (checked) */ - strcat(ctx->RHS, cp); /* (checked) */ + if (issetugid()) + configname = NULL; + else + configname = getenv("HESIOD_CONFIG"); + if (!configname) + configname = _PATH_HESIOD_CONF; + if (read_config_file(ctx, configname) >= 0) { + /* + * The default rhs can be overridden by an + * environment variable, unless set.id. + */ + if (issetugid()) + p = NULL; + else + p = getenv("HES_DOMAIN"); + if (p) { + if (ctx->rhs) + free(ctx->rhs); + ctx->rhs = malloc(strlen(p) + 2); + if (ctx->rhs) { + *ctx->rhs = '.'; + strcpy(ctx->rhs + 1, + (*p == '.') ? p + 1 : p); + return 0; + } else + errno = ENOMEM; + } else + return 0; } - } + } else + errno = ENOMEM; - /* - * If there is no default hesiod realm set, we return an - * error. - */ - if (!ctx->RHS) { - errno = ENOEXEC; - goto cleanup; + serrno = errno; + if (ctx) { + if (ctx->lhs) + free(ctx->lhs); + if (ctx->rhs) + free(ctx->rhs); + free(ctx); } - -#if 0 - if (res_ninit(ctx->res) < 0) - goto cleanup; -#endif - - *context = ctx; - return (0); - - cleanup: - hesiod_end(ctx); - return (-1); + errno = serrno; + return -1; } -/*% - * This function deallocates the hesiod_p +/* + * hesiod_end -- + * Deallocates the hesiod_p. */ -void -hesiod_end(void *context) { +void +hesiod_end(void *context) +{ struct hesiod_p *ctx = (struct hesiod_p *) context; - int save_errno = errno; - if (ctx->res) - res_nclose(ctx->res); - if (ctx->RHS) - free(ctx->RHS); - if (ctx->LHS) - free(ctx->LHS); - if (ctx->res && ctx->free_res) - (*ctx->free_res)(ctx->res); + _DIAGASSERT(context != NULL); + + free(ctx->rhs); + if (ctx->lhs) + free(ctx->lhs); free(ctx); - errno = save_errno; } -/*% - * This function takes a hesiod (name, type) and returns a DNS - * name which is to be resolved. +/* + * hesiod_to_bind -- + * takes a hesiod (name, type) and returns a DNS + * name which is to be resolved. */ char * -hesiod_to_bind(void *context, const char *name, const char *type) { +hesiod_to_bind(void *context, const char *name, const char *type) +{ struct hesiod_p *ctx = (struct hesiod_p *) context; - char *bindname; - char **rhs_list = NULL; - const char *RHS, *cp; - - /* Decide what our RHS is, and set cp to the end of the actual name. */ - if ((cp = strchr(name, '@')) != NULL) { - if (strchr(cp + 1, '.')) - RHS = cp + 1; - else if ((rhs_list = hesiod_resolve(context, cp + 1, - "rhs-extension")) != NULL) - RHS = *rhs_list; - else { - errno = ENOENT; - return (NULL); - } - } else { - RHS = ctx->RHS; - cp = name + strlen(name); - } + char bindname[MAXDNAME], *p, *ret, **rhs_list = NULL; + const char *rhs; + size_t len; + + _DIAGASSERT(context != NULL); + _DIAGASSERT(name != NULL); + _DIAGASSERT(type != NULL); + + if (strlcpy(bindname, name, sizeof(bindname)) >= sizeof(bindname)) { + errno = EMSGSIZE; + return NULL; + } /* - * Allocate the space we need, including up to three periods and - * the terminating NUL. + * Find the right right hand side to use, possibly + * truncating bindname. */ - if ((bindname = malloc((cp - name) + strlen(type) + strlen(RHS) + - (ctx->LHS ? strlen(ctx->LHS) : 0) + 4)) == NULL) { - errno = ENOMEM; + p = strchr(bindname, '@'); + if (p) { + *p++ = 0; + if (strchr(p, '.')) + rhs = name + (p - bindname); + else { + rhs_list = hesiod_resolve(context, p, "rhs-extension"); + if (rhs_list) + rhs = *rhs_list; + else { + errno = ENOENT; + return NULL; + } + } + } else + rhs = ctx->rhs; + + /* See if we have enough room. */ + len = strlen(bindname) + 1 + strlen(type); + if (ctx->lhs) + len += strlen(ctx->lhs) + ((ctx->lhs[0] != '.') ? 1 : 0); + len += strlen(rhs) + ((rhs[0] != '.') ? 1 : 0); + if (len > sizeof(bindname) - 1) { if (rhs_list) hesiod_free_list(context, rhs_list); + errno = EMSGSIZE; return NULL; } + /* Put together the rest of the domain. */ + strlcat(bindname, ".", sizeof(bindname)); + strlcat(bindname, type, sizeof(bindname)); + /* Only append lhs if it isn't empty. */ + if (ctx->lhs && ctx->lhs[0] != '\0' ) { + if (ctx->lhs[0] != '.') + strlcat(bindname, ".", sizeof(bindname)); + strlcat(bindname, ctx->lhs, sizeof(bindname)); + } + if (rhs[0] != '.') + strlcat(bindname, ".", sizeof(bindname)); + strlcat(bindname, rhs, sizeof(bindname)); - /* Now put together the DNS name. */ - memcpy(bindname, name, cp - name); - bindname[cp - name] = '\0'; - strcat(bindname, "."); - strcat(bindname, type); - if (ctx->LHS) { - if (ctx->LHS[0] != '.') - strcat(bindname, "."); - strcat(bindname, ctx->LHS); - } - if (RHS[0] != '.') - strcat(bindname, "."); - strcat(bindname, RHS); - + /* rhs_list is no longer needed, since we're done with rhs. */ if (rhs_list) hesiod_free_list(context, rhs_list); - return (bindname); + /* Make a copy of the result and return it to the caller. */ + ret = strdup(bindname); + if (ret == NULL) + errno = ENOMEM; + return ret; } -/*% - * This is the core function. Given a hesiod (name, type), it - * returns an array of strings returned by the resolver. +/* + * hesiod_resolve -- + * Given a hesiod name and type, return an array of strings returned + * by the resolver. */ char ** -hesiod_resolve(void *context, const char *name, const char *type) { - struct hesiod_p *ctx = (struct hesiod_p *) context; - char *bindname = hesiod_to_bind(context, name, type); - char **retvec; +hesiod_resolve(void *context, const char *name, const char *type) +{ + struct hesiod_p *ctx = (struct hesiod_p *) context; + char *bindname, **retvec; + + _DIAGASSERT(context != NULL); + _DIAGASSERT(name != NULL); + _DIAGASSERT(type != NULL); - if (bindname == NULL) - return (NULL); - if (init(ctx) == -1) { - free(bindname); - return (NULL); - } + bindname = hesiod_to_bind(context, name, type); + if (!bindname) + return NULL; - if ((retvec = get_txt_records(ctx, C_IN, bindname))) { - free(bindname); - return (retvec); - } - - if (errno != ENOENT) - return (NULL); + retvec = get_txt_records(ctx->classes[0], bindname); + if (retvec == NULL && errno == ENOENT && ctx->classes[1]) + retvec = get_txt_records(ctx->classes[1], bindname); - retvec = get_txt_records(ctx, C_HS, bindname); free(bindname); - return (retvec); + return retvec; } -void -hesiod_free_list(void *context, char **list) { - char **p; +/*ARGSUSED*/ +void +hesiod_free_list(void *context, char **list) +{ + char **p; - UNUSED(context); + _DIAGASSERT(context != NULL); + if (list == NULL) + return; for (p = list; *p; p++) free(*p); free(list); } -/*% - * This function parses the /etc/hesiod.conf file - */ -static int -parse_config_file(struct hesiod_p *ctx, const char *filename) { - char *key, *data, *cp, **cpp; - char buf[MAXDNAME+7]; - FILE *fp; - /* - * Clear the existing configuration variable, just in case - * they're set. - */ - if (ctx->RHS) - free(ctx->RHS); - if (ctx->LHS) - free(ctx->LHS); - ctx->RHS = ctx->LHS = 0; +/* read_config_file -- + * Parse the /etc/hesiod.conf file. Returns 0 on success, + * -1 on failure. On failure, it might leave values in ctx->lhs + * or ctx->rhs which need to be freed by the caller. + */ +static int +read_config_file(struct hesiod_p *ctx, const char *filename) +{ + char *buf, *key, *data, *p, **which; + int n; + FILE *fp; + + _DIAGASSERT(ctx != NULL); + _DIAGASSERT(filename != NULL); + + /* Set default query classes. */ + ctx->classes[0] = C_IN; + ctx->classes[1] = C_HS; + + /* Try to open the configuration file. */ + fp = fopen(filename, "r"); + if (!fp) { + /* Use compiled in default domain names. */ + ctx->lhs = strdup(DEF_LHS); + ctx->rhs = strdup(DEF_RHS); + if (ctx->lhs && ctx->rhs) + return 0; + else { + errno = ENOMEM; + return -1; + } + } + ctx->lhs = NULL; + ctx->rhs = NULL; + for (; (buf = fparseln(fp, NULL, NULL, NULL, FPARSELN_UNESCALL)) + != NULL; free(buf)) { + p = buf; + while (*p == ' ' || *p == '\t') + p++; + key = p; + while (*p != ' ' && *p != '\t' && *p != '=' && *p) + p++; - /* - * Now open and parse the file... - */ - if (!(fp = fopen(filename, "r"))) - return (-1); - - while (fgets(buf, sizeof(buf), fp) != NULL) { - cp = buf; - if (*cp == '#' || *cp == '\n' || *cp == '\r') + if (*p == '\0') continue; - while(*cp == ' ' || *cp == '\t') - cp++; - key = cp; - while(*cp != ' ' && *cp != '\t' && *cp != '=') - cp++; - *cp++ = '\0'; - - while(*cp == ' ' || *cp == '\t' || *cp == '=') - cp++; - data = cp; - while(*cp != ' ' && *cp != '\n' && *cp != '\r') - cp++; - *cp++ = '\0'; - - if (strcmp(key, "lhs") == 0) - cpp = &ctx->LHS; - else if (strcmp(key, "rhs") == 0) - cpp = &ctx->RHS; - else + + *p++ = 0; + + while (isspace((u_char) *p) || *p == '=') + p++; + + if (*p == '\0') continue; - *cpp = malloc(strlen(data) + 1); - if (!*cpp) { - errno = ENOMEM; - goto cleanup; + data = p; + while (!isspace((u_char) *p) && *p) + p++; + + *p = 0; + + if (strcasecmp(key, "lhs") == 0 || + strcasecmp(key, "rhs") == 0) { + which = (strcasecmp(key, "lhs") == 0) + ? &ctx->lhs : &ctx->rhs; + *which = strdup(data); + if (!*which) { + errno = ENOMEM; + free(buf); + (void)fclose(fp); + return -1; + } + } else { + if (strcasecmp(key, "classes") == 0) { + n = 0; + while (*data && n < 2) { + p = data; + while (*p && *p != ',') + p++; + if (*p) + *p++ = 0; + if (strcasecmp(data, "IN") == 0) + ctx->classes[n++] = C_IN; + else + if (strcasecmp(data, "HS") == 0) + ctx->classes[n++] = + C_HS; + data = p; + } + while (n < 2) + ctx->classes[n++] = 0; + } } - strcpy(*cpp, data); } fclose(fp); - return (0); - - cleanup: - fclose(fp); - if (ctx->RHS) - free(ctx->RHS); - if (ctx->LHS) - free(ctx->LHS); - ctx->RHS = ctx->LHS = 0; - return (-1); + + if (!ctx->rhs || ctx->classes[0] == 0 || + ctx->classes[0] == ctx->classes[1]) { + errno = ENOEXEC; + return -1; + } + return 0; } -/*% - * Given a DNS class and a DNS name, do a lookup for TXT records, and - * return a list of them. +/* + * get_txt_records -- + * Given a DNS class and a DNS name, do a lookup for TXT records, and + * return a list of them. */ static char ** -get_txt_records(struct hesiod_p *ctx, int class, const char *name) { - struct { - int type; /*%< RR type */ - int class; /*%< RR class */ - int dlen; /*%< len of data section */ - u_char *data; /*%< pointer to data */ - } rr; - HEADER *hp; - u_char qbuf[MAX_HESRESP], abuf[MAX_HESRESP]; - u_char *cp, *erdata, *eom; - char *dst, *edst, **list; - int ancount, qdcount; - int i, j, n, skip; +get_txt_records(int qclass, const char *name) +{ + HEADER *hp; + unsigned char qbuf[PACKETSZ], abuf[MAX_HESRESP], *p, *eom, *eor; + char *dst, **list; + int ancount, qdcount, i, j, n, skip, type, class, len; + res_state res = __res_get_state(); - /* - * Construct the query and send it. - */ - n = res_nmkquery(ctx->res, QUERY, name, class, T_TXT, NULL, 0, - NULL, qbuf, MAX_HESRESP); + if (res == NULL) + return NULL; + + _DIAGASSERT(name != NULL); + + /* Construct the query. */ + n = res_nmkquery(res, QUERY, name, qclass, T_TXT, NULL, 0, + NULL, qbuf, PACKETSZ); if (n < 0) { errno = EMSGSIZE; - return (NULL); + __res_put_state(res); + return NULL; } - n = res_nsend(ctx->res, qbuf, n, abuf, MAX_HESRESP); + + /* Send the query. */ + n = res_nsend(res, qbuf, n, abuf, MAX_HESRESP); + __res_put_state(res); if (n < 0) { errno = ECONNREFUSED; - return (NULL); - } - if (n < HFIXEDSZ) { - errno = EMSGSIZE; - return (NULL); + return NULL; } - - /* - * OK, parse the result. - */ - hp = (HEADER *) abuf; + /* Parse the header of the result. */ + hp = (HEADER *) (void *) abuf; ancount = ntohs(hp->ancount); qdcount = ntohs(hp->qdcount); - cp = abuf + sizeof(HEADER); + p = abuf + sizeof(HEADER); eom = abuf + n; - /* Skip query, trying to get to the answer section which follows. */ + /* + * Skip questions, trying to get to the answer section + * which follows. + */ for (i = 0; i < qdcount; i++) { - skip = dn_skipname(cp, eom); - if (skip < 0 || cp + skip + QFIXEDSZ > eom) { + skip = dn_skipname(p, eom); + if (skip < 0 || p + skip + QFIXEDSZ > eom) { errno = EMSGSIZE; - return (NULL); + return NULL; } - cp += skip + QFIXEDSZ; + p += skip + QFIXEDSZ; } + /* Allocate space for the text record answers. */ list = malloc((ancount + 1) * sizeof(char *)); if (!list) { errno = ENOMEM; - return (NULL); + return NULL; } + /* Parse the answers. */ j = 0; for (i = 0; i < ancount; i++) { - skip = dn_skipname(cp, eom); - if (skip < 0) { + /* Parse the header of this answer. */ + skip = dn_skipname(p, eom); + if (skip < 0 || p + skip + 10 > eom) + break; + type = p[skip + 0] << 8 | p[skip + 1]; + class = p[skip + 2] << 8 | p[skip + 3]; + len = p[skip + 8] << 8 | p[skip + 9]; + p += skip + 10; + if (p + len > eom) { errno = EMSGSIZE; - goto cleanup; + break; } - cp += skip; - if (cp + 3 * INT16SZ + INT32SZ > eom) { - errno = EMSGSIZE; - goto cleanup; + /* Skip entries of the wrong class and type. */ + if (class != qclass || type != T_TXT) { + p += len; + continue; } - rr.type = ns_get16(cp); - cp += INT16SZ; - rr.class = ns_get16(cp); - cp += INT16SZ + INT32SZ; /*%< skip the ttl, too */ - rr.dlen = ns_get16(cp); - cp += INT16SZ; - if (cp + rr.dlen > eom) { - errno = EMSGSIZE; - goto cleanup; + /* Allocate space for this answer. */ + list[j] = malloc((size_t)len); + if (!list[j]) { + errno = ENOMEM; + break; } - rr.data = cp; - cp += rr.dlen; - if (rr.class != class || rr.type != T_TXT) - continue; - if (!(list[j] = malloc(rr.dlen))) - goto cleanup; dst = list[j++]; - edst = dst + rr.dlen; - erdata = rr.data + rr.dlen; - cp = rr.data; - while (cp < erdata) { - n = (unsigned char) *cp++; - if (cp + n > eom || dst + n > edst) { + + /* Copy answer data into the allocated area. */ + eor = p + len; + while (p < eor) { + n = (unsigned char) *p++; + if (p + n > eor) { errno = EMSGSIZE; - goto cleanup; + break; } - memcpy(dst, cp, n); - cp += n; + memcpy(dst, p, (size_t)n); + p += n; dst += n; } - if (cp != erdata) { + if (p < eor) { errno = EMSGSIZE; - goto cleanup; + break; } - *dst = '\0'; + *dst = 0; + } + + /* + * If we didn't terminate the loop normally, something + * went wrong. + */ + if (i < ancount) { + for (i = 0; i < j; i++) + free(list[i]); + free(list); + return NULL; } - list[j] = NULL; if (j == 0) { errno = ENOENT; - goto cleanup; + free(list); + return NULL; } - return (list); + list[j] = NULL; + return list; +} - cleanup: - for (i = 0; i < j; i++) - free(list[i]); - free(list); - return (NULL); +/* + * COMPATIBILITY FUNCTIONS + */ + +static int inited = 0; +static void *context; +static int errval = HES_ER_UNINIT; + +int +hes_init(void) +{ + init_context(); + return errval; } -struct __res_state * -__hesiod_res_get(void *context) { - struct hesiod_p *ctx = context; +char * +hes_to_bind(const char *name, const char *type) +{ + static char *bindname; - if (!ctx->res) { - struct __res_state *res; - res = (struct __res_state *)malloc(sizeof *res); - if (res == NULL) { - errno = ENOMEM; - return (NULL); - } - memset(res, 0, sizeof *res); - __hesiod_res_set(ctx, res, free); - } + _DIAGASSERT(name != NULL); + _DIAGASSERT(type != NULL); - return (ctx->res); + if (init_context() < 0) + return NULL; + if (bindname) + free(bindname); + bindname = hesiod_to_bind(context, name, type); + if (!bindname) + translate_errors(); + return bindname; } -void -__hesiod_res_set(void *context, struct __res_state *res, - void (*free_res)(void *)) { - struct hesiod_p *ctx = context; +char ** +hes_resolve(const char *name, const char *type) +{ + static char **list; - if (ctx->res && ctx->free_res) { - res_nclose(ctx->res); - (*ctx->free_res)(ctx->res); - } + _DIAGASSERT(name != NULL); + _DIAGASSERT(type != NULL); - ctx->res = res; - ctx->free_res = free_res; + if (init_context() < 0) + return NULL; + + /* + * In the old Hesiod interface, the caller was responsible for + * freeing the returned strings but not the vector of strings itself. + */ + if (list) + free(list); + + list = hesiod_resolve(context, name, type); + if (!list) + translate_errors(); + return list; +} + +int +hes_error(void) +{ + return errval; +} + +void +hes_free(char **hp) +{ + hesiod_free_list(context, hp); } static int -init(struct hesiod_p *ctx) { - - if (!ctx->res && !__hesiod_res_get(ctx)) - return (-1); - - if (((ctx->res->options & RES_INIT) == 0U) && - (res_ninit(ctx->res) == -1)) - return (-1); +init_context(void) +{ + if (!inited) { + inited = 1; + if (hesiod_init(&context) < 0) { + errval = HES_ER_CONFIG; + return -1; + } + errval = HES_ER_OK; + } + return 0; +} - return (0); +static void +translate_errors(void) +{ + switch (errno) { + case ENOENT: + errval = HES_ER_NOTFOUND; + break; + case ECONNREFUSED: + case EMSGSIZE: + errval = HES_ER_NET; + break; + default: + /* Not a good match, but the best we can do. */ + errval = HES_ER_CONFIG; + break; + } } diff -pruN /home/reed/src/isc/libbind/libbind/irs/hesiod_p.h /usr/src/lib/libc/net/hesiod_p.h --- /home/reed/src/isc/libbind/libbind/irs/hesiod_p.h 2005-04-26 23:56:27.000000000 -0500 +++ /usr/src/lib/libc/net/hesiod_p.h 1969-12-31 18:00:00.000000000 -0600 @@ -1,48 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1996,1999 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -/* - * $Id: hesiod_p.h,v 1.3 2005/04/27 04:56:27 sra Exp $ - */ - -#ifndef _HESIOD_P_H_INCLUDED -#define _HESIOD_P_H_INCLUDED - -/** \file - * \brief - * hesiod_p.h -- private definitions for the hesiod library. - * - * \author - * This file is primarily maintained by tytso@mit.edu and ghudson@mit.edu. - */ - -#define DEF_RHS ".Athena.MIT.EDU" /*%< Defaults if HESIOD_CONF */ -#define DEF_LHS ".ns" /*%< file is not */ - /*%< present. */ -struct hesiod_p { - char * LHS; /*%< normally ".ns" */ - char * RHS; /*%< AKA the default hesiod domain */ - struct __res_state * res; /*%< resolver context */ - void (*free_res)(void *); - void (*res_set)(struct hesiod_p *, struct __res_state *, - void (*)(void *)); - struct __res_state * (*res_get)(struct hesiod_p *); -}; - -#define MAX_HESRESP 1024 - -#endif /*_HESIOD_P_H_INCLUDED*/ diff -pruN /home/reed/src/isc/libbind/libbind/irs/if_indextoname.c /usr/src/lib/libc/net/if_indextoname.c --- /home/reed/src/isc/libbind/libbind/irs/if_indextoname.c 1969-12-31 18:00:00.000000000 -0600 +++ /usr/src/lib/libc/net/if_indextoname.c 2012-10-24 08:34:36.000000000 -0500 @@ -0,0 +1,101 @@ +/* $NetBSD: if_indextoname.c,v 1.7 2010/12/13 23:10:12 pooka Exp $ */ +/* $KAME: if_indextoname.c,v 1.7 2000/11/08 03:09:30 itojun Exp $ */ + +/*- + * Copyright (c) 1997, 2000 + * Berkeley Software Design, Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * BSDI Id: if_indextoname.c,v 2.3 2000/04/17 22:38:05 dab Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: if_indextoname.c,v 1.7 2010/12/13 23:10:12 pooka Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#ifndef RUMP_ACTION +#include "namespace.h" +#endif +#include +#include +#include +#include +#include +#include +#include +#include + +#ifndef RUMP_ACTION +#ifdef __weak_alias +__weak_alias(if_indextoname,_if_indextoname) +#endif +#endif + +/* + * From RFC 2533: + * + * The second function maps an interface index into its corresponding + * name. + * + * #include + * + * char *if_indextoname(unsigned int ifindex, char *ifname); + * + * The ifname argument must point to a buffer of at least IF_NAMESIZE + * bytes into which the interface name corresponding to the specified + * index is returned. (IF_NAMESIZE is also defined in and + * its value includes a terminating null byte at the end of the + * interface name.) This pointer is also the return value of the + * function. If there is no interface corresponding to the specified + * index, NULL is returned, and errno is set to ENXIO, if there was a + * system error (such as running out of memory), if_indextoname returns + * NULL and errno would be set to the proper value (e.g., ENOMEM). + */ + +char * +if_indextoname(unsigned int ifindex, char *ifname) +{ + struct ifaddrs *ifaddrs, *ifa; + int error = 0; + + if (getifaddrs(&ifaddrs) < 0) + return(NULL); /* getifaddrs properly set errno */ + + for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) { + if (ifa->ifa_addr && + ifa->ifa_addr->sa_family == AF_LINK && + ifindex == ((struct sockaddr_dl*) + (void *)ifa->ifa_addr)->sdl_index) + break; + } + + if (ifa == NULL) { + error = ENXIO; + ifname = NULL; + } + else + strlcpy(ifname, ifa->ifa_name, IFNAMSIZ); + + freeifaddrs(ifaddrs); + + errno = error; + return(ifname); +} diff -pruN /home/reed/src/isc/libbind/libbind/irs/if_nameindex.c /usr/src/lib/libc/net/if_nameindex.c --- /home/reed/src/isc/libbind/libbind/irs/if_nameindex.c 1969-12-31 18:00:00.000000000 -0600 +++ /usr/src/lib/libc/net/if_nameindex.c 2013-06-05 09:26:12.000000000 -0500 @@ -0,0 +1,155 @@ +/* $NetBSD: if_nameindex.c,v 1.7 2012/03/13 21:13:41 christos Exp $ */ +/* $KAME: if_nameindex.c,v 1.8 2000/11/24 08:20:01 itojun Exp $ */ + +/*- + * Copyright (c) 1997, 2000 + * Berkeley Software Design, Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * BSDI Id: if_nameindex.c,v 2.3 2000/04/17 22:38:05 dab Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: if_nameindex.c,v 1.7 2012/03/13 21:13:41 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(if_nameindex,_if_nameindex) +__weak_alias(if_freenameindex,_if_freenameindex) +#endif +/* + * From RFC 2553: + * + * 4.3 Return All Interface Names and Indexes + * + * The if_nameindex structure holds the information about a single + * interface and is defined as a result of including the + * header. + * + * struct if_nameindex { + * unsigned int if_index; + * char *if_name; + * }; + * + * The final function returns an array of if_nameindex structures, one + * structure per interface. + * + * struct if_nameindex *if_nameindex(void); + * + * The end of the array of structures is indicated by a structure with + * an if_index of 0 and an if_name of NULL. The function returns a NULL + * pointer upon an error, and would set errno to the appropriate value. + * + * The memory used for this array of structures along with the interface + * names pointed to by the if_name members is obtained dynamically. + * This memory is freed by the next function. + * + * 4.4. Free Memory + * + * The following function frees the dynamic memory that was allocated by + * if_nameindex(). + * + * #include + * + * void if_freenameindex(struct if_nameindex *ptr); + * + * The argument to this function must be a pointer that was returned by + * if_nameindex(). + */ + +struct if_nameindex * +if_nameindex(void) +{ + struct ifaddrs *ifaddrs, *ifa; + size_t nbytes, ni; + struct if_nameindex *ifni, *ifni2; + char *cp; + + if (getifaddrs(&ifaddrs) < 0) + return(NULL); + + /* + * First, find out how many interfaces there are, and how + * much space we need for the string names. + */ + ni = 0; + nbytes = 0; + for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) { + if (ifa->ifa_addr && + ifa->ifa_addr->sa_family == AF_LINK) { + nbytes += strlen(ifa->ifa_name) + 1; + ni++; + } + } + + /* + * Next, allocate a chunk of memory, use the first part + * for the array of structures, and the last part for + * the strings. + */ + cp = malloc((ni + 1) * sizeof(struct if_nameindex) + nbytes); + ifni = (struct if_nameindex *)(void *)cp; + if (ifni == NULL) + goto out; + cp += (ni + 1) * sizeof(struct if_nameindex); + + /* + * Now just loop through the list of interfaces again, + * filling in the if_nameindex array and making copies + * of all the strings. + */ + ifni2 = ifni; + for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) { + if (ifa->ifa_addr && + ifa->ifa_addr->sa_family == AF_LINK) { + ifni2->if_index = + ((struct sockaddr_dl*) + (void *)ifa->ifa_addr)->sdl_index; + ifni2->if_name = cp; + strcpy(cp, ifa->ifa_name); + ifni2++; + cp += strlen(cp) + 1; + } + } + /* + * Finally, don't forget to terminate the array. + */ + ifni2->if_index = 0; + ifni2->if_name = NULL; +out: + freeifaddrs(ifaddrs); + return(ifni); +} + +void +if_freenameindex(struct if_nameindex *ptr) +{ + free(ptr); +} diff -pruN /home/reed/src/isc/libbind/libbind/irs/if_nametoindex.c /usr/src/lib/libc/net/if_nametoindex.c --- /home/reed/src/isc/libbind/libbind/irs/if_nametoindex.c 1969-12-31 18:00:00.000000000 -0600 +++ /usr/src/lib/libc/net/if_nametoindex.c 2000-11-24 02:21:12.000000000 -0600 @@ -0,0 +1,92 @@ +/* $NetBSD: if_nametoindex.c,v 1.4 2000/11/24 08:21:12 itojun Exp $ */ +/* $KAME: if_nametoindex.c,v 1.6 2000/11/24 08:18:54 itojun Exp $ */ + +/*- + * Copyright (c) 1997, 2000 + * Berkeley Software Design, Inc. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * THIS SOFTWARE IS PROVIDED BY Berkeley Software Design, Inc. ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL Berkeley Software Design, Inc. BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * BSDI Id: if_nametoindex.c,v 2.3 2000/04/17 22:38:05 dab Exp + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: if_nametoindex.c,v 1.4 2000/11/24 08:21:12 itojun Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(if_nametoindex,_if_nametoindex) +#endif + +/* + * From RFC 2553: + * + * 4.1 Name-to-Index + * + * + * The first function maps an interface name into its corresponding + * index. + * + * #include + * + * unsigned int if_nametoindex(const char *ifname); + * + * If the specified interface name does not exist, the return value is + * 0, and errno is set to ENXIO. If there was a system error (such as + * running out of memory), the return value is 0 and errno is set to the + * proper value (e.g., ENOMEM). + */ + +unsigned int +if_nametoindex(const char *ifname) +{ + struct ifaddrs *ifaddrs, *ifa; + unsigned int ni; + + if (getifaddrs(&ifaddrs) < 0) + return(0); + + ni = 0; + + for (ifa = ifaddrs; ifa != NULL; ifa = ifa->ifa_next) { + if (ifa->ifa_addr && + ifa->ifa_addr->sa_family == AF_LINK && + strcmp(ifa->ifa_name, ifname) == 0) { + ni = ((struct sockaddr_dl*) + (void *)ifa->ifa_addr)->sdl_index; + break; + } + } + + freeifaddrs(ifaddrs); + if (!ni) + errno = ENXIO; + return(ni); +} diff -pruN /home/reed/src/isc/libbind/libbind/irs/ip6opt.c /usr/src/lib/libc/net/ip6opt.c --- /home/reed/src/isc/libbind/libbind/irs/ip6opt.c 1969-12-31 18:00:00.000000000 -0600 +++ /usr/src/lib/libc/net/ip6opt.c 2013-06-05 09:26:13.000000000 -0500 @@ -0,0 +1,660 @@ +/* $NetBSD: ip6opt.c,v 1.14 2012/03/20 17:44:18 matt Exp $ */ + +/* + * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the project nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: ip6opt.c,v 1.14 2012/03/20 17:44:18 matt Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include + +#include +#include + +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(inet6_option_alloc,_inet6_option_alloc) +__weak_alias(inet6_option_append,_inet6_option_append) +__weak_alias(inet6_option_find,_inet6_option_find) +__weak_alias(inet6_option_init,_inet6_option_init) +__weak_alias(inet6_option_next,_inet6_option_next) +__weak_alias(inet6_option_space,_inet6_option_space) +__weak_alias(inet6_opt_init, _inet6_opt_init) +__weak_alias(inet6_opt_append, _inet6_opt_append) +__weak_alias(inet6_opt_finish, _inet6_opt_finish) +__weak_alias(inet6_opt_set_val, _inet6_opt_set_val) +__weak_alias(inet6_opt_next, _inet6_opt_next) +__weak_alias(inet6_opt_find, _inet6_opt_find) +__weak_alias(inet6_opt_get_val, _inet6_opt_get_val) +#endif + +static int ip6optlen(uint8_t *opt, uint8_t *lim); +static void inet6_insert_padopt(uint8_t *p, size_t len); + +/* + * This function returns the number of bytes required to hold an option + * when it is stored as ancillary data, including the cmsghdr structure + * at the beginning, and any padding at the end (to make its size a + * multiple of 8 bytes). The argument is the size of the structure + * defining the option, which must include any pad bytes at the + * beginning (the value y in the alignment term "xn + y"), the type + * byte, the length byte, and the option data. + */ +int +inet6_option_space(int nbytes) +{ + size_t sp; + nbytes += 2; /* we need space for nxt-hdr and length fields */ + sp = CMSG_SPACE((nbytes + 7) & ~7); + _DIAGASSERT(__type_fit(int, sp)); + return (int)sp; +} + +/* + * This function is called once per ancillary data object that will + * contain either Hop-by-Hop or Destination options. It returns 0 on + * success or -1 on an error. + */ +int +inet6_option_init(void *bp, struct cmsghdr **cmsgp, int type) +{ + register struct cmsghdr *ch; + + _DIAGASSERT(bp != NULL); + _DIAGASSERT(cmsgp != NULL); + + ch = (struct cmsghdr *)bp; + + /* argument validation */ + if (type != IPV6_HOPOPTS && type != IPV6_DSTOPTS) + return(-1); + + ch->cmsg_level = IPPROTO_IPV6; + ch->cmsg_type = type; + ch->cmsg_len = CMSG_LEN(0); + + *cmsgp = ch; + return(0); +} + +/* + * This function appends a Hop-by-Hop option or a Destination option + * into an ancillary data object that has been initialized by + * inet6_option_init(). This function returns 0 if it succeeds or -1 on + * an error. + * multx is the value x in the alignment term "xn + y" described + * earlier. It must have a value of 1, 2, 4, or 8. + * plusy is the value y in the alignment term "xn + y" described + * earlier. It must have a value between 0 and 7, inclusive. + */ +int +inet6_option_append(struct cmsghdr *cmsg, const uint8_t *typep, int multx, + int plusy) +{ + size_t padlen, optlen, off; + register uint8_t *bp; + struct ip6_ext *eh; + + _DIAGASSERT(cmsg != NULL); + _DIAGASSERT(typep != NULL); + + bp = (uint8_t *)(void *)cmsg + cmsg->cmsg_len; + eh = (struct ip6_ext *)(void *)CMSG_DATA(cmsg); + + /* argument validation */ + if (multx != 1 && multx != 2 && multx != 4 && multx != 8) + return(-1); + if (plusy < 0 || plusy > 7) + return(-1); + + /* + * If this is the first option, allocate space for the + * first 2 bytes(for next header and length fields) of + * the option header. + */ + if (bp == (uint8_t *)(void *)eh) { + bp += 2; + cmsg->cmsg_len += 2; + } + + /* calculate pad length before the option. */ + off = bp - (uint8_t *)(void *)eh; + padlen = (((off % multx) + (multx - 1)) & ~(multx - 1)) - + (off % multx); + padlen += plusy; + padlen %= multx; /* keep the pad as short as possible */ + /* insert padding */ + inet6_insert_padopt(bp, padlen); + _DIAGASSERT(__type_fit(socklen_t, padlen + cmsg->cmsg_len)); + cmsg->cmsg_len += (socklen_t)padlen; + bp += padlen; + + /* copy the option */ + if (typep[0] == IP6OPT_PAD1) + optlen = 1; + else + optlen = typep[1] + 2; + memcpy(bp, typep, (size_t)optlen); + bp += optlen; + _DIAGASSERT(__type_fit(socklen_t, optlen + cmsg->cmsg_len)); + cmsg->cmsg_len += (socklen_t)optlen; + + /* calculate pad length after the option and insert the padding */ + off = bp - (uint8_t *)(void *)eh; + padlen = ((off + 7) & ~7) - off; + inet6_insert_padopt(bp, padlen); + bp += padlen; + _DIAGASSERT(__type_fit(socklen_t, padlen + cmsg->cmsg_len)); + cmsg->cmsg_len += (socklen_t)padlen; + + /* update the length field of the ip6 option header */ + off = bp - (uint8_t *)(void *)eh; + _DIAGASSERT(__type_fit(uint8_t, (off >> 3) - 1)); + eh->ip6e_len = (uint8_t)((off >> 3) - 1); + + return(0); +} + +/* + * This function appends a Hop-by-Hop option or a Destination option + * into an ancillary data object that has been initialized by + * inet6_option_init(). This function returns a pointer to the 8-bit + * option type field that starts the option on success, or NULL on an + * error. + * The difference between this function and inet6_option_append() is + * that the latter copies the contents of a previously built option into + * the ancillary data object while the current function returns a + * pointer to the space in the data object where the option's TLV must + * then be built by the caller. + * + */ +uint8_t * +inet6_option_alloc(struct cmsghdr *cmsg, int datalen, int multx, int plusy) +{ + size_t padlen, off; + register uint8_t *bp; + uint8_t *retval; + struct ip6_ext *eh; + + _DIAGASSERT(cmsg != NULL); + + bp = (uint8_t *)(void *)cmsg + cmsg->cmsg_len; + eh = (struct ip6_ext *)(void *)CMSG_DATA(cmsg); + + /* argument validation */ + if (multx != 1 && multx != 2 && multx != 4 && multx != 8) + return(NULL); + if (plusy < 0 || plusy > 7) + return(NULL); + + /* + * If this is the first option, allocate space for the + * first 2 bytes(for next header and length fields) of + * the option header. + */ + if (bp == (uint8_t *)(void *)eh) { + bp += 2; + cmsg->cmsg_len += 2; + } + + /* calculate pad length before the option. */ + off = bp - (uint8_t *)(void *)eh; + padlen = (((off % multx) + (multx - 1)) & ~(multx - 1)) - + (off % multx); + padlen += plusy; + padlen %= multx; /* keep the pad as short as possible */ + /* insert padding */ + inet6_insert_padopt(bp, padlen); + cmsg->cmsg_len += (socklen_t)padlen; + bp += padlen; + + /* keep space to store specified length of data */ + retval = bp; + bp += datalen; + cmsg->cmsg_len += datalen; + + /* calculate pad length after the option and insert the padding */ + off = bp - (uint8_t *)(void *)eh; + padlen = ((off + 7) & ~7) - off; + inet6_insert_padopt(bp, padlen); + bp += padlen; + _DIAGASSERT(__type_fit(socklen_t, padlen + cmsg->cmsg_len)); + cmsg->cmsg_len += (socklen_t)padlen; + + /* update the length field of the ip6 option header */ + off = bp - (uint8_t *)(void *)eh; + _DIAGASSERT(__type_fit(uint8_t, (off >> 3) - 1)); + eh->ip6e_len = (uint8_t)((off >> 3) - 1); + + return(retval); +} + +/* + * This function processes the next Hop-by-Hop option or Destination + * option in an ancillary data object. If another option remains to be + * processed, the return value of the function is 0 and *tptrp points to + * the 8-bit option type field (which is followed by the 8-bit option + * data length, followed by the option data). If no more options remain + * to be processed, the return value is -1 and *tptrp is NULL. If an + * error occurs, the return value is -1 and *tptrp is not NULL. + * (RFC 2292, 6.3.5) + */ +int +inet6_option_next(const struct cmsghdr *cmsg, uint8_t **tptrp) +{ + struct ip6_ext *ip6e; + int hdrlen, optlen; + uint8_t *lim; + + _DIAGASSERT(cmsg != NULL); + _DIAGASSERT(tptrp != NULL); + + if (cmsg->cmsg_level != IPPROTO_IPV6 || + (cmsg->cmsg_type != IPV6_HOPOPTS && + cmsg->cmsg_type != IPV6_DSTOPTS)) + return(-1); + + /* message length validation */ + if (cmsg->cmsg_len < CMSG_SPACE(sizeof(struct ip6_ext))) + return(-1); + ip6e = __UNCONST(CCMSG_DATA(cmsg)); + hdrlen = (ip6e->ip6e_len + 1) << 3; + if (cmsg->cmsg_len < CMSG_SPACE(hdrlen)) + return(-1); + + /* + * If the caller does not specify the starting point, + * simply return the 1st option. + * Otherwise, search the option list for the next option. + */ + lim = (uint8_t *)(void *)ip6e + hdrlen; + if (*tptrp == NULL) + *tptrp = (uint8_t *)(void *)(ip6e + 1); + else { + if ((optlen = ip6optlen(*tptrp, lim)) == 0) + return(-1); + + *tptrp = *tptrp + optlen; + } + if (*tptrp >= lim) { /* there is no option */ + *tptrp = NULL; + return(-1); + } + /* + * Finally, checks if the next option is safely stored in the + * cmsg data. + */ + if (ip6optlen(*tptrp, lim) == 0) + return(-1); + else + return(0); +} + +/* + * This function is similar to the inet6_option_next() function, + * except this function lets the caller specify the option type to be + * searched for, instead of always returning the next option in the + * ancillary data object. + * Note: RFC 2292 says the type of tptrp is uint8_t *, but we think + * it's a typo. The variable should be type of uint8_t **. + */ +int +inet6_option_find(const struct cmsghdr *cmsg, uint8_t **tptrp, int type) +{ + struct ip6_ext *ip6e; + int hdrlen, optlen; + uint8_t *optp, *lim; + + _DIAGASSERT(cmsg != NULL); + _DIAGASSERT(tptrp != NULL); + + if (cmsg->cmsg_level != IPPROTO_IPV6 || + (cmsg->cmsg_type != IPV6_HOPOPTS && + cmsg->cmsg_type != IPV6_DSTOPTS)) + return(-1); + + /* message length validation */ + if (cmsg->cmsg_len < CMSG_SPACE(sizeof(struct ip6_ext))) + return(-1); + ip6e = __UNCONST(CCMSG_DATA(cmsg)); + hdrlen = (ip6e->ip6e_len + 1) << 3; + if (cmsg->cmsg_len < CMSG_SPACE(hdrlen)) + return(-1); + + /* + * If the caller does not specify the starting point, + * search from the beginning of the option list. + * Otherwise, search from *the next option* of the specified point. + */ + lim = (uint8_t *)(void *)ip6e + hdrlen; + if (*tptrp == NULL) + *tptrp = (uint8_t *)(void *)(ip6e + 1); + else { + if ((optlen = ip6optlen(*tptrp, lim)) == 0) + return(-1); + + *tptrp = *tptrp + optlen; + } + for (optp = *tptrp; optp < lim; optp += optlen) { + if (*optp == type) { + *tptrp = optp; + return(0); + } + if ((optlen = ip6optlen(optp, lim)) == 0) + return(-1); + } + + /* search failed */ + *tptrp = NULL; + return(-1); +} + +/* + * Calculate the length of a given IPv6 option. Also checks + * if the option is safely stored in user's buffer according to the + * calculated length and the limitation of the buffer. + */ +static int +ip6optlen(uint8_t *opt, uint8_t *lim) +{ + int optlen; + + _DIAGASSERT(opt != NULL); + _DIAGASSERT(lim != NULL); + + if (*opt == IP6OPT_PAD1) + optlen = 1; + else { + /* is there enough space to store type and len? */ + if (opt + 2 > lim) + return(0); + optlen = *(opt + 1) + 2; + } + if (opt + optlen <= lim) + return(optlen); + + return(0); +} + +static void +inet6_insert_padopt(uint8_t *p, size_t len) +{ + + _DIAGASSERT(p != NULL); + + switch(len) { + case 0: + return; + case 1: + p[0] = IP6OPT_PAD1; + return; + default: + p[0] = IP6OPT_PADN; + _DIAGASSERT(__type_fit(uint8_t, len - 2)); + p[1] = (uint8_t)(len - 2); + memset(&p[2], 0, len - 2); + return; + } +} + +/* + * The following functions are defined in RFC3542, which is a successor + * of RFC2292. + */ + +int +inet6_opt_init(void *extbuf, socklen_t extlen) +{ + struct ip6_ext *ext = (struct ip6_ext *)extbuf; + + if (extlen % 8) + return (-1); + + if (ext) { + if (extlen == 0) + return (-1); + ext->ip6e_len = (extlen >> 3) - 1; + } + + return (2); /* sizeof the next and the length fields */ +} + +int +inet6_opt_append(void *extbuf, socklen_t extlen, int offset, uint8_t type, + socklen_t len, uint8_t align, void **databufp) +{ + int currentlen = offset; + size_t padlen = 0; + + /* + * The option type must have a value from 2 to 255, inclusive. + * (0 and 1 are reserved for the Pad1 and PadN options, respectively.) + */ + if (type < 2) + return (-1); + + /* + * The option data length must have a value between 0 and 255, + * inclusive, and is the length of the option data that follows. + */ + if (len > 255) + return (-1); + + /* + * The align parameter must have a value of 1, 2, 4, or 8. + * The align value can not exceed the value of len. + */ + if (align != 1 && align != 2 && align != 4 && align != 8) + return (-1); + if (align > len) + return (-1); + + /* Calculate the padding length. */ + currentlen += 2 + len; /* 2 means "type + len" */ + if (currentlen % align) + padlen = align - (currentlen % align); + + /* The option must fit in the extension header buffer. */ + _DIAGASSERT(__type_fit(int, currentlen + padlen)); + currentlen += (int)padlen; + if (extlen && /* XXX: right? */ + (socklen_t)currentlen > extlen) + return (-1); + + if (extbuf) { + uint8_t *optp = (uint8_t *)extbuf + offset; + + if (padlen == 1) { + /* insert a Pad1 option */ + *optp = IP6OPT_PAD1; + optp++; + } else if (padlen > 0) { + /* insert a PadN option for alignment */ + *optp++ = IP6OPT_PADN; + _DIAGASSERT(__type_fit(uint8_t, padlen - 2)); + *optp++ = (uint8_t)(padlen - 2); + memset(optp, 0, padlen - 2); + optp += (padlen - 2); + } + + *optp++ = type; + *optp++ = len; + + *databufp = optp; + } + + return (currentlen); +} + +int +inet6_opt_finish(void *extbuf, socklen_t extlen, int offset) +{ + int updatelen = offset > 0 ? (1 + ((offset - 1) | 7)) : 0; + + if (extbuf) { + uint8_t *padp; + size_t padlen = updatelen - offset; + + if ((socklen_t)updatelen > extlen || padlen >= 256 + 2) + return (-1); + + padp = (uint8_t *)extbuf + offset; + if (padlen == 1) + *padp = IP6OPT_PAD1; + else if (padlen > 0) { + *padp++ = IP6OPT_PADN; + *padp++ = (uint8_t)(padlen - 2); + memset(padp, 0, padlen - 2); + } + } + + return (updatelen); +} + +int +inet6_opt_set_val(void *databuf, int offset, void *val, socklen_t vallen) +{ + + memcpy((uint8_t *)databuf + offset, val, vallen); + return (offset + vallen); +} + +int +inet6_opt_next(void *extbuf, socklen_t extlen, int offset, uint8_t *typep, + socklen_t *lenp, void **databufp) +{ + uint8_t *optp, *lim; + int optlen; + + /* Validate extlen. XXX: is the variable really necessary?? */ + if (extlen == 0 || (extlen % 8)) + return (-1); + lim = (uint8_t *)extbuf + extlen; + + /* + * If this is the first time this function called for this options + * header, simply return the 1st option. + * Otherwise, search the option list for the next option. + */ + if (offset == 0) + optp = (uint8_t *)(void *)((struct ip6_hbh *)extbuf + 1); + else + optp = (uint8_t *)extbuf + offset; + + /* Find the next option skipping any padding options. */ + while (optp < lim) { + ptrdiff_t rv; + switch(*optp) { + case IP6OPT_PAD1: + optp++; + break; + case IP6OPT_PADN: + if ((optlen = ip6optlen(optp, lim)) == 0) + goto optend; + optp += optlen; + break; + default: /* found */ + if ((optlen = ip6optlen(optp, lim)) == 0) + goto optend; + *typep = *optp; + *lenp = optlen - 2; + *databufp = optp + 2; + rv = optp + optlen - (uint8_t *)extbuf; + _DIAGASSERT(__type_fit(int, rv)); + return (int)rv; + } + } + + optend: + *databufp = NULL; /* for safety */ + return (-1); +} + +int +inet6_opt_find(void *extbuf, socklen_t extlen, int offset, uint8_t type, + socklen_t *lenp, void **databufp) +{ + uint8_t *optp, *lim; + int optlen; + + /* Validate extlen. XXX: is the variable really necessary?? */ + if (extlen == 0 || (extlen % 8)) + return (-1); + lim = (uint8_t *)extbuf + extlen; + + /* + * If this is the first time this function called for this options + * header, simply return the 1st option. + * Otherwise, search the option list for the next option. + */ + if (offset == 0) + optp = (uint8_t *)(void *)((struct ip6_hbh *)extbuf + 1); + else + optp = (uint8_t *)extbuf + offset; + + /* Find the specified option */ + while (optp < lim) { + if ((optlen = ip6optlen(optp, lim)) == 0) + goto optend; + + if (*optp == type) { /* found */ + ptrdiff_t td; + *lenp = optlen - 2; + *databufp = optp + 2; + td = optp + optlen - (uint8_t *)extbuf; + _DIAGASSERT(__type_fit(int, td)); + return (int)td; + } + + optp += optlen; + } + + optend: + *databufp = NULL; /* for safety */ + return (-1); +} + +int +inet6_opt_get_val(void *databuf, int offset, void *val, socklen_t vallen) +{ + + /* we can't assume alignment here */ + memcpy(val, (uint8_t *)databuf + offset, vallen); + + return (offset + vallen); +} diff -pruN /home/reed/src/isc/libbind/libbind/irs/irp.c /usr/src/lib/libc/net/irp.c --- /home/reed/src/isc/libbind/libbind/irs/irp.c 2008-11-13 20:36:51.000000000 -0600 +++ /usr/src/lib/libc/net/irp.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,583 +0,0 @@ -/* - * Copyright (C) 2004-2006, 2008 Internet Systems Consortium, Inc. ("ISC") - * Copyright (C) 1996, 1998-2001, 2003 Internet Software Consortium. - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH - * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY - * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, - * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM - * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE - * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR - * PERFORMANCE OF THIS SOFTWARE. - */ - -#if !defined(LINT) && !defined(CODECENTER) -static const char rcsid[] = "$Id: irp.c,v 1.12 2008/11/14 02:36:51 marka Exp $"; -#endif - -/* Imports */ - -#include "port_before.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -#include -#include - -#include "irs_p.h" -#include "irp_p.h" - -#include "port_after.h" - -/* Forward. */ - -static void irp_close(struct irs_acc *); - -#define LINEINCR 128 - -#if !defined(SUN_LEN) -#define SUN_LEN(su) \ - (sizeof (*(su)) - sizeof ((su)->sun_path) + strlen((su)->sun_path)) -#endif - - -/* Public */ - - -/* send errors to syslog if true. */ -int irp_log_errors = 1; - -/*% - * This module handles the irp module connection to irpd. - * - * The client expects a synchronous interface to functions like - * getpwnam(3), so we can't use the ctl_* i/o library on this end of - * the wire (it's used in the server). - */ - -/*% - * irs_acc *irs_irp_acc(const char *options); - * - * Initialize the irp module. - */ -struct irs_acc * -irs_irp_acc(const char *options) { - struct irs_acc *acc; - struct irp_p *irp; - - UNUSED(options); - - if (!(acc = memget(sizeof *acc))) { - errno = ENOMEM; - return (NULL); - } - memset(acc, 0x5e, sizeof *acc); - if (!(irp = memget(sizeof *irp))) { - errno = ENOMEM; - free(acc); - return (NULL); - } - irp->inlast = 0; - irp->incurr = 0; - irp->fdCxn = -1; - acc->private = irp; - -#ifdef WANT_IRS_GR - acc->gr_map = irs_irp_gr; -#else - acc->gr_map = NULL; -#endif -#ifdef WANT_IRS_PW - acc->pw_map = irs_irp_pw; -#else - acc->pw_map = NULL; -#endif - acc->sv_map = irs_irp_sv; - acc->pr_map = irs_irp_pr; - acc->ho_map = irs_irp_ho; - acc->nw_map = irs_irp_nw; - acc->ng_map = irs_irp_ng; - acc->close = irp_close; - return (acc); -} - - -int -irs_irp_connection_setup(struct irp_p *cxndata, int *warned) { - if (irs_irp_is_connected(cxndata)) { - return (0); - } else if (irs_irp_connect(cxndata) != 0) { - if (warned != NULL && !*warned) { - syslog(LOG_ERR, "irpd connection failed: %m\n"); - (*warned)++; - } - - return (-1); - } - - return (0); -} - -/*% - * int irs_irp_connect(void); - * - * Sets up the connection to the remote irpd server. - * - * Returns: - * - * 0 on success, -1 on failure. - * - */ -int -irs_irp_connect(struct irp_p *pvt) { - int flags; - struct sockaddr *addr; - struct sockaddr_in iaddr; -#ifndef NO_SOCKADDR_UN - struct sockaddr_un uaddr; -#endif - long ipaddr; - const char *irphost; - int code; - char text[256]; - int socklen = 0; - - if (pvt->fdCxn != -1) { - perror("fd != 1"); - return (-1); - } - -#ifndef NO_SOCKADDR_UN - memset(&uaddr, 0, sizeof uaddr); -#endif - memset(&iaddr, 0, sizeof iaddr); - - irphost = getenv(IRPD_HOST_ENV); - if (irphost == NULL) { - irphost = "127.0.0.1"; - } - -#ifndef NO_SOCKADDR_UN - if (irphost[0] == '/') { - addr = (struct sockaddr *)&uaddr; - strncpy(uaddr.sun_path, irphost, sizeof uaddr.sun_path); - uaddr.sun_family = AF_UNIX; - socklen = SUN_LEN(&uaddr); -#ifdef HAVE_SA_LEN - uaddr.sun_len = socklen; -#endif - } else -#endif - { - if (inet_pton(AF_INET, irphost, &ipaddr) != 1) { - errno = EADDRNOTAVAIL; - perror("inet_pton"); - return (-1); - } - - addr = (struct sockaddr *)&iaddr; - socklen = sizeof iaddr; -#ifdef HAVE_SA_LEN - iaddr.sin_len = socklen; -#endif - iaddr.sin_family = AF_INET; - iaddr.sin_port = htons(IRPD_PORT); - iaddr.sin_addr.s_addr = ipaddr; - } - - - pvt->fdCxn = socket(addr->sa_family, SOCK_STREAM, PF_UNSPEC); - if (pvt->fdCxn < 0) { - perror("socket"); - return (-1); - } - - if (connect(pvt->fdCxn, addr, socklen) != 0) { - perror("connect"); - return (-1); - } - - flags = fcntl(pvt->fdCxn, F_GETFL, 0); - if (flags < 0) { - close(pvt->fdCxn); - perror("close"); - return (-1); - } - -#if 0 - flags |= O_NONBLOCK; - if (fcntl(pvt->fdCxn, F_SETFL, flags) < 0) { - close(pvt->fdCxn); - perror("fcntl"); - return (-1); - } -#endif - - code = irs_irp_read_response(pvt, text, sizeof text); - if (code != IRPD_WELCOME_CODE) { - if (irp_log_errors) { - syslog(LOG_WARNING, "Connection failed: %s", text); - } - irs_irp_disconnect(pvt); - return (-1); - } - - return (0); -} - -/*% - * int irs_irp_is_connected(struct irp_p *pvt); - * - * Returns: - * - * Non-zero if streams are setup to remote. - * - */ - -int -irs_irp_is_connected(struct irp_p *pvt) { - return (pvt->fdCxn >= 0); -} - -/*% - * void - * irs_irp_disconnect(struct irp_p *pvt); - * - * Closes streams to remote. - */ - -void -irs_irp_disconnect(struct irp_p *pvt) { - if (pvt->fdCxn != -1) { - close(pvt->fdCxn); - pvt->fdCxn = -1; - } -} - - - -int -irs_irp_read_line(struct irp_p *pvt, char *buffer, int len) { - char *realstart = &pvt->inbuffer[0]; - char *p, *start, *end; - int spare; - int i; - int buffpos = 0; - int left = len - 1; - - while (left > 0) { - start = p = &pvt->inbuffer[pvt->incurr]; - end = &pvt->inbuffer[pvt->inlast]; - - while (p != end && *p != '\n') - p++; - - if (p == end) { - /* Found no newline so shift data down if necessary - * and append new data to buffer - */ - if (start > realstart) { - memmove(realstart, start, end - start); - pvt->inlast = end - start; - start = realstart; - pvt->incurr = 0; - end = &pvt->inbuffer[pvt->inlast]; - } - - spare = sizeof (pvt->inbuffer) - pvt->inlast; - - p = end; - i = read(pvt->fdCxn, end, spare); - if (i < 0) { - close(pvt->fdCxn); - pvt->fdCxn = -1; - return (buffpos > 0 ? buffpos : -1); - } else if (i == 0) { - return (buffpos); - } - - end += i; - pvt->inlast += i; - - while (p != end && *p != '\n') - p++; - } - - if (p == end) { - /* full buffer and still no newline */ - i = sizeof pvt->inbuffer; - } else { - /* include newline */ - i = p - start + 1; - } - - if (i > left) - i = left; - memcpy(buffer + buffpos, start, i); - pvt->incurr += i; - buffpos += i; - buffer[buffpos] = '\0'; - - if (p != end) { - left = 0; - } else { - left -= i; - } - } - -#if 0 - fprintf(stderr, "read line: %s\n", buffer); -#endif - return (buffpos); -} - -/*% - * int irp_read_response(struct irp_p *pvt); - * - * Returns: - * - * The number found at the beginning of the line read from - * FP. 0 on failure(0 is not a legal response code). The - * rest of the line is discarded. - * - */ - -int -irs_irp_read_response(struct irp_p *pvt, char *text, size_t textlen) { - char line[1024]; - int code; - char *p; - - if (irs_irp_read_line(pvt, line, sizeof line) <= 0) { - return (0); - } - - p = strchr(line, '\n'); - if (p == NULL) { - return (0); - } - - if (sscanf(line, "%d", &code) != 1) { - code = 0; - } else if (text != NULL && textlen > 0U) { - p = line; - while (isspace((unsigned char)*p)) p++; - while (isdigit((unsigned char)*p)) p++; - while (isspace((unsigned char)*p)) p++; - strncpy(text, p, textlen - 1); - p[textlen - 1] = '\0'; - } - - return (code); -} - -/*% - * char *irp_read_body(struct irp_p *pvt, size_t *size); - * - * Read in the body of a response. Terminated by a line with - * just a dot on it. Lines should be terminated with a CR-LF - * sequence, but we're nt piccky if the CR is missing. - * No leading dot escaping is done as the protcol doesn't - * use leading dots anywhere. - * - * Returns: - * - * Pointer to null-terminated buffer allocated by memget. - * *SIZE is set to the length of the buffer. - * - */ - -char * -irs_irp_read_body(struct irp_p *pvt, size_t *size) { - char line[1024]; - u_int linelen; - size_t len = LINEINCR; - char *buffer = memget(len); - int idx = 0; - - if (buffer == NULL) - return (NULL); - - for (;;) { - if (irs_irp_read_line(pvt, line, sizeof line) <= 0 || - strchr(line, '\n') == NULL) - goto death; - - linelen = strlen(line); - - if (line[linelen - 1] != '\n') - goto death; - - /* We're not strict about missing \r. Should we be?? */ - if (linelen > 2 && line[linelen - 2] == '\r') { - line[linelen - 2] = '\n'; - line[linelen - 1] = '\0'; - linelen--; - } - - if (linelen == 2 && line[0] == '.') { - *size = len; - buffer[idx] = '\0'; - - return (buffer); - } - - if (linelen > (len - (idx + 1))) { - char *p = memget(len + LINEINCR); - - if (p == NULL) - goto death; - memcpy(p, buffer, len); - memput(buffer, len); - buffer = p; - len += LINEINCR; - } - - memcpy(buffer + idx, line, linelen); - idx += linelen; - } - death: - memput(buffer, len); - return (NULL); -} - -/*% - * int irs_irp_get_full_response(struct irp_p *pvt, int *code, - * char **body, size_t *bodylen); - * - * Gets the response to a command. If the response indicates - * there's a body to follow(code % 10 == 1), then the - * body buffer is allcoated with memget and stored in - * *BODY. The length of the allocated body buffer is stored - * in *BODY. The caller must give the body buffer back to - * memput when done. The results code is stored in *CODE. - * - * Returns: - * - * 0 if a result was read. -1 on some sort of failure. - * - */ - -int -irs_irp_get_full_response(struct irp_p *pvt, int *code, char *text, - size_t textlen, char **body, size_t *bodylen) { - int result = irs_irp_read_response(pvt, text, textlen); - - *body = NULL; - - if (result == 0) { - return (-1); - } - - *code = result; - - /* Code that matches 2xx is a good result code. - * Code that matches xx1 means there's a response body coming. - */ - if ((result / 100) == 2 && (result % 10) == 1) { - *body = irs_irp_read_body(pvt, bodylen); - if (*body == NULL) { - return (-1); - } - } - - return (0); -} - -/*% - * int irs_irp_send_command(struct irp_p *pvt, const char *fmt, ...); - * - * Sends command to remote connected via the PVT - * structure. FMT and args after it are fprintf-like - * arguments for formatting. - * - * Returns: - * - * 0 on success, -1 on failure. - */ - -int -irs_irp_send_command(struct irp_p *pvt, const char *fmt, ...) { - va_list ap; - char buffer[1024]; - int pos = 0; - int i, todo; - - - if (pvt->fdCxn < 0) { - return (-1); - } - - va_start(ap, fmt); - (void) vsprintf(buffer, fmt, ap); - todo = strlen(buffer); - va_end(ap); - if (todo > (int)sizeof(buffer) - 3) { - syslog(LOG_CRIT, "memory overrun in irs_irp_send_command()"); - exit(1); - } - strcat(buffer, "\r\n"); - todo = strlen(buffer); - - while (todo > 0) { - i = write(pvt->fdCxn, buffer + pos, todo); -#if 0 - /* XXX brister */ - fprintf(stderr, "Wrote: \""); - fwrite(buffer + pos, sizeof (char), todo, stderr); - fprintf(stderr, "\"\n"); -#endif - if (i < 0) { - close(pvt->fdCxn); - pvt->fdCxn = -1; - return (-1); - } - todo -= i; - } - - return (0); -} - - -/* Methods */ - -/*% - * void irp_close(struct irs_acc *this) - * - */ - -static void -irp_close(struct irs_acc *this) { - struct irp_p *irp = (struct irp_p *)this->private; - - if (irp != NULL) { - irs_irp_disconnect(irp); - memput(irp, sizeof *irp); - } - - memput(this, sizeof *this); -} - - - - -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/irp_gr.c /usr/src/lib/libc/net/irp_gr.c --- /home/reed/src/isc/libbind/libbind/irs/irp_gr.c 2005-04-26 23:56:27.000000000 -0500 +++ /usr/src/lib/libc/net/irp_gr.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,357 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Portions Copyright(c) 1996, 1998 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#if defined(LIBC_SCCS) && !defined(lint) -static const char rcsid[] = "$Id: irp_gr.c,v 1.4 2005/04/27 04:56:27 sra Exp $"; -#endif /* LIBC_SCCS and not lint */ - -/* extern */ - -#include "port_before.h" - -#ifndef WANT_IRS_PW -static int __bind_irs_gr_unneeded; -#else - -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -#include "irs_p.h" -#include "lcl_p.h" -#include "irp_p.h" - -#include "port_after.h" - - -/* Types. */ - -/*! \file - * \brief - * Module for the getnetgrent(3) family to use when connected to a - * remote irp daemon. - * \brief - * See irpd.c for justification of caching done here. - * - */ - -struct pvt { - struct irp_p *girpdata; /*%< global IRP data */ - int warned; - struct group group; -}; - -/* Forward. */ - -static void gr_close(struct irs_gr *); -static struct group * gr_next(struct irs_gr *); -static struct group * gr_byname(struct irs_gr *, const char *); -static struct group * gr_bygid(struct irs_gr *, gid_t); -static void gr_rewind(struct irs_gr *); -static void gr_minimize(struct irs_gr *); - -/* Private */ -static void free_group(struct group *gr); - - -/* Public. */ - -/*% - * Initialize the group sub-module. - * - */ - -struct irs_gr * -irs_irp_gr(struct irs_acc *this) { - struct irs_gr *gr; - struct pvt *pvt; - - if (!(gr = memget(sizeof *gr))) { - errno = ENOMEM; - return (NULL); - } - memset(gr, 0x0, sizeof *gr); - - if (!(pvt = memget(sizeof *pvt))) { - memput(gr, sizeof *gr); - errno = ENOMEM; - return (NULL); - } - memset(pvt, 0x0, sizeof *pvt); - pvt->girpdata = this->private; - - gr->private = pvt; - gr->close = gr_close; - gr->next = gr_next; - gr->byname = gr_byname; - gr->bygid = gr_bygid; - gr->rewind = gr_rewind; - gr->list = make_group_list; - gr->minimize = gr_minimize; - return (gr); -} - -/* Methods. */ - -/*% - * Close the sub-module. - * - */ - -static void -gr_close(struct irs_gr *this) { - struct pvt *pvt = (struct pvt *)this->private; - - gr_minimize(this); - - memput(pvt, sizeof *pvt); - memput(this, sizeof *this); -} - -/*% - * Gets the next group out of the cached data and returns it. - * - */ - -static struct group * -gr_next(struct irs_gr *this) { - struct pvt *pvt = (struct pvt *)this->private; - struct group *gr = &pvt->group; - char *body; - size_t bodylen; - int code; - char text[256]; - - if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) { - return (NULL); - } - - if (irs_irp_send_command(pvt->girpdata, "getgrent") != 0) { - return (NULL); - } - - if (irs_irp_get_full_response(pvt->girpdata, &code, - text, sizeof text, - &body, &bodylen) != 0) { - if (irp_log_errors) { - syslog(LOG_WARNING, "getgrent failed: %s", text); - } - return (NULL); - } - - if (code == IRPD_GETGROUP_OK) { - free_group(gr); - if (irp_unmarshall_gr(gr, body) != 0) { - gr = NULL; - } - } else { - gr = NULL; - } - - if (body != NULL) { - memput(body, bodylen); - } - - return (gr); -} - -/*% - * Gets a group by name from irpd and returns it. - * - */ - -static struct group * -gr_byname(struct irs_gr *this, const char *name) { - struct pvt *pvt = (struct pvt *)this->private; - struct group *gr = &pvt->group; - char *body; - size_t bodylen; - int code; - char text[256]; - - - if (gr->gr_name != NULL && strcmp(name, gr->gr_name) == 0) { - return (gr); - } - - if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) { - return (NULL); - } - - if (irs_irp_send_command(pvt->girpdata, "getgrnam %s", name) != 0) - return (NULL); - - if (irs_irp_get_full_response(pvt->girpdata, &code, - text, sizeof text, - &body, &bodylen) != 0) { - return (NULL); - } - - if (code == IRPD_GETGROUP_OK) { - free_group(gr); - if (irp_unmarshall_gr(gr, body) != 0) { - gr = NULL; - } - } else { - gr = NULL; - } - - if (body != NULL) { - memput(body, bodylen); - } - - return (gr); -} - -/*% - * Gets a group by gid from irpd and returns it. - * - */ - -static struct group * -gr_bygid(struct irs_gr *this, gid_t gid) { - struct pvt *pvt = (struct pvt *)this->private; - struct group *gr = &pvt->group; - char *body; - size_t bodylen; - int code; - char text[256]; - - if (gr->gr_name != NULL && (gid_t)gr->gr_gid == gid) { - return (gr); - } - - if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) { - return (NULL); - } - - if (irs_irp_send_command(pvt->girpdata, "getgrgid %d", gid) != 0) - return (NULL); - - if (irs_irp_get_full_response(pvt->girpdata, &code, - text, sizeof text, - &body, &bodylen) != 0) { - return (NULL); - } - - if (code == IRPD_GETGROUP_OK) { - free_group(gr); - if (irp_unmarshall_gr(gr, body) != 0) { - gr = NULL; - } - } else { - gr = NULL; - } - - if (body != NULL) { - memput(body, bodylen); - } - - return (gr); -} - -/*% - * void gr_rewind(struct irs_gr *this) - * - */ - -static void -gr_rewind(struct irs_gr *this) { - struct pvt *pvt = (struct pvt *)this->private; - char text[256]; - int code; - - if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) { - return; - } - - if (irs_irp_send_command(pvt->girpdata, "setgrent") != 0) { - return; - } - - code = irs_irp_read_response(pvt->girpdata, text, sizeof text); - if (code != IRPD_GETGROUP_SETOK) { - if (irp_log_errors) { - syslog(LOG_WARNING, "setgrent failed: %s", text); - } - } - - return; -} - -/*% - * Frees up cached data and disconnects(if necessary) from the remote. - * - */ - -static void -gr_minimize(struct irs_gr *this) { - struct pvt *pvt = (struct pvt *)this->private; - - free_group(&pvt->group); - irs_irp_disconnect(pvt->girpdata); -} - -/* Private. */ - -/*% - * static void free_group(struct group *gr); - * - * Deallocate all the memory irp_unmarshall_gr allocated. - * - */ - -static void -free_group(struct group *gr) { - char **p; - - if (gr == NULL) - return; - - if (gr->gr_name != NULL) - free(gr->gr_name); - - if (gr->gr_passwd != NULL) - free(gr->gr_passwd); - - for (p = gr->gr_mem ; p != NULL && *p != NULL ; p++) - free(*p); - - if (gr->gr_mem) - free(gr->gr_mem); - - if (p != NULL) - free(p); -} - - -#endif /* WANT_IRS_GR */ -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/irp_ho.c /usr/src/lib/libc/net/irp_ho.c --- /home/reed/src/isc/libbind/libbind/irs/irp_ho.c 2005-04-26 23:56:28.000000000 -0500 +++ /usr/src/lib/libc/net/irp_ho.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,405 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Portions Copyright (c) 1996,1998 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#if defined(LIBC_SCCS) && !defined(lint) -static const char rcsid[] = "$Id: irp_ho.c,v 1.3 2005/04/27 04:56:28 sra Exp $"; -#endif /* LIBC_SCCS and not lint */ - -/* Imports. */ - -#include "port_before.h" - -#include -#include -#include -#include - -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -#include "irs_p.h" -#include "dns_p.h" -#include "irp_p.h" - -#include "port_after.h" - -/* Definitions. */ - -#define MAXALIASES 35 -#define MAXADDRS 35 -#define Max(a,b) ((a) > (b) ? (a) : (b)) - - -struct pvt { - struct irp_p *girpdata; - int warned; - struct hostent host; -}; - -/* Forward. */ - -static void ho_close(struct irs_ho *this); -static struct hostent * ho_byname(struct irs_ho *this, const char *name); -static struct hostent * ho_byname2(struct irs_ho *this, const char *name, - int af); -static struct hostent * ho_byaddr(struct irs_ho *this, const void *addr, - int len, int af); -static struct hostent * ho_next(struct irs_ho *this); -static void ho_rewind(struct irs_ho *this); -static void ho_minimize(struct irs_ho *this); - -static void free_host(struct hostent *ho); -static struct addrinfo * ho_addrinfo(struct irs_ho *this, const char *name, - const struct addrinfo *pai); - -/* Public. */ - -/*% - * struct irs_ho * irs_irp_ho(struct irs_acc *this) - * - * Notes: - * - * Initializes the irp_ho module. - * - */ - -struct irs_ho * -irs_irp_ho(struct irs_acc *this) { - struct irs_ho *ho; - struct pvt *pvt; - - if (!(ho = memget(sizeof *ho))) { - errno = ENOMEM; - return (NULL); - } - memset(ho, 0x0, sizeof *ho); - - if (!(pvt = memget(sizeof *pvt))) { - memput(ho, sizeof *ho); - errno = ENOMEM; - return (NULL); - } - memset(pvt, 0, sizeof *pvt); - pvt->girpdata = this->private; - - ho->private = pvt; - ho->close = ho_close; - ho->byname = ho_byname; - ho->byname2 = ho_byname2; - ho->byaddr = ho_byaddr; - ho->next = ho_next; - ho->rewind = ho_rewind; - ho->minimize = ho_minimize; - ho->addrinfo = ho_addrinfo; - - return (ho); -} - -/* Methods. */ - -/*% - * Closes down the module. - * - */ - -static void -ho_close(struct irs_ho *this) { - struct pvt *pvt = (struct pvt *)this->private; - - ho_minimize(this); - - free_host(&pvt->host); - - memput(pvt, sizeof *pvt); - memput(this, sizeof *this); -} - - - -/* - * struct hostent * ho_byname(struct irs_ho *this, const char *name) - * - */ - -static struct hostent * -ho_byname(struct irs_ho *this, const char *name) { - return (ho_byname2(this, name, AF_INET)); -} - - - - - -/* - * struct hostent * ho_byname2(struct irs_ho *this, const char *name, int af) - * - */ - -static struct hostent * -ho_byname2(struct irs_ho *this, const char *name, int af) { - struct pvt *pvt = (struct pvt *)this->private; - struct hostent *ho = &pvt->host; - char *body = NULL; - size_t bodylen; - int code; - char text[256]; - - if (ho->h_name != NULL && - strcmp(name, ho->h_name) == 0 && - af == ho->h_addrtype) { - return (ho); - } - - if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) { - return (NULL); - } - - if (irs_irp_send_command(pvt->girpdata, "gethostbyname2 %s %s", - name, ADDR_T_STR(af)) != 0) - return (NULL); - - if (irs_irp_get_full_response(pvt->girpdata, &code, - text, sizeof text, - &body, &bodylen) != 0) { - return (NULL); - } - - if (code == IRPD_GETHOST_OK) { - free_host(ho); - if (irp_unmarshall_ho(ho, body) != 0) { - ho = NULL; - } - } else { - ho = NULL; - } - - if (body != NULL) { - memput(body, bodylen); - } - - return (ho); -} - - - -/* - * struct hostent * ho_byaddr(struct irs_ho *this, const void *addr, - * int len, int af) - * - */ - -static struct hostent * -ho_byaddr(struct irs_ho *this, const void *addr, int len, int af) { - struct pvt *pvt = (struct pvt *)this->private; - struct hostent *ho = &pvt->host; - char *body = NULL; - size_t bodylen; - int code; - char **p; - char paddr[MAXPADDRSIZE]; - char text[256]; - - if (ho->h_name != NULL && - af == ho->h_addrtype && - len == ho->h_length) { - for (p = ho->h_addr_list ; *p != NULL ; p++) { - if (memcmp(*p, addr, len) == 0) - return (ho); - } - } - - if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) { - return (NULL); - } - - if (inet_ntop(af, addr, paddr, sizeof paddr) == NULL) { - return (NULL); - } - - if (irs_irp_send_command(pvt->girpdata, "gethostbyaddr %s %s", - paddr, ADDR_T_STR(af)) != 0) { - return (NULL); - } - - if (irs_irp_get_full_response(pvt->girpdata, &code, - text, sizeof text, - &body, &bodylen) != 0) { - return (NULL); - } - - if (code == IRPD_GETHOST_OK) { - free_host(ho); - if (irp_unmarshall_ho(ho, body) != 0) { - ho = NULL; - } - } else { - ho = NULL; - } - - if (body != NULL) { - memput(body, bodylen); - } - - return (ho); -} - -/*% - * The implementation for gethostent(3). The first time it's - * called all the data is pulled from the remote(i.e. what - * the maximum number of gethostent(3) calls would return) - * and that data is cached. - * - */ - -static struct hostent * -ho_next(struct irs_ho *this) { - struct pvt *pvt = (struct pvt *)this->private; - struct hostent *ho = &pvt->host; - char *body; - size_t bodylen; - int code; - char text[256]; - - if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) { - return (NULL); - } - - if (irs_irp_send_command(pvt->girpdata, "gethostent") != 0) { - return (NULL); - } - - if (irs_irp_get_full_response(pvt->girpdata, &code, - text, sizeof text, - &body, &bodylen) != 0) { - return (NULL); - } - - if (code == IRPD_GETHOST_OK) { - free_host(ho); - if (irp_unmarshall_ho(ho, body) != 0) { - ho = NULL; - } - } else { - ho = NULL; - } - - if (body != NULL) { - memput(body, bodylen); - } - - return (ho); -} - -/*% - * void ho_rewind(struct irs_ho *this) - * - */ - -static void -ho_rewind(struct irs_ho *this) { - struct pvt *pvt = (struct pvt *)this->private; - char text[256]; - int code; - - if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) { - return; - } - - if (irs_irp_send_command(pvt->girpdata, "sethostent") != 0) { - return; - } - - code = irs_irp_read_response(pvt->girpdata, text, sizeof text); - if (code != IRPD_GETHOST_SETOK) { - if (irp_log_errors) { - syslog(LOG_WARNING, "sethostent failed: %s", text); - } - } - - return; -} - -/*% - * void ho_minimize(struct irs_ho *this) - * - */ - -static void -ho_minimize(struct irs_ho *this) { - struct pvt *pvt = (struct pvt *)this->private; - - free_host(&pvt->host); - - irs_irp_disconnect(pvt->girpdata); -} - -/*% - * void free_host(struct hostent *ho) - * - */ - -static void -free_host(struct hostent *ho) { - char **p; - - if (ho == NULL) { - return; - } - - if (ho->h_name != NULL) - free(ho->h_name); - - if (ho->h_aliases != NULL) { - for (p = ho->h_aliases ; *p != NULL ; p++) - free(*p); - free(ho->h_aliases); - } - - if (ho->h_addr_list != NULL) { - for (p = ho->h_addr_list ; *p != NULL ; p++) - free(*p); - free(ho->h_addr_list); - } -} - -/* dummy */ -static struct addrinfo * -ho_addrinfo(struct irs_ho *this, const char *name, const struct addrinfo *pai) -{ - UNUSED(this); - UNUSED(name); - UNUSED(pai); - return(NULL); -} - -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/irp_ng.c /usr/src/lib/libc/net/irp_ng.c --- /home/reed/src/isc/libbind/libbind/irs/irp_ng.c 2006-12-06 22:46:27.000000000 -0600 +++ /usr/src/lib/libc/net/irp_ng.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,253 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1996, 1998 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#if !defined(LINT) && !defined(CODECENTER) -static const char rcsid[] = "$Id: irp_ng.c,v 1.4 2006/12/07 04:46:27 marka Exp $"; -#endif - -/* Imports */ - -#include "port_before.h" - -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -#include "irs_p.h" -#include "irp_p.h" - -#include "port_after.h" - -/* Definitions */ - -struct pvt { - struct irp_p *girpdata; - int warned; -}; - - -/* Forward */ - -static void ng_rewind(struct irs_ng *, const char*); -static void ng_close(struct irs_ng *); -static int ng_next(struct irs_ng *, const char **, const char **, - const char **); -static int ng_test(struct irs_ng *, const char *, - const char *, const char *, - const char *); -static void ng_minimize(struct irs_ng *); - - -/* Public */ - -/*% - * Intialize the irp netgroup module. - * - */ - -struct irs_ng * -irs_irp_ng(struct irs_acc *this) { - struct irs_ng *ng; - struct pvt *pvt; - - if (!(ng = memget(sizeof *ng))) { - errno = ENOMEM; - return (NULL); - } - memset(ng, 0x5e, sizeof *ng); - - if (!(pvt = memget(sizeof *pvt))) { - memput(ng, sizeof *ng); - errno = ENOMEM; - return (NULL); - } - memset(pvt, 0, sizeof *pvt); - pvt->girpdata = this->private; - - ng->private = pvt; - ng->close = ng_close; - ng->next = ng_next; - ng->test = ng_test; - ng->rewind = ng_rewind; - ng->minimize = ng_minimize; - return (ng); -} - -/* Methods */ - - - -/* - * void ng_close(struct irs_ng *this) - * - */ - -static void -ng_close(struct irs_ng *this) { - struct pvt *pvt = (struct pvt *)this->private; - - ng_minimize(this); - - memput(pvt, sizeof *pvt); - memput(this, sizeof *this); -} - - - - -/* - * void ng_rewind(struct irs_ng *this, const char *group) - * - * - */ - -static void -ng_rewind(struct irs_ng *this, const char *group) { - struct pvt *pvt = (struct pvt *)this->private; - char text[256]; - int code; - - if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) { - return; - } - - if (irs_irp_send_command(pvt->girpdata, - "setnetgrent %s", group) != 0) { - return; - } - - code = irs_irp_read_response(pvt->girpdata, text, sizeof text); - if (code != IRPD_GETNETGR_SETOK) { - if (irp_log_errors) { - syslog(LOG_WARNING, "setnetgrent(%s) failed: %s", - group, text); - } - } - - return; -} - -/* - * Get the next netgroup item from the cache. - * - */ - -static int -ng_next(struct irs_ng *this, const char **host, const char **user, - const char **domain) -{ - struct pvt *pvt = (struct pvt *)this->private; - int code; - char *body = NULL; - size_t bodylen; - int rval = 0; - char text[256]; - - if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) { - return (0); - } - - if (irs_irp_send_command(pvt->girpdata, "getnetgrent") != 0) - return (0); - - if (irs_irp_get_full_response(pvt->girpdata, &code, - text, sizeof text, - &body, &bodylen) != 0) { - return (0); - } - - if (code == IRPD_GETNETGR_OK) { - if (irp_unmarshall_ng(host, user, domain, body) == 0) { - rval = 1; - } - } - - if (body != NULL) { - memput(body, bodylen); - } - - return (rval); -} - -/* - * Search for a match in a netgroup. - * - */ - -static int -ng_test(struct irs_ng *this, const char *name, - const char *host, const char *user, const char *domain) -{ - struct pvt *pvt = (struct pvt *)this->private; - char *body = NULL; - size_t bodylen = 0; - int code; - char text[256]; - int rval = 0; - - UNUSED(name); - - if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) { - return (0); - } - - if (irp_marshall_ng(host, user, domain, &body, &bodylen) != 0) { - return (0); - } - - if (irs_irp_send_command(pvt->girpdata, "innetgr %s", body) == 0) { - code = irs_irp_read_response(pvt->girpdata, text, sizeof text); - if (code == IRPD_GETNETGR_MATCHES) { - rval = 1; - } - } - - memput(body, bodylen); - - return (rval); -} - - - - -/* - * void ng_minimize(struct irs_ng *this) - * - */ - -static void -ng_minimize(struct irs_ng *this) { - struct pvt *pvt = (struct pvt *)this->private; - - irs_irp_disconnect(pvt->girpdata); -} - - - - -/* Private */ - - -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/irp_nw.c /usr/src/lib/libc/net/irp_nw.c --- /home/reed/src/isc/libbind/libbind/irs/irp_nw.c 2006-03-09 17:57:56.000000000 -0600 +++ /usr/src/lib/libc/net/irp_nw.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,348 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Portions Copyright (c) 1996,1998 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#if defined(LIBC_SCCS) && !defined(lint) -static const char rcsid[] = "$Id: irp_nw.c,v 1.4 2006/03/09 23:57:56 marka Exp $"; -#endif /* LIBC_SCCS and not lint */ - -#if 0 - -#endif - -/* Imports */ - -#include "port_before.h" - -#include -#include -#include - -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include - -#include -#include - -#include "irs_p.h" -#include "lcl_p.h" -#include "irp_p.h" - -#include "port_after.h" - -#define MAXALIASES 35 -#define MAXADDRSIZE 4 - -struct pvt { - struct irp_p *girpdata; - int warned; - struct nwent net; -}; - -/* Forward */ - -static void nw_close(struct irs_nw *); -static struct nwent * nw_byname(struct irs_nw *, const char *, int); -static struct nwent * nw_byaddr(struct irs_nw *, void *, int, int); -static struct nwent * nw_next(struct irs_nw *); -static void nw_rewind(struct irs_nw *); -static void nw_minimize(struct irs_nw *); - -static void free_nw(struct nwent *nw); - - -/* Public */ - -/*% - * struct irs_nw * irs_irp_nw(struct irs_acc *this) - * - */ - -struct irs_nw * -irs_irp_nw(struct irs_acc *this) { - struct irs_nw *nw; - struct pvt *pvt; - - if (!(pvt = memget(sizeof *pvt))) { - errno = ENOMEM; - return (NULL); - } - memset(pvt, 0, sizeof *pvt); - - if (!(nw = memget(sizeof *nw))) { - memput(pvt, sizeof *pvt); - errno = ENOMEM; - return (NULL); - } - memset(nw, 0x0, sizeof *nw); - pvt->girpdata = this->private; - - nw->private = pvt; - nw->close = nw_close; - nw->byname = nw_byname; - nw->byaddr = nw_byaddr; - nw->next = nw_next; - nw->rewind = nw_rewind; - nw->minimize = nw_minimize; - return (nw); -} - -/* Methods */ - -/*% - * void nw_close(struct irs_nw *this) - * - */ - -static void -nw_close(struct irs_nw *this) { - struct pvt *pvt = (struct pvt *)this->private; - - nw_minimize(this); - - free_nw(&pvt->net); - - memput(pvt, sizeof *pvt); - memput(this, sizeof *this); -} - -/*% - * struct nwent * nw_byaddr(struct irs_nw *this, void *net, - * int length, int type) - * - */ - -static struct nwent * -nw_byaddr(struct irs_nw *this, void *net, int length, int type) { - struct pvt *pvt = (struct pvt *)this->private; - struct nwent *nw = &pvt->net; - char *body = NULL; - size_t bodylen; - int code; - char paddr[24]; /*%< bigenough for ip4 w/ cidr spec. */ - char text[256]; - - if (inet_net_ntop(type, net, length, paddr, sizeof paddr) == NULL) { - return (NULL); - } - - if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) { - return (NULL); - } - - if (irs_irp_send_command(pvt->girpdata, "getnetbyaddr %s %s", - paddr, ADDR_T_STR(type)) != 0) - return (NULL); - - if (irs_irp_get_full_response(pvt->girpdata, &code, - text, sizeof text, - &body, &bodylen) != 0) { - return (NULL); - } - - if (code == IRPD_GETNET_OK) { - free_nw(nw); - if (irp_unmarshall_nw(nw, body) != 0) { - nw = NULL; - } - } else { - nw = NULL; - } - - if (body != NULL) { - memput(body, bodylen); - } - - return (nw); -} - -/*% - * struct nwent * nw_byname(struct irs_nw *this, const char *name, int type) - * - */ - -static struct nwent * -nw_byname(struct irs_nw *this, const char *name, int type) { - struct pvt *pvt = (struct pvt *)this->private; - struct nwent *nw = &pvt->net; - char *body = NULL; - size_t bodylen; - int code; - char text[256]; - - if (nw->n_name != NULL && - strcmp(name, nw->n_name) == 0 && - nw->n_addrtype == type) { - return (nw); - } - - if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) { - return (NULL); - } - - if (irs_irp_send_command(pvt->girpdata, "getnetbyname %s", name) != 0) - return (NULL); - - if (irs_irp_get_full_response(pvt->girpdata, &code, - text, sizeof text, - &body, &bodylen) != 0) { - return (NULL); - } - - if (code == IRPD_GETNET_OK) { - free_nw(nw); - if (irp_unmarshall_nw(nw, body) != 0) { - nw = NULL; - } - } else { - nw = NULL; - } - - if (body != NULL) { - memput(body, bodylen); - } - - return (nw); -} - -/*% - * void nw_rewind(struct irs_nw *this) - * - */ - -static void -nw_rewind(struct irs_nw *this) { - struct pvt *pvt = (struct pvt *)this->private; - char text[256]; - int code; - - if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) { - return; - } - - if (irs_irp_send_command(pvt->girpdata, "setnetent") != 0) { - return; - } - - code = irs_irp_read_response(pvt->girpdata, text, sizeof text); - if (code != IRPD_GETNET_SETOK) { - if (irp_log_errors) { - syslog(LOG_WARNING, "setnetent failed: %s", text); - } - } - - return; -} - -/*% - * Prepares the cache if necessary and returns the first, or - * next item from it. - */ - -static struct nwent * -nw_next(struct irs_nw *this) { - struct pvt *pvt = (struct pvt *)this->private; - struct nwent *nw = &pvt->net; - char *body; - size_t bodylen; - int code; - char text[256]; - - if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) { - return (NULL); - } - - if (irs_irp_send_command(pvt->girpdata, "getnetent") != 0) { - return (NULL); - } - - if (irs_irp_get_full_response(pvt->girpdata, &code, - text, sizeof text, - &body, &bodylen) != 0) { - return (NULL); - } - - if (code == IRPD_GETNET_OK) { - free_nw(nw); - if (irp_unmarshall_nw(nw, body) != 0) { - nw = NULL; - } - } else { - nw = NULL; - } - - if (body != NULL) - memput(body, bodylen); - return (nw); -} - -/*% - * void nw_minimize(struct irs_nw *this) - * - */ - -static void -nw_minimize(struct irs_nw *this) { - struct pvt *pvt = (struct pvt *)this->private; - - irs_irp_disconnect(pvt->girpdata); -} - - - - -/* private. */ - -/*% - * deallocate all the memory irp_unmarshall_pw allocated. - * - */ - -static void -free_nw(struct nwent *nw) { - char **p; - - if (nw == NULL) - return; - - if (nw->n_name != NULL) - free(nw->n_name); - - if (nw->n_aliases != NULL) { - for (p = nw->n_aliases ; *p != NULL ; p++) { - free(*p); - } - free(nw->n_aliases); - } - - if (nw->n_addr != NULL) - free(nw->n_addr); -} - -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/irp_p.h /usr/src/lib/libc/net/irp_p.h --- /home/reed/src/isc/libbind/libbind/irs/irp_p.h 2005-04-26 23:56:28.000000000 -0500 +++ /usr/src/lib/libc/net/irp_p.h 1969-12-31 18:00:00.000000000 -0600 @@ -1,60 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1996 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -/* - * $Id: irp_p.h,v 1.5 2005/04/27 04:56:28 sra Exp $ - */ - -#ifndef _IRP_P_H_INCLUDED -#define _IRP_P_H_INCLUDED - -#include - -struct irp_p { - char inbuffer[1024]; - int inlast; /*%< index of one past the last char in buffer */ - int incurr; /*%< index of the next char to be read from buffer */ - int fdCxn; -}; - -/* - * Externs. - */ - -extern struct irs_acc * irs_irp_acc __P((const char *)); -extern struct irs_gr * irs_irp_gr __P((struct irs_acc *)); -extern struct irs_pw * irs_irp_pw __P((struct irs_acc *)); -extern struct irs_sv * irs_irp_sv __P((struct irs_acc *)); -extern struct irs_pr * irs_irp_pr __P((struct irs_acc *)); -extern struct irs_ho * irs_irp_ho __P((struct irs_acc *)); -extern struct irs_nw * irs_irp_nw __P((struct irs_acc *)); -extern struct irs_ng * irs_irp_ng __P((struct irs_acc *)); - -int irs_irp_connect(struct irp_p *pvt); -int irs_irp_is_connected(struct irp_p *pvt); -void irs_irp_disconnect(struct irp_p *pvt); -int irs_irp_read_response(struct irp_p *pvt, char *text, size_t textlen); -char *irs_irp_read_body(struct irp_p *pvt, size_t *size); -int irs_irp_get_full_response(struct irp_p *pvt, int *code, - char *text, size_t textlen, - char **body, size_t *bodylen); - -extern int irp_log_errors; - -#endif - -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/irp_pr.c /usr/src/lib/libc/net/irp_pr.c --- /home/reed/src/isc/libbind/libbind/irs/irp_pr.c 2005-04-26 23:56:29.000000000 -0500 +++ /usr/src/lib/libc/net/irp_pr.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,327 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Portions Copyright (c) 1996 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#if defined(LIBC_SCCS) && !defined(lint) -static const char rcsid[] = "$Id: irp_pr.c,v 1.3 2005/04/27 04:56:29 sra Exp $"; -#endif /* LIBC_SCCS and not lint */ - -/* extern */ - -#include "port_before.h" - -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -#include "irs_p.h" -#include "lcl_p.h" -#include "irp_p.h" - -#include "port_after.h" - - -#define MAXALIASES 35 - -/* Types */ - -struct pvt { - struct irp_p *girpdata; - int warned; - struct protoent proto; -}; - -/* Forward */ - -static void pr_close(struct irs_pr *); -static struct protoent * pr_next(struct irs_pr *); -static struct protoent * pr_byname(struct irs_pr *, const char *); -static struct protoent * pr_bynumber(struct irs_pr *, int); -static void pr_rewind(struct irs_pr *); -static void pr_minimize(struct irs_pr *); - -static void free_proto(struct protoent *pr); - -/* Public */ - -/*% - * struct irs_pr * irs_irp_pr(struct irs_acc *this) - * - */ - -struct irs_pr * -irs_irp_pr(struct irs_acc *this) { - struct irs_pr *pr; - struct pvt *pvt; - - if (!(pr = memget(sizeof *pr))) { - errno = ENOMEM; - return (NULL); - } - memset(pr, 0x0, sizeof *pr); - - if (!(pvt = memget(sizeof *pvt))) { - memput(pr, sizeof *pr); - errno = ENOMEM; - return (NULL); - } - memset(pvt, 0, sizeof *pvt); - pvt->girpdata = this->private; - - pr->private = pvt; - pr->close = pr_close; - pr->byname = pr_byname; - pr->bynumber = pr_bynumber; - pr->next = pr_next; - pr->rewind = pr_rewind; - pr->minimize = pr_minimize; - return (pr); -} - -/* Methods */ - -/*% - * void pr_close(struct irs_pr *this) - * - */ - -static void -pr_close(struct irs_pr *this) { - struct pvt *pvt = (struct pvt *)this->private; - - pr_minimize(this); - - free_proto(&pvt->proto); - - memput(pvt, sizeof *pvt); - memput(this, sizeof *this); -} - -/*% - * struct protoent * pr_byname(struct irs_pr *this, const char *name) - * - */ - -static struct protoent * -pr_byname(struct irs_pr *this, const char *name) { - struct pvt *pvt = (struct pvt *)this->private; - struct protoent *pr = &pvt->proto; - char *body = NULL; - size_t bodylen; - int code; - int i; - char text[256]; - - if (pr->p_name != NULL && strcmp(name, pr->p_name) == 0) { - return (pr); - } - - if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) { - return (NULL); - } - - i = irs_irp_send_command(pvt->girpdata, "getprotobyname %s", name); - if (i != 0) - return (NULL); - - if (irs_irp_get_full_response(pvt->girpdata, &code, - text, sizeof text, - &body, &bodylen) != 0) { - return (NULL); - } - - if (code == IRPD_GETPROTO_OK) { - free_proto(pr); - if (irp_unmarshall_pr(pr, body) != 0) { - pr = NULL; - } - } else { - pr = NULL; - } - - if (body != NULL) { - memput(body, bodylen); - } - - return (pr); -} - -/*% - * struct protoent * pr_bynumber(struct irs_pr *this, int proto) - * - */ - -static struct protoent * -pr_bynumber(struct irs_pr *this, int proto) { - struct pvt *pvt = (struct pvt *)this->private; - struct protoent *pr = &pvt->proto; - char *body = NULL; - size_t bodylen; - int code; - int i; - char text[256]; - - if (pr->p_name != NULL && proto == pr->p_proto) { - return (pr); - } - - if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) { - return (NULL); - } - - i = irs_irp_send_command(pvt->girpdata, "getprotobynumber %d", proto); - if (i != 0) - return (NULL); - - if (irs_irp_get_full_response(pvt->girpdata, &code, - text, sizeof text, - &body, &bodylen) != 0) { - return (NULL); - } - - if (code == IRPD_GETPROTO_OK) { - free_proto(pr); - if (irp_unmarshall_pr(pr, body) != 0) { - pr = NULL; - } - } else { - pr = NULL; - } - - if (body != NULL) { - memput(body, bodylen); - } - - return (pr); -} - -/*% - * void pr_rewind(struct irs_pr *this) - * - */ - -static void -pr_rewind(struct irs_pr *this) { - struct pvt *pvt = (struct pvt *)this->private; - char text[256]; - int code; - - if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) { - return; - } - - if (irs_irp_send_command(pvt->girpdata, "setprotoent") != 0) { - return; - } - - code = irs_irp_read_response(pvt->girpdata, text, sizeof text); - if (code != IRPD_GETPROTO_SETOK) { - if (irp_log_errors) { - syslog(LOG_WARNING, "setprotoent failed: %s", text); - } - } - - return; -} - -/*% - * Prepares the cache if necessary and returns the next item in it. - * - */ - -static struct protoent * -pr_next(struct irs_pr *this) { - struct pvt *pvt = (struct pvt *)this->private; - struct protoent *pr = &pvt->proto; - char *body; - size_t bodylen; - int code; - char text[256]; - - if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) { - return (NULL); - } - - if (irs_irp_send_command(pvt->girpdata, "getprotoent") != 0) { - return (NULL); - } - - if (irs_irp_get_full_response(pvt->girpdata, &code, - text, sizeof text, - &body, &bodylen) != 0) { - return (NULL); - } - - if (code == IRPD_GETPROTO_OK) { - free_proto(pr); - if (irp_unmarshall_pr(pr, body) != 0) { - pr = NULL; - } - } else { - pr = NULL; - } - - if (body != NULL) { - memput(body, bodylen); - } - - return (pr); -} - -/*% - * void pr_minimize(struct irs_pr *this) - * - */ - -static void -pr_minimize(struct irs_pr *this) { - struct pvt *pvt = (struct pvt *)this->private; - - irs_irp_disconnect(pvt->girpdata); -} - -/*% - * Deallocate all the memory irp_unmarshall_pr allocated. - * - */ - -static void -free_proto(struct protoent *pr) { - char **p; - - if (pr == NULL) - return; - - if (pr->p_name != NULL) - free(pr->p_name); - - for (p = pr->p_aliases ; p != NULL && *p != NULL ; p++) - free(*p); -} - -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/irp_pw.c /usr/src/lib/libc/net/irp_pw.c --- /home/reed/src/isc/libbind/libbind/irs/irp_pw.c 2005-04-26 23:56:29.000000000 -0500 +++ /usr/src/lib/libc/net/irp_pw.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,340 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Portions Copyright (c) 1996 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#if defined(LIBC_SCCS) && !defined(lint) -static const char rcsid[] = "$Id: irp_pw.c,v 1.4 2005/04/27 04:56:29 sra Exp $"; -#endif /* LIBC_SCCS and not lint */ - -/* Extern */ - -#include "port_before.h" - -#ifndef WANT_IRS_PW -static int __bind_irs_pw_unneeded; -#else - -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -#include "port_after.h" - -#include "irs_p.h" -#include "irp_p.h" - - -/* Types */ - -struct pvt { - struct irp_p *girpdata; /*%< global IRP data */ - int warned; - struct passwd passwd; /*%< password structure */ -}; - -/* Forward */ - -static void pw_close(struct irs_pw *); -static struct passwd * pw_next(struct irs_pw *); -static struct passwd * pw_byname(struct irs_pw *, const char *); -static struct passwd * pw_byuid(struct irs_pw *, uid_t); -static void pw_rewind(struct irs_pw *); -static void pw_minimize(struct irs_pw *); - -static void free_passwd(struct passwd *pw); - -/* Public */ -struct irs_pw * -irs_irp_pw(struct irs_acc *this) { - struct irs_pw *pw; - struct pvt *pvt; - - if (!(pw = memget(sizeof *pw))) { - errno = ENOMEM; - return (NULL); - } - memset(pw, 0, sizeof *pw); - - if (!(pvt = memget(sizeof *pvt))) { - memput(pw, sizeof *pw); - errno = ENOMEM; - return (NULL); - } - memset(pvt, 0, sizeof *pvt); - pvt->girpdata = this->private; - - pw->private = pvt; - pw->close = pw_close; - pw->next = pw_next; - pw->byname = pw_byname; - pw->byuid = pw_byuid; - pw->rewind = pw_rewind; - pw->minimize = pw_minimize; - - return (pw); -} - -/* Methods */ - -/*% - * void pw_close(struct irs_pw *this) - * - */ - -static void -pw_close(struct irs_pw *this) { - struct pvt *pvt = (struct pvt *)this->private; - - pw_minimize(this); - - free_passwd(&pvt->passwd); - - memput(pvt, sizeof *pvt); - memput(this, sizeof *this); -} - -/*% - * struct passwd * pw_next(struct irs_pw *this) - * - */ - -static struct passwd * -pw_next(struct irs_pw *this) { - struct pvt *pvt = (struct pvt *)this->private; - struct passwd *pw = &pvt->passwd; - char *body; - size_t bodylen; - int code; - char text[256]; - - if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) { - return (NULL); - } - - if (irs_irp_send_command(pvt->girpdata, "getpwent") != 0) { - return (NULL); - } - - if (irs_irp_get_full_response(pvt->girpdata, &code, - text, sizeof text, - &body, &bodylen) != 0) { - return (NULL); - } - - if (code == IRPD_GETUSER_OK) { - free_passwd(pw); - if (irp_unmarshall_pw(pw, body) != 0) { - pw = NULL; - } - } else { - pw = NULL; - } - - if (body != NULL) { - memput(body, bodylen); - } - - return (pw); -} - -/*% - * struct passwd * pw_byname(struct irs_pw *this, const char *name) - * - */ - -static struct passwd * -pw_byname(struct irs_pw *this, const char *name) { - struct pvt *pvt = (struct pvt *)this->private; - struct passwd *pw = &pvt->passwd; - char *body = NULL; - char text[256]; - size_t bodylen; - int code; - - if (pw->pw_name != NULL && strcmp(name, pw->pw_name) == 0) { - return (pw); - } - - if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) { - return (NULL); - } - - if (irs_irp_send_command(pvt->girpdata, "getpwnam %s", name) != 0) { - return (NULL); - } - - if (irs_irp_get_full_response(pvt->girpdata, &code, - text, sizeof text, - &body, &bodylen) != 0) { - return (NULL); - } - - if (code == IRPD_GETUSER_OK) { - free_passwd(pw); - if (irp_unmarshall_pw(pw, body) != 0) { - pw = NULL; - } - } else { - pw = NULL; - } - - if (body != NULL) { - memput(body, bodylen); - } - - return (pw); -} - -/*% - * struct passwd * pw_byuid(struct irs_pw *this, uid_t uid) - * - */ - -static struct passwd * -pw_byuid(struct irs_pw *this, uid_t uid) { - struct pvt *pvt = (struct pvt *)this->private; - char *body; - char text[256]; - size_t bodylen; - int code; - struct passwd *pw = &pvt->passwd; - - if (pw->pw_name != NULL && pw->pw_uid == uid) { - return (pw); - } - - if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) { - return (NULL); - } - - if (irs_irp_send_command(pvt->girpdata, "getpwuid %d", uid) != 0) { - return (NULL); - } - - if (irs_irp_get_full_response(pvt->girpdata, &code, - text, sizeof text, - &body, &bodylen) != 0) { - return (NULL); - } - - if (code == IRPD_GETUSER_OK) { - free_passwd(pw); - if (irp_unmarshall_pw(pw, body) != 0) { - pw = NULL; - } - } else { - pw = NULL; - } - - if (body != NULL) { - memput(body, bodylen); - } - - return (pw); -} - -/*% - * void pw_rewind(struct irs_pw *this) - * - */ - -static void -pw_rewind(struct irs_pw *this) { - struct pvt *pvt = (struct pvt *)this->private; - char text[256]; - int code; - - if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) { - return; - } - - if (irs_irp_send_command(pvt->girpdata, "setpwent") != 0) { - return; - } - - code = irs_irp_read_response(pvt->girpdata, text, sizeof text); - if (code != IRPD_GETUSER_SETOK) { - if (irp_log_errors) { - syslog(LOG_WARNING, "setpwent failed: %s", text); - } - } - - return; -} - -/*% - * void pw_minimize(struct irs_pw *this) - * - */ - -static void -pw_minimize(struct irs_pw *this) { - struct pvt *pvt = (struct pvt *)this->private; - - irs_irp_disconnect(pvt->girpdata); -} - - -/* Private. */ - -/*% - * Deallocate all the memory irp_unmarshall_pw allocated. - * - */ - -static void -free_passwd(struct passwd *pw) { - if (pw == NULL) - return; - - if (pw->pw_name != NULL) - free(pw->pw_name); - - if (pw->pw_passwd != NULL) - free(pw->pw_passwd); - -#ifdef HAVE_PW_CLASS - if (pw->pw_class != NULL) - free(pw->pw_class); -#endif - - if (pw->pw_gecos != NULL) - free(pw->pw_gecos); - - if (pw->pw_dir != NULL) - free(pw->pw_dir); - - if (pw->pw_shell != NULL) - free(pw->pw_shell); -} - -#endif /* WANT_IRS_PW */ -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/irp_sv.c /usr/src/lib/libc/net/irp_sv.c --- /home/reed/src/isc/libbind/libbind/irs/irp_sv.c 2005-04-26 23:56:29.000000000 -0500 +++ /usr/src/lib/libc/net/irp_sv.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,346 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Portions Copyright (c) 1996,1998 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#if defined(LIBC_SCCS) && !defined(lint) -static const char rcsid[] = "$Id: irp_sv.c,v 1.3 2005/04/27 04:56:29 sra Exp $"; -#endif /* LIBC_SCCS and not lint */ - -/* extern */ - -#include "port_before.h" - -#include -#include -#include - -#ifdef IRS_LCL_SV_DB -#include -#endif -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -#include "irs_p.h" -#include "lcl_p.h" -#include "irp_p.h" - -#include "port_after.h" - -/* Types */ - -struct pvt { - struct irp_p *girpdata; - int warned; - struct servent service; -}; - -/* Forward */ - -static void sv_close(struct irs_sv*); -static struct servent * sv_next(struct irs_sv *); -static struct servent * sv_byname(struct irs_sv *, const char *, - const char *); -static struct servent * sv_byport(struct irs_sv *, int, const char *); -static void sv_rewind(struct irs_sv *); -static void sv_minimize(struct irs_sv *); - -static void free_service(struct servent *sv); - - - -/* Public */ - -/*% - * struct irs_sv * irs_irp_sv(struct irs_acc *this) - * - */ - -struct irs_sv * -irs_irp_sv(struct irs_acc *this) { - struct irs_sv *sv; - struct pvt *pvt; - - if ((sv = memget(sizeof *sv)) == NULL) { - errno = ENOMEM; - return (NULL); - } - memset(sv, 0x0, sizeof *sv); - - if ((pvt = memget(sizeof *pvt)) == NULL) { - memput(sv, sizeof *sv); - errno = ENOMEM; - return (NULL); - } - memset(pvt, 0, sizeof *pvt); - pvt->girpdata = this->private; - - sv->private = pvt; - sv->close = sv_close; - sv->next = sv_next; - sv->byname = sv_byname; - sv->byport = sv_byport; - sv->rewind = sv_rewind; - sv->minimize = sv_minimize; - - return (sv); -} - -/* Methods */ - -/*% - * void sv_close(struct irs_sv *this) - * - */ - -static void -sv_close(struct irs_sv *this) { - struct pvt *pvt = (struct pvt *)this->private; - - sv_minimize(this); - - free_service(&pvt->service); - - memput(pvt, sizeof *pvt); - memput(this, sizeof *this); -} - -/*% - * Fills the cache if necessary and returns the next item from it. - * - */ - -static struct servent * -sv_next(struct irs_sv *this) { - struct pvt *pvt = (struct pvt *)this->private; - struct servent *sv = &pvt->service; - char *body; - size_t bodylen; - int code; - char text[256]; - - if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) { - return (NULL); - } - - if (irs_irp_send_command(pvt->girpdata, "getservent") != 0) { - return (NULL); - } - - if (irs_irp_get_full_response(pvt->girpdata, &code, - text, sizeof text, - &body, &bodylen) != 0) { - return (NULL); - } - - if (code == IRPD_GETSERVICE_OK) { - free_service(sv); - if (irp_unmarshall_sv(sv, body) != 0) { - sv = NULL; - } - } else { - sv = NULL; - } - - if (body != NULL) { - memput(body, bodylen); - } - - return (sv); -} - -/*% - * struct servent * sv_byname(struct irs_sv *this, const char *name, - * const char *proto) - * - */ - -static struct servent * -sv_byname(struct irs_sv *this, const char *name, const char *proto) { - struct pvt *pvt = (struct pvt *)this->private; - struct servent *sv = &pvt->service; - char *body; - char text[256]; - size_t bodylen; - int code; - - if (sv->s_name != NULL && - strcmp(name, sv->s_name) == 0 && - strcasecmp(proto, sv->s_proto) == 0) { - return (sv); - } - - if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) { - return (NULL); - } - - if (irs_irp_send_command(pvt->girpdata, "getservbyname %s %s", - name, proto) != 0) - return (NULL); - - if (irs_irp_get_full_response(pvt->girpdata, &code, - text, sizeof text, - &body, &bodylen) != 0) { - return (NULL); - } - - if (code == IRPD_GETSERVICE_OK) { - free_service(sv); - if (irp_unmarshall_sv(sv, body) != 0) { - sv = NULL; - } - } else { - sv = NULL; - } - - if (body != NULL) { - memput(body, bodylen); - } - - return (sv); -} - -/*% - * struct servent * sv_byport(struct irs_sv *this, int port, - * const char *proto) - * - */ - -static struct servent * -sv_byport(struct irs_sv *this, int port, const char *proto) { - struct pvt *pvt = (struct pvt *)this->private; - struct servent *sv = &pvt->service; - char *body; - size_t bodylen; - char text[256]; - int code; - - if (sv->s_name != NULL && - port == sv->s_port && - strcasecmp(proto, sv->s_proto) == 0) { - return (sv); - } - - if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) { - return (NULL); - } - - if (irs_irp_send_command(pvt->girpdata, "getservbyport %d %s", - ntohs((short)port), proto) != 0) { - return (NULL); - } - - if (irs_irp_get_full_response(pvt->girpdata, &code, - text, sizeof text, - &body, &bodylen) != 0) { - return (NULL); - } - - if (code == IRPD_GETSERVICE_OK) { - free_service(sv); - if (irp_unmarshall_sv(sv, body) != 0) { - sv = NULL; - } - } else { - sv = NULL; - } - - if (body != NULL) { - memput(body, bodylen); - } - - return (sv); -} - -/*% - * void sv_rewind(struct irs_sv *this) - * - */ - -static void -sv_rewind(struct irs_sv *this) { - struct pvt *pvt = (struct pvt *)this->private; - char text[256]; - int code; - - if (irs_irp_connection_setup(pvt->girpdata, &pvt->warned) != 0) { - return; - } - - if (irs_irp_send_command(pvt->girpdata, "setservent") != 0) { - return; - } - - code = irs_irp_read_response(pvt->girpdata, text, sizeof text); - if (code != IRPD_GETSERVICE_SETOK) { - if (irp_log_errors) { - syslog(LOG_WARNING, "setservent failed: %s", text); - } - } - - return; -} - -/*% - * void sv_minimize(struct irs_sv *this) - * - */ - -static void -sv_minimize(struct irs_sv *this) { - struct pvt *pvt = (struct pvt *)this->private; - - irs_irp_disconnect(pvt->girpdata); -} - - - - - - -static void -free_service(struct servent *sv) { - char **p; - - if (sv == NULL) { - return; - } - - if (sv->s_name != NULL) { - free(sv->s_name); - } - - for (p = sv->s_aliases ; p != NULL && *p != NULL ; p++) { - free(*p); - } - - if (sv->s_proto != NULL) { - free(sv->s_proto); - } -} - - - -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/irpmarshall.c /usr/src/lib/libc/net/irpmarshall.c --- /home/reed/src/isc/libbind/libbind/irs/irpmarshall.c 2013-06-05 09:33:54.000000000 -0500 +++ /usr/src/lib/libc/net/irpmarshall.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,2297 +0,0 @@ -/* - * Copyright(c) 1989, 1993, 1995 - * The Regents of the University of California. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Portions Copyright (c) 1996 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#if defined(LIBC_SCCS) && !defined(lint) -static const char rcsid[] = "$Id: irpmarshall.c,v 1.8 2013-01-06 23:14:51 marka Exp $"; -#endif /* LIBC_SCCS and not lint */ - -#if 0 - -Check values are in approrpriate endian order. - -Double check memory allocations on unmarhsalling - -#endif - - -/* Extern */ - -#include "port_before.h" - -#include -#include - -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include - -#include "port_after.h" - - -#ifndef HAVE_STRNDUP -static char *strndup(const char *str, size_t len); -#endif - -static char **splitarray(const char *buffer, const char *buffend, char delim); -static int joinarray(char * const * argv, char *buffer, char delim); -static char *getfield(char **res, size_t reslen, char **buffer, char delim); -static size_t joinlength(char * const *argv); -static void free_array(char **argv, size_t entries); - -#define ADDR_T_STR(x) (x == AF_INET ? "AF_INET" :\ - (x == AF_INET6 ? "AF_INET6" : "UNKNOWN")) - -#define MAXPADDRSIZE (sizeof "255.255.255.255" + 1) - -static char COMMA = ','; - -static const char *COMMASTR = ","; -static const char *COLONSTR = ":"; - - - -/* See big comment at bottom of irpmarshall.h for description. */ - - -#ifdef WANT_IRS_PW -/* +++++++++++++++++++++++++ struct passwd +++++++++++++++++++++++++ */ - -/*% - * int irp_marshall_pw(const struct passwd *pw, char **buffer, size_t *len) - * - * notes: \li - * - * See irpmarshall.h - * - * return: \li - * - * 0 on sucess, -1 on failure. - * - */ - -int -irp_marshall_pw(const struct passwd *pw, char **buffer, size_t *len) { - size_t need = 1 ; /*%< for null byte */ - char pwUid[24]; - char pwGid[24]; - char pwChange[24]; - char pwExpire[24]; - const char *pwClass; - const char *fieldsep = COLONSTR; - - if (pw == NULL || len == NULL) { - errno = EINVAL; - return (-1); - } - - sprintf(pwUid, "%ld", (long)pw->pw_uid); - sprintf(pwGid, "%ld", (long)pw->pw_gid); - -#ifdef HAVE_PW_CHANGE - sprintf(pwChange, "%ld", (long)pw->pw_change); -#else - pwChange[0] = '0'; - pwChange[1] = '\0'; -#endif - -#ifdef HAVE_PW_EXPIRE - sprintf(pwExpire, "%ld", (long)pw->pw_expire); -#else - pwExpire[0] = '0'; - pwExpire[1] = '\0'; -#endif - -#ifdef HAVE_PW_CLASS - pwClass = pw->pw_class; -#else - pwClass = ""; -#endif - - need += strlen(pw->pw_name) + 1; /*%< one for fieldsep */ - need += strlen(pw->pw_passwd) + 1; - need += strlen(pwUid) + 1; - need += strlen(pwGid) + 1; - need += strlen(pwClass) + 1; - need += strlen(pwChange) + 1; - need += strlen(pwExpire) + 1; - need += strlen(pw->pw_gecos) + 1; - need += strlen(pw->pw_dir) + 1; - need += strlen(pw->pw_shell) + 1; - - if (buffer == NULL) { - *len = need; - return (0); - } - - if (*buffer != NULL && need > *len) { - errno = EINVAL; - return (-1); - } - - if (*buffer == NULL) { - need += 2; /*%< for CRLF */ - *buffer = memget(need); - if (*buffer == NULL) { - errno = ENOMEM; - return (-1); - } - - *len = need; - } - - strcpy(*buffer, pw->pw_name); strcat(*buffer, fieldsep); - strcat(*buffer, pw->pw_passwd); strcat(*buffer, fieldsep); - strcat(*buffer, pwUid); strcat(*buffer, fieldsep); - strcat(*buffer, pwGid); strcat(*buffer, fieldsep); - strcat(*buffer, pwClass); strcat(*buffer, fieldsep); - strcat(*buffer, pwChange); strcat(*buffer, fieldsep); - strcat(*buffer, pwExpire); strcat(*buffer, fieldsep); - strcat(*buffer, pw->pw_gecos); strcat(*buffer, fieldsep); - strcat(*buffer, pw->pw_dir); strcat(*buffer, fieldsep); - strcat(*buffer, pw->pw_shell); strcat(*buffer, fieldsep); - - return (0); -} - -/*% - * int irp_unmarshall_pw(struct passwd *pw, char *buffer) - * - * notes: \li - * - * See irpmarshall.h - * - * return: \li - * - * 0 on success, -1 on failure - * - */ - -int -irp_unmarshall_pw(struct passwd *pw, char *buffer) { - char *name, *pass, *class, *gecos, *dir, *shell; - uid_t pwuid; - gid_t pwgid; - time_t pwchange; - time_t pwexpire; - char *p; - long t; - char tmpbuf[24]; - char *tb = &tmpbuf[0]; - char fieldsep = ':'; - int myerrno = EINVAL; - - name = pass = class = gecos = dir = shell = NULL; - p = buffer; - - /* pw_name field */ - name = NULL; - if (getfield(&name, 0, &p, fieldsep) == NULL || strlen(name) == 0) { - goto error; - } - - /* pw_passwd field */ - pass = NULL; - if (getfield(&pass, 0, &p, fieldsep) == NULL) { /*%< field can be empty */ - goto error; - } - - - /* pw_uid field */ - tb = tmpbuf; - if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL || - strlen(tb) == 0) { - goto error; - } - t = strtol(tmpbuf, &tb, 10); - if (*tb) { - goto error; /*%< junk in value */ - } - pwuid = (uid_t)t; - if ((long) pwuid != t) { /*%< value must have been too big. */ - goto error; - } - - - - /* pw_gid field */ - tb = tmpbuf; - if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL || - strlen(tb) == 0) { - goto error; - } - t = strtol(tmpbuf, &tb, 10); - if (*tb) { - goto error; /*%< junk in value */ - } - pwgid = (gid_t)t; - if ((long)pwgid != t) { /*%< value must have been too big. */ - goto error; - } - - - - /* pw_class field */ - class = NULL; - if (getfield(&class, 0, &p, fieldsep) == NULL) { - goto error; - } - - - - /* pw_change field */ - tb = tmpbuf; - if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL || - strlen(tb) == 0) { - goto error; - } - t = strtol(tmpbuf, &tb, 10); - if (*tb) { - goto error; /*%< junk in value */ - } - pwchange = (time_t)t; - if ((long)pwchange != t) { /*%< value must have been too big. */ - goto error; - } - - - - /* pw_expire field */ - tb = tmpbuf; - if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL || - strlen(tb) == 0) { - goto error; - } - t = strtol(tmpbuf, &tb, 10); - if (*tb) { - goto error; /*%< junk in value */ - } - pwexpire = (time_t)t; - if ((long) pwexpire != t) { /*%< value must have been too big. */ - goto error; - } - - - - /* pw_gecos field */ - gecos = NULL; - if (getfield(&gecos, 0, &p, fieldsep) == NULL) { - goto error; - } - - - - /* pw_dir field */ - dir = NULL; - if (getfield(&dir, 0, &p, fieldsep) == NULL) { - goto error; - } - - - - /* pw_shell field */ - shell = NULL; - if (getfield(&shell, 0, &p, fieldsep) == NULL) { - goto error; - } - - - - pw->pw_name = name; - pw->pw_passwd = pass; - pw->pw_uid = pwuid; - pw->pw_gid = pwgid; - pw->pw_gecos = gecos; - pw->pw_dir = dir; - pw->pw_shell = shell; - -#ifdef HAVE_PW_CHANGE - pw->pw_change = pwchange; -#endif -#ifdef HAVE_PW_CLASS - pw->pw_class = class; -#endif -#ifdef HAVE_PW_EXPIRE - pw->pw_expire = pwexpire; -#endif - - return (0); - - error: - errno = myerrno; - - if (name != NULL) free(name); - if (pass != NULL) free(pass); - if (gecos != NULL) free(gecos); - if (dir != NULL) free(dir); - if (shell != NULL) free(shell); - - return (-1); -} - -/* ------------------------- struct passwd ------------------------- */ -#endif /* WANT_IRS_PW */ -/* +++++++++++++++++++++++++ struct group +++++++++++++++++++++++++ */ - -/*% - * int irp_marshall_gr(const struct group *gr, char **buffer, size_t *len) - * - * notes: \li - * - * See irpmarshall.h. - * - * return: \li - * - * 0 on success, -1 on failure - */ - -int -irp_marshall_gr(const struct group *gr, char **buffer, size_t *len) { - size_t need = 1; /*%< for null byte */ - char grGid[24]; - const char *fieldsep = COLONSTR; - - if (gr == NULL || len == NULL) { - errno = EINVAL; - return (-1); - } - - sprintf(grGid, "%ld", (long)gr->gr_gid); - - need += strlen(gr->gr_name) + 1; -#ifndef MISSING_GR_PASSWD - need += strlen(gr->gr_passwd) + 1; -#else - need++; -#endif - need += strlen(grGid) + 1; - need += joinlength(gr->gr_mem) + 1; - - if (buffer == NULL) { - *len = need; - return (0); - } - - if (*buffer != NULL && need > *len) { - errno = EINVAL; - return (-1); - } - - if (*buffer == NULL) { - need += 2; /*%< for CRLF */ - *buffer = memget(need); - if (*buffer == NULL) { - errno = ENOMEM; - return (-1); - } - - *len = need; - } - - strcpy(*buffer, gr->gr_name); strcat(*buffer, fieldsep); -#ifndef MISSING_GR_PASSWD - strcat(*buffer, gr->gr_passwd); -#endif - strcat(*buffer, fieldsep); - strcat(*buffer, grGid); strcat(*buffer, fieldsep); - joinarray(gr->gr_mem, *buffer, COMMA) ; strcat(*buffer, fieldsep); - - return (0); -} - -/*% - * int irp_unmarshall_gr(struct group *gr, char *buffer) - * - * notes: \li - * - * See irpmarshall.h - * - * return: \li - * - * 0 on success and -1 on failure. - * - */ - -int -irp_unmarshall_gr(struct group *gr, char *buffer) { - char *p, *q; - gid_t grgid; - long t; - char *name = NULL; - char *pass = NULL; - char **members = NULL; - char tmpbuf[24]; - char *tb; - char fieldsep = ':'; - int myerrno = EINVAL; - - if (gr == NULL || buffer == NULL) { - errno = EINVAL; - return (-1); - } - - p = buffer; - - /* gr_name field */ - name = NULL; - if (getfield(&name, 0, &p, fieldsep) == NULL || strlen(name) == 0U) { - goto error; - } - - - /* gr_passwd field */ - pass = NULL; - if (getfield(&pass, 0, &p, fieldsep) == NULL) { - goto error; - } - - - /* gr_gid field */ - tb = tmpbuf; - if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL || - strlen(tb) == 0U) { - goto error; - } - t = strtol(tmpbuf, &tb, 10); - if (*tb) { - goto error; /*%< junk in value */ - } - grgid = (gid_t)t; - if ((long) grgid != t) { /*%< value must have been too big. */ - goto error; - } - - - /* gr_mem field. Member names are separated by commas */ - q = strchr(p, fieldsep); - if (q == NULL) { - goto error; - } - members = splitarray(p, q, COMMA); - if (members == NULL) { - myerrno = errno; - goto error; - } - p = q + 1; - - - gr->gr_name = name; -#ifndef MISSING_GR_PASSWD - gr->gr_passwd = pass; -#endif - gr->gr_gid = grgid; - gr->gr_mem = members; - - return (0); - - error: - errno = myerrno; - - if (name != NULL) free(name); - if (pass != NULL) free(pass); - - return (-1); -} - - -/* ------------------------- struct group ------------------------- */ - - - - -/* +++++++++++++++++++++++++ struct servent +++++++++++++++++++++++++ */ - -/*% - * int irp_marshall_sv(const struct servent *sv, char **buffer, size_t *len) - * - * notes: \li - * - * See irpmarshall.h - * - * return: \li - * - * 0 on success, -1 on failure. - * - */ - -int -irp_marshall_sv(const struct servent *sv, char **buffer, size_t *len) { - size_t need = 1; /*%< for null byte */ - char svPort[24]; - const char *fieldsep = COLONSTR; - short realport; - - if (sv == NULL || len == NULL) { - errno = EINVAL; - return (-1); - } - - /* the int s_port field is actually a short in network order. We - want host order to make the marshalled data look correct */ - realport = ntohs((short)sv->s_port); - sprintf(svPort, "%d", realport); - - need += strlen(sv->s_name) + 1; - need += joinlength(sv->s_aliases) + 1; - need += strlen(svPort) + 1; - need += strlen(sv->s_proto) + 1; - - if (buffer == NULL) { - *len = need; - return (0); - } - - if (*buffer != NULL && need > *len) { - errno = EINVAL; - return (-1); - } - - if (*buffer == NULL) { - need += 2; /*%< for CRLF */ - *buffer = memget(need); - if (*buffer == NULL) { - errno = ENOMEM; - return (-1); - } - - *len = need; - } - - strcpy(*buffer, sv->s_name); strcat(*buffer, fieldsep); - joinarray(sv->s_aliases, *buffer, COMMA); strcat(*buffer, fieldsep); - strcat(*buffer, svPort); strcat(*buffer, fieldsep); - strcat(*buffer, sv->s_proto); strcat(*buffer, fieldsep); - - return (0); -} - -/*% - * int irp_unmarshall_sv(struct servent *sv, char *buffer) - * - * notes: \li - * - * See irpmarshall.h - * - * return: \li - * - * 0 on success, -1 on failure. - * - */ - -int -irp_unmarshall_sv(struct servent *sv, char *buffer) { - char *p, *q; - short svport; - long t; - char *name = NULL; - char *proto = NULL; - char **aliases = NULL; - char tmpbuf[24]; - char *tb; - char fieldsep = ':'; - int myerrno = EINVAL; - - if (sv == NULL || buffer == NULL) - return (-1); - - p = buffer; - - - /* s_name field */ - name = NULL; - if (getfield(&name, 0, &p, fieldsep) == NULL || strlen(name) == 0U) { - goto error; - } - - - /* s_aliases field */ - q = strchr(p, fieldsep); - if (q == NULL) { - goto error; - } - aliases = splitarray(p, q, COMMA); - if (aliases == NULL) { - myerrno = errno; - goto error; - } - p = q + 1; - - - /* s_port field */ - tb = tmpbuf; - if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL || - strlen(tb) == 0U) { - goto error; - } - t = strtol(tmpbuf, &tb, 10); - if (*tb) { - goto error; /*%< junk in value */ - } - svport = (short)t; - if ((long) svport != t) { /*%< value must have been too big. */ - goto error; - } - svport = htons(svport); - - /* s_proto field */ - proto = NULL; - if (getfield(&proto, 0, &p, fieldsep) == NULL) { - goto error; - } - - sv->s_name = name; - sv->s_aliases = aliases; - sv->s_port = svport; - sv->s_proto = proto; - - return (0); - - error: - errno = myerrno; - - if (name != NULL) free(name); - if (proto != NULL) free(proto); - free_array(aliases, 0); - - return (-1); -} - - -/* ------------------------- struct servent ------------------------- */ - -/* +++++++++++++++++++++++++ struct protoent +++++++++++++++++++++++++ */ - -/*% - * int irp_marshall_pr(struct protoent *pr, char **buffer, size_t *len) - * - * notes: \li - * - * See irpmarshall.h - * - * return: \li - * - * 0 on success and -1 on failure. - * - */ - -int -irp_marshall_pr(struct protoent *pr, char **buffer, size_t *len) { - size_t need = 1; /*%< for null byte */ - char prProto[24]; - const char *fieldsep = COLONSTR; - - if (pr == NULL || len == NULL) { - errno = EINVAL; - return (-1); - } - - sprintf(prProto, "%d", (int)pr->p_proto); - - need += strlen(pr->p_name) + 1; - need += joinlength(pr->p_aliases) + 1; - need += strlen(prProto) + 1; - - if (buffer == NULL) { - *len = need; - return (0); - } - - if (*buffer != NULL && need > *len) { - errno = EINVAL; - return (-1); - } - - if (*buffer == NULL) { - need += 2; /*%< for CRLF */ - *buffer = memget(need); - if (*buffer == NULL) { - errno = ENOMEM; - return (-1); - } - - *len = need; - } - - strcpy(*buffer, pr->p_name); strcat(*buffer, fieldsep); - joinarray(pr->p_aliases, *buffer, COMMA); strcat(*buffer, fieldsep); - strcat(*buffer, prProto); strcat(*buffer, fieldsep); - - return (0); - -} - -/*% - * int irp_unmarshall_pr(struct protoent *pr, char *buffer) - * - * notes: \li - * - * See irpmarshall.h - * - * return: \li - * - * 0 on success, -1 on failure - * - */ - -int irp_unmarshall_pr(struct protoent *pr, char *buffer) { - char *p, *q; - int prproto; - long t; - char *name = NULL; - char **aliases = NULL; - char tmpbuf[24]; - char *tb; - char fieldsep = ':'; - int myerrno = EINVAL; - - if (pr == NULL || buffer == NULL) { - errno = EINVAL; - return (-1); - } - - p = buffer; - - /* p_name field */ - name = NULL; - if (getfield(&name, 0, &p, fieldsep) == NULL || strlen(name) == 0U) { - goto error; - } - - - /* p_aliases field */ - q = strchr(p, fieldsep); - if (q == NULL) { - goto error; - } - aliases = splitarray(p, q, COMMA); - if (aliases == NULL) { - myerrno = errno; - goto error; - } - p = q + 1; - - - /* p_proto field */ - tb = tmpbuf; - if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL || - strlen(tb) == 0U) { - goto error; - } - t = strtol(tmpbuf, &tb, 10); - if (*tb) { - goto error; /*%< junk in value */ - } - prproto = (int)t; - if ((long) prproto != t) { /*%< value must have been too big. */ - goto error; - } - - pr->p_name = name; - pr->p_aliases = aliases; - pr->p_proto = prproto; - - return (0); - - error: - errno = myerrno; - - if (name != NULL) free(name); - free_array(aliases, 0); - - return (-1); -} - -/* ------------------------- struct protoent ------------------------- */ - - - -/* +++++++++++++++++++++++++ struct hostent +++++++++++++++++++++++++ */ - -/*% - * int irp_marshall_ho(struct hostent *ho, char **buffer, size_t *len) - * - * notes: \li - * - * See irpmarshall.h. - * - * return: \li - * - * 0 on success, -1 on failure. - * - */ - -int -irp_marshall_ho(struct hostent *ho, char **buffer, size_t *len) { - size_t need = 1; /*%< for null byte */ - char hoaddrtype[24]; - char holength[24]; - char **av; - char *p; - int addrlen; - int malloced = 0; - size_t remlen; - const char *fieldsep = "@"; - - if (ho == NULL || len == NULL) { - errno = EINVAL; - return (-1); - } - - switch(ho->h_addrtype) { - case AF_INET: - strcpy(hoaddrtype, "AF_INET"); - break; - - case AF_INET6: - strcpy(hoaddrtype, "AF_INET6"); - break; - - default: - errno = EINVAL; - return (-1); - } - - sprintf(holength, "%d", ho->h_length); - - need += strlen(ho->h_name) + 1; - need += joinlength(ho->h_aliases) + 1; - need += strlen(hoaddrtype) + 1; - need += strlen(holength) + 1; - - /* we determine an upper bound on the string length needed, not an - exact length. */ - addrlen = (ho->h_addrtype == AF_INET ? 16 : 46) ; /*%< XX other AF's?? */ - for (av = ho->h_addr_list; av != NULL && *av != NULL ; av++) - need += addrlen; - - if (buffer == NULL) { - *len = need; - return (0); - } - - if (*buffer != NULL && need > *len) { - errno = EINVAL; - return (-1); - } - - if (*buffer == NULL) { - need += 2; /*%< for CRLF */ - *buffer = memget(need); - if (*buffer == NULL) { - errno = ENOMEM; - return (-1); - } - - *len = need; - malloced = 1; - } - - strcpy(*buffer, ho->h_name); strcat(*buffer, fieldsep); - joinarray(ho->h_aliases, *buffer, COMMA); strcat(*buffer, fieldsep); - strcat(*buffer, hoaddrtype); strcat(*buffer, fieldsep); - strcat(*buffer, holength); strcat(*buffer, fieldsep); - - p = *buffer + strlen(*buffer); - remlen = need - strlen(*buffer); - for (av = ho->h_addr_list ; av != NULL && *av != NULL ; av++) { - if (inet_ntop(ho->h_addrtype, *av, p, remlen) == NULL) { - goto error; - } - if (*(av + 1) != NULL) - strcat(p, COMMASTR); - remlen -= strlen(p); - p += strlen(p); - } - strcat(*buffer, fieldsep); - - return (0); - - error: - if (malloced) { - memput(*buffer, need); - } - - return (-1); -} - -/*% - * int irp_unmarshall_ho(struct hostent *ho, char *buffer) - * - * notes: \li - * - * See irpmarshall.h. - * - * return: \li - * - * 0 on success, -1 on failure. - * - */ - -int -irp_unmarshall_ho(struct hostent *ho, char *buffer) { - char *p, *q, *r; - int hoaddrtype; - int holength; - long t; - char *name; - char **aliases = NULL; - char **hohaddrlist = NULL; - size_t hoaddrsize; - char tmpbuf[24]; - char *tb; - char **alist; - int addrcount; - char fieldsep = '@'; - int myerrno = EINVAL; - - if (ho == NULL || buffer == NULL) { - errno = EINVAL; - return (-1); - } - - p = buffer; - - /* h_name field */ - name = NULL; - if (getfield(&name, 0, &p, fieldsep) == NULL || strlen(name) == 0U) { - goto error; - } - - - /* h_aliases field */ - q = strchr(p, fieldsep); - if (q == NULL) { - goto error; - } - aliases = splitarray(p, q, COMMA); - if (aliases == NULL) { - myerrno = errno; - goto error; - } - p = q + 1; - - - /* h_addrtype field */ - tb = tmpbuf; - if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL || - strlen(tb) == 0U) { - goto error; - } - if (strcmp(tmpbuf, "AF_INET") == 0) - hoaddrtype = AF_INET; - else if (strcmp(tmpbuf, "AF_INET6") == 0) - hoaddrtype = AF_INET6; - else - goto error; - - - /* h_length field */ - tb = tmpbuf; - if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL || - strlen(tb) == 0U) { - goto error; - } - t = strtol(tmpbuf, &tb, 10); - if (*tb) { - goto error; /*%< junk in value */ - } - holength = (int)t; - if ((long) holength != t) { /*%< value must have been too big. */ - goto error; - } - - - /* h_addr_list field */ - q = strchr(p, fieldsep); - if (q == NULL) - goto error; - - /* count how many addresss are in there */ - if (q > p + 1) { - for (addrcount = 1, r = p ; r != q ; r++) { - if (*r == COMMA) - addrcount++; - } - } else { - addrcount = 0; - } - - hoaddrsize = (addrcount + 1) * sizeof (char *); - hohaddrlist = malloc(hoaddrsize); - if (hohaddrlist == NULL) { - myerrno = ENOMEM; - goto error; - } - - memset(hohaddrlist, 0x0, hoaddrsize); - - alist = hohaddrlist; - for (t = 0, r = p ; r != q ; p = r + 1, t++) { - char saved; - while (r != q && *r != COMMA) r++; - saved = *r; - *r = 0x0; - - alist[t] = malloc(hoaddrtype == AF_INET ? 4 : 16); - if (alist[t] == NULL) { - myerrno = ENOMEM; - goto error; - } - - if (inet_pton(hoaddrtype, p, alist[t]) == -1) - goto error; - *r = saved; - } - alist[t] = NULL; - - ho->h_name = name; - ho->h_aliases = aliases; - ho->h_addrtype = hoaddrtype; - ho->h_length = holength; - ho->h_addr_list = hohaddrlist; - - return (0); - - error: - errno = myerrno; - - if (name != NULL) free(name); - free_array(hohaddrlist, 0); - free_array(aliases, 0); - - return (-1); -} - -/* ------------------------- struct hostent------------------------- */ - - - -/* +++++++++++++++++++++++++ struct netgrp +++++++++++++++++++++++++ */ - -/*% - * int irp_marshall_ng(const char *host, const char *user, - * const char *domain, char *buffer, size_t *len) - * - * notes: \li - * - * See note for irp_marshall_ng_start - * - * return: \li - * - * 0 on success, 0 on failure. - * - */ - -int -irp_marshall_ng(const char *host, const char *user, const char *domain, - char **buffer, size_t *len) { - size_t need = 1; /*%< for nul byte */ - const char *fieldsep = ","; - - if (len == NULL) { - errno = EINVAL; - return (-1); - } - - need += 4; /*%< two parens and two commas */ - need += (host == NULL ? 0 : strlen(host)); - need += (user == NULL ? 0 : strlen(user)); - need += (domain == NULL ? 0 : strlen(domain)); - - if (buffer == NULL) { - *len = need; - return (0); - } else if (*buffer != NULL && need > *len) { - errno = EINVAL; - return (-1); - } - - if (*buffer == NULL) { - need += 2; /*%< for CRLF */ - *buffer = memget(need); - if (*buffer == NULL) { - errno = ENOMEM; - return (-1); - } - - *len = need; - } - - (*buffer)[0] = '('; - (*buffer)[1] = '\0'; - - if (host != NULL) - strcat(*buffer, host); - strcat(*buffer, fieldsep); - - if (user != NULL) - strcat(*buffer, user); - strcat(*buffer, fieldsep); - - if (domain != NULL) - strcat(*buffer, domain); - strcat(*buffer, ")"); - - return (0); -} - - - -/* ---------- */ - -/*% - * int irp_unmarshall_ng(const char **host, const char **user, - * const char **domain, char *buffer) - * - * notes: \li - * - * Unpacks the BUFFER into 3 character arrays it allocates and assigns - * to *HOST, *USER and *DOMAIN. If any field of the value is empty, - * then the corresponding paramater value will be set to NULL. - * - * return: \li - * - * 0 on success and -1 on failure. - */ - -int -irp_unmarshall_ng(const char **hostp, const char **userp, const char **domainp, - char *buffer) -{ - char *p, *q; - char fieldsep = ','; - int myerrno = EINVAL; - char *host, *user, *domain; - - if (userp == NULL || hostp == NULL || - domainp == NULL || buffer == NULL) { - errno = EINVAL; - return (-1); - } - - host = user = domain = NULL; - - p = buffer; - while (isspace((unsigned char)*p)) { - p++; - } - if (*p != '(') { - goto error; - } - - q = p + 1; - while (*q && *q != fieldsep) - q++; - if (!*q) { - goto error; - } else if (q > p + 1) { - host = strndup(p, q - p); - } - - p = q + 1; - if (!*p) { - goto error; - } else if (*p != fieldsep) { - q = p + 1; - while (*q && *q != fieldsep) - q++; - if (!*q) { - goto error; - } - user = strndup(p, q - p); - } else { - p++; - } - - if (!*p) { - goto error; - } else if (*p != ')') { - q = p + 1; - while (*q && *q != ')') - q++; - if (!*q) { - goto error; - } - domain = strndup(p, q - p); - } - *hostp = host; - *userp = user; - *domainp = domain; - - return (0); - - error: - errno = myerrno; - - if (host != NULL) free(host); - if (user != NULL) free(user); - - return (-1); -} - -/* ------------------------- struct netgrp ------------------------- */ - - - - -/* +++++++++++++++++++++++++ struct nwent +++++++++++++++++++++++++ */ - -/*% - * int irp_marshall_nw(struct nwent *ne, char **buffer, size_t *len) - * - * notes: \li - * - * See at top. - * - * return: \li - * - * 0 on success and -1 on failure. - * - */ - -int -irp_marshall_nw(struct nwent *ne, char **buffer, size_t *len) { - size_t need = 1; /*%< for null byte */ - char nAddrType[24]; - char nNet[MAXPADDRSIZE]; - const char *fieldsep = COLONSTR; - - if (ne == NULL || len == NULL) { - return (-1); - } - - strcpy(nAddrType, ADDR_T_STR(ne->n_addrtype)); - - if (inet_net_ntop(ne->n_addrtype, ne->n_addr, ne->n_length, - nNet, sizeof nNet) == NULL) { - return (-1); - } - - - need += strlen(ne->n_name) + 1; - need += joinlength(ne->n_aliases) + 1; - need += strlen(nAddrType) + 1; - need += strlen(nNet) + 1; - - if (buffer == NULL) { - *len = need; - return (0); - } - - if (*buffer != NULL && need > *len) { - errno = EINVAL; - return (-1); - } - - if (*buffer == NULL) { - need += 2; /*%< for CRLF */ - *buffer = memget(need); - if (*buffer == NULL) { - errno = ENOMEM; - return (-1); - } - - *len = need; - } - - strcpy(*buffer, ne->n_name); strcat(*buffer, fieldsep); - joinarray(ne->n_aliases, *buffer, COMMA) ; strcat(*buffer, fieldsep); - strcat(*buffer, nAddrType); strcat(*buffer, fieldsep); - strcat(*buffer, nNet); strcat(*buffer, fieldsep); - - return (0); -} - -/*% - * int irp_unmarshall_nw(struct nwent *ne, char *buffer) - * - * notes: \li - * - * See note up top. - * - * return: \li - * - * 0 on success and -1 on failure. - * - */ - -int -irp_unmarshall_nw(struct nwent *ne, char *buffer) { - char *p, *q; - int naddrtype; - long nnet; - int bits; - char *name = NULL; - char **aliases = NULL; - char tmpbuf[24]; - char *tb; - char fieldsep = ':'; - int myerrno = EINVAL; - - if (ne == NULL || buffer == NULL) { - goto error; - } - - p = buffer; - - /* n_name field */ - name = NULL; - if (getfield(&name, 0, &p, fieldsep) == NULL || strlen(name) == 0U) { - goto error; - } - - - /* n_aliases field. Aliases are separated by commas */ - q = strchr(p, fieldsep); - if (q == NULL) { - goto error; - } - aliases = splitarray(p, q, COMMA); - if (aliases == NULL) { - myerrno = errno; - goto error; - } - p = q + 1; - - - /* h_addrtype field */ - tb = tmpbuf; - if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL || - strlen(tb) == 0U) { - goto error; - } - if (strcmp(tmpbuf, "AF_INET") == 0) - naddrtype = AF_INET; - else if (strcmp(tmpbuf, "AF_INET6") == 0) - naddrtype = AF_INET6; - else - goto error; - - - /* n_net field */ - tb = tmpbuf; - if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL || - strlen(tb) == 0U) { - goto error; - } - nnet = 0; - bits = inet_net_pton(naddrtype, tmpbuf, &nnet, sizeof nnet); - if (bits < 0) { - goto error; - } - - /* nnet = ntohl(nnet); */ /* keep in network order for nwent */ - - ne->n_name = name; - ne->n_aliases = aliases; - ne->n_addrtype = naddrtype; - ne->n_length = bits; - ne->n_addr = malloc(sizeof nnet); - if (ne->n_addr == NULL) { - goto error; - } - - memcpy(ne->n_addr, &nnet, sizeof nnet); - - return (0); - - error: - errno = myerrno; - - if (name != NULL) free(name); - free_array(aliases, 0); - - return (-1); -} - - -/* ------------------------- struct nwent ------------------------- */ - - -/* +++++++++++++++++++++++++ struct netent +++++++++++++++++++++++++ */ - -/*% - * int irp_marshall_ne(struct netent *ne, char **buffer, size_t *len) - * - * notes: \li - * - * See at top. - * - * return: \li - * - * 0 on success and -1 on failure. - * - */ - -int -irp_marshall_ne(struct netent *ne, char **buffer, size_t *len) { - size_t need = 1; /*%< for null byte */ - char nAddrType[24]; - char nNet[MAXPADDRSIZE]; - const char *fieldsep = COLONSTR; - long nval; - - if (ne == NULL || len == NULL) { - return (-1); - } - - strcpy(nAddrType, ADDR_T_STR(ne->n_addrtype)); - - nval = htonl(ne->n_net); - if (inet_ntop(ne->n_addrtype, &nval, nNet, sizeof nNet) == NULL) { - return (-1); - } - - need += strlen(ne->n_name) + 1; - need += joinlength(ne->n_aliases) + 1; - need += strlen(nAddrType) + 1; - need += strlen(nNet) + 1; - - if (buffer == NULL) { - *len = need; - return (0); - } - - if (*buffer != NULL && need > *len) { - errno = EINVAL; - return (-1); - } - - if (*buffer == NULL) { - need += 2; /*%< for CRLF */ - *buffer = memget(need); - if (*buffer == NULL) { - errno = ENOMEM; - return (-1); - } - - *len = need; - } - - strcpy(*buffer, ne->n_name); strcat(*buffer, fieldsep); - joinarray(ne->n_aliases, *buffer, COMMA) ; strcat(*buffer, fieldsep); - strcat(*buffer, nAddrType); strcat(*buffer, fieldsep); - strcat(*buffer, nNet); strcat(*buffer, fieldsep); - - return (0); -} - -/*% - * int irp_unmarshall_ne(struct netent *ne, char *buffer) - * - * notes: \li - * - * See note up top. - * - * return: \li - * - * 0 on success and -1 on failure. - * - */ - -int -irp_unmarshall_ne(struct netent *ne, char *buffer) { - char *p, *q; - int naddrtype; - long nnet; - int bits; - char *name = NULL; - char **aliases = NULL; - char tmpbuf[24]; - char *tb; - char fieldsep = ':'; - int myerrno = EINVAL; - - if (ne == NULL || buffer == NULL) { - goto error; - } - - p = buffer; - - /* n_name field */ - name = NULL; - if (getfield(&name, 0, &p, fieldsep) == NULL || strlen(name) == 0U) { - goto error; - } - - - /* n_aliases field. Aliases are separated by commas */ - q = strchr(p, fieldsep); - if (q == NULL) { - goto error; - } - aliases = splitarray(p, q, COMMA); - if (aliases == NULL) { - myerrno = errno; - goto error; - } - p = q + 1; - - - /* h_addrtype field */ - tb = tmpbuf; - if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL || - strlen(tb) == 0U) { - goto error; - } - if (strcmp(tmpbuf, "AF_INET") == 0) - naddrtype = AF_INET; - else if (strcmp(tmpbuf, "AF_INET6") == 0) - naddrtype = AF_INET6; - else - goto error; - - - /* n_net field */ - tb = tmpbuf; - if (getfield(&tb, sizeof tmpbuf, &p, fieldsep) == NULL || - strlen(tb) == 0U) { - goto error; - } - bits = inet_net_pton(naddrtype, tmpbuf, &nnet, sizeof nnet); - if (bits < 0) { - goto error; - } - nnet = ntohl(nnet); - - ne->n_name = name; - ne->n_aliases = aliases; - ne->n_addrtype = naddrtype; - ne->n_net = nnet; - - return (0); - - error: - errno = myerrno; - - if (name != NULL) free(name); - free_array(aliases, 0); - - return (-1); -} - - -/* ------------------------- struct netent ------------------------- */ - - -/* =========================================================================== */ - -/*% - * static char ** splitarray(const char *buffer, const char *buffend, char delim) - * - * notes: \li - * - * Split a delim separated astring. Not allowed - * to have two delims next to each other. BUFFER points to begining of - * string, BUFFEND points to one past the end of the string - * (i.e. points at where the null byte would be if null - * terminated). - * - * return: \li - * - * Returns a malloced array of pointers, each pointer pointing to a - * malloced string. If BUFEER is an empty string, then return values is - * array of 1 pointer that is NULL. Returns NULL on failure. - * - */ - -static char ** -splitarray(const char *buffer, const char *buffend, char delim) { - const char *p, *q; - int count = 0; - char **arr = NULL; - char **aptr; - - if (buffend < buffer) - return (NULL); - else if (buffend > buffer && *buffer == delim) - return (NULL); - else if (buffend > buffer && *(buffend - 1) == delim) - return (NULL); - - /* count the number of field and make sure none are empty */ - if (buffend > buffer + 1) { - for (count = 1, q = buffer ; q != buffend ; q++) { - if (*q == delim) { - if (q > buffer && (*(q - 1) == delim)) { - errno = EINVAL; - return (NULL); - } - count++; - } - } - } - - if (count > 0) { - count++ ; /*%< for NULL at end */ - aptr = arr = malloc(count * sizeof (char *)); - if (aptr == NULL) { - errno = ENOMEM; - return (NULL); - } - - memset(arr, 0x0, count * sizeof (char *)); - for (p = buffer ; p < buffend ; p++) { - for (q = p ; *q != delim && q != buffend ; q++) - /* nothing */; - *aptr = strndup(p, q - p); - - p = q; - aptr++; - } - *aptr = NULL; - } else { - arr = malloc(sizeof (char *)); - if (arr == NULL) { - errno = ENOMEM; - return (NULL); - } - - *arr = NULL; - } - - return (arr); -} - -/*% - * static size_t joinlength(char * const *argv) - * - * return: \li - * - * the number of bytes in all the arrays pointed at - * by argv, including their null bytes(which will usually be turned - * into commas). - * - * - */ - -static size_t -joinlength(char * const *argv) { - int len = 0; - - while (argv && *argv) { - len += (strlen(*argv) + 1); - argv++; - } - - return (len); -} - -/*% - * int joinarray(char * const *argv, char *buffer, char delim) - * - * notes: \li - * - * Copy all the ARGV strings into the end of BUFFER - * separating them with DELIM. BUFFER is assumed to have - * enough space to hold everything and to be already null-terminated. - * - * return: \li - * - * 0 unless argv or buffer is NULL. - * - * - */ - -static int -joinarray(char * const *argv, char *buffer, char delim) { - char * const *p; - char sep[2]; - - if (argv == NULL || buffer == NULL) { - errno = EINVAL; - return (-1); - } - - sep[0] = delim; - sep[1] = 0x0; - - for (p = argv ; *p != NULL ; p++) { - strcat(buffer, *p); - if (*(p + 1) != NULL) { - strcat(buffer, sep); - } - } - - return (0); -} - -/*% - * static char * getfield(char **res, size_t reslen, char **ptr, char delim) - * - * notes: \li - * - * Stores in *RES, which is a buffer of length RESLEN, a - * copy of the bytes from *PTR up to and including the first - * instance of DELIM. If *RES is NULL, then it will be - * assigned a malloced buffer to hold the copy. *PTR is - * modified to point at the found delimiter. - * - * return: \li - * - * If there was no delimiter, then NULL is returned, - * otherewise *RES is returned. - * - */ - -static char * -getfield(char **res, size_t reslen, char **ptr, char delim) { - char *q; - - if (res == NULL || ptr == NULL || *ptr == NULL) { - errno = EINVAL; - return (NULL); - } - - q = strchr(*ptr, delim); - - if (q == NULL) { - errno = EINVAL; - return (NULL); - } else { - if (*res == NULL) { - *res = strndup(*ptr, q - *ptr); - } else { - if ((size_t)(q - *ptr + 1) > reslen) { /*%< to big for res */ - errno = EINVAL; - return (NULL); - } else { - strncpy(*res, *ptr, q - *ptr); - (*res)[q - *ptr] = 0x0; - } - } - *ptr = q + 1; - } - - return (*res); -} - - - - - -#ifndef HAVE_STRNDUP -/* - * static char * strndup(const char *str, size_t len) - * - * notes: \li - * - * like strdup, except do len bytes instead of the whole string. Always - * null-terminates. - * - * return: \li - * - * The newly malloced string. - * - */ - -static char * -strndup(const char *str, size_t len) { - char *p = malloc(len + 1); - - if (p == NULL) - return (NULL); - strncpy(p, str, len); - p[len] = 0x0; - return (p); -} -#endif - -#if WANT_MAIN - -/*% - * static int strcmp_nws(const char *a, const char *b) - * - * notes: \li - * - * do a strcmp, except uneven lengths of whitespace compare the same - * - * return: \li - * - */ - -static int -strcmp_nws(const char *a, const char *b) { - while (*a && *b) { - if (isspace(*a) && isspace(*b)) { - do { - a++; - } while (isspace(*a)); - do { - b++; - } while (isspace(*b)); - } - if (*a < *b) - return (-1); - else if (*a > *b) - return (1); - - a++; - b++;; - } - - if (*a == *b) - return (0); - else if (*a > *b) - return (1); - else - return (-1); -} - -#endif - -/*% - * static void free_array(char **argv, size_t entries) - * - * notes: \li - * - * Free argv and each of the pointers inside it. The end of - * the array is when a NULL pointer is found inside. If - * entries is > 0, then NULL pointers inside the array do - * not indicate the end of the array. - * - */ - -static void -free_array(char **argv, size_t entries) { - char **p = argv; - int useEntries = (entries > 0U); - - if (argv == NULL) - return; - - while ((useEntries && entries > 0U) || *p) { - if (*p) - free(*p); - p++; - if (useEntries) - entries--; - } - free(argv); -} - - - - - -/* ************************************************** */ - -#if WANT_MAIN - -/*% takes an option to indicate what sort of marshalling(read the code) and - an argument. If the argument looks like a marshalled buffer(has a ':' - embedded) then it's unmarshalled and the remarshalled and the new string - is compared to the old one. -*/ - -int -main(int argc, char **argv) { - char buffer[1024]; - char *b = &buffer[0]; - size_t len = sizeof buffer; - char option; - - if (argc < 2 || argv[1][0] != '-') - exit(1); - - option = argv[1][1]; - argv++; - argc--; - - -#if 0 - { - char buff[10]; - char *p = argv[1], *q = &buff[0]; - - while (getfield(&q, sizeof buff, &p, ':') != NULL) { - printf("field: \"%s\"\n", q); - p++; - } - printf("p is now \"%s\"\n", p); - } -#endif - -#if 0 - { - char **x = splitarray(argv[1], argv[1] + strlen(argv[1]), - argv[2][0]); - char **p; - - if (x == NULL) - printf("split failed\n"); - - for (p = x ; p != NULL && *p != NULL ; p++) { - printf("\"%s\"\n", *p); - } - } -#endif - -#if 1 - switch(option) { - case 'n': { - struct nwent ne; - int i; - - if (strchr(argv[1], ':') != NULL) { - if (irp_unmarshall_nw(&ne, argv[1]) != 0) { - printf("Unmarhsalling failed\n"); - exit(1); - } - - printf("Name: \"%s\"\n", ne.n_name); - printf("Aliases:"); - for (i = 0 ; ne.n_aliases[i] != NULL ; i++) - printf("\n\t\"%s\"", ne.n_aliases[i]); - printf("\nAddrtype: %s\n", ADDR_T_STR(ne.n_addrtype)); - inet_net_ntop(ne.n_addrtype, ne.n_addr, ne.n_length, - buffer, sizeof buffer); - printf("Net: \"%s\"\n", buffer); - *((long*)ne.n_addr) = htonl(*((long*)ne.n_addr)); - inet_net_ntop(ne.n_addrtype, ne.n_addr, ne.n_length, - buffer, sizeof buffer); - printf("Corrected Net: \"%s\"\n", buffer); - } else { - struct netent *np1 = getnetbyname(argv[1]); - ne.n_name = np1->n_name; - ne.n_aliases = np1->n_aliases; - ne.n_addrtype = np1->n_addrtype; - ne.n_addr = &np1->n_net; - ne.n_length = (IN_CLASSA(np1->n_net) ? - 8 : - (IN_CLASSB(np1->n_net) ? - 16 : - (IN_CLASSC(np1->n_net) ? - 24 : -1))); - np1->n_net = htonl(np1->n_net); - if (irp_marshall_nw(&ne, &b, &len) != 0) { - printf("Marshalling failed\n"); - } - printf("%s\n", b); - } - break; - } - - - case 'r': { - char **hosts, **users, **domains; - size_t entries; - int i; - char *buff; - size_t size; - char *ngname; - - if (strchr(argv[1], '(') != NULL) { - if (irp_unmarshall_ng(&ngname, &entries, - &hosts, &users, &domains, - argv[1]) != 0) { - printf("unmarshall failed\n"); - exit(1); - } - -#define STRVAL(x) (x == NULL ? "*" : x) - - printf("%s {\n", ngname); - for (i = 0 ; i < entries ; i++) - printf("\t\"%s\" : \"%s\" : \"%s\"\n", - STRVAL(hosts[i]), - STRVAL(users[i]), - STRVAL(domains[i])); - printf("}\n\n\n"); - - - irp_marshall_ng_start(ngname, NULL, &size); - for (i = 0 ; i < entries ; i++) - irp_marshall_ng_next(hosts[i], users[i], - domains[i], NULL, &size); - irp_marshall_ng_end(NULL, &size); - - buff = malloc(size); - - irp_marshall_ng_start(ngname, buff, &size); - for (i = 0 ; i < entries ; i++) { - if (irp_marshall_ng_next(hosts[i], users[i], - domains[i], buff, - &size) != 0) - printf("next marshalling failed.\n"); - } - irp_marshall_ng_end(buff, &size); - - if (strcmp_nws(argv[1], buff) != 0) { - printf("compare failed:\n\t%s\n\t%s\n", - buffer, argv[1]); - } else { - printf("compare ok\n"); - } - } else { - char *h, *u, *d, *buff; - size_t size; - - /* run through two times. First to figure out how - much of a buffer we need. Second to do the - actual marshalling */ - - setnetgrent(argv[1]); - irp_marshall_ng_start(argv[1], NULL, &size); - while (getnetgrent(&h, &u, &d) == 1) - irp_marshall_ng_next(h, u, d, NULL, &size); - irp_marshall_ng_end(NULL, &size); - endnetgrent(argv[1]); - - buff = malloc(size); - - setnetgrent(argv[1]); - if (irp_marshall_ng_start(argv[1], buff, &size) != 0) - printf("Marshalling start failed\n"); - - while (getnetgrent(&h, &u, &d) == 1) { - if (irp_marshall_ng_next(h, u, d, buff, &size) - != 0) { - printf("Marshalling failed\n"); - } - } - - irp_marshall_ng_end(buff, &size); - endnetgrent(); - - printf("success: %s\n", buff); - } - break; - } - - - - case 'h': { - struct hostent he, *hp; - int i; - - - if (strchr(argv[1], '@') != NULL) { - if (irp_unmarshall_ho(&he, argv[1]) != 0) { - printf("unmarshall failed\n"); - exit(1); - } - - printf("Host: \"%s\"\nAliases:", he.h_name); - for (i = 0 ; he.h_aliases[i] != NULL ; i++) - printf("\n\t\t\"%s\"", he.h_aliases[i]); - printf("\nAddr Type: \"%s\"\n", - ADDR_T_STR(he.h_addrtype)); - printf("Length: %d\nAddresses:", he.h_length); - for (i = 0 ; he.h_addr_list[i] != 0 ; i++) { - inet_ntop(he.h_addrtype, he.h_addr_list[i], - buffer, sizeof buffer); - printf("\n\t\"%s\"\n", buffer); - } - printf("\n\n"); - - irp_marshall_ho(&he, &b, &len); - if (strcmp(argv[1], buffer) != 0) { - printf("compare failed:\n\t\"%s\"\n\t\"%s\"\n", - buffer, argv[1]); - } else { - printf("compare ok\n"); - } - } else { - if ((hp = gethostbyname(argv[1])) == NULL) { - perror("gethostbyname"); - printf("\"%s\"\n", argv[1]); - exit(1); - } - - if (irp_marshall_ho(hp, &b, &len) != 0) { - printf("irp_marshall_ho failed\n"); - exit(1); - } - - printf("success: \"%s\"\n", buffer); - } - break; - } - - - case 's': { - struct servent *sv; - struct servent sv1; - - if (strchr(argv[1], ':') != NULL) { - sv = &sv1; - memset(sv, 0xef, sizeof (struct servent)); - if (irp_unmarshall_sv(sv, argv[1]) != 0) { - printf("unmarshall failed\n"); - - } - - irp_marshall_sv(sv, &b, &len); - if (strcmp(argv[1], buffer) != 0) { - printf("compare failed:\n\t\"%s\"\n\t\"%s\"\n", - buffer, argv[1]); - } else { - printf("compare ok\n"); - } - } else { - if ((sv = getservbyname(argv[1], argv[2])) == NULL) { - perror("getservent"); - exit(1); - } - - if (irp_marshall_sv(sv, &b, &len) != 0) { - printf("irp_marshall_sv failed\n"); - exit(1); - } - - printf("success: \"%s\"\n", buffer); - } - break; - } - - case 'g': { - struct group *gr; - struct group gr1; - - if (strchr(argv[1], ':') != NULL) { - gr = &gr1; - memset(gr, 0xef, sizeof (struct group)); - if (irp_unmarshall_gr(gr, argv[1]) != 0) { - printf("unmarshall failed\n"); - - } - - irp_marshall_gr(gr, &b, &len); - if (strcmp(argv[1], buffer) != 0) { - printf("compare failed:\n\t\"%s\"\n\t\"%s\"\n", - buffer, argv[1]); - } else { - printf("compare ok\n"); - } - } else { - if ((gr = getgrnam(argv[1])) == NULL) { - perror("getgrnam"); - exit(1); - } - - if (irp_marshall_gr(gr, &b, &len) != 0) { - printf("irp_marshall_gr failed\n"); - exit(1); - } - - printf("success: \"%s\"\n", buffer); - } - break; - } - - - case 'p': { - struct passwd *pw; - struct passwd pw1; - - if (strchr(argv[1], ':') != NULL) { - pw = &pw1; - memset(pw, 0xef, sizeof (*pw)); - if (irp_unmarshall_pw(pw, argv[1]) != 0) { - printf("unmarshall failed\n"); - exit(1); - } - - printf("User: \"%s\"\nPasswd: \"%s\"\nUid: %ld\nGid: %ld\n", - pw->pw_name, pw->pw_passwd, (long)pw->pw_uid, - (long)pw->pw_gid); - printf("Class: \"%s\"\nChange: %ld\nGecos: \"%s\"\n", - pw->pw_class, (long)pw->pw_change, pw->pw_gecos); - printf("Shell: \"%s\"\nDirectory: \"%s\"\n", - pw->pw_shell, pw->pw_dir); - - pw = getpwnam(pw->pw_name); - irp_marshall_pw(pw, &b, &len); - if (strcmp(argv[1], buffer) != 0) { - printf("compare failed:\n\t\"%s\"\n\t\"%s\"\n", - buffer, argv[1]); - } else { - printf("compare ok\n"); - } - } else { - if ((pw = getpwnam(argv[1])) == NULL) { - perror("getpwnam"); - exit(1); - } - - if (irp_marshall_pw(pw, &b, &len) != 0) { - printf("irp_marshall_pw failed\n"); - exit(1); - } - - printf("success: \"%s\"\n", buffer); - } - break; - } - - default: - printf("Wrong option: %c\n", option); - break; - } - -#endif - - return (0); -} - -#endif - -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/irs_data.c /usr/src/lib/libc/net/irs_data.c --- /home/reed/src/isc/libbind/libbind/irs/irs_data.c 2007-08-26 22:32:26.000000000 -0500 +++ /usr/src/lib/libc/net/irs_data.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,246 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1996,1999 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#if !defined(LINT) && !defined(CODECENTER) -static const char rcsid[] = "$Id: irs_data.c,v 1.12 2007/08/27 03:32:26 marka Exp $"; -#endif - -#include "port_before.h" - -#ifndef __BIND_NOSTATIC - -#include - -#include -#include - -#include -#include -#include -#include - -#ifdef DO_PTHREADS -#include -#endif - -#include -#include - -#include "port_after.h" - -#include "irs_data.h" -#undef _res -#if !(__GLIBC__ > 2 || __GLIBC__ == 2 && __GLIBC_MINOR__ >= 3) -#undef h_errno -extern int h_errno; -#endif - -extern struct __res_state _res; - -#ifdef DO_PTHREADS -static pthread_key_t key; -static int once = 0; -#else -static struct net_data *net_data; -#endif - -void -irs_destroy(void) { -#ifndef DO_PTHREADS - if (net_data != NULL) - net_data_destroy(net_data); - net_data = NULL; -#endif -} - -void -net_data_destroy(void *p) { - struct net_data *net_data = p; - - res_ndestroy(net_data->res); - if (net_data->gr != NULL) { - (*net_data->gr->close)(net_data->gr); - net_data->gr = NULL; - } - if (net_data->pw != NULL) { - (*net_data->pw->close)(net_data->pw); - net_data->pw = NULL; - } - if (net_data->sv != NULL) { - (*net_data->sv->close)(net_data->sv); - net_data->sv = NULL; - } - if (net_data->pr != NULL) { - (*net_data->pr->close)(net_data->pr); - net_data->pr = NULL; - } - if (net_data->ho != NULL) { - (*net_data->ho->close)(net_data->ho); - net_data->ho = NULL; - } - if (net_data->nw != NULL) { - (*net_data->nw->close)(net_data->nw); - net_data->nw = NULL; - } - if (net_data->ng != NULL) { - (*net_data->ng->close)(net_data->ng); - net_data->ng = NULL; - } - if (net_data->ho_data != NULL) { - free(net_data->ho_data); - net_data->ho_data = NULL; - } - if (net_data->nw_data != NULL) { - free(net_data->nw_data); - net_data->nw_data = NULL; - } - - (*net_data->irs->close)(net_data->irs); - memput(net_data, sizeof *net_data); -} - -/*% - * applications that need a specific config file other than - * _PATH_IRS_CONF should call net_data_init directly rather than letting - * the various wrapper functions make the first call. - brister - */ - -struct net_data * -net_data_init(const char *conf_file) { -#ifdef DO_PTHREADS -#ifndef LIBBIND_MUTEX_INITIALIZER -#define LIBBIND_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER -#endif - static pthread_mutex_t keylock = LIBBIND_MUTEX_INITIALIZER; - struct net_data *net_data; - - if (!once) { - if (pthread_mutex_lock(&keylock) != 0) - return (NULL); - if (!once) { - if (pthread_key_create(&key, net_data_destroy) != 0) { - (void)pthread_mutex_unlock(&keylock); - return (NULL); - } - once = 1; - } - if (pthread_mutex_unlock(&keylock) != 0) - return (NULL); - } - net_data = pthread_getspecific(key); -#endif - - if (net_data == NULL) { - net_data = net_data_create(conf_file); - if (net_data == NULL) - return (NULL); -#ifdef DO_PTHREADS - if (pthread_setspecific(key, net_data) != 0) { - net_data_destroy(net_data); - return (NULL); - } -#endif - } - - return (net_data); -} - -struct net_data * -net_data_create(const char *conf_file) { - struct net_data *net_data; - - net_data = memget(sizeof (struct net_data)); - if (net_data == NULL) - return (NULL); - memset(net_data, 0, sizeof (struct net_data)); - - if ((net_data->irs = irs_gen_acc("", conf_file)) == NULL) { - memput(net_data, sizeof (struct net_data)); - return (NULL); - } -#ifndef DO_PTHREADS - (*net_data->irs->res_set)(net_data->irs, &_res, NULL); -#endif - - net_data->res = (*net_data->irs->res_get)(net_data->irs); - if (net_data->res == NULL) { - (*net_data->irs->close)(net_data->irs); - memput(net_data, sizeof (struct net_data)); - return (NULL); - } - - if ((net_data->res->options & RES_INIT) == 0U && - res_ninit(net_data->res) == -1) { - (*net_data->irs->close)(net_data->irs); - memput(net_data, sizeof (struct net_data)); - return (NULL); - } - - return (net_data); -} - -void -net_data_minimize(struct net_data *net_data) { - res_nclose(net_data->res); -} - -#ifdef _REENTRANT -struct __res_state * -__res_state(void) { - /* NULL param here means use the default config file. */ - struct net_data *net_data = net_data_init(NULL); - if (net_data && net_data->res) - return (net_data->res); - - return (&_res); -} -#else -#ifdef __linux -struct __res_state * -__res_state(void) { - return (&_res); -} -#endif -#endif - -int * -__h_errno(void) { - /* NULL param here means use the default config file. */ - struct net_data *net_data = net_data_init(NULL); - if (net_data && net_data->res) - return (&net_data->res->res_h_errno); -#if !(__GLIBC__ > 2 || __GLIBC__ == 2 && __GLIBC_MINOR__ >= 3) - return(&_res.res_h_errno); -#else - return (&h_errno); -#endif -} - -void -__h_errno_set(struct __res_state *res, int err) { - - -#if (__GLIBC__ > 2 || __GLIBC__ == 2 && __GLIBC_MINOR__ >= 3) - res->res_h_errno = err; -#else - h_errno = res->res_h_errno = err; -#endif -} - -#endif /*__BIND_NOSTATIC*/ - -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/irs_data.h /usr/src/lib/libc/net/irs_data.h --- /home/reed/src/isc/libbind/libbind/irs/irs_data.h 2005-04-26 23:56:30.000000000 -0500 +++ /usr/src/lib/libc/net/irs_data.h 1969-12-31 18:00:00.000000000 -0600 @@ -1,63 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1996,1999 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -/* - * $Id: irs_data.h,v 1.3 2005/04/27 04:56:30 sra Exp $ - */ - -#ifndef __BIND_NOSTATIC - -#define net_data_init __net_data_init - -struct net_data { - struct irs_acc * irs; - - struct irs_gr * gr; - struct irs_pw * pw; - struct irs_sv * sv; - struct irs_pr * pr; - struct irs_ho * ho; - struct irs_nw * nw; - struct irs_ng * ng; - - struct group * gr_last; - struct passwd * pw_last; - struct servent * sv_last; - struct protoent * pr_last; - struct netent * nw_last; /*%< should have been ne_last */ - struct nwent * nww_last; - struct hostent * ho_last; - - unsigned int gr_stayopen :1; - unsigned int pw_stayopen :1; - unsigned int sv_stayopen :1; - unsigned int pr_stayopen :1; - unsigned int ho_stayopen :1; - unsigned int nw_stayopen :1; - - void * nw_data; - void * ho_data; - - struct __res_state * res; /*%< for gethostent.c */ -}; - -extern struct net_data * net_data_init(const char *conf_file); -extern void net_data_minimize(struct net_data *); - -#endif /*__BIND_NOSTATIC*/ - -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/irs_p.h /usr/src/lib/libc/net/irs_p.h --- /home/reed/src/isc/libbind/libbind/irs/irs_p.h 2005-04-26 23:56:30.000000000 -0500 +++ /usr/src/lib/libc/net/irs_p.h 1969-12-31 18:00:00.000000000 -0600 @@ -1,51 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1996,1999 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -/* - * $Id: irs_p.h,v 1.3 2005/04/27 04:56:30 sra Exp $ - */ - -#ifndef _IRS_P_H_INCLUDED -#define _IRS_P_H_INCLUDED - -#include - -#include "pathnames.h" - -#define IRS_SV_MAXALIASES 35 - -struct lcl_sv { - FILE * fp; - char line[BUFSIZ+1]; - struct servent serv; - char * serv_aliases[IRS_SV_MAXALIASES]; -}; - -#define irs_nul_ng __irs_nul_ng -#define map_v4v6_address __map_v4v6_address -#define make_group_list __make_group_list -#define irs_lclsv_fnxt __irs_lclsv_fnxt - -extern void map_v4v6_address(const char *src, char *dst); -extern int make_group_list(struct irs_gr *, const char *, - gid_t, gid_t *, int *); -extern struct irs_ng * irs_nul_ng(struct irs_acc *); -extern struct servent * irs_lclsv_fnxt(struct lcl_sv *); - -#endif - -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/iso_addr.c /usr/src/lib/libc/net/iso_addr.c --- /home/reed/src/isc/libbind/libbind/irs/iso_addr.c 1969-12-31 18:00:00.000000000 -0600 +++ /usr/src/lib/libc/net/iso_addr.c 2013-06-05 09:26:13.000000000 -0500 @@ -0,0 +1,138 @@ +/* $NetBSD: iso_addr.c,v 1.15 2013/03/01 18:25:16 joerg Exp $ */ + +/* + * Copyright (c) 1989, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)iso_addr.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: iso_addr.c,v 1.15 2013/03/01 18:25:16 joerg Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include + +#include +#include + +struct iso_addr { + uint8_t isoa_len; /* length (in bytes) */ + char isoa_genaddr[20]; /* general opaque address */ +}; + +__BEGIN_DECLS +struct iso_addr *iso_addr(const char *); +char *iso_ntoa(const struct iso_addr *); +__END_DECLS + +/* States*/ +#define VIRGIN 0 +#define GOTONE 1 +#define GOTTWO 2 +/* Inputs */ +#define DIGIT (4*0) +#define END (4*1) +#define DELIM (4*2) + +struct iso_addr * +iso_addr(const char *addr) +{ + static struct iso_addr out_addr; + register char *cp = out_addr.isoa_genaddr; + char *cplim = cp + sizeof(out_addr.isoa_genaddr); + register int byte = 0, state = VIRGIN; + register int newaddr = 0; /* pacify gcc */ + + _DIAGASSERT(addr != NULL); + + (void)memset(&out_addr, 0, sizeof (out_addr)); + do { + if ((*addr >= '0') && (*addr <= '9')) { + newaddr = *addr - '0'; + } else if ((*addr >= 'a') && (*addr <= 'f')) { + newaddr = *addr - 'a' + 10; + } else if ((*addr >= 'A') && (*addr <= 'F')) { + newaddr = *addr - 'A' + 10; + } else if (*addr == 0) + state |= END; + else + state |= DELIM; + addr++; + switch (state /* | INPUT */) { + case GOTTWO | DIGIT: + *cp++ = byte; /*FALLTHROUGH*/ + case VIRGIN | DIGIT: + state = GOTONE; byte = newaddr; continue; + case GOTONE | DIGIT: + state = GOTTWO; byte = newaddr + (byte << 4); continue; + default: /* | DELIM */ + state = VIRGIN; *cp++ = byte; byte = 0; continue; + case GOTONE | END: + case GOTTWO | END: + *cp++ = byte; /* FALLTHROUGH */ + case VIRGIN | END: + break; + } + break; + } while (cp < cplim); + _DIAGASSERT(__type_fit(uint8_t, cp - out_addr.isoa_genaddr)); + out_addr.isoa_len = (uint8_t)(cp - out_addr.isoa_genaddr); + return (&out_addr); +} + +static const char hexlist[16] = "0123456789abcdef"; + +char * +iso_ntoa(const struct iso_addr *isoa) +{ + static char obuf[64]; + char *out = obuf; + size_t i; + const u_char *in = (const u_char *)isoa->isoa_genaddr; + const u_char *inlim = in + isoa->isoa_len; + + _DIAGASSERT(isoa != NULL); + + out[1] = 0; + while (in < inlim) { + i = *in++; + *out++ = '.'; + if (i > 0xf) { + out[1] = hexlist[i & 0xf]; + i >>= 4; + out[0] = hexlist[i]; + out += 2; + } else + *out++ = hexlist[i]; + } + *out = 0; + return(obuf + 1); +} diff -pruN /home/reed/src/isc/libbind/libbind/irs/lcl.c /usr/src/lib/libc/net/lcl.c --- /home/reed/src/isc/libbind/libbind/irs/lcl.c 2005-04-26 23:56:30.000000000 -0500 +++ /usr/src/lib/libc/net/lcl.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,142 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1996-1999 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#if !defined(LINT) && !defined(CODECENTER) -static const char rcsid[] = "$Id: lcl.c,v 1.4 2005/04/27 04:56:30 sra Exp $"; -#endif - -/* Imports */ - -#include "port_before.h" - -#include -#include -#include - -#include -#include -#include -#include - -#include - -#include - -#include "port_after.h" - -#include "irs_p.h" -#include "lcl_p.h" - -/* Forward. */ - -static void lcl_close(struct irs_acc *); -static struct __res_state * lcl_res_get(struct irs_acc *); -static void lcl_res_set(struct irs_acc *, struct __res_state *, - void (*)(void *)); - -/* Public */ - -struct irs_acc * -irs_lcl_acc(const char *options) { - struct irs_acc *acc; - struct lcl_p *lcl; - - UNUSED(options); - - if (!(acc = memget(sizeof *acc))) { - errno = ENOMEM; - return (NULL); - } - memset(acc, 0x5e, sizeof *acc); - if (!(lcl = memget(sizeof *lcl))) { - errno = ENOMEM; - free(acc); - return (NULL); - } - memset(lcl, 0x5e, sizeof *lcl); - lcl->res = NULL; - lcl->free_res = NULL; - acc->private = lcl; -#ifdef WANT_IRS_GR - acc->gr_map = irs_lcl_gr; -#else - acc->gr_map = NULL; -#endif -#ifdef WANT_IRS_PW - acc->pw_map = irs_lcl_pw; -#else - acc->pw_map = NULL; -#endif - acc->sv_map = irs_lcl_sv; - acc->pr_map = irs_lcl_pr; - acc->ho_map = irs_lcl_ho; - acc->nw_map = irs_lcl_nw; - acc->ng_map = irs_lcl_ng; - acc->res_get = lcl_res_get; - acc->res_set = lcl_res_set; - acc->close = lcl_close; - return (acc); -} - -/* Methods */ -static struct __res_state * -lcl_res_get(struct irs_acc *this) { - struct lcl_p *lcl = (struct lcl_p *)this->private; - - if (lcl->res == NULL) { - struct __res_state *res; - res = (struct __res_state *)malloc(sizeof *res); - if (res == NULL) - return (NULL); - memset(res, 0, sizeof *res); - lcl_res_set(this, res, free); - } - - if ((lcl->res->options & RES_INIT) == 0U && - res_ninit(lcl->res) < 0) - return (NULL); - - return (lcl->res); -} - -static void -lcl_res_set(struct irs_acc *this, struct __res_state *res, - void (*free_res)(void *)) { - struct lcl_p *lcl = (struct lcl_p *)this->private; - - if (lcl->res && lcl->free_res) { - res_nclose(lcl->res); - (*lcl->free_res)(lcl->res); - } - - lcl->res = res; - lcl->free_res = free_res; -} - -static void -lcl_close(struct irs_acc *this) { - struct lcl_p *lcl = (struct lcl_p *)this->private; - - if (lcl) { - if (lcl->free_res) - (*lcl->free_res)(lcl->res); - memput(lcl, sizeof *lcl); - } - memput(this, sizeof *this); -} - -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/lcl_gr.c /usr/src/lib/libc/net/lcl_gr.c --- /home/reed/src/isc/libbind/libbind/irs/lcl_gr.c 2013-06-05 09:33:54.000000000 -0500 +++ /usr/src/lib/libc/net/lcl_gr.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,350 +0,0 @@ -/* - * Copyright (c) 1989, 1993, 1995 - * The Regents of the University of California. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Portions Copyright (c) 1996-1999 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#if defined(LIBC_SCCS) && !defined(lint) -static const char rcsid[] = "$Id: lcl_gr.c,v 1.4 2013-01-06 23:14:52 marka Exp $"; -/* from getgrent.c 8.2 (Berkeley) 3/21/94"; */ -/* from BSDI Id: getgrent.c,v 2.8 1996/05/28 18:15:14 bostic Exp $ */ -#endif /* LIBC_SCCS and not lint */ - -/* extern */ - -#include "port_before.h" - -#ifndef WANT_IRS_PW -static int __bind_irs_gr_unneeded; -#else - -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include "irs_p.h" -#include "lcl_p.h" -#include "irp_p.h" - -#include "port_after.h" - - -/* Types. */ - -struct pvt { - FILE * fp; - /*%< - * Need space to store the entries read from the group file. - * The members list also needs space per member, and the - * strings making up the user names must be allocated - * somewhere. Rather than doing lots of small allocations, - * we keep one buffer and resize it as needed. - */ - struct group group; - size_t nmemb; /*%< Malloc'd max index of gr_mem[]. */ - char * membuf; - size_t membufsize; -}; - -/* Forward. */ - -static void gr_close(struct irs_gr *); -static struct group * gr_next(struct irs_gr *); -static struct group * gr_byname(struct irs_gr *, const char *); -static struct group * gr_bygid(struct irs_gr *, gid_t); -static void gr_rewind(struct irs_gr *); -static void gr_minimize(struct irs_gr *); - -static int grstart(struct pvt *); -static char * grnext(struct pvt *); -static struct group * grscan(struct irs_gr *, int, gid_t, const char *); - -/* Portability. */ - -#ifndef SEEK_SET -# define SEEK_SET 0 -#endif - -/* Public. */ - -struct irs_gr * -irs_lcl_gr(struct irs_acc *this) { - struct irs_gr *gr; - struct pvt *pvt; - - UNUSED(this); - - if (!(gr = memget(sizeof *gr))) { - errno = ENOMEM; - return (NULL); - } - memset(gr, 0x5e, sizeof *gr); - if (!(pvt = memget(sizeof *pvt))) { - memput(gr, sizeof *gr); - errno = ENOMEM; - return (NULL); - } - memset(pvt, 0, sizeof *pvt); - gr->private = pvt; - gr->close = gr_close; - gr->next = gr_next; - gr->byname = gr_byname; - gr->bygid = gr_bygid; - gr->rewind = gr_rewind; - gr->list = make_group_list; - gr->minimize = gr_minimize; - gr->res_get = NULL; - gr->res_set = NULL; - return (gr); -} - -/* Methods. */ - -static void -gr_close(struct irs_gr *this) { - struct pvt *pvt = (struct pvt *)this->private; - - if (pvt->fp) - (void)fclose(pvt->fp); - if (pvt->group.gr_mem) - free(pvt->group.gr_mem); - if (pvt->membuf) - free(pvt->membuf); - memput(pvt, sizeof *pvt); - memput(this, sizeof *this); -} - -static struct group * -gr_next(struct irs_gr *this) { - struct pvt *pvt = (struct pvt *)this->private; - - if (!pvt->fp && !grstart(pvt)) - return (NULL); - return (grscan(this, 0, 0, NULL)); -} - -static struct group * -gr_byname(struct irs_gr *this, const char *name) { - if (!grstart((struct pvt *)this->private)) - return (NULL); - return (grscan(this, 1, 0, name)); -} - -static struct group * -gr_bygid(struct irs_gr *this, gid_t gid) { - if (!grstart((struct pvt *)this->private)) - return (NULL); - return (grscan(this, 1, gid, NULL)); -} - -static void -gr_rewind(struct irs_gr *this) { - (void) grstart((struct pvt *)this->private); -} - -static void -gr_minimize(struct irs_gr *this) { - struct pvt *pvt = (struct pvt *)this->private; - - if (pvt->fp != NULL) { - (void)fclose(pvt->fp); - pvt->fp = NULL; - } -} - -/* Private. */ - -static int -grstart(struct pvt *pvt) { - if (pvt->fp) { - if (fseek(pvt->fp, 0L, SEEK_SET) == 0) - return (1); - (void)fclose(pvt->fp); - } - if (!(pvt->fp = fopen(_PATH_GROUP, "r"))) - return (0); - if (fcntl(fileno(pvt->fp), F_SETFD, 1) < 0) { - fclose(pvt->fp); - return (0); - } - return (1); -} - -#define INITIAL_NMEMB 30 /*%< about 120 bytes */ -#define INITIAL_BUFSIZ (INITIAL_NMEMB * 8) /*%< about 240 bytes */ -static char * -grnext(struct pvt *pvt) { - char *w, *e; - int ch; - - /* Make sure we have a buffer. */ - if (pvt->membuf == NULL) { - pvt->membuf = malloc(INITIAL_BUFSIZ); - if (pvt->membuf == NULL) { - enomem: - errno = ENOMEM; - return (NULL); - } - pvt->membufsize = INITIAL_BUFSIZ; - } - - /* Read until EOF or EOL. */ - w = pvt->membuf; - e = pvt->membuf + pvt->membufsize; - while ((ch = fgetc(pvt->fp)) != EOF && ch != '\n') { - /* Make sure we have room for this character and a \0. */ - if (w + 1 == e) { - size_t o = w - pvt->membuf; - size_t n = pvt->membufsize * 2; - char *t = realloc(pvt->membuf, n); - - if (t == NULL) - goto enomem; - pvt->membuf = t; - pvt->membufsize = n; - w = pvt->membuf + o; - e = pvt->membuf + pvt->membufsize; - } - /* Store it. */ - *w++ = (char)ch; - } - - /* Hitting EOF on the first character really does mean EOF. */ - if (w == pvt->membuf && ch == EOF) { - errno = ENOENT; - return (NULL); - } - - /* Last line of /etc/group need not end with \n; we don't care. */ - *w = '\0'; - return (pvt->membuf); -} - -static struct group * -grscan(struct irs_gr *this, int search, gid_t gid, const char *name) { - struct pvt *pvt = (struct pvt *)this->private; - size_t n; - char *bp, **m, *p; - - /* Read lines until we find one that matches our search criteria. */ - for (;;) { - if ((bp = grnext(pvt)) == NULL) - return (NULL); - - /* Optimize the usual case of searching for a name. */ - pvt->group.gr_name = strsep(&bp, ":"); - if (search && name != NULL && - strcmp(pvt->group.gr_name, name) != 0) - continue; - if (bp == NULL || *bp == '\0') - goto corrupt; - - /* Skip past the password field. */ - pvt->group.gr_passwd = strsep(&bp, ":"); - if (bp == NULL || *bp == '\0') - goto corrupt; - - /* Checking for a gid. */ - if ((p = strsep(&bp, ":")) == NULL) - continue; - /* - * Unlike the tests above, the test below is supposed to be - * testing 'p' and not 'bp', in case you think it's a typo. - */ - if (p == NULL || *p == '\0') { - corrupt: - /* warning: corrupted %s file!", _PATH_GROUP */ - continue; - } - pvt->group.gr_gid = atoi(p); - if (search && name == NULL && (gid_t)pvt->group.gr_gid != gid) - continue; - - /* We want this record. */ - break; - } - - /* - * Count commas to find out how many members there might be. - * Note that commas separate, so if there is one comma there - * can be two members (group:*:id:user1,user2). Add another - * to account for the NULL terminator. As above, allocate - * largest of INITIAL_NMEMB, or 2*n. - */ - n = 1; - if (bp != NULL) - for (n = 2, p = bp; (p = strpbrk(p, ", ")) != NULL; ++n) - p += strspn(p, ", "); - if (n > pvt->nmemb || pvt->group.gr_mem == NULL) { - if ((n *= 2) < INITIAL_NMEMB) - n = INITIAL_NMEMB; - if ((m = realloc(pvt->group.gr_mem, n * sizeof *m)) == NULL) - return (NULL); - pvt->group.gr_mem = m; - pvt->nmemb = n; - } - - /* Set the name pointers. */ - for (m = pvt->group.gr_mem; (p = strsep(&bp, ", ")) != NULL;) - if (p[0] != '\0') - *m++ = p; - *m = NULL; - - return (&pvt->group); -} - -#endif /* WANT_IRS_GR */ -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/lcl_ho.c /usr/src/lib/libc/net/lcl_ho.c --- /home/reed/src/isc/libbind/libbind/irs/lcl_ho.c 2013-06-05 09:33:54.000000000 -0500 +++ /usr/src/lib/libc/net/lcl_ho.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,574 +0,0 @@ -/* - * Copyright (c) 1985, 1988, 1993 - * The Regents of the University of California. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Portions Copyright (c) 1996-1999 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -/* from gethostnamadr.c 8.1 (Berkeley) 6/4/93 */ -/* BIND Id: gethnamaddr.c,v 8.15 1996/05/22 04:56:30 vixie Exp $ */ - -#if defined(LIBC_SCCS) && !defined(lint) -static const char rcsid[] = "$Id: lcl_ho.c,v 1.6 2013-01-06 23:14:52 marka Exp $"; -#endif /* LIBC_SCCS and not lint */ - -/* Imports. */ - -#include "port_before.h" - -#include -#include -#include - -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include "port_after.h" - -#include "irs_p.h" -#include "dns_p.h" -#include "lcl_p.h" - -#ifdef SPRINTF_CHAR -# define SPRINTF(x) strlen(sprintf/**/x) -#else -# define SPRINTF(x) sprintf x -#endif - -/* Definitions. */ - -#define MAXALIASES 35 -#define MAXADDRS 35 -#define Max(a,b) ((a) > (b) ? (a) : (b)) - -#if PACKETSZ > 1024 -#define MAXPACKET PACKETSZ -#else -#define MAXPACKET 1024 -#endif - -struct pvt { - FILE * fp; - struct hostent host; - char * h_addr_ptrs[MAXADDRS + 1]; - char * host_aliases[MAXALIASES]; - char hostbuf[8*1024]; - u_char host_addr[16]; /*%< IPv4 or IPv6 */ - struct __res_state *res; - void (*free_res)(void *); -}; - -typedef union { - int32_t al; - char ac; -} align; - -static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff }; -static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 }; - -/* Forward. */ - -static void ho_close(struct irs_ho *this); -static struct hostent * ho_byname(struct irs_ho *this, const char *name); -static struct hostent * ho_byname2(struct irs_ho *this, const char *name, - int af); -static struct hostent * ho_byaddr(struct irs_ho *this, const void *addr, - int len, int af); -static struct hostent * ho_next(struct irs_ho *this); -static void ho_rewind(struct irs_ho *this); -static void ho_minimize(struct irs_ho *this); -static struct __res_state * ho_res_get(struct irs_ho *this); -static void ho_res_set(struct irs_ho *this, - struct __res_state *res, - void (*free_res)(void *)); -static struct addrinfo * ho_addrinfo(struct irs_ho *this, const char *name, - const struct addrinfo *pai); - -static size_t ns_namelen(const char *); -static int init(struct irs_ho *this); - -/* Portability. */ - -#ifndef SEEK_SET -# define SEEK_SET 0 -#endif - -/* Public. */ - -struct irs_ho * -irs_lcl_ho(struct irs_acc *this) { - struct irs_ho *ho; - struct pvt *pvt; - - UNUSED(this); - - if (!(pvt = memget(sizeof *pvt))) { - errno = ENOMEM; - return (NULL); - } - memset(pvt, 0, sizeof *pvt); - if (!(ho = memget(sizeof *ho))) { - memput(pvt, sizeof *pvt); - errno = ENOMEM; - return (NULL); - } - memset(ho, 0x5e, sizeof *ho); - ho->private = pvt; - ho->close = ho_close; - ho->byname = ho_byname; - ho->byname2 = ho_byname2; - ho->byaddr = ho_byaddr; - ho->next = ho_next; - ho->rewind = ho_rewind; - ho->minimize = ho_minimize; - ho->res_get = ho_res_get; - ho->res_set = ho_res_set; - ho->addrinfo = ho_addrinfo; - return (ho); -} - -/* Methods. */ - -static void -ho_close(struct irs_ho *this) { - struct pvt *pvt = (struct pvt *)this->private; - - ho_minimize(this); - if (pvt->fp) - (void) fclose(pvt->fp); - if (pvt->res && pvt->free_res) - (*pvt->free_res)(pvt->res); - memput(pvt, sizeof *pvt); - memput(this, sizeof *this); -} - -static struct hostent * -ho_byname(struct irs_ho *this, const char *name) { - struct pvt *pvt = (struct pvt *)this->private; - struct hostent *hp; - - if (init(this) == -1) - return (NULL); - - if (pvt->res->options & RES_USE_INET6) { - hp = ho_byname2(this, name, AF_INET6); - if (hp) - return (hp); - } - return (ho_byname2(this, name, AF_INET)); -} - -static struct hostent * -ho_byname2(struct irs_ho *this, const char *name, int af) { - struct pvt *pvt = (struct pvt *)this->private; - struct hostent *hp; - char **hap; - size_t n; - - if (init(this) == -1) - return (NULL); - - ho_rewind(this); - n = ns_namelen(name); - while ((hp = ho_next(this)) != NULL) { - size_t nn; - - if (hp->h_addrtype != af) - continue; - nn = ns_namelen(hp->h_name); - if (strncasecmp(hp->h_name, name, Max(n, nn)) == 0) - goto found; - for (hap = hp->h_aliases; *hap; hap++) { - nn = ns_namelen(*hap); - if (strncasecmp(*hap, name, Max(n, nn)) == 0) - goto found; - } - } - found: - if (!hp) { - RES_SET_H_ERRNO(pvt->res, HOST_NOT_FOUND); - return (NULL); - } - RES_SET_H_ERRNO(pvt->res, NETDB_SUCCESS); - return (hp); -} - -static struct hostent * -ho_byaddr(struct irs_ho *this, const void *addr, int len, int af) { - struct pvt *pvt = (struct pvt *)this->private; - const u_char *uaddr = addr; - struct hostent *hp; - int size; - - if (init(this) == -1) - return (NULL); - - if (af == AF_INET6 && len == IN6ADDRSZ && - (!memcmp(uaddr, mapped, sizeof mapped) || - !memcmp(uaddr, tunnelled, sizeof tunnelled))) { - /* Unmap. */ - addr = (const u_char *)addr + sizeof mapped; - uaddr += sizeof mapped; - af = AF_INET; - len = INADDRSZ; - } - switch (af) { - case AF_INET: - size = INADDRSZ; - break; - case AF_INET6: - size = IN6ADDRSZ; - break; - default: - errno = EAFNOSUPPORT; - RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL); - return (NULL); - } - if (size > len) { - errno = EINVAL; - RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL); - return (NULL); - } - - /* - * Do the search. - */ - ho_rewind(this); - while ((hp = ho_next(this)) != NULL) { - char **hap; - - for (hap = hp->h_addr_list; *hap; hap++) { - const u_char *taddr = (const u_char *)*hap; - int taf = hp->h_addrtype; - int tlen = hp->h_length; - - if (taf == AF_INET6 && tlen == IN6ADDRSZ && - (!memcmp(taddr, mapped, sizeof mapped) || - !memcmp(taddr, tunnelled, sizeof tunnelled))) { - /* Unmap. */ - taddr += sizeof mapped; - taf = AF_INET; - tlen = INADDRSZ; - } - if (taf == af && tlen == len && - !memcmp(taddr, uaddr, tlen)) - goto found; - } - } - found: - if (!hp) { - RES_SET_H_ERRNO(pvt->res, HOST_NOT_FOUND); - return (NULL); - } - RES_SET_H_ERRNO(pvt->res, NETDB_SUCCESS); - return (hp); -} - -static struct hostent * -ho_next(struct irs_ho *this) { - struct pvt *pvt = (struct pvt *)this->private; - char *cp, **q, *p; - char *bufp, *ndbuf, *dbuf = NULL; - int c, af, len, bufsiz, offset; - - if (init(this) == -1) - return (NULL); - - if (!pvt->fp) - ho_rewind(this); - if (!pvt->fp) { - RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL); - return (NULL); - } - bufp = pvt->hostbuf; - bufsiz = sizeof pvt->hostbuf; - offset = 0; - again: - if (!(p = fgets(bufp + offset, bufsiz - offset, pvt->fp))) { - RES_SET_H_ERRNO(pvt->res, HOST_NOT_FOUND); - if (dbuf) - free(dbuf); - return (NULL); - } - if (!strchr(p, '\n') && !feof(pvt->fp)) { -#define GROWBUF 1024 - /* allocate space for longer line */ - if (dbuf == NULL) { - if ((ndbuf = malloc(bufsiz + GROWBUF)) != NULL) - strcpy(ndbuf, bufp); - } else - ndbuf = realloc(dbuf, bufsiz + GROWBUF); - if (ndbuf) { - dbuf = ndbuf; - bufp = dbuf; - bufsiz += GROWBUF; - offset = strlen(dbuf); - } else { - /* allocation failed; skip this long line */ - while ((c = getc(pvt->fp)) != EOF) - if (c == '\n') - break; - if (c != EOF) - ungetc(c, pvt->fp); - } - goto again; - } - - p -= offset; - offset = 0; - - if (*p == '#') - goto again; - if ((cp = strpbrk(p, "#\n")) != NULL) - *cp = '\0'; - if (!(cp = strpbrk(p, " \t"))) - goto again; - *cp++ = '\0'; - if (inet_pton(AF_INET6, p, pvt->host_addr) > 0) { - af = AF_INET6; - len = IN6ADDRSZ; - } else if (inet_aton(p, (struct in_addr *)pvt->host_addr) > 0) { - if (pvt->res->options & RES_USE_INET6) { - map_v4v6_address((char*)pvt->host_addr, - (char*)pvt->host_addr); - af = AF_INET6; - len = IN6ADDRSZ; - } else { - af = AF_INET; - len = INADDRSZ; - } - } else { - goto again; - } - pvt->h_addr_ptrs[0] = (char *)pvt->host_addr; - pvt->h_addr_ptrs[1] = NULL; - pvt->host.h_addr_list = pvt->h_addr_ptrs; - pvt->host.h_length = len; - pvt->host.h_addrtype = af; - while (*cp == ' ' || *cp == '\t') - cp++; - pvt->host.h_name = cp; - q = pvt->host.h_aliases = pvt->host_aliases; - if ((cp = strpbrk(cp, " \t")) != NULL) - *cp++ = '\0'; - while (cp && *cp) { - if (*cp == ' ' || *cp == '\t') { - cp++; - continue; - } - if (q < &pvt->host_aliases[MAXALIASES - 1]) - *q++ = cp; - if ((cp = strpbrk(cp, " \t")) != NULL) - *cp++ = '\0'; - } - *q = NULL; - if (dbuf) - free(dbuf); - RES_SET_H_ERRNO(pvt->res, NETDB_SUCCESS); - return (&pvt->host); -} - -static void -ho_rewind(struct irs_ho *this) { - struct pvt *pvt = (struct pvt *)this->private; - - if (pvt->fp) { - if (fseek(pvt->fp, 0L, SEEK_SET) == 0) - return; - (void)fclose(pvt->fp); - } - if (!(pvt->fp = fopen(_PATH_HOSTS, "r"))) - return; - if (fcntl(fileno(pvt->fp), F_SETFD, 1) < 0) { - (void)fclose(pvt->fp); - pvt->fp = NULL; - } -} - -static void -ho_minimize(struct irs_ho *this) { - struct pvt *pvt = (struct pvt *)this->private; - - if (pvt->fp != NULL) { - (void)fclose(pvt->fp); - pvt->fp = NULL; - } - if (pvt->res) - res_nclose(pvt->res); -} - -static struct __res_state * -ho_res_get(struct irs_ho *this) { - struct pvt *pvt = (struct pvt *)this->private; - - if (!pvt->res) { - struct __res_state *res; - res = (struct __res_state *)malloc(sizeof *res); - if (!res) { - errno = ENOMEM; - return (NULL); - } - memset(res, 0, sizeof *res); - ho_res_set(this, res, free); - } - - return (pvt->res); -} - -static void -ho_res_set(struct irs_ho *this, struct __res_state *res, - void (*free_res)(void *)) { - struct pvt *pvt = (struct pvt *)this->private; - - if (pvt->res && pvt->free_res) { - res_nclose(pvt->res); - (*pvt->free_res)(pvt->res); - } - - pvt->res = res; - pvt->free_res = free_res; -} - -struct lcl_res_target { - struct lcl_res_target *next; - int family; -}; - -/* XXX */ -extern struct addrinfo *hostent2addrinfo __P((struct hostent *, - const struct addrinfo *pai)); - -static struct addrinfo * -ho_addrinfo(struct irs_ho *this, const char *name, const struct addrinfo *pai) -{ - struct pvt *pvt = (struct pvt *)this->private; - struct hostent *hp; - struct lcl_res_target q, q2, *p; - struct addrinfo sentinel, *cur; - - memset(&q, 0, sizeof(q2)); - memset(&q2, 0, sizeof(q2)); - memset(&sentinel, 0, sizeof(sentinel)); - cur = &sentinel; - - switch(pai->ai_family) { - case AF_UNSPEC: /*%< INET6 then INET4 */ - q.family = AF_INET6; - q.next = &q2; - q2.family = AF_INET; - break; - case AF_INET6: - q.family = AF_INET6; - break; - case AF_INET: - q.family = AF_INET; - break; - default: - RES_SET_H_ERRNO(pvt->res, NO_RECOVERY); /*%< ??? */ - return(NULL); - } - - for (p = &q; p; p = p->next) { - struct addrinfo *ai; - - hp = (*this->byname2)(this, name, p->family); - if (hp == NULL) { - /* byname2 should've set an appropriate error */ - continue; - } - if ((hp->h_name == NULL) || (hp->h_name[0] == 0) || - (hp->h_addr_list[0] == NULL)) { - RES_SET_H_ERRNO(pvt->res, NO_RECOVERY); - continue; - } - - ai = hostent2addrinfo(hp, pai); - if (ai) { - cur->ai_next = ai; - while (cur->ai_next) - cur = cur->ai_next; - } - } - - if (sentinel.ai_next == NULL) - RES_SET_H_ERRNO(pvt->res, HOST_NOT_FOUND); - - return(sentinel.ai_next); -} - -/* Private. */ - -static size_t -ns_namelen(const char *s) { - int i; - - for (i = strlen(s); i > 0 && s[i-1] == '.'; i--) - (void)NULL; - return ((size_t) i); -} - -static int -init(struct irs_ho *this) { - struct pvt *pvt = (struct pvt *)this->private; - - if (!pvt->res && !ho_res_get(this)) - return (-1); - if (((pvt->res->options & RES_INIT) == 0U) && - res_ninit(pvt->res) == -1) - return (-1); - return (0); -} - -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/lcl_ng.c /usr/src/lib/libc/net/lcl_ng.c --- /home/reed/src/isc/libbind/libbind/irs/lcl_ng.c 2005-04-26 23:56:31.000000000 -0500 +++ /usr/src/lib/libc/net/lcl_ng.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,446 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1996-1999 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#if !defined(LINT) && !defined(CODECENTER) -static const char rcsid[] = "$Id: lcl_ng.c,v 1.3 2005/04/27 04:56:31 sra Exp $"; -#endif - -/* Imports */ - -#include "port_before.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include "port_after.h" - -#include "irs_p.h" -#include "lcl_p.h" - -/* Definitions */ - -#define NG_HOST 0 /*%< Host name */ -#define NG_USER 1 /*%< User name */ -#define NG_DOM 2 /*%< and Domain name */ -#define LINSIZ 1024 /*%< Length of netgroup file line */ -/* - * XXX Warning XXX - * This code is a hack-and-slash special. It realy needs to be - * rewritten with things like strdup, and realloc in mind. - * More reasonable data structures would not be a bad thing. - */ - -/*% - * Static Variables and functions used by setnetgrent(), getnetgrent() and - * endnetgrent(). - * - * There are two linked lists: - * \li linelist is just used by setnetgrent() to parse the net group file via. - * parse_netgrp() - * \li netgrp is the list of entries for the current netgroup - */ -struct linelist { - struct linelist *l_next; /*%< Chain ptr. */ - int l_parsed; /*%< Flag for cycles */ - char * l_groupname; /*%< Name of netgroup */ - char * l_line; /*%< Netgroup entrie(s) to be parsed */ -}; - -struct ng_old_struct { - struct ng_old_struct *ng_next; /*%< Chain ptr */ - char * ng_str[3]; /*%< Field pointers, see below */ -}; - -struct pvt { - FILE *fp; - struct linelist *linehead; - struct ng_old_struct *nextgrp; - struct { - struct ng_old_struct *gr; - char *grname; - } grouphead; -}; - -/* Forward */ - -static void ng_rewind(struct irs_ng *, const char*); -static void ng_close(struct irs_ng *); -static int ng_next(struct irs_ng *, const char **, - const char **, const char **); -static int ng_test(struct irs_ng *, const char *, - const char *, const char *, - const char *); -static void ng_minimize(struct irs_ng *); - -static int parse_netgrp(struct irs_ng *, const char*); -static struct linelist *read_for_group(struct irs_ng *, const char *); -static void freelists(struct irs_ng *); - -/* Public */ - -struct irs_ng * -irs_lcl_ng(struct irs_acc *this) { - struct irs_ng *ng; - struct pvt *pvt; - - UNUSED(this); - - if (!(ng = memget(sizeof *ng))) { - errno = ENOMEM; - return (NULL); - } - memset(ng, 0x5e, sizeof *ng); - if (!(pvt = memget(sizeof *pvt))) { - memput(ng, sizeof *ng); - errno = ENOMEM; - return (NULL); - } - memset(pvt, 0, sizeof *pvt); - ng->private = pvt; - ng->close = ng_close; - ng->next = ng_next; - ng->test = ng_test; - ng->rewind = ng_rewind; - ng->minimize = ng_minimize; - return (ng); -} - -/* Methods */ - -static void -ng_close(struct irs_ng *this) { - struct pvt *pvt = (struct pvt *)this->private; - - if (pvt->fp != NULL) - fclose(pvt->fp); - freelists(this); - memput(pvt, sizeof *pvt); - memput(this, sizeof *this); -} - -/*% - * Parse the netgroup file looking for the netgroup and build the list - * of netgrp structures. Let parse_netgrp() and read_for_group() do - * most of the work. - */ -static void -ng_rewind(struct irs_ng *this, const char *group) { - struct pvt *pvt = (struct pvt *)this->private; - - if (pvt->fp != NULL && fseek(pvt->fp, SEEK_CUR, 0L) == -1) { - fclose(pvt->fp); - pvt->fp = NULL; - } - - if (pvt->fp == NULL || pvt->grouphead.gr == NULL || - strcmp(group, pvt->grouphead.grname)) { - freelists(this); - if (pvt->fp != NULL) - fclose(pvt->fp); - pvt->fp = fopen(_PATH_NETGROUP, "r"); - if (pvt->fp != NULL) { - if (parse_netgrp(this, group)) - freelists(this); - if (!(pvt->grouphead.grname = strdup(group))) - freelists(this); - fclose(pvt->fp); - pvt->fp = NULL; - } - } - pvt->nextgrp = pvt->grouphead.gr; -} - -/*% - * Get the next netgroup off the list. - */ -static int -ng_next(struct irs_ng *this, const char **host, const char **user, - const char **domain) -{ - struct pvt *pvt = (struct pvt *)this->private; - - if (pvt->nextgrp) { - *host = pvt->nextgrp->ng_str[NG_HOST]; - *user = pvt->nextgrp->ng_str[NG_USER]; - *domain = pvt->nextgrp->ng_str[NG_DOM]; - pvt->nextgrp = pvt->nextgrp->ng_next; - return (1); - } - return (0); -} - -/*% - * Search for a match in a netgroup. - */ -static int -ng_test(struct irs_ng *this, const char *name, - const char *host, const char *user, const char *domain) -{ - const char *ng_host, *ng_user, *ng_domain; - - ng_rewind(this, name); - while (ng_next(this, &ng_host, &ng_user, &ng_domain)) - if ((host == NULL || ng_host == NULL || - !strcmp(host, ng_host)) && - (user == NULL || ng_user == NULL || - !strcmp(user, ng_user)) && - (domain == NULL || ng_domain == NULL || - !strcmp(domain, ng_domain))) { - freelists(this); - return (1); - } - freelists(this); - return (0); -} - -static void -ng_minimize(struct irs_ng *this) { - struct pvt *pvt = (struct pvt *)this->private; - - if (pvt->fp != NULL) { - (void)fclose(pvt->fp); - pvt->fp = NULL; - } -} - -/* Private */ - -/*% - * endnetgrent() - cleanup - */ -static void -freelists(struct irs_ng *this) { - struct pvt *pvt = (struct pvt *)this->private; - struct linelist *lp, *olp; - struct ng_old_struct *gp, *ogp; - - lp = pvt->linehead; - while (lp) { - olp = lp; - lp = lp->l_next; - free(olp->l_groupname); - free(olp->l_line); - free((char *)olp); - } - pvt->linehead = NULL; - if (pvt->grouphead.grname) { - free(pvt->grouphead.grname); - pvt->grouphead.grname = NULL; - } - gp = pvt->grouphead.gr; - while (gp) { - ogp = gp; - gp = gp->ng_next; - if (ogp->ng_str[NG_HOST]) - free(ogp->ng_str[NG_HOST]); - if (ogp->ng_str[NG_USER]) - free(ogp->ng_str[NG_USER]); - if (ogp->ng_str[NG_DOM]) - free(ogp->ng_str[NG_DOM]); - free((char *)ogp); - } - pvt->grouphead.gr = NULL; -} - -/*% - * Parse the netgroup file setting up the linked lists. - */ -static int -parse_netgrp(struct irs_ng *this, const char *group) { - struct pvt *pvt = (struct pvt *)this->private; - char *spos, *epos; - int len, strpos; - char *pos, *gpos; - struct ng_old_struct *grp; - struct linelist *lp = pvt->linehead; - - /* - * First, see if the line has already been read in. - */ - while (lp) { - if (!strcmp(group, lp->l_groupname)) - break; - lp = lp->l_next; - } - if (lp == NULL && - (lp = read_for_group(this, group)) == NULL) - return (1); - if (lp->l_parsed) { - /*fprintf(stderr, "Cycle in netgroup %s\n", lp->l_groupname);*/ - return (1); - } else - lp->l_parsed = 1; - pos = lp->l_line; - while (*pos != '\0') { - if (*pos == '(') { - if (!(grp = malloc(sizeof (struct ng_old_struct)))) { - freelists(this); - errno = ENOMEM; - return (1); - } - memset(grp, 0, sizeof (struct ng_old_struct)); - grp->ng_next = pvt->grouphead.gr; - pvt->grouphead.gr = grp; - pos++; - gpos = strsep(&pos, ")"); - for (strpos = 0; strpos < 3; strpos++) { - if ((spos = strsep(&gpos, ","))) { - while (*spos == ' ' || *spos == '\t') - spos++; - if ((epos = strpbrk(spos, " \t"))) { - *epos = '\0'; - len = epos - spos; - } else - len = strlen(spos); - if (len > 0) { - if(!(grp->ng_str[strpos] - = (char *) - malloc(len + 1))) { - freelists(this); - return (1); - } - memcpy(grp->ng_str[strpos], - spos, - len + 1); - } - } else - goto errout; - } - } else { - spos = strsep(&pos, ", \t"); - if (spos != NULL && parse_netgrp(this, spos)) { - freelists(this); - return (1); - } - } - if (pos == NULL) - break; - while (*pos == ' ' || *pos == ',' || *pos == '\t') - pos++; - } - return (0); - errout: - /*fprintf(stderr, "Bad netgroup %s at ..%s\n", lp->l_groupname, - spos);*/ - return (1); -} - -/*% - * Read the netgroup file and save lines until the line for the netgroup - * is found. Return 1 if eof is encountered. - */ -static struct linelist * -read_for_group(struct irs_ng *this, const char *group) { - struct pvt *pvt = (struct pvt *)this->private; - char *pos, *spos, *linep = NULL, *olinep; - int len, olen, cont; - struct linelist *lp; - char line[LINSIZ + 1]; - - while (fgets(line, LINSIZ, pvt->fp) != NULL) { - pos = line; - if (*pos == '#') - continue; - while (*pos == ' ' || *pos == '\t') - pos++; - spos = pos; - while (*pos != ' ' && *pos != '\t' && *pos != '\n' && - *pos != '\0') - pos++; - len = pos - spos; - while (*pos == ' ' || *pos == '\t') - pos++; - if (*pos != '\n' && *pos != '\0') { - if (!(lp = malloc(sizeof (*lp)))) { - freelists(this); - return (NULL); - } - lp->l_parsed = 0; - if (!(lp->l_groupname = malloc(len + 1))) { - free(lp); - freelists(this); - return (NULL); - } - memcpy(lp->l_groupname, spos, len); - *(lp->l_groupname + len) = '\0'; - len = strlen(pos); - olen = 0; - olinep = NULL; - - /* - * Loop around handling line continuations. - */ - do { - if (*(pos + len - 1) == '\n') - len--; - if (*(pos + len - 1) == '\\') { - len--; - cont = 1; - } else - cont = 0; - if (len > 0) { - if (!(linep = malloc(olen + len + 1))){ - if (olen > 0) - free(olinep); - free(lp->l_groupname); - free(lp); - freelists(this); - errno = ENOMEM; - return (NULL); - } - if (olen > 0) { - memcpy(linep, olinep, olen); - free(olinep); - } - memcpy(linep + olen, pos, len); - olen += len; - *(linep + olen) = '\0'; - olinep = linep; - } - if (cont) { - if (fgets(line, LINSIZ, pvt->fp)) { - pos = line; - len = strlen(pos); - } else - cont = 0; - } - } while (cont); - lp->l_line = linep; - lp->l_next = pvt->linehead; - pvt->linehead = lp; - - /* - * If this is the one we wanted, we are done. - */ - if (!strcmp(lp->l_groupname, group)) - return (lp); - } - } - return (NULL); -} - -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/lcl_nw.c /usr/src/lib/libc/net/lcl_nw.c --- /home/reed/src/isc/libbind/libbind/irs/lcl_nw.c 2013-06-05 09:33:54.000000000 -0500 +++ /usr/src/lib/libc/net/lcl_nw.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,369 +0,0 @@ -/* - * Copyright (c) 1989, 1993, 1995 - * The Regents of the University of California. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Portions Copyright (c) 1996-1999 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#if defined(LIBC_SCCS) && !defined(lint) -static const char rcsid[] = "$Id: lcl_nw.c,v 1.5 2013-01-06 23:14:52 marka Exp $"; -/* from getgrent.c 8.2 (Berkeley) 3/21/94"; */ -/* from BSDI Id: getgrent.c,v 2.8 1996/05/28 18:15:14 bostic Exp $ */ -#endif /* LIBC_SCCS and not lint */ - -/* Imports */ - -#include "port_before.h" - -#include -#include - -#include -#include -#include - -#include -#include -#include -#include -#include -#include - -#include -#include - -#include "port_after.h" - -#include -#include "irs_p.h" -#include "lcl_p.h" - -#define MAXALIASES 35 -#define MAXADDRSIZE 4 - -struct pvt { - FILE * fp; - char line[BUFSIZ+1]; - struct nwent net; - char * aliases[MAXALIASES]; - char addr[MAXADDRSIZE]; - struct __res_state * res; - void (*free_res)(void *); -}; - -/* Forward */ - -static void nw_close(struct irs_nw *); -static struct nwent * nw_byname(struct irs_nw *, const char *, int); -static struct nwent * nw_byaddr(struct irs_nw *, void *, int, int); -static struct nwent * nw_next(struct irs_nw *); -static void nw_rewind(struct irs_nw *); -static void nw_minimize(struct irs_nw *); -static struct __res_state * nw_res_get(struct irs_nw *this); -static void nw_res_set(struct irs_nw *this, - struct __res_state *res, - void (*free_res)(void *)); - -static int init(struct irs_nw *this); - -/* Portability. */ - -#ifndef SEEK_SET -# define SEEK_SET 0 -#endif - -/* Public */ - -struct irs_nw * -irs_lcl_nw(struct irs_acc *this) { - struct irs_nw *nw; - struct pvt *pvt; - - UNUSED(this); - - if (!(pvt = memget(sizeof *pvt))) { - errno = ENOMEM; - return (NULL); - } - memset(pvt, 0, sizeof *pvt); - if (!(nw = memget(sizeof *nw))) { - memput(pvt, sizeof *pvt); - errno = ENOMEM; - return (NULL); - } - memset(nw, 0x5e, sizeof *nw); - nw->private = pvt; - nw->close = nw_close; - nw->byname = nw_byname; - nw->byaddr = nw_byaddr; - nw->next = nw_next; - nw->rewind = nw_rewind; - nw->minimize = nw_minimize; - nw->res_get = nw_res_get; - nw->res_set = nw_res_set; - return (nw); -} - -/* Methods */ - -static void -nw_close(struct irs_nw *this) { - struct pvt *pvt = (struct pvt *)this->private; - - nw_minimize(this); - if (pvt->res && pvt->free_res) - (*pvt->free_res)(pvt->res); - if (pvt->fp) - (void)fclose(pvt->fp); - memput(pvt, sizeof *pvt); - memput(this, sizeof *this); -} - -static struct nwent * -nw_byaddr(struct irs_nw *this, void *net, int length, int type) { - struct nwent *p; - - if (init(this) == -1) - return(NULL); - - nw_rewind(this); - while ((p = nw_next(this)) != NULL) - if (p->n_addrtype == type && p->n_length == length) - if (bitncmp(p->n_addr, net, length) == 0) - break; - return (p); -} - -static struct nwent * -nw_byname(struct irs_nw *this, const char *name, int type) { - struct nwent *p; - char **ap; - - if (init(this) == -1) - return(NULL); - - nw_rewind(this); - while ((p = nw_next(this)) != NULL) { - if (ns_samename(p->n_name, name) == 1 && - p->n_addrtype == type) - break; - for (ap = p->n_aliases; *ap; ap++) - if ((ns_samename(*ap, name) == 1) && - (p->n_addrtype == type)) - goto found; - } - found: - return (p); -} - -static void -nw_rewind(struct irs_nw *this) { - struct pvt *pvt = (struct pvt *)this->private; - - if (pvt->fp) { - if (fseek(pvt->fp, 0L, SEEK_SET) == 0) - return; - (void)fclose(pvt->fp); - } - if (!(pvt->fp = fopen(_PATH_NETWORKS, "r"))) - return; - if (fcntl(fileno(pvt->fp), F_SETFD, 1) < 0) { - (void)fclose(pvt->fp); - pvt->fp = NULL; - } -} - -static struct nwent * -nw_next(struct irs_nw *this) { - struct pvt *pvt = (struct pvt *)this->private; - struct nwent *ret = NULL; - char *p, *cp, **q; - char *bufp, *ndbuf, *dbuf = NULL; - int c, bufsiz, offset = 0; - - if (init(this) == -1) - return(NULL); - - if (pvt->fp == NULL) - nw_rewind(this); - if (pvt->fp == NULL) { - RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL); - return (NULL); - } - bufp = pvt->line; - bufsiz = sizeof(pvt->line); - - again: - p = fgets(bufp + offset, bufsiz - offset, pvt->fp); - if (p == NULL) - goto cleanup; - if (!strchr(p, '\n') && !feof(pvt->fp)) { -#define GROWBUF 1024 - /* allocate space for longer line */ - if (dbuf == NULL) { - if ((ndbuf = malloc(bufsiz + GROWBUF)) != NULL) - strcpy(ndbuf, bufp); - } else - ndbuf = realloc(dbuf, bufsiz + GROWBUF); - if (ndbuf) { - dbuf = ndbuf; - bufp = dbuf; - bufsiz += GROWBUF; - offset = strlen(dbuf); - } else { - /* allocation failed; skip this long line */ - while ((c = getc(pvt->fp)) != EOF) - if (c == '\n') - break; - if (c != EOF) - ungetc(c, pvt->fp); - } - goto again; - } - - p -= offset; - offset = 0; - - if (*p == '#') - goto again; - - cp = strpbrk(p, "#\n"); - if (cp != NULL) - *cp = '\0'; - pvt->net.n_name = p; - cp = strpbrk(p, " \t"); - if (cp == NULL) - goto again; - *cp++ = '\0'; - while (*cp == ' ' || *cp == '\t') - cp++; - p = strpbrk(cp, " \t"); - if (p != NULL) - *p++ = '\0'; - pvt->net.n_length = inet_net_pton(AF_INET, cp, pvt->addr, - sizeof pvt->addr); - if (pvt->net.n_length < 0) - goto again; - pvt->net.n_addrtype = AF_INET; - pvt->net.n_addr = pvt->addr; - q = pvt->net.n_aliases = pvt->aliases; - if (p != NULL) { - cp = p; - while (cp && *cp) { - if (*cp == ' ' || *cp == '\t') { - cp++; - continue; - } - if (q < &pvt->aliases[MAXALIASES - 1]) - *q++ = cp; - cp = strpbrk(cp, " \t"); - if (cp != NULL) - *cp++ = '\0'; - } - } - *q = NULL; - ret = &pvt->net; - - cleanup: - if (dbuf) - free(dbuf); - - return (ret); -} - -static void -nw_minimize(struct irs_nw *this) { - struct pvt *pvt = (struct pvt *)this->private; - - if (pvt->res) - res_nclose(pvt->res); - if (pvt->fp != NULL) { - (void)fclose(pvt->fp); - pvt->fp = NULL; - } -} - -static struct __res_state * -nw_res_get(struct irs_nw *this) { - struct pvt *pvt = (struct pvt *)this->private; - - if (!pvt->res) { - struct __res_state *res; - res = (struct __res_state *)malloc(sizeof *res); - if (!res) { - errno = ENOMEM; - return (NULL); - } - memset(res, 0, sizeof *res); - nw_res_set(this, res, free); - } - - return (pvt->res); -} - -static void -nw_res_set(struct irs_nw *this, struct __res_state *res, - void (*free_res)(void *)) { - struct pvt *pvt = (struct pvt *)this->private; - - if (pvt->res && pvt->free_res) { - res_nclose(pvt->res); - (*pvt->free_res)(pvt->res); - } - - pvt->res = res; - pvt->free_res = free_res; -} - -static int -init(struct irs_nw *this) { - struct pvt *pvt = (struct pvt *)this->private; - - if (!pvt->res && !nw_res_get(this)) - return (-1); - if (((pvt->res->options & RES_INIT) == 0U) && - res_ninit(pvt->res) == -1) - return (-1); - return (0); -} - -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/lcl_p.h /usr/src/lib/libc/net/lcl_p.h --- /home/reed/src/isc/libbind/libbind/irs/lcl_p.h 2005-04-26 23:56:31.000000000 -0500 +++ /usr/src/lib/libc/net/lcl_p.h 1969-12-31 18:00:00.000000000 -0600 @@ -1,51 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1996,1999 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -/* - * $Id: lcl_p.h,v 1.3 2005/04/27 04:56:31 sra Exp $ - */ - -/*! \file - * \brief - * lcl_p.h - private include file for the local accessor functions. - */ - -#ifndef _LCL_P_H_INCLUDED -#define _LCL_P_H_INCLUDED - -/*% - * Object state. - */ -struct lcl_p { - struct __res_state * res; - void (*free_res) __P((void *)); -}; - -/* - * Externs. - */ - -extern struct irs_acc * irs_lcl_acc __P((const char *)); -extern struct irs_gr * irs_lcl_gr __P((struct irs_acc *)); -extern struct irs_pw * irs_lcl_pw __P((struct irs_acc *)); -extern struct irs_sv * irs_lcl_sv __P((struct irs_acc *)); -extern struct irs_pr * irs_lcl_pr __P((struct irs_acc *)); -extern struct irs_ho * irs_lcl_ho __P((struct irs_acc *)); -extern struct irs_nw * irs_lcl_nw __P((struct irs_acc *)); -extern struct irs_ng * irs_lcl_ng __P((struct irs_acc *)); - -#endif /*_LCL_P_H_INCLUDED*/ diff -pruN /home/reed/src/isc/libbind/libbind/irs/lcl_pr.c /usr/src/lib/libc/net/lcl_pr.c --- /home/reed/src/isc/libbind/libbind/irs/lcl_pr.c 2013-06-05 09:33:54.000000000 -0500 +++ /usr/src/lib/libc/net/lcl_pr.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,292 +0,0 @@ -/* - * Copyright (c) 1989, 1993, 1995 - * The Regents of the University of California. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Portions Copyright (c) 1996,1999 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#if defined(LIBC_SCCS) && !defined(lint) -static const char rcsid[] = "$Id: lcl_pr.c,v 1.6 2013-01-06 23:23:38 marka Exp $"; -#endif /* LIBC_SCCS and not lint */ - -/* extern */ - -#include "port_before.h" - -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -#include -#include - -#include "port_after.h" - -#include "irs_p.h" -#include "lcl_p.h" - -#ifndef _PATH_PROTOCOLS -#define _PATH_PROTOCOLS "/etc/protocols" -#endif -#define MAXALIASES 35 - -/* Types */ - -struct pvt { - FILE * fp; - char line[BUFSIZ+1]; - char * dbuf; - struct protoent proto; - char * proto_aliases[MAXALIASES]; -}; - -/* Forward */ - -static void pr_close(struct irs_pr *); -static struct protoent * pr_next(struct irs_pr *); -static struct protoent * pr_byname(struct irs_pr *, const char *); -static struct protoent * pr_bynumber(struct irs_pr *, int); -static void pr_rewind(struct irs_pr *); -static void pr_minimize(struct irs_pr *); - -/* Portability. */ - -#ifndef SEEK_SET -# define SEEK_SET 0 -#endif - -/* Public */ - -struct irs_pr * -irs_lcl_pr(struct irs_acc *this) { - struct irs_pr *pr; - struct pvt *pvt; - - UNUSED(this); - - if (!(pr = memget(sizeof *pr))) { - errno = ENOMEM; - return (NULL); - } - if (!(pvt = memget(sizeof *pvt))) { - memput(pr, sizeof *pr); - errno = ENOMEM; - return (NULL); - } - memset(pvt, 0, sizeof *pvt); - pr->private = pvt; - pr->close = pr_close; - pr->byname = pr_byname; - pr->bynumber = pr_bynumber; - pr->next = pr_next; - pr->rewind = pr_rewind; - pr->minimize = pr_minimize; - pr->res_get = NULL; - pr->res_set = NULL; - return (pr); -} - -/* Methods */ - -static void -pr_close(struct irs_pr *this) { - struct pvt *pvt = (struct pvt *)this->private; - - if (pvt->fp) - (void) fclose(pvt->fp); - if (pvt->dbuf) - free(pvt->dbuf); - memput(pvt, sizeof *pvt); - memput(this, sizeof *this); -} - -static struct protoent * -pr_byname(struct irs_pr *this, const char *name) { - - struct protoent *p; - char **cp; - - pr_rewind(this); - while ((p = pr_next(this))) { - if (!strcmp(p->p_name, name)) - goto found; - for (cp = p->p_aliases; *cp; cp++) - if (!strcmp(*cp, name)) - goto found; - } - found: - return (p); -} - -static struct protoent * -pr_bynumber(struct irs_pr *this, int proto) { - struct protoent *p; - - pr_rewind(this); - while ((p = pr_next(this))) - if (p->p_proto == proto) - break; - return (p); -} - -static void -pr_rewind(struct irs_pr *this) { - struct pvt *pvt = (struct pvt *)this->private; - - if (pvt->fp) { - if (fseek(pvt->fp, 0L, SEEK_SET) == 0) - return; - (void)fclose(pvt->fp); - } - if (!(pvt->fp = fopen(_PATH_PROTOCOLS, "r" ))) - return; - if (fcntl(fileno(pvt->fp), F_SETFD, 1) < 0) { - (void)fclose(pvt->fp); - pvt->fp = NULL; - } -} - -static struct protoent * -pr_next(struct irs_pr *this) { - struct pvt *pvt = (struct pvt *)this->private; - char *p, *cp, **q; - char *bufp, *ndbuf, *dbuf = NULL; - int c, bufsiz, offset; - - if (!pvt->fp) - pr_rewind(this); - if (!pvt->fp) - return (NULL); - if (pvt->dbuf) { - free(pvt->dbuf); - pvt->dbuf = NULL; - } - bufp = pvt->line; - bufsiz = BUFSIZ; - offset = 0; - again: - if ((p = fgets(bufp + offset, bufsiz - offset, pvt->fp)) == NULL) { - if (dbuf) - free(dbuf); - return (NULL); - } - if (!strchr(p, '\n') && !feof(pvt->fp)) { -#define GROWBUF 1024 - /* allocate space for longer line */ - if (dbuf == NULL) { - if ((ndbuf = malloc(bufsiz + GROWBUF)) != NULL) - strcpy(ndbuf, bufp); - } else - ndbuf = realloc(dbuf, bufsiz + GROWBUF); - if (ndbuf) { - dbuf = ndbuf; - bufp = dbuf; - bufsiz += GROWBUF; - offset = strlen(dbuf); - } else { - /* allocation failed; skip this long line */ - while ((c = getc(pvt->fp)) != EOF) - if (c == '\n') - break; - if (c != EOF) - ungetc(c, pvt->fp); - } - goto again; - } - - p -= offset; - offset = 0; - - if (*p == '#') - goto again; - cp = strpbrk(p, "#\n"); - if (cp != NULL) - *cp = '\0'; - pvt->proto.p_name = p; - cp = strpbrk(p, " \t"); - if (cp == NULL) - goto again; - *cp++ = '\0'; - while (*cp == ' ' || *cp == '\t') - cp++; - p = strpbrk(cp, " \t"); - if (p != NULL) - *p++ = '\0'; - pvt->proto.p_proto = atoi(cp); - q = pvt->proto.p_aliases = pvt->proto_aliases; - if (p != NULL) { - cp = p; - while (cp && *cp) { - if (*cp == ' ' || *cp == '\t') { - cp++; - continue; - } - if (q < &pvt->proto_aliases[MAXALIASES - 1]) - *q++ = cp; - cp = strpbrk(cp, " \t"); - if (cp != NULL) - *cp++ = '\0'; - } - } - *q = NULL; - pvt->dbuf = dbuf; - return (&pvt->proto); -} - -static void -pr_minimize(struct irs_pr *this) { - struct pvt *pvt = (struct pvt *)this->private; - - if (pvt->fp != NULL) { - (void)fclose(pvt->fp); - pvt->fp = NULL; - } -} - -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/lcl_pw.c /usr/src/lib/libc/net/lcl_pw.c --- /home/reed/src/isc/libbind/libbind/irs/lcl_pw.c 2013-06-05 09:33:54.000000000 -0500 +++ /usr/src/lib/libc/net/lcl_pw.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,305 +0,0 @@ -/* - * Copyright (c) 1989, 1993, 1995 - * The Regents of the University of California. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Portions Copyright (c) 1996,1999 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#if defined(LIBC_SCCS) && !defined(lint) -static const char rcsid[] = "$Id: lcl_pw.c,v 1.4 2013-01-06 23:14:52 marka Exp $"; -#endif /* LIBC_SCCS and not lint */ - -/* Extern */ - -#include "port_before.h" - -#ifndef WANT_IRS_PW -static int __bind_irs_pw_unneeded; -#else - -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include "port_after.h" - -#include "irs_p.h" -#include "lcl_p.h" - -/*! \file - * \brief - * The lookup techniques and data extraction code here must be kept - * in sync with that in `pwd_mkdb'. - */ - - -/* Types */ - -struct pvt { - struct passwd passwd; /*%< password structure */ - DB *pw_db; /*%< password database */ - int pw_keynum; /*%< key counter */ - int warned; - u_int max; - char * line; -}; - -/* Forward */ - -static void pw_close(struct irs_pw *); -static struct passwd * pw_next(struct irs_pw *); -static struct passwd * pw_byname(struct irs_pw *, const char *); -static struct passwd * pw_byuid(struct irs_pw *, uid_t); -static void pw_rewind(struct irs_pw *); -static void pw_minimize(struct irs_pw *); - -static int initdb(struct pvt *); -static int hashpw(struct irs_pw *, DBT *); - -/* Public */ -struct irs_pw * -irs_lcl_pw(struct irs_acc *this) { - struct irs_pw *pw; - struct pvt *pvt; - - UNUSED(this); - - if (!(pw = memget(sizeof *pw))) { - errno = ENOMEM; - return (NULL); - } - memset(pw, 0x5e, sizeof *pw); - if (!(pvt = memget(sizeof *pvt))) { - free(pw); - errno = ENOMEM; - return (NULL); - } - memset(pvt, 0, sizeof *pvt); - pw->private = pvt; - pw->close = pw_close; - pw->next = pw_next; - pw->byname = pw_byname; - pw->byuid = pw_byuid; - pw->rewind = pw_rewind; - pw->minimize = pw_minimize; - pw->res_get = NULL; - pw->res_set = NULL; - return (pw); -} - -/* Methods */ - -static void -pw_close(struct irs_pw *this) { - struct pvt *pvt = (struct pvt *)this->private; - - if (pvt->pw_db) { - (void)(pvt->pw_db->close)(pvt->pw_db); - pvt->pw_db = NULL; - } - if (pvt->line) - memput(pvt->line, pvt->max); - memput(pvt, sizeof *pvt); - memput(this, sizeof *this); -} - -static struct passwd * -pw_next(struct irs_pw *this) { - struct pvt *pvt = (struct pvt *)this->private; - - DBT key; - char bf[sizeof(pvt->pw_keynum) + 1]; - - if (!initdb(pvt)) - return (NULL); - - ++pvt->pw_keynum; - bf[0] = _PW_KEYBYNUM; - memcpy(bf + 1, (char *)&pvt->pw_keynum, sizeof(pvt->pw_keynum)); - key.data = (u_char *)bf; - key.size = sizeof(pvt->pw_keynum) + 1; - return (hashpw(this, &key) ? &pvt->passwd : NULL); -} - -static struct passwd * -pw_byname(struct irs_pw *this, const char *name) { - struct pvt *pvt = (struct pvt *)this->private; - DBT key; - int len, rval; - char bf[UT_NAMESIZE + 1]; - - if (!initdb(pvt)) - return (NULL); - - bf[0] = _PW_KEYBYNAME; - len = strlen(name); - memcpy(bf + 1, name, MIN(len, UT_NAMESIZE)); - key.data = (u_char *)bf; - key.size = len + 1; - rval = hashpw(this, &key); - - return (rval ? &pvt->passwd : NULL); -} - - -static struct passwd * -pw_byuid(struct irs_pw *this, uid_t uid) { - struct pvt *pvt = (struct pvt *)this->private; - DBT key; - int keyuid, rval; - char bf[sizeof(keyuid) + 1]; - - if (!initdb(pvt)) - return (NULL); - - bf[0] = _PW_KEYBYUID; - keyuid = uid; - memcpy(bf + 1, &keyuid, sizeof(keyuid)); - key.data = (u_char *)bf; - key.size = sizeof(keyuid) + 1; - rval = hashpw(this, &key); - - return (rval ? &pvt->passwd : NULL); -} - -static void -pw_rewind(struct irs_pw *this) { - struct pvt *pvt = (struct pvt *)this->private; - - pvt->pw_keynum = 0; -} - -static void -pw_minimize(struct irs_pw *this) { - struct pvt *pvt = (struct pvt *)this->private; - - if (pvt->pw_db != NULL) { - (void) (*pvt->pw_db->close)(pvt->pw_db); - pvt->pw_db = NULL; - } -} - -/* Private. */ - -static int -initdb(struct pvt *pvt) { - const char *p; - - if (pvt->pw_db) { - if (lseek((*pvt->pw_db->fd)(pvt->pw_db), 0L, SEEK_CUR) >= 0L) - return (1); - else - (void) (*pvt->pw_db->close)(pvt->pw_db); - } - pvt->pw_db = dbopen((p = _PATH_SMP_DB), O_RDONLY, 0, DB_HASH, NULL); - if (!pvt->pw_db) - pvt->pw_db = dbopen((p =_PATH_MP_DB), O_RDONLY, - 0, DB_HASH, NULL); - if (pvt->pw_db) - return (1); - if (!pvt->warned) { - syslog(LOG_ERR, "%s: %m", p); - pvt->warned++; - } - return (0); -} - -static int -hashpw(struct irs_pw *this, DBT *key) { - struct pvt *pvt = (struct pvt *)this->private; - char *p, *t, *l; - DBT data; - - if ((pvt->pw_db->get)(pvt->pw_db, key, &data, 0)) - return (0); - p = (char *)data.data; - if (data.size > pvt->max) { - size_t newlen = pvt->max + 1024; - char *p = memget(newlen); - if (p == NULL) { - return (0); - } - if (pvt->line != NULL) { - memcpy(p, pvt->line, pvt->max); - memput(pvt->line, pvt->max); - } - pvt->max = newlen; - pvt->line = p; - } - - /* THIS CODE MUST MATCH THAT IN pwd_mkdb. */ - t = pvt->line; - l = pvt->line + pvt->max; -#define EXPAND(e) if ((e = t) == NULL) return (0); else \ - do if (t >= l) return (0); while ((*t++ = *p++) != '\0') -#define SCALAR(v) if (t + sizeof v >= l) return (0); else \ - (memmove(&(v), p, sizeof v), p += sizeof v) - EXPAND(pvt->passwd.pw_name); - EXPAND(pvt->passwd.pw_passwd); - SCALAR(pvt->passwd.pw_uid); - SCALAR(pvt->passwd.pw_gid); - SCALAR(pvt->passwd.pw_change); - EXPAND(pvt->passwd.pw_class); - EXPAND(pvt->passwd.pw_gecos); - EXPAND(pvt->passwd.pw_dir); - EXPAND(pvt->passwd.pw_shell); - SCALAR(pvt->passwd.pw_expire); - return (1); -} - -#endif /* WANT_IRS_PW */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/lcl_sv.c /usr/src/lib/libc/net/lcl_sv.c --- /home/reed/src/isc/libbind/libbind/irs/lcl_sv.c 2013-06-05 09:33:54.000000000 -0500 +++ /usr/src/lib/libc/net/lcl_sv.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,428 +0,0 @@ -/* - * Copyright (c) 1989, 1993, 1995 - * The Regents of the University of California. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Portions Copyright (c) 1996-1999 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#if defined(LIBC_SCCS) && !defined(lint) -static const char rcsid[] = "$Id: lcl_sv.c,v 1.5 2013-01-06 23:14:52 marka Exp $"; -#endif /* LIBC_SCCS and not lint */ - -/* extern */ - -#include "port_before.h" - -#include -#include -#include -#include -#include - -#ifdef IRS_LCL_SV_DB -#include -#endif -#include -#include -#include -#include -#include -#include - -#include -#include - -#include "port_after.h" - -#include "irs_p.h" -#include "lcl_p.h" - -#ifdef SPRINTF_CHAR -# define SPRINTF(x) strlen(sprintf/**/x) -#else -# define SPRINTF(x) ((size_t)sprintf x) -#endif - -/* Types */ - -struct pvt { -#ifdef IRS_LCL_SV_DB - DB * dbh; - int dbf; -#endif - struct lcl_sv sv; -}; - -/* Forward */ - -static void sv_close(struct irs_sv*); -static struct servent * sv_next(struct irs_sv *); -static struct servent * sv_byname(struct irs_sv *, const char *, - const char *); -static struct servent * sv_byport(struct irs_sv *, int, const char *); -static void sv_rewind(struct irs_sv *); -static void sv_minimize(struct irs_sv *); -/*global*/ struct servent * irs_lclsv_fnxt(struct lcl_sv *); -#ifdef IRS_LCL_SV_DB -static struct servent * sv_db_rec(struct lcl_sv *, DBT *, DBT *); -#endif - -/* Portability */ - -#ifndef SEEK_SET -# define SEEK_SET 0 -#endif - -/* Public */ - -struct irs_sv * -irs_lcl_sv(struct irs_acc *this) { - struct irs_sv *sv; - struct pvt *pvt; - - UNUSED(this); - - if ((sv = memget(sizeof *sv)) == NULL) { - errno = ENOMEM; - return (NULL); - } - memset(sv, 0x5e, sizeof *sv); - if ((pvt = memget(sizeof *pvt)) == NULL) { - memput(sv, sizeof *sv); - errno = ENOMEM; - return (NULL); - } - memset(pvt, 0, sizeof *pvt); - sv->private = pvt; - sv->close = sv_close; - sv->next = sv_next; - sv->byname = sv_byname; - sv->byport = sv_byport; - sv->rewind = sv_rewind; - sv->minimize = sv_minimize; - sv->res_get = NULL; - sv->res_set = NULL; -#ifdef IRS_LCL_SV_DB - pvt->dbf = R_FIRST; -#endif - return (sv); -} - -/* Methods */ - -static void -sv_close(struct irs_sv *this) { - struct pvt *pvt = (struct pvt *)this->private; - -#ifdef IRS_LCL_SV_DB - if (pvt->dbh != NULL) - (*pvt->dbh->close)(pvt->dbh); -#endif - if (pvt->sv.fp) - fclose(pvt->sv.fp); - memput(pvt, sizeof *pvt); - memput(this, sizeof *this); -} - -static struct servent * -sv_byname(struct irs_sv *this, const char *name, const char *proto) { -#ifdef IRS_LCL_SV_DB - struct pvt *pvt = (struct pvt *)this->private; -#endif - struct servent *p; - char **cp; - - sv_rewind(this); -#ifdef IRS_LCL_SV_DB - if (pvt->dbh != NULL) { - DBT key, data; - - /* Note that (sizeof "/") == 2. */ - if ((strlen(name) + sizeof "/" + proto ? strlen(proto) : 0) - > sizeof pvt->sv.line) - goto try_local; - key.data = pvt->sv.line; - key.size = SPRINTF((pvt->sv.line, "%s/%s", name, - proto ? proto : "")) + 1; - if (proto != NULL) { - if ((*pvt->dbh->get)(pvt->dbh, &key, &data, 0) != 0) - return (NULL); - } else if ((*pvt->dbh->seq)(pvt->dbh, &key, &data, R_CURSOR) - != 0) - return (NULL); - return (sv_db_rec(&pvt->sv, &key, &data)); - } - try_local: -#endif - - while ((p = sv_next(this))) { - if (strcmp(name, p->s_name) == 0) - goto gotname; - for (cp = p->s_aliases; *cp; cp++) - if (strcmp(name, *cp) == 0) - goto gotname; - continue; - gotname: - if (proto == NULL || strcmp(p->s_proto, proto) == 0) - break; - } - return (p); -} - -static struct servent * -sv_byport(struct irs_sv *this, int port, const char *proto) { -#ifdef IRS_LCL_SV_DB - struct pvt *pvt = (struct pvt *)this->private; -#endif - struct servent *p; - - sv_rewind(this); -#ifdef IRS_LCL_SV_DB - if (pvt->dbh != NULL) { - DBT key, data; - u_short *ports; - - ports = (u_short *)pvt->sv.line; - ports[0] = 0; - ports[1] = port; - key.data = ports; - key.size = sizeof(u_short) * 2; - if (proto && *proto) { - strncpy((char *)ports + key.size, proto, - BUFSIZ - key.size); - key.size += strlen((char *)ports + key.size) + 1; - if ((*pvt->dbh->get)(pvt->dbh, &key, &data, 0) != 0) - return (NULL); - } else { - if ((*pvt->dbh->seq)(pvt->dbh, &key, &data, R_CURSOR) - != 0) - return (NULL); - } - return (sv_db_rec(&pvt->sv, &key, &data)); - } -#endif - while ((p = sv_next(this))) { - if (p->s_port != port) - continue; - if (proto == NULL || strcmp(p->s_proto, proto) == 0) - break; - } - return (p); -} - -static void -sv_rewind(struct irs_sv *this) { - struct pvt *pvt = (struct pvt *)this->private; - - if (pvt->sv.fp) { - if (fseek(pvt->sv.fp, 0L, SEEK_SET) == 0) - return; - (void)fclose(pvt->sv.fp); - pvt->sv.fp = NULL; - } -#ifdef IRS_LCL_SV_DB - pvt->dbf = R_FIRST; - if (pvt->dbh != NULL) - return; - pvt->dbh = dbopen(_PATH_SERVICES_DB, O_RDONLY,O_RDONLY,DB_BTREE, NULL); - if (pvt->dbh != NULL) { - if (fcntl((*pvt->dbh->fd)(pvt->dbh), F_SETFD, 1) < 0) { - (*pvt->dbh->close)(pvt->dbh); - pvt->dbh = NULL; - } - return; - } -#endif - if ((pvt->sv.fp = fopen(_PATH_SERVICES, "r")) == NULL) - return; - if (fcntl(fileno(pvt->sv.fp), F_SETFD, 1) < 0) { - (void)fclose(pvt->sv.fp); - pvt->sv.fp = NULL; - } -} - -static struct servent * -sv_next(struct irs_sv *this) { - struct pvt *pvt = (struct pvt *)this->private; - -#ifdef IRS_LCL_SV_DB - if (pvt->dbh == NULL && pvt->sv.fp == NULL) -#else - if (pvt->sv.fp == NULL) -#endif - sv_rewind(this); - -#ifdef IRS_LCL_SV_DB - if (pvt->dbh != NULL) { - DBT key, data; - - while ((*pvt->dbh->seq)(pvt->dbh, &key, &data, pvt->dbf) == 0){ - pvt->dbf = R_NEXT; - if (((char *)key.data)[0]) - continue; - return (sv_db_rec(&pvt->sv, &key, &data)); - } - } -#endif - - if (pvt->sv.fp == NULL) - return (NULL); - return (irs_lclsv_fnxt(&pvt->sv)); -} - -static void -sv_minimize(struct irs_sv *this) { - struct pvt *pvt = (struct pvt *)this->private; - -#ifdef IRS_LCL_SV_DB - if (pvt->dbh != NULL) { - (*pvt->dbh->close)(pvt->dbh); - pvt->dbh = NULL; - } -#endif - if (pvt->sv.fp != NULL) { - (void)fclose(pvt->sv.fp); - pvt->sv.fp = NULL; - } -} - -/* Quasipublic. */ - -struct servent * -irs_lclsv_fnxt(struct lcl_sv *sv) { - char *p, *cp, **q; - - again: - if ((p = fgets(sv->line, BUFSIZ, sv->fp)) == NULL) - return (NULL); - if (*p == '#') - goto again; - sv->serv.s_name = p; - while (*p && *p != '\n' && *p != ' ' && *p != '\t' && *p != '#') - ++p; - if (*p == '\0' || *p == '#' || *p == '\n') - goto again; - *p++ = '\0'; - while (*p == ' ' || *p == '\t') - p++; - if (*p == '\0' || *p == '#' || *p == '\n') - goto again; - sv->serv.s_port = htons((u_short)strtol(p, &cp, 10)); - if (cp == p || (*cp != '/' && *cp != ',')) - goto again; - p = cp + 1; - sv->serv.s_proto = p; - - q = sv->serv.s_aliases = sv->serv_aliases; - - while (*p && *p != '\n' && *p != ' ' && *p != '\t' && *p != '#') - ++p; - - while (*p == ' ' || *p == '\t') { - *p++ = '\0'; - while (*p == ' ' || *p == '\t') - ++p; - if (*p == '\0' || *p == '#' || *p == '\n') - break; - if (q < &sv->serv_aliases[IRS_SV_MAXALIASES - 1]) - *q++ = p; - while (*p && *p != '\n' && *p != ' ' && *p != '\t' && *p != '#') - ++p; - } - - *p = '\0'; - *q = NULL; - return (&sv->serv); -} - -/* Private. */ - -#ifdef IRS_LCL_SV_DB -static struct servent * -sv_db_rec(struct lcl_sv *sv, DBT *key, DBT *data) { - char *p, **q; - int n; - - p = data->data; - p[data->size - 1] = '\0'; /*%< should be, but we depend on it */ - if (((char *)key->data)[0] == '\0') { - if (key->size < sizeof(u_short)*2 || data->size < 2) - return (NULL); - sv->serv.s_port = ((u_short *)key->data)[1]; - n = strlen(p) + 1; - if ((size_t)n > sizeof(sv->line)) { - n = sizeof(sv->line); - } - memcpy(sv->line, p, n); - sv->serv.s_name = sv->line; - if ((sv->serv.s_proto = strchr(sv->line, '/')) != NULL) - *(sv->serv.s_proto)++ = '\0'; - p += n; - data->size -= n; - } else { - if (data->size < sizeof(u_short) + 1) - return (NULL); - if (key->size > sizeof(sv->line)) - key->size = sizeof(sv->line); - ((char *)key->data)[key->size - 1] = '\0'; - memcpy(sv->line, key->data, key->size); - sv->serv.s_name = sv->line; - if ((sv->serv.s_proto = strchr(sv->line, '/')) != NULL) - *(sv->serv.s_proto)++ = '\0'; - sv->serv.s_port = *(u_short *)data->data; - p += sizeof(u_short); - data->size -= sizeof(u_short); - } - q = sv->serv.s_aliases = sv->serv_aliases; - while (data->size > 0 && q < &sv->serv_aliases[IRS_SV_MAXALIASES - 1]) { - - *q++ = p; - n = strlen(p) + 1; - data->size -= n; - p += n; - } - *q = NULL; - return (&sv->serv); -} -#endif - -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/linkaddr.c /usr/src/lib/libc/net/linkaddr.c --- /home/reed/src/isc/libbind/libbind/irs/linkaddr.c 1969-12-31 18:00:00.000000000 -0600 +++ /usr/src/lib/libc/net/linkaddr.c 2013-06-05 09:26:13.000000000 -0500 @@ -0,0 +1,171 @@ +/* $NetBSD: linkaddr.c,v 1.16 2012/03/20 17:44:18 matt Exp $ */ + +/*- + * Copyright (c) 1990, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)linkaddr.c 8.1 (Berkeley) 6/4/93"; +#else +__RCSID("$NetBSD: linkaddr.c,v 1.16 2012/03/20 17:44:18 matt Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include +#include + +#include +#include + +/* States*/ +#define NAMING 0 +#define GOTONE 1 +#define GOTTWO 2 +#define RESET 3 +/* Inputs */ +#define DIGIT (4*0) +#define END (4*1) +#define DELIM (4*2) +#define LETTER (4*3) + +void +link_addr(const char *addr, struct sockaddr_dl *sdl) +{ + register char *cp = sdl->sdl_data; + char *cplim = sdl->sdl_len + (char *)(void *)sdl; + int byte = 0, state = NAMING; + size_t newaddr = 0; /* pacify gcc */ + + _DIAGASSERT(addr != NULL); + _DIAGASSERT(sdl != NULL); + + (void)memset(&sdl->sdl_family, 0, (size_t)sdl->sdl_len - 1); + sdl->sdl_family = AF_LINK; + do { + state &= ~LETTER; + if ((*addr >= '0') && (*addr <= '9')) { + newaddr = *addr - '0'; + } else if ((*addr >= 'a') && (*addr <= 'f')) { + newaddr = *addr - 'a' + 10; + } else if ((*addr >= 'A') && (*addr <= 'F')) { + newaddr = *addr - 'A' + 10; + } else if (*addr == 0) { + state |= END; + } else if (state == NAMING && + (((*addr >= 'A') && (*addr <= 'Z')) || + ((*addr >= 'a') && (*addr <= 'z')))) + state |= LETTER; + else + state |= DELIM; + addr++; + switch (state /* | INPUT */) { + case NAMING | DIGIT: + case NAMING | LETTER: + *cp++ = addr[-1]; + continue; + case NAMING | DELIM: + state = RESET; + _DIAGASSERT(__type_fit(uint8_t, cp - sdl->sdl_data)); + sdl->sdl_nlen = (uint8_t)(cp - sdl->sdl_data); + continue; + case GOTTWO | DIGIT: + *cp++ = byte; + /* FALLTHROUGH */ + case RESET | DIGIT: + state = GOTONE; + byte = (int)newaddr; + continue; + case GOTONE | DIGIT: + state = GOTTWO; + byte = (int)newaddr + (byte << 4); + continue; + default: /* | DELIM */ + state = RESET; + *cp++ = byte; + byte = 0; + continue; + case GOTONE | END: + case GOTTWO | END: + *cp++ = byte; + /* FALLTHROUGH */ + case RESET | END: + break; + } + break; + } while (cp < cplim); + + _DIAGASSERT(__type_fit(uint8_t, cp - LLADDR(sdl))); + sdl->sdl_alen = (uint8_t)(cp - LLADDR(sdl)); + newaddr = cp - (char *)(void *)sdl; + if (newaddr > sizeof(*sdl)) { + _DIAGASSERT(__type_fit(uint8_t, newaddr)); + sdl->sdl_len = (uint8_t)newaddr; + } + return; +} + +static const char hexlist[16] = "0123456789abcdef"; + +char * +link_ntoa(const struct sockaddr_dl *sdl) +{ + static char obuf[64]; + register char *out = obuf; + register size_t i; + const u_char *in = (const u_char *)CLLADDR(sdl); + const u_char *inlim = in + sdl->sdl_alen; + int firsttime = 1; + + _DIAGASSERT(sdl != NULL); + + if (sdl->sdl_nlen) { + (void)memcpy(obuf, sdl->sdl_data, (size_t)sdl->sdl_nlen); + out += sdl->sdl_nlen; + if (sdl->sdl_alen) + *out++ = ':'; + } + while (in < inlim) { + if (firsttime) + firsttime = 0; + else + *out++ = '.'; + i = *in++; + if (i > 0xf) { + out[1] = hexlist[i & 0xf]; + i >>= 4; + out[0] = hexlist[i]; + out += 2; + } else + *out++ = hexlist[i]; + } + *out = 0; + return (obuf); +} diff -pruN /home/reed/src/isc/libbind/libbind/irs/nis.c /usr/src/lib/libc/net/nis.c --- /home/reed/src/isc/libbind/libbind/irs/nis.c 2005-04-26 23:56:32.000000000 -0500 +++ /usr/src/lib/libc/net/nis.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,156 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1996-1999 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#if defined(LIBC_SCCS) && !defined(lint) -static const char rcsid[] = "$Id: nis.c,v 1.3 2005/04/27 04:56:32 sra Exp $"; -#endif - -/* Imports */ - -#include "port_before.h" - -#ifdef WANT_IRS_NIS - -#include -#include -#include -#include - -#include -#include -#include - -#include -#include -#ifdef T_NULL -#undef T_NULL /* Silence re-definition warning of T_NULL. */ -#endif -#include -#include - -#include -#include - -#include "port_after.h" - -#include "irs_p.h" -#include "hesiod.h" -#include "nis_p.h" - -/* Forward */ - -static void nis_close(struct irs_acc *); -static struct __res_state * nis_res_get(struct irs_acc *); -static void nis_res_set(struct irs_acc *, struct __res_state *, - void (*)(void *)); - -/* Public */ - -struct irs_acc * -irs_nis_acc(const char *options) { - struct nis_p *nis; - struct irs_acc *acc; - char *domain; - - UNUSED(options); - - if (yp_get_default_domain(&domain) != 0) - return (NULL); - if (!(nis = memget(sizeof *nis))) { - errno = ENOMEM; - return (NULL); - } - memset(nis, 0, sizeof *nis); - if (!(acc = memget(sizeof *acc))) { - memput(nis, sizeof *nis); - errno = ENOMEM; - return (NULL); - } - memset(acc, 0x5e, sizeof *acc); - acc->private = nis; - nis->domain = strdup(domain); -#ifdef WANT_IRS_GR - acc->gr_map = irs_nis_gr; -#else - acc->gr_map = NULL; -#endif -#ifdef WANT_IRS_PW - acc->pw_map = irs_nis_pw; -#else - acc->pw_map = NULL; -#endif - acc->sv_map = irs_nis_sv; - acc->pr_map = irs_nis_pr; - acc->ho_map = irs_nis_ho; - acc->nw_map = irs_nis_nw; - acc->ng_map = irs_nis_ng; - acc->res_get = nis_res_get; - acc->res_set = nis_res_set; - acc->close = nis_close; - return (acc); -} - -/* Methods */ - -static struct __res_state * -nis_res_get(struct irs_acc *this) { - struct nis_p *nis = (struct nis_p *)this->private; - - if (nis->res == NULL) { - struct __res_state *res; - res = (struct __res_state *)malloc(sizeof *res); - if (res == NULL) - return (NULL); - memset(res, 0, sizeof *res); - nis_res_set(this, res, free); - } - - if ((nis->res->options & RES_INIT) == 0 && - res_ninit(nis->res) < 0) - return (NULL); - - return (nis->res); -} - -static void -nis_res_set(struct irs_acc *this, struct __res_state *res, - void (*free_res)(void *)) { - struct nis_p *nis = (struct nis_p *)this->private; - - if (nis->res && nis->free_res) { - res_nclose(nis->res); - (*nis->free_res)(nis->res); - } - - nis->res = res; - nis->free_res = free_res; -} - -static void -nis_close(struct irs_acc *this) { - struct nis_p *nis = (struct nis_p *)this->private; - - if (nis->res && nis->free_res) - (*nis->free_res)(nis->res); - free(nis->domain); - memput(nis, sizeof *nis); - memput(this, sizeof *this); -} - -#endif /*WANT_IRS_NIS*/ - -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/nis_gr.c /usr/src/lib/libc/net/nis_gr.c --- /home/reed/src/isc/libbind/libbind/irs/nis_gr.c 2013-06-05 09:33:54.000000000 -0500 +++ /usr/src/lib/libc/net/nis_gr.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,350 +0,0 @@ -/* - * Copyright (c) 1989, 1993, 1995 - * The Regents of the University of California. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the University nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - */ - -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Portions Copyright (c) 1996-1999 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#if defined(LIBC_SCCS) && !defined(lint) -static const char rcsid[] = "$Id: nis_gr.c,v 1.5 2013-01-06 23:14:52 marka Exp $"; -/* from getgrent.c 8.2 (Berkeley) 3/21/94"; */ -/* from BSDI Id: getgrent.c,v 2.8 1996/05/28 18:15:14 bostic Exp $ */ -#endif /* LIBC_SCCS and not lint */ - -/* Imports */ - -#include "port_before.h" - -#if !defined(WANT_IRS_GR) || !defined(WANT_IRS_NIS) -static int __bind_irs_gr_unneeded; -#else - -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include - -#include - -#include - -#include "port_after.h" - -#include "irs_p.h" -#include "nis_p.h" - -/* Definitions */ - -struct pvt { - int needrewind; - char * nis_domain; - char * curkey_data; - int curkey_len; - char * curval_data; - int curval_len; - /*%< - * Need space to store the entries read from the group file. - * The members list also needs space per member, and the - * strings making up the user names must be allocated - * somewhere. Rather than doing lots of small allocations, - * we keep one buffer and resize it as needed. - */ - struct group group; - size_t nmemb; /*%< Malloc'd max index of gr_mem[]. */ - char * membuf; - size_t membufsize; -}; - -enum do_what { do_none = 0x0, do_key = 0x1, do_val = 0x2, do_all = 0x3 }; - -static /*const*/ char group_bygid[] = "group.bygid"; -static /*const*/ char group_byname[] = "group.byname"; - -/* Forward */ - -static void gr_close(struct irs_gr *); -static struct group * gr_next(struct irs_gr *); -static struct group * gr_byname(struct irs_gr *, const char *); -static struct group * gr_bygid(struct irs_gr *, gid_t); -static void gr_rewind(struct irs_gr *); -static void gr_minimize(struct irs_gr *); - -static struct group * makegroupent(struct irs_gr *); -static void nisfree(struct pvt *, enum do_what); - -/* Public */ - -struct irs_gr * -irs_nis_gr(struct irs_acc *this) { - struct irs_gr *gr; - struct pvt *pvt; - - if (!(gr = memget(sizeof *gr))) { - errno = ENOMEM; - return (NULL); - } - memset(gr, 0x5e, sizeof *gr); - if (!(pvt = memget(sizeof *pvt))) { - memput(gr, sizeof *gr); - errno = ENOMEM; - return (NULL); - } - memset(pvt, 0, sizeof *pvt); - pvt->needrewind = 1; - pvt->nis_domain = ((struct nis_p *)this->private)->domain; - gr->private = pvt; - gr->close = gr_close; - gr->next = gr_next; - gr->byname = gr_byname; - gr->bygid = gr_bygid; - gr->rewind = gr_rewind; - gr->list = make_group_list; - gr->minimize = gr_minimize; - gr->res_get = NULL; - gr->res_set = NULL; - return (gr); -} - -/* Methods */ - -static void -gr_close(struct irs_gr *this) { - struct pvt *pvt = (struct pvt *)this->private; - - if (pvt->group.gr_mem) - free(pvt->group.gr_mem); - if (pvt->membuf) - free(pvt->membuf); - memput(pvt, sizeof *pvt); - memput(this, sizeof *this); -} - -static struct group * -gr_next(struct irs_gr *this) { - struct pvt *pvt = (struct pvt *)this->private; - struct group *rval; - int r; - - do { - if (pvt->needrewind) { - nisfree(pvt, do_all); - r = yp_first(pvt->nis_domain, group_byname, - &pvt->curkey_data, &pvt->curkey_len, - &pvt->curval_data, &pvt->curval_len); - pvt->needrewind = 0; - } else { - char *newkey_data; - int newkey_len; - - nisfree(pvt, do_val); - r = yp_next(pvt->nis_domain, group_byname, - pvt->curkey_data, pvt->curkey_len, - &newkey_data, &newkey_len, - &pvt->curval_data, &pvt->curval_len); - nisfree(pvt, do_key); - pvt->curkey_data = newkey_data; - pvt->curkey_len = newkey_len; - } - if (r != 0) { - errno = ENOENT; - return (NULL); - } - rval = makegroupent(this); - } while (rval == NULL); - return (rval); -} - -static struct group * -gr_byname(struct irs_gr *this, const char *name) { - struct pvt *pvt = (struct pvt *)this->private; - int r; - - nisfree(pvt, do_val); - r = yp_match(pvt->nis_domain, group_byname, name, strlen(name), - &pvt->curval_data, &pvt->curval_len); - if (r != 0) { - errno = ENOENT; - return (NULL); - } - return (makegroupent(this)); -} - -static struct group * -gr_bygid(struct irs_gr *this, gid_t gid) { - struct pvt *pvt = (struct pvt *)this->private; - char tmp[sizeof "4294967295"]; - int r; - - nisfree(pvt, do_val); - (void) sprintf(tmp, "%u", (unsigned int)gid); - r = yp_match(pvt->nis_domain, group_bygid, tmp, strlen(tmp), - &pvt->curval_data, &pvt->curval_len); - if (r != 0) { - errno = ENOENT; - return (NULL); - } - return (makegroupent(this)); -} - -static void -gr_rewind(struct irs_gr *this) { - struct pvt *pvt = (struct pvt *)this->private; - - pvt->needrewind = 1; -} - -static void -gr_minimize(struct irs_gr *this) { - UNUSED(this); - /* NOOP */ -} - -/* Private */ - -static struct group * -makegroupent(struct irs_gr *this) { - struct pvt *pvt = (struct pvt *)this->private; - unsigned int num_members = 0; - char *cp, **new; - u_long t; - - if (pvt->group.gr_mem) { - free(pvt->group.gr_mem); - pvt->group.gr_mem = NULL; - pvt->nmemb = 0; - } - if (pvt->membuf) - free(pvt->membuf); - pvt->membuf = pvt->curval_data; - pvt->curval_data = NULL; - - cp = pvt->membuf; - pvt->group.gr_name = cp; - if (!(cp = strchr(cp, ':'))) - goto cleanup; - *cp++ = '\0'; - - pvt->group.gr_passwd = cp; - if (!(cp = strchr(cp, ':'))) - goto cleanup; - *cp++ = '\0'; - - errno = 0; - t = strtoul(cp, NULL, 10); - if (errno == ERANGE) - goto cleanup; - pvt->group.gr_gid = (gid_t) t; - if (!(cp = strchr(cp, ':'))) - goto cleanup; - cp++; - - if (*cp && cp[strlen(cp)-1] == '\n') - cp[strlen(cp)-1] = '\0'; - - /* - * Parse the members out. - */ - while (*cp) { - if (num_members+1 >= pvt->nmemb || pvt->group.gr_mem == NULL) { - pvt->nmemb += 10; - new = realloc(pvt->group.gr_mem, - pvt->nmemb * sizeof(char *)); - if (new == NULL) - goto cleanup; - pvt->group.gr_mem = new; - } - pvt->group.gr_mem[num_members++] = cp; - if (!(cp = strchr(cp, ','))) - break; - *cp++ = '\0'; - } - if (pvt->group.gr_mem == NULL) { - pvt->group.gr_mem = malloc(sizeof(char*)); - if (!pvt->group.gr_mem) - goto cleanup; - pvt->nmemb = 1; - } - pvt->group.gr_mem[num_members] = NULL; - - return (&pvt->group); - - cleanup: - if (pvt->group.gr_mem) { - free(pvt->group.gr_mem); - pvt->group.gr_mem = NULL; - pvt->nmemb = 0; - } - if (pvt->membuf) { - free(pvt->membuf); - pvt->membuf = NULL; - } - return (NULL); -} - -static void -nisfree(struct pvt *pvt, enum do_what do_what) { - if ((do_what & do_key) && pvt->curkey_data) { - free(pvt->curkey_data); - pvt->curkey_data = NULL; - } - if ((do_what & do_val) && pvt->curval_data) { - free(pvt->curval_data); - pvt->curval_data = NULL; - } -} - -#endif /* WANT_IRS_GR && WANT_IRS_NIS */ -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/nis_ho.c /usr/src/lib/libc/net/nis_ho.c --- /home/reed/src/isc/libbind/libbind/irs/nis_ho.c 2005-04-26 23:56:32.000000000 -0500 +++ /usr/src/lib/libc/net/nis_ho.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,535 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1996,1999 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#if defined(LIBC_SCCS) && !defined(lint) -static const char rcsid[] = "$Id: nis_ho.c,v 1.5 2005/04/27 04:56:32 sra Exp $"; -#endif /* LIBC_SCCS and not lint */ - -/* Imports */ - -#include "port_before.h" - -#ifndef WANT_IRS_NIS -static int __bind_irs_nis_unneeded; -#else - -#include -#include -#include -#include -#include -#include -#ifdef T_NULL -#undef T_NULL /* Silence re-definition warning of T_NULL. */ -#endif -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include "port_after.h" - -#include "irs_p.h" -#include "nis_p.h" - -/* Definitions */ - -#define MAXALIASES 35 -#define MAXADDRS 35 - -#if PACKETSZ > 1024 -#define MAXPACKET PACKETSZ -#else -#define MAXPACKET 1024 -#endif - -struct pvt { - int needrewind; - char * nis_domain; - char * curkey_data; - int curkey_len; - char * curval_data; - int curval_len; - struct hostent host; - char * h_addr_ptrs[MAXADDRS + 1]; - char * host_aliases[MAXALIASES + 1]; - char hostbuf[8*1024]; - u_char host_addr[16]; /*%< IPv4 or IPv6 */ - struct __res_state *res; - void (*free_res)(void *); -}; - -enum do_what { do_none = 0x0, do_key = 0x1, do_val = 0x2, do_all = 0x3 }; - -static const u_char mapped[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0xff,0xff }; -static const u_char tunnelled[] = { 0,0, 0,0, 0,0, 0,0, 0,0, 0,0 }; -static /*const*/ char hosts_byname[] = "hosts.byname"; -static /*const*/ char hosts_byaddr[] = "hosts.byaddr"; -static /*const*/ char ipnode_byname[] = "ipnode.byname"; -static /*const*/ char ipnode_byaddr[] = "ipnode.byaddr"; -static /*const*/ char yp_multi[] = "YP_MULTI_"; - -/* Forwards */ - -static void ho_close(struct irs_ho *this); -static struct hostent * ho_byname(struct irs_ho *this, const char *name); -static struct hostent * ho_byname2(struct irs_ho *this, const char *name, - int af); -static struct hostent * ho_byaddr(struct irs_ho *this, const void *addr, - int len, int af); -static struct hostent * ho_next(struct irs_ho *this); -static void ho_rewind(struct irs_ho *this); -static void ho_minimize(struct irs_ho *this); -static struct __res_state * ho_res_get(struct irs_ho *this); -static void ho_res_set(struct irs_ho *this, - struct __res_state *res, - void (*free_res)(void *)); -static struct addrinfo * ho_addrinfo(struct irs_ho *this, const char *name, - const struct addrinfo *pai); - -static struct hostent * makehostent(struct irs_ho *this); -static void nisfree(struct pvt *, enum do_what); -static int init(struct irs_ho *this); - -/* Public */ - -struct irs_ho * -irs_nis_ho(struct irs_acc *this) { - struct irs_ho *ho; - struct pvt *pvt; - - if (!(pvt = memget(sizeof *pvt))) { - errno = ENOMEM; - return (NULL); - } - memset(pvt, 0, sizeof *pvt); - if (!(ho = memget(sizeof *ho))) { - memput(pvt, sizeof *pvt); - errno = ENOMEM; - return (NULL); - } - memset(ho, 0x5e, sizeof *ho); - pvt->needrewind = 1; - pvt->nis_domain = ((struct nis_p *)this->private)->domain; - ho->private = pvt; - ho->close = ho_close; - ho->byname = ho_byname; - ho->byname2 = ho_byname2; - ho->byaddr = ho_byaddr; - ho->next = ho_next; - ho->rewind = ho_rewind; - ho->minimize = ho_minimize; - ho->res_set = ho_res_set; - ho->res_get = ho_res_get; - ho->addrinfo = ho_addrinfo; - return (ho); -} - -/* Methods */ - -static void -ho_close(struct irs_ho *this) { - struct pvt *pvt = (struct pvt *)this->private; - - ho_minimize(this); - nisfree(pvt, do_all); - if (pvt->res && pvt->free_res) - (*pvt->free_res)(pvt->res); - memput(pvt, sizeof *pvt); - memput(this, sizeof *this); -} - -static struct hostent * -ho_byname(struct irs_ho *this, const char *name) { - struct pvt *pvt = (struct pvt *)this->private; - struct hostent *hp; - - if (init(this) == -1) - return (NULL); - - if (pvt->res->options & RES_USE_INET6) { - hp = ho_byname2(this, name, AF_INET6); - if (hp) - return (hp); - } - return (ho_byname2(this, name, AF_INET)); -} - -static struct hostent * -ho_byname2(struct irs_ho *this, const char *name, int af) { - struct pvt *pvt = (struct pvt *)this->private; - int r; - char *tmp; - - UNUSED(af); - - if (init(this) == -1) - return (NULL); - - nisfree(pvt, do_val); - - strcpy(pvt->hostbuf, yp_multi); - strncat(pvt->hostbuf, name, sizeof(pvt->hostbuf) - sizeof(yp_multi)); - pvt->hostbuf[sizeof(pvt->hostbuf) - 1] = '\0'; - for (r = sizeof(yp_multi) - 1; pvt->hostbuf[r] != '\0'; r++) - if (isupper((unsigned char)pvt->hostbuf[r])) - tolower(pvt->hostbuf[r]); - - tmp = pvt->hostbuf; - r = yp_match(pvt->nis_domain, ipnode_byname, tmp, - strlen(tmp), &pvt->curval_data, &pvt->curval_len); - if (r != 0) { - tmp = pvt->hostbuf + sizeof(yp_multi) - 1; - r = yp_match(pvt->nis_domain, ipnode_byname, tmp, - strlen(tmp), &pvt->curval_data, &pvt->curval_len); - } - if (r != 0) { - tmp = pvt->hostbuf; - r = yp_match(pvt->nis_domain, hosts_byname, tmp, - strlen(tmp), &pvt->curval_data, &pvt->curval_len); - } - if (r != 0) { - tmp = pvt->hostbuf + sizeof(yp_multi) - 1; - r = yp_match(pvt->nis_domain, hosts_byname, tmp, - strlen(tmp), &pvt->curval_data, &pvt->curval_len); - } - if (r != 0) { - RES_SET_H_ERRNO(pvt->res, HOST_NOT_FOUND); - return (NULL); - } - return (makehostent(this)); -} - -static struct hostent * -ho_byaddr(struct irs_ho *this, const void *addr, int len, int af) { - struct pvt *pvt = (struct pvt *)this->private; - char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"]; - const u_char *uaddr = addr; - int r; - - if (init(this) == -1) - return (NULL); - - if (af == AF_INET6 && len == IN6ADDRSZ && - (!memcmp(uaddr, mapped, sizeof mapped) || - !memcmp(uaddr, tunnelled, sizeof tunnelled))) { - /* Unmap. */ - addr = (const u_char *)addr + sizeof mapped; - uaddr += sizeof mapped; - af = AF_INET; - len = INADDRSZ; - } - if (inet_ntop(af, uaddr, tmp, sizeof tmp) == NULL) { - RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL); - return (NULL); - } - nisfree(pvt, do_val); - r = yp_match(pvt->nis_domain, ipnode_byaddr, tmp, strlen(tmp), - &pvt->curval_data, &pvt->curval_len); - if (r != 0) - r = yp_match(pvt->nis_domain, hosts_byaddr, tmp, strlen(tmp), - &pvt->curval_data, &pvt->curval_len); - if (r != 0) { - RES_SET_H_ERRNO(pvt->res, HOST_NOT_FOUND); - return (NULL); - } - return (makehostent(this)); -} - -static struct hostent * -ho_next(struct irs_ho *this) { - struct pvt *pvt = (struct pvt *)this->private; - struct hostent *rval; - int r; - - if (init(this) == -1) - return (NULL); - - do { - if (pvt->needrewind) { - nisfree(pvt, do_all); - r = yp_first(pvt->nis_domain, hosts_byaddr, - &pvt->curkey_data, &pvt->curkey_len, - &pvt->curval_data, &pvt->curval_len); - pvt->needrewind = 0; - } else { - char *newkey_data; - int newkey_len; - - nisfree(pvt, do_val); - r = yp_next(pvt->nis_domain, hosts_byaddr, - pvt->curkey_data, pvt->curkey_len, - &newkey_data, &newkey_len, - &pvt->curval_data, &pvt->curval_len); - nisfree(pvt, do_key); - pvt->curkey_data = newkey_data; - pvt->curkey_len = newkey_len; - } - if (r != 0) { - RES_SET_H_ERRNO(pvt->res, HOST_NOT_FOUND); - return (NULL); - } - rval = makehostent(this); - } while (rval == NULL); - return (rval); -} - -static void -ho_rewind(struct irs_ho *this) { - struct pvt *pvt = (struct pvt *)this->private; - - pvt->needrewind = 1; -} - -static void -ho_minimize(struct irs_ho *this) { - struct pvt *pvt = (struct pvt *)this->private; - - if (pvt->res) - res_nclose(pvt->res); -} - -static struct __res_state * -ho_res_get(struct irs_ho *this) { - struct pvt *pvt = (struct pvt *)this->private; - - if (!pvt->res) { - struct __res_state *res; - res = (struct __res_state *)malloc(sizeof *res); - if (!res) { - errno = ENOMEM; - return (NULL); - } - memset(res, 0, sizeof *res); - ho_res_set(this, res, free); - } - - return (pvt->res); -} - -static void -ho_res_set(struct irs_ho *this, struct __res_state *res, - void (*free_res)(void *)) { - struct pvt *pvt = (struct pvt *)this->private; - - if (pvt->res && pvt->free_res) { - res_nclose(pvt->res); - (*pvt->free_res)(pvt->res); - } - - pvt->res = res; - pvt->free_res = free_res; -} - -struct nis_res_target { - struct nis_res_target *next; - int family; -}; - -/* XXX */ -extern struct addrinfo *hostent2addrinfo __P((struct hostent *, - const struct addrinfo *pai)); - -static struct addrinfo * -ho_addrinfo(struct irs_ho *this, const char *name, const struct addrinfo *pai) -{ - struct pvt *pvt = (struct pvt *)this->private; - struct hostent *hp; - struct nis_res_target q, q2, *p; - struct addrinfo sentinel, *cur; - - memset(&q, 0, sizeof(q2)); - memset(&q2, 0, sizeof(q2)); - memset(&sentinel, 0, sizeof(sentinel)); - cur = &sentinel; - - switch(pai->ai_family) { - case AF_UNSPEC: /*%< INET6 then INET4 */ - q.family = AF_INET6; - q.next = &q2; - q2.family = AF_INET; - break; - case AF_INET6: - q.family = AF_INET6; - break; - case AF_INET: - q.family = AF_INET; - break; - default: - RES_SET_H_ERRNO(pvt->res, NO_RECOVERY); /*%< ??? */ - return(NULL); - } - - for (p = &q; p; p = p->next) { - struct addrinfo *ai; - - hp = (*this->byname2)(this, name, p->family); - if (hp == NULL) { - /* byname2 should've set an appropriate error */ - continue; - } - if ((hp->h_name == NULL) || (hp->h_name[0] == 0) || - (hp->h_addr_list[0] == NULL)) { - RES_SET_H_ERRNO(pvt->res, NO_RECOVERY); - continue; - } - ai = hostent2addrinfo(hp, pai); - if (ai) { - cur->ai_next = ai; - while (cur && cur->ai_next) - cur = cur->ai_next; - } - } - - if (sentinel.ai_next == NULL) - RES_SET_H_ERRNO(pvt->res, HOST_NOT_FOUND); - - return(sentinel.ai_next); -} - -/* Private */ - -/*% -ipnodes: -::1 localhost -127.0.0.1 localhost -1.2.3.4 FOO bar -1.2.6.4 FOO bar -1.2.6.5 host - -ipnodes.byname: -YP_MULTI_localhost ::1,127.0.0.1 localhost -YP_MULTI_foo 1.2.3.4,1.2.6.4 FOO bar -YP_MULTI_bar 1.2.3.4,1.2.6.4 FOO bar -host 1.2.6.5 host - -hosts.byname: -localhost 127.0.0.1 localhost -host 1.2.6.5 host -YP_MULTI_foo 1.2.3.4,1.2.6.4 FOO bar -YP_MULTI_bar 1.2.3.4,1.2.6.4 FOO bar -*/ - -static struct hostent * -makehostent(struct irs_ho *this) { - struct pvt *pvt = (struct pvt *)this->private; - static const char spaces[] = " \t"; - char *cp, **q, *p, *comma, *ap; - int af = 0, len = 0; - int multi = 0; - int addr = 0; - - p = pvt->curval_data; - if ((cp = strpbrk(p, "#\n")) != NULL) - *cp = '\0'; - if (!(cp = strpbrk(p, spaces))) - return (NULL); - *cp++ = '\0'; - ap = pvt->hostbuf; - do { - if ((comma = strchr(p, ',')) != NULL) { - *comma++ = '\0'; - multi = 1; - } - if ((ap + IN6ADDRSZ) > (pvt->hostbuf + sizeof(pvt->hostbuf))) - break; - if ((pvt->res->options & RES_USE_INET6) && - inet_pton(AF_INET6, p, ap) > 0) { - af = AF_INET6; - len = IN6ADDRSZ; - } else if (inet_pton(AF_INET, p, pvt->host_addr) > 0) { - if (pvt->res->options & RES_USE_INET6) { - map_v4v6_address((char*)pvt->host_addr, ap); - af = AF_INET6; - len = IN6ADDRSZ; - } else { - af = AF_INET; - len = INADDRSZ; - } - } else { - if (!multi) - return (NULL); - continue; - } - if (addr < MAXADDRS) { - pvt->h_addr_ptrs[addr++] = ap; - pvt->h_addr_ptrs[addr] = NULL; - ap += len; - } - } while ((p = comma) != NULL); - if (ap == pvt->hostbuf) - return (NULL); - pvt->host.h_addr_list = pvt->h_addr_ptrs; - pvt->host.h_length = len; - pvt->host.h_addrtype = af; - cp += strspn(cp, spaces); - pvt->host.h_name = cp; - q = pvt->host.h_aliases = pvt->host_aliases; - if ((cp = strpbrk(cp, spaces)) != NULL) - *cp++ = '\0'; - while (cp && *cp) { - if (*cp == ' ' || *cp == '\t') { - cp++; - continue; - } - if (q < &pvt->host_aliases[MAXALIASES]) - *q++ = cp; - if ((cp = strpbrk(cp, spaces)) != NULL) - *cp++ = '\0'; - } - *q = NULL; - RES_SET_H_ERRNO(pvt->res, NETDB_SUCCESS); - return (&pvt->host); -} - -static void -nisfree(struct pvt *pvt, enum do_what do_what) { - if ((do_what & do_key) && pvt->curkey_data) { - free(pvt->curkey_data); - pvt->curkey_data = NULL; - } - if ((do_what & do_val) && pvt->curval_data) { - free(pvt->curval_data); - pvt->curval_data = NULL; - } -} - -static int -init(struct irs_ho *this) { - struct pvt *pvt = (struct pvt *)this->private; - - if (!pvt->res && !ho_res_get(this)) - return (-1); - if (((pvt->res->options & RES_INIT) == 0) && - res_ninit(pvt->res) == -1) - return (-1); - return (0); -} -#endif /*WANT_IRS_NIS*/ - -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/nis_ng.c /usr/src/lib/libc/net/nis_ng.c --- /home/reed/src/isc/libbind/libbind/irs/nis_ng.c 2005-04-26 23:56:32.000000000 -0500 +++ /usr/src/lib/libc/net/nis_ng.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,304 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1996,1999 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#if defined(LIBC_SCCS) && !defined(lint) -static const char rcsid[] = "$Id: nis_ng.c,v 1.4 2005/04/27 04:56:32 sra Exp $"; -#endif - -/* Imports */ - -#include "port_before.h" - -#ifndef WANT_IRS_NIS -static int __bind_irs_nis_unneeded; -#else - -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#include -#ifdef T_NULL -#undef T_NULL /* Silence re-definition warning of T_NULL. */ -#endif -#include -#include - -#include -#include - -#include "port_after.h" - -#include "irs_p.h" -#include "nis_p.h" - -/* Definitions */ - -struct tmpgrp { - const char * name; - const char * host; - const char * user; - const char * domain; - struct tmpgrp * next; -}; - -struct pvt { - char * nis_domain; - struct tmpgrp * tmp; - struct tmpgrp * cur; - char * tmpgroup; -}; - -enum do_what { do_none = 0x0, do_key = 0x1, do_val = 0x2, do_all = 0x3 }; - -static /*const*/ char netgroup_map[] = "netgroup"; - -/* Forward */ - -static void ng_close(struct irs_ng *); -static int ng_next(struct irs_ng *, const char **, - const char **, const char **); -static int ng_test(struct irs_ng *, - const char *, const char *, - const char *, const char *); -static void ng_rewind(struct irs_ng *, const char *); -static void ng_minimize(struct irs_ng *); - -static void add_group_to_list(struct pvt *, const char *, int); -static void add_tuple_to_list(struct pvt *, const char *, char *); -static void tmpfree(struct pvt *); - -/* Public */ - -struct irs_ng * -irs_nis_ng(struct irs_acc *this) { - struct irs_ng *ng; - struct pvt *pvt; - - if (!(ng = memget(sizeof *ng))) { - errno = ENOMEM; - return (NULL); - } - memset(ng, 0x5e, sizeof *ng); - if (!(pvt = memget(sizeof *pvt))) { - memput(ng, sizeof *ng); - errno = ENOMEM; - return (NULL); - } - memset(pvt, 0, sizeof *pvt); - pvt->nis_domain = ((struct nis_p *)this->private)->domain; - ng->private = pvt; - ng->close = ng_close; - ng->next = ng_next; - ng->test = ng_test; - ng->rewind = ng_rewind; - ng->minimize = ng_minimize; - return (ng); -} - -/* Methods */ - -static void -ng_close(struct irs_ng *this) { - struct pvt *pvt = (struct pvt *)this->private; - - tmpfree(pvt); - memput(pvt, sizeof *pvt); - memput(this, sizeof *this); -} - -static int -ng_next(struct irs_ng *this, const char **host, const char **user, const char **domain) { - struct pvt *pvt = (struct pvt *)this->private; - - if (!pvt->cur) - return (0); - *host = pvt->cur->host; - *user = pvt->cur->user; - *domain = pvt->cur->domain; - pvt->cur = pvt->cur->next; - return (1); -} - -static int -ng_test(struct irs_ng *this, const char *name, - const char *host, const char *user, const char *domain) -{ - struct pvt *pvt = (struct pvt *)this->private; - struct tmpgrp *cur; - - tmpfree(pvt); - add_group_to_list(pvt, name, strlen(name)); - for (cur = pvt->tmp; cur; cur = cur->next) { - if ((!host || !cur->host || !strcmp(host, cur->host)) && - (!user || !cur->user || !strcmp(user, cur->user)) && - (!domain || !cur->domain || !strcmp(domain, cur->domain))) - break; - } - tmpfree(pvt); - return ((cur == NULL) ? 0 : 1); -} - -static void -ng_rewind(struct irs_ng *this, const char *name) { - struct pvt *pvt = (struct pvt *)this->private; - - /* Either hand back or free the existing list. */ - if (pvt->tmpgroup) { - if (pvt->tmp && !strcmp(pvt->tmpgroup, name)) - goto reset; - tmpfree(pvt); - } - pvt->tmpgroup = strdup(name); - add_group_to_list(pvt, name, strlen(name)); - reset: - pvt->cur = pvt->tmp; -} - -static void -ng_minimize(struct irs_ng *this) { - UNUSED(this); - /* NOOP */ -} - -/* Private */ - -static void -add_group_to_list(struct pvt *pvt, const char *name, int len) { - char *vdata, *cp, *np; - struct tmpgrp *tmp; - int vlen, r; - char *nametmp; - - /* Don't add the same group to the list more than once. */ - for (tmp = pvt->tmp; tmp; tmp = tmp->next) - if (!strcmp(tmp->name, name)) - return; - - DE_CONST(name, nametmp); - r = yp_match(pvt->nis_domain, netgroup_map, nametmp, len, - &vdata, &vlen); - if (r == 0) { - cp = vdata; - if (*cp && cp[strlen(cp)-1] == '\n') - cp[strlen(cp)-1] = '\0'; - for ( ; cp; cp = np) { - np = strchr(cp, ' '); - if (np) - *np++ = '\0'; - if (*cp == '(') - add_tuple_to_list(pvt, name, cp); - else - add_group_to_list(pvt, cp, strlen(cp)); - } - free(vdata); - } -} - -static void -add_tuple_to_list(struct pvt *pvt, const char *name, char *cp) { - struct tmpgrp *tmp; - char *tp, *np; - - INSIST(*cp++ == '('); - - tmp = malloc(sizeof *tmp + strlen(name) + sizeof '\0' + - strlen(cp) - sizeof ')'); - if (!tmp) - return; - memset(tmp, 0, sizeof *tmp); - tp = ((char *)tmp) + sizeof *tmp; - - /* Name */ - strcpy(tp, name); - tmp->name = tp; - tp += strlen(tp) + 1; - - /* Host */ - if (!(np = strchr(cp, ','))) - goto cleanup; - *np++ = '\0'; - strcpy(tp, cp); - tmp->host = tp; - tp += strlen(tp) + 1; - cp = np; - - /* User */ - if (!(np = strchr(cp, ','))) - goto cleanup; - *np++ = '\0'; - strcpy(tp, cp); - tmp->user = tp; - tp += strlen(tp) + 1; - cp = np; - - /* Domain */ - if (!(np = strchr(cp, ')'))) - goto cleanup; - *np++ = '\0'; - strcpy(tp, cp); - tmp->domain = tp; - - /* - * Empty string in file means wildcard, but - * NULL string in return value means wildcard. - */ - if (!*tmp->host) - tmp->host = NULL; - if (!*tmp->user) - tmp->user = NULL; - if (!*tmp->domain) - tmp->domain = NULL; - - /* Add to list (LIFO). */ - tmp->next = pvt->tmp; - pvt->tmp = tmp; - return; - - cleanup: - free(tmp); -} - -static void -tmpfree(struct pvt *pvt) { - struct tmpgrp *cur, *next; - - if (pvt->tmpgroup) { - free(pvt->tmpgroup); - pvt->tmpgroup = NULL; - } - for (cur = pvt->tmp; cur; cur = next) { - next = cur->next; - free(cur); - } - pvt->tmp = NULL; -} - -#endif /*WANT_IRS_NIS*/ - -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/nis_nw.c /usr/src/lib/libc/net/nis_nw.c --- /home/reed/src/isc/libbind/libbind/irs/nis_nw.c 2005-04-26 23:56:33.000000000 -0500 +++ /usr/src/lib/libc/net/nis_nw.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,385 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1996,1999 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#if defined(LIBC_SCCS) && !defined(lint) -static const char rcsid[] = "$Id: nis_nw.c,v 1.4 2005/04/27 04:56:33 sra Exp $"; -#endif /* LIBC_SCCS and not lint */ - -/* Imports */ - -#include "port_before.h" - -#ifndef WANT_IRS_NIS -static int __bind_irs_nis_unneeded; -#else - -#include -#include - -#include -#include -#include -#ifdef T_NULL -#undef T_NULL /* Silence re-definition warning of T_NULL. */ -#endif -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -#include -#include - -#include "port_after.h" - -#include "irs_p.h" -#include "nis_p.h" - -/* Definitions */ - -#define MAXALIASES 35 -#define MAXADDRSIZE 4 - -struct pvt { - int needrewind; - char * nis_domain; - char * curkey_data; - int curkey_len; - char * curval_data; - int curval_len; - - struct nwent nwent; - char * nwbuf; - - char * aliases[MAXALIASES + 1]; - u_char addr[MAXADDRSIZE]; - - struct __res_state * res; - void (*free_res)(void *); -}; - -enum do_what { do_none = 0x0, do_key = 0x1, do_val = 0x2, do_all = 0x3 }; - -static /*const*/ char networks_byname[] = "networks.byname"; -static /*const*/ char networks_byaddr[] = "networks.byaddr"; - -/* Forward */ - -static void nw_close(struct irs_nw *); -static struct nwent * nw_byname(struct irs_nw *, const char *, int); -static struct nwent * nw_byaddr(struct irs_nw *, void *, int, int); -static struct nwent * nw_next(struct irs_nw *); -static void nw_rewind(struct irs_nw *); -static void nw_minimize(struct irs_nw *); -static struct __res_state * nw_res_get(struct irs_nw *this); -static void nw_res_set(struct irs_nw *this, - struct __res_state *res, - void (*free_res)(void *)); - -static struct nwent * makenwent(struct irs_nw *this); -static void nisfree(struct pvt *, enum do_what); -static int init(struct irs_nw *this); - -/* Public */ - -struct irs_nw * -irs_nis_nw(struct irs_acc *this) { - struct irs_nw *nw; - struct pvt *pvt; - - if (!(pvt = memget(sizeof *pvt))) { - errno = ENOMEM; - return (NULL); - } - memset(pvt, 0, sizeof *pvt); - if (!(nw = memget(sizeof *nw))) { - memput(pvt, sizeof *pvt); - errno = ENOMEM; - return (NULL); - } - memset(nw, 0x5e, sizeof *nw); - pvt->needrewind = 1; - pvt->nis_domain = ((struct nis_p *)this->private)->domain; - nw->private = pvt; - nw->close = nw_close; - nw->byname = nw_byname; - nw->byaddr = nw_byaddr; - nw->next = nw_next; - nw->rewind = nw_rewind; - nw->minimize = nw_minimize; - nw->res_get = nw_res_get; - nw->res_set = nw_res_set; - return (nw); -} - -/* Methods */ - -static void -nw_close(struct irs_nw *this) { - struct pvt *pvt = (struct pvt *)this->private; - - nw_minimize(this); - if (pvt->res && pvt->free_res) - (*pvt->free_res)(pvt->res); - if (pvt->nwbuf) - free(pvt->nwbuf); - memput(pvt, sizeof *pvt); - memput(this, sizeof *this); -} - -static struct nwent * -nw_byaddr(struct irs_nw *this, void *net, int length, int af) { - struct pvt *pvt = (struct pvt *)this->private; - char tmp[sizeof "255.255.255.255/32"], *t; - int r; - - if (init(this) == -1) - return (NULL); - - if (af != AF_INET) { - RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL); - errno = EAFNOSUPPORT; - return (NULL); - } - nisfree(pvt, do_val); - /* Try it with /CIDR first. */ - if (inet_net_ntop(AF_INET, net, length, tmp, sizeof tmp) == NULL) { - RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL); - return (NULL); - } - r = yp_match(pvt->nis_domain, networks_byaddr, tmp, strlen(tmp), - &pvt->curval_data, &pvt->curval_len); - if (r != 0) { - /* Give it a shot without the /CIDR. */ - if ((t = strchr(tmp, '/')) != NULL) { - *t = '\0'; - r = yp_match(pvt->nis_domain, networks_byaddr, - tmp, strlen(tmp), - &pvt->curval_data, &pvt->curval_len); - } - if (r != 0) { - RES_SET_H_ERRNO(pvt->res, HOST_NOT_FOUND); - return (NULL); - } - } - return (makenwent(this)); -} - -static struct nwent * -nw_byname(struct irs_nw *this, const char *name, int af) { - struct pvt *pvt = (struct pvt *)this->private; - int r; - char *tmp; - - if (init(this) == -1) - return (NULL); - - if (af != AF_INET) { - RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL); - errno = EAFNOSUPPORT; - return (NULL); - } - nisfree(pvt, do_val); - DE_CONST(name, tmp); - r = yp_match(pvt->nis_domain, networks_byname, tmp, - strlen(tmp), &pvt->curval_data, &pvt->curval_len); - if (r != 0) { - RES_SET_H_ERRNO(pvt->res, HOST_NOT_FOUND); - return (NULL); - } - return (makenwent(this)); -} - -static void -nw_rewind(struct irs_nw *this) { - struct pvt *pvt = (struct pvt *)this->private; - - pvt->needrewind = 1; -} - -static struct nwent * -nw_next(struct irs_nw *this) { - struct pvt *pvt = (struct pvt *)this->private; - struct nwent *rval; - int r; - - if (init(this) == -1) - return (NULL); - - do { - if (pvt->needrewind) { - nisfree(pvt, do_all); - r = yp_first(pvt->nis_domain, networks_byaddr, - &pvt->curkey_data, &pvt->curkey_len, - &pvt->curval_data, &pvt->curval_len); - pvt->needrewind = 0; - } else { - char *newkey_data; - int newkey_len; - - nisfree(pvt, do_val); - r = yp_next(pvt->nis_domain, networks_byaddr, - pvt->curkey_data, pvt->curkey_len, - &newkey_data, &newkey_len, - &pvt->curval_data, &pvt->curval_len); - nisfree(pvt, do_key); - pvt->curkey_data = newkey_data; - pvt->curkey_len = newkey_len; - } - if (r != 0) { - RES_SET_H_ERRNO(pvt->res, HOST_NOT_FOUND); - return (NULL); - } - rval = makenwent(this); - } while (rval == NULL); - return (rval); -} - -static void -nw_minimize(struct irs_nw *this) { - struct pvt *pvt = (struct pvt *)this->private; - - if (pvt->res) - res_nclose(pvt->res); -} - -static struct __res_state * -nw_res_get(struct irs_nw *this) { - struct pvt *pvt = (struct pvt *)this->private; - - if (!pvt->res) { - struct __res_state *res; - res = (struct __res_state *)malloc(sizeof *res); - if (!res) { - errno = ENOMEM; - return (NULL); - } - memset(res, 0, sizeof *res); - nw_res_set(this, res, free); - } - - return (pvt->res); -} - -static void -nw_res_set(struct irs_nw *this, struct __res_state *res, - void (*free_res)(void *)) { - struct pvt *pvt = (struct pvt *)this->private; - - if (pvt->res && pvt->free_res) { - res_nclose(pvt->res); - (*pvt->free_res)(pvt->res); - } - - pvt->res = res; - pvt->free_res = free_res; -} - -/* Private */ - -static struct nwent * -makenwent(struct irs_nw *this) { - struct pvt *pvt = (struct pvt *)this->private; - static const char spaces[] = " \t"; - char *t, *cp, **ap; - - if (pvt->nwbuf) - free(pvt->nwbuf); - pvt->nwbuf = pvt->curval_data; - pvt->curval_data = NULL; - - if ((cp = strpbrk(pvt->nwbuf, "#\n")) != NULL) - *cp = '\0'; - cp = pvt->nwbuf; - - /* Name */ - pvt->nwent.n_name = cp; - cp += strcspn(cp, spaces); - if (!*cp) - goto cleanup; - *cp++ = '\0'; - cp += strspn(cp, spaces); - - /* Network */ - pvt->nwent.n_addrtype = AF_INET; - t = cp + strcspn(cp, spaces); - if (*t) - *t++ = '\0'; - pvt->nwent.n_length = inet_net_pton(AF_INET, cp, - pvt->addr, sizeof pvt->addr); - if (pvt->nwent.n_length < 0) - goto cleanup; - pvt->nwent.n_addr = pvt->addr; - cp = t; - - /* Aliases */ - ap = pvt->nwent.n_aliases = pvt->aliases; - while (*cp) { - if (ap >= &pvt->aliases[MAXALIASES]) - break; - *ap++ = cp; - cp += strcspn(cp, spaces); - if (!*cp) - break; - *cp++ = '\0'; - cp += strspn(cp, spaces); - } - *ap = NULL; - - return (&pvt->nwent); - - cleanup: - if (pvt->nwbuf) { - free(pvt->nwbuf); - pvt->nwbuf = NULL; - } - return (NULL); -} - -static void -nisfree(struct pvt *pvt, enum do_what do_what) { - if ((do_what & do_key) && pvt->curkey_data) { - free(pvt->curkey_data); - pvt->curkey_data = NULL; - } - if ((do_what & do_val) && pvt->curval_data) { - free(pvt->curval_data); - pvt->curval_data = NULL; - } -} - -static int -init(struct irs_nw *this) { - struct pvt *pvt = (struct pvt *)this->private; - - if (!pvt->res && !nw_res_get(this)) - return (-1); - if (((pvt->res->options & RES_INIT) == 0) && - res_ninit(pvt->res) == -1) - return (-1); - return (0); -} - -#endif /*WANT_IRS_NIS*/ - -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/nis_p.h /usr/src/lib/libc/net/nis_p.h --- /home/reed/src/isc/libbind/libbind/irs/nis_p.h 2005-04-26 23:56:33.000000000 -0500 +++ /usr/src/lib/libc/net/nis_p.h 1969-12-31 18:00:00.000000000 -0600 @@ -1,47 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1996,1999 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -/* - * $Id: nis_p.h,v 1.3 2005/04/27 04:56:33 sra Exp $ - */ - -/*! \file - * \brief - * nis_p.h - private include file for the NIS functions. - */ - -/*% - * Object state. - */ -struct nis_p { - char * domain; - struct __res_state * res; - void (*free_res) __P((void *)); -}; - - -/* - * Methods. - */ - -extern struct irs_gr * irs_nis_gr __P((struct irs_acc *)); -extern struct irs_pw * irs_nis_pw __P((struct irs_acc *)); -extern struct irs_sv * irs_nis_sv __P((struct irs_acc *)); -extern struct irs_pr * irs_nis_pr __P((struct irs_acc *)); -extern struct irs_ho * irs_nis_ho __P((struct irs_acc *)); -extern struct irs_nw * irs_nis_nw __P((struct irs_acc *)); -extern struct irs_ng * irs_nis_ng __P((struct irs_acc *)); diff -pruN /home/reed/src/isc/libbind/libbind/irs/nis_pr.c /usr/src/lib/libc/net/nis_pr.c --- /home/reed/src/isc/libbind/libbind/irs/nis_pr.c 2005-04-26 23:56:33.000000000 -0500 +++ /usr/src/lib/libc/net/nis_pr.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,302 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1996,1999 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#if defined(LIBC_SCCS) && !defined(lint) -static const char rcsid[] = "$Id: nis_pr.c,v 1.4 2005/04/27 04:56:33 sra Exp $"; -#endif - -/* Imports */ - -#include "port_before.h" - -#ifndef WANT_IRS_NIS -static int __bind_irs_nis_unneeded; -#else - -#include -#include -#include -#include -#ifdef T_NULL -#undef T_NULL /* Silence re-definition warning of T_NULL. */ -#endif -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include - -#include -#include - -#include "port_after.h" - -#include "irs_p.h" -#include "nis_p.h" - -/* Definitions */ - -struct pvt { - int needrewind; - char * nis_domain; - char * curkey_data; - int curkey_len; - char * curval_data; - int curval_len; - struct protoent proto; - char * prbuf; -}; - -enum do_what { do_none = 0x0, do_key = 0x1, do_val = 0x2, do_all = 0x3 }; - -static /*const*/ char protocols_byname[] = "protocols.byname"; -static /*const*/ char protocols_bynumber[] = "protocols.bynumber"; - -/* Forward */ - -static void pr_close(struct irs_pr *); -static struct protoent * pr_byname(struct irs_pr *, const char *); -static struct protoent * pr_bynumber(struct irs_pr *, int); -static struct protoent * pr_next(struct irs_pr *); -static void pr_rewind(struct irs_pr *); -static void pr_minimize(struct irs_pr *); - -static struct protoent * makeprotoent(struct irs_pr *this); -static void nisfree(struct pvt *, enum do_what); - -/* Public */ - -struct irs_pr * -irs_nis_pr(struct irs_acc *this) { - struct irs_pr *pr; - struct pvt *pvt; - - if (!(pr = memget(sizeof *pr))) { - errno = ENOMEM; - return (NULL); - } - memset(pr, 0x5e, sizeof *pr); - if (!(pvt = memget(sizeof *pvt))) { - memput(pr, sizeof *pr); - errno = ENOMEM; - return (NULL); - } - memset(pvt, 0, sizeof *pvt); - pvt->needrewind = 1; - pvt->nis_domain = ((struct nis_p *)this->private)->domain; - pr->private = pvt; - pr->byname = pr_byname; - pr->bynumber = pr_bynumber; - pr->next = pr_next; - pr->rewind = pr_rewind; - pr->close = pr_close; - pr->minimize = pr_minimize; - pr->res_get = NULL; - pr->res_set = NULL; - return (pr); -} - -/* Methods. */ - -static void -pr_close(struct irs_pr *this) { - struct pvt *pvt = (struct pvt *)this->private; - - nisfree(pvt, do_all); - if (pvt->proto.p_aliases) - free(pvt->proto.p_aliases); - if (pvt->prbuf) - free(pvt->prbuf); - memput(pvt, sizeof *pvt); - memput(this, sizeof *this); -} - -static struct protoent * -pr_byname(struct irs_pr *this, const char *name) { - struct pvt *pvt = (struct pvt *)this->private; - int r; - char *tmp; - - nisfree(pvt, do_val); - DE_CONST(name, tmp); - r = yp_match(pvt->nis_domain, protocols_byname, tmp, - strlen(tmp), &pvt->curval_data, &pvt->curval_len); - if (r != 0) { - errno = ENOENT; - return (NULL); - } - return (makeprotoent(this)); -} - -static struct protoent * -pr_bynumber(struct irs_pr *this, int num) { - struct pvt *pvt = (struct pvt *)this->private; - char tmp[sizeof "-4294967295"]; - int r; - - nisfree(pvt, do_val); - (void) sprintf(tmp, "%d", num); - r = yp_match(pvt->nis_domain, protocols_bynumber, tmp, strlen(tmp), - &pvt->curval_data, &pvt->curval_len); - if (r != 0) { - errno = ENOENT; - return (NULL); - } - return (makeprotoent(this)); -} - -static struct protoent * -pr_next(struct irs_pr *this) { - struct pvt *pvt = (struct pvt *)this->private; - struct protoent *rval; - int r; - - do { - if (pvt->needrewind) { - nisfree(pvt, do_all); - r = yp_first(pvt->nis_domain, protocols_bynumber, - &pvt->curkey_data, &pvt->curkey_len, - &pvt->curval_data, &pvt->curval_len); - pvt->needrewind = 0; - } else { - char *newkey_data; - int newkey_len; - - nisfree(pvt, do_val); - r = yp_next(pvt->nis_domain, protocols_bynumber, - pvt->curkey_data, pvt->curkey_len, - &newkey_data, &newkey_len, - &pvt->curval_data, &pvt->curval_len); - nisfree(pvt, do_key); - pvt->curkey_data = newkey_data; - pvt->curkey_len = newkey_len; - } - if (r != 0) { - errno = ENOENT; - return (NULL); - } - rval = makeprotoent(this); - } while (rval == NULL); - return (rval); -} - -static void -pr_rewind(struct irs_pr *this) { - struct pvt *pvt = (struct pvt *)this->private; - - pvt->needrewind = 1; -} - -static void -pr_minimize(struct irs_pr *this) { - UNUSED(this); - /* NOOP */ -} - -/* Private */ - -static struct protoent * -makeprotoent(struct irs_pr *this) { - struct pvt *pvt = (struct pvt *)this->private; - char *p, **t; - int n, m; - - if (pvt->prbuf) - free(pvt->prbuf); - pvt->prbuf = pvt->curval_data; - pvt->curval_data = NULL; - - for (p = pvt->prbuf; *p && *p != '#';) - p++; - while (p > pvt->prbuf && isspace((unsigned char)(p[-1]))) - p--; - *p = '\0'; - - p = pvt->prbuf; - n = m = 0; - - pvt->proto.p_name = p; - while (*p && !isspace((unsigned char)*p)) - p++; - if (!*p) - return (NULL); - *p++ = '\0'; - - while (*p && isspace((unsigned char)*p)) - p++; - pvt->proto.p_proto = atoi(p); - while (*p && !isspace((unsigned char)*p)) - p++; - *p++ = '\0'; - - while (*p) { - if ((n + 1) >= m || !pvt->proto.p_aliases) { - m += 10; - t = realloc(pvt->proto.p_aliases, - m * sizeof(char *)); - if (!t) { - errno = ENOMEM; - goto cleanup; - } - pvt->proto.p_aliases = t; - } - pvt->proto.p_aliases[n++] = p; - while (*p && !isspace((unsigned char)*p)) - p++; - if (*p) - *p++ = '\0'; - } - if (!pvt->proto.p_aliases) - pvt->proto.p_aliases = malloc(sizeof(char *)); - if (!pvt->proto.p_aliases) - goto cleanup; - pvt->proto.p_aliases[n] = NULL; - return (&pvt->proto); - - cleanup: - if (pvt->proto.p_aliases) { - free(pvt->proto.p_aliases); - pvt->proto.p_aliases = NULL; - } - if (pvt->prbuf) { - free(pvt->prbuf); - pvt->prbuf = NULL; - } - return (NULL); -} - -static void -nisfree(struct pvt *pvt, enum do_what do_what) { - if ((do_what & do_key) && pvt->curkey_data) { - free(pvt->curkey_data); - pvt->curkey_data = NULL; - } - if ((do_what & do_val) && pvt->curval_data) { - free(pvt->curval_data); - pvt->curval_data = NULL; - } -} - -#endif /*WANT_IRS_NIS*/ - -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/nis_pw.c /usr/src/lib/libc/net/nis_pw.c --- /home/reed/src/isc/libbind/libbind/irs/nis_pw.c 2005-04-26 23:56:33.000000000 -0500 +++ /usr/src/lib/libc/net/nis_pw.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,288 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1996,1999 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#if defined(LIBC_SCCS) && !defined(lint) -static const char rcsid[] = "$Id: nis_pw.c,v 1.4 2005/04/27 04:56:33 sra Exp $"; -#endif /* LIBC_SCCS and not lint */ - -/* Imports */ - -#include "port_before.h" - -#if !defined(WANT_IRS_PW) || !defined(WANT_IRS_NIS) -static int __bind_irs_pw_unneeded; -#else - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#include - -#include - -#include "port_after.h" - -#include "irs_p.h" -#include "nis_p.h" - -/* Definitions */ - -struct pvt { - int needrewind; - char * nis_domain; - char * curkey_data; - int curkey_len; - char * curval_data; - int curval_len; - struct passwd passwd; - char * pwbuf; -}; - -enum do_what { do_none = 0x0, do_key = 0x1, do_val = 0x2, do_all = 0x3 }; - -static /*const*/ char passwd_byname[] = "passwd.byname"; -static /*const*/ char passwd_byuid[] = "passwd.byuid"; - -/* Forward */ - -static void pw_close(struct irs_pw *); -static struct passwd * pw_next(struct irs_pw *); -static struct passwd * pw_byname(struct irs_pw *, const char *); -static struct passwd * pw_byuid(struct irs_pw *, uid_t); -static void pw_rewind(struct irs_pw *); -static void pw_minimize(struct irs_pw *); - -static struct passwd * makepasswdent(struct irs_pw *); -static void nisfree(struct pvt *, enum do_what); - -/* Public */ - -struct irs_pw * -irs_nis_pw(struct irs_acc *this) { - struct irs_pw *pw; - struct pvt *pvt; - - if (!(pw = memget(sizeof *pw))) { - errno = ENOMEM; - return (NULL); - } - memset(pw, 0x5e, sizeof *pw); - if (!(pvt = memget(sizeof *pvt))) { - memput(pw, sizeof *pw); - errno = ENOMEM; - return (NULL); - } - memset(pvt, 0, sizeof *pvt); - pvt->needrewind = 1; - pvt->nis_domain = ((struct nis_p *)this->private)->domain; - pw->private = pvt; - pw->close = pw_close; - pw->next = pw_next; - pw->byname = pw_byname; - pw->byuid = pw_byuid; - pw->rewind = pw_rewind; - pw->minimize = pw_minimize; - pw->res_get = NULL; - pw->res_set = NULL; - return (pw); -} - -/* Methods */ - -static void -pw_close(struct irs_pw *this) { - struct pvt *pvt = (struct pvt *)this->private; - - if (pvt->pwbuf) - free(pvt->pwbuf); - nisfree(pvt, do_all); - memput(pvt, sizeof *pvt); - memput(this, sizeof *this); -} - -static struct passwd * -pw_next(struct irs_pw *this) { - struct pvt *pvt = (struct pvt *)this->private; - struct passwd *rval; - int r; - - do { - if (pvt->needrewind) { - nisfree(pvt, do_all); - r = yp_first(pvt->nis_domain, passwd_byname, - &pvt->curkey_data, &pvt->curkey_len, - &pvt->curval_data, &pvt->curval_len); - pvt->needrewind = 0; - } else { - char *newkey_data; - int newkey_len; - - nisfree(pvt, do_val); - r = yp_next(pvt->nis_domain, passwd_byname, - pvt->curkey_data, pvt->curkey_len, - &newkey_data, &newkey_len, - &pvt->curval_data, &pvt->curval_len); - nisfree(pvt, do_key); - pvt->curkey_data = newkey_data; - pvt->curkey_len = newkey_len; - } - if (r != 0) { - errno = ENOENT; - return (NULL); - } - rval = makepasswdent(this); - } while (rval == NULL); - return (rval); -} - -static struct passwd * -pw_byname(struct irs_pw *this, const char *name) { - struct pvt *pvt = (struct pvt *)this->private; - int r; - char *tmp; - - nisfree(pvt, do_val); - DE_CONST(name, tmp); - r = yp_match(pvt->nis_domain, passwd_byname, tmp, strlen(tmp), - &pvt->curval_data, &pvt->curval_len); - if (r != 0) { - errno = ENOENT; - return (NULL); - } - return (makepasswdent(this)); -} - -static struct passwd * -pw_byuid(struct irs_pw *this, uid_t uid) { - struct pvt *pvt = (struct pvt *)this->private; - char tmp[sizeof "4294967295"]; - int r; - - nisfree(pvt, do_val); - (void) sprintf(tmp, "%u", (unsigned int)uid); - r = yp_match(pvt->nis_domain, passwd_byuid, tmp, strlen(tmp), - &pvt->curval_data, &pvt->curval_len); - if (r != 0) { - errno = ENOENT; - return (NULL); - } - return (makepasswdent(this)); -} - -static void -pw_rewind(struct irs_pw *this) { - struct pvt *pvt = (struct pvt *)this->private; - - pvt->needrewind = 1; -} - -static void -pw_minimize(struct irs_pw *this) { - UNUSED(this); - /* NOOP */ -} - -/* Private */ - -static struct passwd * -makepasswdent(struct irs_pw *this) { - struct pvt *pvt = (struct pvt *)this->private; - char *cp; - - memset(&pvt->passwd, 0, sizeof pvt->passwd); - if (pvt->pwbuf) - free(pvt->pwbuf); - pvt->pwbuf = pvt->curval_data; - pvt->curval_data = NULL; - - cp = pvt->pwbuf; - pvt->passwd.pw_name = cp; - if (!(cp = strchr(cp, ':'))) - goto cleanup; -#ifdef HAS_PW_CLASS - pvt->passwd.pw_class = cp; /*%< Needs to point at a \0. */ -#endif - *cp++ = '\0'; - - pvt->passwd.pw_passwd = cp; - if (!(cp = strchr(cp, ':'))) - goto cleanup; - *cp++ = '\0'; - - pvt->passwd.pw_uid = atoi(cp); - if (!(cp = strchr(cp, ':'))) - goto cleanup; - *cp++ = '\0'; - - pvt->passwd.pw_gid = atoi(cp); - if (!(cp = strchr(cp, ':'))) - goto cleanup; - *cp++ = '\0'; - - pvt->passwd.pw_gecos = cp; - if (!(cp = strchr(cp, ':'))) - goto cleanup; - *cp++ = '\0'; - - pvt->passwd.pw_dir = cp; - if (!(cp = strchr(cp, ':'))) - goto cleanup; - *cp++ = '\0'; - - pvt->passwd.pw_shell = cp; - - if ((cp = strchr(cp, '\n')) != NULL) - *cp = '\0'; - - return (&pvt->passwd); - - cleanup: - free(pvt->pwbuf); - pvt->pwbuf = NULL; - return (NULL); -} - -static void -nisfree(struct pvt *pvt, enum do_what do_what) { - if ((do_what & do_key) && pvt->curkey_data) { - free(pvt->curkey_data); - pvt->curkey_data = NULL; - } - if ((do_what & do_val) && pvt->curval_data) { - free(pvt->curval_data); - pvt->curval_data = NULL; - } -} - -#endif /* WANT_IRS_PW && WANT_IRS_NIS */ -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/nis_sv.c /usr/src/lib/libc/net/nis_sv.c --- /home/reed/src/isc/libbind/libbind/irs/nis_sv.c 2005-04-26 23:56:34.000000000 -0500 +++ /usr/src/lib/libc/net/nis_sv.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,310 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1996,1999 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#if defined(LIBC_SCCS) && !defined(lint) -static const char rcsid[] = "$Id: nis_sv.c,v 1.4 2005/04/27 04:56:34 sra Exp $"; -#endif /* LIBC_SCCS and not lint */ - -/* Imports */ - -#include "port_before.h" - -#ifndef WANT_IRS_NIS -static int __bind_irs_nis_unneeded; -#else - -#include -#include -#include -#include -#include -#ifdef T_NULL -#undef T_NULL /* Silence re-definition warning of T_NULL. */ -#endif -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -#include -#include - -#include "port_after.h" - -#include "irs_p.h" -#include "nis_p.h" - -/* Definitions */ - -struct pvt { - int needrewind; - char * nis_domain; - char * curkey_data; - int curkey_len; - char * curval_data; - int curval_len; - char line[BUFSIZ+1]; - struct servent serv; - char * svbuf; -}; - -enum do_what { do_none = 0x0, do_key = 0x1, do_val = 0x2, do_all = 0x3 }; - -static /*const*/ char services_byname[] = "services.byname"; - -/* Forward */ - -static void sv_close(struct irs_sv*); -static struct servent * sv_next(struct irs_sv *); -static struct servent * sv_byname(struct irs_sv *, const char *, - const char *); -static struct servent * sv_byport(struct irs_sv *, int, const char *); -static void sv_rewind(struct irs_sv *); -static void sv_minimize(struct irs_sv *); - -static struct servent * makeservent(struct irs_sv *this); -static void nisfree(struct pvt *, enum do_what); - -/* Public */ - -struct irs_sv * -irs_nis_sv(struct irs_acc *this) { - struct irs_sv *sv; - struct pvt *pvt; - - if (!(sv = memget(sizeof *sv))) { - errno = ENOMEM; - return (NULL); - } - memset(sv, 0x5e, sizeof *sv); - if (!(pvt = memget(sizeof *pvt))) { - memput(sv, sizeof *sv); - errno = ENOMEM; - return (NULL); - } - memset(pvt, 0, sizeof *pvt); - pvt->needrewind = 1; - pvt->nis_domain = ((struct nis_p *)this->private)->domain; - sv->private = pvt; - sv->close = sv_close; - sv->next = sv_next; - sv->byname = sv_byname; - sv->byport = sv_byport; - sv->rewind = sv_rewind; - sv->minimize = sv_minimize; - sv->res_get = NULL; - sv->res_set = NULL; - return (sv); -} - -/* Methods */ - -static void -sv_close(struct irs_sv *this) { - struct pvt *pvt = (struct pvt *)this->private; - - nisfree(pvt, do_all); - if (pvt->serv.s_aliases) - free(pvt->serv.s_aliases); - if (pvt->svbuf) - free(pvt->svbuf); - memput(pvt, sizeof *pvt); - memput(this, sizeof *this); -} - -static struct servent * -sv_byname(struct irs_sv *this, const char *name, const char *proto) { - struct servent *serv; - char **sap; - - sv_rewind(this); - while ((serv = sv_next(this)) != NULL) { - if (proto != NULL && strcmp(proto, serv->s_proto)) - continue; - if (!strcmp(name, serv->s_name)) - break; - for (sap = serv->s_aliases; sap && *sap; sap++) - if (!strcmp(name, *sap)) - break; - } - return (serv); -} - -static struct servent * -sv_byport(struct irs_sv *this, int port, const char *proto) { - struct servent *serv; - - sv_rewind(this); - while ((serv = sv_next(this)) != NULL) { - if (proto != NULL && strcmp(proto, serv->s_proto)) - continue; - if (serv->s_port == port) - break; - } - return (serv); -} - -static void -sv_rewind(struct irs_sv *this) { - struct pvt *pvt = (struct pvt *)this->private; - - pvt->needrewind = 1; -} - -static struct servent * -sv_next(struct irs_sv *this) { - struct pvt *pvt = (struct pvt *)this->private; - struct servent *rval; - int r; - - do { - if (pvt->needrewind) { - nisfree(pvt, do_all); - r = yp_first(pvt->nis_domain, services_byname, - &pvt->curkey_data, &pvt->curkey_len, - &pvt->curval_data, &pvt->curval_len); - pvt->needrewind = 0; - } else { - char *newkey_data; - int newkey_len; - - nisfree(pvt, do_val); - r = yp_next(pvt->nis_domain, services_byname, - pvt->curkey_data, pvt->curkey_len, - &newkey_data, &newkey_len, - &pvt->curval_data, &pvt->curval_len); - nisfree(pvt, do_key); - pvt->curkey_data = newkey_data; - pvt->curkey_len = newkey_len; - } - if (r != 0) { - errno = ENOENT; - return (NULL); - } - rval = makeservent(this); - } while (rval == NULL); - return (rval); -} - -static void -sv_minimize(struct irs_sv *this) { - UNUSED(this); - /* NOOP */ -} - -/* Private */ - -static struct servent * -makeservent(struct irs_sv *this) { - struct pvt *pvt = (struct pvt *)this->private; - static const char spaces[] = " \t"; - char *p, **t; - int n, m; - - if (pvt->svbuf) - free(pvt->svbuf); - pvt->svbuf = pvt->curval_data; - pvt->curval_data = NULL; - - if (pvt->serv.s_aliases) { - free(pvt->serv.s_aliases); - pvt->serv.s_aliases = NULL; - } - - if ((p = strpbrk(pvt->svbuf, "#\n"))) - *p = '\0'; - - p = pvt->svbuf; - - pvt->serv.s_name = p; - p += strcspn(p, spaces); - if (!*p) - goto cleanup; - *p++ = '\0'; - p += strspn(p, spaces); - - pvt->serv.s_port = htons((u_short) atoi(p)); - pvt->serv.s_proto = NULL; - - while (*p && !isspace((unsigned char)*p)) - if (*p++ == '/') - pvt->serv.s_proto = p; - if (!pvt->serv.s_proto) - goto cleanup; - if (*p) { - *p++ = '\0'; - p += strspn(p, spaces); - } - - n = m = 0; - while (*p) { - if ((n + 1) >= m || !pvt->serv.s_aliases) { - m += 10; - t = realloc(pvt->serv.s_aliases, m * sizeof(char *)); - if (!t) { - errno = ENOMEM; - goto cleanup; - } - pvt->serv.s_aliases = t; - } - pvt->serv.s_aliases[n++] = p; - p += strcspn(p, spaces); - if (!*p) - break; - *p++ = '\0'; - p += strspn(p, spaces); - } - if (!pvt->serv.s_aliases) - pvt->serv.s_aliases = malloc(sizeof(char *)); - if (!pvt->serv.s_aliases) - goto cleanup; - pvt->serv.s_aliases[n] = NULL; - return (&pvt->serv); - - cleanup: - if (pvt->serv.s_aliases) { - free(pvt->serv.s_aliases); - pvt->serv.s_aliases = NULL; - } - if (pvt->svbuf) { - free(pvt->svbuf); - pvt->svbuf = NULL; - } - return (NULL); -} - -static void -nisfree(struct pvt *pvt, enum do_what do_what) { - if ((do_what & do_key) && pvt->curkey_data) { - free(pvt->curkey_data); - pvt->curkey_data = NULL; - } - if ((do_what & do_val) && pvt->curval_data) { - free(pvt->curval_data); - pvt->curval_data = NULL; - } -} - -#endif /*WANT_IRS_NIS*/ - -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/nsdispatch.c /usr/src/lib/libc/net/nsdispatch.c --- /home/reed/src/isc/libbind/libbind/irs/nsdispatch.c 1969-12-31 18:00:00.000000000 -0600 +++ /usr/src/lib/libc/net/nsdispatch.c 2013-06-05 09:26:13.000000000 -0500 @@ -0,0 +1,692 @@ +/* $NetBSD: nsdispatch.c,v 1.37 2012/03/13 21:13:42 christos Exp $ */ + +/*- + * Copyright (c) 1997, 1998, 1999, 2004 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Luke Mewburn; and by Jason R. Thorpe. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +/*- + * Copyright (c) 2003 Networks Associates Technology, Inc. + * All rights reserved. + * + * Portions of this software were developed for the FreeBSD Project by + * Jacques A. Vidrine, Safeport Network Services, and Network + * Associates Laboratories, the Security Research Division of Network + * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 + * ("CBOSS"), as part of the DARPA CHATS research program. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: nsdispatch.c,v 1.37 2012/03/13 21:13:42 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include +#include +#include +#include + +#include +#ifdef __ELF__ +#include +#endif /* __ELF__ */ +#include +#include +#define _NS_PRIVATE +#include +#include +#include +#include +#include +#include + +#include "reentrant.h" + +extern FILE *_nsyyin; +extern int _nsyyparse(void); + + +#ifdef __weak_alias +__weak_alias(nsdispatch,_nsdispatch) +#endif + + +/* + * default sourcelist: `files' + */ +const ns_src __nsdefaultsrc[] = { + { NSSRC_FILES, NS_SUCCESS }, + { 0, 0 }, +}; + +const ns_src __nsdefaultcompat[] = { + { NSSRC_COMPAT, NS_SUCCESS }, + { 0, 0 } +}; + +const ns_src __nsdefaultcompat_forceall[] = { + { NSSRC_COMPAT, NS_SUCCESS | NS_FORCEALL }, + { 0, 0 } +}; + +const ns_src __nsdefaultfiles[] = { + { NSSRC_FILES, NS_SUCCESS }, + { 0, 0 }, +}; + +const ns_src __nsdefaultfiles_forceall[] = { + { NSSRC_FILES, NS_SUCCESS | NS_FORCEALL }, + { 0, 0 }, +}; + +const ns_src __nsdefaultnis[] = { + { NSSRC_NIS, NS_SUCCESS }, + { 0, 0 } +}; + +const ns_src __nsdefaultnis_forceall[] = { + { NSSRC_NIS, NS_SUCCESS | NS_FORCEALL }, + { 0, 0 } +}; + + +/* Database, source mappings. */ +static u_int _nsmapsize; +static ns_dbt *_nsmap; + +/* Nsswitch modules. */ +static u_int _nsmodsize; +static ns_mod *_nsmod; + +/* Placeholder for built-in modules' dlopen() handles. */ +static void *_nsbuiltin = &_nsbuiltin; + +#ifdef _REENTRANT +/* + * Global nsswitch data structures are mostly read-only, but we update them + * when we read or re-read nsswitch.conf. + */ +static rwlock_t _nslock = RWLOCK_INITIALIZER; + +/* + * List of threads currently in nsdispatch(). We use this to detect + * recursive calls and avoid reloading configuration in such cases, + * which could cause deadlock. + */ +struct _ns_drec { + LIST_ENTRY(_ns_drec) list; + thr_t thr; +}; +static LIST_HEAD(, _ns_drec) _ns_drec = LIST_HEAD_INITIALIZER(&_ns_drec); +static mutex_t _ns_drec_lock = MUTEX_INITIALIZER; +#endif /* _REENTRANT */ + + +/* + * Runtime determination of whether we are dynamically linked or not. + */ +#ifndef __ELF__ +#define is_dynamic() (0) /* don't bother - switch to ELF! */ +#else +__weakref_visible int rtld_DYNAMIC __weak_reference(_DYNAMIC); +#define is_dynamic() (&rtld_DYNAMIC != NULL) +#endif + + +/* + * size of dynamic array chunk for _nsmap and _nsmap[x].srclist (and other + * growing arrays). + */ +#define NSELEMSPERCHUNK 8 + +/* + * Dynamically growable arrays are used for lists of databases, sources, + * and modules. The following "vector" API is used to isolate the + * common operations. + */ +typedef void (*_nsvect_free_elem)(void *); + +static void * +_nsvect_append(const void *elem, void *vec, u_int *count, size_t esize) +{ + void *p; + + if ((*count % NSELEMSPERCHUNK) == 0) { + p = realloc(vec, (*count + NSELEMSPERCHUNK) * esize); + if (p == NULL) + return (NULL); + vec = p; + } + memmove((void *)(((uintptr_t)vec) + (*count * esize)), elem, esize); + (*count)++; + return (vec); +} + +static void * +_nsvect_elem(u_int i, void *vec, u_int count, size_t esize) +{ + + if (i < count) + return ((void *)((uintptr_t)vec + (i * esize))); + else + return (NULL); +} + +static void +_nsvect_free(void *vec, u_int *count, size_t esize, _nsvect_free_elem free_elem) +{ + void *elem; + u_int i; + + for (i = 0; i < *count; i++) { + elem = _nsvect_elem(i, vec, *count, esize); + if (elem != NULL) + (*free_elem)(elem); + } + if (vec != NULL) + free(vec); + *count = 0; +} +#define _NSVECT_FREE(v, c, s, f) \ +do { \ + _nsvect_free((v), (c), (s), (f)); \ + (v) = NULL; \ +} while (/*CONSTCOND*/0) + +static int +_nsdbtcmp(const void *a, const void *b) +{ + + return (strcasecmp(((const ns_dbt *)a)->name, + ((const ns_dbt *)b)->name)); +} + +static int +_nsmodcmp(const void *a, const void *b) +{ + + return (strcasecmp(((const ns_mod *)a)->name, + ((const ns_mod *)b)->name)); +} + +static int +_nsmtabcmp(const void *a, const void *b) +{ + int cmp; + + cmp = strcmp(((const ns_mtab *)a)->name, + ((const ns_mtab *)b)->name); + if (cmp) + return (cmp); + + return (strcasecmp(((const ns_mtab *)a)->database, + ((const ns_mtab *)b)->database)); +} + +static void +_nsmodfree(ns_mod *mod) +{ + + free(__UNCONST(mod->name)); + if (mod->handle == NULL) + return; + if (mod->unregister != NULL) + (*mod->unregister)(mod->mtab, mod->mtabsize); +#ifdef __ELF__ + if (mod->handle != _nsbuiltin) + (void) dlclose(mod->handle); +#endif /* __ELF__ */ +} + +/* + * Load a built-in or dyanamically linked module. If the `reg_fn' + * argument is non-NULL, assume a built-in module and use `reg_fn' + * to register it. Otherwise, search for a dynamic nsswitch module. + */ +static int +_nsloadmod(const char *source, nss_module_register_fn reg_fn) +{ +#ifdef __ELF__ + char buf[PATH_MAX]; +#endif + ns_mod mod, *new; + + memset(&mod, 0, sizeof(mod)); + mod.name = strdup(source); + if (mod.name == NULL) + return (-1); + + if (reg_fn != NULL) { + /* + * The placeholder is required, as a NULL handle + * represents an invalid module. + */ + mod.handle = _nsbuiltin; + } else if (!is_dynamic()) { + goto out; + } else { +#ifdef __ELF__ + if (snprintf(buf, sizeof(buf), "nss_%s.so.%d", mod.name, + NSS_MODULE_INTERFACE_VERSION) >= (int)sizeof(buf)) + goto out; + mod.handle = dlopen(buf, RTLD_LOCAL | RTLD_LAZY); + if (mod.handle == NULL) { +#ifdef _NSS_DEBUG + /* + * This gets pretty annoying, since the built-in + * sources are not yet modules. + */ + /* XXX log some error? */ +#endif + goto out; + } + reg_fn = (nss_module_register_fn) dlsym(mod.handle, + "nss_module_register"); + if (reg_fn == NULL) { + (void) dlclose(mod.handle); + mod.handle = NULL; + /* XXX log some error? */ + goto out; + } +#else /* ! __ELF__ */ + mod.handle = NULL; +#endif /* __ELF__ */ + } + mod.mtab = (*reg_fn)(mod.name, &mod.mtabsize, &mod.unregister); + if (mod.mtab == NULL || mod.mtabsize == 0) { +#ifdef __ELF__ + if (mod.handle != _nsbuiltin) + (void) dlclose(mod.handle); +#endif /* __ELF__ */ + mod.handle = NULL; + /* XXX log some error? */ + goto out; + } + if (mod.mtabsize > 1) + qsort(mod.mtab, mod.mtabsize, sizeof(mod.mtab[0]), + _nsmtabcmp); + out: + new = _nsvect_append(&mod, _nsmod, &_nsmodsize, sizeof(*_nsmod)); + if (new == NULL) { + _nsmodfree(&mod); + return (-1); + } + _nsmod = new; + /* _nsmodsize already incremented */ + + qsort(_nsmod, _nsmodsize, sizeof(*_nsmod), _nsmodcmp); + return (0); +} + +static void +_nsloadbuiltin(void) +{ + + /* Do nothing, for now. */ +} + +int +_nsdbtaddsrc(ns_dbt *dbt, const ns_src *src) +{ + void *new; + const ns_mod *mod; + ns_mod modkey; + + _DIAGASSERT(dbt != NULL); + _DIAGASSERT(src != NULL); + + new = _nsvect_append(src, dbt->srclist, &dbt->srclistsize, + sizeof(*src)); + if (new == NULL) + return (-1); + dbt->srclist = new; + /* dbt->srclistsize already incremented */ + + modkey.name = src->name; + mod = bsearch(&modkey, _nsmod, _nsmodsize, sizeof(*_nsmod), _nsmodcmp); + if (mod == NULL) + return (_nsloadmod(src->name, NULL)); + + return (0); +} + +void +_nsdbtdump(const ns_dbt *dbt) +{ + unsigned int i; + + _DIAGASSERT(dbt != NULL); + + printf("%s (%d source%s):", dbt->name, dbt->srclistsize, + dbt->srclistsize == 1 ? "" : "s"); + for (i = 0; i < dbt->srclistsize; i++) { + printf(" %s", dbt->srclist[i].name); + if (!(dbt->srclist[i].flags & + (NS_UNAVAIL|NS_NOTFOUND|NS_TRYAGAIN)) && + (dbt->srclist[i].flags & NS_SUCCESS)) + continue; + printf(" ["); + if (!(dbt->srclist[i].flags & NS_SUCCESS)) + printf(" SUCCESS=continue"); + if (dbt->srclist[i].flags & NS_UNAVAIL) + printf(" UNAVAIL=return"); + if (dbt->srclist[i].flags & NS_NOTFOUND) + printf(" NOTFOUND=return"); + if (dbt->srclist[i].flags & NS_TRYAGAIN) + printf(" TRYAGAIN=return"); + printf(" ]"); + } + printf("\n"); +} + +static void +_nssrclist_free(ns_src **src, u_int srclistsize) +{ + u_int i; + + for (i = 0; i < srclistsize; i++) { + if ((*src)[i].name != NULL) + free(__UNCONST((*src)[i].name)); + } + free(*src); + *src = NULL; +} + +static void +_nsdbtfree(ns_dbt *dbt) +{ + + _nssrclist_free(&dbt->srclist, dbt->srclistsize); + if (dbt->name != NULL) + free(__UNCONST(dbt->name)); +} + +int +_nsdbtput(const ns_dbt *dbt) +{ + ns_dbt *p; + void *new; + u_int i; + + _DIAGASSERT(dbt != NULL); + + for (i = 0; i < _nsmapsize; i++) { + p = _nsvect_elem(i, _nsmap, _nsmapsize, sizeof(*_nsmap)); + if (strcasecmp(dbt->name, p->name) == 0) { + /* overwrite existing entry */ + if (p->srclist != NULL) + _nssrclist_free(&p->srclist, p->srclistsize); + memmove(p, dbt, sizeof(*dbt)); + return (0); + } + } + new = _nsvect_append(dbt, _nsmap, &_nsmapsize, sizeof(*_nsmap)); + if (new == NULL) + return (-1); + _nsmap = new; + /* _nsmapsize already incremented */ + + return (0); +} + +/* + * This function is called each time nsdispatch() is called. If this + * is the first call, or if the configuration has changed, (re-)prepare + * the global data used by NSS. + */ +static int +_nsconfigure(void) +{ +#ifdef _REENTRANT + static mutex_t _nsconflock = MUTEX_INITIALIZER; +#endif + static time_t _nsconfmod; + struct stat statbuf; + + mutex_lock(&_nsconflock); + + if (stat(_PATH_NS_CONF, &statbuf) == -1) { + /* + * No nsswitch.conf; just use whatever configuration we + * currently have, or fall back on the defaults specified + * by the caller. + */ + mutex_unlock(&_nsconflock); + return (0); + } + + if (statbuf.st_mtime <= _nsconfmod) { + /* Internal state is up-to-date with nsswitch.conf. */ + mutex_unlock(&_nsconflock); + return (0); + } + + /* + * Ok, we've decided we need to update the nsswitch configuration + * structures. Acquire a write-lock on _nslock while continuing + * to hold _nsconflock. Acquiring a write-lock blocks while + * waiting for other threads already holding a read-lock to clear. + * We hold _nsconflock for the duration, and update the time stamp + * at the end of the update operation, at which time we release + * both locks. + */ + rwlock_wrlock(&_nslock); + + _nsyyin = fopen(_PATH_NS_CONF, "r"); + if (_nsyyin == NULL) { + /* + * Unable to open nsswitch.conf; behave as though the + * stat() above failed. Even though we have already + * updated _nsconfmod, if the file reappears, the + * mtime will change. + */ + goto out; + } + + _NSVECT_FREE(_nsmap, &_nsmapsize, sizeof(*_nsmap), + (_nsvect_free_elem) _nsdbtfree); + _NSVECT_FREE(_nsmod, &_nsmodsize, sizeof(*_nsmod), + (_nsvect_free_elem) _nsmodfree); + + _nsloadbuiltin(); + + _nsyyparse(); + (void) fclose(_nsyyin); + if (_nsmapsize != 0) + qsort(_nsmap, _nsmapsize, sizeof(*_nsmap), _nsdbtcmp); + + _nsconfmod = statbuf.st_mtime; + + out: + rwlock_unlock(&_nslock); + mutex_unlock(&_nsconflock); + return (0); +} + +static nss_method +_nsmethod(const char *source, const char *database, const char *method, + const ns_dtab disp_tab[], void **cb_data) +{ + int curdisp; + ns_mod *mod, modkey; + ns_mtab *mtab, mtabkey; + + if (disp_tab != NULL) { + for (curdisp = 0; disp_tab[curdisp].src != NULL; curdisp++) { + if (strcasecmp(source, disp_tab[curdisp].src) == 0) { + *cb_data = disp_tab[curdisp].cb_data; + return (disp_tab[curdisp].callback); + } + } + } + + modkey.name = source; + mod = bsearch(&modkey, _nsmod, _nsmodsize, sizeof(*_nsmod), + _nsmodcmp); + if (mod != NULL && mod->handle != NULL) { + mtabkey.database = database; + mtabkey.name = method; + mtab = bsearch(&mtabkey, mod->mtab, mod->mtabsize, + sizeof(mod->mtab[0]), _nsmtabcmp); + if (mtab != NULL) { + *cb_data = mtab->mdata; + return (mtab->method); + } + } + + *cb_data = NULL; + return (NULL); +} + +int +/*ARGSUSED*/ +nsdispatch(void *retval, const ns_dtab disp_tab[], const char *database, + const char *method, const ns_src defaults[], ...) +{ + static int _nsdispatching; +#ifdef _REENTRANT + struct _ns_drec drec, *ldrec; +#endif + va_list ap; + int i, result; + ns_dbt key; + const ns_dbt *dbt; + const ns_src *srclist; + int srclistsize; + nss_method cb; + void *cb_data; + + /* retval may be NULL */ + /* disp_tab may be NULL */ + _DIAGASSERT(database != NULL); + _DIAGASSERT(method != NULL); + _DIAGASSERT(defaults != NULL); + if (database == NULL || method == NULL || defaults == NULL) + return (NS_UNAVAIL); + + /* + * In both the threaded and non-threaded cases, avoid reloading + * the configuration if the current thread is already running + * nsdispatch() (i.e. recursive call). + * + * In the non-threaded case, this avoids changing the data structures + * while we're using them. + * + * In the threaded case, this avoids trying to take a write lock + * while the current thread holds a read lock (which would result + * in deadlock). + */ +#ifdef _REENTRANT + if (__isthreaded) { + drec.thr = thr_self(); + mutex_lock(&_ns_drec_lock); + LIST_FOREACH(ldrec, &_ns_drec, list) { + if (ldrec->thr == drec.thr) + break; + } + LIST_INSERT_HEAD(&_ns_drec, &drec, list); + mutex_unlock(&_ns_drec_lock); + if (ldrec == NULL && _nsconfigure()) { + mutex_lock(&_ns_drec_lock); + LIST_REMOVE(&drec, list); + mutex_unlock(&_ns_drec_lock); + return (NS_UNAVAIL); + } + } else +#endif /* _REENTRANT */ + if (_nsdispatching++ == 0 && _nsconfigure()) { + _nsdispatching--; + return (NS_UNAVAIL); + } + + rwlock_rdlock(&_nslock); + + key.name = database; + dbt = bsearch(&key, _nsmap, _nsmapsize, sizeof(*_nsmap), _nsdbtcmp); + if (dbt != NULL) { + srclist = dbt->srclist; + srclistsize = dbt->srclistsize; + } else { + srclist = defaults; + srclistsize = 0; + while (srclist[srclistsize].name != NULL) + srclistsize++; + } + result = 0; + + for (i = 0; i < srclistsize; i++) { + cb = _nsmethod(srclist[i].name, database, method, + disp_tab, &cb_data); + result = 0; + if (cb != NULL) { + va_start(ap, defaults); + result = (*cb)(retval, cb_data, ap); + va_end(ap); + if (defaults[0].flags & NS_FORCEALL) + continue; + if (result & srclist[i].flags) + break; + } + } + result &= NS_STATUSMASK; /* clear private flags in result */ + + rwlock_unlock(&_nslock); + +#ifdef _REENTRANT + if (__isthreaded) { + mutex_lock(&_ns_drec_lock); + LIST_REMOVE(&drec, list); + mutex_unlock(&_ns_drec_lock); + } else +#endif /* _REENTRANT */ + _nsdispatching--; + + return (result ? result : NS_NOTFOUND); +} diff -pruN /home/reed/src/isc/libbind/libbind/irs/nslexer.l /usr/src/lib/libc/net/nslexer.l --- /home/reed/src/isc/libbind/libbind/irs/nslexer.l 1969-12-31 18:00:00.000000000 -0600 +++ /usr/src/lib/libc/net/nslexer.l 2013-06-05 09:26:13.000000000 -0500 @@ -0,0 +1,108 @@ +%{ +/* $NetBSD: nslexer.l,v 1.13 2012/06/25 22:32:45 abs Exp $ */ + +/*- + * Copyright (c) 1997, 1998, 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Luke Mewburn. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: nslexer.l,v 1.13 2012/06/25 22:32:45 abs Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#define _NS_PRIVATE +#include +#include +#include + +#include "nsparser.h" + +%} + +%option yylineno nounput noinput +%option never-interactive + +BLANK [ \t] +CR \n +STRING [a-zA-Z][a-zA-Z0-9_]* + +%% + +{BLANK}+ ; /* skip whitespace */ + +#.* ; /* skip comments */ + +\\{CR} ; /* allow continuation */ + +{CR} return NL; + +[sS][uU][cC][cC][eE][sS][sS] return SUCCESS; +[uU][nN][aA][vV][aA][iI][lL] return UNAVAIL; +[nN][oO][tT][fF][oO][uU][nN][dD] return NOTFOUND; +[tT][rR][yY][aA][gG][aA][iI][nN] return TRYAGAIN; + +[rR][eE][tT][uU][rR][nN] return RETURN; +[cC][oO][nN][tT][iI][nN][uU][eE] return CONTINUE; + +{STRING} { + char *p; + size_t i; + + if ((p = strdup(yytext)) == NULL) { + syslog(LOG_ERR, "libc nsdispatch: %m"); + return NL; + } + + for (i = 0; i < strlen(p); i++) { + if (isupper((unsigned char)p[i])) + p[i] = tolower((unsigned char)p[i]); + } + _nsyylval.str = p; + return STRING; + } + +. return yytext[0]; + +%% + +#undef _nsyywrap +int +_nsyywrap(void) +{ + return 1; +} /* _nsyywrap */ + +void +_nsyyerror(const char *msg) +{ + + syslog(LOG_WARNING, "libc nsdispatch: %s line %d: %s at '%s'", + _PATH_NS_CONF, yylineno, msg, yytext); +} /* _nsyyerror */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/nsparser.y /usr/src/lib/libc/net/nsparser.y --- /home/reed/src/isc/libbind/libbind/irs/nsparser.y 1969-12-31 18:00:00.000000000 -0600 +++ /usr/src/lib/libc/net/nsparser.y 2013-06-05 09:26:13.000000000 -0500 @@ -0,0 +1,191 @@ +%{ +/* $NetBSD: nsparser.y,v 1.12 2012/03/20 17:44:18 matt Exp $ */ + +/*- + * Copyright (c) 1997, 1998, 1999 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Luke Mewburn. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: nsparser.y,v 1.12 2012/03/20 17:44:18 matt Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" + +#include +#define _NS_PRIVATE +#include +#include +#include +#include + + +static void _nsaddsrctomap(const char *); + +static ns_dbt curdbt; +static ns_src cursrc; + +extern char * _nsyytext; +extern int _nsyylineno; +%} + +%union { + char *str; + int mapval; +} + +%token NL +%token SUCCESS UNAVAIL NOTFOUND TRYAGAIN +%token RETURN CONTINUE +%token STRING + +%type Status Action + +%% + +File + : /* empty */ + | Lines + ; + +Lines + : Entry + | Lines Entry + ; + +Entry + : NL + | Database ':' NL + | Database ':' Srclist NL + { + int lineno; + + lineno = _nsyylineno - (*_nsyytext == '\n' ? 1 : 0); + if (_nsdbtput(&curdbt) == -1) + syslog(LOG_WARNING, + "libc nsdispatch: %s line %d: %s", + _PATH_NS_CONF, lineno, + "error adding entry"); + } + | error NL + { + yyerrok; + } + ; + +Database + : STRING + { + curdbt.name = yylval.str; + curdbt.srclist = NULL; + curdbt.srclistsize = 0; + } + ; + +Srclist + : Item + | Srclist Item + ; + +Item + : STRING + { + cursrc.flags = NS_SUCCESS; + _nsaddsrctomap($1); + } + | STRING '[' { cursrc.flags = NS_SUCCESS; } Criteria ']' + { + _nsaddsrctomap($1); + } + ; + +Criteria + : Criterion + | Criteria Criterion + ; + +Criterion + : Status '=' Action + { + if ($3) /* if action == RETURN set RETURN bit */ + cursrc.flags |= $1; + else /* else unset it */ + cursrc.flags &= ~$1; + } + ; + +Status + : SUCCESS { $$ = NS_SUCCESS; } + | UNAVAIL { $$ = NS_UNAVAIL; } + | NOTFOUND { $$ = NS_NOTFOUND; } + | TRYAGAIN { $$ = NS_TRYAGAIN; } + ; + +Action + : RETURN { $$ = 1L; } + | CONTINUE { $$ = 0L; } + ; + +%% + +static void +_nsaddsrctomap(const char *elem) +{ + unsigned int i; + int lineno; + + _DIAGASSERT(elem != NULL); + + lineno = _nsyylineno - (*_nsyytext == '\n' ? 1 : 0); + if (curdbt.srclistsize > 0) { + if ((strcasecmp(elem, NSSRC_COMPAT) == 0) || + (strcasecmp(curdbt.srclist[0].name, NSSRC_COMPAT) == 0)) { + syslog(LOG_WARNING, + "libc nsdispatch: %s line %d: %s", + _PATH_NS_CONF, lineno, + "'compat' used with other sources"); + return; + } + } + for (i = 0; i < curdbt.srclistsize; i++) { + if (strcasecmp(curdbt.srclist[i].name, elem) == 0) { + syslog(LOG_WARNING, + "libc nsdispatch: %s line %d: %s '%s'", + _PATH_NS_CONF, lineno, + "duplicate source", elem); + return; + } + } + cursrc.name = elem; + if (_nsdbtaddsrc(&curdbt, &cursrc) == -1) { + syslog(LOG_WARNING, + "libc nsdispatch: %s line %d: %s '%s'", + _PATH_NS_CONF, lineno, + "error adding", elem); + } +} diff -pruN /home/reed/src/isc/libbind/libbind/irs/nul_ng.c /usr/src/lib/libc/net/nul_ng.c --- /home/reed/src/isc/libbind/libbind/irs/nul_ng.c 2005-04-26 23:56:34.000000000 -0500 +++ /usr/src/lib/libc/net/nul_ng.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,127 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1996,1999 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#if defined(LIBC_SCCS) && !defined(lint) -static const char rcsid[] = "$Id: nul_ng.c,v 1.3 2005/04/27 04:56:34 sra Exp $"; -#endif - -/*! \file - * \brief - * nul_ng.c - the netgroup accessor null map - */ - -#include "port_before.h" - -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include - -#include -#include - -#include "port_after.h" - -#include "irs_p.h" -#include "hesiod.h" -#include "dns_p.h" - -/* Forward. */ - -static void ng_close(struct irs_ng *); -static int ng_next(struct irs_ng *, const char **, - const char **, const char **); -static int ng_test(struct irs_ng *, - const char *, const char *, - const char *, const char *); -static void ng_rewind(struct irs_ng *, const char *); -static void ng_minimize(struct irs_ng *); - -/* Public. */ - -struct irs_ng * -irs_nul_ng(struct irs_acc *this) { - struct irs_ng *ng; - - UNUSED(this); - - if (!(ng = memget(sizeof *ng))) { - errno = ENOMEM; - return (NULL); - } - memset(ng, 0x5e, sizeof *ng); - ng->private = NULL; - ng->close = ng_close; - ng->next = ng_next; - ng->test = ng_test; - ng->rewind = ng_rewind; - ng->minimize = ng_minimize; - return (ng); -} - -/* Methods. */ - -static void -ng_close(struct irs_ng *this) { - memput(this, sizeof *this); -} - -/* ARGSUSED */ -static int -ng_next(struct irs_ng *this, const char **host, const char **user, - const char **domain) -{ - UNUSED(this); - UNUSED(host); - UNUSED(user); - UNUSED(domain); - errno = ENOENT; - return (-1); -} - -static int -ng_test(struct irs_ng *this, const char *name, - const char *user, const char *host, const char *domain) -{ - UNUSED(this); - UNUSED(name); - UNUSED(user); - UNUSED(host); - UNUSED(domain); - errno = ENODEV; - return (-1); -} - -static void -ng_rewind(struct irs_ng *this, const char *netgroup) { - UNUSED(this); - UNUSED(netgroup); - /* NOOP */ -} - -static void -ng_minimize(struct irs_ng *this) { - UNUSED(this); - /* NOOP */ -} diff -pruN /home/reed/src/isc/libbind/libbind/irs/pathnames.h /usr/src/lib/libc/net/pathnames.h --- /home/reed/src/isc/libbind/libbind/irs/pathnames.h 2005-04-26 23:56:34.000000000 -0500 +++ /usr/src/lib/libc/net/pathnames.h 1969-12-31 18:00:00.000000000 -0600 @@ -1,52 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1996,1999 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -/* - * $Id: pathnames.h,v 1.3 2005/04/27 04:56:34 sra Exp $ - */ - -#ifndef _PATH_IRS_CONF -#define _PATH_IRS_CONF "/etc/irs.conf" -#endif - -#ifndef _PATH_NETWORKS -#define _PATH_NETWORKS "/etc/networks" -#endif - -#ifndef _PATH_GROUP -#define _PATH_GROUP "/etc/group" -#endif - -#ifndef _PATH_NETGROUP -#define _PATH_NETGROUP "/etc/netgroup" -#endif - -#ifndef _PATH_SERVICES -#define _PATH_SERVICES "/etc/services" -#endif - -#ifdef IRS_LCL_SV_DB -#ifndef _PATH_SERVICES_DB -#define _PATH_SERVICES_DB _PATH_SERVICES ".db" -#endif -#endif - -#ifndef _PATH_HESIOD_CONF -#define _PATH_HESIOD_CONF "/etc/hesiod.conf" -#endif - -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/protoent.h /usr/src/lib/libc/net/protoent.h --- /home/reed/src/isc/libbind/libbind/irs/protoent.h 1969-12-31 18:00:00.000000000 -0600 +++ /usr/src/lib/libc/net/protoent.h 2012-10-24 08:34:36.000000000 -0500 @@ -0,0 +1,50 @@ +/* $NetBSD: protoent.h,v 1.2 2008/04/28 20:23:00 martin Exp $ */ + +/*- + * Copyright (c) 2004 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include + +struct protoent_data { + FILE *fp; + struct protoent proto; + char **aliases; + size_t maxaliases; + int stayopen; + char *line; + void *dummy; +}; + +struct protoent *getprotoent_r(struct protoent *, struct protoent_data *); +struct protoent *getprotobyname_r(const char *, + struct protoent *, struct protoent_data *); +struct protoent *getprotobynumber_r(int, + struct protoent *, struct protoent_data *); +void setprotoent_r(int, struct protoent_data *); +void endprotoent_r(struct protoent_data *); diff -pruN /home/reed/src/isc/libbind/libbind/irs/rcmd.c /usr/src/lib/libc/net/rcmd.c --- /home/reed/src/isc/libbind/libbind/irs/rcmd.c 1969-12-31 18:00:00.000000000 -0600 +++ /usr/src/lib/libc/net/rcmd.c 2013-06-05 09:26:13.000000000 -0500 @@ -0,0 +1,1016 @@ +/* $NetBSD: rcmd.c,v 1.68 2012/07/14 15:06:26 darrenr Exp $ */ + +/* + * Copyright (c) 1983, 1993, 1994 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)rcmd.c 8.3 (Berkeley) 3/26/94"; +#else +__RCSID("$NetBSD: rcmd.c,v 1.68 2012/07/14 15:06:26 darrenr Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#ifdef _LIBC +#include "namespace.h" +#endif +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "pathnames.h" + +int orcmd(char **, u_int, const char *, const char *, const char *, int *); +int orcmd_af(char **, u_int, const char *, const char *, const char *, + int *, int); +int __ivaliduser(FILE *, u_int32_t, const char *, const char *); +int __ivaliduser_sa(FILE *, const struct sockaddr *, socklen_t, + const char *, const char *); +static int rshrcmd(int, char **, u_int32_t, const char *, + const char *, const char *, int *, const char *); +static int resrcmd(struct addrinfo *, char **, u_int32_t, const char *, + const char *, const char *, int *); +static int __icheckhost(const struct sockaddr *, socklen_t, + const char *); +static char *__gethostloop(const struct sockaddr *, socklen_t); + +int +rcmd(char **ahost, int rport, const char *locuser, const char *remuser, + const char *cmd, int *fd2p) +{ + + return rcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, AF_INET); +} + +int +rcmd_af(char **ahost, int rport, const char *locuser, const char *remuser, + const char *cmd, int *fd2p, int af) +{ + static char hbuf[MAXHOSTNAMELEN]; + char pbuf[NI_MAXSERV]; + struct addrinfo hints, *res; + int error; + struct servent *sp; + + _DIAGASSERT(ahost != NULL); + _DIAGASSERT(locuser != NULL); + _DIAGASSERT(remuser != NULL); + _DIAGASSERT(cmd != NULL); + /* fd2p may be NULL */ + + snprintf(pbuf, sizeof(pbuf), "%u", ntohs(rport)); + memset(&hints, 0, sizeof(hints)); + hints.ai_family = af; + hints.ai_socktype = SOCK_STREAM; + hints.ai_flags = AI_CANONNAME; + error = getaddrinfo(*ahost, pbuf, &hints, &res); + if (error) { + warnx("%s: %s", *ahost, gai_strerror(error)); /*XXX*/ + return -1; + } + if (res->ai_canonname) { + /* + * Canonicalise hostname. + * XXX: Should we really do this? + */ + strlcpy(hbuf, res->ai_canonname, sizeof(hbuf)); + *ahost = hbuf; + } + + /* + * Check if rport is the same as the shell port, and that the fd2p. If + * it is not, the program isn't expecting 'rsh' and so we can't use the + * RCMD_CMD environment. + */ + sp = getservbyname("shell", "tcp"); + if (sp != NULL && sp->s_port == rport) + error = rshrcmd(af, ahost, (u_int32_t)rport, + locuser, remuser, cmd, fd2p, getenv("RCMD_CMD")); + else + error = resrcmd(res, ahost, (u_int32_t)rport, + locuser, remuser, cmd, fd2p); + freeaddrinfo(res); + return error; +} + +/* this is simply a wrapper around hprcmd() that handles ahost first */ +int +orcmd(char **ahost, u_int rport, const char *locuser, const char *remuser, + const char *cmd, int *fd2p) +{ + return orcmd_af(ahost, rport, locuser, remuser, cmd, fd2p, AF_INET); +} + +int +orcmd_af(char **ahost, u_int rport, const char *locuser, const char *remuser, + const char *cmd, int *fd2p, int af) +{ + static char hbuf[MAXHOSTNAMELEN]; + char pbuf[NI_MAXSERV]; + struct addrinfo hints, *res; + int error; + + _DIAGASSERT(ahost != NULL); + _DIAGASSERT(locuser != NULL); + _DIAGASSERT(remuser != NULL); + _DIAGASSERT(cmd != NULL); + /* fd2p may be NULL */ + + snprintf(pbuf, sizeof(pbuf), "%u", ntohs(rport)); + memset(&hints, 0, sizeof(hints)); + hints.ai_family = af; + hints.ai_socktype = SOCK_STREAM; + hints.ai_flags = AI_CANONNAME; + error = getaddrinfo(*ahost, pbuf, &hints, &res); + if (error) { + warnx("%s: %s", *ahost, gai_strerror(error)); /*XXX*/ + return -1; + } + if (res->ai_canonname) { + strlcpy(hbuf, res->ai_canonname, sizeof(hbuf)); + *ahost = hbuf; + } + + error = resrcmd(res, ahost, rport, locuser, remuser, cmd, fd2p); + freeaddrinfo(res); + return error; +} + +/*ARGSUSED*/ +static int +resrcmd(struct addrinfo *res, char **ahost, u_int32_t rport, + const char *locuser, const char *remuser, const char *cmd, int *fd2p) +{ + struct addrinfo *r; + struct sockaddr_storage from; + struct pollfd reads[2]; + sigset_t nmask, omask; + pid_t pid; + int s, lport, timo; + int pollr; + char c; + int refused; + + _DIAGASSERT(res != NULL); + _DIAGASSERT(ahost != NULL); + _DIAGASSERT(locuser != NULL); + _DIAGASSERT(remuser != NULL); + _DIAGASSERT(cmd != NULL); + /* fd2p may be NULL */ + + r = res; + refused = 0; + pid = getpid(); + sigemptyset(&nmask); + sigaddset(&nmask, SIGURG); + if (sigprocmask(SIG_BLOCK, &nmask, &omask) == -1) + return -1; + for (timo = 1, lport = IPPORT_RESERVED - 1;;) { + s = rresvport_af(&lport, r->ai_family); + if (s < 0) { + if (errno == EAGAIN) + warnx("rcmd: socket: All ports in use"); + else + warn("rcmd: socket"); + if (r->ai_next) { + r = r->ai_next; + continue; + } else { + (void)sigprocmask(SIG_SETMASK, &omask, NULL); + return -1; + } + } + fcntl(s, F_SETOWN, pid); + if (connect(s, r->ai_addr, r->ai_addrlen) >= 0) + break; + (void)close(s); + if (errno == EADDRINUSE) { + lport--; + continue; + } else if (errno == ECONNREFUSED) + refused++; + if (r->ai_next) { + int oerrno = errno; + char hbuf[NI_MAXHOST]; + const int niflags = NI_NUMERICHOST; + + hbuf[0] = '\0'; + if (getnameinfo(r->ai_addr, r->ai_addrlen, + hbuf, (socklen_t)sizeof(hbuf), NULL, 0, niflags) != + 0) + strlcpy(hbuf, "(invalid)", sizeof(hbuf)); + errno = oerrno; + warn("rcmd: connect to address %s", hbuf); + r = r->ai_next; + hbuf[0] = '\0'; + if (getnameinfo(r->ai_addr, r->ai_addrlen, + hbuf, (socklen_t)sizeof(hbuf), NULL, 0, niflags) != + 0) + strlcpy(hbuf, "(invalid)", sizeof(hbuf)); + (void)fprintf(stderr, "Trying %s...\n", hbuf); + continue; + } + if (refused && timo <= 16) { + (void)sleep((unsigned int)timo); + timo *= 2; + r = res; + refused = 0; + continue; + } + (void)fprintf(stderr, "%s: %s\n", res->ai_canonname, + strerror(errno)); + (void)sigprocmask(SIG_SETMASK, &omask, NULL); + return -1; + } + lport--; + if (fd2p == 0) { + write(s, "", 1); + lport = 0; + } else { + char num[8]; + int s2 = rresvport_af(&lport, r->ai_family), s3; + socklen_t len = sizeof(from); + + if (s2 < 0) + goto bad; + listen(s2, 1); + (void)snprintf(num, sizeof(num), "%d", lport); + if (write(s, num, strlen(num) + 1) != + (ssize_t) (strlen(num) + 1)) { + warn("rcmd: write (setting up stderr)"); + (void)close(s2); + goto bad; + } + reads[0].fd = s; + reads[0].events = POLLIN; + reads[1].fd = s2; + reads[1].events = POLLIN; + errno = 0; + pollr = poll(reads, 2, INFTIM); + if (pollr < 1 || (reads[1].revents & POLLIN) == 0) { + if (errno != 0) + warn("poll: setting up stderr"); + else + warnx( + "poll: protocol failure in circuit setup"); + (void)close(s2); + goto bad; + } + s3 = accept(s2, (struct sockaddr *)(void *)&from, &len); + (void)close(s2); + if (s3 < 0) { + warn("rcmd: accept"); + lport = 0; + goto bad; + } + *fd2p = s3; + switch (((struct sockaddr *)(void *)&from)->sa_family) { + case AF_INET: +#ifdef INET6 + case AF_INET6: +#endif + if (getnameinfo((struct sockaddr *)(void *)&from, len, + NULL, 0, num, (socklen_t)sizeof(num), + NI_NUMERICSERV) != 0 || + (atoi(num) >= IPPORT_RESERVED || + atoi(num) < IPPORT_RESERVED / 2)) { + warnx( + "rcmd: protocol failure in circuit setup."); + goto bad2; + } + break; + default: + break; + } + } + + (void)write(s, locuser, strlen(locuser)+1); + (void)write(s, remuser, strlen(remuser)+1); + (void)write(s, cmd, strlen(cmd)+1); + if (read(s, &c, 1) != 1) { + warn("%s", *ahost); + goto bad2; + } + if (c != 0) { + while (read(s, &c, 1) == 1) { + (void)write(STDERR_FILENO, &c, 1); + if (c == '\n') + break; + } + goto bad2; + } + (void)sigprocmask(SIG_SETMASK, &omask, NULL); + return s; +bad2: + if (lport) + (void)close(*fd2p); +bad: + (void)close(s); + (void)sigprocmask(SIG_SETMASK, &omask, NULL); + return -1; +} + +/* + * based on code written by Chris Siebenmann + */ +/* ARGSUSED */ +static int +rshrcmd(int af, char **ahost, u_int32_t rport, const char *locuser, + const char *remuser, const char *cmd, int *fd2p, const char *rshcmd) +{ + pid_t pid; + int sp[2], ep[2]; + char *p; + struct passwd *pw, pwres; + char pwbuf[1024]; + + _DIAGASSERT(ahost != NULL); + _DIAGASSERT(locuser != NULL); + _DIAGASSERT(remuser != NULL); + _DIAGASSERT(cmd != NULL); + /* fd2p may be NULL */ + + /* What rsh/shell to use. */ + if (rshcmd == NULL) + rshcmd = _PATH_BIN_RCMD; + + /* locuser must exist on this host. */ + if (getpwnam_r(locuser, &pwres, pwbuf, sizeof(pwbuf), &pw) != 0 || + pw == NULL) { + warnx("%s: unknown user: %s", __func__, locuser); + return -1; + } + + /* get a socketpair we'll use for stdin and stdout. */ + if (socketpair(AF_LOCAL, SOCK_STREAM, 0, sp) < 0) { + warn("%s: socketpair", __func__); + return -1; + } + /* we will use this for the fd2 pointer */ + if (fd2p) { + if (socketpair(AF_LOCAL, SOCK_STREAM, 0, ep) < 0) { + warn("%s: socketpair", __func__); + return -1; + } + *fd2p = ep[0]; + } + + pid = fork(); + if (pid < 0) { + warn("%s: fork", __func__); + return -1; + } + if (pid == 0) { + /* + * child + * - we use sp[1] to be stdin/stdout, and close sp[0] + * - with fd2p, we use ep[1] for stderr, and close ep[0] + */ + (void)close(sp[0]); + if (dup2(sp[1], 0) < 0 || dup2(0, 1) < 0) { + warn("%s: dup2", __func__); + _exit(1); + } + (void)close(sp[1]); + if (fd2p) { + if (dup2(ep[1], 2) < 0) { + warn("%s: dup2", __func__); + _exit(1); + } + (void)close(ep[0]); + (void)close(ep[1]); + } else if (dup2(0, 2) < 0) { + warn("%s: dup2", __func__); + _exit(1); + } + /* fork again to lose parent. */ + pid = fork(); + if (pid < 0) { + warn("%s: second fork", __func__); + _exit(1); + } + if (pid > 0) + _exit(0); + + /* Orphan. Become local user for rshprog. */ + if (setuid(pw->pw_uid)) { + warn("%s: setuid(%lu)", __func__, (u_long)pw->pw_uid); + _exit(1); + } + + /* + * If we are rcmd'ing to "localhost" as the same user as we + * are, then avoid running remote shell for efficiency. + */ + if (strcmp(*ahost, "localhost") == 0 && + strcmp(locuser, remuser) == 0) { + if (pw->pw_shell[0] == '\0') + rshcmd = _PATH_BSHELL; + else + rshcmd = pw->pw_shell; + p = strrchr(rshcmd, '/'); + execlp(rshcmd, p ? p + 1 : rshcmd, "-c", cmd, NULL); + } else { + const char *program; + program = strrchr(rshcmd, '/'); + program = program ? program + 1 : rshcmd; + switch (af) { + case AF_INET: + execlp(rshcmd, program, "-4", "-l", remuser, + *ahost, cmd, NULL); + break; + + case AF_INET6: + execlp(rshcmd, program, "-6", "-l", remuser, + *ahost, cmd, NULL); + break; + + default: + /* typically AF_UNSPEC, plus whatever */ + execlp(rshcmd, program, "-l", remuser, + *ahost, cmd, NULL); + break; + } + } + warn("%s: exec %s", __func__, rshcmd); + _exit(1); + } + /* Parent */ + (void)close(sp[1]); + if (fd2p) + (void)close(ep[1]); + + (void)waitpid(pid, NULL, 0); + return sp[0]; +} + +int +rresvport(int *alport) +{ + + _DIAGASSERT(alport != NULL); + + return rresvport_af(alport, AF_INET); +} + +int +rresvport_af(int *alport, int family) +{ + return rresvport_af_addr(alport, family, NULL); +} + +int +rresvport_af_addr(int *alport, int family, void *addr) +{ + struct sockaddr_storage ss; + struct sockaddr *sa; + socklen_t salen; + int s; + u_int16_t *portp; + + _DIAGASSERT(alport != NULL); + + memset(&ss, 0, sizeof(ss)); + sa = (struct sockaddr *)(void *)&ss; + switch (family) { + case AF_INET: +#ifdef BSD4_4 + sa->sa_len = +#endif + salen = sizeof(struct sockaddr_in); + if (addr) + ((struct sockaddr_in *)(void *)sa)->sin_addr = + ((struct sockaddr_in *)addr)->sin_addr; + portp = &((struct sockaddr_in *)(void *)sa)->sin_port; + break; +#ifdef INET6 + case AF_INET6: +#ifdef BSD4_4 + sa->sa_len = +#endif + salen = sizeof(struct sockaddr_in6); + if (addr) + ((struct sockaddr_in6 *)(void *)sa)->sin6_addr = + ((struct sockaddr_in6 *)addr)->sin6_addr; + portp = &((struct sockaddr_in6 *)(void *)sa)->sin6_port; + break; +#endif + default: + errno = EAFNOSUPPORT; + return -1; + } + sa->sa_family = family; + s = socket(family, SOCK_STREAM, 0); + if (s < 0) + return -1; +#ifdef BSD4_4 + switch (family) { + case AF_INET: + case AF_INET6: + *portp = 0; + if (bindresvport(s, (struct sockaddr_in *)(void *)sa) < 0) { + int sverr = errno; + + (void)close(s); + errno = sverr; + return -1; + } + *alport = (int)ntohs(*portp); + return s; + default: + /* is it necessary to try keep code for other AFs? */ + break; + } +#endif + for (;;) { + *portp = htons((u_short)*alport); + if (bind(s, sa, salen) >= 0) + return s; + if (errno != EADDRINUSE) { + (void)close(s); + return -1; + } + (*alport)--; + if (*alport == IPPORT_RESERVED/2) { + (void)close(s); + errno = EAGAIN; /* close */ + return -1; + } + } +} + +int __check_rhosts_file = 1; +const char *__rcmd_errstr; + +int +ruserok(const char *rhost, int superuser, const char *ruser, const char *luser) +{ + struct addrinfo hints, *res, *r; + int error; + + _DIAGASSERT(rhost != NULL); + _DIAGASSERT(ruser != NULL); + _DIAGASSERT(luser != NULL); + + memset(&hints, 0, sizeof(hints)); + hints.ai_family = PF_UNSPEC; + hints.ai_socktype = SOCK_DGRAM; /*dummy*/ + error = getaddrinfo(rhost, "0", &hints, &res); + if (error) + return -1; + + for (r = res; r; r = r->ai_next) { + if (iruserok_sa(r->ai_addr, (int)r->ai_addrlen, superuser, + ruser, luser) == 0) { + freeaddrinfo(res); + return 0; + } + } + freeaddrinfo(res); + return -1; +} + +/* + * New .rhosts strategy: We are passed an ip address. We spin through + * hosts.equiv and .rhosts looking for a match. When the .rhosts only + * has ip addresses, we don't have to trust a nameserver. When it + * contains hostnames, we spin through the list of addresses the nameserver + * gives us and look for a match. + * + * Returns 0 if ok, -1 if not ok. + */ +int +iruserok(u_int32_t raddr, int superuser, const char *ruser, const char *luser) +{ + struct sockaddr_in irsin; + + memset(&irsin, 0, sizeof(irsin)); + irsin.sin_family = AF_INET; +#ifdef BSD4_4 + irsin.sin_len = sizeof(irsin); +#endif + memcpy(&irsin.sin_addr, &raddr, sizeof(irsin.sin_addr)); + return iruserok_sa(&irsin, (socklen_t)sizeof(irsin), superuser, ruser, + luser); +} + +/* + * 2nd and 3rd arguments are typed like this, to avoid dependency between + * unistd.h and sys/socket.h. There's no better way. + */ +int +iruserok_sa(const void *raddr, int rlen, int superuser, const char *ruser, + const char *luser) +{ + const struct sockaddr *sa; + struct stat sbuf; + struct passwd *pwd, pwres; + FILE *hostf; + uid_t uid; + gid_t gid; + int isvaliduser; + char pbuf[MAXPATHLEN]; + char pwbuf[1024]; + + _DIAGASSERT(raddr != NULL); + _DIAGASSERT(ruser != NULL); + _DIAGASSERT(luser != NULL); + + sa = raddr; + + __rcmd_errstr = NULL; + + hostf = superuser ? NULL : fopen(_PATH_HEQUIV, "r"); + + if (hostf) { + if (__ivaliduser_sa(hostf, sa, (socklen_t)rlen, luser, + ruser) == 0) { + (void)fclose(hostf); + return 0; + } + (void)fclose(hostf); + } + + isvaliduser = -1; + if (__check_rhosts_file || superuser) { + + if (getpwnam_r(luser, &pwres, pwbuf, sizeof(pwbuf), &pwd) != 0 + || pwd == NULL) + return -1; + (void)strlcpy(pbuf, pwd->pw_dir, sizeof(pbuf)); + (void)strlcat(pbuf, "/.rhosts", sizeof(pbuf)); + + /* + * Change effective uid while opening and reading .rhosts. + * If root and reading an NFS mounted file system, can't + * read files that are protected read/write owner only. + */ + uid = geteuid(); + gid = getegid(); + (void)setegid(pwd->pw_gid); + initgroups(pwd->pw_name, pwd->pw_gid); + (void)seteuid(pwd->pw_uid); + hostf = fopen(pbuf, "r"); + + if (hostf != NULL) { + /* + * If not a regular file, or is owned by someone other + * than user or root or if writable by anyone but the + * owner, quit. + */ + if (lstat(pbuf, &sbuf) < 0) + __rcmd_errstr = ".rhosts lstat failed"; + else if (!S_ISREG(sbuf.st_mode)) + __rcmd_errstr = ".rhosts not regular file"; + else if (fstat(fileno(hostf), &sbuf) < 0) + __rcmd_errstr = ".rhosts fstat failed"; + else if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid) + __rcmd_errstr = "bad .rhosts owner"; + else if (sbuf.st_mode & (S_IWGRP|S_IWOTH)) + __rcmd_errstr = + ".rhosts writable by other than owner"; + else + isvaliduser = + __ivaliduser_sa(hostf, sa, (socklen_t)rlen, + luser, ruser); + + (void)fclose(hostf); + } + (void)seteuid(uid); + (void)setegid(gid); + + } + return isvaliduser; +} + +/* + * XXX + * Don't make static, used by lpd(8). We will be able to change the function + * into static function, when we bump libc major #. + * + * Returns 0 if ok, -1 if not ok. + */ +#ifdef notdef /*_LIBC*/ +static +#endif +int +__ivaliduser(FILE *hostf, u_int32_t raddr, const char *luser, + const char *ruser) +{ + struct sockaddr_in ivusin; + + memset(&ivusin, 0, sizeof(ivusin)); + ivusin.sin_family = AF_INET; +#ifdef BSD4_4 + ivusin.sin_len = sizeof(ivusin); +#endif + memcpy(&ivusin.sin_addr, &raddr, sizeof(ivusin.sin_addr)); + return __ivaliduser_sa(hostf, (struct sockaddr *)(void *)&ivusin, + (socklen_t)sizeof(ivusin), luser, ruser); +} + +#ifdef notdef /*_LIBC*/ +static +#endif +int +__ivaliduser_sa(FILE *hostf, const struct sockaddr *raddr, socklen_t salen, + const char *luser, const char *ruser) +{ + char *user, *p; + int ch; + char buf[MAXHOSTNAMELEN + 128]; /* host + login */ + const char *auser, *ahost; + int hostok, userok; + char *rhost = NULL; + int firsttime = 1; + char domain[MAXHOSTNAMELEN]; + + getdomainname(domain, sizeof(domain)); + + _DIAGASSERT(hostf != NULL); + _DIAGASSERT(luser != NULL); + _DIAGASSERT(ruser != NULL); + + while (fgets(buf, (int)sizeof(buf), hostf)) { + p = buf; + /* Skip lines that are too long. */ + if (strchr(p, '\n') == NULL) { + while ((ch = getc(hostf)) != '\n' && ch != EOF) + ; + continue; + } + while (*p != '\n' && *p != ' ' && *p != '\t' && *p != '\0') { + *p = isupper((unsigned char)*p) ? + tolower((unsigned char)*p) : *p; + p++; + } + if (*p == ' ' || *p == '\t') { + *p++ = '\0'; + while (*p == ' ' || *p == '\t') + p++; + user = p; + while (*p != '\n' && *p != ' ' && + *p != '\t' && *p != '\0') + p++; + } else + user = p; + *p = '\0'; + + if (p == buf) + continue; + + auser = *user ? user : luser; + ahost = buf; + + if (ahost[0] == '+') + switch (ahost[1]) { + case '\0': + hostok = 1; + break; + + case '@': + if (firsttime) { + rhost = __gethostloop(raddr, salen); + firsttime = 0; + } + if (rhost) + hostok = innetgr(&ahost[2], rhost, + NULL, domain); + else + hostok = 0; + break; + + default: + hostok = __icheckhost(raddr, salen, &ahost[1]); + break; + } + else if (ahost[0] == '-') + switch (ahost[1]) { + case '\0': + hostok = -1; + break; + + case '@': + if (firsttime) { + rhost = __gethostloop(raddr, salen); + firsttime = 0; + } + if (rhost) + hostok = -innetgr(&ahost[2], rhost, + NULL, domain); + else + hostok = 0; + break; + + default: + hostok = + -__icheckhost(raddr, salen, &ahost[1]); + break; + } + else + hostok = __icheckhost(raddr, salen, ahost); + + + if (auser[0] == '+') + switch (auser[1]) { + case '\0': + userok = 1; + break; + + case '@': + userok = innetgr(&auser[2], NULL, ruser, + domain); + break; + + default: + userok = strcmp(ruser, &auser[1]) == 0; + break; + } + else if (auser[0] == '-') + switch (auser[1]) { + case '\0': + userok = -1; + break; + + case '@': + userok = -innetgr(&auser[2], NULL, ruser, + domain); + break; + + default: + userok = + -(strcmp(ruser, &auser[1]) == 0 ? 1 : 0); + break; + } + else + userok = strcmp(ruser, auser) == 0; + + /* Check if one component did not match */ + if (hostok == 0 || userok == 0) + continue; + + /* Check if we got a forbidden pair */ + if (userok == -1 || hostok == -1) + return -1; + + /* Check if we got a valid pair */ + if (hostok == 1 && userok == 1) + return 0; + } + return -1; +} + +/* + * Returns "true" if match, 0 if no match. + */ +static int +__icheckhost(const struct sockaddr *raddr, socklen_t salen, const char *lhost) +{ + struct addrinfo hints, *res, *r; + char h1[NI_MAXHOST], h2[NI_MAXHOST]; + int error; + const int niflags = NI_NUMERICHOST; + + _DIAGASSERT(raddr != NULL); + _DIAGASSERT(lhost != NULL); + + h1[0] = '\0'; + if (getnameinfo(raddr, salen, h1, (socklen_t)sizeof(h1), NULL, 0, + niflags) != 0) + return 0; + + /* Resolve laddr into sockaddr */ + memset(&hints, 0, sizeof(hints)); + hints.ai_family = raddr->sa_family; + hints.ai_socktype = SOCK_DGRAM; /*dummy*/ + res = NULL; + error = getaddrinfo(lhost, "0", &hints, &res); + if (error) + return 0; + + /* + * Try string comparisons between raddr and laddr. + */ + for (r = res; r; r = r->ai_next) { + h2[0] = '\0'; + if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, + (socklen_t)sizeof(h2), NULL, 0, niflags) != 0) + continue; + if (strcmp(h1, h2) == 0) { + freeaddrinfo(res); + return 1; + } + } + + /* No match. */ + freeaddrinfo(res); + return 0; +} + +/* + * Return the hostname associated with the supplied address. + * Do a reverse lookup as well for security. If a loop cannot + * be found, pack the numeric IP address into the string. + */ +static char * +__gethostloop(const struct sockaddr *raddr, socklen_t salen) +{ + static char remotehost[NI_MAXHOST]; + char h1[NI_MAXHOST], h2[NI_MAXHOST]; + struct addrinfo hints, *res, *r; + int error; + const int niflags = NI_NUMERICHOST; + + _DIAGASSERT(raddr != NULL); + + h1[0] = remotehost[0] = '\0'; + if (getnameinfo(raddr, salen, remotehost, (socklen_t)sizeof(remotehost), + NULL, 0, NI_NAMEREQD) != 0) + return NULL; + if (getnameinfo(raddr, salen, h1, (socklen_t)sizeof(h1), NULL, 0, + niflags) != 0) + return NULL; + + /* + * Look up the name and check that the supplied + * address is in the list + */ + memset(&hints, 0, sizeof(hints)); + hints.ai_family = raddr->sa_family; + hints.ai_socktype = SOCK_DGRAM; /*dummy*/ + hints.ai_flags = AI_CANONNAME; + res = NULL; + error = getaddrinfo(remotehost, "0", &hints, &res); + if (error) + return NULL; + + for (r = res; r; r = r->ai_next) { + h2[0] = '\0'; + if (getnameinfo(r->ai_addr, r->ai_addrlen, h2, + (socklen_t)sizeof(h2), NULL, 0, niflags) != 0) + continue; + if (strcmp(h1, h2) == 0) { + freeaddrinfo(res); + return remotehost; + } + } + + /* + * either the DNS adminstrator has made a configuration + * mistake, or someone has attempted to spoof us + */ + syslog(LOG_NOTICE, "rcmd: address %s not listed for host %s", + h1, res->ai_canonname ? res->ai_canonname : remotehost); + freeaddrinfo(res); + return NULL; +} diff -pruN /home/reed/src/isc/libbind/libbind/irs/recv.c /usr/src/lib/libc/net/recv.c --- /home/reed/src/isc/libbind/libbind/irs/recv.c 1969-12-31 18:00:00.000000000 -0600 +++ /usr/src/lib/libc/net/recv.c 2013-06-05 09:26:13.000000000 -0500 @@ -0,0 +1,50 @@ +/* $NetBSD: recv.c,v 1.10 2012/03/20 17:44:18 matt Exp $ */ + +/* + * Copyright (c) 1988, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)recv.c 8.2 (Berkeley) 2/21/94"; +#else +__RCSID("$NetBSD: recv.c,v 1.10 2012/03/20 17:44:18 matt Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include +#include + +#include + +ssize_t +recv(int s, void *buf, size_t len, int flags) +{ + return (recvfrom(s, buf, len, flags, NULL, NULL)); +} diff -pruN /home/reed/src/isc/libbind/libbind/irs/rthdr.c /usr/src/lib/libc/net/rthdr.c --- /home/reed/src/isc/libbind/libbind/irs/rthdr.c 1969-12-31 18:00:00.000000000 -0600 +++ /usr/src/lib/libc/net/rthdr.c 2013-06-05 09:26:13.000000000 -0500 @@ -0,0 +1,459 @@ +/* $NetBSD: rthdr.c,v 1.18 2012/03/13 21:13:42 christos Exp $ */ + +/* + * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the project nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: rthdr.c,v 1.18 2012/03/13 21:13:42 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include + +#include +#include + +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(inet6_rthdr_add,_inet6_rthdr_add) +__weak_alias(inet6_rthdr_getaddr,_inet6_rthdr_getaddr) +__weak_alias(inet6_rthdr_getflags,_inet6_rthdr_getflags) +__weak_alias(inet6_rthdr_init,_inet6_rthdr_init) +__weak_alias(inet6_rthdr_lasthop,_inet6_rthdr_lasthop) +__weak_alias(inet6_rthdr_segments,_inet6_rthdr_segments) +__weak_alias(inet6_rthdr_space,_inet6_rthdr_space) +__weak_alias(inet6_rth_space, _inet6_rth_space) +__weak_alias(inet6_rth_init, _inet6_rth_init) +__weak_alias(inet6_rth_add, _inet6_rth_add) +__weak_alias(inet6_rth_reverse, _inet6_rth_reverse) +__weak_alias(inet6_rth_segments, _inet6_rth_segments) +__weak_alias(inet6_rth_getaddr, _inet6_rth_getaddr) +#endif + +/* + * RFC2292 API + */ + +size_t +inet6_rthdr_space(int type, int seg) +{ + switch (type) { + case IPV6_RTHDR_TYPE_0: + if (seg < 1 || seg > 23) + return (0); + return (CMSG_SPACE(sizeof(struct in6_addr) * seg + + sizeof(struct ip6_rthdr0))); + default: + return (0); + } +} + +struct cmsghdr * +inet6_rthdr_init(void *bp, int type) +{ + struct cmsghdr *ch; + struct ip6_rthdr *rthdr; + + _DIAGASSERT(bp != NULL); + + ch = (struct cmsghdr *)bp; + rthdr = (struct ip6_rthdr *)(void *)CMSG_DATA(ch); + + ch->cmsg_level = IPPROTO_IPV6; + ch->cmsg_type = IPV6_RTHDR; + + switch (type) { + case IPV6_RTHDR_TYPE_0: +#ifdef COMPAT_RFC2292 + ch->cmsg_len = CMSG_LEN(sizeof(struct ip6_rthdr0) - + sizeof(struct in6_addr)); +#else + ch->cmsg_len = CMSG_LEN(sizeof(struct ip6_rthdr0)); +#endif + (void)memset(rthdr, 0, sizeof(struct ip6_rthdr0)); + rthdr->ip6r_type = IPV6_RTHDR_TYPE_0; + return (ch); + default: + return (NULL); + } +} + +int +inet6_rthdr_add(struct cmsghdr *cmsg, const struct in6_addr *addr, u_int flags) +{ + struct ip6_rthdr *rthdr; + + _DIAGASSERT(cmsg != NULL); + _DIAGASSERT(addr != NULL); + + rthdr = (struct ip6_rthdr *)(void *)CMSG_DATA(cmsg); + + switch (rthdr->ip6r_type) { + case IPV6_RTHDR_TYPE_0: + { + size_t len; + struct ip6_rthdr0 *rt0 = (struct ip6_rthdr0 *)(void *)rthdr; + if (flags != IPV6_RTHDR_LOOSE && flags != IPV6_RTHDR_STRICT) + return (-1); + if (rt0->ip6r0_segleft == 23) + return (-1); + if (flags != IPV6_RTHDR_LOOSE) + return (-1); + rt0->ip6r0_segleft++; + (void)memcpy(((caddr_t)(void *)rt0) + + ((rt0->ip6r0_len + 1) << 3), addr, sizeof(struct in6_addr)); + rt0->ip6r0_len += sizeof(struct in6_addr) >> 3; + len = CMSG_LEN((rt0->ip6r0_len + 1) << 3); + _DIAGASSERT(__type_fit(socklen_t, len)); + cmsg->cmsg_len = (socklen_t)len; + break; + } + default: + return (-1); + } + + return (0); +} + +int +inet6_rthdr_lasthop(struct cmsghdr *cmsg, unsigned int flags) +{ + struct ip6_rthdr *rthdr; + + _DIAGASSERT(cmsg != NULL); + + rthdr = (struct ip6_rthdr *)(void *)CMSG_DATA(cmsg); + + switch (rthdr->ip6r_type) { + case IPV6_RTHDR_TYPE_0: + { + struct ip6_rthdr0 *rt0 = (struct ip6_rthdr0 *)(void *)rthdr; + if (rt0->ip6r0_segleft > 23) + return (-1); + if (flags != IPV6_RTHDR_LOOSE) + return (-1); + break; + } + default: + return (-1); + } + + return (0); +} + +#if 0 +int +inet6_rthdr_reverse(const struct cmsghdr *in, struct cmsghdr *out) +{ + + return (-1); +} +#endif + +int +inet6_rthdr_segments(const struct cmsghdr *cmsg) +{ + const struct ip6_rthdr *rthdr; + + _DIAGASSERT(cmsg != NULL); + + rthdr = __UNCONST(CCMSG_DATA(cmsg)); + + switch (rthdr->ip6r_type) { + case IPV6_RTHDR_TYPE_0: + { + const struct ip6_rthdr0 *rt0 = + (const struct ip6_rthdr0 *)(const void *)rthdr; + size_t len; + + if (rt0->ip6r0_len % 2 || 46 < rt0->ip6r0_len) + return (-1); + + len = (rt0->ip6r0_len * 8) / sizeof(struct in6_addr); + _DIAGASSERT(__type_fit(int, len)); + return (int)len; + } + + default: + return (-1); + } +} + +struct in6_addr * +inet6_rthdr_getaddr(struct cmsghdr *cmsg, int idx) +{ + struct ip6_rthdr *rthdr; + + _DIAGASSERT(cmsg != NULL); + + rthdr = (struct ip6_rthdr *)(void *)CMSG_DATA(cmsg); + + switch (rthdr->ip6r_type) { + case IPV6_RTHDR_TYPE_0: + { + struct ip6_rthdr0 *rt0 = (struct ip6_rthdr0 *)(void *)rthdr; + int naddr; + size_t len; + + if (rt0->ip6r0_len % 2 || 46 < rt0->ip6r0_len) + return NULL; + len = (rt0->ip6r0_len * 8) / sizeof(struct in6_addr); + _DIAGASSERT(__type_fit(int, len)); + naddr = (int)len; + if (idx <= 0 || naddr < idx) + return NULL; +#ifdef COMPAT_RFC2292 + return ((struct in6_addr *)(void *)(rt0 + 1)) + idx - 1; +#else + return ((struct in6_addr *)(void *)(rt0 + 1)) + idx; +#endif + } + + default: + return NULL; + } +} + +int +inet6_rthdr_getflags(const struct cmsghdr *cmsg, int idx) +{ + const struct ip6_rthdr *rthdr; + + _DIAGASSERT(cmsg != NULL); + + rthdr = __UNCONST(CCMSG_DATA(cmsg)); + + switch (rthdr->ip6r_type) { + case IPV6_RTHDR_TYPE_0: + { + const struct ip6_rthdr0 *rt0 = (const struct ip6_rthdr0 *) + (const void *)rthdr; + int naddr; + size_t len; + + if (rt0->ip6r0_len % 2 || 46 < rt0->ip6r0_len) + return (-1); + len = (rt0->ip6r0_len * 8) / sizeof(struct in6_addr); + _DIAGASSERT(__type_fit(int, len)); + naddr = (int)len; + if (idx < 0 || naddr < idx) + return (-1); + return IPV6_RTHDR_LOOSE; + } + + default: + return (-1); + } +} + +/* + * RFC3542 (2292bis) API + */ + +socklen_t +inet6_rth_space(int type, int segments) +{ + switch (type) { + case IPV6_RTHDR_TYPE_0: + return (((segments * 2) + 1) << 3); + default: + return (0); /* type not suppported */ + } +} + +void * +inet6_rth_init(void *bp, socklen_t bp_len, int type, int segments) +{ + struct ip6_rthdr *rth; + struct ip6_rthdr0 *rth0; + + _DIAGASSERT(bp != NULL); + + rth = (struct ip6_rthdr *)bp; + + switch (type) { + case IPV6_RTHDR_TYPE_0: + /* length validation */ + if (bp_len < inet6_rth_space(IPV6_RTHDR_TYPE_0, segments)) + return (NULL); + + memset(bp, 0, bp_len); + rth0 = (struct ip6_rthdr0 *)(void *)rth; + rth0->ip6r0_len = segments * 2; + rth0->ip6r0_type = IPV6_RTHDR_TYPE_0; + rth0->ip6r0_segleft = 0; + rth0->ip6r0_reserved = 0; + break; + default: + return (NULL); /* type not supported */ + } + + return (bp); +} + +int +inet6_rth_add(void *bp, const struct in6_addr *addr) +{ + struct ip6_rthdr *rth; + struct ip6_rthdr0 *rth0; + struct in6_addr *nextaddr; + + _DIAGASSERT(bp != NULL); + + rth = (struct ip6_rthdr *)bp; + + switch (rth->ip6r_type) { + case IPV6_RTHDR_TYPE_0: + rth0 = (struct ip6_rthdr0 *)(void *)rth; + nextaddr = (struct in6_addr *)(void *)(rth0 + 1) + + rth0->ip6r0_segleft; + *nextaddr = *addr; + rth0->ip6r0_segleft++; + break; + default: + return (-1); /* type not supported */ + } + + return (0); +} + +int +inet6_rth_reverse(const void *in, void *out) +{ + const struct ip6_rthdr *rth_in; + const struct ip6_rthdr0 *rth0_in; + struct ip6_rthdr0 *rth0_out; + int i, segments; + + _DIAGASSERT(in != NULL); + _DIAGASSERT(out != NULL); + + rth_in = (const struct ip6_rthdr *)in; + + switch (rth_in->ip6r_type) { + case IPV6_RTHDR_TYPE_0: + rth0_in = (const struct ip6_rthdr0 *)in; + rth0_out = (struct ip6_rthdr0 *)out; + + /* parameter validation XXX too paranoid? */ + if (rth0_in->ip6r0_len % 2) + return (-1); + segments = rth0_in->ip6r0_len / 2; + + /* we can't use memcpy here, since in and out may overlap */ + memmove((void *)rth0_out, (const void *)rth0_in, + (unsigned int)(((rth0_in->ip6r0_len) + 1) << 3)); + rth0_out->ip6r0_segleft = segments; + + /* reverse the addresses */ + for (i = 0; i < segments / 2; i++) { + struct in6_addr addr_tmp, *addr1, *addr2; + + addr1 = (struct in6_addr *)(void *)(rth0_out + 1) + i; + addr2 = (struct in6_addr *)(void *)(rth0_out + 1) + + (segments - i - 1); + addr_tmp = *addr1; + *addr1 = *addr2; + *addr2 = addr_tmp; + } + + break; + default: + return (-1); /* type not supported */ + } + + return (0); +} + +int +inet6_rth_segments(const void *bp) +{ + const struct ip6_rthdr *rh; + const struct ip6_rthdr0 *rh0; + unsigned int addrs; + + _DIAGASSERT(bp != NULL); + + rh = (const struct ip6_rthdr *)bp; + + switch (rh->ip6r_type) { + case IPV6_RTHDR_TYPE_0: + rh0 = (const struct ip6_rthdr0 *)bp; + + /* + * Validation for a type-0 routing header. + * Is this too strict? + */ + if ((rh0->ip6r0_len % 2) != 0 || + (addrs = (rh0->ip6r0_len / 2)) < rh0->ip6r0_segleft) + return (-1); + + return (addrs); + default: + return (-1); /* unknown type */ + } +} + +struct in6_addr * +inet6_rth_getaddr(const void *bp, int idx) +{ + const struct ip6_rthdr *rh; + const struct ip6_rthdr0 *rh0; + unsigned int addrs; + + _DIAGASSERT(bp != NULL); + + rh = (const struct ip6_rthdr *)bp; + + switch (rh->ip6r_type) { + case IPV6_RTHDR_TYPE_0: + rh0 = (const struct ip6_rthdr0 *)bp; + + /* + * Validation for a type-0 routing header. + * Is this too strict? + */ + if ((rh0->ip6r0_len % 2) != 0 || + (addrs = (rh0->ip6r0_len / 2)) < rh0->ip6r0_segleft) + return (NULL); + + if (idx < 0 || addrs <= (unsigned int)idx) + return (NULL); + + return (((struct in6_addr *)(void *)__UNCONST(rh0 + 1)) + idx); + default: + return (NULL); /* unknown type */ + } +} diff -pruN /home/reed/src/isc/libbind/libbind/irs/send.c /usr/src/lib/libc/net/send.c --- /home/reed/src/isc/libbind/libbind/irs/send.c 1969-12-31 18:00:00.000000000 -0600 +++ /usr/src/lib/libc/net/send.c 2013-06-05 09:26:13.000000000 -0500 @@ -0,0 +1,56 @@ +/* $NetBSD: send.c,v 1.10 2012/03/20 17:44:18 matt Exp $ */ + +/* + * Copyright (c) 1988, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)send.c 8.2 (Berkeley) 2/21/94"; +#else +__RCSID("$NetBSD: send.c,v 1.10 2012/03/20 17:44:18 matt Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include + +#include + +#ifdef __weak_alias +__weak_alias(send, _send) +#endif + +ssize_t +send(int s, const void *msg, size_t len, int flags) +{ + + return (sendto(s, msg, len, flags, NULL, 0)); +} diff -pruN /home/reed/src/isc/libbind/libbind/irs/servent.h /usr/src/lib/libc/net/servent.h --- /home/reed/src/isc/libbind/libbind/irs/servent.h 1969-12-31 18:00:00.000000000 -0600 +++ /usr/src/lib/libc/net/servent.h 2012-10-24 08:34:36.000000000 -0500 @@ -0,0 +1,65 @@ +/* $NetBSD: servent.h,v 1.4 2010/04/25 00:54:46 joerg Exp $ */ + +/*- + * Copyright (c) 2004 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Christos Zoulas. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include + +struct servent_data { + FILE *plainfile; + struct cdbr *cdb; + struct servent serv; + char **aliases; + size_t maxaliases; + int flags; +#define _SV_STAYOPEN 1 +#define _SV_CDB 2 +#define _SV_PLAINFILE 4 +#define _SV_FIRST 8 + uint32_t cdb_index; + uint8_t *cdb_buf; + size_t cdb_buf_len; + char *line; + void *dummy; +}; + +struct servent *getservent_r(struct servent *, struct servent_data *); +struct servent *getservbyname_r(const char *, const char *, + struct servent *, struct servent_data *); +struct servent *getservbyport_r(int, const char *, + struct servent *, struct servent_data *); +void setservent_r(int, struct servent_data *); +void endservent_r(struct servent_data *); + +int _servent_open(struct servent_data *); +void _servent_close(struct servent_data *); +int _servent_getline(struct servent_data *); +struct servent *_servent_parseline(struct servent_data *, struct servent *); +struct servent *_servent_parsedb(struct servent_data *, struct servent *, + const uint8_t *, size_t); diff -pruN /home/reed/src/isc/libbind/libbind/irs/sethostent.c /usr/src/lib/libc/net/sethostent.c --- /home/reed/src/isc/libbind/libbind/irs/sethostent.c 1969-12-31 18:00:00.000000000 -0600 +++ /usr/src/lib/libc/net/sethostent.c 2013-06-05 09:26:13.000000000 -0500 @@ -0,0 +1,81 @@ +/* $NetBSD: sethostent.c,v 1.17 2012/03/20 17:44:18 matt Exp $ */ + +/* + * Copyright (c) 1985, 1993 + * The Regents of the University of California. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +#if 0 +static char sccsid[] = "@(#)sethostent.c 8.1 (Berkeley) 6/4/93"; +static char rcsid[] = "Id: sethostent.c,v 8.5 1996/09/28 06:51:07 vixie Exp "; +#else +__RCSID("$NetBSD: sethostent.c,v 1.17 2012/03/20 17:44:18 matt Exp $"); +#endif +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include +#include +#include +#include + +#ifdef __weak_alias +__weak_alias(sethostent,_sethostent) +__weak_alias(endhostent,_endhostent) +#endif + +void _endhtent(void); +#ifndef _REENTRANT +void res_close(void); +#endif +void _sethtent(int); + +void +/*ARGSUSED*/ +sethostent(int stayopen) +{ +#ifndef _REENTRANT + if ((_res.options & RES_INIT) == 0 && res_init() == -1) + return; + if (stayopen) + _res.options |= RES_STAYOPEN | RES_USEVC; +#endif + _sethtent(stayopen); +} + +void +endhostent(void) +{ +#ifndef _REENTRANT + _res.options &= ~(RES_STAYOPEN | RES_USEVC); + res_close(); +#endif + _endhtent(); +} diff -pruN /home/reed/src/isc/libbind/libbind/irs/sockatmark.c /usr/src/lib/libc/net/sockatmark.c --- /home/reed/src/isc/libbind/libbind/irs/sockatmark.c 1969-12-31 18:00:00.000000000 -0600 +++ /usr/src/lib/libc/net/sockatmark.c 2013-06-05 09:26:13.000000000 -0500 @@ -0,0 +1,55 @@ +/* $NetBSD: sockatmark.c,v 1.3 2012/03/20 17:44:18 matt Exp $ */ + +/*- + * Copyright (c) 2001 The NetBSD Foundation, Inc. + * All rights reserved. + * + * This code is derived from software contributed to The NetBSD Foundation + * by Klaus Klein. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: sockatmark.c,v 1.3 2012/03/20 17:44:18 matt Exp $"); +#endif + +#include "namespace.h" + +#include +#include + +#include + +int +sockatmark(int s) +{ + int val; + + _DIAGASSERT(s != -1); + + if (ioctl(s, SIOCATMARK, &val) == -1) + return (-1); + + return (val); +} diff -pruN /home/reed/src/isc/libbind/libbind/irs/util.c /usr/src/lib/libc/net/util.c --- /home/reed/src/isc/libbind/libbind/irs/util.c 2005-04-26 23:56:34.000000000 -0500 +++ /usr/src/lib/libc/net/util.c 1969-12-31 18:00:00.000000000 -0600 @@ -1,109 +0,0 @@ -/* - * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") - * Copyright (c) 1996,1999 by Internet Software Consortium. - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT - * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -#if defined(LIBC_SCCS) && !defined(lint) -static const char rcsid[] = "$Id: util.c,v 1.3 2005/04/27 04:56:34 sra Exp $"; -#endif - -#include "port_before.h" - -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -#include - -#include "port_after.h" - -#include "irs_p.h" - -#ifdef SPRINTF_CHAR -# define SPRINTF(x) strlen(sprintf/**/x) -#else -# define SPRINTF(x) sprintf x -#endif - -void -map_v4v6_address(const char *src, char *dst) { - u_char *p = (u_char *)dst; - char tmp[NS_INADDRSZ]; - int i; - - /* Stash a temporary copy so our caller can update in place. */ - memcpy(tmp, src, NS_INADDRSZ); - /* Mark this ipv6 addr as a mapped ipv4. */ - for (i = 0; i < 10; i++) - *p++ = 0x00; - *p++ = 0xff; - *p++ = 0xff; - /* Retrieve the saved copy and we're done. */ - memcpy((void*)p, tmp, NS_INADDRSZ); -} - -int -make_group_list(struct irs_gr *this, const char *name, - gid_t basegid, gid_t *groups, int *ngroups) -{ - struct group *grp; - int i, ng; - int ret, maxgroups; - - ret = -1; - ng = 0; - maxgroups = *ngroups; - /* - * When installing primary group, duplicate it; - * the first element of groups is the effective gid - * and will be overwritten when a setgid file is executed. - */ - if (ng >= maxgroups) - goto done; - groups[ng++] = basegid; - if (ng >= maxgroups) - goto done; - groups[ng++] = basegid; - /* - * Scan the group file to find additional groups. - */ - (*this->rewind)(this); - while ((grp = (*this->next)(this)) != NULL) { - if ((gid_t)grp->gr_gid == basegid) - continue; - for (i = 0; grp->gr_mem[i]; i++) { - if (!strcmp(grp->gr_mem[i], name)) { - if (ng >= maxgroups) - goto done; - groups[ng++] = grp->gr_gid; - break; - } - } - } - ret = 0; - done: - *ngroups = ng; - return (ret); -} - -/*! \file */ diff -pruN /home/reed/src/isc/libbind/libbind/irs/vars6.c /usr/src/lib/libc/net/vars6.c --- /home/reed/src/isc/libbind/libbind/irs/vars6.c 1969-12-31 18:00:00.000000000 -0600 +++ /usr/src/lib/libc/net/vars6.c 2005-08-07 11:00:01.000000000 -0500 @@ -0,0 +1,57 @@ +/* $NetBSD: vars6.c,v 1.7 2005/08/07 16:00:01 christos Exp $ */ + +/* + * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the project nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#include +#if defined(LIBC_SCCS) && !defined(lint) +__RCSID("$NetBSD: vars6.c,v 1.7 2005/08/07 16:00:01 christos Exp $"); +#endif /* LIBC_SCCS and not lint */ + +#include "namespace.h" +#include +#include + +#ifdef __weak_alias +__weak_alias(in6addr_any, _in6addr_any) +__weak_alias(in6addr_loopback, _in6addr_loopback) +__weak_alias(in6addr_nodelocal_allnodes, _in6addr_nodelocal_allnodes) +__weak_alias(in6addr_linklocal_allnodes, _in6addr_linklocal_allnodes) +__weak_alias(in6addr_linklocal_allrouters, _in6addr_linklocal_allrouters) +#endif + +/* + * Definitions of some constant IPv6 addresses. + */ +const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT; +const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT; +const struct in6_addr in6addr_nodelocal_allnodes = IN6ADDR_NODELOCAL_ALLNODES_INIT; +const struct in6_addr in6addr_linklocal_allnodes = IN6ADDR_LINKLOCAL_ALLNODES_INIT; +const struct in6_addr in6addr_linklocal_allrouters = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT; +