This file is indexed.

/usr/sbin/myproxy-replicate is in myproxy-admin 6.1.16-1.

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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
#!/usr/bin/perl
use strict;
use Getopt::Long;
##use File::Find;
use Fcntl ':flock'; # import LOCK_* constants
use File::stat;
##use Cwd;
##use Config;
use Data::Dumper;
use Time::Local;
use IPC::Open3;
require Pod::Usage;

my $MYPROXY_DEFAULT_PORT     = "7512";
my $MYPROXY_DEFAULT_LOCATION = "/var/lib/myproxy";
my $MYPROXY_REPLICATE_FILE   = "\.myproxy_replicate";
my $MYPROXY_DELETED_FILE     = "\.myproxy_deleted";
my $MYPROXY_PID_FILE         = "\.myproxy_pid";
my $SECONDS_PER_HOUR         = (60 * 60);

#
# Do a perl check for version >= 5.005.
#
if ( ! ( defined eval "require 5.005" ) )
{
    die "Requires at least Perl version 5.005";
}

my $gpath = $ENV{GLOBUS_LOCATION};

if (!defined($gpath))
{
  print "GLOBUS_LOCATION not defined in environment. Defaulting to /usr\n";
  $ENV{GLOBUS_LOCATION} = '/usr';
  $gpath = $ENV{GLOBUS_LOCATION};
}

# process the -option options
my ( $repository, $verbose, $debug, $help, $usage, $config );


GetOptions( 'storage|r=s' => \$repository,
            'config|c=s'  => \$config,
            'verbose|v'   => \$verbose,
            'debug|d'     => \$debug,
            'usage|u'     => \$usage,
            'help|h'      => \$help)
  or Pod::Usage::pod2usage(0);

Pod::Usage::pod2usage(0) if $help;
Pod::Usage::pod2usage(0) if $usage;

my $dbglvl = 0;

$dbglvl += 1 if( $verbose );
$dbglvl += 2 if( $debug );


my $globus_dir = $ENV{GLOBUS_LOCATION};

##
## Find the MyProxy Repository.  If one is not given check the default
## locations.
##
if( !defined($repository) )
{
  print "Checking for $MYPROXY_DEFAULT_LOCATION\n" if( $dbglvl > 0 );

  if( !(-d $MYPROXY_DEFAULT_LOCATION) )
  {
    print "Checking for $globus_dir$MYPROXY_DEFAULT_LOCATION\n" 
        if( $dbglvl > 0 );

    if( !(-d "$globus_dir$MYPROXY_DEFAULT_LOCATION") )
    {
      die "Could not find MyProxy repository in any of the default " .
          "locations.\nDefault: $MYPROXY_DEFAULT_LOCATION or " . 
          "\$GLOBUS_LOCATION$MYPROXY_DEFAULT_LOCATION.\n";
    }

    $repository = "$globus_dir$MYPROXY_DEFAULT_LOCATION";
  }
  else
  {
    $repository = $MYPROXY_DEFAULT_LOCATION;
  }

  print "Setting repository to $repository\n" if( $dbglvl > 1 );    
}

##
## Check to see if myproxy-store is found and executable
##
my $myproxy_store;
chomp($myproxy_store = `which myproxy-store 2>/dev/null`);
die "myproxy-store not in PATH, stopped" if (!(-x $myproxy_store));

##
## Check to see if myproxy-destroy is found and executable
##
my $myproxy_destroy;
chomp($myproxy_destroy = `which myproxy-destroy 2>/dev/null`);
die "myproxy-destroy not in PATH, stopped" if (!(-x $myproxy_destroy));

##
## Check for a server configuration file.  If one is not given, check the
## default locations.
##
if( !defined($config) )
{
  if( !(-e "/etc/myproxy-server.config") )
  {
    if( !(-e "$globus_dir/etc/myproxy-server.config") )
    {
      die "Could not find MyProxy configuration file in any of the " . 
          "default locations.\nDefault: /etc/myproxy-server.config or " .
          "\$GLOBUS_LOCATION/etc/myproxy-server.config.\n";
    }

    $config = "$globus_dir/etc/myproxy-server.config";
  }
  else
  {
    $config = "/etc/myproxy-server.config";
  }
}

print "Using server config file: $config\n" if( $dbglvl > 0 );

my $lst_rep_time;

my $new_rep_time = undef;
my @file_list;

my $retval = main();
exit( $retval );


###########################################################################
##  Do everything
###########################################################################

