|
|
b5b1010 |
#!/usr/bin/perl
|
|
|
b5b1010 |
# The above Perl path may vary on your system; fix it!!! -*- perl -*-
|
|
|
b5b1010 |
|
|
|
b5b1010 |
# dnssd - Search for network printers with the avahi-browse command
|
|
|
b5b1010 |
# (Zeroconf, DNS-SD)
|
|
|
b5b1010 |
|
|
|
b5b1010 |
# Printer discovery CUPS backend (like the SNMP backend)
|
|
|
b5b1010 |
# See also http://qa.mandriva.com/show_bug.cgi?id=21812
|
|
|
b5b1010 |
|
|
|
b5b1010 |
# Copyright 2007 Till Kamppeter <till.kamppeter@gmail.com>
|
|
|
b5b1010 |
#
|
|
|
b5b1010 |
# This program is free software; you can redistribute it and/or modify it
|
|
|
b5b1010 |
# under the terms of the GNU General Public License as published by the
|
|
|
b5b1010 |
# Free Software Foundation; either version 2 of the License, or (at your
|
|
|
b5b1010 |
# option) any later version.
|
|
|
b5b1010 |
#
|
|
|
b5b1010 |
# This program is distributed in the hope that it will be useful, but
|
|
|
b5b1010 |
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
b5b1010 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
b5b1010 |
# Public License for more details.
|
|
|
b5b1010 |
#
|
|
|
b5b1010 |
# You should have received a copy of the GNU General Public License
|
|
|
b5b1010 |
# along with this program; if not, write to the Free Software
|
|
|
b5b1010 |
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
|
|
b5b1010 |
# USA.
|
|
|
b5b1010 |
|
|
|
b5b1010 |
# Usage:
|
|
|
b5b1010 |
#
|
|
|
b5b1010 |
# cp dnssd /usr/lib/cups/backend/
|
|
|
b5b1010 |
# chmod 755 /usr/lib/cups/backend/dnssd
|
|
|
b5b1010 |
# killall -HUP cupsd (or "/etc/init.d/cups restart", CUPS 1.1.x only)
|
|
|
b5b1010 |
# lpinfo -v (or use any printer setup tool)
|
|
|
b5b1010 |
|
|
|
b5b1010 |
#use strict;
|
|
|
b5b1010 |
|
|
|
b5b1010 |
$0 =~ m!^(.*)/([^/]+)\s*$!;
|
|
|
b5b1010 |
my $progname = ($2 || $0 || "dnssd");
|
|
|
b5b1010 |
my $progpath = ($1 || "/usr/lib/cups/backend");
|
|
|
b5b1010 |
|
|
|
b5b1010 |
if ($ARGV[0]){
|
|
|
b5b1010 |
die "This backend is only for printer discovery, not for actual printing.\n";
|
|
|
b5b1010 |
}
|
|
|
b5b1010 |
|
|
|
b5b1010 |
my $avahicmd = "avahi-browse -k -t -v -r -a 2> /dev/null";
|
|
|
b5b1010 |
|
|
|
b5b1010 |
# IPs which are for computers, consider their printer entries as queues
|
|
|
b5b1010 |
# set up with the local printing system (CUPS, LPD, Windows/Samba SMB, ...)
|
|
|
b0dd15a |
my @computerips = ();
|
|
|
b5b1010 |
my $output;
|
|
|
b0dd15a |
my $hosts;
|
|
|
b5b1010 |
my ($interface, $nettype, $ip, $host, $make, $model, $description, $cmd, $makemodel, $deviceid, $protocol, $port, $uriext, $uri);
|
|
|
b5b1010 |
|
|
|
b0dd15a |
open (AVAHI, "$avahicmd |") or exit 0;
|
|
|
b5b1010 |
while (my $line = <AVAHI>) {
|
|
|
b5b1010 |
chomp ($line);
|
|
|
b5b1010 |
if ($line =~ /^\s*=\s+(\S+)\s+(\S+)\s+(.*?)\s+(\S+)\s+(\S+)\s*$/) {
|
|
|
b5b1010 |
# New item
|
|
|
b5b1010 |
$interface = $1;
|
|
|
b5b1010 |
$nettype = $2;
|
|
|
b5b1010 |
my $itemname = $3;
|
|
|
b5b1010 |
my $protocolinfo = $4;
|
|
|
b5b1010 |
if ($protocolinfo =~ /_workstation/) {
|
|
|
b5b1010 |
$protocol = "computer";
|
|
|
b5b1010 |
} elsif ($protocolinfo =~ /_pdl-datastream/) {
|
|
|
b5b1010 |
$protocol = "socket";
|
|
|
b5b1010 |
} elsif ($protocolinfo =~ /_printer/) {
|
|
|
b5b1010 |
$protocol = "lpd";
|
|
|
b5b1010 |
} elsif ($protocolinfo =~ /_ipp/) {
|
|
|
b5b1010 |
$protocol = "ipp";
|
|
|
b5b1010 |
}
|
|
|
b5b1010 |
} elsif ($line =~ /^\s*hostname\s*=\s*\[([^\]]+)\]\s*$/) {
|
|
|
b5b1010 |
$host = $1;
|
|
|
b5b1010 |
$host =~ s/\.local\.?$//;
|
|
|
b5b1010 |
} elsif ($line =~ /^\s*address\s*=\s*\[([^\]]+)\]\s*$/) {
|
|
|
b5b1010 |
$ip = $1;
|
|
|
b5b1010 |
if ($protocol eq "computer") {
|
|
|
b0dd15a |
push (@computerips, $ip);
|
|
|
b5b1010 |
$protocol = "";
|
|
|
b5b1010 |
}
|
|
|
b5b1010 |
} elsif ($line =~ /^\s*port\s*=\s*\[([^\]]+)\]\s*$/) {
|
|
|
b5b1010 |
$port = $1;
|
|
|
b5b1010 |
} elsif ($line =~ /^\s*txt\s*=\s*\[(.+)\]\s*$/) {
|
|
|
b5b1010 |
my $info = $1;
|
|
|
b5b1010 |
if ($protocol) {
|
|
|
b5b1010 |
my ($ty, $product, $pdls, $usb_MFG, $usb_MDL, $usb_DES, $usb_CMD) =
|
|
|
b5b1010 |
("", "", "", "", "", "", "");
|
|
|
b5b1010 |
while ($info =~ s/^\s*\"([^\"]+)\"\s*//) {
|
|
|
b5b1010 |
my $infoitem = $1;
|
|
|
b5b1010 |
if ($infoitem =~ /^([^=]*)=(.*)$/) {
|
|
|
b5b1010 |
my $field = $1;
|
|
|
b5b1010 |
my $content = $2;
|
|
|
b5b1010 |
if ($field eq "ty") {
|
|
|
b5b1010 |
$ty = $content;
|
|
|
b5b1010 |
} elsif ($field eq "product") {
|
|
|
b5b1010 |
$product = $content;
|
|
|
b5b1010 |
$product =~ s/^\((.*)\)$/$1/;
|
|
|
b5b1010 |
} elsif ($field eq "usb_MFG") {
|
|
|
b5b1010 |
$usb_MFG = $content;
|
|
|
b5b1010 |
} elsif ($field eq "usb_MDL") {
|
|
|
b5b1010 |
$usb_MDL = $content;
|
|
|
b5b1010 |
} elsif ($field eq "usb_DES") {
|
|
|
b5b1010 |
$usb_DES = $content;
|
|
|
b5b1010 |
} elsif ($field eq "usb_CMD") {
|
|
|
b5b1010 |
$usb_CMD = $content;
|
|
|
b5b1010 |
} elsif ($field eq "rp") {
|
|
|
b5b1010 |
$uriext = $content;
|
|
|
b5b1010 |
} elsif ($field eq "pdl") {
|
|
|
b5b1010 |
while ($content =~ s/^\s*([^\,]+?)\s*\,\s*//) {
|
|
|
b5b1010 |
my $i = $1;
|
|
|
b5b1010 |
if ($i =~ m!\b(postscript|ps)\b!i) {
|
|
|
b5b1010 |
$pdls .= "POSTSCRIPT,";
|
|
|
b5b1010 |
} elsif ($i =~ m!\b(pdf)\b!i) {
|
|
|
b5b1010 |
$pdls .= "PDF,";
|
|
|
b5b1010 |
} elsif ($i =~ m!\b(pcl6|pclxl|pxl)\b!i) {
|
|
|
b5b1010 |
$pdls .= "PCLXL,";
|
|
|
b5b1010 |
} elsif ($i =~ m!\b(pcl[345][ce]?|pcl)\b!i) {
|
|
|
b5b1010 |
$pdls .= "PCL,";
|
|
|
b5b1010 |
}
|
|
|
b5b1010 |
}
|
|
|
b5b1010 |
$pdls =~ s/\,$//;
|
|
|
b5b1010 |
}
|
|
|
b5b1010 |
}
|
|
|
b5b1010 |
}
|
|
|
b5b1010 |
$usb_MDL ||= $ty;
|
|
|
b5b1010 |
$usb_DES ||= $product;
|
|
|
b5b1010 |
if ($usb_MFG) {
|
|
|
b5b1010 |
$make = $usb_MFG;
|
|
|
b0dd15a |
} elsif ($usb_DES =~ /^KONICA\s*MINOLTA\b/i) {
|
|
|
b5b1010 |
$make = "KONICA MINOLTA";
|
|
|
b5b1010 |
} elsif ($usb_DES) {
|
|
|
b5b1010 |
$usb_DES =~ /^\s*(\S*)\b/;
|
|
|
b5b1010 |
$make = $1;
|
|
|
b5b1010 |
}
|
|
|
b5b1010 |
$model = $usb_MDL;
|
|
|
b5b1010 |
if (!$model) {
|
|
|
b5b1010 |
$usb_DES =~ /^\s*\S*\s*(.*)$/;
|
|
|
b5b1010 |
$model = $1;
|
|
|
b5b1010 |
}
|
|
|
b5b1010 |
$usb_CMD ||= $pdls;
|
|
|
b5b1010 |
my $extra;
|
|
|
b5b1010 |
if ($protocol eq "socket") {
|
|
|
b0dd15a |
if ($port eq "9100") {
|
|
|
b0dd15a |
$uri = "socket://$ip";
|
|
|
b0dd15a |
} else {
|
|
|
b0dd15a |
$uri = "socket://$ip:$port";
|
|
|
b0dd15a |
}
|
|
|
b5b1010 |
$extra = "Port $port";
|
|
|
b5b1010 |
} elsif ($protocol eq "lpd") {
|
|
|
b5b1010 |
$uri = "lpd://$ip" . ($uriext ? "/$uriext" : "");
|
|
|
b5b1010 |
$extra = ($uriext ? "Queue: $uriext" : "Default queue");
|
|
|
b5b1010 |
} elsif ($protocol eq "ipp") {
|
|
|
b5b1010 |
$uri = "ipp://$ip:$port" . ($uriext ? "/$uriext" : "");
|
|
|
b5b1010 |
$extra = ($uriext ? "Queue: $uriext" : "Default queue");
|
|
|
b5b1010 |
}
|
|
|
b5b1010 |
if ($make && $model) {
|
|
|
b5b1010 |
$make =~ s/Hewlett.?Packard/HP/i;
|
|
|
b5b1010 |
$make =~ s/Lexmark.?International/Lexmark/i;
|
|
|
b5b1010 |
$model =~ s/Hewlett.?Packard/HP/i;
|
|
|
b5b1010 |
$model =~ s/Lexmark.?International/Lexmark/i;
|
|
|
b5b1010 |
while ($model =~ s/^\s*$make\s*//i) {};
|
|
|
b5b1010 |
$makemodel = "$make $model";
|
|
|
b5b1010 |
} elsif ($usb_DES) {
|
|
|
b5b1010 |
$makemodel = $usb_DES;
|
|
|
b5b1010 |
} else {
|
|
|
b5b1010 |
$makemodel = "Unknown";
|
|
|
b5b1010 |
}
|
|
|
b5b1010 |
$deviceid = ($usb_MFG ? "MFG:$usb_MFG;" : "") .
|
|
|
b5b1010 |
($usb_MDL ? "MDL:$usb_MDL;" : "") .
|
|
|
b5b1010 |
($usb_DES ? "DES:$usb_DES;" : "") .
|
|
|
b5b1010 |
($usb_CMD ? "CMD:$usb_CMD;" : "");
|
|
|
b5b1010 |
$deviceid .= "CLS:PRINTER;" if $deviceid;
|
|
|
b0dd15a |
$hosts->{$ip} = $hostname if ($hostname);
|
|
|
b0dd15a |
$output->{$ip}{$protocol}{$uriext} =
|
|
|
b5b1010 |
"network $uri \"$makemodel\" \"$makemodel $ip ($extra)\" \"$deviceid\"\n";
|
|
|
b5b1010 |
($interface, $nettype, $ip, $host, $make, $model, $description, $cmd, $makemodel, $deviceid, $protocol, $port, $uriext, $uri) =
|
|
|
b5b1010 |
("", "", "", "", "", "", "", "", "", "", "", "", "", "");
|
|
|
b5b1010 |
}
|
|
|
b5b1010 |
}
|
|
|
b5b1010 |
}
|
|
|
b5b1010 |
|
|
|
b0dd15a |
my $localqueues = {};
|
|
|
b0dd15a |
my $queue = undef;
|
|
|
b0dd15a |
if (open LPSTAT, "LC_ALL=C lpstat -l -p -v |") {
|
|
|
b0dd15a |
while (my $line = <LPSTAT>) {
|
|
|
b0dd15a |
chomp $line;
|
|
|
b0dd15a |
if ($line =~ /^printer\s+(\S+)/i) {
|
|
|
b0dd15a |
$queue = $1;
|
|
|
b0dd15a |
$localqueues->{$queue} = {};
|
|
|
b0dd15a |
} elsif ($queue) {
|
|
|
b0dd15a |
if ($line =~ /^\s+Connection:\s+remote/i) {
|
|
|
b0dd15a |
$localqueues->{$queue}{remote} = 1;
|
|
|
b0dd15a |
} elsif ($line =~ /^\s+Interface:\s+(\S+)/i) {
|
|
|
b0dd15a |
$localqueues->{$queue}{interface} = $1;
|
|
|
b0dd15a |
} elsif ($line =~ /^device\s+for\s+(\S+)\s*:\s*(\S+)/i) {
|
|
|
b0dd15a |
$localqueues->{$1}{uri} = $2;
|
|
|
b0dd15a |
}
|
|
|
b0dd15a |
}
|
|
|
b0dd15a |
}
|
|
|
b0dd15a |
close LPSTAT
|
|
|
b0dd15a |
}
|
|
|
b0dd15a |
|
|
|
b0dd15a |
my @localips = ();
|
|
|
b0dd15a |
if (open IFCONFIG, "LC_ALL=C /sbin/ifconfig |") {
|
|
|
b0dd15a |
while (my $line = <IFCONFIG>) {
|
|
|
b0dd15a |
chomp $line;
|
|
|
b0dd15a |
if ($line =~ /^\s*inet\s+addr:\s*(\S+)/i) {
|
|
|
b0dd15a |
push (@localips, $1);
|
|
|
b0dd15a |
}
|
|
|
b0dd15a |
}
|
|
|
b0dd15a |
close IFCONFIG;
|
|
|
b0dd15a |
}
|
|
|
b0dd15a |
|
|
|
b5b1010 |
foreach my $ip (keys(%{$output})) {
|
|
|
b0dd15a |
# Do not list print queues of the local machine
|
|
|
b0dd15a |
next if member($ip, @localips);
|
|
|
b5b1010 |
if ($output->{$ip}{"socket"}) {
|
|
|
b0dd15a |
foreach my $uriext (keys(%{$output->{$ip}{"socket"}})) {
|
|
|
b0dd15a |
if (keys(%{$output->{$ip}{"socket"}}) == 1) {
|
|
|
b0dd15a |
$output->{$ip}{"socket"}{$uriext} =~
|
|
|
b5b1010 |
s/^(\s*\S*\s*\S*\s*\"[^\"]*\"\s*\"[^\"\(]*?)\s*\([^\)]*\)\s*(\"\s*.*)$/$1$2/;
|
|
|
b5b1010 |
}
|
|
|
b0dd15a |
print $output->{$ip}{"socket"}{$uriext};
|
|
|
b5b1010 |
}
|
|
|
b5b1010 |
} elsif ($output->{$ip}{"lpd"}) {
|
|
|
b0dd15a |
foreach my $uriext (keys(%{$output->{$ip}{"lpd"}})) {
|
|
|
b0dd15a |
if (keys(%{$output->{$ip}{"lpd"}}) == 1) {
|
|
|
b0dd15a |
$output->{$ip}{"lpd"}{$uriext} =~
|
|
|
b5b1010 |
s/^(\s*\S*\s*\S*\s*\"[^\"]*\"\s*\"[^\"\(]*?)\s*\([^\)]*\)\s*(\"\s*.*)$/$1$2/;
|
|
|
b5b1010 |
}
|
|
|
b0dd15a |
print $output->{$ip}{"lpd"}{$uriext};
|
|
|
b5b1010 |
}
|
|
|
b5b1010 |
} elsif ($output->{$ip}{"ipp"}) {
|
|
|
b0dd15a |
foreach my $uriext (keys(%{$output->{$ip}{"ipp"}})) {
|
|
|
b0dd15a |
if ($uriext =~ /^(printers|classes)\/(\S+)$/) {
|
|
|
b0dd15a |
# Queue from a CUPS server. We should suppress it if it
|
|
|
b0dd15a |
# is a queue received via CUPS broadcast
|
|
|
b0dd15a |
$queue=$2;
|
|
|
b0dd15a |
if (defined($localqueues->{"$queue\@$ip"})) {
|
|
|
b0dd15a |
$queue = "$queue\@$ip";
|
|
|
b0dd15a |
} elsif (defined($localqueues->{"$queue\@$hosts->{$ip}"})) {
|
|
|
b0dd15a |
$queue = "$queue\@$hosts->{$ip}";
|
|
|
b0dd15a |
}
|
|
|
b0dd15a |
if (defined($localqueues->{$queue})) {
|
|
|
b0dd15a |
if ($localqueues->{$queue}{remote} &&
|
|
|
b0dd15a |
($localqueues->{$queue}{uri} =~
|
|
|
b0dd15a |
/^ipp:\/\/([^\/:]+)(:\d+|)\/(\S+)/)) {
|
|
|
b0dd15a |
my $host = $1;
|
|
|
b0dd15a |
my $ue = $3;
|
|
|
b0dd15a |
if ($host !~ /\d+\.\d+\.\d+\.\d+/) {
|
|
|
b0dd15a |
$host =
|
|
|
b0dd15a |
join(".", unpack("C4", gethostbyname($host)));
|
|
|
b0dd15a |
}
|
|
|
b0dd15a |
next if ($host eq $ip) && ($ue eq $uriext);
|
|
|
b0dd15a |
}
|
|
|
b0dd15a |
}
|
|
|
b0dd15a |
}
|
|
|
b5b1010 |
if (keys(%{$output->{$ip}{"ipp"}}) == 1) {
|
|
|
b0dd15a |
$output->{$ip}{"ipp"}{$uriext} =~
|
|
|
b5b1010 |
s/^(\s*\S*\s*\S*\s*\"[^\"]*\"\s*\"[^\"]*?)\s*\([^\)]*\)\s*(\"\s*.*)$/$1$2/;
|
|
|
b5b1010 |
}
|
|
|
b0dd15a |
print $output->{$ip}{"ipp"}{$uriext};
|
|
|
b5b1010 |
}
|
|
|
b5b1010 |
}
|
|
|
b5b1010 |
}
|
|
|
b5b1010 |
|
|
|
b5b1010 |
exit 0;
|
|
|
b5b1010 |
|
|
|
b5b1010 |
# member( $a, @b ) returns 1 if $a is in @b, 0 otherwise.
|
|
|
b5b1010 |
sub member { my $e = shift; foreach (@_) { $e eq $_ and return 1 } 0 };
|