4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder/*
e9458b1a7a19a63aa4c179f9ab20f4d50681c168Jens Elkner Authors:
81d182b21020b815887e9057959228546cf61b6bChristian Maeder Pavel Březina <pbrezina@redhat.com>
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder
98890889ffb2e8f6f722b00e265a211f13b5a861Corneliu-Claudiu Prodescu Copyright (C) 2016 Red Hat
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder
3f69b6948966979163bdfe8331c38833d5d90ecdChristian Maeder This program is free software; you can redistribute it and/or modify
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder it under the terms of the GNU General Public License as published by
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder the Free Software Foundation; either version 3 of the License, or
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder (at your option) any later version.
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder This program is distributed in the hope that it will be useful,
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder but WITHOUT ANY WARRANTY; without even the implied warranty of
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder GNU General Public License for more details.
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder You should have received a copy of the GNU General Public License
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder along with this program. If not, see <http://www.gnu.org/licenses/>.
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder*/
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder#include <talloc.h>
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder#include "responder/common/cache_req/cache_req_private.h"
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maederstatic struct cache_req_data *
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maedercache_req_data_create(TALLOC_CTX *mem_ctx,
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder enum cache_req_type type,
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder struct cache_req_data *input)
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder{
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder struct cache_req_data *data;
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder errno_t ret;
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder data = talloc_zero(mem_ctx, struct cache_req_data);
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder if (data == NULL) {
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder DEBUG(SSSDBG_CRIT_FAILURE, "talloc_zero() failed\n");
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder return NULL;
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder }
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder data->type = type;
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder switch (type) {
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder case CACHE_REQ_USER_BY_NAME:
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder case CACHE_REQ_USER_BY_UPN:
72b9099aeec0762bae4546db3bc4b48721027bf4Christian Maeder case CACHE_REQ_GROUP_BY_NAME:
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder case CACHE_REQ_USER_BY_FILTER:
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder case CACHE_REQ_GROUP_BY_FILTER:
72b9099aeec0762bae4546db3bc4b48721027bf4Christian Maeder case CACHE_REQ_INITGROUPS:
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder case CACHE_REQ_INITGROUPS_BY_UPN:
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder if (input->name.input == NULL) {
72b9099aeec0762bae4546db3bc4b48721027bf4Christian Maeder DEBUG(SSSDBG_CRIT_FAILURE, "Bug: name cannot be NULL!\n");
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder ret = ERR_INTERNAL;
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder goto done;
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder }
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder data->name.input = talloc_strdup(data, input->name.input);
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder if (data->name.input == NULL) {
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder ret = ENOMEM;
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder goto done;
1600a2e47d5ed599df94d20411f0767fb6d68587Christian Maeder }
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder break;
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder case CACHE_REQ_USER_BY_CERT:
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder if (input->cert == NULL) {
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder DEBUG(SSSDBG_CRIT_FAILURE, "Bug: certificate cannot be NULL!\n");
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder ret = ERR_INTERNAL;
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder goto done;
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder }
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder data->cert = talloc_strdup(data, input->cert);
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder if (data->cert == NULL) {
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder ret = ENOMEM;
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder goto done;
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder }
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder break;
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder case CACHE_REQ_USER_BY_ID:
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder case CACHE_REQ_GROUP_BY_ID:
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder if (input->id == 0) {
1600a2e47d5ed599df94d20411f0767fb6d68587Christian Maeder DEBUG(SSSDBG_CRIT_FAILURE, "Bug: id cannot be 0!\n");
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder ret = ERR_INTERNAL;
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder goto done;
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder }
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder data->id = input->id;
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder break;
84c37bab085e7818efde0a4db6033afa28d4ef80Christian Maeder case CACHE_REQ_OBJECT_BY_SID:
84c37bab085e7818efde0a4db6033afa28d4ef80Christian Maeder if (input->sid == NULL) {
84c37bab085e7818efde0a4db6033afa28d4ef80Christian Maeder DEBUG(SSSDBG_CRIT_FAILURE, "Bug: SID cannot be NULL!\n");
84c37bab085e7818efde0a4db6033afa28d4ef80Christian Maeder ret = ERR_INTERNAL;
84c37bab085e7818efde0a4db6033afa28d4ef80Christian Maeder goto done;
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder }
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder data->sid = talloc_strdup(data, input->sid);
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder if (data->sid == NULL) {
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder ret = ENOMEM;
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder goto done;
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder }
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder break;
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder case CACHE_REQ_SENTINEL:
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder DEBUG(SSSDBG_CRIT_FAILURE, "Invalid cache request type!\n");
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder ret = ERR_INTERNAL;
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder goto done;
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder }
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder if (input->attrs != NULL) {
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder data->attrs = dup_string_list(data, input->attrs);
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder if (data->attrs == NULL) {
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder ret = ENOMEM;
6b1153c560b677f9f5da2a60ee8a10de75ff90c5Christian Maeder goto done;
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder }
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder }
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder ret = EOK;
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maederdone:
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder if (ret != EOK) {
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder talloc_zfree(data);
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder DEBUG(SSSDBG_CRIT_FAILURE, "Unable to create cache_req data "
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder "[%d]: %s\n", ret, sss_strerror(ret));
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder return NULL;
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder }
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder return data;
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder}
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maederstruct cache_req_data *
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maedercache_req_data_name(TALLOC_CTX *mem_ctx,
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder enum cache_req_type type,
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder const char *name)
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder{
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder struct cache_req_data input = {0};
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder input.name.input = name;
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder return cache_req_data_create(mem_ctx, type, &input);
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder}
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maederstruct cache_req_data *
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maedercache_req_data_id(TALLOC_CTX *mem_ctx,
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder enum cache_req_type type,
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder uint32_t id)
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder{
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder struct cache_req_data input = {0};
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder input.id = id;
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder
3d3889e0cefcdce9b3f43c53aaa201943ac2e895Jonathan von Schroeder return cache_req_data_create(mem_ctx, type, &input);
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder}
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maederstruct cache_req_data *
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maedercache_req_data_cert(TALLOC_CTX *mem_ctx,
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder enum cache_req_type type,
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder const char *cert)
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder{
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder struct cache_req_data input = {0};
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder input.cert = cert;
4550ee0c945684d86f2f129046f5abba97ecd4c4Christian Maeder
return cache_req_data_create(mem_ctx, type, &input);
}
struct cache_req_data *
cache_req_data_sid(TALLOC_CTX *mem_ctx,
enum cache_req_type type,
const char *sid,
const char **attrs)
{
struct cache_req_data input = {0};
input.sid = sid;
input.attrs = attrs;
return cache_req_data_create(mem_ctx, type, &input);
}