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