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