git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #10460 from abbradar/remote-askpass

Support network in initrd and entering LUKS passphrase via SSH

commitd4c27381dc3fff79a4c7aa54b35d4df7169b71bd authorNikolay Amiantov <ab@fmap.me> dateTue 10 Nov 2015, 16:41 (11 years ago) parenta8a58fa12fcfe3
11 files changed+252−27
nixos/modules/module-list.nix+1 nixos/modules/system/boot/initrd-network.nix+149 nixos/modules/system/boot/luksroot.nix+19−7 nixos/modules/system/boot/stage-1-init.sh+4 nixos/modules/system/boot/stage-1.nix+10−2 pkgs/build-support/nuke-references/builder.sh+20−5 pkgs/build-support/nuke-references/default.nix+3−2 pkgs/os-specific/linux/mkinitcpio-nfs-utils/default.nix+26 pkgs/tools/networking/dropbear/default.nix+4−2 pkgs/tools/networking/dropbear/pass-path.patch+14−9 pkgs/top-level/all-packages.nix+2
nixos/modules/module-list.nix+1
@@ -441,6 +441,7 @@
441441 ./system/activation/top-level.nix
442442 ./system/boot/coredump.nix
443443 ./system/boot/emergency-mode.nix
444+ ./system/boot/initrd-network.nix
444445 ./system/boot/kernel.nix
445446 ./system/boot/kexec.nix
446447 ./system/boot/loader/efi.nix
nixos/modules/system/boot/initrd-network.nixnew file+149
@@ -0,0 +1,149 @@
1+{ config, lib, pkgs, ... }:
2+
3+with lib;
4+
5+let
6+ cfg = config.boot.initrd.network;
7+
8+in
9+{
10+
11+ options = {
12+
13+ boot.initrd.network.enable = mkOption {
14+ type = types.bool;
15+ default = false;
16+ description = ''
17+ Add network connectivity support to initrd.
18+
19+ Network options are configured via <literal>ip</literal> kernel
20+ option, according to the kernel documentation.
21+ '';
22+ };
23+
24+ boot.initrd.network.ssh.enable = mkOption {
25+ type = types.bool;
26+ default = false;
27+ description = ''
28+ Start SSH service during initrd boot. It can be used to debug failing
29+ boot on a remote server, enter pasphrase for an encrypted partition etc.
30+ Service is killed when stage-1 boot is finished.
31+ '';
32+ };
33+
34+ boot.initrd.network.ssh.port = mkOption {
35+ type = types.int;
36+ default = 22;
37+ description = ''
38+ Port on which SSH initrd service should listen.
39+ '';
40+ };
41+
42+ boot.initrd.network.ssh.shell = mkOption {
43+ type = types.str;
44+ default = "/bin/ash";
45+ description = ''
46+ Login shell of the remote user. Can be used to limit actions user can do.
47+ '';
48+ };
49+
50+ boot.initrd.network.ssh.hostRSAKey = mkOption {
51+ type = types.nullOr types.path;
52+ default = null;
53+ description = ''
54+ RSA SSH private key file in the Dropbear format.
55+
56+ WARNING: This key is contained insecurely in the global Nix store. Do NOT
57+ use your regular SSH host private keys for this purpose or you'll expose
58+ them to regular users!
59+ '';
60+ };
61+
62+ boot.initrd.network.ssh.hostDSSKey = mkOption {
63+ type = types.nullOr types.path;
64+ default = null;
65+ description = ''
66+ DSS SSH private key file in the Dropbear format.
67+
68+ WARNING: This key is contained insecurely in the global Nix store. Do NOT
69+ use your regular SSH host private keys for this purpose or you'll expose
70+ them to regular users!
71+ '';
72+ };
73+
74+ boot.initrd.network.ssh.hostECDSAKey = mkOption {
75+ type = types.nullOr types.path;
76+ default = null;
77+ description = ''
78+ ECDSA SSH private key file in the Dropbear format.
79+
80+ WARNING: This key is contained insecurely in the global Nix store. Do NOT
81+ use your regular SSH host private keys for this purpose or you'll expose
82+ them to regular users!
83+ '';
84+ };
85+
86+ boot.initrd.network.ssh.authorizedKeys = mkOption {
87+ type = types.listOf types.str;
88+ default = config.users.extraUsers.root.openssh.authorizedKeys.keys;
89+ description = ''
90+ Authorized keys for the root user on initrd.
91+ '';
92+ };
93+
94+ };
95+
96+ config = mkIf cfg.enable {
97+
98+ boot.initrd.kernelModules = [ "af_packet" ];
99+
100+ boot.initrd.extraUtilsCommands = ''
101+ copy_bin_and_libs ${pkgs.mkinitcpio-nfs-utils}/bin/ipconfig
102+ '' + optionalString cfg.ssh.enable ''
103+ copy_bin_and_libs ${pkgs.dropbear}/bin/dropbear
104+
105+ cp -pv ${pkgs.glibc}/lib/libnss_files.so.* $out/lib
106+ '';
107+
108+ boot.initrd.extraUtilsCommandsTest = optionalString cfg.ssh.enable ''
109+ $out/bin/dropbear -V
110+ '';
111+
112+ boot.initrd.postEarlyDeviceCommands = ''
113+ # Search for interface definitions in command line
114+ for o in $(cat /proc/cmdline); do
115+ case $o in
116+ ip=*)
117+ ipconfig $o && hasNetwork=1
118+ ;;
119+ esac
120+ done
121+ '' + optionalString cfg.ssh.enable ''
122+ if [ -n "$hasNetwork" ]; then
123+ mkdir /dev/pts
124+ mount -t devpts devpts /dev/pts
125+
126+ mkdir -p /etc
127+ echo 'root:x:0:0:root:/root:${cfg.ssh.shell}' > /etc/passwd
128+ echo '${cfg.ssh.shell}' > /etc/shells
129+ echo 'passwd: files' > /etc/nsswitch.conf
130+
131+ mkdir -p /var/log
132+ touch /var/log/lastlog
133+
134+ mkdir -p /etc/dropbear
135+ ${optionalString (cfg.ssh.hostRSAKey != null) "ln -s ${cfg.ssh.hostRSAKey} /etc/dropbear/dropbear_rsa_host_key"}
136+ ${optionalString (cfg.ssh.hostDSSKey != null) "ln -s ${cfg.ssh.hostDSSKey} /etc/dropbear/dropbear_dss_host_key"}
137+ ${optionalString (cfg.ssh.hostECDSAKey != null) "ln -s ${cfg.ssh.hostECDSAKey} /etc/dropbear/dropbear_ecdsa_host_key"}
138+
139+ mkdir -p /root/.ssh
140+ ${concatStrings (map (key: ''
141+ echo -n ${escapeShellArg key} >> /root/.ssh/authorized_keys
142+ '') cfg.ssh.authorizedKeys)}
143+
144+ dropbear -s -j -k -E -m -p ${toString cfg.ssh.port}
145+ fi
146+ '';
147+
148+ };
149+}
nixos/modules/system/boot/luksroot.nix+19−7
@@ -32,9 +32,12 @@ let
3232 ''}
3333
3434 open_normally() {
35− cryptsetup luksOpen ${device} ${name} ${optionalString allowDiscards "--allow-discards"} \
35+ echo luksOpen ${device} ${name} ${optionalString allowDiscards "--allow-discards"} \
3636 ${optionalString (header != null) "--header=${header}"} \
37− ${optionalString (keyFile != null) "--key-file=${keyFile} ${optionalString (keyFileSize != null) "--keyfile-size=${toString keyFileSize}"}"}
37+ ${optionalString (keyFile != null) "--key-file=${keyFile} ${optionalString (keyFileSize != null) "--keyfile-size=${toString keyFileSize}"}"} \
38+ > /.luksopen_args
39+ cryptsetup-askpass
40+ rm /.luksopen_args
3841 }
3942
4043 ${optionalString (luks.yubikeySupport && (yubikey != null)) ''
@@ -418,6 +421,18 @@ in
418421 boot.initrd.extraUtilsCommands = ''
419422 copy_bin_and_libs ${pkgs.cryptsetup}/bin/cryptsetup
420423
424+ cat > $out/bin/cryptsetup-askpass <<EOF
425+ #!$out/bin/sh -e
426+ if [ -e /.luksopen_args ]; then
427+ cryptsetup \$(cat /.luksopen_args)
428+ killall cryptsetup
429+ else
430+ echo "Passphrase is not requested now"
431+ exit 1
432+ fi
433+ EOF
434+ chmod +x $out/bin/cryptsetup-askpass
435+
421436 ${optionalString luks.yubikeySupport ''
422437 copy_bin_and_libs ${pkgs.ykpers}/bin/ykchalresp
423438 copy_bin_and_libs ${pkgs.ykpers}/bin/ykinfo
@@ -432,6 +447,8 @@ in
432447
433448 cat > $out/bin/openssl-wrap <<EOF
434449 #!$out/bin/sh
450+ export OPENSSL_CONF=$out/etc/ssl/openssl.cnf
451+ $out/bin/openssl "\$@"
435452 EOF
436453 chmod +x $out/bin/openssl-wrap
437454 ''}
@@ -442,11 +459,6 @@ in
442459 ${optionalString luks.yubikeySupport ''
443460 $out/bin/ykchalresp -V
444461 $out/bin/ykinfo -V
445− cat > $out/bin/openssl-wrap <<EOF
446− #!$out/bin/sh
447− export OPENSSL_CONF=$out/etc/ssl/openssl.cnf
448− $out/bin/openssl "\$@"
449− EOF
450462 $out/bin/openssl-wrap version
451463 ''}
452464 '';
nixos/modules/system/boot/stage-1-init.sh+4
@@ -149,6 +149,10 @@ udevadm trigger --action=add
149149udevadm settle
150150
151151
152+# Additional devices initialization.
153+@postEarlyDeviceCommands@
154+
155+
152156# Load boot-time keymap before any LVM/LUKS initialization
153157@extraUtils@/bin/busybox loadkmap < "@busyboxKeymap@"
154158
nixos/modules/system/boot/stage-1.nix+10−2
@@ -104,7 +104,7 @@ let
104104 stripDirs "lib bin" "-s"
105105
106106 # Run patchelf to make the programs refer to the copied libraries.
107− for i in $out/bin/* $out/lib/*; do if ! test -L $i; then nuke-refs $i; fi; done
107+ for i in $out/bin/* $out/lib/*; do if ! test -L $i; then nuke-refs -e $out $i; fi; done
108108
109109 for i in $out/bin/*; do
110110 if ! test -L $i; then
@@ -203,7 +203,7 @@ let
203203 inherit (config.boot) resumeDevice devSize runSize;
204204
205205 inherit (config.boot.initrd) checkJournalingFS
206− preLVMCommands postDeviceCommands postMountCommands kernelModules;
206+ postEarlyDeviceCommands preLVMCommands postDeviceCommands postMountCommands kernelModules;
207207
208208 resumeDevices = map (sd: if sd ? device then sd.device else "/dev/disk/by-label/${sd.label}")
209209 (filter (sd: (sd ? label || hasPrefix "/dev/" sd.device) && !sd.randomEncryption) config.swapDevices);
@@ -313,6 +313,14 @@ in
313313 '';
314314 };
315315
316+ boot.initrd.postEarlyDeviceCommands = mkOption {
317+ default = "";
318+ type = types.lines;
319+ description = ''
320+ Shell commands to be executed early after creation of device nodes.
321+ '';
322+ };
323+
316324 boot.initrd.postMountCommands = mkOption {
317325 default = "";
318326 type = types.lines;
pkgs/build-support/nuke-references/builder.sh+20−5
@@ -3,11 +3,26 @@ source $stdenv/setup
33mkdir -p $out/bin
44cat > $out/bin/nuke-refs <<EOF
55#! $SHELL -e
6−for i in \$*; do
7− if test ! -L \$i -a -f \$i; then
8− cat \$i | sed "s|$NIX_STORE/[a-z0-9]*-|$NIX_STORE/eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee-|g" > \$i.tmp
9− if test -x \$i; then chmod +x \$i.tmp; fi
10− mv \$i.tmp \$i
6+
7+excludes=""
8+while getopts e: o; do
9+ case "\$o" in
10+ e) storeId=\$(echo "\$OPTARG" | sed -n "s|^$NIX_STORE/\\([a-z0-9]\{32\}\\)-.*|\1|p")
11+ if [ -z "\$storeId" ]; then
12+ echo "-e argument must be a Nix store path"
13+ exit 1
14+ fi
15+ excludes="\$excludes(?!\$storeId)"
16+ ;;
17+ esac
18+done
19+shift \$((\$OPTIND-1))
20+
21+for i in "\$@"; do
22+ if test ! -L "\$i" -a -f "\$i"; then
23+ cat "\$i" | $perl/bin/perl -pe "s|$NIX_STORE/\$excludes[a-z0-9]{32}-|$NIX_STORE/eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee-|g" > "\$i.tmp"
24+ if test -x "\$i"; then chmod +x "\$i.tmp"; fi
25+ mv "\$i.tmp" "\$i"
1126 fi
1227done
1328EOF
pkgs/build-support/nuke-references/default.nix+3−2
@@ -3,9 +3,10 @@
33# path (/nix/store/eeee...). This is useful for getting rid of
44# dependencies that you know are not actually needed at runtime.
55
6−{stdenv}:
6+{ stdenv, perl }:
77
88stdenv.mkDerivation {
99 name = "nuke-references";
1010 builder = ./builder.sh;
11−}
11+ inherit perl;
12+}
pkgs/os-specific/linux/mkinitcpio-nfs-utils/default.nixnew file+26
@@ -0,0 +1,26 @@
1+{ stdenv, fetchurl, xz }:
2+
3+stdenv.mkDerivation rec {
4+ name = "mkinitcpio-nfs-utils-0.3";
5+
6+ src = fetchurl {
7+ url = "https://sources.archlinux.org/other/mkinitcpio/${name}.tar.xz";
8+ sha256 = "0fc93sfk41ycpa33083kyd7i4y00ykpbhj5qlw611bjghj4x946j";
9+ # ugh, upstream...
10+ name = "${name}.tar.gz";
11+ };
12+
13+ makeFlags = [ "DESTDIR=$(out)" "bindir=/bin" ];
14+
15+ postInstall = ''
16+ rm -rf $out/usr
17+ '';
18+
19+ meta = with stdenv.lib; {
20+ homepage = https://archlinux.org/;
21+ description = "ipconfig and nfsmount tools for root on NFS, ported from klibc";
22+ license = licenses.gpl2;
23+ platforms = platforms.linux;
24+ maintainers = with maintainers; [ abbradar ];
25+ };
26+}
pkgs/tools/networking/dropbear/default.nix+4−2
@@ -35,9 +35,11 @@ stdenv.mkDerivation rec {
3535
3636 buildInputs = [ zlib ];
3737
38− meta = {
38+ meta = with stdenv.lib; {
3939 homepage = http://matt.ucc.asn.au/dropbear/dropbear.html;
4040 description = "An small footprint implementation of the SSH 2 protocol";
41− license = stdenv.lib.licenses.mit;
41+ license = licenses.mit;
42+ maintainers = with maintainers; [ abbradar ];
43+ platforms = platforms.unix;
4244 };
4345}
pkgs/tools/networking/dropbear/pass-path.patch+14−9
@@ -1,31 +1,36 @@
11diff --git a/svr-chansession.c b/svr-chansession.c
2−index 23dad8c..32cac13 100644
2+index e44299e..7ef750a 100644
33--- a/svr-chansession.c
44+++ b/svr-chansession.c
5−@@ -823,6 +823,7 @@ static void addchildpid(struct ChanSess *chansess, pid_t pid) {
5+@@ -893,6 +893,8 @@ static void addchildpid(struct ChanSess *chansess, pid_t pid) {
66 static void execchild(void *user_data) {
77 struct ChanSess *chansess = user_data;
88 char *usershell = NULL;
99+ const char *path = DEFAULT_PATH;
10++ const char *ldpath = NULL;
1011
11− /* with uClinux we'll have vfork()ed, so don't want to overwrite the
12− * hostkey. can't think of a workaround to clear it */
13−@@ -835,6 +836,9 @@ static void execchild(void *user_data) {
14− reseedrandom();
12+ /* with uClinux we'll have vfork()ed, so don't want to overwrite the
13+ * hostkey. can't think of a workaround to clear it */
14+@@ -905,6 +907,10 @@ static void execchild(void *user_data) {
15+ seedrandom();
1516 #endif
1617
17−+ if (getenv("PATH"))
18−+ path = getenv("PATH");
18++ if (getenv("PATH"))
19++ path = getenv("PATH");
20++ ldpath = getenv("LD_LIBRARY_PATH");
1921+
2022 /* clear environment */
2123 /* if we're debugging using valgrind etc, we need to keep the LD_PRELOAD
2224 * etc. This is hazardous, so should only be used for debugging. */
23−@@ -878,7 +882,7 @@ static void execchild(void *user_data) {
25+@@ -948,7 +954,10 @@ static void execchild(void *user_data) {
2426 addnewvar("LOGNAME", ses.authstate.pw_name);
2527 addnewvar("HOME", ses.authstate.pw_dir);
2628 addnewvar("SHELL", get_user_shell());
2729- addnewvar("PATH", DEFAULT_PATH);
2830+ addnewvar("PATH", path);
31++ if (ldpath != NULL) {
32++ addnewvar("LD_LIBRARY_PATH", ldpath);
33++ }
2934 if (chansess->term != NULL) {
3035 addnewvar("TERM", chansess->term);
3136 }
pkgs/top-level/all-packages.nix+2
@@ -10169,6 +10169,8 @@ let
1016910169 systemd = systemd.override { enableKDbus = true; };
1017010170 };
1017110171
10172+ mkinitcpio-nfs-utils = callPackage ../os-specific/linux/mkinitcpio-nfs-utils { };
10173+
1017210174 module_init_tools = callPackage ../os-specific/linux/module-init-tools { };
1017310175
1017410176 aggregateModules = modules: