added perms

master
Avril 4 years ago
parent 837f72f19c
commit 3837d23be7
Signed by: flanchan
GPG Key ID: 284488987C31F630

1
.gitignore vendored

@ -2,3 +2,4 @@
target/ target/
rel.drawio.png rel.drawio.png
src/config/build_cfg.rs src/config/build_cfg.rs
*.log

13
Cargo.lock generated

@ -388,7 +388,6 @@ dependencies = [
name = "lolicron" name = "lolicron"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"bitflags",
"cfg-if", "cfg-if",
"chrono", "chrono",
"config_struct", "config_struct",
@ -399,6 +398,7 @@ dependencies = [
"mopa", "mopa",
"notify", "notify",
"once_cell", "once_cell",
"readable-perms",
"recolored", "recolored",
"rustc_version", "rustc_version",
"sexp", "sexp",
@ -603,6 +603,17 @@ dependencies = [
"proc-macro2", "proc-macro2",
] ]
[[package]]
name = "readable-perms"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "027e9ff27619e387809844e9f87d2798b288cf6257c45d67e2b9996d1ca4a41f"
dependencies = [
"bitflags",
"libc",
"rustc_version",
]
[[package]] [[package]]
name = "recolored" name = "recolored"
version = "1.9.3" version = "1.9.3"

@ -1,3 +1,4 @@
Log filtering rules in config file. For `Trace`s, `Level`s, sublevels, etc. Log filtering rules in config file. For `Trace`s, `Level`s, sublevels, etc.
Log normalise source path in terminal print Log normalise source path in terminal print
Log change system. Use `Log` trait that we can define different printers for terminal, logfile, etc
Log have #[cfg(feature="threaded")] for deferring write to background worker

@ -53,6 +53,29 @@ pub struct LogFileHook
sender: Option<mpsc::Sender<Command>>, sender: Option<mpsc::Sender<Command>>,
} }
impl Internal
{
//funcs we might both want to use
fn valid_perms(&self) -> readable_perms::Permissions
{
//TODO: Add field for this
readable_perms::Permissions::new()
.add_mask(readable_perms::User::Owner, readable_perms::Bit::Read | readable_perms::Bit::Write)
.add_mask(readable_perms::User::Group, readable_perms::Bit::Read)
.add_mask(readable_perms::User::Owner, readable_perms::Bit::None)
}
/// Fix permissions on this file to what we want.
fn fix_perms(&self, file: &mut tokio::fs::File)
{
use readable_perms::*;
if let Err(e) = file.chmod(self.valid_perms()) {
crate::warn!("Error fixing logfile permissions: {}", e);
}
}
}
impl LogFileHook impl LogFileHook
{ {
/// Create a new log file hook /// Create a new log file hook
@ -100,6 +123,7 @@ impl Hook for LogFileHook
let mut file = match OpenOptions::new() let mut file = match OpenOptions::new()
.append(true) .append(true)
.create(true) .create(true)
.write(true)
.open(path).await { .open(path).await {
Ok(file) => file, Ok(file) => file,
Err(err) => { Err(err) => {
@ -108,6 +132,8 @@ impl Hook for LogFileHook
}, },
}; };
internal.fix_perms(&mut file);
if let Err(err) = write_log(&mut file, command).await { if let Err(err) = write_log(&mut file, command).await {
crate::warn!("Failed writing to logfile {:?}: {}", path, err); crate::warn!("Failed writing to logfile {:?}: {}", path, err);
} }

@ -26,7 +26,7 @@ pub use level::*;
mod trace; mod trace;
pub use trace::*; pub use trace::*;
mod custom; pub mod custom;
/// A logger that prints to stdout and stderr idk. /// A logger that prints to stdout and stderr idk.
#[derive(Debug)] #[derive(Debug)]

@ -32,8 +32,6 @@ mod hot;
mod context; mod context;
mod job; mod job;
mod perms;
//This is a test function, when we have a real job server, remove it. //This is a test function, when we have a real job server, remove it.
async fn do_thing_every() -> Result<(mpsc::Sender<()>, task::JoinHandle<()>), Box<dyn std::error::Error>> async fn do_thing_every() -> Result<(mpsc::Sender<()>, task::JoinHandle<()>), Box<dyn std::error::Error>>
{ {
@ -125,7 +123,7 @@ fn print_stats()
async fn main() -> Result<(), Box<dyn std::error::Error>> { async fn main() -> Result<(), Box<dyn std::error::Error>> {
log::init(log::Level::Debug); log::init(log::Level::Debug);
//log::Logger::global().add_hook(log::custom::LogFileHook::new(log::Level::Debug, "test.log", "test.err.log", false)); log::Logger::global().add_hook(log::custom::LogFileHook::new(log::Level::Debug, "test.log", "test.err.log", false));
debug!("Logger initialised"); //TODO: Parse config first debug!("Logger initialised"); //TODO: Parse config first
print_stats(); print_stats();

@ -1,202 +0,0 @@
//! Permissions shit kms
//!
//! No I don't care about Microshaft wangblows
#![allow(non_upper_case_globals)]
use bitflags::bitflags;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
enum Class
{
Owner(Bit),
Group(Bit),
Other(Bit),
}
impl Class
{
/// Lmfao. Fuck bit masks and unix permissiong, `const fn` so idc
const fn mode(&self) -> u32
{
macro_rules! map {
($bit:expr, $bt:path, $constant:ident) => {
if $bit.contains($bt) {
$constant
} else {
0
}
}
}
match self {
Self::Owner(bit) => {
0u32 |
map!(bit, Bit::Read, MODE_USER_READ) |
map!(bit, Bit::Write, MODE_USER_WRITE) |
map!(bit, Bit::Execute, MODE_USER_EXECUTE)
},
Self::Group(bit) => {
0u32 |
map!(bit, Bit::Read, MODE_GROUP_READ) |
map!(bit, Bit::Write, MODE_GROUP_WRITE) |
map!(bit, Bit::Execute, MODE_GROUP_EXECUTE)
},
Self::Other(bit) => {
0u32 |
map!(bit, Bit::Read, MODE_OTHER_READ) |
map!(bit, Bit::Write, MODE_OTHER_WRITE) |
map!(bit, Bit::Execute, MODE_OTHER_EXECUTE)
},
}
}
const fn mask_mode(&self, bit: u32) -> Bit
{
macro_rules! map {
($bit:expr, $bt:path, $constant:ident) => {
if ($bit & $constant) == $constant {$bt.bits()} else {0u32}
}
}
Bit::from_bits_truncate(match self {
Self::Owner(_) => {
map!(bit, Bit::Read, MODE_USER_READ) |
map!(bit, Bit::Write, MODE_USER_WRITE) |
map!(bit, Bit::Execute, MODE_USER_EXECUTE)
},
Self::Group(_) => {
map!(bit, Bit::Read, MODE_GROUP_READ) |
map!(bit, Bit::Write, MODE_GROUP_WRITE) |
map!(bit, Bit::Execute, MODE_GROUP_EXECUTE)
},
Self::Other(_) => {
map!(bit, Bit::Read, MODE_OTHER_READ) |
map!(bit, Bit::Write, MODE_OTHER_WRITE) |
map!(bit, Bit::Execute, MODE_OTHER_EXECUTE)
},
})
}
}
const MODE_USER: u32 = 0o700;
const MODE_USER_READ: u32 = 0o400;
const MODE_USER_WRITE: u32 = 0o200;
const MODE_USER_EXECUTE: u32 = 0o100;
const MODE_GROUP: u32 = 0o70;
const MODE_GROUP_READ: u32 = 0o40;
const MODE_GROUP_WRITE: u32 = 0o20;
const MODE_GROUP_EXECUTE: u32 = 0o10;
const MODE_OTHER: u32 = 0o7;
const MODE_OTHER_READ: u32 = 0o4;
const MODE_OTHER_WRITE: u32 = 0o2;
const MODE_OTHER_EXECUTE: u32 = 0o1;
const MODE: u32 = 0o777;
bitflags!{
pub struct Bit: u32 {
const None = 0;
const Read = 1;
const Write = 2;
const Execute = 4;
const Mask = 7;
}
}
bitflags!{
struct StickyBit: u32{
const None = 0;
const Setuid = 0o40000;
const Setgid = 0o20000;
const SaveSwap = 0o10000;
}
}
/// Permissions struct in readable format
#[derive(Debug, Clone, PartialEq, Eq, Hash, Copy, Ord, PartialOrd)]
pub struct Permissions
{
pub u_owner: Bit,
pub u_group: Bit,
pub u_other: Bit,
_u_sticky: StickyBit, //idc about this either
}
impl Default for Permissions
{
#[inline] fn default() -> Self
{
Self::new()
}
}
impl Permissions
{
/// Create a new empty `Permissions` struct
pub const fn new() -> Self
{
Self {
u_owner: Bit::None,
u_group: Bit::None,
u_other: Bit::None,
_u_sticky: StickyBit::None,
}
}
}
// Mask impl
impl Permissions
{
#[inline] const fn c_owner(&self) -> Class
{
Class::Owner(self.u_owner)
}
#[inline] const fn c_group(&self) -> Class
{
Class::Group(self.u_group)
}
#[inline] const fn c_other(&self) -> Class
{
Class::Other(self.u_other)
}
/// Convert into `mode_t` repr.
#[inline] pub const fn mask(&self) -> u32
{
self.c_owner().mode() |
self.c_group().mode() |
self.c_other().mode()
}
/// Convert from `mode_t` repr.
#[inline] pub const fn from_mask(bit: u32) -> Self
{
Self{
u_owner: Class::Owner(Bit::Mask).mask_mode(bit),
u_group: Class::Group(Bit::Mask).mask_mode(bit),
u_other: Class::Other(Bit::Mask).mask_mode(bit),
_u_sticky: StickyBit::None,
}
}
}
impl From<Permissions> for u32
{
#[inline] fn from(from: Permissions) -> Self
{
from.mask()
}
}
impl From<u32> for Permissions
{
#[inline] fn from(from: u32) -> Self
{
Self::from_mask(from)
}
}

@ -1,21 +0,0 @@
2020-08-05 01:04:28 UTC <src/main.rs:128:5> [Debug]: Logger initialised2020-08-05 01:04:28 UTC <src/main.rs:88:5> [Debug]: This is the lolicron daemon version 0.1.0 by Avril <flanchan@cumallover.me>, Mx. Package Install <boku@plum.moe> (debug build)2020-08-05 01:04:28 UTC <src/main.rs:89:5> [Debug]: ---2020-08-05 01:04:28 UTC <src/main.rs:90:5> [Debug]: Compiled with (on (default), off (default)):2020-08-05 01:04:28 UTC <src/main.rs:92:21> [Debug]:  +nightly2020-08-05 01:04:28 UTC <src/main.rs:93:30> [Debug]:  +debug_assertions2020-08-05 01:04:28 UTC <src/main.rs:95:5> [Debug]: features:2020-08-05 01:04:28 UTC <src/main.rs:97:32> [Debug]:  +threaded2020-08-05 01:04:28 UTC <src/main.rs:100:31> [Debug]:  +watcher2020-08-05 01:04:28 UTC <src/main.rs:103:36> [Debug]:  +debug_logger2020-08-05 01:04:28 UTC <src/main.rs:107:46> [Debug]:  -watcher_unlimited2020-08-05 01:04:28 UTC <src/main.rs:109:39> [Debug]:  +watcher_timeout2020-08-05 01:04:28 UTC <src/main.rs:112:5> [Debug]: 2020-08-05 01:04:28 UTC <src/config/mod.rs:71:2> [Debug]: Static options:2020-08-05 01:04:28 UTC <src/config/mod.rs:72:2> [Debug]: hot::BACKLOG = 1002020-08-05 01:04:28 UTC <src/config/mod.rs:73:2> [Debug]: hot::TIMEOUT = 52020-08-05 01:04:28 UTC <src/main.rs:116:5> [Debug]: GPl'd with <32020-08-05 01:04:28 UTC <src/main.rs:117:5> [Debug]: Please enjoy2020-08-05 01:04:28 UTC <src/main.rs:119:5> [Debug]: ---2020-08-05 01:04:28 UTC <src/hot.rs:276:6> [Debug]: Watcher initialised, starting for path "/home/avril/work/lolicron"2020-08-05 01:04:28 UTC <src/hot.rs:305:6> [Info]: Watcher up2020-08-05 01:05:39 UTC <src/main.rs:128:5> [Debug]: Logger initialised
2020-08-05 01:05:39 UTC <src/main.rs:88:5> [Debug]: This is the lolicron daemon version 0.1.0 by Avril <flanchan@cumallover.me>, Mx. Package Install <boku@plum.moe> (debug build)
2020-08-05 01:05:39 UTC <src/main.rs:89:5> [Debug]: ---
2020-08-05 01:05:39 UTC <src/main.rs:90:5> [Debug]: Compiled with (on (default), off (default)):
2020-08-05 01:05:39 UTC <src/main.rs:92:21> [Debug]:  +nightly
2020-08-05 01:05:39 UTC <src/main.rs:93:30> [Debug]:  +debug_assertions
2020-08-05 01:05:39 UTC <src/main.rs:95:5> [Debug]: features:
2020-08-05 01:05:39 UTC <src/main.rs:97:32> [Debug]:  +threaded
2020-08-05 01:05:39 UTC <src/main.rs:100:31> [Debug]:  +watcher
2020-08-05 01:05:39 UTC <src/main.rs:103:36> [Debug]:  +debug_logger
2020-08-05 01:05:39 UTC <src/main.rs:107:46> [Debug]:  -watcher_unlimited
2020-08-05 01:05:39 UTC <src/main.rs:109:39> [Debug]:  +watcher_timeout
2020-08-05 01:05:39 UTC <src/main.rs:112:5> [Debug]:
2020-08-05 01:05:39 UTC <src/config/mod.rs:71:2> [Debug]: Static options:
2020-08-05 01:05:39 UTC <src/config/mod.rs:72:2> [Debug]: hot::BACKLOG = 100
2020-08-05 01:05:39 UTC <src/config/mod.rs:73:2> [Debug]: hot::TIMEOUT = 5
2020-08-05 01:05:39 UTC <src/main.rs:116:5> [Debug]: GPl'd with <3
2020-08-05 01:05:39 UTC <src/main.rs:117:5> [Debug]: Please enjoy
2020-08-05 01:05:39 UTC <src/main.rs:119:5> [Debug]: ---
2020-08-05 01:05:39 UTC <src/hot.rs:276:6> [Debug]: Watcher initialised, starting for path "/home/avril/work/lolicron"
2020-08-05 01:05:39 UTC <src/hot.rs:305:6> [Info]: Watcher up
Loading…
Cancel
Save