source: trunk/debathena/debathena/libnss-nonlocal/nonlocal-shadow.c @ 26035

Revision 26035, 4.4 KB checked in by andersk, 12 years ago (diff)
In libnss-nonlocal: * New upstream release. - Support Automake 1.12. - Guard one-time initialization with memory barriers. - Make initgroups_dyn succeed when adding only magic groups. * Rewrite packaging with Debhelper 7. * Move magic user and group creation to separate libnss-nonlocal-common package. Full history: git://andersk.mit.edu/nss_nonlocal.git debian/2.1-0debathena1
Line 
1/*
2 * nonlocal-shadow.c
3 * shadow database for nss_nonlocal proxy.
4 *
5 * Copyright © 2007–2010 Anders Kaseorg <andersk@mit.edu>
6 *
7 * This file is part of nss_nonlocal.
8 *
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.
13 *
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
23 */
24
25#define _GNU_SOURCE
26
27#include <sys/types.h>
28#include <dlfcn.h>
29#include <errno.h>
30#include <nss.h>
31#include <shadow.h>
32#include <stdbool.h>
33#include <stddef.h>
34#include <stdlib.h>
35#include <string.h>
36#include <syslog.h>
37
38#include "nsswitch-internal.h"
39#include "nonlocal.h"
40
41
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)
48{
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;
55
56    *fctp = __nss_lookup_function(*ni, fct_name);
57    return 0;
58}
59
60
61static bool spent_initialized = false;
62static service_user *spent_startp, *spent_nip;
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{
74    enum nss_status status;
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
83    if (status != NSS_STATUS_SUCCESS)
84        return status;
85
86    if (!spent_initialized) {
87        __nss_shadow_nonlocal_lookup(&spent_startp, spent_fct_name,
88                                     &spent_fct_start);
89        __sync_synchronize();
90        spent_initialized = true;
91    }
92    spent_nip = spent_startp;
93    spent_fct.ptr = spent_fct_start;
94    return NSS_STATUS_SUCCESS;
95}
96
97enum nss_status
98_nss_nonlocal_endspent(void)
99{
100    enum nss_status status;
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;
106
107    spent_nip = NULL;
108
109#define args ()
110#include "walk_nss.h"
111#undef args
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;
120
121    char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
122    if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
123        return NSS_STATUS_UNAVAIL;
124
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{
151    enum nss_status status;
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
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;
169}
Note: See TracBrowser for help on using the repository browser.