summaryrefslogtreecommitdiff
path: root/src/monkeysphere
blob: cf7752adac72385cb58c8dc370b2dbda63269ac7 (plain)
  1. #!/usr/bin/env bash
  2. # monkeysphere: Monkeysphere client tool
  3. #
  4. # The monkeysphere scripts are written by:
  5. # Jameson Rollins <jrollins@finestructure.net>
  6. # Jamie McClelland <jm@mayfirst.org>
  7. # Daniel Kahn Gillmor <dkg@fifthhorseman.net>
  8. # Micah Anderson <micah@riseup.net>
  9. #
  10. # They are Copyright 2008-2009, and are all released under the GPL, version 3
  11. # or later.
  12. ########################################################################
  13. set -e
  14. PGRM=$(basename $0)
  15. SYSSHAREDIR=${MONKEYSPHERE_SYSSHAREDIR:-"__SYSSHAREDIR_PREFIX__/share/monkeysphere"}
  16. export SYSSHAREDIR
  17. . "${SYSSHAREDIR}/defaultenv"
  18. . "${SYSSHAREDIR}/common"
  19. # sharedir for host functions
  20. MSHAREDIR="${SYSSHAREDIR}/m"
  21. # UTC date in ISO 8601 format if needed
  22. DATE=$(date -u '+%FT%T')
  23. # unset some environment variables that could screw things up
  24. unset GREP_OPTIONS
  25. # set the file creation mask to be only owner rw
  26. umask 077
  27. ########################################################################
  28. # FUNCTIONS
  29. ########################################################################
  30. usage() {
  31. cat <<EOF >&2
  32. usage: $PGRM <subcommand> [options] [args]
  33. Monkeysphere client tool.
  34. subcommands:
  35. update-known_hosts (k) [HOST]... update known_hosts file
  36. update-authorized_keys (a) update authorized_keys file
  37. ssh-proxycommand HOST [PORT] monkeysphere ssh ProxyCommand
  38. --no-connect do not make TCP connection to host
  39. subkey-to-ssh-agent (s) store authentication subkey in ssh-agent
  40. keys-for-userid (u) USERID output valid ssh keys for given user id
  41. sshfprs-for-userid USERID output ssh fingerprints for given user id
  42. gen-subkey (g) [KEYID] generate an authentication subkey
  43. --length (-l) BITS key length in bits (2048)
  44. version (v) show version number
  45. help (h,?) this help
  46. EOF
  47. }
  48. # user gpg command to define common options
  49. gpg_user() {
  50. LC_ALL=C gpg --fixed-list-mode --no-greeting --quiet --no-tty "$@"
  51. }
  52. # output the ssh fingerprint of a gpg key
  53. gpg_ssh_fingerprint() {
  54. keyid="$1"
  55. gpg_user --export "$keyid" --no-armor | "$SYSSHAREDIR/keytrans" openpgp2sshfpr "$keyid"
  56. }
  57. # take a secret key ID and check that only zero or one ID is provided,
  58. # and that it corresponds to only a single secret key ID
  59. check_gpg_sec_key_id() {
  60. local gpgSecOut
  61. case "$#" in
  62. 0)
  63. gpgSecOut=$(gpg_user --list-secret-keys --with-colons 2>/dev/null | egrep '^sec:')
  64. ;;
  65. 1)
  66. gpgSecOut=$(gpg_user --list-secret-keys --with-colons "$1" | egrep '^sec:') || failure
  67. ;;
  68. *)
  69. failure "You must specify only a single primary key ID."
  70. ;;
  71. esac
  72. # check that only a single secret key was found
  73. case $(echo "$gpgSecOut" | grep -c '^sec:') in
  74. 0)
  75. failure "No secret keys found. Create an OpenPGP key with the following command:
  76. gpg --gen-key"
  77. ;;
  78. 1)
  79. echo "$gpgSecOut" | cut -d: -f5
  80. ;;
  81. *)
  82. local seckeys=$(echo "$gpgSecOut" | cut -d: -f5)
  83. failure "Multiple primary secret keys found:
  84. $seckeys
  85. Please specify which primary key to use."
  86. ;;
  87. esac
  88. }
  89. # check that a valid authentication subkey does not already exist
  90. check_gpg_authentication_subkey() {
  91. local keyID
  92. local IFS
  93. local line
  94. local type
  95. local validity
  96. local usage
  97. keyID="$1"
  98. # check that a valid authentication key does not already exist
  99. IFS=$'\n'
  100. for line in $(gpg_user --list-keys --with-colons "$keyID") ; do
  101. type=$(echo "$line" | cut -d: -f1)
  102. validity=$(echo "$line" | cut -d: -f2)
  103. usage=$(echo "$line" | cut -d: -f12)
  104. # look at keys only
  105. if [ "$type" != 'pub' -a "$type" != 'sub' ] ; then
  106. continue
  107. fi
  108. # check for authentication capability
  109. if ! check_capability "$usage" 'a' ; then
  110. continue
  111. fi
  112. # if authentication key is valid, prompt to continue
  113. if [ "$validity" = 'u' ] ; then
  114. echo "A valid authentication key already exists for primary key '$keyID'." 1>&2
  115. if [ "$PROMPT" != "false" ] ; then
  116. printf "Are you sure you would like to generate another one? (y/N) " >&2
  117. read OK; OK=${OK:N}
  118. if [ "${OK/y/Y}" != 'Y' ] ; then
  119. failure "aborting."
  120. fi
  121. break
  122. else
  123. failure "aborting."
  124. fi
  125. fi
  126. done
  127. }
  128. ########################################################################
  129. # MAIN
  130. ########################################################################
  131. # set unset default variables
  132. GNUPGHOME=${GNUPGHOME:="${HOME}/.gnupg"}
  133. KNOWN_HOSTS="${HOME}/.ssh/known_hosts"
  134. HASH_KNOWN_HOSTS="false"
  135. AUTHORIZED_KEYS="${HOME}/.ssh/authorized_keys"
  136. # unset the check keyserver variable, since that needs to have
  137. # different defaults for the different functions
  138. unset CHECK_KEYSERVER
  139. # load global config
  140. [ -r "${SYSCONFIGDIR}/monkeysphere.conf" ] \
  141. && . "${SYSCONFIGDIR}/monkeysphere.conf"
  142. # set monkeysphere home directory
  143. MONKEYSPHERE_HOME=${MONKEYSPHERE_HOME:="${HOME}/.monkeysphere"}
  144. mkdir -p -m 0700 "$MONKEYSPHERE_HOME"
  145. # load local config
  146. [ -e ${MONKEYSPHERE_CONFIG:="${MONKEYSPHERE_HOME}/monkeysphere.conf"} ] \
  147. && . "$MONKEYSPHERE_CONFIG"
  148. # set empty config variables with ones from the environment
  149. GNUPGHOME=${MONKEYSPHERE_GNUPGHOME:=$GNUPGHOME}
  150. LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=$LOG_LEVEL}
  151. KEYSERVER=${MONKEYSPHERE_KEYSERVER:=$KEYSERVER}
  152. # if keyserver not specified in env or conf, then look in gpg.conf
  153. if [ -z "$KEYSERVER" ] ; then
  154. if [ -f "${GNUPGHOME}/gpg.conf" ] ; then
  155. KEYSERVER=$(grep -e "^[[:space:]]*keyserver " "${GNUPGHOME}/gpg.conf" | tail -1 | awk '{ print $2 }')
  156. fi
  157. fi
  158. PROMPT=${MONKEYSPHERE_PROMPT:=$PROMPT}
  159. KNOWN_HOSTS=${MONKEYSPHERE_KNOWN_HOSTS:=$KNOWN_HOSTS}
  160. HASH_KNOWN_HOSTS=${MONKEYSPHERE_HASH_KNOWN_HOSTS:=$HASH_KNOWN_HOSTS}
  161. AUTHORIZED_KEYS=${MONKEYSPHERE_AUTHORIZED_KEYS:=$AUTHORIZED_KEYS}
  162. STRICT_MODES=${MONKEYSPHERE_STRICT_MODES:=$STRICT_MODES}
  163. # other variables not in config file
  164. AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:="${MONKEYSPHERE_HOME}/authorized_user_ids"}
  165. REQUIRED_HOST_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_HOST_KEY_CAPABILITY:="a"}
  166. REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
  167. # note that only using '=' instead of ':=' tests only if the variable
  168. # in unset, not if it's "null"
  169. LOG_PREFIX=${MONKEYSPHERE_LOG_PREFIX='ms: '}
  170. # export GNUPGHOME and make sure gpg home exists with proper
  171. # permissions
  172. export GNUPGHOME
  173. mkdir -p -m 0700 "$GNUPGHOME"
  174. export LOG_LEVEL
  175. export LOG_PREFIX
  176. if [ "$#" -eq 0 ] ; then
  177. usage
  178. failure "Please supply a subcommand."
  179. fi
  180. # get subcommand
  181. COMMAND="$1"
  182. shift
  183. case $COMMAND in
  184. 'update-known_hosts'|'update-known-hosts'|'k')
  185. # whether or not to check keyservers
  186. CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=${CHECK_KEYSERVER:="true"}}
  187. source "${MSHAREDIR}/update_known_hosts"
  188. # if hosts are specified on the command line, process just
  189. # those hosts
  190. if [ "$1" ] ; then
  191. update_known_hosts "$@"
  192. # otherwise, if no hosts are specified, process every host
  193. # in the user's known_hosts file
  194. else
  195. process_known_hosts
  196. fi
  197. ;;
  198. 'update-authorized_keys'|'update-authorized-keys'|'a')
  199. # whether or not to check keyservers
  200. CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=${CHECK_KEYSERVER:="true"}}
  201. source "${MSHAREDIR}/update_authorized_keys"
  202. update_authorized_keys
  203. ;;
  204. 'import-subkey'|'import'|'i')
  205. source "${MSHAREDIR}/import_subkey"
  206. import_subkey "$@"
  207. ;;
  208. 'gen-subkey'|'g')
  209. source "${MSHAREDIR}/gen_subkey"
  210. gen_subkey "$@"
  211. ;;
  212. 'ssh-proxycommand'|'p')
  213. source "${MSHAREDIR}/ssh_proxycommand"
  214. ssh_proxycommand "$@"
  215. ;;
  216. 'subkey-to-ssh-agent'|'s')
  217. source "${MSHAREDIR}/subkey_to_ssh_agent"
  218. subkey_to_ssh_agent "$@"
  219. ;;
  220. 'sshfpr')
  221. echo "Warning: 'sshfpr' is deprecated. Please use 'sshfprs-for-userid' instead." >&2
  222. gpg_ssh_fingerprint "$@"
  223. ;;
  224. 'keys-for-userid'|'u')
  225. CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=${CHECK_KEYSERVER:="true"}}
  226. source "${MSHAREDIR}/keys_for_userid"
  227. keys_for_userid "$@"
  228. ;;
  229. 'sshfprs-for-userid')
  230. CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=${CHECK_KEYSERVER:="true"}}
  231. source "${MSHAREDIR}/keys_for_userid"
  232. keys_for_userid "$@" | "$SYSSHAREDIR/keytrans" sshfpr
  233. ;;
  234. 'keys-from-userid')
  235. echo "Warning: 'keys-from-userid' is deprecated. Please use 'keys-for-userid' instead." >&2
  236. CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=${CHECK_KEYSERVER:="true"}}
  237. source "${MSHAREDIR}/keys_for_userid"
  238. keys_for_userid "$@"
  239. ;;
  240. 'version'|'--version'|'v')
  241. version
  242. ;;
  243. 'help'|'--help'|'-h'|'h'|'?')
  244. usage
  245. ;;
  246. *)
  247. failure "Unknown command: '$COMMAND'
  248. Try '$PGRM help' for usage."
  249. ;;
  250. esac