Rewrite listener in Rust
authorJacob Casper <dev@jacobcasper.com>
Fri, 24 Apr 2020 23:01:20 +0000 (18:01 -0500)
committerJacob Casper <dev@jacobcasper.com>
Sat, 25 Apr 2020 02:50:27 +0000 (21:50 -0500)
.gitignore
Cargo.lock [new file with mode: 0644]
Cargo.toml [new file with mode: 0644]
Makefile
sockgit-listen@.service
src/main.rs [new file with mode: 0644]

index d874ad6..36f8a9c 100644 (file)
@@ -1 +1,6 @@
 *.tar
+
+
+#Added by cargo
+
+/target
diff --git a/Cargo.lock b/Cargo.lock
new file mode 100644 (file)
index 0000000..253295d
--- /dev/null
@@ -0,0 +1,203 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+[[package]]
+name = "autocfg"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d"
+
+[[package]]
+name = "bitflags"
+version = "1.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
+
+[[package]]
+name = "cc"
+version = "1.0.52"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c3d87b23d6a92cd03af510a5ade527033f6aa6fa92161e2d5863a907d4c5e31d"
+dependencies = [
+ "jobserver",
+]
+
+[[package]]
+name = "cfg-if"
+version = "0.1.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
+
+[[package]]
+name = "git2"
+version = "0.13.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e1e02a51cd90229028c9bd8be0a0364f85b6b3199cccaa0ef39005ddbd5ac165"
+dependencies = [
+ "bitflags",
+ "libc",
+ "libgit2-sys",
+ "log",
+ "openssl-probe",
+ "openssl-sys",
+ "url",
+]
+
+[[package]]
+name = "idna"
+version = "0.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "02e2673c30ee86b5b96a9cb52ad15718aa1f966f5ab9ad54a8b95d5ca33120a9"
+dependencies = [
+ "matches",
+ "unicode-bidi",
+ "unicode-normalization",
+]
+
+[[package]]
+name = "jobserver"
+version = "0.1.21"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5c71313ebb9439f74b00d9d2dcec36440beaf57a6aa0623068441dd7cd81a7f2"
+dependencies = [
+ "libc",
+]
+
+[[package]]
+name = "libc"
+version = "0.2.69"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "99e85c08494b21a9054e7fe1374a732aeadaff3980b6990b94bfd3a70f690005"
+
+[[package]]
+name = "libgit2-sys"
+version = "0.12.5+1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3eadeec65514971355bf7134967a543f71372f35b53ac6c7143e7bd157f07535"
+dependencies = [
+ "cc",
+ "libc",
+ "libssh2-sys",
+ "libz-sys",
+ "openssl-sys",
+ "pkg-config",
+]
+
+[[package]]
+name = "libssh2-sys"
+version = "0.2.16"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7bb70f29dc7c31d32c97577f13f41221af981b31248083e347b7f2c39225a6bc"
+dependencies = [
+ "cc",
+ "libc",
+ "libz-sys",
+ "openssl-sys",
+ "pkg-config",
+ "vcpkg",
+]
+
+[[package]]
+name = "libz-sys"
+version = "1.0.25"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2eb5e43362e38e2bca2fd5f5134c4d4564a23a5c28e9b95411652021a8675ebe"
+dependencies = [
+ "cc",
+ "libc",
+ "pkg-config",
+ "vcpkg",
+]
+
+[[package]]
+name = "log"
+version = "0.4.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7"
+dependencies = [
+ "cfg-if",
+]
+
+[[package]]
+name = "matches"
+version = "0.1.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08"
+
+[[package]]
+name = "openssl-probe"
+version = "0.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "77af24da69f9d9341038eba93a073b1fdaaa1b788221b00a69bce9e762cb32de"
+
+[[package]]
+name = "openssl-sys"
+version = "0.9.55"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7717097d810a0f2e2323f9e5d11e71608355e24828410b55b9d4f18aa5f9a5d8"
+dependencies = [
+ "autocfg",
+ "cc",
+ "libc",
+ "pkg-config",
+ "vcpkg",
+]
+
+[[package]]
+name = "percent-encoding"
+version = "2.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e"
+
+[[package]]
+name = "pkg-config"
+version = "0.3.17"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "05da548ad6865900e60eaba7f589cc0783590a92e940c26953ff81ddbab2d677"
+
+[[package]]
+name = "smallvec"
+version = "1.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c7cb5678e1615754284ec264d9bb5b4c27d2018577fd90ac0ceb578591ed5ee4"
+
+[[package]]
+name = "sockgit"
+version = "0.1.0"
+dependencies = [
+ "git2",
+]
+
+[[package]]
+name = "unicode-bidi"
+version = "0.3.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "49f2bd0c6468a8230e1db229cff8029217cf623c767ea5d60bfbd42729ea54d5"
+dependencies = [
+ "matches",
+]
+
+[[package]]
+name = "unicode-normalization"
+version = "0.1.12"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5479532badd04e128284890390c1e876ef7a993d0570b3597ae43dfa1d59afa4"
+dependencies = [
+ "smallvec",
+]
+
+[[package]]
+name = "url"
+version = "2.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "829d4a8476c35c9bf0bbce5a3b23f4106f79728039b726d292bb93bc106787cb"
+dependencies = [
+ "idna",
+ "matches",
+ "percent-encoding",
+]
+
+[[package]]
+name = "vcpkg"
+version = "0.2.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3fc439f2794e98976c88a2a2dafce96b930fe8010b0a256b3c2199a773933168"
diff --git a/Cargo.toml b/Cargo.toml
new file mode 100644 (file)
index 0000000..058174e
--- /dev/null
@@ -0,0 +1,10 @@
+[package]
+name = "sockgit"
+version = "0.1.0"
+authors = ["Jacob Casper <jacob.thomas.casper@gmail.com>"]
+edition = "2018"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+git2 = "0.13"
index e892f74..6bea3fa 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,11 +1,9 @@
 .PHONY: install uninstall
 install:
