Blame SOURCES/bz1376819-3-qnetd-Check-existence-of-NSS-DB-dir-before-fork.patch

478375
From eac28dffdf7f060f41f2b2e95bb0f4c6c033425d Mon Sep 17 00:00:00 2001
478375
From: Jan Friesse <jfriesse@redhat.com>
478375
Date: Tue, 19 Mar 2019 14:40:12 +0100
478375
Subject: [PATCH] qnetd: Check existence of NSS DB dir before fork
478375
478375
Previously, when user tried start corosync-qnetd without
478375
initialized NSS database then generic (not very helpful
478375
and misleading) NSS error was logged
478375
"NSS error (-8015): The certificate/key database is in an old,
478375
unsupported format.".
478375
478375
Solution is to check if it's possible to open NSS DB directory and
478375
display (usually much more informative) result of strerror function.
478375
478375
Such check is called before fork, so init system can return error code
478375
during start.
478375
478375
To make error reporting work with systemd it's also needed to change
478375
unit type from simple to forking.
478375
478375
Signed-off-by: Jan Friesse <jfriesse@redhat.com>
478375
Reviewed-by: Christine Caulfield <ccaulfie@redhat.com>
478375
---
478375
 init/corosync-qnetd.service.in |  4 ++--
478375
 qdevices/corosync-qnetd.c      | 12 +++++++++++-
478375
 qdevices/nss-sock.c            | 23 ++++++++++++++++++++++-
478375
 qdevices/nss-sock.h            |  4 +++-
478375
 4 files changed, 38 insertions(+), 5 deletions(-)
478375
478375
diff --git a/init/corosync-qnetd.service.in b/init/corosync-qnetd.service.in
478375
index 54b9849..8cff766 100644
478375
--- a/init/corosync-qnetd.service.in
478375
+++ b/init/corosync-qnetd.service.in
478375
@@ -7,8 +7,8 @@ After=network-online.target
478375
 
478375
 [Service]
478375
 EnvironmentFile=-@INITCONFIGDIR@/corosync-qnetd
478375
-ExecStart=@BINDIR@/corosync-qnetd -f $COROSYNC_QNETD_OPTIONS
478375
-Type=simple
478375
+ExecStart=@BINDIR@/corosync-qnetd $COROSYNC_QNETD_OPTIONS
478375
+Type=forking
478375
 Restart=on-abnormal
478375
 # Uncomment and set user who should be used for executing qnetd
478375
 #User=coroqnetd
478375
diff --git a/qdevices/corosync-qnetd.c b/qdevices/corosync-qnetd.c
478375
index 9af94b7..938e4ce 100644
478375
--- a/qdevices/corosync-qnetd.c
478375
+++ b/qdevices/corosync-qnetd.c
478375
@@ -1,5 +1,5 @@
478375
 /*
478375
- * Copyright (c) 2015-2016 Red Hat, Inc.
478375
+ * Copyright (c) 2015-2019 Red Hat, Inc.
478375
  *
478375
  * All rights reserved.
478375
  *
478375
@@ -543,6 +543,16 @@ main(int argc, char * const argv[])
478375
 	qnetd_log_set_priority_bump(bump_log_priority);
478375
 
478375
 	/*
478375
+	 * Check that it's possible to open NSS dir if needed
478375
+	 */
478375
+	if (nss_sock_check_db_dir((tls_supported != TLV_TLS_UNSUPPORTED ?
478375
+	    advanced_settings.nss_db_dir : NULL)) != 0) {
478375
+		qnetd_log_err(LOG_ERR, "Can't open NSS DB directory");
478375
+
478375
+		exit (1);
478375
+	}
478375
+
478375
+	/*
478375
 	 * Daemonize
478375
 	 */
478375
 	if (!foreground) {
478375
diff --git a/qdevices/nss-sock.c b/qdevices/nss-sock.c
478375
index 3c63927..483d417 100644
478375
--- a/qdevices/nss-sock.c
478375
+++ b/qdevices/nss-sock.c
478375
@@ -1,5 +1,5 @@
478375
 /*
478375
- * Copyright (c) 2015-2016 Red Hat, Inc.
478375
+ * Copyright (c) 2015-2019 Red Hat, Inc.
478375
  *
478375
  * All rights reserved.
478375
  *
478375
@@ -32,6 +32,9 @@
478375
  * THE POSSIBILITY OF SUCH DAMAGE.
478375
  */
478375
 
478375
+#include <sys/types.h>
478375
+
478375
+#include <dirent.h>
478375
 #include <limits.h>
478375
 
478375
 #include "nss-sock.h"
478375
@@ -56,6 +59,24 @@ nss_sock_init_nss(char *config_dir)
478375
 	return (0);
478375
 }
478375
 
478375
+int
478375
+nss_sock_check_db_dir(const char *config_dir)
478375
+{
478375
+	DIR *dirp;
478375
+
478375
+	if (config_dir == NULL) {
478375
+		return (0);
478375
+	}
478375
+
478375
+	if ((dirp = opendir(config_dir)) == NULL) {
478375
+		return (-1);
478375
+	}
478375
+
478375
+	(void)closedir(dirp);
478375
+
478375
+	return (0);
478375
+}
478375
+
478375
 /*
478375
  * Set NSS socket non-blocking
478375
  */
478375
diff --git a/qdevices/nss-sock.h b/qdevices/nss-sock.h
478375
index cc16d96..4f82e0a 100644
478375
--- a/qdevices/nss-sock.h
478375
+++ b/qdevices/nss-sock.h
478375
@@ -1,5 +1,5 @@
478375
 /*
478375
- * Copyright (c) 2015-2016 Red Hat, Inc.
478375
+ * Copyright (c) 2015-2019 Red Hat, Inc.
478375
  *
478375
  * All rights reserved.
478375
  *
478375
@@ -56,6 +56,8 @@ struct nss_sock_non_blocking_client {
478375
 
478375
 extern int		nss_sock_init_nss(char *config_dir);
478375
 
478375
+extern int		nss_sock_check_db_dir(const char *config_dir);
478375
+
478375
 extern PRFileDesc	*nss_sock_create_listen_socket(const char *hostname, uint16_t port,
478375
     PRIntn af);
478375
 
478375
-- 
478375
1.8.3.1
478375