This file is indexed.

/usr/share/arc/InfosysHelper.pm is in nordugrid-arc-arex 5.0.5-1ubuntu1.

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
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
package InfosysHelper;

# Helper functions to be used for communication between the A-REX infoprovider and ldap-infosys
#
#  * for A-REX infoprovider:
#      - createLdifScript: creates a script that prints the ldif from the infoprovider when executed,
#      - notifyInfosys: notifies ldap-infosys through a fifo file created by ldap-infosys.
#  * for ldap-infosys:
#      - waitForProvider: waits for A-REX infoprovider to give a life sign on the fifo it created
#      - ldifIsReady: calls waitForProvider and checks that ldif is fresh enough

use POSIX;
use Fcntl;
use English;
use File::Basename;
use File::Temp qw(tempfile tempdir);
use File::Path qw(mkpath);
#use Data::Dumper::Concise;
## usage:
# print Dumper($datastructure); 

use LogUtils;

our $log = LogUtils->getLogger(__PACKAGE__);

LogUtils::level("VERBOSE");

#
# Given a pid file, returns the user id of the running process
#
sub uidFromPidfile {
    my ($pidfile) = @_;
    open(my $fh, "<", "$pidfile") or return undef;
    my @stat = stat $pidfile;
    my $pid = <$fh>;
    close $fh;
    $pid =~ m/^\s*(\d+)\s*$/ or return undef;
    my $uid = `ps -ouid= $pid`;
    close $fh;
    $uid =~ m/^\s*(\d+)\s*$/ or return undef;
    return $1;
}

#
# stat the file, get the uid, pid
#
sub uidGidFromFile {
    my ($file) = @_;
    my @stat = stat $file;
    return () unless @stat;
    return (@stat[4,5]);
}

#
# switch effective user if possible. This is reversible.
# It switches back to root if the passed parameter
# is 0.
#
sub switchEffectiveUser {
    my ($uid) = @_;
    if ($UID == 0 && $uid != 0) {
        my ($name, $pass, $uid, $gid) = getpwuid($uid);
        return unless defined $gid;
        eval { $EGID = $gid;
               $EUID = $uid;
        };
    # Switch back to original UID/GID
	} else {
		eval { $EGID = $GID;
               $EUID = $UID;
        };
    };
}

#
# Waits for a sign from the infoprovider. Implemented using a fifo file.
#  * creates a fifo (unlinks it first if it already exists)
#  * opens the fifo -- this blocks until the other end opens the fifo for writing
#  * returns false in case of error
#
sub waitForProvider {
    my ($runtime_dir) = @_;
 
    my $fifopath = "$runtime_dir/ldif-provider.fifo";

    if (! -d $runtime_dir) {
        $log->warning("No such directory: $runtime_dir");
        return undef;
    }

    if (-e $fifopath) {
        $log->info("Unlinking stale fifo file: $fifopath");
        unlink $fifopath;
    }

    unless (POSIX::mkfifo $fifopath, 0600) {
        $log->warning("Mkfifo failed: $fifopath: $!");
        return undef;
    }
    $log->verbose("New fifo created: $fifopath");
 
    my $handle;

    # This might be a long wait. In case somebody kills us, be nice and clean up.
    $log->info("Start waiting for notification from A-REX's infoprovider");
    my $ret;
    eval {
        local $SIG{TERM} = sub { die "terminated\n" };
        unless ($ret = sysopen($handle, $fifopath, O_RDONLY)) {
            $log->warning("Failed to open: $fifopath: $!");
            unlink $fifopath;
        } else {
            while(<$handle>){}; # not interested in contents
        }
    };
    close $handle;
    unlink $fifopath;
    if ($@) {
        $log->error("Unexpected: $@") unless $@ eq "terminated\n";

        $log->warning("SIGTERM caught while waiting for notification from A-REX's infoprovider");
        return undef;
    }
    return undef unless $ret;

    $log->info("Notification received from A-REX's infoprovider");
    return 1;
}

