From: Ian Jackson Date: Sat, 25 Jul 2020 22:47:18 +0000 (+0100) Subject: wip auth X-Git-Tag: otter-0.2.0~1262 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=829724c55bd4257a6ae53f627d4b4f3c454f5365;p=otter.git wip auth --- diff --git a/src/cmdlistener.rs b/src/cmdlistener.rs index 404ff021..86e269f0 100644 --- a/src/cmdlistener.rs +++ b/src/cmdlistener.rs @@ -27,6 +27,7 @@ struct CommandStream { euid : Result, read : io::Lines>, write : CSWrite, + scope : Option, } type CSE = anyhow::Error; @@ -68,12 +69,70 @@ fn decode_process_inner(s: &str)-> MgmtResponse { execute(cmd)? } +const USERLIST : &str = "/etc/userlist"; + +fn authorize_scope(cs: &CommandStream, wanted: &ManagementScope) { + type AS = AuthorizedScope; + + match &wanted { + ManagementScope::XXX => { + let y : AS<( + Authorized<(Passwd,uid_t)>, + )> = { + let our_euid = unsafe { libc::getuid() }; + let ok = cs.authorized_uid(our_euid)?; + AS((ok,), + ManagementScope:::XXX) + }; + y.into() + }, + Unix(user) => { + let y : AS<( + Authorized<(Passwd,uid_t)>, // caller_has + Authorized, // in_userlist: + )> = { + let pwent = Passwd::from_name(user)?: + let caller_has = cs.authorized_uid(pwent.uid)?; + let found = (||{ + let allowed = File::open(USERLIST)?; + let found = allowed.lines()?.map(|l| l.trim() == user).any(); + Ok(found) + })?; + let in_userlist = Authorized::from_bool(USERLIST)?; + AS((caller_has, in_userlist), + ManagementScope::Unix(pwent.username)) + }; + y.into() + } + }; +} + #[throws(ME)] -fn execute(cmd: MgmtCommand) -> MgmtResponse { +fn execute(cs: &mut CommandStream, cmd: MgmtCommand) -> MgmtResponse { + use MgmgError::*; + match cmd { Noop { } => Fine { }, - AddPiece(_) => Fine { - }, // xxx + + Scope(wanted_scope) => { + let (_: AuthorizedConclusion, authorized: ManagementScope) = + authorize_scope(cs, &wanted_scope)?; + cs.scope = authorized; + Fine { } + } +/* + CreateGame(game) => { + + }, + AddPiece(game, { pos,count,name,info }) => { + let game = cs.lookup_game(&game)?; + let count = spec.count.unwrap_or(1); + let pos = spec.ok_or(XXU("missing piece pos"))?; + let _xxx_name = spec.name; + let pc = info.load()?; + + } + }, // xxx*/ } } diff --git a/src/commands.rs b/src/commands.rs index 10cc788b..73c8102a 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -16,6 +16,8 @@ pub enum MgmtResponse { #[derive(Debug,Error)] pub enum MgmtError { ParseFailed(String), + SetScope(ManagementScope), + XXXU(&'static str), } display_as_debug!{MgmtError} diff --git a/src/global.rs b/src/global.rs index 9521b945..d4512f00 100644 --- a/src/global.rs +++ b/src/global.rs @@ -26,7 +26,7 @@ pub struct Instance { pub tokens_clients : TokenRegistry, } -#[derive(Debug,Serialize)] +#[derive(Debug,Deserialize,Serialize)] pub enum ManagementScope { XXX, Unix { user : String /* username, so filename-safe */ },