summaryrefslogtreecommitdiff
path: root/src/subcommands/ma/update-users
blob: d3a72dc183d811bd1641349bb2c16cecdd4fcd5e (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. local unames
  13. local uname
  14. local authorizedKeysDir
  15. local authorizedUserIDs
  16. if [ "$1" ] ; then
  17. # get users from command line
  18. unames="$@"
  19. else
  20. # or just look at all users if none specified
  21. unames=$(getent passwd | cut -d: -f1)
  22. fi
  23. RETURN=0
  24. # set mode
  25. MODE="authorized_keys"
  26. # set gnupg home
  27. GNUPGHOME="$GNUPGHOME_SPHERE"
  28. # the authorized_keys directory
  29. authorizedKeysDir="${SYSDATADIR}/authentication/authorized_keys"
  30. # check to see if the gpg trust database has been initialized
  31. if [ ! -s "${GNUPGHOME}/trustdb.gpg" ] ; then
  32. failure "GNUPG trust database uninitialized. Please see MONKEYSPHERE-SERVER(8)."
  33. fi
  34. # make sure the authorized_keys directory exists
  35. mkdir -p "${authorizedKeysDir}"
  36. # loop over users
  37. for uname in $unames ; do
  38. # check all specified users exist
  39. if ! id "$uname" >/dev/null ; then
  40. log error "----- unknown user '$uname' -----"
  41. continue
  42. fi
  43. log verbose "----- user: $uname -----"
  44. # make temporary directory
  45. TMPLOC=$(mktemp -d ${MATMPDIR}/tmp.XXXXXXXXXX) || failure "Could not create temporary directory!"
  46. # trap to delete temporary directory on exit
  47. trap "rm -rf $TMPLOC" EXIT
  48. # create temporary authorized_user_ids file
  49. TMP_AUTHORIZED_USER_IDS="${TMPLOC}/authorized_user_ids"
  50. touch "$TMP_AUTHORIZED_USER_IDS"
  51. # create temporary authorized_keys file
  52. AUTHORIZED_KEYS="${TMPLOC}/authorized_keys"
  53. touch "$AUTHORIZED_KEYS"
  54. # set restrictive permissions on the temporary files
  55. # FIXME: is there a better way to do this?
  56. chmod 0700 "$TMPLOC"
  57. chmod 0600 "$AUTHORIZED_KEYS"
  58. chmod 0600 "$TMP_AUTHORIZED_USER_IDS"
  59. chown -R "$MONKEYSPHERE_USER" "$TMPLOC"
  60. # process authorized_user_ids file
  61. log debug "checking for authorized_user_ids..."
  62. # translating ssh-style path variables
  63. authorizedUserIDs=$(translate_ssh_variables "$uname" "$AUTHORIZED_USER_IDS")
  64. if [ -s "$authorizedUserIDs" ] ; then
  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; process_authorized_user_ids $TMP_AUTHORIZED_USER_IDS"
  76. RETURN="$?"
  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. RETURN=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. }