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