providers.c revision e625eb47a3091d92eda2271b123f8aab06227b63
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek Secrets Responder
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek Copyright (C) Simo Sorce <ssorce@redhat.com> 2016
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek This program is free software; you can redistribute it and/or modify
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek it under the terms of the GNU General Public License as published by
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek the Free Software Foundation; either version 3 of the License, or
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek (at your option) any later version.
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek This program is distributed in the hope that it will be useful,
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek but WITHOUT ANY WARRANTY; without even the implied warranty of
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek GNU General Public License for more details.
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek You should have received a copy of the GNU General Public License
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek along with this program. If not, see <http://www.gnu.org/licenses/>.
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidekint sec_map_url_to_user_path(struct sec_req_ctx *secreq, char **mapped_path)
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek /* change path to be user specific */
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek talloc_asprintf(secreq, SEC_BASEPATH"users/%"SPRIuid"/%s",
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek &secreq->parsed_url.path[sizeof(SEC_BASEPATH) - 1]);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek "Failed to map request to user specific url\n");
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidekint sec_req_routing(TALLOC_CTX *mem_ctx, struct sec_req_ctx *secreq,
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek sctx = talloc_get_type(secreq->cctx->rctx->pvt_ctx, struct sec_ctx);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek /* patch must start with /secrets/ for now */
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek "Path [%s] does not start with "SEC_BASEPATH"\n",
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek ret = sec_map_url_to_user_path(secreq, &secreq->mapped_path);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek /* source default provider */
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek ret = confdb_get_string(secreq->cctx->rctx->cdb, mem_ctx,
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek ret = confdb_get_sub_sections(mem_ctx, secreq->cctx->rctx->cdb,
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek // TODO order by length ?
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov for (int i = 0; i < num_sections; i++) {
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek secreq->base_path = talloc_asprintf(secreq, SEC_BASEPATH"%s/", sections[i]);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek if (strncmp(secreq->base_path, secreq->mapped_path, slen) == 0) {
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek secname = talloc_asprintf(mem_ctx, CONFDB_SEC_CONF_ENTRY"/%s",
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek ret = confdb_get_string(secreq->cctx->rctx->cdb, mem_ctx,
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek secreq->cfg_section = talloc_steal(secreq, secname);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek if (!secreq->base_path) secreq->base_path = SEC_BASEPATH;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek ret = sec_get_provider(sctx, provider, handle);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek ret = local_secrets_provider_handle(sctx, handle);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek } else if (strcasecmp(provider, "PROXY") == 0) {
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek ret = proxy_secrets_provider_handle(sctx, handle);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek const char *text;
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek "The request format is invalid." },
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek "Access to the requested resource requires authentication." },
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek "Access to the requested resource is forbidden." },
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek "The requested resource was not found." },
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek "Request method not allowed for this resource." },
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek "The request cannot be accepted." },
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek "The requested resource already exists." },
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek "The server encountered an internal error." },
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidekint sec_http_status_reply(TALLOC_CTX *mem_ctx, struct sec_data *reply,
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek "<html>\r\n"
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek "<head>\r\n<title>%d %s</title></head>\r\n"
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek "<body>\r\n"
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek "<h1>%s</h1>\r\n"
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek "<p>%s</p>\r\n"
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek sec_http_status_format_table[code].description);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek "HTTP/1.1 %d %s\r\n"
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek "Content-Length: %u\r\n"
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek "Content-Type: text/html\r\n"
19d8bc19b5b5597427878645644fa354ef6cb54dMichal Zidekint sec_http_reply_with_body(TALLOC_CTX *mem_ctx, struct sec_data *reply,
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek "HTTP/1.1 %d %s\r\n"
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek "Content-Type: %s\r\n"
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek "Content-Length: %lu\r\n"
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek reply->data = talloc_realloc(mem_ctx, reply->data, char,
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek memcpy(&reply->data[head_size], body->data, body->length);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidekint sec_http_append_header(TALLOC_CTX *mem_ctx, char **dest,
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek *dest = talloc_asprintf(mem_ctx, "%s: %s\r\n", field, value);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek *dest = talloc_asprintf_append_buffer(*dest, "%s: %s\r\n",
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidekint sec_http_reply_with_headers(TALLOC_CTX *mem_ctx, struct sec_data *reply,
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek const char *reason_phrase = reason ? reason : "";
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek /* Status-Line */
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek reply->data = talloc_asprintf(mem_ctx, "HTTP/1.1 %d %s\r\n",
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek /* Headers */
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek for (int i = 0; i < num_headers; i++) {
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek if (strcasecmp(headers[i].name, "Content-Length") == 0) {
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek } else if (strcasecmp(headers[i].name, "Content-Type") == 0) {
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek ret = sec_http_append_header(mem_ctx, &reply->data,
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek reply->data = talloc_asprintf_append_buffer(reply->data,
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek "Content-Length: %u\r\n", (unsigned)body->length);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek /* CRLF separator before body */
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek reply->data = talloc_strdup_append_buffer(reply->data, "\r\n");
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek /* Message-Body */
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek reply->data = talloc_realloc(mem_ctx, reply->data, char,
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek memcpy(&reply->data[reply->length], body->data, body->length);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidekenum sec_http_status_codes sec_errno_to_http_status(errno_t err)
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidekint sec_json_to_simple_secret(TALLOC_CTX *mem_ctx,
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek "Failed to parse JSON payload on line %d: %s\n",
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek DEBUG(SSSDBG_CRIT_FAILURE, "Json data is not an object.\n");
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek DEBUG(SSSDBG_CRIT_FAILURE, "Json data key 'type' not found.\n");
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek DEBUG(SSSDBG_CRIT_FAILURE, "Json object 'type' is not a string.\n");
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek if (strcmp(json_string_value(element), "simple") != 0) {
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek DEBUG(SSSDBG_CRIT_FAILURE, "Token type is not 'simple'.\n");
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek DEBUG(SSSDBG_CRIT_FAILURE, "Json key 'value' not found.\n");
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek DEBUG(SSSDBG_CRIT_FAILURE, "Json object 'value' is not a string.\n");
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek *secret = talloc_strdup(mem_ctx, json_string_value(element));
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidekint sec_simple_secret_to_json(TALLOC_CTX *mem_ctx,
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek root = json_pack("{s:s, s:s}", "type", "simple", "value", secret);
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek for (int i = 0; i < count; i++) {
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek // FIXME: json_string mem leak ?
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek // FIXME: Error checking
25f8fac2489fd209d603acb2b494f7c72968e9bbMichal Zidek json_array_append_new(root, json_string(array[i]));
if (!jsonized) {
goto done;
if (!*output) {
goto done;
done:
return ret;
return EOK;
return ENOENT;
return EOK;