summaryrefslogtreecommitdiff
path: root/src/monkeysphere
blob: ff4423ba11107365afb2343f46232baf35bf1b22 (plain)
  1. #!/bin/sh
  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-ae-subkey (g) KEYID generate an 'ae' capable subkey
  33. help (h,?) this help
  34. EOF
  35. }
  36. # generate a subkey with the 'a' and 'e' usage flags set
  37. gen_ae_subkey(){
  38. local keyID
  39. local gpgOut
  40. local userID
  41. log "warning: this function is still not working."
  42. keyID="$1"
  43. # set subkey defaults
  44. SUBKEY_TYPE=${KEY_TYPE:-"RSA"}
  45. SUBKEY_LENGTH=${KEY_LENGTH:-"1024"}
  46. SUBKEY_USAGE=${KEY_USAGE:-"encrypt,auth"}
  47. gpgOut=$(gpg --fixed-list-mode --list-keys --with-colons \
  48. "$keyID" 2> /dev/null)
  49. # return 1 if there only "tru" lines are output from gpg
  50. if [ -z "$(echo "$gpgOut" | grep -v '^tru:')" ] ; then
  51. log " key not found."
  52. return 1
  53. fi
  54. userID=$(echo "$gpgOut" | grep "^uid:" | cut -d: -f10)
  55. # set key parameters
  56. keyParameters=$(cat <<EOF
  57. Subkey-Type: $SUBKEY_TYPE
  58. Subkey-Length: $SUBKEY_LENGTH
  59. Subkey-Usage: $SUBKEY_USAGE
  60. Name-Real: $userID
  61. EOF
  62. )
  63. echo "The following key parameters will be used:"
  64. echo "$keyParameters"
  65. read -p "generate key? [Y|n]: " OK; OK=${OK:=Y}
  66. if [ ${OK/y/Y} != 'Y' ] ; then
  67. failure "aborting."
  68. fi
  69. # add commit command
  70. keyParameters="${keyParameters}"$(cat <<EOF
  71. %commit
  72. %echo done
  73. EOF
  74. )
  75. echo "generating subkey..."
  76. echo "$keyParameters" | gpg --batch --gen-key
  77. }
  78. ########################################################################
  79. # MAIN
  80. ########################################################################
  81. COMMAND="$1"
  82. [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
  83. shift
  84. # set ms home directory
  85. MS_HOME=${MS_HOME:-"${HOME}/.config/monkeysphere"}
  86. # load configuration file
  87. MS_CONF=${MS_CONF:-"${MS_HOME}/monkeysphere.conf"}
  88. [ -e "$MS_CONF" ] && . "$MS_CONF"
  89. # set empty config variable with defaults
  90. AUTHORIZED_USER_IDS=${AUTHORIZED_USER_IDS:-"${MS_HOME}/authorized_user_ids"}
  91. GNUPGHOME=${GNUPGHOME:-"${HOME}/.gnupg"}
  92. KEYSERVER=${KEYSERVER:-"subkeys.pgp.net"}
  93. REQUIRED_HOST_KEY_CAPABILITY=${REQUIRED_HOST_KEY_CAPABILITY:-"e a"}
  94. REQUIRED_USER_KEY_CAPABILITY=${REQUIRED_USER_KEY_CAPABILITY:-"a"}
  95. USER_CONTROLLED_AUTHORIZED_KEYS=${USER_CONTROLLED_AUTHORIZED_KEYS:-"%h/.ssh/authorized_keys"}
  96. USER_KNOWN_HOSTS=${USER_KNOWN_HOSTS:-"${HOME}/.ssh/known_hosts"}
  97. HASH_KNOWN_HOSTS=${HASH_KNOWN_HOSTS:-"true"}
  98. export GNUPGHOME
  99. # stagging locations
  100. hostKeysCacheDir="${MS_HOME}/host_keys"
  101. userKeysCacheDir="${MS_HOME}/user_keys"
  102. msAuthorizedKeys="${MS_HOME}/authorized_keys"
  103. # make sure gpg home exists with proper permissions
  104. mkdir -p -m 0700 "$GNUPGHOME"
  105. # make sure the user monkeysphere home directory exists
  106. mkdir -p -m 0700 "$MS_HOME"
  107. mkdir -p "$hostKeysCacheDir"
  108. mkdir -p "$userKeysCacheDir"
  109. touch "$AUTHORIZED_USER_IDS"
  110. case $COMMAND in
  111. 'update-known_hosts'|'update-known-hosts'|'k')
  112. MODE='known_hosts'
  113. # touch the known_hosts file to make sure it exists
  114. # ssh-keygen complains if it doesn't exist
  115. touch "$USER_KNOWN_HOSTS"
  116. # if hosts are specified on the command line, process just
  117. # those hosts
  118. if [ "$1" ] ; then
  119. for host ; do
  120. process_host "$host" "$hostKeysCacheDir"
  121. done
  122. # otherwise, if no hosts are specified, process every user
  123. # in the user's known_hosts file
  124. else
  125. if [ ! -s "$USER_KNOWN_HOSTS" ] ; then
  126. failure "known_hosts file '$USER_KNOWN_HOSTS' is empty."
  127. fi
  128. log "processing known_hosts file..."
  129. process_known_hosts "$USER_KNOWN_HOSTS" "$hostKeysCacheDir"
  130. fi
  131. ;;
  132. 'update-userids'|'update-userid'|'u')
  133. if [ -z "$1" ] ; then
  134. failure "you must specify at least one userid."
  135. fi
  136. for userID ; do
  137. update_userid "$userID" "$userKeysCacheDir"
  138. done
  139. log "run the following to update your monkeysphere authorized_keys file:"
  140. log "$PGRM update-authorized_keys"
  141. ;;
  142. 'remove-userids'|'remove-userid'|'r')
  143. if [ -z "$1" ] ; then
  144. failure "you must specify at least one userid."
  145. fi
  146. for userID ; do
  147. remove_userid "$userID"
  148. done
  149. log "run the following to update your monkeysphere authorized_keys file:"
  150. log "$PGRM update-authorized_keys"
  151. ;;
  152. 'update-authorized_keys'|'update-authorized-keys'|'a')
  153. MODE='authorized_keys'
  154. # fail if the authorized_user_ids file is empty
  155. if [ ! -s "$AUTHORIZED_USER_IDS" ] ; then
  156. failure "$AUTHORIZED_USER_IDS is empty."
  157. fi
  158. # set user-controlled authorized_keys file path
  159. userAuthorizedKeys=${USER_CONTROLLED_AUTHORIZED_KEYS/\%h/"$HOME"}
  160. # update authorized_keys
  161. update_authorized_keys "$msAuthorizedKeys" "$userAuthorizedKeys" "$userKeysCacheDir"
  162. ;;
  163. 'gen-ae-subkey'|'g')
  164. keyID="$1"
  165. if [ -z "$keyID" ] ; then
  166. failure "you must specify keyid of primary key."
  167. fi
  168. gen_ae_subkey "$keyID"
  169. ;;
  170. 'help'|'h'|'?')
  171. usage
  172. ;;
  173. *)
  174. failure "Unknown command: '$COMMAND'
  175. Type '$PGRM help' for usage."
  176. ;;
  177. esac