<?xml version="1.0" encoding="UTF-8" ?><oembed><version>1.0</version><provider_name>Jui-Nan Lin&#039;s Blog</provider_name><provider_url>https://jnlin.org</provider_url><author_name>jnlin</author_name><author_url>https://jnlin.org/author/jnlin/</author_url><title>Enable nss_ldap in FreeBSD</title><html>請安裝 ports/net/nss_ldap:
&lt;!--more--&gt;
&lt;blockquote&gt;&lt;code&gt;&lt;pre&gt;
# cd /usr/ports/net/nss_ldap
# cat &gt; files/patch-shadow &lt;&lt;EOF
--- ldap-pwd.c.orig	Sat Oct 16 22:22:29 2004
+++ ldap-pwd.c	Sun Oct 17 12:45:08 2004
@@ -92,19 +92,19 @@
   size_t tmplen;
   char *tmp;
 
-  if (_nss_ldap_oc_check (ld, e, &quot;shadowAccount&quot;) == NSS_SUCCESS)
-    {
-      /* don&#039;t include password for shadowAccount */
-      if (buflen &lt; 3)
-	return NSS_TRYAGAIN;
-
-      pw-&gt;pw_passwd = buffer;
-      strcpy (buffer, &quot;x&quot;);
-      buffer += 2;
-      buflen -= 2;
-    }
-  else
-    {
+/*  if (_nss_ldap_oc_check (ld, e, &quot;shadowAccount&quot;) == NSS_SUCCESS)
+ *    {
+ */      /* don&#039;t include password for shadowAccount */
+/*      if (buflen &lt; 3)
+ *	return NSS_TRYAGAIN;
+ *
+ *     pw-&gt;pw_passwd = buffer;
+ *     strcpy (buffer, &quot;x&quot;);
+ *     buffer += 2;
+ *     buflen -= 2;
+ *   }
+ * else
+ */    {
       stat =
 	_nss_ldap_assign_userpassword (ld, e, AT (userPassword),
 				       &amp;pw-&gt;pw_passwd, &amp;buffer, &amp;buflen);
EOF
# make install clean
&lt;/pre&gt;&lt;/code&gt;&lt;/blockquote&gt;

Patch 的原因請看&lt;a href=&quot;http://lists.freebsd.org/pipermail/freebsd-ports-bugs/2004-November/044589.html&quot;&gt;這裡&lt;/a&gt;。

接著請修改 /usr/local/etc/nss_ldap.conf:
&lt;blockquote&gt;&lt;code&gt;
# cat &gt; /usr/local/etc/nss_ldap.conf &lt;&lt;EOF
# The distinguished name of the search base.
base dc=cs,dc=nctu,dc=edu,dc=tw

# Another way to specify your LDAP server is to provide an
# uri with the server name. This allows to use
# Unix Domain Sockets to connect to a local LDAP Server.
#uri ldap://127.0.0.1/
#uri ldapi://%2fvar%2frun%2fopenldap%2fldapi/
#uri ldaps://127.0.0.1/
uri ldaps://ldap.cs.nctu.edu.tw/

# The distinguished name to bind to the server with
# if the effective user ID is root. Password is
# stored in /etc/ldap.secret (mode 600)
rootbinddn cn=Manager,dc=cs,dc=nctu,dc=edu,dc=tw

# Filter to AND with uid=%s
pam_filter objectclass=posixAccount

# The user ID attribute (defaults to uid)
pam_login_attribute uid

# RFC2307bis naming contexts
# Syntax:
# nss_base_XXX          base?scope?filter
# where scope is {base,one,sub}
# and filter is a filter to be &amp;&#039;d with the
# default filter.
# You can omit the suffix eg:
# nss_base_passwd       ou=People,
# to append the default base DN but this
# may incur a small performance impact.
#nss_base_passwd        ou=People,
#nss_base_shadow        ou=People,
nss_base_group          ou=Group,
nss_base_hosts          ou=Hosts,
nss_base_services       ou=Services,
nss_base_networks       ou=Networks,
nss_base_protocols      ou=Protocols,
nss_base_rpc            ou=Rpc,
nss_base_ethers ou=Ethers,
nss_base_netmasks       ou=Networks,
nss_base_bootparams     ou=Ethers,
nss_base_aliases        ou=Aliases,
nss_base_netgroup       ou=Netgroup,

EOF
&lt;/code&gt;&lt;/blockquote&gt;

如果你不使用 samba, 則 nss_base_passwd 及 nss_base_shadow 兩行前面的註解可以拿掉。

接著請將 rootdn 的密碼存放在 /usr/local/etc/nss_ldap.secret 中，讓 nss_ldap 可以用 rootdn 讀寫 LDAP 中的密碼。

&lt;blockquote&gt;&lt;code&gt;
      # echo &#039;rootdn_password&#039; &gt; /usr/local/etc/nss_ldap.secret
      # chmod 600 /usr/local/etc/nss_ldap.secret 
&lt;/code&gt;&lt;/blockquote&gt;

接著修改 /etc/master.passwd，在最後面加上一行: +:*::::::::
還有 /etc/group，也是在最後面加上: +:*::

接著修改 /etc/nsswitch.conf：（斜體字的部份表示增加的部份）

&lt;blockquote&gt;&lt;code&gt;
group: compat
group_compat: &lt;i&gt;ldap&lt;/i&gt; nis
hosts: files dns
networks: files
passwd: compat
passwd_compat: &lt;i&gt;ldap&lt;/i&gt; nis
shells: files
&lt;/code&gt;&lt;/blockquote&gt;

接下來就可以登入測試看看了。

這樣的修改方式不能使用 /usr/bin/passwd 修改密碼，可以使用&lt;a href=&quot;http://www.csie.nctu.edu.tw/~jnlin/docs/ldappasswd.txt&quot;&gt;這個 script &lt;/a&gt;來讓使用者修改密碼。原本的程式在&lt;a href=&quot;http://www.mami.net/univr/tng-ldap/howto/#how_to_change_password&quot;&gt;這裡&lt;/a&gt;，我稍微修改了一下讓它適合用在目前的 LDAP 上。如果你不使用 Samba，請將 Samba 的部份 mark 起來。

註:
1. 如果你要讓 Linux 的系統也使用 LDAP 作認證，則你一定要加上 nss_ldap 的 patch，否則 FreeBSD 會無法登入。
2. 如果你可以登入，但是在普通 user下打 id 出來的結果沒有 username，那你要去修改 slapd.conf，將 Anonymous Read 的權限加入即可。 
3. 目前 FreeBSD 5.x 只支援 passwd 與 group 檔的 name service switching, netgroup, amd map, hosts, alias 等尚未支援。</html><type>rich</type></oembed>