forked from OpenPrinting/cups-filters
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimplicitclass.c
444 lines (395 loc) · 13.9 KB
/
implicitclass.c
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
/*
* implicitclass backend for implementing an implicit-class-like behavior
* of redundant print servers managed by cups-browsed.
*
* Copyright 2015-2019 by Till Kamppeter
* Copyright 2018-2019 by Deepak Patankar
*
* This is based on dnssd.c of CUPS
* dnssd.c copyright notice is follows:
*
* Copyright 2008-2015 by Apple Inc.
*
* These coded instructions, statements, and computer programs are the
* property of Apple Inc. and are protected by Federal copyright
* law. Distribution and use rights are outlined in the file "COPYING"
* which should have been included with this file.
*/
/*
* Include necessary headers.
*/
#include <config.h>
#include <cups/cups.h>
#include <cups/backend.h>
#include <cups/array.h>
#include <ctype.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <cupsfilters/filter.h>
#include <cupsfilters/ipp.h>
/*
* Local globals...
*/
/* IPP Attribute which cups-browsed uses to tell us the destination queue for
the current job */
#define CUPS_BROWSED_DEST_PRINTER "cups-browsed-dest-printer"
static int job_canceled = 0; /* Set to 1 on SIGTERM */
/*
* Local functions...
*/
static void sigterm_handler(int sig);
#if (CUPS_VERSION_MAJOR > 1) || (CUPS_VERSION_MINOR > 5)
#define HAVE_CUPS_1_6 1
#endif
#ifndef HAVE_CUPS_1_6
int
ippGetInteger(ipp_attribute_t *attr,
int element)
{
return (attr->values[element].integer);
}
#endif
int /* O - Next delay value */
next_delay(int current, /* I - Current delay value or 0 */
int *previous) /* IO - Previous delay value */
{
int next; /* Next delay value */
if (current > 0) {
next = (current + *previous) % 12;
*previous = next < current ? 0 : current;
} else {
next = 1;
*previous = 0;
}
return (next);
}
/*
* 'main()' - Browse for printers.
*/
int /* O - Exit status */
main(int argc, /* I - Number of command-line args */
char *argv[]) /* I - Command-line arguments */
{
const char *device_uri; /* URI with which we were called */
char scheme[64], username[32], queue_name[1024], resource[32],
printer_uri[1024],document_format[256],resolution[16];
int port, status;
const char *ptr1 = NULL;
char *ptr2,*ptr3,*ptr4;
const char *job_id;
int i;
char dest_host[1024]; /* Destination host */
ipp_t *request, *response;
ipp_attribute_t *attr;
char uri[HTTP_MAX_URI];
static const char *pattrs[] =
{
"printer-defaults"
};
#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
struct sigaction action; /* Actions for POSIX signals */
#endif /* HAVE_SIGACTION && !HAVE_SIGSET */
/*
* Don't buffer stderr, and catch SIGTERM...
*/
setbuf(stderr, NULL);
#ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
sigset(SIGTERM, sigterm_handler);
#elif defined(HAVE_SIGACTION)
memset(&action, 0, sizeof(action));
sigemptyset(&action.sa_mask);
action.sa_handler = sigterm_handler;
sigaction(SIGTERM, &action, NULL);
#else
signal(SIGTERM, sigterm_handler);
#endif /* HAVE_SIGSET */
/*
* Check command-line...
*/
if (argc >= 6) {
if ((device_uri = getenv("DEVICE_URI")) == NULL) {
if (!argv || !argv[0] || !strchr(argv[0], ':'))
return (-1);
device_uri = argv[0];
}
status = httpSeparateURI(HTTP_URI_CODING_ALL, device_uri,
scheme, sizeof(scheme),
username, sizeof(username),
queue_name, sizeof(queue_name),
&port,
resource, sizeof(resource));
if (status != HTTP_URI_STATUS_OK &&
status != HTTP_URI_STATUS_UNKNOWN_SCHEME) {
fprintf(stderr, "ERROR: Incorrect device URI syntax: %s\n",
device_uri);
return (CUPS_BACKEND_STOP);
}
httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
"localhost", ippPort(), "/printers/%s", queue_name);
job_id = argv[1];
for (i = 0; i < 120; i++) {
/* Wait up to 60 sec for cups-browsed to supply the destination host */
/* Try reading the option in which cups-browsed has deposited the
destination host */
request = ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL,
uri);
ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
"requested-attributes",
sizeof(pattrs) / sizeof(pattrs[0]),
NULL, pattrs);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
"requesting-user-name",
NULL, cupsUser());
if ((response = cupsDoRequest(CUPS_HTTP_DEFAULT, request, "/")) ==
NULL)
goto failed;
for (attr = ippFirstAttribute(response); attr != NULL;
attr = ippNextAttribute(response)) {
while (attr != NULL && ippGetGroupTag(attr) != IPP_TAG_PRINTER)
attr = ippNextAttribute(response);
if (attr == NULL)
break;
ptr1 = NULL;
while (attr != NULL && ippGetGroupTag(attr) ==
IPP_TAG_PRINTER) {
if (!strcmp(ippGetName(attr),
CUPS_BROWSED_DEST_PRINTER "-default"))
ptr1 = ippGetString(attr, 0, NULL);
if (ptr1 != NULL)
break;
attr = ippNextAttribute(response);
}
if (ptr1 != NULL)
break;
}
fprintf(stderr, "DEBUG: Read " CUPS_BROWSED_DEST_PRINTER " option: %s\n",
(ptr1 ? ptr1 : "Option not found"));
if (ptr1 == NULL)
goto failed;
/* Destination host is between double quotes, as double quotes are
illegal in host names one easily recognizes whether the option is
complete and avoids accepting a partially written host name */
if (*ptr1 != '"')
goto failed;
ptr1 ++;
/* Check whether option was set for this job, if not, keep waiting */
if (strncmp(ptr1, job_id, strlen(job_id)) != 0)
goto failed;
ptr1 += strlen(job_id);
if (*ptr1 != ' ')
goto failed;
ptr1 ++;
/* Read destination host name (or message) and check whether it is
complete (second double quote) */
if ((ptr2 = strchr(ptr1, '"')) != NULL) {
*ptr2 = '\0';
break;
}
failed:
/* Pause half a second before next attempt */
usleep(500000);
}
if (i >= 120) {
/* Timeout, no useful data from cups-browsed received */
fprintf(stderr, "ERROR: No destination host name supplied by cups-browsed for printer \"%s\", is cups-browsed running?\n",
queue_name);
return (CUPS_BACKEND_STOP);
}
strncpy(dest_host,ptr1,sizeof(dest_host) - 1);
if (!strcmp(dest_host, "NO_DEST_FOUND")) {
/* All remote queues are either disabled or not accepting jobs, let
CUPS retry after the usual interval */
fprintf(stderr, "ERROR: No suitable destination host found by cups-browsed.\n");
return (CUPS_BACKEND_RETRY);
} else if (!strcmp(dest_host, "ALL_DESTS_BUSY")) {
/* We queue on the client and all remote queues are busy, so we wait
5 sec and check again then */
fprintf(stderr, "DEBUG: No free destination host found by cups-browsed, retrying in 5 sec.\n");
sleep(5);
return (CUPS_BACKEND_RETRY_CURRENT);
} else {
/* We have the destination host name now, do the job */
char *title;
int num_options = 0;
cups_option_t *options = NULL;
int fd, nullfd;
filter_data_t filter_data;
filter_input_output_format_t input_output_format;
filter_external_cups_t ipp_backend_params;
filter_filter_in_chain_t universal_in_chain,
ipp_in_chain;
cups_array_t *filter_chain;
int retval;
fprintf(stderr, "DEBUG: Received destination host name from cups-browsed: printer-uri %s\n",
ptr1);
/* Parse the command line options and prepare them for the new print
job */
cupsSetUser(argv[2]);
title = argv[3];
if (title == NULL) {
if (argc == 7) {
if ((title = strrchr(argv[6], '/')) != NULL)
title ++;
else
title = argv[6];
} else
title = "(stdin)";
}
num_options = cupsAddOption("copies", argv[4], num_options, &options);
num_options = cupsParseOptions(argv[5], num_options, &options);
if (argc == 7)
fd = open(argv[6], O_RDONLY);
else
fd = 0; /* stdin */
/* Finding the document format in which the pdftoippprinter will
convert the pdf file */
if ((ptr3 = strchr(ptr1, ' ')) != NULL) {
*ptr3='\0';
ptr3++;
}
/* Finding the resolution requested for the job */
if ((ptr4 = strchr(ptr3, ' ')) != NULL) {
*ptr4='\0';
ptr4++;
}
strncpy(printer_uri, ptr1, sizeof(printer_uri) - 1);
strncpy(document_format, ptr3, sizeof(document_format) - 1);
strncpy(resolution, ptr4, sizeof(resolution) - 1);
fprintf(stderr,"DEBUG: Received job for the printer with the destination uri - %s, Final-document format for the printer - %s and requested resolution - %s\n",
printer_uri, document_format, resolution);
/* Adjust option list for the universal() filter function call */
num_options = cupsAddOption("Resolution", resolution,
num_options, &options);
num_options = cupsRemoveOption("cups-browsed-dest-printer",
num_options, &options);
num_options = cupsRemoveOption("cups-browsed",
num_options, &options);
/* Set up filter data record to be used by the filter functions to
process the job */
filter_data.printer = printer_uri;
filter_data.job_id = atoi(argv[1]);
filter_data.job_user = argv[2];
filter_data.job_title = title;
filter_data.copies = atoi(argv[4]);
filter_data.job_attrs = NULL; /* We use command line options */
if ((filter_data.printer_attrs =
get_printer_attributes4(printer_uri, NULL, 0, NULL, 0, 1, 0)) !=
NULL)
/* Poll the printer attributes from
the printer */
filter_data.ppdfile = NULL; /* We have successfully polled
the IPP attributes from the
printer. This is the most
precise printer capability info.
As the queue's PPD is only
for the cluster we prefer the
IPP attributes */
else
filter_data.ppdfile = getenv("PPD");/*The polling of the printer's
IPP attribute failed, meaning
that it is most probably not a
driverless IPP printers (IPP 2.x)
but a legacy IPP printer (IPP
1.x) which usually has
unsufficient capability info.
Therefore we fall back to the
PPD file here which contains
some info from the printer's
DNS-SD record. */
if (filter_data.ppdfile)
filter_data.ppd = ppdOpenFile(filter_data.ppdfile);
else
filter_data.ppd = NULL;
if (filter_data.printer_attrs == NULL && filter_data.ppd == NULL)
{
ippDelete(response);
fprintf(stderr, "ERROR: Unable to get sufficient capability info of the destination printer.\n");
return (CUPS_BACKEND_FAILED);
}
filter_data.num_options = num_options;
filter_data.options = options; /* Command line options from 5th
arg */
filter_data.back_pipe[0] = -1;
filter_data.back_pipe[1] = -1;
filter_data.side_pipe[0] = -1;
filter_data.side_pipe[1] = -1;
filter_data.logfunc = cups_logfunc; /* Logging scheme of CUPS */
filter_data.logdata = NULL;
filter_data.iscanceledfunc = cups_iscanceledfunc; /* Job-is-canceled
function */
filter_data.iscanceleddata = &job_canceled;
filterOpenBackAndSidePipes(&filter_data);
/* Parameters (input/output MIME types) for universal() call */
input_output_format.input_format = "application/vnd.cups-pdf";
input_output_format.output_format = document_format;
/* Parameters for filterExternalCUPS() call for IPP backend */
ipp_backend_params.filter = "ipp";
ipp_backend_params.is_backend = 1;
ipp_backend_params.device_uri = printer_uri;
ipp_backend_params.num_options = 0;
ipp_backend_params.options = NULL;
ipp_backend_params.envp = NULL;
/* Filter chain entry for the universal() filter function call */
universal_in_chain.function = universal;
universal_in_chain.parameters = &input_output_format;
universal_in_chain.name = "Filters";
/* Filter chain entry for the IPP CUPS backend call */
ipp_in_chain.function = filterExternalCUPS;
ipp_in_chain.parameters = &ipp_backend_params;
ipp_in_chain.name = "Backend";
filter_chain = cupsArrayNew(NULL, NULL);
cupsArrayAdd(filter_chain, &universal_in_chain);
cupsArrayAdd(filter_chain, &ipp_in_chain);
/* DEVICE_URI environment variable */
setenv("DEVICE_URI", printer_uri, 1);
/* FINAL_CONTENT_TYPE environment variable */
setenv("FINAL_CONTENT_TYPE", document_format, 1);
/* Mark the defaults and option settings in the PPD file */
if (filter_data.ppd)
{
ppdMarkDefaults(filter_data.ppd);
ppdMarkOptions(filter_data.ppd, num_options, options);
}
/* We call the IPP CUPS backend at the end of the chain, so we have
no output */
nullfd = open("/dev/null", O_WRONLY);
/* Call the filter chain to run the needed filters and the backend */
retval = filterChain(fd, nullfd, fd != 0 ? 1 : 0, &filter_data,
filter_chain);
filterCloseBackAndSidePipes(&filter_data);
/* Clean up */
cupsArrayDelete(filter_chain);
ippDelete(response);
if (retval) {
fprintf(stderr, "ERROR: Job processing failed.\n");
return (CUPS_BACKEND_FAILED);
}
}
} else if (argc != 1) {
fprintf(stderr,
"Usage: %s job-id user title copies options [file]\n",
argv[0]);
return (CUPS_BACKEND_FAILED);
}
/*
* No discovery mode at all for this backend
*/
return (CUPS_BACKEND_OK);
}
/*
* 'sigterm_handler()' - Handle termination signals.
*/
static void
sigterm_handler(int sig) /* I - Signal number (unused) */
{
(void)sig;
if (job_canceled)
_exit(CUPS_BACKEND_OK);
else
job_canceled = 1;
}