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