summaryrefslogtreecommitdiff
path: root/src/monkeysphere-authentication
blob: df7d9bcf6ddf8fd2db17971973a37b7ca20ec0c8 (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. refresh-keys (r) refresh keys in keyring
  44. add-id-certifier (c+) KEYID|FILE import and tsign a certification key
  45. [--domain (-n) DOMAIN] limit ID certifications to DOMAIN
  46. [--trust (-t) TRUST] trust level of certifier (default: full)
  47. [--depth (-d) DEPTH] trust depth for certifier (default: 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" 2>&1 | log debug
  82. }
  83. ########################################################################
  84. # MAIN
  85. ########################################################################
  86. # set unset default variables
  87. AUTHORIZED_USER_IDS="%h/.monkeysphere/authorized_user_ids"
  88. RAW_AUTHORIZED_KEYS="%h/.ssh/authorized_keys"
  89. # load configuration file
  90. [ -e ${MONKEYSPHERE_AUTHENTICATION_CONFIG:="${SYSCONFIGDIR}/monkeysphere-authentication.conf"} ] \
  91. && . "$MONKEYSPHERE_AUTHENTICATION_CONFIG"
  92. # set empty config variable with ones from the environment
  93. LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=$LOG_LEVEL}
  94. KEYSERVER=${MONKEYSPHERE_KEYSERVER:=$KEYSERVER}
  95. CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:=$CHECK_KEYSERVER}
  96. MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=$MONKEYSPHERE_USER}
  97. MONKEYSPHERE_GROUP=$(get_primary_group "$MONKEYSPHERE_USER")
  98. PROMPT=${MONKEYSPHERE_PROMPT:=$PROMPT}
  99. AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:=$AUTHORIZED_USER_IDS}
  100. RAW_AUTHORIZED_KEYS=${MONKEYSPHERE_RAW_AUTHORIZED_KEYS:=$RAW_AUTHORIZED_KEYS}
  101. STRICT_MODES=${MONKEYSPHERE_STRICT_MODES:=$STRICT_MODES}
  102. # other variables
  103. REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
  104. GNUPGHOME_CORE=${MONKEYSPHERE_GNUPGHOME_CORE:="${MADATADIR}/core"}
  105. GNUPGHOME_SPHERE=${MONKEYSPHERE_GNUPGHOME_SPHERE:="${MADATADIR}/sphere"}
  106. CORE_KEYLENGTH=${MONKEYSPHERE_CORE_KEYLENGTH:="2048"}
  107. LOG_PREFIX=${MONKEYSPHERE_LOG_PREFIX:='ms: '}
  108. # export variables needed in su invocation
  109. export DATE
  110. export MODE
  111. export LOG_LEVEL
  112. export KEYSERVER
  113. export MONKEYSPHERE_USER
  114. export MONKEYSPHERE_GROUP
  115. export PROMPT
  116. export CHECK_KEYSERVER
  117. export REQUIRED_USER_KEY_CAPABILITY
  118. export GNUPGHOME_CORE
  119. export GNUPGHOME_SPHERE
  120. export GNUPGHOME
  121. export CORE_KEYLENGTH
  122. export LOG_PREFIX
  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. 'refresh-keys'|'r')
  139. source "${MASHAREDIR}/setup"
  140. setup
  141. gpg_sphere "--keyserver $KEYSERVER --refresh-keys"
  142. ;;
  143. 'add-identity-certifier'|'add-id-certifier'|'add-certifier'|'c+')
  144. source "${MASHAREDIR}/setup"
  145. setup
  146. source "${MASHAREDIR}/add_certifier"
  147. add_certifier "$@"
  148. ;;
  149. 'remove-identity-certifier'|'remove-id-certifier'|'remove-certifier'|'c-')
  150. source "${MASHAREDIR}/setup"
  151. setup
  152. source "${MASHAREDIR}/remove_certifier"
  153. remove_certifier "$@"
  154. ;;
  155. 'list-identity-certifiers'|'list-id-certifiers'|'list-certifiers'|'list-certifier'|'c')
  156. source "${MASHAREDIR}/setup"
  157. setup
  158. source "${MASHAREDIR}/list_certifiers"
  159. list_certifiers
  160. ;;
  161. 'diagnostics'|'d')
  162. source "${MASHAREDIR}/setup"
  163. setup
  164. source "${MASHAREDIR}/diagnostics"
  165. diagnostics
  166. ;;
  167. 'gpg-cmd')
  168. source "${MASHAREDIR}/setup"
  169. setup
  170. gpg_sphere "$@"
  171. ;;
  172. 'version'|'v')
  173. version
  174. ;;
  175. '--help'|'help'|'-h'|'h'|'?')
  176. usage
  177. ;;
  178. *)
  179. failure "Unknown command: '$COMMAND'
  180. Type '$PGRM help' for usage."
  181. ;;
  182. esac