Custom commands - available user variables/array

SYSTEM INFORMATION
OS type and version Ubuntu 26.04.1 LTS
Webmin version 2.660
Virtualmin version 8.1.0 GPL

I work too much & missed my window to respond to my previous topic.
The previous topic is not solved so I’ve decided to make a request.

Some (actually many) custom commands must be run as root (else a permission error) but they are for other users that require certain functions.
These custom commands end up being for multiple users therefore the user name that invoked the command is necessary so that all users can apply something that only affects their specific user directory or database(s).
No matter what I do it seems that if the command has to be run as root then only the root user name is in a variable which is completely useless.
printenv only contains data for the root user because the command has to be run as root.
No variable or bash command seems to show the actual Virtualmin user that invoked the command.
Why not introduce a global array that has some user values that can be used this way?
Is it not possible for the parent window to pass this data somehow?

Please.. try my described scenario & perhaps you will see the frustration.
This is just a basic scenario that will show what variables/arrays are available.
Keep in mind that the website users on this system do not have sudo access nor any terminal access.

Create a custom command that runs a bash script and you can pass the user account at the end of the command.
Ensure the command is run as root and checkmark the user environment setting.

example command (try all known variables/array keys for the user account to see what is available - $REMOTE_USER is only an example that does not work!):
bash /usr/local/bin/virtualmin-example.sh “$REMOTE_USER”

Commands within your script have to be for the user account that executed the command but your OS will only allow root to invoke the commands else a permission error.
In this case it is only a test so we are only echoing the passed variable to see what we can work with.

The bash script for this example ( file: /usr/local/bin/virtualmin-example.sh )
printf ‘The first variable value passed as reference: %s’ “$1”;

This currently outputs nothing.
What I propose is to add the ability to access some parent user variables when the command has to run as root.
Multiple users will be using these commands but currently when a user is allowed to use custom commands they see them all.

Hello,

This should already be possible, I mean to run the command as root, but keep the logged-in Webmin username in $REMOTE_USER.

Set the form to:

  • Command: bash /usr/local/bin/virtualmin-example.sh "$REMOTE_USER"
  • Run as user: choose the second option and enter root
  • Use user’s environment: unchecked
  • Clear environment variables: No
  • Run on Webmin servers: this server
  • Available in Usermin: No

Test it first:

printf 'Webmin user: %s\nRunning as: %s\n' "$REMOTE_USER" "$(id -un)"

It will show:

Webmin user: exampleuser
Running as: root

Then use this script:

#!/bin/bash
set -euo pipefail

username=${1:?Missing Webmin username}

mapfile -t domains < <(
    virtualmin list-domains \
        --user "$username" \
        --toplevel \
        --name-only
)

if ((${#domains[@]} != 1)); then
    printf 'Expected one domain for %s, found %d\n' \
        "$username" "${#domains[@]}" >&2
    exit 1
fi

domain_name=${domains[0]}

printf 'User: %s\nDomain: %s\n' "$username" "$domain_name"

# Run the rest required commands here

The script does not need the user’s password because it already runs as root, so users should only be allowed to run the command, and not edit it or provide free-form parameters!

This worked!

Thanks very much.. now I can make custom commands that are tailored for the user that selects it. Such as database manipulation, clearing log files, etc.

I’m glad to hear that it worked for you!