summaryrefslogtreecommitdiff
path: root/src/monkeysphere
blob: f959a386f56b82ef0f1e51079ff31bd0ae78a862 (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. --length (-l) BITS key length in bits (2048)
  34. --expire (-e) EXPIRE date to expire
  35. help (h,?) this help
  36. EOF
  37. }
  38. # generate a subkey with the 'a' usage flags set
  39. gen_subkey(){
  40. local keyLength
  41. local keyExpire
  42. local keyID
  43. local gpgOut
  44. local userID
  45. # set default key parameter values
  46. keyLength=
  47. keyExpire=
  48. # get options
  49. TEMP=$(getopt -o l:e: -l length:,expire: -n "$PGRM" -- "$@")
  50. if [ $? != 0 ] ; then
  51. exit 1
  52. fi
  53. # Note the quotes around `$TEMP': they are essential!
  54. eval set -- "$TEMP"
  55. while true ; do
  56. case "$1" in
  57. -l|--length)
  58. keyLength="$2"
  59. shift 2
  60. ;;
  61. -e|--expire)
  62. keyExpire="$2"
  63. shift 2
  64. ;;
  65. --)
  66. shift
  67. ;;
  68. *)
  69. break
  70. ;;
  71. esac
  72. done
  73. if [ -z "$1" ] ; then
  74. # find all secret keys
  75. keyID=$(gpg --with-colons --list-secret-keys | grep ^sec | cut -f5 -d:)
  76. # if multiple sec keys exist, fail
  77. if (( $(echo "$keyID" | wc -l) > 1 )) ; then
  78. echo "Multiple secret keys found:"
  79. echo "$keyID"
  80. failure "Please specify which primary key to use."
  81. fi
  82. else
  83. keyID="$1"
  84. fi
  85. if [ -z "$keyID" ] ; then
  86. failure "You have no secret key available. You should create an OpenPGP
  87. key before joining the monkeysphere. You can do this with:
  88. gpg --gen-key"
  89. fi
  90. # get key output, and fail if not found
  91. gpgOut=$(gpg --quiet --fixed-list-mode --list-secret-keys --with-colons \
  92. "$keyID") || failure
  93. # fail if multiple sec lines are returned, which means the id
  94. # given is not unique
  95. if [ $(echo "$gpgOut" | grep '^sec:' | wc -l) -gt '1' ] ; then
  96. failure "Key ID '$keyID' is not unique."
  97. fi
  98. # prompt if an authentication subkey already exists
  99. if echo "$gpgOut" | egrep "^(sec|ssb):" | cut -d: -f 12 | grep -q a ; then
  100. echo "An authentication subkey already exists for key '$keyID'."
  101. read -p "Are you sure you would like to generate another one? (y/N) " OK; OK=${OK:N}
  102. if [ "${OK/y/Y}" != 'Y' ] ; then
  103. failure "aborting."
  104. fi
  105. fi
  106. # set subkey defaults
  107. # prompt about key expiration if not specified
  108. if [ -z "$keyExpire" ] ; then
  109. cat <<EOF
  110. Please specify how long the key should be valid.
  111. 0 = key does not expire
  112. <n> = key expires in n days
  113. <n>w = key expires in n weeks
  114. <n>m = key expires in n months
  115. <n>y = key expires in n years
  116. EOF
  117. while [ -z "$keyExpire" ] ; do
  118. read -p "Key is valid for? (0) " keyExpire
  119. if ! test_gpg_expire ${keyExpire:=0} ; then
  120. echo "invalid value"
  121. unset keyExpire
  122. fi
  123. done
  124. elif ! test_gpg_expire "$keyExpire" ; then
  125. failure "invalid key expiration value '$keyExpire'."
  126. fi
  127. # generate the list of commands that will be passed to edit-key
  128. editCommands=$(cat <<EOF
  129. addkey
  130. 7
  131. S
  132. E
  133. A
  134. Q
  135. $keyLength
  136. $keyExpire
  137. save
  138. EOF
  139. )
  140. log "generating subkey..."
  141. fifoDir=$(mktemp -d)
  142. (umask 077 && mkfifo "$fifoDir/pass")
  143. echo "$editCommands" | gpg --passphrase-fd 3 3< "$fifoDir/pass" --expert --command-fd 0 --edit-key "$keyID" &
  144. if [ "$DISPLAY" ] && which ssh-askpass >/dev/null; then
  145. ssh-askpass "Please enter your passphrase for $keyID: " > "$fifoDir/pass"
  146. else
  147. read -s -p "Please enter your passphrase for $keyID: " PASS
  148. echo "$PASS" > "$fifoDir/pass"
  149. fi
  150. rm -rf "$fifoDir"
  151. wait
  152. log "done."
  153. }
  154. ########################################################################
  155. # MAIN
  156. ########################################################################
  157. # unset variables that should be defined only in config file
  158. unset KEYSERVER
  159. unset CHECK_KEYSERVER
  160. unset KNOWN_HOSTS
  161. unset HASH_KNOWN_HOSTS
  162. unset AUTHORIZED_KEYS
  163. # load global config
  164. [ -r "${ETC}/monkeysphere.conf" ] && . "${ETC}/monkeysphere.conf"
  165. # set monkeysphere home directory
  166. MONKEYSPHERE_HOME=${MONKEYSPHERE_HOME:="${HOME}/.config/monkeysphere"}
  167. mkdir -p -m 0700 "$MONKEYSPHERE_HOME"
  168. # load local config
  169. [ -e ${MONKEYSPHERE_CONFIG:="${MONKEYSPHERE_HOME}/monkeysphere.conf"} ] && . "$MONKEYSPHERE_CONFIG"
  170. # set empty config variables with ones from the environment, or from
  171. # config file, or with defaults
  172. GNUPGHOME=${MONKEYSPHERE_GNUPGHOME:=${GNUPGHOME:="${HOME}/.gnupg"}}
  173. KEYSERVER=${MONKEYSPHERE_KEYSERVER:="$KEYSERVER"}
  174. # if keyserver not specified in env or monkeysphere.conf,
  175. # look in gpg.conf
  176. if [ -z "$KEYSERVER" ] ; then
  177. if [ -f "${GNUPGHOME}/gpg.conf" ] ; then
  178. KEYSERVER=$(grep -e "^[[:space:]]*keyserver " "${GNUPGHOME}/gpg.conf" | tail -1 | awk '{ print $2 }')
  179. fi
  180. fi
  181. # if it's still not specified, use the default
  182. KEYSERVER=${KEYSERVER:="subkeys.pgp.net"}
  183. CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=${CHECK_KEYSERVER:="true"}}
  184. KNOWN_HOSTS=${MONKEYSPHERE_KNOWN_HOSTS:=${KNOWN_HOSTS:="${HOME}/.ssh/known_hosts"}}
  185. HASH_KNOWN_HOSTS=${MONKEYSPHERE_HASH_KNOWN_HOSTS:=${HASH_KNOWN_HOSTS:="true"}}
  186. AUTHORIZED_KEYS=${MONKEYSPHERE_AUTHORIZED_KEYS:=${AUTHORIZED_KEYS:="${HOME}/.ssh/authorized_keys"}}
  187. # other variables not in config file
  188. AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:="${MONKEYSPHERE_HOME}/authorized_user_ids"}
  189. REQUIRED_HOST_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_HOST_KEY_CAPABILITY:="a"}
  190. REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
  191. # export GNUPGHOME and make sure gpg home exists with proper
  192. # permissions
  193. export GNUPGHOME
  194. mkdir -p -m 0700 "$GNUPGHOME"
  195. # get subcommand
  196. COMMAND="$1"
  197. [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
  198. shift
  199. case $COMMAND in
  200. 'update-known_hosts'|'update-known-hosts'|'k')
  201. MODE='known_hosts'
  202. # check permissions on the known_hosts file path
  203. if ! check_key_file_permissions "$USER" "$KNOWN_HOSTS" ; then
  204. failure "Improper permissions on known_hosts file path."
  205. fi
  206. # if hosts are specified on the command line, process just
  207. # those hosts
  208. if [ "$1" ] ; then
  209. update_known_hosts "$@"
  210. RETURN="$?"
  211. # otherwise, if no hosts are specified, process every host
  212. # in the user's known_hosts file
  213. else
  214. # exit if the known_hosts file does not exist
  215. if [ ! -e "$KNOWN_HOSTS" ] ; then
  216. log "known_hosts file '$KNOWN_HOSTS' does not exist."
  217. exit
  218. fi
  219. process_known_hosts
  220. RETURN="$?"
  221. fi
  222. ;;
  223. 'update-authorized_keys'|'update-authorized-keys'|'a')
  224. MODE='authorized_keys'
  225. # check permissions on the authorized_user_ids file path
  226. if ! check_key_file_permissions "$USER" "$AUTHORIZED_USER_IDS" ; then
  227. failure "Improper permissions on authorized_user_ids file path."
  228. fi
  229. # check permissions on the authorized_keys file path
  230. if ! check_key_file_permissions "$USER" "$AUTHORIZED_KEYS" ; then
  231. failure "Improper permissions on authorized_keys file path."
  232. fi
  233. # exit if the authorized_user_ids file is empty
  234. if [ ! -e "$AUTHORIZED_USER_IDS" ] ; then
  235. log "authorized_user_ids file '$AUTHORIZED_USER_IDS' does not exist."
  236. exit
  237. fi
  238. # process authorized_user_ids file
  239. process_authorized_user_ids "$AUTHORIZED_USER_IDS"
  240. RETURN="$?"
  241. ;;
  242. 'gen-subkey'|'g')
  243. gen_subkey "$@"
  244. ;;
  245. 'help'|'h'|'?')
  246. usage
  247. ;;
  248. *)
  249. failure "Unknown command: '$COMMAND'
  250. Type '$PGRM help' for usage."
  251. ;;
  252. esac
  253. exit "$RETURN"