summaryrefslogtreecommitdiff
path: root/src/monkeysphere-authentication
blob: 1def4cdeec34100e207edeed5785269720947fe9 (plain)
  1. #!/usr/bin/env bash
  2. # monkeysphere-authentication: Monkeysphere authentication 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 authentication functions
  23. MASHAREDIR="${SYSSHAREDIR}/ma"
  24. # datadir for authentication functions
  25. MADATADIR="${SYSDATADIR}/authentication"
  26. # temp directory to enable atomic moves of authorized_keys files
  27. MATMPDIR="${MADATADIR}/tmp"
  28. export MATMPDIR
  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. # default return code
  34. RETURN=0
  35. ########################################################################
  36. # FUNCTIONS
  37. ########################################################################
  38. usage() {
  39. cat <<EOF >&2
  40. usage: $PGRM <subcommand> [options] [args]
  41. Monkeysphere authentication admin tool.
  42. subcommands:
  43. update-users (u) [USER]... update user authorized_keys files
  44. add-id-certifier (c+) KEYID import and tsign a certification key
  45. --domain (-n) DOMAIN limit ID certifications to DOMAIN
  46. --trust (-t) TRUST trust level of certifier (full)
  47. --depth (-d) DEPTH trust depth for certifier (1)
  48. remove-id-certifier (c-) KEYID remove a certification key
  49. list-id-certifiers (c) list certification keys
  50. version (v) show version number
  51. help (h,?) this help
  52. See ${PGRM}(8) for more info.
  53. EOF
  54. }
  55. # function to interact with the gpg core keyring
  56. gpg_core() {
  57. GNUPGHOME="$GNUPGHOME_CORE"
  58. export GNUPGHOME
  59. gpg --no-greeting --quiet --no-tty "$@"
  60. }
  61. # function to interact with the gpg sphere keyring
  62. # FIXME: this function requires only a single argument because of
  63. # problems with quote expansion. this needs to be fixed/improved.
  64. gpg_sphere() {
  65. GNUPGHOME="$GNUPGHOME_SPHERE"
  66. export GNUPGHOME
  67. su_monkeysphere_user "gpg --no-greeting --quiet --no-tty $@"
  68. }
  69. # output to stdout the core fingerprint from the gpg core secret
  70. # keyring
  71. core_fingerprint() {
  72. log debug "determining core key fingerprint..."
  73. gpg_core --list-secret-key --with-colons \
  74. --fixed-list-mode --with-fingerprint \
  75. | grep ^fpr: | cut -d: -f10
  76. }
  77. # export signatures from core to sphere
  78. gpg_core_sphere_sig_transfer() {
  79. log debug "exporting core local sigs to sphere..."
  80. gpg_core --export-options export-local-sigs --export | \
  81. gpg_sphere "--import-options import-local-sigs --import"
  82. }
  83. ########################################################################
  84. # MAIN
  85. ########################################################################
  86. # unset variables that should be defined only in config file of in
  87. # MONKEYSPHERE_ variables
  88. unset LOG_LEVEL
  89. unset KEYSERVER
  90. unset AUTHORIZED_USER_IDS
  91. unset RAW_AUTHORIZED_KEYS
  92. unset MONKEYSPHERE_USER
  93. unset PROMPT
  94. # load configuration file
  95. [ -e ${MONKEYSPHERE_AUTHENTICATION_CONFIG:="${SYSCONFIGDIR}/monkeysphere-authentication.conf"} ] && . "$MONKEYSPHERE_AUTHENTICATION_CONFIG"
  96. # set empty config variable with ones from the environment, or with
  97. # defaults
  98. LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=${LOG_LEVEL:="INFO"}}
  99. KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="pool.sks-keyservers.net"}}
  100. AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:=${AUTHORIZED_USER_IDS:="%h/.monkeysphere/authorized_user_ids"}}
  101. RAW_AUTHORIZED_KEYS=${MONKEYSPHERE_RAW_AUTHORIZED_KEYS:=${RAW_AUTHORIZED_KEYS:="%h/.ssh/authorized_keys"}}
  102. MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=${MONKEYSPHERE_USER:="monkeysphere"}}
  103. PROMPT=${MONKEYSPHERE_PROMPT:=${PROMPT:="true"}}
  104. # other variables
  105. CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="true"}
  106. REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
  107. GNUPGHOME_CORE=${MONKEYSPHERE_GNUPGHOME_CORE:="${MADATADIR}/core"}
  108. GNUPGHOME_SPHERE=${MONKEYSPHERE_GNUPGHOME_SPHERE:="${MADATADIR}/sphere"}
  109. CORE_KEYLENGTH=${MONKEYSPHERE_CORE_KEYLENGTH:="2048"}
  110. # export variables needed in su invocation
  111. export DATE
  112. export MODE
  113. export LOG_LEVEL
  114. export KEYSERVER
  115. export MONKEYSPHERE_USER
  116. export PROMPT
  117. export CHECK_KEYSERVER
  118. export REQUIRED_USER_KEY_CAPABILITY
  119. export GNUPGHOME_CORE
  120. export GNUPGHOME_SPHERE
  121. export GNUPGHOME
  122. export CORE_KEYLENGTH
  123. # get subcommand
  124. COMMAND="$1"
  125. [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
  126. shift
  127. case $COMMAND in
  128. 'setup'|'setup'|'s')
  129. source "${MASHAREDIR}/setup"
  130. setup
  131. ;;
  132. 'update-users'|'update-user'|'u')
  133. source "${MASHAREDIR}/setup"
  134. setup
  135. source "${MASHAREDIR}/update_users"
  136. update_users "$@"
  137. ;;
  138. 'add-identity-certifier'|'add-id-certifier'|'add-certifier'|'c+')
  139. source "${MASHAREDIR}/setup"
  140. setup
  141. source "${MASHAREDIR}/add_certifier"
  142. add_certifier "$@"
  143. ;;
  144. 'remove-identity-certifier'|'remove-id-certifier'|'remove-certifier'|'c-')
  145. source "${MASHAREDIR}/setup"
  146. setup
  147. source "${MASHAREDIR}/remove_certifier"
  148. remove_certifier "$@"
  149. ;;
  150. 'list-identity-certifiers'|'list-id-certifiers'|'list-certifiers'|'list-certifier'|'c')
  151. source "${MASHAREDIR}/setup"
  152. setup
  153. source "${MASHAREDIR}/list_certifiers"
  154. list_certifiers
  155. ;;
  156. 'diagnostics'|'d')
  157. source "${MASHAREDIR}/setup"
  158. setup
  159. source "${MASHAREDIR}/diagnostics"
  160. diagnostics
  161. ;;
  162. 'gpg-cmd')
  163. source "${MASHAREDIR}/setup"
  164. setup
  165. gpg_sphere "$@"
  166. ;;
  167. 'version'|'v')
  168. echo "$VERSION"
  169. ;;
  170. '--help'|'help'|'-h'|'h'|'?')
  171. usage
  172. ;;
  173. *)
  174. failure "Unknown command: '$COMMAND'
  175. Type '$PGRM help' for usage."
  176. ;;
  177. esac
  178. exit "$RETURN"