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