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