LDAP + samba

時隔五年之久,既實驗了一下 LDAP 的設定這篇之後,又再度遇上了 LDAP 的設定問題。
當年照那篇文章設定的話,在多台 samba servers 的環境下其實是會撞上一些問題的,也就是帳號認證會失敗。
在問題發生的機器上輸入 pdbedit -L 的話,則會看到「sid S-1-5-21-3723331297-2499002124-1306837190-1001 does not belong to our domain」這樣的訊息。
處理的方式很簡單,因為每台 server 的 smb.conf 都是 standalone server,採取「security = user」的設定,所以把 local SID 設成一樣就可以了。
譬如在建立那個 samba 帳號的原機器輸入「net getlocalsid」的話,會看到「SID for domain <hostname> is: S-1-5-21-2347759184-1676931091-2532609757」。
所以只要在每台機器都下「net setlocalsid S-1-5-21-2347759184-1676931091-2532609757」,這個問題就會解決。
但是到了現在,這招已經不管用了。net setlocalsid 怎麼下都還是沒有作用,SID 死都不會變,所以需要學點新方法。

爬了一下網路上的相關問題,結論就是以前那種 workaround 已經不能用了,現在要好好地設定一個 PDC (Primary Domain Controller) 來玩才行,不然就是得每台都使用不同的 LDAP 帳號來儲存。
於是我就跑去參考了這份文件:Chapter 3. Server Types and Security Modes,順著裡面的「What Makes Samba a Domain Controller?」和「What Makes Samba a Domain Member?」稍微翻了一下。
總而言之,當成 PDC 的那台除了需要照我先前那篇設定去連接 LDAP server 外,還必須設定成這樣:

改完以後重開 samba server,輸入 testparm 後就會看到「Server role: ROLE_DOMAIN_PDC」。
相關的設定參考這份文件:Chapter 4. Domain Control

再來就是參考這份文件去讓其它台機器加入網域:Chapter 6. Domain Membership
其它台機器就不需要和 LDAP server 連接了,只要 workgroup 和 PDC 一致,然後設成 security = domain 就行,testparm 會顯示「Server role: ROLE_DOMAIN_MEMBER」。
假設要加入網域的機器 NETBIOS name 叫 server2,那麼必須在 PDC 這台機器上建立一個叫 server2$ 的帳號,建在本機或 LDAP 上都行,總之不需要 login shell 和 home directory。
建立好系統帳號以後,再執行「smbpasswd -a -m server2$」在 samba 上新增一個 machine trust account,再去 server2 這台機器上執行 net join <workgroup name> 就能讓 server2 加入網域。

做完這些設定後,可能還是會發現無法從 Windows XP 登入 samba server,這可能是因為 workgroup 不符合的關係。
譬如公司本身有個 workgroup 叫 MYGROUP,也有一台 Windows Server 擔任 PDC 的角色。
如果是自己的 team 裡的 Linux servers 自己弄一個叫 MYSUBGROUP 的 workgroup,那麼就會陷入這個問題,因為 Windows PC 無法同時加入兩個工作群組。
以往 samba server 是有個機制去自動對應的,但是在 3.4 以後把這機制變成要設定「map untrusted to domain = yes」才能用:

Authentication Changes
======================
Previously, when Samba was a domain member and a client was connecting using an
untrusted domain name, such as BOGUS\user smbd would remap the untrusted
domain to the primary domain smbd was a member of and attempt authentication
using that DOMAIN\user name. This differed from how a Windows member server
would behave. Now, smbd will replace the BOGUS name with it's SAM name. In
the case where smbd is acting as a PDC this will be DOMAIN\user. In the case
where smbd is acting as a domain member server this will be WORKSTATION\user.
Thus, smbd will never assume that an incoming user name which is not qualified
with the same primary domain, is part of smbd's primary domain.

While this behavior matches Windows, it may break some workflows which depended
on smbd to always pass through bogus names to the DC for verification. A new
parameter "map untrusted to domain" can be enabled to revert to the legacy
behavior.

雖然不知道這個選項在未來還能存在多久,目前確定同事 Fedora 上裝的 samba 4.0.9 還能使用,所以就先這樣吧。

附帶一提,Fedora 因為導入 sssd 的關係,LDAP 的設定不再是裝 nss_ldap 然後改 nsswitch.conf 就能搞定。
必須在 /etc/sssd/sssd.conf 寫類似這樣的東西:

每個選項的意義可以直接在 Fedora 的系統上 man sssd-ldap 看到。