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