Cleaned/fix up update-userid function. also some general cleanup.
[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)                 generate an 'ae' capable subkey
41   help (h,?)                        this help
42
43 EOF
44 }
45
46 ########################################################################
47 # MAIN
48 ########################################################################
49
50 COMMAND="$1"
51 [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
52 shift
53
54 # set ms home directory
55 MS_HOME=${MS_HOME:-"$HOME"/.config/monkeysphere}
56
57 # load configuration file
58 MS_CONF=${MS_CONF:-"$MS_HOME"/monkeysphere.conf}
59 [ -e "$MS_CONF" ] && . "$MS_CONF"
60
61 # set empty config variable with defaults
62 AUTHORIZED_USER_IDS=${AUTHORIZED_USER_IDS:-"$MS_HOME"/authorized_user_ids}
63 GNUPGHOME=${GNUPGHOME:-"$HOME"/.gnupg}
64 KEYSERVER=${KEYSERVER:-subkeys.pgp.net}
65 REQUIRED_KEY_CAPABILITY=${REQUIRED_KEY_CAPABILITY:-"e a"}
66 USER_CONTROLLED_AUTHORIZED_KEYS=${USER_CONTROLLED_AUTHORIZED_KEYS:-%h/.ssh/authorized_keys}
67 USER_KNOWN_HOSTS=${USER_KNOWN_HOSTS:-"$HOME"/.ssh/known_hosts}
68 HASH_KNOWN_HOSTS=${HASH_KNOWN_HOSTS:-}
69
70 export GNUPGHOME
71
72 # stagging locations
73 hostKeysCacheDir="$MS_HOME"/host_keys
74 userKeysCacheDir="$MS_HOME"/user_keys
75 msAuthorizedKeys="$MS_HOME"/authorized_keys
76
77 # make sure gpg home exists with proper permissions
78 mkdir -p -m 0700 "$GNUPGHOME"
79
80 case $COMMAND in
81     'update-known-hosts'|'k')
82         MODE='known_hosts'
83
84         # touch the known_hosts file to make sure it exists
85         touch "$USER_KNOWN_HOSTS"
86
87         # if hosts are specified on the command line, process just
88         # those hosts
89         if [ "$1" ] ; then
90             for host ; do
91                 process_host "$host" "$hostKeysCacheDir"
92             done
93
94         # otherwise, if no hosts are specified, process the user
95         # known_hosts file
96         else
97             if [ ! -s "$USER_KNOWN_HOSTS" ] ; then
98                 failure "known_hosts file '$USER_KNOWN_HOSTS' is empty."
99             fi
100             log "processing known_hosts file..."
101             process_known_hosts "$USER_KNOWN_HOSTS" "$hostKeysCacheDir"
102         fi
103         ;;
104
105     'update-authorized-keys'|'a')
106         MODE='authorized_keys'
107
108         log "processing authorized_user_ids file..."
109
110         # make sure authorized_user_ids file exists
111         if [ ! -s "$AUTHORIZED_USER_IDS" ] ; then
112             log "authorized_user_ids file is empty or does not exist."
113             exit
114         fi
115
116         process_authorized_ids "$AUTHORIZED_USER_IDS" "$userKeysCacheDir"
117
118         # write output key file
119         log "writing monkeysphere authorized_keys file... "
120         touch "$msAuthorizedKeys"
121         if [ "$(ls "$userKeysCacheDir")" ] ; then
122             log -n "adding gpg keys... "
123             cat "$userKeysCacheDir"/* > "$msAuthorizedKeys"
124             echo "done."
125         else
126         log "no gpg keys to add."
127         fi
128         if [ "$USER_CONTROLLED_AUTHORIZED_KEYS" ] ; then
129             userAuthorizedKeys=${USER_CONTROLLED_AUTHORIZED_KEYS/\%h/"$HOME"}
130             if [ -s "$userAuthorizedKeys" ] ; then
131                 log -n "adding user authorized_keys file... "
132                 cat "$userAuthorizedKeys" >> "$msAuthorizedKeys"
133                 echo "done."
134             fi
135         fi
136         log "monkeysphere authorized_keys file generated:"
137         log "$msAuthorizedKeys"
138         ;;
139
140     'update-userids'|'u')
141         if [ -z "$1" ] ; then
142             failure "you must specify at least one userid."
143         fi
144         for userID ; do
145             update_userid "$userID" "$userKeysCacheDir"
146         done
147         ;;
148
149     'gen-ae-subkey'|)
150         failure "function not implemented yet."
151         ;;
152
153     'help'|'h'|'?')
154         usage
155         ;;
156
157     *)
158         failure "Unknown command: '$COMMAND'
159 Type 'cereal-admin help' for usage."
160         ;;
161 esac