Blame SOURCES/0031-SECRETS-Store-ccaches-in-secrets-for-the-KCM-respond.patch

ecf709
From 91c099a993252680f103084431b1d0f5798d8a24 Mon Sep 17 00:00:00 2001
ecf709
From: Jakub Hrozek <jhrozek@redhat.com>
ecf709
Date: Tue, 21 Mar 2017 14:14:42 +0100
ecf709
Subject: [PATCH 31/36] SECRETS: Store ccaches in secrets for the KCM responder
ecf709
MIME-Version: 1.0
ecf709
Content-Type: text/plain; charset=UTF-8
ecf709
Content-Transfer-Encoding: 8bit
ecf709
ecf709
Adds a new "hive" to the secrets responder whose base path is /kcm. Only
ecf709
root can contact the /kcm hive, because the KCM responder only runs as
ecf709
root and it must impersonate other users and store ccaches on their behalf.
ecf709
ecf709
Reviewed-by: Michal Židek <mzidek@redhat.com>
ecf709
Reviewed-by: Simo Sorce <simo@redhat.com>
ecf709
---
ecf709
 src/responder/secrets/local.c          | 16 +++++++-
ecf709
 src/responder/secrets/providers.c      | 71 ++++++++++++++++++++++++++++++----
ecf709
 src/responder/secrets/secsrv_private.h | 10 ++++-
ecf709
 3 files changed, 86 insertions(+), 11 deletions(-)
ecf709
ecf709
diff --git a/src/responder/secrets/local.c b/src/responder/secrets/local.c
ecf709
index 26c97a2849febbf0ac482d526cf927bfc103b4f2..02007ada8b673071ecba033df0eb3f81af93fcbd 100644
ecf709
--- a/src/responder/secrets/local.c
ecf709
+++ b/src/responder/secrets/local.c
ecf709
@@ -26,6 +26,9 @@
ecf709
 
ecf709
 #define MKEY_SIZE (256 / 8)
ecf709
 