{   my $cache = undef;

    #
    # Finds infosys' runtime directory and the infosys user's uid, gid
    # TODO: this is a bit complicated and due to BDII4/BDII5.
    # Maybe it needs simplification, but requires understanding
    # of what happens in BDII5 since they changed directory paths.
    #
    sub findInfosys {
        return @$cache if defined $cache;
        my ($config) = @_;
        my ($bdii_run_dir) = $config->{bdii_run_dir} || "/var/run/arc/bdii";
        # remove trailing slashes
        $bdii_run_dir =~ s|/\z||;
        $log->debug("BDII run dir set to: $bdii_run_dir");
        
        # TODO: remove this legacy BDII4 location from here and from grid-infosys
        my ($bdii_var_dir) = $config->{bdii_var_dir} || "/var/lib/arc/bdii";
        # remove trailing slashes
        $bdii_var_dir =~ s|/\z||;
        $log->debug("BDII var dir set to: $bdii_var_dir");
        
        my ($bdii_update_pid_file) = $config->{bdii_update_pid_file} || "$bdii_run_dir/bdii-update.pid";
        $log->debug("BDII pid guessed location: $bdii_update_pid_file. Will search for it later");
        
        my ($infosys_uid, $infosys_gid);
        
        my $infosys_ldap_run_dir = $config->{infosys_ldap_run_dir} || "/var/run/arc/infosys";
        # remove trailing slashes
        $infosys_ldap_run_dir =~ s|/\z||;
        $log->debug("LDAP subsystem run dir set to $infosys_ldap_run_dir");
        
        # search for bdii pid file: legacy bdii4 locations still here
        # TODO: remove bdii_var_dir from everywhere (also from grid-infosys)
        # if not specified with bdii_update_pid_file, it's likely here
        my $existsPidFile = 0;
        my $bdii5_pidfile = "$bdii_run_dir/bdii-update.pid";
        my $bdii4_pidfile = "$bdii_var_dir/bdii-update.pid";
        for my $pidfile ( $bdii_update_pid_file, $bdii5_pidfile, $bdii4_pidfile) {
            unless ( ($infosys_uid, $infosys_gid) = uidGidFromFile($pidfile) ) {
                $log->verbose("BDII pidfile not found at: $pidfile");
                next;
            }
            $existsPidFile = 1;
            $log->verbose("BDII pidfile found at: $pidfile");
            next unless (my $user = getpwuid($infosys_uid));
            $log->verbose("BDII pidfile owned by: $user ($infosys_uid)");
            last;
        }
        unless ($existsPidFile) {
            $log->warning("BDII pid file not found. Check that nordugrid-arc-bdii is running, or that bdii_run_dir is set");
            return @$cache = ();
        }
        unless (-d $infosys_ldap_run_dir) {
            $log->warning("LDAP information system runtime directory does not exist. Check that:\n \t *) The arc.conf parameter infosys_ldap_run_dir is correctly set im manually added. \n \t *) nordugrid-arc-bdii is running");
            return @$cache = ();
        }
        return @$cache = ($infosys_ldap_run_dir, $infosys_uid, $infosys_gid);
    }
}

#
# 
# Notify Infosys that there is a new fresh ldif. Implemented using a fifo file.
#  * finds out whether there is a reader on the other end if the fifo
#  * opens the file and then closes it (thus waking up the listener on other end)
#  * returns false on error
#
sub notifyInfosys {
    my ($config) = @_;

    my ($infosys_ldap_run_dir) = findInfosys($config);
    return undef unless $infosys_ldap_run_dir;

    my $fifopath = "$infosys_ldap_run_dir/ldif-provider.fifo";

    unless (-e $fifopath) {
        $log->info("LDAP subsystem has not yet created fifo file $fifopath");
        return undef;
    }

    my $handle;
 
    # Open the fifo -- Normally it should't block since the other end is
    # supposed to be listening. If it blocks nevertheless, something must have
    # happened to the reader and it's not worth waiting here. Set an alarm and
    # get out.
    my $ret;
    eval {
        local $SIG{ALRM} = sub { die "alarm\n" };
        alarm 5;
        unless ($ret = sysopen($handle, $fifopath, O_WRONLY)) {
            $log->warning("Failed to open fifo (as user id $EUID): $fifopath: $!");
        }
        alarm 0;
    };
    if ($@) {
        $log->error("Unexpected: $@") unless $@ eq "alarm\n";
 
        # timed out -- no reader
        $log->warning("Fifo file exists but LDAP information system is not listening");
        return undef;
    }
    return undef unless $ret;
    close $handle;

    $log->info("LDAP information system notified on fifo: $fifopath");
    return $handle;
}

