new tool
[hband-tools.git] / half-baked / cred
blobb8d72ca3541840b893fa2afb226b07840aa670bb
1 #!/bin/bash
3 set -eE
4 set -o pipefail
5 set -u
7 . /usr/lib/tool/bash-utils
9 print_traceback()
11 local i stack_size=${#FUNCNAME[@]}
12 for (( i=1; i<stack_size; i++ )); do
13 local func="${FUNCNAME[$i]}"
14 [[ $func = "" ]] && func=MAIN
15 local linen="${BASH_LINENO[$(( i - 1 ))]}"
16 local src="${BASH_SOURCE[$i]}"
17 [[ "$src" = "" ]] && src=non_file_source
19 echo " function: $func"
20 echo " file: $src (line $linen)"
21 nl -ba -w ${#linen} -s ' | ' "$src" | grep -C 2 -E "^\s*$linen " | prefixlines ' ' | sed -e "s/^\(\s*$linen .*\)/\x1B[1m\1\x1B[0m/"
22 done
25 trap 'print_traceback >&2' ERR
28 basedir=~/cred
30 subcmd=$1
31 shift
33 case "$subcmd" in
34 (dump)
35 site=${1:?'enter site name!'}
36 shift
37 options=("$@")
40 cd "$basedir/$site"
41 GLOBIGNORE=${GLOBIGNORE:-}${GLOBIGNORE:+:}".:..:.[!/.]*:..[!/]*:-*"
42 for file in *
44 if [ ! -f "$file" ]
45 then
46 continue
48 shopt -s nocasematch
49 if [[ $file =~ password|key|cvc|secret|pin ]]
50 then
51 if in_list reveal-secrets "${options[@]}"
52 then
53 true
54 elif in_list hash-secrets "${options[@]}"
55 then
56 echo "$file"$'\t'MD5=`cat "$file" | md5sum | cut -c 1-32`
57 continue
58 else
59 echo "$file"$'\t'"*****"
60 continue
64 grep . "$file" | prefixlines "$file"$'\t'
65 done
67 site=${site%/}
68 if [[ $site =~ / ]]
69 then
70 parent=${site%/*}
71 cred dump "$parent" "${options[@]}"
75 (password)
76 site=${1:?'enter site name!'}
77 cat "$basedir/$site/PASSWORD"
79 (clip-password)
80 site=${1:?'enter site name!'}
81 cat "$basedir/$site/PASSWORD" | xclip -i -selection clipboard
83 (list-sites)
84 find -L "$basedir" -path "$basedir/${1:-}*" -type d -printf "%P\n"
86 (prop)
87 site=${1:?'enter site name and property name!'}
88 shift
89 prop=${1:?'enter property name!'}
90 shift
91 if [ $# = 0 ]
92 then
93 cat "$basedir/$site/$prop"
94 else
95 case "$1" in
96 (set)
97 shift
98 val=$*
99 mkdir -p "$basedir/$site"
100 echo "$val" > "$basedir/$site/$prop"
102 (edit)
103 mkdir -p "$basedir/$site"
104 "${EDITOR:-$VISUAL}" "$basedir/$site/$prop"
106 (del)
107 rm "$basedir/$site/$prop"
110 false
112 esac
115 (list-props)
116 site=${1:?'enter site name, property name and value!'}
117 find "$basedir/$site" -type f -printf "%P\n"
119 (pwgen)
120 site=${1:?'enter site name!'}
121 newpwd=$(pwgen -y 8 1)$(pwgen -y 8 1)
122 pwdfile=$basedir/$site/PASSWORD
123 mkdir -p "$basedir/$site"
124 if [ -s "$pwdfile" ]
125 then
126 cur_pwd=`cat "$pwdfile"`
127 atime=`stat -c %x "$pwdfile"`
128 now=`date +'%F %T.%N %z'`
129 echo "$atime $now $cur_pwd" >> "$basedir/$site/OLDPASSWORDS"
131 printf '%s' "$newpwd" > "$pwdfile"
133 (compscript)
134 cat <<'EOF'
135 _autocomplete_cred() {
136 local compreply
137 local curr_word=${COMP_WORDS[$COMP_CWORD]}
138 local prev_word=${COMP_WORDS[$[COMP_CWORD - 1]]}
139 if [ $COMP_CWORD = 1 ]
140 then
141 compreply="list-sites dump dump-with-secrets password clip-password pwgen help compscript prop list-props"
142 elif [ $COMP_CWORD = 2 ]
143 then
144 compreply=`cred list-sites "$curr_word"`
145 else
146 case "${COMP_WORDS[1]}" in
147 (prop)
148 case $COMP_CWORD in
150 site=${COMP_WORDS[2]}
151 compreply=`cred list-props "$site"`
154 compreply="set edit del"
156 esac
158 (dump)
159 case $COMP_CWORD in
161 compreply="reveal-secrets mask-secrets hash-secrets"
163 esac
164 esac
166 COMPREPLY=($(compgen -W "$compreply" -- "${COMP_WORDS[$COMP_CWORD]}"))
167 return 0
169 complete -F _autocomplete_cred cred
170 # use this in your bash session by eg: eval "$(cred compscript)"
175 echo 'Use tab-completion!'
176 echo 'Example:'
177 echo 'eval "$(cred compscript)"'
178 } >&2
179 false
181 esac