This file is indexed.

/usr/lib/cups/filter/hpcac is in hplip 3.12.6-3.1+deb7u1.

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
#!/usr/bin/perl  -W
use strict;
use File::Temp qw(tempfile);
use File::Basename;
use Sys::Hostname;

#######################################################
#
###################################################### 

my $arg_job                 = $ARGV[0];
my $arg_user                = $ARGV[1];
my $arg_title               = $ARGV[2];
my $arg_copies              = $ARGV[3];
my $arg_options             = $ARGV[4];

my $progname = basename( $0 );

# Set to 1 to capture the Postscript that you generate to a temp file
my $capture_postscript = 0;

# Set to 1 to print debug messages to CUPS error log
my $debug = 0;

my $capture_fh = undef;
my $capture_filename = undef;
my $procfile;

#----------------------------------------------------

###################################################### 
# Various debug & error subs
# Error and debugging messages have to go to STDERR
###################################################### 
sub abort_exit
{
    my $mesg = shift;
    print STDERR "ERROR: $progname - $mesg\n";
    exit( 1 );
}

sub print_debug
{
    return if( ! $debug );
    my $output = shift;
    print STDERR "DEBUG: $progname [PID $$] $output\n";
}

sub print_info
{
    my $output = shift;
    print STDERR "INFO: $progname [PID $$] $output\n";
}

sub print_warning
{
    my $output = shift;
    print STDERR "WARN: $progname [PID $$] $output\n";
}

###################################################################
# Open a temp file to capture the Postscript that I send to the printer.
# Used for debugging
###################################################################
sub open_capture
{
    if( $capture_postscript ) {
	( $capture_fh, $capture_filename ) =
	    tempfile( "${progname}-$$-capture-XXXXXX", DIR => "/tmp", SUFFIX => ".ps", UNLINK => 0 );
	if ( !defined $capture_fh ) {
	    # Not a fatal issue
	    print_warning( "Could not open capture file.  Will not save Postscript output." );
	    $capture_filename = undef;
	    $capture_postscript = 0;
	}
	else {
	    print_info( "Capturing Postscript to '$capture_filename'" );
	}
    }
}

###################################################################
# Close the debugging capture log
###################################################################
sub close_capture
{
    if( $capture_postscript && defined $capture_fh ) {
	close( $capture_fh ) or print_warning( "Could not close capture file '$capture_filename': $!" );
    }
}

###################################################################
# Print out the given line to STDOUT (print data stream)
# and optionally capture it for debugging.
###################################################################
sub emit_line
{
    my $line = shift;
    print $line;

    if( $capture_postscript && $capture_fh ) {
	print $capture_fh $line;
    }
}

###################################################################
# Get the type of accounting that is used for the printer
# by looking at the PPD for specific values.
# Currently only support HPAccountingInfo: 1
###################################################################
sub GetHPAccountingType
{
    my $ppd = $ENV{"PPD"};
    if( (! $ppd ) || (! -r $ppd ) ) {
	return( "NONE", "NONE" );
    }
    
    open( PPDFILE, "$ppd" ) or 	return( "NONE", "NONE" );

    my $accounting_type = "NONE";
    my $accounting_mode = "NONE";
    my $ppdline;
    while( <PPDFILE> ) {
	$ppdline = $_;
	if( $ppdline =~ /^\*HPAccountingInfo:\s*(\d*)\s*$/ ) {
	    $accounting_type = "HPAccountingInfo";
	    $accounting_mode = $1;
	    print_debug("HPAccountingInfo found $accounting_mode");
	}
    }
    close( PPDFILE );

    return( $accounting_type, $accounting_mode );
}

###################################################################
# The Postscript code containing the accounting info
# has starting and ending snippets.
###################################################################
sub InsertFeatureStart
{
    emit_line( "[{\n" );
}

sub InsertFeatureEnd
{
    emit_line( "} stopped cleartomark\n" );
}

###################################################################
sub GetUser
{
    my $user;
    if ( $arg_user ) {
	$user = $arg_user;
    }
    else {
	$user = "nobody";
    }

    return( $user );
}

###################################################################
sub GetJob { }


###################################################################
sub GetTitle { }

