summaryrefslogtreecommitdiff
path: root/src/monkeysphere
blob: 91401b93a0c324c0b34b80768ae788542f377ac6 (plain)
  1. #!/bin/bash
  2. # monkeysphere: MonkeySphere client tool
  3. #
  4. # The monkeysphere scripts are written by:
  5. # Jameson Rollins <jrollins@fifthhorseman.net>
  6. #
  7. # They are Copyright 2008, and are all released under the GPL, version 3
  8. # or later.
  9. ########################################################################
  10. PGRM=$(basename $0)
  11. SHAREDIR=${SHAREDIR:-"/usr/share/monkeysphere"}
  12. export SHAREDIR
  13. . "${SHAREDIR}/common"
  14. GLOBAL_CONFIG=${GLOBAL_CONFIG:-"${ETC}/monkeysphere.conf"}
  15. [ -r "$GLOBAL_CONFIG" ] && . "$GLOBAL_CONFIG"
  16. # date in UTF format if needed
  17. DATE=$(date -u '+%FT%T')
  18. # unset some environment variables that could screw things up
  19. GREP_OPTIONS=
  20. ########################################################################
  21. # FUNCTIONS
  22. ########################################################################
  23. usage() {
  24. cat <<EOF
  25. usage: $PGRM <subcommand> [args]
  26. MonkeySphere client tool.
  27. subcommands:
  28. update-known_hosts (k) [HOST]... update known_hosts file
  29. update-userids (u) [USERID]... add/update user IDs
  30. remove-userids (r) [USERID]... remove user IDs
  31. update-authorized_keys (a) update authorized_keys file
  32. gen-subkey (g) KEYID generate an 'a' capable subkey
  33. help (h,?) this help
  34. EOF
  35. }
  36. # generate a subkey with the 'a' usage flags set
  37. # FIXME: this needs some tweaking to clean it up
  38. gen_subkey(){
  39. local keyID
  40. local gpgOut
  41. local userID
  42. keyID="$1"
  43. gpgOut=$(gpg --fixed-list-mode --list-keys --with-colons \
  44. "$keyID" 2> /dev/null)
  45. # return 1 if there only "tru" lines are output from gpg
  46. if [ -z "$(echo "$gpgOut" | grep -v '^tru:')" ] ; then
  47. failure "Key ID '$keyID' not found."
  48. fi
  49. # set subkey defaults
  50. SUBKEY_TYPE=${SUBKEY_TYPE:-"RSA"}
  51. #SUBKEY_LENGTH=${SUBKEY_LENGTH:-"2048"}
  52. SUBKEY_USAGE=${SUBKEY_USAGE:-"auth"}
  53. SUBKEY_EXPIRE=${SUBKEY_EXPIRE:-"0"}
  54. cat <<EOF
  55. Please specify how long the key should be valid.
  56. 0 = key does not expire
  57. <n> = key expires in n days
  58. <n>w = key expires in n weeks
  59. <n>m = key expires in n months
  60. <n>y = key expires in n years
  61. EOF
  62. read -p "Key is valid for? ($SUBKEY_EXPIRE) " SUBKEY_EXPIRE; SUBKEY_EXPIRE=${SUBKEY_EXPIRE:-"0"}
  63. # generate the list of commands that will be passed to edit-key
  64. editCommands=$(cat <<EOF
  65. addkey
  66. 7
  67. S
  68. E
  69. A
  70. Q
  71. $SUBKEY_LENGTH
  72. $SUBKEY_EXPIRE
  73. save
  74. EOF
  75. )
  76. echo "generating subkey..."
  77. echo "$editCommands" | gpg --expert --command-fd 0 --edit-key "$keyID"
  78. }
  79. ########################################################################
  80. # MAIN
  81. ########################################################################
  82. COMMAND="$1"
  83. [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
  84. shift
  85. # set ms home directory
  86. MS_HOME=${MS_HOME:-"${HOME}/.config/monkeysphere"}
  87. # load configuration file
  88. MS_CONF=${MS_CONF:-"${MS_HOME}/monkeysphere.conf"}
  89. [ -e "$MS_CONF" ] && . "$MS_CONF"
  90. # set empty config variable with defaults
  91. AUTHORIZED_USER_IDS=${AUTHORIZED_USER_IDS:-"${MS_HOME}/authorized_user_ids"}
  92. GNUPGHOME=${GNUPGHOME:-"${HOME}/.gnupg"}
  93. KEYSERVER=${KEYSERVER:-"subkeys.pgp.net"}
  94. REQUIRED_HOST_KEY_CAPABILITY=${REQUIRED_HOST_KEY_CAPABILITY:-"e a"}
  95. REQUIRED_USER_KEY_CAPABILITY=${REQUIRED_USER_KEY_CAPABILITY:-"a"}
  96. USER_CONTROLLED_AUTHORIZED_KEYS=${USER_CONTROLLED_AUTHORIZED_KEYS:-"%h/.ssh/authorized_keys"}
  97. USER_KNOWN_HOSTS=${USER_KNOWN_HOSTS:-"${HOME}/.ssh/known_hosts"}
  98. HASH_KNOWN_HOSTS=${HASH_KNOWN_HOSTS:-"true"}
  99. export GNUPGHOME
  100. # stagging locations
  101. hostKeysCacheDir="${MS_HOME}/host_keys"
  102. userKeysCacheDir="${MS_HOME}/user_keys"
  103. msAuthorizedKeys="${MS_HOME}/authorized_keys"
  104. # make sure gpg home exists with proper permissions
  105. mkdir -p -m 0700 "$GNUPGHOME"
  106. # make sure the user monkeysphere home directory exists
  107. mkdir -p -m 0700 "$MS_HOME"
  108. mkdir -p "$hostKeysCacheDir"
  109. mkdir -p "$userKeysCacheDir"
  110. touch "$AUTHORIZED_USER_IDS"
  111. case $COMMAND in
  112. 'update-known_hosts'|'update-known-hosts'|'k')
  113. MODE='known_hosts'
  114. # touch the known_hosts file to make sure it exists
  115. # ssh-keygen complains if it doesn't exist
  116. touch "$USER_KNOWN_HOSTS"
  117. # if hosts are specified on the command line, process just
  118. # those hosts
  119. if [ "$1" ] ; then
  120. for host ; do
  121. process_host "$host"
  122. done
  123. # otherwise, if no hosts are specified, process every user
  124. # in the user's known_hosts file
  125. else
  126. if [ ! -s "$USER_KNOWN_HOSTS" ] ; then
  127. failure "known_hosts file '$USER_KNOWN_HOSTS' is empty."
  128. fi
  129. log "processing known_hosts file..."
  130. process_known_hosts "$USER_KNOWN_HOSTS"
  131. fi
  132. ;;
  133. 'update-userids'|'update-userid'|'u')
  134. if [ -z "$1" ] ; then
  135. failure "you must specify at least one userid."
  136. fi
  137. for userID ; do
  138. update_userid "$userID"
  139. done
  140. log "Run the following to update your monkeysphere authorized_keys file:"
  141. log "$PGRM update-authorized_keys"
  142. ;;
  143. 'remove-userids'|'remove-userid'|'r')
  144. if [ -z "$1" ] ; then
  145. failure "you must specify at least one userid."
  146. fi
  147. for userID ; do
  148. remove_userid "$userID"
  149. done
  150. log "Run the following to update your monkeysphere authorized_keys file:"
  151. log "$PGRM update-authorized_keys"
  152. ;;
  153. 'update-authorized_keys'|'update-authorized-keys'|'a')
  154. MODE='authorized_keys'
  155. # fail if the authorized_user_ids file is empty
  156. if [ ! -s "$AUTHORIZED_USER_IDS" ] ; then
  157. failure "$AUTHORIZED_USER_IDS is empty."
  158. fi
  159. # set user-controlled authorized_keys file path
  160. userAuthorizedKeys=${USER_CONTROLLED_AUTHORIZED_KEYS/\%h/"$HOME"}
  161. # update authorized_keys
  162. update_authorized_keys "$msAuthorizedKeys" "$userAuthorizedKeys" "$userKeysCacheDir"
  163. ;;
  164. 'gen-subkey'|'g')
  165. keyID="$1"
  166. if [ -z "$keyID" ] ; then
  167. failure "You must specify the key ID of your primary key."
  168. fi
  169. gen_subkey "$keyID"
  170. ;;
  171. 'help'|'h'|'?')
  172. usage
  173. ;;
  174. *)
  175. failure "Unknown command: '$COMMAND'
  176. Type '$PGRM help' for usage."
  177. ;;
  178. esac