#
# To be called by the A-REX infoprovider
#  * Takes the ldif generated by calling &$print_ldif and creates an executable
#    script that when executed, outputs that ldif.
#  * If applicable: switches user to that running infosys and then switches back to root
#  * Returns false on error
#
sub createLdifScript {
    my ($config, $print_ldif) = @_;

    my ($infosys_ldap_run_dir, $infosys_uid, $infosys_gid) = findInfosys($config);
    return undef unless $infosys_ldap_run_dir;

    eval { mkpath($infosys_ldap_run_dir); };
    if ($@) {
        $log->warning("Failed creating parent directory $infosys_ldap_run_dir: $@");
        return undef;
    }
    unless (chown $infosys_uid, $infosys_gid, $infosys_ldap_run_dir) {
        $log->warning("Chown to uid($infosys_uid) gid($infosys_gid) failed on: $infosys_ldap_run_dir: $!");
        return undef;
    }

    switchEffectiveUser($infosys_uid);

    my ($h, $tmpscript);
    eval {
        my $template = "ldif-provider.sh.XXXXXXX";
        ($h, $tmpscript) = tempfile($template, DIR => $infosys_ldap_run_dir);
    };
    if ($@) {
        $log->warning("Failed to create temporary file: $@");
        switchEffectiveUser($UID);
        return undef;
    }

    # Hopefully this string is not in the ldif
    my $mark=substr rand(), 2;

    eval {
        local $SIG{TERM} = sub { die "terminated\n" };
        die "file\n" unless print $h "#!/bin/sh\n\n";
        die "file\n" unless print $h "# Autogenerated by A-REX's infoprovider\n\n";
        die "file\n" unless print $h "cat <<'EOF_$mark'\n";
        &$print_ldif($h);
        die "file\n" unless print $h "\nEOF_$mark\n";
        die "file\n" unless close $h;
    };
    if ($@) {
        my $msg = "An error occured while creating ldif generator script: $@";
        $msg = "An error occured while writing to: $tmpscript: $!" if $@ eq "file\n";
        $msg = "SIGTERM caught while creating ldif generator script" if $@ eq "terminated\n";
        close $h;
        unlink $tmpscript;
        $log->warning($msg);
        $log->verbose("Removing temporary ldif generator script");
        switchEffectiveUser($UID);
        return undef;
    }

    unless (chmod 0700, $tmpscript) {
        $log->warning("Chmod failed: $tmpscript: $!");
        unlink $tmpscript;
        switchEffectiveUser($UID);
        return undef;
    }

    my $finalscript = "$infosys_ldap_run_dir/ldif-provider.sh";

    unless (rename $tmpscript, $finalscript) {
        $log->warning("Failed renaming temporary script to $finalscript: $!");
        unlink $tmpscript;
        switchEffectiveUser($UID);
        return undef;
    }
    $log->verbose("Ldif generator script created: $finalscript");

    switchEffectiveUser($UID);
    return 1;
}

