aboutsummaryrefslogtreecommitdiff
path: root/src/login.rs
blob: 8639d35fc308c6e540d3ced74f4e09c9b00e4361 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
use std::path::{Path, PathBuf};

use crate::config::RepoConfig;
use crate::Login;

use clap::Args;
use clap::{Parser, Subcommand};

#[derive(Parser)]
#[command()]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    #[command(name = "git-upload-pack")]
    GitUploadPack(GitUploadPack),
    #[command(name = "git-receive-pack")]
    GitReceivePack(GitUploadPack), // TODO: own args
}
#[derive(Args)]
struct GitUploadPack {
    repo_path: String,
}
pub(crate) fn do_login(login: &Login) {
    let command = std::env::var("SSH_ORIGINAL_COMMAND").unwrap_or("<no command>".to_owned());
    log::info!(
        "Performed token login for {} {} with command: {}",
        login.keytype,
        login.keydata,
        command
    );
    let user_token = get_user_token(login.keytype.to_owned(), login.keydata.to_owned());
    let mut command = shlex::split(&command).unwrap();
    command.insert(0, "fagit-dummy".to_owned());
    let args = Cli::parse_from(command.iter());
    match args.command {
        Commands::GitUploadPack(upload_pack) => {
            let repo_path = canonicalize_repo(&upload_pack.repo_path, &user_token);
            if can_read(&repo_path, &user_token) {
                let mut child = std::process::Command::new("git-upload-pack")
                    .args([repo_path.repo_path().as_os_str()])
                    .spawn()
                    .unwrap();
                child.wait().unwrap();
            }
        }
        Commands::GitReceivePack(upload_pack) => {
            let repo_path = canonicalize_repo(&upload_pack.repo_path, &user_token);
            if can_write(&repo_path, &user_token) {
                let mut child = std::process::Command::new("git-receive-pack")
                    .args([repo_path.repo_path().as_os_str()])
                    .spawn()
                    .unwrap();
                child.wait().unwrap();
            }
        }
    }
}
struct UserToken {
    keytype: String,
    keydata: String,

    is_operator: bool,
}
struct RepoId(String);
impl RepoId {
    fn is_meta_repo(&self) -> bool {
        return self.0.starts_with("meta/") || self.0 == "meta.git";
    }
    fn repo_path(&self) -> PathBuf {
        return Path::join(crate::config::repo_store(), self.0.clone());
    }

    fn get_config(&self) -> RepoConfig {
        let config_process = std::process::Command::new("git")
            .args([
                "-C",
                self.repo_path().to_str().unwrap(),
                "show",
                "HEAD:.fagit.toml",
            ])
            .output()
            .unwrap();
        if !config_process.status.success() {
            return RepoConfig::default();
        }
        toml::de::from_str(&String::from_utf8(config_process.stdout).unwrap()).unwrap()
    }
}

fn canonicalize_repo(path: &str, user: &UserToken) -> RepoId {
    let mut path = path.to_owned();
    path.make_ascii_lowercase();
    if path.starts_with("/") {
        path.remove(0);
    }
    if path.ends_with("/") {
        path.remove(path.len() - 1);
    }
    if !path.ends_with(".git") {
        path += ".git";
    }
    // TODO: properly check directory traversal or smth here. just a regex should do the trick
    return RepoId(path);
}

fn can_write(repo_path: &RepoId, user_token: &UserToken) -> bool {
    if repo_path.is_meta_repo() {
        return user_token.is_operator;
    }
    return true;
}
fn can_read(repo_path: &RepoId, user_token: &UserToken) -> bool {
    if repo_path.is_meta_repo() {
        return user_token.is_operator;
    }
    return true;
}

fn get_user_token(keytype: String, keydata: String) -> UserToken {
    // TODO: settings
    let is_operator = "ssh-ed25519" == keytype
        && "AAAAC3NzaC1lZDI1NTE5AAAAINg2WYMRKINwbH5UCqqK2qq/qW0gG1NnaALHqEyU4NzM" == keydata;
    return UserToken {
        keytype,
        keydata,
        is_operator,
    };
}