1 | /* Copyright (C) 1996-2021 Free Software Foundation, Inc. |
2 | This file is part of the GNU C Library. |
3 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996. |
4 | |
5 | The GNU C Library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either |
8 | version 2.1 of the License, or (at your option) any later version. |
9 | |
10 | The GNU C Library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Lesser General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, see |
17 | <https://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <errno.h> |
20 | #include <libc-lock.h> |
21 | |
22 | #include "nsswitch.h" |
23 | |
24 | /*******************************************************************\ |
25 | |* Here we assume several symbols to be defined: *| |
26 | |* *| |
27 | |* LOOKUP_TYPE - the return type of the function *| |
28 | |* *| |
29 | |* SETFUNC_NAME - name of the non-reentrant setXXXent function *| |
30 | |* *| |
31 | |* GETFUNC_NAME - name of the non-reentrant getXXXent function *| |
32 | |* *| |
33 | |* ENDFUNC_NAME - name of the non-reentrant endXXXent function *| |
34 | |* *| |
35 | |* DATABASE_NAME - name of the database the function accesses *| |
36 | |* (e.g., host, services, ...) *| |
37 | |* *| |
38 | |* Optionally the following vars can be defined: *| |
39 | |* *| |
40 | |* STAYOPEN - variable declaration for setXXXent function *| |
41 | |* *| |
42 | |* STAYOPEN_VAR - variable name for setXXXent function *| |
43 | |* *| |
44 | |* NEED_H_ERRNO - an extra parameter will be passed to point to *| |
45 | |* the global `h_errno' variable. *| |
46 | |* *| |
47 | \*******************************************************************/ |
48 | |
49 | /* To make the real sources a bit prettier. */ |
50 | #define REENTRANT_GETNAME APPEND_R (GETFUNC_NAME) |
51 | #define APPEND_R(Name) CONCAT2_2 (Name, _r) |
52 | #define INTERNAL(Name) CONCAT2_2 (__, Name) |
53 | #define CONCAT2_1(Pre, Post) CONCAT2_2 (Pre, Post) |
54 | #define CONCAT2_2(Pre, Post) Pre##Post |
55 | #define NEW(name) NEW1 (name) |
56 | #define NEW1(name) __new_##name |
57 | |
58 | #define SETFUNC_NAME_STRING STRINGIZE (SETFUNC_NAME) |
59 | #define GETFUNC_NAME_STRING STRINGIZE (REENTRANT_GETNAME) |
60 | #define ENDFUNC_NAME_STRING STRINGIZE (ENDFUNC_NAME) |
61 | #define DATABASE_NAME_STRING STRINGIZE (DATABASE_NAME) |
62 | #define STRINGIZE(Name) STRINGIZE1 (Name) |
63 | #define STRINGIZE1(Name) #Name |
64 | |
65 | #ifndef DB_LOOKUP_FCT |
66 | # define DB_LOOKUP_FCT CONCAT3_1 (__nss_, DATABASE_NAME, _lookup2) |
67 | # define CONCAT3_1(Pre, Name, Post) CONCAT3_2 (Pre, Name, Post) |
68 | # define CONCAT3_2(Pre, Name, Post) Pre##Name##Post |
69 | #endif |
70 | |
71 | /* Sometimes we need to store error codes in the `h_errno' variable. */ |
72 | #ifdef NEED_H_ERRNO |
73 | # define H_ERRNO_PARM , int *h_errnop |
74 | # define H_ERRNO_VAR , &h_errno |
75 | # define H_ERRNO_VAR_P &h_errno |
76 | #else |
77 | # define H_ERRNO_PARM |
78 | # define H_ERRNO_VAR |
79 | # define H_ERRNO_VAR_P NULL |
80 | #endif |
81 | |
82 | /* Some databases take the `stayopen' flag. */ |
83 | #ifdef STAYOPEN |
84 | # define STAYOPEN_TMP CONCAT2_1 (STAYOPEN, _tmp) |
85 | # define STAYOPEN_TMPVAR &CONCAT2_1 (STAYOPEN_VAR, _tmp) |
86 | #else |
87 | # define STAYOPEN void |
88 | # define STAYOPEN_VAR 0 |
89 | # define STAYOPEN_TMPVAR NULL |
90 | #endif |
91 | |
92 | #ifndef NEED__RES |
93 | # define NEED__RES 0 |
94 | #endif |
95 | |
96 | /* This handle for the NSS data base is shared between all |
97 | set/get/endXXXent functions. */ |
98 | static nss_action_list nip; |
99 | /* Remember the last service used since the last call to `endXXent'. */ |
100 | static nss_action_list last_nip; |
101 | /* Remember the first service_entry across set/get/endent. */ |
102 | static nss_action_list startp; |
103 | |
104 | #ifdef STAYOPEN_TMP |
105 | /* We need to remember the last `stayopen' flag given by the user |
106 | since the `setent' function is only called for the first available |
107 | service. */ |
108 | static STAYOPEN_TMP; |
109 | #endif |
110 | |
111 | /* Protect above variable against multiple uses at the same time. */ |
112 | __libc_lock_define_initialized (static, lock) |
113 | |
114 | /* The lookup function for the first entry of this service. */ |
115 | extern int DB_LOOKUP_FCT (nss_action_list *nip, const char *name, |
116 | const char *name2, void **fctp); |
117 | libc_hidden_proto (DB_LOOKUP_FCT) |
118 | |
119 | void |
120 | SETFUNC_NAME (STAYOPEN) |
121 | { |
122 | int save; |
123 | |
124 | __libc_lock_lock (lock); |
125 | __nss_setent (SETFUNC_NAME_STRING, DB_LOOKUP_FCT, &nip, &startp, |
126 | &last_nip, STAYOPEN_VAR, STAYOPEN_TMPVAR, NEED__RES); |
127 | |
128 | save = errno; |
129 | __libc_lock_unlock (lock); |
130 | __set_errno (save); |
131 | } |
132 | |
133 | |
134 | void |
135 | ENDFUNC_NAME (void) |
136 | { |
137 | int save; |
138 | |
139 | /* If the service has not been used before do not do anything. */ |
140 | if (startp != NULL) |
141 | { |
142 | __libc_lock_lock (lock); |
143 | __nss_endent (ENDFUNC_NAME_STRING, DB_LOOKUP_FCT, &nip, &startp, |
144 | &last_nip, NEED__RES); |
145 | save = errno; |
146 | __libc_lock_unlock (lock); |
147 | __set_errno (save); |
148 | } |
149 | } |
150 | |
151 | |
152 | int |
153 | INTERNAL (REENTRANT_GETNAME) (LOOKUP_TYPE *resbuf, char *buffer, size_t buflen, |
154 | LOOKUP_TYPE **result H_ERRNO_PARM) |
155 | { |
156 | int status; |
157 | int save; |
158 | |
159 | __libc_lock_lock (lock); |
160 | status = __nss_getent_r (GETFUNC_NAME_STRING, SETFUNC_NAME_STRING, |
161 | DB_LOOKUP_FCT, &nip, &startp, &last_nip, |
162 | STAYOPEN_TMPVAR, NEED__RES, resbuf, buffer, |
163 | buflen, (void **) result, H_ERRNO_VAR_P); |
164 | save = errno; |
165 | __libc_lock_unlock (lock); |
166 | __set_errno (save); |
167 | return status; |
168 | } |
169 | |
170 | |
171 | #ifdef NO_COMPAT_NEEDED |
172 | strong_alias (INTERNAL (REENTRANT_GETNAME), REENTRANT_GETNAME); |
173 | #else |
174 | # include <shlib-compat.h> |
175 | # if SHLIB_COMPAT (libc, GLIBC_2_0, GLIBC_2_1_2) |
176 | # define OLD(name) OLD1 (name) |
177 | # define OLD1(name) __old_##name |
178 | |
179 | int |
180 | attribute_compat_text_section |
181 | OLD (REENTRANT_GETNAME) (LOOKUP_TYPE *resbuf, char *buffer, size_t buflen, |
182 | LOOKUP_TYPE **result H_ERRNO_PARM) |
183 | { |
184 | int ret = INTERNAL (REENTRANT_GETNAME) (resbuf, buffer, buflen, |
185 | result H_ERRNO_VAR); |
186 | |
187 | if (ret != 0) |
188 | ret = -1; |
189 | |
190 | return ret; |
191 | } |
192 | |
193 | # define do_symbol_version(real, name, version) \ |
194 | compat_symbol (libc, real, name, version) |
195 | do_symbol_version (OLD (REENTRANT_GETNAME), REENTRANT_GETNAME, GLIBC_2_0); |
196 | # endif |
197 | |
198 | /* As INTERNAL (REENTRANT_GETNAME) may be hidden, we need an alias |
199 | in between so that the REENTRANT_GETNAME@@GLIBC_2.1.2 is not |
200 | hidden too. */ |
201 | strong_alias (INTERNAL (REENTRANT_GETNAME), NEW (REENTRANT_GETNAME)); |
202 | |
203 | # define do_default_symbol_version(real, name, version) \ |
204 | versioned_symbol (libc, real, name, version) |
205 | do_default_symbol_version (NEW (REENTRANT_GETNAME), |
206 | REENTRANT_GETNAME, GLIBC_2_1_2); |
207 | #endif |
208 | |
209 | nss_interface_function (SETFUNC_NAME) |
210 | nss_interface_function (ENDFUNC_NAME) |
211 | nss_interface_function (REENTRANT_GETNAME) |
212 | |