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