This file is indexed.

/usr/share/perl5/Argonaut/Libraries/Ldap2zone.pm is in argonaut-ldap2zone 0.9.1-4.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#######################################################################
#
# Argonaut::Libraries::Ldap2zone -- create zone files from LDAP DNS zones
#
# Copyright (C) 2012-2014 FusionDirectory project <contact@fusiondirectory.org>
#
# Author: Côme BERNIGAUD
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>
#
#######################################################################

package Argonaut::Libraries::Ldap2zone;

use Exporter 'import';              # gives you Exporter's import() method directly
@EXPORT_OK = qw(&argonaut_ldap2zone);  # symbols to export on request

use strict;
use warnings;

use 5.008;

use DNS::ZoneParse;

use Argonaut::Libraries::Common qw(:ldap :config);

my @record_types = ('a','cname','mx','ns','ptr','txt','srv','hinfo','rp','loc');

=item argonaut_ldap2zone
Write a zone file for the LDAP zone and its reverse, generate named.conf files and assure they are included
Params : zone name, verbose flag
=cut
sub argonaut_ldap2zone
{
  my($zone,$verbose,$norefresh,$dumpdir) = @_;

  my $config = argonaut_read_config;

  my $settings = argonaut_get_ldap2zone_settings($config,$config->{'client_ip'});

  my $BIND_DIR                =   $settings->{'binddir'};
  my $BIND_CACHE_DIR          =   $settings->{'bindcachedir'};
  my ($output_BIND_DIR, $output_BIND_CACHE_DIR);
  if ($dumpdir) {
    $output_BIND_DIR = $dumpdir;
    $output_BIND_CACHE_DIR = $dumpdir;
  } else {
    $output_BIND_DIR = $BIND_DIR;
    $output_BIND_CACHE_DIR = $BIND_CACHE_DIR;
  }
  my $ALLOW_NOTIFY            =   $settings->{'allownotify'};
  my $ALLOW_UPDATE            =   $settings->{'allowupdate'};
  my $ALLOW_TRANSFER          =   $settings->{'allowtransfer'};
  my $TTL                     =   $settings->{'ttl'};
  my $RNDC                    =   $settings->{'rndc'};

  if (not -d $output_BIND_DIR) {
    die "Bind directory '$output_BIND_DIR' does not exist\n";
  }

  if (not -d $output_BIND_CACHE_DIR) {
    die "Bind cache directory '$output_BIND_CACHE_DIR' does not exist\n";
  }

  if (!-e $RNDC) {
    die "Rndc path '$RNDC' doesn't seem to exists\n";
  }

  if (substr($zone,-1) ne ".") { # If the end point is not there, add it
    $zone = $zone.".";
  }

  print "Searching DNS Zone '$zone'\n" if $verbose;

  my ($ldap,$ldap_base) = argonaut_ldap_handle($config);

  my $dn = zoneparse($ldap,$ldap_base,$zone,$output_BIND_CACHE_DIR,$TTL,$verbose);

  my $reverse_zone = get_reverse_zone($ldap,$ldap_base,$dn);
  print "Reverse zone is $reverse_zone\n" if $verbose;

  zoneparse($ldap,$ldap_base,$reverse_zone,$output_BIND_CACHE_DIR,$TTL,$verbose);

  create_namedconf($zone,$reverse_zone,$BIND_DIR,$BIND_CACHE_DIR,$output_BIND_DIR,$ALLOW_NOTIFY,$ALLOW_UPDATE,$ALLOW_TRANSFER,$verbose);

  unless ($norefresh) {
    system("$RNDC reconfig")  == 0 or die "$RNDC reconfig failed : $?";
    system("$RNDC freeze")    == 0 or die "$RNDC freeze failed : $?";
    system("$RNDC reload")    == 0 or die "$RNDC reload failed : $?";
    system("$RNDC thaw")      == 0 or die "$RNDC thaw failed : $?";
  }
}

