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-1995 Sun Microsystems Inc
24  *	All Rights Reserved.
25  *
26  *	files/getservent.c -- "files" backend for nsswitch "services" database
27  */
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 #include <netdb.h>
32 #include "files_common.h"
33 #include <sys/types.h>
34 #include <netinet/in.h>
35 #include <strings.h>
36 
37 static int
38 check_name(args)
39 	nss_XbyY_args_t		*args;
40 {
41 	struct servent		*serv	= (struct servent *) args->returnval;
42 	const char		*name	= args->key.serv.serv.name;
43 	const char		*proto	= args->key.serv.proto;
44 	char			**aliasp;
45 
46 	if (proto != 0 && strcmp(serv->s_proto, proto) != 0) {
47 		return (0);
48 	}
49 	if (strcmp(serv->s_name, name) == 0) {
50 		return (1);
51 	}
52 	for (aliasp = serv->s_aliases;  *aliasp != 0;  aliasp++) {
53 		if (strcmp(*aliasp, name) == 0) {
54 			return (1);
55 		}
56 	}
57 	return (0);
58 }
59 
60 static nss_status_t
61 getbyname(be, a)
62 	files_backend_ptr_t	be;
63 	void			*a;
64 {
65 	nss_XbyY_args_t		*argp = (nss_XbyY_args_t *) a;
66 
67 	return (_nss_files_XY_all(be, argp, 1,
68 				argp->key.serv.serv.name, check_name));
69 }
70 
71 static int
72 check_port(args)
73 	nss_XbyY_args_t		*args;
74 {
75 	struct servent		*serv	= (struct servent *) args->returnval;
76 	const char		*proto	= args->key.serv.proto;
77 
78 	return (serv->s_port == args->key.serv.serv.port &&
79 		(proto == 0 || strcmp(serv->s_proto, proto) == 0));
80 }
81 
82 static nss_status_t
83 getbyport(be, a)
84 	files_backend_ptr_t	be;
85 	void			*a;
86 {
87 	nss_XbyY_args_t		*argp	= (nss_XbyY_args_t *) a;
88 	char			portstr[12];
89 
90 	sprintf(portstr, "%d", ntohs(argp->key.serv.serv.port));
91 	return (_nss_files_XY_all(be, argp, 1, portstr, check_port));
92 }
93 
94 static files_backend_op_t serv_ops[] = {
95 	_nss_files_destr,
96 	_nss_files_endent,
97 	_nss_files_setent,
98 	_nss_files_getent_netdb,
99 	getbyname,
100 	getbyport
101 };
102 
103 /*ARGSUSED*/
104 nss_backend_t *
105 _nss_files_services_constr(dummy1, dummy2, dummy3)
106 	const char	*dummy1, *dummy2, *dummy3;
107 {
108 	return (_nss_files_constr(serv_ops,
109 				sizeof (serv_ops) / sizeof (serv_ops[0]),
110 				_PATH_SERVICES,
111 				NSS_LINELEN_SERVICES,
112 				NULL));
113 }
114