xref: /illumos-gate/usr/src/lib/nsswitch/nis/common/gethostent6.c (revision 581cede61ac9c14d8d4ea452562a567189eead78)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 #include <ctype.h>
31 #include <netdb.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
36 #include <sys/socket.h>
37 #include "nis_common.h"
38 #include <stdlib.h>
39 
40 
41 static nss_status_t
42 getbyname(be, a)
43 	nis_backend_ptr_t	be;
44 	void			*a;
45 {
46 	nss_XbyY_args_t		*argp = (nss_XbyY_args_t *)a;
47 	nss_status_t	res;
48 
49 	const char		*s;
50 	char			c;
51 
52 	for (s = argp->key.ipnode.name;  (c = *s) != '\0';  s++) {
53 		if (isupper(c)) {
54 			char		*copy;
55 			char		*mung;
56 
57 			if ((copy = strdup(argp->key.ipnode.name)) == 0) {
58 				return (NSS_UNAVAIL);
59 			}
60 			for (mung = copy + (s - argp->key.ipnode.name);
61 			    (c = *mung) != '\0';  mung++) {
62 				if (isupper(c)) {
63 					*mung = _tolower(c);
64 				}
65 			}
66 			res = _nss_nis_lookup(be, argp, 1, "ipnodes.byname",
67 				copy, 0);
68 			if (res != NSS_SUCCESS)
69 				argp->h_errno = __nss2herrno(res);
70 			free(copy);
71 			return (res);
72 		}
73 	}
74 	res = _nss_nis_lookup(be, argp, 1,
75 				"ipnodes.byname", argp->key.ipnode.name, 0);
76 	if (res != NSS_SUCCESS)
77 		argp->h_errno = __nss2herrno(res);
78 	return (res);
79 }
80 
81 static nss_status_t
82 getbyaddr(be, a)
83 	nis_backend_ptr_t	be;
84 	void			*a;
85 {
86 	nss_XbyY_args_t		*argp	= (nss_XbyY_args_t *)a;
87 	struct in6_addr		addr;
88 	char			buf[INET6_ADDRSTRLEN + 1];
89 	nss_status_t	res;
90 
91 	/* === Do we really want to be this pedantic? */
92 	if (argp->key.hostaddr.type != AF_INET6 ||
93 	    argp->key.hostaddr.len  != sizeof (addr)) {
94 		return (NSS_NOTFOUND);
95 	}
96 	(void) memcpy(&addr, argp->key.hostaddr.addr, sizeof (addr));
97 	if (IN6_IS_ADDR_V4MAPPED(&addr)) {
98 		if (inet_ntop(AF_INET, (void *) &addr.s6_addr[12],
99 				(void *)buf, INET_ADDRSTRLEN) == NULL) {
100 			return (NSS_NOTFOUND);
101 		}
102 	} else {
103 		if (inet_ntop(AF_INET6, (void *)&addr, (void *)buf,
104 						INET6_ADDRSTRLEN) == NULL)
105 			return (NSS_NOTFOUND);
106 	}
107 
108 	res = _nss_nis_lookup(be, argp, 1, "ipnodes.byaddr", buf, 0);
109 	if (res != NSS_SUCCESS)
110 		argp->h_errno = __nss2herrno(res);
111 	return (res);
112 }
113 
114 
115 static nis_backend_op_t ipnodes_ops[] = {
116 	_nss_nis_destr,
117 	_nss_nis_endent,
118 	_nss_nis_setent,
119 	_nss_nis_getent_netdb,
120 	getbyname,
121 	getbyaddr
122 };
123 
124 /*ARGSUSED*/
125 nss_backend_t *
126 _nss_nis_ipnodes_constr(dummy1, dummy2, dummy3)
127 	const char	*dummy1, *dummy2, *dummy3;
128 {
129 	return (_nss_nis_constr(ipnodes_ops,
130 			sizeof (ipnodes_ops) / sizeof (ipnodes_ops[0]),
131 			"ipnodes.byaddr"));
132 }
133