OpenLDAP automatisch installieren und einrichten

Ich habe heu­te ein Script geschrie­ben, wel­ches OpenLDAP auf Debi­an­sys­te­men auto­ma­tisch instal­lie­ren und auch gleich für ppo­li­cy kon­fi­gu­rie­ren kann. Wer sowas schon­mal von Hand gemacht hat, weiß um die Schwie­rig­kei­ten. Es soll­te auf den meis­ten Debi­an­de­ri­va­ten funk­tio­nie­ren (ent­wi­ckelt habe ich auf stretch), ist aber leicht anpass­bar, da ich alle Ein­zel­schrit­te in Funk­tio­nen gepackt habe.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/bin/bash
 
# globals
LDAPDB="mdb"
PASSWORD="test1234"
HOSTNAME=`hostname`
DOMAIN=`echo  $HOSTNAME | awk -v FS="." '{print $1}'`
TLD=`echo  $HOSTNAME | awk -v FS="." '{print $2}'`
PPOLICY_FILE="/etc/ldap/schema/ppolicy.ldif"
LOGFILE="debug.txt"
 
# basesetup()
# Installs slapd (openLDAP) unattended
# using debconf
 
basesetup() {
 
	PASS=$1
        HOST=$2
	DBTYPE=$3
 
	echo "Building LDAP-Roottree ...\n"
 
	export DEBIAN_FRONTEND=noninteractive
	echo -e " \
		slapd    slapd/internal/generated_adminpw    password   $PASS
		slapd    slapd/password2    password    $PASS
		slapd    slapd/internal/adminpw    password   $PASS
		slapd    slapd/password1    password    $PASS
		slapd	 slapd/backend: string	$DBTYPE
		slapd	 slapd/domain	string	$HOST
	" | debconf-set-selections
 
	apt-get install -y slapd ldap-utils
 
}
 
make_index() {
 
	echo "Adding index ...\n"
 
        echo -e " \
dn: olcDatabase={1}$LDAPDB,cn=config
changetype: modify
add: olcDbIndex
olcDbIndex: mail,givenName eq,subinitial
        " | ldapmodify -Y EXTERNAL -H ldapi:///
 
}
 
# configure_policy()
# installs: 	ppolicy-scheme
# 		ppolicy-module
# 		overlay
# 		ppolicycontext
# 		defaultpolicy
 
configure_policy() {
 
        echo "Setting temporary ACLs ..."
 
        echo -e " \
dn: olcDatabase={1}$LDAPDB,cn=config
changetype: modify
replace: olcAccess
olcAccess: {0}to * by dn.base="gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth" write by * none
        " | ldapmodify -Y EXTERNAL -H ldapi:///
 
	echo "Adding ppolicy-scheme ..."
 
	ldapadd -Q -Y EXTERNAL -H ldapi:/// -f $PPOLICY_FILE
 
        echo "Activating ppolicy-module ..."
 
        echo -e " \
dn: cn=module{0},cn=config
changetype: modify
add: olcModuleLoad
olcModuleLoad: ppolicy.la
        " | ldapmodify -Y EXTERNAL -H ldapi:///
 
	/etc/init.d/slapd restart
 
        echo "Generating ppolicy-context ..."
 
        echo -e " \
dn: ou=policies,dc=$DOMAIN,dc=$TLD
objectClass: organizationalUnit
objectClass: top
ou: policies
        " | ldapadd -Q -Y EXTERNAL -H ldapi:///
 
        echo "Setting default policy ..."
 
        echo -e " \
dn: cn=default,ou=policies,dc=$DOMAIN,dc=$TLD
objectClass: top
objectClass: person
objectClass: pwdPolicy
cn: default
sn: default
pwdAllowUserChange: TRUE
# this don't work though documentation says it should
# pwdAttribute: userPassword
# So we use OID for workaround
pwdAttribute: 2.5.4.35
pwdInhistory: 3
pwdLockout: TRUE
pwdLockoutDuration: 1800
pwdMaxAge: 0
pwdMaxFailure: 3
pwdMinLength: 6
pwdMustChange: TRUE
pwdSafeModify: TRUE
# comment for syntax reason (trailing TAB here leads to syntax error when importing)
        " | ldapadd -Q -Y EXTERNAL -H ldapi:///
 
        echo "Generating overlay ..."
 
        echo -e " \
dn: olcOverlay=ppolicy,olcDatabase={1}$LDAPDB,cn=config
objectClass: olcOverlayConfig
objectClass: olcPPolicyConfig
olcOverlay: ppolicy
olcPPolicyDefault: cn=default,ou=policies,dc=$DOMAIN,dc=$TLD
olcPPolicyHashCleartext: FALSE
olcPPolicyUseLockout: FALSE
olcPPolicyForwardUpdates: FALSE
# comment for syntax reason (trailing TAB here leads to syntax error when importing)
        " | ldapadd -Q -Y EXTERNAL -H ldapi:///
 
}
 
# configure_tls()
# does:
#	generating of cert-authority
#	generating of certs for slapd
#	configuring of slapd for using tls
 
configure_tls() {
 
	echo
 
}
 
# toggle_acl()
# sets ACL back to save values after install
 
toggle_acl() {
 
	echo
 
}
 
# debug_output()
# dumps to file:
#	debconf values for slapd
#	complete Root-DN
#	complete cn=config
 
debug_output() {
 
	debconf-show slapd > $LOGFILE
	slapcat >> $LOGFILE
	ldapsearch -Y EXTERNAL -H ldapi:/// -b cn=config >> $LOGFILE
 
}
 
 
# cleanldap()
# Removes all of openLDAP
 
cleanldap() {
 
	apt-get remove -y slapd ldap-utils --purge
 
}
 
# main
 
basesetup $PASSWORD $HOSTNAME $LDAPDB
make_index
configure_policy
debug_output
cleanldap

Eigent­lich muss man oben nur ein ande­res Pass­wort set­zen und am Ende das „cle­anldap“ aus­kom­men­tie­ren. Da das Gan­ze noch „Work in Pro­gress“ ist, feh­len noch eini­ge Funktionen:

  1. Kon­fi­gu­ra­ti­on für TLS
  2. Inte­gra­ti­on des freeradius-Schemas
  3. Ver­nünf­ti­ge ACLs nach Abschluss der Instal­la­ti­on setzen

Ja, ich ste­he in die­sem Jahr ziem­lich auf LDAP …

Facebook Like

2 Kommentare

  • phihos

    Hey, vie­len Dank für das Script :-)
    In der Funk­ti­on „debug_output“ hat sich ein klei­ner Feh­ler ein­ge­schli­chen: Die „>“-Zei­chen wur­den durch ihr HTML-Pen­dant „>“ ersetzt.

  • Dan­ke für den Hin­weis. Der Syn­tax-High­ligh­ter escaped anschei­nend die­se Zei­chen der­ge­stalt … Wohl aus Sicherheitsgründen.

Schreibe einen Kommentar zu Maik Riecken Antworten abbrechen

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert