This file is indexed.

/usr/sbin/ldap2fai is in goto-fai 3.1-1.2.

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

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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
#!/usr/bin/perl -w
#
# Copyright (c) 2008 Landeshauptstadt München
# Copyright (c) 2008-2010 GONICUS GmbH
#
# Authors: Jan-Marek Glogowski
#          Cajus Pollmeier
#
# 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/>
#


=head1 NAME

ldap2fai - read FAI config from LDAP and create config space.

=head1 SYNOPSIS

ldap2fai [-hnvW] [-c config] [-D bind_dn ] [-w bind password] [-d dump_dir] [-H hostname] <MAC>

=head1 OPTIONS

B<-h>, B<--help>
    print out this help message

B<-v>, B<--verbose>
    be verbose (multiple v's will increase verbosity) 

B<-n>, B<--foreground>
    dry run (includes verbose)

B<-c>
    LDAP config file (default: /etc/ldap/ldap.conf)

B<-d>
    output dir (default: /var/lib/fai/config)

B<-D>
    bind dn

B<-W>
    prompt for password
    
B<-w>
    read password from command line
		
B<-H>
    check hostname

=head1 DESCRIPTION

ldap2fai is a script to create read the fai config from LDAP and create fai config space.

=head1 BUGS 

Please report any bugs, or post any suggestions, to the GOsa mailing list <gosa-devel@oss.gonicus.de> or to <https://oss.gonicus.de/labs/gosa>


=head1 LICENCE AND COPYRIGHT

This code is part of GOsa (L<http://www.gosa-project.org>)

COPYRIGHT
        Copyright (c) 2008 Landeshauptstadt München
        Copyright (c) 2008-2010 GONICUS GmbH

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.

=cut

use strict;
use warnings;

use Net::LDAP;
use Net::LDAP::Util qw(:escape);
use Getopt::Long;
use File::Path;

use GOsa::Common qw(:ldap :misc);
use GOsa::FAI qw(:flags);

my $bind_dn;
my $bind_pwd;
my $prompt_pwd;
my $ldapuris;
my $ldap_conf = "/etc/ldap/ldap.conf";
my $dump_dir = "/var/lib/fai/config";
my $verbose = 0;
my $print_classes = 0;
my( $hostname, $host_base, $host_dn, $host_tag );
my $fai_mirror;
my $dry_run = 0;
my $release_var = 'FAIclientRelease';
my $check_hostname;
my $kernel;

Getopt::Long::Configure ("bundling");

GetOptions( 'v|verbose' => \$verbose,
            'h|help' => \&usage,
            'c|config=s' => \$ldap_conf,
            'd|dump-dir=s' => \$dump_dir,
            'D|bind-dn=s' => \$bind_dn,
            'n|dry-run' => \$dry_run,
            'W|prompt-pwd' => \$prompt_pwd,
            'w|password=s' => \$bind_pwd,
            'H|hostname=s' => \$check_hostname )
  or usage( 'Wrong parameters' );

# If we use dry-run, be verbose
$verbose = 1 if( $dry_run );

# Get MAC from cmdline
my $mac = shift @ARGV;
$mac eq '' && usage( "MAC address not specified." );
usage( "No valid MAC address specified." )
  if( ! ($mac =~ m/^([0-9a-f]{2}:){5}[0-9a-f]{2}/i) );

# Is dump_dir a directory
if( ! $dry_run ) {
  -d "$dump_dir" 
    || usage("'$dump_dir' is not a directory.\n");
}

# initialize ldap
my $init_results =
  gosa_ldap_init( $ldap_conf, 0, $bind_dn, $prompt_pwd, $bind_pwd );
do_exit( 3, $init_results ) if( 'HASH' ne ref( $init_results ) );

my $ldap = $init_results->{ 'HANDLE' };
my $base = $init_results->{ 'BASE' };

# Get FAI object
my $faiobj = GOsa::FAI->new( 'LDAP' => $ldap,
                             'base' => $base,
                             'dumpdir' => $dump_dir );

# Set FAI flags
$faiobj->flags( FAI_FLAG_VERBOSE ) if( $verbose );
$faiobj->flags( FAI_FLAG_VERBOSE | FAI_FLAG_DRY_RUN ) if( $dry_run );

my $class_str = get_classes( $mac );
print( "  + FAIclass string:    $class_str\n" ) if( $verbose );

my ($res_classlist, $release) = $faiobj->resolve_classlist( $class_str );
if( 'ARRAY' eq ref( $res_classlist ) ) {
  if( $verbose ) {
    print( "  + Release:            $release\n" );
    print( "  + Resolved classlist: " . join( ' ', @$res_classlist ) . "\n" );
  }
}
else { do_exit( 8, $res_classlist ); }

if( ! $dry_run ) {
  create_dir( "$dump_dir/class" );
  open (FAICLASS,">$dump_dir/class/${hostname}")
    || do_exit( 4, "Can't create $dump_dir/class/${hostname}. $!\n" );
  print( FAICLASS join( ' ', @$res_classlist ) );
  close( FAICLASS );
}

$res_classlist = $faiobj->expand_fai_classlist( $res_classlist, $hostname );
if( 'ARRAY' eq ref( $res_classlist ) ) {
  print( "  + FAI classlist:      " . join( ' ', @$res_classlist ) . "\n" )
    if( $verbose );
}

print( "Extending FAI classtree with real objects...\n" );
$faiobj->extend_class_cache( $release );

print( "Dumping config space to '$dump_dir'...\n" );
my( $sections, $error ) = $faiobj->dump_release( $release, $res_classlist, $hostname );
print $error . "\n" if( defined $error );

if( defined ${hostname} ) {
  if( open( HOSTVARS, ">> ${dump_dir}/class/${hostname}.var" ) ) {
    print( HOSTVARS "FAIclientRelease='$release'\n" );
    print( HOSTVARS "MAXPACKAGES=20000\n" );
    print( HOSTVARS "printk=0\n" );
    print( HOSTVARS "STOP_ON_ERROR=700\n" );
    close( HOSTVARS );
  }
}

generate_sources_list( $sections );
generate_kernel_packagelist( $kernel );

$ldap->unbind();   # take down session
$ldap->disconnect();

exit 0;

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sub usage
{
  (@_) && print STDERR "\n@_\n\n";

  print STDERR << "EOF";
 usage: $0 [-hnvW] [-c config] [-D bind_dn ] [-w bind password] [-d dump_dir] [-H hostname] <MAC>

  -h         : this (help) message
  -n         : dry run (includes verbose)
  -v         : be verbose
  -c         : LDAP config file (default: ${ldap_conf})
  -d         : dump dir (default: ${dump_dir})
  -D         : bind dn 
  -W         : prompt for password
  -w         : read password from command line
  -H <name>  : check hostname

EOF
  exit -1;
}

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
sub do_exit {
  my ($code,$msg) = @_;

  my @exit_msg = (
    0, # Ok
    0, # Usage
    0, # LDAP error
    0, # No entries found
    0, # Create file
    0, # Mkdir (5)
    0, # LDAP lookup
    0, # FAI object
    "No releases found in classlist. Releases are classes starting with ':'.",
    "Multiple releases found! Fix your classes or profiles.\n",
    0, # Hostname mismatch (10)
    0, # Release object not found
    0, # Multiple profiles
  );

  if( ! defined $msg ) {
    if( exists $exit_msg[ $code ] ) {
      $msg = $exit_msg[ $code ];
    }
  }
  else {
    if( ! exists $exit_msg[ $code ] ) {
      $msg .= "\nMissing exit ID - assign one!";
    }
    elsif( $exit_msg[ $code ] ) {
      $msg .= "\n" . $exit_msg[ $code ];
    }
  }

  print( "$msg\n" ) if( defined $msg );

  $ldap->unbind() if( defined $ldap );

  exit( -1 * $code );
}


sub create_dir
{
  if( ! -d "$_[0]" ) {
    return if( $dry_run );
    eval { 
      mkpath "$_[0]";
    };
    do_exit( 5, "Can't create dir $_[0]: $!\n" ) if( $@ );
  }
}


sub get_classes {

  # return list of FAI classes defined for host
  my $mac = shift;
  my (@classes,$mesg,$entry);
  my $host_info;
  my $real_hostname;

  print( "Lookup host for MAC '$mac'...\n" ) if( $verbose );

  my $filter = "(&(objectClass=goHard)(macAddress=$mac))";
  $mesg = $ldap->search(
    base => "$base",
    filter => $filter,
    attrs => [ 'FAIclass', 'cn', 'FAIdebianMirror', 'gosaUnitTag', 'gotoBootKernel' ]);
  $mesg->code && do_exit( 2, sprintf( "LDAP error: %s (%i)", $mesg->error, __LINE__ ) );

  # normally, only one value should be returned
  if( 1 != $mesg->count ) {
    if( 0 == $mesg->count ) {
      do_exit( 3, "LDAP search for client failed!\n"
        . "No entries have been returned.\n"
        . "  - Base:   $base\n" 
        . "  - Filter: $filter\n" );
    }
    else {
      do_exit( 3, "LDAP search for client failed!\n"
        . $mesg->count . " entries have been returned.\n"
        . "  - Base:   $base\n"
        . "  - Filter: $filter\n" );
    }
  }

  # get the entry, host DN and hostname
  $entry = ($mesg->entries)[0];
  $host_dn = $entry->dn;
  $hostname = $entry->get_value( 'cn' );
  $host_tag = $entry->get_value( 'gosaUnitTag' );
  $kernel = $entry->get_value( 'gotoBootKernel' );
  $real_hostname = $hostname;

  $faiobj->tag( $host_tag ) if( defined $host_tag );

  # set $host_base
  my @rdn = gosa_ldap_split_dn( $host_dn );
  shift( @rdn ); # hostname
  shift( @rdn ); # servers / workstations / terminals
  shift( @rdn ); # systems
  $host_base = join( ',', @rdn );

  # strip domain from LDAP hostname for FAI class
  $hostname =~ s/\..*//;

  $host_info  = "  + Host DN:            $host_dn\n"
              . "  + Base:               $host_base\n"
              . "  + Hostname:           $hostname";
  $host_info .= ' (' . $real_hostname . ')'
    if ( $hostname ne $real_hostname );
  $host_info .= "\n";

  # Check for hostname mismatch
  if( defined $check_hostname ) {
    if( $real_hostname !~ m/^${check_hostname}$/i ) {
      # Try stripped domain (non-FQDN) hostname
      do_exit( 10, "Hostname mismatch: net='$check_hostname', "
          . "LDAP='$real_hostname', non-FQDN='$hostname'" )
        if( $hostname !~ m/^${check_hostname}$/i );
    }
  }

  # check, if we have a FAIclass value, otherwise check groups
  my $fai_class_str = $entry->get_value( 'FAIclass' );
  if( (! defined $fai_class_str) || ('' eq $fai_class_str) ) {
    print( "No FAI information stored in host object - looking for host groups...\n" ) if( $verbose );

    $filter = '(&(member=' . escape_filter_value(${host_dn}) . ')(objectClass=gosaGroupOfNames)(gosaGroupObjects=[W])(objectClass=FAIobject))';
    $mesg = $ldap->search(
      base => "$base",
      filter => $faiobj->prepare_filter( $filter ),
      attrs => [ 'FAIclass', 'cn', 'FAIdebianMirror', 'gotoBootKernel' ]);
    $mesg->code && do_exit( 2, sprintf( "LDAP error: %s (%i)", $mesg->error, __LINE__ ) );

    if( 1 != $mesg->count ) {
      if( 0 == $mesg->count ) {
        do_exit( 3, "LDAP search for object groups with FAIobject containing the client failed!\n"
        . "No entries have been returned.\n"
        . "  - Base:   $base\n"
        . "  - Filter: $filter\n" );
      }
      else {
        do_exit( 3, "LDAP search for object groups with FAIobject containing the client failed!\n"
          . $mesg->count . " entries have been returned.\n"
          . "  - Base:   $base\n"
          . "  - Filter: $filter\n" );
      }
    }

    $entry = ($mesg->entries())[0];
    print( "Found FAI information in object group '" . $entry->get_value( 'cn' )  . "'\n"
          . '  + Object group:       ' . $entry->dn() . "\n" )
      if( $verbose );

    if (not defined $kernel){
      $kernel = $entry->get_value( 'gotoBootKernel' );
    }
  }

  if (not defined $kernel){
      do_exit( 3, "There is no kernel defined for this client: check the gotoBootKernel attribute!\n" );
  }

  $fai_mirror = $entry->get_value( 'FAIdebianMirror' );
 
  print( $host_info ) if $verbose;

  return $entry->get_value( 'FAIclass' );
}


sub generate_sources_list {
  my( $sections ) = @_;
  my( $mesg, $entry, $line, @deblines, @modsections, @rdns, %saw, $debline );

  # Create unique list
  undef %saw;
  @saw{@$sections} = ();
  @$sections = sort keys %saw;

  if ($verbose) {
    print "Generate template '/etc/apt/sources.list' for class 'LAST'\n"
        . " - searching server(s) for\n"
        . "   + release:  ${release}\n"
        . "   + sections: @$sections\n";
  }

  create_dir( "${dump_dir}/files/etc/apt/sources.list" );
  if( ! $dry_run ) {
    open (SOURCES,">${dump_dir}/files/etc/apt/sources.list/LAST")
      || do_exit( 4, "Can't create ${dump_dir}/files/etc/apt/sources.list/LAST. $!\n" );
  }

  if( "auto" ne "$fai_mirror" ) {
    if( ! $dry_run ) {
      print SOURCES "deb $fai_mirror $release @$sections\n";
      close (SOURCES);
    }
    print( " = Using default: $fai_mirror\n" ) if( $verbose );
    return 0;
  }

  my %release_sections = ();
  my @sec = @$sections;
  my ($search_base,@entries);
  $release_sections{ "$release" } = \@sec;

  my $fin = 0;
  
  while( 1 ) {
    # Prepare search base
    if( ! defined $search_base )
      { $search_base = $host_base; }
    else {
      my @rdn = gosa_ldap_split_dn( $search_base );
      shift( @rdn );
      $search_base = join( ',', @rdn );
    }

    print( " - using search start base: $search_base\n" ) if $verbose;

    # Look for repository servers
    ($mesg,$search_base) = gosa_ldap_rsearch( $ldap, $host_base, '',
      $faiobj->prepare_filter( '(objectClass=FAIrepositoryServer)' ),
      'one', 'ou=servers,ou=systems', [ 'FAIrepository', 'cn' ] );

    goto BAILOUT_CHECK_SERVER if( ! defined $mesg );
    $mesg->code && do_exit
      ( 2, sprintf( "LDAP error: %s (%i)", $mesg->error, __LINE__ ) );
    if( 0 == scalar $mesg->entries ) {
      next;
    }

    # Check all found servers
    print( " - found matches in base: $search_base\n" )
       if( $verbose && $mesg->count() );

    $fin = 1;
    foreach $entry ($mesg->entries) {
      print "   - inspecting repository server: " 
        . $entry->get_value('cn') . "\n" if $verbose;

      foreach my $repoline ($entry->get_value('FAIrepository')){
        my (@items) = split( '\|', ${repoline} );
        my (@modsections) = split( ',', $items[3] );

        # Check repository release
        
        if( exists $release_sections{ $items[ 2 ] } ) {

          # Check sections
          # Idea: try to remove local section from global section list.
          # If not remove, removed from local list
          # and add to 
          my $index = 0;
          foreach my $section (@modsections) {
            if( 0 == gosa_array_find_and_remove
              ( $release_sections{ $items[ 2 ] }, $section ) ) 
            {
              splice( @modsections, $index, 1 );
              if( 0 == scalar $release_sections{ $items[ 2 ] } ) {
                delete $release_sections{ $items[ 2 ] };
                last;
              }
            }
            $index++;
          }

          # Add deb-line for server, if we have local sections
          if( scalar @modsections > 0 ) {
            $debline = "deb $items[ 0 ] $items[ 2 ] " . join(' ',@modsections) . "\n";
            print "   + add: $debline" if $verbose;
            print SOURCES "$debline" if( ! $dry_run );
          }

          last if( 0 == scalar keys ( %release_sections ) );
        }
      }

      # Check, if there we still have some sections in any release
      while ( my ($key, $value) = each(%release_sections) ) {
        if (0 != scalar @$value) {
          $fin = 0;
          last;
        }
      }
      last if (1 == $fin);
    }
    last if(1 == $fin);
  }

BAILOUT_CHECK_SERVER:
  if( 0 == $fin ) {
    if( $verbose ) {
      print "Missing sections for release:\n";
      while ( my ($key, $value) = each(%release_sections) ) {
        print " + $key: @$value\n"
      }
    }
    exit -2;
  }

  close (SOURCES) if( ! $dry_run );
}


sub generate_kernel_packagelist {
  my $kernel= shift;
  my( $mesg, $entry, $line, @deblines, @modsections, @rdns, %saw, $debline );

  # Some fallback for "default" values
  if( $kernel eq "default" ) {
    $kernel= "linux-image-2.6-486";
  }

  if ($verbose) {
    print "Generate kernel package script for class 'LAST'\n"
        . " - kernel: ${kernel}\n"
  }

  create_dir( "${dump_dir}/scripts/LAST" );
  if( ! $dry_run ) {
    open (SOURCES,">${dump_dir}/scripts/LAST/99-install-kernel")
      || do_exit( 4, "Can't create ${dump_dir}/scripts/LAST/99-install-kernel. $!\n" );

    print SOURCES "#!/bin/sh\n";
    print SOURCES "# - automatically created by ldap2fai -\n";
    print SOURCES "\$ROOTCMD aptitude -o \"Aptitude::CmdLine::Ignore-Trust-Violations=yes\" -y install $kernel\n";
    close (SOURCES);
    chmod 0700, "${dump_dir}/scripts/LAST/99-install-kernel";
  }
}

# vim:ts=2:sw=2:expandtab:shiftwidth=2:syntax:paste