From d17fc280867f656da09f71dafae4b2491432ff8e Mon Sep 17 00:00:00 2001 From: Sumit Bose Date: Wed, 13 Oct 2010 21:52:26 +0200 Subject: [PATCH 2/2] Implement netgroups for proxy provider --- Makefile.am | 1 + src/providers/proxy/proxy.h | 7 ++ src/providers/proxy/proxy_id.c | 4 +- src/providers/proxy/proxy_netgroup.c | 134 ++++++++++++++++++++++++++++++++++ 4 files changed, 144 insertions(+), 2 deletions(-) create mode 100644 src/providers/proxy/proxy_netgroup.c diff --git a/Makefile.am b/Makefile.am index 65860b6..294b174 100644 --- a/Makefile.am +++ b/Makefile.am @@ -795,6 +795,7 @@ libsss_proxy_la_SOURCES = \ src/providers/proxy/proxy_common.c \ src/providers/proxy/proxy_init.c \ src/providers/proxy/proxy_id.c \ + src/providers/proxy/proxy_netgroup.c \ src/providers/proxy/proxy_auth.c libsss_proxy_la_CFLAGS = \ $(AM_CFLAGS) diff --git a/src/providers/proxy/proxy.h b/src/providers/proxy/proxy.h index e35e91c..9faa967 100644 --- a/src/providers/proxy/proxy.h +++ b/src/providers/proxy/proxy.h @@ -138,4 +138,11 @@ void proxy_get_account_info(struct be_req *breq); /* From proxy_auth.c */ void proxy_pam_handler(struct be_req *req); +/* From proxy_netgroup.c */ +errno_t get_netgroup(struct proxy_id_ctx *ctx, + struct sysdb_ctx *sysdb, + struct sss_domain_info *dom, + const char *name); + + #endif /* __PROXY_H__ */ diff --git a/src/providers/proxy/proxy_id.c b/src/providers/proxy/proxy_id.c index ead6ce0..ff3ddf8 100644 --- a/src/providers/proxy/proxy_id.c +++ b/src/providers/proxy/proxy_id.c @@ -1149,8 +1149,8 @@ void proxy_get_account_info(struct be_req *breq) ENODEV, "Netgroups are not supported"); } - return proxy_reply(breq, DP_ERR_FATAL, - ENOSYS, "Netgroups not implemented"); + ret = get_netgroup(ctx, sysdb, domain, ar->filter_value); + break; default: /*fail*/ return proxy_reply(breq, DP_ERR_FATAL, diff --git a/src/providers/proxy/proxy_netgroup.c b/src/providers/proxy/proxy_netgroup.c new file mode 100644 index 0000000..cdcb2a8 --- /dev/null +++ b/src/providers/proxy/proxy_netgroup.c @@ -0,0 +1,134 @@ +/* + SSSD + + Proxy netgroup handler + + Authors: + + Sumit Bose + + Copyright (C) 2010 Red Hat + + 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; either version 3 of the License, or + (at your option) any later version. + + 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, see . +*/ + +#include "providers/proxy/proxy.h" +#include "util/util.h" + +#define BUFLEN 1024 + +static errno_t make_netgroup_attr(struct __netgrent netgrent, + struct sysdb_attrs *attrs) +{ + int ret; + char *dummy; + + if (netgrent.type == group_val) { + ret =sysdb_attrs_add_string(attrs, SYSDB_NETGROUP_MEMBER, + netgrent.val.group); + if (ret != EOK) { + DEBUG(1, ("sysdb_attrs_add_string failed.\n")); + return ret; + } + } else if (netgrent.type == triple_val) { + dummy = talloc_asprintf(attrs, "(%s,%s,%s)", netgrent.val.triple.host, + netgrent.val.triple.user, + netgrent.val.triple.domain); + if (dummy == NULL) { + DEBUG(1, ("talloc_asprintf failed.\n")); + return ENOMEM; + } + + ret = sysdb_attrs_add_string(attrs, SYSDB_NETGROUP_TRIPLE, dummy); + if (ret != EOK) { + DEBUG(1, ("sysdb_attrs_add_string failed.\n")); + return ret; + } + } else { + DEBUG(1, ("Unknown netgrent entry type [%d].\n", netgrent.type)); + return EINVAL; + } + + return EOK; +} + +errno_t get_netgroup(struct proxy_id_ctx *ctx, + struct sysdb_ctx *sysdb, + struct sss_domain_info *dom, + const char *name) +{ + struct __netgrent result; + enum nss_status status; + char buffer[BUFLEN]; + int ret; + TALLOC_CTX *tmp_ctx; + struct sysdb_attrs *attrs; + + memset(&result, 0 ,sizeof(result)); + status = ctx->ops.setnetgrent(name, &result); + if (status != NSS_STATUS_SUCCESS) { + DEBUG(5, ("setnetgrent failed for netgroup [%s].\n", name)); + return EIO; + } + + tmp_ctx = talloc_new(NULL); + if (tmp_ctx == NULL) { + DEBUG(1, ("talloc_new failed.\n")); + return ENOMEM; + } + + attrs = sysdb_new_attrs(tmp_ctx); + if (attrs == NULL) { + DEBUG(1, ("sysdb_new_attrs failed.\n")); + return ENOMEM; + } + + do { + status = ctx->ops.getnetgrent_r(&result, buffer, BUFLEN, &ret); + if (status != NSS_STATUS_SUCCESS && status != NSS_STATUS_RETURN) { + DEBUG(1, ("getnetgrent_r failed for netgroup [%s]: [%d][%s].\n", + name, ret, strerror(ret))); + goto done; + } + + if (status == NSS_STATUS_SUCCESS) { + ret = make_netgroup_attr(result, attrs); + if (ret != EOK) { + DEBUG(1, ("make_netgroup_attr failed.\n")); + goto done; + } + } + } while (status != NSS_STATUS_RETURN); + + status = ctx->ops.endnetgrent(&result); + if (status != NSS_STATUS_SUCCESS) { + DEBUG(1, ("endnetgrent failed.\n")); + ret = EIO; + goto done; + } + + ret = sysdb_add_netgroup(sysdb, dom, name, NULL, attrs, + ctx->entry_cache_timeout); + if (ret != EOK) { + DEBUG(1, ("sysdb_add_netgroup failed.\n")); + goto done; + } + + ret = EOK; + +done: + talloc_free(tmp_ctx); + + return ret; +} -- 1.7.2.3