系统环境:centos7
1、关闭SELINUX和防火墙
setenforce 0
修改/etc/selinux/config
SELINUX=disabled
service firewalld stop
chkconfig firewalld off
2、安装软件
yum -y install openldap compat-openldap openldap-clients openldap-servers openldap-servers-sql openldap-devel migrationtools
3、配置
slappasswd -s yourpassword
记住生成的这一段字符串,类似于这样的{SSHA}VDkARCy6V7aJHpF2oq1aJIgRxYVRADre
修改前最好先备份:
cp /etc/openldap/slapd.d/cn\=config/olcDatabase\=\{2\}hdb.ldif /etc/openldap/slapd.d/cn\=config/olcDatabase\=\{2\}hdb.ldif.bak
cp /etc/openldap/slapd.d/cn\=config/olcDatabase\=\{1\}monitor.ldif /etc/openldap/slapd.d/cn\=config/olcDatabase\=\{1\}monitor.ldif.bak
修改后的内容如下:
[root@v77 ~]# cat /etc/openldap/slapd.d/cn=config/olcDatabase\=\{2\}hdb.ldif
# AUTO-GENERATED FILE - DO NOT EDIT!! Use ldapmodify.
# CRC32 fb9f59f5
dn: olcDatabase={2}hdb
objectClass: olcDatabaseConfig
objectClass: olcHdbConfig
olcDatabase: {2}hdb
olcDbDirectory: /var/lib/ldap
olcSuffix: dc=bcdgptvs,dc=com
olcRootDN: cn=Manager,dc=bcdgptvs,dc=com
olcDbIndex: objectClass eq,pres
olcDbIndex: ou,cn,mail,surname,givenname eq,pres,sub
structuralObjectClass: olcHdbConfig
entryUUID: 5a85e3a6-85c8-1038-9c14-757b822e1671
creatorsName: cn=config
createTimestamp: 20181126131012Z
entryCSN: 20181126131012.947412Z#000000#000#000000
modifiersName: cn=config
modifyTimestamp: 20181126131012Z
olcRootPW: {SSHA}VDkARCy6V7aJHpF2oq1aJIgRxYVRADre
这个文件改了域名和加入最后一行,后面的字符串来自于slappasswd -s yourpassword的输出
[root@v77 ~]# cat /etc/openldap/slapd.d/cn=config/olcDatabase\=\{1\}monitor.ldif
# AUTO-GENERATED FILE - DO NOT EDIT!! Use ldapmodify.
# CRC32 5fba0628
dn: olcDatabase={1}monitor
objectClass: olcDatabaseConfig
olcDatabase: {1}monitor
olcAccess: {0}to * by dn.base="gidNumber=0+uidNumber=0,cn=peercred,cn=extern
al,cn=auth" read by dn.base="cn=Manager,dc=bcdgptvs,dc=com" read by * none
structuralObjectClass: olcDatabaseConfig
entryUUID: 5a85ddca-85c8-1038-9c13-757b822e1671
creatorsName: cn=config
createTimestamp: 20181126131012Z
entryCSN: 20181126131012.947262Z#000000#000#000000
modifiersName: cn=config
modifyTimestamp: 20181126131012Z
这个文件改了域名
systemctl start slpad
lsof -i:389
配置OpenLDAP数据库:
cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG
chown ldap:ldap -R /var/lib/ldap
chmod 700 -R /var/lib/ldap
导入基本Schema
ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/cosine.ldif
ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/nis.ldif
ldapadd -Y EXTERNAL -H ldapi:/// -f /etc/openldap/schema/inetorgperson.ldif
修改migrate_common.ph文件
cp /usr/share/migrationtools/migrate_common.ph /usr/share/migrationtools/migrate_common.ph.bak
打开71行开始编辑,关注没有注释的这三行
vim +71 /usr/share/migrationtools/migrate_common.ph
# Default DNS domain
$DEFAULT_MAIL_DOMAIN = "bcdgptvs.com";
# Default base
$DEFAULT_BASE = "dc=bcdgptvs,dc=com";
$EXTENDED_SCHEMA = 1;
先添加系统用户和组,再导入到LDAP
groupadd ldapgroup1
groupadd ldapgroup2
groupadd g1
groupadd g2
useradd -g g1 u1
useradd -g g2 u2
echo '123456' | passwd --stdin u1
echo '123456' | passwd --stdin u2
cat /etc/passwd | grep u1 >> /ldapuser
cat /etc/passwd | grep u2 >> /ldapuser
cat /etc/group | grep g1 >> /ldapgroup
cat /etc/group | grep g2 >> /ldapgroup
cat /ldapgroup /ldapuser
/usr/share/migrationtools/migrate_passwd.pl /ldapuser > /users.ldif
/usr/share/migrationtools/migrate_group.pl /ldapgroup > groups.ldif
准备导入前先配置好openldap基础的数据库,如下:
注意所有dn:的这一行前面必须空出一行,其它行前面不能有空行,为避免出错,后面的配置也遵循这个规则
[root@v77 ~]# cat /base.ldif
dn: dc=bcdgptvs,dc=com
o: bcdgptvs com
dc: bcdgptvs
objectClass: top
objectClass: dcObject
objectclass: organization
dn: cn=Manager,dc=bcdgptvs,dc=com
cn: Manager
objectClass: organizationalRole
description: Directory Manager
dn: ou=People,dc=bcdgptvs,dc=com
ou: People
objectClass: top
objectClass: organizationalUnit
dn: ou=Group,dc=bcdgptvs,dc=com
ou: Group
objectClass: top
objectClass: organizationalUnit
导入ldif格式的用户和组数据及基础的数据库,这里用的是大写的W,输入命令以后再输入密码,如果是小写w,直接在参数后面跟密码,密码就是开始slappasswd -s yourpassword那个
ldapadd -x -W -D "cn=Manager,dc=bcdgptvs,dc=com" -f /base.ldif
ldapadd -x -W -D "cn=Manager,dc=bcdgptvs,dc=com" -f /users.ldif
ldapadd -x -W -D "cn=Manager,dc=bcdgptvs,dc=com" -f /groups.ldif
查看BerkeleyDB数据库文件
[root@v77 ~]# ll /var/lib/ldap/
总用量 484
-rwx------ 1 ldap ldap 2048 11月 26 21:59 alock
-rw------- 1 ldap ldap 8192 11月 26 21:59 cn.bdb
-rwx------ 1 ldap ldap 262144 11月 26 22:59 __db.001
-rwx------ 1 ldap ldap 32768 11月 26 22:59 __db.002
-rwx------ 1 ldap ldap 93592 11月 26 22:59 __db.003
-rwx------ 1 ldap ldap 845 11月 26 21:23 DB_CONFIG
-rwx------ 1 ldap ldap 8192 11月 26 21:59 dn2id.bdb
-rwx------ 1 ldap ldap 32768 11月 26 21:59 id2entry.bdb
-rwx------ 1 ldap ldap 10485760 11月 26 21:59 log.0000000001
-rw------- 1 ldap ldap 8192 11月 26 21:59 mail.bdb
-rw------- 1 ldap ldap 8192 11月 26 21:59 objectClass.bdb
-rw------- 1 ldap ldap 8192 11月 26 21:59 ou.bdb
-rw------- 1 ldap ldap 8192 11月 26 21:59 sn.bdb
把OpenLDAP数据库中的用户添加到用户组,按如下内容编辑文件:
[root@v77 ~]# cat /add_user_to_groups.ldif
dn: cn=g1,ou=Group,dc=bcdgptvs,dc=com
changetype: modify
add: memberuid
memberuid: u1
再添加:
ldapadd -x -W -D "cn=Manager,dc=bcdgptvs,dc=com" -f add_user_to_groups.ldif
查询:
[root@v77 ~]# ldapsearch -x -b "dc=bcdgptvs,dc=com" -H ldap://192.168.156.77
可以看到已经成功添加到组:
开启日志功能:
[root@v77 ~]# cat /loglevel.ldif
dn: cn=config
changetype: modify
replace: olcLogLevel
olcLogLevel: stats
导入重启
ldapmodify -Y EXTERNAL -H ldapi:/// -f /loglevel.ldif
systemctl restart slapd
修改/etc/rsyslog.conf
加入一行:
local4.* /var/log/slapd.log
并重启服务:
systemctl restart rsyslog
这样就能看到日志了
通过LDAP Browser工具查看信息:
4、可以通过 phpldapadmin管理openldap
先安装好这些工具
yum -y install httpd php php-ldap php-gd php-mbstring php-pear php-bcmath php-xml
下载,拷贝,解压,重命名:
wget http://sourceforge.net/projects/phpldapadmin/files/phpldapadmin-php5/1.2.3/phpldapadmin-1.2.3.zip
cp phpldapadmin-1.2.3.zip /tmp/
cd /tmp/
unzip phpldapadmin-1.2.3.zip
mv phpldapadmin-1.2.3 /var/www/html/phpldapadmin
cd /var/www/html/phpldapadmin/config
cp config.php.example config.php
修改config.php,在后面,在?>前添加:
$config->custom->appearance['hide_template_warning'] = true;
$config->custom->appearance['minimalMode'] = true;
$config->custom->appearance['friendly_attrs'] = array(
'facsimileTelephoneNumber' => 'Fax',
'gid' => 'Group',
'mail' => 'Email',
'telephoneNumber' => 'Telephone',
'uid' => 'User Name',
'userPassword' => 'Password'
);
$servers = new Datastore();
$servers->newServer('ldap_pla');
$servers->setValue('server','name','bcdgptvs LDAP Server');
$servers->setValue('appearance','password_hash','');
$servers->setValue('login','attr','dn');
$servers->setValue('server','host','192.168.156.77');
$servers->setValue('server','port',389);
$servers->setValue('server','base',array('dc=bcdgptvs,dc=com'));
$servers->setValue('login','auth_type','session');
$servers->setValue('server','tls',false);
$servers->setValue('unique','attrs',array('uid','sn'));
重启http服务,登录:
大约是这样用的:
5、再将运维工具rundeck集成到LDAP进行认证
测试的rundeck是以war包的方式启动的,进入到与包所在目录的server/config目录中,确认该目录下有
jaas-loginmodule.conf realm.properties等文件,一般rundeck的war包启动过一次就会用
rundeck的配置如下,最后一行是为了保证rundeck原有的用户也能正常登陆
[root@V71 etc]# cat /runtest/server/config/jaas-m2.conf
multiauth {
com.dtolabs.rundeck.jetty.jaas.JettyCachingLdapLoginModule sufficient
debug="true"
contextFactory="com.sun.jndi.ldap.LdapCtxFactory"
providerUrl="ldap://192.168.156.77:389"
bindDn="cn=Manager,dc=bcdgptvs,dc=com"
bindPassword="yourpassword" #此处填写有LDAP查询权限的用户密码,这里用了Manager
authenticationMethod="simple"
forceBindingLogin="true"
userBaseDn="ou=People,dc=bcdgptvs,dc=com"
userRdnAttribute="cn"
userIdAttribute="cn"
userPasswordAttribute="unicodePwd"
userObjectClass="posixAccount"
roleBaseDn="ou=Group,dc=bcdgptvs,dc=com"
roleNameAttribute="cn"
roleUsernameMemberAttribute="member"
roleMemberAttribute="member"
roleObjectClass="groupOfNames"
cacheDurationMillis="300000"
supplementalRoles="user"
nestedGroups="false"
reportStatistics="true";
org.eclipse.jetty.jaas.spi.PropertyFileLoginModule required
debug="true"
file="/runtest/server/config/realm.properties";
};
权限的配置,这里测试的权限放得比较开,抄袭了admin的权限:
[root@V71 etc]# cat /runtest/etc/user.aclpolicy
description: Admin, all access.
context:
project: '.*' # all projects
for:
resource:
- allow: '*' # allow read/create all kinds
adhoc:
- allow: '*' # allow read/running/killing adhoc jobs
job:
- allow: '*' # allow read/write/delete/run/kill of all jobs
node:
- allow: '*' # allow read/run for all nodes
by:
group: user
---
description: Admin, all access.
context:
application: 'rundeck'
for:
resource:
- allow: '*' # allow create of projects
project:
- allow: '*' # allow view/admin of all projects
project_acl:
- allow: '*' # allow admin of all project-level ACL policies
storage:
- allow: '*' # allow read/create/update/delete for all /keys/* storage content
by:
group: user
6、rundeck的启动,用如下参数启动
[root@V71 etc]# java -Drundeck.jaaslogin=true -Dloginmodule.conf.name=jaas-m2.conf -Dloginmodule.name=multiauth -jar rundeck-3.0.7-20181008.war &
登陆测试,用LDAP用户成功登陆:
注销出来,用rundeck的admin用户也能正常登陆: