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