#
# OpenLDAP directory server instance class
#
# Copyright (c) 2015 Red Hat, Inc.
# Author: Nikolai Kondrashov <Nikolai.Kondrashov@redhat.com>
#
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 only
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import hashlib
import base64
import urllib
import time
import ldap
import os
import errno
import signal
import shutil
import sys
from util import *
"""Generate userPassword value for a password."""
"""OpenLDAP directory server instance."""
"""
Initialize the instance.
Arguments:
dir Path to the root of the filesystem hierarchy to create
the instance under.
port TCP port on localhost to bind the server to.
base_dn Base DN.
admin_rdn Administrator DN, relative to BASE_DN.
admin_pw Administrator password.
"""
"""Setup the instance initial configuration."""
#
# Add configuration
#
dn: cn=config
objectClass: olcGlobal
cn: config
olcPidFile: {self.pid_path}
olcArgsFile: {args_file}
# Read slapd.conf(5) for possible values
olcLogLevel: none
# Frontend settings
dn: olcDatabase={{-1}}frontend,cn=config
objectClass: olcDatabaseConfig
objectClass: olcFrontendConfig
olcDatabase: {{-1}}frontend
# The maximum number of entries that is returned for
# a search operation
olcSizeLimit: 500
# Allow unlimited access to local connection from the local root
olcAccess: {{0}}to * by dn.exact=gidNumber={gid}+uidNumber={uid},
cn=peercred,cn=external,cn=auth manage by * break
# Allow unauthenticated read access for schema and
# base DN autodiscovery
olcAccess: {{1}}to dn.exact="" by * read
olcAccess: {{2}}to dn.base="cn=Subschema" by * read
# Config db settings
dn: olcDatabase=config,cn=config
objectClass: olcDatabaseConfig
olcDatabase: config
# Allow unlimited access to local connection from the local root
olcAccess: to * by dn.exact=gidNumber={gid}+uidNumber={uid},
cn=peercred,cn=external,cn=auth manage by * break
olcRootDN: {self.admin_rdn},cn=config
olcRootPW: {admin_pw_hash}
# Load schemas
dn: cn=schema,cn=config
objectClass: olcSchemaConfig
cn: schema
include: file://{dist_conf_dir}/schema/cosine.ldif
include: file://{dist_conf_dir}/schema/inetorgperson.ldif
# Load module
dn: cn=module{{0}},cn=config
objectClass: olcModuleList
cn: module{{0}}
olcModulePath: {dist_lib_dir}
olcModuleLoad: back_hdb
# Set defaults for the backend
dn: olcBackend=hdb,cn=config
objectClass: olcBackendConfig
olcBackend: hdb
# The database definition.
dn: olcDatabase=hdb,cn=config
objectClass: olcDatabaseConfig
objectClass: olcHdbConfig
olcDatabase: hdb
olcDbCheckpoint: 512 30
olcLastMod: TRUE
olcSuffix: {self.base_dn}
olcDbDirectory: {self.data_dir}
olcRootDN: {self.admin_dn}
olcRootPW: {admin_pw_hash}
olcDbIndex: objectClass eq
olcDbIndex: cn,uid eq
olcDbIndex: uidNumber,gidNumber eq
olcDbIndex: member,memberUid eq
olcAccess: to attrs=userPassword,shadowLastChange
by self write
by anonymous auth
by * none
olcAccess: to dn.base="" by * read
olcAccess: to *
by * read
)
raise Exception("Failed to add configuration with slapadd")
#
# Add database config (example from distribution)
#
# One 0.25 GB cache
set_cachesize 0 268435456 1
# Transaction Log settings
set_lg_regionmax 262144
set_lg_bsize 2097152
""")
"""Setup the instance."""
#
# Setup initial configuration
#
#
# Start the daemon
#
raise Exception("Failed to start slapd")
#
# Wait until it is available
#
attempt = 0
while True:
try:
break
except ldap.SERVER_DOWN:
pass
if attempt > 30:
raise Exception("Failed to start slapd")
#
# Relax requirement of member attribute presence in groupOfNames
#
modlist = [
"{7}( 2.5.6.9 NAME 'groupOfNames' "
"DESC 'RFC2256: a group of names (DNs)' SUP top "
"STRUCTURAL MUST ( member $ cn ) MAY ( businessCategory $ "
"seeAlso $ owner $ ou $ o $ description ) )"),
"{7}( 2.5.6.9 NAME 'groupOfNames' "
"DESC 'RFC2256: a group of names (DNs)' SUP top "
"STRUCTURAL MUST ( cn ) MAY ( member $ businessCategory $ "
"seeAlso $ owner $ ou $ o $ description ) )"),
]
#
# Add data
#
("objectClass", ["dcObject", "organization"]),
("o", "Example Company"),
])
("objectClass", "organizationalRole"),
])
("objectClass", ["top", "organizationalUnit"]),
])
"""Teardown the instance."""
# Wait for slapd to stop
try:
try:
finally:
attempt = 0
if attempt > 30:
raise Exception("Failed to stop slapd")
except IOError as e:
raise