aboutsummaryrefslogtreecommitdiff
path: root/nfsn_cleanup.pl
blob: f72a9dc936cc40d1c1cc7010d62b41181f821143 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env perl
use warnings;
use strict;
use WebService::NFSN;
print "nfsn_cleanup.pl, Copyright (C) 2020 Miquel Lionel";

###vvvEDIT THISvvv###
my $USER = 'username';
my $API_KEY = '12345678901';
my $ACCOUNT_ID = 'A1B2-C3D4E5F6';
###^^^EDIT THIS^^^###

my $nfsn = WebService::NFSN->new($USER, $API_KEY);
my $DOMAIN = $ENV{'CERTBOT_DOMAIN'};
my $TXT_DATA = $ENV{'CERTBOT_VALIDATION'};

my $i = rindex($DOMAIN, '.');
my $NOTGTLD = substr ($DOMAIN,0,$i);
my $GTLD = substr ($DOMAIN, $i+1);
my $i2 = rindex($NOTGTLD, '.');
my $SUBDOMAIN = substr ($NOTGTLD,0,$i2);
my $PART_HOSTNAME = substr ($NOTGTLD, $i2+1);
my $HOSTNAME = qq{$PART_HOSTNAME.$GTLD};

my $dnszone =  $nfsn->dns($HOSTNAME);
my $ACME_CHALLENGE_STR = "_acme-challenge";

if ($NOTGTLD =~ /\./){
    # subdomain detected
    $ACME_CHALLENGE_STR = qq{$ACME_CHALLENGE_STR.$SUBDOMAIN};
}
else {
    # no subdomain
    $SUBDOMAIN = '';
}

$dnszone->removeRR(
    name => "$ACME_CHALLENGE_STR",
    type => 'TXT',
    data => "$TXT_DATA"
);

my $nfsn_err = $nfsn->last_response->decoded_content;
if ( $nfsn_err =~ /^([\w0-9])$/ ){
    die "\nNFSN API ERROR: $nfsn_err\n";
}
else{
    print "\nThe DNS records have been updated.\n";
}
sleep 5;