summaryrefslogtreecommitdiff
path: root/src/monkeysphere-host
blob: eadd74bcedb1e4d1abdd896c199b7ebf4b18e63a (plain)
  1. #!/usr/bin/env bash
  2. # monkeysphere-host: Monkeysphere host admin 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-2010, and are all released under the GPL,
  11. # version 3 or later.
  12. ########################################################################
  13. set -e
  14. # set the pipefail option so pipelines fail on first command failure
  15. set -o pipefail
  16. PGRM=$(basename $0)
  17. SYSSHAREDIR=${MONKEYSPHERE_SYSSHAREDIR:-"/usr/share/monkeysphere"}
  18. export SYSSHAREDIR
  19. . "${SYSSHAREDIR}/defaultenv"
  20. . "${SYSSHAREDIR}/common"
  21. SYSDATADIR=${MONKEYSPHERE_SYSDATADIR:-"/var/lib/monkeysphere"}
  22. export SYSDATADIR
  23. # sharedir for host functions
  24. MHSHAREDIR="${SYSSHAREDIR}/mh"
  25. # datadir for host functions
  26. MHDATADIR="${SYSDATADIR}/host"
  27. # host pub key files
  28. HOST_KEY_FILE="${SYSDATADIR}/host_keys.pub.gpg"
  29. # host pub key fingerprints file
  30. HOST_KEY_FPR_FILE="${SYSDATADIR}/host_keys.fprs"
  31. # UTC date in ISO 8601 format if needed
  32. DATE=$(date -u '+%FT%T')
  33. # unset some environment variables that could screw things up
  34. unset GREP_OPTIONS
  35. ########################################################################
  36. # FUNCTIONS
  37. ########################################################################
  38. usage() {
  39. cat <<EOF >&2
  40. usage: $PGRM <subcommand> [options] [args]
  41. Monkeysphere host admin tool.
  42. subcommands:
  43. import-key (i) FILE SERVICENAME import PEM-encoded key from file
  44. show-keys (s) [KEYID ...] output host key information
  45. publish-keys (p) [KEYID ...] publish key(s) to keyserver
  46. set-expire (e) EXPIRE [KEYID] set key expiration
  47. add-servicename (n+) SERVICENAME [KEYID]
  48. add a service name to key
  49. revoke-servicename (n-) SERVICENAME [KEYID]
  50. revoke a service name from key
  51. add-revoker (r+) REVOKER_KEYID|FILE [KEYID]
  52. add a revoker to key
  53. revoke-key [KEYID] generate and/or publish revocation
  54. certificate for key
  55. version (v) show version number
  56. help (h,?) this help
  57. See ${PGRM}(8) for more info.
  58. EOF
  59. }
  60. # function to interact with the gpg keyring
  61. gpg_host() {
  62. GNUPGHOME="$GNUPGHOME_HOST" gpg --no-greeting --quiet --no-tty "$@"
  63. }
  64. # list the info about the a key, in colon format, to stdout
  65. gpg_host_list_keys() {
  66. gpg_host --list-keys --with-colons --fixed-list-mode \
  67. --with-fingerprint --with-fingerprint \
  68. "$1"
  69. }
  70. # edit key scripts, takes scripts on stdin, and keyID as first input
  71. gpg_host_edit() {
  72. gpg_host --command-fd 0 --edit-key "$@"
  73. }
  74. # export the monkeysphere gpg pub key file
  75. update_gpg_pub_file() {
  76. log debug "updating openpgp public key file '$HOST_KEY_FILE'..."
  77. gpg_host --export --armor --export-options export-minimal > "$HOST_KEY_FILE"
  78. log debug "updating fingerprint file '$HOST_KEY_FPR_FILE'..."
  79. gpg_host --list-secret-key --with-colons --with-fingerprint \
  80. | awk -F: '/^fpr:/{ print $10 }' > "$HOST_KEY_FPR_FILE"
  81. }
  82. host_fingerprints() {
  83. local fprs=($(cat "$HOST_KEY_FPR_FILE"))
  84. log debug "host key fingerprints:"
  85. printf '%s\n' "${fprs[@]}" | log debug
  86. printf '%s\n' "${fprs[@]}"
  87. }
  88. # check that the service name is well formed
  89. check_service_name() {
  90. local name="$1"
  91. log error "FIX ME: check service name"
  92. }
  93. # fail if host key not present
  94. check_no_keys() {
  95. [ -s "$HOST_KEY_FILE" ] || [ -s "$HOST_KEY_FPR_FILE" ] \
  96. || failure "You don't appear to have a Monkeysphere host key on this server.
  97. Please run 'monkeysphere-host import-key' import a key."
  98. }
  99. # key input to functions, outputs full fingerprint of specified key if
  100. # found
  101. check_key_input() {
  102. local keyID="$1"
  103. # array of fingerprints
  104. local fprs=($(host_fingerprints))
  105. case ${#fprs[@]} in
  106. 0)
  107. failure "You don't appear to have any Monkeysphere host keys.
  108. Please run 'monkeysphere-host import-key' to import a key."
  109. ;;
  110. 1)
  111. :
  112. ;;
  113. *)
  114. if [ -z "$keyID" ] ; then
  115. failure "Your host keyring contains multiple keys.
  116. Please specify one to act on (see 'monkeysphere-host show-key')."
  117. fi
  118. ;;
  119. esac
  120. printf '%s\n' "${fprs[@]}" | grep "${keyID}$" \
  121. || failure "Host key '$keyID' not found."
  122. }
  123. # return 0 if user ID was found.
  124. # return 1 if user ID not found.
  125. check_key_userid() {
  126. local keyID="$1"
  127. local userID="$2"
  128. local tmpuidMatch
  129. # match to only "unknown" user IDs (host has no need for ultimate trust)
  130. tmpuidMatch="uid:-:$(echo $userID | gpg_escape)"
  131. # See whether the requsted user ID is present
  132. gpg_host_list_keys "$keyID" | cut -f1,2,10 -d: | \
  133. grep -q -x -F "$tmpuidMatch" 2>/dev/null
  134. }
  135. # run command looped over keys
  136. multi_key() {
  137. local cmd="$1"
  138. shift
  139. local keys=$@
  140. local i=0
  141. local fprs=($(host_fingerprints))
  142. local key
  143. check_no_keys
  144. if [[ -z "$1" || "$1" == '--all' ]] ; then
  145. keys="${fprs[@]}"
  146. fi
  147. for key in $keys ; do
  148. if (( i++ > 0 )) ; then
  149. echo "##############################"
  150. fi
  151. eval "$cmd" "$key"
  152. done
  153. }
  154. # show info about the a key
  155. show_key() {
  156. local id="$1"
  157. local GNUPGHOME
  158. local fingerprint
  159. local tmpssh
  160. local revokers
  161. # tmp gpghome dir
  162. export GNUPGHOME=$(msmktempdir)
  163. # trap to remove tmp dir if break
  164. trap "rm -rf $GNUPGHOME" EXIT
  165. # import the host key into the tmp dir
  166. gpg --quiet --import <"$HOST_KEY_FILE"
  167. # get the gpg fingerprint
  168. if gpg --quiet --list-keys \
  169. --with-colons --with-fingerprint "$id" \
  170. | grep '^fpr:' | cut -d: -f10 > "$GNUPGHOME"/fingerprint ; then
  171. fingerprint=$(cat "$GNUPGHOME"/fingerprint)
  172. else
  173. failure "ID '$id' not found."
  174. fi
  175. # create the ssh key
  176. tmpssh="$GNUPGHOME"/ssh_host_key_rsa_pub
  177. gpg --export "$fingerprint" 2>/dev/null \
  178. | openpgp2ssh 2>/dev/null >"$tmpssh"
  179. # list the host key info
  180. # FIXME: make no-show-keyring work so we don't have to do the grep'ing
  181. # FIXME: can we show uid validity somehow?
  182. gpg --list-keys --list-options show-unusable-uids "$fingerprint" 2>/dev/null \
  183. | grep -v "^${GNUPGHOME}/pubring.gpg$" \
  184. | egrep -v '^-+$'
  185. # list revokers, if there are any
  186. revokers=$(gpg --list-keys --with-colons --fixed-list-mode "$fingerprint" \
  187. | awk -F: '/^rvk:/{ print $10 }' )
  188. if [ "$revokers" ] ; then
  189. echo "The following keys are allowed to revoke this host key:"
  190. for key in $revokers ; do
  191. echo "revoker: $key"
  192. done
  193. echo
  194. fi
  195. # list the pgp fingerprint
  196. echo "OpenPGP fingerprint: $fingerprint"
  197. # list the ssh fingerprint
  198. echo -n "ssh fingerprint: "
  199. ssh-keygen -l -f "$tmpssh" | awk '{ print $1, $2, $4 }'
  200. # remove the tmp file
  201. trap - EXIT
  202. rm -rf "$GNUPGHOME"
  203. }
  204. ########################################################################
  205. # MAIN
  206. ########################################################################
  207. # load configuration file
  208. [ -e ${MONKEYSPHERE_HOST_CONFIG:="${SYSCONFIGDIR}/monkeysphere-host.conf"} ] \
  209. && . "$MONKEYSPHERE_HOST_CONFIG"
  210. # set empty config variable with ones from the environment, or with
  211. # defaults
  212. LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=$LOG_LEVEL}
  213. KEYSERVER=${MONKEYSPHERE_KEYSERVER:=$KEYSERVER}
  214. CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=$CHECK_KEYSERVER}
  215. MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=$MONKEYSPHERE_USER}
  216. MONKEYSPHERE_GROUP=$(get_primary_group "$MONKEYSPHERE_USER")
  217. PROMPT=${MONKEYSPHERE_PROMPT:=$PROMPT}
  218. # other variables
  219. GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${MHDATADIR}"}
  220. LOG_PREFIX=${MONKEYSPHERE_LOG_PREFIX:='ms: '}
  221. # export variables needed in su invocation
  222. export DATE
  223. export LOG_LEVEL
  224. export KEYSERVER
  225. export CHECK_KEYSERVER
  226. export MONKEYSPHERE_USER
  227. export MONKEYSPHERE_GROUP
  228. export PROMPT
  229. export GNUPGHOME_HOST
  230. export GNUPGHOME
  231. export HOST_FINGERPRINT
  232. export LOG_PREFIX
  233. if [ "$#" -eq 0 ] ; then
  234. usage
  235. failure "Please supply a subcommand."
  236. fi
  237. # get subcommand
  238. COMMAND="$1"
  239. shift
  240. case $COMMAND in
  241. 'import-key'|'i')
  242. source "${MHSHAREDIR}/import_key"
  243. import_key "$@"
  244. ;;
  245. 'show-keys'|'show-key'|'show'|'s')
  246. multi_key show_key "$@"
  247. ;;
  248. 'set-expire'|'extend-key'|'e')
  249. source "${MHSHAREDIR}/set_expire"
  250. set_expire "$@"
  251. ;;
  252. 'add-servicename'|'add-hostname'|'add-name'|'n+')
  253. source "${MHSHAREDIR}/add_name"
  254. add_name "$@"
  255. ;;
  256. 'revoke-servicename'|'revoke-hostname'|'revoke-name'|'n-')
  257. source "${MHSHAREDIR}/revoke_name"
  258. revoke_name "$@"
  259. ;;
  260. 'add-revoker'|'r+')
  261. source "${MHSHAREDIR}/add_revoker"
  262. add_revoker "$@"
  263. ;;
  264. 'revoke-key')
  265. source "${MHSHAREDIR}/revoke_key"
  266. revoke_key "$@"
  267. ;;
  268. 'publish-keys'|'publish-key'|'publish'|'p')
  269. source "${MHSHAREDIR}/publish_key"
  270. multi_key publish_key "$@"
  271. ;;
  272. 'diagnostics'|'d')
  273. source "${MHSHAREDIR}/diagnostics"
  274. diagnostics
  275. ;;
  276. 'update-gpg-pub-file')
  277. update_gpg_pub_file
  278. ;;
  279. 'version'|'v')
  280. version
  281. ;;
  282. '--help'|'help'|'-h'|'h'|'?')
  283. usage
  284. ;;
  285. *)
  286. failure "Unknown command: '$COMMAND'
  287. Try '$PGRM help' for usage."
  288. ;;
  289. esac