sub main
{

  ##
  ## Make sure no other instance of myproxy-replicate can run until
  ## current one finishes.
  ##
  open PID, ">$repository/$MYPROXY_PID_FILE";
  flock( PID, LOCK_EX );
  print PID "$$\n";

  my $rep_file = "$repository/$MYPROXY_REPLICATE_FILE";

  ##
  ## Get the timestamp of the last replication.
  ##
  if( -e $rep_file )
  {
    $lst_rep_time = get_last_replicate_time( $rep_file );
  }
  else
  {
    $lst_rep_time = 0;
  }

  $new_rep_time = timelocal(localtime);

  ##
  ## Retrieve all of the slave MyProxy servers from config file.
  ##
  my $slave_servers = get_slaves( $config );

  print "Slave Servers:\n" . Dumper $slave_servers if( $dbglvl > 1 );

  ##
  ## Read the repository and find the files that have changed since the
  ## last replication.
  ##
  my $files = read_dir( $repository, "\.creds" );  
  print Dumper $files if( $dbglvl > 1 );

  ##
  ## send the files to the slave servers.
  ##
  my $ret = replicate_files( $repository, $files, $slave_servers );

  if( $ret == 0 )
  {
    $ret = delete_files( $repository, $files, $slave_servers );
  }

  ##
  ## Check to see if we had a problem with either replicating or 
  ## deleting.  If there was a problem don't update .myproxy_replicate
  ## or .myproxy_delete.
  ##
  if( !$ret )
  {
    print "Replication complete: ", localtime() . "\n";
    finish_up( $repository );
  }
  else
  {
    print STDERR "Replication Failed\n";
  }


##sleep( 50 );
  flock( PID, LOCK_UN );
  close PID;
  unlink( "$repository/$MYPROXY_PID_FILE" );

  return( $ret );
}


###########################################################################
##  Functions
###########################################################################

##
## get_last_replicate_time( file )
##
## Read the replication timestamp.
##
sub get_last_replicate_time
{
  my $filename = shift;

  open LSTREP, $filename;
  $lst_rep_time = <LSTREP>;
  close LSTREP;

  return $lst_rep_time;
}

##
## get_slaves( file )
##
## Get the list of slave servers from the configuration file. 
##
sub get_slaves
{
  my $config = shift;

  my $slist; 
  my $junk;
  my @slave_list;

  open CFG, $config;

  for (<CFG>)
  {
    next if( !($_ =~ /^slave_servers/) );

    ($junk, $slist) = split /slave_servers/, $_;
    my @slaves = split /;/, $slist;

    for my $s (@slaves)
    {
      my ($server, $port) = split /:/, $s;

      chomp($server);
      chomp($port);

      my $ops = "-s $server ";
      $ops .= "-p $port " if( length($port) > 0 );

      push @slave_list, $ops;
    }
  }

  close CFG;

  return \@slave_list;
}

##
## read_dir( directory, expression )
##
## Read the MyProxy repository and find files that match expression. 
##
sub read_dir
{
  my $directory  = shift;
  my $expression = shift;

  my $files;

  my $stuff = undef;

  opendir(DIR, $directory) or die 
    print "ERROR: directory \"$directory\" could not be opened!\n";

  @file_list =
      map { $_->[0] } # Form a list of names without paths.
      grep { $_->[0] =~ /$expression/ } # extract the files.
      map { [ $_, "$directory/$_" ] } # form anonymous array [name, pathname] 
      # because readdir strips the path from the bname
      grep { ! /^\.\.?$/ } # remove the current directory and its parent
      readdir(DIR); #read all of the filenames in the directory

  for my $f (@file_list)
  {
    my $sb       = stat("$directory/$f");
    my $mod_time = localtime $sb->mtime;

    printf "File is %s, mtime %s\n", $f, $mod_time if( $dbglvl > 1 );

    if( $sb->mtime >= $lst_rep_time )
    {
      push @{$files}, $f;
    } 
  }

  $stuff->{'files'} = $files;
  
  closedir(DIR);

  $stuff->{'del'} = missing_files( $directory, @file_list );

  return $stuff;
}

