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