1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 | printcap parsing
|
---|
4 | Copyright (C) Karl Auer 1993-1998
|
---|
5 |
|
---|
6 | Re-working by Martin Kiff, 1994
|
---|
7 |
|
---|
8 | Re-written again by Andrew Tridgell
|
---|
9 |
|
---|
10 | Modified for SVID support by Norm Jacobs, 1997
|
---|
11 |
|
---|
12 | Modified for CUPS support by Michael Sweet, 1999
|
---|
13 |
|
---|
14 | This program is free software; you can redistribute it and/or modify
|
---|
15 | it under the terms of the GNU General Public License as published by
|
---|
16 | the Free Software Foundation; either version 3 of the License, or
|
---|
17 | (at your option) any later version.
|
---|
18 |
|
---|
19 | This program is distributed in the hope that it will be useful,
|
---|
20 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
22 | GNU General Public License for more details.
|
---|
23 |
|
---|
24 | You should have received a copy of the GNU General Public License
|
---|
25 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
26 | */
|
---|
27 |
|
---|
28 | /*
|
---|
29 | * This module contains code to parse and cache printcap data, possibly
|
---|
30 | * in concert with the CUPS/SYSV/AIX-specific code found elsewhere.
|
---|
31 | *
|
---|
32 | * The way this module looks at the printcap file is very simplistic.
|
---|
33 | * Only the local printcap file is inspected (no searching of NIS
|
---|
34 | * databases etc).
|
---|
35 | *
|
---|
36 | * There are assumed to be one or more printer names per record, held
|
---|
37 | * as a set of sub-fields separated by vertical bar symbols ('|') in the
|
---|
38 | * first field of the record. The field separator is assumed to be a colon
|
---|
39 | * ':' and the record separator a newline.
|
---|
40 | *
|
---|
41 | * Lines ending with a backspace '\' are assumed to flag that the following
|
---|
42 | * line is a continuation line so that a set of lines can be read as one
|
---|
43 | * printcap entry.
|
---|
44 | *
|
---|
45 | * A line stating with a hash '#' is assumed to be a comment and is ignored
|
---|
46 | * Comments are discarded before the record is strung together from the
|
---|
47 | * set of continuation lines.
|
---|
48 | *
|
---|
49 | * Opening a pipe for "lpc status" and reading that would probably
|
---|
50 | * be pretty effective. Code to do this already exists in the freely
|
---|
51 | * distributable PCNFS server code.
|
---|
52 | *
|
---|
53 | * Modified to call SVID/XPG4 support if printcap name is set to "lpstat"
|
---|
54 | * in smb.conf under Solaris.
|
---|
55 | *
|
---|
56 | * Modified to call CUPS support if printcap name is set to "cups"
|
---|
57 | * in smb.conf.
|
---|
58 | *
|
---|
59 | * Modified to call iPrint support if printcap name is set to "iprint"
|
---|
60 | * in smb.conf.
|
---|
61 | */
|
---|
62 |
|
---|
63 | #include "includes.h"
|
---|
64 |
|
---|
65 |
|
---|
66 | struct pcap_cache {
|
---|
67 | char *name;
|
---|
68 | char *comment;
|
---|
69 | struct pcap_cache *next;
|
---|
70 | };
|
---|
71 |
|
---|
72 | /* The systemwide printcap cache. */
|
---|
73 | static struct pcap_cache *pcap_cache = NULL;
|
---|
74 |
|
---|
75 | bool pcap_cache_add_specific(struct pcap_cache **ppcache, const char *name, const char *comment)
|
---|
76 | {
|
---|
77 | struct pcap_cache *p;
|
---|
78 |
|
---|
79 | if (name == NULL || ((p = SMB_MALLOC_P(struct pcap_cache)) == NULL))
|
---|
80 | return false;
|
---|
81 |
|
---|
82 | p->name = SMB_STRDUP(name);
|
---|
83 | p->comment = (comment && *comment) ? SMB_STRDUP(comment) : NULL;
|
---|
84 |
|
---|
85 | DEBUG(11,("pcap_cache_add_specific: Adding name %s info %s\n",
|
---|
86 | p->name, p->comment ? p->comment : ""));
|
---|
87 |
|
---|
88 | p->next = *ppcache;
|
---|
89 | *ppcache = p;
|
---|
90 |
|
---|
91 | return true;
|
---|
92 | }
|
---|
93 |
|
---|
94 | void pcap_cache_destroy_specific(struct pcap_cache **pp_cache)
|
---|
95 | {
|
---|
96 | struct pcap_cache *p, *next;
|
---|
97 |
|
---|
98 | for (p = *pp_cache; p != NULL; p = next) {
|
---|
99 | next = p->next;
|
---|
100 |
|
---|
101 | SAFE_FREE(p->name);
|
---|
102 | SAFE_FREE(p->comment);
|
---|
103 | SAFE_FREE(p);
|
---|
104 | }
|
---|
105 | *pp_cache = NULL;
|
---|
106 | }
|
---|
107 |
|
---|
108 | bool pcap_cache_add(const char *name, const char *comment)
|
---|
109 | {
|
---|
110 | return pcap_cache_add_specific(&pcap_cache, name, comment);
|
---|
111 | }
|
---|
112 |
|
---|
113 | bool pcap_cache_loaded(void)
|
---|
114 | {
|
---|
115 | return (pcap_cache != NULL);
|
---|
116 | }
|
---|
117 |
|
---|
118 | void pcap_cache_replace(const struct pcap_cache *pcache)
|
---|
119 | {
|
---|
120 | const struct pcap_cache *p;
|
---|
121 |
|
---|
122 | pcap_cache_destroy_specific(&pcap_cache);
|
---|
123 | for (p = pcache; p; p = p->next) {
|
---|
124 | pcap_cache_add(p->name, p->comment);
|
---|
125 | }
|
---|
126 | }
|
---|
127 |
|
---|
128 | void pcap_cache_reload(void)
|
---|
129 | {
|
---|
130 | const char *pcap_name = lp_printcapname();
|
---|
131 | bool pcap_reloaded = False;
|
---|
132 | struct pcap_cache *tmp_cache = NULL;
|
---|
133 | XFILE *pcap_file;
|
---|
134 | char *pcap_line;
|
---|
135 |
|
---|
136 | DEBUG(3, ("reloading printcap cache\n"));
|
---|
137 |
|
---|
138 | /* only go looking if no printcap name supplied */
|
---|
139 | if (pcap_name == NULL || *pcap_name == 0) {
|
---|
140 | DEBUG(0, ("No printcap file name configured!\n"));
|
---|
141 | return;
|
---|
142 | }
|
---|
143 |
|
---|
144 | tmp_cache = pcap_cache;
|
---|
145 | pcap_cache = NULL;
|
---|
146 |
|
---|
147 | #ifdef HAVE_CUPS
|
---|
148 | if (strequal(pcap_name, "cups")) {
|
---|
149 | pcap_reloaded = cups_cache_reload();
|
---|
150 | goto done;
|
---|
151 | }
|
---|
152 | #endif
|
---|
153 |
|
---|
154 | #ifdef HAVE_IPRINT
|
---|
155 | if (strequal(pcap_name, "iprint")) {
|
---|
156 | pcap_reloaded = iprint_cache_reload();
|
---|
157 | goto done;
|
---|
158 | }
|
---|
159 | #endif
|
---|
160 |
|
---|
161 | #if defined(SYSV) || defined(HPUX)
|
---|
162 | if (strequal(pcap_name, "lpstat")) {
|
---|
163 | pcap_reloaded = sysv_cache_reload();
|
---|
164 | goto done;
|
---|
165 | }
|
---|
166 | #endif
|
---|
167 |
|
---|
168 | #ifdef AIX
|
---|
169 | if (strstr_m(pcap_name, "/qconfig") != NULL) {
|
---|
170 | pcap_reloaded = aix_cache_reload();
|
---|
171 | goto done;
|
---|
172 | }
|
---|
173 | #endif
|
---|
174 |
|
---|
175 | /* handle standard printcap - moved from pcap_printer_fn() */
|
---|
176 |
|
---|
177 | if ((pcap_file = x_fopen(pcap_name, O_RDONLY, 0)) == NULL) {
|
---|
178 | DEBUG(0, ("Unable to open printcap file %s for read!\n", pcap_name));
|
---|
179 | goto done;
|
---|
180 | }
|
---|
181 |
|
---|
182 | for (; (pcap_line = fgets_slash(NULL, 1024, pcap_file)) != NULL; safe_free(pcap_line)) {
|
---|
183 | char name[MAXPRINTERLEN+1];
|
---|
184 | char comment[62];
|
---|
185 | char *p, *q;
|
---|
186 |
|
---|
187 | if (*pcap_line == '#' || *pcap_line == 0)
|
---|
188 | continue;
|
---|
189 |
|
---|
190 | /* now we have a real printer line - cut at the first : */
|
---|
191 | if ((p = strchr_m(pcap_line, ':')) != NULL)
|
---|
192 | *p = 0;
|
---|
193 |
|
---|
194 | /*
|
---|
195 | * now find the most likely printer name and comment
|
---|
196 | * this is pure guesswork, but it's better than nothing
|
---|
197 | */
|
---|
198 | for (*name = *comment = 0, p = pcap_line; p != NULL; p = q) {
|
---|
199 | bool has_punctuation;
|
---|
200 |
|
---|
201 | if ((q = strchr_m(p, '|')) != NULL)
|
---|
202 | *q++ = 0;
|
---|
203 |
|
---|
204 | has_punctuation = (strchr_m(p, ' ') ||
|
---|
205 | strchr_m(p, '\t') ||
|
---|
206 | strchr_m(p, '(') ||
|
---|
207 | strchr_m(p, ')'));
|
---|
208 |
|
---|
209 | if (strlen(p) > strlen(comment) && has_punctuation) {
|
---|
210 | strlcpy(comment, p, sizeof(comment));
|
---|
211 | continue;
|
---|
212 | }
|
---|
213 |
|
---|
214 | if (strlen(p) <= MAXPRINTERLEN &&
|
---|
215 | strlen(p) > strlen(name) && !has_punctuation) {
|
---|
216 | if (!*comment) {
|
---|
217 | strlcpy(comment, name, sizeof(comment));
|
---|
218 | }
|
---|
219 | strlcpy(name, p, sizeof(name));
|
---|
220 | continue;
|
---|
221 | }
|
---|
222 |
|
---|
223 | if (!strchr_m(comment, ' ') &&
|
---|
224 | strlen(p) > strlen(comment)) {
|
---|
225 | strlcpy(comment, p, sizeof(comment));
|
---|
226 | continue;
|
---|
227 | }
|
---|
228 | }
|
---|
229 |
|
---|
230 | comment[60] = 0;
|
---|
231 | name[MAXPRINTERLEN] = 0;
|
---|
232 |
|
---|
233 | if (*name && !pcap_cache_add(name, comment)) {
|
---|
234 | x_fclose(pcap_file);
|
---|
235 | goto done;
|
---|
236 | }
|
---|
237 | }
|
---|
238 |
|
---|
239 | x_fclose(pcap_file);
|
---|
240 | pcap_reloaded = True;
|
---|
241 |
|
---|
242 | done:
|
---|
243 | DEBUG(3, ("reload status: %s\n", (pcap_reloaded) ? "ok" : "error"));
|
---|
244 |
|
---|
245 | if (pcap_reloaded)
|
---|
246 | pcap_cache_destroy_specific(&tmp_cache);
|
---|
247 | else {
|
---|
248 | pcap_cache_destroy_specific(&pcap_cache);
|
---|
249 | pcap_cache = tmp_cache;
|
---|
250 | }
|
---|
251 |
|
---|
252 | return;
|
---|
253 | }
|
---|
254 |
|
---|
255 |
|
---|
256 | bool pcap_printername_ok(const char *printername)
|
---|
257 | {
|
---|
258 | struct pcap_cache *p;
|
---|
259 |
|
---|
260 | for (p = pcap_cache; p != NULL; p = p->next)
|
---|
261 | if (strequal(p->name, printername))
|
---|
262 | return True;
|
---|
263 |
|
---|
264 | return False;
|
---|
265 | }
|
---|
266 |
|
---|
267 | /***************************************************************************
|
---|
268 | run a function on each printer name in the printcap file.
|
---|
269 | ***************************************************************************/
|
---|
270 |
|
---|
271 | void pcap_printer_fn_specific(const struct pcap_cache *pc,
|
---|
272 | void (*fn)(const char *, const char *, void *),
|
---|
273 | void *pdata)
|
---|
274 | {
|
---|
275 | const struct pcap_cache *p;
|
---|
276 |
|
---|
277 | for (p = pc; p != NULL; p = p->next)
|
---|
278 | fn(p->name, p->comment, pdata);
|
---|
279 |
|
---|
280 | return;
|
---|
281 | }
|
---|
282 |
|
---|
283 | void pcap_printer_fn(void (*fn)(const char *, const char *, void *), void *pdata)
|
---|
284 | {
|
---|
285 | pcap_printer_fn_specific(pcap_cache, fn, pdata);
|
---|
286 | }
|
---|