yagit

Yet another static site generator for Git 🙀️

Commit
23c444f89e7b760df12422ea553d85a1c6931a48
Parent
912a76d6d61c0d98e3c82d44845d6a1de5daee71
Author
Pablo <pablo-pie@riseup.net>
Date

Added a check for the git user

Check if the program is running under the "git" user

Diffstats

1 files changed, 30 insertions, 8 deletions

Status Name Changes Insertions Deletions
Modified src/main.rs 2 files changed 30 8
diff --git a/src/main.rs b/src/main.rs
@@ -31,6 +31,9 @@ mod log;
 mod markdown;
 mod time;
 
+#[cfg(not(debug_assertions))]
+const GIT_USER: &str = "git";
+
 const TREE_SUBDIR:   &str = "tree";
 const BLOB_SUBDIR:   &str = "blob";
 const COMMIT_SUBDIR: &str = "commit";
@@ -1391,7 +1394,7 @@ enum SubCommand {
 }
 
 impl SubCommand {
-  pub fn parse() -> Result<Self, ()> {
+  pub fn parse() -> Result<(Self, String), ()> {
     let mut args = env::args();
 
     #[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -1453,23 +1456,42 @@ impl SubCommand {
     }
 
     match tag {
-      Tag::RenderBatch => Ok(
-        Self::RenderBatch { batch_path: input_path, output_path, }
-      ),
-      Tag::Render => Ok(
-        Self::Render { repo_path: input_path, output_path, }
-      ),
+      Tag::RenderBatch => Ok((
+        Self::RenderBatch { batch_path: input_path, output_path, },
+        program_name
+      )),
+      Tag::Render => Ok((
+        Self::Render { repo_path: input_path, output_path, },
+        program_name
+      )),
     }
   }
 }
 
 fn main() -> ExitCode {
-  let cmd = if let Ok(cmd) = SubCommand::parse() {
+  #[allow(unused_variables)]
+  let (cmd, program_name) = if let Ok(cmd) = SubCommand::parse() {
     cmd
   } else {
     return ExitCode::FAILURE;
   };
 
+  #[cfg(not(debug_assertions))]
+  unsafe {
+    use std::ffi::CStr;
+
+    let uid = libc::getuid();
+    let pw = libc::getpwuid(uid);
+    if !pw.is_null() {
+      let user = CStr::from_ptr((*pw).pw_name).to_string_lossy();
+
+      if user != GIT_USER {
+        errorln!("Running {program_name} as the {user:?} user. Re-run as {GIT_USER:?}");
+        return ExitCode::FAILURE;
+      }
+    }
+  }
+
   match cmd {
     SubCommand::RenderBatch { batch_path, output_path } => {
       let repos = if let Ok(rs) = RepoInfo::from_batch_path(&batch_path) {