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