Skip to content

Commit

Permalink
Draft: Perform CFG for musl platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
coastalwhite committed Jun 27, 2023
1 parent 73ad7dd commit 4d16767
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 29 deletions.
59 changes: 34 additions & 25 deletions src/auth/utmpx.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
use std::time::SystemTime;

use libc::{c_char, utmpx};
use log::{error, info};

pub struct UtmpxSession(utmpx);
pub struct UtmpxSession {
#[cfg(target_env = "gnu")]
session: libc::utmpx,
}

#[cfg(target_env = "gnu")]
pub fn add_utmpx_entry(username: &str, tty: u8, pid: u32) -> UtmpxSession {
info!("Adding UTMPX record");
log::info!("Adding UTMPX record");

// Check the MAN page for utmp for more information
// `man utmp`
//
// https://man7.org/linux/man-pages/man0/utmpx.h.0p.html
// https://github.com/fairyglade/ly/blob/master/src/login.c
let entry = {
let mut s: utmpx = unsafe { std::mem::zeroed() };
let mut s: libc::utmpx = unsafe { std::mem::zeroed() };

// ut_line --- Device name of tty - "/dev/"
// ut_id --- Terminal name suffix
Expand All @@ -32,36 +31,38 @@ pub fn add_utmpx_entry(username: &str, tty: u8, pid: u32) -> UtmpxSession {
s.ut_pid = pid as libc::pid_t;

for (i, b) in username.as_bytes().iter().take(32).enumerate() {
s.ut_user[i] = *b as c_char;
s.ut_user[i] = *b as libc::c_char;
}

if tty > 12 {
error!("Invalid TTY");
log::error!("Invalid TTY");
std::process::exit(1);
}
let tty_c_char = (b'0' + tty) as c_char;
let tty_c_char = (b'0' + tty) as libc::c_char;

s.ut_line[0] = b't' as c_char;
s.ut_line[1] = b't' as c_char;
s.ut_line[2] = b'y' as c_char;
s.ut_line[0] = b't' as libc::c_char;
s.ut_line[1] = b't' as libc::c_char;
s.ut_line[2] = b'y' as libc::c_char;
s.ut_line[3] = tty_c_char;

s.ut_id[0] = tty_c_char;

use std::time::SystemTime;

let epoch_duration = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap_or_else(|_| {
error!("Invalid System Time");
log::error!("Invalid System Time");
std::process::exit(1);
})
.as_micros();

s.ut_tv.tv_sec = (epoch_duration / 1_000_000).try_into().unwrap_or_else(|_| {
error!("Invalid System Time (TV_SEC Overflow)");
log::error!("Invalid System Time (TV_SEC Overflow)");
std::process::exit(1);
});
s.ut_tv.tv_usec = (epoch_duration % 1_000_000).try_into().unwrap_or_else(|_| {
error!("Invalid System Time (TV_USEC Overflow)");
log::error!("Invalid System Time (TV_USEC Overflow)");
std::process::exit(1);
});

Expand All @@ -70,31 +71,39 @@ pub fn add_utmpx_entry(username: &str, tty: u8, pid: u32) -> UtmpxSession {

unsafe {
libc::setutxent();
libc::pututxline(&entry as *const utmpx);
libc::pututxline(&entry as *const libc::utmpx);
};

info!("Added UTMPX record");
log::info!("Added UTMPX record");

UtmpxSession { session: entry }
}

#[cfg(not(target_env = "gnu"))]
pub fn add_utmpx_entry(_username: &str, _tty: u8, _pid: u32) -> UtmpxSession {
log::info!("Incompatible platform for UTMPX. Skipping...");

UtmpxSession(entry)
UtmpxSession {}
}

#[cfg(target_env = "gnu")]
impl Drop for UtmpxSession {
fn drop(&mut self) {
let UtmpxSession(mut entry) = self;
let UtmpxSession { session: mut entry } = self;

info!("Removing UTMPX record");
log::info!("Removing UTMPX record");

entry.ut_type = libc::DEAD_PROCESS;

entry.ut_line = <[c_char; 32]>::default();
entry.ut_user = <[c_char; 32]>::default();
entry.ut_line = <[libc::c_char; 32]>::default();
entry.ut_user = <[libc::c_char; 32]>::default();

entry.ut_tv.tv_usec = 0;
entry.ut_tv.tv_sec = 0;

unsafe {
libc::setutxent();
libc::pututxline(&entry as *const utmpx);
libc::pututxline(&entry as *const libc::utmpx);
libc::endutxent();
}
}
Expand Down
13 changes: 9 additions & 4 deletions src/chvt.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
//! Adapted From https://github.com/jonay2000/chvt-rs

use libc::{c_int, c_ulong};
#[cfg(not(target_env = "musl"))]
type RequestType = libc::c_ulong;
#[cfg(target_env = "musl")]
type RequestType = libc::c_int;

use libc::c_int;
use nix::errno::Errno;
use nix::fcntl::{self, OFlag};
use nix::sys::stat::Mode;
use nix::unistd::close;
use std::error::Error;
use std::fmt::{self, Debug, Display, Formatter};

const VT_ACTIVATE: c_ulong = 0x5606;
const VT_WAITACTIVE: c_ulong = 0x5607;
const VT_ACTIVATE: RequestType = 0x5606;
const VT_WAITACTIVE: RequestType = 0x5607;

// Request Number to get Keyboard Type
const KDGKBTYPE: c_ulong = 0x4B33;
const KDGKBTYPE: RequestType = 0x4B33;

const KB_101: u8 = 0x02;
const KB_84: u8 = 0x01;
Expand Down

0 comments on commit 4d16767

Please sign in to comment.