summaryrefslogtreecommitdiff
path: root/src/monkeysphere
blob: 6853f581ff2ff1b81083ed385ba3566326be7f32 (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 --quiet --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. log "generating subkey..."
  77. echo "$editCommands" | gpg --expert --command-fd 0 --edit-key "$keyID"
  78. log "done."
  79. }
  80. ########################################################################
  81. # MAIN
  82. ########################################################################
  83. COMMAND="$1"
  84. [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
  85. shift
  86. # set ms home directory
  87. MS_HOME=${MS_HOME:-"${HOME}/.config/monkeysphere"}
  88. # load configuration file
  89. MS_CONF=${MS_CONF:-"${MS_HOME}/monkeysphere.conf"}
  90. [ -e "$MS_CONF" ] && . "$MS_CONF"
  91. # set empty config variable with defaults
  92. AUTHORIZED_USER_IDS=${AUTHORIZED_USER_IDS:-"${MS_HOME}/authorized_user_ids"}
  93. GNUPGHOME=${GNUPGHOME:-"${HOME}/.gnupg"}
  94. KEYSERVER=${KEYSERVER:-"subkeys.pgp.net"}
  95. CHECK_KEYSERVER=${CHECK_KEYSERVER:="true"}
  96. REQUIRED_HOST_KEY_CAPABILITY=${REQUIRED_HOST_KEY_CAPABILITY:-"e a"}
  97. REQUIRED_USER_KEY_CAPABILITY=${REQUIRED_USER_KEY_CAPABILITY:-"a"}
  98. KNOWN_HOSTS=${KNOWN_HOSTS:-"${HOME}/.ssh/known_hosts"}
  99. AUTHORIZED_KEYS=${AUTHORIZED_KEYS:-"${HOME}/.ssh/authorized_keys"}
  100. HASH_KNOWN_HOSTS=${HASH_KNOWN_HOSTS:-"true"}
  101. export GNUPGHOME
  102. # make sure gpg home exists with proper permissions
  103. mkdir -p -m 0700 "$GNUPGHOME"
  104. # make sure the user monkeysphere home directory exists
  105. mkdir -p -m 0700 "$MS_HOME"
  106. touch "$AUTHORIZED_USER_IDS"
  107. touch "$AUTHORIZED_KEYS"
  108. case $COMMAND in
  109. 'update-known_hosts'|'update-known-hosts'|'k')
  110. MODE='known_hosts'
  111. # touch the known_hosts file to make sure it exists
  112. # ssh-keygen complains if it doesn't exist
  113. touch "$KNOWN_HOSTS"
  114. # if hosts are specified on the command line, process just
  115. # those hosts
  116. if [ "$1" ] ; then
  117. for host ; do
  118. process_host_known_hosts "$host"
  119. done
  120. log "known_hosts file updated."
  121. # otherwise, if no hosts are specified, process every user
  122. # in the user's known_hosts file
  123. else
  124. if [ ! -s "$KNOWN_HOSTS" ] ; then
  125. failure "known_hosts file '$KNOWN_HOSTS' is empty."
  126. fi
  127. log "processing known_hosts file..."
  128. process_known_hosts
  129. log "known_hosts file updated."
  130. fi
  131. ;;
  132. 'update-userids'|'update-userid'|'u')
  133. if [ -z "$1" ] ; then
  134. failure "you must specify at least one userid."
  135. fi
  136. for userID ; do
  137. update_userid "$userID"
  138. done
  139. log "Run the following to update your monkeysphere authorized_keys file:"
  140. log "$PGRM update-authorized_keys"
  141. ;;
  142. 'remove-userids'|'remove-userid'|'r')
  143. if [ -z "$1" ] ; then
  144. failure "you must specify at least one userid."
  145. fi
  146. for userID ; do
  147. remove_userid "$userID"
  148. done
  149. log "Run the following to update your monkeysphere authorized_keys file:"
  150. log "$PGRM update-authorized_keys"
  151. ;;
  152. 'update-authorized_keys'|'update-authorized-keys'|'a')
  153. MODE='authorized_keys'
  154. # fail if the authorized_user_ids file is empty
  155. if [ ! -s "$AUTHORIZED_USER_IDS" ] ; then
  156. failure "$AUTHORIZED_USER_IDS is empty."
  157. fi
  158. # process authorized_user_ids file
  159. log "processing authorized_user_ids file..."
  160. process_authorized_user_ids
  161. log "authorized_keys file updated."
  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