Switch to unified view

a b/Allura/ldap-setup.py
1
#!/usr/bin/env python
2
import os
3
import shutil
4
import string
5
from contextlib import contextmanager
6
from tempfile import mkstemp
7
from ConfigParser import ConfigParser, NoOptionError
8
9
config = ConfigParser()
10
11
def main():
12
    config.read('.setup-scm-cache')
13
    if not config.has_section('scm'):
14
        config.add_section('scm')
15
    suffix = get_value('suffix', 'dc=localdomain')
16
    secret = get_value('admin password', 'secret')
17
    firstdc = suffix.split(',')[0].split('=')[1]
18
    if get_value('clear ldap config', 'y') == 'y':
19
        run('apt-get -f install')
20
        run('apt-get remove --purge slapd ldap-utils')
21
        run('apt-get install slapd ldap-utils')
22
    if get_value('start slapd', 'y') == 'y':
23
        run('service slapd start')
24
    if get_value('add base ldap schemas', 'y') == 'y':
25
        run('ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/ldap/schema/cosine.ldif')
26
        run('ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/ldap/schema/nis.ldif')
27
        run('ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/ldap/schema/inetorgperson.ldif')
28
    if get_value('add backend ldif', 'y') == 'y':
29
        with tempfile(backend_ldif, locals()) as name:
30
            run('ldapadd -Y EXTERNAL -H ldapi:/// -f %s' % name)
31
    with open('/etc/ldap.secret', 'w') as fp:
32
        fp.write(secret)
33
    os.chmod('/etc/ldap.secret', 0400)
34
    if get_value('add frontend ldif', 'y') == 'y':
35
        with tempfile(frontend_ldif, locals()) as name:
36
            run('ldapadd -c -x -D cn=admin,%s -W -f %s -y /etc/ldap.secret' % (suffix, name))
37
    if get_value('add initial user/group', 'y') == 'y':
38
        with tempfile(initial_user_ldif, locals()) as name:
39
            run('ldapadd -c -x -D cn=admin,%s -W -f %s -y /etc/ldap.secret' % (suffix, name))
40
    if get_value('setup ldap auth', 'y') == 'y':
41
        run('apt-get install libnss-ldap')
42
        run('dpkg-reconfigure ldap-auth-config')
43
        run('auth-client-config -t nss -p lac_ldap')
44
        run('pam-auth-update')
45
    if get_value('setup ldapscripts', 'y') == 'y':
46
        run('apt-get install ldapscripts')
47
        with tempfile(ldapscripts_conf, locals()) as name:
48
            shutil.copy(name, '/etc/ldapscripts/ldapscripts.conf')
49
        print 'writing passwd'
50
        with open('/etc/ldapscripts/ldapscripts.passwd', 'w') as fp:
51
            fp.write(secret)
52
        os.chmod('/etc/ldapscripts/ldapscripts.passwd', 0400)
53
        print 'writing runtime'
54
        with open('/usr/share/ldapscripts/runtime.debian', 'w') as fp:
55
            fp.write(ldapscripts_debian)
56
57
def get_value(key, default):
58
    try:
59
        default = config.get('scm', key)
60
    except NoOptionError:
61
        pass
62
    value = raw_input('%s? [%s]' % (key, default))
63
    if not value: value = default
64
    config.set('scm', key, value)
65
    with open('.setup-scm-cache', 'w') as fp:
66
        config.write(fp)
67
    return value
68
69
def run(command):
70
    rc = os.system(command)
71
    if rc != 0:
72
        print 'Error running %s' % command
73
    assert rc == 0
74
    return rc
75
76
@contextmanager
77
def tempfile(template, values):
78
    fd, name = mkstemp()
79
    os.write(fd, template.safe_substitute(values))
80
    os.close(fd)
81
    yield name
82
    os.remove(name)
