summaryrefslogtreecommitdiff
path: root/src/monkeysphere
blob: 6d9e6c3a28469880895525d2a51e64667cb8de7c (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"}
  165. # if keyserver not specified in env or monkeysphere.conf,
  166. # look in gpg.conf
  167. if [ -z "$KEYSERVER" ] ; then
  168. if [ -f "${GNUPGHOME}/gpg.conf" ] ; then
  169. KEYSERVER=$(grep -e "^[[:space:]]*keyserver " "${GNUPGHOME}/gpg.conf" | tail -1 | awk '{ print $2 }')
  170. fi
  171. fi
  172. # if it's still not specified, use the default
  173. KEYSERVER=${KEYSERVER:="subkeys.pgp.net"}
  174. CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=${CHECK_KEYSERVER:="true"}}
  175. KNOWN_HOSTS=${MONKEYSPHERE_KNOWN_HOSTS:=${KNOWN_HOSTS:="${HOME}/.ssh/known_hosts"}}
  176. HASH_KNOWN_HOSTS=${MONKEYSPHERE_HASH_KNOWN_HOSTS:=${HASH_KNOWN_HOSTS:="true"}}
  177. AUTHORIZED_KEYS=${MONKEYSPHERE_AUTHORIZED_KEYS:=${AUTHORIZED_KEYS:="${HOME}/.ssh/authorized_keys"}}
  178. # other variables not in config file
  179. AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:="${MONKEYSPHERE_HOME}/authorized_user_ids"}
  180. REQUIRED_HOST_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_HOST_KEY_CAPABILITY:="a"}
  181. REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
  182. # export GNUPGHOME and make sure gpg home exists with proper
  183. # permissions
  184. export GNUPGHOME
  185. mkdir -p -m 0700 "$GNUPGHOME"
  186. # get subcommand
  187. COMMAND="$1"
  188. [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
  189. shift
  190. case $COMMAND in
  191. 'update-known_hosts'|'update-known-hosts'|'k')
  192. MODE='known_hosts'
  193. # check permissions on the known_hosts file path
  194. if ! check_key_file_permissions "$USER" "$KNOWN_HOSTS" ; then
  195. failure "Improper permissions on known_hosts file path."
  196. fi
  197. # if hosts are specified on the command line, process just
  198. # those hosts
  199. if [ "$1" ] ; then
  200. update_known_hosts "$@"
  201. RETURN="$?"
  202. # otherwise, if no hosts are specified, process every host
  203. # in the user's known_hosts file
  204. else
  205. # exit if the known_hosts file does not exist
  206. if [ ! -e "$KNOWN_HOSTS" ] ; then
  207. log "known_hosts file '$KNOWN_HOSTS' does not exist."
  208. exit
  209. fi
  210. process_known_hosts
  211. RETURN="$?"
  212. fi
  213. ;;
  214. 'update-authorized_keys'|'update-authorized-keys'|'a')
  215. MODE='authorized_keys'
  216. # check permissions on the authorized_user_ids file path
  217. if ! check_key_file_permissions "$USER" "$AUTHORIZED_USER_IDS" ; then
  218. failure "Improper permissions on authorized_user_ids file path."
  219. fi
  220. # check permissions on the authorized_keys file path
  221. if ! check_key_file_permissions "$USER" "$AUTHORIZED_KEYS" ; then
  222. failure "Improper permissions on authorized_keys file path."
  223. fi
  224. # exit if the authorized_user_ids file is empty
  225. if [ ! -e "$AUTHORIZED_USER_IDS" ] ; then
  226. log "authorized_user_ids file '$AUTHORIZED_USER_IDS' does not exist."
  227. exit
  228. fi
  229. # process authorized_user_ids file
  230. process_authorized_user_ids "$AUTHORIZED_USER_IDS"
  231. RETURN="$?"
  232. ;;
  233. 'gen-subkey'|'g')
  234. gen_subkey "$@"
  235. ;;
  236. 'help'|'h'|'?')
  237. usage
  238. ;;
  239. *)
  240. failure "Unknown command: '$COMMAND'
  241. Type '$PGRM help' for usage."
  242. ;;
  243. esac
  244. exit "$RETURN"