From 937cf1522b3e1643a348365edd20c0c64b890bbf Mon Sep 17 00:00:00 2001 From: Carter Canedy Date: Sun, 13 Oct 2024 14:29:59 -0700 Subject: [PATCH] implement root check --- tui/src/main.rs | 79 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 71 insertions(+), 8 deletions(-) diff --git a/tui/src/main.rs b/tui/src/main.rs index 2c309ee8b..8320cc6d3 100644 --- a/tui/src/main.rs +++ b/tui/src/main.rs @@ -15,12 +15,18 @@ use std::{ use crate::theme::Theme; use clap::Parser; use crossterm::{ - event::{self, DisableMouseCapture, Event, KeyEventKind}, + event::{self, DisableMouseCapture, Event, KeyCode, KeyEvent, KeyEventKind}, style::ResetColor, terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, ExecutableCommand, }; -use ratatui::{backend::CrosstermBackend, Terminal}; +use ratatui::{ + backend::CrosstermBackend, + layout::{Alignment, Constraint, Layout}, + style::Stylize, + widgets::{Paragraph, Wrap}, + Terminal, +}; use state::AppState; // Linux utility toolbox @@ -36,12 +42,6 @@ struct Args { } fn main() -> io::Result<()> { - if sudo::check() != sudo::RunningAs::User { - eprintln!("Error: This program is not intended to be run with elevated privileges."); - eprintln!("Please run this program as a regular user to ensure proper security and functionality."); - std::process::exit(1); - } - let args = Args::parse(); let mut state = AppState::new(args.theme, args.override_validation); @@ -67,6 +67,69 @@ fn run( terminal: &mut Terminal>, state: &mut AppState, ) -> io::Result<()> { + if sudo::check() == sudo::RunningAs::Root { + terminal.draw(|frame| { + let root_warn = Paragraph::new( + r#" +!!!!!!!!!!!!!! YOU ARE ABOUT TO RUN LINUTIL AS ROOT !!!!!!!!!!!!!! + +This utility prioritizes compatibility with non-root environments. +Some scripts may work without any issues, some may not. +You have been warned! + +!!!!!!!!!!!!!!!!!!!!!! PROCEED WITH CAUTION !!!!!!!!!!!!!!!!!!!!!! + +Press [y] to continue, [n] to abort +"#, + ) + .on_black() + .white() + .alignment(Alignment::Center) + .wrap(Wrap { trim: true }); + + let rects = Layout::vertical([ + Constraint::Fill(1), + Constraint::Length(10), + Constraint::Fill(1), + ]) + .split(frame.area()); + + let centered = rects[1]; + + frame.render_widget(root_warn, centered); + })?; + + loop { + match event::read()? { + Event::Key( + KeyEvent { + code: KeyCode::Char('y'), + .. + } + | KeyEvent { + code: KeyCode::Char('Y'), + .. + }, + ) => { + break; + } + Event::Key( + KeyEvent { + code: KeyCode::Char('n'), + .. + } + | KeyEvent { + code: KeyCode::Char('N'), + .. + }, + ) => { + return Ok(()); + } + _ => {} + } + } + } + loop { terminal.draw(|frame| state.draw(frame)).unwrap(); // Wait for an event