You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
4.9 KiB

2 months ago
  1. { pkgs, lib, config, inputs, ... }:
  2. with lib;
  3. let
  4. baseDN = concatMapStringsSep ","
  5. (part: "dc=${part}")
  6. (splitString "." "informatik.hs-fulda.de");
  7. ldap-sync =
  8. let
  9. wrapped = pkgs.callPackage inputs.ldap-sync { };
  10. env = pkgs.runCommand "ldap-sync-env" { } ''
  11. mkdir -p $out
  12. ln -s ${config.sops.secrets."ldap/sync/config".path} $out/ldap-sync.properties
  13. '';
  14. in
  15. pkgs.runCommand "ldap-sync-wrapper"
  16. {
  17. nativeBuildInputs = [ pkgs.makeWrapper ];
  18. } ''
  19. mkdir -p $out/bin
  20. makeWrapper "${wrapped}/bin/ldap-sync" $out/bin/ldap-sync \
  21. --chdir "${env}"
  22. '';
  23. in
  24. {
  25. services.openldap = {
  26. enable = true;
  27. package = (pkgs.openldap.overrideAttrs (final: prev: {
  28. configureFlags = prev.configureFlags ++ [
  29. "--enable-overlays"
  30. "--enable-remoteauth"
  31. "--enable-spasswd"
  32. "--with-cyrus-sasl"
  33. ];
  34. doCheck = false;
  35. })).override {
  36. cyrus_sasl = pkgs.cyrus_sasl.override {
  37. enableLdap = true;
  38. };
  39. };
  40. urlList = [ "ldap:///" "ldaps:///" ];
  41. settings = {
  42. attrs = {
  43. olcLogLevel = "config ACL stats stats2 trace";
  44. olcTLSCertificateFile = config.sops.secrets."ldap/tls/crt".path;
  45. olcTLSCertificateKeyFile = config.sops.secrets."ldap/tls/key".path;
  46. olcTLSCRLCheck = "none";
  47. olcTLSVerifyClient = "never";
  48. olcTLSProtocolMin = "3.1";
  49. olcSaslHost = "localhost";
  50. olcSaslSecProps = "none";
  51. olcSizeLimit = "unlimited";
  52. };
  53. children = {
  54. "cn=schema".includes = [
  55. "${config.services.openldap.package}/etc/schema/core.ldif"
  56. "${config.services.openldap.package}/etc/schema/cosine.ldif"
  57. "${config.services.openldap.package}/etc/schema/inetorgperson.ldif"
  58. "${config.services.openldap.package}/etc/schema/nis.ldif"
  59. ];
  60. "olcDatabase={1}mdb" = {
  61. attrs = {
  62. objectClass = [ "olcDatabaseConfig" "olcMdbConfig" ];
  63. olcDatabase = "{1}mdb";
  64. olcDbDirectory = "/var/lib/openldap/db";
  65. olcSuffix = baseDN;
  66. olcRootDN = "cn=root,${baseDN}";
  67. olcRootPW.path = config.sops.secrets."ldap/root/password".path;
  68. olcAccess = [
  69. # Custom access rules for userPassword attributes
  70. ''{0}to attrs=userPassword
  71. by self read
  72. by anonymous auth
  73. by * none
  74. ''
  75. # Synced is managed by sync
  76. ''{1}to dn.subtree="ou=synced,ou=users,dc=informatik,dc=hs-fulda,dc=de"
  77. by dn.base="cn=sync,dc=informatik,dc=hs-fulda,dc=de" manage
  78. by * break
  79. ''
  80. # Allow login to read users
  81. ''{2}to dn.subtree="ou=users,dc=informatik,dc=hs-fulda,dc=de"
  82. by dn.base="cn=login,dc=informatik,dc=hs-fulda,dc=de" read
  83. by self read
  84. by * break
  85. ''
  86. # Prevent access
  87. ''{3}to *
  88. by * none
  89. ''
  90. ];
  91. };
  92. children = {
  93. "olcOverlay={0}remoteauth" = {
  94. attrs = {
  95. objectClass = [ "olcOverlayConfig" "olcRemoteAuthCfg" ];
  96. olcOverlay = "{0}remoteauth";
  97. olcRemoteAuthTLS = "starttls=yes tls_cacert=\"/etc/ssl/certs/ca-certificates.crt\"";
  98. olcRemoteAuthDNAttribute = "seeAlso";
  99. olcRemoteAuthDomainAttribute = "associatedDomain";
  100. olcRemoteAuthDefaultDomain = "upstream";
  101. olcRemoteAuthDefaultRealm = "file://${config.sops.secrets."ldap/upstream".path}";
  102. olcRemoteAuthRetryCount = "3";
  103. olcRemoteAuthStore = "false";
  104. };
  105. };
  106. };
  107. };
  108. };
  109. };
  110. };
  111. systemd.services.openldap = {
  112. environment = {
  113. SASL_PATH = pkgs.writeTextFile {
  114. name = "openldap-sasl-path";
  115. destination = "/slapd.conf";
  116. text = ''
  117. pwcheck_method: saslauthd
  118. saslauthd_path: /var/run/saslauthd/mux
  119. mech_list: GSSAPI EXTERNAL PLAIN NTLM
  120. '';
  121. };
  122. };
  123. };
  124. systemd.services."ldap-sync" = {
  125. script = "${ldap-sync}/bin/ldap-sync";
  126. startAt = "hourly";
  127. };
  128. sops.secrets."ldap/root/password" = {
  129. sopsFile = ./secrets/ldap.yaml;
  130. owner = "openldap";
  131. };
  132. sops.secrets."ldap/upstream" = {
  133. sopsFile = ./secrets/ldap.yaml;
  134. owner = "openldap";
  135. };
  136. sops.secrets."ldap/tls/key" = {
  137. sopsFile = ./secrets/ldap.tls.key;
  138. format = "binary";
  139. owner = "openldap";
  140. };
  141. sops.secrets."ldap/tls/crt" = {
  142. sopsFile = ./secrets/ldap.tls.crt;
  143. format = "binary";
  144. owner = "openldap";
  145. };
  146. sops.secrets."ldap/sync/config" = {
  147. sopsFile = ./secrets/ldap.yaml;
  148. };
  149. networking.firewall.allowedTCPPorts = [
  150. 22
  151. 389
  152. 636
  153. ];
  154. }