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