1 /* Copyright (C) 2000-2020 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
21 #include <resolv/resolv_context.h>
23 /* Set up NIP to run through the services. If ALL is zero, use NIP's
24 current location if it's not nil. Return nonzero if there are no
27 setup (const char *func_name, db_lookup_function lookup_fct,
28 void **fctp, service_user **nip, service_user **startp, int all)
33 no_more = lookup_fct (nip, func_name, NULL, fctp);
34 *startp = no_more ? (service_user *) -1l : *nip;
36 else if (*startp == (service_user *) -1l)
37 /* No services at all. */
42 /* Reset to the beginning of the service list. */
44 /* Look up the first function. */
45 no_more = __nss_lookup (nip, func_name, NULL, fctp);
51 __nss_setent (const char *func_name, db_lookup_function lookup_fct,
52 service_user **nip, service_user **startp,
53 service_user **last_nip, int stayopen, int *stayopen_tmp,
63 struct resolv_context *res_ctx = NULL;
66 res_ctx = __resolv_context_get ();
69 __set_h_errno (NETDB_INTERNAL);
74 /* Cycle through the services and run their `setXXent' functions until
75 we find an available service. */
76 no_more = setup (func_name, lookup_fct, &fct.ptr, nip,
80 int is_last_nip = *nip == *last_nip;
81 enum nss_status status;
84 status = DL_CALL_FCT (fct.f, (*stayopen_tmp));
86 status = DL_CALL_FCT (fct.f, (0));
89 /* This is a special-case. When [SUCCESS=merge] is in play,
90 _nss_next2() will skip to the next database. Due to the
91 implementation of that function, we can't know whether we're
92 in an enumeration or an individual lookup, which behaves
93 differently with regards to merging. We'll treat SUCCESS as
94 an indication to start the enumeration at this database. */
95 if (nss_next_action (*nip, status) == NSS_ACTION_MERGE)
98 no_more = __nss_next2 (nip, func_name, NULL, &fct.ptr, status, 0);
104 __resolv_context_put (res_ctx);
107 *stayopen_tmp = stayopen;
112 __nss_endent (const char *func_name, db_lookup_function lookup_fct,
113 service_user **nip, service_user **startp,
114 service_user **last_nip, int res)
123 struct resolv_context *res_ctx = NULL;
126 res_ctx = __resolv_context_get ();
129 __set_h_errno (NETDB_INTERNAL);
134 /* Cycle through all the services and run their endXXent functions. */
135 no_more = setup (func_name, lookup_fct, &fct.ptr, nip, startp, 1);
138 /* Ignore status, we force check in __NSS_NEXT. */
139 DL_CALL_FCT (fct.f, ());
141 if (*nip == *last_nip)
142 /* We have processed all services which were used. */
145 no_more = __nss_next2 (nip, func_name, NULL, &fct.ptr, 0, 1);
147 *last_nip = *nip = NULL;
149 __resolv_context_put (res_ctx);
154 __nss_getent_r (const char *getent_func_name,
155 const char *setent_func_name,
156 db_lookup_function lookup_fct,
157 service_user **nip, service_user **startp,
158 service_user **last_nip, int *stayopen_tmp, int res,
159 void *resbuf, char *buffer, size_t buflen,
160 void **result, int *h_errnop)
168 enum nss_status status;
170 struct resolv_context *res_ctx = NULL;
173 res_ctx = __resolv_context_get ();
176 *h_errnop = NETDB_INTERNAL;
182 /* Initialize status to return if no more functions are found. */
183 status = NSS_STATUS_NOTFOUND;
185 /* Run through available functions, starting with the same function last
186 run. We will repeat each function as long as it succeeds, and then go
187 on to the next service action. */
188 no_more = setup (getent_func_name, lookup_fct, &fct.ptr, nip,
192 int is_last_nip = *nip == *last_nip;
194 status = DL_CALL_FCT (fct.f,
195 (resbuf, buffer, buflen, &errno, &h_errno));
197 /* The status is NSS_STATUS_TRYAGAIN and errno is ERANGE the
198 provided buffer is too small. In this case we should give
199 the user the possibility to enlarge the buffer and we should
200 not simply go on with the next service (even if the TRYAGAIN
201 action tells us so). */
202 if (status == NSS_STATUS_TRYAGAIN
203 && (h_errnop == NULL || *h_errnop == NETDB_INTERNAL)
209 /* This is a special-case. When [SUCCESS=merge] is in play,
210 _nss_next2() will skip to the next database. Due to the
211 implementation of that function, we can't know whether we're
212 in an enumeration or an individual lookup, which behaves
213 differently with regards to merging. We'll treat SUCCESS as
214 an indication to return the results here. */
215 if (status == NSS_STATUS_SUCCESS
216 && nss_next_action (*nip, status) == NSS_ACTION_MERGE)
219 no_more = __nss_next2 (nip, getent_func_name, NULL, &fct.ptr,
227 /* Call the `setXXent' function. This wasn't done before. */
234 no_more = __nss_lookup (nip, setent_func_name, NULL, &sfct.ptr);
239 status = DL_CALL_FCT (sfct.f, (*stayopen_tmp));
241 status = DL_CALL_FCT (sfct.f, (0));
244 status = NSS_STATUS_NOTFOUND;
247 while (! no_more && status != NSS_STATUS_SUCCESS);
250 __resolv_context_put (res_ctx);
252 *result = status == NSS_STATUS_SUCCESS ? resbuf : NULL;
253 return (status == NSS_STATUS_SUCCESS ? 0
254 : status != NSS_STATUS_TRYAGAIN ? ENOENT
255 /* h_errno functions only set errno if h_errno is NETDB_INTERNAL. */
256 : (h_errnop == NULL || *h_errnop == NETDB_INTERNAL) ? errno