Merge commit 'mlcastle/master'
[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-authorized_keys (a)        update authorized_keys file
39   gen-subkey (g) KEYID              generate an 'a' capable subkey
40   help (h,?)                        this help
41
42 EOF
43 }
44
45 # generate a subkey with the 'a' usage flags set
46 # FIXME: this needs some tweaking to clean it up
47 gen_subkey(){
48     local keyID
49     local gpgOut
50     local userID
51
52     keyID="$1"
53
54     gpgOut=$(gpg --quiet --fixed-list-mode --list-keys --with-colons \
55         "$keyID" 2> /dev/null)
56
57     # return 1 if there only "tru" lines are output from gpg
58     if [ -z "$(echo "$gpgOut" | grep -v '^tru:')" ] ; then
59         failure "Key ID '$keyID' not found."
60     fi
61
62     # set subkey defaults
63     SUBKEY_TYPE=${SUBKEY_TYPE:-"RSA"}
64     #SUBKEY_LENGTH=${SUBKEY_LENGTH:-"2048"}
65     SUBKEY_USAGE=${SUBKEY_USAGE:-"auth"}
66     SUBKEY_EXPIRE=${SUBKEY_EXPIRE:-"0"}
67     cat <<EOF
68 Please specify how long the key should be valid.
69          0 = key does not expire
70       <n>  = key expires in n days
71       <n>w = key expires in n weeks
72       <n>m = key expires in n months
73       <n>y = key expires in n years
74 EOF
75     read -p "Key is valid for? ($SUBKEY_EXPIRE) " SUBKEY_EXPIRE; SUBKEY_EXPIRE=${SUBKEY_EXPIRE:-"0"}
76
77     # generate the list of commands that will be passed to edit-key
78     editCommands=$(cat <<EOF
79 addkey
80 7
81 S
82 E
83 A
84 Q
85 $SUBKEY_LENGTH
86 $SUBKEY_EXPIRE
87 save
88 EOF
89 )
90
91     log "generating subkey..."
92     echo "$editCommands" | gpg --expert --command-fd 0 --edit-key "$keyID"
93     log "done."
94 }
95
96 ########################################################################
97 # MAIN
98 ########################################################################
99
100 COMMAND="$1"
101 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
102 shift
103
104 # set ms home directory
105 MS_HOME=${MS_HOME:-"${HOME}/.config/monkeysphere"}
106
107 # load configuration file
108 MS_CONF=${MS_CONF:-"${MS_HOME}/monkeysphere.conf"}
109 [ -e "$MS_CONF" ] && . "$MS_CONF"
110
111 # set empty config variable with defaults
112 AUTHORIZED_USER_IDS=${AUTHORIZED_USER_IDS:-"${MS_HOME}/authorized_user_ids"}
113 GNUPGHOME=${GNUPGHOME:-"${HOME}/.gnupg"}
114 KEYSERVER=${KEYSERVER:-"subkeys.pgp.net"}
115 CHECK_KEYSERVER=${CHECK_KEYSERVER:="true"}
116 REQUIRED_HOST_KEY_CAPABILITY=${REQUIRED_HOST_KEY_CAPABILITY:-"a"}
117 REQUIRED_USER_KEY_CAPABILITY=${REQUIRED_USER_KEY_CAPABILITY:-"a"}
118 KNOWN_HOSTS=${KNOWN_HOSTS:-"${HOME}/.ssh/known_hosts"}
119 AUTHORIZED_KEYS=${AUTHORIZED_KEYS:-"${HOME}/.ssh/authorized_keys"}
120 HASH_KNOWN_HOSTS=${HASH_KNOWN_HOSTS:-"true"}
121
122 export GNUPGHOME
123
124 # make sure gpg home exists with proper permissions
125 mkdir -p -m 0700 "$GNUPGHOME"
126
127 # make sure the user monkeysphere home directory exists
128 mkdir -p -m 0700 "$MS_HOME"
129 touch "$AUTHORIZED_USER_IDS"
130 touch "$AUTHORIZED_KEYS"
131
132 case $COMMAND in
133     'update-known_hosts'|'update-known-hosts'|'k')
134         MODE='known_hosts'
135
136         # touch the known_hosts file to make sure it exists
137         # ssh-keygen complains if it doesn't exist
138         touch "$KNOWN_HOSTS"
139
140         # if hosts are specified on the command line, process just
141         # those hosts
142         if [ "$1" ] ; then
143             process_hosts_known_hosts "$@"
144
145         # otherwise, if no hosts are specified, process every host
146         # in the user's known_hosts file
147         else
148             if [ ! -s "$KNOWN_HOSTS" ] ; then
149                 failure "known_hosts file '$KNOWN_HOSTS' is empty."
150             fi
151             log "processing known_hosts file..."
152             process_known_hosts
153         fi
154
155         log "known_hosts file updated."
156         ;;
157
158     'update-authorized_keys'|'update-authorized-keys'|'a')
159         MODE='authorized_keys'
160
161         # fail if the authorized_user_ids file is empty
162         if [ ! -s "$AUTHORIZED_USER_IDS" ] ; then
163             failure "$AUTHORIZED_USER_IDS is empty."
164         fi
165
166         # process authorized_user_ids file
167         log "processing authorized_user_ids file..."
168         process_authorized_user_ids "$AUTHORIZED_USER_IDS"
169         log "authorized_keys file updated."
170         ;;
171
172     'gen-subkey'|'g')
173         keyID="$1"
174         if [ -z "$keyID" ] ; then
175             failure "You must specify the key ID of your primary key."
176         fi
177         gen_subkey "$keyID"
178         ;;
179
180     'help'|'h'|'?')
181         usage
182         ;;
183
184     *)
185         failure "Unknown command: '$COMMAND'
186 Type '$PGRM help' for usage."
187         ;;
188 esac