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