[22686] | 1 | /* |
---|
| 2 | * nonlocal-passwd.c |
---|
| 3 | * passwd database for nss_nonlocal proxy. |
---|
| 4 | * |
---|
[24585] | 5 | * Copyright © 2007–2010 Anders Kaseorg <andersk@mit.edu> and Tim |
---|
| 6 | * Abbott <tabbott@mit.edu> |
---|
[22686] | 7 | * |
---|
[24585] | 8 | * This file is part of nss_nonlocal. |
---|
[22686] | 9 | * |
---|
[24585] | 10 | * nss_nonlocal is free software; you can redistribute it and/or |
---|
| 11 | * modify it under the terms of the GNU Lesser General Public License |
---|
| 12 | * as published by the Free Software Foundation; either version 2.1 of |
---|
| 13 | * the License, or (at your option) any later version. |
---|
[22686] | 14 | * |
---|
[24585] | 15 | * nss_nonlocal is distributed in the hope that it will be useful, but |
---|
| 16 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 18 | * Lesser General Public License for more details. |
---|
| 19 | * |
---|
| 20 | * You should have received a copy of the GNU Lesser General Public |
---|
| 21 | * License along with nss_nonlocal; if not, write to the Free Software |
---|
| 22 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
---|
| 23 | * 02110-1301 USA |
---|
[22686] | 24 | */ |
---|
| 25 | |
---|
| 26 | |
---|
| 27 | #define _GNU_SOURCE |
---|
| 28 | #include <sys/types.h> |
---|
| 29 | #include <unistd.h> |
---|
| 30 | #include <stdlib.h> |
---|
| 31 | #include <stdint.h> |
---|
| 32 | #include <string.h> |
---|
| 33 | #include <dlfcn.h> |
---|
| 34 | #include <stdio.h> |
---|
| 35 | #include <syslog.h> |
---|
| 36 | #include <errno.h> |
---|
| 37 | #include <pwd.h> |
---|
| 38 | #include <grp.h> |
---|
| 39 | #include <nss.h> |
---|
| 40 | #include "nsswitch-internal.h" |
---|
| 41 | #include "nonlocal.h" |
---|
| 42 | |
---|
| 43 | |
---|
[23113] | 44 | enum nss_status |
---|
| 45 | _nss_nonlocal_getpwuid_r(uid_t uid, struct passwd *pwd, |
---|
| 46 | char *buffer, size_t buflen, int *errnop); |
---|
| 47 | enum nss_status |
---|
| 48 | _nss_nonlocal_getpwnam_r(const char *name, struct passwd *pwd, |
---|
| 49 | char *buffer, size_t buflen, int *errnop); |
---|
[22686] | 50 | |
---|
[23113] | 51 | |
---|
[25083] | 52 | static service_user *__nss_passwd_nonlocal_database; |
---|
| 53 | |
---|
| 54 | static int |
---|
| 55 | internal_function |
---|
| 56 | __nss_passwd_nonlocal_lookup(service_user **ni, const char *fct_name, |
---|
| 57 | void **fctp) |
---|
[22686] | 58 | { |
---|
[25083] | 59 | if (__nss_passwd_nonlocal_database == NULL |
---|
| 60 | && __nss_database_lookup("passwd_nonlocal", NULL, NULL, |
---|
| 61 | &__nss_passwd_nonlocal_database) < 0) |
---|
| 62 | return -1; |
---|
[22686] | 63 | |
---|
[25083] | 64 | *ni = __nss_passwd_nonlocal_database; |
---|
| 65 | |
---|
| 66 | *fctp = __nss_lookup_function(*ni, fct_name); |
---|
| 67 | return 0; |
---|
[22686] | 68 | } |
---|
| 69 | |
---|
| 70 | |
---|
| 71 | enum nss_status |
---|
| 72 | check_nonlocal_uid(const char *user, uid_t uid, int *errnop) |
---|
| 73 | { |
---|
[23113] | 74 | enum nss_status status; |
---|
[22686] | 75 | struct passwd pwbuf; |
---|
[25083] | 76 | char *buf; |
---|
[24122] | 77 | size_t buflen = sysconf(_SC_GETPW_R_SIZE_MAX); |
---|
[25083] | 78 | const struct walk_nss w = { |
---|
| 79 | .lookup = &__nss_passwd_lookup, .fct_name = "getpwuid_r", |
---|
| 80 | .status = &status, .errnop = errnop, .buf = &buf, .buflen = &buflen |
---|
| 81 | }; |
---|
| 82 | const __typeof__(&_nss_nonlocal_getpwuid_r) self = &_nss_nonlocal_getpwuid_r; |
---|
| 83 | #define args (uid, &pwbuf, buf, buflen, errnop) |
---|
| 84 | #include "walk_nss.h" |
---|
| 85 | #undef args |
---|
[23113] | 86 | |
---|
| 87 | if (status == NSS_STATUS_SUCCESS) { |
---|
[22686] | 88 | syslog(LOG_ERR, "nss_nonlocal: possible spoofing attack: non-local user %s has same UID as local user %s!\n", user, pwbuf.pw_name); |
---|
[25083] | 89 | free(buf); |
---|
[22686] | 90 | status = NSS_STATUS_NOTFOUND; |
---|
[23113] | 91 | } else if (status != NSS_STATUS_TRYAGAIN) { |
---|
| 92 | status = NSS_STATUS_SUCCESS; |
---|
[22686] | 93 | } |
---|
[23113] | 94 | |
---|
[22686] | 95 | return status; |
---|
| 96 | } |
---|
| 97 | |
---|
[22714] | 98 | enum nss_status |
---|
[24122] | 99 | check_nonlocal_passwd(const char *user, struct passwd *pwd, int *errnop) |
---|
| 100 | { |
---|
| 101 | enum nss_status status = NSS_STATUS_SUCCESS; |
---|
| 102 | int old_errno = errno; |
---|
| 103 | char *end; |
---|
| 104 | unsigned long uid; |
---|
| 105 | |
---|
| 106 | errno = 0; |
---|
| 107 | uid = strtoul(pwd->pw_name, &end, 10); |
---|
[25083] | 108 | if (errno == 0 && *end == '\0' && (uid_t)uid == uid) { |
---|
| 109 | errno = old_errno; |
---|
[24122] | 110 | status = check_nonlocal_uid(user, uid, errnop); |
---|
[25083] | 111 | } else { |
---|
| 112 | errno = old_errno; |
---|
| 113 | } |
---|
[24122] | 114 | if (status != NSS_STATUS_SUCCESS) |
---|
| 115 | return status; |
---|
| 116 | |
---|
| 117 | return check_nonlocal_uid(user, pwd->pw_uid, errnop); |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | enum nss_status |
---|
[22714] | 121 | check_nonlocal_user(const char *user, int *errnop) |
---|
| 122 | { |
---|
[23113] | 123 | enum nss_status status; |
---|
[22714] | 124 | struct passwd pwbuf; |
---|
[25083] | 125 | char *buf; |
---|
[24122] | 126 | size_t buflen = sysconf(_SC_GETPW_R_SIZE_MAX); |
---|
[25083] | 127 | const struct walk_nss w = { |
---|
| 128 | .lookup = __nss_passwd_lookup, .fct_name = "getpwnam_r", |
---|
| 129 | .status = &status, .errnop = errnop, .buf = &buf, .buflen = &buflen |
---|
| 130 | }; |
---|
| 131 | const __typeof__(&_nss_nonlocal_getpwnam_r) self = &_nss_nonlocal_getpwnam_r; |
---|
| 132 | #define args (user, &pwbuf, buf, buflen, errnop) |
---|
| 133 | #include "walk_nss.h" |
---|
| 134 | #undef args |
---|
[23113] | 135 | |
---|
[25083] | 136 | if (status == NSS_STATUS_SUCCESS) { |
---|
[23113] | 137 | free(buf); |
---|
[22714] | 138 | status = NSS_STATUS_NOTFOUND; |
---|
[25083] | 139 | } else if (status != NSS_STATUS_TRYAGAIN) { |
---|
[23113] | 140 | status = NSS_STATUS_SUCCESS; |
---|
[25083] | 141 | } |
---|
[23113] | 142 | |
---|
[22714] | 143 | return status; |
---|
| 144 | } |
---|
[22686] | 145 | |
---|
[25083] | 146 | enum nss_status |
---|
| 147 | get_nonlocal_passwd(const char *name, struct passwd *pwd, char **buffer, |
---|
| 148 | int *errnop) |
---|
| 149 | { |
---|
| 150 | enum nss_status status; |
---|
| 151 | size_t buflen = sysconf(_SC_GETPW_R_SIZE_MAX); |
---|
| 152 | const struct walk_nss w = { |
---|
| 153 | .lookup = __nss_passwd_nonlocal_lookup, .fct_name = "getpwnam_r", |
---|
| 154 | .status = &status, .errnop = errnop, .buf = buffer, .buflen = &buflen |
---|
| 155 | }; |
---|
| 156 | const __typeof__(&_nss_nonlocal_getpwnam_r) self = NULL; |
---|
| 157 | #define args (name, pwd, *buffer, buflen, errnop) |
---|
| 158 | #include "walk_nss.h" |
---|
| 159 | #undef args |
---|
| 160 | return status; |
---|
| 161 | } |
---|
[22755] | 162 | |
---|
[25083] | 163 | |
---|
| 164 | static service_user *pwent_startp, *pwent_nip; |
---|
[22686] | 165 | static void *pwent_fct_start; |
---|
| 166 | static union { |
---|
| 167 | enum nss_status (*l)(struct passwd *pwd, char *buffer, size_t buflen, |
---|
| 168 | int *errnop); |
---|
| 169 | void *ptr; |
---|
| 170 | } pwent_fct; |
---|
| 171 | static const char *pwent_fct_name = "getpwent_r"; |
---|
| 172 | |
---|
| 173 | enum nss_status |
---|
| 174 | _nss_nonlocal_setpwent(int stayopen) |
---|
| 175 | { |
---|
| 176 | enum nss_status status; |
---|
[25083] | 177 | const struct walk_nss w = { |
---|
| 178 | .lookup = &__nss_passwd_nonlocal_lookup, .fct_name = "setpwent", |
---|
| 179 | .status = &status |
---|
| 180 | }; |
---|
| 181 | const __typeof__(&_nss_nonlocal_setpwent) self = NULL; |
---|
| 182 | #define args (stayopen) |
---|
| 183 | #include "walk_nss.h" |
---|
| 184 | #undef args |
---|
[22686] | 185 | if (status != NSS_STATUS_SUCCESS) |
---|
| 186 | return status; |
---|
| 187 | |
---|
| 188 | if (pwent_fct_start == NULL) |
---|
[25083] | 189 | __nss_passwd_nonlocal_lookup(&pwent_startp, pwent_fct_name, |
---|
| 190 | &pwent_fct_start); |
---|
| 191 | pwent_nip = pwent_startp; |
---|
[22686] | 192 | pwent_fct.ptr = pwent_fct_start; |
---|
| 193 | return NSS_STATUS_SUCCESS; |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | enum nss_status |
---|
| 197 | _nss_nonlocal_endpwent(void) |
---|
| 198 | { |
---|
| 199 | enum nss_status status; |
---|
[25083] | 200 | const struct walk_nss w = { |
---|
| 201 | .lookup = &__nss_passwd_nonlocal_lookup, .fct_name = "endpwent", |
---|
| 202 | .status = &status |
---|
| 203 | }; |
---|
| 204 | const __typeof__(&_nss_nonlocal_endpwent) self = NULL; |
---|
[22686] | 205 | |
---|
| 206 | pwent_nip = NULL; |
---|
| 207 | |
---|
[25083] | 208 | #define args () |
---|
| 209 | #include "walk_nss.h" |
---|
| 210 | #undef args |
---|
[22686] | 211 | return status; |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | enum nss_status |
---|
| 215 | _nss_nonlocal_getpwent_r(struct passwd *pwd, char *buffer, size_t buflen, |
---|
| 216 | int *errnop) |
---|
| 217 | { |
---|
| 218 | enum nss_status status; |
---|
[22768] | 219 | |
---|
| 220 | char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV); |
---|
[23113] | 221 | if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0') |
---|
[22768] | 222 | return NSS_STATUS_UNAVAIL; |
---|
| 223 | |
---|
[22686] | 224 | if (pwent_nip == NULL) { |
---|
| 225 | status = _nss_nonlocal_setpwent(0); |
---|
| 226 | if (status != NSS_STATUS_SUCCESS) |
---|
| 227 | return status; |
---|
| 228 | } |
---|
| 229 | do { |
---|
| 230 | if (pwent_fct.ptr == NULL) |
---|
| 231 | status = NSS_STATUS_UNAVAIL; |
---|
| 232 | else { |
---|
| 233 | int nonlocal_errno; |
---|
| 234 | do |
---|
[22768] | 235 | status = DL_CALL_FCT(pwent_fct.l, (pwd, buffer, buflen, errnop)); |
---|
[22686] | 236 | while (status == NSS_STATUS_SUCCESS && |
---|
[24122] | 237 | check_nonlocal_passwd(pwd->pw_name, pwd, &nonlocal_errno) != NSS_STATUS_SUCCESS); |
---|
[22686] | 238 | } |
---|
| 239 | if (status == NSS_STATUS_TRYAGAIN && *errnop == ERANGE) |
---|
| 240 | return status; |
---|
| 241 | |
---|
| 242 | if (status == NSS_STATUS_SUCCESS) |
---|
| 243 | return NSS_STATUS_SUCCESS; |
---|
| 244 | } while (__nss_next(&pwent_nip, pwent_fct_name, &pwent_fct.ptr, status, 0) == 0); |
---|
| 245 | |
---|
| 246 | pwent_nip = NULL; |
---|
| 247 | return NSS_STATUS_NOTFOUND; |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | |
---|
| 251 | enum nss_status |
---|
| 252 | _nss_nonlocal_getpwnam_r(const char *name, struct passwd *pwd, |
---|
| 253 | char *buffer, size_t buflen, int *errnop) |
---|
| 254 | { |
---|
| 255 | enum nss_status status; |
---|
| 256 | int group_errno; |
---|
[25083] | 257 | const struct walk_nss w = { |
---|
| 258 | .lookup = __nss_passwd_nonlocal_lookup, .fct_name = "getpwnam_r", |
---|
| 259 | .status = &status, .errnop = errnop |
---|
| 260 | }; |
---|
| 261 | const __typeof__(&_nss_nonlocal_getpwnam_r) self = NULL; |
---|
[22686] | 262 | |
---|
[22768] | 263 | char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV); |
---|
[23113] | 264 | if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0') |
---|
[22714] | 265 | return NSS_STATUS_UNAVAIL; |
---|
| 266 | |
---|
[25083] | 267 | #define args (name, pwd, buffer, buflen, errnop) |
---|
| 268 | #include "walk_nss.h" |
---|
| 269 | #undef args |
---|
[22686] | 270 | if (status != NSS_STATUS_SUCCESS) |
---|
| 271 | return status; |
---|
| 272 | |
---|
[24122] | 273 | if (strcmp(name, pwd->pw_name) != 0) { |
---|
| 274 | syslog(LOG_ERR, "nss_nonlocal: discarding user %s from lookup for user %s\n", pwd->pw_name, name); |
---|
| 275 | return NSS_STATUS_NOTFOUND; |
---|
| 276 | } |
---|
| 277 | |
---|
| 278 | status = check_nonlocal_passwd(name, pwd, errnop); |
---|
[22686] | 279 | if (status != NSS_STATUS_SUCCESS) |
---|
| 280 | return status; |
---|
| 281 | |
---|
[25083] | 282 | if (check_nonlocal_gid(name, NULL, pwd->pw_gid, &group_errno) != |
---|
[22686] | 283 | NSS_STATUS_SUCCESS) |
---|
| 284 | pwd->pw_gid = 65534 /* nogroup */; |
---|
| 285 | return NSS_STATUS_SUCCESS; |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | enum nss_status |
---|
| 289 | _nss_nonlocal_getpwuid_r(uid_t uid, struct passwd *pwd, |
---|
| 290 | char *buffer, size_t buflen, int *errnop) |
---|
| 291 | { |
---|
| 292 | enum nss_status status; |
---|
| 293 | int group_errno; |
---|
[25083] | 294 | const struct walk_nss w = { |
---|
| 295 | .lookup = &__nss_passwd_nonlocal_lookup, .fct_name = "getpwuid_r", |
---|
| 296 | .status = &status, .errnop = errnop |
---|
| 297 | }; |
---|
| 298 | const __typeof__(&_nss_nonlocal_getpwuid_r) self = NULL; |
---|
[22686] | 299 | |
---|
[22768] | 300 | char *nonlocal_ignore = getenv(NONLOCAL_IGNORE_ENV); |
---|
[23113] | 301 | if (nonlocal_ignore != NULL && nonlocal_ignore[0] != '\0') |
---|
[22686] | 302 | return NSS_STATUS_UNAVAIL; |
---|
| 303 | |
---|
[25083] | 304 | #define args (uid, pwd, buffer, buflen, errnop) |
---|
| 305 | #include "walk_nss.h" |
---|
| 306 | #undef args |
---|
[22686] | 307 | if (status != NSS_STATUS_SUCCESS) |
---|
| 308 | return status; |
---|
| 309 | |
---|
[24585] | 310 | if (uid != pwd->pw_uid) { |
---|
| 311 | syslog(LOG_ERR, "nss_nonlocal: discarding uid %d from lookup for uid %d\n", pwd->pw_uid, uid); |
---|
| 312 | return NSS_STATUS_NOTFOUND; |
---|
| 313 | } |
---|
| 314 | |
---|
[24122] | 315 | status = check_nonlocal_passwd(pwd->pw_name, pwd, errnop); |
---|
[22686] | 316 | if (status != NSS_STATUS_SUCCESS) |
---|
| 317 | return status; |
---|
| 318 | |
---|
[25083] | 319 | if (check_nonlocal_gid(pwd->pw_name, NULL, pwd->pw_gid, &group_errno) != |
---|
[22686] | 320 | NSS_STATUS_SUCCESS) |
---|
| 321 | pwd->pw_gid = 65534 /* nogroup */; |
---|
| 322 | return NSS_STATUS_SUCCESS; |
---|
| 323 | } |
---|