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