summaryrefslogtreecommitdiff
path: root/src/monkeysphere
blob: cb6c06c1907ac384a9450189bd2b82381f100994 (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. SHARE=${MONKEYSPHERE_SHARE:-"/usr/share/monkeysphere"}
  12. export SHARE
  13. . "${SHARE}/common" || exit 1
  14. # date in UTF format if needed
  15. DATE=$(date -u '+%FT%T')
  16. # unset some environment variables that could screw things up
  17. unset GREP_OPTIONS
  18. # default return code
  19. RETURN=0
  20. # set the file creation mask to be only owner rw
  21. umask 077
  22. ########################################################################
  23. # FUNCTIONS
  24. ########################################################################
  25. usage() {
  26. cat <<EOF
  27. usage: $PGRM <subcommand> [options] [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. -l|--length BITS key length in bits (2048)
  34. -e|--expire EXPIRE date to expire
  35. help (h,?) this help
  36. EOF
  37. }
  38. # generate a subkey with the 'a' usage flags set
  39. # FIXME: this needs some tweaking to clean it up
  40. gen_subkey(){
  41. local keyLength
  42. local keyExpire
  43. local keyID
  44. local gpgOut
  45. local userID
  46. # set default key parameter values
  47. keyLength=
  48. keyExpire=
  49. # get options
  50. TEMP=$(getopt -o l:e: -l length:,expire: -n "$PGRM" -- "$@")
  51. if [ $? != 0 ] ; then
  52. exit 1
  53. fi
  54. # Note the quotes around `$TEMP': they are essential!
  55. eval set -- "$TEMP"
  56. while true ; do
  57. case "$1" in
  58. -l|--length)
  59. keyLength="$2"
  60. shift 2
  61. ;;
  62. -e|--expire)
  63. keyExpire="$2"
  64. shift 2
  65. ;;
  66. --)
  67. shift
  68. ;;
  69. *)
  70. break
  71. ;;
  72. esac
  73. done
  74. keyID="$1"
  75. gpgOut=$(gpg --quiet --fixed-list-mode --list-keys --with-colons \
  76. "$keyID" 2> /dev/null)
  77. # fail if there only "tru" lines are output from gpg, which
  78. # indicates the key was not found.
  79. if [ -z "$(echo "$gpgOut" | grep -v '^tru:')" ] ; then
  80. failure "Key ID '$keyID' not found."
  81. fi
  82. # fail if multiple pub lines are returned, which means the id given
  83. # is not unique
  84. if [ $(echo "$gpgOut" | grep '^pub:' | wc -l) -gt '1' ] ; then
  85. failure "Key ID '$keyID' is not unique."
  86. fi
  87. # prompt if an authentication subkey already exists
  88. if echo "$gpgOut" | egrep "^(pub|sub):" | cut -d: -f 12 | grep -q a ; then
  89. echo "An authentication subkey already exists for key '$keyID'."
  90. read -p "Are you sure you would like to generate another one? (y/N) " OK; OK=${OK:N}
  91. if [ "${OK/y/Y}" != 'Y' ] ; then
  92. failure "aborting."
  93. fi
  94. fi
  95. # set subkey defaults
  96. # prompt about key expiration if not specified
  97. if [ -z "$keyExpire" ] ; then
  98. cat <<EOF
  99. Please specify how long the key should be valid.
  100. 0 = key does not expire
  101. <n> = key expires in n days
  102. <n>w = key expires in n weeks
  103. <n>m = key expires in n months
  104. <n>y = key expires in n years
  105. EOF
  106. while [ -z "$keyExpire" ] ; do
  107. read -p "Key is valid for? (0) " keyExpire
  108. if ! test_gpg_expire ${keyExpire:=0} ; then
  109. echo "invalid value"
  110. unset keyExpire
  111. fi
  112. done
  113. elif ! test_gpg_expire "$keyExpire" ; then
  114. failure "invalid key expiration value '$keyExpire'."
  115. fi
  116. # generate the list of commands that will be passed to edit-key
  117. editCommands=$(cat <<EOF
  118. addkey
  119. 7
  120. S
  121. E
  122. A
  123. Q
  124. $keyLength
  125. $keyExpire
  126. save
  127. EOF
  128. )
  129. log "generating subkey..."
  130. echo "$editCommands" | gpg --expert --command-fd 0 --edit-key "$keyID"
  131. log "done."
  132. }
  133. ########################################################################
  134. # MAIN
  135. ########################################################################
  136. # unset variables that should be defined only in config file
  137. unset KEYSERVER
  138. unset CHECK_KEYSERVER
  139. unset KNOWN_HOSTS
  140. unset HASH_KNOWN_HOSTS
  141. unset AUTHORIZED_KEYS
  142. # load global config
  143. [ -r "${ETC}/monkeysphere.conf" ] && . "${ETC}/monkeysphere.conf"
  144. # set monkeysphere home directory
  145. MONKEYSPHERE_HOME=${MONKEYSPHERE_HOME:="${HOME}/.config/monkeysphere"}
  146. mkdir -p -m 0700 "$MONKEYSPHERE_HOME"
  147. # load local config
  148. [ -e ${MONKEYSPHERE_CONFIG:="${MONKEYSPHERE_HOME}/monkeysphere.conf"} ] && . "$MONKEYSPHERE_CONFIG"
  149. # set empty config variables with ones from the environment, or from
  150. # config file, or with defaults
  151. GNUPGHOME=${MONKEYSPHERE_GNUPGHOME:=${GNUPGHOME:="${HOME}/.gnupg"}}
  152. KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="subkeys.pgp.net"}}
  153. CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=${CHECK_KEYSERVER:="true"}}
  154. KNOWN_HOSTS=${MONKEYSPHERE_KNOWN_HOSTS:=${KNOWN_HOSTS:="${HOME}/.ssh/known_hosts"}}
  155. HASH_KNOWN_HOSTS=${MONKEYSPHERE_HASH_KNOWN_HOSTS:=${HASH_KNOWN_HOSTS:="true"}}
  156. AUTHORIZED_KEYS=${MONKEYSPHERE_AUTHORIZED_KEYS:=${AUTHORIZED_KEYS:="${HOME}/.ssh/authorized_keys"}}
  157. # other variables not in config file
  158. AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:="${MONKEYSPHERE_HOME}/authorized_user_ids"}
  159. REQUIRED_HOST_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_HOST_KEY_CAPABILITY:="a"}
  160. REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
  161. # export GNUPGHOME and make sure gpg home exists with proper
  162. # permissions
  163. export GNUPGHOME
  164. mkdir -p -m 0700 "$GNUPGHOME"
  165. # get subcommand
  166. COMMAND="$1"
  167. [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
  168. shift
  169. case $COMMAND in
  170. 'update-known_hosts'|'update-known-hosts'|'k')
  171. MODE='known_hosts'
  172. if ! check_key_file_permissions "$USER" "$KNOWN_HOSTS" ; then
  173. failure "Improper permissions on known_hosts file."
  174. fi
  175. # if hosts are specified on the command line, process just
  176. # those hosts
  177. if [ "$1" ] ; then
  178. update_known_hosts "$@"
  179. RETURN="$?"
  180. # otherwise, if no hosts are specified, process every host
  181. # in the user's known_hosts file
  182. else
  183. if [ ! -s "$KNOWN_HOSTS" ] ; then
  184. failure "known_hosts file '$KNOWN_HOSTS' is empty or does not exist."
  185. fi
  186. process_known_hosts
  187. RETURN="$?"
  188. fi
  189. ;;
  190. 'update-authorized_keys'|'update-authorized-keys'|'a')
  191. MODE='authorized_keys'
  192. # fail if the authorized_user_ids file is empty
  193. if [ ! -s "$AUTHORIZED_USER_IDS" ] ; then
  194. failure "authorized_user_ids file '$AUTHORIZED_USER_IDS' is empty or does not exist."
  195. fi
  196. if ! check_key_file_permissions "$USER" "$AUTHORIZED_USER_IDS" ; then
  197. failure "Improper permissions on authorized_user_ids file."
  198. fi
  199. # process authorized_user_ids file
  200. process_authorized_user_ids "$AUTHORIZED_USER_IDS"
  201. RETURN="$?"
  202. ;;
  203. 'gen-subkey'|'g')
  204. keyID="$1"
  205. if [ -z "$keyID" ] ; then
  206. failure "You must specify the key ID of your primary key."
  207. fi
  208. gen_subkey "$keyID"
  209. ;;
  210. 'help'|'h'|'?')
  211. usage
  212. ;;
  213. *)
  214. failure "Unknown command: '$COMMAND'
  215. Type '$PGRM help' for usage."
  216. ;;
  217. esac
  218. exit "$RETURN"