a3470f
From 355e366ff59dfc2ecd4fdf1e5653664b9ac0c45f Mon Sep 17 00:00:00 2001
a3470f
From: Mohit Agrawal <moagrawa@redhat.com>
a3470f
Date: Wed, 14 Mar 2018 09:37:52 +0530
a3470f
Subject: [PATCH 202/212] glusterd: TLS verification fails while using
a3470f
 intermediate CA
a3470f
a3470f
Problem: TLS verification fails while using intermediate CA
a3470f
         if mgmt SSL is enabled.
a3470f
a3470f
Solution: There are two main issue of TLS verification failing
a3470f
          1) not calling ssl_api to set cert_depth
a3470f
          2) The current code does not allow to set certificate depth
a3470f
             while MGMT SSL is enabled.
a3470f
          After apply this patch to set certificate depth user
a3470f
          need to set parameter option transport.socket.ssl-cert-depth <depth>
a3470f
          in /var/lib/glusterd/secure_acccess instead to set in
a3470f
          /etc/glusterfs/glusterd.vol. At the time of set secure_mgmt in ctx
a3470f
          we will check the value of cert-depth and save the value of cert-depth
a3470f
          in ctx.If user does not provide any value in cert-depth in that case
a3470f
          it will consider default value is 1
a3470f
a3470f
> BUG: 1555154
a3470f
> Change-Id: I89e9a9e1026e37efb5c20f9ec62b1989ef644f35
a3470f
> Reviewed on https://review.gluster.org/#/c/19708/
a3470f
> (cherry pick from commit cf06dd544004701ef43fa81c5b7a95353d5c1d65)
a3470f
a3470f
BUG: 1446046
a3470f
Change-Id: I94000bc8741ceb5659ec9f376eac447ae84792ad
a3470f
Signed-off-by: Mohit Agrawal <moagrawa@redhat.com>
a3470f
Reviewed-on: https://code.engineering.redhat.com/gerrit/133849
a3470f
Tested-by: RHGS Build Bot <nigelb@redhat.com>
a3470f
Reviewed-by: Sunil Kumar Heggodu Gopala Acharya <sheggodu@redhat.com>
a3470f
---
a3470f
 api/src/glfs-mgmt.c                          |  1 +
a3470f
 cli/src/cli.c                                |  1 +
a3470f
 glusterfsd/src/glusterfsd-mgmt.c             |  2 ++
a3470f
 glusterfsd/src/glusterfsd.c                  |  1 +
a3470f
 heal/src/glfs-heal.c                         |  1 +
a3470f
 libglusterfs/src/glusterfs.h                 |  6 ++++
a3470f
 libglusterfs/src/graph.c                     | 42 +++++++++++++++++++++++++++-
a3470f
 rpc/rpc-transport/socket/src/socket.c        | 12 +++++---
a3470f
 xlators/mgmt/glusterd/src/glusterd-handler.c |  3 ++
a3470f
 9 files changed, 64 insertions(+), 5 deletions(-)
a3470f
a3470f
diff --git a/api/src/glfs-mgmt.c b/api/src/glfs-mgmt.c
a3470f
index 32b9dbd..b70dc35 100644
a3470f
--- a/api/src/glfs-mgmt.c
a3470f
+++ b/api/src/glfs-mgmt.c
a3470f
@@ -996,6 +996,7 @@ glfs_mgmt_init (struct glfs *fs)
a3470f
 
a3470f
         if (sys_access (SECURE_ACCESS_FILE, F_OK) == 0) {
a3470f
                 ctx->secure_mgmt = 1;
a3470f
+                ctx->ssl_cert_depth = glusterfs_read_secure_access_file ();
a3470f
         }
a3470f
 
a3470f
 	rpc = rpc_clnt_new (options, THIS, THIS->name, 8);
a3470f
diff --git a/cli/src/cli.c b/cli/src/cli.c
a3470f
index 52c1b67..b64d4ef 100644
a3470f
--- a/cli/src/cli.c
a3470f
+++ b/cli/src/cli.c
a3470f
@@ -432,6 +432,7 @@ parse_cmdline (int argc, char *argv[], struct cli_state *state)
a3470f
         /* Do this first so that an option can override. */
a3470f
         if (sys_access (SECURE_ACCESS_FILE, F_OK) == 0) {
a3470f
                 state->ctx->secure_mgmt = 1;
a3470f
+                state->ctx->ssl_cert_depth = glusterfs_read_secure_access_file ();
a3470f
         }
a3470f
 
