跳到主内容

LDAP服务器

对于使用 LDAP 认证的系统(例如 Nexus),Casdoor 可以充当一个简单的 LDAP 服务器。它支持基于简单认证(Simple Authentication)的 bind 和 search。

端口​

LDAP服务器默认监听389端口。可以通过 conf/app.conf 中的 ldapServerPort 修改。

行为​

  • 用户条目遵循 posixAccount 样式,同时也包含 Samba 对象类。参见 用户属性。
  • Bind:服务器解析 cn(用户名)和 ou(组织)。 dc 会被忽略。它通过 Casdoor 验证用户,并为后续操作授予权限。
  • Search:服务器检查客户端是否有权限(来自 bind),再相应返回结果。
信息

只支持 Simple Authentication。

绑定​

使用这种格式的 DN:cn=<username>,ou=<organization>,dc=example,dc=com。

示例:cn=admin,ou=built-in,dc=example,dc=com。把管理员的 DN 设成这个格式,然后用这个 DN 和用户密码进行 bind。成功后,客户端获得 search 权限。 If the DN has no ou, Casdoor uses the built-in organization.

DN 中的属性名称不区分大小写,因此 CN=admin,OU=built-in 也是有效的。名称和值周围的空格会被忽略。值保持区分大小写:ou=Built-in 是与 ou=built-in 不同的组织。

即使密码正确,绑定仍会在以下情况下因 invalidCredentials 失败:

  • 用户开启了多因素认证 (MFA)。错误提示为 "the user has MFA enabled and cannot bind with a password"。LDAP 绑定无法请求第二因素验证。
  • 用户所属的组织开启了 Disable signin。
  • 用户必须先更改密码,或必须设置组织要求的 MFA。用户必须在 Casdoor 登录页面上完成此操作后,LDAP 绑定才能生效。

bind 成功后:

  • 单个用户(例如 built-in 中的 Alice):Base DN ou=built-in,dc=example,dc=com,过滤器 cn=Alice。
  • 某个组织的所有用户(例如 built-in):Base DN ou=built-in,dc=example,dc=com,过滤器 cn=*。
  • 所有组织的所有用户(如果有权限):Base DN ou=*,dc=example,dc=com,过滤器 cn=*。
  • 群组中的用户:使用包含 Casdoor 群组 ID 的过滤器,例如 (memberOf=<organization>/<group-name>)。
  • 按 POSIX ID 查找单个用户:使用类似 (uidNumber=10001) 的过滤器。

用户属性​

属性描述来源
cn通用名用户名
uid用户 ID用户 ID
uidNumberPOSIX user IDThe user's UID number. When it's 0, a number derived from the username.
gidNumberPOSIX primary group IDSame value as uidNumber
homeDirectory主目录/home/{username}
邮箱邮箱用户邮箱
手机电话用户手机号
sn姓用户的姓
givenName名用户的名
memberOf用户所属的组One DN per group of the user, in the form cn=<group-name>,ou=<group-organization>,<dc-parts>
loginShell登录 shelluser.Properties["loginShell"],默认为 /bin/bash
gecosGECOS(全名)user.DisplayName,为空时退回 user.Name
sshPublicKeySSH 公钥user.Properties["sshPublicKey"],为空时省略
userPasswordPasswordReturned only when the organization has Enable LDAP password on. See Password attributes.

Samba 属性列在 Samba 和 SMB 客户端 中。

在 memberOf 中,<group-organization> 是拥有该群组的组织,<dc-parts> 是搜索基础 DN 的 dc= 部分。例如,使用基础 DN ou=my-org,dc=example,dc=com 进行搜索时,对于群组 my-org/dev 返回 memberOf: cn=dev,ou=my-org,dc=example,dc=com。如果基础 DN 没有 dc= 部分,则值为 cn=<group-name>,ou=<group-organization>。

所有条目都包含对象类 posixAccount(Linux 身份验证中 PAM 和 NSS 所必需的),以及 sambaSamAccount 和 sambaIdmapEntry。

组织的 LDAP attributes 设置可以限制返回哪些属性。请参阅 LDAP server attribute filtering。

Linux 机器登录​

Casdoor 的 LDAP 服务器提供兼容 posixAccount 的条目,所以 Linux 主机可以通过 sssd、nss-ldap 或 pam_ldap 等标准工具直接用 Casdoor 认证用户。

要为每个用户设置 loginShell 和 sshPublicKey,在用户的 Properties 字段(键值映射)中设置:

{
"loginShell": "/bin/zsh",
"sshPublicKey": "ssh-ed25519 AAAA..."
}

如果没有设置 loginShell,使用 /bin/bash。如果没有 sshPublicKey,LDAP 条目中就完全不包含这个属性。

设置 POSIX 用户和组 ID​

默认情况下,Casdoor 从用户名派生 uidNumber,从组名派生组的 gidNumber。只要名称不变,这些数字就会保持不变。若要发布固定数字(例如以匹配现有主机上的文件所有权):

  1. 在用户编辑页面上,设置 UID number。将其保留为 0 以保持派生的数字。只有管理员可以看到并修改此字段。
  2. 在群组编辑页面上,设置 GID number。将其保留为 0 以保持派生的数字。

如果用户编辑页面上没有 UID number,请将其添加到组织的 Account items 中。LDAP 同步也可以填充 UID number。请参阅 Sync POSIX IDs。数据库同步器可以将列映射到 UidNumber 字段。

