summaryrefslogtreecommitdiff
path: root/src/monkeysphere-host
blob: d6e4c686287a57c0782e33896c08d44200252247 (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-2009, 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}/common" || exit 1
  20. SYSDATADIR=${MONKEYSPHERE_SYSDATADIR:-"/var/lib/monkeysphere"}
  21. export SYSDATADIR
  22. # sharedir for host functions
  23. MHSHAREDIR="${SYSSHAREDIR}/mh"
  24. # datadir for host functions
  25. MHDATADIR="${SYSDATADIR}/host"
  26. # temp directory for temp gnupghome directories for add_revoker
  27. MHTMPDIR="${MHDATADIR}/tmp"
  28. export MHTMPDIR
  29. # host pub key files
  30. HOST_KEY_PUB="${SYSDATADIR}/ssh_host_rsa_key.pub"
  31. HOST_KEY_PUB_GPG="${SYSDATADIR}/ssh_host_rsa_key.pub.gpg"
  32. # UTC date in ISO 8601 format if needed
  33. DATE=$(date -u '+%FT%T')
  34. # unset some environment variables that could screw things up
  35. unset GREP_OPTIONS
  36. # default return code
  37. RETURN=0
  38. ########################################################################
  39. # FUNCTIONS
  40. ########################################################################
  41. usage() {
  42. cat <<EOF >&2
  43. usage: $PGRM <subcommand> [options] [args]
  44. Monkeysphere host admin tool.
  45. subcommands:
  46. show-key (s) output all host key information
  47. set-expire (e) EXPIRE set host key expiration
  48. add-hostname (n+) NAME[:PORT] add hostname user ID to host key
  49. revoke-hostname (n-) NAME[:PORT] revoke hostname user ID
  50. add-revoker (o) FINGERPRINT add a revoker to the host key
  51. revoke-key (r) revoke host key
  52. publish-key (p) publish host key to keyserver
  53. expert <expert-subcommand> run expert command
  54. expert help expert command help
  55. version (v) show version number
  56. help (h,?) this help
  57. EOF
  58. }
  59. # function to interact with the gpg keyring
  60. gpg_host() {
  61. GNUPGHOME="$GNUPGHOME_HOST" gpg "$@"
  62. }
  63. # command to list the info about the host key, in colon format
  64. gpg_host_list() {
  65. gpg_host --list-keys --with-colons --fixed-list-mode \
  66. --with-fingerprint --with-fingerprint \
  67. "0x${HOST_FINGERPRINT}!"
  68. }
  69. # command for edit key scripts, takes scripts on stdin
  70. gpg_host_edit() {
  71. gpg_host --quiet --command-fd 0 --edit-key \
  72. "0x${HOST_FINGERPRINT}!" "$@"
  73. }
  74. # export the host key to stdout
  75. gpg_host_export() {
  76. gpg_host --export --armor --export-options export-minimal \
  77. "0x${HOST_FINGERPRINT}!"
  78. }
  79. # export the host public key to the monkeysphere gpg pub key file
  80. create_gpg_pub_file() {
  81. log debug "creating openpgp public key file..."
  82. gpg_host_export > "$HOST_KEY_PUB_GPG"
  83. log info "GPG host public key file: $HOST_KEY_PUB_GPG"
  84. }
  85. # load the host fingerprint into the fingerprint variable, using the
  86. # export gpg pub key file
  87. # FIXME: this seems much less than ideal, with all this temp keyring
  88. # stuff. is there a way we can do this without having to create temp
  89. # files?
  90. load_fingerprint() {
  91. if [ -f "$HOST_KEY_PUB_GPG" ] ; then
  92. HOST_FINGERPRINT=$( \
  93. (FUBAR=$(mktemp -d) && export GNUPGHOME="$FUBAR" \
  94. && gpg --quiet --import \
  95. && gpg --quiet --list-keys --with-colons --with-fingerprint \
  96. && rm -rf "$FUBAR") <"$HOST_KEY_PUB_GPG" \
  97. | grep '^fpr:' | cut -d: -f10 )
  98. else
  99. HOST_FINGERPRINT=
  100. fi
  101. }
  102. # load the host fingerprint into the fingerprint variable, using the
  103. # gpg host secret key
  104. load_fingerprint_secret() {
  105. HOST_FINGERPRINT=$( \
  106. gpg_host --quiet --list-secret-key \
  107. --with-colons --with-fingerprint \
  108. | grep '^fpr:' | cut -d: -f10 )
  109. }
  110. # output host key ssh fingerprint
  111. load_ssh_fingerprint() {
  112. [ -f "$HOST_KEY_PUB" ] || return 0
  113. HOST_FINGERPRINT_SSH=$(ssh-keygen -l -f "$HOST_KEY_PUB" \
  114. | awk '{ print $1, $2, $4 }')
  115. }
  116. # fail if host key present
  117. check_host_key() {
  118. [ -z "$HOST_FINGERPRINT" ] \
  119. || failure "An OpenPGP host key already exists."
  120. }
  121. # fail if host key not present
  122. check_host_no_key() {
  123. [ "$HOST_FINGERPRINT" ] \
  124. || failure "You don't appear to have a Monkeysphere host key on this server. Please run 'monkeysphere-host expert import-key' first."
  125. }
  126. # output the index of a user ID on the host key
  127. # return 1 if user ID not found
  128. find_host_userid() {
  129. local userID="$1"
  130. local tmpuidMatch
  131. local line
  132. # match to only ultimately trusted user IDs
  133. tmpuidMatch="u:$(echo $userID | gpg_escape)"
  134. # find the index of the requsted user ID
  135. # NOTE: this is based on circumstantial evidence that the order of
  136. # this output is the appropriate index
  137. line=$(gpg_host_list | egrep '^(uid|uat):' | cut -f2,10 -d: | \
  138. grep -n -x -F "$tmpuidMatch" 2>/dev/null)
  139. if [ "$line" ] ; then
  140. echo ${line%%:*}
  141. return 0
  142. else
  143. return 1
  144. fi
  145. }
  146. # show info about the host key
  147. show_key() {
  148. gpg_host --fingerprint --list-key --list-options show-unusable-uids \
  149. "0x${HOST_FINGERPRINT}!" 2>/dev/null || true
  150. # FIXME: make sure expiration date is shown
  151. echo "OpenPGP fingerprint: $HOST_FINGERPRINT"
  152. load_ssh_fingerprint
  153. if [ "$HOST_FINGERPRINT_SSH" ] ; then
  154. echo "ssh fingerprint: $HOST_FINGERPRINT_SSH"
  155. else
  156. log error "SSH host key not found."
  157. fi
  158. # FIXME: other relevant key parameters?
  159. }
  160. ########################################################################
  161. # MAIN
  162. ########################################################################
  163. # unset variables that should be defined only in config file
  164. unset KEYSERVER
  165. unset MONKEYSPHERE_USER
  166. # load configuration file
  167. [ -e ${MONKEYSPHERE_HOST_CONFIG:="${SYSCONFIGDIR}/monkeysphere-host.conf"} ] && . "$MONKEYSPHERE_HOST_CONFIG"
  168. # set empty config variable with ones from the environment, or with
  169. # defaults
  170. LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=${LOG_LEVEL:="INFO"}}
  171. KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="pool.sks-keyservers.net"}}
  172. AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:=${AUTHORIZED_USER_IDS:="%h/.monkeysphere/authorized_user_ids"}}
  173. RAW_AUTHORIZED_KEYS=${MONKEYSPHERE_RAW_AUTHORIZED_KEYS:=${RAW_AUTHORIZED_KEYS:="%h/.ssh/authorized_keys"}}
  174. MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=${MONKEYSPHERE_USER:="monkeysphere"}}
  175. # other variables
  176. CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="true"}
  177. GNUPGHOME_HOST=${MONKEYSPHERE_GNUPGHOME_HOST:="${MHDATADIR}"}
  178. # export variables needed in su invocation
  179. export DATE
  180. export MODE
  181. export LOG_LEVEL
  182. export MONKEYSPHERE_USER
  183. export KEYSERVER
  184. export GNUPGHOME_HOST
  185. export GNUPGHOME
  186. export HOST_FINGERPRINT=
  187. export HOST_FINGERPRINT_SSH=
  188. # get subcommand
  189. COMMAND="$1"
  190. [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
  191. shift
  192. case $COMMAND in
  193. 'show-key'|'show'|'s')
  194. load_fingerprint
  195. check_host_no_key
  196. show_key
  197. ;;
  198. 'set-expire'|'extend-key'|'e')
  199. load_fingerprint
  200. check_host_no_key
  201. source "${MHSHAREDIR}/set_expire"
  202. set_expire "$@"
  203. ;;
  204. 'add-hostname'|'add-name'|'n+')
  205. load_fingerprint
  206. check_host_no_key
  207. source "${MHSHAREDIR}/add_hostname"
  208. add_hostname "$@"
  209. ;;
  210. 'revoke-hostname'|'revoke-name'|'n-')
  211. load_fingerprint
  212. check_host_no_key
  213. source "${MHSHAREDIR}/revoke_hostname"
  214. revoke_hostname "$@"
  215. ;;
  216. 'add-revoker'|'o')
  217. load_fingerprint
  218. check_host_no_key
  219. source "${MHSHAREDIR}/add_revoker"
  220. add_revoker "$@"
  221. ;;
  222. 'revoke-key'|'r')
  223. load_fingerprint
  224. check_host_no_key
  225. source "${MHSHAREDIR}/revoke_key"
  226. revoke_key "$@"
  227. ;;
  228. 'publish-key'|'publish'|'p')
  229. load_fingerprint
  230. check_host_no_key
  231. source "${MHSHAREDIR}/publish_key"
  232. publish_key
  233. ;;
  234. 'expert')
  235. SUBCOMMAND="$1"
  236. shift
  237. case "$SUBCOMMAND" in
  238. 'help'|'h'|'?')
  239. cat <<EOF
  240. usage: $PGRM expert <subcommand> [options] [args]
  241. expert subcommands:
  242. import-key (i) FILE [NAME[:PORT]] import existing ssh key to gpg
  243. gen-key (g) [NAME[:PORT]] generate gpg key for the host
  244. --length (-l) BITS key length in bits (2048)
  245. diagnostics (d) monkeysphere host status
  246. EOF
  247. ;;
  248. 'import-key'|'i')
  249. load_fingerprint
  250. check_host_key
  251. source "${MHSHAREDIR}/import_key"
  252. import_key "$@"
  253. ;;
  254. 'gen-key'|'g')
  255. load_fingerprint
  256. check_host_key
  257. source "${MHSHAREDIR}/gen_key"
  258. gen_key "$@"
  259. ;;
  260. 'diagnostics'|'d')
  261. source "${MHSHAREDIR}/diagnostics"
  262. diagnostics
  263. ;;
  264. *)
  265. failure "Unknown expert subcommand: '$COMMAND'
  266. Type '$PGRM help' for usage."
  267. ;;
  268. esac
  269. ;;
  270. 'version'|'v')
  271. echo "$VERSION"
  272. ;;
  273. '--help'|'help'|'-h'|'h'|'?')
  274. usage
  275. ;;
  276. *)
  277. failure "Unknown command: '$COMMAND'
  278. Type '$PGRM help' for usage."
  279. ;;
  280. esac
  281. exit "$RETURN"