-       mkdir -p /usr/share/sockgit/templates && cp -r templates/* /usr/share/sockgit/templates/
        cp sockgit-listen* /etc/systemd/system/
        ln -s $(shell pwd)/sockgit.sh /usr/bin/sockgit
 
 uninstall:
        rm -f /etc/systemd/system/sockgit-listen.socket
        rm -f /etc/systemd/system/sockgit-listen@.service
-       rm -rf /usr/share/sockgit
        rm -f /usr/bin/sockgit
index 869e37f..3ce1884 100644 (file)
@@ -8,7 +8,7 @@ Type=oneshot
 User=git
 Group=git
 WorkingDirectory=/srv/git
-ExecStart=/bin/bash -c '[ $REMOTE_ADDR == $WHITELIST_IP ] && while read REPO; do /usr/bin/git init --bare --shared $REPO.git && cd $REPO.git && /usr/bin/git remote add public $USER@$PUBLIC:$PATH/$REPO.git; done;'
+ExecStart=/opt/sockgit/target/release/sockgit
 StandardInput=socket
 StandardOutput=syslog
 StandardError=syslog
diff --git a/src/main.rs b/src/main.rs
new file mode 100644 (file)
index 0000000..2b11523
--- /dev/null
@@ -0,0 +1,56 @@
+use std::io;
+use std::env;
+use std::path;
+use std::error;
+use git2;
+
+fn check_ip() -> Result<(), io::Error> {
+    match env::var("REMOTE_ADDR") {
+        Ok(remote_ip) => match env::var("WHITELIST_IP") {
+            Ok(whitelist_ip) => {
+                if remote_ip == whitelist_ip {
+                    Ok(())
+                } else {
+                    Err(io::Error::new(io::ErrorKind::ConnectionRefused, format!("Blocked connection from {}", remote_ip)))
+                }
+            }
+            Err(e) => Err(io::Error::new(io::ErrorKind::ConnectionRefused, e)),
+        }
+        Err(e) => Err(io::Error::new(io::ErrorKind::ConnectionRefused, e)),
+    }
+}
+
+fn main() -> Result<(), Box<dyn error::Error>> {
+    match check_ip() {
+        Ok(()) => {
+            let mut repo_name = String::new();
+            io::stdin().read_line(&mut repo_name)?;
+            // remove trailing newline
+            repo_name.pop();
+
+            let mut opts = git2::RepositoryInitOptions::new();
+            git2::RepositoryInitOptions::bare(&mut opts, true)
+                .mode(git2::RepositoryInitMode::SHARED_GROUP)
+                .no_reinit(true)
+                .template_path(path::Path::new("./templates"));
+
+            let repo = git2::Repository::init_opts(format!("{}.git", repo_name), &opts)?;
+
+            let public_user = env::var("USER").unwrap();
+            let public_name = env::var("PUBLIC").unwrap();
+            let public_path = env::var("PATH").unwrap();
+            repo.remote(
+                &public_name, 
+                &format!(
+                    "{user}@{public}:{path}/{repo}.git", 
+                    user = public_user, 
+                    public = public_name, 
+                    path = public_path, 
+                    repo = repo_name
+                    )
+                )?;
+            Ok(())
+        } 
+        Err(e) => Err(Box::new(e)),
+    }
+}