From 4f118f428c24172fca06e9d07e71cad85241bf6e Mon Sep 17 00:00:00 2001 From: Matúš Honěk Date: Sep 25 2018 14:01:04 +0000 Subject: Issue 49947 - Coverity: 389-ds-base-1.4.0.16.20180905git8ff8cb8 Bugs and fixes description: - 15607: - in lib389/suffix.py: Structurally dead code - 15604: - in bug_harness.py: Null dereference + solved by removing the file as it is unused and outdated - additionally, remove the static_var decorator definition as it is not used anyway any more - 15754: - in pwdPolicy_syntax_test.py: fix typo in identifier + Fix log msg in file pw.c - 17046, 17061, 17063, 17069, 17084: - in backend_test and backendLegacy_test: fix use before NULL check - 17473: - in lib389/__init__.py: Identical code in different branches + removed the wrapper altogether as we don't support Python2 any more - replace SafeConfigParser with ConfigParser for it has been just an alias since Python 3.2 and will be removed in the future https://pagure.io/389-ds-base/issue/49947 Author: mhonek Review by: spichugi, firstyear (Thanks!) --- diff --git a/dirsrvtests/tests/suites/password/pwdPolicy_syntax_test.py b/dirsrvtests/tests/suites/password/pwdPolicy_syntax_test.py index a81ccee..0fd0fdb 100644 --- a/dirsrvtests/tests/suites/password/pwdPolicy_syntax_test.py +++ b/dirsrvtests/tests/suites/password/pwdPolicy_syntax_test.py @@ -246,7 +246,7 @@ def test_basic(topology_st, create_user, password_policy): 'password123', 'does not contain minimum number of alphas') # Max Repeats tryPassword(topology_st.standalone, 'passwordMaxRepeats', 2, 0, 'passsword', - 'pasword123', 'too many repeating characters') + 'password123', 'too many repeating characters') # Min Specials tryPassword(topology_st.standalone, 'passwordMinSpecials', 2, 0, 'passwd', 'password_#$', diff --git a/ldap/servers/slapd/pw.c b/ldap/servers/slapd/pw.c index 186657d..3f32e5a 100644 --- a/ldap/servers/slapd/pw.c +++ b/ldap/servers/slapd/pw.c @@ -1221,7 +1221,7 @@ check_pw_syntax_ext(Slapi_PBlock *pb, const Slapi_DN *sdn, Slapi_Value **vals, c syntax_violation = 1; PR_snprintf(errormsg, sizeof(errormsg) - 1, "invalid password syntax - a character cannot be repeated more than %d times", - (pwpolicy->pw_maxrepeats + 1)); + (pwpolicy->pw_maxrepeats)); } else if (pwpolicy->pw_mincategories > num_categories) { syntax_violation = 1; PR_snprintf(errormsg, sizeof(errormsg) - 1, diff --git a/src/lib389/bug_harness.py b/src/lib389/bug_harness.py deleted file mode 100644 index ba61344..0000000 --- a/src/lib389/bug_harness.py +++ /dev/null @@ -1,107 +0,0 @@ -# --- BEGIN COPYRIGHT BLOCK --- -# Copyright (C) 2015 Red Hat, Inc. -# All rights reserved. -# -# License: GPL (version 3 or any later version). -# See LICENSE for details. -# --- END COPYRIGHT BLOCK --- - -from bug_harness import DSAdminHarness as DSAdmin -from dsadmin.tools import DSAdminTools -from lib389.properties import * -""" - An harness for bug replication. - -""" -import os - -REPLBINDDN = '' -REPLBINDPW = '' - - -@static_var("REPLICAID", 1) -def get_next_replicaid(replica_id=None, replica_type=None): - if replica_id: - REPLICAID = replica_id - return REPLICAID - # get a default replica_id if it's a MASTER, - # or 0 if consumer - if replica_type == MASTER_TYPE: - REPLICAID += 1 - return REPLICAID - - return 0 - - -class DSAdminHarness(DSAdmin, DSAdminTools): - """Harness wrapper around dsadmin. - - Specialize the DSAdmin behavior (No, I don't care about Liskov ;)) - """ - def setupSSL(self, secport, sourcedir=os.environ['SECDIR'], secargs=None): - """Bug scripts requires SECDIR.""" - return DSAdminTools.setupSSL(self, secport, sourcedir, secargs) - - def setupAgreement(self, repoth, args): - """Set default replia credentials """ - args.setdefault('binddn', REPLBINDDN) - args.setdefault('bindpw', REPLBINDPW) - - return DSAdmin.createAgreement(self, repoth, args) - - def setupReplica(self, args): - """Set default replia credentials """ - args.setdefault('binddn', REPLBINDDN) - args.setdefault('bindpw', REPLBINDPW) - # manage a progressive REPLICAID - args.setdefault( - 'id', get_next_replicaid(args.get('id'), args.get('type'))) - return DSAdmin.setupReplica(self, args) - - def setupBindDN(self, binddn=REPLBINDDN, bindpw=REPLBINDPW): - return DSAdmin.setupBindDN(self, binddn, bindpw) - - def setupReplBindDN(self, binddn=REPLBINDDN, bindpw=REPLBINDPW): - return self.setupBindDN(binddn, bindpw) - - def setupBackend(self, suffix, binddn=None, bindpw=None, urls=None, - attrvals=None, benamebase=None, verbose=False): - """Create a backends using the first available cn.""" - # if benamebase is set, try creating without appending - if benamebase: - benum = 0 - else: - benum = 1 - - # figure out what type of be based on args - if binddn and bindpw and urls: # its a chaining be - benamebase = benamebase or "chaindb" - else: # its a ldbm be - benamebase = benamebase or "localdb" - - done = False - while not done: - # if benamebase is set, benum starts at 0 - # and the first attempt tries to create the - # simple benamebase. On failure benum is - # incremented and the suffix is appended - # to the cn - if benum: - benamebase_tmp = benamebase + str(benum) # e.g. localdb1 - else: - benamebase_tmp = benamebase - - try: - cn = DSAdmin.setupBackend(suffix, binddn, bindpw, - urls, attrvals, benamebase, verbose) - done = True - except ldap.ALREADY_EXISTS: - benum += 1 - - return cn - - def createInstance(args): - # eventually set prefix - args.setdefault(SER_DEPLOYED_DIR, os.environ.get('PREFIX', None)) - args.setdefault('sroot', os.environ.get('SERVER_ROOT', None)) - DSAdminTools.createInstance(args) diff --git a/src/lib389/lib389/cli_base/__init__.py b/src/lib389/lib389/cli_base/__init__.py index db2e474..678d221 100644 --- a/src/lib389/lib389/cli_base/__init__.py +++ b/src/lib389/lib389/cli_base/__init__.py @@ -16,16 +16,6 @@ from lib389 import DirSrv from lib389.utils import assert_c, get_ldapurl_from_serverid from lib389.properties import * -MAJOR, MINOR, _, _, _ = sys.version_info - - -# REALLY PYTHON 3? REALLY??? -def _input(msg): - if MAJOR >= 3: - return input(msg) - else: - return input(msg) - def _get_arg(args, msg=None, hidden=False, confirm=False): if args is not None and len(args) > 0: @@ -43,7 +33,7 @@ def _get_arg(args, msg=None, hidden=False, confirm=False): else: return getpass("%s : " % msg) else: - return _input("%s : " % msg) + return input("%s : " % msg) def _get_args(args, kws): @@ -57,7 +47,7 @@ def _get_args(args, kws): if priv: kwargs[kw] = getpass("%s : " % msg) else: - kwargs[kw] = _input("%s : " % msg) + kwargs[kw] = input("%s : " % msg) return kwargs @@ -76,7 +66,7 @@ def _get_attributes(args, attrs): kwargs[attr] = getpass("Enter value for %s : " % attr) else: attr_normal = attr.lower() - kwargs[attr_normal] = _input("Enter value for %s : " % attr) + kwargs[attr_normal] = input("Enter value for %s : " % attr) return kwargs @@ -84,7 +74,7 @@ def _get_attributes(args, attrs): def _warn(data, msg=None): if msg is not None: print("%s :" % msg) - if 'Yes I am sure' != _input("Type 'Yes I am sure' to continue: "): + if 'Yes I am sure' != input("Type 'Yes I am sure' to continue: "): raise Exception("Not sure if want") return data diff --git a/src/lib389/lib389/cli_base/dsrc.py b/src/lib389/lib389/cli_base/dsrc.py index 700ef1e..8fd8364 100644 --- a/src/lib389/lib389/cli_base/dsrc.py +++ b/src/lib389/lib389/cli_base/dsrc.py @@ -87,7 +87,7 @@ def dsrc_to_ldap(path, instance_name, log): log.debug("dsrc path: %s" % path) # First read our config # No such file? - config = configparser.SafeConfigParser() + config = configparser.ConfigParser() config.read([path]) log.debug("dsrc instances: %s" % config.sections()) diff --git a/src/lib389/lib389/instance/setup.py b/src/lib389/lib389/instance/setup.py index 5d2d6aa..9335613 100644 --- a/src/lib389/lib389/instance/setup.py +++ b/src/lib389/lib389/instance/setup.py @@ -442,7 +442,7 @@ class SetupDs(object): return False config = None try: - config = configparser.SafeConfigParser() + config = configparser.ConfigParser() config.read([inf_path]) except Exception as e: self.log.error("Exception %s occured", e) diff --git a/src/lib389/lib389/paths.py b/src/lib389/lib389/paths.py index 5479831..df6334c 100644 --- a/src/lib389/lib389/paths.py +++ b/src/lib389/lib389/paths.py @@ -128,7 +128,7 @@ class Paths(object): def _read_defaults(self): spath = self._get_defaults_loc(DEFAULTS_PATH) - self._config = configparser.SafeConfigParser() + self._config = configparser.ConfigParser() self._config.read([spath]) self._defaults_cached = True diff --git a/src/lib389/lib389/suffix.py b/src/lib389/lib389/suffix.py index 2ea759e..176b6fb 100644 --- a/src/lib389/lib389/suffix.py +++ b/src/lib389/lib389/suffix.py @@ -86,8 +86,6 @@ class Suffix(object): else: return None - raise NotImplemented - def setProperties(self, suffix): ''' Supported properties: diff --git a/src/lib389/lib389/tests/backendLegacy_test.py b/src/lib389/lib389/tests/backendLegacy_test.py index e000850..a442bec 100644 --- a/src/lib389/lib389/tests/backendLegacy_test.py +++ b/src/lib389/lib389/tests/backendLegacy_test.py @@ -108,20 +108,20 @@ def test_list(topology): # In the new api this becomes a get over the selector type # In the case this fails, this actually throws exception ent = topology.standalone.backends.get(NEW_SUFFIX_1) + assert ent is not None topology.standalone.log.info("List suffix (%d): backend %s" % (1, ent.dn)) - assert ent is not None log.info("Check list a backend by its name") ent = topology.standalone.backends.get(NEW_BACKEND_2) + assert ent is not None topology.standalone.log.info("List name (%d): backend %s" % (1, ent.dn)) - assert ent is not None log.info("Check get backends by their DN") ents = topology.standalone.backends.get(dn=ent.dn) - topology.standalone.log.info("List DN (%d): backend %s" % (1, ents.dn)) assert ents is not None + topology.standalone.log.info("List DN (%d): backend %s" % (1, ents.dn)) # The new backends api just does selection on a single attr, and # You would then validate the other attributes on retrival. diff --git a/src/lib389/lib389/tests/backend_test.py b/src/lib389/lib389/tests/backend_test.py index 30a6598..30f9b8d 100644 --- a/src/lib389/lib389/tests/backend_test.py +++ b/src/lib389/lib389/tests/backend_test.py @@ -179,18 +179,18 @@ def test_get_valid(topology_st, backend): log.info("Try to get a backend with backend suffix") d1 = backends.get(NEW_SUFFIX_1_RDN) - topology_st.standalone.log.info("Suffix (%d): backend %s" % (1, d1.dn)) assert d1 is not None + topology_st.standalone.log.info("Suffix (%d): backend %s" % (1, d1.dn)) log.info("Try to get a backend with backend name") d2 = backends.get(BACKEND_NAME_1) - topology_st.standalone.log.info("Backend (%d): backend (%s)" % (1, d2.dn)) assert d2 is not None + topology_st.standalone.log.info("Backend (%d): backend %s" % (1, d2.dn)) log.info("Try to get a backend with backend DN") d3 = backends.get(dn=backend.dn) - topology_st.standalone.log.info("DN (%d): backend %s" % (1, d3.dn)) assert d3 is not None + topology_st.standalone.log.info("DN (%d): backend %s" % (1, d3.dn)) def test_get_invalid(topology_st, backend): diff --git a/src/lib389/lib389/utils.py b/src/lib389/lib389/utils.py index 1fb2ee6..beecd3a 100644 --- a/src/lib389/lib389/utils.py +++ b/src/lib389/lib389/utils.py @@ -54,17 +54,6 @@ from lib389.properties import ( MAJOR, MINOR, _, _, _ = sys.version_info log = logging.getLogger(__name__) -# -# Decorator -# - - -def static_var(varname, value): - def decorate(func): - setattr(func, varname, value) - return func - return decorate - # # Various searches to be used in getEntry