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