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