Bailing out from hookable event

sparek-3

Well-Known Member
Aug 10, 2002
2,019
226
368
cPanel Access Level
Root Administrator
Perhaps this is in documentation some where, I couldn't find it, but documentation is scattered all over the place and may or may not be up to date any more.

How does one bailout from allowing an action in a hookable event (pre stage)?

According to the documentation I found, printing BAILOUT to STDOUT is suppose to accomplish this, but does not appear to be doing anything.

I'm wanting to check arguments to an action and disallow if certain criteria is met.
 

sparek-3

Well-Known Member
Aug 10, 2002
2,019
226
368
cPanel Access Level
Root Administrator
It's seeing the BAILOUT, it's just not doing jack with it.


[2021-06-15 12:23:07 -0600] info [cpanel] STDOUT output from hook: /var/cpanel/hooks/addsub.php
[2021-06-15 12:23:07 -0600] info [cpanel] BAILOUT BAD

[2021-06-15 12:23:07 -0600] info [cpanel] End STDOUT from hook
[2021-06-15 12:23:07 -0600] warn [cpanel] An exception was thrown in the hook “/var/cpanel/hooks/addsub.php” for “Cpanel::Api2::SubDomain::addsubdomain”: BAILOUT The hook has thrown an exception indicating that it should be halted. BAD
at /usr/local/cpanel/Cpanel/Hooks.pm line 281.
Cpanel::Hooks::hook(HASH(0x2d45960), HASH(0x3039d30)) called at /usr/local/cpanel/Cpanel/EventHandler.pm line 61
Cpanel::EventHandler::_event(2, "pre", "SubDomain", "addsubdomain", HASH(0x3026ca8), undef) called at /usr/local/cpanel/Cpanel/Api2/Exec.pm line 118
Cpanel::Api2::Exec::api2_exec("SubDomain", "addsubdomain", HASH(0x2d03a58), HASH(0x3026ca8)) called at /usr/local/cpanel/Cpanel/Template/Plugin/Api2.pm line 211
eval {...} called at /usr/local/cpanel/Cpanel/Template/Plugin/Api2.pm line 211
Cpanel::Template::Plugin::Api2::_api2_exec("SubDomain", "addsubdomain", HASH(0x3026ca8)) called at (eval 3) line 1
eval {...} called at (eval 3) line 1
eval {...} called at (eval 3) line 1
Cpanel::Template::Shared::__ANON__(Template::Context=HASH(0x2cf1978)) called at /usr/local/cpanel/3rdparty/perl/532/lib/perl5/cpanel_lib/x86_64-linux-64int/Template/Document.pm line 162
eval {...} called at /usr/local/cpanel/3rdparty/perl/532/lib/perl5/cpanel_lib/x86_64-linux-64int/Template/Document.pm line 160
Template::Document::process(Template::Document=HASH(0x2cf4e38), Template::Context=HASH(0x2cf1978)) called at /usr/local/cpanel/3rdparty/perl/532/lib/perl5/cpanel_lib/x86_64-linux-64int/Template/Context.pm line 352
eval {...} called at /usr/local/cpanel/3rdparty/perl/532/lib/perl5/cpanel_lib/x86_64-linux-64int/Template/Context.pm line 322
Template::Context::process(Template::Context=HASH(0x2cf1978), Template::Document=HASH(0x2cf4e38)) called at /usr/local/cpanel/3rdparty/perl/532/lib/perl5/cpanel_lib/x86_64-linux-64int/Template/Service.pm line 94
eval {...} called at /usr/local/cpanel/3rdparty/perl/532/lib/perl5/cpanel_lib/x86_64-linux-64int/Template/Service.pm line 91
Template::Service::process(Template::Service=HASH(0x2cede90), "/usr/local/cpanel/base/frontend/paper_lantern/subdomain/doadd"..., HASH(0x2ced6b0)) called at /usr/local/cpanel/3rdparty/perl/532/lib/perl5/cpanel_lib/x86_64-linux-64int/Template.pm line 66
Template::process(Template=HASH(0x2ced9c8), "/usr/local/cpanel/base/frontend/paper_lantern/subdomain/doadd"..., HASH(0x2ced6b0), SCALAR(0x13e3588)) called at /usr/local/cpanel/Cpanel/Template.pm line 483
Cpanel::Template::process_template("cpanel", HASH(0x2ced6b0), HASH(0x2ced368)) called at cpanel.pl line 1109
cpanel::cpanel::cptt_exectag("/usr/local/cpanel/base/frontend/paper_lantern/subdomain/doadd"..., 1) called at cpanel.pl line 4634
cpanel::cpanel::run_standard_mode() called at cpanel.pl line 932
cpanel::cpanel::script("cpanel::cpanel", "./frontend/paper_lantern/subdomain/doadddomain.html.tt") called at cpanel.pl line 328