a3470f
         if (state->argc > GEO_REP_CMD_CONFIG_INDEX &&
a3470f
diff --git a/glusterfsd/src/glusterfsd-mgmt.c b/glusterfsd/src/glusterfsd-mgmt.c
a3470f
index 69d93f5..ef53d09 100644
a3470f
--- a/glusterfsd/src/glusterfsd-mgmt.c
a3470f
+++ b/glusterfsd/src/glusterfsd-mgmt.c
a3470f
@@ -2467,6 +2467,8 @@ glusterfs_mgmt_init (glusterfs_ctx_t *ctx)
a3470f
                         goto out;
a3470f
 
a3470f
                 }
a3470f
+
a3470f
+                ctx->ssl_cert_depth = glusterfs_read_secure_access_file ();
a3470f
         }
a3470f
 
a3470f
         rpc = rpc_clnt_new (options, THIS, THIS->name, 8);
a3470f
diff --git a/glusterfsd/src/glusterfsd.c b/glusterfsd/src/glusterfsd.c
a3470f
index 38b863c..3ae89a6 100644
a3470f
--- a/glusterfsd/src/glusterfsd.c
a3470f
+++ b/glusterfsd/src/glusterfsd.c
a3470f
@@ -1917,6 +1917,7 @@ parse_cmdline (int argc, char *argv[], glusterfs_ctx_t *ctx)
a3470f
         /* Do this before argp_parse so it can be overridden. */
a3470f
         if (sys_access (SECURE_ACCESS_FILE, F_OK) == 0) {
a3470f
                 cmd_args->secure_mgmt = 1;
a3470f
+                ctx->ssl_cert_depth = glusterfs_read_secure_access_file ();
a3470f
         }
a3470f
 
a3470f
         argp_parse (&argp, argc, argv, ARGP_IN_ORDER, NULL, cmd_args);
a3470f
diff --git a/heal/src/glfs-heal.c b/heal/src/glfs-heal.c
a3470f
index 532b6f9..153cd29 100644
a3470f
--- a/heal/src/glfs-heal.c
a3470f
+++ b/heal/src/glfs-heal.c
a3470f
@@ -1617,6 +1617,7 @@ main (int argc, char **argv)
a3470f
 
a3470f
         if (sys_access(SECURE_ACCESS_FILE, F_OK) == 0) {
a3470f
                 fs->ctx->secure_mgmt = 1;
a3470f
+                fs->ctx->ssl_cert_depth = glusterfs_read_secure_access_file ();
a3470f
         }
a3470f
 
a3470f
         ret = glfs_set_volfile_server (fs, "unix", DEFAULT_GLUSTERD_SOCKFILE, 0);
a3470f
diff --git a/libglusterfs/src/glusterfs.h b/libglusterfs/src/glusterfs.h
a3470f
index 5abfafa..5d5f5c8 100644
a3470f
--- a/libglusterfs/src/glusterfs.h
a3470f
+++ b/libglusterfs/src/glusterfs.h
a3470f
@@ -536,6 +536,11 @@ struct _glusterfs_ctx {
a3470f
          */
a3470f
         int                secure_mgmt;
a3470f
 
a3470f
+        /* The option is use to set cert_depth while management connection
a3470f
+           use SSL
a3470f
+         */
a3470f
+        int                ssl_cert_depth;
a3470f
+
a3470f
         /*
a3470f
          * Should *our* server/inbound connections use SSL?  This is only true
a3470f
          * if we're glusterd and secure_mgmt is set, or if we're glusterfsd
a3470f
@@ -638,4 +643,5 @@ int glusterfs_graph_parent_up (glusterfs_graph_t *graph);
a3470f
 void
a3470f
 gf_free_mig_locks (lock_migration_info_t *locks);
a3470f
 
a3470f
+int glusterfs_read_secure_access_file (void);
a3470f
 #endif /* _GLUSTERFS_H */
a3470f
diff --git a/libglusterfs/src/graph.c b/libglusterfs/src/graph.c
a3470f
index 738cd96..cdd7123 100644
a3470f
--- a/libglusterfs/src/graph.c
a3470f
+++ b/libglusterfs/src/graph.c
a3470f
@@ -16,7 +16,7 @@
a3470f
 #include "defaults.h"
a3470f
 #include <unistd.h>
a3470f
 #include "syscall.h"
a3470f
-
a3470f
+#include <regex.h>
a3470f
 #include "libglusterfs-messages.h"
a3470f
 
a3470f
 #if 0
a3470f
@@ -68,7 +68,47 @@ _gf_dump_details (int argc, char **argv)
a3470f
 }
a3470f
 #endif
a3470f
 
