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