Modify how logging is handled. Now send most everything to stderr.
[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 userid
39   update-authorized_keys (a)        update authorized_keys file
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         log "  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     echo "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:-"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
140 case $COMMAND in
141     'update-known_hosts'|'update-known-hosts'|'k')
142         MODE='known_hosts'
143
144         # touch the known_hosts file to make sure it exists
145         # ssh-keygen complains if it doesn't exist
146         touch "$USER_KNOWN_HOSTS"
147
148         # if hosts are specified on the command line, process just
149         # those hosts
150         if [ "$1" ] ; then
151             for host ; do
152                 process_host "$host" "$hostKeysCacheDir"
153             done
154
155         # otherwise, if no hosts are specified, process every user
156         # in the user's known_hosts file
157         else
158             if [ ! -s "$USER_KNOWN_HOSTS" ] ; then
159                 failure "known_hosts file '$USER_KNOWN_HOSTS' is empty."
160             fi
161             log "processing known_hosts file..."
162             process_known_hosts "$USER_KNOWN_HOSTS" "$hostKeysCacheDir"
163         fi
164         ;;
165
166     'update-authorized_keys'|'update-authorized-keys'|'a')
167         MODE='authorized_keys'
168
169         # make sure authorized_user_ids file exists
170         if [ ! -s "$AUTHORIZED_USER_IDS" ] ; then
171             failure "authorized_user_ids file is empty or does not exist."
172         fi
173
174         # set user-controlled authorized_keys file path
175         userAuthorizedKeys=${USER_CONTROLLED_AUTHORIZED_KEYS/\%h/"$HOME"}
176
177         # update authorized_keys
178         update_authorized_keys "$msAuthorizedKeys" "$userAuthorizedKeys" "$userKeysCacheDir"
179         ;;
180
181     'update-userids'|'u')
182         if [ -z "$1" ] ; then
183             failure "you must specify at least one userid."
184         fi
185         for userID ; do
186             update_userid "$userID" "$userKeysCacheDir"
187         done
188         ;;
189
190     'gen-ae-subkey'|'g')
191         keyID="$1"
192         if [ -z "$keyID" ] ; then
193             failure "you must specify keyid of primary key."
194         fi
195         gen_ae_subkey "$keyID"
196         ;;
197
198     'help'|'h'|'?')
199         usage
200         ;;
201
202     *)
203         failure "Unknown command: '$COMMAND'
204 Type 'cereal-admin help' for usage."
205         ;;
206 esac