summaryrefslogtreecommitdiff
path: root/src/share/ma/update_users
blob: d23c1254773b3281918eb77943e8b952f108f558 (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. # check permissions on the authorized_user_ids file path
  62. if check_key_file_permissions "$uname" "$authorizedUserIDs" ; then
  63. log verbose "processing authorized_user_ids..."
  64. # process authorized_user_ids file, as monkeysphere user
  65. su_monkeysphere_user \
  66. /usr/bin/env "STRICT_MODES=$STRICT_MODES" "${SYSSHAREDIR}/common" process_authorized_user_ids - \
  67. < "$authorizedUserIDs" \
  68. > "$tmpAuthorizedKeys"
  69. else
  70. log debug "not processing authorized_user_ids."
  71. fi
  72. else
  73. log debug "empty or absent authorized_user_ids file."
  74. fi
  75. # add user-controlled authorized_keys file if specified translate
  76. # ssh-style path variables
  77. rawAuthorizedKeys=$(translate_ssh_variables "$uname" "$RAW_AUTHORIZED_KEYS")
  78. if [ "$rawAuthorizedKeys" != 'none' ] ; then
  79. log debug "checking for raw authorized_keys..."
  80. if [ -s "$rawAuthorizedKeys" ] ; then
  81. # check permissions on the authorized_keys file path
  82. if check_key_file_permissions "$uname" "$rawAuthorizedKeys" ; then
  83. log verbose "adding raw authorized_keys..."
  84. cat "$rawAuthorizedKeys" >> "$tmpAuthorizedKeys"
  85. else
  86. log debug "not adding raw authorized_keys."
  87. fi
  88. else
  89. log debug "empty or absent authorized_keys file."
  90. fi
  91. fi
  92. # move the new authorized_keys file into place
  93. if [ -s "$tmpAuthorizedKeys" ] ; then
  94. # openssh appears to check the contents of the authorized_keys
  95. # file as the user in question, so the file must be readable
  96. # by that user at least.
  97. # but in general, we don't want the user tampering with this
  98. # file directly, so we'll adopt this approach: Own the file by
  99. # the monkeysphere-server invoker (usually root, but should be
  100. # the same uid that sshd is launched as); change the group of
  101. # the file so that members of the user's group can read it.
  102. if [ "$OUTPUT_STDOUT" ] ; then
  103. log debug "outputting keys to stdout..."
  104. cat "$tmpAuthorizedKeys"
  105. else
  106. log debug "moving new file to ${authorizedKeysDir}/${uname}..."
  107. # FIXME: is there a better way to do this?
  108. chown $(whoami) "$tmpAuthorizedKeys" && \
  109. chgrp $(id -g "$uname") "$tmpAuthorizedKeys" && \
  110. chmod g+r "$tmpAuthorizedKeys" && \
  111. mv -f "$tmpAuthorizedKeys" "${authorizedKeysDir}/${uname}" || \
  112. {
  113. log error "Failed to install authorized_keys for '$uname'!"
  114. rm -f "${authorizedKeysDir}/${uname}"
  115. # indicate that there has been a failure:
  116. returnCode=1
  117. }
  118. fi
  119. else
  120. rm -f "${authorizedKeysDir}/${uname}"
  121. fi
  122. # unset the trap
  123. trap - EXIT
  124. # destroy temporary directory
  125. rm -rf "$TMPLOC"
  126. done
  127. return $returnCode
  128. }