summaryrefslogtreecommitdiff
path: root/src/monkeysphere-host
blob: a49823d540691d722cc19555c893e691490b6429 (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:-"__SYSSHAREDIR_PREFIX__/share/monkeysphere"}
  18. export SYSSHAREDIR
  19. . "${SYSSHAREDIR}/defaultenv"
  20. . "${SYSSHAREDIR}/common"
  21. # sharedir for host functions
  22. MHSHAREDIR="${SYSSHAREDIR}/mh"
  23. # datadir for host functions
  24. MHDATADIR="${SYSDATADIR}/host"
  25. # host pub key files
  26. HOST_KEY_FILE="${SYSDATADIR}/host_keys.pub.pgp"
  27. # UTC date in ISO 8601 format if needed
  28. DATE=$(date -u '+%FT%T')
  29. # unset some environment variables that could screw things up
  30. unset GREP_OPTIONS
  31. ########################################################################
  32. # FUNCTIONS
  33. ########################################################################
  34. usage() {
  35. cat <<EOF >&2
  36. usage: $PGRM <subcommand> [options] [args]
  37. Monkeysphere host admin tool.
  38. subcommands:
  39. import-key (i) FILE SERVICENAME import PEM-encoded key from file
  40. show-keys (s) [KEYID ...] output host key information
  41. publish-keys (p) [KEYID ...] publish key(s) to keyserver
  42. set-expire (e) EXPIRE [KEYID] set key expiration
  43. add-servicename (n+) SERVICENAME [KEYID]
  44. add a service name to key
  45. revoke-servicename (n-) SERVICENAME [KEYID]
  46. revoke a service name from key
  47. add-revoker (r+) REVOKER_KEYID|FILE [KEYID]
  48. add a revoker to key
  49. revoke-key [KEYID] generate and/or publish revocation
  50. certificate for key
  51. version (v) show version number
  52. help (h,?) this help
  53. See ${PGRM}(8) for more info.
  54. EOF
  55. }
  56. # function to interact with the gpg keyring
  57. gpg_host() {
  58. GNUPGHOME="$GNUPGHOME_HOST" LC_ALL=C gpg --no-auto-check-trustdb --trust-model=always --no-greeting --quiet --no-tty --fixed-list-mode "$@"
  59. }
  60. # list the info about the a key, in colon format, to stdout
  61. gpg_host_list_keys() {
  62. if [ "$1" ] ; then
  63. gpg_host --list-keys --with-colons \
  64. --with-fingerprint --with-fingerprint \
  65. "$1"
  66. else
  67. gpg_host --list-keys --with-colons \
  68. --with-fingerprint --with-fingerprint
  69. fi
  70. }
  71. # edit key scripts, takes scripts on stdin, and keyID as first input
  72. gpg_host_edit() {
  73. gpg_host --command-fd 0 --edit-key "$@"
  74. }
  75. # export the monkeysphere OpenPGP pub key file
  76. update_pgp_pub_file() {
  77. log debug "updating openpgp public key file '$HOST_KEY_FILE'..."
  78. gpg_host --export --armor --export-options export-minimal \
  79. $(gpg_host --list-secret-keys --with-colons --fingerprint | grep ^fpr | cut -f10 -d:) \
  80. > "$HOST_KEY_FILE"
  81. }
  82. # check that the service name is well formed. we assume that the
  83. # service name refers to a host; DNS labels for host names are limited
  84. # to a very small range of characters (see RFC 1912, section 2.1).
  85. # FIXME: i'm failing to check here for label components that are
  86. # all-number (e.g. ssh://666.666), which are technically not allowed
  87. # (though some exist on the 'net, apparently)
  88. # FIXME: this will probably misbehave if raw IP addresses are provided,
  89. # either IPv4 or IPv6 using the bracket notation.
  90. # FIXME: this doesn't address the use of hashed User IDs.
  91. check_service_name() {
  92. local name="$1"
  93. local errs=""
  94. local scheme
  95. local port
  96. local assigned_ports
  97. [ -n "$name" ] || \
  98. failure "You must supply a service name to check"
  99. printf '%s' "$name" | perl -n -e '($str = $_) =~ s/\s//g ; exit !(lc($str) eq $_);' || \
  100. failure "Not a valid service name: '$name'
  101. Service names should be canonicalized to all lower-case,
  102. with no whitespace"
  103. [[ "$name" =~ ^[a-z0-9./:-]+$ ]] || \
  104. failure "Not a valid service name: '$name'
  105. Service names should contain only lower-case ASCII letters
  106. numbers, dots (.), hyphens (-), slashes (/), and a colon (:).
  107. If you are using non-ASCII characters (e.g. IDN), you should
  108. use the canonicalized ASCII (NAMEPREP -> Punycode) representation
  109. (see RFC 3490)."
  110. [[ "$name" =~ \. ]] || \
  111. failure "Not a valid service name: '$name'
  112. Service names should use fully-qualified domain names (FQDN), but the
  113. domain name you chose appears to only have the local part. For
  114. example: don't use 'ssh://foo' ; use 'ssh://foo.example.com' instead."
  115. [[ "$name" =~ ^[a-z0-9]([a-z0-9-]*[a-z0-9])?://[a-z0-9]([a-z0-9-]*[a-z0-9])?(\.|((\.[a-z0-9]([a-z0-9-]*[a-z0-9])?)+))(:[1-9][0-9]{0,4})?$ ]] || \
  116. failure "Not a valid service name: '$name'
  117. Service names look like <scheme>://full.example.com[:<portnumber>],
  118. where <scheme> is something like ssh or https, and <portnumber> is
  119. a decimal number (supplied only if the service is on a non-standard
  120. port)."
  121. scheme=$(cut -f1 -d: <<<"$name")
  122. port=$(cut -f3 -d: <<<"$name")
  123. # check that the scheme name is found in the system services
  124. # database
  125. available_=$(get_port_for_service "$scheme") || \
  126. log error "Error looking up service scheme named '%s'" "$scheme"
  127. # FIXME: if the service isn't found, or does not have a port, what
  128. # should we do? at the moment, we're just warning.
  129. if [ -n "$port" ]; then
  130. # check that the port number is a legitimate port number (> 0, < 65536)
  131. [ "$port" -gt 0 ] && [ "$port" -lt 65536 ] || \
  132. failure "The given port number should be greater than 0 and
  133. less than 65536. '$port' is not OK"
  134. # if the port number is given, and the scheme is in the services
  135. # database, check that the port number does *not* match the
  136. # default port.
  137. if (printf '%s' "$assigned_ports" | grep -q -F -x "$port" ) ; then
  138. failure $(printf "The scheme %s uses port number %d by default.
  139. You should leave off the port number if it is the default" "$scheme" "$port")
  140. fi
  141. fi
  142. }
  143. # fail if host key not present
  144. check_no_keys() {
  145. [ -s "$HOST_KEY_FILE" ] \
  146. || failure "You don't appear to have a Monkeysphere host key on this server.
  147. Please run 'monkeysphere-host import-key' import a key."
  148. }
  149. # key input to functions, outputs full fingerprint of specified key if
  150. # found
  151. check_key_input() {
  152. local keyID="$1"
  153. # array of fingerprints
  154. local fprs=($(list_primary_fingerprints <"$HOST_KEY_FILE"))
  155. case ${#fprs[@]} in
  156. 0)
  157. failure "You don't appear to have any Monkeysphere host keys.
  158. Please run 'monkeysphere-host import-key' to import a key."
  159. ;;
  160. 1)
  161. :
  162. ;;
  163. *)
  164. if [ -z "$keyID" ] ; then
  165. failure "Your host keyring contains multiple keys.
  166. Please specify one to act on (see 'monkeysphere-host show-keys')."
  167. fi
  168. ;;
  169. esac
  170. printf '%s\n' "${fprs[@]}" | grep "${keyID}$" \
  171. || failure "Host key '$keyID' not found."
  172. }
  173. # return 0 if user ID was found.
  174. # return 1 if user ID not found.
  175. check_key_userid() {
  176. local keyID="$1"
  177. local userID="$2"
  178. local tmpuidMatch
  179. # match to only "unknown" user IDs (host has no need for ultimate trust)
  180. tmpuidMatch="uid:-:$(echo $userID | gpg_escape)"
  181. # See whether the requsted user ID is present
  182. gpg_host_list_keys "$keyID" | cut -f1,2,10 -d: | \
  183. grep -q -x -F "$tmpuidMatch" 2>/dev/null
  184. }
  185. prompt_userid_exists() {
  186. local userID="$1"
  187. local gpgOut
  188. local fingerprint
  189. if gpgOut=$(gpg_host_list_keys "=${userID}" 2>/dev/null) ; then
  190. fingerprint=$(echo "$gpgOut" | grep '^fpr:' | cut -d: -f10)
  191. if [ "$PROMPT" != "false" ] ; then
  192. printf "Service name '%s' is already being used by key '%s'.\nAre you sure you want to use it again? (y/N) " "$userID" "$fingerprint" >&2
  193. read OK; OK=${OK:=N}
  194. if [ "${OK/y/Y}" != 'Y' ] ; then
  195. failure "Service name not added."
  196. fi
  197. else
  198. log info "Key '%s' is already using the service name '%s'." "$fingerprint" "$userID" >&2
  199. fi
  200. fi
  201. }
  202. # run command looped over keys
  203. multi_key() {
  204. local cmd="$1"
  205. shift
  206. local keys=$@
  207. local i=0
  208. local key
  209. check_no_keys
  210. log debug "listing primary fingerprints from $HOST_KEY_FILE"
  211. local fprs=($(list_primary_fingerprints <"$HOST_KEY_FILE"))
  212. log debug "obtained the following fingerprints: $fprs"
  213. if [[ -z "$1" || "$1" == '--all' ]] ; then
  214. log debug "publishing all keys"
  215. keys="${fprs[@]}"
  216. fi
  217. log debug "using keys: $keys"
  218. for key in $keys ; do
  219. if (( i++ > 0 )) ; then
  220. printf "\n"
  221. fi
  222. log debug "invoking $cmd $key"
  223. "$cmd" "$key"
  224. done
  225. }
  226. # show info about the a key
  227. show_key() {
  228. local id="$1"
  229. local GNUPGHOME
  230. local fingerprint
  231. local revokers
  232. # tmp gpghome dir
  233. export GNUPGHOME=$(msmktempdir)
  234. # trap to remove tmp dir if break
  235. trap "rm -rf $GNUPGHOME" EXIT
  236. # import the host key into the tmp dir
  237. gpg --quiet --import <"$HOST_KEY_FILE"
  238. # get the gpg fingerprint
  239. if gpg --quiet --list-keys \
  240. --with-colons --with-fingerprint "$id" \
  241. | grep '^fpr:' | cut -d: -f10 > "$GNUPGHOME"/fingerprint ; then
  242. fingerprint=$(cat "$GNUPGHOME"/fingerprint)
  243. else
  244. failure "ID '$id' not found."
  245. fi
  246. # list the host key info
  247. # FIXME: make no-show-keyring work so we don't have to do the grep'ing
  248. # FIXME: can we show uid validity somehow?
  249. gpg --list-keys --list-options show-unusable-uids "$fingerprint" 2>/dev/null \
  250. | grep -v "^${GNUPGHOME}/pubring.gpg$" \
  251. | egrep -v '^-+$' \
  252. | grep -v '^$'
  253. # list revokers, if there are any
  254. revokers=$(gpg --list-keys --with-colons --fixed-list-mode "$fingerprint" \
  255. | awk -F: '/^rvk:/{ print $10 }' )
  256. if [ "$revokers" ] ; then
  257. echo "The following keys are allowed to revoke this host key:"
  258. for key in $revokers ; do
  259. echo "revoker: $key"
  260. done
  261. fi
  262. # list the pgp fingerprint
  263. echo "OpenPGP fingerprint: $fingerprint"
  264. # list the ssh fingerprint
  265. printf "ssh fingerprint: %s\n" \
  266. "$(gpg --export --no-armor "$fingerprint" 2>/dev/null | "$SYSSHAREDIR/keytrans" openpgp2sshfpr "$fingerprint")"
  267. # remove the tmp file
  268. trap - EXIT
  269. rm -rf "$GNUPGHOME"
  270. }
  271. ########################################################################
  272. # MAIN
  273. ########################################################################
  274. # load configuration file
  275. [ -e ${MONKEYSPHERE_HOST_CONFIG:="${SYSCONFIGDIR}/monkeysphere-host.conf"} ] \
  276. && . "$MONKEYSPHERE_HOST_CONFIG"
  277. # set empty config variable with ones from the environment, or with
  278. # defaults
  279. LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=$LOG_LEVEL}
  280. KEYSERVER=${MONKEYSPHERE_KEYSERVER:=$KEYSERVER}
  281. log debug "using keyserver: $KEYSERVER"
  282. CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=$CHECK_KEYSERVER}
  283. MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=$MONKEYSPHERE_USER}
  284. MONKEYSPHERE_GROUP=$(get_primary_group "$MONKEYSPHERE_USER")
  285. PROMPT=${MONKEYSPHERE_PROMPT:=$PROMPT}
  286. # other variables
  287. GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${MHDATADIR}"}
  288. LOG_PREFIX=${MONKEYSPHERE_LOG_PREFIX:='ms: '}
  289. # export variables needed in su invocation
  290. export DATE
  291. export LOG_LEVEL
  292. export KEYSERVER
  293. export CHECK_KEYSERVER
  294. export MONKEYSPHERE_USER
  295. export MONKEYSPHERE_GROUP
  296. export PROMPT
  297. export GNUPGHOME_HOST
  298. export GNUPGHOME
  299. export HOST_FINGERPRINT
  300. export LOG_PREFIX
  301. if [ "$#" -eq 0 ] ; then
  302. usage
  303. failure "Please supply a subcommand."
  304. fi
  305. # get subcommand
  306. COMMAND="$1"
  307. shift
  308. case $COMMAND in
  309. 'import-key'|'import'|'i')
  310. source "${MHSHAREDIR}/import_key"
  311. import_key "$@"
  312. ;;
  313. 'show-keys'|'show-key'|'show'|'s')
  314. multi_key show_key "$@"
  315. ;;
  316. 'set-expire'|'extend-key'|'extend'|'e')
  317. source "${MHSHAREDIR}/set_expire"
  318. set_expire "$@"
  319. ;;
  320. 'add-servicename'|'add-hostname'|'add-name'|'n+')
  321. source "${MHSHAREDIR}/add_name"
  322. add_name "$@"
  323. ;;
  324. 'revoke-servicename'|'revoke-hostname'|'revoke-name'|'n-')
  325. source "${MHSHAREDIR}/revoke_name"
  326. revoke_name "$@"
  327. ;;
  328. 'add-revoker'|'r+')
  329. source "${MHSHAREDIR}/add_revoker"
  330. add_revoker "$@"
  331. ;;
  332. 'revoke-key')
  333. source "${MHSHAREDIR}/revoke_key"
  334. revoke_key "$@"
  335. ;;
  336. 'publish-keys'|'publish-key'|'publish'|'p')
  337. source "${MHSHAREDIR}/publish_key"
  338. multi_key publish_key "$@"
  339. ;;
  340. 'diagnostics'|'d')
  341. source "${MHSHAREDIR}/diagnostics"
  342. diagnostics
  343. ;;
  344. 'update-pgp-pub-file')
  345. update_pgp_pub_file
  346. ;;
  347. 'version'|'--version'|'v')
  348. version
  349. ;;
  350. '--help'|'help'|'-h'|'h'|'?')
  351. usage
  352. ;;
  353. *)
  354. failure "Unknown command: '$COMMAND'
  355. Try '$PGRM help' for usage."
  356. ;;
  357. esac