source: trunk/server/common/oursrc/nss_nonlocal/nonlocal-shadow.c @ 2717

Last change on this file since 2717 was 2432, checked in by andersk, 11 years ago
Update nss_nonlocal to 2.1 - Support Automake 1.12. - Guard one-time initialization with memory barriers. - Make initgroups_dyn succeed when adding only magic groups.
File size: 4.4 KB
RevLine 
[750]1/*
2 * nonlocal-shadow.c
3 * shadow database for nss_nonlocal proxy.
4 *
[1553]5 * Copyright © 2007–2010 Anders Kaseorg <andersk@mit.edu>
[750]6 *
[1553]7 * This file is part of nss_nonlocal.
[750]8 *
[1553]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.
[750]13 *
[1553]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
[750]23 */
24
25#define _GNU_SOURCE
[2432]26
[750]27#include <sys/types.h>
[2432]28#include <dlfcn.h>
29#include <errno.h>
30#include <nss.h>
31#include <shadow.h>
32#include <stdbool.h>
33#include <stddef.h>
[750]34#include <stdlib.h>
35#include <string.h>
[1553]36#include <syslog.h>
[750]37
38#include "nsswitch-internal.h"
39#include "nonlocal.h"
40
41
[1825]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)
[750]48{
[1825]49    if (__nss_shadow_nonlocal_database == NULL
50        && __nss_database_lookup("shadow_nonlocal", NULL, NULL,
51                                 &__nss_shadow_nonlocal_database) < 0)
52        return -1;
[750]53
[1825]54    *ni = __nss_shadow_nonlocal_database;
55
56    *fctp = __nss_lookup_function(*ni, fct_name);
57    return 0;
[750]58}
59
60
[2432]61static bool spent_initialized = false;
[1825]62static service_user *spent_startp, *spent_nip;
[750]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;
[1825]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
[750]83    if (status != NSS_STATUS_SUCCESS)
84        return status;
85
[2432]86    if (!spent_initialized) {
[1825]87        __nss_shadow_nonlocal_lookup(&spent_startp, spent_fct_name,
88                                     &spent_fct_start);
[2432]89        __sync_synchronize();
90        spent_initialized = true;
91    }
[1825]92    spent_nip = spent_startp;
[750]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;
[1825]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;
[750]106
107    spent_nip = NULL;
108
[1825]109#define args ()
110#include "walk_nss.h"
111#undef args
[750]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;
[2432]120
121    char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV);
122    if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0')
123        return NSS_STATUS_UNAVAIL;
124
[750]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;
[1825]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
[1553]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;
[750]169}
Note: See TracBrowser for help on using the repository browser.