summaryrefslogtreecommitdiff
path: root/src/monkeysphere-authentication
blob: a8f17f9a336536ab0393f5b4d8b05648cf11468a (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. 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 <expert-subcommand> run expert command
  46. expert help expert command help
  47. version (v) show version number
  48. help (h,?) this help
  49. EOF
  50. }
  51. # function to run command as monkeysphere user
  52. su_monkeysphere_user() {
  53. # if the current user is the monkeysphere user, then just eval
  54. # command
  55. if [ $(id -un) = "$MONKEYSPHERE_USER" ] ; then
  56. eval "$@"
  57. # otherwise su command as monkeysphere user
  58. else
  59. su "$MONKEYSPHERE_USER" -c "$@"
  60. fi
  61. }
  62. # function to interact with the gpg core keyring
  63. gpg_core() {
  64. local returnCode
  65. GNUPGHOME="$GNUPGHOME_CORE"
  66. export GNUPGHOME
  67. # NOTE: we supress this warning because we need the monkeysphere
  68. # user to be able to read the host pubring. we realize this might
  69. # be problematic, but it's the simplest solution, without too much
  70. # loss of security.
  71. gpg --no-permission-warning "$@"
  72. returnCode="$?"
  73. # always reset the permissions on the host pubring so that the
  74. # monkeysphere user can read the trust signatures
  75. chgrp "$MONKEYSPHERE_USER" "${GNUPGHOME_CORE}/pubring.gpg"
  76. chmod g+r "${GNUPGHOME_CORE}/pubring.gpg"
  77. return "$returnCode"
  78. }
  79. # function to interact with the gpg sphere keyring
  80. # FIXME: this function requires basically accepts only a single
  81. # argument because of problems with quote expansion. this needs to be
  82. # fixed/improved.
  83. gpg_sphere() {
  84. GNUPGHOME="$GNUPGHOME_SPHERE"
  85. export GNUPGHOME
  86. su_monkeysphere_user "gpg $@"
  87. }
  88. ########################################################################
  89. # MAIN
  90. ########################################################################
  91. # unset variables that should be defined only in config file
  92. unset KEYSERVER
  93. unset AUTHORIZED_USER_IDS
  94. unset RAW_AUTHORIZED_KEYS
  95. unset MONKEYSPHERE_USER
  96. # load configuration file
  97. [ -e ${MONKEYSPHERE_AUTHENTICATION_CONFIG:="${SYSCONFIGDIR}/monkeysphere-authentication.conf"} ] && . "$MONKEYSPHERE_AUTHENTICATION_CONFIG"
  98. # set empty config variable with ones from the environment, or with
  99. # defaults
  100. LOG_LEVEL=${MONKEYSPHERE_LOG_LEVEL:=${LOG_LEVEL:="INFO"}}
  101. KEYSERVER=${MONKEYSPHERE_KEYSERVER:=${KEYSERVER:="pool.sks-keyservers.net"}}
  102. AUTHORIZED_USER_IDS=${MONKEYSPHERE_AUTHORIZED_USER_IDS:=${AUTHORIZED_USER_IDS:="%h/.monkeysphere/authorized_user_ids"}}
  103. RAW_AUTHORIZED_KEYS=${MONKEYSPHERE_RAW_AUTHORIZED_KEYS:=${RAW_AUTHORIZED_KEYS:="%h/.ssh/authorized_keys"}}
  104. MONKEYSPHERE_USER=${MONKEYSPHERE_MONKEYSPHERE_USER:=${MONKEYSPHERE_USER:="monkeysphere"}}
  105. # other variables
  106. CHECK_KEYSERVER=${MONKEYSPHERE_CHECK_KEYSERVER:="true"}
  107. REQUIRED_USER_KEY_CAPABILITY=${MONKEYSPHERE_REQUIRED_USER_KEY_CAPABILITY:="a"}
  108. GNUPGHOME_CORE=${MONKEYSPHERE_GNUPGHOME_CORE:="${SYSDATADIR}/authentication/core"}
  109. GNUPGHOME_SPHERE=${MONKEYSPHERE_GNUPGHOME_SPHERE:="${SYSDATADIR}/authentication/sphere"}
  110. # export variables needed in su invocation
  111. export DATE
  112. export MODE
  113. export LOG_LEVEL
  114. export MONKEYSPHERE_USER
  115. export KEYSERVER
  116. export CHECK_KEYSERVER
  117. export REQUIRED_USER_KEY_CAPABILITY
  118. export GNUPGHOME_CORE
  119. export GNUPGHOME_SPHERE
  120. export GNUPGHOME
  121. # get subcommand
  122. COMMAND="$1"
  123. [ "$COMMAND" ] || failure "Type '$PGRM help' for usage."
  124. shift
  125. case $COMMAND in
  126. 'update-users'|'update-user'|'u')
  127. source "${MASHAREDIR}/update_users"
  128. update_users "$@"
  129. ;;
  130. 'add-identity-certifier'|'add-id-certifier'|'add-certifier'|'c+')
  131. source "${MASHAREDIR}/add_certifier"
  132. add_certifier "$@"
  133. ;;
  134. 'remove-identity-certifier'|'remove-id-certifier'|'remove-certifier'|'c-')
  135. source "${MASHAREDIR}/remove_certifier"
  136. remove_certifier "$@"
  137. ;;
  138. 'list-identity-certifiers'|'list-id-certifiers'|'list-certifiers'|'list-certifier'|'c')
  139. source "${MASHAREDIR}/list_certifiers"
  140. list_certifiers "$@"
  141. ;;
  142. 'expert'|'e')
  143. SUBCOMMAND="$1"
  144. shift
  145. case "$SUBCOMMAND" in
  146. 'help'|'h'|'?')
  147. cat <<EOF
  148. usage: $PGRM expert <subcommand> [options] [args]
  149. expert subcommands:
  150. diagnostics (d) monkeysphere authentication status
  151. gpg-cmd CMD execute gpg command
  152. EOF
  153. ;;
  154. 'diagnostics'|'d')
  155. source "${MASHAREDIR}/diagnostics"
  156. diagnostics
  157. ;;
  158. 'gpg-cmd')
  159. gpg_sphere "$@"
  160. ;;
  161. *)
  162. failure "Unknown expert subcommand: '$COMMAND'
  163. Type '$PGRM help' for usage."
  164. ;;
  165. esac
  166. ;;
  167. 'version'|'v')
  168. echo "$VERSION"
  169. ;;
  170. '--help'|'help'|'-h'|'h'|'?')
  171. usage
  172. ;;
  173. *)
  174. failure "Unknown command: '$COMMAND'
  175. Type '$PGRM help' for usage."
  176. ;;
  177. esac
  178. exit "$RETURN"