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