Adding custom dns-entries on sub-domain creation

mhofer

Member
May 10, 2013
16
0
1
cPanel Access Level
Root Administrator
Hi

Because we were not satisfied with the default proxy-subdomain creation of cpanel/whm, we try to create our own.
For domains / parked domains / addon-domains this was no problem by editing the Zone-Template files.

Unfortunately I could not find something similar for subdomains... that's why i'm trying to create something manually.

I tried it with the CustomEventHandler.pm as follows:
Code:
package Cpanel::CustomEventHandler;

# cpanel12 - CustomEventHandler.pm                      Copyright(c) 2012 cPanel, Inc.
#                                                           All rights Reserved.
# [email protected]                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

use strict;
use Cpanel::Logger ();

# api_version = api_version
# type = pre,post
# module = Cpanel::<modname>
# event = the event ie addpop
# cfg ref is a hash of the conf variables passed to the event. If its a legacy event, the hash keys are numbered, newer events have names.
# dataref = the data returned from the event (post events only)
sub event {
    my ( $api_version, $type, $module, $func, $cfgref, $dataref ) = @_;
    ## UAPI note: $api_version will be '3', and the method signature becomes the similar:
    ##   my ( $api_version, $type, $module, $func, $args, $result ) = @_;

    if ($module eq 'subdomain') {
        if ($func eq 'addsubdomain') {
            if ($type eq 'post') {
                my $hdata = $cfgref->[0];
                my $sub = $hdata->{'domain'};
                my $domain = $hdata->{'rootdomain'};
                #require Cpanel::API
                Cpanel::API::execute( 'ZoneEdit', 'add_zone_record', { domain => $domain, name => 'autoconfig.' .  $sub, type => 'CNAME', cname => $domain . '.' } );
            }
        }
    }

    return 1;
}

1;
when i create a subdomain in cpanel, it gets created normally and this script has no effect.

Can someone help me figure out why? Is there an easier solution to accomplish this?

Following should be done:
- user creates subdomain
- system creates several custom dns-entries based on this subdomain
¨
Greetings
Marcel
 

KostonConsulting

Well-Known Member
Verifed Vendor
Jun 17, 2010
255
1
68
San Francisco, CA
cPanel Access Level
Root Administrator
Marcel,
Don't use CustomEventHandler.pm, it's an old system and there can only be one CustomEventHandler.pm so you run the risk of it getting overwritten by some 3rd party plugin that hasn't moved past that hook yet.

Instead, use cPanel's Standardized Hooks (Introduction to Standardized Hooks). Here's an example of adding a hook for Subdomain::addsubdomain():


Hook code:

Code:
#!/usr/bin/perl

package ScriptHook;

sub describe {
    my $hooks = {
        'hook'      => 'ScriptHook::changedns',
        'function'  => 'Api2::Subdomain::addsubdomain',
        'exectype'  => 'module',
        'namespace' => 'Cpanel',
        'stage'     => 'post'
    };
    return $hooks;
}

sub changedns {
    #require Cpanel::API
    Cpanel::API::execute( 'ZoneEdit', 'add_zone_record', { domain => $domain, name => 'autoconfig.' .  $sub, type => 'CNAME', cname => $domain . '.' } );
}

1;

Here's how to add the module hook:

Code:
$mkdir /var/cpanel/perl5
$mkdir /var/cpanel/perl5/lib
$cp ScriptHook.pm /var/cpanel/perl5/lib/
$/usr/local/cpanel/bin/manage_hooks add module ScriptHook