|
1 | 1 | use clap::Parser; |
2 | | -use std::{net, path}; |
| 2 | +use std::{net, os::unix::ffi::OsStrExt, path}; |
3 | 3 |
|
4 | 4 | #[derive(clap::Args)] |
5 | 5 | #[group(required = true, multiple = false)] |
@@ -54,10 +54,12 @@ struct Args { |
54 | 54 | help = "Exits after receiving max_files files" |
55 | 55 | )] |
56 | 56 | max_files: usize, |
57 | | - #[clap(long, value_name = "overwrite", help = "Overwrite existing files")] |
| 57 | + #[clap(long, help = "Overwrite existing files")] |
58 | 58 | overwrite: bool, |
59 | 59 | #[clap(flatten)] |
60 | 60 | tls: lidi_clients::Tls, |
| 61 | + #[clap(long, help = "Chroot in output directory before receiving files")] |
| 62 | + chroot: bool, |
61 | 63 | #[clap(default_value = ".", help = "Output directory")] |
62 | 64 | output_directory: path::PathBuf, |
63 | 65 | } |
@@ -94,7 +96,41 @@ fn main() { |
94 | 96 | tls: args.tls, |
95 | 97 | }; |
96 | 98 |
|
97 | | - if let Err(e) = lidi_clients::file::receive::receive_files(&config, &args.output_directory) { |
| 99 | + let output_directory = if args.chroot { |
| 100 | + let mut bytes_output_directory = Vec::from(args.output_directory.as_os_str().as_bytes()); |
| 101 | + bytes_output_directory.push(0); |
| 102 | + |
| 103 | + let c_output_directory = match std::ffi::CString::from_vec_with_nul(bytes_output_directory) |
| 104 | + { |
| 105 | + Ok(res) => res, |
| 106 | + Err(e) => { |
| 107 | + log::error!( |
| 108 | + "failed to convert output directory to C string {}: {e}", |
| 109 | + args.output_directory.display() |
| 110 | + ); |
| 111 | + std::process::exit(1); |
| 112 | + } |
| 113 | + }; |
| 114 | + |
| 115 | + if unsafe { libc::chroot(c_output_directory.as_ptr()) } != 0 { |
| 116 | + let err_str = |
| 117 | + unsafe { std::ffi::CStr::from_ptr(libc::strerror(*libc::__errno_location())) } |
| 118 | + .to_string_lossy(); |
| 119 | + log::error!( |
| 120 | + "failed to chroot in {}: {err_str}", |
| 121 | + args.output_directory.display() |
| 122 | + ); |
| 123 | + std::process::exit(1); |
| 124 | + } |
| 125 | + |
| 126 | + log::info!("chrooted in {}", args.output_directory.display()); |
| 127 | + |
| 128 | + path::PathBuf::from("/") |
| 129 | + } else { |
| 130 | + args.output_directory |
| 131 | + }; |
| 132 | + |
| 133 | + if let Err(e) = lidi_clients::file::receive::receive_files(&config, &output_directory) { |
98 | 134 | log::error!("{e}"); |
99 | 135 | } |
100 | 136 | } |
0 commit comments