server private key conf and remove debug prints
This commit is contained in:
parent
1efe2ad5c2
commit
b384154d73
4 changed files with 10 additions and 23 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1,3 +1,5 @@
|
||||||
/target
|
/target
|
||||||
sftptest
|
sftptest
|
||||||
sftptest.pub
|
sftptest.pub
|
||||||
|
server
|
||||||
|
server.pub
|
||||||
|
|
|
@ -10,7 +10,8 @@ pub(crate) struct Config {
|
||||||
pub(crate) struct GeneralConfig {
|
pub(crate) struct GeneralConfig {
|
||||||
pub(crate) listen_address: String,
|
pub(crate) listen_address: String,
|
||||||
pub(crate) port: u16,
|
pub(crate) port: u16,
|
||||||
pub(crate) jail_dir: String
|
pub(crate) jail_dir: String,
|
||||||
|
pub(crate) private_key_file: String
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone)]
|
#[derive(Serialize, Deserialize, Clone)]
|
||||||
|
@ -61,7 +62,8 @@ impl Default for Config {
|
||||||
general: GeneralConfig {
|
general: GeneralConfig {
|
||||||
listen_address: String::from("0.0.0.0"),
|
listen_address: String::from("0.0.0.0"),
|
||||||
port: 2222,
|
port: 2222,
|
||||||
jail_dir: String::from("/srv/sftp")
|
jail_dir: String::from("/srv/sftp"),
|
||||||
|
private_key_file: String::from("~/.ssh/flux-sftp")
|
||||||
},
|
},
|
||||||
database: DBConfig {
|
database: DBConfig {
|
||||||
driver: DriverConfig::Sqlite {
|
driver: DriverConfig::Sqlite {
|
||||||
|
|
11
src/main.rs
11
src/main.rs
|
@ -1,10 +1,10 @@
|
||||||
mod sftp;
|
mod sftp;
|
||||||
mod config;
|
mod config;
|
||||||
|
|
||||||
use std::{fmt::format, io::ErrorKind, net::SocketAddr, sync::Arc, time::Duration};
|
use std::{io::ErrorKind, net::SocketAddr, path::Path, sync::Arc, time::Duration};
|
||||||
use bcrypt::{hash, DEFAULT_COST};
|
use bcrypt::{hash, DEFAULT_COST};
|
||||||
use config::{Config, DriverConfig};
|
use config::{Config, DriverConfig};
|
||||||
use russh::{keys::ssh_key::{rand_core::OsRng, PublicKey}, server::{Auth, Handler as SshHandler, Msg, Server, Session}, Channel, ChannelId};
|
use russh::{keys::ssh_key::PublicKey, server::{Auth, Handler as SshHandler, Msg, Server, Session}, Channel, ChannelId};
|
||||||
use sftp::SftpSession;
|
use sftp::SftpSession;
|
||||||
use sqlx::{mysql::MySqlPoolOptions, postgres::PgPoolOptions, sqlite::SqlitePoolOptions, MySql, Pool, Postgres, Row, Sqlite};
|
use sqlx::{mysql::MySqlPoolOptions, postgres::PgPoolOptions, sqlite::SqlitePoolOptions, MySql, Pool, Postgres, Row, Sqlite};
|
||||||
use tokio::fs;
|
use tokio::fs;
|
||||||
|
@ -151,9 +151,6 @@ enum DBPool {
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), sqlx::Error> {
|
async fn main() -> Result<(), sqlx::Error> {
|
||||||
// let config = Config::default();
|
|
||||||
// let toml = toml::to_string(&config).unwrap();
|
|
||||||
// println!("{}", toml);
|
|
||||||
|
|
||||||
const CONFIG_PATH: &str = "/etc/flux-sftp/config.toml";
|
const CONFIG_PATH: &str = "/etc/flux-sftp/config.toml";
|
||||||
let config: Arc<Config>;
|
let config: Arc<Config>;
|
||||||
|
@ -195,11 +192,11 @@ async fn main() -> Result<(), sqlx::Error> {
|
||||||
auth_rejection_time: Duration::from_secs(3),
|
auth_rejection_time: Duration::from_secs(3),
|
||||||
auth_rejection_time_initial: Some(Duration::from_secs(0)),
|
auth_rejection_time_initial: Some(Duration::from_secs(0)),
|
||||||
keys: vec![
|
keys: vec![
|
||||||
russh::keys::PrivateKey::random(&mut OsRng, russh::keys::Algorithm::Ed25519).unwrap(),
|
russh::keys::PrivateKey::read_openssh_file(Path::new(&config.general.private_key_file)).unwrap()
|
||||||
],
|
],
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
server.run_on_address(Arc::new(russh_config), (&config.general.listen_address as &str, config.general.port)).await.unwrap();
|
server.run_on_address(Arc::new(russh_config), (&config.general.listen_address as &str, config.general.port)).await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
14
src/sftp.rs
14
src/sftp.rs
|
@ -53,7 +53,6 @@ impl SftpHandler for SftpSession {
|
||||||
id: u32,
|
id: u32,
|
||||||
path: String,
|
path: String,
|
||||||
) -> Result<Name, Self::Error> {
|
) -> Result<Name, Self::Error> {
|
||||||
println!("realpath called, path: {}", path);
|
|
||||||
|
|
||||||
let re_1 = Regex::new(r"/[^/]+/\.\.").unwrap();
|
let re_1 = Regex::new(r"/[^/]+/\.\.").unwrap();
|
||||||
self.cwd = re_1.replace_all(&path, "").to_string();
|
self.cwd = re_1.replace_all(&path, "").to_string();
|
||||||
|
@ -78,9 +77,6 @@ impl SftpHandler for SftpSession {
|
||||||
pflags: OpenFlags,
|
pflags: OpenFlags,
|
||||||
_attrs: FileAttributes,
|
_attrs: FileAttributes,
|
||||||
) -> Result<SftpHandle, Self::Error> {
|
) -> Result<SftpHandle, Self::Error> {
|
||||||
println!("open called, path: {}", filename);
|
|
||||||
println!("pflags raw: {:b}", pflags.bits());
|
|
||||||
println!("pflags: read: {}, write: {}, append: {}, create: {}, truncate: {}", pflags.contains(OpenFlags::READ), pflags.contains(OpenFlags::WRITE), pflags.contains(OpenFlags::APPEND), pflags.contains(OpenFlags::CREATE), pflags.contains(OpenFlags::TRUNCATE));
|
|
||||||
let path = format!("{}{}", self.jail_dir, filename);
|
let path = format!("{}{}", self.jail_dir, filename);
|
||||||
if pflags.contains(OpenFlags::EXCLUDE) && fs::metadata(&path).await.is_ok() {
|
if pflags.contains(OpenFlags::EXCLUDE) && fs::metadata(&path).await.is_ok() {
|
||||||
return Err(StatusCode::Failure)
|
return Err(StatusCode::Failure)
|
||||||
|
@ -156,7 +152,6 @@ impl SftpHandler for SftpSession {
|
||||||
offset: u64,
|
offset: u64,
|
||||||
data: Vec<u8>,
|
data: Vec<u8>,
|
||||||
) -> Result<Status, Self::Error> {
|
) -> Result<Status, Self::Error> {
|
||||||
println!("write called, offset: {}, data: {:?}", offset, String::from_utf8(data.clone()));
|
|
||||||
if let Handle::File(file) = self.handles.get_mut(&handle).unwrap() {
|
if let Handle::File(file) = self.handles.get_mut(&handle).unwrap() {
|
||||||
match file.seek(SeekFrom::Start(offset)).await {
|
match file.seek(SeekFrom::Start(offset)).await {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
|
@ -192,7 +187,6 @@ impl SftpHandler for SftpSession {
|
||||||
id: u32,
|
id: u32,
|
||||||
path: String,
|
path: String,
|
||||||
) -> Result<SftpHandle, Self::Error> {
|
) -> Result<SftpHandle, Self::Error> {
|
||||||
println!("opendir called: {}", path);
|
|
||||||
let path = format!("{}{}", self.jail_dir, path);
|
let path = format!("{}{}", self.jail_dir, path);
|
||||||
match fs::read_dir(&path).await {
|
match fs::read_dir(&path).await {
|
||||||
Ok(entries) => {
|
Ok(entries) => {
|
||||||
|
@ -250,7 +244,6 @@ impl SftpHandler for SftpSession {
|
||||||
id: u32,
|
id: u32,
|
||||||
handle: String,
|
handle: String,
|
||||||
) -> Result<Status, Self::Error> {
|
) -> Result<Status, Self::Error> {
|
||||||
println!("close called");
|
|
||||||
self.handles.remove(&handle);
|
self.handles.remove(&handle);
|
||||||
Ok(Status {
|
Ok(Status {
|
||||||
id,
|
id,
|
||||||
|
@ -265,7 +258,6 @@ impl SftpHandler for SftpSession {
|
||||||
id: u32,
|
id: u32,
|
||||||
path: String,
|
path: String,
|
||||||
) -> Result<Attrs, Self::Error> {
|
) -> Result<Attrs, Self::Error> {
|
||||||
println!("stat called: {}", path);
|
|
||||||
let path = format!("{}{}", self.jail_dir, path);
|
let path = format!("{}{}", self.jail_dir, path);
|
||||||
match fs::metadata(path).await {
|
match fs::metadata(path).await {
|
||||||
Ok(metadata) => Ok(Attrs { id, attrs: FileAttributes {
|
Ok(metadata) => Ok(Attrs { id, attrs: FileAttributes {
|
||||||
|
@ -284,7 +276,6 @@ impl SftpHandler for SftpSession {
|
||||||
id: u32,
|
id: u32,
|
||||||
path: String,
|
path: String,
|
||||||
) -> Result<Attrs, Self::Error> {
|
) -> Result<Attrs, Self::Error> {
|
||||||
println!("lstat called: {}", path);
|
|
||||||
let path = format!("{}{}", self.jail_dir, path);
|
let path = format!("{}{}", self.jail_dir, path);
|
||||||
match fs::symlink_metadata(path).await {
|
match fs::symlink_metadata(path).await {
|
||||||
Ok(metadata) => Ok(Attrs { id, attrs: FileAttributes {
|
Ok(metadata) => Ok(Attrs { id, attrs: FileAttributes {
|
||||||
|
@ -304,7 +295,6 @@ impl SftpHandler for SftpSession {
|
||||||
id: u32,
|
id: u32,
|
||||||
handle: String,
|
handle: String,
|
||||||
) -> Result<Attrs, Self::Error> {
|
) -> Result<Attrs, Self::Error> {
|
||||||
println!("fstat called: {}", handle);
|
|
||||||
if let Handle::File(file) = self.handles.get(&handle).unwrap() {
|
if let Handle::File(file) = self.handles.get(&handle).unwrap() {
|
||||||
let metadata = file.metadata().await.unwrap();
|
let metadata = file.metadata().await.unwrap();
|
||||||
Ok(Attrs { id, attrs: FileAttributes {
|
Ok(Attrs { id, attrs: FileAttributes {
|
||||||
|
@ -327,7 +317,6 @@ impl SftpHandler for SftpSession {
|
||||||
id: u32,
|
id: u32,
|
||||||
filename: String,
|
filename: String,
|
||||||
) -> Result<Status, Self::Error> {
|
) -> Result<Status, Self::Error> {
|
||||||
println!("remove called: {}", filename);
|
|
||||||
let path = format!("{}{}", self.jail_dir, filename);
|
let path = format!("{}{}", self.jail_dir, filename);
|
||||||
match_expr!(fs::remove_file(path).await, "error removing file: {}", id)
|
match_expr!(fs::remove_file(path).await, "error removing file: {}", id)
|
||||||
}
|
}
|
||||||
|
@ -338,7 +327,6 @@ impl SftpHandler for SftpSession {
|
||||||
path: String,
|
path: String,
|
||||||
_attrs: FileAttributes,
|
_attrs: FileAttributes,
|
||||||
) -> Result<Status, Self::Error> {
|
) -> Result<Status, Self::Error> {
|
||||||
println!("mkdir called: {}", path);
|
|
||||||
let path = format!("{}{}", self.jail_dir, path);
|
let path = format!("{}{}", self.jail_dir, path);
|
||||||
match_expr!(fs::create_dir(path).await, "error creating dir: {}", id)
|
match_expr!(fs::create_dir(path).await, "error creating dir: {}", id)
|
||||||
}
|
}
|
||||||
|
@ -348,7 +336,6 @@ impl SftpHandler for SftpSession {
|
||||||
id: u32,
|
id: u32,
|
||||||
path: String,
|
path: String,
|
||||||
) -> Result<Status, Self::Error> {
|
) -> Result<Status, Self::Error> {
|
||||||
println!("rmdir called: {}", path);
|
|
||||||
let path = format!("{}{}", self.jail_dir, path);
|
let path = format!("{}{}", self.jail_dir, path);
|
||||||
match_expr!(fs::remove_dir(path).await, "error removing file: {}", id)
|
match_expr!(fs::remove_dir(path).await, "error removing file: {}", id)
|
||||||
}
|
}
|
||||||
|
@ -359,7 +346,6 @@ impl SftpHandler for SftpSession {
|
||||||
oldpath: String,
|
oldpath: String,
|
||||||
newpath: String,
|
newpath: String,
|
||||||
) -> Result<Status, Self::Error> {
|
) -> Result<Status, Self::Error> {
|
||||||
println!("rename called from: {}, to: {}", oldpath, newpath);
|
|
||||||
let oldpath = format!("{}{}", self.jail_dir, oldpath);
|
let oldpath = format!("{}{}", self.jail_dir, oldpath);
|
||||||
let newpath = format!("{}{}", self.jail_dir, newpath);
|
let newpath = format!("{}{}", self.jail_dir, newpath);
|
||||||
match_expr!(fs::rename(oldpath, newpath).await, "error renaming file: {}", id)
|
match_expr!(fs::rename(oldpath, newpath).await, "error renaming file: {}", id)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue