LADP使用


<?php


//1、连接ladp
$ldaphost = "ldap.example.com"; // your ldap servers
$ldapport = 389; // your ldap server's port number

// Connecting to LDAP
$ds= ldap_connect($ldaphost, $ldapport) or die("Could not connect to $ldaphost");

if ($ds) {
$r=ldap_bind($ds,"cn=root, o=My Company, c=US", "secret");
$info["cn"]="Roberto Celestino";
$info["sn"]="Roberto";
$info["mail"]="rcelestino@here.and.now";
$info["objectclass"]="person";
// add data to directory
$r=ldap_add($ds, "cn=Celestino Roberto, o=Mi Compania, c=US", $info);

//ldap_search\ldap_get_entries
$dn = "o=My Company, c=US";
$filter="(|(sn=$person*)(givenname=$person*))";
$justthese = array("ou", "sn", "givenname", "mail");

$sr=ldap_search($ds, $dn, $filter, $justthese);

$info = ldap_get_entries($ds, $sr);

//Close the link apointed by the identified $ds (get with ldap_connect)
ldap_close($ds);
}



LDAP目录记录的标识名(Distinguished Name,简称DN)是用来读取单个记录,以及回溯到树的顶部。
LDAP目录树的最顶部就是根,也就是所谓的“基准DN"。基准DN通常使用下面列出的三种格式:

o=foobar . com
(用公司的Internet地址表示的基准DN)
这种格式很直观,用公司的 域名作为基准DN。这也是现在最常用的格式。
dc=foobar, dc=com
(用DNS 域名的不同部分组成的基准DN)
就象上面那一种格式,这种格式也是以DNS 域名为基础的,但是上面那种格式不改变域名(也就更易读),而这种格式把域名:foobar点com分成两部分 dc=foobar, dc=com。

DN是LDAP记录项的名字
在LDAP目录中的所有记录项都有一个唯一的“Distinguished Name",也就是DN。每一个LDAP记录项的DN是由两个部分组成的:相对DN(RDN)和记录在LDAP目录中的位置。


好的,以下是在CentOS 7.6上部署LDAP的步骤: 1. 安装OpenLDAP和相关工具: ``` yum install openldap openldap-servers openldap-clients migrationtools -y ``` 2. 配置slapd.conf文件: 编辑/etc/openldap/slapd.conf文件并添加以下内容: ``` include /etc/openldap/schema/core.schema include /etc/openldap/schema/cosine.schema include /etc/openldap/schema/inetorgperson.schema include /etc/openldap/schema/nis.schema pidfile /var/run/openldap/slapd.pid argsfile /var/run/openldap/slapd.args database bdb suffix "dc=example,dc=com" rootdn "cn=Manager,dc=example,dc=com" rootpw {SHA}hashed_password directory /var/lib/ldap access to * by * read ``` 将 "dc=example,dc=com" 修改为您的组织名称,并设置 "rootpw" 为管理员密码的SHA哈希值。您可以使用以下命令生成SHA哈希值: ``` slappasswd -s 密码 ``` 3. 配置ldap.conf文件: 编辑/etc/openldap/ldap.conf文件并添加以下内容: ``` BASE dc=example,dc=com URI ldap://localhost ``` 将 "dc=example,dc=com" 修改为您的组织名称。 4. 导入示例数据: 在/usr/share/openldap/migration目录下有一个示例数据文件,您可以使用以下命令将示例数据导入LDAP数据库: ``` cp /usr/share/openldap/migration/migrate_common.ph /usr/share/openldap/migration/migrate_common.ph.bak sed -i 's/^\$DEFAULT_MAIL_DOMAIN.*$/\$DEFAULT_MAIL_DOMAIN = "example.com";/g' /usr/share/openldap/migration/migrate_common.ph /usr/share/openldap/migration/migrate_base.pl > /tmp/base.ldif /usr/share/openldap/migration/migrate_passwd.pl /etc/passwd > /tmp/passwd.ldif /usr/share/openldap/migration/migrate_group.pl /etc/group > /tmp/group.ldif ldapadd -x -D "cn=Manager,dc=example,dc=com" -f /tmp/base.ldif -w 管理员密码 ldapadd -x -D "cn=Manager,dc=example,dc=com" -f /tmp/passwd.ldif -w 管理员密码 ldapadd -x -D "cn=Manager,dc=example,dc=com" -f /tmp/group.ldif -w 管理员密码 ``` 将 "example.com" 修改为您的邮件域名,并将 "dc=example,dc=com" 修改为您的组织名称。确保替换 "管理员密码" 为您的管理员密码。 5. 启动slapd服务: ``` systemctl start slapd systemctl enable slapd ``` 现在,您已经在CentOS 7.6上成功部署了LDAP服务。您可以使用ldapsearch命令测试LDAP是否正常工作。例如,使用以下命令查询所有用户信息: ``` ldapsearch -x -b "dc=example,dc=com" "(objectClass=*)" -D "cn=Manager,dc=example,dc=com" -w 管理员密码 ``` 将 "dc=example,dc=com" 修改为您的组织名称,并将 "管理员密码" 修改为您的管理员密码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值