summaryrefslogtreecommitdiff
path: root/src/monkeysphere
blob: 1ba51d79619de218f1c50bf205e752ec50232145 (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_KEY_CAPABILITY=${REQUIRED_KEY_CAPABILITY:-"e a"}
  94. USER_CONTROLLED_AUTHORIZED_KEYS=${USER_CONTROLLED_AUTHORIZED_KEYS:-"%h/.ssh/authorized_keys"}
  95. USER_KNOWN_HOSTS=${USER_KNOWN_HOSTS:-"${HOME}/.ssh/known_hosts"}
  96. HASH_KNOWN_HOSTS=${HASH_KNOWN_HOSTS:-"true"}
  97. export GNUPGHOME
  98. # stagging locations
  99. hostKeysCacheDir="${MS_HOME}/host_keys"
  100. userKeysCacheDir="${MS_HOME}/user_keys"
  101. msAuthorizedKeys="${MS_HOME}/authorized_keys"
  102. # make sure gpg home exists with proper permissions
  103. mkdir -p -m 0700 "$GNUPGHOME"
  104. # make sure the user monkeysphere home directory exists
  105. mkdir -p -m 0700 "$MS_HOME"
  106. mkdir -p "$hostKeysCacheDir"
  107. mkdir -p "$userKeysCacheDir"
  108. touch "$AUTHORIZED_USER_IDS"
  109. case $COMMAND in
  110. 'update-known_hosts'|'update-known-hosts'|'k')
  111. MODE='known_hosts'
  112. # touch the known_hosts file to make sure it exists
  113. # ssh-keygen complains if it doesn't exist
  114. touch "$USER_KNOWN_HOSTS"
  115. # if hosts are specified on the command line, process just
  116. # those hosts
  117. if [ "$1" ] ; then
  118. for host ; do
  119. process_host "$host" "$hostKeysCacheDir"
  120. done
  121. # otherwise, if no hosts are specified, process every user
  122. # in the user's known_hosts file
  123. else
  124. if [ ! -s "$USER_KNOWN_HOSTS" ] ; then
  125. failure "known_hosts file '$USER_KNOWN_HOSTS' is empty."
  126. fi
  127. log "processing known_hosts file..."
  128. process_known_hosts "$USER_KNOWN_HOSTS" "$hostKeysCacheDir"
  129. fi
  130. ;;
  131. 'update-userids'|'update-userid'|'u')
  132. if [ -z "$1" ] ; then
  133. failure "you must specify at least one userid."
  134. fi
  135. for userID ; do
  136. update_userid "$userID" "$userKeysCacheDir"
  137. done
  138. log "run the following to update your monkeysphere authorized_keys file:"
  139. log "$PGRM update-authorized_keys"
  140. ;;
  141. 'remove-userids'|'remove-userid'|'r')
  142. if [ -z "$1" ] ; then
  143. failure "you must specify at least one userid."
  144. fi
  145. for userID ; do
  146. remove_userid "$userID"
  147. done
  148. log "run the following to update your monkeysphere authorized_keys file:"
  149. log "$PGRM update-authorized_keys"
  150. ;;
  151. 'update-authorized_keys'|'update-authorized-keys'|'a')
  152. MODE='authorized_keys'
  153. # fail if the authorized_user_ids file is empty
  154. if [ ! -s "$AUTHORIZED_USER_IDS" ] ; then
  155. failure "$AUTHORIZED_USER_IDS is empty."
  156. fi
  157. # set user-controlled authorized_keys file path
  158. userAuthorizedKeys=${USER_CONTROLLED_AUTHORIZED_KEYS/\%h/"$HOME"}
  159. # update authorized_keys
  160. update_authorized_keys "$msAuthorizedKeys" "$userAuthorizedKeys" "$userKeysCacheDir"
  161. ;;
  162. 'gen-ae-subkey'|'g')
  163. keyID="$1"
  164. if [ -z "$keyID" ] ; then
  165. failure "you must specify keyid of primary key."
  166. fi
  167. gen_ae_subkey "$keyID"
  168. ;;
  169. 'help'|'h'|'?')
  170. usage
  171. ;;
  172. *)
  173. failure "Unknown command: '$COMMAND'
  174. Type '$PGRM help' for usage."
  175. ;;
  176. esac