summaryrefslogtreecommitdiff
path: root/src/monkeysphere-host
blob: 6216dce8d9c67da68f99fb391a6ab41472b14dfe (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 --trust-model=always --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 revokers
  229. # tmp gpghome dir
  230. export GNUPGHOME=$(msmktempdir)
  231. # trap to remove tmp dir if break
  232. trap "rm -rf $GNUPGHOME" EXIT
  233. # import the host key into the tmp dir
  234. gpg --quiet --import <"$HOST_KEY_FILE"
  235. # get the gpg fingerprint
  236. if gpg --quiet --list-keys \
  237. --with-colons --with-fingerprint "$id" \
  238. | grep '^fpr:' | cut -d: -f10 > "$GNUPGHOME"/fingerprint ; then
  239. fingerprint=$(cat "$GNUPGHOME"/fingerprint)
  240. else
  241. failure "ID '$id' not found."
  242. fi
  243. # list the host key info
  244. # FIXME: make no-show-keyring work so we don't have to do the grep'ing
  245. # FIXME: can we show uid validity somehow?
  246. gpg --list-keys --list-options show-unusable-uids "$fingerprint" 2>/dev/null \
  247. | grep -v "^${GNUPGHOME}/pubring.gpg$" \
  248. | egrep -v '^-+$' \
  249. | grep -v '^$'
  250. # list revokers, if there are any
  251. revokers=$(gpg --list-keys --with-colons --fixed-list-mode "$fingerprint" \
  252. | awk -F: '/^rvk:/{ print $10 }' )
  253. if [ "$revokers" ] ; then
  254. echo "The following keys are allowed to revoke this host key:"
  255. for key in $revokers ; do
  256. echo "revoker: $key"
  257. done
  258. fi
  259. # list the pgp fingerprint
  260. echo "OpenPGP fingerprint: $fingerprint"
  261. # list the ssh fingerprint
  262. printf "ssh fingerprint: %s\n" \
  263. "$(gpg --export --no-armor "$fingerprint" 2>/dev/null | "$SYSSHAREDIR/keytrans" openpgp2sshfpr "$fingerprint")"
  264. # remove the tmp file
  265. trap - EXIT
  266. rm -rf "$GNUPGHOME"
  267. }
  268. ########################################################################
  269. # MAIN
  270. ########################################################################
  271. # load configuration file
  272. [ -e ${MONKEYSPHERE_HOST_CONFIG:="${SYSCONFIGDIR}/monkeysphere-host.conf"} ] \
  273. && . "$MONKEYSPHERE_HOST_CONFIG"
  274. # set empty config variable with ones from the environment, or with
  275. # defaults
  276. LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=$LOG_LEVEL}
  277. KEYSERVER=${MONKEYSPHERE_KEYSERVER:=$KEYSERVER}
  278. CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=$CHECK_KEYSERVER}
  279. MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=$MONKEYSPHERE_USER}
  280. MONKEYSPHERE_GROUP=$(get_primary_group "$MONKEYSPHERE_USER")
  281. PROMPT=${MONKEYSPHERE_PROMPT:=$PROMPT}
  282. # other variables
  283. GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${MHDATADIR}"}
  284. LOG_PREFIX=${MONKEYSPHERE_LOG_PREFIX:='ms: '}
  285. # export variables needed in su invocation
  286. export DATE
  287. export LOG_LEVEL
  288. export KEYSERVER
  289. export CHECK_KEYSERVER
  290. export MONKEYSPHERE_USER
  291. export MONKEYSPHERE_GROUP
  292. export PROMPT
  293. export GNUPGHOME_HOST
  294. export GNUPGHOME
  295. export HOST_FINGERPRINT
  296. export LOG_PREFIX
  297. if [ "$#" -eq 0 ] ; then
  298. usage
  299. failure "Please supply a subcommand."
  300. fi
  301. # get subcommand
  302. COMMAND="$1"
  303. shift
  304. case $COMMAND in
  305. 'import-key'|'import'|'i')
  306. source "${MHSHAREDIR}/import_key"
  307. import_key "$@"
  308. ;;
  309. 'show-keys'|'show-key'|'show'|'s')
  310. multi_key show_key "$@"
  311. ;;
  312. 'set-expire'|'extend-key'|'extend'|'e')
  313. source "${MHSHAREDIR}/set_expire"
  314. set_expire "$@"
  315. ;;
  316. 'add-servicename'|'add-hostname'|'add-name'|'n+')
  317. source "${MHSHAREDIR}/add_name"
  318. add_name "$@"
  319. ;;
  320. 'revoke-servicename'|'revoke-hostname'|'revoke-name'|'n-')
  321. source "${MHSHAREDIR}/revoke_name"
  322. revoke_name "$@"
  323. ;;
  324. 'add-revoker'|'r+')
  325. source "${MHSHAREDIR}/add_revoker"
  326. add_revoker "$@"
  327. ;;
  328. 'revoke-key')
  329. source "${MHSHAREDIR}/revoke_key"
  330. revoke_key "$@"
  331. ;;
  332. 'publish-keys'|'publish-key'|'publish'|'p')
  333. source "${MHSHAREDIR}/publish_key"
  334. multi_key publish_key "$@"
  335. ;;
  336. 'diagnostics'|'d')
  337. source "${MHSHAREDIR}/diagnostics"
  338. diagnostics
  339. ;;
  340. 'update-pgp-pub-file')
  341. update_pgp_pub_file
  342. ;;
  343. 'version'|'--version'|'v')
  344. version
  345. ;;
  346. '--help'|'help'|'-h'|'h'|'?')
  347. usage
  348. ;;
  349. *)
  350. failure "Unknown command: '$COMMAND'
  351. Try '$PGRM help' for usage."
  352. ;;
  353. esac