Skip to content

Commit

Permalink
Add more wrappers and stuff (CI won't work)
Browse files Browse the repository at this point in the history
  • Loading branch information
MEhrn00 committed Feb 18, 2024
1 parent 90b494a commit acf33c1
Show file tree
Hide file tree
Showing 20 changed files with 796 additions and 197 deletions.
28 changes: 23 additions & 5 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ARG userGid=1000
ARG upgradePackages=false
ARG goversion=1.22.0
ARG golangcilintversion=1.56.2
ARG shell=bash
ARG shell=fish
ARG extraPackages
ARG protocversion=25.3

Expand Down Expand Up @@ -38,13 +38,21 @@ WORKDIR /tmp
RUN curl -L "https://go.dev/dl/go${goversion}.linux-amd64.tar.gz" -o go${goversion}.linux-amd64.tar.gz
RUN rm -rf /usr/local/go
RUN tar -C /usr/local -xzf go${goversion}.linux-amd64.tar.gz
RUN echo "export PATH=$PATH:/usr/local/go/bin" >> /home/${username}/.profile

RUN grep -qxF 'export PATH=$PATH:/usr/local/go/bin' /home/${username}/.profile \
|| echo 'export PATH=$PATH:/usr/local/go/bin' >> /home/${username}/.profile

RUN rm -f go${goversion}.linux-amd64.tar.gz

# Install golangci-lint
USER ${username}
RUN mkdir -p /home/${username}/go/bin
RUN curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b /home/${username}/go/bin v${golangcilintversion}

RUN curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh \
| sh -s -- -b /home/${username}/go/bin v${golangcilintversion}

RUN grep -qxF 'export PATH=$PATH:/home/${username}/go/bin' /home/${username}/.profile \
|| echo 'export PATH=$PATH:/home/${username}/go/bin' >> /home/${username}/.profile

# Install Rust
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs -o rustup.sh \
Expand All @@ -56,7 +64,9 @@ RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs -o rustup.sh \
-t i686-unknown-linux-gnu \
-t i686-pc-windows-gnu

RUN echo '. "$HOME/.cargo/env"' >> /home/${username}/.profile
RUN grep -qxF '. "$HOME/.cargo/env"' /home/${username}/.profile \
|| echo '. "$HOME/.cargo/env"' >> /home/${username}/.profile

RUN rm -f rustup.sh

# Install rustfmt and clippy
Expand All @@ -67,7 +77,8 @@ USER 0
# Install protoc
RUN mkdir -p /tmp/protoc
WORKDIR /tmp/protoc
RUN curl -L https://github.com/protocolbuffers/protobuf/releases/download/v${protocversion}/protoc-${protocversion}-linux-x86_64.zip -o protoc.zip
RUN curl -L https://github.com/protocolbuffers/protobuf/releases/download/v${protocversion}/protoc-${protocversion}-linux-x86_64.zip \
-o protoc.zip
RUN unzip protoc.zip
RUN mv bin/protoc /usr/local/bin
RUN chmod 755 /usr/local/bin/protoc
Expand All @@ -90,3 +101,10 @@ RUN echo "${username} ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers
RUN if [ "${extraPackages}" != "" ]; then DEBIAN_FRONTEND=noninteractive apt-get install -y ${extraPackages}; fi

USER ${username}

# Setup PATH if fish is the shell. This needs to be in the context of the user
RUN if [ "${shell}" = "fish" ]; then \
/usr/bin/fish -c 'set -U fish_user_paths /usr/local/go/bin $fish_user_paths' \
&& /usr/bin/fish -c 'set -U fish_user_paths $HOME/.cargo/bin $fish_user_paths' \
&& /usr/bin/fish -c 'set -U fish_user_paths $HOME/go/bin $fish_user_paths' \
; fi
2 changes: 2 additions & 0 deletions Payload_Type/thanatos/agent/errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ pub enum ThanatosError {
FFIError(FfiError),
NotDomainJoined,

IoError(std::io::Error),

ConfigParseError,
}

Expand Down
3 changes: 3 additions & 0 deletions Payload_Type/thanatos/agent/ffiwrappers/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ pub enum FfiError {
GaiError(EaiError),
NonNullPointer,
CanonNameNotFound,

#[cfg(target_os = "linux")]
NoGroupMembership,
}

impl FfiError {
Expand Down
121 changes: 29 additions & 92 deletions Payload_Type/thanatos/agent/ffiwrappers/src/linux/addrinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,64 +3,7 @@ use std::{ffi::CStr, marker::PhantomData, ptr::NonNull};
use crate::errors::{EaiError, FfiError};
use bitflags::bitflags;

#[repr(i32)]
#[derive(Default)]
pub enum Family {
AfInet = libc::AF_INET,
AfInet6 = libc::AF_INET6,
#[default]
Unspec = libc::AF_UNSPEC,
Other(i32),
}

impl From<i32> for Family {
fn from(value: i32) -> Self {
match value {
libc::AF_INET => Self::AfInet,
libc::AF_INET6 => Self::AfInet6,
libc::AF_UNSPEC => Self::Unspec,
_ => Self::Other(value),
}
}
}

impl From<Family> for i32 {
fn from(value: Family) -> Self {
match value {
Family::AfInet => libc::AF_INET,
Family::AfInet6 => libc::AF_INET6,
Family::Unspec => libc::AF_UNSPEC,
Family::Other(v) => v,
}
}
}

#[repr(i32)]
#[derive(Default)]
pub enum SockType {
#[default]
Any = 0,
SockStream = libc::SOCK_STREAM,
SockDgram = libc::SOCK_DGRAM,
SockSeqPacket = libc::SOCK_SEQPACKET,
SockRaw = libc::SOCK_RAW,
SockRdm = libc::SOCK_RDM,
SockPacket = libc::SOCK_PACKET,
}

impl From<i32> for SockType {
fn from(value: i32) -> Self {
match value {
libc::SOCK_STREAM => Self::SockStream,
libc::SOCK_DGRAM => Self::SockDgram,
libc::SOCK_SEQPACKET => Self::SockSeqPacket,
libc::SOCK_RAW => Self::SockRaw,
libc::SOCK_RDM => Self::SockRdm,
libc::SOCK_PACKET => Self::SockPacket,
_ => Self::Any,
}
}
}
use super::socket::{Family, SockType};

bitflags! {
pub struct AiFlags: i32 {
Expand All @@ -85,7 +28,6 @@ pub struct Hints {
pub flags: AiFlags,
}

#[repr(transparent)]
pub struct AddrInfoList {
addrinfo: NonNull<libc::addrinfo>,
_marker: PhantomData<libc::addrinfo>,
Expand Down Expand Up @@ -125,11 +67,8 @@ impl AddrInfoList {
})
}

pub fn iter(&self) -> AddrInfoListIterator<'_> {
AddrInfoListIterator {
addrinfo: self.addrinfo.as_ptr(),
_marker: PhantomData,
}
pub fn first<'a>(&'a self) -> AddrInfo<'a> {
self.addrinfo.into()
}
}