=item zoneparse
Create a Zone file for a zone taken from the LDAP
Params : ldap handle, ldap base, zone name, bind dir, TTL, verbose flag
Returns : dn of the zone
=cut
sub zoneparse
{
  my ($ldap,$ldap_base,$zone,$output_BIND_CACHE_DIR,$TTL,$verbose) = @_;
  my $mesg = $ldap->search( # perform a search
          base   => $ldap_base,
          filter => "zoneName=$zone",
          #~ attrs => [ 'ipHostNumber' ]
          );

  $mesg->code && die "Error while searching DNS Zone '$zone' :".$mesg->error;

  print "Found ".scalar($mesg->entries())." results\n" if $verbose;

  my $zonefile = DNS::ZoneParse->new(\"", $zone);

  my $records = {};
  foreach my $record (@record_types) {
    eval { #try
      $records->{$record} = $zonefile->$record();
    };
    if ($@) { # catch
      print "This DNS::ZoneParse version does not support '$record' record\n" if $verbose;
    };
  }

  my $dn; # Dn of zone entry;

  foreach my $entry ($mesg->entries()) {
    my $name = $entry->get_value("relativeDomainName");
    if(!$name) { print "no name\n"; next; }
    my $class = $entry->get_value("dnsClass");
    if(!$class) { print "no class\n"; next; }
    my $ttl = $entry->get_value("dNSTTL");
    if(!$ttl) {
      $ttl = "";#$default_ttl;
    }
    while(my ($type,$list) = each %{$records}){
      foreach my $value ($entry->get_value($type."Record")) {
        if($name ne "@") {
          push @{$list},{ name => $name, class => $class,
                          host => $value, ttl => $ttl, ORIGIN => $zone };
        } else {
          push @{$list},{ host => $value, ttl => $ttl, ORIGIN => $zone };
        }
        print "Added record $type $name $class $value $ttl\n" if $verbose;
      }
    }
    my $soa = $entry->get_value("soaRecord");
    if($soa) {
      my $soa_record = $zonefile->soa();
      my (@soa_fields) = split(' ',$soa);
      $soa_record->{'primary'}  = $soa_fields[0];
      $soa_record->{'email'}    = $soa_fields[1];
      $soa_record->{'serial'}   = $soa_fields[2];
      $soa_record->{'refresh'}  = $soa_fields[3];
      $soa_record->{'retry'}    = $soa_fields[4];
      $soa_record->{'expire'}   = $soa_fields[5];
      $soa_record->{'minimumTTL'}  = $soa_fields[6];

      $soa_record->{'class'}    = $class;
      $soa_record->{'ttl'}      = $TTL;
      $soa_record->{'origin'}   = $name;
      $soa_record->{'ORIGIN'}   = $zone;
      print "Added record SOA $name $class $soa $TTL\n" if $verbose;
      $dn = $entry->dn();
    }
  }

  if (not defined $dn) {
    die "Zone $zone was not found in LDAP!\n";
  }

  # write the new zone file to disk
  print "Writing DNS Zone '$zone' in $output_BIND_CACHE_DIR/db.$zone\n" if $verbose;
  my $file_output = "$output_BIND_CACHE_DIR/db.$zone";
  my $newzone;
  open($newzone, '>', $file_output) or die "error while trying to open $file_output";
  print $newzone $zonefile->output();
  close $newzone;

  return $dn;
}

=item get_reverse_zone
Params : ldap handle, ldap base, zone dn
Returns : reverse zone name
=cut
sub get_reverse_zone
{
  my($ldap,$ldap_base,$zone_dn) = @_;
  my $mesg = $ldap->search( # Searching reverse zone name
          base   => $zone_dn,
          filter => "(&(zoneName=*)(relativeDomainName=@))",
          scope => 'one',
          attrs => [ 'zoneName' ]
          );

  $mesg->code && die "Error while searching DNS reverse zone :".$mesg->error;

  die "Error : found ".scalar($mesg->entries())." results  (1 expected) for reverse DNS zone of dn $zone_dn\n" if (scalar($mesg->entries()) != 1);

  return ($mesg->entries)[0]->get_value("zoneName");
}

=item create_namedconf
Create file $output_BIND_DIR/named.conf.ldap2zone
Params : zone name, reverse zone name
Returns :
=cut
sub create_namedconf
{
  my($zone,$reverse_zone,$BIND_DIR,$BIND_CACHE_DIR,$output_BIND_DIR,$ALLOW_NOTIFY,$ALLOW_UPDATE,$ALLOW_TRANSFER,$verbose) = @_;

  if($ALLOW_NOTIFY eq "TRUE") {
    $ALLOW_NOTIFY = "notify yes;";
  } else {
    $ALLOW_NOTIFY = "";
  }

  if ($ALLOW_UPDATE ne "") {
    $ALLOW_UPDATE = "allow-update {$ALLOW_UPDATE};";
  } else {
    $ALLOW_UPDATE = "";
  }

  if ($ALLOW_TRANSFER ne "") {
    $ALLOW_TRANSFER = "allow-transfer {$ALLOW_TRANSFER};";
  } else {
    $ALLOW_TRANSFER = "";
  }

  print "Writing named.conf file in $output_BIND_DIR/named.conf.ldap2zone.$zone\n" if $verbose;
  my $namedfile;
  open($namedfile, '>', "$output_BIND_DIR/named.conf.ldap2zone.$zone") or die "error while trying to open $output_BIND_DIR/named.conf.ldap2zone.$zone";
  print $namedfile <<EOF;
zone "$zone" {
  type master;
  $ALLOW_NOTIFY
  file "$BIND_CACHE_DIR/db.$zone";
  $ALLOW_UPDATE
  $ALLOW_TRANSFER
};
zone "$reverse_zone" {
  type master;
  $ALLOW_NOTIFY
  file "$BIND_CACHE_DIR/db.$reverse_zone";
  $ALLOW_UPDATE
  $ALLOW_TRANSFER
};
EOF
  close $namedfile;

  print "Writing file $output_BIND_DIR/named.conf.ldap2zone\n" if $verbose;
  open($namedfile, '>', "$output_BIND_DIR/named.conf.ldap2zone") or die "error while trying to open $output_BIND_DIR/named.conf.ldap2zone";
  opendir DIR, $output_BIND_DIR or die "Error while openning $output_BIND_DIR!";
  my @files = readdir DIR;
  foreach my $file (grep { /^named\.conf\.ldap2zone\./ } @files) {
    print $namedfile qq{include "$BIND_DIR/$file";\n};
  }
  close $namedfile;
}

1;

__END__