Debian Codegraph @

keyringer

raw
1 #!/usr/bin/env bash
2 #
3 # Keyringer key management system.
4 #
5 # Copyright (C) 2010 Silvio Rhatto - rhatto at riseup.net
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published
9 # by the Free Software Foundation, either version 3 of the License,
10 # or any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #
20
21 # Initialize a new keyring
22 function keyringer_init {
23 BASEDIR="$3"
24 URL="$4"
25 RECIPIENTS="$BASEDIR/config/recipients"
26 OPTIONS="$BASEDIR/config/options"
27 VERSION_INFO="$BASEDIR/config/version"
28
29 # We are initializing, so avoid some checks
30 export KEYRINGER_CHECK_VERSION="false"
31 export KEYRINGER_CHECK_RECIPIENTS="false"
32
33 # Parse
34 if [ -z "$BASEDIR" ]; then
35 echo "Usage: $BASENAME <keyring> init <path> [url]"
36 exit 1
37 elif grep -q -e "^$KEYRING=" $CONFIG; then
38 echo "Keyring $KEYRING already defined"
39 exit 1
40 fi
41
42 # Check user configuration: git might complain if those aren't set
43 if [ -z "`git config --global --includes --get user.name`" ] || [ -z "`git config --global --includes --get user.email`" ]; then
44 echo "No git config found, so please chose a name and email address to identify your changes in the new keyring repository."
45 read -p "Enter your desired name/pseudonym: " name
46 read -p "Enter your desired email address: " email
47
48 if [ -z "$name" ] || [ -z "$email" ]; then
49 echo "Aborting."
50 exit 1
51 fi
52 fi
53
54 # Setup
55 if [ ! -z "$URL" ]; then
56 git clone "$URL" "$BASEDIR"
57 if [ "$?" != "0" ]; then
58 echo "Error cloning remote $URL"
59 exit 1
60 fi
61 else
62 if [ -e "$BASEDIR" ]; then
63 if [ ! -d "$BASEDIR/keys" ] || [ ! -e "$RECIPIENTS" ]; then
64 echo "Invalid keyring $BASEDIR: incomplete installation"
65
66 # A common mistake
67 if [ -d "$BASEDIR/../keys" ]; then
68 echo "You might try `cd $BASEDIR/.. && pwd` instead"
69 fi
70
71 exit 1
72 fi
73 else
74 # Setup folders
75 mkdir -p "$BASEDIR/"{config,keys}
76
77 # Setup recipients
78 keyringer_create_new_recipients "$RECIPIENTS/default"
79
80 # Setup options
81 touch "$OPTIONS"
82
83 # Setup README
84 echo "Keyring repository powered by https://keyringer.fluxo.info" > "$BASEDIR/README"
85 echo "" >> "$BASEDIR/README"
86
87 # Set config version
88 echo $CONFIG_VERSION > $VERSION_INFO
89 fi
90
91 # Secure
92 chmod 700 "$RECIPIENTS"
93 fi
94
95 # Reparse basedir to force absolute folder
96 BASEDIR="`cd $BASEDIR &> /dev/null && pwd`"
97
98 # Add entry
99 chmod 700 "$BASEDIR"
100 echo "$KEYRING=\"$BASEDIR\"" >> "$CONFIG"
101 touch $CONFIG_BASE/$KEYRING
102
103 # Init
104 if ! keyringer_is_git "$BASEDIR"; then
105 keyringer_exec git "$BASEDIR" init
106 keyringer_git_ignore 'tmp/*'
107
108 # Git configuration
109 if [ ! -z "$email" ]; then
110 git config user.email "$email"
111 git config user.name "$name"
112 fi
113
114 # Edit default recipients
115 if [ "$KEYRINGER_NON_INTERACTIVE" != "1" ]; then
116 echo "Now you have to edit the default recipient configuration to be able to encrypt secrets."
117 echo "Press any key to proceed editing..."
118 read key
119 keyringer_exec recipients "$BASEDIR" edit default
120 fi
121
122 # Stage and commit
123 keyringer_exec git "$BASEDIR" add .
124 keyringer_exec git "$BASEDIR" commit -m Initializing
125 fi
126 }
127
128 # Action dispatcher
129 function keyringer_dispatch {
130 BASEDIR="`keyringer_config $KEYRING`"
131
132 # Dispatch
133 if [ ! -z "$BASEDIR" ]; then
134 shift 2
135 keyringer_exec "$ACTION" "$BASEDIR" $*
136 exit $?
137 else
138 echo "No keydir configured for $KEYRING"
139 exit 1
140 fi
141 }
142
143 # Config
144 NAME="keyringer"
145 KEYRINGER_VERSION="0.6.0"
146 CONFIG_VERSION="0.1"
147 CONFIG_BASE="$HOME/.$NAME"
148 CONFIG="$CONFIG_BASE/config"
149 BASENAME="`basename $0`"
150 KEYRING="$1"
151 ACTION="$2"
152
153 # Turn off pathname expansion so expansion can work properly
154 set -f
155
156 # Set functions location
157 if [ -e "`dirname $(readlink -f $0)`/lib/$NAME/functions" ]; then
158 # Development or local installation layout
159 LIB="`dirname $(readlink -f $0)`/lib/$NAME/functions"
160 else
161 # System installation layout
162 LIB="`dirname $(readlink -f $0)`/../lib/$NAME/functions"
163 fi
164
165 # Set shared files location
166 if [ -e "`dirname $(readlink -f $0)`/share/$NAME" ]; then
167 # Development or local installation layout
168 SHARE="`dirname $(readlink -f $0)`/share/$NAME"
169 else
170 # System installation layout
171 SHARE="`dirname $(readlink -f $0)`/../share/$NAME"
172 fi
173
174 # Set actions location
175 if [ -e "`dirname $(readlink -f $0)`/lib/$NAME/actions" ]; then
176 # Development or local installation layout
177 ACTIONS="`dirname $(readlink -f $0)`/lib/$NAME/actions"
178 else
179 # System installation layout
180 ACTIONS="`dirname $(readlink -f $0)`/../lib/$NAME/actions"
181 fi
182
183 # Export globals for other scripts
184 export PREFERENCES="`dirname $CONFIG`/$KEYRING"
185 export KEYRINGER_VERSION
186 export CONFIG_VERSION
187 export KEYRING
188 export CONFIG
189 export SHARE
190
191 # Load functions
192 source "$LIB" || exit 1
193
194 # Basic checks
195 if [ -z "$KEYRING" ]; then
196 keyringer_usage
197 exit 1
198 elif [ ! -f "$CONFIG_BASE/$KEYRING" ] && [ "$ACTION" != "init" ]; then
199 echo "No such keyring $KEYRING"
200 exit 1
201 fi
202
203 # Setup main configuration and load preferences
204 keyringer_config_load
205
206 # Dispatch
207 if [ -z "$ACTION" ]; then
208 # Run shell if no action were given
209 keyringer $KEYRING shell
210 elif [ "$ACTION" == "init" ]; then
211 keyringer_init $*
212 elif keyringer_has_action "$ACTION"; then
213 keyringer_dispatch $*
214 else
215 echo "No such action $ACTION"
216 exit 1
217 fi