##
## replicate_files( reposityr, files, slaves )
##
## Replicate all of the files listed to all of the slaves listed. 
##
sub replicate_files
{
  my $rep    = shift;
  my $files  = shift;
  my $slaves = shift;

  my ($exitstatus, $output);
  my $ret = 0;

  for my $f (@{$files->{'files'}})
  {
    my $data = $f;
    $data =~ s/creds/data/;

    my $options = parse_datafile( $rep, $data );

    print "File not found: $rep/$data\n  File $rep/$f not replicated.\n\n" 
        if( !defined($options) );
    next if( !defined($options) );

    $options .= "-c \"$rep/$f\" -y \"$rep/$f\" ";

    for my $s (@{$slaves})
    {
      print "OPTIONS: $myproxy_store\n$options $s\n\n" if( $dbglvl > 2 );

      ($exitstatus, $output) = runcmd( "$myproxy_store $options $s" );

      if( $exitstatus != 0 )
      {
        $ret = 1;
      }

      print STDERR "STATUS: $exitstatus\n $output\n" if( $dbglvl > 0 );
    }
    $options = undef;
  }

  return $ret;
}

##
## parse_datafile( repository, datafile )
##
## Read the data file and use the information in it to create the option
## list for replication. 
##
sub parse_datafile
{
  my $rep   = shift;
  my $fname = shift;

  my $options = undef;

  open( FN, "$rep/$fname" ) or return undef;

  for my $value (<FN>)
  {
    my( $tag, $val ) = split( /\=/, $value );
    # OWNER creds->owner_name
    # where does this come from?  It seems like it is something that is passed
    # but not flaged.  If this is true, how do we get it to replicate?
    if( $value =~ /OWNER=(.*)/ )
    {
    }
    # LIFETIME -t
    elsif( $value =~ /LIFETIME=(.*)/ )
    {
      $options .= "-t " . $1 / $SECONDS_PER_HOUR . " ";
    }
    # NAME -k creds->credname
    elsif( $value =~ /^NAME=(.*)/ )
    {
      $options .= "-k \"$1\" ";
    }
    # USERNAME -l 
    elsif( $value =~ /^USERNAME=(.*)/ )
    {
      $options .= "-l \"$1\" ";
    }
    # DESCRIPTION -K creds->creddesc
    elsif( $value =~ /DESCRIPTION=(.*)/ )
    {
      $options .= "-K \"$1\" ";
    }
    # RETRIEVERS -r creds->retrievers
    # what about anonymous retrievers?
    elsif( $value =~ /RETRIEVERS=(.*)/ )
    {
      $options .= "-x -r \"$1\" ";
    }
    # RENEWERS -R creds->renewers
    # what about anonymous renewers?
    elsif( $value =~ /RENEWERS=(.*)/ )
    {
      $options .= "-x -R \"$1\" ";
    }
    # KEYRETRIEVERS -E creds->keyretrieve
    elsif( $value =~ /KEYRETRIEVERS=(.*)/ )
    {
      $options .= "-x -E \"$1\" ";
    }
    # END_OPTIONS
    elsif( $tag eq "END_OPTIONS" )
    {
    }
  }
  close FN;

  return $options;
}

##
## missing_files( repository, files )
##
## Look at the last snapshot of the repository on the master.  Compare it
## to the current list of files in the repository.  If any are missing
## from the repository, they must have been deleted, so we need to delete
## them. 
##
sub missing_files
{
  my $rep   = shift;
  my @files = @_;

  my @delfiles;

  open FD, "$rep/$MYPROXY_DELETED_FILE" or return undef;

  my @fd = <FD>;

  for my $r (@fd)
  {
    my $fnd = 0;

    chomp($r);

    for my $f (@files)
    {
      chomp($f);
      $fnd = 1 if( $f eq $r );
    }

    if( !$fnd )
    {
      push @delfiles, $r;
    }
  }    

  return \@delfiles
}

##
## delete_files( repostiroy, files, slaves )
##
## Using the list of files that are to be deleted, send a destroy command
## to each of the listed slaves.
##
sub delete_files
{
  my $rep    = shift;
  my $files  = shift;
  my $slaves = shift;

  my ($exitstatus, $output);

  my $ret = 0;

  for my $f (@{$files->{'del'}})
  {
    my ($name, $ext) = split /\./, $f;
    my ($uname, $oname ) = split /-/, $name;

    my $options = "-l \"$uname\" ";
    $options .= "-k \"$oname\" " if( length($oname) > 0 );

    for my $s (@{$slaves})
    {
      print "OPTIONS: myproxy_destroy\n$options $s -v\n\n" if( $dbglvl > 2 );

      ($exitstatus, $output) = runcmd( "$myproxy_destroy $options $s -v" );

      if( ($exitstatus != 0) &&
          !($output =~ /No such file or directory/ ||
            $output =~ /do not exist/) )
      {
print "Bad delete\n";
        $ret = 1;
        # For now do nothing about this.  myproxy-server neeeds to be modified
        # to return more error information.  There is no way to tell why this
        # failed.
        #
        # There are several problems with the current retry scheme.  If we do
        # not update the date and directory snap shot.  The next time around
        # we are going to have problems.  Resending myproxy-destroy commands
        # to servers where the cred has already been destroied returns an
        # error message.  This will just cause an infinate cycle of failures.
        #
        # If we log the deletes and then rerun them we can run into problems
        # with missing creds.  If we have a case where a cred is stored but
        # the store fails and then before the next replicate, the user 
        # destories that cred we will have a problem with the destory.  The
        # server will have no idea what we are trying to destroy and return
        # an error.  Again, we end up in an infinate cycle of destories.
        #
        # If we log both the stores and destories we still have problems.  Is
        # we do a store and it fails and we log it, then the user destories
        # the cred before the next replicate.  We try and do a store on a 
        # cred that is not there.  This causes an error.  Then the destory
        # runs and we end up with another error.  Now we have two infinate
        # cycles going.
        #
        # We need a better solution!  I still like the idea of just taring
        # the directory and coping it over to each slave.
        #
      }
      print STDERR "STATUS: $exitstatus\n $output\n" if( $dbglvl > 0 );
    }
    $options = undef;
  }

  return $ret;
}

