NixOS deployment for LinuxLab
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.

231 lines
6.1 KiB

1 week ago
1 week ago
1 week ago
1 week ago
1 week ago
1 week 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. # Increase max DB size to fit all the users
  69. olcDbMaxSize = "10737418240";
  70. olcAccess = [
  71. # Custom access rules for userPassword attributes
  72. ''{0}to attrs=userPassword
  73. by self read
  74. by anonymous auth
  75. by * none
  76. ''
  77. # Synced is managed by sync
  78. ''{1}to dn.subtree="ou=synced,ou=users,dc=informatik,dc=hs-fulda,dc=de"
  79. by dn.base="cn=sync,dc=informatik,dc=hs-fulda,dc=de" manage
  80. by * break
  81. ''
  82. # Allow login to read users
  83. ''{2}to dn.subtree="ou=users,dc=informatik,dc=hs-fulda,dc=de"
  84. by dn.base="cn=login,dc=informatik,dc=hs-fulda,dc=de" read
  85. by self read
  86. by * break
  87. ''
  88. # Prevent access
  89. ''{3}to *
  90. by * none
  91. ''
  92. ];
  93. };
  94. children = {
  95. "olcOverlay={0}remoteauth" = {
  96. attrs = {
  97. objectClass = [ "olcOverlayConfig" "olcRemoteAuthCfg" ];
  98. olcOverlay = "{0}remoteauth";
  99. olcRemoteAuthTLS = "starttls=yes tls_cacert=\"/etc/ssl/certs/ca-certificates.crt\"";
  100. olcRemoteAuthDNAttribute = "seeAlso";
  101. olcRemoteAuthDomainAttribute = "associatedDomain";
  102. olcRemoteAuthDefaultDomain = "upstream";
  103. olcRemoteAuthDefaultRealm = "file://${config.sops.secrets."ldap/upstream".path}";
  104. olcRemoteAuthRetryCount = "3";
  105. olcRemoteAuthStore = "false";
  106. };
  107. };
  108. };
  109. };
  110. };
  111. };
  112. declarativeContents = {
  113. "dc=informatik,dc=hs-fulda,dc=de" = ''
  114. dn: dc=informatik,dc=hs-fulda,dc=de
  115. objectClass: domain
  116. dc: informatik
  117. dn: ou=users,dc=informatik,dc=hs-fulda,dc=de
  118. objectClass: organizationalUnit
  119. ou: users
  120. dn: ou=synced,ou=users,dc=informatik,dc=hs-fulda,dc=de
  121. objectClass: organizationalUnit
  122. ou: users
  123. dn: cn=sync,dc=informatik,dc=hs-fulda,dc=de
  124. objectClass: applicationProcess
  125. objectClass: simpleSecurityObject
  126. objectClass: top
  127. cn: sync
  128. userPassword: {SSHA}Kf5ViggnBdUAPJ3/X5F80Qf/tXOzGI9G
  129. dn: cn=login,dc=informatik,dc=hs-fulda,dc=de
  130. objectClass: applicationProcess
  131. objectClass: simpleSecurityObject
  132. objectClass: top
  133. cn: login
  134. userPassword: {SSHA}esWkdMFThbFD0gSE5tC+jJ1rjwfUuI0p
  135. '';
  136. };
  137. };
  138. systemd.services."openldap" = {
  139. environment = {
  140. SASL_PATH = pkgs.writeTextFile {
  141. name = "openldap-sasl-path";
  142. destination = "/slapd.conf";
  143. text = ''
  144. pwcheck_method: saslauthd
  145. saslauthd_path: /var/run/saslauthd/mux
  146. mech_list: GSSAPI EXTERNAL PLAIN NTLM
  147. '';
  148. };
  149. };
  150. onSuccess = [ "ldap-sync.service" ];
  151. };
  152. systemd.services."ldap-sync" = {
  153. script = "${ldap-sync}/bin/ldap-sync";
  154. startAt = "hourly";
  155. requisite = [ "openldap.service" ];
  156. # Flush caches
  157. postStop = ''
  158. ${config.services.nscd.package}/bin/nscd --invalidate=group
  159. ${config.services.nscd.package}/bin/nscd --invalidate=passwd
  160. '';
  161. };
  162. sops.secrets = {
  163. "ldap/root/password" = {
  164. sopsFile = ./secrets/ldap.yaml;
  165. owner = "openldap";
  166. };
  167. "ldap/upstream" = {
  168. sopsFile = ./secrets/ldap.yaml;
  169. owner = "openldap";
  170. };
  171. "ldap/tls/key" = {
  172. sopsFile = ./secrets/ldap.tls.key;
  173. format = "binary";
  174. owner = "openldap";
  175. };
  176. "ldap/tls/crt" = {
  177. sopsFile = ./secrets/ldap.tls.crt;
  178. format = "binary";
  179. owner = "openldap";
  180. };
  181. "ldap/sync/config" = {
  182. sopsFile = ./secrets/ldap.yaml;
  183. };
  184. };
  185. networking.firewall.allowedTCPPorts = [
  186. 22
  187. 389
  188. 636
  189. ];
  190. }