Viewing File: /usr/local/cpanel/whostmgr/docroot/cgi/activate_remote_nameservers.cgi

#!/usr/local/cpanel/3rdparty/bin/perl

# cpanel - whostmgr/docroot/cgi/activate_remote_nameservers.cgi
#                                                  Copyright 2022 cPanel, L.L.C.
#                                                           All rights reserved.
# copyright@cpanel.net                                         http://cpanel.net
# This code is subject to the cPanel license. Unauthorized copying is prohibited

use strict;
use warnings;

use Cpanel::Form               ();
use Cpanel::IxHash             ();
use Cpanel::Encoder::Tiny      ();
use Cpanel::DNSLib::PeerConfig ();
use Whostmgr::DNS::Cluster     ();
use Whostmgr::DNS::Cluster::UI ();
use Whostmgr::HTMLInterface    ();
use Cpanel::Encoder::Tiny      ();
use Cpanel::LoadModule         ();
use Cpanel::DNSLib::PeerStatus ();

$Cpanel::App::appname   = 'whostmgr-cgi';
$Cpanel::IxHash::Modify = 'safe_html_encode';

local $| = 1;

# init_app() will init ACLS and verify we have the
# cluster acl.  TODO: Refactor all the cgis
# to use Whostmgr::CgiApp::DnsCluster
Whostmgr::DNS::Cluster::UI::init_app(1);

my %FORM         = Cpanel::Form::parseform();
my $cluster_user = Whostmgr::DNS::Cluster::get_validated_cluster_user_from_formenv( $FORM{'cluster_user'}, $ENV{'REMOTE_USER'} );

Whostmgr::DNS::Cluster::UI::render_cluster_masquerade_include_if_available($cluster_user);

my $pm = $FORM{'module'} // '';
$pm =~ s/\///g;
$pm =~ s/\.yaml$//g;

Whostmgr::DNS::Cluster::UI::fatal_error_and_exit("Invalid module: $pm")
  unless $Whostmgr::DNS::Cluster::VALID_NS_MODULES{$pm};

my $namespace = "Cpanel::NameServer::Setup::Remote::$pm";
Cpanel::LoadModule::load_perl_module($namespace);

my ( $status, $statusmsg, $notices, $servername );
{
    # TODO:
    #
    # Setting $ENV{'REMOTE_USER'} is a workaround to ensure all third party
    # Cpanel::NameServer::Setup::Remote modules continue to work
    #
    # We should come up with a better method to pass the user to
    # setup the nameserver remote for in the future
    #
    local $ENV{'REMOTE_USER'} = $cluster_user;
    local $@;
    ( $status, $statusmsg, $notices, $servername ) = eval { $namespace->setup(%FORM) };
    $statusmsg ||= $@;
}

if ($status) {
    print '<div class="okmsg">' . join( '<br />', split( /\n/, Cpanel::Encoder::Tiny::safe_html_encode_str($statusmsg) ) ) . '</div>';

    ( $status, $statusmsg ) = Cpanel::DNSLib::PeerConfig::change_dns_role( $servername, $FORM{'dnsrole'}, $cluster_user );

    if ($status) {
        print '<div class="okmsg" id="activateNameserverSucceeded">' . join( '<br />', split( /\n/, Cpanel::Encoder::Tiny::safe_html_encode_str($statusmsg) ) ) . '</div>';
    }
    else {
        print '<div class="errormsg" id="activateNameserverFailed">' . join( '<br />', split( /\n/, Cpanel::Encoder::Tiny::safe_html_encode_str($statusmsg) ) ) . '</div>';
    }

    # Invalidate cache for cluster member
    Cpanel::DNSLib::PeerStatus::invalidate_and_refresh_cache( $cluster_user, $servername );
}
else {
    print '<div class="errormsg" id="activateNameserverFailed">' . join( '<br />', split( /\n/, Cpanel::Encoder::Tiny::safe_html_encode_str($statusmsg) ) ) . '</div>';
}

print '<div class="warningmsg">' . join( '<br />', split( /\n/, Cpanel::Encoder::Tiny::safe_html_encode_str($notices) ) ) . '</div>' . "\n" if length $notices;

Whostmgr::DNS::Cluster::UI::render_return_to_cluster_status($cluster_user);
Whostmgr::HTMLInterface::deffooter();

#FIXME: template include /usr/local/cpanel/Cpanel/NameServer/Setup/Remote/$module/activate_remote_nameserver_footer.tmpl
Back to Directory File Manager