summaryrefslogtreecommitdiff
path: root/src/subcommands/ma/update-users
blob: 0335e31448f9b3dbc409bf71af406f62ff5cc27f (plain)
  1. # -*-shell-script-*-
  2. # This should be sourced by bash (though we welcome changes to make it POSIX sh compliant)
  3. # Monkeysphere authentication update-users subcommand
  4. #
  5. # The monkeysphere scripts are written by:
  6. # Jameson Rollins <jrollins@finestructure.net>
  7. # Jamie McClelland <jm@mayfirst.org>
  8. # Daniel Kahn Gillmor <dkg@fifthhorseman.net>
  9. #
  10. # They are Copyright 2008-2009, and are all released under the GPL,
  11. # version 3 or later.
  12. update_users() {
  13. if [ "$1" ] ; then
  14. # get users from command line
  15. unames="$@"
  16. else
  17. # or just look at all users if none specified
  18. unames=$(getent passwd | cut -d: -f1)
  19. fi
  20. RETCODE=0
  21. # set mode
  22. MODE="authorized_keys"
  23. # set gnupg home
  24. GNUPGHOME="$GNUPGHOME_AUTHENTICATION"
  25. # check to see if the gpg trust database has been initialized
  26. if [ ! -s "${GNUPGHOME}/trustdb.gpg" ] ; then
  27. failure "GNUPG trust database uninitialized. Please see MONKEYSPHERE-SERVER(8)."
  28. fi
  29. # make sure the authorized_keys directory exists
  30. mkdir -p "${SYSDATADIR}/authorized_keys"
  31. # loop over users
  32. for uname in $unames ; do
  33. # check all specified users exist
  34. if ! id "$uname" >/dev/null ; then
  35. log error "----- unknown user '$uname' -----"
  36. continue
  37. fi
  38. log verbose "----- user: $uname -----"
  39. # make temporary directory
  40. TMPLOC=$(mktemp -d ${MSTMPDIR}/tmp.XXXXXXXXXX) || failure "Could not create temporary directory!"
  41. # trap to delete temporary directory on exit
  42. trap "rm -rf $TMPLOC" EXIT
  43. # create temporary authorized_user_ids file
  44. TMP_AUTHORIZED_USER_IDS="${TMPLOC}/authorized_user_ids"
  45. touch "$TMP_AUTHORIZED_USER_IDS"
  46. # create temporary authorized_keys file
  47. AUTHORIZED_KEYS="${TMPLOC}/authorized_keys"
  48. touch "$AUTHORIZED_KEYS"
  49. # set restrictive permissions on the temporary files
  50. # FIXME: is there a better way to do this?
  51. chmod 0700 "$TMPLOC"
  52. chmod 0600 "$AUTHORIZED_KEYS"
  53. chmod 0600 "$TMP_AUTHORIZED_USER_IDS"
  54. chown -R "$MONKEYSPHERE_USER" "$TMPLOC"
  55. # process authorized_user_ids file
  56. log debug "checking for authorized_user_ids..."
  57. # translating ssh-style path variables
  58. authorizedUserIDs=$(translate_ssh_variables "$uname" "$AUTHORIZED_USER_IDS")
  59. if [ -s "$authorizedUserIDs" ] ; then
  60. # check permissions on the authorized_user_ids file path
  61. if check_key_file_permissions "$uname" "$authorizedUserIDs" ; then
  62. # copy user authorized_user_ids file to temporary
  63. # location
  64. cat "$authorizedUserIDs" > "$TMP_AUTHORIZED_USER_IDS"
  65. # export needed variables
  66. export AUTHORIZED_KEYS
  67. export TMP_AUTHORIZED_USER_IDS
  68. # process authorized_user_ids file, as monkeysphere user
  69. su_monkeysphere_user \
  70. ". ${SYSSHAREDIR}/common; process_authorized_user_ids $TMP_AUTHORIZED_USER_IDS"
  71. RETURN="$?"
  72. else
  73. log debug "not processing authorized_user_ids."
  74. fi
  75. else
  76. log debug "empty or absent authorized_user_ids file."
  77. fi
  78. # add user-controlled authorized_keys file if specified translate
  79. # ssh-style path variables
  80. rawAuthorizedKeys=$(translate_ssh_variables "$uname" "$RAW_AUTHORIZED_KEYS")
  81. if [ "$rawAuthorizedKeys" != 'none' ] ; then
  82. log debug "checking for raw authorized_keys..."
  83. if [ -s "$rawAuthorizedKeys" ] ; then
  84. # check permissions on the authorized_keys file path
  85. if check_key_file_permissions "$uname" "$rawAuthorizedKeys" ; then
  86. log verbose "adding raw authorized_keys file... "
  87. cat "$rawAuthorizedKeys" >> "$AUTHORIZED_KEYS"
  88. else
  89. log debug "not adding raw authorized_keys file."
  90. fi
  91. else
  92. log debug "empty or absent authorized_keys file."
  93. fi
  94. fi
  95. # move the new authorized_keys file into place
  96. if [ -s "$AUTHORIZED_KEYS" ] ; then
  97. # openssh appears to check the contents of the authorized_keys
  98. # file as the user in question, so the file must be readable
  99. # by that user at least.
  100. # but in general, we don't want the user tampering with this
  101. # file directly, so we'll adopt this approach: Own the file by
  102. # the monkeysphere-server invoker (usually root, but should be
  103. # the same uid that sshd is launched as); change the group of
  104. # the file so that members of the user's group can read it.
  105. # FIXME: is there a better way to do this?
  106. chown $(whoami) "$AUTHORIZED_KEYS" && \
  107. chgrp $(id -g "$uname") "$AUTHORIZED_KEYS" && \
  108. chmod g+r "$AUTHORIZED_KEYS" && \
  109. mv -f "$AUTHORIZED_KEYS" "${SYSDATADIR}/authorized_keys/${uname}" || \
  110. {
  111. log error "Failed to install authorized_keys for '$uname'!"
  112. rm -f "${SYSDATADIR}/authorized_keys/${uname}"
  113. # indicate that there has been a failure:
  114. RETURN=1
  115. }
  116. else
  117. rm -f "${SYSDATADIR}/authorized_keys/${uname}"
  118. fi
  119. # unset the trap
  120. trap - EXIT
  121. # destroy temporary directory
  122. rm -rf "$TMPLOC"
  123. done
  124. }