##
## runcmd( command )
##
## Run a MyProxy command and capture the exit value and the output
##
sub runcmd 
{
  my ($command) = @_;

  my $pid = open3(*Writer, *Reader, 0, "exec $command") ||
         die "failed to run $command";

  close(Writer);

  my @output = <Reader>;
  close(Reader);

  waitpid($pid, 0);

  my $exitstatus = $?;
  my $output = join('', @output);

  return ($exitstatus, $output);
}

sub write_timestamp
{
  my $rep = shift;

  open FD, ">$rep/$MYPROXY_REPLICATE_FILE";
  print FD $new_rep_time;
  close FD;
}

sub write_delete_file
{
  my $rep = shift;

  open FD, ">$rep/$MYPROXY_DELETED_FILE" or die
      print "ERROR: $rep/$MYPROXY_DELETED_FILE could not be opened!\n";

  for (@file_list)
  {
    print FD "$_\n";
  }

  close FD;
}

##
## findish_up( repository )
##
## Update the .myproxy_replicate with the current timesamp.  Update
## .myproxy_delete with the current snapshot of the repository.
##
sub finish_up
{
  my $rep = shift;

  open FD, ">$rep/$MYPROXY_REPLICATE_FILE";
  print FD $new_rep_time;
  close FD;

  open FD, ">$rep/$MYPROXY_DELETED_FILE" or die
      print "ERROR: $rep/$MYPROXY_DELETED_FILE could not be opened!\n";

  for (@file_list)
  {
    print FD "$_\n";
  }

  close FD;
}

__END__


=head1 NAME

B<myproxy-replicate> - Stores data from the MyProxy master repository to all
the slave servers.

=head1 SYNOPSIS

B<myproxy-replicate> [options] ...

  Options:
     [-verbose|-v]                      Print copious output
     [-help|-h]                         Print usage
     [-storage|-r]=<path to repository> Directory of the MyProxy repository.
     [-config|-c]=<path to config file> Directory of the MyProxy Server
                                        configuration file.
     [-debug|-d]                        Run in debug mode 

=head1 DESCRIPTION

B<myproxy-replicate> Replicates data. 

This utility will read a specified MyProxy repository and send any new or
changed data to a slave MyProxy server.  The slave servers are specified 
in the B<myproxy-server.config(5)> file.  

This utility will need to run at some specified interval in order to keep
the slave repositories semi current with the Master repository.  This can
best be accomplished using cron, or some similar mechanism.

=head1 OPTIONS

=over 8

=item B<-v>, B<-verbose>

Enables verbose debugging output to the terminal.

=item B<-h>, B<-help>

Displays command usage text and exits.

=item B<-u>, B<-usage>

Displays command usage text and exits.

=item B<-r> I<dir>, B<-storage> I<dir>

Specifies the location of the credential storage directory.
The directory must be accessible only by the user running the
B<myproxy-server> process for security reasons.  
Default: /var/lib/myproxy or $GLOBUS_LOCATION/var/myproxy

=item B<-c> I<file>, B<-config> I<file>

Specifies the location of the myproxy-server configuration file.
Default: /etc/myproxy-server.config or 
         $GLOBUS_LOCA-TION/etc/myproxy-server.config

=back

=head1 SEE ALSO

myproxy-init(1) myproxy-store(1) myproxy-retrieve(1) myproxy-delegate(1)
myproxy-server(8) myproxy-server.config(5)

=head1 AUTHOR

=cut