soupnsandwich: There are more hoops for the OP's thread because he can't make assumptions about which desktop environment is used, can't assume that only one user is logged into computer, can't assume that the user's terminal is local, and can't configure the users Xorg access configuration behind their back, as xhost would do.
This line could be used to pass the xauth cookie from the user to root:
Code:
/bin/su $USER -c "/usr/bin/xauth extract - $DISPLAY" | /usr/bin/xauth merge -
The USER and DISPLAY variables can be extracted from a process's environment. The trick is figuring out what process to look for if you can't make assumptions on which desktop environment the user is running.
Perhaps "ps -u $USER" to collect the PIDs, and then looking at the environment of more than one process, extracting the USER and DISPLAY, sorting, and then running the list through `uniq' so that there are no repeated USER/PID pairs. Remember that a logged in user may not be using X at all.
After all of the needed information is gathered, and your variables set.