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