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