Expand All @@ -139,26 +78,6 @@ impl Drop for AddrInfoList {
}
}

#[repr(transparent)]
pub struct AddrInfoListIterator<'a> {
addrinfo: *mut libc::addrinfo,
_marker: PhantomData<&'a libc::addrinfo>,
}

impl<'a> Iterator for AddrInfoListIterator<'a> {
type Item = AddrInfo<'a>;

fn next(&mut self) -> Option<Self::Item> {
let curr = AddrInfo {
addrinfo: NonNull::new(self.addrinfo)?,
_marker: PhantomData,
};

self.addrinfo = unsafe { *self.addrinfo }.ai_next;
Some(curr)
}
}

#[repr(transparent)]
pub struct AddrInfo<'a> {
addrinfo: NonNull<libc::addrinfo>,
Expand All @@ -167,26 +86,44 @@ pub struct AddrInfo<'a> {

impl<'a> AddrInfo<'a> {
pub fn ai_flags(&self) -> i32 {
unsafe { self.addrinfo.as_ref() }.ai_flags
unsafe { self.addrinfo.as_ref().ai_flags }
}

pub fn ai_family(&self) -> Family {
unsafe { self.addrinfo.as_ref() }.ai_family.into()
unsafe { self.addrinfo.as_ref().ai_family }.into()
}

pub fn ai_socktype(&self) -> SockType {
unsafe { self.addrinfo.as_ref() }.ai_socktype.into()
unsafe { self.addrinfo.as_ref().ai_socktype }.into()
}

pub fn ai_protocol(&self) -> i32 {
unsafe { self.addrinfo.as_ref() }.ai_protocol
unsafe { self.addrinfo.as_ref().ai_protocol }
}

pub fn canonname(&self) -> Option<&CStr> {
if unsafe { self.addrinfo.as_ref().ai_canonname }.is_null() {
return None;
pub fn canonname(&self) -> &str {
unsafe {
CStr::from_ptr(self.addrinfo.as_ref().ai_canonname)
.to_str()
.unwrap_unchecked()
}
}
}

impl<'a> From<NonNull<libc::addrinfo>> for AddrInfo<'a> {
fn from(value: NonNull<libc::addrinfo>) -> Self {
AddrInfo {
addrinfo: value,
_marker: PhantomData,
}
}
}

Some(unsafe { CStr::from_ptr(self.addrinfo.as_ref().ai_canonname) })
impl<'a> Iterator for AddrInfo<'a> {
type Item = AddrInfo<'a>;

fn next(&mut self) -> Option<Self::Item> {
self.addrinfo = NonNull::new(unsafe { self.addrinfo.as_ref().ai_next })?;
Some(self.addrinfo.into())
}
}
49 changes: 49 additions & 0 deletions Payload_Type/thanatos/agent/ffiwrappers/src/linux/group.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use std::{ffi::CStr, ptr::NonNull};

use crate::errors::FfiError;

pub struct GroupInfo(NonNull<libc::group>);

impl GroupInfo {
pub fn current_group() -> Result<GroupInfo, FfiError> {
Self::lookup_gid(unsafe { libc::getgid() })
}

pub fn effective_user() -> Result<GroupInfo, FfiError> {
Self::lookup_gid(unsafe { libc::getegid() })
}

pub fn lookup_username(username: &CStr) -> Result<GroupInfo, FfiError> {
let grpasswd = unsafe { libc::getgrnam(username.as_ptr()) };
Ok(Self(
NonNull::new(grpasswd).ok_or_else(|| FfiError::os_error())?,
))
}

pub fn lookup_gid(gid: u32) -> Result<GroupInfo, FfiError> {
let grpasswd = unsafe { libc::getgrgid(gid) };
Ok(Self(
NonNull::new(grpasswd).ok_or_else(|| FfiError::os_error())?,
))
}

pub fn groupname<'a>(&'a self) -> &'a str {
unsafe {
CStr::from_ptr(self.0.as_ref().gr_name)
.to_str()
.unwrap_unchecked()
}
}

pub fn passwd<'a>(&'a self) -> &'a str {
unsafe {
CStr::from_ptr(self.0.as_ref().gr_passwd)
.to_str()
.unwrap_unchecked()
}
}

