/* * Copyright 2013-2017 Red Hat, Inc. * * This Program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; version 2 of the License. * * This Program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this Program; if not, write to the * * Free Software Foundation, Inc. * 59 Temple Place, Suite 330 * Boston, MA 02111-1307 USA * */ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include #include #include #include #include #include #include #include #include #include "back-sch-nss.h" struct nss_ops_ctx { void *dl_handle; long int initgroups_start; enum nss_status (*getpwnam_r)(const char *name, struct passwd *result, char *buffer, size_t buflen, int *errnop); enum nss_status (*getpwuid_r)(uid_t uid, struct passwd *result, char *buffer, size_t buflen, int *errnop); enum nss_status (*getgrnam_r)(const char *name, struct group *result, char *buffer, size_t buflen, int *errnop); enum nss_status (*getgrgid_r)(gid_t gid, struct group *result, char *buffer, size_t buflen, int *errnop); enum nss_status (*initgroups_dyn)(const char *user, gid_t group, long int *start, long int *size, gid_t **groups, long int limit, int *errnop); }; void backend_nss_init_context(struct nss_ops_ctx **nss_context) { struct nss_ops_ctx *ctx = NULL; if (nss_context == NULL) { return; } ctx = calloc(1, sizeof(struct nss_ops_ctx)); *nss_context = ctx; if (ctx == NULL) { return; } ctx->dl_handle = dlopen("libnss_sss.so.2", RTLD_NOW); if (ctx->dl_handle == NULL) { goto fail; } ctx->getpwnam_r = dlsym(ctx->dl_handle, "_nss_sss_getpwnam_r"); if (ctx->getpwnam_r == NULL) { goto fail; } ctx->getpwuid_r = dlsym(ctx->dl_handle, "_nss_sss_getpwuid_r"); if (ctx->getpwuid_r == NULL) { goto fail; } ctx->getgrnam_r = dlsym(ctx->dl_handle, "_nss_sss_getgrnam_r"); if (ctx->getgrnam_r == NULL) { goto fail; } ctx->getgrgid_r = dlsym(ctx->dl_handle, "_nss_sss_getgrgid_r"); if (ctx->getgrgid_r == NULL) { goto fail; } ctx->initgroups_dyn = dlsym(ctx->dl_handle, "_nss_sss_initgroups_dyn"); if (ctx->initgroups_dyn == NULL) { goto fail; } return; fail: backend_nss_free_context(nss_context); return; } void backend_nss_free_context(struct nss_ops_ctx **nss_context) { if (nss_context == NULL) { return; } if ((*nss_context)->dl_handle != NULL) { dlclose((*nss_context)->dl_handle); } free((*nss_context)); *nss_context = NULL; } /* Following three functions cannot be implemented with nss_sss.so.2 * As result, we simply do nothing here */ void backend_nss_set_timeout(struct nss_ops_ctx **nss_context, unsigned int timeout) { /* no operation */ } void backend_nss_evict_user(struct nss_ops_ctx **nss_context, const char *name) { /* no operation */ } void backend_nss_evict_group(struct nss_ops_ctx **nss_context, const char *name) { /* no operation */ } enum nss_status backend_nss_getpwnam(struct nss_ops_ctx *nss_context, const char *name, struct passwd *pwd, char *buffer, size_t buflen, struct passwd **result, int *lerrno) { if (nss_context == NULL) { return NSS_STATUS_UNAVAIL; } return (enum nss_status) nss_context->getpwnam_r(name, pwd, buffer, buflen, result, lerrno); } enum nss_status backend_nss_getpwuid(struct nss_ops_ctx *nss_context, uid_t uid, struct passwd *pwd, char *buffer, size_t buflen, struct passwd **result, int *lerrno) { if (nss_context == NULL) { return NSS_STATUS_UNAVAIL; } return (enum nss_status) nss_context->getpwuid_r(uid, pwd, buffer, buflen, result, lerrno); } enum nss_status backend_nss_getgrnam(struct nss_ops_ctx *nss_context, const char *name, struct group *grp, char *buffer, size_t buflen, struct group **result, int *lerrno) { if (nss_context == NULL) { return NSS_STATUS_UNAVAIL; } return (enum nss_status) nss_context->getgrnam_r(name, grp, buffer, buflen, result, lerrno); } enum nss_status backend_nss_getgrgid(struct nss_ops_ctx *nss_context, gid_t gid, struct group *grp, char *buffer, size_t buflen, struct group **result, int *lerrno) { if (nss_context == NULL) { return NSS_STATUS_UNAVAIL; } return (enum nss_status) nss_context->getgrgid_r(gid, grp, buffer, buflen, result, lerrno); } enum nss_status backend_nss_getgrouplist(struct nss_ops_ctx *nss_context, const char *name, gid_t group, gid_t *groups, int *ngroups, int *lerrno) { enum nss_status ret = NSS_STATUS_UNAVAIL; if (nss_context == NULL) { return NSS_STATUS_UNAVAIL; } if (nss_context->initgroups_start == 0) { groups[0] = group; nss_context->initgroups_start++; } ret = nss_context->initgroups_dyn(name, group, &nss_context->initgroups_start, &ngroups, &groups, -1, &lerrno); if (ret == NSS_STATUS_SUCCESS) { nss_context->initgroups_start = 0; } return ret; }