3 * shadow database for nss_nonlocal proxy.
5 * Copyright © 2007–2010 Anders Kaseorg <andersk@mit.edu>
7 * This file is part of nss_nonlocal.
9 * nss_nonlocal is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1 of
12 * the License, or (at your option) any later version.
14 * nss_nonlocal is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with nss_nonlocal; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
27 #include <sys/types.h>
38 #include "nsswitch-internal.h"
42 static service_user *__nss_shadow_nonlocal_database;
46 __nss_shadow_nonlocal_lookup2(service_user **ni, const char *fct_name,
47 const char *fct2_name, void **fctp)
49 if (__nss_shadow_nonlocal_database == NULL
50 && __nss_database_lookup("shadow_nonlocal", NULL, NULL,
51 &__nss_shadow_nonlocal_database) < 0)
54 *ni = __nss_shadow_nonlocal_database;
56 *fctp = __nss_lookup_function(*ni, fct_name);
57 if (*fctp == NULL && fct2_name != NULL)
58 *fctp = __nss_lookup_function(*ni, fct2_name);
63 static bool spent_initialized = false;
64 static service_user *spent_startp, *spent_nip;
65 static void *spent_fct_start;
67 enum nss_status (*l)(struct spwd *pwd, char *buffer, size_t buflen,
71 static const char *spent_fct_name = "getspent_r";
74 _nss_nonlocal_setspent(int stayopen)
76 enum nss_status status;
77 const struct walk_nss w = {
78 .lookup2 = &__nss_shadow_nonlocal_lookup2, .fct_name = "setspent",
81 const __typeof__(&_nss_nonlocal_setspent) self = NULL;
82 #define args (stayopen)
85 if (status != NSS_STATUS_SUCCESS)
88 if (!spent_initialized) {
89 __nss_shadow_nonlocal_lookup2(&spent_startp, spent_fct_name, NULL,
92 spent_initialized = true;
94 spent_nip = spent_startp;
95 spent_fct.ptr = spent_fct_start;
96 return NSS_STATUS_SUCCESS;
100 _nss_nonlocal_endspent(void)
102 enum nss_status status;
103 const struct walk_nss w = {
104 .lookup2 = &__nss_shadow_nonlocal_lookup2, .fct_name = "endspent",
107 const __typeof__(&_nss_nonlocal_endspent) self = NULL;
112 #include "walk_nss.h"
118 _nss_nonlocal_getspent_r(struct spwd *pwd, char *buffer, size_t buflen,
121 enum nss_status status;
123 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
124 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
125 return NSS_STATUS_UNAVAIL;
127 if (spent_nip == NULL) {
128 status = _nss_nonlocal_setspent(0);
129 if (status != NSS_STATUS_SUCCESS)
133 if (spent_fct.ptr == NULL)
134 status = NSS_STATUS_UNAVAIL;
136 status = DL_CALL_FCT(spent_fct.l, (pwd, buffer, buflen, errnop));
137 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
140 if (status == NSS_STATUS_SUCCESS)
141 return NSS_STATUS_SUCCESS;
142 } while (__nss_next2(&spent_nip, spent_fct_name, NULL, &spent_fct.ptr,
146 return NSS_STATUS_NOTFOUND;
151 _nss_nonlocal_getspnam_r(const char *name, struct spwd *pwd,
152 char *buffer, size_t buflen, int *errnop)
154 enum nss_status status;
155 const struct walk_nss w = {
156 .lookup2 = __nss_shadow_nonlocal_lookup2, .fct_name = "getspnam_r",
157 .status = &status, .errnop = errnop
159 const __typeof__(&_nss_nonlocal_getspnam_r) self = NULL;
160 #define args (name, pwd, buffer, buflen, errnop)
161 #include "walk_nss.h"
163 if (status != NSS_STATUS_SUCCESS)
166 if (strcmp(name, pwd->sp_namp) != 0) {
167 syslog(LOG_ERR, "nss_nonlocal: discarding shadow %s from lookup for shadow %s\n", pwd->sp_namp, name);
168 return NSS_STATUS_NOTFOUND;
171 return NSS_STATUS_SUCCESS;