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