Merge commit 'dkg/master'
[monkeysphere.git] / src / monkeysphere
1 #!/bin/sh
2
3 # monkeysphere: MonkeySphere client tool
4 #
5 # The monkeysphere scripts are written by:
6 # Jameson Rollins <jrollins@fifthhorseman.net>
7 #
8 # They are Copyright 2008, and are all released under the GPL, version 3
9 # or later.
10
11 ########################################################################
12 PGRM=$(basename $0)
13
14 SHAREDIR=${SHAREDIR:-"/usr/share/monkeysphere"}
15 export SHAREDIR
16 . "${SHAREDIR}/common"
17
18 GLOBAL_CONFIG=${GLOBAL_CONFIG:-"${ETC}/monkeysphere.conf"}
19 [ -r "$GLOBAL_CONFIG" ] && . "$GLOBAL_CONFIG"
20
21 # date in UTF format if needed
22 DATE=$(date -u '+%FT%T')
23
24 # unset some environment variables that could screw things up
25 GREP_OPTIONS=
26
27 ########################################################################
28 # FUNCTIONS
29 ########################################################################
30
31 usage() {
32 cat <<EOF
33 usage: $PGRM <subcommand> [args]
34 MonkeySphere client tool.
35
36 subcommands:
37   update-known_hosts (k) [HOST]...  update known_hosts file
38   update-userids (u) [USERID]...    add/update user IDs
39   remove-userids (r) [USERID]...    remove user IDs
40   update-authorized_keys (a)        update authorized_keys file
41   gen-ae-subkey (g) KEYID           generate an 'ae' capable subkey
42   help (h,?)                        this help
43
44 EOF
45 }
46
47 # generate a subkey with the 'a' and 'e' usage flags set
48 gen_ae_subkey(){
49     local keyID
50     local gpgOut
51     local userID
52
53     log "warning: this function is still not working."
54
55     keyID="$1"
56
57     # set subkey defaults
58     SUBKEY_TYPE=${KEY_TYPE:-"RSA"}
59     SUBKEY_LENGTH=${KEY_LENGTH:-"1024"}
60     SUBKEY_USAGE=${KEY_USAGE:-"encrypt,auth"}
61
62     gpgOut=$(gpg --fixed-list-mode --list-keys --with-colons \
63         "$keyID" 2> /dev/null)
64
65     # return 1 if there only "tru" lines are output from gpg
66     if [ -z "$(echo "$gpgOut" | grep -v '^tru:')" ] ; then
67         log "  key not found."
68         return 1
69     fi
70
71     userID=$(echo "$gpgOut" | grep "^uid:" | cut -d: -f10)
72
73     # set key parameters
74     keyParameters=$(cat <<EOF
75 Subkey-Type: $SUBKEY_TYPE
76 Subkey-Length: $SUBKEY_LENGTH
77 Subkey-Usage: $SUBKEY_USAGE
78 Name-Real: $userID
79 EOF
80 )
81
82     echo "The following key parameters will be used:"
83     echo "$keyParameters"
84
85     read -p "generate key? [Y|n]: " OK; OK=${OK:=Y}
86     if [ ${OK/y/Y} != 'Y' ] ; then
87         failure "aborting."
88     fi
89
90     # add commit command
91     keyParameters="${keyParameters}"$(cat <<EOF
92
93 %commit
94 %echo done
95 EOF
96 )
97
98     echo "generating subkey..."
99     echo "$keyParameters" | gpg --batch --gen-key
100 }
101
102 ########################################################################
103 # MAIN
104 ########################################################################
105
106 COMMAND="$1"
107 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
108 shift
109
110 # set ms home directory
111 MS_HOME=${MS_HOME:-"${HOME}/.config/monkeysphere"}
112
113 # load configuration file
114 MS_CONF=${MS_CONF:-"${MS_HOME}/monkeysphere.conf"}
115 [ -e "$MS_CONF" ] && . "$MS_CONF"
116
117 # set empty config variable with defaults
118 AUTHORIZED_USER_IDS=${AUTHORIZED_USER_IDS:-"${MS_HOME}/authorized_user_ids"}
119 GNUPGHOME=${GNUPGHOME:-"${HOME}/.gnupg"}
120 KEYSERVER=${KEYSERVER:-"subkeys.pgp.net"}
121 REQUIRED_KEY_CAPABILITY=${REQUIRED_KEY_CAPABILITY:-"e a"}
122 USER_CONTROLLED_AUTHORIZED_KEYS=${USER_CONTROLLED_AUTHORIZED_KEYS:-"%h/.ssh/authorized_keys"}
123 USER_KNOWN_HOSTS=${USER_KNOWN_HOSTS:-"${HOME}/.ssh/known_hosts"}
124 HASH_KNOWN_HOSTS=${HASH_KNOWN_HOSTS:-"true"}
125
126 export GNUPGHOME
127
128 # stagging locations
129 hostKeysCacheDir="${MS_HOME}/host_keys"
130 userKeysCacheDir="${MS_HOME}/user_keys"
131 msAuthorizedKeys="${MS_HOME}/authorized_keys"
132
133 # make sure gpg home exists with proper permissions
134 mkdir -p -m 0700 "$GNUPGHOME"
135
136 # make sure the user monkeysphere home directory exists
137 mkdir -p -m 0700 "$MS_HOME"
138 mkdir -p "$hostKeysCacheDir"
139 mkdir -p "$userKeysCacheDir"
140 touch "$AUTHORIZED_USER_IDS"
141
142 case $COMMAND in
143     'update-known_hosts'|'update-known-hosts'|'k')
144         MODE='known_hosts'
145
146         # touch the known_hosts file to make sure it exists
147         # ssh-keygen complains if it doesn't exist
148         touch "$USER_KNOWN_HOSTS"
149
150         # if hosts are specified on the command line, process just
151         # those hosts
152         if [ "$1" ] ; then
153             for host ; do
154                 process_host "$host" "$hostKeysCacheDir"
155             done
156
157         # otherwise, if no hosts are specified, process every user
158         # in the user's known_hosts file
159         else
160             if [ ! -s "$USER_KNOWN_HOSTS" ] ; then
161                 failure "known_hosts file '$USER_KNOWN_HOSTS' is empty."
162             fi
163             log "processing known_hosts file..."
164             process_known_hosts "$USER_KNOWN_HOSTS" "$hostKeysCacheDir"
165         fi
166         ;;
167
168     'update-userids'|'update-userid'|'u')
169         if [ -z "$1" ] ; then
170             failure "you must specify at least one userid."
171         fi
172         for userID ; do
173             update_userid "$userID" "$userKeysCacheDir"
174         done
175         log "run the following to update your monkeysphere authorized_keys file:"
176         log "$PGRM update-authorized_keys"
177         ;;
178
179     'remove-userids'|'remove-userid'|'r')
180         if [ -z "$1" ] ; then
181             failure "you must specify at least one userid."
182         fi
183         for userID ; do
184             remove_userid "$userID"
185         done
186         log "run the following to update your monkeysphere authorized_keys file:"
187         log "$PGRM update-authorized_keys"
188         ;;
189
190     'update-authorized_keys'|'update-authorized-keys'|'a')
191         MODE='authorized_keys'
192
193         # fail if the authorized_user_ids file is empty
194         if [ ! -s "$AUTHORIZED_USER_IDS" ] ; then
195             failure "$AUTHORIZED_USER_IDS is empty."
196         fi
197
198         # set user-controlled authorized_keys file path
199         userAuthorizedKeys=${USER_CONTROLLED_AUTHORIZED_KEYS/\%h/"$HOME"}
200
201         # update authorized_keys
202         update_authorized_keys "$msAuthorizedKeys" "$userAuthorizedKeys" "$userKeysCacheDir"
203         ;;
204
205     'gen-ae-subkey'|'g')
206         keyID="$1"
207         if [ -z "$keyID" ] ; then
208             failure "you must specify keyid of primary key."
209         fi
210         gen_ae_subkey "$keyID"
211         ;;
212
213     'help'|'h'|'?')
214         usage
215         ;;
216
217     *)
218         failure "Unknown command: '$COMMAND'
219 Type '$PGRM help' for usage."
220         ;;
221 esac