| 1 | #!/usr/bin/perl
 | 
|---|
| 2 | # Add "a bunch" of domains all at once.  Should rarely be used as most bulk
 | 
|---|
| 3 | # adds should be by AXFR rather than completely new domain names.
 | 
|---|
| 4 | ##
 | 
|---|
| 5 | #    This program is free software: you can redistribute it and/or modify
 | 
|---|
| 6 | #    it under the terms of the GNU General Public License as published by
 | 
|---|
| 7 | #    the Free Software Foundation, either version 3 of the License, or
 | 
|---|
| 8 | #    (at your option) any later version.
 | 
|---|
| 9 | #
 | 
|---|
| 10 | #    This program is distributed in the hope that it will be useful,
 | 
|---|
| 11 | #    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
| 12 | #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
| 13 | #    GNU General Public License for more details.
 | 
|---|
| 14 | #
 | 
|---|
| 15 | #    You should have received a copy of the GNU General Public License
 | 
|---|
| 16 | #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | 
|---|
| 17 | ##
 | 
|---|
| 18 | 
 | 
|---|
| 19 | 
 | 
|---|
| 20 | use strict;
 | 
|---|
| 21 | use warnings;
 | 
|---|
| 22 | 
 | 
|---|
| 23 | # push "the directory the script is in" into @INC
 | 
|---|
| 24 | use FindBin;
 | 
|---|
| 25 | use lib "$FindBin::RealBin/";
 | 
|---|
| 26 | 
 | 
|---|
| 27 | use DNSDB;
 | 
|---|
| 28 | 
 | 
|---|
| 29 | if (!@ARGV) {
 | 
|---|
| 30 |   print qq(usage: bulk-add-domain [domain list file 1] [domain list file 2] ...
 | 
|---|
| 31 |   Each domain list file should be a file with one to four comma-separated
 | 
|---|
| 32 |   elements:
 | 
|---|
| 33 |     Domain
 | 
|---|
| 34 |     Group
 | 
|---|
| 35 |     State (active/inactive)
 | 
|---|
| 36 |     Location/view
 | 
|---|
| 37 |   The group and location/view can be expressed as the internal group ID or
 | 
|---|
| 38 |   location ID instead of the text identifier.
 | 
|---|
| 39 | );
 | 
|---|
| 40 |   exit;
 | 
|---|
| 41 | }
 | 
|---|
| 42 | 
 | 
|---|
| 43 | my $dnsdb = new DNSDB;
 | 
|---|
| 44 | 
 | 
|---|
| 45 | # get userdata for log
 | 
|---|
| 46 | ($dnsdb->{logusername}, undef, undef, undef, undef, undef, $dnsdb->{logfullname}) = getpwuid($<);
 | 
|---|
| 47 | $dnsdb->{logfullname} =~ s/,//g;
 | 
|---|
| 48 | $dnsdb->{loguserid} = 0;        # not worth setting up a pseudouser the way the RPC system does
 | 
|---|
| 49 | $dnsdb->{logusername} = $dnsdb->{logusername}."/bulk-add-domain";
 | 
|---|
| 50 | $dnsdb->{logfullname} = $dnsdb->{logusername} if !$dnsdb->{logfullname};
 | 
|---|
| 51 | 
 | 
|---|
| 52 | foreach my $fname (@ARGV) {
 | 
|---|
| 53 |   if (-e $fname && !-z $fname) {
 | 
|---|
| 54 |     open DLIST, "<$fname";
 | 
|---|
| 55 |     while (<DLIST>) {
 | 
|---|
| 56 |       chomp;
 | 
|---|
| 57 |       my ($domain, $group, $state, $location) = split ',';
 | 
|---|
| 58 |       if ($domain !~ /^(?:[a-z0-9_-]+\.)+[a-z0-9_-]+$/) {
 | 
|---|
| 59 |         warn "skipping invalid domain $domain\n";
 | 
|---|
| 60 |         next;
 | 
|---|
| 61 |       }
 | 
|---|
| 62 |       $group = 1 if !defined($group);
 | 
|---|
| 63 |       ##fixme:  doesn't work with all-numeric group names
 | 
|---|
| 64 |       my $grpid = ($group =~ /^\d+$/ ? $group : $dnsdb->groupID($group) );
 | 
|---|
| 65 |       $grpid = 1 if !$grpid;
 | 
|---|
| 66 |       $state = 1 if !defined($state);
 | 
|---|
| 67 |       $state = 0 if !$state;
 | 
|---|
| 68 |       # Munge in some alternate state values
 | 
|---|
| 69 |       $state = 1 if $state eq 'active';
 | 
|---|
| 70 |       $state = 1 if $state eq 'on';
 | 
|---|
| 71 |       $state = 0 if $state eq 'inactive';
 | 
|---|
| 72 |       $state = 0 if $state eq 'off';
 | 
|---|
| 73 |       warn "invalid state for $domain, skipping\n" if !($state == 0 || $state == 1);
 | 
|---|
| 74 |       $location = '' if !defined($location);
 | 
|---|
| 75 |       
 | 
|---|
| 76 |       my ($res,$msg) = $dnsdb->addDomain($domain, $grpid, $state, $location);
 | 
|---|
| 77 |       warn "error adding $domain: $msg\n" if $res ne 'OK';
 | 
|---|
| 78 |     }
 | 
|---|
| 79 |   } else {
 | 
|---|
| 80 |     warn "$fname empty or missing\n";
 | 
|---|
| 81 |   }
 | 
|---|
| 82 | }
 | 
|---|