3 * shadow database for nss_nonlocal proxy.
5 * Copyright © 2007 Anders Kaseorg <andersk@mit.edu>
7 * Permission is hereby granted, free of charge, to any person
8 * obtaining a copy of this software and associated documentation
9 * files (the "Software"), to deal in the Software without
10 * restriction, including without limitation the rights to use, copy,
11 * modify, merge, publish, distribute, sublicense, and/or sell copies
12 * of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 #include <sys/types.h>
41 #include "nsswitch-internal.h"
46 nss_shadow_nonlocal_database(void)
48 static service_user *nip = NULL;
50 __nss_database_lookup("shadow_nonlocal", NULL, "", &nip);
56 static service_user *spent_nip = NULL;
57 static void *spent_fct_start;
59 enum nss_status (*l)(struct spwd *pwd, char *buffer, size_t buflen,
63 static const char *spent_fct_name = "getspent_r";
66 _nss_nonlocal_setspent(int stayopen)
68 static const char *fct_name = "setspent";
69 static void *fct_start = NULL;
70 enum nss_status status;
73 enum nss_status (*l)(int stayopen);
77 nip = nss_shadow_nonlocal_database();
79 return NSS_STATUS_UNAVAIL;
80 if (fct_start == NULL)
81 fct_start = __nss_lookup_function(nip, fct_name);
85 status = NSS_STATUS_UNAVAIL;
87 status = DL_CALL_FCT(fct.l, (stayopen));
88 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
89 if (status != NSS_STATUS_SUCCESS)
93 if (spent_fct_start == NULL)
94 spent_fct_start = __nss_lookup_function(nip, spent_fct_name);
95 spent_fct.ptr = spent_fct_start;
96 return NSS_STATUS_SUCCESS;
100 _nss_nonlocal_endspent(void)
102 static const char *fct_name = "endspent";
103 static void *fct_start = NULL;
104 enum nss_status status;
107 enum nss_status (*l)(void);
113 nip = nss_shadow_nonlocal_database();
115 return NSS_STATUS_UNAVAIL;
116 if (fct_start == NULL)
117 fct_start = __nss_lookup_function(nip, fct_name);
121 status = NSS_STATUS_UNAVAIL;
123 status = DL_CALL_FCT(fct.l, ());
124 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
129 _nss_nonlocal_getspent_r(struct spwd *pwd, char *buffer, size_t buflen,
132 enum nss_status status;
133 if (spent_nip == NULL) {
134 status = _nss_nonlocal_setspent(0);
135 if (status != NSS_STATUS_SUCCESS)
139 if (spent_fct.ptr == NULL)
140 status = NSS_STATUS_UNAVAIL;
142 status = DL_CALL_FCT(spent_fct.l, (pwd, buffer, buflen, errnop));
143 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
146 if (status == NSS_STATUS_SUCCESS)
147 return NSS_STATUS_SUCCESS;
148 } while (__nss_next(&spent_nip, spent_fct_name, &spent_fct.ptr, status, 0) == 0);
151 return NSS_STATUS_NOTFOUND;
156 _nss_nonlocal_getspnam_r(const char *name, struct spwd *pwd,
157 char *buffer, size_t buflen, int *errnop)
159 static const char *fct_name = "getspnam_r";
160 static void *fct_start = NULL;
161 enum nss_status status;
164 enum nss_status (*l)(const char *name, struct spwd *pwd,
165 char *buffer, size_t buflen, int *errnop);
169 nip = nss_shadow_nonlocal_database();
171 return NSS_STATUS_UNAVAIL;
172 if (fct_start == NULL)
173 fct_start = __nss_lookup_function(nip, fct_name);
177 status = NSS_STATUS_UNAVAIL;
179 status = DL_CALL_FCT(fct.l, (name, pwd, buffer, buflen, errnop));
180 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
182 } while (__nss_next(&nip, fct_name, &fct.ptr, status, 0) == 0);
183 if (status != NSS_STATUS_SUCCESS)
186 if (strcmp(name, pwd->sp_namp) != 0) {
187 syslog(LOG_ERR, "nss_nonlocal: discarding shadow %s from lookup for shadow %s\n", pwd->sp_namp, name);
188 return NSS_STATUS_NOTFOUND;
191 return NSS_STATUS_SUCCESS;