diff options
Diffstat (limited to 'system-profiles/initrd-ssh')
| -rw-r--r-- | system-profiles/initrd-ssh/default.nix | 47 | ||||
| -rw-r--r-- | system-profiles/initrd-ssh/host-keys/vidhar-private.yaml | 31 | ||||
| -rw-r--r-- | system-profiles/initrd-ssh/host-keys/vidhar-public.yaml | 4 | ||||
| -rw-r--r-- | system-profiles/initrd-ssh/module.nix | 216 |
4 files changed, 0 insertions, 298 deletions
diff --git a/system-profiles/initrd-ssh/default.nix b/system-profiles/initrd-ssh/default.nix deleted file mode 100644 index ef469343..00000000 --- a/system-profiles/initrd-ssh/default.nix +++ /dev/null | |||
| @@ -1,47 +0,0 @@ | |||
| 1 | { hostName, config, pkgs, lib, ... }: | ||
| 2 | |||
| 3 | with lib; | ||
| 4 | |||
| 5 | { | ||
| 6 | config = { | ||
| 7 | boot.initrd = { | ||
| 8 | network = { | ||
| 9 | enable = true; | ||
| 10 | ssh = { | ||
| 11 | enable = true; | ||
| 12 | hostKeys = [ "/etc/ssh/ssh_host_ed25519_key" "/etc/ssh/ssh_host_rsa_key" ]; | ||
| 13 | authorizedKeys = config.users.users.root.openssh.authorizedKeys.keys ++ map (kF: builtins.readFile kF) config.users.users.root.openssh.authorizedKeys.keyFiles; | ||
| 14 | |||
| 15 | extraConfig = '' | ||
| 16 | ForceCommand cryptsetup-askpass | ||
| 17 | ''; | ||
| 18 | }; | ||
| 19 | }; | ||
| 20 | |||
| 21 | secrets = with config.sops.secrets; { | ||
| 22 | "/etc/ssh/ssh_host_ed25519_key" = mkForce initrd_ssh_host_ed25519_key.path; | ||
| 23 | "/etc/ssh/ssh_host_rsa_key" = mkForce initrd_ssh_host_rsa_key.path; | ||
| 24 | }; | ||
| 25 | |||
| 26 | extraFiles = let | ||
| 27 | mkPubkey = typ: pkgs.runCommand "ssh_host_${typ}_key.pub" { buildInputs = with pkgs; [ yq ]; } '' | ||
| 28 | yq -r '.${typ}' ${./host-keys + "/${hostName}-public.yaml"} > $out | ||
| 29 | ''; | ||
| 30 | in { | ||
| 31 | "/etc/ssh/ssh_host_rsa_key.pub".source = mkPubkey "rsa"; | ||
| 32 | "/etc/ssh/ssh_host_ed25519_key.pub".source = mkPubkey "ed25519"; | ||
| 33 | }; | ||
| 34 | }; | ||
| 35 | |||
| 36 | sops.secrets = { | ||
| 37 | initrd_ssh_host_rsa_key = { | ||
| 38 | key = "rsa"; | ||
| 39 | sopsFile = ./host-keys + "/${hostName}-private.yaml"; | ||
| 40 | }; | ||
| 41 | initrd_ssh_host_ed25519_key = { | ||
| 42 | key = "ed25519"; | ||
| 43 | sopsFile = ./host-keys + "/${hostName}-private.yaml"; | ||
| 44 | }; | ||
| 45 | }; | ||
| 46 | }; | ||
| 47 | } | ||
diff --git a/system-profiles/initrd-ssh/host-keys/vidhar-private.yaml b/system-profiles/initrd-ssh/host-keys/vidhar-private.yaml deleted file mode 100644 index 2254f7b6..00000000 --- a/system-profiles/initrd-ssh/host-keys/vidhar-private.yaml +++ /dev/null | |||
| @@ -1,31 +0,0 @@ | |||
| 1 | rsa: ENC[AES256_GCM,data:u5NbMQLLIHzcH1oUvGbxNRiK0DUX84tQ/sIaUae370jkTdiRTrwTSOX+TielSgwEyLo7KAOVO0olbEheJjP6tLlOExDZZCO09YBqvZksSkT+0irnpA9K3oIPZVAKMgoGMRvr8I1fWn9ZQGpzFBrEOGN9+f5bJ4y44IHUxepvCalQQb4wFNqZv/tPAH8ku18v4U6Nocqfj4PfECzCaslRBY3tCiZ4ipbnBSaLM5KHDZWHYti88kG67xwupJ/v48wLtKwVQIzTvY+oN4ZnwRGzmqHfuz8EET/BrLZ/WLJtf3JuEqTZQIw24qP/8Q2/zEH40IzxeZvP5D1MzPpEVamU1FM9WAtwOQkrpoZL8+lRpcJvQdqbZqz1qLGU0MZmI++MudxebuIZ5g36y7xLAcYS3OuaekG66e6fhp1txqK09HB3wxXJVq7B13ip0MRDl1xfbVkSfDFBSSZ7hCueFS+2ee8d7fha7Y1wINZ5eU9WCjRG3yD/vMvLbJjRDK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kxwe5uagCIupjP6lqnkzAuf4Dc1jIfXdEoEV0jtVUSjgeXHPymSPkg1J95VUMEIfVbncFxvzSQngv5kuE/XoH3adujuKR9fC/Z5u08hSjTzGQzMdgZk5BYvy7AWYN8SmfWcyAkClywj9DCzgaw8KY+MzSxQWrhAt3rqobmtVoJoJ5oc1/l6m5pPsyb/Ex2wwFh/rWm7SNfhW/Ev/ZuIX7llL7HCgXxhiCJIl9X4jkwF6OvslvK1wYgJ6rvGQwyM+/7Q8/rX1KJnq3qA93SfDi/fPLuJxcMYj/mZ/bluG4bsTFaUzDf1l6C251p24JJt3F0o1RTF1zgACrdhKK1BN79sQt4xDnHawCCwEkBbdeE0pmml1i3aUy5thl,iv:6cYTjmJd44Uc+uwnIZI1CUuKhYNGHuWa8wr7CYJY3RY=,tag:+PGy7aVeHyLH+B04Nj8BRQ==,type:str] | ||
| 3 | sops: | ||
| 4 | kms: [] | ||
| 5 | gcp_kms: [] | ||
| 6 | azure_kv: [] | ||
| 7 | hc_vault: [] | ||
| 8 | age: | ||
| 9 | - recipient: age1rmmhetcmllq0ahl5qznlr0eya2zdxwl9h6y5wnl97d2wtyx5t99sm2u866 | ||
| 10 | enc: | | ||
| 11 | -----BEGIN AGE ENCRYPTED FILE----- | ||
| 12 | YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSBiem1qTmo4bjhUUDFDM2ds | ||
| 13 | Y3dRc3RJT1YvYkJXblZsbHF4NTRWUGsvWTE4CnRDV1IwSjdVUUttWXVETElQYXRJ | ||
| 14 | SmxvL0dDQ3E5ZEpCMFZ1T3EvU3Y4Vm8KLS0tIFlVMnorMGdCZFJWYkp6bFV6eUVB | ||
| 15 | Vjd5dXd3a1UrSnAyd2xGbXVyVFN1bWcKYm8yCZdvyNn9DzAyGrVt8U25KPk8eXD2 | ||
| 16 | 051EQi99DS0U3BYRI+hhL41cm26nai1USU6Oo2PvxswYmObMDfKrPg== | ||
| 17 | -----END AGE ENCRYPTED FILE----- | ||
| 18 | - recipient: age1qffdqvy9arld9zd5a5cylt0n98xhcns5shxhrhwjq5g4qa844ejselaa4l | ||
| 19 | enc: | | ||
| 20 | -----BEGIN AGE ENCRYPTED FILE----- | ||
| 21 | YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSAra0FydFJpbHBXNms0aVUw | ||
| 22 | T3U4OXFJck1Ta0dvMHJ4ckRVaFQvcnhlQ1ZJCmx5UjhZNGNEb0N5NUJ5cFdCc2Nq | ||
| 23 | anRrc1BOb2NHVFY1cG5xMUY0WGx2cDAKLS0tIFQ3aU1hM1BlOWw2RkZPYlp3SkYx | ||
| 24 | d0Fhd0tKVnFkc0dOTXFaaG1OcmY2YVkKb0xFefYhjcjZ+wL9oGoN52hqvLPeXtkT | ||
| 25 | xOinOyzi2Cm+wsZu4BZlTabVoAU8uu810D315Ex7+bFL8f8KROEaOg== | ||
| 26 | -----END AGE ENCRYPTED FILE----- | ||
| 27 | lastmodified: "2021-08-03T14:47:32Z" | ||
| 28 | mac: ENC[AES256_GCM,data:gWbmGMZ+/Ts7NP9J1q/kjQmJ7V6lJ5xFpjZNJ+aTOmkz7a6sG8SRvNEW/qrpJfCzEFdQJYhOW3X9FhWpb5U6j4gINrgqUGdusQpw0PmIieC5tCPQPlTPHMReK0xaZ3NViMdHJhGdtehGfPqAtA3Bifn2ZZzOrzTOaPN2fH11fZw=,iv:FhKERfmDPmWn5ZKkuHWMc/vINpmJTr0jZ1iCkSgAUEs=,tag:ibe+m8vz6b+a+as5mz4+eA==,type:str] | ||
| 29 | pgp: [] | ||
| 30 | unencrypted_suffix: _unencrypted | ||
| 31 | version: 3.7.1 | ||
diff --git a/system-profiles/initrd-ssh/host-keys/vidhar-public.yaml b/system-profiles/initrd-ssh/host-keys/vidhar-public.yaml deleted file mode 100644 index af521564..00000000 --- a/system-profiles/initrd-ssh/host-keys/vidhar-public.yaml +++ /dev/null | |||
| @@ -1,4 +0,0 @@ | |||
| 1 | rsa: | | ||
| 2 | ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCs0bzHfJBgG5AP0nSUgkGBoxRLRDu9FMfwJN3NFLeUf1axmKPja0mO+ddUQeLobDcl4zU4VyBxdd5qrrSkAufM76GJJFqXGJs98WudELXD5I4/o99Fh7xSf6QtafftTbjUfcTzX9t30Cb109Ppv1sKANcfLOx5EWH4TX3WK0vWTZHlS/OHvojwx7Y8/8dYbA96neMSBK5ceDu/VAG/JHYz5JgtDczSKUnbh026xav8rv9Sd3paC42XNlWSkjvpXxXoJ+ta5SuOXAl+4gPDvjgxK+QJ+yglhJma7LdHt7tqQjy/R0v5+wMzbNCKdQ1E9GiCw+hSE3mbCj/PVFy+J1oadBta+qF5SKCSurFeCRaehTTz6Qynxu0OxLQV/OHX6yC2i9OVJOs0PNGiot9pjxoSKyKrlT7gshP6MRfC1F330oVDSOPAkFLFFkTJtzb0ifJ5s1LyM0LyJAPso+5ytCcSOoQpcKd54VbSTYXBcnWtRFtEzUDIeLDeRWgQ0zJsef8= | ||
| 3 | ed25519: | | ||
| 4 | ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIBb+RVaConednm1DsYh18ttUEs/FJ7+E3g0YGbZcJthp | ||
diff --git a/system-profiles/initrd-ssh/module.nix b/system-profiles/initrd-ssh/module.nix deleted file mode 100644 index db973b72..00000000 --- a/system-profiles/initrd-ssh/module.nix +++ /dev/null | |||
| @@ -1,216 +0,0 @@ | |||
| 1 | { flakeInputs, config, lib, pkgs, ... }: | ||
| 2 | |||
| 3 | with lib; | ||
| 4 | |||
| 5 | let | ||
| 6 | |||
| 7 | cfg = config.boot.initrd.network.ssh; | ||
| 8 | |||
| 9 | in | ||
| 10 | |||
| 11 | { | ||
| 12 | disabledModules = [ "system/boot/initrd-ssh.nix" ]; | ||
| 13 | |||
| 14 | options.boot.initrd.network.ssh = { | ||
| 15 | enable = mkOption { | ||
| 16 | type = types.bool; | ||
| 17 | default = false; | ||
| 18 | description = '' | ||
| 19 | Start SSH service during initrd boot. It can be used to debug failing | ||
| 20 | boot on a remote server, enter pasphrase for an encrypted partition etc. | ||
| 21 | Service is killed when stage-1 boot is finished. | ||
| 22 | |||
| 23 | The sshd configuration is largely inherited from | ||
| 24 | {option}`services.openssh`. | ||
| 25 | ''; | ||
| 26 | }; | ||
| 27 | |||
| 28 | port = mkOption { | ||
| 29 | type = types.port; | ||
| 30 | default = 22; | ||
| 31 | description = '' | ||
| 32 | Port on which SSH initrd service should listen. | ||
| 33 | ''; | ||
| 34 | }; | ||
| 35 | |||
| 36 | shell = mkOption { | ||
| 37 | type = types.str; | ||
| 38 | default = "/bin/ash"; | ||
| 39 | description = '' | ||
| 40 | Login shell of the remote user. Can be used to limit actions user can do. | ||
| 41 | ''; | ||
| 42 | }; | ||
| 43 | |||
| 44 | hostKeys = mkOption { | ||
| 45 | type = types.listOf (types.either types.str types.path); | ||
| 46 | default = []; | ||
| 47 | example = [ | ||
| 48 | "/etc/secrets/initrd/ssh_host_rsa_key" | ||
| 49 | "/etc/secrets/initrd/ssh_host_ed25519_key" | ||
| 50 | ]; | ||
| 51 | description = '' | ||
| 52 | Specify SSH host keys to import into the initrd. | ||
| 53 | |||
| 54 | To generate keys, use | ||
| 55 | {manpage}`ssh-keygen(1)` | ||
| 56 | as root: | ||
| 57 | |||
| 58 | ``` | ||
| 59 | ssh-keygen -t rsa -N "" -f /etc/secrets/initrd/ssh_host_rsa_key | ||
| 60 | ssh-keygen -t ed25519 -N "" -f /etc/secrets/initrd/ssh_host_ed25519_key | ||
| 61 | ``` | ||
| 62 | |||
| 63 | ::: {.warning} | ||
| 64 | Unless your bootloader supports initrd secrets, these keys | ||
| 65 | are stored insecurely in the global Nix store. Do NOT use | ||
| 66 | your regular SSH host private keys for this purpose or | ||
| 67 | you'll expose them to regular users! | ||
| 68 | |||
| 69 | Additionally, even if your initrd supports secrets, if | ||
| 70 | you're using initrd SSH to unlock an encrypted disk then | ||
| 71 | using your regular host keys exposes the private keys on | ||
| 72 | your unencrypted boot partition. | ||
| 73 | ::: | ||
| 74 | ''; | ||
| 75 | }; | ||
| 76 | |||
| 77 | manageHostKeySecrets = mkEnableOption "automatically managing initrd secrets for configured hostkeys"; | ||
| 78 | |||
| 79 | authorizedKeys = mkOption { | ||
| 80 | type = types.listOf types.str; | ||
| 81 | default = config.users.users.root.openssh.authorizedKeys.keys; | ||
| 82 | defaultText = literalExpression "config.users.users.root.openssh.authorizedKeys.keys"; | ||
| 83 | description = '' | ||
| 84 | Authorized keys for the root user on initrd. | ||
| 85 | ''; | ||
| 86 | }; | ||
| 87 | |||
| 88 | extraConfig = mkOption { | ||
| 89 | type = types.lines; | ||
| 90 | default = ""; | ||
| 91 | description = "Verbatim contents of {file}`sshd_config`."; | ||
| 92 | }; | ||
| 93 | }; | ||
| 94 | |||
| 95 | imports = | ||
| 96 | map (opt: mkRemovedOptionModule ([ "boot" "initrd" "network" "ssh" ] ++ [ opt ]) '' | ||
| 97 | The initrd SSH functionality now uses OpenSSH rather than Dropbear. | ||
| 98 | |||
| 99 | If you want to keep your existing initrd SSH host keys, convert them with | ||
| 100 | $ dropbearconvert dropbear openssh dropbear_host_$type_key ssh_host_$type_key | ||
| 101 | and then set options.boot.initrd.network.ssh.hostKeys. | ||
| 102 | '') [ "hostRSAKey" "hostDSSKey" "hostECDSAKey" ]; | ||
| 103 | |||
| 104 | config = let | ||
| 105 | # Nix complains if you include a store hash in initrd path names, so | ||
| 106 | # as an awful hack we drop the first character of the hash. | ||
| 107 | initrdKeyPath = path: if isString path | ||
| 108 | then path | ||
| 109 | else let name = builtins.baseNameOf path; in | ||
| 110 | builtins.unsafeDiscardStringContext ("/etc/ssh/" + | ||
| 111 | substring 1 (stringLength name) name); | ||
| 112 | |||
| 113 | sshdCfg = config.services.openssh; | ||
| 114 | |||
| 115 | sshdConfig = '' | ||
| 116 | Port ${toString cfg.port} | ||
| 117 | |||
| 118 | PasswordAuthentication no | ||
| 119 | KbdInteractiveAuthentication no | ||
| 120 | |||
| 121 | ${flip concatMapStrings cfg.hostKeys (path: '' | ||
| 122 | HostKey ${initrdKeyPath path} | ||
| 123 | '')} | ||
| 124 | |||
| 125 | KexAlgorithms ${concatStringsSep "," sshdCfg.settings.KexAlgorithms} | ||
| 126 | Ciphers ${concatStringsSep "," sshdCfg.settings.Ciphers} | ||
| 127 | MACs ${concatStringsSep "," sshdCfg.settings.Macs} | ||
| 128 | |||
| 129 | LogLevel ${sshdCfg.settings.LogLevel} | ||
| 130 | |||
| 131 | ${if sshdCfg.settings.UseDns then '' | ||
| 132 | UseDNS yes | ||
| 133 | '' else '' | ||
| 134 | UseDNS no | ||
| 135 | ''} | ||
| 136 | |||
| 137 | ${cfg.extraConfig} | ||
| 138 | ''; | ||
| 139 | in mkIf (config.boot.initrd.network.enable && cfg.enable) { | ||
| 140 | assertions = [ | ||
| 141 | { | ||
| 142 | assertion = cfg.authorizedKeys != []; | ||
| 143 | message = "You should specify at least one authorized key for initrd SSH"; | ||
| 144 | } | ||
| 145 | |||
| 146 | { | ||
| 147 | assertion = cfg.hostKeys != []; | ||
| 148 | message = '' | ||
| 149 | You must now pre-generate the host keys for initrd SSH. | ||
| 150 | See the boot.initrd.network.ssh.hostKeys documentation | ||
| 151 | for instructions. | ||
| 152 | ''; | ||
| 153 | } | ||
| 154 | ]; | ||
| 155 | |||
| 156 | boot.initrd.extraUtilsCommands = '' | ||
| 157 | copy_bin_and_libs ${pkgs.openssh}/bin/sshd | ||
| 158 | cp -pv ${pkgs.glibc.out}/lib/libnss_files.so.* $out/lib | ||
| 159 | ''; | ||
| 160 | |||
| 161 | boot.initrd.extraUtilsCommandsTest = '' | ||
| 162 | # sshd requires a host key to check config, so we pass in the test's | ||
| 163 | tmpkey="$(mktemp initrd-ssh-testkey.XXXXXXXXXX)" | ||
| 164 | cp ${escapeShellArg (flakeInputs.nixpkgs.outPath + "/nixos/tests/initrd-network-ssh/ssh_host_ed25519_key")} "$tmpkey" | ||
| 165 | # keys from Nix store are world-readable, which sshd doesn't like | ||
| 166 | chmod 600 "$tmpkey" | ||
| 167 | echo -n ${escapeShellArg sshdConfig} | | ||
| 168 | $out/bin/sshd -t -f /dev/stdin \ | ||
| 169 | -h "$tmpkey" | ||
| 170 | rm "$tmpkey" | ||
| 171 | ''; | ||
| 172 | |||
| 173 | boot.initrd.network.postCommands = '' | ||
| 174 | echo '${cfg.shell}' > /etc/shells | ||
| 175 | echo 'root:x:0:0:root:/root:${cfg.shell}' > /etc/passwd | ||
| 176 | echo 'sshd:x:1:1:sshd:/var/empty:/bin/nologin' >> /etc/passwd | ||
| 177 | echo 'passwd: files' > /etc/nsswitch.conf | ||
| 178 | |||
| 179 | mkdir -p /var/log /var/empty | ||
| 180 | touch /var/log/lastlog | ||
| 181 | |||
| 182 | mkdir -p /etc/ssh | ||
| 183 | echo -n ${escapeShellArg sshdConfig} > /etc/ssh/sshd_config | ||
| 184 | |||
| 185 | echo "export PATH=$PATH" >> /etc/profile | ||
| 186 | echo "export LD_LIBRARY_PATH=$LD_LIBRARY_PATH" >> /etc/profile | ||
| 187 | |||
| 188 | mkdir -p /root/.ssh | ||
| 189 | ${concatStrings (map (key: '' | ||
| 190 | echo ${escapeShellArg key} >> /root/.ssh/authorized_keys | ||
| 191 | '') cfg.authorizedKeys)} | ||
| 192 | |||
| 193 | ${flip concatMapStrings cfg.hostKeys (path: '' | ||
| 194 | # keys from Nix store are world-readable, which sshd doesn't like | ||
| 195 | chmod 0600 "${initrdKeyPath path}" | ||
| 196 | '')} | ||
| 197 | |||
| 198 | /bin/sshd -e | ||
| 199 | ''; | ||
| 200 | |||
| 201 | boot.initrd.postMountCommands = '' | ||
| 202 | # Stop sshd cleanly before stage 2. | ||
| 203 | # | ||
| 204 | # If you want to keep it around to debug post-mount SSH issues, | ||
| 205 | # run `touch /.keep_sshd` (either from an SSH session or in | ||
| 206 | # another initrd hook like preDeviceCommands). | ||
| 207 | if ! [ -e /.keep_sshd ]; then | ||
| 208 | pkill -x sshd | ||
| 209 | fi | ||
| 210 | ''; | ||
| 211 | |||
| 212 | boot.initrd.secrets = mkIf cfg.manageHostKeySecrets (listToAttrs | ||
| 213 | (map (path: nameValuePair (initrdKeyPath path) path) cfg.hostKeys)); | ||
| 214 | }; | ||
| 215 | |||
| 216 | } | ||
