1 {pkgs, lib, config, ...}:
2 let inherit (config) networking;
3 inherit (config.services) openldap;
4 inherit (config.users) users groups;
5 inherit (pkgs.lib) unlines;
6 domainSuffix = openldap.domainSuffix;
10 , gidNumber ? uidNumber
13 , userPassword ? "{SSHA}JtC8S4nzm+eX9cVgbyL6gquPWDZD4xXY"
14 # NOTE: doveadm pw -s SSHA -u $user -p $pass
16 , loginShell ? "/run/current-system/sw/bin/bash"
18 , mailForwardingAddress ? []
19 , domain ? networking.domain
20 }: "\n" + lib.concatStringsSep "\n\n" [
22 dn: uid=${uid},ou=accounts,ou=posix,${domainSuffix}
24 objectClass: posixAccount
25 objectClass: shadowAccount
26 objectClass: PostfixBookMailAccount
27 objectClass: PostfixBookMailForward
30 mail: ${uid}${lib.optionalString (networking.domain != "") "@${networking.domain}"}
31 mailEnabled: ${if mailEnabled then "TRUE" else "FALSE"}
32 #mailGroupMember: ${networking.domainBase}
33 homeDirectory: /home/${uid}
34 uidNumber: ${toString uidNumber}
35 gidNumber: ${toString gidNumber}
36 loginShell: ${loginShell}'' ]
37 ++ lib.optional (userPassword != "") "userPassword: ${userPassword}"
38 ++ map (forward: "mailForwardingAddress: ${forward}") mailForwardingAddress
39 ++ map (alias: "mailAlias: ${alias}@${networking.domain}") mailAlias
40 ++ lib.optional (mailAlias == []) "mailAlias:"
41 # NOTE: required by PostfixBookMailForward
44 dn: cn=${uid},ou=groups,ou=posix,${domainSuffix}
46 objectClass: posixGroup
47 gidNumber: ${toString gidNumber}
53 config = lib.mkIf config.users.ldap.enable {
59 # sudo ldapsearch -LLL -H ldapi:// -D cn=admin,cn=config -Y EXTERNAL -b 'olcDatabase={1}mdb,cn=config' -s sub
60 dn: olcBackend={1}mdb,cn=config
61 objectClass: olcBackendConfig
63 dn: olcDatabase={1}mdb,cn=config
64 objectClass: olcDatabaseConfig
65 objectClass: olcMdbConfig
66 # NOTE: checkpoint the database periodically in case of system failure
67 # and to speed slapd shutdown.
68 olcDbCheckpoint: 512 30
69 # Database max size is 1G
70 olcDbMaxSize: 1073741824
72 # NOTE: database superuser. Needed for syncrepl.
73 olcRootDN: cn=admin,${domainSuffix}
74 # NOTE: superuser password, generated with slappasswd -h "{SSHA}" -s "$password"
75 #olcRootPW: {SSHA}COkATGNe7rs/g8vWcYP5rqt4u5sWdMgP
77 olcDbIndex: objectClass eq
79 olcDbIndex: uidNumber,gidNumber eq
80 olcDbIndex: member,memberUid eq
82 olcDbIndex: mailAlias eq
83 olcDbIndex: mailEnabled eq
85 olcAccess: to attrs=userPassword
88 by dn="gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth" write
90 olcAccess: to attrs=shadowLastChange
92 by dn="gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth" write
94 olcAccess: to dn.sub="ou=posix,${domainSuffix}"
96 by dn="gidNumber=${toString groups.nslcd.gid}+uidNumber=${toString users.nslcd.uid},cn=peercred,cn=external,cn=auth" read
97 by dn="gidNumber=${toString groups.postfix.gid}+uidNumber=${toString users.postfix.uid},cn=peercred,cn=external,cn=auth" read
98 by dn="gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth" read
99 # NOTE: dovecot/auth runs as root, hence the gidNumber=0+uidNumber=0
107 objectClass: dcObject
108 objectClass: organization
109 o: ${networking.domainBase}
111 dn: cn=admin,${domainSuffix}
112 objectClass: simpleSecurityObject
113 objectClass: organizationalRole
114 description: ${networking.domainBase} LDAP administrator
115 roleOccupant: ${domainSuffix}
117 #userPassword: {SSHA}NONVwwKnKsCBmFxkMqTCFekdu3SJQHc9
119 dn: ou=posix,${domainSuffix}
121 objectClass: organizationalUnit
123 dn: ou=accounts,ou=posix,${domainSuffix}
125 objectClass: organizationalUnit
127 dn: ou=groups,ou=posix,${domainSuffix}
129 objectClass: organizationalUnit
131 dn: cn=${networking.domainBase},ou=groups,ou=posix,${domainSuffix}
133 objectClass: posixGroup
139 + lib.concatMapStrings posixAccount [
140 { uid="ju"; uidNumber=10000; cn="Julien M."; sn="julm"; mailAlias = ["juju"]; }
141 { uid="sevy"; uidNumber=10001; cn="Séverine P."; sn="sévy"; mailAlias = ["severine.popek" "ouais-ouais"]; }
142 { uid="nomail"; uidNumber=10002; mailAlias = ["noalias"]; mailEnabled = false; }
143 { uid="post"; domain="friot"; mailForwardingAddress = ["ju@${networking.domain}"]; }
144 { uid="host"; mailForwardingAddress = ["ju@${networking.domain}"]; }