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