-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpublish-cups-dns
More file actions
executable file
·203 lines (177 loc) · 5.52 KB
/
publish-cups-dns
File metadata and controls
executable file
·203 lines (177 loc) · 5.52 KB
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
#!/usr/bin/perl
# Script to push discovery information about CUPS printer
# queues into wide-area DNS for DNS-SD
# Hostname/IP of your DNS server
$DNSSRV='';
# Set this to '' if cups is installed in /
#$CUPS='/usr/local/cups';
$CUPS='';
# Hostname / IP of your CUPS server
$CUPS_SERVER='printhost';
# Space-separated list of domains to push DNS-SD info into
$SD_DOMAINS="dom1.example.com dom2.example.com";
####################################################################
#
# Nothing below this line should need to be changed
# unless your CUPS install is weird. You have been warned!
#
use Switch;
$hostname=`hostname -f`;
chomp($hostname);
($cupshost, $myDomain)=split('\.', $hostname, 2);
$PPDS="$CUPS/etc/cups/ppd";
if($ARGV[0] eq "-d") { $delonly=1; shift @ARGV; }
if($ARGV[0] eq "-g") { $dbg=1 ; shift @ARGV };
if($ARGV[0] eq "-v") { $dbg=1 ; shift @ARGV }
if($ARGV[0] eq "-n") {
$dryrun=1;
} else {
$dryrun=0;
}
# Tell the cups tools which server to query
$ENV{'CUPS_SERVER'}=$CUPS_SERVER;
# Start the nsupdate process
if ($dryrun) {
open(NDC, ">/tmp/dns-update.log") || die ("Cannot create dry run logfile!\n");
} else {
open(NDC, "|tee /tmp/commands | /usr/bin/nsupdate -v >/tmp/dns-update.log 2>&1") || die ("Fatal - cannot start 'nsupdate'!\n");
}
foreach $DOM (split(' ', $SD_DOMAINS)) {
####### Determine the DNS master to send updates to ########
if ($DNSSRV eq '') {
$res=`dig +short $DOM IN SOA`;
chomp($res);
if ($res eq '') {
die ("Error: can't determine DNS master for zone $DOM\n");
} else {
$DNSSRV=$res;
$DNSSRV=~s/\. .*//;
print "Sending updates to server $DNSSRV\n" if ($dbg);
}
}
print (NDC "server $DNSSRV\n") || die ("Error writing to nsupdate process!\n");
####### Check to make sure the domain is set up for wide-area DNS-SD ########
foreach $pointer ('b', 'lb', 'db') {
$res=`dig +short $pointer._dns-sd._udp.$DOM IN PTR`;
chomp($res);
print "$pointer pointer is $res\n" if ($dbg);
if ($res ne "$DOM.") {
print "Warning: domain $DOM is missing DNS-SD $pointer pointer\n";
print " Attempting to add\n";
print (NDC "update add $pointer._dns-sd._udp.$DOM. 600 IN PTR $DOM.\n");
print (NDC "send\n");
}
}
###### Delete all existing cups data from dns ################
print ("Clearing old dns-sd cups data...\n") if ($dbg);
print (NDC "update delete _cups._sub._ipp._tcp.$DOM IN PTR\n");
print (NDC "update delete _ipp._tcp.$DOM. IN PTR\n");
print (NDC "send\n");
open (DIG, "dig \@$DNSSRV $DOM in axfr|");
while ($dig=<DIG>) {
if ($dig=~/_tcp.*TXT/){
($prt) = split(/ /, $dig);
$prt=~s/\..*//;
$printers{$prt}=1;
}
}
close(DIG);
foreach $prt (keys(%printers)) {
print ("Removing old printer: $prt._ipp._tcp.$DOM\n") if ($dbg);
print (NDC "update delete $prt._ipp._tcp.$DOM IN ANY\n");
}
print (NDC "send\n");
########## Publish the new cups DNS-SD info ############
if (! -e "$CUPS/etc/cups/printers.conf") {
die ("Error: Cannot find cups or printers.conf!\n");
}
open(PRN, "< $CUPS/etc/cups/printers.conf");
while($lp=<PRN>) {
chomp($lp);
print ("Parsing line $lp\n") if ($dbg);
if ($lp=~/\<(Default)*Printer /) {
# Found a new printer
$inlp=1; $prod="";
$prt=$lp;
$prt=~s/\<.*Printer (.*)\>/$1/;
print ("Found printer $prt\n") if ($dbg);
$ppd=$PPDS."/".$prt.".ppd";
print (" PPD file is $ppd\n") if ($dbg);
if (! -e "$ppd") {
print (" Printer $prt not a real printer - skipping\n") if ($dbg);
next;
}
}
if ($lp=~/^Info/) {
#$ty=substr($lp, 5);
print (" Info: $ty\n") if ($dbg);
}
if ($lp=~/^Location/) {
$note=substr($lp, 9);
print (" Location: $note\n") if ($dbg);
}
if ($lp=~/^Shared Yes/) {
$shared=1
}
if ($lp=~/^Shared No/) {
$shared=0
}
if ($lp=~/\<\/Printer\>/) {
# End of printer block - write the config
if (!$inlp) {
# Bad printer block - bail out!
die ("Error: Malformed printers.conf!\n");
}
# Find product type from the PPD file
open (PPD, "<$ppd") || die ("Error: Cannot find PPD for $prt!\n");
while(<PPD>) {
if (/^*Product:\s/) {
chomp();
$prod=$_;
$prod=~s/\*Product:\s*"(.*)"/$1/;
$prod=~s/\cM//;
$ty=$prod;
$ty=~s/[()]//g;
print (" Printer is a $prod\n") if ($dbg);
}
}
close (PPD);
# Try to read duplex capability from lpoptions
$duplex=0; $add="";
open (INFO, "lpoptions -p $prt -l|");
while(<INFO>) {
if (/Duplex Unit.*\*/) {
chomp();
$dup=$_;
$dup=~s/.*\*(\w+)\s*.*/$1/;
switch ($dup) {
case "False" { $duplex=0 };
case "True" { $duplex=1; $add="\"Duplex=T\"" };
case "Installed" { $duplex=1; $add="\"Duplex=T\"" };
case "NotInstalled" { $duplex=0 };
}
}
}
close (INFO);
if ($prod eq "") {
print ("Error - no model info for $prt!\n");
$inlp=0;
next;
}
if (!$delonly) {
if ($shared) {
print "Adding $prt to $DOM...\n" if ($dbg);
print (NDC "update add _ipp._tcp.$DOM. 60 IN PTR $prt._ipp._tcp.$DOM.\n");
print (NDC "update add _cups._sub._ipp._tcp.$DOM. 60 IN PTR $prt._ipp._tcp.$DOM.\n");
print (NDC "update add $prt._ipp._tcp.$DOM. 60 IN SRV 0 0 631 $cupshost.\n");
print (NDC "update add $prt._ipp._tcp.$DOM. 60 IN TXT \"txtvers=1\" \"qtotal=1\" \"rp=printers/$prt\" \"ty=$ty\" \"note=$note\" \"product=$prod\" $add\n");
print (NDC "send\n");
}
}
$inlp=0;
}
}
} # End $SD_DOMAINS loop
print (NDC "quit\n");
close (NDC);
close (PRN);