virtualmin / virtualmin-gpl

Virtualmin web hosting control panel for Webmin
https://www.virtualmin.com
GNU General Public License v3.0
376 stars 118 forks source link

$VIRTUALSERVER_GID returns the group name and not the GID #1010

Closed danboid closed 1 month ago

danboid commented 2 months ago
os: Ubuntu Linux 22.04.5
theme version: 23.01
virtualmin version: 7.20.2.pro-1
webmin version: 2.301

I am trying to write a domain/user creation script that will auto configure a users ~/.snapshots dir to be chowned root:$GID where $GID is the group ID of the new user but when I use $VIRTUALSERVER_GID in such a script I get the text name of the group returned instead of the GID which should take the form of a 4 digit int and not a string.

$VIRTUALSERVER_GROUP is for the users group name as a string but I need GID.

Thanks

jcameron commented 2 months ago

That's odd, because these variables come directly from each domain's config file.

If you run virtualmin list-domains --domain yourdom.com --file-only to get the config file, what does the gid= line in that file contain?

danboid commented 2 months ago

That's odd, because these variables come directly from each domain's config file.

If you run virtualmin list-domains --domain yourdom.com --file-only to get the config file, what does the gid= line in that file contain?

I ran

virtualmin list-domains --domain yourdom.com --file-only

then looked in the file whose path it printed and gid was given as a number, which is what I want.

This is what I'm trying to run. This script is to be run by vmin's "Command to run before making changes to a server" option:

#!/bin/sh
if [ "$VIRTUALSERVER_ACTION" = "CREATE_DOMAIN" ]; then
      #Create a new subvol for the user
      /usr/bin/btrfs sub create /home/$VIRTUALSERVER_USER

      #Limit the subvol to 1GB
      /usr/bin/btrfs qgroup limit 1024M /home/$VIRTUALSERVER_USER

          #Create a snapper config for the new user/subvol
          /usr/bin/snapper -c $VIRTUALSERVER_USER create-config /home/$VIRTUALSERVER_USER

      #Configure the users snapper config to allow access to their snapshots
      /usr/bin/snapper -c $VIRTUALSERVER_USER set-config ALLOW_USERS=$VIRTUALSERVER_USER SYNC_ACL=yes

      #These commands don't work yet as vmin doesn't return $VIRTUALSERVER_GID as an integer.
      #chown root:$VIRTUALSERVER_GID /home/$VIRTUALSERVER_USER/.snapshots/
      #chmod g+rx /home/$VIRTUALSERVER_USER/.snapshots/
fi

The chown command will only work if I feed it the groups GID number but its getting the groups name instead.

If I stick a:

echo $VIRTUALSERVER_GID > /var/log/vmintest

In that script before the

chown root:$VIRTUALSERVER_GID /home/$VIRTUALSERVER_USER/.snapshots/

/var/log/vmintest contains the group name in its text and not its numeric format after the script has been run.

jcameron commented 2 months ago

Is this domain a top-level server, or a sub-server? Also are you creating it from the UI, or from the CLI?

danboid commented 2 months ago

This script is for creating a top level server from the cli using the vmin create-domain command.

jcameron commented 2 months ago

Ok I think I see the issue - you can't use $VIRTUALSERVER_GID in the pre-creation script because it hasn't been allocated yet.

The best work-around would be to do the chown in a post-creation script.

danboid commented 2 months ago

When/how to run such a command? I need this command to be run after the users home dir/subvol and ~/.snapshots subvol has been created.

https://www.virtualmin.com/docs/development/domain-management-api/#available-variables-in-scripts lists the following actions:

Action Description CREATE_DOMAIN Creating a new virtual server MODIFY_DOMAIN Modifying a virtual server DELETE_DOMAIN Deleting a virtual server DISABLE_DOMAIN Temporarily disabling a server ENABLE_DOMAIN Re-enabling a server DBNAME_DOMAIN Changing a server’s database login DBPASS_DOMAIN Changing a server’s database password RESTORE_DOMAIN Restoring a server from backup SSL_DOMAIN Modifying a server’s SSL certificate

None of them sound suitable apart from CREATE_DOMAIN but thats what I've been trying to use.

iliajie commented 2 months ago

Could you dump the available environment variables to see what's there?

printenv > /tmp/.webmin/printenv
danboid commented 2 months ago

printenv output for root user:

SHELL=/bin/bash
SUDO_GID=1003
LC_ADDRESS=C.UTF-8
LC_NAME=C.UTF-8
SUDO_COMMAND=/bin/bash
LC_MONETARY=C.UTF-8
SUDO_USER=myuserhere
PWD=/home/myuseragain
LOGNAME=root
HOME=/root
LC_PAPER=C.UTF-8
LANG=C.UTF-8
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.webp=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:
LESSCLOSE=/usr/bin/lesspipe %s %s
LC_IDENTIFICATION=C.UTF-8
TERM=xterm-256color
LESSOPEN=| /usr/bin/lesspipe %s
USER=root
SHLVL=1
LC_TELEPHONE=C.UTF-8
LC_MEASUREMENT=C.UTF-8
LC_TIME=C.UTF-8
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin
SUDO_UID=1002
MAIL=/var/mail/root
LC_NUMERIC=C.UTF-8
_=/usr/bin/printenv
iliajie commented 2 months ago

Did you run it from your script though!?

jcameron commented 2 months ago

You can configure the post-domain creation script here :

Image

It gets run at the end of the whole process though..

danboid commented 1 month ago

Thanks @jcameron , $VIRTUALSERVER_GID works as I want when I run it via "Command to run after making changes to a server".

danboid commented 1 month ago

I have updated my virtualmin BTRFS / snapper integration script forum post, now that I know how snapper really works:

https://forum.virtualmin.com/t/my-updated-virtualmin-btrfs-snapper-integration-script/131638/2

jcameron commented 1 month ago

FYI, the reason why we can't make VIRTUALSERVER_GID available in the pre-creation script is that we can't be 100% sure what GID will be allocated until the group is actually created, as there could be two domains being created at the same time.

iliajie commented 1 month ago

It's not clear—why not simply run all that logic in the post-creation script where UID and GUI will be available?

jcameron commented 1 month ago

I think that's what the OP ended up doing.

danboid commented 1 month ago

It's not clear—why not simply run all that logic in the post-creation script where UID and GUI will be available?

Why not? That would be tidier, I've just not tested it. No reason why it wouldn't work right?