Merge commit 'jrollins/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-authorized_keys (a)        update authorized_keys file
39   update-userids (u) [USERID]...    add/update userid
40   gen-ae-subkey (g) KEYID           generate an 'ae' capable subkey
41   help (h,?)                        this help
42
43 EOF
44 }
45
46 # generate a subkey with the 'a' and 'e' usage flags set
47 gen_ae_subkey(){
48     local keyID
49     local gpgOut
50     local userID
51
52     log "warning: this function is still not working."
53
54     keyID="$1"
55
56     # set subkey defaults
57     SUBKEY_TYPE=${KEY_TYPE:-"RSA"}
58     SUBKEY_LENGTH=${KEY_LENGTH:-"1024"}
59     SUBKEY_USAGE=${KEY_USAGE:-"encrypt,auth"}
60
61     gpgOut=$(gpg --fixed-list-mode --list-keys --with-colons \
62         "$keyID" 2> /dev/null)
63
64     # return 1 if there only "tru" lines are output from gpg
65     if [ -z "$(echo "$gpgOut" | grep -v '^tru:')" ] ; then
66         loge "  key not found."
67         return 1
68     fi
69
70     userID=$(echo "$gpgOut" | grep "^uid:" | cut -d: -f10)
71
72     # set key parameters
73     keyParameters=$(cat <<EOF
74 Subkey-Type: $SUBKEY_TYPE
75 Subkey-Length: $SUBKEY_LENGTH
76 Subkey-Usage: $SUBKEY_USAGE
77 Name-Real: $userID
78 EOF
79 )
80
81     log "The following key parameters will be used:"
82     echo "$keyParameters"
83
84     read -p "generate key? [Y|n]: " OK; OK=${OK:=Y}
85     if [ ${OK/y/Y} != 'Y' ] ; then
86         failure "aborting."
87     fi
88
89     # add commit command
90     keyParameters="${keyParameters}"$(cat <<EOF
91
92 %commit
93 %echo done
94 EOF
95 )
96
97     echo "generating subkey..."
98     echo "$keyParameters" | gpg --batch --gen-key
99 }
100
101 ########################################################################
102 # MAIN
103 ########################################################################
104
105 COMMAND="$1"
106 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
107 shift
108
109 # set ms home directory
110 MS_HOME=${MS_HOME:-"$HOME"/.config/monkeysphere}
111
112 # load configuration file
113 MS_CONF=${MS_CONF:-"$MS_HOME"/monkeysphere.conf}
114 [ -e "$MS_CONF" ] && . "$MS_CONF"
115
116 # set empty config variable with defaults
117 AUTHORIZED_USER_IDS=${AUTHORIZED_USER_IDS:-"$MS_HOME"/authorized_user_ids}
118 GNUPGHOME=${GNUPGHOME:-"$HOME"/.gnupg}
119 KEYSERVER=${KEYSERVER:-subkeys.pgp.net}
120 REQUIRED_KEY_CAPABILITY=${REQUIRED_KEY_CAPABILITY:-"e 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:-}
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 case $COMMAND in
136     'update-known_hosts'|'update-known-hosts'|'k')
137         MODE='known_hosts'
138
139         # touch the known_hosts file to make sure it exists
140         touch "$USER_KNOWN_HOSTS"
141
142         # if hosts are specified on the command line, process just
143         # those hosts
144         if [ "$1" ] ; then
145             for host ; do
146                 process_host "$host" "$hostKeysCacheDir"
147             done
148
149         # otherwise, if no hosts are specified, process the user
150         # known_hosts file
151         else
152             if [ ! -s "$USER_KNOWN_HOSTS" ] ; then
153                 failure "known_hosts file '$USER_KNOWN_HOSTS' is empty."
154             fi
155             log "processing known_hosts file..."
156             process_known_hosts "$USER_KNOWN_HOSTS" "$hostKeysCacheDir"
157         fi
158         ;;
159
160     'update-authorized_keys'|'update-authorized-keys'|'a')
161         MODE='authorized_keys'
162
163         # make sure authorized_user_ids file exists
164         if [ ! -s "$AUTHORIZED_USER_IDS" ] ; then
165             log "authorized_user_ids file is empty or does not exist."
166             exit
167         fi
168
169         # set user-controlled authorized_keys file path
170         userAuthorizedKeys=${USER_CONTROLLED_AUTHORIZED_KEYS/\%h/"$HOME"}
171
172         # update authorized_keys
173         update_authorized_keys "$userKeysCacheDir" "$msAuthorizedKeys" "$userAuthorizedKeys"
174         ;;
175
176     'update-userids'|'u')
177         if [ -z "$1" ] ; then
178             failure "you must specify at least one userid."
179         fi
180         for userID ; do
181             update_userid "$userID" "$userKeysCacheDir"
182         done
183         ;;
184
185     'gen-ae-subkey'|'g')
186         keyID="$1"
187         if [ -z "$keyID" ] ; then
188             failure "you must specify keyid of primary key."
189         fi
190         gen_ae_subkey "$keyID"
191         ;;
192
193     'help'|'h'|'?')
194         usage
195         ;;
196
197     *)
198         failure "Unknown command: '$COMMAND'
199 Type 'cereal-admin help' for usage."
200         ;;
201 esac