How to Use Wrapper to Get Root Privilege for Scripts in Plesk Module

When you try to build a Plesk module you will come across number of doubts in that some of them are

How to Run a Shell Script or Binary as Root in Plesk Module?
How to use plesk wrapper to get root privilege for scripts?
How to use plesk wrapper from sbin?
What to do with “failed to open stream: Permission denied” in plesk module?

Plesk-modules
Its very difficult to get help on plesk module development. The tutorial provided by Parallels is really insufficient and incomplete. When I was trying to build a plesk module package, I faced many problems and googling never gave any solution too. After doing so much research I was able to develop a plesk module for my Client. I hope this post will be helpful for many who are trying to build plesk module and wanted root privilege to run a script in it.

Following is one of the methods which I used successfully in plesk 10.4. I hope it will work for all plesk versions.

Imagine you want to build a plesk module and that should have functionality to set ownership of  configuration file to root:root or any other ownership. Also need to write in to /etc/passwd or any file which can be edited only with root permission. This functionality is not possible with simple php script like chown, or system functions. It is because, plesk modules runs under psaadm user privilege so it can’t change files which is under root privilege. But there is one solution for this which is provided in plesk i.e. plesk module wrappers. This can be used to get root privilege for some scripts. To achieve this, you can follow below procedure or steps.

Imagine you have a shell script [setperm.sh] which is used to set permission for all files related to your module. To execute this script with root privilege use following steps.

1) Place setperm.sh under “/usr/local/psa/admin/sbin”  [don’t forget this path is very important]
2) Create a symbolic link in “/usr/local/psa/admin/bin/modules/<ur-module-foldername>”. By executing
“ln -s /usr/local/psa/admin/sbin/wrapper /usr/local/psa/admin/bin/modules/<ur-module-foldername>/ setperm.sh”
3) You are done with wrapper; now you can use this script in any php file inside your module script and it will execute as root.
4) For example you can call this script like this
<?php
system(/usr/local/psa/admin/bin/modules/<ur-module-foldername>/ setperm.sh, $varout);
?>
5) You can create this symbolic link while building RPM itself.

So this is all about plesk wrapper usage to get root privilege for scripts under plesk module.