summaryrefslogtreecommitdiff
path: root/src/monkeysphere-authentication
blob: 46f349a21b83cb87ed1f72b1ddf674d84249ea73 (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:-"__SYSSHAREDIR_PREFIX__/share/monkeysphere"}
  18. export SYSSHAREDIR
  19. . "${SYSSHAREDIR}/defaultenv"
  20. . "${SYSSHAREDIR}/common"
  21. # sharedir for authentication functions
  22. MASHAREDIR="${SYSSHAREDIR}/ma"
  23. # datadir for authentication functions
  24. MADATADIR="${SYSDATADIR}/authentication"
  25. # temp directory to enable atomic moves of authorized_keys files
  26. MATMPDIR="${MADATADIR}/tmp"
  27. export MATMPDIR
  28. # UTC date in ISO 8601 format if needed
  29. DATE=$(date -u '+%FT%T')
  30. # unset some environment variables that could screw things up
  31. unset GREP_OPTIONS
  32. ########################################################################
  33. # FUNCTIONS
  34. ########################################################################
  35. usage() {
  36. cat <<EOF >&2
  37. usage: $PGRM <subcommand> [options] [args]
  38. Monkeysphere authentication admin tool.
  39. subcommands:
  40. update-users (u) [USER]... update user authorized_keys files
  41. keys-for-user (k) USER output user authorized_keys lines to stdout
  42. refresh-keys (r) refresh keys in keyring
  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 --fixed-list-mode --no-greeting --quiet --no-tty "$@"
  59. }
  60. # function to interact with the gpg sphere keyring
  61. gpg_sphere() {
  62. GNUPGHOME="$GNUPGHOME_SPHERE"
  63. export GNUPGHOME
  64. su_monkeysphere_user "gpg --fixed-list-mode --no-greeting --quiet --no-tty $@"
  65. }
  66. # output to stdout the core fingerprint from the gpg core secret
  67. # keyring
  68. core_fingerprint() {
  69. log debug "determining core key fingerprint..."
  70. gpg_core --list-secret-key --with-colons \
  71. --with-fingerprint \
  72. | grep ^fpr: | cut -d: -f10
  73. }
  74. # export signatures from core to sphere
  75. gpg_core_sphere_sig_transfer() {
  76. log debug "exporting core local sigs to sphere..."
  77. gpg_core --export-options export-local-sigs --export | \
  78. gpg_sphere --import-options import-local-sigs --import 2>&1 | log debug
  79. }
  80. ########################################################################
  81. # MAIN
  82. ########################################################################
  83. # set unset default variables
  84. AUTHORIZED_USER_IDS="%h/.monkeysphere/authorized_user_ids"
  85. RAW_AUTHORIZED_KEYS="%h/.ssh/authorized_keys"
  86. # load configuration file
  87. [ -e ${MONKEYSPHERE_AUTHENTICATION_CONFIG:="${SYSCONFIGDIR}/monkeysphere-authentication.conf"} ] \
  88. && . "$MONKEYSPHERE_AUTHENTICATION_CONFIG"
  89. # set empty config variable with ones from the environment
  90. LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=$LOG_LEVEL}
  91. KEYSERVER=${MONKEYSPHERE_KEYSERVER:=$KEYSERVER}
  92. CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=$CHECK_KEYSERVER}
  93. MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=$MONKEYSPHERE_USER}
  94. MONKEYSPHERE_GROUP=$(get_primary_group "$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. STRICT_MODES=${MONKEYSPHERE_STRICT_MODES:=$STRICT_MODES}
  99. # other variables
  100. REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
  101. GNUPGHOME_CORE=${MONKEYSPHERE_GNUPGHOME_CORE:="${MADATADIR}/core"}
  102. GNUPGHOME_SPHERE=${MONKEYSPHERE_GNUPGHOME_SPHERE:="${MADATADIR}/sphere"}
  103. CORE_KEYLENGTH=${MONKEYSPHERE_CORE_KEYLENGTH:="2048"}
  104. LOG_PREFIX=${MONKEYSPHERE_LOG_PREFIX:='ms: '}
  105. # export variables needed in su invocation
  106. export DATE
  107. export LOG_LEVEL
  108. export KEYSERVER
  109. export MONKEYSPHERE_USER
  110. export MONKEYSPHERE_GROUP
  111. export PROMPT
  112. export CHECK_KEYSERVER
  113. export REQUIRED_USER_KEY_CAPABILITY
  114. export GNUPGHOME_CORE
  115. export GNUPGHOME_SPHERE
  116. export GNUPGHOME
  117. export CORE_KEYLENGTH
  118. export LOG_PREFIX
  119. if [ "$#" -eq 0 ] ; then
  120. usage
  121. failure "Please supply a subcommand."
  122. fi
  123. # get subcommand
  124. COMMAND="$1"
  125. shift
  126. case $COMMAND in
  127. 'setup'|'setup'|'s')
  128. source "${MASHAREDIR}/setup"
  129. setup
  130. ;;
  131. 'update-users'|'update-user'|'update'|'u')
  132. source "${MASHAREDIR}/setup"
  133. setup
  134. source "${MASHAREDIR}/update_users"
  135. OUTPUT_STDOUT= update_users "$@"
  136. ;;
  137. 'keys-for-user'|'k')
  138. (( $# > 0 )) || failure "Must specify user."
  139. source "${MASHAREDIR}/setup"
  140. setup
  141. source "${MASHAREDIR}/update_users"
  142. OUTPUT_STDOUT=true update_users "$1"
  143. ;;
  144. 'refresh-keys'|'refresh'|'r')
  145. source "${MASHAREDIR}/setup"
  146. setup
  147. gpg_sphere --keyserver "$KEYSERVER" --refresh-keys
  148. ;;
  149. 'add-identity-certifier'|'add-id-certifier'|'add-certifier'|'c+')
  150. source "${MASHAREDIR}/setup"
  151. setup
  152. source "${MASHAREDIR}/add_certifier"
  153. add_certifier "$@"
  154. ;;
  155. 'remove-identity-certifier'|'remove-id-certifier'|'remove-certifier'|'c-')
  156. source "${MASHAREDIR}/setup"
  157. setup
  158. source "${MASHAREDIR}/remove_certifier"
  159. remove_certifier "$@"
  160. ;;
  161. 'list-identity-certifiers'|'list-id-certifiers'|'list-certifiers'|'list-certifier'|'c')
  162. source "${MASHAREDIR}/setup"
  163. setup
  164. source "${MASHAREDIR}/list_certifiers"
  165. list_certifiers
  166. ;;
  167. 'diagnostics'|'d')
  168. source "${MASHAREDIR}/setup"
  169. setup
  170. source "${MASHAREDIR}/diagnostics"
  171. diagnostics
  172. ;;
  173. 'gpg-cmd')
  174. source "${MASHAREDIR}/setup"
  175. setup
  176. gpg_sphere "$@"
  177. ;;
  178. 'version'|'--version'|'v')
  179. version
  180. ;;
  181. '--help'|'help'|'-h'|'h'|'?')
  182. usage
  183. ;;
  184. *)
  185. failure "Unknown command: '$COMMAND'
  186. Try '$PGRM help' for usage."
  187. ;;
  188. esac