Fix gen-subkey function for client.
[monkeysphere.git] / src / monkeysphere
1 #!/bin/bash
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-subkey (g) KEYID              generate an 'a' capable subkey
42   help (h,?)                        this help
43
44 EOF
45 }
46
47 # generate a subkey with the 'a' usage flags set
48 # FIXME: not working yet.
49 gen_subkey(){
50     local keyID
51     local gpgOut
52     local userID
53
54     log "warning: this function is still not working."
55
56     keyID="$1"
57
58     gpgOut=$(gpg --fixed-list-mode --list-keys --with-colons \
59         "$keyID" 2> /dev/null)
60
61     # return 1 if there only "tru" lines are output from gpg
62     if [ -z "$(echo "$gpgOut" | grep -v '^tru:')" ] ; then
63         log "  key not found."
64         return 1
65     fi
66
67     # set subkey defaults
68     SUBKEY_TYPE=${SUBKEY_TYPE:-"RSA"}
69     #SUBKEY_LENGTH=${SUBKEY_LENGTH:-"2048"}
70     SUBKEY_USAGE=${SUBKEY_USAGE:-"auth"}
71     SUBKEY_EXPIRE=${SUBKEY_EXPIRE:-"0"}
72     cat <<EOF
73 Please specify how long the key should be valid.
74          0 = key does not expire
75       <n>  = key expires in n days
76       <n>w = key expires in n weeks
77       <n>m = key expires in n months
78       <n>y = key expires in n years
79 EOF
80     read -p "Key is valid for? ($SUBKEY_EXPIRE) " SUBKEY_EXPIRE; SUBKEY_EXPIRE=${SUBKEY_EXPIRE:-"0"}
81
82     # generate the list of commands that will be passed to edit-key
83     editCommands=$(cat <<EOF
84 addkey
85 7
86 S
87 E
88 A
89 Q
90 $SUBKEY_LENGTH
91 $SUBKEY_EXPIRE
92 save
93 EOF
94 )
95
96     echo "generating subkey..."
97     echo "$editCommands" | gpg --expert --command-fd 0 --edit-key "$keyID"
98 }
99
100 ########################################################################
101 # MAIN
102 ########################################################################
103
104 COMMAND="$1"
105 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
106 shift
107
108 # set ms home directory
109 MS_HOME=${MS_HOME:-"${HOME}/.config/monkeysphere"}
110
111 # load configuration file
112 MS_CONF=${MS_CONF:-"${MS_HOME}/monkeysphere.conf"}
113 [ -e "$MS_CONF" ] && . "$MS_CONF"
114
115 # set empty config variable with defaults
116 AUTHORIZED_USER_IDS=${AUTHORIZED_USER_IDS:-"${MS_HOME}/authorized_user_ids"}
117 GNUPGHOME=${GNUPGHOME:-"${HOME}/.gnupg"}
118 KEYSERVER=${KEYSERVER:-"subkeys.pgp.net"}
119 REQUIRED_HOST_KEY_CAPABILITY=${REQUIRED_HOST_KEY_CAPABILITY:-"e a"}
120 REQUIRED_USER_KEY_CAPABILITY=${REQUIRED_USER_KEY_CAPABILITY:-"a"}
121 USER_CONTROLLED_AUTHORIZED_KEYS=${USER_CONTROLLED_AUTHORIZED_KEYS:-"%h/.ssh/authorized_keys"}
122 USER_KNOWN_HOSTS=${USER_KNOWN_HOSTS:-"${HOME}/.ssh/known_hosts"}
123 HASH_KNOWN_HOSTS=${HASH_KNOWN_HOSTS:-"true"}
124
125 export GNUPGHOME
126
127 # stagging locations
128 hostKeysCacheDir="${MS_HOME}/host_keys"
129 userKeysCacheDir="${MS_HOME}/user_keys"
130 msAuthorizedKeys="${MS_HOME}/authorized_keys"
131
132 # make sure gpg home exists with proper permissions
133 mkdir -p -m 0700 "$GNUPGHOME"
134
135 # make sure the user monkeysphere home directory exists
136 mkdir -p -m 0700 "$MS_HOME"
137 mkdir -p "$hostKeysCacheDir"
138 mkdir -p "$userKeysCacheDir"
139 touch "$AUTHORIZED_USER_IDS"
140
141 case $COMMAND in
142     'update-known_hosts'|'update-known-hosts'|'k')
143         MODE='known_hosts'
144
145         # touch the known_hosts file to make sure it exists
146         # ssh-keygen complains if it doesn't exist
147         touch "$USER_KNOWN_HOSTS"
148
149         # if hosts are specified on the command line, process just
150         # those hosts
151         if [ "$1" ] ; then
152             for host ; do
153                 process_host "$host" "$hostKeysCacheDir"
154             done
155
156         # otherwise, if no hosts are specified, process every user
157         # in the user's known_hosts file
158         else
159             if [ ! -s "$USER_KNOWN_HOSTS" ] ; then
160                 failure "known_hosts file '$USER_KNOWN_HOSTS' is empty."
161             fi
162             log "processing known_hosts file..."
163             process_known_hosts "$USER_KNOWN_HOSTS" "$hostKeysCacheDir"
164         fi
165         ;;
166
167     'update-userids'|'update-userid'|'u')
168         if [ -z "$1" ] ; then
169             failure "you must specify at least one userid."
170         fi
171         for userID ; do
172             update_userid "$userID" "$userKeysCacheDir"
173         done
174         log "run the following to update your monkeysphere authorized_keys file:"
175         log "$PGRM update-authorized_keys"
176         ;;
177
178     'remove-userids'|'remove-userid'|'r')
179         if [ -z "$1" ] ; then
180             failure "you must specify at least one userid."
181         fi
182         for userID ; do
183             remove_userid "$userID"
184         done
185         log "run the following to update your monkeysphere authorized_keys file:"
186         log "$PGRM update-authorized_keys"
187         ;;
188
189     'update-authorized_keys'|'update-authorized-keys'|'a')
190         MODE='authorized_keys'
191
192         # fail if the authorized_user_ids file is empty
193         if [ ! -s "$AUTHORIZED_USER_IDS" ] ; then
194             failure "$AUTHORIZED_USER_IDS is empty."
195         fi
196
197         # set user-controlled authorized_keys file path
198         userAuthorizedKeys=${USER_CONTROLLED_AUTHORIZED_KEYS/\%h/"$HOME"}
199
200         # update authorized_keys
201         update_authorized_keys "$msAuthorizedKeys" "$userAuthorizedKeys" "$userKeysCacheDir"
202         ;;
203
204     'gen-subkey'|'g')
205         keyID="$1"
206         if [ -z "$keyID" ] ; then
207             failure "you must specify keyid of primary key."
208         fi
209         gen_subkey "$keyID"
210         ;;
211
212     'help'|'h'|'?')
213         usage
214         ;;
215
216     *)
217         failure "Unknown command: '$COMMAND'
218 Type '$PGRM help' for usage."
219         ;;
220 esac