xref: /titanic_52/usr/src/lib/nsswitch/nis/common/gethostent6.c (revision 7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fe)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1988-1992, 2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  *
26  *	nis/gethostent.c -- "nis" backend for nsswitch "ipnodes" database
27  */
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 #include <ctype.h>
32 #include <netdb.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
37 #include <sys/socket.h>
38 #include "nis_common.h"
39 #include <stdlib.h>
40 
41 
42 static nss_status_t
43 getbyname(be, a)
44 	nis_backend_ptr_t	be;
45 	void			*a;
46 {
47 	nss_XbyY_args_t		*argp = (nss_XbyY_args_t *)a;
48 	nss_status_t	res;
49 
50 	const char		*s;
51 	char			c;
52 
53 	for (s = argp->key.ipnode.name;  (c = *s) != '\0';  s++) {
54 		if (isupper(c)) {
55 			char		*copy;
56 			char		*mung;
57 
58 			if ((copy = strdup(argp->key.ipnode.name)) == 0) {
59 				return (NSS_UNAVAIL);
60 			}
61 			for (mung = copy + (s - argp->key.ipnode.name);
62 			    (c = *mung) != '\0';  mung++) {
63 				if (isupper(c)) {
64 					*mung = _tolower(c);
65 				}
66 			}
67 			res = _nss_nis_lookup(be, argp, 1, "ipnodes.byname",
68 				copy, 0);
69 			if (res != NSS_SUCCESS)
70 				argp->h_errno = __nss2herrno(res);
71 			free(copy);
72 			return (res);
73 		}
74 	}
75 	res = _nss_nis_lookup(be, argp, 1,
76 				"ipnodes.byname", argp->key.ipnode.name, 0);
77 	if (res != NSS_SUCCESS)
78 		argp->h_errno = __nss2herrno(res);
79 	return (res);
80 }
81 
82 static nss_status_t
83 getbyaddr(be, a)
84 	nis_backend_ptr_t	be;
85 	void			*a;
86 {
87 	nss_XbyY_args_t		*argp	= (nss_XbyY_args_t *)a;
88 	struct in6_addr		addr;
89 	char			buf[INET6_ADDRSTRLEN + 1];
90 	nss_status_t	res;
91 
92 	/* === Do we really want to be this pedantic? */
93 	if (argp->key.hostaddr.type != AF_INET6 ||
94 	    argp->key.hostaddr.len  != sizeof (addr)) {
95 		return (NSS_NOTFOUND);
96 	}
97 	memcpy(&addr, argp->key.hostaddr.addr, sizeof (addr));
98 	if (IN6_IS_ADDR_V4MAPPED(&addr)) {
99 		if (inet_ntop(AF_INET, (void *) &addr.s6_addr[12],
100 				(void *)buf, INET_ADDRSTRLEN) == NULL) {
101 			return (NSS_NOTFOUND);
102 		}
103 	} else {
104 		if (inet_ntop(AF_INET6, (void *)&addr, (void *)buf,
105 						INET6_ADDRSTRLEN) == NULL)
106 			return (NSS_NOTFOUND);
107 	}
108 
109 	res = _nss_nis_lookup(be, argp, 1, "ipnodes.byaddr", buf, 0);
110 	if (res != NSS_SUCCESS)
111 		argp->h_errno = __nss2herrno(res);
112 	return (res);
113 }
114 
115 
116 static nis_backend_op_t ipnodes_ops[] = {
117 	_nss_nis_destr,
118 	_nss_nis_endent,
119 	_nss_nis_setent,
120 	_nss_nis_getent_netdb,
121 	getbyname,
122 	getbyaddr
123 };
124 
125 /*ARGSUSED*/
126 nss_backend_t *
127 _nss_nis_ipnodes_constr(dummy1, dummy2, dummy3)
128 	const char	*dummy1, *dummy2, *dummy3;
129 {
130 	return (_nss_nis_constr(ipnodes_ops,
131 			sizeof (ipnodes_ops) / sizeof (ipnodes_ops[0]),
132 			"ipnodes.byaddr"));
133 }
134