1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 1994, Garrett Wollman 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 #include <sys/param.h> 30 #include <sys/socket.h> 31 #include <netinet/in.h> 32 #include <arpa/inet.h> 33 #include <netdb.h> 34 #include <resolv.h> 35 #include <stdio.h> 36 #include <stdlib.h> 37 #include <ctype.h> 38 #include <errno.h> 39 #include <string.h> 40 #include <stdarg.h> 41 #include <nsswitch.h> 42 #include <arpa/nameser.h> 43 #ifdef YP 44 #include <rpc/rpc.h> 45 #include <rpcsvc/yp_prot.h> 46 #include <rpcsvc/ypclnt.h> 47 #endif 48 #include "netdb_private.h" 49 50 #ifdef YP 51 static int 52 _getnetbynis(const char *name, char *map, int af, struct netent *ne, 53 struct netent_data *ned) 54 { 55 char *p, *bp, *ep; 56 char *cp, **q; 57 char *result; 58 int resultlen, len; 59 char *ypbuf; 60 61 switch(af) { 62 case AF_INET: 63 break; 64 default: 65 case AF_INET6: 66 errno = EAFNOSUPPORT; 67 return (-1); 68 } 69 70 if (ned->yp_domain == (char *)NULL) 71 if (yp_get_default_domain (&ned->yp_domain)) 72 return (-1); 73 74 if (yp_match(ned->yp_domain, map, name, strlen(name), &result, 75 &resultlen)) 76 return (-1); 77 78 ypbuf = alloca(resultlen + 2); 79 bcopy(result, ypbuf, resultlen); 80 ypbuf[resultlen] = '\0'; 81 free(result); 82 result = ypbuf; 83 84 if ((cp = strchr(result, '\n'))) 85 *cp = '\0'; 86 87 cp = strpbrk(result, " \t"); 88 *cp++ = '\0'; 89 bp = ned->netbuf; 90 ep = ned->netbuf + sizeof ned->netbuf; 91 len = strlen(result) + 1; 92 if (ep - bp < len) { 93 RES_SET_H_ERRNO(__res_state(), NO_RECOVERY); 94 return (-1); 95 } 96 strlcpy(bp, result, ep - bp); 97 ne->n_name = bp; 98 bp += len; 99 100 while (*cp == ' ' || *cp == '\t') 101 cp++; 102 103 ne->n_net = inet_network(cp); 104 ne->n_addrtype = AF_INET; 105 106 q = ne->n_aliases = ned->net_aliases; 107 cp = strpbrk(cp, " \t"); 108 if (cp != NULL) 109 *cp++ = '\0'; 110 while (cp && *cp) { 111 if (*cp == ' ' || *cp == '\t') { 112 cp++; 113 continue; 114 } 115 if (q > &ned->net_aliases[_MAXALIASES - 1]) 116 break; 117 p = strpbrk(cp, " \t"); 118 if (p != NULL) 119 *p++ = '\0'; 120 len = strlen(cp) + 1; 121 if (ep - bp < len) 122 break; 123 strlcpy(bp, cp, ep - bp); 124 *q++ = bp; 125 bp += len; 126 cp = p; 127 } 128 *q = NULL; 129 return (0); 130 } 131 #endif /* YP */ 132 133 int 134 _nis_getnetbyname(void *rval, void *cb_data, va_list ap) 135 { 136 #ifdef YP 137 const char *name; 138 char *buffer; 139 size_t buflen; 140 int *errnop, *h_errnop; 141 struct netent *nptr, ne; 142 struct netent_data *ned; 143 res_state statp; 144 145 name = va_arg(ap, const char *); 146 nptr = va_arg(ap, struct netent *); 147 buffer = va_arg(ap, char *); 148 buflen = va_arg(ap, size_t); 149 errnop = va_arg(ap, int *); 150 h_errnop = va_arg(ap, int *); 151 152 statp = __res_state(); 153 if ((ned = __netent_data_init()) == NULL) { 154 RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 155 *h_errnop = statp->res_h_errno; 156 return (NS_UNAVAIL); 157 } 158 159 if (_getnetbynis(name, "networks.byname", AF_INET, &ne, ned) != 0) { 160 *h_errnop = statp->res_h_errno; 161 return (NS_NOTFOUND); 162 } 163 if (__copy_netent(&ne, nptr, buffer, buflen) != 0) { 164 *errnop = errno; 165 RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 166 *h_errnop = statp->res_h_errno; 167 return (NS_RETURN); 168 } 169 *((struct netent **)rval) = nptr; 170 return (NS_SUCCESS); 171 #else 172 return (NS_UNAVAIL); 173 #endif 174 175 } 176 177 int 178 _nis_getnetbyaddr(void *rval, void *cb_data, va_list ap) 179 { 180 #ifdef YP 181 uint32_t addr; 182 int af; 183 char *buffer; 184 size_t buflen; 185 int *errnop, *h_errnop; 186 struct netent *nptr, ne; 187 struct netent_data *ned; 188 char *str, *cp; 189 uint32_t net2; 190 int nn; 191 unsigned int netbr[4]; 192 char buf[MAXDNAME]; 193 res_state statp; 194 195 addr = va_arg(ap, uint32_t); 196 af = va_arg(ap, int); 197 nptr = va_arg(ap, struct netent *); 198 buffer = va_arg(ap, char *); 199 buflen = va_arg(ap, size_t); 200 errnop = va_arg(ap, int *); 201 h_errnop = va_arg(ap, int *); 202 203 statp = __res_state(); 204 if ((ned = __netent_data_init()) == NULL) { 205 RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 206 *h_errnop = statp->res_h_errno; 207 return (NS_UNAVAIL); 208 } 209 210 if (af != AF_INET) { 211 RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 212 *h_errnop = statp->res_h_errno; 213 errno = EAFNOSUPPORT; 214 return (NS_UNAVAIL); 215 } 216 217 for (nn = 4, net2 = addr; net2; net2 >>= 8) { 218 netbr[--nn] = net2 & 0xff; 219 } 220 221 switch (nn) { 222 case 3: /* Class A */ 223 sprintf(buf, "%u", netbr[3]); 224 break; 225 case 2: /* Class B */ 226 sprintf(buf, "%u.%u", netbr[2], netbr[3]); 227 break; 228 case 1: /* Class C */ 229 sprintf(buf, "%u.%u.%u", netbr[1], netbr[2], netbr[3]); 230 break; 231 case 0: /* Class D - E */ 232 sprintf(buf, "%u.%u.%u.%u", netbr[0], netbr[1], 233 netbr[2], netbr[3]); 234 break; 235 } 236 237 str = (char *)&buf; 238 cp = str + (strlen(str) - 2); 239 240 while(!strcmp(cp, ".0")) { 241 *cp = '\0'; 242 cp = str + (strlen(str) - 2); 243 } 244 245 if (_getnetbynis(str, "networks.byaddr", af, &ne, ned) != 0) { 246 *h_errnop = statp->res_h_errno; 247 return (NS_NOTFOUND); 248 } 249 if (__copy_netent(&ne, nptr, buffer, buflen) != 0) { 250 *errnop = errno; 251 RES_SET_H_ERRNO(statp, NETDB_INTERNAL); 252 *h_errnop = statp->res_h_errno; 253 return (NS_RETURN); 254 } 255 *((struct netent **)rval) = nptr; 256 return (NS_SUCCESS); 257 #else 258 return (NS_UNAVAIL); 259 #endif /* YP */ 260 } 261