83
84
backend_ldif=string.Template('''
85
# Load dynamic backend modules
86
dn: cn=module,cn=config
87
objectClass: olcModuleList
88
cn: module
89
olcModulepath: /usr/lib/ldap
90
olcModuleload: back_hdb
91
92
# Database settings
93
dn: olcDatabase=hdb,cn=config
94
objectClass: olcDatabaseConfig
95
objectClass: olcHdbConfig
96
olcDatabase: {1}hdb
97
olcSuffix: $suffix
98
olcDbDirectory: /var/lib/ldap
99
olcRootDN: cn=admin,$suffix
100
olcRootPW: $secret
101
olcDbConfig: set_cachesize 0 2097152 0
102
olcDbConfig: set_lk_max_objects 1500
103
olcDbConfig: set_lk_max_locks 1500
104
olcDbConfig: set_lk_max_lockers 1500
105
olcDbIndex: objectClass eq
106
olcLastMod: TRUE
107
olcDbCheckpoint: 512 30
108
olcAccess: to attrs=userPassword by dn="cn=admin,$suffix" write by anonymous auth by self write by * none
109
olcAccess: to attrs=shadowLastChange by self write by * read
110
olcAccess: to dn.base="" by * read
111
olcAccess: to * by dn="cn=admin,$suffix" write by * read
112
113
''')
114
115
frontend_ldif=string.Template('''
116
# Create top-level object in domain
117
dn: $suffix
118
objectClass: top
119
objectClass: dcObject
120
objectclass: organization
121
o: Example Organization
122
dc: $firstdc
123
description: LDAP Example
124
125
# Create max uid generator
126
dn: cn=maxUid,$suffix
127
objectClass: extensibleObject
128
objectClass: top
129
uidNumber: 10000
130
131
# Admin user.
132
dn: cn=admin,$suffix
133
objectClass: simpleSecurityObject
134
objectClass: organizationalRole
135
cn: admin
136
description: LDAP administrator
137
userPassword: $secret
138
139
dn: ou=people,$suffix
140
objectClass: organizationalUnit
141
ou: people
142
143
dn: ou=groups,$suffix
144
objectClass: organizationalUnit
145
ou: groups
146
''')
147
148
initial_user_ldif=string.Template('''
149
dn: uid=john,ou=people,$suffix
150
objectClass: inetOrgPerson
151
objectClass: posixAccount
152
objectClass: shadowAccount
153
uid: john
154
sn: Doe
155
givenName: John
156
cn: John Doe
157
displayName: John Doe
158
uidNumber: 1000
159
gidNumber: 10000
160
userPassword: password
161
gecos: John Doe
162
loginShell: /bin/bash
163
homeDirectory: /home/john
164
shadowExpire: -1
165
shadowFlag: 0
166
shadowWarning: 7
167
shadowMin: 8
168
shadowMax: 999999
169
shadowLastChange: 10877
170
mail: john.doe@example.com
171
postalCode: 31000
172
l: Toulouse
173
o: Example
174
mobile: +33 (0)6 xx xx xx xx
175
homePhone: +33 (0)5 xx xx xx xx
176
title: System Administrator
177
postalAddress:
178
initials: JD
179
180
dn: cn=example,ou=groups,$suffix
181
objectClass: posixGroup
182
cn: example
183
gidNumber: 10000
184
''')
185
186
open_ldap_config=string.Template('''
187
[open_ldap]
188
nss_passwd=passwd: files ldap
189
nss_group=group: files ldap
190
nss_shadow=shadow: files ldap
191
nss_netgroup=netgroup: files ldap
192
pam_auth=auth       required     pam_env.so
193
        auth       sufficient   pam_unix.so likeauth nullok
194
#the following line (containing pam_group.so) must be placed before pam_ldap.so
195
#for ldap users to be placed in local groups such as fuse, plugdev, scanner, etc ...
196
        auth       required     pam_group.so use_first_pass
197
        auth       sufficient   pam_ldap.so use_first_pass
198
        auth       required     pam_deny.so
199
pam_account=account    sufficient   pam_unix.so
200
        account    sufficient   pam_ldap.so
201
        account    required     pam_deny.so
202
pam_password=password   sufficient   pam_unix.so nullok md5 shadow
203
        password   sufficient   pam_ldap.so use_first_pass
204
        password   required     pam_deny.so
205
pam_session=session    required     pam_limits.so
206
        session    required     pam_mkhomedir.so skel=/etc/skel/
207
        session    required     pam_unix.so
208
        session    optional     pam_ldap.so
209
''')
210
211
ldapscripts_conf=string.Template('''
212
SERVER=127.0.0.1
213
BINDDN='cn=admin,$suffix'
214
BINDPWDFILE="/etc/ldapscripts/ldapscripts.passwd"
215
SUFFIX='$suffix'
216
GSUFFIX='ou=Groups'
217
USUFFIX='ou=People'
218
MSUFFIX='ou=Computers'
219
GIDSTART=10000
220
UIDSTART=10000
221
MIDSTART=10000
222
''')
223
224
225
ldapscripts_debian='''
226
### Allura-customized
227
### This file predefine some ldapscripts variables for Debian boxes.
228
#
229
#  Copyright (c) 2005 Ganal LAPLANCHE - Linagora
230
#  Copyright (c) 2005-2007 Pierre Habouzit
231
#  Copyright (c) 2009 Alexander GQ Gerasiov
232
#
233
#  This program is free software; you can redistribute it and/or
234
#  modify it under the terms of the GNU General Public License
235
#  as published by the Free Software Foundation; either version 2
236
#  of the License, or (at your option) any later version.
237
#
238
#  This program is distributed in the hope that it will be useful,
239
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
240
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
241
#  GNU General Public License for more details.
242
#
243
#  You should have received a copy of the GNU General Public License
244
#  along with this program; if not, write to the Free Software
245
#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
246
#  USA.
247
248
##### Beginning of ldapscripts configuration #####
249
250
getfield() {
251
    local field="$1"
252
    local nssconffile='/etc/libnss-ldap.conf'
253
    if [ -f "$nssconffile" ];then
254
  local value=$(awk "/^\s*$field/ {print \$2}" /etc/libnss-ldap.conf)
255
    else
256
  local value="$2"
257
    fi
258
    echo ${value:-$2}
259
}
260
261
getsuffix() {
262
    field="$1"
263
    value="$(getfield "$1" | sed -e "s/,.*$//")"
264
    echo ${value:-$2}
265
}
266
267
# LDAP Configuration
268
SERVER=$(getfield uri "$(getfield host '')")
269
BINDDN=$(getfield rootbinddn '')
270
if [ -f /etc/libnss-ldap.secret ];then
271
  BINDPWDFILE=/etc/libnss-ldap.secret
272
elif [ -f /etc/ldap.secret ];then
273
  BINDPWDFILE=/etc/ldap.secret
274
fi
275
276
SUFFIX=`getfield base`
277
GSUFFIX=`getsuffix nss_base_group   'ou=Group'`
278
USUFFIX=`getsuffix nss_base_passwd  'ou=People'`
279
MSUFFIX=`getsuffix nss_base_hosts   'ou=Hosts'`
280
281
# User properties
282
[ -f /etc/adduser.conf ] && . /etc/adduser.conf
283
USHELL=${DSHELL:-"/bin/bash"}
284
UHOMES=${DHOME:-"/home"}"/%u"
285
HOMESKEL=${SKEL:-"/etc/skel"}
286
HOMEPERMS=${DIR_MODE:-"0755"}
287
288
289
# Where to log
290
LOGFILE="/var/log/ldapscripts.log"
291
292
# Various binaries used within scripts
293
LDAPSEARCHBIN=`which ldapsearch`
294
LDAPADDBIN=`which ldapadd`
295
LDAPDELETEBIN=`which ldapdelete`
296
LDAPMODIFYBIN=`which ldapmodify`
297
LDAPMODRDNBIN=`which ldapmodrdn`
298
LDAPPASSWDBIN=`which ldappasswd`
299
300
# Getent command to use - choose the ones used on your system. Leave blank or comment for auto-guess.
301
# GNU/Linux
302
GETENTPWCMD="getent passwd"
303
GETENTGRCMD="getent group"
304
305
306
TMPDIR="/tmp"
307
##### End of configuration #####
308
'''
309
if __name__ == '__main__':
310
    main()