summaryrefslogtreecommitdiff
path: root/src/monkeysphere
blob: 3f127e6f1c0f7dd49a2c08517814350d992ee602 (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. if [ -z "$keyID" ] ; then
  76. failure "You must specify the key ID of your primary key."
  77. fi
  78. # get key output, and fail if not found
  79. gpgOut=$(gpg --quiet --fixed-list-mode --list-secret-keys --with-colons \
  80. "$keyID") || failure
  81. # fail if multiple sec lines are returned, which means the id
  82. # given is not unique
  83. if [ $(echo "$gpgOut" | grep '^sec:' | wc -l) -gt '1' ] ; then
  84. failure "Key ID '$keyID' is not unique."
  85. fi
  86. # prompt if an authentication subkey already exists
  87. if echo "$gpgOut" | egrep "^(sec|ssb):" | cut -d: -f 12 | grep -q a ; then
  88. echo "An authentication subkey already exists for key '$keyID'."
  89. read -p "Are you sure you would like to generate another one? (y/N) " OK; OK=${OK:N}
  90. if [ "${OK/y/Y}" != 'Y' ] ; then
  91. failure "aborting."
  92. fi
  93. fi
  94. # set subkey defaults
  95. # prompt about key expiration if not specified
  96. if [ -z "$keyExpire" ] ; then
  97. cat <<EOF
  98. Please specify how long the key should be valid.
  99. 0 = key does not expire
  100. <n> = key expires in n days
  101. <n>w = key expires in n weeks
  102. <n>m = key expires in n months
  103. <n>y = key expires in n years
  104. EOF
  105. while [ -z "$keyExpire" ] ; do
  106. read -p "Key is valid for? (0) " keyExpire
  107. if ! test_gpg_expire ${keyExpire:=0} ; then
  108. echo "invalid value"
  109. unset keyExpire
  110. fi
  111. done
  112. elif ! test_gpg_expire "$keyExpire" ; then
  113. failure "invalid key expiration value '$keyExpire'."
  114. fi
  115. # generate the list of commands that will be passed to edit-key
  116. editCommands=$(cat <<EOF
  117. addkey
  118. 7
  119. S
  120. E
  121. A
  122. Q
  123. $keyLength
  124. $keyExpire
  125. save
  126. EOF
  127. )
  128. log "generating subkey..."
  129. echo "$editCommands" | gpg --expert --command-fd 0 --edit-key "$keyID"
  130. log "done."
  131. }
  132. ########################################################################
  133. # MAIN
  134. ########################################################################
  135. # unset variables that should be defined only in config file
  136. unset KEYSERVER
  137. unset CHECK_KEYSERVER
  138. unset KNOWN_HOSTS
  139. unset HASH_KNOWN_HOSTS
  140. unset AUTHORIZED_KEYS
  141. # load global config
  142. [ -r "${ETC}/monkeysphere.conf" ] && . "${ETC}/monkeysphere.conf"
  143. # set monkeysphere home directory
  144. MONKEYSPHERE_HOME=${MONKEYSPHERE_HOME:="${HOME}/.config/monkeysphere"}
  145. mkdir -p -m 0700 "$MONKEYSPHERE_HOME"
  146. # load local config
  147. [ -e ${MONKEYSPHERE_CONFIG:="${MONKEYSPHERE_HOME}/monkeysphere.conf"} ] && . "$MONKEYSPHERE_CONFIG"
  148. # set empty config variables with ones from the environment, or from
  149. # config file, or with defaults
  150. GNUPGHOME=${MONKEYSPHERE_GNUPGHOME:=${GNUPGHOME:="${HOME}/.gnupg"}}
  151. KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="subkeys.pgp.net"}}
  152. CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=${CHECK_KEYSERVER:="true"}}
  153. KNOWN_HOSTS=${MONKEYSPHERE_KNOWN_HOSTS:=${KNOWN_HOSTS:="${HOME}/.ssh/known_hosts"}}
  154. HASH_KNOWN_HOSTS=${MONKEYSPHERE_HASH_KNOWN_HOSTS:=${HASH_KNOWN_HOSTS:="true"}}
  155. AUTHORIZED_KEYS=${MONKEYSPHERE_AUTHORIZED_KEYS:=${AUTHORIZED_KEYS:="${HOME}/.ssh/authorized_keys"}}
  156. # other variables not in config file
  157. AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:="${MONKEYSPHERE_HOME}/authorized_user_ids"}
  158. REQUIRED_HOST_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_HOST_KEY_CAPABILITY:="a"}
  159. REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
  160. # export GNUPGHOME and make sure gpg home exists with proper
  161. # permissions
  162. export GNUPGHOME
  163. mkdir -p -m 0700 "$GNUPGHOME"
  164. # get subcommand
  165. COMMAND="$1"
  166. [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
  167. shift
  168. case $COMMAND in
  169. 'update-known_hosts'|'update-known-hosts'|'k')
  170. MODE='known_hosts'
  171. if ! check_key_file_permissions "$USER" "$KNOWN_HOSTS" ; then
  172. failure "Improper permissions on known_hosts file."
  173. fi
  174. # if hosts are specified on the command line, process just
  175. # those hosts
  176. if [ "$1" ] ; then
  177. update_known_hosts "$@"
  178. RETURN="$?"
  179. # otherwise, if no hosts are specified, process every host
  180. # in the user's known_hosts file
  181. else
  182. if [ ! -e "$KNOWN_HOSTS" ] ; then
  183. failure "known_hosts file '$KNOWN_HOSTS' does not exist."
  184. fi
  185. process_known_hosts
  186. RETURN="$?"
  187. fi
  188. ;;
  189. 'update-authorized_keys'|'update-authorized-keys'|'a')
  190. MODE='authorized_keys'
  191. # fail if the authorized_user_ids file is empty
  192. if [ ! -e "$AUTHORIZED_USER_IDS" ] ; then
  193. failure "authorized_user_ids file '$AUTHORIZED_USER_IDS' does not exist."
  194. fi
  195. if ! check_key_file_permissions "$USER" "$AUTHORIZED_USER_IDS" ; then
  196. failure "Improper permissions on authorized_user_ids file."
  197. fi
  198. # process authorized_user_ids file
  199. process_authorized_user_ids "$AUTHORIZED_USER_IDS"
  200. RETURN="$?"
  201. ;;
  202. 'gen-subkey'|'g')
  203. gen_subkey "$@"
  204. ;;
  205. 'help'|'h'|'?')
  206. usage
  207. ;;
  208. *)
  209. failure "Unknown command: '$COMMAND'
  210. Type '$PGRM help' for usage."
  211. ;;
  212. esac
  213. exit "$RETURN"