赞
踩
http://www.openldap.org/software/download/
ftp://ftp.openldap.org/pub/OpenLDAP/openldap-release.tgz
tar -zxvf openldap-2.4.48.tgz
cd openldap-2.4.48/
./configure
编译错误
configure: error: BDB/HDB: BerkeleyDB not available
本地没有安装BerkeleyDB数据库
去Oracle官网下载:
http://www.oracle.com/technetwork/database/database-technologies/berkeleydb/downloads/index.html
BerkeleyDB版本:
根据openldap-2.4.48源码中README的描述,要求用Oracle Berkeley 4.4-4.8或者5.0-5.1版本的
所以选择下载db-5.1.29版本
解压到本地
切换到build_unix 目录
cd build_unix
../dist/configure
make
make install
设置系统变量
vim /etc/ld.so.conf
添加一行:/usr/local/BerkeleyDB.5.1/lib/
保存退出。
执行生效
ldconfig –v
进入OpenLDAP源码包
./configure CPPFLAGS="-I/usr/local/BerkeleyDB.5.1/include -D_GNU_SOURCE" LDFLAGS="-L/usr/local/BerkeleyDB.5.1/lib
make depend
make
make test
make install
/usr/local/libexec/slapd -VV
slappasswd
cd /usr/local/etc/openldap/
vim slapd.conf
schema默认只有core.schema,各级需要添加,这里将同配置文件一个目录的schema目录中有的schema文件都加到配置文件中
include /usr/local/etc/openldap/schema/collective.schema
include /usr/local/etc/openldap/schema/corba.schema
include /usr/local/etc/openldap/schema/cosine.schema
include /usr/local/etc/openldap/schema/duaconf.schema
include /usr/local/etc/openldap/schema/dyngroup.schema
include /usr/local/etc/openldap/schema/inetorgperson.schema
include /usr/local/etc/openldap/schema/java.schema
include /usr/local/etc/openldap/schema/misc.schema
include /usr/local/etc/openldap/schema/nis.schema
include /usr/local/etc/openldap/schema/openldap.schema
include /usr/local/etc/openldap/schema/pmi.schema
include /usr/local/etc/openldap/schema/ppolicy.schema
pidfile /usr/local/openldap-2.4.44/var/run/slapd.pid
argsfile /usr/local/openldap-2.4.44/var/run/slapd.args
新增日志文件级别与路径,需要在编译时–enable-debug,否则日志文件输出,不影响调试模式
loglevel 256
logfile /usr/local/var/slapd.log
使用mdb做后端数据库,也可修改为"bdb"参数,在OpenLDAP 官方文档" 11.4. LMDB"章节中有介绍mdb是推荐使用的后端数据库
database mdb
使用mdb做后端数据库时,根据官方文档中说明需要设置一个空间值," In addition to the usual parameters that a minimal configuration requires, the mdb backend requires a maximum size to be set. This should be the largest that the database is ever anticipated to grow (in bytes). The filesystem must also provide enough free space to accommodate this size.";如果使用bdb做后端数据库,需要将此项参数注释
maxsize 1073741824
修改域名及管理员账户名。
suffix "dc=nmcm,dc=com"
rootdn "cn=admin,dc=nmcm,dc=com"
使用密文密码,即使用slappasswd生成的密文
rootpw {SSHA}Q7CJ8dxnR6uuEyhZ3FHn6O2PaoJlsvlJ
#openldap数据目录,采用mdb时,在相应目录生成" data.mdb"与" lock.mdb"文件;采用bdb时,在相应目录生成" dn2id.bdb"与" id2entry.bdb",及多个" __db.00*"文件
directory /usr/local/var/openldap-data
index objectClass eq
如果采用mdb做后端数据库,此步可忽略,DB_CONFIG是 bdb/hdb数据库使用的;
与主配置文件中的配置有关,主配置文件确定使用bdb与数据存放路径。
cd /usr/local/var/openldap-data/
cp DB_CONFIG.example DB_CONFIG
#后台启动
#非root用户不能监听端口1~1024,如果是非root用户,有可能需要重新定义服务端口
/usr/local/libexec/slapd
#前端启动,输出debug信息
/usr/local/libexec/slapd -d 256
OpenLDAP默认监听的端口是389
ldapsearch -x -b '' -s base'(objectclass=*)'
ldapsearch -x -b '' -s base '(objectclass=*)' namingContexts
vim admin.ldif
dn: dc=nmcm,dc=com
objectclass: dcObject
objectclass: organization
o: NMCM.Inc
dc: nmcm
dn: cn=admin,dc=nmcm,dc=com
objectclass: organizationalRole
cn: admin
ldapadd -x -D "cn=admin,dc=nmcm,dc=com" -W -f admin.ldif
ldapsearch -x -b 'dc=nmcm,dc=com' '(objectClass=*)'
dn: ou=nsccwx,dc=nmcm,dc=com ou: nsccwx objectClass: organizationalUnit dn: cn=zhaojq,ou=nsccwx,dc=nmcm,dc=com ou: nsccwx cn: nmcm sn: zhaojq objectClass: inetOrgPerson objectClass: organizationalPerson dn: cn=jiangyt,ou=nsccwx,dc=nmcm,dc=com ou: nsccwx cn: nmcm sn: jiangyt objectClass: inetOrgPerson objectClass: organizationalPerson
ldapadd -x -D "cn=admin,dc=nmcm,dc=com" -W -f nsccwx.ldif
ldapsearch -x -b 'dc=nmcm,dc=com' '(objectClass=*)'
ldappasswd -x -D "cn=zhaojq,ou=nsccwx,dc=nmcm,dc=com" -w 123456 "cn=zhaojq,ou=nsccwx,dc=nmcm,dc=com" -s 123456
出现userPassword
ldapsearch -x -b 'cn=zhaojq,ou=nsccwx,dc=nmcm,dc=com' '(objectClass=*)'
import java.util.Hashtable; import javax.naming.Context; import javax.naming.directory.DirContext; import javax.naming.directory.InitialDirContext; public class LdapDemo{ public static void main(String[] args){ System.out.println("IPAdress: " + args[0]); System.out.println("DN: " + args[1]); System.out.println("Password: " + args[2]); Hashtable<String, String> tbl = new Hashtable<String, String>(4); String LDAP_URL = "ldap://" +args[0]+ "/dc=nmcm,dc=com"; tbl.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); tbl.put(Context.PROVIDER_URL, LDAP_URL); tbl.put(Context.SECURITY_AUTHENTICATION, "simple"); tbl.put(Context.SECURITY_PRINCIPAL, args[1]); tbl.put(Context.SECURITY_CREDENTIALS, args[2]); System.out.println("env setting"); DirContext context = null; try { System.out.println("login verification begins..."); context = new InitialDirContext(tbl); System.out.println("login successfully."); } catch (Exception ex) { System.out.println("login failed."); } finally { try { if (context != null) { context.close(); context = null; } tbl.clear(); } catch (Exception e) { System.out.println("exception happened."); } } } }
java LdapDemo "localhost" "cn=zhaojq,ou=nsccwx,dc=nmcm,dc=com" "123456"
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。