a3470f
+int
a3470f
+glusterfs_read_secure_access_file (void)
a3470f
+{
a3470f
+        FILE *fp = NULL;
a3470f
+        char  line[100] = {0,};
a3470f
+        int   cert_depth = 1;   /* Default SSL CERT DEPTH */
a3470f
+        regex_t regcmpl;
a3470f
+        char *key = {"^option transport.socket.ssl-cert-depth"};
a3470f
+        char  keyval[50] = {0,};
a3470f
+        int start = 0, end = 0, copy_len = 0;
a3470f
+        regmatch_t result[1] = {{0} };
a3470f
+
a3470f
+        fp = fopen (SECURE_ACCESS_FILE, "r");
a3470f
+        if (!fp)
a3470f
+                goto out;
a3470f
 
a3470f
+        /* Check if any line matches with key */
a3470f
+        while (fgets(line, sizeof(line), fp) != NULL) {
a3470f
+                if (regcomp (&regcmpl, key, REG_EXTENDED)) {
a3470f
+                        goto out;
a3470f
+                }
a3470f
+                if (!regexec (&regcmpl, line, 1, result, 0)) {
a3470f
+                        start = result[0].rm_so;
a3470f
+                        end  = result[0].rm_eo;
a3470f
+                        copy_len = end - start;
a3470f
+                        strcpy (keyval, line+copy_len);
a3470f
+                        if (keyval[0]) {
a3470f
+                                cert_depth = atoi(keyval);
a3470f
+                                if (cert_depth == 0)
a3470f
+                                        cert_depth = 1; /* Default SSL CERT DEPTH */
a3470f
+                                break;
a3470f
+                        }
a3470f
+                }
a3470f
+                regfree(&regcmpl);
a3470f
+        }
a3470f
+
a3470f
+out:
a3470f
+        if (fp)
a3470f
+                fclose (fp);
a3470f
+        return cert_depth;
a3470f
+}
a3470f
 
a3470f
 int
a3470f
 glusterfs_xlator_link (xlator_t *pxl, xlator_t *cxl)
a3470f
diff --git a/rpc/rpc-transport/socket/src/socket.c b/rpc/rpc-transport/socket/src/socket.c
a3470f
index 590d465..157b5b7 100644
a3470f
--- a/rpc/rpc-transport/socket/src/socket.c
a3470f
+++ b/rpc/rpc-transport/socket/src/socket.c
a3470f
@@ -4324,7 +4324,13 @@ socket_init (rpc_transport_t *this)
a3470f
                "using %s polling thread",
a3470f
 	       priv->own_thread ? "private" : "system");
a3470f
 
a3470f
-        if (!dict_get_int32 (this->options, SSL_CERT_DEPTH_OPT, &cert_depth)) {
a3470f
+        if (!priv->mgmt_ssl) {
a3470f
+                if (!dict_get_int32 (this->options, SSL_CERT_DEPTH_OPT, &cert_depth)) {
a3470f
+                        gf_log (this->name, GF_LOG_INFO,
a3470f
+                                "using certificate depth %d", cert_depth);
a3470f
+                }
a3470f
+        } else {
a3470f
+                cert_depth = this->ctx->ssl_cert_depth;
a3470f
                 gf_log (this->name, GF_LOG_INFO,
a3470f
                         "using certificate depth %d", cert_depth);
a3470f
         }
a3470f
@@ -4463,9 +4469,7 @@ socket_init (rpc_transport_t *this)
a3470f
 			goto err;
a3470f
 		}
a3470f
 
a3470f
-#if (OPENSSL_VERSION_NUMBER < 0x00905100L)
a3470f
-		SSL_CTX_set_verify_depth(ctx,cert_depth);
a3470f
-#endif
a3470f
+                SSL_CTX_set_verify_depth(priv->ssl_ctx, cert_depth);
a3470f
 
a3470f
 		if (crl_path) {
a3470f
 #ifdef X509_V_FLAG_CRL_CHECK_ALL
a3470f
diff --git a/xlators/mgmt/glusterd/src/glusterd-handler.c b/xlators/mgmt/glusterd/src/glusterd-handler.c
a3470f
index 16a3773..ddab159 100644
a3470f
--- a/xlators/mgmt/glusterd/src/glusterd-handler.c
a3470f
+++ b/xlators/mgmt/glusterd/src/glusterd-handler.c
a3470f
@@ -3544,6 +3544,9 @@ glusterd_friend_rpc_create (xlator_t *this, glusterd_peerinfo_t *peerinfo,
a3470f
                                 "failed to set ssl-enabled in dict");
a3470f
                         goto out;
a3470f
                 }
a3470f
+
a3470f
+                this->ctx->ssl_cert_depth = glusterfs_read_secure_access_file ();
a3470f
+
a3470f
         }
a3470f
 
a3470f
         ret = glusterd_rpc_create (&peerinfo->rpc, options,
a3470f
-- 
a3470f
1.8.3.1
a3470f