Openvpn execute script on client-connect with restricted user

openvpn

I am trying to execute a command on my openvpn-server every time a client connects. I have an user openvpn that was created by using

adduser --system --shell /usr/sbin/nologin --no-create-home openvpn
groupadd openvpn
usermod -g openvpn openvpn

This user is used in my server config (and you also see the command I want to execute on every client-connect):

... <parts omitted>
user openvpn
group openvpn

client-connect '/bin/bash -c "sudo /usr/local/psa/bin/dns -a my.domain -a $common_name -ip $ifconfig_pool_remote_ip"'
script-security 2

I also added an entry via visudo (openvpn ALL=NOPASSWD: /usr/local/psa/bin/dns) to make the script executable for openvpn with sudo.
The server starts without errors, but if a client (in this log with the name server03) connects, the script fails with the following log:

sudo[31369]:  openvpn : TTY=unknown ; PWD=/etc/openvpn ; USER=root ; COMMAND=/usr/local/psa/bin/dns -a my.domain -a server03 -ip 10.9.0.2
sudo[31369]: pam_unix(sudo:session): session opened for user root by (uid=0)
sudo[31369]: pam_unix(sudo:session): session closed for user root
ovpn-user-server[31357]: server03/87.XXX.XXX.4:40594 WARNING: Failed running command (--client-connect): external program exited with error status: 1

As you can see in the logs the command is executed with the right variables, but somehow fails. But if I execute the command (as root) from the CMD, it terminates successfully and also if I log into the openvpn-user via

su -s /bin/bash openvpn

the script also terminates successfully. Log:

openvpn@...:/etc/openvpn$ sudo /usr/local/psa/bin/dns -a my.domain -a server03 -ip 10.9.0.2
SUCCESS: Creation of DNS record in Domain 'my.domain' complete.

What am I doing wrong?

Best Answer

Sudo as a default require TTY. Once you run the script manually TTY is availbale. Once it is run in cron or (I guess also) by openvpn as called script there is no TTY available.

I see 2 options: