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