资源描述
用mysql作后台数据库安装openldap
openldap是一个基于x.500协议用来集中认证和目录搜索的级目录访问服务器。这篇文 章包含的内容是:利用sql服务来配置这个服务器 ,用来保存对象和数据。允许第三方应用访问、管理这些保存在SQL数据库里的对象。
需要的ports有:
MySQL 4.x server : /usr/ports/databases/mysql41-server
MySQL 4.x client : /usr/ports/databases/mysql41-client
LibIODBC 3.x : /usr/ports/databases/libiodbc
MyODBC 3.x : /usr/ports/databases/myodbc
OpenLDAP 2.x : /usr/ports/databases/openldap21-server WITH_ODBC="YES"
配置mysql服务器
可选很多不同种类的数据库。在这种情况下,我将使用mysql。要完成这个任务的第一步是建立一个openldap将要使用的mysql数据库
root@host # mysqladmin create ldap
下面我将建立一个openldap会用的mysql帐号,对应我们新建立的ldap数据库。
root@host # mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10 to server version: 4.0.18
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql>;grant all privileges on ldap.* to 'ldap'@'localhost'
->;identified by 'password' with grant option;
Query OK, 0 rows affected (0.13 sec)
配置LibODBC使用MyODBC驱动,事实上我们只需要简单得修改这里的两个文件就可让LibODBC使用MyODBC驱动来访问 MySQL服务器。
修改配置文件:vi /etc/odbcinst.ini
[ODBC Drivers]
MySQL = Installed
[MySQL]
Description=ODBC for MySQL
Driver=/usr/local/lib/libmyodbc3.so
看看这个文件/usr/local/etc/libiodbc/odbc.ini并作如下修改:
[ODBC Data Sources]
ldap = MySQL LDAP DSN
[ldap]
Driver = /usr/local/lib/libmyodbc3.so
Description = OpenLDAP Database
Host = localhost
ServerType = MySQL
Port = 3306
FetchBufferSize = 99
User = ldap
Password = password
Database = ldap
ReadOnly = no
Socket = /tmp/mysql.sock
[ODBC]
InstallDir=/usr/local/lib
再次,为我们在mysql数据库建立的ldap用户的密码。
在安装配置openldap以前,我们可以测试我们当前的配置。LibIODBC提供了检测 DSN配置的测试工具。
darxpryte提示:在这篇指南的下面,我发现iodbctest没有自动建立,以后或许 会修正这个问题,不过如果你需要如此的话,按照修面的操作:
cd /usr/ports/databases/libiodbc/
make extract
cd work/libiodbc-3.52.2/samples
make install
这将把iodbctest安装到/usr/local/bin/,一旦你安装了iodbctest,你就可以安装下面的方法来测试你的连接:
root@host # iodbctest
iODBC Demonstration program
This program shows an interactive SQL processor
Driver Manager: 03.51.0001.0908
Enter ODBC connect string (? shows list): ?
DSN | Description
---------------------------------------------------------------
ldap | MySQL LDAP DSN
Enter ODBC connect string (? shows list):DSN=ldap
Driver: 03.51.06
SQL>;show tables;
Tables_in_ldap
---------------------
authors_docs
documents
institutes
ldap_attr_mappings
ldap_entries
ldap_entry_objclasses
ldap_oc_mappings
ldap_referrals
persons
phones
result set 1 returned 10 rows.
这表示,DSN已经为LibIODBC配置好使用MyODBC驱动,好用来连接到我们在 mysql服务器上安装的ldap数据库。如果你通过测试程序时有问题(显示定义在odbc.ini里面的DSN名字),尝试输入下面的 shell环境变量:
export ODBCINI=/usr/local/etc/libiodbc/odbc.ini
配置openldap使用mysql
在编译openldap的时候,我们需要跳过 WITH_ODBC="YES"选项,这样,服 务器编译专用的sql配置。在安装过程完毕后,我们将复制使用SQL作后台的slapd.conf文件,这个文件在 openldap的ports目录的下面的路径中生成:
work/openldap-2.1.30/servers/slapd/back-sql/rdbms_depend/mysql
Change to this directory, from the ports directory of OpenLDAP, and copy the configuration file over
>; cp slapd.conf /usr/local/etc/openldap
Then we can import the back SQL file from this directory into our running MySQL server database
root@host # mysql < backsql_create.sql ldap
root@host # mysql < testdb_create.sql ldap
Optionally we can import the testdb_data and testdb_metadata files into the database so that we can have example data with which to work
Next we need to edit the /usr/local/etc/openldap/slapd.conf file and make the protper adjustments. We need to setup the slapd service to use a SQL backend under the "SQL database definitions" section
database sql
suffix "o=sql,c=RU"
rootdn "cn=root,o=sql,c=RU"
rootpw secret
dbname ldap
dbuser ldap
dbpasswd password
subtree_cond "ldap_entries.dn LIKE CONCAT('%',?)"
insentry_query "INSERT INTO ldap_entries (dn,oc_map_id,parent,keyval) VALUES (?,?,?,?)"
Go ahead and comment out or delete any other example configurations for alternate SQL connectors such as Postgres and/or MsSQL settings. (Unless of course you are using a Postgres or MsSQL server as your backend
Post installation configuration
Next, we need to edit the /etc/rc.conf and configure the OpenLDAP server to star on boot by making the following changes
slapd_enable="YES"
slapd_flags='-h "ldapi://%2fvar%2frun%2fopenldap%2fldapi/ ldap://0.0.0.0/"'
slapd_sockets="/var/run/openldap/ldapi"
And finally we need to edit the OpenLDAP startup script and setup the ODBC path for the server to use. Edit /etc/rc.d/slapd file and add the following line:
export ODBCINI=/usr/local/etc/libiodbc/odbc.ini
Just as we performed the iodbctest, this variable is essential for OpenLDAP to know where the configuration file to use for ODBC connectivity
Now we are ready to try and bring up our OpenLDAP server. Let us start by running slapd manually in debug mode to see the output of startup:
root@host # /usr/local/libexec/slapd -d 1
We should see the following at the end of the debug output:
<==load_schema_map()
<==backsql_get_db_conn()
==>;backsql_free_db_conn()
backsql_free_db_conn(): closing db connection
==>;backsql_close_db_conn()
<==backsql_close_db_conn()
<==backsql_free_db_conn()
<==backsql_db_open(): test succeeded, schema map loaded
slapd starting
If this is the given output then it looks like our configuration is correct and we are ready to start up OpenLDAP normally for operation.
/etc/rc.d/slapd start
This will startup the OpenLDAP server and we can verify it is running with the following command:
root@host # sockstat |grep slapd
ldap slapd 71838 5 dgram -< /var/run/log
ldap slapd 71838 8 stream /var/run/openldap/ldapi
ldap slapd 71838 9 tcp4 *:389 *:*
From here, use any OpenLDAP Administration tool of your choice to add, edit and remove data from your LDAP server
参考资料::
OpenLdap与BerkeleyDB安装过程
一、下载 openldap-2.4.13.tar.gz和db-4.6.21.tar.gz
二、安装db-4.6.21.tar(先删除系统自带的db包rpm -qa|grep db)
1.tar -zxvf db-4.6.21.tar.gz
2.cd db-4.6.21/build_unix
3. ../dist/configure --prefix=/usr/local/BerkeleyDB
4.make
5.make install
6.vi /etc/ld.so.conf 加入下面一行
/usr/local/BerkeleyDB/lib
7.ldconfig
三、安装openldap-2.4.13.tar.gz
1.tar -zxvf openldap-2.4.13.tar.gz
2.cd openldap-2.4.13
3.
export CPPFLAGS="-I/usr/local/BerkeleyDB/include"
export LDFLAGS="-L/usr/local/BerkeleyDB/lib"
export LD_LIBRARY_PATH="/home/Ldap/db-4.6.21/build_unix/.libs"
./configure --prefix=/usr/local/openldap --enable-ldbm (手动输入)
如果报错:
checking Berkeley DB version for BDB/HDB backends... no
configure: error: BDB/HDB: BerkeleyDB version incompatible
解决方法:
把/usr/local/BerkeleyDB.4.7/include中的所有文件拷贝到/usr/include
/usr/local/BerkeleyDB.4.7/lib中的所有文件拷贝到/usr/lib
4.make depend
5.make
如果提示错误:
make[3]: *** [init.lo] Error 1
make[3]: Leaving directory `/work/ceno/lab/openldap-2.4.11/servers/slapd/back-bdb'
make[2]: *** [.backend] Error 1
make[2]: Leaving directory `/work/ceno/lab/openldap-2.4.11/servers/slapd'
make[1]: *** [all-common] Error 1
make[1]: Leaving directory `/work/ceno/lab/openldap-2.4.11/servers'
make: *** [all-common] Error 1
解决方法:
cd openldap-2.4.11/servers/slapd/back-bdb/
将init.c 和 cache.c中
#if DB_VERSION_FULL >= 0x04060012
改为
#if 0 && DB_VERSION_FULL >= 0x04060012
之后再make 就能通过了。
6.make test
7.make install
四、配置文件
1.vi etc/openldap/slapd.conf
include /usr/local/openldap/etc/openldap/schema/core.schema #设置schema配置文档包含
pidfile /usr/local/openldap/var/slapd.pid
argsfile /usr/local/openldap/var/slapd.args #设置pid和args文档位置
database bdb #设置使用的资料库,也可lbdm。
suffix "dc=my-domain,dc=com" #设置目录后缀
rootdn "cn=Manager,dc=my-domain,dc=com" #设置目录管理员
# Cleartext passwords, especially for the rootdn, should
# be avoid. See slappasswd(8) and slapd.conf(5) for details.
# Use of strong authentication encouraged.
rootpw secret #设置管理密码,这里用了明文的“secret”密码。这样设置不安全,需使用加密的密码,下面会讲到如何设置加密密码。
# The database directory MUST exist prior to running slapd AND
# should only be accessible by the slapd and slap tools.
# Mode 700 recommended.
directory /usr/local/openldap/var/openldap-data #设置资料库路径
# Indices to maintain
index objectClass eq #设置目录项索引
2. ./slapd启动服务
3.vi etc/openldap/ldap.conf
# $OpenLDAP: pkg/ldap/libraries/libldap/ldap.conf,v 1.9 2000/09/04 19:57:01 kurt Exp $
#
# LDAP Defaults
#
# See ldap.conf(5) for details
# This file should be world readable but not world writable.
BASE dc=it, dc=com设置目录起点
#URI ldap:// ldap://ldap-:666
#SIZELIMIT 12
#TIMELIMIT 15
#DEREF never
OK!
初始化OpenLDAP
(一).OpenLDAP的基本配置
1.设置LDAP使用的Schema
Schema(模式)定义了LDAP中的对象类型、属性、语法和匹配规则等,如用户的电子邮件、联系地址和联系电话等属性,它类似于关系数据库中的表结构。
找到语句:
include /usr/local/openldap/etc/openldap/schema/core.schema
在该语句的后面添加以下语句。
include /usr/local/openldap/etc/openldap/schema/corba.schema
include /usr/local/openldap/etc/openldap/schema/cosine.schema
include /usr/local/openldap/etc/openldap/schema/dyngroup.schema
include /usr/local/openldap/etc/openldap/schema/inetorgperson.schema
include /usr/local/openldap/etc/openldap/schema/java.schema
include /usr/local/openldap/etc/openldap/schema/misc.schema
include /usr/local/openldap/etc/openldap/schema/nis.schema
include /usr/local/openldap/etc/openldap/schema/openldap.schema
2.设置目录树的后缀
找到语句:
suffix "dc=my-domain,dc=com"
将其改为:
suffix "dc=example,dc=com"
3.该语句设置LDAP管理员的DN
找到语句:
rootdn "cn=Manager,dc=my-domain,dc=com"
将其改为:
rootdn "cn=Manager,dc=example,dc=com"
4.设置LDAP管理员的口令
找到语句:
rootpw secret
将其改为:
rootpw {SSHA}NXV9Fl28qCHMmA6P sjhVX0uejTKE6OYr
(二).启动OpenLDAP服务器
启动OpenLDAP服务器,应执行以下命令:
/usr/local/openldap/libexec/slapd
为了确保slapd进程已经启动,应执行以下命令:
pstree|grep "slapd"
如果出现“|-slapd”,则表示slapd进程已经成功启动
(三).建立初始化数据
① 建立LDIF文件。使用vi等文本编辑工具建立名为example.ldif的文件,内容如下:
dn:dc=example,dc=com
objectclass:dcObject
objectclass:organization
o:Example, Inc.
dc:example
dn:cn=Manager,dc=example,dc=com
objectclass:organizationalRole
cn:Manager
② 执行以下命令导入数据。
/usr/local/openldap/bin/ldapadd -x -W -D "cn=Manager,dc=example,dc=com" -f example.ldif
命令执行输入LDAP管理员的口令
五.phpLDAPadmin的安装
(一).安装Apache服务
由于基于Web界面的LDAP客户端软件phpLDAPadmin使用PHP编写而成,因此在使用前应安装Apache服务器并建立好PHP 的运行环 境。由于phpLDAPadmin支持中文管理界面,因此还要将Apache的默认字符集设置为中文(Apache服务具体的安装和配置方法参见本书第7 章Web服务的配置与应用)。
(二).下载phpLDAPadmin
使用Web浏览器访问 phpLDAPadmin最新的稳定版 。
(三).安装phpLDAPadmin
安装phpLDAPadmin的具体步骤如下。
① 使用下面的命令解压安装包。
tar zxvf phpldapadmin-0.9.8.4.tar.gz 下面的命令进入解压目录。
cp -a phpldapadmin-0.9.8.4 /usr/local/phpldapadmin
(四).配置Apache服务
1.加入用户认证功能
2.建立虚拟目录
在Apache的主配置文件httpd.conf中加入以下语句建立虚拟目录。
Alias /phpldapadmin "/usr/local/phpldapadmin"
<Directory "/usr/local/phpldapadmin">
AuthType Basic
AuthName "Please Login to phpldapadmin"
AuthUserFile /etc/httpd/php_ldap_admin_pwd
Require user admin
</Directory>
六.配置phpLDAPadmin
(一).生成phpLDAPadmin主配置文件
phpLDAPadmin的主配置文件是/usr/local/phpldapadmin/config /config.php.example,phpLDAPadmin提供了一个默认的例子文件config.php.example,使用以下的命令可生 成phpLDAPadmin主配置文件。
cp /usr/local/phpldapadmin/config/config.php.example /usr/local/phpldapadmin/config/config.php
(二).配置phpLDAPadmin使用中文
① 编辑文件/usr/local/phpldapadmin/config/config.php,修改语句:
// $config->custom->appearance['language'] = 'auto';
将“//”注释符号去处,并将语句改为:
$config-> custom->appearance['language'] = 'zh_CN';
② 使用下列的命令转换phpLDAPadmin语言文件的编码。
iconv -f gbk -t utf8 /usr/local/phpldapadmin/locale/zh_CN/LC_MESSAGES/messages.po/usr/local/phpldapadmin/locale/zh_CN/LC_MESSAGES/messages.new.po
msgfmt -o /usr/local/phpldapadmin/locale/zh_CN/LC_MESSAGES/messages.mo /usr/ local/phpldapadmin/locale/zh_CN/LC_MESSAGES/messages.new.po
(三).配置phpLDAPadmin
(1)设置口令加密字符串
找到语句:
$config->custom->session['blowfish'] = ' ';
将其改为:
$config->custom- >session['blowfish'] = 'welcome';
(2)设置LDAP服务器的地址
找到语句:
$ldapservers->SetValue($i,'server','host','127.0.0.1');将其改为LDAP服务器对应的 名称和IP地址。
(3)设置目录树的基准DN
找到语句:
$queries[$q]['base'] = 'dc=example,dc=com';根据实际将其改为目录树的基准DN。
(4)设置LDAP服务器管理员的DN
找到语句:
$ldapservers->SetValue($i,'login','dn','cn=Manager,dc=example,dc=com'); 根据实际将其改为LDAP服务器管理员的DN。
(5)设置LDAP服务器管理员的口令
找到语句:
$ldapservers->SetValue($i,'login','pass','');
确保其口令为空。
(6)设置用户认证方式
phpLDAPadmin提供了cookie、session和config这3种认证方式找到语句:
$ldapservers->SetValue($i,'server','auth_type','cookie');
确保其认证方式为cookie。
七.使用phpLDAPadmin管理目录树
(一).登录phpLDAPadmin
启动Apache服务,然后使用浏览器访问http://Linux服务器的IP或域名/phpldapadmin/,输入用户名为 “admin”和口令后即可进入phpLDAPadmin的管理主页面 。
(二).创建OU
(三).创建用户组
(四).创建用户账号
八.LDAP服务的身份验证实例
(一).Linux系统用户验证
1.安装相关软件
(1)安装nss_ldap软件
(2)安装openldap-client软件
2.关闭SELinux
3.设置使用LDAP进行用户认证
(二).FTP用户验证
① 确认已经安装Red Hat Enterprise Linux 4 Update 1第4张安装光盘/RedHat/RPMS目录下的openldap-clients-2.2.13-2.i386.rpm的RPM包。
② 安装好pure-ftpd服务器并确认安装编译pure-ftpd时在./configure脚本命令行上加入“--with-ldap”参数 (pure-ftpd具体的安装和配置方法参见本书第8章 FTP服务的配置与应用)。
③ 进入pure-ftpd源文件解压后的目录,编辑配置文件pureftpd-ldap.conf并进行以下操作。
修改语句“LDAPServer ldap.c9x.org”为“LDAPServer 192.168.16.177”。 语句“LDAPBaseDN cn=Users,dc=c9x,dc=org”为“LDAPBaseDN dc=xyz,dc=com”。
修改语句“LDAPBindDN cn=Manager,dc=c9x,dc=org”为“LDAPBindDN cn=Manager,dc=example,dc=com”。
修改语句“LDAPBindPW r00tPaSsw0rD”为“LDAPBindPW helloldap”。
④ 使用以下命令将文件pureftpd-ldap.conf复制到/etc目录中。
cp pureftpd-ldap.conf /etc
⑤ 编辑pure-ftpd的主配置文件/etc/pure-ftpd.conf,找到语句“# LDAPConfigFile /etc/pureftpd-ldap.conf”,将该语句前的“#”号删掉。
⑥ 重新启动pure-ftpd服务后即可使用在LDAP服务器建立的用户登录FTP。
(三).Web用户验证
① 确认已经安装Red Hat Enterprise Linux 4 Update 1第4张安装光盘/RedHat/RPMS目录下的openldap-clients-2.2.13-2.i386.rpm的RPM包。
② 编辑Apache的主配置文件httpd.conf,添加如下语句(本例是对private目录进行用户验证)。
Alias /mysecret "/usr/local/mysecret"
<Directory /usr/local/mysecret>
AuthType Basic
AuthName "Please Login:"
AuthLDAPURL "ldap://192.168.16.177/dc=example,dc=com"
require valid-user
</Directory>
③ 重新启动Apache服务 。
LDIF文件
LDIF 是一种普遍使用的文件格式,用来描述目录信息或可对目录执行的修改操作。LDIF 完全独立于在所有特定目录中使用的储存格式,LDIF 通常用于从 LDAP 服务器导出目录信息或将数据导入 LDAP 服务器。
LDIF 一般很容易生成。因此,可以借助于一些工具(如 awk 或 perl)将专有格式的数据移入 LDAP 目录。也可以编写脚本来生成 LDIF 格式的测试数据。
LDIF 文件格式
Novell 导入转换导出导入需要 LDIF 1 格式的文件。以下是 LDIF 1 文件的基本规则:
第一个非注释行必须是版本号:1.
版本号后面跟有一个或多个记录。
每个记录由多个字段组成,一行一个字段。
各行使用换行符或回车符/换行符对分隔。
各记录由一个或多个空行分隔。
存在两种不同类型的 LDIF 记录:内容记录和更改记录。对 LDIF 文件可以包含的记录数目没有限制,但它们必须属于同一类型。在同一个 LDIF 文件中不能既有内容记录又有更改记录。
以井字符 (#) 开头的行是注释行,在处理 LDIF 文件时将被忽略。
LDIF 内容记录
LDIF 内容记录代表整个项的内容。以下是 LDIF 文件示例,共包含四个内容记录:
1 version: 1
2 dn: c=US
3 objectClass: top
4 objectClass: country
5
6 dn: l=San Francisco, c=US
7 objectClass: top
8 objectClass: locality
9 st: San Francisco
10
11 dn: ou=Artists, l=San Francisco, c=US
12 objectClass: top
13 objectClass: organizationalUnit
14 telephoneNumber: +1 415 5
展开阅读全文