7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina/*
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina Authors:
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina Pavel Březina <pbrezina@redhat.com>
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina Copyright (C) 2016 Red Hat
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina This program is free software; you can redistribute it and/or modify
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina it under the terms of the GNU General Public License as published by
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina the Free Software Foundation; either version 3 of the License, or
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina (at your option) any later version.
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina This program is distributed in the hope that it will be useful,
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina but WITHOUT ANY WARRANTY; without even the implied warranty of
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina GNU General Public License for more details.
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina You should have received a copy of the GNU General Public License
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina along with this program. If not, see <http://www.gnu.org/licenses/>.
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina*/
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina#include <talloc.h>
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina#include <ldb.h>
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina#include "db/sysdb.h"
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina#include "util/util.h"
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina#include "providers/data_provider.h"
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina#include "responder/common/cache_req/cache_req_plugin.h"
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina
2af80640f18966d65cf82106059ce3c060df93bfamitkumaerrno_t cache_req_idminmax_check(struct cache_req_data *data,
2af80640f18966d65cf82106059ce3c060df93bfamitkuma struct sss_domain_info *domain)
2af80640f18966d65cf82106059ce3c060df93bfamitkuma{
2af80640f18966d65cf82106059ce3c060df93bfamitkuma if (((domain->id_min != 0) && (data->id < domain->id_min)) ||
2af80640f18966d65cf82106059ce3c060df93bfamitkuma ((domain->id_max != 0) && (data->id > domain->id_max))) {
2af80640f18966d65cf82106059ce3c060df93bfamitkuma DEBUG(SSSDBG_FUNC_DATA, "id exceeds min/max boundaries\n");
2af80640f18966d65cf82106059ce3c060df93bfamitkuma return ERR_ID_OUTSIDE_RANGE;
2af80640f18966d65cf82106059ce3c060df93bfamitkuma }
2af80640f18966d65cf82106059ce3c060df93bfamitkuma return EOK;
2af80640f18966d65cf82106059ce3c060df93bfamitkuma}
2af80640f18966d65cf82106059ce3c060df93bfamitkuma
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březinastatic struct ldb_message *
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březinacache_req_well_known_sid_msg(TALLOC_CTX *mem_ctx,
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina const char *sid,
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina const char *name)
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina{
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina struct ldb_message *msg;
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina const char *dup_sid;
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina const char *dup_name;
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina int ldberr;
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina msg = ldb_msg_new(NULL);
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina if (msg == NULL) {
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina return NULL;
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina }
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina dup_sid = talloc_strdup(msg, sid);
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina if (dup_sid == NULL) {
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina ldberr = LDB_ERR_OTHER;
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina goto done;
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina }
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina dup_name = talloc_strdup(msg, name);
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina if (name == NULL) {
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina ldberr = LDB_ERR_OTHER;
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina goto done;
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina }
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina
0e238c259c066cf997aaa940d33d6bda96c15925Sumit Bose ldberr = ldb_msg_add_string(msg, SYSDB_OBJECTCATEGORY, SYSDB_GROUP_CLASS);
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina if (ldberr != LDB_SUCCESS) {
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina goto done;
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina }
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina ldberr = ldb_msg_add_string(msg, SYSDB_NAME, dup_name);
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina if (ldberr != LDB_SUCCESS) {
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina goto done;
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina }
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina ldberr = ldb_msg_add_string(msg, SYSDB_SID_STR, dup_sid);
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina if (ldberr != LDB_SUCCESS) {
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina goto done;
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina }
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březinadone:
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina if (ldberr != LDB_SUCCESS) {
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina talloc_free(msg);
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina return NULL;
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina }
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina return msg;
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina}
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březinastruct cache_req_result *
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březinacache_req_well_known_sid_result(TALLOC_CTX *mem_ctx,
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina struct cache_req *cr,
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina const char *domname,
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina const char *sid,
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina const char *name)
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina{
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina struct cache_req_result *result;
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina struct sss_domain_info *domain;
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina struct ldb_message *msg;
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina msg = cache_req_well_known_sid_msg(NULL, sid, name);
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina if (msg == NULL) {
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina return NULL;
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina }
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina if (domname != NULL) {
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina domain = find_domain_by_name(cr->rctx->domains, domname, true);
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina } else {
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina domain = NULL;
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina }
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina result = cache_req_create_result_from_msg(mem_ctx, domain, msg,
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina name, domname);
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina if (result == NULL) {
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina talloc_free(msg);
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina }
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina return result;
7a2ca8d776df685bddbb64370181fb32d776f676Pavel Březina}
4df7aec645f87342f3a5146062abcb15f71f4fd9Pavel Březina
4df7aec645f87342f3a5146062abcb15f71f4fd9Pavel Březinabool
4df7aec645f87342f3a5146062abcb15f71f4fd9Pavel Březinacache_req_common_dp_recv(struct tevent_req *subreq,
4df7aec645f87342f3a5146062abcb15f71f4fd9Pavel Březina struct cache_req *cr)
4df7aec645f87342f3a5146062abcb15f71f4fd9Pavel Březina{
4df7aec645f87342f3a5146062abcb15f71f4fd9Pavel Březina char *err_msg;
4df7aec645f87342f3a5146062abcb15f71f4fd9Pavel Březina dbus_uint16_t err_maj;
4df7aec645f87342f3a5146062abcb15f71f4fd9Pavel Březina dbus_uint32_t err_min;
4df7aec645f87342f3a5146062abcb15f71f4fd9Pavel Březina errno_t ret;
4df7aec645f87342f3a5146062abcb15f71f4fd9Pavel Březina bool bret;
4df7aec645f87342f3a5146062abcb15f71f4fd9Pavel Březina
4df7aec645f87342f3a5146062abcb15f71f4fd9Pavel Březina ret = sss_dp_req_recv(NULL, subreq, &err_maj, &err_min, &err_msg);
4df7aec645f87342f3a5146062abcb15f71f4fd9Pavel Březina if (ret != EOK) {
4df7aec645f87342f3a5146062abcb15f71f4fd9Pavel Březina CACHE_REQ_DEBUG(SSSDBG_OP_FAILURE, cr,
4df7aec645f87342f3a5146062abcb15f71f4fd9Pavel Březina "Could not get account info [%d]: %s\n",
4df7aec645f87342f3a5146062abcb15f71f4fd9Pavel Březina ret, sss_strerror(ret));
4df7aec645f87342f3a5146062abcb15f71f4fd9Pavel Březina CACHE_REQ_DEBUG(SSSDBG_TRACE_FUNC, cr,
4df7aec645f87342f3a5146062abcb15f71f4fd9Pavel Březina "Due to an error we will return cached data\n");
4df7aec645f87342f3a5146062abcb15f71f4fd9Pavel Březina
4df7aec645f87342f3a5146062abcb15f71f4fd9Pavel Březina bret = false;
4df7aec645f87342f3a5146062abcb15f71f4fd9Pavel Březina goto done;
4df7aec645f87342f3a5146062abcb15f71f4fd9Pavel Březina }
4df7aec645f87342f3a5146062abcb15f71f4fd9Pavel Březina
4df7aec645f87342f3a5146062abcb15f71f4fd9Pavel Březina if (err_maj) {
4df7aec645f87342f3a5146062abcb15f71f4fd9Pavel Březina CACHE_REQ_DEBUG(SSSDBG_OP_FAILURE, cr,
4df7aec645f87342f3a5146062abcb15f71f4fd9Pavel Březina "Data Provider Error: %u, %u, %s\n",
4df7aec645f87342f3a5146062abcb15f71f4fd9Pavel Březina (unsigned int)err_maj, (unsigned int)err_min, err_msg);
4df7aec645f87342f3a5146062abcb15f71f4fd9Pavel Březina CACHE_REQ_DEBUG(SSSDBG_TRACE_FUNC, cr,
4df7aec645f87342f3a5146062abcb15f71f4fd9Pavel Březina "Due to an error we will return cached data\n");
4df7aec645f87342f3a5146062abcb15f71f4fd9Pavel Březina
4df7aec645f87342f3a5146062abcb15f71f4fd9Pavel Březina bret = false;
4df7aec645f87342f3a5146062abcb15f71f4fd9Pavel Březina goto done;
4df7aec645f87342f3a5146062abcb15f71f4fd9Pavel Březina }
4df7aec645f87342f3a5146062abcb15f71f4fd9Pavel Březina
4df7aec645f87342f3a5146062abcb15f71f4fd9Pavel Březina bret = true;
4df7aec645f87342f3a5146062abcb15f71f4fd9Pavel Březina
4df7aec645f87342f3a5146062abcb15f71f4fd9Pavel Březinadone:
4df7aec645f87342f3a5146062abcb15f71f4fd9Pavel Březina talloc_free(err_msg);
4df7aec645f87342f3a5146062abcb15f71f4fd9Pavel Březina return bret;
4df7aec645f87342f3a5146062abcb15f71f4fd9Pavel Březina}
a6eb9c4c3ff68d134bc745e8374f182737e9696bJakub Hrozek
a6eb9c4c3ff68d134bc745e8374f182737e9696bJakub Hrozekerrno_t
aa8608253630d8f3679dba5771ed019051c96930Fabiano Fidênciocache_req_common_get_acct_domain_recv(TALLOC_CTX *mem_ctx,
a6eb9c4c3ff68d134bc745e8374f182737e9696bJakub Hrozek struct tevent_req *subreq,
a6eb9c4c3ff68d134bc745e8374f182737e9696bJakub Hrozek struct cache_req *cr,
a6eb9c4c3ff68d134bc745e8374f182737e9696bJakub Hrozek char **_domain)
a6eb9c4c3ff68d134bc745e8374f182737e9696bJakub Hrozek{
a6eb9c4c3ff68d134bc745e8374f182737e9696bJakub Hrozek errno_t ret;
a6eb9c4c3ff68d134bc745e8374f182737e9696bJakub Hrozek
a6eb9c4c3ff68d134bc745e8374f182737e9696bJakub Hrozek ret = sss_dp_get_account_domain_recv(mem_ctx, subreq, _domain);
a6eb9c4c3ff68d134bc745e8374f182737e9696bJakub Hrozek if (ret != EOK) {
a6eb9c4c3ff68d134bc745e8374f182737e9696bJakub Hrozek CACHE_REQ_DEBUG(SSSDBG_MINOR_FAILURE, cr,
a6eb9c4c3ff68d134bc745e8374f182737e9696bJakub Hrozek "Could not get account domain [%d]: %s\n",
a6eb9c4c3ff68d134bc745e8374f182737e9696bJakub Hrozek ret, sss_strerror(ret));
a6eb9c4c3ff68d134bc745e8374f182737e9696bJakub Hrozek }
a6eb9c4c3ff68d134bc745e8374f182737e9696bJakub Hrozek return ret;
a6eb9c4c3ff68d134bc745e8374f182737e9696bJakub Hrozek}