forked from webmin/webmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaix-lib.pl
executable file
·395 lines (367 loc) · 9.45 KB
/
aix-lib.pl
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
# aix-lib.pl
# Functions for AIX-style printer management
# get_qconfig()
sub get_qconfig
{
local @rv;
local $prn;
local $lnum = 0;
open(CONF, "<".$config{'printcap_file'});
while(<CONF>) {
s/\r|\n//g;
s/^\s*#.*$//;
s/^\s*\*.*$//;
if (/^(\S+):\s*$/) {
# Start of a new printer
$prn = { 'name' => $1,
'line' => $lnum,
'eline' => $lnum };
push(@rv, $prn);
}
elsif (/^\s+(\S+)\s*=\s*(.*)/ && $prn) {
# Variable within a printer
$prn->{'eline'} = $lnum;
$prn->{'values'}->{lc($1)} = $2;
}
$lnum++;
}
close(CONF);
return @rv;
}
# list_printers()
# Returns an array of known printer names
sub list_printers
{
local @qc = &get_qconfig();
local (@rv, $q);
foreach $q (@qc) {
if ($q->{'values'}->{'device'}) {
# A real printer
push(@rv, $q->{'name'});
}
}
return &unique(@rv);
}
# get_printer(name, [nostatus])
# Returns a reference to an associative array of printer details
sub get_printer
{
if (!%enabled_cache) {
%enabled_cache = &get_enabled();
}
# Find the printer in qconfig
local @qc = &get_qconfig();
local %prn;
local ($real) = grep { $_->{'name'} eq $_[0] } @qc;
return undef if (!$real);
local ($device) = grep { $_->{'name'} eq $real->{'values'}->{'device'} &&
$_->{'line'} > $real->{'line'} } @qc;
# Construct info object
$prn{'name'} = $_[0];
$prn{'accepting'} = uc($real->{'values'}->{'up'}) ne 'FALSE';
$prn{'enabled'} = $enabled_cache{$_[0]};
$prn{'desc'} = $real->{'values'}->{'device'}; # XXX
if ($real->{'values'}->{'rq'}) {
# Remote printer
$prn{'rhost'} = $real->{'values'}->{'host'};
$prn{'rqueue'} = $real->{'values'}->{'rq'};
if ($real->{'values'}->{'s_statfilter'} =~ /bsdshort/) {
$prn{'type'} = 'bsd';
}
elsif ($real->{'values'}->{'s_statfilter'} =~ /attshort/) {
$prn{'type'} = 's5';
}
elsif ($real->{'values'}->{'s_statfilter'} =~ /aixv2short/) {
$prn{'type'} = 'aix2';
}
else {
# Assume AIX by default
$prn{'type'} = 'aix';
}
}
elsif ($device->{'values'}->{'backend'} =~ /piojetd\s+(\S+)\s+(\d+)/) {
# Jetdirect printer
$prn{'dhost'} = $1;
$prn{'dport'} = $2;
}
else {
# Local printer
$prn{'dev'} = "/dev/$_[0]"; # XXX
}
$prn{'banner'} = 1 if (lc($device->{'values'}->{'header'}) eq 'always');
if (!$prn{'dhost'} &&
$device->{'values'}->{'backend'} !~ /(rembak|piojetd|piob)/) {
$prn{'iface'} = $device->{'values'}->{'backend'};
}
return \%prn;
# XXX user access control
# XXX default printer?
# XXX remote system type
}
# get_jobs(printer)
sub get_jobs
{
local @jobs;
local $esc = quotemeta($_[0]);
local $doneheader;
open(LPQ, "lpq -P$esc |");
while(<LPQ>) {
s/\r|\n//g;
if (/^\-\-\-/) {
$doneheader++;
}
elsif ($doneheader &&
/^\s+(\S+)\s+(\d+)\s+(\S+)\s+(\S+)/) {
local $job = { 'id' => $2,
'user' => $4,
'file' => $3,
'printing' => lc($1) eq "active" };
push(@jobs, $job);
}
}
close(LPQ);
return @jobs;
}
# printer_support(option)
sub printer_support
{
return $_[0] !~ /^(why|allow|default|msize|ctype|alias|sysv|ipp|rnoqueue)$/;
}
# create_printer(&details)
# Create a new printer
sub create_printer
{
local $lref = &read_file_lines($config{'printcap_file'});
push(@$lref, &qconfig_real_lines($_[0]));
push(@$lref, &qconfig_device_lines($_[0]));
&flush_file_lines();
&enable_disable($_[0]);
}
# modify_printer(&details)
# Change an existing printer
sub modify_printer
{
# Find old entry
local @qc = &get_qconfig();
local ($real) = grep { $_->{'name'} eq $_[0]->{'name'} } @qc;
$real || &error("Failed to find old printer!");
local ($device) = grep { $_->{'name'} eq $real->{'values'}->{'device'} &&
$_->{'line'} > $real->{'line'} } @qc;
# Update lines in file
local $lref = &read_file_lines($config{'printcap_file'});
if ($device) {
splice(@$lref, $device->{'line'},
$device->{'eline'} - $device->{'line'} + 1,
&qconfig_device_lines($_[0], $device));
}
else {
splice(@$lref, $real->{'eline'} + 1, 0,
&qconfig_device_lines($_[0]));
}
splice(@$lref, $real->{'line'}, $real->{'eline'} - $real->{'line'} + 1,
&qconfig_real_lines($_[0], $real));
&flush_file_lines();
&enable_disable($_[0]);
}
# enable_disable(&printer)
# Enable or disable some printer
sub enable_disable
{
local %ena = &get_enabled();
if ($_[0]->{'enabled'} && !$ena{$_[0]->{'name'}}) {
&system_logged("enable ".quotemeta($_[0]->{'name'}));
}
elsif (!$_[0]->{'enabled'} && $ena{$_[0]->{'name'}}) {
&system_logged("disable ".quotemeta($_[0]->{'name'}));
}
}
# get_enabled()
# Returns a hash from printer names to their enabled statuses
sub get_enabled
{
local %ena;
open(ENA, "lpstat -s -W |");
while(<ENA>) {
s/\r|\n//g;
next if (/^Queue|\-\-\-\-/);
if (/^\s*(\S+)\s+(\S+)\s+(\S+)/) {
$ena{$1} = $3 eq 'READY';
}
}
close(ENA);
return %ena;
}
# delete_printer(name)
# Deletes some existing printer
sub delete_printer
{
# Find old entry
local @qc = &get_qconfig();
local ($real) = grep { $_->{'name'} eq $_[0] } @qc;
local ($device) = grep { $_->{'name'} eq $real->{'values'}->{'device'} &&
$_->{'line'} > $real->{'line'} } @qc;
# Take lines out of file
local $lref = &read_file_lines($config{'printcap_file'});
if ($device) {
splice(@$lref, $device->{'line'},
$device->{'eline'} - $device->{'line'} + 1);
}
splice(@$lref, $real->{'line'},
$real->{'eline'} - $real->{'line'} + 1);
&flush_file_lines();
}
# qconfig_real_lines(&printer, [&old-real])
sub qconfig_real_lines
{
local $real = $_[1] || { 'name' => $_[0]->{'name'},
'values' => { } };
$_[0]->{'desc'} ||= "$_[0]->{'name'}_device";
$real->{'values'}->{'device'} = $_[0]->{'desc'};
if ($_[0]->{'accepting'}) {
delete($real->{'values'}->{'up'});
}
else {
$real->{'values'}->{'up'} = 'FALSE';
}
if ($_[0]->{'rhost'}) {
$real->{'values'}->{'host'} = $_[0]->{'rhost'};
$real->{'values'}->{'rq'} = $_[0]->{'rqueue'};
if ($_[0]->{'type'} eq 'bsd') {
$real->{'values'}->{'s_statfilter'} = "/usr/lpd/bsdshort";
$real->{'values'}->{'l_statfilter'} = "/usr/lpd/bsdlong";
}
elsif ($_[0]->{'type'} eq 's5') {
$real->{'values'}->{'s_statfilter'} = "/usr/lpd/attshort";
$real->{'values'}->{'l_statfilter'} = "/usr/lpd/attlong";
}
elsif ($_[0]->{'type'} eq 'aix2') {
$real->{'values'}->{'s_statfilter'} = "/usr/lpd/aixv2short";
$real->{'values'}->{'l_statfilter'} = "/usr/lpd/aixv2long";
}
else {
$real->{'values'}->{'s_statfilter'} = "/usr/lpd/aixshort";
$real->{'values'}->{'l_statfilter'} = "/usr/lpd/aixlong";
}
}
else {
delete($real->{'values'}->{'host'});
delete($real->{'values'}->{'rq'});
delete($real->{'values'}->{'s_statfilter'});
delete($real->{'values'}->{'l_statfilter'});
}
return &qconfig_lines($real);
}
# qconfig_device_lines(&printer, [&old-device])
sub qconfig_device_lines
{
local $device = $_[1] || { 'name' => $_[0]->{'desc'},
'values' => { } };
if ($_[0]->{'rhost'}) {
$device->{'values'}->{'backend'} = "/usr/lib/lpd/rembak";
}
elsif ($_[0]->{'dhost'}) {
$device->{'values'}->{'backend'} = "/usr/lib/lpd/pio/etc/piojetd $_[0]->{'dhost'} $_[0]->{'dport'}";
if (!$device->{'values'}->{'file'}) {
local $dfile = "/var/spool/lpd/pio/\@local/dev/hp\@$_[0]->{'dhost'}#hpJetDirect#$_[0]->{'dport'}";
$device->{'values'}->{'file'} = $dfile;
&open_tempfile(DFILE, ">$dfile");
&print_tempfile(DFILE, "desc\t=\thpJetDirect\n");
&close_tempfile(DFILE);
}
}
elsif ($_[0]->{'iface'}) {
$device->{'values'}->{'backend'} = $_[0]->{'iface'};
}
else {
$device->{'values'}->{'backend'} = "/usr/lib/lpd/piobe";
}
if ($_[0]->{'banner'}) {
$device->{'values'}->{'header'} = "always";
}
else {
$device->{'values'}->{'header'} = "never";
}
return &qconfig_lines($device);
}
sub qconfig_lines
{
local @rv = ( "$_[0]->{'name'}:" );
local $k;
foreach $k (keys %{$_[0]->{'values'}}) {
push(@rv, "\t$k = $_[0]->{'values'}->{$k}");
}
return @rv;
}
# cancel_job(printer, id)
# Cancels some print job
sub cancel_job
{
local $esc = quotemeta($_[0]);
local $iesc = quotemeta($_[1]);
local $out = &backquote_logged("lprm -P $esc $iesc 2>&1");
if ($?) { &error("cancel failed : <pre>$out</pre>"); }
sleep(1);
}
# sched_running()
# Returns the pid if lpsched is running, 0 if not, -1 if cannot be stopped
sub sched_running
{
local @qpid = &find_byname("qdaemon");
local @lpid = &find_byname("lpd");
if (@qpid && @lpid) { return $qpid[0]; }
return 0;
}
# start_sched()
# Start lpsched
sub start_sched
{
local $s;
foreach $s ("qdaemon", "lpd") {
if (!&find_byname($s)) {
local $out = &backquote_logged("/usr/bin/startsrc -s$s 2>&1");
if ($?) { &error("failed to start $s : <tt>$out</tt>"); }
}
}
}
# stop_sched(pid)
# Stop the running lpsched process
sub stop_sched
{
local $s;
foreach $s ("qdaemon", "lpd") {
if (&find_byname($s)) {
local $out = &backquote_logged("/usr/bin/stopsrc -s$s 2>&1");
if ($?) { &error("failed to start $s : <tt>$out</tt>"); }
}
}
}
# print_command(printer, file)
# Returns the command to print some file on some printer
sub print_command
{
local $esc = quotemeta($_[0]);
local $fesc = quotemeta($_[1]);
return "lpr -P $esc $fesc";
}
# check_print_system()
sub check_print_system
{
&has_command("qdaemon") || return &text('aix_ecmd', "<tt>qdaemon</tt>");
return undef;
}
# validate_printer(&printer)
# Performs extra OS-specific printer validation, and returns an error message
# if there is a problem
sub validate_printer
{
return $text{'aix_edesc'} if ($_[0]->{'desc'} !~ /^[a-z0-9\-\.\_\@]*$/i);
return undef;
}
sub remote_printer_types
{
return ( [ 'aix', 'AIX' ], [ 'bsd', 'BSD' ],
[ 's5', 'ATT SysV' ], [ 'aix2', 'AIX v2' ] );
}
@device_files = ("/dev/lp0", "/dev/lp1", "/dev/null" );
@device_names = (&text('aix_port', "0"), &text('aix_port', "1"),
$text{'solaris_null'});