###################################################################
sub GetOptions
{ 
    my %opt_hash = ();
    my $options = $arg_options;
    my @opt_array = split( ' ', $options );
    foreach my $option ( @opt_array ) {
	my( $name, $value ) = split( /=/, $option, 2 );
	if ( !$value ) {
	    $value = "";
	}
	print_debug( "Option $name='$value'" );
	$opt_hash{$name} = $value;
    }
    return( \%opt_hash );
}

###################################################################
# If a UUID wasn't given to us by CUPS, try a few methods
# to create one ourselves.
###################################################################
sub CreateUUID
{
    my $uuid_str = "";
    eval {
	require Data::UUID;
	my $ug = new Data::UUID;
	$uuid_str = $ug->create_str;
    };
    if ($@) {
	$uuid_str = `uuidgen`;
	$uuid_str =~ s/\r?\n?$//;
    }

    return( $uuid_str );
}

###################################################################
# CUPS passes in an option value that contains a UUID.
# If it's there, use that and if not, try to create one ourselves
###################################################################
sub GetUUID
{
    my $uuid = "";
    my $r_opt_hash = GetOptions();
    # job-uuid=urn:uuid:018c1dab-3c0c-3edf-6036-1e87af479038

    my $job_uuid = $$r_opt_hash{ "job-uuid" };
    if ( $job_uuid ) {
	if ( $job_uuid =~ /urn:uuid:(.*)/ ) {
	    $uuid = $1;
	    print_debug( "UUID in job-uuid=$uuid" );
	}
	else {
	    print_debug( "bad job-uuid" );
	    $uuid = CreateUUID();
	}
    }
    else {
	$uuid = CreateUUID();
    }

    if ( ! $uuid ) {
	print_debug( "EPIC FAIL: Could not find nor generate UUID" );
    }
    
    return( $uuid );
}

###################################################################
# Get the system name
###################################################################
sub GetSystem
{
    my $system_name = hostname;
    if( !$system_name ) {
	$system_name = "unknown-system_name";
    }

    return( $system_name );

}

###################################################################
# The domain value is really a Windows domain.  Since we're
# not using this in the Windows environment, we just
# use the hostname. The value is not used for Color Access Control
# filtering but it still has to be included in the job accounting info
###################################################################
sub GetDomain
{
    my $domain = hostname;
    if( !$domain ) {
	$domain = "unknown-domain";
    }

    return( $domain );
}

###################################################################
# Gather up all of the information needed for the accounting data
# to be included in the print stream
###################################################################
sub GetHPAccountingInfo
{
    my( $jobname, $user, $system, $domain, $date, $uuid, $app, $appexe, $dept );

    $jobname = $arg_job;
    $user = GetUser();
    $system = GetSystem();
    $domain = GetDomain();
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
    $year += 1900;
    $mon += 1;
    # YYYYMMDDhhmmss
    $date = sprintf( "%s%02d%02d%02d%02d%02d", $year, $mon, $mday, $hour, $min, $sec);
    $uuid = GetUUID(); 

    # There is no way to get the name of the application that generated this PS file.
    # Set it to this as a fallback.
    $app = "HP Linux Printing";
    $appexe = "HP Linux Printing";

    $dept = $user;

    return( $jobname, $user, $system, $domain, $date, $uuid, $app, $appexe, $dept );
}

