6 use Data::Dump qw(dump);
10 open(STDERR, '>>', '/var/log/cups/find_owner_log');
12 my ($file, $local_user, $remote_user) = @ARGV;
14 my $job_id = $1 if ( $file =~ m/job_(\d+)/ );
16 die "can't find job_id in [$file]" unless $job_id;
18 my $c_file = sprintf "/var/spool/cups/c%05d", $job_id;
20 my $blob = read_file $c_file;
22 my (undef,$ip) = split(/job-originating-host-name\x00/, $blob, 2);
23 my $len = ord(substr($ip,0,1));
24 $ip = substr($ip,1,$len);
26 my $database = 'pGinaDB';
27 my $hostname = '10.60.4.9';
30 my $password = 'secret';
32 my $dsn = "DBI:mysql:database=$database;host=$hostname;port=$port";
33 my $dbh = DBI->connect($dsn, $user, $password);
35 my $sth = $dbh->prepare(qq{
36 select * from pGinaSession where ipaddress = ? and logoutstamp is null order by loginstamp desc
37 }) or die "prepare statement failed: $dbh->errstr()";
38 $sth->execute($ip) or die "execution failed: $dbh->errstr()";
39 if ( $sth->rows < 1 ) {
40 die "can't find IP for job $job_id";
41 } elsif ( $sth->rows > 1 ) {
42 warn "ERROR: found $sth->rows() rows for $job_id, usng first one\n";
44 my $row = $sth->fetchrow_hashref();
45 warn "## row = ",dump($row);
49 my $username = $row->{username} || die "no username in row = ",dump($row);
50 $username =~ s/\@ffzg.hr$//; # strip domain, same as pGina
52 my $spool = '/var/spool/cups-pdf/';
53 mkdir "$spool/$username" if ( ! -e "$spool/$username" );
54 my $filename_only = $file;
55 $filename_only =~ s/^.*\///; # basename
57 my $to = "$spool/$username/$filename_only";