pub fn gid(&self) -> u32 {
unsafe { self.0.as_ref().gr_gid }
}
}
66 changes: 66 additions & 0 deletions Payload_Type/thanatos/agent/ffiwrappers/src/linux/ifaddrs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use std::{ffi::CStr, marker::PhantomData, ptr::NonNull};

use crate::errors::FfiError;

pub struct IfAddrsList {
ifaddrs: NonNull<libc::ifaddrs>,
_marker: PhantomData<libc::addrinfo>,
}

impl IfAddrsList {
pub fn new() -> Result<IfAddrsList, FfiError> {
let mut ifap = std::ptr::null_mut();

if unsafe { libc::getifaddrs(&mut ifap) } != 0 {
return Err(FfiError::os_error());
}

Ok(IfAddrsList {
ifaddrs: NonNull::new(ifap).ok_or(FfiError::os_error())?,
_marker: PhantomData,
})
}

pub fn first<'a>(&'a self) -> IfAddr<'a> {
self.ifaddrs.into()
}
}

impl Drop for IfAddrsList {
fn drop(&mut self) {
unsafe { libc::freeifaddrs(self.ifaddrs.as_ptr()) };
}
}

pub struct IfAddr<'a> {
ifaddr: NonNull<libc::ifaddrs>,
_marker: PhantomData<&'a libc::ifaddrs>,
}

impl<'a> IfAddr<'a> {
pub fn name(&self) -> &str {
unsafe {
CStr::from_ptr(self.ifaddr.as_ref().ifa_name)
.to_str()
.unwrap_unchecked()
}
}
}

impl<'a> From<NonNull<libc::ifaddrs>> for IfAddr<'a> {
fn from(value: NonNull<libc::ifaddrs>) -> Self {
IfAddr {
ifaddr: value,
_marker: PhantomData,
}
}
}

impl<'a> Iterator for IfAddr<'a> {
type Item = IfAddr<'a>;

fn next(&mut self) -> Option<Self::Item> {
self.ifaddr = NonNull::new(unsafe { self.ifaddr.as_ref().ifa_next })?;
Some(self.ifaddr.into())
}
}
7 changes: 5 additions & 2 deletions Payload_Type/thanatos/agent/ffiwrappers/src/linux/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
mod gethostname;
mod username;

pub mod addrinfo;
pub mod group;
pub mod ifaddrs;
pub mod socket;
pub mod uname;
pub mod user;

pub use gethostname::gethostname;
pub use username::username;
Loading

0 comments on commit acf33c1

Please sign in to comment.