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