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 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 /*
27  * Copyright (c) 2017, Joyent, INc.
28  */
29 
30 /*
31  * Common code and structures used by name-service-switch "files" backends.
32  */
33 
34 #ifndef _FILES_COMMON_H
35 #define	_FILES_COMMON_H
36 
37 #include <nss_common.h>
38 #include <nss_dbdefs.h>
39 #include <stdio.h>
40 
41 #ifdef	__cplusplus
42 extern "C" {
43 #endif
44 
45 typedef struct files_backend *files_backend_ptr_t;
46 typedef nss_status_t	(*files_backend_op_t)(files_backend_ptr_t, void *);
47 
48 typedef uint_t (*files_hash_func)(nss_XbyY_args_t *, int, const char *, int);
49 
50 typedef struct files_hashent {
51 	struct files_hashent	*h_first;
52 	struct files_hashent	*h_next;
53 	uint_t			h_hash;
54 } files_hashent_t;
55 
56 typedef struct {
57 	char			*l_start;
58 	int			l_len;
59 } files_linetab_t;
60 
61 typedef struct {
62 	mutex_t		fh_lock;
63 	int		fh_resultsize;
64 	int		fh_bufsize;
65 	int		fh_nhtab;
66 	files_hash_func	*fh_hash_func;
67 	int		fh_refcnt;
68 	int		fh_size;
69 	timestruc_t	fh_mtime;
70 	char		*fh_file_start;
71 	char		*fh_file_end;
72 	files_linetab_t	*fh_line;
73 	files_hashent_t	*fh_table;
74 } files_hash_t;
75 
76 struct files_backend {
77 	files_backend_op_t	*ops;
78 	int			n_ops;
79 	const char		*filename;
80 	FILE			*f;
81 	int			minbuf;
82 	char			*buf;
83 	files_hash_t		*hashinfo;
84 };
85 
86 /*
87  * Iterator function for _nss_files_do_all(), which probably calls yp_all().
88  *   NSS_NOTFOUND means "keep enumerating", NSS_SUCCESS means"return now",
89  *   other values don't make much sense.  In other words we're abusing
90  *   (overloading) the meaning of nss_status_t, but hey...
91  * _nss_files_XY_all() is a wrapper around _nss_files_do_all() that does the
92  *   generic work for nss_XbyY_args_t backends (calls cstr2ent etc).
93  */
94 typedef nss_status_t	(*files_do_all_func_t)(const char *, int, void *args);
95 typedef int		(*files_XY_check_func)(nss_XbyY_args_t *,
96 						const char *, int);
97 
98 #if defined(__STDC__)
99 extern nss_backend_t	*_nss_files_constr(files_backend_op_t	*ops,
100 					int			n_ops,
101 					const char		*filename,
102 					int			min_bufsize,
103 					files_hash_t		*fhp);
104 extern nss_status_t	_nss_files_destr(files_backend_ptr_t, void *dummy);
105 extern nss_status_t	_nss_files_setent(files_backend_ptr_t, void *dummy);
106 extern nss_status_t	_nss_files_endent(files_backend_ptr_t, void *dummy);
107 extern nss_status_t	_nss_files_getent_rigid(files_backend_ptr_t, void *);
108 extern nss_status_t	_nss_files_getent_netdb(files_backend_ptr_t, void *);
109 extern nss_status_t 	_nss_files_do_all(files_backend_ptr_t,
110 					void			*func_priv,
111 					const char		*filter,
112 					files_do_all_func_t	func);
113 extern nss_status_t 	_nss_files_XY_all(files_backend_ptr_t	be,
114 					nss_XbyY_args_t		*args,
115 					int 			netdb,
116 					const char		*filter,
117 					files_XY_check_func	check);
118 extern nss_status_t 	_nss_files_XY_hash(files_backend_ptr_t	be,
119 					nss_XbyY_args_t		*args,
120 					int 			netdb,
121 					files_hash_t		*fhp,
122 					int			hashop,
123 					files_XY_check_func	check);
124 int _nss_files_read_line(FILE *f, char *buffer, int	buflen);
125 #else
126 extern nss_backend_t	*_nss_files_constr();
127 extern nss_status_t	_nss_files_destr();
128 extern nss_status_t	_nss_files_setent();
129 extern nss_status_t	_nss_files_endent();
130 extern nss_status_t	_nss_files_getent_rigid();
131 extern nss_status_t	_nss_files_getent_netdb();
132 extern nss_status_t	_nss_files_do_all();
133 extern nss_status_t	_nss_files_XY_all();
134 extern nss_status_t	_nss_files_XY_hash();
135 #endif
136 
137 int	_nss_files_check_name_aliases(nss_XbyY_args_t *, const char *, int);
138 int	_nss_files_check_name_colon(nss_XbyY_args_t *, const char *, int);
139 
140 /* passwd and group validation functions */
141 extern int	validate_group_ids(char *line, int *linelenp, int buflen,
142 			int extra_chars, files_XY_check_func check);
143 extern int	validate_passwd_ids(char *line, int *linelenp, int buflen,
144 			int extra_chars);
145 
146 /*
147  * Destructors used by different bits.
148  */
149 extern void getexecattr_fini(void);
150 
151 #ifdef	__cplusplus
152 }
153 #endif
154 
155 #endif /* _FILES_COMMON_H */
156