diff --git a/Cargo.lock b/Cargo.lock index c3a9bb1..28aebf2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,6 +8,43 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" +[[package]] +name = "aead" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1973cfbc1a2daf9cf550e74e1f088c28e7f7d8c1e1418fb6c9dc5184b7e84c99" +dependencies = [ + "crypto-common 0.2.2", + "inout", +] + +[[package]] +name = "aes" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8eb277bec05f56a0e0591f155a484cbd0f4f07ff2905051a48c72f004f7ed58" +dependencies = [ + "cipher", + "cpubits", + "cpufeatures 0.3.1", + "zeroize", +] + +[[package]] +name = "aes-gcm" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f2b8006a0c83f52b62ba44a97b58bf76fe2f70a329e588f67f89691d93d498f" +dependencies = [ + "aead", + "aes", + "cipher", + "ctr", + "ctutils", + "ghash", + "zeroize", +] + [[package]] name = "aho-corasick" version = "1.1.5" @@ -63,6 +100,18 @@ version = "1.0.104" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470" +[[package]] +name = "argon2" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "134c52ddac6d63c576bef8168db10c83c49c26444ecbc68060fef078925a901c" +dependencies = [ + "base64ct", + "blake2", + "cpufeatures 0.3.1", + "password-hash", +] + [[package]] name = "async-broadcast" version = "0.7.2" @@ -229,6 +278,12 @@ version = "1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53" +[[package]] +name = "base16ct" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd307490d624467aa6f74b0eabb77633d1f758a7b25f12bceb0b22e08d9726f6" + [[package]] name = "base64" version = "0.21.7" @@ -247,6 +302,23 @@ version = "0.23.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac07cdecf99051d9a5238b80f35af32cdeba5b336e55d957b318b50137e18da5" +[[package]] +name = "base64ct" +version = "1.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2af50177e190e07a26ab74f8b1efbfe2ef87da2116221318cb1c2e82baf7de06" + +[[package]] +name = "bcrypt-pbkdf" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "144e573728da132683b9488acd528274c790e07fc06ff81ee29f9d8f8b1041e0" +dependencies = [ + "blowfish", + "pbkdf2", + "sha2 0.11.1", +] + [[package]] name = "bindgen" version = "0.72.1" @@ -315,13 +387,41 @@ dependencies = [ "serde_core", ] +[[package]] +name = "blake2" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b5d4d889834ee8ecfc0f8426ad30faf7cdcb10f741a8e6d7224d95325479f6f" +dependencies = [ + "digest 0.11.3", +] + [[package]] name = "block-buffer" version = "0.10.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" dependencies = [ - "generic-array", + "generic-array 0.14.7", +] + +[[package]] +name = "block-buffer" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2f6c7dbe95a6ed67ad9f18e57daf93a2f034c524b99fd2b76d18fdfeb6660aa" +dependencies = [ + "hybrid-array", + "zeroize", +] + +[[package]] +name = "block-padding" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "710f1dd022ef4e93f8a438b4ba958de7f64308434fa6a87104481645cc30068b" +dependencies = [ + "hybrid-array", ] [[package]] @@ -346,6 +446,16 @@ dependencies = [ "piper", ] +[[package]] +name = "blowfish" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62ce3946557b35e71d1bbe07ec385073ce9eda05043f95de134eb578fcf1a298" +dependencies = [ + "byteorder", + "cipher", +] + [[package]] name = "borsh" version = "1.8.1" @@ -525,6 +635,15 @@ dependencies = [ "toml 0.9.12+spec-1.1.0", ] +[[package]] +name = "cbc" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce2dc9ee5f88d11e0beb842c88b33c8a5cf0d1329c4b19494af42b07dbfe8896" +dependencies = [ + "cipher", +] + [[package]] name = "cc" version = "1.8.0" @@ -606,8 +725,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "65c35e4b699c7e15ccbe7ee35c005e4fc0a278d22238a2857e6ce2dadeda1b06" dependencies = [ "cfg-if", + "cipher", "cpufeatures 0.3.1", "rand_core", + "zeroize", ] [[package]] @@ -617,11 +738,25 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1aa79e62e7697b8e29b513a68abacf485adcd1fe8284a4316c5ae868e6633327" dependencies = [ "iana-time-zone", + "js-sys", "num-traits", "serde", + "wasm-bindgen", "windows-link 0.2.1", ] +[[package]] +name = "cipher" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8cf2a2c93cd704877c0858356ed03480ff301ee950b43f1cbe4573b088bfa6c" +dependencies = [ + "block-buffer 0.12.1", + "crypto-common 0.2.2", + "inout", + "zeroize", +] + [[package]] name = "clang-sys" version = "1.9.1" @@ -633,6 +768,12 @@ dependencies = [ "libloading 0.8.9", ] +[[package]] +name = "cmov" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c9ea0ac24bc397ab3c98583a3c9ba74fa56b09a4449bbe172b9b1ddb016027a" + [[package]] name = "combine" version = "4.6.8" @@ -652,6 +793,12 @@ dependencies = [ "crossbeam-utils", ] +[[package]] +name = "const-oid" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6ef517f0926dd24a1582492c791b6a4818a4d94e789a334894aa15b0d12f55c" + [[package]] name = "cookie" version = "0.18.2" @@ -668,6 +815,16 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9885fa71e26b8ab7855e2ec7cae6e9b380edff76cd052e07c683a0319d51b3a2" +[[package]] +name = "core-foundation" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91e195e091a93c46f7102ec7818a2aa394e1e1771c3ab4825963fa03e45afb8f" +dependencies = [ + "core-foundation-sys", + "libc", +] + [[package]] name = "core-foundation" version = "0.10.1" @@ -691,7 +848,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "064badf302c3194842cf2c5d61f56cc88e54a759313879cdf03abdd27d0c3b97" dependencies = [ "bitflags 2.13.2", - "core-foundation", + "core-foundation 0.10.1", "core-graphics-types", "foreign-types", "libc", @@ -704,7 +861,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d44a101f213f6c4cdc1853d4b78aef6db6bdfa3468798cc1d9912f4735013eb" dependencies = [ "bitflags 2.13.2", - "core-foundation", + "core-foundation 0.10.1", "libc", ] @@ -714,6 +871,12 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f8f80099a98041a3d1622845c271458a2d73e688351bf3cb999266764b81d48" +[[package]] +name = "cpubits" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15b85f9c39137c3a891689859392b1bd49812121d0d61c9caf00d46ed5ce06ae" + [[package]] name = "cpufeatures" version = "0.2.17" @@ -775,16 +938,54 @@ version = "0.8.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a31eee39dddec8330830986fcd7625edb5a24ec90ea038215273bbc3adb08ac6" +[[package]] +name = "crypto-bigint" +version = "0.7.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c035cb64cba03dad2fb921f3e218785e8cf9a41790da5eb3a1400f62fa2be11" +dependencies = [ + "cpubits", + "ctutils", + "getrandom 0.4.3", + "hybrid-array", + "num-traits", + "rand_core", + "serdect", + "subtle", + "zeroize", +] + [[package]] name = "crypto-common" version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a" dependencies = [ - "generic-array", + "generic-array 0.14.7", "typenum", ] +[[package]] +name = "crypto-common" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce6e4c961d6cd6c9a86db418387425e8bdeaf05b3c8bc1411e6dca4c252f1453" +dependencies = [ + "getrandom 0.4.3", + "hybrid-array", + "rand_core", +] + +[[package]] +name = "crypto-primes" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3633a51a39c69ebbaa4feaa694bd83d241e4093901c84a0963b19d9bb3f0cf8f" +dependencies = [ + "crypto-bigint", + "rand_core", +] + [[package]] name = "cssparser" version = "0.36.0" @@ -824,6 +1025,53 @@ version = "0.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "52560adf09603e58c9a7ee1fe1dcb95a16927b17c127f0ac02d6e768a0e25bc1" +[[package]] +name = "ctr" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baaca1c4b237092596f64d571e9db6ce4109c4ef9742e27590f1709594461f21" +dependencies = [ + "cipher", +] + +[[package]] +name = "ctutils" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2376ffd6f96d8a9db59875b4b6b0237d6900b5baf64279714e05493ce64828a" +dependencies = [ + "cmov", + "subtle", +] + +[[package]] +name = "curve25519-dalek" +version = "5.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5eed333089e2e1c1ac8c6c0398e5e2497b4c9926ca6d0365ed1e099afa5bc23" +dependencies = [ + "cfg-if", + "cpufeatures 0.3.1", + "curve25519-dalek-derive", + "digest 0.11.3", + "fiat-crypto", + "rand_core", + "rustc_version", + "subtle", + "zeroize", +] + +[[package]] +name = "curve25519-dalek-derive" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f46882e17999c6cc590af592290432be3bce0428cb0d5f8b6715e4dc7b383eb3" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "darling" version = "0.24.1" @@ -858,6 +1106,26 @@ dependencies = [ "syn 3.0.9", ] +[[package]] +name = "dashmap" +version = "6.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6361d5c062261c78a176addb82d4c821ae42bed6089de0e12603cd25de2059c" +dependencies = [ + "cfg-if", + "crossbeam-utils", + "hashbrown 0.14.5", + "lock_api", + "once_cell", + "parking_lot_core", +] + +[[package]] +name = "data-encoding" +version = "2.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4583a4551df46e2792f82ceeac45e850d2e2d5debba0b91f102385cda5b11f06" + [[package]] name = "dbus" version = "0.9.12" @@ -900,6 +1168,28 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "delegate" +version = "0.13.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "780eb241654bf097afb00fc5f054a09b687dad862e485fdcf8399bb056565370" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "der" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a996a108feb8b427a6e12fcf6af4aa83409b79d72c15337d7741368131ee004" +dependencies = [ + "const-oid", + "pem-rfc7468", + "zeroize", +] + [[package]] name = "deranged" version = "0.5.9" @@ -930,14 +1220,35 @@ dependencies = [ "syn 2.0.119", ] +[[package]] +name = "des" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "916a94e407b54f9034d71dd748234cd1e516ced6284009906ae246f177eafe5a" +dependencies = [ + "cipher", +] + [[package]] name = "digest" version = "0.10.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" dependencies = [ - "block-buffer", - "crypto-common", + "block-buffer 0.10.4", + "crypto-common 0.1.7", +] + +[[package]] +name = "digest" +version = "0.11.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1dd6dbb5841937940781866fa1281a1ff7bd3bf827091440879f9994983d5c2" +dependencies = [ + "block-buffer 0.12.1", + "const-oid", + "crypto-common 0.2.2", + "ctutils", ] [[package]] @@ -1143,12 +1454,75 @@ version = "1.0.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d0881ea181b1df73ff77ffaaf9c7544ecc11e82fba9b5f27b262a3c73a332555" +[[package]] +name = "ecdsa" +version = "0.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0681a4fc24c767085329728d8dfba959af91228aa4610cca4f8ce317ba46ae0" +dependencies = [ + "der", + "digest 0.11.3", + "elliptic-curve", + "rfc6979", + "signature", + "spki", + "zeroize", +] + +[[package]] +name = "ed25519" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29fcf32e6c73d1079f83ab4d782de2d81620346a5f38c6237a86a22f8368980a" +dependencies = [ + "pkcs8", + "signature", +] + +[[package]] +name = "ed25519-dalek" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ebaa1a2bf1290ab3bfe5a7b771d050ebffab2711c19a81691c683a5144a25de" +dependencies = [ + "curve25519-dalek", + "ed25519", + "rand_core", + "serde", + "sha2 0.11.1", + "signature", + "subtle", + "zeroize", +] + [[package]] name = "either" version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0e9c71c2167ca323c882b99918929403426e2373ea17242ff5653e0d5e1058be" +[[package]] +name = "elliptic-curve" +version = "0.14.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "900f37e7e18d74a2347389af6cd69e3a30d5cc9e697f40004865679aa75e9c7c" +dependencies = [ + "base16ct", + "crypto-bigint", + "crypto-common 0.2.2", + "digest 0.11.3", + "ff", + "group", + "hkdf", + "hybrid-array", + "pem-rfc7468", + "pkcs8", + "rand_core", + "sec1", + "subtle", + "zeroize", +] + [[package]] name = "embed-resource" version = "3.0.12" @@ -1198,6 +1572,18 @@ version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "66b7e2430c6dff6a955451e2cfc438f09cea1965a9d6f87f7e3b90decc014099" +[[package]] +name = "enum_dispatch" +version = "0.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa18ce2bc66555b3218614519ac839ddb759a7d6720732f979ef8d13be147ecd" +dependencies = [ + "once_cell", + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "enumflags2" version = "0.7.12" @@ -1281,6 +1667,22 @@ dependencies = [ "simd-adler32", ] +[[package]] +name = "ff" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1f686ab92a9fb0eaf188f6c6c87b89490baa6fdb0db4544ba4dc47f7942489f" +dependencies = [ + "rand_core", + "subtle", +] + +[[package]] +name = "fiat-crypto" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64cd1e32ddd350061ae6edb1b082d7c54915b5c672c389143b9a63403a109f24" + [[package]] name = "field-offset" version = "0.3.6" @@ -1376,6 +1778,21 @@ dependencies = [ "libc", ] +[[package]] +name = "futures" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a31d2a3fbaaeb2af2368bbdd904aa8e812d3c04a1ee10d3171f52d556e5d0a3" +dependencies = [ + "futures-channel", + "futures-core", + "futures-executor", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + [[package]] name = "futures-channel" version = "0.3.34" @@ -1383,6 +1800,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b1f9e3d69d39e4862ffed03ed071a76f9a13ba1d9109d355b0f0aa6b15e393c4" dependencies = [ "futures-core", + "futures-sink", ] [[package]] @@ -1450,6 +1868,7 @@ version = "0.3.34" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0d50a92467f8ba5dd6e3ee5d4bd04d73ab2e4e1c44474a0674821dfce14b79bc" dependencies = [ + "futures-channel", "futures-core", "futures-io", "futures-macro", @@ -1593,6 +2012,17 @@ dependencies = [ "version_check", ] +[[package]] +name = "generic-array" +version = "1.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "337d46834ee672ab3e48caca2cb0c78cc174fb12b3a68d0d88f99a0519a5e36e" +dependencies = [ + "generic-array 0.14.7", + "rustversion", + "typenum", +] + [[package]] name = "getrandom" version = "0.2.17" @@ -1623,9 +2053,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "300e883d756b2e4ec94e02791f39b04b522276138852cfc41d9fb7e904106099" dependencies = [ "cfg-if", + "js-sys", "libc", "r-efi 6.0.0", "rand_core", + "wasm-bindgen", +] + +[[package]] +name = "ghash" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2eecf2d5dc9b66b732b97707a0210906b1d30523eb773193ab777c0c84b3e8d5" +dependencies = [ + "polyval", + "zeroize", ] [[package]] @@ -1746,6 +2188,18 @@ dependencies = [ "regex-syntax", ] +[[package]] +name = "gloo-timers" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "482ce8a491a501da4cd806bd190275363d674f2845005c6ddbd5d3e1dd54495d" +dependencies = [ + "futures-channel", + "futures-core", + "js-sys", + "wasm-bindgen", +] + [[package]] name = "gobject-sys" version = "0.18.0" @@ -1794,6 +2248,17 @@ dependencies = [ "memmap2", ] +[[package]] +name = "group" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fd1a1c7a5206c5b7a3f5a0d7ccd3ff85d0c8f5133d62a02680255b0004af5f4" +dependencies = [ + "ff", + "rand_core", + "subtle", +] + [[package]] name = "gtk" version = "0.18.2" @@ -1852,6 +2317,12 @@ version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" +[[package]] +name = "hashbrown" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" + [[package]] name = "hashbrown" version = "0.17.1" @@ -1882,6 +2353,30 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" +[[package]] +name = "hex-literal" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e712f64ec3850b98572bffac52e2c6f282b29fe6c5fa6d42334b30be438d95c1" + +[[package]] +name = "hkdf" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4aaa26c720c68b866f2c96ef5c1264b3e6f473fe5d4ce61cd44bbe913e553018" +dependencies = [ + "hmac", +] + +[[package]] +name = "hmac" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6303bc9732ae41b04cb554b844a762b4115a61bfaa81e3e83050991eeb56863f" +dependencies = [ + "digest 0.11.3", +] + [[package]] name = "html5ever" version = "0.38.0" @@ -1931,6 +2426,18 @@ version = "1.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87" +[[package]] +name = "hybrid-array" +version = "0.4.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "27f864f10dfb56725ce5ce5472bc52252c8f93a4ab86327122cebf62c5f59a17" +dependencies = [ + "ctutils", + "subtle", + "typenum", + "zeroize", +] + [[package]] name = "hyper" version = "1.12.0" @@ -2199,6 +2706,28 @@ dependencies = [ "libc", ] +[[package]] +name = "inout" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b5cf4372d022092674a83b8febf5865f7a573cc0c5baec5e49c879b6a20978f" +dependencies = [ + "block-padding", + "hybrid-array", +] + +[[package]] +name = "internal-russh-num-bigint" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae8e22120c32fb4d19ec55fba35015f57095cd95a2e3b732e44457f5915b2ee8" +dependencies = [ + "num-integer", + "num-traits", + "rand", + "rand_core", +] + [[package]] name = "ipnet" version = "2.12.2" @@ -2374,6 +2903,26 @@ dependencies = [ "serde_json", ] +[[package]] +name = "keccak" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8f198d1db720e4940b5a493201d199d9f24f568f8f746bd13706243a2f71598" +dependencies = [ + "cfg-if", + "cpufeatures 0.3.1", +] + +[[package]] +name = "kem" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01737161ba802849cfd486b5bd209d38ba4943494c249a8126005170c7621edd" +dependencies = [ + "crypto-common 0.2.2", + "rand_core", +] + [[package]] name = "keyboard-types" version = "0.7.0" @@ -2385,6 +2934,20 @@ dependencies = [ "unicode-segmentation", ] +[[package]] +name = "keyring" +version = "3.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eebcc3aff044e5944a8fbaf69eb277d11986064cba30c468730e8b9909fb551c" +dependencies = [ + "byteorder", + "log", + "security-framework 2.11.1", + "security-framework 3.7.0", + "windows-sys 0.60.2", + "zeroize", +] + [[package]] name = "khronos-egl" version = "6.0.0" @@ -2421,6 +2984,29 @@ dependencies = [ "libc", ] +[[package]] +name = "lazy-regex" +version = "3.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0a2cce8a22920a29fdcc8c9170aa5d29edd9fbcf8bea856e24c2cb1c2419f67" +dependencies = [ + "lazy-regex-proc_macros", + "once_cell", + "regex", +] + +[[package]] +name = "lazy-regex-proc_macros" +version = "3.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f1e4b1f1d8c71edb4ff2b2150ec4606445fd2ddf67ddc097655daf59286baa6" +dependencies = [ + "proc-macro2", + "quote", + "regex", + "syn 3.0.9", +] + [[package]] name = "lazy_static" version = "1.5.1" @@ -2609,6 +3195,25 @@ dependencies = [ "tempfile", ] +[[package]] +name = "lutra-deploy" +version = "0.1.0-alpha" +dependencies = [ + "keyring", + "lutra-vfs", + "proptest", + "russh", + "russh-sftp", + "rustls", + "rustls-native-certs", + "serde", + "serde_json", + "suppaftp", + "tempfile", + "thiserror 2.0.21", + "tokio", +] + [[package]] name = "lutra-desktop" version = "0.1.0-alpha" @@ -2782,6 +3387,12 @@ dependencies = [ "web_atoms", ] +[[package]] +name = "md5" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ebb8d8732c6a6df3d8f032a82911cfc747e00efb95cc46e8d0acd5b5b88570c" + [[package]] name = "memchr" version = "2.8.3" @@ -2850,6 +3461,32 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "ml-kem" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84ca78b66f46050d49fa21693ae8adab2081d8101b65d8067316b6b70b64a0d1" +dependencies = [ + "hybrid-array", + "kem", + "module-lattice", + "pkcs8", + "rand_core", + "sha3", + "shake", +] + +[[package]] +name = "module-lattice" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4a1e412c8ee83a1c678f427806219c49c96b2511b9f07497514f0e43fb26155" +dependencies = [ + "ctutils", + "hybrid-array", + "num-traits", +] + [[package]] name = "moxcms" version = "0.8.1" @@ -2929,6 +3566,18 @@ dependencies = [ "libc", ] +[[package]] +name = "nix" +version = "0.31.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf20d2fde8ff38632c426f1165ed7436270b44f199fc55284c38276f9db47c3d" +dependencies = [ + "bitflags 2.13.2", + "cfg-if", + "cfg_aliases 0.2.2", + "libc", +] + [[package]] name = "nom" version = "7.1.3" @@ -2984,12 +3633,31 @@ dependencies = [ "memchr", ] +[[package]] +name = "num-bigint" +version = "0.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c89e69e7e0f03bea5ef08013795c25018e101932225a656383bd384495ecc367" +dependencies = [ + "num-integer", + "num-traits", +] + [[package]] name = "num-conv" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "521739c6d2bac4aa25192232afe6841231376b2b26d4d9fae5ecf8ca5772e441" +[[package]] +name = "num-integer" +version = "0.1.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ce2d95d4b3734dc35aa2f45e1aa22cd416814592a4f9d9205e11affd5b8e10b" +dependencies = [ + "num-traits", +] + [[package]] name = "num-traits" version = "0.2.19" @@ -3378,6 +4046,67 @@ dependencies = [ "pin-project-lite", ] +[[package]] +name = "p256" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2c9239b2dbc807adbbe147e8cf72ea7450c3a0aabe62cb8e75ff4ec22e1f72a" +dependencies = [ + "ecdsa", + "elliptic-curve", + "primefield", + "primeorder", + "sha2 0.11.1", +] + +[[package]] +name = "p384" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d17b851e6b3e378ab4ecb07fa2ed23f4d15f075735f8fec9fa1e7bdce5f8301f" +dependencies = [ + "ecdsa", + "elliptic-curve", + "fiat-crypto", + "primefield", + "primeorder", + "sha2 0.11.1", +] + +[[package]] +name = "p521" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ad64cc32c2dc466317c12ee5853e61f159f9eab1fe7efade0395dc2e7b43449" +dependencies = [ + "base16ct", + "ecdsa", + "elliptic-curve", + "primefield", + "primeorder", + "sha2 0.11.1", +] + +[[package]] +name = "pageant" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3adadc44070da6f464b0918655a12f5792c156e088d8c4082d13e27d94c3e791" +dependencies = [ + "base16ct", + "byteorder", + "bytes", + "delegate", + "futures", + "log", + "rand", + "sha2 0.11.1", + "thiserror 2.0.21", + "tokio", + "windows 0.62.2", + "windows-strings 0.5.1", +] + [[package]] name = "pango" version = "0.18.3" @@ -3432,12 +4161,50 @@ dependencies = [ "windows-link 0.2.1", ] +[[package]] +name = "password-hash" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aab41826031698d6ffcd9cff78ef56ef998e39dc7e5067cdfebe373842d4723b" +dependencies = [ + "phc", +] + +[[package]] +name = "pbkdf2" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "112d82ceb8c5bf524d9af484d4e4970c9fd5a0cc15ba14ad93dccd28873b0629" +dependencies = [ + "digest 0.11.3", + "hmac", +] + +[[package]] +name = "pem-rfc7468" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6305423e0e7738146434843d1694d621cce767262b2a86910beab705e4493d9" +dependencies = [ + "base64ct", +] + [[package]] name = "percent-encoding" version = "2.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" +[[package]] +name = "phc" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44dc769b75f93afdddd8c7fa12d685292ddeff1e66f7f0f3a234cf1818afe892" +dependencies = [ + "base64ct", + "ctutils", +] + [[package]] name = "phf" version = "0.13.1" @@ -3533,6 +4300,45 @@ dependencies = [ "system-deps 7.0.8", ] +[[package]] +name = "pkcs1" +version = "0.8.0-rc.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "986d2e952779af96ea048f160fd9194e1751b4faea78bcf3ceb456efe008088e" +dependencies = [ + "der", + "spki", +] + +[[package]] +name = "pkcs5" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63d440a804ec8d6fafbb6b84471e013286658d373248927692ab3366686220ca" +dependencies = [ + "aes", + "aes-gcm", + "cbc", + "der", + "pbkdf2", + "rand_core", + "scrypt", + "sha2 0.11.1", + "spki", +] + +[[package]] +name = "pkcs8" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "451913da69c775a56034ea8d9003d27ee8948e12443eae7c038ba100a4f21cb7" +dependencies = [ + "der", + "pkcs5", + "rand_core", + "spki", +] + [[package]] name = "pkg-config" version = "0.3.34" @@ -3592,6 +4398,29 @@ dependencies = [ "windows-sys 0.61.2", ] +[[package]] +name = "poly1305" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e2d0073b297041425c7c3df6eb4792d598a15323fe63346852b092eca02904c" +dependencies = [ + "cpufeatures 0.3.1", + "universal-hash", + "zeroize", +] + +[[package]] +name = "polyval" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0fa31d631f2b2cb2a544d0aa321ce847a94764d701ca2becc411138b93d49cd" +dependencies = [ + "cpubits", + "cpufeatures 0.3.1", + "universal-hash", + "zeroize", +] + [[package]] name = "portable-atomic" version = "1.15.0" @@ -3620,7 +4449,7 @@ dependencies = [ "lazy_static", "libc", "log", - "nix", + "nix 0.28.0", "serial2", "shared_library", "shell-words", @@ -3649,6 +4478,33 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c" +[[package]] +name = "primefield" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c555a6e4eb7d4e158fcb028c835c3b8642206ddc279b5c6b202ef9a8bdb592f4" +dependencies = [ + "crypto-bigint", + "crypto-common 0.2.2", + "ff", + "rand_core", + "subtle", + "zeroize", +] + +[[package]] +name = "primeorder" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c9f42978c78a00e3d68f69fc03e57a234debae69da4020a4fb588fcdcd07b06" +dependencies = [ + "elliptic-curve", + "once_cell", + "primefield", + "serdect", + "wnaf", +] + [[package]] name = "proc-macro-crate" version = "1.3.1" @@ -3916,6 +4772,16 @@ dependencies = [ "web-sys", ] +[[package]] +name = "rfc6979" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4a459cddafb3fe76b31fd8f1108007566c40301feb64dc7b54656eb7388172b" +dependencies = [ + "crypto-bigint", + "hmac", +] + [[package]] name = "rfd" version = "0.16.0" @@ -3964,6 +4830,141 @@ dependencies = [ "str_indices", ] +[[package]] +name = "rsa" +version = "0.10.0-rc.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30b2aa4ba0d89f73d1e332df05be0eeab8840351c36ca5654341dfdb57bb3caf" +dependencies = [ + "const-oid", + "crypto-bigint", + "crypto-primes", + "digest 0.11.3", + "pkcs1", + "pkcs8", + "rand_core", + "sha2 0.11.1", + "signature", + "spki", + "zeroize", +] + +[[package]] +name = "russh" +version = "0.63.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "35bab1b87d915817d5d9cc352637cd40d5f0b298a48c6309af9156a4addc3031" +dependencies = [ + "aes", + "bitflags 2.13.2", + "block-padding", + "byteorder", + "bytes", + "cbc", + "cipher", + "crypto-bigint", + "ctr", + "curve25519-dalek", + "data-encoding", + "delegate", + "der", + "digest 0.11.3", + "ecdsa", + "ed25519-dalek", + "elliptic-curve", + "enum_dispatch", + "flate2", + "futures", + "generic-array 1.4.5", + "getrandom 0.4.3", + "ghash", + "hex-literal", + "hmac", + "inout", + "internal-russh-num-bigint", + "keccak", + "log", + "md5", + "ml-kem", + "module-lattice", + "num-bigint", + "p256", + "p384", + "p521", + "pageant", + "pbkdf2", + "pkcs1", + "pkcs5", + "pkcs8", + "polyval", + "rand", + "rand_core", + "ring", + "rsa", + "russh-cryptovec", + "russh-util", + "salsa20", + "scrypt", + "sec1", + "sha1", + "sha2 0.11.1", + "sha3", + "signature", + "spki", + "ssh-encoding", + "ssh-key", + "subtle", + "thiserror 2.0.21", + "tokio", + "typenum", + "universal-hash", + "zeroize", +] + +[[package]] +name = "russh-cryptovec" +version = "0.62.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3aec6cb630dbe85d72ffd7bcd95f07e1bd69f9f270ee8adfa1afe443a6331438" +dependencies = [ + "log", + "nix 0.31.3", + "ssh-encoding", + "windows-sys 0.61.2", +] + +[[package]] +name = "russh-sftp" +version = "3.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9bb17f72aba4314656b359ffba72c68dcbd70ed9e97497898b75498fe7198042" +dependencies = [ + "bitflags 2.13.2", + "bytes", + "chrono", + "dashmap", + "gloo-timers", + "log", + "serde", + "serde_bytes", + "thiserror 2.0.21", + "tokio", + "tokio-util", + "wasm-bindgen-futures", +] + +[[package]] +name = "russh-util" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "668424a5dde0bcb45b55ba7de8476b93831b4aa2fa6947e145f3b053e22c60b6" +dependencies = [ + "chrono", + "tokio", + "wasm-bindgen", + "wasm-bindgen-futures", +] + [[package]] name = "rustc-hash" version = "2.1.3" @@ -4029,7 +5030,7 @@ dependencies = [ "openssl-probe", "rustls-pki-types", "schannel", - "security-framework", + "security-framework 3.7.0", ] [[package]] @@ -4070,6 +5071,16 @@ dependencies = [ "wait-timeout", ] +[[package]] +name = "salsa20" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f874456e72520ff1375a06c588eaf074b0f01f9e9e1aada45bd9b7954a6e42c" +dependencies = [ + "cfg-if", + "cipher", +] + [[package]] name = "same-file" version = "1.0.6" @@ -4151,6 +5162,45 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" +[[package]] +name = "scrypt" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d87af57419b594aa23fa95f09f0e06d80d84ba01c26148c43844cad6ff4485f0" +dependencies = [ + "cfg-if", + "pbkdf2", + "salsa20", + "sha2 0.11.1", +] + +[[package]] +name = "sec1" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d56d437c2f19203ce5f7122e507831de96f3d2d4d3be5af44a0b0a09d8a80e4d" +dependencies = [ + "base16ct", + "ctutils", + "der", + "hybrid-array", + "subtle", + "zeroize", +] + +[[package]] +name = "security-framework" +version = "2.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "897b2245f0b511c87893af39b033e5ca9cce68824c4d7e7630b5a1d339658d02" +dependencies = [ + "bitflags 2.13.2", + "core-foundation 0.9.4", + "core-foundation-sys", + "libc", + "security-framework-sys", +] + [[package]] name = "security-framework" version = "3.7.0" @@ -4158,7 +5208,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b7f4bc775c73d9a02cde8bf7b2ec4c9d12743edf609006c7facc23998404cd1d" dependencies = [ "bitflags 2.13.2", - "core-foundation", + "core-foundation 0.10.1", "core-foundation-sys", "libc", "security-framework-sys", @@ -4225,6 +5275,16 @@ dependencies = [ "typeid", ] +[[package]] +name = "serde_bytes" +version = "0.11.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5d440709e79d88e51ac01c4b72fc6cb7314017bb7da9eeff678aa94c10e3ea8" +dependencies = [ + "serde", + "serde_core", +] + [[package]] name = "serde_core" version = "1.0.229" @@ -4333,6 +5393,16 @@ dependencies = [ "syn 3.0.9", ] +[[package]] +name = "serdect" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "66cf8fedced2fcf12406bcb34223dffb92eaf34908ede12fed414c82b7f00b3e" +dependencies = [ + "base16ct", + "serde", +] + [[package]] name = "serial2" version = "0.2.38" @@ -4375,6 +5445,17 @@ dependencies = [ "stable_deref_trait", ] +[[package]] +name = "sha1" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aacc4cc499359472b4abe1bf11d0b12e688af9a805fa5e3016f9a386dc2d0214" +dependencies = [ + "cfg-if", + "cpufeatures 0.3.1", + "digest 0.11.3", +] + [[package]] name = "sha2" version = "0.10.9" @@ -4383,7 +5464,40 @@ checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283" dependencies = [ "cfg-if", "cpufeatures 0.2.17", - "digest", + "digest 0.10.7", +] + +[[package]] +name = "sha2" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47d7069beb7d6ac7b9acd1039986e73443f24234f41074da099d6f994ac9ad19" +dependencies = [ + "cfg-if", + "cpufeatures 0.3.1", + "digest 0.11.3", +] + +[[package]] +name = "sha3" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc9bad02c26382724b2d2692c6f179285e4b54eeecd7968f52a50059c3c11759" +dependencies = [ + "digest 0.11.3", + "keccak", + "sponge-cursor", +] + +[[package]] +name = "shake" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09057cb2149ad4cbd2da1e26b351f9a4c354219421229c69c3063e6f61947c4a" +dependencies = [ + "digest 0.11.3", + "keccak", + "sponge-cursor", ] [[package]] @@ -4424,6 +5538,16 @@ dependencies = [ "libc", ] +[[package]] +name = "signature" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28d567dcbaf0049cb8ac2608a76cd95ff9e4412e1899d389ee400918ca7537f5" +dependencies = [ + "digest 0.11.3", + "rand_core", +] + [[package]] name = "simd-adler32" version = "0.3.10" @@ -4518,6 +5642,81 @@ dependencies = [ "system-deps 6.2.2", ] +[[package]] +name = "spki" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ef958a98b9d5da290cfc78946e9f3e61e1e62a18db0d92cac0b83cc161491a9" +dependencies = [ + "base64ct", + "der", +] + +[[package]] +name = "sponge-cursor" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a0219bd7d979d58245a4f41f695e1ac9f8befdffadd7f61f1bae9e39abc6620" + +[[package]] +name = "ssh-cipher" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d801accda99469cde6d73da741422610fdf6508a72d9a69d1b55cb241c720597" +dependencies = [ + "aead", + "aes", + "aes-gcm", + "chacha20", + "cipher", + "ctutils", + "des", + "poly1305", + "ssh-encoding", + "zeroize", +] + +[[package]] +name = "ssh-encoding" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b54d0ed0498daf3f78d82e00e28c8eec9d75a067c4cfbcc7a0f7d0f4077749e" +dependencies = [ + "base64ct", + "bytes", + "crypto-bigint", + "ctutils", + "digest 0.11.3", + "pem-rfc7468", + "zeroize", +] + +[[package]] +name = "ssh-key" +version = "0.7.0-rc.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9a32fae177b74a22aa9c5b01bf7e68b33545be32d9e381e248058d2adc15ce3" +dependencies = [ + "argon2", + "bcrypt-pbkdf", + "ctutils", + "ed25519-dalek", + "hex", + "hmac", + "p256", + "p384", + "p521", + "rand_core", + "rsa", + "sec1", + "sha1", + "sha2 0.11.1", + "signature", + "ssh-cipher", + "ssh-encoding", + "zeroize", +] + [[package]] name = "stable_deref_trait" version = "1.2.1" @@ -4572,6 +5771,19 @@ version = "2.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" +[[package]] +name = "suppaftp" +version = "12.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b58fcf921745cca6f0127c9f822069947da8c6dbeff52e9004750bcc448171f" +dependencies = [ + "chrono", + "lazy-regex", + "log", + "rustls", + "thiserror 2.0.21", +] + [[package]] name = "swift-rs" version = "1.0.9" @@ -4669,7 +5881,7 @@ checksum = "d1c93047acf68669466a34690ac58cca7010bd1b201e1ec86f1fd0a75d3dd4a9" dependencies = [ "bitflags 2.13.2", "block2", - "core-foundation", + "core-foundation 0.10.1", "core-graphics", "crossbeam-channel", "dbus", @@ -4813,7 +6025,7 @@ dependencies = [ "semver", "serde", "serde_json", - "sha2", + "sha2 0.10.9", "syn 2.0.119", "tauri-utils", "thiserror 2.0.21", @@ -5114,9 +6326,21 @@ dependencies = [ "mio", "pin-project-lite", "socket2", + "tokio-macros", "windows-sys 0.61.2", ] +[[package]] +name = "tokio-macros" +version = "2.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78773a2a397f451582ce068015985c33193cf6dea8b74d2a639fe457b2f07b0e" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.9", +] + [[package]] name = "tokio-util" version = "0.7.20" @@ -5126,6 +6350,7 @@ dependencies = [ "bytes", "futures-core", "futures-sink", + "futures-util", "pin-project-lite", "tokio", ] @@ -5472,6 +6697,16 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4ac048d71ede7ee76d585517add45da530660ef4390e49b098733c6e897f254" +[[package]] +name = "universal-hash" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4987bdc12753382e0bec4a65c50738ffaabc998b9cdd1f952fb5f39b0048a96" +dependencies = [ + "crypto-common 0.2.2", + "ctutils", +] + [[package]] name = "untrusted" version = "0.9.0" @@ -6428,6 +7663,18 @@ version = "0.57.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1ebf944e87a7c253233ad6766e082e3cd714b5d03812acc24c318f549614536e" +[[package]] +name = "wnaf" +version = "0.14.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "795ca18b3fdb5e62bf982199278341ddcf7ebf7d32e25e212ad05d496e95f6fa" +dependencies = [ + "ff", + "group", + "hybrid-array", + "primefield", +] + [[package]] name = "writeable" version = "0.6.4" @@ -6464,7 +7711,7 @@ dependencies = [ "once_cell", "percent-encoding", "raw-window-handle", - "sha2", + "sha2 0.10.9", "soup3", "tao-macros", "thiserror 2.0.21", diff --git a/Cargo.toml b/Cargo.toml index 768abfd..ee30710 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,7 @@ lutra-search = { path = "crates/search" } lutra-lsp = { path = "crates/lsp" } lutra-terminal = { path = "crates/terminal" } lutra-vcs = { path = "crates/vcs" } +lutra-deploy = { path = "crates/deploy" } lutra-engine = { path = "crates/engine" } lutra-control = { path = "crates/control" } lutra-mcp = { path = "crates/mcp" } diff --git a/crates/deploy/Cargo.toml b/crates/deploy/Cargo.toml new file mode 100644 index 0000000..7f9bf13 --- /dev/null +++ b/crates/deploy/Cargo.toml @@ -0,0 +1,43 @@ +[package] +name = "lutra-deploy" +description = "Deployment: upload and download over SFTP, FTP, FTPS or to a folder, with credentials in the OS keychain" +version.workspace = true +edition.workspace = true +rust-version.workspace = true +license.workspace = true +authors.workspace = true +publish.workspace = true + +[package.metadata.lutra] +layer = 3 + +[dependencies] +keyring = { version = "3", default-features = false, features = ["windows-native", "apple-native"] } +russh = { version = "0.63", default-features = false, features = ["ring", "rsa", "flate2"] } +russh-sftp = "3" +rustls = { version = "0.23", default-features = false, features = ["ring", "std", "tls12"] } +rustls-native-certs = "0.8" +serde = { version = "1.0.229", features = ["derive"] } +serde_json = "1.0.154" +suppaftp = { version = "12", default-features = false, features = ["rustls-ring"] } +thiserror = "2.0.21" +tokio = { version = "1", features = ["rt", "net", "io-util", "sync", "time", "macros"] } +lutra-vfs.workspace = true + +[lints] +workspace = true + +[dev-dependencies] +proptest.workspace = true +tempfile.workspace = true + +[features] +testserver = [] + +[[test]] +name = "transfer" +required-features = ["testserver"] + +[[example]] +name = "testserver" +required-features = ["testserver"] diff --git a/crates/deploy/examples/testserver.rs b/crates/deploy/examples/testserver.rs new file mode 100644 index 0000000..9433b08 --- /dev/null +++ b/crates/deploy/examples/testserver.rs @@ -0,0 +1,26 @@ +//! Runs the local SFTP and FTP test servers for a live check: +//! `cargo run -p lutra-deploy --features testserver --example testserver -- DIR USER PASSWORD`. +//! Both listen on 127.0.0.1 only and serve `DIR`. Prints their ports and +//! the SFTP host key, then runs until standard input closes. + +use std::io::Read; +use std::path::PathBuf; + +use lutra_deploy::testserver::{FtpServer, SftpServer}; + +fn main() -> std::io::Result<()> { + let args: Vec = std::env::args().skip(1).collect(); + let [dir, user, password] = args.as_slice() else { + eprintln!("usage: testserver DIR USER PASSWORD"); + std::process::exit(2); + }; + let dir = PathBuf::from(dir); + std::fs::create_dir_all(&dir)?; + let sftp = SftpServer::start(&dir, user, Some(password), None, 9)?; + let ftp = FtpServer::start(&dir, user, password)?; + println!("sftp {} {} {}", sftp.addr, sftp.algorithm, sftp.fingerprint); + println!("ftp {}", ftp.addr); + let mut sink = Vec::new(); + std::io::stdin().read_to_end(&mut sink)?; + Ok(()) +} diff --git a/crates/deploy/src/config.rs b/crates/deploy/src/config.rs new file mode 100644 index 0000000..bd061d8 --- /dev/null +++ b/crates/deploy/src/config.rs @@ -0,0 +1,460 @@ +//! Server definitions of one project and their storage. +//! +//! A project's deployment configuration is a JSON file the engine keeps in +//! Lutra's config directory, one per project. It never holds a password or +//! a passphrase: those live only in the OS keychain ([`crate::secrets`]). +//! Unknown fields are refused, so a `password` field written by hand is an +//! error instead of being kept on disk unnoticed. + +use std::path::Path; + +use serde::{Deserialize, Serialize}; + +use crate::error::{DeployError, Result}; +use crate::mapping::{normalize_relative, valid_pattern}; + +/// Largest configuration file read. +const MAX_FILE: u64 = 1024 * 1024; + +/// Most servers per project. +pub const MAX_SERVERS: usize = 100; + +/// Most path mappings or excluded paths per server. +pub const MAX_ENTRIES: usize = 200; + +/// How Lutra reaches a server. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "lowercase")] +pub enum ServerKind { + /// SFTP over SSH. + Sftp, + /// Plain FTP. Everything, including the password, crosses the network + /// unencrypted. + Ftp, + /// FTP with explicit TLS (`AUTH TLS`), certificates checked against the + /// operating system's roots. + Ftps, + /// A local or mounted folder. + Folder, +} + +impl ServerKind { + /// The port used when a server sets none. + pub fn default_port(self) -> u16 { + match self { + Self::Sftp => 22, + Self::Ftp | Self::Ftps => 21, + Self::Folder => 0, + } + } + + /// The name used in messages and the log. + pub fn name(self) -> &'static str { + match self { + Self::Sftp => "sftp", + Self::Ftp => "ftp", + Self::Ftps => "ftps", + Self::Folder => "folder", + } + } +} + +/// How Lutra signs in. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)] +#[serde(rename_all = "lowercase")] +pub enum AuthKind { + /// A password from the keychain (SFTP, FTP, FTPS). + #[default] + Password, + /// An SSH private key file, with an optional passphrase from the + /// keychain (SFTP). + Key, + /// The running SSH agent: Pageant or the OpenSSH agent on Windows, + /// `SSH_AUTH_SOCK` elsewhere (SFTP). + Agent, +} + +/// One local folder uploaded to one remote folder. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct Mapping { + /// Project-relative folder with `/`; empty for the project root. + pub local: String, + /// Remote folder: relative to the server's root, or absolute when it + /// starts with `/`. Empty for the root itself. + pub remote: String, +} + +/// One deployment server. +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields, rename_all = "camelCase")] +pub struct Server { + /// Stable id within the project: letters, digits, `-` and `_`. + pub id: String, + /// Shown name. + pub name: String, + /// Protocol. + pub kind: ServerKind, + /// Host name or address; empty for a folder. + #[serde(default)] + pub host: String, + /// Port; `None` for the protocol's default. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub port: Option, + /// User name; empty for a folder. + #[serde(default)] + pub user: String, + /// The deployment root: a remote folder (empty for the login folder) + /// or, for a folder server, an absolute local path. + #[serde(default)] + pub root: String, + /// How to sign in. + #[serde(default)] + pub auth: AuthKind, + /// The private key file for [`AuthKind::Key`]. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub key_path: Option, + /// Path mappings; none means the project root maps to the root. + #[serde(default)] + pub mappings: Vec, + /// Paths never uploaded or downloaded: project-relative paths (a + /// folder excludes everything inside), or file name patterns with `*` + /// and `?` such as `*.log`. + #[serde(default)] + pub excluded: Vec, + /// Upload a file after every save. Off by default. + #[serde(default)] + pub upload_on_save: bool, +} + +impl Server { + /// The port to connect to. + pub fn effective_port(&self) -> u16 { + self.port.unwrap_or_else(|| self.kind.default_port()) + } + + /// `host:port` for messages and the known-hosts file. + pub fn address(&self) -> String { + format!("{}:{}", self.host, self.effective_port()) + } + + /// Whether signing in needs a secret from the keychain, and which: + /// `password` or `passphrase`. A key may have no passphrase, so the + /// key's is `Some` but optional. + pub fn secret_kind(&self) -> Option<&'static str> { + match (self.kind, self.auth) { + (ServerKind::Folder, _) | (ServerKind::Sftp, AuthKind::Agent) => None, + (ServerKind::Sftp, AuthKind::Key) => Some("passphrase"), + _ => Some("password"), + } + } + + /// Checks every field and normalizes the mappings. + /// + /// # Errors + /// [`DeployError::Invalid`] naming the first bad field. + pub fn validate(&mut self) -> Result<()> { + let bad = |m: String| Err(DeployError::Invalid(m)); + if self.id.is_empty() + || self.id.len() > 64 + || !self + .id + .chars() + .all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_') + { + return bad(format!( + "server id {:?} must be 1 to 64 letters, digits, - or _", + self.id + )); + } + self.name = self.name.trim().to_string(); + if self.name.is_empty() || self.name.chars().count() > 100 || has_control(&self.name) { + return bad("the server needs a name of at most 100 characters".into()); + } + self.host = self.host.trim().to_string(); + self.user = self.user.trim().to_string(); + if self.kind == ServerKind::Folder { + if !Path::new(&self.root).is_absolute() { + return bad("a folder server needs an absolute local folder as its root".into()); + } + } else { + if self.host.is_empty() + || self.host.len() > 255 + || self.host.starts_with('-') + || self + .host + .chars() + .any(|c| c.is_whitespace() || c.is_control()) + { + return bad(format!("{:?} is not a valid host name", self.host)); + } + if self.port == Some(0) { + return bad("the port must be between 1 and 65535".into()); + } + if self.user.is_empty() || self.user.len() > 255 || has_control(&self.user) { + return bad("the server needs a user name".into()); + } + if self.kind != ServerKind::Sftp && self.auth != AuthKind::Password { + return bad("FTP and FTPS servers sign in with a password".into()); + } + if has_control(&self.root) || self.root.len() > 4096 { + return bad("the root folder contains control characters".into()); + } + } + if self.auth == AuthKind::Key { + let key = self.key_path.as_deref().unwrap_or_default().trim(); + if !Path::new(key).is_absolute() { + return bad("key sign-in needs the absolute path of a private key file".into()); + } + self.key_path = Some(key.to_string()); + } else { + self.key_path = None; + } + if self.mappings.len() > MAX_ENTRIES || self.excluded.len() > MAX_ENTRIES { + return bad(format!( + "at most {MAX_ENTRIES} mappings and {MAX_ENTRIES} excluded paths per server" + )); + } + for mapping in &mut self.mappings { + mapping.local = normalize_relative(&mapping.local).ok_or_else(|| { + DeployError::Invalid(format!( + "the local path {:?} of a mapping must stay inside the project", + mapping.local + )) + })?; + let absolute = mapping.remote.starts_with('/'); + let rest = normalize_relative(&mapping.remote).ok_or_else(|| { + DeployError::Invalid(format!( + "the remote path {:?} of a mapping may not contain .. or control characters", + mapping.remote + )) + })?; + mapping.remote = if absolute { format!("/{rest}") } else { rest }; + } + for pattern in &mut self.excluded { + *pattern = pattern.trim().trim_matches('/').to_string(); + if !valid_pattern(pattern) { + return bad(format!("{pattern:?} is not a valid excluded path")); + } + } + Ok(()) + } +} + +/// Whether `text` holds a control character. +fn has_control(text: &str) -> bool { + text.chars().any(char::is_control) +} + +/// The deployment configuration of one project. +#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(deny_unknown_fields, rename_all = "camelCase")] +pub struct Deployment { + /// The servers, in the user's order. + #[serde(default)] + pub servers: Vec, + /// The id of the default server. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub default_server: Option, +} + +impl Deployment { + /// The server with `id`. + pub fn server(&self, id: &str) -> Option<&Server> { + self.servers.iter().find(|s| s.id == id) + } + + /// The default server: the chosen one, else the only one. + pub fn default_target(&self) -> Option<&Server> { + match &self.default_server { + Some(id) => self.server(id), + None if self.servers.len() == 1 => self.servers.first(), + None => None, + } + } + + /// Adds `server`, or replaces the one with the same id, after + /// validating it. + /// + /// # Errors + /// [`DeployError::Invalid`] for a bad server or too many servers. + pub fn upsert(&mut self, mut server: Server) -> Result<()> { + server.validate()?; + if let Some(slot) = self.servers.iter_mut().find(|s| s.id == server.id) { + *slot = server; + return Ok(()); + } + if self.servers.len() >= MAX_SERVERS { + return Err(DeployError::Invalid(format!( + "at most {MAX_SERVERS} servers per project" + ))); + } + self.servers.push(server); + Ok(()) + } + + /// Removes the server with `id`; clears the default if it was that one. + /// Returns whether it existed. + pub fn remove(&mut self, id: &str) -> bool { + let before = self.servers.len(); + self.servers.retain(|s| s.id != id); + if self.default_server.as_deref() == Some(id) { + self.default_server = None; + } + self.servers.len() != before + } + + /// A fresh id: `s1`, `s2`… whichever is free. + pub fn fresh_id(&self) -> String { + (1..) + .map(|n: u32| format!("s{n}")) + .find(|id| self.server(id).is_none()) + .unwrap_or_else(|| "s0".into()) + } + + /// Reads the configuration at `path`; a missing file is an empty one. + /// + /// # Errors + /// [`DeployError::Transfer`] when the file cannot be read or is not a + /// valid configuration; the file is left as it is. + pub fn load(path: &Path) -> Result { + let meta = match std::fs::metadata(path) { + Ok(meta) => meta, + Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(Self::default()), + Err(e) => return Err(io_error(path, &e)), + }; + if meta.len() > MAX_FILE { + return Err(DeployError::Transfer(format!( + "{} is larger than 1 MiB", + path.display() + ))); + } + let bytes = std::fs::read(path).map_err(|e| io_error(path, &e))?; + let mut deployment: Self = serde_json::from_slice(&bytes).map_err(|e| { + DeployError::Transfer(format!( + "{} is not a valid deployment configuration: {e}", + path.display() + )) + })?; + for server in &mut deployment.servers { + server.validate()?; + } + Ok(deployment) + } + + /// Writes the configuration to `path` atomically, creating its folder. + /// + /// # Errors + /// [`DeployError::Transfer`] when it cannot be written. + pub fn save(&self, path: &Path) -> Result<()> { + if let Some(dir) = path.parent() { + std::fs::create_dir_all(dir).map_err(|e| io_error(dir, &e))?; + } + let text = serde_json::to_vec_pretty(self) + .map_err(|e| DeployError::Transfer(format!("cannot encode the configuration: {e}")))?; + lutra_vfs::write_atomic(path, &text).map_err(|e| DeployError::Transfer(e.to_string())) + } +} + +/// A local file error as a [`DeployError::Transfer`]. +pub(crate) fn io_error(path: &Path, error: &std::io::Error) -> DeployError { + DeployError::Transfer(format!("{}: {error}", path.display())) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn sftp() -> Server { + Server { + id: "prod".into(), + name: " Production ".into(), + kind: ServerKind::Sftp, + host: "example.test".into(), + port: None, + user: "deploy".into(), + root: "/var/www".into(), + auth: AuthKind::Password, + key_path: Some("ignored".into()), + mappings: vec![Mapping { + local: "./public/".into(), + remote: "/srv//html/".into(), + }], + excluded: vec!["/.env".into(), "*.log".into()], + upload_on_save: false, + } + } + + #[test] + fn validation_normalizes() { + let mut s = sftp(); + s.validate().unwrap(); + assert_eq!(s.name, "Production"); + assert_eq!(s.key_path, None); + assert_eq!(s.mappings[0].local, "public"); + assert_eq!(s.mappings[0].remote, "/srv/html"); + assert_eq!(s.excluded, vec![".env", "*.log"]); + assert_eq!(s.address(), "example.test:22"); + assert_eq!(s.secret_kind(), Some("password")); + } + + /// One change that makes a valid server invalid. + type Change = Box; + + #[test] + fn validation_refuses_bad_fields() { + let cases: Vec = vec![ + Box::new(|s| s.id = "a b".into()), + Box::new(|s| s.name = " ".into()), + Box::new(|s| s.host = "-oProxyCommand=x".into()), + Box::new(|s| s.host = "a b".into()), + Box::new(|s| s.port = Some(0)), + Box::new(|s| s.user = String::new()), + Box::new(|s| s.auth = AuthKind::Key), + Box::new(|s| s.mappings[0].local = "../up".into()), + Box::new(|s| s.mappings[0].remote = "a/../../b".into()), + Box::new(|s| { + s.kind = ServerKind::Ftp; + s.auth = AuthKind::Agent; + }), + Box::new(|s| { + s.kind = ServerKind::Folder; + s.root = "relative".into(); + }), + ]; + for (i, change) in cases.iter().enumerate() { + let mut s = sftp(); + change(&mut s); + assert!(s.validate().is_err(), "case {i} passed"); + } + } + + #[test] + fn a_password_field_is_refused() { + let text = r#"{"servers":[{"id":"a","name":"A","kind":"ftp","host":"h","user":"u","password":"x"}]}"#; + assert!(serde_json::from_str::(text).is_err()); + } + + #[test] + fn upsert_remove_default_and_round_trip() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("sub").join("d.json"); + let mut d = Deployment::load(&path).unwrap(); + assert!(d.servers.is_empty()); + d.upsert(sftp()).unwrap(); + assert_eq!(d.default_target().unwrap().id, "prod"); + let mut other = sftp(); + other.id = d.fresh_id(); + assert_eq!(other.id, "s1"); + d.upsert(other).unwrap(); + assert!(d.default_target().is_none()); + d.default_server = Some("s1".into()); + d.save(&path).unwrap(); + let back = Deployment::load(&path).unwrap(); + assert_eq!(back, d); + d.remove("s1"); + assert_eq!(d.default_server, None); + std::fs::write(&path, "nope").unwrap(); + assert!(Deployment::load(&path).is_err()); + } +} diff --git a/crates/deploy/src/error.rs b/crates/deploy/src/error.rs new file mode 100644 index 0000000..1046616 --- /dev/null +++ b/crates/deploy/src/error.rs @@ -0,0 +1,122 @@ +//! The crate's error type. Every message says what happened and, where the +//! cause is known, what to do. No message ever contains a password or a +//! passphrase: errors from the protocol libraries pass through +//! [`DeployError::redacted`] with the secret that was in use. + +use thiserror::Error; + +/// What can go wrong while configuring, connecting or transferring. +#[derive(Debug, Error, Clone, PartialEq, Eq)] +pub enum DeployError { + /// A server definition or a parameter is invalid; the caller's mistake. + #[error("{0}")] + Invalid(String), + /// The server's host key is not in Lutra's known-hosts file yet. + #[error( + "the host key of {host} is not known yet ({algorithm} {fingerprint}); check the fingerprint with the server's administrator, then trust it with Test Connection" + )] + HostKeyUnknown { + /// `host:port`. + host: String, + /// The key's algorithm, e.g. `ssh-ed25519`. + algorithm: String, + /// `SHA256:` and the base64 digest. + fingerprint: String, + }, + /// The server's host key differs from the trusted one. + #[error( + "the host key of {host} changed: it is now {algorithm} {fingerprint}, but {known} is trusted. Lutra refuses to connect, because someone may be intercepting the connection. If the server was reinstalled, remove the old key in the server's settings and test the connection again" + )] + HostKeyMismatch { + /// `host:port`. + host: String, + /// The offered key's algorithm. + algorithm: String, + /// The offered key's fingerprint. + fingerprint: String, + /// The trusted fingerprint. + known: String, + }, + /// The password or passphrase the server needs is not in the keychain. + #[error( + "no {kind} for {server} is stored in the system keychain; enter it in the server's settings" + )] + MissingSecret { + /// The server's name. + server: String, + /// `password` or `passphrase`. + kind: String, + }, + /// The OS keychain refused or is not available. + #[error("the system keychain is not available: {0}")] + Keychain(String), + /// The server is unreachable or the connection broke. + #[error("cannot reach {host}: {message}. Check the host name, the port and your network")] + Connect { + /// `host:port`. + host: String, + /// The cause. + message: String, + }, + /// The server refused the sign-in. + #[error("{0}")] + Auth(String), + /// The server refused an operation, or a local file failed. + #[error("{0}")] + Transfer(String), + /// The job was cancelled. + #[error("cancelled")] + Cancelled, +} + +impl DeployError { + /// Whether the caller's input was wrong, as opposed to the server, the + /// network or the disk. + pub fn is_invalid_input(&self) -> bool { + matches!(self, Self::Invalid(_)) + } + + /// The same error with every occurrence of `secret` in its text + /// replaced by `***`. Applied to every error that a protocol library + /// produced while `secret` was in use. + #[must_use] + pub fn redacted(self, secret: Option<&str>) -> Self { + let Some(secret) = secret.filter(|s| !s.is_empty()) else { + return self; + }; + let clean = |text: String| text.replace(secret, "***"); + match self { + Self::Invalid(m) => Self::Invalid(clean(m)), + Self::Connect { host, message } => Self::Connect { + host, + message: clean(message), + }, + Self::Auth(m) => Self::Auth(clean(m)), + Self::Transfer(m) => Self::Transfer(clean(m)), + Self::Keychain(m) => Self::Keychain(clean(m)), + other => other, + } + } +} + +/// The crate's result type. +pub type Result = std::result::Result; + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn redaction_removes_the_secret() { + let e = DeployError::Auth("server said: bad pass s3cr3t!".into()).redacted(Some("s3cr3t!")); + assert_eq!(e.to_string(), "server said: bad pass ***"); + let e = DeployError::Transfer("x".into()).redacted(Some("")); + assert_eq!(e.to_string(), "x"); + } + + #[test] + fn invalid_is_the_callers() { + assert!(DeployError::Invalid("x".into()).is_invalid_input()); + assert!(!DeployError::Cancelled.is_invalid_input()); + } +} diff --git a/crates/deploy/src/ftp.rs b/crates/deploy/src/ftp.rs new file mode 100644 index 0000000..db994a5 --- /dev/null +++ b/crates/deploy/src/ftp.rs @@ -0,0 +1,248 @@ +//! FTP and FTPS with `suppaftp`, blocking, TLS through `rustls` with +//! `ring`. +//! +//! FTPS is explicit TLS (`AUTH TLS` on the control port); the server's +//! certificate is checked against the operating system's roots. Transfers +//! are binary and passive, with the NAT workaround on (a server behind NAT +//! often announces its private address). Plain FTP sends everything, +//! including the password, unencrypted; the documentation says so. + +use std::io::{Read, Write}; +use std::net::ToSocketAddrs; +use std::sync::Arc; +use std::time::Duration; + +use suppaftp::types::FileType; +use suppaftp::{FtpError, ImplFtpStream, RustlsConnector, RustlsFtpStream, TlsStream}; + +use crate::config::{Server, ServerKind}; +use crate::error::{DeployError, Result}; +use crate::remote::{Cancel, Entry, MAX_LISTING, Remote, Tick, copy_chunks, safe_name}; +use crate::secrets::Secret; + +/// Longest wait for the TCP connection. +const CONNECT_TIMEOUT: Duration = Duration::from_secs(20); + +/// Longest wait for any read or write on the control connection. +const IO_TIMEOUT: Duration = Duration::from_secs(60); + +/// An open FTP or FTPS session. +pub struct Ftp { + /// The control connection; TLS-wrapped for FTPS, plain for FTP (the + /// same stream type, never upgraded). + stream: RustlsFtpStream, + /// The folder the server put us in after sign-in. + home: String, + /// The job's cancel flag. + cancel: Cancel, +} + +/// The TLS settings for FTPS: the OS roots, TLS 1.2 and 1.3, `ring`. +/// +/// # Errors +/// [`DeployError::Connect`] when no root certificate could be loaded. +fn tls_config(host: &str) -> Result { + let mut roots = rustls::RootCertStore::empty(); + let found = rustls_native_certs::load_native_certs(); + let (added, _) = roots.add_parsable_certificates(found.certs); + if added == 0 { + return Err(DeployError::Connect { + host: host.to_string(), + message: "no trusted root certificates were found on this system for FTPS".into(), + }); + } + rustls::ClientConfig::builder_with_provider(Arc::new(rustls::crypto::ring::default_provider())) + .with_safe_default_protocol_versions() + .map(|b| b.with_root_certificates(roots).with_no_client_auth()) + .map_err(|e| DeployError::Connect { + host: host.to_string(), + message: format!("TLS setup failed: {e}"), + }) +} + +/// An FTP library error as ours. +fn ftp_error(what: &str, e: &FtpError) -> DeployError { + DeployError::Transfer(format!("{what}: {e}")) +} + +/// Signs in on a connected stream and switches to binary mode; returns the +/// stream and the login folder. +fn sign_in( + mut stream: ImplFtpStream, + server: &Server, + secret: &Secret, +) -> Result<(ImplFtpStream, String)> { + stream.set_passive_nat_workaround(true); + stream + .login(server.user.as_str(), secret.expose()) + .map_err(|e| match e { + FtpError::UnexpectedResponse(_) => DeployError::Auth(format!( + "{} refused the sign-in; check the user name and password", + server.address() + )), + other => DeployError::Connect { + host: server.address(), + message: other.to_string(), + }, + })?; + stream + .transfer_type(FileType::Binary) + .map_err(|e| ftp_error("the server refused binary mode", &e))?; + let home = stream.pwd().unwrap_or_else(|_| "/".into()); + Ok((stream, home)) +} + +impl Ftp { + /// Connects to `server`, upgrades to TLS for FTPS, signs in with + /// `secret` and switches to binary mode. + /// + /// # Errors + /// [`DeployError::MissingSecret`] without a password, + /// [`DeployError::Connect`] when the server or its TLS fails, + /// [`DeployError::Auth`] when it refuses the sign-in. + /// + /// Socket timeouts that cannot be set are ignored: the connection + /// still works, only without the 60 s limit. + pub fn connect(server: &Server, secret: Option<&Secret>, cancel: &Cancel) -> Result { + let Some(secret) = secret else { + return Err(DeployError::MissingSecret { + server: server.name.clone(), + kind: "password".into(), + }); + }; + let host = server.address(); + let connect_error = |message: String| DeployError::Connect { + host: host.clone(), + message, + }; + let addr = (server.host.as_str(), server.effective_port()) + .to_socket_addrs() + .map_err(|e| connect_error(e.to_string()))? + .next() + .ok_or_else(|| connect_error("the host name has no address".into()))?; + cancel.check()?; + let set_timeouts = |tcp: &std::net::TcpStream| { + let _ = tcp.set_read_timeout(Some(IO_TIMEOUT)); + let _ = tcp.set_write_timeout(Some(IO_TIMEOUT)); + }; + let tcp = std::net::TcpStream::connect_timeout(&addr, CONNECT_TIMEOUT) + .map_err(|e| connect_error(e.to_string()))?; + set_timeouts(&tcp); + let stream = + RustlsFtpStream::connect_with_stream(tcp).map_err(|e| connect_error(e.to_string()))?; + let stream = if server.kind == ServerKind::Ftps { + let config = tls_config(&host)?; + stream + .into_secure(RustlsConnector::from(Arc::new(config)), &server.host) + .map_err(|e| connect_error(format!("TLS failed: {e}")))? + } else { + stream + }; + cancel.check()?; + let (stream, home) = sign_in(stream, server, secret)?; + Ok(Self { + stream, + home, + cancel: cancel.clone(), + }) + } + + /// The control stream. + fn ftp(&mut self) -> &mut RustlsFtpStream { + &mut self.stream + } +} + +impl Remote for Ftp { + /// Creates each missing folder. A refused `MKD` is ignored when the + /// folder exists already, which is the usual reason. + fn mkdir_all(&mut self, dir: &str) -> Result<()> { + let absolute = dir.starts_with('/'); + let mut path = String::new(); + for part in dir.split('/').filter(|p| !p.is_empty()) { + self.cancel.check()?; + if absolute || !path.is_empty() { + path.push('/'); + } + path.push_str(part); + if self.kind(&path)? == Some(true) { + continue; + } + self.ftp() + .mkdir(&path) + .map_err(|e| ftp_error(&format!("cannot create the folder {path}"), &e))?; + } + Ok(()) + } + + fn put(&mut self, path: &str, data: &mut dyn Read, tick: Tick<'_>) -> Result<()> { + let mut transfer = self + .ftp() + .put_with_stream(path) + .map_err(|e| ftp_error(&format!("cannot write {path}"), &e))?; + copy_chunks(data, &mut transfer, tick)?; + transfer + .finish() + .map_err(|e| ftp_error(&format!("cannot finish {path}"), &e)) + } + + fn get(&mut self, path: &str, out: &mut dyn Write, tick: Tick<'_>) -> Result<()> { + let mut transfer = self + .ftp() + .retr_as_stream(path) + .map_err(|e| ftp_error(&format!("cannot read {path}"), &e))?; + copy_chunks(&mut transfer, out, tick)?; + transfer + .finish() + .map_err(|e| ftp_error(&format!("cannot finish {path}"), &e)) + } + + /// A folder is a path the server lets us change into (then we change + /// back to the login folder); a file is one with a size. + fn kind(&mut self, path: &str) -> Result> { + let target = if path.is_empty() { + self.home.clone() + } else { + path.to_string() + }; + if self.ftp().cwd(&target).is_ok() { + let home = self.home.clone(); + self.ftp() + .cwd(&home) + .map_err(|e| ftp_error("cannot return to the login folder", &e))?; + return Ok(Some(true)); + } + Ok(self.ftp().size(&target).ok().map(|_| false)) + } + + fn list(&mut self, path: &str) -> Result> { + let target = if path.is_empty() { + self.home.clone() + } else { + path.to_string() + }; + let lines = self + .ftp() + .list(Some(&target)) + .map_err(|e| ftp_error(&format!("cannot list {path}"), &e))?; + let mut entries: Vec = lines + .iter() + .filter_map(|line| line.parse::().ok()) + .filter(|f| safe_name(f.name())) + .take(MAX_LISTING) + .map(|f| Entry { + name: f.name().to_string(), + is_dir: f.is_directory(), + size: (!f.is_directory()).then(|| f.size() as u64), + }) + .collect(); + entries.sort_by(|a, b| a.name.cmp(&b.name)); + Ok(entries) + } + + /// Sends `QUIT`; a failure is ignored because the connection is + /// dropped right after. + fn close(&mut self) { + let _ = self.ftp().quit(); + } +} diff --git a/crates/deploy/src/job.rs b/crates/deploy/src/job.rs new file mode 100644 index 0000000..95a4ea8 --- /dev/null +++ b/crates/deploy/src/job.rs @@ -0,0 +1,627 @@ +//! Jobs: test a connection, upload or download project paths. +//! +//! A job first plans (walks the local folders or lists the remote ones and +//! maps every file), then connects once and transfers file by file. Its +//! [`Monitor`] holds the progress and the new log lines; the engine reads +//! it from another thread. A failing file is logged and the job goes on; a +//! lost connection, a refused sign-in or a cancel ends it. +//! +//! Folder walks skip symbolic links to folders and the version-control +//! folders `.git`, `.svn` and `.hg`, which are never deployed. + +use std::collections::BTreeSet; +use std::fs::File; +use std::path::Path; +use std::sync::{Arc, Mutex, PoisonError}; + +use serde::Serialize; + +use crate::config::{Server, ServerKind, io_error}; +use crate::error::{DeployError, Result}; +use crate::mapping::{is_excluded, normalize_relative, remote_path}; +use crate::remote::{Cancel, Folder, Remote}; +use crate::secrets::Secret; + +/// Most files one job plans. +pub const MAX_FILES: usize = 200_000; + +/// Deepest folder nesting a job follows. +const MAX_DEPTH: usize = 64; + +/// Folders never deployed. +const VCS_DIRS: [&str; 3] = [".git", ".svn", ".hg"]; + +/// Severity of a log line. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)] +#[serde(rename_all = "lowercase")] +pub enum Level { + /// Something worked. + Info, + /// Something was skipped. + Warning, + /// Something failed. + Error, +} + +/// One line of the deployment log. +#[derive(Debug, Clone, PartialEq, Eq, Serialize)] +pub struct LogLine { + /// Severity. + pub level: Level, + /// The text; never contains a secret. + pub message: String, +} + +/// Progress of a running job. +#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct Progress { + /// `connecting`, `planning`, `transferring`. + pub phase: String, + /// The file being transferred, project-relative. + #[serde(skip_serializing_if = "Option::is_none")] + pub file: Option, + /// Files finished, successful or not. + pub files_done: usize, + /// Files planned. + pub files_total: usize, + /// Bytes transferred. + pub bytes_done: u64, + /// Bytes planned (uploads only; 0 when unknown). + pub bytes_total: u64, +} + +/// What a job shares with its observer. +#[derive(Debug, Default)] +struct Shared { + /// The progress. + progress: Progress, + /// Log lines not taken yet. + lines: Vec, +} + +/// A job's progress and log, readable from another thread. Clones share +/// the state. +#[derive(Debug, Clone, Default)] +pub struct Monitor(Arc>); + +impl Monitor { + /// A fresh monitor. + pub fn new() -> Self { + Self::default() + } + + /// The current progress. + pub fn progress(&self) -> Progress { + self.0 + .lock() + .unwrap_or_else(PoisonError::into_inner) + .progress + .clone() + } + + /// Takes the log lines written since the last call. + pub fn take_lines(&self) -> Vec { + std::mem::take(&mut self.0.lock().unwrap_or_else(PoisonError::into_inner).lines) + } + + /// Changes the progress. + fn update(&self, change: impl FnOnce(&mut Progress)) { + change( + &mut self + .0 + .lock() + .unwrap_or_else(PoisonError::into_inner) + .progress, + ); + } + + /// Adds a log line. + fn log(&self, level: Level, message: String) { + self.0 + .lock() + .unwrap_or_else(PoisonError::into_inner) + .lines + .push(LogLine { level, message }); + } +} + +/// Everything a job needs. +#[derive(Debug, Clone)] +pub struct Job { + /// The server. + pub server: Server, + /// The project root on disk. + pub project_root: std::path::PathBuf, + /// The password or passphrase from the keychain, when the server + /// needs one and it is stored. + pub secret: Option, + /// Lutra's known-hosts file. + pub known_hosts: std::path::PathBuf, + /// Stops the job. + pub cancel: Cancel, + /// Progress and log. + pub monitor: Monitor, +} + +/// What a finished upload or download did. +#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct Summary { + /// Files transferred. + pub files: usize, + /// Bytes transferred. + pub bytes: u64, + /// Files skipped: excluded, not mapped, or missing on the server. + pub skipped: usize, + /// Files that failed. + pub failed: usize, +} + +/// One planned file. +#[derive(Debug, Clone)] +struct Planned { + /// Project-relative path. + local: String, + /// Remote path. + remote: String, + /// Size, when known. + size: u64, +} + +/// Connects to the job's server. +/// +/// # Errors +/// What the transport's `connect` returns, with the secret redacted. +pub fn connect(job: &Job) -> Result> { + job.monitor.update(|p| p.phase = "connecting".into()); + let secret = job.secret.as_ref(); + let remote: Result> = match job.server.kind { + ServerKind::Sftp => { + crate::sftp::Sftp::connect(&job.server, secret, &job.known_hosts, &job.cancel) + .map(|s| Box::new(s) as Box) + } + ServerKind::Ftp | ServerKind::Ftps => { + crate::ftp::Ftp::connect(&job.server, secret, &job.cancel) + .map(|s| Box::new(s) as Box) + } + ServerKind::Folder => { + Folder::open(Path::new(&job.server.root)).map(|s| Box::new(s) as Box) + } + }; + remote.map_err(|e| e.redacted(secret.map(Secret::expose))) +} + +/// Connects, signs in and checks the root folder. Returns a sentence for +/// the user. +/// +/// # Errors +/// What [`connect`] returns. +pub fn test_connection(job: &Job) -> Result { + let mut remote = connect(job)?; + let root = if job.server.kind == ServerKind::Folder { + String::new() + } else { + job.server.root.clone() + }; + let kind = remote + .kind(&root) + .map_err(|e| e.redacted(job.secret.as_ref().map(Secret::expose))); + remote.close(); + let kind = kind?; + let name = &job.server.name; + let shown = display_root(&root); + let message = match kind { + _ if job.server.kind == ServerKind::Folder => { + format!("Connected to the folder {}.", job.server.root) + } + Some(true) => format!("Connected to {name}; the root folder {shown} exists."), + Some(false) => { + format!("Connected to {name}, but the root {shown} is a file, not a folder.") + } + None => format!( + "Connected to {name}; the root folder {shown} does not exist yet and is created by the first upload." + ), + }; + job.monitor.log(Level::Info, message.clone()); + Ok(message) +} + +/// The root as shown to the user. +fn display_root(root: &str) -> &str { + if root.is_empty() { + "(login folder)" + } else { + root + } +} + +/// Adds the files under the project-relative `rel` to `plan`. +fn walk_local( + job: &Job, + rel: &str, + depth: usize, + plan: &mut Vec, + skipped: &mut usize, +) -> Result<()> { + job.cancel.check()?; + let path = if rel.is_empty() { + job.project_root.clone() + } else { + job.project_root.join(rel) + }; + let meta = std::fs::symlink_metadata(&path).map_err(|e| io_error(&path, &e))?; + let is_dir = meta.is_dir() || (meta.file_type().is_symlink() && depth == 0 && path.is_dir()); + if !is_dir { + let size = std::fs::metadata(&path) + .map_err(|e| io_error(&path, &e))? + .len(); + match remote_path(&job.server, rel) { + Some(remote) if !rel.is_empty() => plan.push(Planned { + local: rel.to_string(), + remote, + size, + }), + _ => { + *skipped += 1; + job.monitor.log( + Level::Warning, + format!("{rel}: skipped (excluded or not mapped)"), + ); + } + } + if plan.len() > MAX_FILES { + return Err(DeployError::Invalid(format!( + "more than {MAX_FILES} files; upload a smaller folder" + ))); + } + return Ok(()); + } + if depth > MAX_DEPTH || (!rel.is_empty() && is_excluded(&job.server, rel)) { + return Ok(()); + } + let mut names: Vec = std::fs::read_dir(&path) + .map_err(|e| io_error(&path, &e))? + .filter_map(|e| e.ok()) + .filter(|e| { + e.file_type() + .map(|t| !(t.is_symlink() && e.path().is_dir())) + .unwrap_or(false) + }) + .map(|e| e.file_name().to_string_lossy().into_owned()) + .filter(|n| !VCS_DIRS.contains(&n.as_str())) + .collect(); + names.sort(); + for name in names { + let child = if rel.is_empty() { + name + } else { + format!("{rel}/{name}") + }; + walk_local(job, &child, depth + 1, plan, skipped)?; + } + Ok(()) +} + +/// The parent folder of a remote path; empty for a top-level one. +fn parent(remote: &str) -> &str { + match remote.rfind('/') { + Some(0) => "/", + Some(i) => remote.get(..i).unwrap_or_default(), + None => "", + } +} + +/// Whether an error ends the whole job instead of one file. +fn is_fatal(e: &DeployError) -> bool { + !matches!(e, DeployError::Transfer(_)) +} + +/// Uploads the project-relative `paths` (files or folders). +/// +/// # Errors +/// [`DeployError::Invalid`] for a path outside the project, otherwise +/// what [`connect`] returns, or [`DeployError::Cancelled`]. Failing files +/// are counted in [`Summary::failed`] instead. +pub fn upload(job: &Job, paths: &[String]) -> Result { + let secret = job.secret.as_ref().map(Secret::expose); + job.monitor.update(|p| p.phase = "planning".into()); + let mut plan = Vec::new(); + let mut summary = Summary::default(); + for path in paths { + let rel = normalize_relative(path) + .ok_or_else(|| DeployError::Invalid(format!("{path:?} is not inside the project")))?; + walk_local(job, &rel, 0, &mut plan, &mut summary.skipped)?; + } + let total: u64 = plan.iter().map(|p| p.size).sum(); + job.monitor.update(|p| { + p.files_total = plan.len(); + p.bytes_total = total; + }); + if plan.is_empty() { + job.monitor.log(Level::Warning, "nothing to upload".into()); + return Ok(summary); + } + let mut remote = connect(job)?; + job.monitor.update(|p| p.phase = "transferring".into()); + let mut made: BTreeSet = BTreeSet::new(); + let result = (|| { + for file in &plan { + job.cancel.check()?; + job.monitor.update(|p| p.file = Some(file.local.clone())); + let outcome = + upload_one(job, remote.as_mut(), file, &mut made).map_err(|e| e.redacted(secret)); + job.monitor.update(|p| p.files_done += 1); + match outcome { + Ok(()) => { + summary.files += 1; + summary.bytes += file.size; + job.monitor + .log(Level::Info, format!("{} → {}", file.local, file.remote)); + } + Err(e) if is_fatal(&e) => return Err(e), + Err(e) => { + summary.failed += 1; + job.monitor + .log(Level::Error, format!("{}: {e}", file.local)); + } + } + } + Ok(()) + })(); + remote.close(); + result.map(|()| summary) +} + +/// Uploads one file, creating its remote folder first. +fn upload_one( + job: &Job, + remote: &mut dyn Remote, + file: &Planned, + made: &mut BTreeSet, +) -> Result<()> { + let dir = parent(&file.remote).to_string(); + if !dir.is_empty() && dir != "/" && !made.contains(&dir) { + remote.mkdir_all(&dir)?; + made.insert(dir); + } + let path = job.project_root.join(&file.local); + let mut data = File::open(&path).map_err(|e| io_error(&path, &e))?; + let monitor = job.monitor.clone(); + let cancel = job.cancel.clone(); + remote.put(&file.remote, &mut data, &mut |n| { + monitor.update(|p| p.bytes_done = p.bytes_done.saturating_add(n)); + cancel.check() + }) +} + +/// Adds the remote files under the project-relative `rel` to `plan`. +fn walk_remote( + job: &Job, + remote: &mut dyn Remote, + rel: &str, + depth: usize, + plan: &mut Vec, + skipped: &mut usize, +) -> Result<()> { + job.cancel.check()?; + let Some(path) = remote_path(&job.server, rel) else { + *skipped += 1; + job.monitor.log( + Level::Warning, + format!("{rel}: skipped (excluded or not mapped)"), + ); + return Ok(()); + }; + match remote.kind(&path)? { + None => { + *skipped += 1; + job.monitor + .log(Level::Warning, format!("{rel}: not on the server ({path})")); + } + Some(false) if !rel.is_empty() => plan.push(Planned { + local: rel.to_string(), + remote: path, + size: 0, + }), + Some(false) => { + *skipped += 1; + } + Some(true) if depth <= MAX_DEPTH => { + for entry in remote.list(&path)? { + if VCS_DIRS.contains(&entry.name.as_str()) { + continue; + } + let child = if rel.is_empty() { + entry.name + } else { + format!("{rel}/{}", entry.name) + }; + walk_remote(job, remote, &child, depth + 1, plan, skipped)?; + if plan.len() > MAX_FILES { + return Err(DeployError::Invalid(format!( + "more than {MAX_FILES} files; download a smaller folder" + ))); + } + } + } + Some(true) => {} + } + Ok(()) +} + +/// Downloads the project-relative `paths` (files or folders) from the +/// server into the project, replacing local files. +/// +/// # Errors +/// Like [`upload`]. +pub fn download(job: &Job, paths: &[String]) -> Result { + let secret = job.secret.as_ref().map(Secret::expose); + let mut rels = Vec::new(); + for path in paths { + rels.push( + normalize_relative(path).ok_or_else(|| { + DeployError::Invalid(format!("{path:?} is not inside the project")) + })?, + ); + } + let mut remote = connect(job)?; + let mut summary = Summary::default(); + let result = (|| { + job.monitor.update(|p| p.phase = "planning".into()); + let mut plan = Vec::new(); + for rel in &rels { + walk_remote( + job, + remote.as_mut(), + rel, + 0, + &mut plan, + &mut summary.skipped, + ) + .map_err(|e| e.redacted(secret))?; + } + job.monitor.update(|p| { + p.files_total = plan.len(); + p.phase = "transferring".into(); + }); + for file in &plan { + job.cancel.check()?; + job.monitor.update(|p| p.file = Some(file.local.clone())); + let outcome = download_one(job, remote.as_mut(), file).map_err(|e| e.redacted(secret)); + job.monitor.update(|p| p.files_done += 1); + match outcome { + Ok(bytes) => { + summary.files += 1; + summary.bytes += bytes; + job.monitor + .log(Level::Info, format!("{} ← {}", file.local, file.remote)); + } + Err(e) if is_fatal(&e) => return Err(e), + Err(e) => { + summary.failed += 1; + job.monitor + .log(Level::Error, format!("{}: {e}", file.local)); + } + } + } + Ok(()) + })(); + remote.close(); + result.map(|()| summary) +} + +/// Downloads one file through a temporary file next to it. Returns its +/// size. A temporary file that cannot be removed after a failure is left; +/// the next download of the same file replaces it. +fn download_one(job: &Job, remote: &mut dyn Remote, file: &Planned) -> Result { + let target = job.project_root.join(&file.local); + if let Some(dir) = target.parent() { + std::fs::create_dir_all(dir).map_err(|e| io_error(dir, &e))?; + } + let temp = target.with_extension("lutra-download"); + let monitor = job.monitor.clone(); + let cancel = job.cancel.clone(); + let mut bytes = 0u64; + let result = (|| { + let mut out = File::create(&temp).map_err(|e| io_error(&temp, &e))?; + remote.get(&file.remote, &mut out, &mut |n| { + bytes = bytes.saturating_add(n); + monitor.update(|p| p.bytes_done = p.bytes_done.saturating_add(n)); + cancel.check() + })?; + drop(out); + std::fs::rename(&temp, &target).map_err(|e| io_error(&target, &e)) + })(); + if result.is_err() { + let _ = std::fs::remove_file(&temp); + } + result.map(|()| bytes) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::config::{AuthKind, Mapping}; + + fn folder_job(project: &Path, target: &Path) -> Job { + Job { + server: Server { + id: "f".into(), + name: "Mounted".into(), + kind: ServerKind::Folder, + host: String::new(), + port: None, + user: String::new(), + root: target.display().to_string(), + auth: AuthKind::Password, + key_path: None, + mappings: vec![Mapping { + local: String::new(), + remote: "site".into(), + }], + excluded: vec!["*.log".into()], + upload_on_save: false, + }, + project_root: project.to_path_buf(), + secret: None, + known_hosts: project.join("kh"), + cancel: Cancel::new(), + monitor: Monitor::new(), + } + } + + #[test] + fn parents() { + assert_eq!(parent("/a/b"), "/a"); + assert_eq!(parent("/a"), "/"); + assert_eq!(parent("a/b"), "a"); + assert_eq!(parent("a"), ""); + } + + #[test] + fn upload_and_download_through_a_folder() { + let project = tempfile::tempdir().unwrap(); + let target = tempfile::tempdir().unwrap(); + std::fs::create_dir_all(project.path().join("src/sub")).unwrap(); + std::fs::create_dir_all(project.path().join(".git")).unwrap(); + std::fs::write(project.path().join("src/a.php"), " Result> { + let meta = match std::fs::metadata(path) { + Ok(meta) => meta, + Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(Vec::new()), + Err(e) => return Err(io_error(path, &e)), + }; + if meta.len() > MAX_FILE { + return Err(DeployError::Transfer(format!( + "{} is larger than 1 MiB", + path.display() + ))); + } + let text = std::fs::read_to_string(path).map_err(|e| io_error(path, &e))?; + Ok(text + .lines() + .filter(|l| !l.trim().is_empty() && !l.trim_start().starts_with('#')) + .filter_map(|line| { + let mut fields = line.split_whitespace(); + let host = fields.next()?; + let algorithm = fields.next()?; + let fingerprint = fields.next()?; + Some(HostKey { + host: host.to_string(), + algorithm: algorithm.to_string(), + fingerprint: fingerprint.to_string(), + }) + }) + .collect()) +} + +/// Checks the offered `fingerprint` of `host` against the file. +/// +/// # Errors +/// [`DeployError::Transfer`] when the file cannot be read. +pub fn check(path: &Path, host: &str, fingerprint: &str) -> Result { + let keys = load(path)?; + let mut known: Option = None; + for key in keys.iter().filter(|k| k.host == host) { + if key.fingerprint == fingerprint { + return Ok(HostKeyStatus::Trusted); + } + known = Some(key.fingerprint.clone()); + } + Ok(known.map_or(HostKeyStatus::Unknown, HostKeyStatus::Mismatch)) +} + +/// Writes `keys` to the file atomically. +fn store(path: &Path, keys: &[HostKey]) -> Result<()> { + if let Some(dir) = path.parent() { + std::fs::create_dir_all(dir).map_err(|e| io_error(dir, &e))?; + } + let mut text = String::from("# Lutra deployment: trusted SFTP host keys\n"); + for key in keys { + text.push_str(&format!( + "{} {} {}\n", + key.host, key.algorithm, key.fingerprint + )); + } + lutra_vfs::write_atomic(path, text.as_bytes()).map_err(|e| DeployError::Transfer(e.to_string())) +} + +/// Trusts `key` for its host, unless another key is trusted for it. +/// +/// # Errors +/// [`DeployError::Invalid`] for a field with whitespace or an empty one, +/// [`DeployError::HostKeyMismatch`] when another key is trusted (remove it +/// with [`forget`] first), [`DeployError::Transfer`] when the file cannot +/// be read or written. +pub fn trust(path: &Path, key: &HostKey) -> Result<()> { + for field in [&key.host, &key.algorithm, &key.fingerprint] { + if field.is_empty() + || field.len() > 512 + || field.chars().any(|c| c.is_whitespace() || c.is_control()) + { + return Err(DeployError::Invalid(format!( + "{field:?} is not a valid host key field" + ))); + } + } + let mut keys = load(path)?; + if let Some(old) = keys.iter().find(|k| k.host == key.host) { + if old.fingerprint == key.fingerprint { + return Ok(()); + } + return Err(DeployError::HostKeyMismatch { + host: key.host.clone(), + algorithm: key.algorithm.clone(), + fingerprint: key.fingerprint.clone(), + known: old.fingerprint.clone(), + }); + } + keys.push(key.clone()); + store(path, &keys) +} + +/// Removes every trusted key of `host`. Returns whether there was one. +/// +/// # Errors +/// [`DeployError::Transfer`] when the file cannot be read or written. +pub fn forget(path: &Path, host: &str) -> Result { + let mut keys = load(path)?; + let before = keys.len(); + keys.retain(|k| k.host != host); + if keys.len() == before { + return Ok(false); + } + store(path, &keys)?; + Ok(true) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn key(fp: &str) -> HostKey { + HostKey { + host: "h:22".into(), + algorithm: "ssh-ed25519".into(), + fingerprint: fp.into(), + } + } + + #[test] + fn trust_check_mismatch_forget() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("d").join("known_hosts"); + assert_eq!( + check(&path, "h:22", "SHA256:a").unwrap(), + HostKeyStatus::Unknown + ); + trust(&path, &key("SHA256:a")).unwrap(); + trust(&path, &key("SHA256:a")).unwrap(); + assert_eq!( + check(&path, "h:22", "SHA256:a").unwrap(), + HostKeyStatus::Trusted + ); + assert_eq!( + check(&path, "h:22", "SHA256:b").unwrap(), + HostKeyStatus::Mismatch("SHA256:a".into()) + ); + assert!(matches!( + trust(&path, &key("SHA256:b")), + Err(DeployError::HostKeyMismatch { .. }) + )); + assert_eq!( + check(&path, "other:22", "SHA256:a").unwrap(), + HostKeyStatus::Unknown + ); + assert!(forget(&path, "h:22").unwrap()); + assert!(!forget(&path, "h:22").unwrap()); + trust(&path, &key("SHA256:b")).unwrap(); + assert_eq!(load(&path).unwrap().len(), 1); + assert!(trust(&path, &key("SHA256:a b")).is_err()); + } +} diff --git a/crates/deploy/src/lib.rs b/crates/deploy/src/lib.rs new file mode 100644 index 0000000..89a835f --- /dev/null +++ b/crates/deploy/src/lib.rs @@ -0,0 +1,36 @@ +//! Deployment (G17, owner decision D27): upload and download project files +//! over SFTP, FTP, FTPS or to a local or mounted folder, with path +//! mappings and excluded paths per server. +//! +//! - [`config`]: server definitions, stored per project without secrets. +//! - [`mapping`]: which remote path a project file goes to. +//! - [`secrets`]: passwords and passphrases, only in the OS keychain. +//! - [`known_hosts`]: Lutra's own trusted SFTP host keys. +//! - [`remote`]: the operations every transport offers (the extension +//! point for the remote browser and compare of D2), and the folder +//! transport; `sftp` and `ftp` are the network ones. +//! - [`job`]: test connection, upload and download with progress, log and +//! cancel. +//! +//! Pure Rust: `russh` and `russh-sftp` for SFTP, `suppaftp` with `rustls` +//! for FTP and FTPS, `ring` for cryptography, `keyring` for the keychain. +//! No credential is ever written to a file, a log or an error message. + +pub mod config; +pub mod error; +mod ftp; +pub mod job; +pub mod known_hosts; +pub mod mapping; +pub mod remote; +pub mod secrets; +mod sftp; + +#[cfg(feature = "testserver")] +pub mod testserver; + +pub use config::{AuthKind, Deployment, Mapping, Server, ServerKind}; +pub use error::{DeployError, Result}; +pub use job::{Job, Level, LogLine, Monitor, Progress, Summary}; +pub use remote::Cancel; +pub use secrets::{Keychain, MemoryStore, Secret, SecretStore}; diff --git a/crates/deploy/src/mapping.rs b/crates/deploy/src/mapping.rs new file mode 100644 index 0000000..9f957c1 --- /dev/null +++ b/crates/deploy/src/mapping.rs @@ -0,0 +1,262 @@ +//! Path mappings: which remote path a project file goes to, and which files +//! are excluded. +//! +//! Local paths are project-relative with `/`. A file maps through the +//! mapping with the longest local folder that contains it; without any +//! mapping the project root maps to the server's root. Remote paths are +//! joined with `/`, never contain `..`, and stay inside the mapping's +//! remote folder. + +use crate::config::Server; + +/// `path` with `\` turned into `/`, empty and `.` segments dropped, and no +/// leading or trailing `/`. `None` if it contains `..` or a control +/// character. +pub fn normalize_relative(path: &str) -> Option { + if path.chars().any(char::is_control) { + return None; + } + let mut parts = Vec::new(); + for part in path.split(['/', '\\']) { + match part { + "" | "." => {} + ".." => return None, + other => parts.push(other), + } + } + Some(parts.join("/")) +} + +/// Whether `pattern` is a usable excluded path: not empty, no `..`, no +/// control characters. +pub fn valid_pattern(pattern: &str) -> bool { + !pattern.is_empty() && normalize_relative(pattern).is_some_and(|p| !p.is_empty()) +} + +/// Whether `name` matches `pattern`, where `*` matches any run of +/// characters and `?` exactly one. +fn glob(pattern: &[char], name: &[char]) -> bool { + let (mut p, mut n) = (0usize, 0usize); + let mut star: Option<(usize, usize)> = None; + while n < name.len() { + match pattern.get(p) { + Some('*') => { + star = Some((p, n)); + p += 1; + } + Some(c) if *c == '?' || Some(c) == name.get(n) => { + p += 1; + n += 1; + } + _ => match star { + Some((sp, sn)) => { + p = sp + 1; + n = sn + 1; + star = Some((sp, sn + 1)); + } + None => return false, + }, + } + } + pattern + .get(p..) + .is_some_and(|rest| rest.iter().all(|c| *c == '*')) +} + +/// Whether the project-relative `path` (normalized) is excluded by +/// `server`: a pattern without `*` or `?` excludes that path and +/// everything below it; a pattern with them is matched against every +/// segment's name. +pub fn is_excluded(server: &Server, path: &str) -> bool { + server.excluded.iter().any(|pattern| { + if pattern.contains(['*', '?']) { + let pattern: Vec = pattern.chars().collect(); + path.split('/').any(|segment| { + let name: Vec = segment.chars().collect(); + glob(&pattern, &name) + }) + } else { + path == pattern + || path + .strip_prefix(pattern.as_str()) + .is_some_and(|rest| rest.starts_with('/')) + } + }) +} + +/// Joins remote path pieces with `/`, skipping empty ones. Keeps a leading +/// `/` of the first non-empty piece. +fn join(pieces: &[&str]) -> String { + let mut out = String::new(); + for piece in pieces.iter().filter(|p| !p.is_empty()) { + if out.is_empty() { + out.push_str(piece.trim_end_matches('/')); + if out.is_empty() { + out.push('/'); + } + } else { + if !out.ends_with('/') { + out.push('/'); + } + out.push_str(piece.trim_matches('/')); + } + } + out +} + +/// The remote path of the project-relative `path` on `server`, or `None` +/// when it is excluded, escapes the project, or no mapping covers it. +/// For a folder server the result is relative to its root folder. +pub fn remote_path(server: &Server, path: &str) -> Option { + let path = normalize_relative(path)?; + if is_excluded(server, &path) { + return None; + } + let (local, remote) = if server.mappings.is_empty() { + ("", "") + } else { + server + .mappings + .iter() + .filter(|m| { + m.local.is_empty() + || path == m.local + || path + .strip_prefix(m.local.as_str()) + .is_some_and(|rest| rest.starts_with('/')) + }) + .max_by_key(|m| m.local.len()) + .map(|m| (m.local.as_str(), m.remote.as_str()))? + }; + let rest = path + .strip_prefix(local) + .unwrap_or(&path) + .trim_start_matches('/'); + let root = if remote.starts_with('/') || server.kind == crate::ServerKind::Folder { + "" + } else { + server.root.as_str() + }; + let joined = join(&[root, remote, rest]); + Some(joined) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::config::{AuthKind, Mapping, ServerKind}; + use proptest::prelude::*; + + fn server(mappings: &[(&str, &str)], excluded: &[&str]) -> Server { + Server { + id: "s".into(), + name: "S".into(), + kind: ServerKind::Sftp, + host: "h".into(), + port: None, + user: "u".into(), + root: "/var/www".into(), + auth: AuthKind::Password, + key_path: None, + mappings: mappings + .iter() + .map(|(l, r)| Mapping { + local: (*l).into(), + remote: (*r).into(), + }) + .collect(), + excluded: excluded.iter().map(|e| (*e).into()).collect(), + upload_on_save: false, + } + } + + #[test] + fn normalizes() { + assert_eq!(normalize_relative("./a//b\\c/").unwrap(), "a/b/c"); + assert_eq!(normalize_relative("").unwrap(), ""); + assert!(normalize_relative("a/../b").is_none()); + assert!(normalize_relative("a\nb").is_none()); + } + + #[test] + fn default_mapping_uses_the_root() { + let s = server(&[], &[]); + assert_eq!(remote_path(&s, "src/a.php").unwrap(), "/var/www/src/a.php"); + let mut s = s; + s.root = String::new(); + assert_eq!(remote_path(&s, "src/a.php").unwrap(), "src/a.php"); + s.root = "/".into(); + assert_eq!(remote_path(&s, "a.php").unwrap(), "/a.php"); + } + + #[test] + fn longest_mapping_wins() { + let s = server(&[("", "app"), ("public", "/srv/html"), ("pub", "x")], &[]); + assert_eq!( + remote_path(&s, "public/index.php").unwrap(), + "/srv/html/index.php" + ); + assert_eq!(remote_path(&s, "public").unwrap(), "/srv/html"); + assert_eq!( + remote_path(&s, "publicity.txt").unwrap(), + "/var/www/app/publicity.txt" + ); + assert_eq!(remote_path(&s, "pub/a").unwrap(), "/var/www/x/a"); + } + + #[test] + fn unmapped_and_excluded_paths_are_none() { + let s = server( + &[("public", "html")], + &[".env", "var/cache", "*.log", "tmp?"], + ); + assert!(remote_path(&s, "src/a.php").is_none()); + let s = server(&[], &[".env", "var/cache", "*.log", "tmp?"]); + assert!(remote_path(&s, ".env").is_none()); + assert!(remote_path(&s, "var/cache/x").is_none()); + assert!(remote_path(&s, "var/cache").is_none()); + assert!(remote_path(&s, "var/cached").is_some()); + assert!(remote_path(&s, "logs/app.log").is_none()); + assert!(remote_path(&s, "tmp1/a").is_none()); + assert!(remote_path(&s, "tmp12/a").is_some()); + assert!(remote_path(&s, "../x").is_none()); + } + + #[test] + fn globs() { + let g = |p: &str, n: &str| { + glob( + &p.chars().collect::>(), + &n.chars().collect::>(), + ) + }; + assert!(g("*.log", "a.log")); + assert!(g("*", "")); + assert!(g("a*b*c", "aXbYbZc")); + assert!(!g("a*b", "aXc")); + assert!(!g("?", "")); + } + + proptest! { + #[test] + fn mapped_paths_stay_under_the_mapping( + parts in proptest::collection::vec("[a-zA-Z0-9._ -]{1,8}", 1..6), + remote in "[a-z]{0,6}", + ) { + let s = server(&[("", &remote)], &[]); + let path = parts.join("/"); + if let Some(r) = remote_path(&s, &path) { + let base = join(&["/var/www", &remote]); + prop_assert!(r.starts_with(&base)); + prop_assert!(!r.split('/').any(|seg| seg == "..")); + } else { + prop_assert!(normalize_relative(&path).is_none()); + } + } + + #[test] + fn glob_never_panics(p in ".{0,12}", n in ".{0,12}") { + let _ = glob(&p.chars().collect::>(), &n.chars().collect::>()); + } + } +} diff --git a/crates/deploy/src/remote.rs b/crates/deploy/src/remote.rs new file mode 100644 index 0000000..23455b5 --- /dev/null +++ b/crates/deploy/src/remote.rs @@ -0,0 +1,294 @@ +//! The operations every transport offers, cancellation and progress, and +//! the folder transport. +//! +//! Remote paths are `/`-separated strings as [`crate::mapping`] builds +//! them. Directory listings come from the server and are untrusted: names +//! with `/`, `\`, control characters, `.` or `..` are dropped by +//! [`safe_name`] before they become local paths. + +use std::io::{Read, Write}; +use std::path::{Path, PathBuf}; +use std::sync::Arc; +use std::sync::atomic::{AtomicBool, Ordering}; + +use crate::config::io_error; +use crate::error::{DeployError, Result}; +use crate::mapping::normalize_relative; + +/// Size of one transfer chunk; progress and cancellation are checked +/// between chunks. +pub const CHUNK: usize = 32 * 1024; + +/// Most entries read from one remote folder. +pub const MAX_LISTING: usize = 100_000; + +/// Stops a running job. Clones share the flag. +#[derive(Debug, Clone, Default)] +pub struct Cancel(Arc); + +impl Cancel { + /// A flag that is not set. + pub fn new() -> Self { + Self::default() + } + + /// Asks the job to stop. + pub fn cancel(&self) { + self.0.store(true, Ordering::SeqCst); + } + + /// Whether the job was asked to stop. + pub fn is_cancelled(&self) -> bool { + self.0.load(Ordering::SeqCst) + } + + /// `Err(Cancelled)` once the flag is set. + /// + /// # Errors + /// [`DeployError::Cancelled`]. + pub fn check(&self) -> Result<()> { + if self.is_cancelled() { + Err(DeployError::Cancelled) + } else { + Ok(()) + } + } +} + +/// One entry of a remote folder. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct Entry { + /// The entry's name, checked by [`safe_name`]. + pub name: String, + /// Whether it is a folder. + pub is_dir: bool, + /// Its size in bytes, when the server said. + pub size: Option, +} + +/// Whether a name from a remote listing may become a local path segment. +pub fn safe_name(name: &str) -> bool { + !name.is_empty() + && name != "." + && name != ".." + && name.len() <= 255 + && !name.contains(['/', '\\']) + && !name.chars().any(char::is_control) +} + +/// Called after every chunk with its size; returning an error (such as +/// [`DeployError::Cancelled`]) stops the transfer. +pub type Tick<'a> = &'a mut dyn FnMut(u64) -> Result<()>; + +/// A connected server. +pub trait Remote { + /// Creates `dir` and every missing parent. + /// + /// # Errors + /// [`DeployError::Transfer`] when the server refuses. + fn mkdir_all(&mut self, dir: &str) -> Result<()>; + + /// Writes `data` to the file `path`, replacing it. + /// + /// # Errors + /// [`DeployError::Transfer`], or what `tick` returned. + fn put(&mut self, path: &str, data: &mut dyn Read, tick: Tick<'_>) -> Result<()>; + + /// Reads the file `path` into `out`. + /// + /// # Errors + /// [`DeployError::Transfer`], or what `tick` returned. + fn get(&mut self, path: &str, out: &mut dyn Write, tick: Tick<'_>) -> Result<()>; + + /// Whether `path` is a folder (`Some(true)`), a file (`Some(false)`) + /// or missing (`None`). + /// + /// # Errors + /// [`DeployError::Transfer`] when the server fails otherwise. + fn kind(&mut self, path: &str) -> Result>; + + /// The entries of the folder `path`, without unsafe names, at most + /// [`MAX_LISTING`]. Meant for downloads now and the remote browser + /// later. + /// + /// # Errors + /// [`DeployError::Transfer`] when the server refuses. + fn list(&mut self, path: &str) -> Result>; + + /// Ends the session politely; failures are ignored because the + /// connection is dropped anyway. + fn close(&mut self); +} + +/// Copies `from` into `to` in chunks, calling `tick` after each. +/// +/// # Errors +/// [`DeployError::Transfer`] on a read or write failure, or what `tick` +/// returned. +pub fn copy_chunks(from: &mut dyn Read, to: &mut dyn Write, tick: Tick<'_>) -> Result { + let mut buf = vec![0u8; CHUNK]; + let mut total = 0u64; + loop { + let n = from + .read(&mut buf) + .map_err(|e| DeployError::Transfer(format!("read failed: {e}")))?; + if n == 0 { + break; + } + let chunk = buf.get(..n).unwrap_or_default(); + to.write_all(chunk) + .map_err(|e| DeployError::Transfer(format!("write failed: {e}")))?; + total = total.saturating_add(n as u64); + tick(n as u64)?; + } + to.flush() + .map_err(|e| DeployError::Transfer(format!("write failed: {e}")))?; + Ok(total) +} + +/// A local or mounted folder as a server. +#[derive(Debug)] +pub struct Folder { + /// The deployment root. + root: PathBuf, +} + +impl Folder { + /// Opens the folder `root`. + /// + /// # Errors + /// [`DeployError::Connect`] when it is not an existing folder. + pub fn open(root: &Path) -> Result { + if !root.is_dir() { + return Err(DeployError::Connect { + host: root.display().to_string(), + message: "the folder does not exist or is not mounted".into(), + }); + } + Ok(Self { + root: root.to_path_buf(), + }) + } + + /// The local path of the remote `path`, always inside the root. + fn resolve(&self, path: &str) -> Result { + let rel = normalize_relative(path).ok_or_else(|| { + DeployError::Invalid(format!("{path:?} leaves the deployment folder")) + })?; + Ok(if rel.is_empty() { + self.root.clone() + } else { + self.root.join(rel) + }) + } +} + +impl Remote for Folder { + fn mkdir_all(&mut self, dir: &str) -> Result<()> { + let path = self.resolve(dir)?; + std::fs::create_dir_all(&path).map_err(|e| io_error(&path, &e)) + } + + /// Writes a temporary file next to the target and renames it. A failed + /// or cancelled write removes the temporary file; if even that fails it + /// is left, and the next upload of the same file replaces it. + fn put(&mut self, path: &str, data: &mut dyn Read, tick: Tick<'_>) -> Result<()> { + let target = self.resolve(path)?; + let temp = target.with_extension("lutra-upload"); + let result = (|| { + let mut file = std::fs::File::create(&temp).map_err(|e| io_error(&temp, &e))?; + copy_chunks(data, &mut file, tick)?; + drop(file); + std::fs::rename(&temp, &target).map_err(|e| io_error(&target, &e)) + })(); + if result.is_err() { + let _ = std::fs::remove_file(&temp); + } + result + } + + fn get(&mut self, path: &str, out: &mut dyn Write, tick: Tick<'_>) -> Result<()> { + let source = self.resolve(path)?; + let mut file = std::fs::File::open(&source).map_err(|e| io_error(&source, &e))?; + copy_chunks(&mut file, out, tick).map(|_| ()) + } + + fn kind(&mut self, path: &str) -> Result> { + let path = self.resolve(path)?; + match std::fs::metadata(&path) { + Ok(meta) => Ok(Some(meta.is_dir())), + Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None), + Err(e) => Err(io_error(&path, &e)), + } + } + + fn list(&mut self, path: &str) -> Result> { + let dir = self.resolve(path)?; + let mut entries = Vec::new(); + for entry in std::fs::read_dir(&dir).map_err(|e| io_error(&dir, &e))? { + let entry = entry.map_err(|e| io_error(&dir, &e))?; + let name = entry.file_name().to_string_lossy().into_owned(); + if !safe_name(&name) { + continue; + } + let Ok(meta) = entry.metadata() else { + continue; + }; + entries.push(Entry { + name, + is_dir: meta.is_dir(), + size: meta.is_file().then_some(meta.len()), + }); + if entries.len() >= MAX_LISTING { + break; + } + } + entries.sort_by(|a, b| a.name.cmp(&b.name)); + Ok(entries) + } + + fn close(&mut self) {} +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn names() { + assert!(safe_name("a.php")); + for bad in ["", ".", "..", "a/b", "a\\b", "a\nb"] { + assert!(!safe_name(bad), "{bad:?}"); + } + } + + #[test] + fn folder_round_trip() { + let dir = tempfile::tempdir().unwrap(); + let mut f = Folder::open(dir.path()).unwrap(); + f.mkdir_all("/a/b").unwrap(); + let mut ticks = 0u64; + f.put("a/b/x.txt", &mut &b"hello"[..], &mut |n| { + ticks += n; + Ok(()) + }) + .unwrap(); + assert_eq!(ticks, 5); + assert_eq!(f.kind("a/b").unwrap(), Some(true)); + assert_eq!(f.kind("a/b/x.txt").unwrap(), Some(false)); + assert_eq!(f.kind("nope").unwrap(), None); + let mut out = Vec::new(); + f.get("a/b/x.txt", &mut out, &mut |_| Ok(())).unwrap(); + assert_eq!(out, b"hello"); + assert_eq!(f.list("a/b").unwrap()[0].name, "x.txt"); + assert!(f.resolve("../x").is_err()); + let cancel = Cancel::new(); + cancel.cancel(); + let err = f + .put("a/y.txt", &mut &b"z"[..], &mut |_| cancel.check()) + .unwrap_err(); + assert_eq!(err, DeployError::Cancelled); + assert_eq!(f.kind("a/y.txt").unwrap(), None); + assert!(Folder::open(&dir.path().join("missing")).is_err()); + } +} diff --git a/crates/deploy/src/secrets.rs b/crates/deploy/src/secrets.rs new file mode 100644 index 0000000..f7edb10 --- /dev/null +++ b/crates/deploy/src/secrets.rs @@ -0,0 +1,195 @@ +//! Passwords and passphrases, kept only in the OS keychain (owner decision +//! D27): the Windows Credential Manager and the macOS Keychain through the +//! `keyring` crate. Lutra never writes a secret to a file, a log, the +//! journal, an event or an error message. +//! +//! Entries use the service [`SERVICE`] and the account +//! `//`, so each project's servers have their +//! own entries. Other platforms have no store yet and refuse with a clear +//! message; agent and key sign-in without a passphrase still work there. +//! +//! Tests and hosts without a keychain use [`MemoryStore`]. + +use std::collections::BTreeMap; +use std::fmt; +use std::sync::{Mutex, PoisonError}; + +use crate::error::{DeployError, Result}; + +/// The keychain service name of Lutra's deployment entries. +pub const SERVICE: &str = "Lutra Deployment"; + +/// Longest secret accepted. +pub const MAX_SECRET: usize = 4096; + +/// A password or passphrase. Its `Debug` output is `Secret(***)`, so it +/// never leaks through a debug print. +#[derive(Clone, PartialEq, Eq)] +pub struct Secret(String); + +impl Secret { + /// Wraps `value`. + /// + /// # Errors + /// [`DeployError::Invalid`] if it is empty, longer than + /// [`MAX_SECRET`] bytes, or contains a NUL. + pub fn new(value: String) -> Result { + if value.is_empty() || value.len() > MAX_SECRET || value.contains('\0') { + return Err(DeployError::Invalid( + "the secret must be 1 to 4096 bytes without NUL".into(), + )); + } + Ok(Self(value)) + } + + /// The plain value, for the protocol call that needs it. + pub fn expose(&self) -> &str { + &self.0 + } +} + +impl fmt::Debug for Secret { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str("Secret(***)") + } +} + +/// Where secrets are kept. +pub trait SecretStore: Send + Sync + fmt::Debug { + /// The secret stored under `account`, if any. + /// + /// # Errors + /// [`DeployError::Keychain`] when the store fails. + fn get(&self, account: &str) -> Result>; + + /// Stores `secret` under `account`, replacing an older one. + /// + /// # Errors + /// [`DeployError::Keychain`] when the store fails. + fn set(&self, account: &str, secret: &Secret) -> Result<()>; + + /// Removes the secret under `account`; a missing one is fine. + /// + /// # Errors + /// [`DeployError::Keychain`] when the store fails. + fn delete(&self, account: &str) -> Result<()>; +} + +/// The account name of one server's secret. +pub fn account(project_id: &str, server_id: &str, kind: &str) -> String { + format!("{project_id}/{server_id}/{kind}") +} + +/// The OS keychain. +#[derive(Debug, Default)] +pub struct Keychain; + +#[cfg(any(windows, target_os = "macos"))] +impl Keychain { + /// The keychain entry of `account`. + fn entry(account: &str) -> Result { + keyring::Entry::new(SERVICE, account).map_err(|e| DeployError::Keychain(e.to_string())) + } +} + +#[cfg(any(windows, target_os = "macos"))] +impl SecretStore for Keychain { + fn get(&self, account: &str) -> Result> { + match Self::entry(account)?.get_password() { + Ok(value) => Secret::new(value).map(Some), + Err(keyring::Error::NoEntry) => Ok(None), + Err(e) => Err(DeployError::Keychain(e.to_string())), + } + } + + fn set(&self, account: &str, secret: &Secret) -> Result<()> { + Self::entry(account)? + .set_password(secret.expose()) + .map_err(|e| DeployError::Keychain(e.to_string())) + } + + fn delete(&self, account: &str) -> Result<()> { + match Self::entry(account)?.delete_credential() { + Ok(()) | Err(keyring::Error::NoEntry) => Ok(()), + Err(e) => Err(DeployError::Keychain(e.to_string())), + } + } +} + +#[cfg(not(any(windows, target_os = "macos")))] +impl SecretStore for Keychain { + fn get(&self, _account: &str) -> Result> { + Ok(None) + } + + fn set(&self, _account: &str, _secret: &Secret) -> Result<()> { + Err(DeployError::Keychain( + "Lutra stores deployment passwords only in the Windows Credential Manager or the macOS Keychain; on this system use an SSH agent or a key without a passphrase".into(), + )) + } + + fn delete(&self, _account: &str) -> Result<()> { + Ok(()) + } +} + +/// Secrets in memory only, for tests and hosts without a keychain. +#[derive(Debug, Default)] +pub struct MemoryStore { + /// Secrets by account. + entries: Mutex>, +} + +impl SecretStore for MemoryStore { + fn get(&self, account: &str) -> Result> { + Ok(self + .entries + .lock() + .unwrap_or_else(PoisonError::into_inner) + .get(account) + .cloned()) + } + + fn set(&self, account: &str, secret: &Secret) -> Result<()> { + self.entries + .lock() + .unwrap_or_else(PoisonError::into_inner) + .insert(account.to_string(), secret.clone()); + Ok(()) + } + + fn delete(&self, account: &str) -> Result<()> { + self.entries + .lock() + .unwrap_or_else(PoisonError::into_inner) + .remove(account); + Ok(()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn debug_hides_the_value() { + let s = Secret::new("hunter2".into()).unwrap(); + assert_eq!(format!("{s:?}"), "Secret(***)"); + assert_eq!(s.expose(), "hunter2"); + assert!(Secret::new(String::new()).is_err()); + assert!(Secret::new("a\0b".into()).is_err()); + } + + #[test] + fn memory_store_round_trip() { + let store = MemoryStore::default(); + let acc = account("p", "s", "password"); + assert_eq!(acc, "p/s/password"); + assert!(store.get(&acc).unwrap().is_none()); + store.set(&acc, &Secret::new("x".into()).unwrap()).unwrap(); + assert_eq!(store.get(&acc).unwrap().unwrap().expose(), "x"); + store.delete(&acc).unwrap(); + store.delete(&acc).unwrap(); + assert!(store.get(&acc).unwrap().is_none()); + } +} diff --git a/crates/deploy/src/sftp.rs b/crates/deploy/src/sftp.rs new file mode 100644 index 0000000..965b3cf --- /dev/null +++ b/crates/deploy/src/sftp.rs @@ -0,0 +1,458 @@ +//! SFTP over SSH with `russh` and `russh-sftp`, pure Rust with `ring`. +//! +//! The library is async; [`Sftp`] owns a single-threaded tokio runtime and +//! blocks on it, so callers stay synchronous like the other transports. +//! Every network wait races the job's [`Cancel`] flag (checked every 50 +//! ms) and a timeout. +//! +//! The server's host key is checked against Lutra's known-hosts file +//! before any credential is sent. Sign-in uses a password from the +//! keychain, a private key file (with its passphrase from the keychain), or +//! the SSH agent. + +use std::future::Future; +use std::io::{Read, Write}; +use std::path::{Path, PathBuf}; +use std::sync::{Arc, Mutex, PoisonError}; +use std::time::Duration; + +use russh::client::{self, Handle}; +use russh::keys::{HashAlg, PrivateKeyWithHashAlg, PublicKeyOrCertificate}; +use russh_sftp::client::SftpSession; +use russh_sftp::protocol::StatusCode; +use tokio::io::{AsyncReadExt, AsyncWriteExt}; +use tokio::runtime::Runtime; + +use crate::config::{AuthKind, Server}; +use crate::error::{DeployError, Result}; +use crate::known_hosts::{self, HostKeyStatus}; +use crate::remote::{CHUNK, Cancel, Entry, MAX_LISTING, Remote, Tick, safe_name}; +use crate::secrets::Secret; + +/// Longest wait for the TCP connection and the SSH handshake. +const CONNECT_TIMEOUT: Duration = Duration::from_secs(20); + +/// Longest wait for one SFTP request or chunk. +const REQUEST_TIMEOUT: Duration = Duration::from_secs(60); + +/// The SSH client's callbacks: checks the host key. +struct Client { + /// `host:port`. + host: String, + /// Lutra's known-hosts file. + known_hosts: PathBuf, + /// Why the key was refused, read after the handshake failed. + verdict: Arc>>, +} + +impl Client { + /// Remembers why the key was refused. + fn refuse(&self, error: DeployError) -> bool { + *self.verdict.lock().unwrap_or_else(PoisonError::into_inner) = Some(error); + false + } +} + +impl client::Handler for Client { + type Error = russh::Error; + + async fn check_server_key( + &mut self, + offered: &PublicKeyOrCertificate, + ) -> std::result::Result { + let PublicKeyOrCertificate::PublicKey { key, .. } = offered else { + return Ok(self.refuse(DeployError::Auth(format!( + "{} offered an SSH certificate as its host key; Lutra accepts plain host keys only", + self.host + )))); + }; + let fingerprint = key.fingerprint(HashAlg::Sha256).to_string(); + let algorithm = key.algorithm().to_string(); + Ok( + match known_hosts::check(&self.known_hosts, &self.host, &fingerprint) { + Ok(HostKeyStatus::Trusted) => true, + Ok(HostKeyStatus::Unknown) => self.refuse(DeployError::HostKeyUnknown { + host: self.host.clone(), + algorithm, + fingerprint, + }), + Ok(HostKeyStatus::Mismatch(known)) => self.refuse(DeployError::HostKeyMismatch { + host: self.host.clone(), + algorithm, + fingerprint, + known, + }), + Err(e) => self.refuse(e), + }, + ) + } +} + +/// An open SFTP session. +pub struct Sftp { + /// Drives the async library. + runtime: Runtime, + /// The SSH connection. + handle: Handle, + /// The SFTP channel. + sftp: SftpSession, + /// The job's cancel flag. + cancel: Cancel, +} + +/// Runs `fut` until it ends, `limit` passes or `cancel` is set. +async fn bounded>( + cancel: &Cancel, + limit: Duration, + what: &str, + fut: F, +) -> Result { + let watch = async { + loop { + if cancel.is_cancelled() { + return; + } + tokio::time::sleep(Duration::from_millis(50)).await; + } + }; + tokio::select! { + out = tokio::time::timeout(limit, fut) => out.map_err(|_| { + DeployError::Transfer(format!("{what} timed out after {} s", limit.as_secs())) + }), + () = watch => Err(DeployError::Cancelled), + } +} + +/// An SFTP library error as a transfer error. +fn sftp_error(what: &str, path: &str, e: &russh_sftp::client::error::Error) -> DeployError { + DeployError::Transfer(format!("{what} {path}: {e}")) +} + +/// Whether `e` says the path does not exist. +fn is_missing(e: &russh_sftp::client::error::Error) -> bool { + matches!(e, russh_sftp::client::error::Error::Status(s) if s.status_code == StatusCode::NoSuchFile) +} + +impl Sftp { + /// Connects to `server`, checks its host key against `known_hosts`, + /// signs in and opens the SFTP subsystem. + /// + /// # Errors + /// [`DeployError::HostKeyUnknown`] or [`DeployError::HostKeyMismatch`] + /// before any credential is sent; [`DeployError::MissingSecret`] when a + /// password is needed but `secret` is `None`; [`DeployError::Auth`] + /// when the server refuses; [`DeployError::Connect`] when it cannot be + /// reached; [`DeployError::Cancelled`]. + pub fn connect( + server: &Server, + secret: Option<&Secret>, + known_hosts: &Path, + cancel: &Cancel, + ) -> Result { + let runtime = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .map_err(|e| DeployError::Transfer(format!("cannot start the SSH runtime: {e}")))?; + let host = server.address(); + let verdict = Arc::new(Mutex::new(None)); + let handler = Client { + host: host.clone(), + known_hosts: known_hosts.to_path_buf(), + verdict: Arc::clone(&verdict), + }; + let config = Arc::new(client::Config { + inactivity_timeout: Some(Duration::from_secs(120)), + ..Default::default() + }); + let target = (server.host.clone(), server.effective_port()); + let connected = runtime.block_on(bounded( + cancel, + CONNECT_TIMEOUT, + "connecting", + client::connect(config, target, handler), + ))?; + let mut handle = match connected { + Ok(handle) => handle, + Err(e) => { + if let Some(refusal) = verdict + .lock() + .unwrap_or_else(PoisonError::into_inner) + .take() + { + return Err(refusal); + } + return Err(DeployError::Connect { + host, + message: e.to_string(), + }); + } + }; + let signed_in = runtime.block_on(bounded( + cancel, + CONNECT_TIMEOUT, + "signing in", + authenticate(&mut handle, server, secret), + ))??; + if !signed_in { + let how = match server.auth { + AuthKind::Password => "the user name or password", + AuthKind::Key => "the user name or the key", + AuthKind::Agent => "the user name or the keys in the SSH agent", + }; + return Err(DeployError::Auth(format!( + "{host} refused the sign-in; check {how}" + ))); + } + let sftp = runtime + .block_on(bounded(cancel, CONNECT_TIMEOUT, "opening SFTP", async { + let channel = handle.channel_open_session().await?; + channel.request_subsystem(true, "sftp").await?; + Ok::<_, russh::Error>(channel.into_stream()) + }))? + .map_err(|e| DeployError::Connect { + host: host.clone(), + message: format!("the server offers no SFTP: {e}"), + })?; + let sftp = runtime + .block_on(bounded( + cancel, + CONNECT_TIMEOUT, + "opening SFTP", + SftpSession::new(sftp), + ))? + .map_err(|e| DeployError::Connect { + host, + message: format!("the SFTP session failed: {e}"), + })?; + Ok(Self { + runtime, + handle, + sftp, + cancel: cancel.clone(), + }) + } + + /// Runs one request with the request timeout and the cancel flag. + fn run>(&self, what: &str, fut: F) -> Result { + self.runtime + .block_on(bounded(&self.cancel, REQUEST_TIMEOUT, what, fut)) + } +} + +/// Signs in on `handle` the way `server` says. Returns whether the server +/// accepted. +async fn authenticate( + handle: &mut Handle, + server: &Server, + secret: Option<&Secret>, +) -> Result { + let user = server.user.clone(); + let auth_error = |e: russh::Error| DeployError::Auth(format!("signing in failed: {e}")); + match server.auth { + AuthKind::Password => { + let Some(secret) = secret else { + return Err(DeployError::MissingSecret { + server: server.name.clone(), + kind: "password".into(), + }); + }; + let result = handle + .authenticate_password(user, secret.expose()) + .await + .map_err(auth_error)?; + Ok(result.success()) + } + AuthKind::Key => { + let path = server.key_path.clone().unwrap_or_default(); + let key = match russh::keys::load_secret_key(&path, secret.map(Secret::expose)) { + Ok(key) => key, + Err(russh::keys::Error::KeyIsEncrypted) if secret.is_none() => { + return Err(DeployError::MissingSecret { + server: server.name.clone(), + kind: "passphrase".into(), + }); + } + Err(e) => { + return Err(DeployError::Auth(format!( + "cannot read the private key {path}: {e}" + ))); + } + }; + let hash = handle + .best_supported_rsa_hash() + .await + .map_err(auth_error)? + .flatten(); + let result = handle + .authenticate_publickey(user, PrivateKeyWithHashAlg::new(Arc::new(key), hash)) + .await + .map_err(auth_error)?; + Ok(result.success()) + } + AuthKind::Agent => agent(handle, &user).await, + } +} + +/// Tries every key of the SSH agent. +#[cfg(windows)] +async fn agent(handle: &mut Handle, user: &str) -> Result { + use russh::keys::agent::client::AgentClient; + if let Ok(client) = AgentClient::connect_named_pipe(r"\\.\pipe\openssh-ssh-agent").await { + return agent_keys(handle, user, client).await; + } + match AgentClient::connect_pageant().await { + Ok(client) => agent_keys(handle, user, client).await, + Err(_) => Err(DeployError::Auth( + "no SSH agent is running: start the OpenSSH Authentication Agent service or Pageant and add your key".into(), + )), + } +} + +/// Tries every key of the SSH agent. +#[cfg(unix)] +async fn agent(handle: &mut Handle, user: &str) -> Result { + use russh::keys::agent::client::AgentClient; + match AgentClient::connect_env().await { + Ok(client) => agent_keys(handle, user, client).await, + Err(_) => Err(DeployError::Auth( + "no SSH agent is reachable through SSH_AUTH_SOCK: start one and add your key (ssh-add)" + .into(), + )), + } +} + +/// Tries each key the agent holds until the server accepts one. +async fn agent_keys( + handle: &mut Handle, + user: &str, + mut client: russh::keys::agent::client::AgentClient, +) -> Result +where + S: tokio::io::AsyncRead + tokio::io::AsyncWrite + Unpin + Send + 'static, +{ + let identities = client + .request_identities() + .await + .map_err(|e| DeployError::Auth(format!("the SSH agent did not answer: {e}")))?; + let hash = handle + .best_supported_rsa_hash() + .await + .ok() + .flatten() + .flatten(); + for identity in identities { + let russh::keys::agent::AgentIdentity::PublicKey { key, .. } = identity else { + continue; + }; + if let Ok(result) = handle + .authenticate_publickey_with(user, key, hash, &mut client) + .await + && result.success() + { + return Ok(true); + } + } + Ok(false) +} + +impl Remote for Sftp { + fn mkdir_all(&mut self, dir: &str) -> Result<()> { + let absolute = dir.starts_with('/'); + let mut path = String::new(); + for part in dir.split('/').filter(|p| !p.is_empty()) { + if absolute || !path.is_empty() { + path.push('/'); + } + path.push_str(part); + let exists = self + .run("checking a folder", self.sftp.try_exists(path.clone()))? + .map_err(|e| sftp_error("cannot check", &path, &e))?; + if !exists { + self.run("creating a folder", self.sftp.create_dir(path.clone()))? + .map_err(|e| sftp_error("cannot create the folder", &path, &e))?; + } + } + Ok(()) + } + + fn put(&mut self, path: &str, data: &mut dyn Read, tick: Tick<'_>) -> Result<()> { + let mut file = self + .run("opening a file", self.sftp.create(path.to_string()))? + .map_err(|e| sftp_error("cannot write", path, &e))?; + let mut buf = vec![0u8; CHUNK]; + loop { + let n = data + .read(&mut buf) + .map_err(|e| DeployError::Transfer(format!("read failed: {e}")))?; + if n == 0 { + break; + } + let chunk = buf.get(..n).unwrap_or_default(); + self.run("uploading", file.write_all(chunk))? + .map_err(|e| DeployError::Transfer(format!("cannot write {path}: {e}")))?; + tick(n as u64)?; + } + self.run("closing a file", file.shutdown())? + .map_err(|e| DeployError::Transfer(format!("cannot finish {path}: {e}"))) + } + + fn get(&mut self, path: &str, out: &mut dyn Write, tick: Tick<'_>) -> Result<()> { + let mut file = self + .run("opening a file", self.sftp.open(path.to_string()))? + .map_err(|e| sftp_error("cannot read", path, &e))?; + let mut buf = vec![0u8; CHUNK]; + loop { + let n = self + .run("downloading", file.read(&mut buf))? + .map_err(|e| DeployError::Transfer(format!("cannot read {path}: {e}")))?; + if n == 0 { + break; + } + out.write_all(buf.get(..n).unwrap_or_default()) + .map_err(|e| DeployError::Transfer(format!("write failed: {e}")))?; + tick(n as u64)?; + } + Ok(()) + } + + fn kind(&mut self, path: &str) -> Result> { + let target = if path.is_empty() { "." } else { path }; + match self.run("checking a path", self.sftp.metadata(target.to_string()))? { + Ok(meta) => Ok(Some(meta.is_dir())), + Err(e) if is_missing(&e) => Ok(None), + Err(e) => Err(sftp_error("cannot check", path, &e)), + } + } + + fn list(&mut self, path: &str) -> Result> { + let target = if path.is_empty() { "." } else { path }; + let dir = self + .run("listing a folder", self.sftp.read_dir(target.to_string()))? + .map_err(|e| sftp_error("cannot list", path, &e))?; + let mut entries: Vec = dir + .filter(|e| safe_name(&e.file_name())) + .take(MAX_LISTING) + .map(|e| { + let meta = e.metadata(); + Entry { + name: e.file_name(), + is_dir: meta.is_dir(), + size: meta.size, + } + }) + .collect(); + entries.sort_by(|a, b| a.name.cmp(&b.name)); + Ok(entries) + } + + /// Closes the SFTP channel and the connection. Failures are ignored: + /// the connection is dropped right after. + fn close(&mut self) { + let _ = self.run("closing", self.sftp.close()); + let _ = self.run( + "disconnecting", + self.handle + .disconnect(russh::Disconnect::ByApplication, "", "en"), + ); + } +} diff --git a/crates/deploy/src/testserver.rs b/crates/deploy/src/testserver.rs new file mode 100644 index 0000000..6e748f7 --- /dev/null +++ b/crates/deploy/src/testserver.rs @@ -0,0 +1,664 @@ +//! Local servers for tests and live checks (feature `testserver`): an SFTP +//! server built on `russh` and a small FTP server, both bound to +//! `127.0.0.1` on a free port and serving one folder. They never listen on +//! another interface and are not part of the shipped app. +//! +//! The SFTP server has a fixed Ed25519 host key (from a seed) and accepts +//! one user with one password, or one public key. The FTP server speaks +//! enough of RFC 959 for Lutra's client: USER, PASS, SYST, FEAT, TYPE, +//! PWD, CWD, MKD, SIZE, PASV, STOR, RETR, LIST, NOOP, QUIT. Both stop when +//! dropped. + +use std::collections::HashMap; +use std::io::{BufRead, BufReader, Read, Seek, SeekFrom, Write}; +use std::net::{SocketAddr, TcpListener, TcpStream}; +use std::path::{Path, PathBuf}; +use std::sync::Arc; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::time::Duration; + +use russh::keys::ssh_key::private::Ed25519Keypair; +use russh::keys::{HashAlg, PrivateKey, PublicKey}; +use russh::server::{Auth, Msg, Session}; +use russh::{Channel, ChannelId}; +use russh_sftp::protocol::{ + Attrs, Data, File as SftpFile, FileAttributes, Handle, Name, OpenFlags, Status, StatusCode, + Version, +}; +use tokio::sync::Notify; + +use crate::mapping::normalize_relative; + +/// The path under `root` of the server path `path`, relative to `cwd`; +/// `None` when it would leave the root. +fn resolve(root: &Path, cwd: &str, path: &str) -> Option { + let joined = if path.starts_with('/') { + path.to_string() + } else { + format!("{cwd}/{path}") + }; + let rel = normalize_relative(&joined)?; + Some(if rel.is_empty() { + root.to_path_buf() + } else { + root.join(rel) + }) +} + +/// SFTP attributes of a local file, with the POSIX type bits. +fn attrs(meta: &std::fs::Metadata) -> FileAttributes { + FileAttributes { + size: Some(meta.len()), + permissions: Some(if meta.is_dir() { 0o040_755 } else { 0o100_644 }), + mtime: Some(0), + atime: Some(0), + ..FileAttributes::default() + } +} + +/// A running SFTP server. +pub struct SftpServer { + /// Its address. + pub addr: SocketAddr, + /// Its host key's fingerprint, `SHA256:…`. + pub fingerprint: String, + /// Its host key's algorithm. + pub algorithm: String, + /// Stops it. + stop: Arc, +} + +impl Drop for SftpServer { + fn drop(&mut self) { + self.stop.notify_waiters(); + self.stop.notify_one(); + } +} + +/// Who may sign in to the SFTP server. +#[derive(Clone)] +struct Credentials { + /// The user name. + user: String, + /// The accepted password. + password: Option, + /// The accepted public key. + key: Option, +} + +/// A fixed host key for the test server, from `seed`. +pub fn host_key(seed: u8) -> PrivateKey { + PrivateKey::from(Ed25519Keypair::from_seed(&[seed; 32])) +} + +impl SftpServer { + /// Starts a server for `root` that accepts `user` with `password` or + /// with `key`. `seed` picks the host key, so a test can simulate a + /// changed one. + /// + /// # Errors + /// An I/O error when no port can be bound or the runtime fails. + pub fn start( + root: &Path, + user: &str, + password: Option<&str>, + key: Option, + seed: u8, + ) -> std::io::Result { + let host = host_key(seed); + let fingerprint = host.public_key().fingerprint(HashAlg::Sha256).to_string(); + let algorithm = host.public_key().algorithm().to_string(); + let config = Arc::new(russh::server::Config { + keys: vec![host], + auth_rejection_time: Duration::from_millis(10), + auth_rejection_time_initial: Some(Duration::from_millis(0)), + inactivity_timeout: Some(Duration::from_secs(60)), + ..Default::default() + }); + let listener = TcpListener::bind("127.0.0.1:0")?; + listener.set_nonblocking(true)?; + let addr = listener.local_addr()?; + let stop = Arc::new(Notify::new()); + let stopped = Arc::clone(&stop); + let creds = Credentials { + user: user.to_string(), + password: password.map(str::to_string), + key, + }; + let root = root.to_path_buf(); + let runtime = tokio::runtime::Builder::new_multi_thread() + .worker_threads(2) + .enable_all() + .build()?; + std::thread::Builder::new() + .name("lutra-test-sftp".into()) + .spawn(move || { + runtime.block_on(async move { + let Ok(listener) = tokio::net::TcpListener::from_std(listener) else { + return; + }; + loop { + let accepted = tokio::select! { + a = listener.accept() => a, + () = stopped.notified() => return, + }; + let Ok((socket, _)) = accepted else { + continue; + }; + let handler = SshHandler { + creds: creds.clone(), + root: root.clone(), + channels: HashMap::new(), + }; + let config = Arc::clone(&config); + tokio::spawn(async move { + if let Ok(session) = + russh::server::run_stream(config, socket, handler).await + { + let _ = session.await; + } + }); + } + }); + })?; + Ok(Self { + addr, + fingerprint, + algorithm, + stop, + }) + } +} + +/// One SSH connection. +struct SshHandler { + /// Who may sign in. + creds: Credentials, + /// The served folder. + root: PathBuf, + /// Open session channels, until they ask for SFTP. + channels: HashMap>, +} + +impl russh::server::Handler for SshHandler { + type Error = russh::Error; + + async fn auth_password(&mut self, user: &str, password: &str) -> Result { + Ok( + if user == self.creds.user && self.creds.password.as_deref() == Some(password) { + Auth::Accept + } else { + Auth::reject() + }, + ) + } + + async fn auth_publickey_offered( + &mut self, + user: &str, + key: &PublicKey, + ) -> Result { + self.auth_publickey(user, key).await + } + + async fn auth_publickey(&mut self, user: &str, key: &PublicKey) -> Result { + Ok( + if user == self.creds.user + && self + .creds + .key + .as_ref() + .is_some_and(|k| k.key_data() == key.key_data()) + { + Auth::Accept + } else { + Auth::reject() + }, + ) + } + + async fn channel_open_session( + &mut self, + channel: Channel, + reply: russh::server::ChannelOpenHandle, + _session: &mut Session, + ) -> Result<(), Self::Error> { + self.channels.insert(channel.id(), channel); + reply.accept().await; + Ok(()) + } + + async fn subsystem_request( + &mut self, + id: ChannelId, + name: &str, + session: &mut Session, + ) -> Result<(), Self::Error> { + match (name, self.channels.remove(&id)) { + ("sftp", Some(channel)) => { + session.channel_success(id)?; + let handler = SftpHandler { + root: self.root.clone(), + handles: HashMap::new(), + next: 0, + }; + tokio::spawn(russh_sftp::server::run(channel.into_stream(), handler)); + } + _ => session.channel_failure(id)?, + } + Ok(()) + } +} + +/// What an SFTP handle refers to. +enum Open { + /// An open file. + File(std::fs::File), + /// A folder listing; `None` once sent. + Dir(Option>), +} + +/// The SFTP subsystem over a local folder. +struct SftpHandler { + /// The served folder. + root: PathBuf, + /// Open handles. + handles: HashMap, + /// The next handle number. + next: u64, +} + +impl SftpHandler { + /// The local path of `path`. + fn path(&self, path: &str) -> Result { + resolve(&self.root, "/", path).ok_or(StatusCode::PermissionDenied) + } + + /// Stores `open` under a fresh handle. + fn add(&mut self, id: u32, open: Open) -> Handle { + self.next += 1; + let handle = format!("h{}", self.next); + self.handles.insert(handle.clone(), open); + Handle { id, handle } + } +} + +/// An `Ok` status. +fn ok(id: u32) -> Status { + Status { + id, + status_code: StatusCode::Ok, + error_message: "Ok".into(), + language_tag: "en-US".into(), + } +} + +/// A local I/O error as an SFTP status. +fn io_status(e: &std::io::Error) -> StatusCode { + match e.kind() { + std::io::ErrorKind::NotFound => StatusCode::NoSuchFile, + std::io::ErrorKind::PermissionDenied => StatusCode::PermissionDenied, + _ => StatusCode::Failure, + } +} + +impl russh_sftp::server::Handler for SftpHandler { + type Error = StatusCode; + + fn unimplemented(&self) -> Self::Error { + StatusCode::OpUnsupported + } + + async fn init( + &mut self, + _version: u32, + _extensions: HashMap, + ) -> Result { + Ok(Version::new()) + } + + async fn open( + &mut self, + id: u32, + filename: String, + flags: OpenFlags, + _attrs: FileAttributes, + ) -> Result { + let path = self.path(&filename)?; + let file = std::fs::OpenOptions::new() + .read(flags.contains(OpenFlags::READ)) + .write(flags.contains(OpenFlags::WRITE)) + .create(flags.contains(OpenFlags::CREATE)) + .truncate(flags.contains(OpenFlags::TRUNCATE)) + .open(path) + .map_err(|e| io_status(&e))?; + Ok(self.add(id, Open::File(file))) + } + + async fn close(&mut self, id: u32, handle: String) -> Result { + self.handles.remove(&handle); + Ok(ok(id)) + } + + async fn read( + &mut self, + id: u32, + handle: String, + offset: u64, + len: u32, + ) -> Result { + let Some(Open::File(file)) = self.handles.get_mut(&handle) else { + return Err(StatusCode::Failure); + }; + file.seek(SeekFrom::Start(offset)) + .map_err(|e| io_status(&e))?; + let mut data = vec![0u8; len.min(256 * 1024) as usize]; + let n = file.read(&mut data).map_err(|e| io_status(&e))?; + if n == 0 { + return Err(StatusCode::Eof); + } + data.truncate(n); + Ok(Data { id, data }) + } + + async fn write( + &mut self, + id: u32, + handle: String, + offset: u64, + data: Vec, + ) -> Result { + let Some(Open::File(file)) = self.handles.get_mut(&handle) else { + return Err(StatusCode::Failure); + }; + file.seek(SeekFrom::Start(offset)) + .map_err(|e| io_status(&e))?; + file.write_all(&data).map_err(|e| io_status(&e))?; + Ok(ok(id)) + } + + async fn stat(&mut self, id: u32, path: String) -> Result { + let meta = std::fs::metadata(self.path(&path)?).map_err(|e| io_status(&e))?; + Ok(Attrs { + id, + attrs: attrs(&meta), + }) + } + + async fn lstat(&mut self, id: u32, path: String) -> Result { + self.stat(id, path).await + } + + async fn fstat(&mut self, id: u32, handle: String) -> Result { + let Some(Open::File(file)) = self.handles.get(&handle) else { + return Err(StatusCode::Failure); + }; + let meta = file.metadata().map_err(|e| io_status(&e))?; + Ok(Attrs { + id, + attrs: attrs(&meta), + }) + } + + async fn setstat( + &mut self, + id: u32, + _path: String, + _attrs: FileAttributes, + ) -> Result { + Ok(ok(id)) + } + + async fn fsetstat( + &mut self, + id: u32, + _handle: String, + _attrs: FileAttributes, + ) -> Result { + Ok(ok(id)) + } + + async fn mkdir( + &mut self, + id: u32, + path: String, + _attrs: FileAttributes, + ) -> Result { + std::fs::create_dir(self.path(&path)?).map_err(|e| io_status(&e))?; + Ok(ok(id)) + } + + async fn opendir(&mut self, id: u32, path: String) -> Result { + let dir = self.path(&path)?; + let mut files = Vec::new(); + for entry in std::fs::read_dir(dir).map_err(|e| io_status(&e))?.flatten() { + if let Ok(meta) = entry.metadata() { + files.push(SftpFile::new( + entry.file_name().to_string_lossy(), + attrs(&meta), + )); + } + } + Ok(self.add(id, Open::Dir(Some(files)))) + } + + async fn readdir(&mut self, id: u32, handle: String) -> Result { + let Some(Open::Dir(files)) = self.handles.get_mut(&handle) else { + return Err(StatusCode::Failure); + }; + match files.take() { + Some(files) if !files.is_empty() => Ok(Name { id, files }), + _ => Err(StatusCode::Eof), + } + } + + async fn realpath(&mut self, id: u32, path: String) -> Result { + let rel = normalize_relative(&path).ok_or(StatusCode::NoSuchFile)?; + Ok(Name { + id, + files: vec![SftpFile::dummy(format!("/{rel}"))], + }) + } + + async fn remove(&mut self, id: u32, filename: String) -> Result { + std::fs::remove_file(self.path(&filename)?).map_err(|e| io_status(&e))?; + Ok(ok(id)) + } + + async fn rmdir(&mut self, id: u32, path: String) -> Result { + std::fs::remove_dir(self.path(&path)?).map_err(|e| io_status(&e))?; + Ok(ok(id)) + } +} + +/// A running FTP server. +pub struct FtpServer { + /// Its address. + pub addr: SocketAddr, + /// Stops it. + stop: Arc, +} + +impl Drop for FtpServer { + fn drop(&mut self) { + self.stop.store(true, Ordering::SeqCst); + } +} + +impl FtpServer { + /// Starts a server for `root` that accepts `user` with `password`. + /// + /// # Errors + /// An I/O error when no port can be bound. + pub fn start(root: &Path, user: &str, password: &str) -> std::io::Result { + let listener = TcpListener::bind("127.0.0.1:0")?; + listener.set_nonblocking(true)?; + let addr = listener.local_addr()?; + let stop = Arc::new(AtomicBool::new(false)); + let stopped = Arc::clone(&stop); + let root = root.to_path_buf(); + let (user, password) = (user.to_string(), password.to_string()); + std::thread::Builder::new() + .name("lutra-test-ftp".into()) + .spawn(move || { + while !stopped.load(Ordering::SeqCst) { + match listener.accept() { + Ok((stream, _)) => { + let (root, user, password) = + (root.clone(), user.clone(), password.clone()); + let _ = std::thread::Builder::new() + .name("lutra-test-ftp-conn".into()) + .spawn(move || { + let _ = ftp_session(stream, &root, &user, &password); + }); + } + Err(_) => std::thread::sleep(Duration::from_millis(20)), + } + } + })?; + Ok(Self { addr, stop }) + } +} + +/// Serves one FTP control connection until QUIT or a closed socket. +fn ftp_session(stream: TcpStream, root: &Path, user: &str, password: &str) -> std::io::Result<()> { + stream.set_nonblocking(false)?; + stream.set_read_timeout(Some(Duration::from_secs(60)))?; + let mut out = stream.try_clone()?; + let mut reader = BufReader::new(stream); + let mut send = |line: &str| out.write_all(format!("{line}\r\n").as_bytes()); + send("220 Lutra test FTP")?; + let mut given_user = String::new(); + let mut signed_in = false; + let mut cwd = String::from("/"); + let mut passive: Option = None; + loop { + let mut line = String::new(); + if reader.read_line(&mut line)? == 0 { + return Ok(()); + } + let line = line.trim_end_matches(['\r', '\n']); + let (cmd, arg) = line.split_once(' ').unwrap_or((line, "")); + let cmd = cmd.to_ascii_uppercase(); + if !signed_in + && !matches!( + cmd.as_str(), + "USER" | "PASS" | "QUIT" | "FEAT" | "SYST" | "AUTH" + ) + { + send("530 Not logged in")?; + continue; + } + let here = cwd.clone(); + let path = |arg: &str| resolve(root, &here, arg); + match cmd.as_str() { + "USER" => { + given_user = arg.to_string(); + send("331 Password required")?; + } + "PASS" => { + signed_in = given_user == user && arg == password; + send(if signed_in { + "230 Logged in" + } else { + "530 Login incorrect" + })?; + } + "SYST" => send("215 UNIX Type: L8")?, + "FEAT" | "AUTH" => send("502 Not implemented")?, + "TYPE" | "NOOP" | "OPTS" => send("200 OK")?, + "PWD" => send(&format!("257 \"{cwd}\" is the current directory"))?, + "CWD" => match (path(arg), virtual_path(&here, arg)) { + (Some(p), Some(v)) if p.is_dir() => { + cwd = v; + send("250 OK")?; + } + _ => send("550 No such directory")?, + }, + "MKD" => match path(arg).map(std::fs::create_dir) { + Some(Ok(())) => send(&format!("257 \"{arg}\" created"))?, + _ => send("550 Cannot create")?, + }, + "SIZE" => match path(arg).and_then(|p| std::fs::metadata(p).ok()) { + Some(m) if m.is_file() => send(&format!("213 {}", m.len()))?, + _ => send("550 No such file")?, + }, + "PASV" => { + let l = TcpListener::bind("127.0.0.1:0")?; + let port = l.local_addr()?.port(); + passive = Some(l); + send(&format!( + "227 Entering Passive Mode (127,0,0,1,{},{})", + port >> 8, + port & 0xff + ))?; + } + "STOR" | "RETR" | "LIST" => { + let Some(listener) = passive.take() else { + send("425 Use PASV first")?; + continue; + }; + let target = if cmd == "LIST" && arg.is_empty() { + path(".") + } else { + path(arg) + }; + let Some(target) = target else { + send("550 Bad path")?; + continue; + }; + if cmd == "RETR" && !target.is_file() { + send("550 No such file")?; + continue; + } + send("150 Opening data connection")?; + let (mut data, _) = listener.accept()?; + let result = match cmd.as_str() { + "STOR" => std::fs::File::create(&target) + .and_then(|mut f| std::io::copy(&mut data, &mut f).map(|_| ())), + "RETR" => std::fs::File::open(&target) + .and_then(|mut f| std::io::copy(&mut f, &mut data).map(|_| ())), + _ => list_dir(&target).and_then(|text| data.write_all(text.as_bytes())), + }; + drop(data); + send(if result.is_ok() { + "226 Transfer complete" + } else { + "451 Transfer failed" + })?; + } + "QUIT" => { + send("221 Bye")?; + return Ok(()); + } + _ => send("502 Not implemented")?, + } + } +} + +/// The FTP path of `arg`, relative to `cwd`, as an absolute path. +fn virtual_path(cwd: &str, arg: &str) -> Option { + let joined = if arg.starts_with('/') { + arg.to_string() + } else { + format!("{cwd}/{arg}") + }; + normalize_relative(&joined).map(|r| format!("/{r}")) +} + +/// A POSIX `ls -l` style listing of `dir`. +fn list_dir(dir: &Path) -> std::io::Result { + let mut text = String::new(); + for entry in std::fs::read_dir(dir)?.flatten() { + let Ok(meta) = entry.metadata() else { + continue; + }; + let kind = if meta.is_dir() { 'd' } else { '-' }; + text.push_str(&format!( + "{kind}rw-r--r-- 1 lutra lutra {} Jan 01 00:00 {}\r\n", + if meta.is_dir() { 0 } else { meta.len() }, + entry.file_name().to_string_lossy() + )); + } + Ok(text) +} diff --git a/crates/deploy/tests/transfer.rs b/crates/deploy/tests/transfer.rs new file mode 100644 index 0000000..737f0df --- /dev/null +++ b/crates/deploy/tests/transfer.rs @@ -0,0 +1,354 @@ +//! Uploads, downloads, host keys, sign-in failures and cancel against the +//! local SFTP and FTP test servers. No real network: both listen on +//! 127.0.0.1 only. + +#![allow( + clippy::unwrap_used, + clippy::expect_used, + clippy::panic, + clippy::indexing_slicing +)] + +use std::path::Path; +use std::time::Duration; + +use lutra_deploy::known_hosts::{self, HostKey}; +use lutra_deploy::testserver::{FtpServer, SftpServer, host_key}; +use lutra_deploy::{ + AuthKind, Cancel, DeployError, Job, Mapping, Monitor, Secret, Server, ServerKind, job, +}; + +const PASSWORD: &str = "Otter-Test-Pa55word!"; + +fn server(kind: ServerKind, port: u16) -> Server { + Server { + id: "test".into(), + name: "Test server".into(), + kind, + host: "127.0.0.1".into(), + port: Some(port), + user: "lutra".into(), + root: "/www".into(), + auth: AuthKind::Password, + key_path: None, + mappings: vec![Mapping { + local: String::new(), + remote: "site".into(), + }], + excluded: vec!["*.log".into()], + upload_on_save: false, + } +} + +fn project() -> tempfile::TempDir { + let dir = tempfile::tempdir().unwrap(); + std::fs::create_dir_all(dir.path().join("src/sub")).unwrap(); + std::fs::write(dir.path().join("index.php"), ") -> Job { + Job { + server, + project_root: project.to_path_buf(), + secret: secret.map(|s| Secret::new(s.into()).unwrap()), + known_hosts: known.to_path_buf(), + cancel: Cancel::new(), + monitor: Monitor::new(), + } +} + +/// Collects everything a job left for the user to read. +fn transcript(job: &Job, errors: &[String]) -> String { + let mut text: Vec = job + .monitor + .take_lines() + .into_iter() + .map(|l| l.message) + .collect(); + text.extend(errors.iter().cloned()); + text.join("\n") +} + +#[test] +fn sftp_host_key_then_upload_and_download() { + let remote = tempfile::tempdir().unwrap(); + std::fs::create_dir(remote.path().join("www")).unwrap(); + let sftp = SftpServer::start(remote.path(), "lutra", Some(PASSWORD), None, 7).unwrap(); + let project = project(); + let state = tempfile::tempdir().unwrap(); + let known = state.path().join("known_hosts"); + let j = job( + server(ServerKind::Sftp, sftp.addr.port()), + project.path(), + &known, + Some(PASSWORD), + ); + + let err = job::test_connection(&j).unwrap_err(); + let DeployError::HostKeyUnknown { + host, + algorithm, + fingerprint, + } = &err + else { + panic!("{err:?}"); + }; + assert_eq!(fingerprint, &sftp.fingerprint); + assert_eq!(algorithm, &sftp.algorithm); + assert!(err.to_string().contains("Test Connection")); + known_hosts::trust( + &known, + &HostKey { + host: host.clone(), + algorithm: algorithm.clone(), + fingerprint: fingerprint.clone(), + }, + ) + .unwrap(); + + let msg = job::test_connection(&j).unwrap(); + assert!(msg.contains("/www exists"), "{msg}"); + + let summary = job::upload(&j, &[String::new()]).unwrap(); + assert_eq!( + (summary.files, summary.failed, summary.skipped), + (3, 0, 1), + "{summary:?}" + ); + let up = remote.path().join("www/site"); + assert_eq!( + std::fs::read_to_string(up.join("src/sub/b.php")).unwrap(), + " 0 { + cancel.cancel(); + return true; + } + std::thread::sleep(Duration::from_millis(5)); + } + false + }); + let started = std::time::Instant::now(); + let result = job::upload(&j, &["big.bin".into()]); + assert!(watcher.join().unwrap()); + assert_eq!(result, Err(DeployError::Cancelled)); + assert!(started.elapsed() < Duration::from_secs(10)); +} + +#[test] +fn ftp_upload_download_and_wrong_password() { + let remote = tempfile::tempdir().unwrap(); + let ftp = FtpServer::start(remote.path(), "lutra", PASSWORD).unwrap(); + let project = project(); + let state = tempfile::tempdir().unwrap(); + let known = state.path().join("known_hosts"); + let j = job( + server(ServerKind::Ftp, ftp.addr.port()), + project.path(), + &known, + Some(PASSWORD), + ); + + let msg = job::test_connection(&j).unwrap(); + assert!(msg.contains("does not exist yet"), "{msg}"); + let summary = job::upload(&j, &[String::new()]).unwrap(); + assert_eq!((summary.files, summary.failed), (3, 0), "{summary:?}"); + let up = remote.path().join("www/site"); + assert_eq!( + std::fs::read_to_string(up.join("src/sub/b.php")).unwrap(), + "