providers.c revision 677a31351c80453d9ce006481364399a96312052
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen Secrets Responder
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen Copyright (C) Simo Sorce <ssorce@redhat.com> 2016
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen This program is free software; you can redistribute it and/or modify
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen it under the terms of the GNU General Public License as published by
ed51404d355ae65d762cee68aefdbf900903a658Timo Sirainen the Free Software Foundation; either version 3 of the License, or
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen (at your option) any later version.
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen This program is distributed in the hope that it will be useful,
4afaedfcbd43896befbe1fd5c10eba42246f3fdeTimo Sirainen but WITHOUT ANY WARRANTY; without even the implied warranty of
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen GNU General Public License for more details.
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen You should have received a copy of the GNU General Public License
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen along with this program. If not, see <http://www.gnu.org/licenses/>.
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainentypedef int (*url_mapper_fn)(struct sec_req_ctx *secreq,
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainenstatic int sec_map_url_to_user_path(struct sec_req_ctx *secreq,
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen /* change path to be user specific */
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen talloc_asprintf(secreq, SEC_BASEPATH"users/%"SPRIuid"/%s",
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen &secreq->parsed_url.path[sizeof(SEC_BASEPATH) - 1]);
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen "Failed to map request to user specific url\n");
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen "User-specific secrets path is [%s]\n", *mapped_path);
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainenstatic int kcm_map_url_to_path(struct sec_req_ctx *secreq,
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen *mapped_path = talloc_strdup(secreq, secreq->parsed_url.path );
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen "Failed to map request to user specific url\n");
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen "User-specific KCM path is [%s]\n", *mapped_path);
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainenstatic struct url_pfx_router secrets_url_mapping[] = {
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainenint sec_req_routing(TALLOC_CTX *mem_ctx, struct sec_req_ctx *secreq,
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen sctx = talloc_get_type(secreq->cctx->rctx->pvt_ctx, struct sec_ctx);
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen for (int i = 0; secrets_url_mapping[i].prefix != NULL; i++) {
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen strlen(secrets_url_mapping[i].prefix)) == 0) {
1d2b188f0eedc3cab6e27ceac5425a037f38042eTimo Sirainen "Mapping prefix %s\n", secrets_url_mapping[i].prefix);
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen "Path [%s] does not start with any allowed prefix\n",
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen ret = mapper_fn(secreq, &secreq->mapped_path);
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen "Failed to map the user path [%d]: %s\n",
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen /* source default provider */
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen ret = confdb_get_string(secreq->cctx->rctx->cdb, mem_ctx,
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen "The default provider is '%s'\n", def_provider);
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen ret = confdb_get_sub_sections(mem_ctx, secreq->cctx->rctx->cdb,
f185133819c115c8cbc1c7e96804f237e23d255cTimo Sirainen "confdb section %s has %d sub-sections\n",
ed51404d355ae65d762cee68aefdbf900903a658Timo Sirainen // TODO order by length?
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen for (int i = 0; i < num_sections; i++) {
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen secreq->base_path = talloc_asprintf(secreq, SEC_BASEPATH"%s/", sections[i]);
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen if (strncmp(secreq->base_path, secreq->mapped_path, slen) == 0) {
96308127e006bb3b1108093bcf4cc1fd9481cb7aTimo Sirainen secname = talloc_asprintf(mem_ctx, CONFDB_SEC_CONF_ENTRY"/%s",
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen ret = confdb_get_string(secreq->cctx->rctx->cdb, mem_ctx,
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen "matched subsection %s with provider %s\n",
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen secreq->cfg_section = talloc_steal(secreq, secname);
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen if (!secreq->base_path) secreq->base_path = SEC_BASEPATH;
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen "Request base path is [%s]\n", secreq->base_path);
2ef5254ab6b446b93ce7733bc96eeefa6f731ee4Timo Sirainen ret = sec_get_provider(sctx, provider, handle);
2ef5254ab6b446b93ce7733bc96eeefa6f731ee4Timo Sirainen ret = local_secrets_provider_handle(sctx, handle);
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen } else if (strcasecmp(provider, "PROXY") == 0) {
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen ret = proxy_secrets_provider_handle(sctx, handle);
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainenint sec_provider_recv(struct tevent_req *req) {
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen DEBUG(SSSDBG_TRACE_INTERNAL, "Request finished\n");
2ef5254ab6b446b93ce7733bc96eeefa6f731ee4Timo Sirainen "The request format is invalid." },
2ef5254ab6b446b93ce7733bc96eeefa6f731ee4Timo Sirainen "Access to the requested resource requires authentication." },
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen "Access to the requested resource is forbidden." },
2615df45a8027948a474abe5e817b34b0499c171Timo Sirainen "The requested resource was not found." },
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen "Request method not allowed for this resource." },
2ef5254ab6b446b93ce7733bc96eeefa6f731ee4Timo Sirainen "The request cannot be accepted." },
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen "The requested resource already exists." },
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen "The secret payload is too large." },
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen "The server encountered an internal error." },
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen "No response from a proxy server." },
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen "The server is unable to store the resource needed to complete the request." },
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainenint sec_http_status_reply(TALLOC_CTX *mem_ctx, struct sec_data *reply,
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen "<head>\r\n<title>%d %s</title></head>\r\n"
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen "<h1>%s</h1>\r\n"
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen "<p>%s</p>\r\n"
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen sec_http_status_format_table[code].description);
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen "HTTP/1.1 %d %s\r\n"
4afaedfcbd43896befbe1fd5c10eba42246f3fdeTimo Sirainen "Content-Length: %u\r\n"
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen "Content-Type: text/html\r\n"
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen "HTTP reply %d: %s\n",
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainenint sec_http_reply_with_body(TALLOC_CTX *mem_ctx, struct sec_data *reply,
int head_size;
return EOK;
return EOK;
bool add_content_length = true;
bool has_content_type = false;
int ret;
for (int i = 0; i < num_headers; i++) {
add_content_length = false;
has_content_type = true;
if (!has_content_type) {
return EINVAL;
if (add_content_length) {
return EOK;
static errno_t
const char **headers,
const char **body)
char *delim;
return EINVAL;
const char *headers,
return headers;
int response_code,
const char *headers;
const char *body;
return EOK;
return ENOMEM;
return ENOMEM;
return EOK;
switch (err) {
case EOK:
return STATUS_200;
case EINVAL:
return STATUS_400;
case EACCES:
return STATUS_401;
case EPERM:
return STATUS_403;
case ENOENT:
return STATUS_404;
case EISDIR:
return STATUS_405;
case EMEDIUMTYPE:
return STATUS_406;
case EEXIST:
return STATUS_409;
return STATUS_413;
case ERR_SEC_NO_PROXY:
return STATUS_504;
return STATUS_507;
return STATUS_500;
const char *input,
char **secret)
int ret;
if (!root) {
return EINVAL;
goto done;
if (!element) {
goto done;
goto done;
goto done;
if (!element) {
goto done;
goto done;
if (!*secret) {
done:
return ret;
const char *secret,
char **output)
int ret;
if (!root) {
return ENOMEM;
if (!jsonized) {
goto done;
if (!*output) {
goto done;
done:
return ret;
char **output)
int ret;
goto done;
for (int i = 0; i < count; i++) {
if (!jsonized) {
goto done;
if (!*output) {
goto done;
done:
return ret;
return EOK;
return ENOENT;
return EOK;