summaryrefslogtreecommitdiff
path: root/src/monkeysphere
blob: f279d86395a249b5169df2fd9d26fa30b1aceb90 (plain)
  1. #!/bin/sh
  2. ########################################################################
  3. PGRM=$(basename $0)
  4. SHAREDIR=${SHAREDIR:-"/usr/share/monkeysphere"}
  5. export SHAREDIR
  6. . "${SHAREDIR}/common"
  7. GLOBAL_CONFIG=${GLOBAL_CONFIG:-"${ETC}"/monkeysphere.conf}
  8. [ -r "$GLOBAL_CONFIG" ] && . "$GLOBAL_CONFIG"
  9. # date in UTF format if needed
  10. DATE=$(date -u '+%FT%T')
  11. # unset some environment variables that could screw things up
  12. GREP_OPTIONS=
  13. ########################################################################
  14. # FUNCTIONS
  15. ########################################################################
  16. usage() {
  17. cat <<EOF
  18. usage: $PGRM <subcommand> [args]
  19. Monkeysphere client tool.
  20. subcommands:
  21. update-known-hosts (k) [HOST]... update known_hosts file
  22. update-authorized-keys (a) update authorized_keys file
  23. update-userid (u) [USERID]... add/update userid to
  24. authorized_user_ids
  25. help (h,?) this help
  26. EOF
  27. }
  28. ########################################################################
  29. # MAIN
  30. ########################################################################
  31. COMMAND="$1"
  32. [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
  33. shift
  34. # set ms home directory
  35. MS_HOME=${MS_HOME:-"$HOME"/.config/monkeysphere}
  36. # load configuration file
  37. MS_CONF=${MS_CONF:-"$MS_HOME"/monkeysphere.conf}
  38. [ -e "$MS_CONF" ] && . "$MS_CONF"
  39. # set empty config variable with defaults
  40. AUTHORIZED_USER_IDS=${AUTHORIZED_USER_IDS:-"$MS_HOME"/authorized_user_ids}
  41. GNUPGHOME=${GNUPGHOME:-"$HOME"/.gnupg}
  42. KEYSERVER=${KEYSERVER:-subkeys.pgp.net}
  43. REQUIRED_KEY_CAPABILITY=${REQUIRED_KEY_CAPABILITY:-"e a"}
  44. USER_CONTROLLED_AUTHORIZED_KEYS=${USER_CONTROLLED_AUTHORIZED_KEYS:-%h/.ssh/authorized_keys}
  45. USER_KNOWN_HOSTS=${USER_KNOWN_HOSTS:-"$HOME"/.ssh/known_hosts}
  46. HASH_KNOWN_HOSTS=${HASH_KNOWN_HOSTS:-}
  47. export GNUPGHOME
  48. # stagging locations
  49. hostKeysCacheDir="$MS_HOME"/host_keys
  50. userKeysCacheDir="$MS_HOME"/user_keys
  51. msAuthorizedKeys="$MS_HOME"/authorized_keys
  52. # make sure gpg home exists with proper permissions
  53. mkdir -p -m 0700 "$GNUPGHOME"
  54. case $COMMAND in
  55. 'update-known-hosts'|'k')
  56. MODE='known_hosts'
  57. # touch the known_hosts file to make sure it exists
  58. touch "$USER_KNOWN_HOSTS"
  59. # if hosts are specified on the command line, process just
  60. # those hosts
  61. if [ "$1" ] ; then
  62. for host ; do
  63. process_host "$host" "$hostKeysCacheDir"
  64. done
  65. # otherwise, if no hosts are specified, process the user
  66. # known_hosts file
  67. else
  68. if [ ! -s "$USER_KNOWN_HOSTS" ] ; then
  69. failure "known_hosts file '$USER_KNOWN_HOSTS' is empty."
  70. fi
  71. log "processing known_hosts file..."
  72. process_known_hosts "$USER_KNOWN_HOSTS" "$hostKeysCacheDir"
  73. fi
  74. ;;
  75. 'update-authorized-keys'|'a')
  76. MODE='authorized_keys'
  77. log "processing authorized_user_ids file..."
  78. # make sure authorized_user_ids file exists
  79. if [ ! -s "$AUTHORIZED_USER_IDS" ] ; then
  80. log "authorized_user_ids file is empty or does not exist."
  81. exit
  82. fi
  83. process_authorized_ids "$AUTHORIZED_USER_IDS" "$userKeysCacheDir"
  84. # write output key file
  85. log "writing monkeysphere authorized_keys file... "
  86. touch "$msAuthorizedKeys"
  87. if [ "$(ls "$userKeysCacheDir")" ] ; then
  88. log -n "adding gpg keys... "
  89. cat "$userKeysCacheDir"/* > "$msAuthorizedKeys"
  90. echo "done."
  91. else
  92. log "no gpg keys to add."
  93. fi
  94. if [ "$USER_CONTROLLED_AUTHORIZED_KEYS" ] ; then
  95. userAuthorizedKeys=${USER_CONTROLLED_AUTHORIZED_KEYS/\%h/"$HOME"}
  96. if [ -s "$userAuthorizedKeys" ] ; then
  97. log -n "adding user authorized_keys file... "
  98. cat "$userAuthorizedKeys" >> "$msAuthorizedKeys"
  99. echo "done."
  100. fi
  101. fi
  102. log "monkeysphere authorized_keys file generated:"
  103. log "$msAuthorizedKeys"
  104. ;;
  105. 'update-userid'|'u')
  106. if [ -z "$1" ] ; then
  107. failure "you must specify at least one userid."
  108. fi
  109. for userID ; do
  110. if ! grep -q "^${userID}\$" "$AUTHORIZED_USER_IDS" ; then
  111. log "userid '$userID' not in authorized_user_ids file."
  112. continue
  113. fi
  114. log "processing user id: '$userID'"
  115. process_user_id "$userID" "$userKeysCacheDir" > /dev/null
  116. done
  117. ;;
  118. 'help'|'h'|'?')
  119. usage
  120. ;;
  121. *)
  122. failure "Unknown command: '$COMMAND'
  123. Type 'cereal-admin help' for usage."
  124. ;;
  125. esac