summaryrefslogtreecommitdiff
path: root/src/monkeysphere-authentication
blob: dd44efe8637a6392c4a9c74a1405ff95663a8fab (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 LOG_LEVEL
  111. export KEYSERVER
  112. export MONKEYSPHERE_USER
  113. export MONKEYSPHERE_GROUP
  114. export PROMPT
  115. export CHECK_KEYSERVER
  116. export REQUIRED_USER_KEY_CAPABILITY
  117. export GNUPGHOME_CORE
  118. export GNUPGHOME_SPHERE
  119. export GNUPGHOME
  120. export CORE_KEYLENGTH
  121. export LOG_PREFIX
  122. if [ "$#" -eq 0 ] ; then
  123. usage
  124. failure "Please supply a subcommand."
  125. fi
  126. # get subcommand
  127. COMMAND="$1"
  128. shift
  129. case $COMMAND in
  130. 'setup'|'setup'|'s')
  131. source "${MASHAREDIR}/setup"
  132. setup
  133. ;;
  134. 'update-users'|'update-user'|'u')
  135. source "${MASHAREDIR}/setup"
  136. setup
  137. source "${MASHAREDIR}/update_users"
  138. update_users "$@"
  139. ;;
  140. 'refresh-keys'|'r')
  141. source "${MASHAREDIR}/setup"
  142. setup
  143. gpg_sphere "--keyserver $KEYSERVER --refresh-keys"
  144. ;;
  145. 'add-identity-certifier'|'add-id-certifier'|'add-certifier'|'c+')
  146. source "${MASHAREDIR}/setup"
  147. setup
  148. source "${MASHAREDIR}/add_certifier"
  149. add_certifier "$@"
  150. ;;
  151. 'remove-identity-certifier'|'remove-id-certifier'|'remove-certifier'|'c-')
  152. source "${MASHAREDIR}/setup"
  153. setup
  154. source "${MASHAREDIR}/remove_certifier"
  155. remove_certifier "$@"
  156. ;;
  157. 'list-identity-certifiers'|'list-id-certifiers'|'list-certifiers'|'list-certifier'|'c')
  158. source "${MASHAREDIR}/setup"
  159. setup
  160. source "${MASHAREDIR}/list_certifiers"
  161. list_certifiers
  162. ;;
  163. 'diagnostics'|'d')
  164. source "${MASHAREDIR}/setup"
  165. setup
  166. source "${MASHAREDIR}/diagnostics"
  167. diagnostics
  168. ;;
  169. 'gpg-cmd')
  170. source "${MASHAREDIR}/setup"
  171. setup
  172. gpg_sphere "$@"
  173. ;;
  174. 'version'|'v')
  175. version
  176. ;;
  177. '--help'|'help'|'-h'|'h'|'?')
  178. usage
  179. ;;
  180. *)
  181. failure "Unknown command: '$COMMAND'
  182. Try '$PGRM help' for usage."
  183. ;;
  184. esac