The Community Forums

Interact with an entire community of cPanel & WHM users.
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

postsuspendacct -> change dns ? simple script

Discussion in 'cPanel Developers' started by altomarketing2, Oct 28, 2011.

  1. altomarketing2

    altomarketing2 Well-Known Member

    Joined:
    Oct 8, 2004
    Messages:
    55
    Likes Received:
    0
    Trophy Points:
    6
    Location:
    SouthAmerica
    Hi, im not a full developer so im looking to do this:

    a simple thing: After suspend an account change comment MX dns to default in server so queries does not resolve

    Why ? i have thousands of user using gmail or livedomain , and they dont care about suspending, because our dns server still works, and they keep using they emails.


    maybe using ApiZoneEdit < ApiDocs/Api2 < TWiki

    and/or cPanel & WHM Script Hooks

    maybe with a /scripts/postsuspendacct

    How can I use it ?

    Could someone write this code?
     
  2. morissette

    morissette Well-Known Member

    Joined:
    May 24, 2009
    Messages:
    119
    Likes Received:
    0
    Trophy Points:
    16
    Location:
    Austin, TX
    cPanel Access Level:
    Root Administrator
    I am willing to write this for you but the ApiZoneEdit function does not provide functionality for MX record edits so it would need to be done without the API. Is perl good for you, I'll do it for $25. Contact me at mori@vabch.org for more details on experience and so forth.
     
  3. sirdopes

    sirdopes Well-Known Member
    PartnerNOC

    Joined:
    Sep 25, 2007
    Messages:
    141
    Likes Received:
    0
    Trophy Points:
    16
    An easy way to do this would be to use postsuspendacct to copy the current zone file to the users suspended folder or another folder, then use ZoneEdit::resetzone to set all of the records back to the defaults. There is also a preunsuspendacct to copy the zone file back.
     
  4. altomarketing2

    altomarketing2 Well-Known Member

    Joined:
    Oct 8, 2004
    Messages:
    55
    Likes Received:
    0
    Trophy Points:
    6
    Location:
    SouthAmerica
    Can you do it for me?
     
  5. morissette

    morissette Well-Known Member

    Joined:
    May 24, 2009
    Messages:
    119
    Likes Received:
    0
    Trophy Points:
    16
    Location:
    Austin, TX
    cPanel Access Level:
    Root Administrator
    You know what, im bored, I'll do it to add to my completed list. I'll let you know when it's done, do you want the code posted here or in pm?
     
  6. morissette

    morissette Well-Known Member

    Joined:
    May 24, 2009
    Messages:
    119
    Likes Received:
    0
    Trophy Points:
    16
    Location:
    Austin, TX
    cPanel Access Level:
    Root Administrator
    Done...

    Code:
    
    #!/usr/bin/perl
    ###### Matthew Harris ######
    ### Sunday October 30, 2011 ###
    ### Post Suspension DNS UP ###
    ######################
    
    use strict;
    use warnings;
    
    use lib '/usr/local/cpanel';
    use Cpanel::PublicAPI();
    use Cpanel::AddonDomain();
    use File::Copy;
    
    get_accesshash();
    my $cp = Cpanel::PublicAPI->new( 'user' => 'root', 'accesshash' => my $accesshash );
    $ENV{'REMOTE_USER'} = 'root';
    
    # variable declaration
    my $user = $ARGV[0];
    my $suspzone = "/var/named/suspended";
    my(@add_ons, $maindom);
    
    deps();
    findaddons();
    maindom();
    backupaddons();
    editmx();
    rndcre();
    
    sub deps{
    	if(!-d $suspzone){
    		mkdir($suspzone, 0755) or die "Unable to create $suspzone\n";
    	}
    }
    
    sub findaddons{
    	my $addons = $cp->cpanel_api2_request('whostmgr', { 'module' => 'AddonDomain', 'func' => 'listaddondomains', 'user' => "$user" },
    		{
    			'regex' => ".*",
    		}
    	);
    	@add_ons = map $_->{domain}, @{$addons->{cpanelresult}{data}};
    }
    
    sub maindom{
            my $ownfile = '/etc/trueuserdomains';
            open my $fh, "<", $ownfile;
                    my @contents = <$fh>;
                    @contents = grep /$user/, @contents;
                    ($maindom, undef) = split(':', $contents[0]);   
            close $fh;
    }
    
    sub backupaddons{
    	foreach(@add_ons){
    		my $zonefile = "$_.db";
    		copy("/var/named/$zonefile", "$suspzone/$zonefile") or die "Could not backup zonefile: $!";
    	}
            copy("/var/named/$maindom.db", "$suspzone/$maindom.db") or die "Could not backup zonefile: $!";
    }
    
    sub editmx{
    	my($zonefile, $fh, @contents, @maincont, $main, $serial, $newserial);
    	foreach(@add_ons){
    		$zonefile = "/var/named/$_.db";
    	        open $fh, "<", $zonefile;
    	        @contents = <$fh>;
    		@contents = grep !/MX/, @contents;
           		close $fh;
    		open $fh, ">", $zonefile;
    		foreach(@contents){
            		if($_ =~ /([0-9]{10}).*serial/){
                    		$serial = $1;
                    		$newserial = $serial + 1;
                    		$_ =~ s/$serial/$newserial/g;
                    		print $fh @contents;
            		}
    		}
    		close $fh;
    	}
    	my $maindomzone = "/var/named/$maindom.db";
    	open $main, "<", "$maindomzone";
    	@maincont = <$main>;
    	@maincont = grep !/MX/, @maincont;
    	close $main;
    	open $main, ">", $maindomzone;
    	   	foreach(@maincont){
                            if($_ =~ /([0-9]{10}).*serial/){
                                    $serial = $1;
                                    $newserial = $serial + 1;
                                    $_ =~ s/$serial/$newserial/g;
                                    print $main @maincont;
                            }
                    }
    	close $main;
    }
    
    sub rndcre{
    	system("/usr/sbin/rndc reload");
    }
    
    sub get_accesshash{
    	open my $hash, "<", "/root/.accesshash";
            	my @accesshash = <$hash>;
    	close $hash;
    	foreach(@accesshash){
            	$accesshash .= $_
    	}
    }
    
     
  7. morissette

    morissette Well-Known Member

    Joined:
    May 24, 2009
    Messages:
    119
    Likes Received:
    0
    Trophy Points:
    16
    Location:
    Austin, TX
    cPanel Access Level:
    Root Administrator
    Note this was only tested via running directly and not as /scripts/postsuspendacct

    May need to add/change the following:

    Code:
    my %OPTS = @ARGV 
    my $user = $OPTS{'user'}; 
    
    So the user being suspended is passed correctly to the hook
     
    #7 morissette, Oct 30, 2011
    Last edited: Oct 30, 2011
  8. altomarketing2

    altomarketing2 Well-Known Member

    Joined:
    Oct 8, 2004
    Messages:
    55
    Likes Received:
    0
    Trophy Points:
    6
    Location:
    SouthAmerica
    Thanks !!

    you said "May need to add/change the following:"

    I did not understand that....
     
  9. morissette

    morissette Well-Known Member

    Joined:
    May 24, 2009
    Messages:
    119
    Likes Received:
    0
    Trophy Points:
    16
    Location:
    Austin, TX
    cPanel Access Level:
    Root Administrator
    Under the Script Hooks page (cPanel & WHM Script Hooks) it shows the code I listed for accepting the arguments from the suspension script, I did not implement this at the time of writing as I did not see that section.

    So pretty much all you should need to do is change


    my $user = $ARGV[0];

    to


    my %OPTS = @ARGV;
    my $user = $OPTS{'user'};
     
Loading...

Share This Page