#
# To be called by ldap-infosys
#  * returns true if/when there is a fresh ldif
#
sub ldifIsReady {
    my ($infosys_ldap_run_dir, $max_age) = @_;

    LogUtils::timestamps(1);

    # Check if ldif generator script exists and is fresh enough
    my $scriptpath = "$infosys_ldap_run_dir/ldif-provider.sh";
    unless (-e $scriptpath) {
        $log->info("The ldif generator script was not found ($scriptpath)");
        $log->info("This file should have been created by A-REX's infoprovider. Check that A-REX is running.");
        return undef;
    }
    my @stat = stat $scriptpath;
    $log->error("Cant't stat $scriptpath: $!") unless @stat;
    if (time() - $stat[9] > $max_age) {
        $log->info("The ldif generator script is too old ($scriptpath)");
        $log->info("This file should have been refreshed by A-REX's infoprovider. Check that A-REX is running.");
        return undef;
    }

    # A-REX has started up... Wait for the next infoprovider cycle
    waitForProvider($infosys_ldap_run_dir)
        or $log->warning("Failed to receive notification from A-REX's infoprovider");

    $log->verbose("Using ldif generator script: $scriptpath");
    return 1;
}

#
# infosys heartbeat simply implemented using a file
# input: config datastructure
#
sub heartbeatBeat {
    my ($config) = @_;
    # complicated to use run dir. Reverted to controldir
    # TODO: with better configuration settings, there should be only
    # one run dir.
    #my ($infosys_ldap_run_dir) = findInfosys($config);
    #my $heartbeatFileName = $infosys_ldap_run_dir.'/infosys_heartbeat';
    my $controldir = $config->{'control'}->{'.'}->{'controldir'};
    my $heartbeatFileName = $controldir.'/infosys_heartbeat';
    
    # set drift to 20 seconds before timeout
    my $infoproviders_timeout_drift = 60;
    #my $heartbeatFileName = '/tmp/infosys_heartbeat';
    
    # check if heartbeat file exists, if not create it
    my $mtime = (stat($heartbeatFileName))[9];
    if ( !( defined $mtime ) ) {
		$log->debug("Creating heartbeat file $heartbeatFileName");
        local (*HB);
        open (HB, ">>$heartbeatFileName") || $log->error("Failed creating heartbeat file $heartbeatFileName. Check if controldir is writable");
        close (HB);
    } else {
        # touch if file exists, every minute.
	    my $now = time;
	    my $drift = $now - $mtime;
	    $log->debug("Heartbeat file mtime: $mtime now: $now drift: $drift infoprov_timeout_drift: $infoproviders_timeout_drift");
	    if ($drift > $infoproviders_timeout_drift) {
			$log->debug("Touching heartbeat file: last touch more than 60 sec.");
			unless (utime $now, $now, $heartbeatFileName) { $log->error("Failed touching heartbeat file $heartbeatFileName. Check if controldir is writable"); };
			$log->verbose("Touching heartbeat file $heartbeatFileName successful");
		} else {
	        $log->debug("Skipping heartbeat: less than 60 sec since last touch.");     
	    }
	}
}

#
# Delete the heartbeat file.
# input: configuration datastructure
#
sub heartbeatRemove {
    my ($config) = @_;
    # see comment above on controldir choice.
    my $controldir = $config->{'control'}->{'.'}->{'controldir'};
    my $heartbeatFileName = $controldir.'/infosys_heartbeat';
    
	unlink $heartbeatFileName or $log->warning("Could not unlink $heartbeatFileName: $!");
	$log->verbose("Removing hearbeat file $heartbeatFileName");
}

#
# Parametrized versions of heartbeat that expects controldir and timeout.
# To use in infoproviders to avoid passing the whole config
# input: controldir path
#
sub heartbeatBeatParam {
	my ($controldir) = @_;
	my $fakeconfig = {};
	$fakeconfig->{'control'}->{'.'}->{'controldir'} = $controldir;
	heartbeatBeat($fakeconfig);
}

sub heartbeatRemoveParam {
	my ($controldir) = @_;
	my $fakeconfig = {};
	$fakeconfig->{'control'}->{'.'}->{'controldir'} = $controldir;
	heartbeatRemove($fakeconfig);
}
	
1;