summaryrefslogtreecommitdiff
path: root/src/subcommands/ma/update-users
blob: 086136403638fe492a3f462c7e0f7bd4e728b480 (plain)
  1. #!/usr/bin/env bash
  2. # Monkeysphere authentication update-users subcommand
  3. #
  4. # The monkeysphere scripts are written by:
  5. # Jameson Rollins <jrollins@fifthhorseman.net>
  6. # Jamie McClelland <jm@mayfirst.org>
  7. # Daniel Kahn Gillmor <dkg@fifthhorseman.net>
  8. #
  9. # They are Copyright 2008, and are all released under the GPL, version 3
  10. # or later.
  11. update_users() {
  12. if [ "$1" ] ; then
  13. # get users from command line
  14. unames="$@"
  15. else
  16. # or just look at all users if none specified
  17. unames=$(getent passwd | cut -d: -f1)
  18. fi
  19. RETCODE=0
  20. # set mode
  21. MODE="authorized_keys"
  22. # set gnupg home
  23. GNUPGHOME="$GNUPGHOME_AUTHENTICATION"
  24. # check to see if the gpg trust database has been initialized
  25. if [ ! -s "${GNUPGHOME}/trustdb.gpg" ] ; then
  26. failure "GNUPG trust database uninitialized. Please see MONKEYSPHERE-SERVER(8)."
  27. fi
  28. # make sure the authorized_keys directory exists
  29. mkdir -p "${SYSDATADIR}/authorized_keys"
  30. # loop over users
  31. for uname in $unames ; do
  32. # check all specified users exist
  33. if ! id "$uname" >/dev/null ; then
  34. log error "----- unknown user '$uname' -----"
  35. continue
  36. fi
  37. log verbose "----- user: $uname -----"
  38. # make temporary directory
  39. TMPLOC=$(mktemp -d ${MSTMPDIR}/tmp.XXXXXXXXXX) || failure "Could not create temporary directory!"
  40. # trap to delete temporary directory on exit
  41. trap "rm -rf $TMPLOC" EXIT
  42. # create temporary authorized_user_ids file
  43. TMP_AUTHORIZED_USER_IDS="${TMPLOC}/authorized_user_ids"
  44. touch "$TMP_AUTHORIZED_USER_IDS"
  45. # create temporary authorized_keys file
  46. AUTHORIZED_KEYS="${TMPLOC}/authorized_keys"
  47. touch "$AUTHORIZED_KEYS"
  48. # set restrictive permissions on the temporary files
  49. # FIXME: is there a better way to do this?
  50. chmod 0700 "$TMPLOC"
  51. chmod 0600 "$AUTHORIZED_KEYS"
  52. chmod 0600 "$TMP_AUTHORIZED_USER_IDS"
  53. chown -R "$MONKEYSPHERE_USER" "$TMPLOC"
  54. # process authorized_user_ids file
  55. log debug "checking for authorized_user_ids..."
  56. # translating ssh-style path variables
  57. authorizedUserIDs=$(translate_ssh_variables "$uname" "$AUTHORIZED_USER_IDS")
  58. if [ -s "$authorizedUserIDs" ] ; then
  59. # check permissions on the authorized_user_ids file path
  60. if check_key_file_permissions "$uname" "$authorizedUserIDs" ; then
  61. # copy user authorized_user_ids file to temporary
  62. # location
  63. cat "$authorizedUserIDs" > "$TMP_AUTHORIZED_USER_IDS"
  64. # export needed variables
  65. export AUTHORIZED_KEYS
  66. export TMP_AUTHORIZED_USER_IDS
  67. # process authorized_user_ids file, as monkeysphere user
  68. su_monkeysphere_user \
  69. ". ${SYSSHAREDIR}/common; process_authorized_user_ids $TMP_AUTHORIZED_USER_IDS"
  70. RETURN="$?"
  71. else
  72. log debug "not processing authorized_user_ids."
  73. fi
  74. else
  75. log debug "empty or absent authorized_user_ids file."
  76. fi
  77. # add user-controlled authorized_keys file if specified translate
  78. # ssh-style path variables
  79. rawAuthorizedKeys=$(translate_ssh_variables "$uname" "$RAW_AUTHORIZED_KEYS")
  80. if [ "$rawAuthorizedKeys" != 'none' ] ; then
  81. log debug "checking for raw authorized_keys..."
  82. if [ -s "$rawAuthorizedKeys" ] ; then
  83. # check permissions on the authorized_keys file path
  84. if check_key_file_permissions "$uname" "$rawAuthorizedKeys" ; then
  85. log verbose "adding raw authorized_keys file... "
  86. cat "$rawAuthorizedKeys" >> "$AUTHORIZED_KEYS"
  87. else
  88. log debug "not adding raw authorized_keys file."
  89. fi
  90. else
  91. log debug "empty or absent authorized_keys file."
  92. fi
  93. fi
  94. # move the new authorized_keys file into place
  95. if [ -s "$AUTHORIZED_KEYS" ] ; then
  96. # openssh appears to check the contents of the authorized_keys
  97. # file as the user in question, so the file must be readable
  98. # by that user at least.
  99. # but in general, we don't want the user tampering with this
  100. # file directly, so we'll adopt this approach: Own the file by
  101. # the monkeysphere-server invoker (usually root, but should be
  102. # the same uid that sshd is launched as); change the group of
  103. # the file so that members of the user's group can read it.
  104. # FIXME: is there a better way to do this?
  105. chown $(whoami) "$AUTHORIZED_KEYS" && \
  106. chgrp $(id -g "$uname") "$AUTHORIZED_KEYS" && \
  107. chmod g+r "$AUTHORIZED_KEYS" && \
  108. mv -f "$AUTHORIZED_KEYS" "${SYSDATADIR}/authorized_keys/${uname}" || \
  109. {
  110. log error "Failed to install authorized_keys for '$uname'!"
  111. rm -f "${SYSDATADIR}/authorized_keys/${uname}"
  112. # indicate that there has been a failure:
  113. RETURN=1
  114. }
  115. else
  116. rm -f "${SYSDATADIR}/authorized_keys/${uname}"
  117. fi
  118. # unset the trap
  119. trap - EXIT
  120. # destroy temporary directory
  121. rm -rf "$TMPLOC"
  122. done
  123. }