xref: /freebsd/lib/libc/include/nscache.h (revision 8881d206f4e68b564c2c5f50fc717086fc3e827a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2005 Michael Bushkov <bushman@rsu.ru>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #ifndef __NS_CACHE_H__
32 #define __NS_CACHE_H__
33 
34 #include "nscachedcli.h"
35 
36 typedef int (*nss_cache_id_func_t)(char *, size_t *, va_list, void *);
37 typedef int (*nss_cache_marshal_func_t)(char *, size_t *, void *, va_list,
38 	void *);
39 typedef int (*nss_cache_unmarshal_func_t)(char *, size_t, void *, va_list,
40 	void *);
41 
42 typedef	void (*nss_set_mp_ws_func_t)(cached_mp_write_session);
43 typedef	cached_mp_write_session	(*nss_get_mp_ws_func_t)(void);
44 
45 typedef void (*nss_set_mp_rs_func_t)(cached_mp_read_session);
46 typedef cached_mp_read_session	(*nss_get_mp_rs_func_t)(void);
47 
48 typedef struct _nss_cache_info {
49 	char	*entry_name;
50 	void	*mdata;
51 
52 	/*
53 	 * These 3 functions should be implemented specifically for each
54 	 * nsswitch database.
55 	 */
56 	nss_cache_id_func_t id_func;	/* marshals the request parameters */
57 	nss_cache_marshal_func_t marshal_func;	   /* marshals response */
58 	nss_cache_unmarshal_func_t unmarshal_func; /* unmarshals response */
59 
60 	/*
61 	 * These 4 functions should be generated with NSS_MP_CACHE_HANDLING
62 	 * macro.
63 	 */
64 	nss_set_mp_ws_func_t set_mp_ws_func; /* sets current write session */
65 	nss_get_mp_ws_func_t get_mp_ws_func; /* gets current write session */
66 
67 	nss_set_mp_rs_func_t set_mp_rs_func; /* sets current read session */
68 	nss_get_mp_rs_func_t get_mp_rs_func; /* gets current read session */
69 } nss_cache_info;
70 
71 /*
72  * NSS_MP_CACHE_HANDLING implements the set_mp_ws, get_mp_ws, set_mp_rs,
73  * get_mp_rs functions, that are used in _nss_cache_info. It uses
74  * NSS_TLS_HANDLING macro to organize thread local storage.
75  */
76 #define NSS_MP_CACHE_HANDLING(name)					\
77 struct name##_mp_state {						\
78 	cached_mp_write_session	mp_write_session;			\
79 	cached_mp_read_session	mp_read_session;			\
80 };									\
81 									\
82 static void								\
83 name##_mp_endstate(void *s) {						\
84 	struct name##_mp_state	*mp_state;				\
85 									\
86 	mp_state = (struct name##_mp_state *)s;				\
87 	if (mp_state->mp_write_session != INVALID_CACHED_MP_WRITE_SESSION)\
88 		__abandon_cached_mp_write_session(mp_state->mp_write_session);\
89 									\
90 	if (mp_state->mp_read_session != INVALID_CACHED_MP_READ_SESSION)\
91 		__close_cached_mp_read_session(mp_state->mp_read_session);\
92 }									\
93 NSS_TLS_HANDLING(name##_mp);						\
94 									\
95 static void								\
96 name##_set_mp_ws(cached_mp_write_session ws)				\
97 {									\
98 	struct name##_mp_state	*mp_state;				\
99 	int	res;							\
100 									\
101 	res = name##_mp_getstate(&mp_state);				\
102 	if (res != 0)							\
103 		return;							\
104 									\
105 	mp_state->mp_write_session = ws;				\
106 }									\
107 									\
108 static cached_mp_write_session						\
109 name##_get_mp_ws(void)							\
110 {									\
111 	struct name##_mp_state	*mp_state;				\
112 	int	res;							\
113 									\
114 	res = name##_mp_getstate(&mp_state);				\
115 	if (res != 0)							\
116 		return (INVALID_CACHED_MP_WRITE_SESSION);		\
117 									\
118 	return (mp_state->mp_write_session);				\
119 }									\
120 									\
121 static void								\
122 name##_set_mp_rs(cached_mp_read_session rs)				\
123 {									\
124 	struct name##_mp_state	*mp_state;				\
125 	int	res;							\
126 									\
127 	res = name##_mp_getstate(&mp_state);				\
128 	if (res != 0)							\
129 		return;							\
130 									\
131 	mp_state->mp_read_session = rs;					\
132 }									\
133 									\
134 static cached_mp_read_session						\
135 name##_get_mp_rs(void)							\
136 {									\
137 	struct name##_mp_state	*mp_state;				\
138 	int	res;							\
139 									\
140 	res = name##_mp_getstate(&mp_state);				\
141 	if (res != 0)							\
142 		return (INVALID_CACHED_MP_READ_SESSION);		\
143 									\
144 	return (mp_state->mp_read_session);				\
145 }
146 
147 /*
148  * These macros should be used to initialize _nss_cache_info structure. For
149  * multipart queries in setXXXent and getXXXent functions mf and uf
150  * (marshal function and unmarshal function) should be both NULL.
151  */
152 #define NS_COMMON_CACHE_INFO_INITIALIZER(name, mdata, if, mf, uf)	\
153 	{#name, mdata, if, mf, uf, NULL, NULL, NULL, NULL}
154 #define NS_MP_CACHE_INFO_INITIALIZER(name, mdata, mf, uf)		\
155 	{#name, mdata, NULL, mf, uf, name##_set_mp_ws, name##_get_mp_ws,\
156 		name##_set_mp_rs, name##_get_mp_rs }
157 
158 /*
159  * Analog of other XXX_CB macros. Has the pointer to _nss_cache_info
160  * structure as the only argument.
161  */
162 #define NS_CACHE_CB(cinfo) {NSSRC_CACHE, __nss_cache_handler, (void *)(cinfo) },
163 
164 /* args are: current pointer, current buffer, initial buffer, pointer type */
165 #define NS_APPLY_OFFSET(cp, cb, ib, p_type)				\
166 	if ((cp) != NULL)						\
167 		(cp) = (p_type)((char *)(cb) + (size_t)(cp) - (size_t)(ib))
168 /*
169  * Gets new pointer from the marshalled buffer by uisng initial address
170  * and initial buffer address
171  */
172 #define NS_GET_NEWP(cp, cb, ib)						\
173 	((char *)(cb) + (size_t)(cp) - (size_t)(ib))
174 
175 typedef struct _nss_cache_data {
176 	char	*key;
177 	size_t	key_size;
178 
179 	nss_cache_info const	*info;
180 } nss_cache_data;
181 
182 __BEGIN_DECLS
183 /* dummy function, which is needed to make nss_method_lookup happy */
184 extern	int	__nss_cache_handler(void *, void *, va_list);
185 
186 #ifdef _NS_PRIVATE
187 extern	int	__nss_common_cache_read(void *, void *, va_list);
188 extern	int	__nss_common_cache_write(void *, void *, va_list);
189 extern	int	__nss_common_cache_write_negative(void *);
190 
191 extern	int	__nss_mp_cache_read(void *, void *, va_list);
192 extern	int	__nss_mp_cache_write(void *, void *, va_list);
193 extern	int	__nss_mp_cache_write_submit(void *, void *, va_list);
194 extern	int	__nss_mp_cache_end(void *, void *, va_list);
195 #endif /* _NS_PRIVATE */
196 
197 __END_DECLS
198 
199 #endif
200