10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*2830Sdjl * Common Development and Distribution License (the "License").
6*2830Sdjl * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*2830Sdjl * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
240Sstevel@tonic-gate *
250Sstevel@tonic-gate * nis/gethostent.c -- "nis" backend for nsswitch "ipnodes" database
260Sstevel@tonic-gate */
270Sstevel@tonic-gate
280Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
290Sstevel@tonic-gate
300Sstevel@tonic-gate #include <ctype.h>
310Sstevel@tonic-gate #include <netdb.h>
320Sstevel@tonic-gate #include <stdio.h>
330Sstevel@tonic-gate #include <string.h>
340Sstevel@tonic-gate #include <netinet/in.h>
350Sstevel@tonic-gate #include <arpa/inet.h>
360Sstevel@tonic-gate #include <sys/socket.h>
370Sstevel@tonic-gate #include "nis_common.h"
380Sstevel@tonic-gate #include <stdlib.h>
390Sstevel@tonic-gate
400Sstevel@tonic-gate
410Sstevel@tonic-gate static nss_status_t
getbyname(be,a)420Sstevel@tonic-gate getbyname(be, a)
430Sstevel@tonic-gate nis_backend_ptr_t be;
440Sstevel@tonic-gate void *a;
450Sstevel@tonic-gate {
460Sstevel@tonic-gate nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a;
470Sstevel@tonic-gate nss_status_t res;
480Sstevel@tonic-gate
490Sstevel@tonic-gate const char *s;
500Sstevel@tonic-gate char c;
510Sstevel@tonic-gate
520Sstevel@tonic-gate for (s = argp->key.ipnode.name; (c = *s) != '\0'; s++) {
530Sstevel@tonic-gate if (isupper(c)) {
540Sstevel@tonic-gate char *copy;
550Sstevel@tonic-gate char *mung;
560Sstevel@tonic-gate
570Sstevel@tonic-gate if ((copy = strdup(argp->key.ipnode.name)) == 0) {
580Sstevel@tonic-gate return (NSS_UNAVAIL);
590Sstevel@tonic-gate }
600Sstevel@tonic-gate for (mung = copy + (s - argp->key.ipnode.name);
610Sstevel@tonic-gate (c = *mung) != '\0'; mung++) {
620Sstevel@tonic-gate if (isupper(c)) {
630Sstevel@tonic-gate *mung = _tolower(c);
640Sstevel@tonic-gate }
650Sstevel@tonic-gate }
660Sstevel@tonic-gate res = _nss_nis_lookup(be, argp, 1, "ipnodes.byname",
670Sstevel@tonic-gate copy, 0);
680Sstevel@tonic-gate if (res != NSS_SUCCESS)
690Sstevel@tonic-gate argp->h_errno = __nss2herrno(res);
700Sstevel@tonic-gate free(copy);
710Sstevel@tonic-gate return (res);
720Sstevel@tonic-gate }
730Sstevel@tonic-gate }
740Sstevel@tonic-gate res = _nss_nis_lookup(be, argp, 1,
750Sstevel@tonic-gate "ipnodes.byname", argp->key.ipnode.name, 0);
760Sstevel@tonic-gate if (res != NSS_SUCCESS)
770Sstevel@tonic-gate argp->h_errno = __nss2herrno(res);
780Sstevel@tonic-gate return (res);
790Sstevel@tonic-gate }
800Sstevel@tonic-gate
810Sstevel@tonic-gate static nss_status_t
getbyaddr(be,a)820Sstevel@tonic-gate getbyaddr(be, a)
830Sstevel@tonic-gate nis_backend_ptr_t be;
840Sstevel@tonic-gate void *a;
850Sstevel@tonic-gate {
860Sstevel@tonic-gate nss_XbyY_args_t *argp = (nss_XbyY_args_t *)a;
870Sstevel@tonic-gate struct in6_addr addr;
880Sstevel@tonic-gate char buf[INET6_ADDRSTRLEN + 1];
890Sstevel@tonic-gate nss_status_t res;
900Sstevel@tonic-gate
910Sstevel@tonic-gate /* === Do we really want to be this pedantic? */
920Sstevel@tonic-gate if (argp->key.hostaddr.type != AF_INET6 ||
930Sstevel@tonic-gate argp->key.hostaddr.len != sizeof (addr)) {
940Sstevel@tonic-gate return (NSS_NOTFOUND);
950Sstevel@tonic-gate }
96*2830Sdjl (void) memcpy(&addr, argp->key.hostaddr.addr, sizeof (addr));
970Sstevel@tonic-gate if (IN6_IS_ADDR_V4MAPPED(&addr)) {
980Sstevel@tonic-gate if (inet_ntop(AF_INET, (void *) &addr.s6_addr[12],
990Sstevel@tonic-gate (void *)buf, INET_ADDRSTRLEN) == NULL) {
1000Sstevel@tonic-gate return (NSS_NOTFOUND);
1010Sstevel@tonic-gate }
1020Sstevel@tonic-gate } else {
1030Sstevel@tonic-gate if (inet_ntop(AF_INET6, (void *)&addr, (void *)buf,
1040Sstevel@tonic-gate INET6_ADDRSTRLEN) == NULL)
1050Sstevel@tonic-gate return (NSS_NOTFOUND);
1060Sstevel@tonic-gate }
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate res = _nss_nis_lookup(be, argp, 1, "ipnodes.byaddr", buf, 0);
1090Sstevel@tonic-gate if (res != NSS_SUCCESS)
1100Sstevel@tonic-gate argp->h_errno = __nss2herrno(res);
1110Sstevel@tonic-gate return (res);
1120Sstevel@tonic-gate }
1130Sstevel@tonic-gate
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate static nis_backend_op_t ipnodes_ops[] = {
1160Sstevel@tonic-gate _nss_nis_destr,
1170Sstevel@tonic-gate _nss_nis_endent,
1180Sstevel@tonic-gate _nss_nis_setent,
1190Sstevel@tonic-gate _nss_nis_getent_netdb,
1200Sstevel@tonic-gate getbyname,
1210Sstevel@tonic-gate getbyaddr
1220Sstevel@tonic-gate };
1230Sstevel@tonic-gate
1240Sstevel@tonic-gate /*ARGSUSED*/
1250Sstevel@tonic-gate nss_backend_t *
_nss_nis_ipnodes_constr(dummy1,dummy2,dummy3)1260Sstevel@tonic-gate _nss_nis_ipnodes_constr(dummy1, dummy2, dummy3)
1270Sstevel@tonic-gate const char *dummy1, *dummy2, *dummy3;
1280Sstevel@tonic-gate {
1290Sstevel@tonic-gate return (_nss_nis_constr(ipnodes_ops,
1300Sstevel@tonic-gate sizeof (ipnodes_ops) / sizeof (ipnodes_ops[0]),
1310Sstevel@tonic-gate "ipnodes.byaddr"));
1320Sstevel@tonic-gate }
133