###################################################################
# Once the accounting data has been gathered, format it and
# include it in the print stream.
###################################################################
sub InsertHPAccountingInfo
{

    my ( $accounting_type, $accounting_mode ) = GetHPAccountingType();
    print_debug( "Accounting Type=$accounting_type\tAccounting Mode=$accounting_mode");

    if( ! ( ($accounting_type eq "HPAccountingInfo") && ($accounting_mode == 1)) ) {
	print_debug( "Color Access Control not in effect." );
	return;
    }

    InsertFeatureStart();
    my( $jobname, $user, $system, $domain, $date, $uuid, $app, $appexe, $dept ) = GetHPAccountingInfo();

    my $accounting_feature = "";
    $accounting_feature = "%%BeginFeature: *HPAccountingInfo\n" .
	"    currentpagedevice /StringCodeSet known\n" .
	"    {\n" .
	"        << /StringCodeSet (UTF8) >> setpagedevice\n" .
	"        <<\n" .
	"            /JobName ($jobname)\n" .
	"            /JobAcct1 ($user)\n" .
	"            /JobAcct2 ($system)\n" .
	"            /JobAcct3 ($domain)\n" .
	"            /JobAcct4 ($date)\n" .
	"            /JobAcct5 ($uuid)\n" .
	"            /JobAcct6 ($app)\n" .
	"            /JobAcct7 ($appexe)\n" .
	"            /JobAcct8 ($dept)\n" .
	"        >> setuserparams\n" .
	"    }\n" .
	"    {\n" .
	"        <<\n" .
	"            /JobName ($jobname)\n" .
	"            /JobAcct1 ($user)\n" .
	"            /JobAcct2 ($system)\n" .
	"            /JobAcct3 ($domain)\n" .
	"            /JobAcct4 ($date)\n" .
	"            /JobAcct5 ($uuid)\n" .
	"            /JobAcct6 ($app)\n" .
	"            /JobAcct7 ($appexe)\n" .
	"            /JobAcct8 ($dept)\n" .
	"        >> setuserparams\n" .
	"    } ifelse\n" .
	"%%EndFeature\n";

    emit_line( $accounting_feature );
    InsertFeatureEnd();
}

###################################################################
# If the print job is from stdin, save a copy
###################################################################
sub stdin2file
{
    my ( $tmp_fh, $tmp_filename ) =
	tempfile( "${progname}-$$-stdin-XXXXXX", SUFFIX => ".ps", DIR => "/tmp", UNLINK => 0 );
    if ( !defined $tmp_fh ) {
	abort_exit( "Cannot create tempfile '$tmp_filename'" );
    }

    print_debug( "Copying STDIN to $tmp_filename" );
    while (<STDIN>)
    {
	print $tmp_fh $_;
    }
    close $tmp_fh or abort_exit( "Cannot close tempfile '$tmp_filename': $!" );
    return( $tmp_filename );
}

###################################################################
# If the print job is contained in a file, make a copy.
# A copy is made because the original file could be deleted
# before this print job is completed.
###################################################################
sub copy_file
{
        my $from = shift;

        open (FROM, "<$from" ) or abort_exit( "Cannot read '$from': $!" );
	my ( $tmp_fh, $tmp_filename ) =
	    tempfile( "${progname}-$$-copy-XXXXXX", SUFFIX => ".ps", DIR => "/tmp", UNLINK => 0 );
	if ( !defined $tmp_fh ) {
	    abort_exit( "Couldn't create temporary file '$tmp_filename'" );
	}

	print_debug( "Copying $from to $tmp_filename" );
        while (<FROM>) {
	    print $tmp_fh $_ or abort_exit( "Can't write to tempfile '$tmp_filename': $!" );
	}
        close $tmp_fh or abort_exit( "Cannot close tempfile '$tmp_filename': $!" );
        close FROM;
        return $tmp_filename;
}

###################################################################
# Given the print job, look through it for the right place to
# insert the accounting info.  The document is assumed to be in
# the Adobe DSC format already and should contain the BeginProlog
# item.  Upstream CUPS filters will have converted the Postscript
# data into DSC format.
###################################################################
sub processfile
{
        my $fn = shift;
        open (PSFILE, "<$fn") or abort_exit( "Cannot open '$fn': $!");
        while (<PSFILE>)
        {
                emit_line( $_ );
		if( /%%BeginProlog/ ) {
		    InsertHPAccountingInfo();
		}
        }
        close PSFILE;
}

###################################################################
# Main
###################################################################

if ($#ARGV == 4)
{
    $procfile = stdin2file();
}
elsif ($#ARGV == 5)
{
    $procfile = copy_file($ARGV[5]);
} else {
    abort_exit( "job-id user title copies options [file]");
}

for my $i ( 0 .. $#ARGV ) {
    print_debug( "ARG $i=$ARGV[$i]")
}
print_debug("job-id=$arg_job user=$arg_user title=$arg_title copies=$arg_copies $arg_options $procfile");

my ($key, $value);
foreach $key ( sort keys %ENV) {
    print_debug("ENV $key=$ENV{$key}");
}

print_debug( "Processing file");
open_capture();
processfile($procfile);
close_capture();

unlink($procfile) or print STDERR "ERROR: $progname - Couldn't remove '$procfile': $!\n";
exit 0;