summaryrefslogtreecommitdiff
path: root/src/monkeysphere-authentication
blob: 4aaf02d41d18b19dfe28445689cc604d9b245aa5 (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. setup (s) setup monkeysphere user authentication
  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 <expert-subcommand> run expert command
  47. expert help expert command help
  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. 'setup'|'setup'|'s')
  128. source "${MASHAREDIR}/setup"
  129. setup "$@"
  130. ;;
  131. 'update-users'|'update-user'|'u')
  132. source "${MASHAREDIR}/update_users"
  133. update_users "$@"
  134. ;;
  135. 'add-identity-certifier'|'add-id-certifier'|'add-certifier'|'c+')
  136. source "${MASHAREDIR}/add_certifier"
  137. add_certifier "$@"
  138. ;;
  139. 'remove-identity-certifier'|'remove-id-certifier'|'remove-certifier'|'c-')
  140. source "${MASHAREDIR}/remove_certifier"
  141. remove_certifier "$@"
  142. ;;
  143. 'list-identity-certifiers'|'list-id-certifiers'|'list-certifiers'|'list-certifier'|'c')
  144. source "${MASHAREDIR}/list_certifiers"
  145. list_certifiers "$@"
  146. ;;
  147. 'expert'|'e')
  148. SUBCOMMAND="$1"
  149. shift
  150. case "$SUBCOMMAND" in
  151. 'help'|'h'|'?')
  152. cat <<EOF
  153. usage: $PGRM expert <subcommand> [options] [args]
  154. expert subcommands:
  155. diagnostics (d) monkeysphere authentication status
  156. gpg-cmd CMD execute gpg command
  157. EOF
  158. ;;
  159. 'diagnostics'|'d')
  160. source "${MASHAREDIR}/diagnostics"
  161. diagnostics
  162. ;;
  163. 'gpg-cmd')
  164. gpg_sphere "$@"
  165. ;;
  166. *)
  167. failure "Unknown expert subcommand: '$COMMAND'
  168. Type '$PGRM help' for usage."
  169. ;;
  170. esac
  171. ;;
  172. 'version'|'v')
  173. echo "$VERSION"
  174. ;;
  175. '--help'|'help'|'-h'|'h'|'?')
  176. usage
  177. ;;
  178. *)
  179. failure "Unknown command: '$COMMAND'
  180. Type '$PGRM help' for usage."
  181. ;;
  182. esac
  183. exit "$RETURN"