1 {pkgs, lib, config, ...}:
2 let inherit (builtins) hasAttr;
3 inherit (config) networking;
4 inherit (config.services) openldap;
5 inherit (config.users) users groups;
6 inherit (pkgs.lib) unlines;
7 domainSuffix = openldap.domainSuffix;
11 , gidNumber ? uidNumber
14 , userPassword ? "{SSHA}JtC8S4nzm+eX9cVgbyL6gquPWDZD4xXY"
15 # NOTE: doveadm pw -s SSHA -u $user -p $pass
17 , loginShell ? "/run/current-system/sw/bin/bash"
19 , mailForwardingAddress ? []
20 , domain ? networking.domain
21 }: "\n" + lib.concatStringsSep "\n\n" [
23 dn: uid=${uid},ou=accounts,ou=posix,${domainSuffix}
25 objectClass: posixAccount
26 objectClass: shadowAccount
27 objectClass: PostfixBookMailAccount
28 objectClass: PostfixBookMailForward
31 mail: ${uid}${lib.optionalString (networking.domain != "") "@${networking.domain}"}
32 mailEnabled: ${if mailEnabled then "TRUE" else "FALSE"}
33 #mailGroupMember: ${networking.domainBase}
34 homeDirectory: /home/${uid}
35 uidNumber: ${toString uidNumber}
36 gidNumber: ${toString gidNumber}
37 loginShell: ${loginShell}'' ]
38 ++ lib.optional (userPassword != "") "userPassword: ${userPassword}"
39 ++ map (forward: "mailForwardingAddress: ${forward}") mailForwardingAddress
40 ++ map (alias: "mailAlias: ${alias}@${networking.domain}") mailAlias
41 ++ lib.optional (mailAlias == []) "mailAlias:"
42 # NOTE: required by PostfixBookMailForward
45 dn: cn=${uid},ou=groups,ou=posix,${domainSuffix}
47 objectClass: posixGroup
48 gidNumber: ${toString gidNumber}
54 config = lib.mkIf config.users.ldap.enable {
60 # WARNING: this deletes data.
65 # sudo ldapsearch -LLL -H ldapi:// -D cn=admin,cn=config -Y EXTERNAL -b 'olcDatabase={1}mdb,cn=config' -s sub
66 dn: olcBackend={1}mdb,cn=config
67 objectClass: olcBackendConfig
69 dn: olcDatabase={1}mdb,cn=config
70 objectClass: olcDatabaseConfig
71 objectClass: olcMdbConfig
72 # NOTE: checkpoint the database periodically in case of system failure
73 # and to speed slapd shutdown.
74 olcDbCheckpoint: 512 30
75 # Database max size is 1G
76 olcDbMaxSize: 1073741824
78 # NOTE: database superuser. Needed for syncrepl.
79 olcRootDN: cn=admin,${domainSuffix}
80 # NOTE: superuser password, generated with slappasswd -h "{SSHA}" -s "$password"
81 #olcRootPW: {SSHA}COkATGNe7rs/g8vWcYP5rqt4u5sWdMgP
83 olcDbIndex: objectClass eq
85 olcDbIndex: uidNumber,gidNumber eq
86 olcDbIndex: member,memberUid eq
88 olcDbIndex: mailAlias eq
89 olcDbIndex: mailEnabled eq
91 olcAccess: to attrs=userPassword
94 by dn="gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth" write
96 olcAccess: to attrs=shadowLastChange
98 by dn="gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth" write
100 olcAccess: to dn.sub="ou=posix,${domainSuffix}"
102 by dn="gidNumber=${toString groups.nslcd.gid}+uidNumber=${toString users.nslcd.uid},cn=peercred,cn=external,cn=auth" read
103 ${lib.optionalString (hasAttr "postfix" users) ''by dn="gidNumber=${toString groups.postfix.gid}+uidNumber=${toString users.postfix.uid},cn=peercred,cn=external,cn=auth" read''}
104 by dn="gidNumber=0+uidNumber=0,cn=peercred,cn=external,cn=auth" read
105 # NOTE: dovecot/auth runs as root, hence the gidNumber=0+uidNumber=0
113 objectClass: dcObject
114 objectClass: organization
115 o: ${networking.domainBase}
117 dn: cn=admin,${domainSuffix}
118 objectClass: simpleSecurityObject
119 objectClass: organizationalRole
120 description: ${networking.domainBase} LDAP administrator
121 roleOccupant: ${domainSuffix}
123 #userPassword: {SSHA}NONVwwKnKsCBmFxkMqTCFekdu3SJQHc9
125 dn: ou=posix,${domainSuffix}
127 objectClass: organizationalUnit
129 dn: ou=accounts,ou=posix,${domainSuffix}
131 objectClass: organizationalUnit
133 dn: ou=groups,ou=posix,${domainSuffix}
135 objectClass: organizationalUnit
137 dn: cn=${networking.domainBase},ou=groups,ou=posix,${domainSuffix}
139 objectClass: posixGroup
145 + lib.concatMapStrings posixAccount [
146 { uid="julm"; uidNumber=10000; cn="Julien Moutinho"; sn="julm"; mailAlias = ["julien.moutinho"]; }
147 { uid="sevy"; uidNumber=10001; cn="Séverine Popek"; sn="sévy"; mailAlias = ["severine.popek" "ouais-ouais"]; }
148 #{ uid="nomail"; uidNumber=10002; mailAlias = ["noalias"]; mailEnabled = false; }
149 #{ uid="post"; domain="friot"; mailForwardingAddress = ["ju@${networking.domain}"]; }
150 #{ uid="host"; mailForwardingAddress = ["ju@${networking.domain}"]; }