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"

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/181292.html
標籤:其他
下一篇:ceph客戶端安裝配置訪問rbd
