summaryrefslogtreecommitdiff
path: root/src/monkeysphere
blob: 5d865c96a3a7a747aa7054213500149baaec3ca4 (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. loge " 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. log "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:-}
  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. case $COMMAND in
  104. 'update-known_hosts'|'update-known-hosts'|'k')
  105. MODE='known_hosts'
  106. # touch the known_hosts file to make sure it exists
  107. touch "$USER_KNOWN_HOSTS"
  108. # if hosts are specified on the command line, process just
  109. # those hosts
  110. if [ "$1" ] ; then
  111. for host ; do
  112. process_host "$host" "$hostKeysCacheDir"
  113. done
  114. # otherwise, if no hosts are specified, process the user
  115. # known_hosts file
  116. else
  117. if [ ! -s "$USER_KNOWN_HOSTS" ] ; then
  118. failure "known_hosts file '$USER_KNOWN_HOSTS' is empty."
  119. fi
  120. log "processing known_hosts file..."
  121. process_known_hosts "$USER_KNOWN_HOSTS" "$hostKeysCacheDir"
  122. fi
  123. ;;
  124. 'update-authorized_keys'|'update-authorized-keys'|'a')
  125. MODE='authorized_keys'
  126. # make sure authorized_user_ids file exists
  127. if [ ! -s "$AUTHORIZED_USER_IDS" ] ; then
  128. log "authorized_user_ids file is empty or does not exist."
  129. exit
  130. fi
  131. # set user-controlled authorized_keys file path
  132. userAuthorizedKeys=${USER_CONTROLLED_AUTHORIZED_KEYS/\%h/"$HOME"}
  133. # update authorized_keys
  134. update_authorized_keys "$msAuthorizedKeys" "$userAuthorizedKeys" "$userKeysCacheDir"
  135. ;;
  136. 'update-userids'|'u')
  137. if [ -z "$1" ] ; then
  138. failure "you must specify at least one userid."
  139. fi
  140. for userID ; do
  141. update_userid "$userID" "$userKeysCacheDir"
  142. done
  143. ;;
  144. 'gen-ae-subkey'|'g')
  145. keyID="$1"
  146. if [ -z "$keyID" ] ; then
  147. failure "you must specify keyid of primary key."
  148. fi
  149. gen_ae_subkey "$keyID"
  150. ;;
  151. 'help'|'h'|'?')
  152. usage
  153. ;;
  154. *)
  155. failure "Unknown command: '$COMMAND'
  156. Type 'cereal-admin help' for usage."
  157. ;;
  158. esac