summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorReiner Herrmann <reiner@reiner-h.de>2019-02-26 20:15:37 +0100
committerReiner Herrmann <reiner@reiner-h.de>2019-02-26 20:15:37 +0100
commite7a5944aaee4f296fda47d1620e9aaade97f6111 (patch)
tree82daabd05701f8f1b0758d1e8fa32afcd6be4bed /src
parenta0c17a26109310a81ce255b50ecfa23bb7f6d973 (diff)
Change directory after dropping privileges, to detect permission problems early
Diffstat (limited to 'src')
-rw-r--r--src/tftpd.rs22
1 files changed, 12 insertions, 10 deletions
diff --git a/src/tftpd.rs b/src/tftpd.rs
index 2df7a5c..c54d9d7 100644
--- a/src/tftpd.rs
+++ b/src/tftpd.rs
@@ -24,6 +24,7 @@ struct Configuration {
gid: u32,
ro: bool,
wo: bool,
+ dir: PathBuf,
}
fn wait_for_ack(sock: &UdpSocket, expected_block: u16) -> Result<bool, io::Error> {
@@ -223,7 +224,6 @@ fn file_allowed(filename: &Path) -> Option<PathBuf> {
}
fn handle_wrq(socket: &UdpSocket, cl: &SocketAddr, buf: &[u8]) -> Result<(), io::Error> {
-
let (filename, mode) = parse_file_mode(buf)?;
match mode.as_ref() {
@@ -374,6 +374,7 @@ fn parse_commandline<'a>(args: &'a Vec<String>) -> Result<Configuration, &'a str
gid: 65534,
ro: false,
wo: false,
+ dir: env::current_dir().expect("Can't get current directory"),
};
let mut opts = Options::new();
opts.optflag("h", "help", "display usage information");
@@ -423,20 +424,13 @@ fn parse_commandline<'a>(args: &'a Vec<String>) -> Result<Configuration, &'a str
return Err("ro and wo");
}
if matches.opt_present("d") {
- let basedir = match matches.opt_str("d") {
- Some(d) => d,
+ conf.dir = match matches.opt_str("d") {
+ Some(d) => Path::new(&d).to_path_buf(),
None => {
usage(opts, None);
return Err("directory");
}
};
- match env::set_current_dir(Path::new(&basedir)) {
- Ok(_) => (),
- Err(err) => {
- usage(opts, Some(err.to_string()));
- return Err("changing directory");
- }
- }
}
return Ok(conf);
@@ -465,6 +459,14 @@ fn main() {
}
};
+ match env::set_current_dir(&conf.dir) {
+ Ok(_) => (),
+ Err(err) => {
+ println!("Changing directory to {} failed ({}).", &conf.dir.display(), err);
+ return;
+ }
+ }
+
loop {
let mut buf = [0; 2048];
let (n, src) = match socket.recv_from(&mut buf) {