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