OpenLDAP源碼安裝
下載OpenLDAP源碼
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數據庫
安裝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
進入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
OpenLDAP目錄結構
配置文件目錄
服務端可執行指令目錄
服務端啓動工具slapd
客戶端可執行指令目錄
bdb數據、log存放目錄
OpenLDAP配置管理
版本管理
/usr/local/libexec/slapd -VV
配置rootdn密碼
slappasswd
主配置文件slapd.conf
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
初始化OpenLADP
如果採用mdb做後端數據庫,此步可忽略,DB_CONFIG是 bdb/hdb數據庫使用的;
與主配置文件中的配置有關,主配置文件確定使用bdb與數據存放路徑。
cd /usr/local/var/openldap-data/
cp DB_CONFIG.example DB_CONFIG
啓動OpenLADP
#後臺啓動
#非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
創建管理員賬號
編輯ldif文件
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=*)'
創建具有組屬性的用戶
編輯ldif文件
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=*)'
使用Java驗證OpenLdap用戶登錄
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"