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