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