[22686] | 1 | /* |
---|
| 2 | * nonlocal-shadow.c |
---|
| 3 | * shadow database for nss_nonlocal proxy. |
---|
| 4 | * |
---|
[24585] | 5 | * Copyright © 2007–2010 Anders Kaseorg <andersk@mit.edu> |
---|
[22686] | 6 | * |
---|
[24585] | 7 | * This file is part of nss_nonlocal. |
---|
[22686] | 8 | * |
---|
[24585] | 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. |
---|
[22686] | 13 | * |
---|
[24585] | 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 |
---|
[22686] | 23 | */ |
---|
| 24 | |
---|
| 25 | #define _GNU_SOURCE |
---|
[26035] | 26 | |
---|
[22686] | 27 | #include <sys/types.h> |
---|
[26035] | 28 | #include <dlfcn.h> |
---|
| 29 | #include <errno.h> |
---|
| 30 | #include <nss.h> |
---|
| 31 | #include <shadow.h> |
---|
| 32 | #include <stdbool.h> |
---|
| 33 | #include <stddef.h> |
---|
[22686] | 34 | #include <stdlib.h> |
---|
| 35 | #include <string.h> |
---|
[24122] | 36 | #include <syslog.h> |
---|
[22686] | 37 | |
---|
| 38 | #include "nsswitch-internal.h" |
---|
| 39 | #include "nonlocal.h" |
---|
| 40 | |
---|
| 41 | |
---|
[25083] | 42 | static service_user *__nss_shadow_nonlocal_database; |
---|
| 43 | |
---|
| 44 | static int |
---|
| 45 | internal_function |
---|
| 46 | __nss_shadow_nonlocal_lookup(service_user **ni, const char *fct_name, |
---|
| 47 | void **fctp) |
---|
[22686] | 48 | { |
---|
[25083] | 49 | if (__nss_shadow_nonlocal_database == NULL |
---|
| 50 | && __nss_database_lookup("shadow_nonlocal", NULL, NULL, |
---|
| 51 | &__nss_shadow_nonlocal_database) < 0) |
---|
| 52 | return -1; |
---|
[22686] | 53 | |
---|
[25083] | 54 | *ni = __nss_shadow_nonlocal_database; |
---|
| 55 | |
---|
| 56 | *fctp = __nss_lookup_function(*ni, fct_name); |
---|
| 57 | return 0; |
---|
[22686] | 58 | } |
---|
| 59 | |
---|
| 60 | |
---|
[26035] | 61 | static bool spent_initialized = false; |
---|
[25083] | 62 | static service_user *spent_startp, *spent_nip; |
---|
[22686] | 63 | static void *spent_fct_start; |
---|
| 64 | static union { |
---|
| 65 | enum nss_status (*l)(struct spwd *pwd, char *buffer, size_t buflen, |
---|
| 66 | int *errnop); |
---|
| 67 | void *ptr; |
---|
| 68 | } spent_fct; |
---|
| 69 | static const char *spent_fct_name = "getspent_r"; |
---|
| 70 | |
---|
| 71 | enum nss_status |
---|
| 72 | _nss_nonlocal_setspent(int stayopen) |
---|
| 73 | { |
---|
| 74 | enum nss_status status; |
---|
[25083] | 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 |
---|
[22686] | 83 | if (status != NSS_STATUS_SUCCESS) |
---|
| 84 | return status; |
---|
| 85 | |
---|
[26035] | 86 | if (!spent_initialized) { |
---|
[25083] | 87 | __nss_shadow_nonlocal_lookup(&spent_startp, spent_fct_name, |
---|
| 88 | &spent_fct_start); |
---|
[26035] | 89 | __sync_synchronize(); |
---|
| 90 | spent_initialized = true; |
---|
| 91 | } |
---|
[25083] | 92 | spent_nip = spent_startp; |
---|
[22686] | 93 | spent_fct.ptr = spent_fct_start; |
---|
| 94 | return NSS_STATUS_SUCCESS; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | enum nss_status |
---|
| 98 | _nss_nonlocal_endspent(void) |
---|
| 99 | { |
---|
| 100 | enum nss_status status; |
---|
[25083] | 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; |
---|
[22686] | 106 | |
---|
| 107 | spent_nip = NULL; |
---|
| 108 | |
---|
[25083] | 109 | #define args () |
---|
| 110 | #include "walk_nss.h" |
---|
| 111 | #undef args |
---|
[22686] | 112 | return status; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | enum nss_status |
---|
| 116 | _nss_nonlocal_getspent_r(struct spwd *pwd, char *buffer, size_t buflen, |
---|
| 117 | int *errnop) |
---|
| 118 | { |
---|
| 119 | enum nss_status status; |
---|
[26035] | 120 | |
---|
| 121 | char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV); |
---|
| 122 | if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0') |
---|
| 123 | return NSS_STATUS_UNAVAIL; |
---|
| 124 | |
---|
[22686] | 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 | |
---|
| 147 | enum 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; |
---|
[25083] | 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 |
---|
[24122] | 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; |
---|
[22686] | 169 | } |
---|