This file is indexed.

/usr/share/perl5/Rex/Cron/Base.pm is in rex 1.4.1-1.

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
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
#
# (c) Jan Gehring <jan.gehring@gmail.com>
#
# vim: set ts=2 sw=2 tw=0:
# vim: set expandtab:

package Rex::Cron::Base;

use strict;
use warnings;

our $VERSION = '1.4.1'; # VERSION

use Rex::Logger;
use Rex::Commands;
use Rex::Commands::File;
use Rex::Commands::Fs;
use Rex::Commands::Run;
use Rex::Helper::Run;
use Data::Dumper;
use Rex::Helper::Path;

sub new {
  my $that  = shift;
  my $proto = ref($that) || $that;
  my $self  = {@_};

  bless( $self, $proto );

  return $self;
}

sub list {
  my ($self) = @_;
  return @{ $self->{cron} };
}

sub list_jobs {
  my ($self) = @_;
  my @jobs = @{ $self->{cron} };
  my @ret =
    map { { line => $_->{line}, %{ $_->{cron} } } }
    grep { $_->{type} eq "job" } @jobs;
}

sub list_envs {
  my ($self) = @_;
  my @jobs = @{ $self->{cron} };
  my @ret = grep { $_->{type} eq "env" } @jobs;
}

sub add {
  my ( $self, %config ) = @_;

  %config = $self->_create_defaults(%config);

  my $new_cron = sprintf(
    "%s %s %s %s %s %s",
    $config{"minute"}, $config{"hour"},        $config{"day_of_month"},
    $config{"month"},  $config{"day_of_week"}, $config{"command"},
  );

  my $dupe = grep { $_->{line} eq $new_cron } @{ $self->{cron} };
  if ($dupe) {
    Rex::Logger::debug("Job \"$new_cron\" already installed, skipping.");
    return 0;
  }

  push(
    @{ $self->{cron} },
    {
      type => "job",
      line => $new_cron,
      cron => \%config,
    }
  );

  return 1;
}

sub add_env {
  my ( $self, $name, $value ) = @_;

  my $env_index = 0;
  my $exists    = 0;
  for my $env ( $self->list_envs ) {
    if ( $env->{name} eq "$name" ) {
      if ( $env->{value} ne "\"$value\"" ) {
        Rex::Logger::debug("Environment variable changed : $name");
        $self->delete_env($env_index);
      }
      else {
        Rex::Logger::debug(
          "Environment variable already exists with same value: $name=$value");
        $exists = 1;
      }
    }

    $env_index++;
  }

  if ( $exists == 0 ) {
    unshift(
      @{ $self->{cron} },
      {
        type  => "env",
        line  => "$name=\"$value\"",
        name  => $name,
        value => $value,
      }
    );
  }
}

sub delete_job {
  my ( $self, $num ) = @_;
  my @jobs = $self->list_jobs;

  my $i = 0;
  my $to_delete;
  for my $j ( @{ $self->{cron} } ) {
    if ( $j->{line} eq $jobs[$num]->{line} ) {
      $to_delete = $i;
      last;
    }

    $i++;
  }

  unless ( defined $to_delete ) {
    die("Cron Entry $num not found.");
  }

  $self->delete($to_delete);
}

sub delete_env {
  my ( $self, $num ) = @_;

  my @jobs = $self->list_envs;

  my $i = 0;
  my $to_delete;
  for my $j ( @{ $self->{cron} } ) {
    if ( $j->{line} eq $jobs[$num]->{line} ) {
      $to_delete = $i;
      last;
    }

    $i++;
  }

  unless ( defined $to_delete ) {
    die("Cron Entry $num not found.");
  }

  $self->delete($to_delete);
}

sub delete {
  my ( $self, $num ) = @_;
  splice( @{ $self->{cron} }, $num, 1 );
}

# returns a filename where the new cron is written to
# after that the cronfile must be activated
sub write_cron {
  my ($self) = @_;

  my $rnd_file = get_tmp_file;

  my @lines = map { $_->{line} } @{ $self->{cron} };

  my $fh = file_write $rnd_file;
  $fh->write( join( "\n", @lines ) . "\n" );
  $fh->close;

  return $rnd_file;
}

sub activate_user_cron {
  my ( $self, $file, $user ) = @_;
  $user = undef if $user eq &_whoami;

  my $command = 'crontab';
  $command .= " -u $user" if defined $user;

  i_run "$command $file";
  unlink $file;
}

sub read_user_cron {
  my ( $self, $user ) = @_;
  $user = undef if $user eq &_whoami;

  my $command = 'crontab -l';
  $command .= " -u $user" if defined $user;
  $command .= ' 2> /dev/null';

  my @lines = i_run $command;
  $self->parse_cron(@lines);
}

sub parse_cron {
  my ( $self, @lines ) = @_;

  chomp @lines;

  my @cron;

  for my $line (@lines) {

    # comment
    if ( $line =~ m/^#/ ) {
      push(
        @cron,
        {
          type => "comment",
          line => $line,
        }
      );
    }

    # empty line
    elsif ( $line =~ m/^\s*$/ ) {
      push(
        @cron,
        {
          type => "empty",
          line => $line,
        }
      );
    }

    # job
    elsif ( $line =~ m/^(@|\*|[0-9])/ ) {
      my ( $min, $hour, $day, $month, $dow, $cmd ) = split( /\s+/, $line, 6 );
      push(
        @cron,
        {
          type => "job",
          line => $line,
          cron => {
            minute       => $min,
            hour         => $hour,
            day_of_month => $day,
            month        => $month,
            day_of_week  => $dow,
            command      => $cmd,
          },
        }
      );
    }

    elsif ( $line =~ m/=/ ) {
      my ( $name, $value ) = split( /=/, $line, 2 );
      $name =~ s/^\s+//;
      $name =~ s/\s+$//;
      $value =~ s/^\s+//;
      $value =~ s/\s+$//;

      push(
        @cron,
        {
          type  => "env",
          line  => $line,
          name  => $name,
          value => $value,
        }
      );
    }

    else {
      Rex::Logger::debug("Error parsing cron line: $line");
      next;
    }

  }

  $self->{cron} = \@cron;
  return @cron;
}

sub _create_defaults {
  my ( $self, %config ) = @_;

  $config{"minute"}       = "*" unless defined $config{minute};
  $config{"hour"}         = "*" unless defined $config{hour};
  $config{"day_of_month"} = "*" unless defined $config{day_of_month};
  $config{"month"}        = "*" unless defined $config{month};
  $config{"day_of_week"}  = "*" unless defined $config{day_of_week};
  $config{"command"} ||= "false";

  return %config;
}

sub _whoami {
  return i_run q(perl -e 'print scalar getpwuid($<)');
}

1;