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