ecf709
+#define SECRETS_BASEDN  "cn=secrets"
ecf709
+#define KCM_BASEDN      "cn=kcm"
ecf709
+
ecf709
 struct local_context {
ecf709
     struct ldb_context *ldb;
ecf709
     struct sec_data master_key;
ecf709
@@ -119,6 +122,7 @@ static int local_encrypt(struct local_context *lctx, TALLOC_CTX *mem_ctx,
ecf709
 
ecf709
 static int local_db_dn(TALLOC_CTX *mem_ctx,
ecf709
                        struct ldb_context *ldb,
ecf709
+                       const char *basedn,
ecf709
                        const char *req_path,
ecf709
                        struct ldb_dn **req_dn)
ecf709
 {
ecf709
@@ -126,7 +130,7 @@ static int local_db_dn(TALLOC_CTX *mem_ctx,
ecf709
     const char *s, *e;
ecf709
     int ret;
ecf709
 
ecf709
-    dn = ldb_dn_new(mem_ctx, ldb, "cn=secrets");
ecf709
+    dn = ldb_dn_new(mem_ctx, ldb, basedn);
ecf709
     if (!dn) {
ecf709
         ret = ENOMEM;
ecf709
         goto done;
ecf709
@@ -738,6 +742,11 @@ static int local_secrets_map_path(TALLOC_CTX *mem_ctx,
ecf709
         lc_req->path = talloc_strdup(lc_req,
ecf709
                                      secreq->mapped_path + (sizeof(SEC_BASEPATH) - 1));
ecf709
         basedn = SECRETS_BASEDN;
ecf709
+    } else if (strncmp(secreq->mapped_path,
ecf709
+               SEC_KCM_BASEPATH, sizeof(SEC_KCM_BASEPATH) - 1) == 0) {
ecf709
+        lc_req->path = talloc_strdup(lc_req,
ecf709
+                                     secreq->mapped_path + (sizeof(SEC_KCM_BASEPATH) - 1));
ecf709
+        basedn = KCM_BASEDN;
ecf709
     } else {
ecf709
         ret = EINVAL;
ecf709
         goto done;
ecf709
@@ -820,7 +829,10 @@ static struct tevent_req *local_secret_req(TALLOC_CTX *mem_ctx,
ecf709
     DEBUG(SSSDBG_TRACE_LIBS, "Content-Type: %s\n", content_type);
ecf709
 
ecf709
     ret = local_secrets_map_path(state, lctx->ldb, secreq, &lc_req);
ecf709
-    if (ret) goto done;
ecf709
+    if (ret) {
ecf709
+        DEBUG(SSSDBG_OP_FAILURE, "Cannot map request path to local path\n");
ecf709
+        goto done;
ecf709
+    }
ecf709
 
ecf709
     switch (secreq->method) {
ecf709
     case HTTP_GET:
ecf709
diff --git a/src/responder/secrets/providers.c b/src/responder/secrets/providers.c
ecf709
index eba555d2e422d08db211979422a2957e48b51589..94831c73036d269addca45c0117811a2c68873fd 100644
ecf709
--- a/src/responder/secrets/providers.c
ecf709
+++ b/src/responder/secrets/providers.c
ecf709
@@ -24,6 +24,14 @@
ecf709
 #include "responder/secrets/secsrv_proxy.h"
ecf709
 #include <jansson.h>
ecf709
 
ecf709
+typedef int (*url_mapper_fn)(struct sec_req_ctx *secreq,
ecf709
+                             char **mapped_path);
ecf709
+
ecf709
+struct url_pfx_router {
ecf709
+    const char *prefix;
ecf709
+    url_mapper_fn mapper_fn;
ecf709
+};
ecf709
+
ecf709
 static int sec_map_url_to_user_path(struct sec_req_ctx *secreq,
ecf709
                                     char **mapped_path)
ecf709
 {
ecf709
@@ -42,10 +50,43 @@ static int sec_map_url_to_user_path(struct sec_req_ctx *secreq,
ecf709
         return ENOMEM;
ecf709
     }
ecf709
 
ecf709
-    DEBUG(SSSDBG_TRACE_LIBS, "User-specific path is [%s]\n", *mapped_path);
ecf709
+    DEBUG(SSSDBG_TRACE_LIBS,
ecf709
+          "User-specific secrets path is [%s]\n", *mapped_path);
ecf709
     return EOK;
ecf709
 }
ecf709
 
ecf709
+static int kcm_map_url_to_path(struct sec_req_ctx *secreq,
ecf709
+                               char **mapped_path)
ecf709
+{
ecf709
+    uid_t c_euid;
ecf709
+
ecf709
+    c_euid = client_euid(secreq->cctx->creds);
ecf709
+    if (c_euid != KCM_PEER_UID) {
ecf709
+        DEBUG(SSSDBG_CRIT_FAILURE,
ecf709
+              "UID %"SPRIuid" is not allowed to access "
ecf709
+              "the "SEC_KCM_BASEPATH" hive\n",
ecf709
+              c_euid);
ecf709
+        return EPERM;
ecf709
+    }
ecf709
+
ecf709
+    *mapped_path = talloc_strdup(secreq, secreq->parsed_url.path );
ecf709
+    if (!*mapped_path) {
ecf709
+        DEBUG(SSSDBG_CRIT_FAILURE,
ecf709
+              "Failed to map request to user specific url\n");
ecf709
+        return ENOMEM;
ecf709
+    }
ecf709
+
ecf709
+    DEBUG(SSSDBG_TRACE_LIBS,
ecf709
+          "User-specific KCM path is [%s]\n", *mapped_path);
ecf709
+    return EOK;
ecf709
+}
ecf709
+
ecf709
+static struct url_pfx_router secrets_url_mapping[] = {
ecf709
+    { SEC_BASEPATH, sec_map_url_to_user_path },
ecf709
+    { SEC_KCM_BASEPATH, kcm_map_url_to_path },
ecf709
+    { NULL, NULL },
ecf709
+};
ecf709
+
ecf709
 int sec_req_routing(TALLOC_CTX *mem_ctx, struct sec_req_ctx *secreq,
ecf709
                     struct provider_handle **handle)
ecf709
 {
ecf709
@@ -55,21 +96,35 @@ int sec_req_routing(TALLOC_CTX *mem_ctx, struct sec_req_ctx *secreq,
ecf709
     char *provider;
ecf709
     int num_sections;
ecf709
     int ret;
ecf709
+    url_mapper_fn mapper_fn = NULL;
ecf709
 
ecf709
     sctx = talloc_get_type(secreq->cctx->rctx->pvt_ctx, struct sec_ctx);
ecf709
 
ecf709
-    /* patch must start with /secrets/ for now */
ecf709
-    ret = strncasecmp(secreq->parsed_url.path,
ecf709
-                      SEC_BASEPATH, sizeof(SEC_BASEPATH) - 1);
ecf709
-    if (ret != 0) {
ecf709
+    for (int i = 0; secrets_url_mapping[i].prefix != NULL; i++) {
ecf709
+        if (strncasecmp(secreq->parsed_url.path,
ecf709
+                        secrets_url_mapping[i].prefix,
ecf709
+                        strlen(secrets_url_mapping[i].prefix)) == 0) {
ecf709
+            DEBUG(SSSDBG_TRACE_LIBS,
ecf709
+                  "Mapping prefix %s\n", secrets_url_mapping[i].prefix);
ecf709
+            mapper_fn = secrets_url_mapping[i].mapper_fn;
ecf709
+            break;
ecf709
+        }
ecf709
+    }
ecf709
+
ecf709
+    if (mapper_fn == NULL) {
ecf709
         DEBUG(SSSDBG_CRIT_FAILURE,
ecf709
-              "Path [%s] does not start with "SEC_BASEPATH"\n",
ecf709
+              "Path [%s] does not start with any allowed prefix\n",
ecf709
               secreq->parsed_url.path);
ecf709
         return EPERM;
ecf709
     }
ecf709
 
ecf709
-    ret = sec_map_url_to_user_path(secreq, &secreq->mapped_path);
ecf709
-    if (ret) return ret;
ecf709
+    ret = mapper_fn(secreq, &secreq->mapped_path);
ecf709
+    if (ret != EOK) {
ecf709
+        DEBUG(SSSDBG_CRIT_FAILURE,
ecf709
+              "Failed to map the user path [%d]: %s\n",
ecf709
+              ret, sss_strerror(ret));
ecf709
+        return ret;
ecf709
+    }
ecf709
 
ecf709
     /* source default provider */
ecf709
     ret = confdb_get_string(secreq->cctx->rctx->cdb, mem_ctx,
ecf709
diff --git a/src/responder/secrets/secsrv_private.h b/src/responder/secrets/secsrv_private.h
ecf709
index 1c3fbd8eadb237551233f048503ddc01b4ba00ae..a8544f656517a17fe4576247779bff4850beaf97 100644
ecf709
--- a/src/responder/secrets/secsrv_private.h
ecf709
+++ b/src/responder/secrets/secsrv_private.h
ecf709
@@ -101,7 +101,15 @@ int sec_get_provider(struct sec_ctx *sctx, const char *name,
ecf709
                      struct provider_handle **out_handle);
ecf709
 int sec_add_provider(struct sec_ctx *sctx, struct provider_handle *handle);
ecf709
 
ecf709
-#define SEC_BASEPATH "/secrets/"
ecf709
+#define SEC_BASEPATH            "/secrets/"
ecf709
+#define SEC_KCM_BASEPATH        "/kcm/"
ecf709
+
ecf709
+/* The KCM responder must "impersonate" the owner of the credentials.
ecf709
+ * Only a trusted UID can do that -- root by default, but unit
ecf709
+ * tests might choose otherwise */
ecf709
+#ifndef KCM_PEER_UID
ecf709
+#define KCM_PEER_UID            0
ecf709
+#endif /* KCM_PEER_UID */
ecf709
 
ecf709
 /* providers.c */
ecf709
 int sec_req_routing(TALLOC_CTX *mem_ctx, struct sec_req_ctx *secreq,
ecf709
-- 
ecf709
2.9.3
ecf709