Blame SOURCES/0005-Issue-51086-Improve-dscreate-instance-name-validatio.patch

b55ad6
From 9710c327b3034d7a9d112306961c9cec98083df5 Mon Sep 17 00:00:00 2001
b55ad6
From: Simon Pichugin <simon.pichugin@gmail.com>
b55ad6
Date: Mon, 18 May 2020 22:33:45 +0200
b55ad6
Subject: [PATCH 05/12] Issue 51086 - Improve dscreate instance name validation
b55ad6
b55ad6
Bug Description: When creating an instance using dscreate, it doesn't enforce
b55ad6
max name length. The ldapi socket name contains name of the instance. If it's
b55ad6
too long, we can hit limits, and the file name will be truncated. Also, it
b55ad6
doesn't sanitize the instance name, it's possible to create an instance with
b55ad6
non-ascii symbols in its name.
b55ad6
b55ad6
Fix Description: Add more checks to 'dscreate from-file' installation.
b55ad6
Add a limitation for nsslapd-ldapifilepath string lenght because it is
b55ad6
limited by sizeof((*ports_info.i_listenaddr)->local.path)) it is copied to.
b55ad6
b55ad6
https://pagure.io/389-ds-base/issue/51086
b55ad6
b55ad6
Reviewed by: firstyear, mreynolds (Thanks!)
b55ad6
---
b55ad6
 ldap/servers/slapd/libglobs.c       | 12 ++++++++++++
b55ad6
 src/cockpit/389-console/src/ds.jsx  |  8 ++++++--
b55ad6
 src/lib389/lib389/instance/setup.py |  9 +++++++++
b55ad6
 3 files changed, 27 insertions(+), 2 deletions(-)
b55ad6
b55ad6
diff --git a/ldap/servers/slapd/libglobs.c b/ldap/servers/slapd/libglobs.c
b55ad6
index 0d3d9a924..fbf90d92d 100644
b55ad6
--- a/ldap/servers/slapd/libglobs.c
b55ad6
+++ b/ldap/servers/slapd/libglobs.c
b55ad6
@@ -2390,11 +2390,23 @@ config_set_ldapi_filename(const char *attrname, char *value, char *errorbuf, int
b55ad6
 {
b55ad6
     int retVal = LDAP_SUCCESS;
b55ad6
     slapdFrontendConfig_t *slapdFrontendConfig = getFrontendConfig();
b55ad6
+    /*
b55ad6
+     * LDAPI file path length is limited by sizeof((*ports_info.i_listenaddr)->local.path))
b55ad6
+     * which is set in main.c inside of "#if defined(ENABLE_LDAPI)" block
b55ad6
+     * ports_info.i_listenaddr is sizeof(PRNetAddr) and our required sizes is 8 bytes less
b55ad6
+     */
b55ad6
+    size_t result_size = sizeof(PRNetAddr) - 8;
b55ad6
 
b55ad6
     if (config_value_is_null(attrname, value, errorbuf, 0)) {
b55ad6
         return LDAP_OPERATIONS_ERROR;
b55ad6
     }
b55ad6
 
b55ad6
+    if (strlen(value) >= result_size) {
b55ad6
+        slapi_create_errormsg(errorbuf, SLAPI_DSE_RETURNTEXT_SIZE, "%s: \"%s\" is invalid, its length must be less than %d",
b55ad6
+                              attrname, value, result_size);
b55ad6
+        return LDAP_OPERATIONS_ERROR;
b55ad6
+    }
b55ad6
+
b55ad6
     if (apply) {
b55ad6
         CFG_LOCK_WRITE(slapdFrontendConfig);
b55ad6
 
b55ad6
diff --git a/src/cockpit/389-console/src/ds.jsx b/src/cockpit/389-console/src/ds.jsx
b55ad6
index 90d9e5abd..53aa5cb79 100644
b55ad6
--- a/src/cockpit/389-console/src/ds.jsx
b55ad6
+++ b/src/cockpit/389-console/src/ds.jsx
b55ad6
@@ -793,10 +793,14 @@ class CreateInstanceModal extends React.Component {
b55ad6
             return;
b55ad6
         }
b55ad6
         newServerId = newServerId.replace(/^slapd-/i, ""); // strip "slapd-"
b55ad6
-        if (newServerId.length > 128) {
b55ad6
+        if (newServerId === "admin") {
b55ad6
+            addNotification("warning", "Instance Name 'admin' is reserved, please choose a different name");
b55ad6
+            return;
b55ad6
+        }
b55ad6
+        if (newServerId.length > 80) {
b55ad6
             addNotification(
b55ad6
                 "warning",
b55ad6
-                "Instance name is too long, it must not exceed 128 characters"
b55ad6
+                "Instance name is too long, it must not exceed 80 characters"
b55ad6
             );
b55ad6
             return;
b55ad6
         }
b55ad6
diff --git a/src/lib389/lib389/instance/setup.py b/src/lib389/lib389/instance/setup.py
b55ad6
index 803992275..f5fc5495d 100644
b55ad6
--- a/src/lib389/lib389/instance/setup.py
b55ad6
+++ b/src/lib389/lib389/instance/setup.py
b55ad6
@@ -567,6 +567,15 @@ class SetupDs(object):
b55ad6
 
b55ad6
         # We need to know the prefix before we can do the instance checks
b55ad6
         assert_c(slapd['instance_name'] is not None, "Configuration instance_name in section [slapd] not found")
b55ad6
+        assert_c(len(slapd['instance_name']) <= 80, "Server identifier should not be longer than 80 symbols")
b55ad6
+        assert_c(all(ord(c) < 128 for c in slapd['instance_name']), "Server identifier can not contain non ascii characters")
b55ad6
+        assert_c(' ' not in slapd['instance_name'], "Server identifier can not contain a space")
b55ad6
+        assert_c(slapd['instance_name'] != 'admin', "Server identifier \"admin\" is reserved, please choose a different identifier")
b55ad6
+
b55ad6
+        # Check that valid characters are used
b55ad6
+        safe = re.compile(r'^[#%:\w@_-]+$').search
b55ad6
+        assert_c(bool(safe(slapd['instance_name'])), "Server identifier has invalid characters, please choose a different value")
b55ad6
+
b55ad6
         # Check if the instance exists or not.
b55ad6
         # Should I move this import? I think this prevents some recursion
b55ad6
         from lib389 import DirSrv
b55ad6
-- 
b55ad6
2.26.2
b55ad6