xref: /illumos-gate/usr/src/lib/nsswitch/files/common/getprotoent.c (revision 4c87aefe8930bd07275b8dd2e96ea5f24d93a52e)
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  * files/getprotoent.c -- "files" backend for nsswitch "protocols" database
26  */
27 
28 #pragma ident	"%Z%%M%	%I%	%E% SMI"
29 
30 #include <netdb.h>
31 #include "files_common.h"
32 #include <strings.h>
33 #include <ctype.h>
34 #include <stdlib.h>
35 
36 static nss_status_t
37 getbyname(be, a)
38 	files_backend_ptr_t	be;
39 	void		*a;
40 {
41 	nss_XbyY_args_t		*argp = (nss_XbyY_args_t *)a;
42 
43 	return (_nss_files_XY_all(be, argp, 1, argp->key.name,
44 			_nss_files_check_name_aliases));
45 }
46 
47 static int
48 check_addr(nss_XbyY_args_t *argp, const char *line, int linelen)
49 {
50 	int		proto;
51 	const char	*limit, *linep;
52 
53 	linep = line;
54 	limit = line + linelen;
55 
56 	/* skip name */
57 	while (linep < limit && !isspace(*linep))
58 		linep++;
59 	/* skip the delimiting spaces */
60 	while (linep < limit && isspace(*linep))
61 		linep++;
62 	if (linep == limit)
63 		return (0);
64 	proto = (int)strtol(linep, NULL, 10);
65 	return (proto == argp->key.number);
66 }
67 
68 static nss_status_t
69 getbynumber(be, a)
70 	files_backend_ptr_t	be;
71 	void		*a;
72 {
73 	nss_XbyY_args_t		*argp = (nss_XbyY_args_t *)a;
74 	char		numstr[12];
75 
76 	(void) snprintf(numstr, 12, "%d", argp->key.number);
77 	return (_nss_files_XY_all(be, argp, 1, 0, check_addr));
78 }
79 
80 static files_backend_op_t proto_ops[] = {
81 	_nss_files_destr,
82 	_nss_files_endent,
83 	_nss_files_setent,
84 	_nss_files_getent_netdb,
85 	getbyname,
86 	getbynumber
87 };
88 
89 /*ARGSUSED*/
90 nss_backend_t *
91 _nss_files_protocols_constr(dummy1, dummy2, dummy3)
92 	const char  *dummy1, *dummy2, *dummy3;
93 {
94 	return (_nss_files_constr(proto_ops,
95 				sizeof (proto_ops) / sizeof (proto_ops[0]),
96 				_PATH_PROTOCOLS,
97 				NSS_LINELEN_PROTOCOLS,
98 				NULL));
99 }
100