Refactor drop privileges method
This commit is contained in:
		
							parent
							
								
									7ed3a9e52d
								
							
						
					
					
						commit
						ec461a1a14
					
				
					 7 changed files with 71 additions and 62 deletions
				
			
		| 
						 | 
				
			
			@ -23,3 +23,4 @@ serde_json = "1.0"
 | 
			
		|||
serde_derive = "1.0"
 | 
			
		||||
 | 
			
		||||
futures = "0.3.29"
 | 
			
		||||
libc = "0.2"
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -2,14 +2,15 @@ use actix_cors::Cors;
 | 
			
		|||
use std::net::TcpListener;
 | 
			
		||||
use std::str::FromStr;
 | 
			
		||||
 | 
			
		||||
use crate::utils::drop_privileges;
 | 
			
		||||
use actix_web::middleware::TrailingSlash;
 | 
			
		||||
use actix_web::{middleware, web, App, HttpServer};
 | 
			
		||||
use emgauwa_lib::handlers;
 | 
			
		||||
use emgauwa_lib::utils::drop_privileges;
 | 
			
		||||
use log::{trace, LevelFilter};
 | 
			
		||||
use simple_logger::SimpleLogger;
 | 
			
		||||
 | 
			
		||||
mod settings;
 | 
			
		||||
mod utils;
 | 
			
		||||
 | 
			
		||||
#[actix_web::main]
 | 
			
		||||
async fn main() -> std::io::Result<()> {
 | 
			
		||||
| 
						 | 
				
			
			@ -27,23 +28,13 @@ async fn main() -> std::io::Result<()> {
 | 
			
		|||
	let listener = TcpListener::bind(format!("{}:{}", settings.host, settings.port))
 | 
			
		||||
		.expect("Error creating listener");
 | 
			
		||||
 | 
			
		||||
	if !settings.user.is_empty() && !settings.group.is_empty() {
 | 
			
		||||
		log::info!(
 | 
			
		||||
			"Dropping privileges to {}:{}",
 | 
			
		||||
			settings.user,
 | 
			
		||||
			settings.group
 | 
			
		||||
		);
 | 
			
		||||
		drop_privileges(&settings.user, &settings.group).expect("Error dropping privileges");
 | 
			
		||||
	}
 | 
			
		||||
	drop_privileges(&settings).expect("Error dropping privileges");
 | 
			
		||||
 | 
			
		||||
	let pool = emgauwa_lib::db::init(&settings.database).await;
 | 
			
		||||
 | 
			
		||||
	log::info!("Starting server on {}:{}", settings.host, settings.port);
 | 
			
		||||
	HttpServer::new(move || {
 | 
			
		||||
		let cors = Cors::default()
 | 
			
		||||
			.allow_any_method()
 | 
			
		||||
			.allow_any_header()
 | 
			
		||||
			.max_age(3600);
 | 
			
		||||
		let cors = Cors::default().allow_any_method().allow_any_header();
 | 
			
		||||
 | 
			
		||||
		let origins = settings.origins.clone();
 | 
			
		||||
		let cors = match settings.origins.is_empty() {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										64
									
								
								emgauwa-core/src/utils.rs
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										64
									
								
								emgauwa-core/src/utils.rs
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,64 @@
 | 
			
		|||
use crate::settings::Settings;
 | 
			
		||||
use std::ffi::CString;
 | 
			
		||||
use std::io::{Error, ErrorKind};
 | 
			
		||||
 | 
			
		||||
// https://blog.lxsang.me/post/id/28.0
 | 
			
		||||
pub fn drop_privileges(settings: &Settings) -> Result<(), Error> {
 | 
			
		||||
	log::info!(
 | 
			
		||||
		"Dropping privileges to {}:{}",
 | 
			
		||||
		settings.user,
 | 
			
		||||
		settings.group
 | 
			
		||||
	);
 | 
			
		||||
 | 
			
		||||
	// the group id need to be set first, because, when the user privileges drop,
 | 
			
		||||
	// we are unable to drop the group privileges
 | 
			
		||||
	if !settings.group.is_empty() {
 | 
			
		||||
		drop_privileges_group(&settings.group)?;
 | 
			
		||||
	}
 | 
			
		||||
	if !settings.user.is_empty() {
 | 
			
		||||
		drop_privileges_user(&settings.user)?;
 | 
			
		||||
	}
 | 
			
		||||
	Ok(())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn drop_privileges_group(group: &str) -> Result<(), Error> {
 | 
			
		||||
	// get the gid from username
 | 
			
		||||
	if let Ok(cstr) = CString::new(group.as_bytes()) {
 | 
			
		||||
		let p = unsafe { libc::getgrnam(cstr.as_ptr()) };
 | 
			
		||||
		if p.is_null() {
 | 
			
		||||
			eprintln!("Unable to getgrnam of group: {}", group);
 | 
			
		||||
			return Err(Error::last_os_error());
 | 
			
		||||
		}
 | 
			
		||||
		if unsafe { libc::setgid((*p).gr_gid) } != 0 {
 | 
			
		||||
			eprintln!("Unable to setgid of group: {}", group);
 | 
			
		||||
			return Err(Error::last_os_error());
 | 
			
		||||
		}
 | 
			
		||||
	} else {
 | 
			
		||||
		return Err(Error::new(
 | 
			
		||||
			ErrorKind::Other,
 | 
			
		||||
			"Cannot create CString from String (group)!",
 | 
			
		||||
		));
 | 
			
		||||
	}
 | 
			
		||||
	Ok(())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn drop_privileges_user(user: &str) -> Result<(), Error> {
 | 
			
		||||
	// get the uid from username
 | 
			
		||||
	if let Ok(cstr) = CString::new(user.as_bytes()) {
 | 
			
		||||
		let p = unsafe { libc::getpwnam(cstr.as_ptr()) };
 | 
			
		||||
		if p.is_null() {
 | 
			
		||||
			eprintln!("Unable to getpwnam of user: {}", user);
 | 
			
		||||
			return Err(Error::last_os_error());
 | 
			
		||||
		}
 | 
			
		||||
		if unsafe { libc::setuid((*p).pw_uid) } != 0 {
 | 
			
		||||
			eprintln!("Unable to setuid of user: {}", user);
 | 
			
		||||
			return Err(Error::last_os_error());
 | 
			
		||||
		}
 | 
			
		||||
	} else {
 | 
			
		||||
		return Err(Error::new(
 | 
			
		||||
			ErrorKind::Other,
 | 
			
		||||
			"Cannot create CString from String (user)!",
 | 
			
		||||
		));
 | 
			
		||||
	}
 | 
			
		||||
	Ok(())
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue