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