diff --git a/gseeker b/gseeker new file mode 100755 index 0000000..98e83b5 --- /dev/null +++ b/gseeker @@ -0,0 +1,402 @@ +#!/bin/sh + +### DEFINE + +VERSION="0.3" +GSEEKER_HOME="$HOME/.gseeker" +GSEEKER_REPORTS="$GSEEKER_HOME/reports" +GSEEKER_TMPDIR="$GSEEKER_HOME/tmp/pid_$$" +GSEEKER_TREPORT="$GSEEKER_TMPDIR/report" +GSEEKER_CONFIG="$GSEEKER_HOME/config" +GSEEKER_TRACKER="$GSEEKER_HOME/tracker" + +### + +cleanup() { + rm -rf $GSEEKER_TMPDIR +} + +cmd_tree() { + case $1 in + config ) + cmd_tree_config $* + ;; + run ) + cmd_tree_run $* + ;; + show ) + cmd_tree_show $* + ;; + help ) + cmd_tree_help $* + ;; + * ) + echo "usage: $0 []" + echo "" + echo "These are common gseeker commands used in various situations:" + echo "" + echo " config add and delete settings" + echo " show show metadata" + echo " run run diagnostics on chosen URL's or groups added through " + echo "" + echo "See '$0 help ' to read about a specific subcommand." + ;; + esac +} + +cmd_tree_help() { + case $2 in + config ) + cmd_tree_help_config $* + ;; + show ) + cmd_tree_help_show $* + ;; + run ) + cmd_tree_help_run $* + ;; + * ) + echo "usage: $0 help " + echo "" + echo " config show help for the config command" + echo " show show help for the show command" + echo " run show help for the run command" + echo "" + ;; + esac +} + +cmd_tree_help_config() { + case $3 in + group ) + cmd_tree_help_config_group $* + ;; + url ) + cmd_tree_help_config_url $* + ;; + * ) + echo "usage: $0 help config [group, url]" + ;; + esac +} + +cmd_tree_help_config_group() { + case $4 in + add ) + echo "usage: $0 config group add " + echo "" + echo "Examples" + echo "" + echo " $0 config group add testingGroup" + echo " $0 config group add anotherGroup" + ;; + del ) + echo "usage: $0 config group del " + echo "" + echo "Examples:" + echo "" + echo " $0 config group del testingGroup" + echo " $0 config group del anotherGroup" + ;; + * ) + echo "usage: $0 help config group [add, del]" + ;; + esac +} + +cmd_tree_help_config_url() { + case $4 in + add ) + echo "usage: $0 config url add " + echo "" + echo "Examples:" + echo "" + echo '$0 config url add "https://git.sr.ht/~umgeher/gseeker" gseekerGroup' + echo '$0 config url add "https://git.sr.ht/~someperson/someproject" someGroup' + ;; + del ) + echo "usage: $0 config url del " + echo "" + echo "Examples:" + echo "" + echo '$0 config url del 05e8314dd1e2552e7d124048fba8f275d4ad0f60 gseekerGroup' + echo '$0 config url del cbbb2a6efd6217a48fd16b5c9430f2876bab222a someGroup' + echo "" + echo " are the sha1 hashes of the URL's" + ;; + * ) + echo "usage: $0 help config url [add, del]" + ;; + esac +} + +cmd_tree_help_show() { + case $3 in + config ) + echo "prints all URL's and groups added to the config file through the" + echo '"config [group, url] add " command to standard output' + echo "" + echo "Example:" + echo "" + echo "$0 show config" + ;; + * ) + echo "usage: $0 help show config" + ;; + esac +} + +cmd_tree_help_run() { + case $3 in + null ) + echo 'where "null" stands for no following :' + echo "" + echo "runs diagnostics on all URL's found within any group in the config" + echo "file" + echo "" + echo "Example:" + echo "" + echo "$0 run" + ;; + group ) + cmd_tree_help_run_group $* + ;; + * ) + echo "usage: $0 help run [null, group]" + ;; + esac +} + +cmd_tree_help_run_group() { + case $4 in + null ) + echo 'where "null" stands for no following :' + echo "" + echo "runs diagnostics on all URL's found within the specified group in the" + echo "config file" + echo "" + echo "Example:" + echo "" + echo "$0 run gseekerGroup" + echo "$0 run someGroup" + ;; + url ) + echo "runs diagnostics on the specified URL found in the specified group" + echo "" + echo "Example:" + echo "" + echo '$0 run gseekerGroup "https://git.sr.ht/~umgeher/gseeker"' + echo '$0 run someGroup "https://git.sr.ht/~someperson/someproject"' + ;; + * ) + echo "usage: $0 help run group [null, url]" + ;; + esac +} + +cmd_tree_config() { + case $2 in + group ) + cmd_tree_config_group $* + ;; + url ) + cmd_tree_config_url $* + ;; + * ) + echo "usage: $0 config []" + echo "" + echo " group add or delete a group from the config file" + echo " url add or delete a URL from the config file" + echo "" + echo "See '$0 help config ' to read about a specific subcommand." + ;; + esac +} + +cmd_tree_config_group() { + case $3 in + add ) + cmd_tree_config_group_add $* + ;; + del ) + cmd_tree_config_group_del $* + ;; + * ) + echo "usage: $0 config group []" + echo "" + echo " add add a group to the config file" + echo " del delete a group from the config file" + echo "" + echo "See '$0 help config group ' to read about a specific subcommand." + ;; + esac +} + +cmd_tree_config_group_add() { + if [[ -n $4 ]]; then + HASH=$(sha1 -qs $4) + echo "group,name,$4,$HASH" >> $GSEEKER_CONFIG + else + echo "usage: $0 config group add " + fi +} + +cmd_tree_config_group_del() { + if [[ -n $4 ]]; then + sed -i "/group,name,$4,/d" $GSEEKER_CONFIG + else + echo "usage: $0 config group del " + fi +} + +cmd_tree_config_url() { + case $3 in + add ) + cmd_tree_config_url_add $* + ;; + del ) + cmd_tree_config_url_del $* + ;; + * ) + echo "usage: $0 config url []" + echo "" + echo " add add a URL to the config file" + echo " del delete a URL from the config file" + echo "" + echo "See '$0 help config url ' to read about a specific subcommand." + ;; + esac +} + +cmd_tree_config_url_add() { + if [[ -n $4 && -n $5 ]]; then + echo "url,$(sha1 -qs $4),$(sha1 -qs $5),$4" >> $GSEEKER_CONFIG + else + echo "usage: $0 config url add " + fi +} + +cmd_tree_config_url_del() { + if [[ -n $4 && -n $5 ]]; then + sed -i "/url,$4,$(sha1 -qs $5),/d" $GSEEKER_CONFIG + else + echo "usage: $0 config url del " + fi +} + +cmd_tree_run() { + if [[ -n $2 ]]; then + GROUP_HASH=$(sha1 -qs $2) + + if [[ -n $(cat $GSEEKER_CONFIG | grep $GROUP_HASH) ]]; then + if [[ -n $3 ]]; then + if [[ -n $(cat $GSEEKER_CONFIG | grep $3) ]]; then + rep_run $GROUP_HASH $3 + else + echo "error: repo $3 not found" + fi + else + group_run $GROUP_HASH + fi + else + echo "error: group $2 not found" + fi + else + for GROUP in $(cat $GSEEKER_CONFIG | grep "group,name," | cut -f4 -d,); do + group_run $GROUP + done + fi +} + +cmd_tree_show() { + case $2 in + config ) + cat $GSEEKER_CONFIG + ;; + * ) + echo "usage: $0 show " + echo "" + echo "See '$0 help show ' to read about a specific subcommand." + ;; + esac +} + +group_run() { + for HASH in $(cat $GSEEKER_CONFIG | grep $1 | grep "url," | cut -f2 -d,); do + rep_run $1 $HASH + done +} + +main() { + setup + cmd_tree $* + cleanup +} + +rep_run() { + URL=$(cat $GSEEKER_CONFIG | grep "$2,$1" | cut -f4 -d,) + mkdir -p $GSEEKER_TMPDIR/$1/ + git clone $URL $GSEEKER_TMPDIR/$1/$2 + rep_stats $1 $2 +} + +rep_stats() { + FILE="$GSEEKER_TREPORT/repo_$2" + DATE=$(date +%s) + echo "date,$DATE" >> $FILE + echo "version,$VERSION" >> $FILE + echo "url,$(cd $GSEEKER_TMPDIR/$1/$2 ; git config --list | grep 'remote.origin.url=' | cut -f2 -d'=')" >> $FILE + echo "head,$(cd $GSEEKER_TMPDIR/$1/$2 ; git branch -r | grep ' origin/HEAD' | sed 's/ origin\/HEAD -> origin\///g')" >> $FILE + echo "commit,last,$(cd $GSEEKER_TMPDIR/$1/$2 ; git log --max-count=1 --pretty=short | grep 'commit ' | sed 's/commit //g')" >> $FILE + echo "size,pack,$(cd $GSEEKER_TMPDIR/$1/$2 ; git count-objects -vH | grep "size-pack" | sed 's/size-pack: //g')" >> $FILE + + for BRANCH in $(cd $GSEEKER_TMPDIR/$1/$2 ; git branch -r | grep ' origin/' | grep -v 'HEAD' | sed 's/origin\///g'); do + echo "branch,$BRANCH" >> $FILE + done + + for LINE in $(cd $GSEEKER_TMPDIR/$1/$2 ; git shortlog -se | tr '\t' ',' | sed 's/ /_/g'); do + COMMITS=$(echo $LINE | cut -f1 -d, | sed 's/_//g') + NAME=$(echo $LINE | cut -f2 -d, | sed 's/_//g') + echo "shortlog,$EMAIL,$NAME,$COMMITS" >> $FILE + done + + $(cat $GSEEKER_TRACKER | grep "commit,number," | grep ",$2,$1" | cut -f3 -d, > $GSEEKER_TMPDIR/tracker_$2_$1) + $(cd $GSEEKER_TMPDIR/$1/$2 ; git log --oneline | nl | sort -rn | cut -f2 | cut -d" " -f1 > $GSEEKER_TMPDIR/cn_$2_$1) + + for CNUMBER in $(grep -Fvf $GSEEKER_TMPDIR/tracker_$2_$1 $GSEEKER_TMPDIR/cn_$2_$1); do + $(cd $GSEEKER_TMPDIR/$1/$2 ; git checkout $CNUMBER) + echo "commit,number,$CNUMBER" >> $FILE + scan_file_alert $1 $2 + echo "commit,number,$CNUMBER,$2,$1" >> $GSEEKER_TRACKER + done + + cp $FILE "$GSEEKER_REPORTS/report-$DATE-$2-$1.gseeker" +} + +scan_file_alert() { + for FTYPE in "*.pem" "*.zip" "*.pub"; do + FLIST=$(cd $GSEEKER_TMPDIR/$1/$2 ; find . -type f -name $FTYPE) + + if [[ -n $FLIST ]]; then + for TF in $FLIST; do + HASH=$(cd $GSEEKER_TMPDIR/$1/$2 ; sha1 -q $TF) + + if [[ -z $(cat $FILE | grep "file,alert,$HASH,") ]]; then + echo "file,alert,$HASH,$CNUMBER,$TF" >> $FILE + fi + done + fi + done +} + +setup() { + test -d $GSEEKER_HOME || (mkdir -p $GSEEKER_HOME) + test -d $GSEEKER_REPORTS || (mkdir -p $GSEEKER_REPORTS) + test -d $GSEEKER_TMPDIR || (mkdir -p $GSEEKER_TMPDIR) + test -d $GSEEKER_TREPORT || (mkdir -p $GSEEKER_TREPORT) + test -f $GSEEKER_CONFIG || (touch $GSEEKER_CONFIG) + test -f $GSEEKER_TRACKER || (touch $GSEEKER_TRACKER) +} + +main $* diff --git a/makefile b/makefile new file mode 100644 index 0000000..0319fbe --- /dev/null +++ b/makefile @@ -0,0 +1,10 @@ +.PHONY: install uninstall +.MAIN: install + +install: + mkdir -p ~/bin + cp -v gseeker ~/bin + chmod +x ~/bin/gseeker + +uninstall: + rm -vf ~/bin/gseeker