]> the.earth.li Git - autodns.git/blob - contrib/add-dns.pl
Make add-dns.pl croak if you don't configure it.
[autodns.git] / contrib / add-dns.pl
1 #!/usr/bin/perl -w
2 #
3 # add-dns.pl version 0.0.4alpha
4 # Dominic Hargreaves <dom@earth.li>
5 #
6 # Use this to tell nameservers supporting AutoDNS
7 # (http://www.earth.li/projectpurple/progs/autodns.html) to slave for you.
8 #
9 # Changelog:
10 #
11 # 03/07/01 dom: Added remotehelp and remove command for AutoDNS 0.0.4alpha
12 #               Changed to header to ns1 (from terry)
13 # 22/05/01 dom: Initial code completed for AutoDNS 0.0.3
14
15 use strict;
16 use IPC::Open3;
17
18 sub usage;
19
20
21 ##############################################################################
22 ## Configuration section
23 ##############################################################################
24
25 my $from = "FILL HERE";
26 my $keyid = "FILL HERE";
27 my $to = 'dns-auto@ns0.blackcatnetworks.co.uk,'.
28         'dns-auto@ns1.blackcatnetworks.co.uk';
29
30 ##############################################################################
31 ##############################################################################
32
33 if ($from eq "FILL HERE") {
34         die "You need to edit the script to specify your email address".
35                 " and GPG key ID (please also check the addresses of the".
36                 " servers to update)\n");
37 }
38
39 my $sendmail = "/usr/sbin/sendmail";
40 my $gpg = "/usr/bin/gpg";
41
42 #my $to = 'dom';
43 my $cc = $from;
44 #my $cc = "";
45 my $command;
46 my @validcommands = qw(add remove list remotehelp);
47
48 # Only proceed if we have been given a valid command
49
50 die usage unless $ARGV[0];
51
52 my $ok = 0;
53 for (@validcommands) {
54    if ($ARGV[0] eq $_) {
55       $ok = 1;
56    }
57 }
58
59 die usage unless ($ok == 1);
60
61 # Open gpg filehandle
62
63 open3(\*GPGIN, \*GPGOUT, \*GPGERR, "$gpg --default-key $keyid --clearsign");
64
65 # Generate AutoDNS commands
66
67 print GPGIN "BEGIN\n";
68
69 $command = shift(@ARGV);
70
71 if ($command eq "list") {
72    print GPGIN "LIST\n";
73 } elsif ($command eq "remotehelp") {
74    print GPGIN "HELP\n";
75 } elsif ($command eq "add") {
76    for (@ARGV) {
77       print GPGIN "ADD $_\n";
78    }
79 } elsif ($command eq "remove") {
80    for (@ARGV) {
81       print GPGIN "DEL $_\n";
82    }
83 } else {
84    die usage;
85 }
86
87 print GPGIN "END\n";
88
89 close GPGIN;
90
91 my @gpgerror=<GPGERR>;
92 my @gpgout=<GPGOUT>;
93 close GPGERR;
94 close GPGOUT;
95
96 # Show user any error output from gpg...
97
98 for (@gpgerror) {
99    print STDERR $_;
100 }
101
102 # ...and what we intend to send
103
104 for (@gpgout) {
105    print $_;
106 }
107
108 # Ask user whether this is ok
109
110 print "Send this mail to:\n";
111 print $to;
112 print "\nand\n";
113 print $cc;
114 print "\n? (y/n): ";
115
116 my $mark = "0";
117 while (<STDIN>) {
118    if (/^y/i) {
119       $mark = "1";
120       last;
121    }
122    /^n/i and last;
123 }
124
125 if (!($mark eq "1")) {
126    print "Mail NOT sent.\n";
127    exit 1;
128 }
129
130 # Compose email
131
132 open MAIL, "|$sendmail -t -oi" or die $!;
133 print MAIL "From: $from\n" or die "Configuration error: $!";
134 print MAIL "To: $to\n" or die "Configuration error: $!";
135 print MAIL "Cc: $cc\n" if $cc;
136 print MAIL "Subject: AutoDNS commands\n";
137 print MAIL "X-Mailer: add-dns.pl 0.0.4alpha\n\n";
138
139 for (@gpgout) {
140    print MAIL $_;
141 }
142
143 print MAIL "\n";
144
145 # Send email
146
147 close MAIL;
148
149 sub usage {
150    print<<EOF;
151 Usage: $0 <command> [list of domains]
152
153 Possible commands: add: Adds domains
154                    remove: Removes domains
155                    list: Lists current domains
156                    remotehelp: Requests help from remote end
157
158 (You must have the private key $keyid in your gpg keyring
159 for this to work)
160 EOF
161
162 }
163