summaryrefslogtreecommitdiff
path: root/src/monkeysphere-host
blob: b45b50ec60e38c9969426119c469e968751d5dbc (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 "Keyring contains multiple keys. Please specify one to act on (see 'monkeysphere-host show-key')."
  116. fi
  117. ;;
  118. esac
  119. printf '%s\n' "${fprs[@]}" | grep "${keyID}$" \
  120. || failure "Key '$keyID' not found."
  121. }
  122. # return 0 if user ID was found.
  123. # return 1 if user ID not found.
  124. check_key_userid() {
  125. local keyID="$1"
  126. local userID="$2"
  127. local tmpuidMatch
  128. # match to only "unknown" user IDs (host has no need for ultimate trust)
  129. tmpuidMatch="uid:-:$(echo $userID | gpg_escape)"
  130. # See whether the requsted user ID is present
  131. gpg_host_list_keys "$keyID" | cut -f1,2,10 -d: | \
  132. grep -q -x -F "$tmpuidMatch" 2>/dev/null
  133. }
  134. # run command looped over keys
  135. multi_key() {
  136. local cmd="$1"
  137. shift
  138. local keys=$@
  139. local i=0
  140. local fprs=($(host_fingerprints))
  141. local key
  142. check_no_keys
  143. if [[ -z "$1" || "$1" == '--all' ]] ; then
  144. keys="${fprs[@]}"
  145. else
  146. for key in $keys ; do
  147. printf '%s\n' "${fprs[@]}" | grep "${key}$" \
  148. || failure "Key '$key' not found."
  149. done
  150. fi
  151. for key in $keys ; do
  152. if (( i++ > 0 )) ; then
  153. echo "##############################"
  154. fi
  155. eval "$cmd" "$key"
  156. done
  157. }
  158. # show info about the a key
  159. show_key() {
  160. local id="$1"
  161. local GNUPGHOME
  162. local TMPSSH
  163. local fingerprint
  164. local revokers
  165. # tmp gpghome dir
  166. export GNUPGHOME=$(msmktempdir)
  167. # trap to remove tmp dir if break
  168. trap "rm -rf $GNUPGHOME" EXIT
  169. # import the host key into the tmp dir
  170. gpg --quiet --import <"$HOST_KEY_FILE"
  171. # create the ssh key
  172. TMPSSH="$GNUPGHOME"/ssh_host_key_rsa_pub
  173. gpg --export "$id" | openpgp2ssh 2>/dev/null >"$TMPSSH"
  174. # get the gpg fingerprint
  175. fingerprint=$(gpg --quiet --list-keys \
  176. --with-colons --with-fingerprint "$id" \
  177. | grep '^fpr:' | cut -d: -f10 )
  178. # list the host key info
  179. # FIXME: make no-show-keyring work so we don't have to do the grep'ing
  180. # FIXME: can we show uid validity somehow?
  181. gpg --list-keys --list-options show-unusable-uids "$id" 2>/dev/null \
  182. | grep -v "^${GNUPGHOME}/pubring.gpg$" \
  183. | egrep -v '^-+$'
  184. # list revokers, if there are any
  185. revokers=$(gpg --list-keys --with-colons --fixed-list-mode "$id" \
  186. | awk -F: '/^rvk:/{ print $10 }' )
  187. if [ "$revokers" ] ; then
  188. echo "The following keys are allowed to revoke this host key:"
  189. for key in $revokers ; do
  190. echo "revoker: $key"
  191. done
  192. echo
  193. fi
  194. # list the pgp fingerprint
  195. echo "OpenPGP fingerprint: $fingerprint"
  196. # list the ssh fingerprint
  197. echo -n "ssh fingerprint: "
  198. ssh-keygen -l -f "$TMPSSH" | awk '{ print $1, $2, $4 }'
  199. # remove the tmp file
  200. trap - EXIT
  201. rm -rf "$GNUPGHOME"
  202. }
  203. ########################################################################
  204. # MAIN
  205. ########################################################################
  206. # load configuration file
  207. [ -e ${MONKEYSPHERE_HOST_CONFIG:="${SYSCONFIGDIR}/monkeysphere-host.conf"} ] \
  208. && . "$MONKEYSPHERE_HOST_CONFIG"
  209. # set empty config variable with ones from the environment, or with
  210. # defaults
  211. LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=$LOG_LEVEL}
  212. KEYSERVER=${MONKEYSPHERE_KEYSERVER:=$KEYSERVER}
  213. CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=$CHECK_KEYSERVER}
  214. MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=$MONKEYSPHERE_USER}
  215. MONKEYSPHERE_GROUP=$(get_primary_group "$MONKEYSPHERE_USER")
  216. PROMPT=${MONKEYSPHERE_PROMPT:=$PROMPT}
  217. # other variables
  218. GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${MHDATADIR}"}
  219. LOG_PREFIX=${MONKEYSPHERE_LOG_PREFIX:='ms: '}
  220. # export variables needed in su invocation
  221. export DATE
  222. export LOG_LEVEL
  223. export KEYSERVER
  224. export CHECK_KEYSERVER
  225. export MONKEYSPHERE_USER
  226. export MONKEYSPHERE_GROUP
  227. export PROMPT
  228. export GNUPGHOME_HOST
  229. export GNUPGHOME
  230. export HOST_FINGERPRINT
  231. export LOG_PREFIX
  232. if [ "$#" -eq 0 ] ; then
  233. usage
  234. failure "Please supply a subcommand."
  235. fi
  236. # get subcommand
  237. COMMAND="$1"
  238. shift
  239. case $COMMAND in
  240. 'import-key'|'i')
  241. source "${MHSHAREDIR}/import_key"
  242. import_key "$@"
  243. ;;
  244. 'show-keys'|'show-key'|'show'|'s')
  245. multi_key show_key "$@"
  246. ;;
  247. 'set-expire'|'extend-key'|'e')
  248. source "${MHSHAREDIR}/set_expire"
  249. set_expire "$@"
  250. ;;
  251. 'add-servicename'|'add-hostname'|'add-name'|'n+')
  252. source "${MHSHAREDIR}/add_name"
  253. add_name "$@"
  254. ;;
  255. 'revoke-servicename'|'revoke-hostname'|'revoke-name'|'n-')
  256. source "${MHSHAREDIR}/revoke_name"
  257. revoke_name "$@"
  258. ;;
  259. 'add-revoker'|'r+')
  260. source "${MHSHAREDIR}/add_revoker"
  261. add_revoker "$@"
  262. ;;
  263. 'revoke-key')
  264. source "${MHSHAREDIR}/revoke_key"
  265. revoke_key "$@"
  266. ;;
  267. 'publish-keys'|'publish-key'|'publish'|'p')
  268. source "${MHSHAREDIR}/publish_key"
  269. multi_key publish_key "$@"
  270. ;;
  271. 'diagnostics'|'d')
  272. source "${MHSHAREDIR}/diagnostics"
  273. diagnostics
  274. ;;
  275. 'update-gpg-pub-file')
  276. update_gpg_pub_file
  277. ;;
  278. 'version'|'v')
  279. version
  280. ;;
  281. '--help'|'help'|'-h'|'h'|'?')
  282. usage
  283. ;;
  284. *)
  285. failure "Unknown command: '$COMMAND'
  286. Try '$PGRM help' for usage."
  287. ;;
  288. esac