summaryrefslogtreecommitdiff
path: root/src/monkeysphere-authentication
blob: bd8e54050501a7c9a08e4fe0876025a67b29933b (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. #
  9. # They are Copyright 2008, and are all released under the GPL, version 3
  10. # or later.
  11. ########################################################################
  12. set -e
  13. PGRM=$(basename $0)
  14. SYSSHAREDIR=${MONKEYSPHERE_SYSSHAREDIR:-"/usr/share/monkeysphere"}
  15. export SYSSHAREDIR
  16. . "${SYSSHAREDIR}/common" || exit 1
  17. # sharedir for authentication functions
  18. MASHAREDIR="${SYSSHAREDIR}/ma"
  19. SYSDATADIR=${MONKEYSPHERE_SYSDATADIR:-"/var/lib/monkeysphere"}
  20. export SYSDATADIR
  21. # temp directory to enable atomic moves of authorized_keys files
  22. MATMPDIR="${SYSDATADIR}/tmp"
  23. export MSTMPDIR
  24. # UTC date in ISO 8601 format if needed
  25. DATE=$(date -u '+%FT%T')
  26. # unset some environment variables that could screw things up
  27. unset GREP_OPTIONS
  28. # default return code
  29. RETURN=0
  30. ########################################################################
  31. # FUNCTIONS
  32. ########################################################################
  33. usage() {
  34. cat <<EOF >&2
  35. usage: $PGRM <subcommand> [options] [args]
  36. Monkeysphere authentication admin tool.
  37. subcommands:
  38. update-users (u) [USER]... update user authorized_keys files
  39. add-id-certifier (c+) KEYID import and tsign a certification key
  40. --domain (-n) DOMAIN limit ID certifications to DOMAIN
  41. --trust (-t) TRUST trust level of certifier (full)
  42. --depth (-d) DEPTH trust depth for certifier (1)
  43. remove-id-certifier (c-) KEYID remove a certification key
  44. list-id-certifiers (c) list certification keys
  45. expert
  46. diagnostics (d) monkeysphere authentication status
  47. gpg-cmd CMD execute gpg command
  48. version (v) show version number
  49. help (h,?) this help
  50. EOF
  51. }
  52. # function to run command as monkeysphere user
  53. su_monkeysphere_user() {
  54. # if the current user is the monkeysphere user, then just eval
  55. # command
  56. if [ $(id -un) = "$MONKEYSPHERE_USER" ] ; then
  57. eval "$@"
  58. # otherwise su command as monkeysphere user
  59. else
  60. su "$MONKEYSPHERE_USER" -c "$@"
  61. fi
  62. }
  63. # function to interact with the gpg core keyring
  64. gpg_core() {
  65. local returnCode
  66. GNUPGHOME="$GNUPGHOME_CORE"
  67. export GNUPGHOME
  68. # NOTE: we supress this warning because we need the monkeysphere
  69. # user to be able to read the host pubring. we realize this might
  70. # be problematic, but it's the simplest solution, without too much
  71. # loss of security.
  72. gpg --no-permission-warning "$@"
  73. returnCode="$?"
  74. # always reset the permissions on the host pubring so that the
  75. # monkeysphere user can read the trust signatures
  76. chgrp "$MONKEYSPHERE_USER" "${GNUPGHOME_CORE}/pubring.gpg"
  77. chmod g+r "${GNUPGHOME_CORE}/pubring.gpg"
  78. return "$returnCode"
  79. }
  80. # function to interact with the gpg sphere keyring
  81. # FIXME: this function requires basically accepts only a single
  82. # argument because of problems with quote expansion. this needs to be
  83. # fixed/improved.
  84. gpg_sphere() {
  85. GNUPGHOME="$GNUPGHOME_SPHERE"
  86. export GNUPGHOME
  87. su_monkeysphere_user "gpg $@"
  88. }
  89. ########################################################################
  90. # MAIN
  91. ########################################################################
  92. # unset variables that should be defined only in config file
  93. unset KEYSERVER
  94. unset AUTHORIZED_USER_IDS
  95. unset RAW_AUTHORIZED_KEYS
  96. unset MONKEYSPHERE_USER
  97. # load configuration file
  98. [ -e ${MONKEYSPHERE_AUTHENTICATION_CONFIG:="${SYSCONFIGDIR}/monkeysphere-authentication.conf"} ] && . "$MONKEYSPHERE_AUTHENTICATION_CONFIG"
  99. # set empty config variable with ones from the environment, or with
  100. # defaults
  101. LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=${LOG_LEVEL:="INFO"}}
  102. KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="pool.sks-keyservers.net"}}
  103. AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:=${AUTHORIZED_USER_IDS:="%h/.monkeysphere/authorized_user_ids"}}
  104. RAW_AUTHORIZED_KEYS=${MONKEYSPHERE_RAW_AUTHORIZED_KEYS:=${RAW_AUTHORIZED_KEYS:="%h/.ssh/authorized_keys"}}
  105. MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=${MONKEYSPHERE_USER:="monkeysphere"}}
  106. # other variables
  107. CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="true"}
  108. REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
  109. GNUPGHOME_CORE=${MONKEYSPHERE_GNUPGHOME_CORE:="${SYSDATADIR}/authentication/core"}
  110. GNUPGHOME_SPHERE=${MONKEYSPHERE_GNUPGHOME_SPHERE:="${SYSDATADIR}/authentication/sphere"}
  111. # export variables needed in su invocation
  112. export DATE
  113. export MODE
  114. export LOG_LEVEL
  115. export MONKEYSPHERE_USER
  116. export KEYSERVER
  117. export CHECK_KEYSERVER
  118. export REQUIRED_USER_KEY_CAPABILITY
  119. export GNUPGHOME_CORE
  120. export GNUPGHOME_SPHERE
  121. export GNUPGHOME
  122. # get subcommand
  123. COMMAND="$1"
  124. [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
  125. shift
  126. case $COMMAND in
  127. 'update-users'|'update-user'|'u')
  128. source "${MASHAREDIR}/update_users"
  129. update_users "$@"
  130. ;;
  131. 'add-identity-certifier'|'add-id-certifier'|'add-certifier'|'c+')
  132. source "${MASHAREDIR}/add_certifier"
  133. add_certifier "$@"
  134. ;;
  135. 'remove-identity-certifier'|'remove-id-certifier'|'remove-certifier'|'c-')
  136. source "${MASHAREDIR}/remove_certifier"
  137. remove_certifier "$@"
  138. ;;
  139. 'list-identity-certifiers'|'list-id-certifiers'|'list-certifiers'|'list-certifier'|'c')
  140. source "${MASHAREDIR}/list_certifiers"
  141. list_certifiers "$@"
  142. ;;
  143. 'expert'|'e')
  144. SUBCOMMAND="$1"
  145. shift
  146. case "$SUBCOMMAND" in
  147. 'diagnostics'|'d')
  148. source "${MASHAREDIR}/diagnostics"
  149. diagnostics
  150. ;;
  151. 'gpg-cmd')
  152. gpg_sphere "$@"
  153. ;;
  154. *)
  155. failure "Unknown expert subcommand: '$COMMAND'
  156. Type '$PGRM help' for usage."
  157. ;;
  158. esac
  159. ;;
  160. 'version'|'v')
  161. echo "$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
  171. exit "$RETURN"