summaryrefslogtreecommitdiff
path: root/src/monkeysphere
blob: 58f0fdc632edaeb8f7aa679b24dbfa6e6c81809c (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. # return 1 if there only "tru" lines are output from gpg
  44. if [ -z "$(echo "$gpgOut" | grep -v '^tru:')" ] ; then
  45. failure "Key ID '$keyID' not found."
  46. fi
  47. # set subkey defaults
  48. SUBKEY_TYPE=${SUBKEY_TYPE:-"RSA"}
  49. #SUBKEY_LENGTH=${SUBKEY_LENGTH:-"2048"}
  50. SUBKEY_USAGE=${SUBKEY_USAGE:-"auth"}
  51. SUBKEY_EXPIRE=${SUBKEY_EXPIRE:-"0"}
  52. cat <<EOF
  53. Please specify how long the key should be valid.
  54. 0 = key does not expire
  55. <n> = key expires in n days
  56. <n>w = key expires in n weeks
  57. <n>m = key expires in n months
  58. <n>y = key expires in n years
  59. EOF
  60. read -p "Key is valid for? ($SUBKEY_EXPIRE) " SUBKEY_EXPIRE; SUBKEY_EXPIRE=${SUBKEY_EXPIRE:-"0"}
  61. # generate the list of commands that will be passed to edit-key
  62. editCommands=$(cat <<EOF
  63. addkey
  64. 7
  65. S
  66. E
  67. A
  68. Q
  69. $SUBKEY_LENGTH
  70. $SUBKEY_EXPIRE
  71. save
  72. EOF
  73. )
  74. log "generating subkey..."
  75. echo "$editCommands" | gpg --expert --command-fd 0 --edit-key "$keyID"
  76. log "done."
  77. }
  78. ########################################################################
  79. # MAIN
  80. ########################################################################
  81. COMMAND="$1"
  82. [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
  83. shift
  84. # set ms home directory
  85. MS_HOME=${MS_HOME:-"${HOME}/.config/monkeysphere"}
  86. # load configuration file
  87. MS_CONF=${MS_CONF:-"${MS_HOME}/monkeysphere.conf"}
  88. [ -e "$MS_CONF" ] && . "$MS_CONF"
  89. # set empty config variable with defaults
  90. AUTHORIZED_USER_IDS=${AUTHORIZED_USER_IDS:-"${MS_HOME}/authorized_user_ids"}
  91. GNUPGHOME=${GNUPGHOME:-"${HOME}/.gnupg"}
  92. KEYSERVER=${KEYSERVER:-"subkeys.pgp.net"}
  93. CHECK_KEYSERVER=${CHECK_KEYSERVER:="true"}
  94. REQUIRED_HOST_KEY_CAPABILITY=${REQUIRED_HOST_KEY_CAPABILITY:-"a"}
  95. REQUIRED_USER_KEY_CAPABILITY=${REQUIRED_USER_KEY_CAPABILITY:-"a"}
  96. KNOWN_HOSTS=${KNOWN_HOSTS:-"${HOME}/.ssh/known_hosts"}
  97. AUTHORIZED_KEYS=${AUTHORIZED_KEYS:-"${HOME}/.ssh/authorized_keys"}
  98. HASH_KNOWN_HOSTS=${HASH_KNOWN_HOSTS:-"true"}
  99. export GNUPGHOME
  100. # make sure gpg home exists with proper permissions
  101. mkdir -p -m 0700 "$GNUPGHOME"
  102. # make sure the user monkeysphere home directory exists
  103. mkdir -p -m 0700 "$MS_HOME"
  104. touch "$AUTHORIZED_USER_IDS"
  105. touch "$AUTHORIZED_KEYS"
  106. case $COMMAND in
  107. 'update-known_hosts'|'update-known-hosts'|'k')
  108. MODE='known_hosts'
  109. # touch the known_hosts file to make sure it exists
  110. # ssh-keygen complains if it doesn't exist
  111. touch "$KNOWN_HOSTS"
  112. # if hosts are specified on the command line, process just
  113. # those hosts
  114. if [ "$1" ] ; then
  115. process_hosts_known_hosts "$@"
  116. # otherwise, if no hosts are specified, process every host
  117. # in the user's known_hosts file
  118. else
  119. if [ ! -s "$KNOWN_HOSTS" ] ; then
  120. failure "known_hosts file '$KNOWN_HOSTS' is empty."
  121. fi
  122. log "processing known_hosts file..."
  123. process_known_hosts
  124. fi
  125. log "known_hosts file updated."
  126. ;;
  127. 'update-authorized_keys'|'update-authorized-keys'|'a')
  128. MODE='authorized_keys'
  129. # fail if the authorized_user_ids file is empty
  130. if [ ! -s "$AUTHORIZED_USER_IDS" ] ; then
  131. failure "$AUTHORIZED_USER_IDS is empty."
  132. fi
  133. # process authorized_user_ids file
  134. log "processing authorized_user_ids file..."
  135. process_authorized_user_ids "$AUTHORIZED_USER_IDS"
  136. log "authorized_keys file updated."
  137. ;;
  138. 'gen-subkey'|'g')
  139. keyID="$1"
  140. if [ -z "$keyID" ] ; then
  141. failure "You must specify the key ID of your primary key."
  142. fi
  143. gen_subkey "$keyID"
  144. ;;
  145. 'help'|'h'|'?')
  146. usage
  147. ;;
  148. *)
  149. failure "Unknown command: '$COMMAND'
  150. Type '$PGRM help' for usage."
  151. ;;
  152. esac