]> andersk Git - nss_nonlocal.git/blame - nonlocal-shadow.c
Guard one-time initialization with memory barriers
[nss_nonlocal.git] / nonlocal-shadow.c
CommitLineData
f6903667
AK
1/*
2 * nonlocal-shadow.c
3 * shadow database for nss_nonlocal proxy.
4 *
96a1ee0f 5 * Copyright © 2007–2010 Anders Kaseorg <andersk@mit.edu>
f6903667 6 *
96a1ee0f 7 * This file is part of nss_nonlocal.
f6903667 8 *
96a1ee0f
AK
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.
f6903667 13 *
96a1ee0f
AK
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.
18 *
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
22 * 02110-1301 USA
f6903667
AK
23 */
24
25#define _GNU_SOURCE
dc397f8f 26
f6903667 27#include <sys/types.h>
f6903667 28#include <dlfcn.h>
f6903667 29#include <errno.h>
f6903667 30#include <nss.h>
dc397f8f
AK
31#include <shadow.h>
32#include <stdbool.h>
33#include <stddef.h>
34#include <stdlib.h>
35#include <string.h>
36#include <syslog.h>
f6903667
AK
37
38#include "nsswitch-internal.h"
39#include "nonlocal.h"
40
41
cbb0e3ea
AK
42static service_user *__nss_shadow_nonlocal_database;
43
44static int
45internal_function
46__nss_shadow_nonlocal_lookup(service_user **ni, const char *fct_name,
47 void **fctp)
f6903667 48{
cbb0e3ea
AK
49 if (__nss_shadow_nonlocal_database == NULL
50 && __nss_database_lookup("shadow_nonlocal", NULL, NULL,
51 &__nss_shadow_nonlocal_database) < 0)
52 return -1;
53
54 *ni = __nss_shadow_nonlocal_database;
f6903667 55
cbb0e3ea
AK
56 *fctp = __nss_lookup_function(*ni, fct_name);
57 return 0;
f6903667
AK
58}
59
60
48939704 61static bool spent_initialized = false;
cbb0e3ea 62static service_user *spent_startp, *spent_nip;
f6903667
AK
63static void *spent_fct_start;
64static union {
65 enum nss_status (*l)(struct spwd *pwd, char *buffer, size_t buflen,
66 int *errnop);
67 void *ptr;
68} spent_fct;
69static const char *spent_fct_name = "getspent_r";
70
71enum nss_status
72_nss_nonlocal_setspent(int stayopen)
73{
f6903667 74 enum nss_status status;
cbb0e3ea
AK
75 const struct walk_nss w = {
76 .lookup = &__nss_shadow_nonlocal_lookup, .fct_name = "setspent",
77 .status = &status
78 };
79 const __typeof__(&_nss_nonlocal_setspent) self = NULL;
80#define args (stayopen)
81#include "walk_nss.h"
82#undef args
f6903667
AK
83 if (status != NSS_STATUS_SUCCESS)
84 return status;
85
48939704 86 if (!spent_initialized) {
cbb0e3ea
AK
87 __nss_shadow_nonlocal_lookup(&spent_startp, spent_fct_name,
88 &spent_fct_start);
48939704
AK
89 __sync_synchronize();
90 spent_initialized = true;
91 }
cbb0e3ea 92 spent_nip = spent_startp;
f6903667
AK
93 spent_fct.ptr = spent_fct_start;
94 return NSS_STATUS_SUCCESS;
95}
96
97enum nss_status
98_nss_nonlocal_endspent(void)
99{
f6903667 100 enum nss_status status;
cbb0e3ea
AK
101 const struct walk_nss w = {
102 .lookup = &__nss_shadow_nonlocal_lookup, .fct_name = "endspent",
103 .status = &status
104 };
105 const __typeof__(&_nss_nonlocal_endspent) self = NULL;
f6903667
AK
106
107 spent_nip = NULL;
108
cbb0e3ea
AK
109#define args ()
110#include "walk_nss.h"
111#undef args
f6903667
AK
112 return status;
113}
114
115enum nss_status
116_nss_nonlocal_getspent_r(struct spwd *pwd, char *buffer, size_t buflen,
117 int *errnop)
118{
119 enum nss_status status;
9cd080be
AK
120
121 char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
122 if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
123 return NSS_STATUS_UNAVAIL;
124
f6903667
AK
125 if (spent_nip == NULL) {
126 status = _nss_nonlocal_setspent(0);
127 if (status != NSS_STATUS_SUCCESS)
128 return status;
129 }
130 do {
131 if (spent_fct.ptr == NULL)
132 status = NSS_STATUS_UNAVAIL;
133 else
134 status = DL_CALL_FCT(spent_fct.l, (pwd, buffer, buflen, errnop));
135 if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE)
136 return status;
137
138 if (status == NSS_STATUS_SUCCESS)
139 return NSS_STATUS_SUCCESS;
140 } while (__nss_next(&spent_nip, spent_fct_name, &spent_fct.ptr, status, 0) == 0);
141
142 spent_nip = NULL;
143 return NSS_STATUS_NOTFOUND;
144}
145
146
147enum nss_status
148_nss_nonlocal_getspnam_r(const char *name, struct spwd *pwd,
149 char *buffer, size_t buflen, int *errnop)
150{
f6903667 151 enum nss_status status;
cbb0e3ea
AK
152 const struct walk_nss w = {
153 .lookup = __nss_shadow_nonlocal_lookup, .fct_name = "getspnam_r",
154 .status = &status, .errnop = errnop
155 };
156 const __typeof__(&_nss_nonlocal_getspnam_r) self = NULL;
157#define args (name, pwd, buffer, buflen, errnop)
158#include "walk_nss.h"
159#undef args
22562df0
AK
160 if (status != NSS_STATUS_SUCCESS)
161 return status;
162
163 if (strcmp(name, pwd->sp_namp) != 0) {
164 syslog(LOG_ERR, "nss_nonlocal: discarding shadow %s from lookup for shadow %s\n", pwd->sp_namp, name);
165 return NSS_STATUS_NOTFOUND;
166 }
167
168 return NSS_STATUS_SUCCESS;
f6903667 169}
This page took 0.076519 seconds and 5 git commands to generate.