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