I like the "The hook has thrown an exception indicating that it should be halted." ... but we're going to go ahead and execute it anyway.
 

sparek-3

Well-Known Member
Aug 10, 2002
2,019
226
368
cPanel Access Level
Root Administrator
After spending some quality time reading through /usr/local/cpanel/Cpanel/Hooks.pm I surmised that you had to add a blocking element to the event hook.

But how does one add this? There's no documentation (that I can find) of /usr/local/cpanel/bin/manage_hooks having a blocking element.

But if you manually edit the respective yaml file in /var/cpanel/hooks/data/Cpanel/ and add a blocking: 1 to the yaml structure, the hook blocks the event as expected. But I seriously doubt this is the proper way of doing this.
 

sparek-3

Well-Known Member
Aug 10, 2002
2,019
226
368
cPanel Access Level
Root Administrator
You have to add blocking => 1 to the describe() function for the hook. Then this works as expected.

The only other issue I see is that the code:

echo "0 This is not allowed";

does not actually display the error message - "this is not allowed" - to the end user. Maybe it's not suppose to - instead users just get a generic There was a problem creating the sub-domain: - which is better than nothing and at least it disallows the action based on the defined criteria.
 

cPRex

Jurassic Moderator
Staff member
Oct 19, 2014
6,148
784
313
cPanel Access Level
Root Administrator
Hey there! What about our example here:


is that not working how you expect?
 

sparek-3

Well-Known Member
Aug 10, 2002
2,019
226
368
cPanel Access Level
Root Administrator
Is it suppose to output the error message to the end user's cPanel?

That doesn't seem to be the behavior I'm seeing.

I'm using PHP for the hookable event code and it's more simplified than the examples given

if (TRUE) {
print("BAILOUT Not allowed here");
}


It does prevent the subdomain from being created - but it doesn't display "Not allowed here" in the user's cPanel.

Maybe that's just not how the blocking message is suppose to work, it would be nice if it did. But I can live with it this way.
 

cPRex

Jurassic Moderator
Staff member
Oct 19, 2014
6,148
784
313
cPanel Access Level
Root Administrator
We'd like to do some testing on our end with this. Do you have some sample hook code you could provide so we could look into that? If it's too much to post here, feel free to make a ticket and mention this thread and I'll get to it soon.
 

sparek-3

Well-Known Member
Aug 10, 2002
2,019
226
368
cPanel Access Level
Root Administrator
The code I'm using:

PHP:
#!/usr/local/bin/php
<?php

function get_passed_data() {
    $raw_data = "";
    $stdin_fh = fopen('php://stdin', 'r');
    if ( is_resource($stdin_fh) ) {
        stream_set_blocking($stdin_fh, 0);
        while ( ($line = fgets( $stdin_fh, 1024 )) !== false ) {
            $raw_data .= trim($line);
        }
        fclose($stdin_fh);
    }
    if ($raw_data) {
        $input_data = json_decode($raw_data, true);
    } else {
        $input_data = array('context'=>array(),'data'=>array(), 'hook'=>array());
    }

    return $input_data;

}

function describe() {
        $event = array(
                'category' => 'Cpanel',
                'event' => 'Api2::SubDomain::addsubdomain',
                'stage' => 'pre',
                'hook' => __FILE__ . ' --check',
                'exectype' => 'script',
                'action' => '--check',
                'blocking' => 1,
        );
        return $event;
}

$switches = (count($argv) > 1) ? $argv : array();
if (in_array('--describe', $switches)) {
        print(json_encode(describe()));
}
elseif (in_array('--check', $switches)) {
        $cpanelinfo = get_passed_data();
        if (preg_match("/amazon/i", $cpanelinfo['data']['args']['domain'])) {
            print("BAILOUT Not allowed here");
        }
}
else {
        print("0 " . __FILE__ . " needs a valid switch");
}

The aim is to prevent users from creating subdomains with "amazon" in their name - I'll probably add to this later.

This works as intended - except end users don't see the "Not allowed here" message in part of the error message in their cPanel. And that may never have worked or been the intention of the error message. It's not a huge deal, but I'm just wondering if I'm missing something.
 

cPRex

Jurassic Moderator
Staff member
Oct 19, 2014
6,148
784
313
cPanel Access Level
Root Administrator
I haven't been able to come up with a good way to properly test this on my end, and I wasn't able to steal away a developer for some time today. I didn't want you to think I'd abandoned the thread though.

Is there any chance you could make a ticket for this so our team can do some more research?