xref: /illumos-gate/usr/src/lib/nsswitch/nis/common/gethostent.c (revision 1a2d662a91cee3bf82f41cd47c7ae6f3825d9db2)
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 "hosts" 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 static nss_status_t
39 getbyname(be, a)
40 	nis_backend_ptr_t	be;
41 	void			*a;
42 {
43 	nss_XbyY_args_t		*argp = (nss_XbyY_args_t *)a;
44 	nss_status_t	res;
45 
46 	const char		*s;
47 	char			c;
48 
49 	for (s = argp->key.name;  (c = *s) != '\0';  s++) {
50 		if (isupper(c)) {
51 			char		*copy;
52 			char		*mung;
53 
54 			if ((copy = strdup(argp->key.name)) == 0) {
55 				return (NSS_UNAVAIL);
56 			}
57 			for (mung = copy + (s - argp->key.name);
58 			    (c = *mung) != '\0';  mung++) {
59 				if (isupper(c)) {
60 					*mung = _tolower(c);
61 				}
62 			}
63 			res = _nss_nis_lookup(be, argp, 1, "hosts.byname",
64 				copy, 0);
65 			free(copy);
66 			argp->h_errno = __nss2herrno(res);
67 			return (res);
68 		}
69 	}
70 	res = _nss_nis_lookup(be, argp, 1,
71 				"hosts.byname", argp->key.name, 0);
72 	if (res != NSS_SUCCESS)
73 		argp->h_errno = __nss2herrno(res);
74 	return (res);
75 }
76 
77 static nss_status_t
78 getbyaddr(be, a)
79 	nis_backend_ptr_t	be;
80 	void			*a;
81 {
82 	nss_XbyY_args_t		*argp	= (nss_XbyY_args_t *)a;
83 	struct in_addr		addr;
84 	char			buf[18];
85 	nss_status_t	res;
86 	extern char	*inet_ntoa_r(); /* not an advertised function from */
87 					/* libnsl (no man page), so no */
88 					/* prototype. */
89 
90 	/* === Do we really want to be this pedantic? */
91 	if (argp->key.hostaddr.type != AF_INET ||
92 	    argp->key.hostaddr.len  != sizeof (addr)) {
93 		return (NSS_NOTFOUND);
94 	}
95 	(void) memcpy(&addr, argp->key.hostaddr.addr, sizeof (addr));
96 	res = _nss_nis_lookup(be, argp, 1, "hosts.byaddr",
97 				inet_ntoa_r(addr, buf), 0);
98 	if (res != NSS_SUCCESS)
99 		argp->h_errno = __nss2herrno(res);
100 	return (res);
101 }
102 
103 
104 static nis_backend_op_t host_ops[] = {
105 	_nss_nis_destr,
106 	_nss_nis_endent,
107 	_nss_nis_setent,
108 	_nss_nis_getent_netdb,
109 	getbyname,
110 	getbyaddr
111 };
112 
113 /*ARGSUSED*/
114 nss_backend_t *
115 _nss_nis_hosts_constr(dummy1, dummy2, dummy3)
116 	const char	*dummy1, *dummy2, *dummy3;
117 {
118 	return (_nss_nis_constr(host_ops,
119 				sizeof (host_ops) / sizeof (host_ops[0]),
120 				"hosts.byaddr"));
121 }
122