Samba 和 SMB 客户端​

每个用户条目也是一个 Samba 账户(sambaSamAccount、sambaIdmapEntry),因此通过 LDAP 读取账户的 SMB 客户端(如 Synology DSM 和 TrueNAS)可以使用 Casdoor 作为其目录。

AttributeSource
sambaSIDUser property sambaSID. Otherwise <domain-sid>-<uidNumber>, where <domain-sid> is S-1-5-21- followed by three numbers derived from the organization name.
sambaPrimaryGroupSIDUser property sambaPrimaryGroupSID. Otherwise the same value as the derived sambaSID.
sambaAcctFlags[U ], or [DU ] when the user is forbidden or deleted
sambaPwdLastSetUnix time of the user's last password change, otherwise of the user's creation time. It's never 0, which Samba reads as "must change password at next logon".
sambaDomainNameOrganization name
sambaLMPasswordPlaceholder of 32 X characters. The LAN Manager hash isn't supported.
sambaPasswordHistoryPlaceholder of 64 0 characters
sambaNTPasswordReturned only when the organization has Enable LDAP password on. See Password attributes.

你可以使用相等条件搜索 sambaSID、sambaPrimaryGroupSID、sambaAcctFlags、sambaDomainName 和 sambaPwdLastSet,例如 (sambaDomainName=built-in)。它们遵循与 POSIX filters 中其他计算属性相同的规则。

要设置用户的 SID 或 NT 哈希,请在用户的 Properties 字段中添加 sambaSID、sambaPrimaryGroupSID 或 sambaNTPassword 键。

密码属性​

内置 LDAP 服务器仅在组织开启了 Enable LDAP password 时才返回密码属性 userPassword 和 sambaNTPassword。当该选项关闭时,即使客户端请求这些属性或组织的 LDAP attributes 中列出了这些属性,也永远不会返回它们。

只有全局管理员可以在组织编辑页面上开启 Enable LDAP password。Casdoor 会先要求确认。

危险

开启 Enable LDAP password 后,任何能够搜索组织用户的客户端都可以读取他们的密码哈希值,或者如果组织以明文存储密码,则直接读取密码本身。NT 哈希可用于直接登录 SMB 服务(pass-the-hash)。仅当集成(如 Samba)需要时才开启此功能。

  • userPassword:带有组织 Password type 前缀的已存储密码:salt 类型为 {sha256},md5-salt 类型为 {md5},pbkdf2-salt 类型为 {pbkdf2},其他类型为 {<password-type>}。如果是 plain 类型,则该值为密码本身。
  • sambaNTPassword:用户属性 sambaNTPassword 的大写形式(如果已设置)。否则,Casdoor 会计算 NT 哈希,但仅当组织的 Password type 为 plain 时才会如此。对于其他密码类型,请设置该属性,否则该属性将被省略。

RFC 风格的功能​

Root DSE(baseDN="")​

  • namingContexts:ldapsearch -x -H ldap://<casdoor-host>:389 -D "cn=admin,ou=built-in" -w <passwd> -b "" -s base "(objectClass=*)" namingContexts,返回可见的组织 DN。
  • subschemaSubentry:ldapsearch -x -H ldap://<casdoor-host>:389 -D "cn=admin,ou=built-in" -w <passwd> -b "" -s base "(objectClass=*)" subschemaSubentry,返回 subschemaSubentry: cn=Subschema。

架构​

查询 objectClasses:ldapsearch -x -H ldap://<casdoor-host>:389 -D "cn=admin,ou=built-in" -w <passwd> -b "cn=Subschema" -s base "(objectClass=*)" objectClasses — 返回 posixAccount、posixGroup、sambaSamAccount 和 sambaIdmapEntry 定义。

POSIX 过滤器​

  • (objectClass=posixAccount):用户列表。 (objectClass=sambaSamAccount) and (objectClass=sambaIdmapEntry) return the same list.
  • (objectClass=posixGroup):某个组织的群组列表(例如 -b "ou=<org>" "(objectClass=posixGroup)")。

uidNumber、gidNumber、homeDirectory 和 Samba 属性由服务器计算得出。对其中任一属性的相等条件可以单独使用,也可以在 & 中使用,例如 (&(objectClass=posixAccount)(uidNumber=10001))。如果在 | 或 ! 中使用,或者作为子字符串条件(如 (uidNumber=100*)),搜索将不会返回任何条目。

群组搜索是指过滤器为 (objectClass=posixGroup) 或包含该条件的 & 过滤器的搜索。Casdoor 会将过滤器的其余部分与每个群组条目进行检查,因此你可以在 cn、gidNumber、memberUid 和 objectClass 上组合使用 &、|、!、相等、存在和子字符串条件。群组值的比较不区分大小写。示例如下:

  • (&(objectClass=posixGroup)(gidNumber=12345)) — 具有该 GID 的群组。
  • (&(objectClass=posixGroup)(cn=dev*)) — 名称以 dev 开头的群组。
  • (&(objectClass=posixGroup)(memberUid=alice)) — 用户 alice 所属的群组。

每个群组条目的 DN 为 cn=<group-name>,<base-dn>,并具有以下属性:

AttributeSource
cnGroup name
gidNumberThe group's GID number. When it's 0, a number derived from the group name.
memberUidUsername of each member
objectClassposixGroup

另请参阅​