Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
srohatgi01
GitHub Repository: srohatgi01/cups
Path: blob/master/backend/ipp.c
1090 views
1
/*
2
* IPP backend for CUPS.
3
*
4
* Copyright © 2021-2022 by OpenPrinting
5
* Copyright © 2007-2021 by Apple Inc.
6
* Copyright © 1997-2007 by Easy Software Products, all rights reserved.
7
*
8
* Licensed under Apache License v2.0. See the file "LICENSE" for more
9
* information.
10
*/
11
12
/*
13
* Include necessary headers.
14
*/
15
16
#include "backend-private.h"
17
#include <cups/ppd-private.h>
18
#include <cups/array-private.h>
19
#include <sys/types.h>
20
#include <sys/stat.h>
21
#include <sys/wait.h>
22
#if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
23
# include <xpc/xpc.h>
24
# define kPMPrintUIToolAgent "com.apple.printuitool.agent"
25
# define kPMStartJob 100
26
# define kPMWaitForJob 101
27
extern void xpc_connection_set_target_uid(xpc_connection_t connection,
28
uid_t uid);
29
#endif /* HAVE_GSSAPI && HAVE_XPC */
30
31
32
/*
33
* Bits for job-state-reasons we care about...
34
*/
35
36
#define _CUPS_JSR_ACCOUNT_AUTHORIZATION_FAILED 0x01
37
#define _CUPS_JSR_ACCOUNT_CLOSED 0x02
38
#define _CUPS_JSR_ACCOUNT_INFO_NEEDED 0x04
39
#define _CUPS_JSR_ACCOUNT_LIMIT_REACHED 0x08
40
#define _CUPS_JSR_JOB_PASSWORD_WAIT 0x10
41
#define _CUPS_JSR_JOB_RELEASE_WAIT 0x20
42
#define _CUPS_JSR_DOCUMENT_FORMAT_ERROR 0x40
43
#define _CUPS_JSR_DOCUMENT_UNPRINTABLE 0x80
44
45
46
/*
47
* Types...
48
*/
49
50
typedef struct _cups_monitor_s /**** Monitoring data ****/
51
{
52
const char *uri, /* Printer URI */
53
*hostname, /* Hostname */
54
*user, /* Username */
55
*resource; /* Resource path */
56
int port, /* Port number */
57
version, /* IPP version */
58
job_id, /* Job ID for submitted job */
59
job_reasons, /* Job state reasons bits */
60
create_job, /* Support Create-Job? */
61
get_job_attrs; /* Support Get-Job-Attributes? */
62
const char *job_name; /* Job name for submitted job */
63
http_encryption_t encryption; /* Use encryption? */
64
ipp_jstate_t job_state; /* Current job state */
65
ipp_pstate_t printer_state; /* Current printer state */
66
int retryable; /* Is this a job that should be retried? */
67
} _cups_monitor_t;
68
69
70
/*
71
* Globals...
72
*/
73
#if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
74
static pid_t child_pid = 0; /* Child process ID */
75
#endif /* HAVE_GSSAPI && HAVE_XPC */
76
static const char * const jattrs[] = /* Job attributes we want */
77
{
78
"job-id",
79
"job-impressions-completed",
80
"job-media-sheets-completed",
81
"job-name",
82
"job-originating-user-name",
83
"job-state",
84
"job-state-reasons"
85
};
86
static int job_canceled = 0,
87
/* Job cancelled? */
88
uri_credentials = 0;
89
/* Credentials supplied in URI? */
90
static char username[256] = "",
91
/* Username for device URI */
92
*password = NULL;
93
/* Password for device URI */
94
static const char * const pattrs[] = /* Printer attributes we want */
95
{
96
#ifdef HAVE_LIBZ
97
"compression-supported",
98
#endif /* HAVE_LIBZ */
99
"copies-supported",
100
"cups-version",
101
"document-format-supported",
102
"job-password-encryption-supported",
103
"marker-colors",
104
"marker-high-levels",
105
"marker-levels",
106
"marker-low-levels",
107
"marker-message",
108
"marker-names",
109
"marker-types",
110
"media-col-supported",
111
"multiple-document-handling-supported",
112
"operations-supported",
113
"print-color-mode-supported",
114
"printer-alert",
115
"printer-alert-description",
116
"printer-is-accepting-jobs",
117
"printer-mandatory-job-attributes",
118
"printer-state",
119
"printer-state-message",
120
"printer-state-reasons"
121
};
122
static const char * const remote_job_states[] =
123
{ /* Remote job state keywords */
124
"+cups-remote-pending",
125
"+cups-remote-pending-held",
126
"+cups-remote-processing",
127
"+cups-remote-stopped",
128
"+cups-remote-canceled",
129
"+cups-remote-aborted",
130
"+cups-remote-completed"
131
};
132
static _cups_mutex_t report_mutex = _CUPS_MUTEX_INITIALIZER;
133
/* Mutex to control access */
134
static int num_attr_cache = 0;
135
/* Number of cached attributes */
136
static cups_option_t *attr_cache = NULL;
137
/* Cached attributes */
138
static cups_array_t *state_reasons; /* Array of printe-state-reasons keywords */
139
static char tmpfilename[1024] = "";
140
/* Temporary spool file name */
141
static char mandatory_attrs[1024] = "";
142
/* cupsMandatory value */
143
144
145
/*
146
* Local functions...
147
*/
148
149
static void cancel_job(http_t *http, const char *uri, int id,
150
const char *resource, const char *user,
151
int version);
152
static ipp_pstate_t check_printer_state(http_t *http, const char *uri,
153
const char *resource,
154
const char *user, int version);
155
static void debug_attributes(ipp_t *ipp);
156
static void *monitor_printer(_cups_monitor_t *monitor);
157
static ipp_t *new_request(ipp_op_t op, int version, const char *uri,
158
const char *user, const char *title,
159
int num_options, cups_option_t *options,
160
const char *compression, int copies,
161
const char *format, _ppd_cache_t *pc,
162
ppd_file_t *ppd,
163
ipp_attribute_t *media_col_sup,
164
ipp_attribute_t *doc_handling_sup,
165
ipp_attribute_t *print_color_mode_sup);
166
static const char *password_cb(const char *prompt, http_t *http,
167
const char *method, const char *resource,
168
int *user_data);
169
static const char *quote_string(const char *s, char *q, size_t qsize);
170
static void report_attr(ipp_attribute_t *attr);
171
static void report_printer_state(ipp_t *ipp);
172
#if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
173
static int run_as_user(char *argv[], uid_t uid,
174
const char *device_uri, int fd);
175
#endif /* HAVE_GSSAPI && HAVE_XPC */
176
static void sigterm_handler(int sig);
177
static int timeout_cb(http_t *http, void *user_data);
178
static void update_reasons(ipp_attribute_t *attr, const char *s);
179
180
181
/*
182
* 'main()' - Send a file to the printer or server.
183
*
184
* Usage:
185
*
186
* printer-uri job-id user title copies options [file]
187
*/
188
189
int /* O - Exit status */
190
main(int argc, /* I - Number of command-line args */
191
char *argv[]) /* I - Command-line arguments */
192
{
193
int i; /* Looping var */
194
int send_options; /* Send job options? */
195
int num_options; /* Number of printer options */
196
cups_option_t *options; /* Printer options */
197
const char *device_uri; /* Device URI */
198
char scheme[255], /* Scheme in URI */
199
hostname[1024], /* Hostname */
200
resource[1024], /* Resource info (printer name) */
201
addrname[256], /* Address name */
202
*optptr, /* Pointer to URI options */
203
*name, /* Name of option */
204
*value, /* Value of option */
205
sep; /* Separator character */
206
int password_tries = 0; /* Password tries */
207
http_addrlist_t *addrlist; /* Address of printer */
208
int snmp_enabled = 1; /* Is SNMP enabled? */
209
int snmp_fd, /* SNMP socket */
210
start_count, /* Page count via SNMP at start */
211
page_count, /* Page count via SNMP */
212
have_supplies; /* Printer supports supply levels? */
213
int num_files; /* Number of files to print */
214
char **files, /* Files to print */
215
*compatfile = NULL; /* Compatibility filename */
216
off_t compatsize = 0; /* Size of compatibility file */
217
int port; /* Port number (not used) */
218
char uri[HTTP_MAX_URI]; /* Updated URI without user/pass */
219
char print_job_name[1024]; /* Update job-name for Print-Job */
220
http_status_t http_status; /* Status of HTTP request */
221
ipp_status_t ipp_status; /* Status of IPP request */
222
http_t *http; /* HTTP connection */
223
ipp_t *request, /* IPP request */
224
*response, /* IPP response */
225
*supported; /* get-printer-attributes response */
226
time_t start_time; /* Time of first connect */
227
int contimeout; /* Connection timeout */
228
int delay, /* Delay for retries */
229
prev_delay; /* Previous delay */
230
const char *compression; /* Compression mode */
231
int waitjob, /* Wait for job complete? */
232
waitjob_tries = 0, /* Number of times we've waited */
233
waitprinter; /* Wait for printer ready? */
234
_cups_monitor_t monitor; /* Monitoring data */
235
ipp_attribute_t *job_id_attr; /* job-id attribute */
236
int job_id; /* job-id value */
237
ipp_attribute_t *job_sheets; /* job-media-sheets-completed */
238
ipp_attribute_t *job_state; /* job-state */
239
#ifdef HAVE_LIBZ
240
ipp_attribute_t *compression_sup; /* compression-supported */
241
#endif /* HAVE_LIBZ */
242
ipp_attribute_t *copies_sup; /* copies-supported */
243
ipp_attribute_t *cups_version; /* cups-version */
244
ipp_attribute_t *encryption_sup; /* job-password-encryption-supported */
245
ipp_attribute_t *format_sup; /* document-format-supported */
246
ipp_attribute_t *job_auth; /* job-authorization-uri */
247
ipp_attribute_t *media_col_sup; /* media-col-supported */
248
ipp_attribute_t *operations_sup; /* operations-supported */
249
ipp_attribute_t *doc_handling_sup; /* multiple-document-handling-supported */
250
ipp_attribute_t *printer_state; /* printer-state attribute */
251
ipp_attribute_t *printer_accepting; /* printer-is-accepting-jobs */
252
ipp_attribute_t *print_color_mode_sup;/* Does printer support print-color-mode? */
253
int create_job = 0, /* Does printer support Create-Job? */
254
get_job_attrs = 0, /* Does printer support Get-Job-Attributes? */
255
send_document = 0, /* Does printer support Send-Document? */
256
validate_job = 0, /* Does printer support Validate-Job? */
257
validate_retried = 0, /* Was Validate-Job request retried? */
258
copies, /* Number of copies for job */
259
copies_remaining; /* Number of copies remaining */
260
const char *auth_info_required, /* New auth-info-required value */
261
*content_type, /* CONTENT_TYPE environment variable */
262
*final_content_type, /* FINAL_CONTENT_TYPE environment var */
263
*document_format; /* document-format value */
264
int fd; /* File descriptor */
265
off_t bytes = 0; /* Bytes copied */
266
char buffer[16384]; /* Copy buffer */
267
#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
268
struct sigaction action; /* Actions for POSIX signals */
269
#endif /* HAVE_SIGACTION && !HAVE_SIGSET */
270
int version; /* IPP version */
271
ppd_file_t *ppd = NULL; /* PPD file */
272
_ppd_cache_t *pc = NULL; /* PPD cache and mapping data */
273
fd_set input; /* Input set for select() */
274
275
276
/*
277
* Make sure status messages are not buffered...
278
*/
279
280
setbuf(stderr, NULL);
281
282
/*
283
* Ignore SIGPIPE and catch SIGTERM signals...
284
*/
285
286
#ifdef HAVE_SIGSET
287
sigset(SIGPIPE, SIG_IGN);
288
sigset(SIGTERM, sigterm_handler);
289
#elif defined(HAVE_SIGACTION)
290
memset(&action, 0, sizeof(action));
291
action.sa_handler = SIG_IGN;
292
sigaction(SIGPIPE, &action, NULL);
293
294
sigemptyset(&action.sa_mask);
295
sigaddset(&action.sa_mask, SIGTERM);
296
action.sa_handler = sigterm_handler;
297
sigaction(SIGTERM, &action, NULL);
298
#else
299
signal(SIGPIPE, SIG_IGN);
300
signal(SIGTERM, sigterm_handler);
301
#endif /* HAVE_SIGSET */
302
303
/*
304
* Check command-line...
305
*/
306
307
if (argc == 1)
308
{
309
char *s;
310
311
if ((s = strrchr(argv[0], '/')) != NULL)
312
s ++;
313
else
314
s = argv[0];
315
316
printf("network %s \"Unknown\" \"%s (%s)\"\n",
317
s, _cupsLangString(cupsLangDefault(),
318
_("Internet Printing Protocol")), s);
319
return (CUPS_BACKEND_OK);
320
}
321
else if (argc < 6)
322
{
323
_cupsLangPrintf(stderr,
324
_("Usage: %s job-id user title copies options [file]"),
325
argv[0]);
326
return (CUPS_BACKEND_STOP);
327
}
328
329
/*
330
* Get the device URI...
331
*/
332
333
while ((device_uri = cupsBackendDeviceURI(argv)) == NULL)
334
{
335
_cupsLangPrintFilter(stderr, "INFO", _("Unable to locate printer."));
336
sleep(10);
337
338
if (getenv("CLASS") != NULL)
339
return (CUPS_BACKEND_FAILED);
340
}
341
342
if ((auth_info_required = getenv("AUTH_INFO_REQUIRED")) == NULL)
343
auth_info_required = "none";
344
345
state_reasons = _cupsArrayNewStrings(getenv("PRINTER_STATE_REASONS"), ',');
346
347
#ifdef HAVE_GSSAPI
348
/*
349
* For Kerberos, become the printing user (if we can) to get the credentials
350
* that way.
351
*/
352
353
if (!getuid() && (value = getenv("AUTH_UID")) != NULL)
354
{
355
uid_t uid = (uid_t)strtoul(value, NULL, 10);
356
/* User ID */
357
358
# ifdef HAVE_XPC
359
if (uid)
360
{
361
if (argc == 6)
362
return (run_as_user(argv, uid, device_uri, 0));
363
else
364
{
365
int status = 0; /* Exit status */
366
367
for (i = 6; i < argc && !status && !job_canceled; i ++)
368
{
369
if ((fd = open(argv[i], O_RDONLY)) >= 0)
370
{
371
status = run_as_user(argv, uid, device_uri, fd);
372
close(fd);
373
}
374
else
375
{
376
_cupsLangPrintError("ERROR", _("Unable to open print file"));
377
status = CUPS_BACKEND_FAILED;
378
}
379
}
380
381
return (status);
382
}
383
}
384
385
# else /* No XPC, just try to run as the user ID */
386
if (uid)
387
setuid(uid);
388
# endif /* HAVE_XPC */
389
}
390
#endif /* HAVE_GSSAPI */
391
392
/*
393
* Get the (final) content type...
394
*/
395
396
if ((content_type = getenv("CONTENT_TYPE")) == NULL)
397
content_type = "application/octet-stream";
398
399
if ((final_content_type = getenv("FINAL_CONTENT_TYPE")) == NULL)
400
{
401
final_content_type = content_type;
402
403
if (!strncmp(final_content_type, "printer/", 8))
404
final_content_type = "application/vnd.cups-raw";
405
}
406
407
/*
408
* Extract the hostname and printer name from the URI...
409
*/
410
411
httpSeparateURI(HTTP_URI_CODING_ALL, device_uri, scheme, sizeof(scheme),
412
username, sizeof(username), hostname, sizeof(hostname), &port,
413
resource, sizeof(resource));
414
415
if (!port)
416
port = IPP_PORT; /* Default to port 631 */
417
418
if (!strcmp(scheme, "https") || !strcmp(scheme, "ipps"))
419
cupsSetEncryption(HTTP_ENCRYPTION_ALWAYS);
420
else
421
cupsSetEncryption(HTTP_ENCRYPTION_IF_REQUESTED);
422
423
/*
424
* See if there are any options...
425
*/
426
427
compression = NULL;
428
version = 20;
429
waitjob = 1;
430
waitprinter = 1;
431
contimeout = 7 * 24 * 60 * 60;
432
433
if ((optptr = strchr(resource, '?')) != NULL)
434
{
435
/*
436
* Yup, terminate the device name string and move to the first
437
* character of the optptr...
438
*/
439
440
*optptr++ = '\0';
441
442
/*
443
* Then parse the optptr...
444
*/
445
446
while (*optptr)
447
{
448
/*
449
* Get the name...
450
*/
451
452
name = optptr;
453
454
while (*optptr && *optptr != '=' && *optptr != '+' && *optptr != '&')
455
optptr ++;
456
457
if ((sep = *optptr) != '\0')
458
*optptr++ = '\0';
459
460
if (sep == '=')
461
{
462
/*
463
* Get the value...
464
*/
465
466
value = optptr;
467
468
while (*optptr && *optptr != '+' && *optptr != '&')
469
optptr ++;
470
471
if (*optptr)
472
*optptr++ = '\0';
473
}
474
else
475
value = (char *)"";
476
477
/*
478
* Process the option...
479
*/
480
481
if (!_cups_strcasecmp(name, "waitjob"))
482
{
483
/*
484
* Wait for job completion?
485
*/
486
487
waitjob = !_cups_strcasecmp(value, "on") ||
488
!_cups_strcasecmp(value, "yes") ||
489
!_cups_strcasecmp(value, "true");
490
}
491
else if (!_cups_strcasecmp(name, "waitprinter"))
492
{
493
/*
494
* Wait for printer idle?
495
*/
496
497
waitprinter = !_cups_strcasecmp(value, "on") ||
498
!_cups_strcasecmp(value, "yes") ||
499
!_cups_strcasecmp(value, "true");
500
}
501
else if (!_cups_strcasecmp(name, "encryption"))
502
{
503
/*
504
* Enable/disable encryption?
505
*/
506
507
if (!_cups_strcasecmp(value, "always"))
508
cupsSetEncryption(HTTP_ENCRYPTION_ALWAYS);
509
else if (!_cups_strcasecmp(value, "required"))
510
cupsSetEncryption(HTTP_ENCRYPTION_REQUIRED);
511
else if (!_cups_strcasecmp(value, "never"))
512
cupsSetEncryption(HTTP_ENCRYPTION_NEVER);
513
else if (!_cups_strcasecmp(value, "ifrequested"))
514
cupsSetEncryption(HTTP_ENCRYPTION_IF_REQUESTED);
515
else
516
{
517
_cupsLangPrintFilter(stderr, "ERROR",
518
_("Unknown encryption option value: \"%s\"."),
519
value);
520
}
521
}
522
else if (!_cups_strcasecmp(name, "snmp"))
523
{
524
/*
525
* Enable/disable SNMP stuff...
526
*/
527
528
snmp_enabled = !value[0] || !_cups_strcasecmp(value, "on") ||
529
!_cups_strcasecmp(value, "yes") ||
530
!_cups_strcasecmp(value, "true");
531
}
532
else if (!_cups_strcasecmp(name, "version"))
533
{
534
if (!strcmp(value, "1.0"))
535
version = 10;
536
else if (!strcmp(value, "1.1"))
537
version = 11;
538
else if (!strcmp(value, "2.0"))
539
version = 20;
540
else if (!strcmp(value, "2.1"))
541
version = 21;
542
else if (!strcmp(value, "2.2"))
543
version = 22;
544
else
545
{
546
_cupsLangPrintFilter(stderr, "ERROR",
547
_("Unknown version option value: \"%s\"."),
548
value);
549
}
550
}
551
#ifdef HAVE_LIBZ
552
else if (!_cups_strcasecmp(name, "compression"))
553
{
554
if (!_cups_strcasecmp(value, "true") || !_cups_strcasecmp(value, "yes") ||
555
!_cups_strcasecmp(value, "on") || !_cups_strcasecmp(value, "gzip"))
556
compression = "gzip";
557
else if (!_cups_strcasecmp(value, "deflate"))
558
compression = "deflate";
559
else if (!_cups_strcasecmp(value, "false") ||
560
!_cups_strcasecmp(value, "no") ||
561
!_cups_strcasecmp(value, "off") ||
562
!_cups_strcasecmp(value, "none"))
563
compression = "none";
564
}
565
#endif /* HAVE_LIBZ */
566
else if (!_cups_strcasecmp(name, "contimeout"))
567
{
568
int value_int = atoi(value);
569
/*
570
* Set the connection timeout...
571
*/
572
573
if (value_int > 0)
574
contimeout = value_int;
575
}
576
else
577
{
578
/*
579
* Unknown option...
580
*/
581
582
_cupsLangPrintFilter(stderr, "ERROR",
583
_("Unknown option \"%s\" with value \"%s\"."),
584
name, value);
585
}
586
}
587
}
588
589
/*
590
* If we have 7 arguments, print the file named on the command-line.
591
* Otherwise, copy stdin to a temporary file and print the temporary
592
* file.
593
*/
594
595
if (argc == 6)
596
{
597
num_files = 0;
598
files = NULL;
599
send_options = !_cups_strcasecmp(final_content_type, "application/pdf") ||
600
!_cups_strcasecmp(final_content_type, "application/vnd.cups-pdf") ||
601
!_cups_strncasecmp(final_content_type, "image/", 6);
602
603
fputs("DEBUG: Sending stdin for job...\n", stderr);
604
}
605
else
606
{
607
/*
608
* Point to the files on the command-line...
609
*/
610
611
num_files = argc - 6;
612
files = argv + 6;
613
send_options = 1;
614
615
fprintf(stderr, "DEBUG: %d files to send in job...\n", num_files);
616
}
617
618
/*
619
* Set the authentication info, if any...
620
*/
621
622
cupsSetPasswordCB2((cups_password_cb2_t)password_cb, &password_tries);
623
624
if (username[0])
625
{
626
/*
627
* Use authentication information in the device URI...
628
*/
629
630
if ((password = strchr(username, ':')) != NULL)
631
*password++ = '\0';
632
633
cupsSetUser(username);
634
uri_credentials = 1;
635
}
636
else
637
{
638
/*
639
* Try loading authentication information from the environment.
640
*/
641
642
const char *ptr = getenv("AUTH_USERNAME");
643
644
if (ptr)
645
{
646
strlcpy(username, ptr, sizeof(username));
647
cupsSetUser(ptr);
648
}
649
650
password = getenv("AUTH_PASSWORD");
651
}
652
653
/*
654
* Try finding the remote server...
655
*/
656
657
start_time = time(NULL);
658
659
addrlist = backendLookup(hostname, port, &job_canceled);
660
661
http = httpConnect2(hostname, port, addrlist, AF_UNSPEC, cupsEncryption(), 1,
662
0, NULL);
663
httpSetTimeout(http, 30.0, timeout_cb, NULL);
664
665
/*
666
* See if the printer supports SNMP...
667
*/
668
669
if (snmp_enabled)
670
snmp_fd = _cupsSNMPOpen(addrlist->addr.addr.sa_family);
671
else
672
snmp_fd = -1;
673
674
if (snmp_fd >= 0)
675
have_supplies = !backendSNMPSupplies(snmp_fd, &(addrlist->addr),
676
&start_count, NULL);
677
else
678
have_supplies = start_count = 0;
679
680
/*
681
* Wait for data from the filter...
682
*/
683
684
if (num_files == 0)
685
{
686
if (!backendWaitLoop(snmp_fd, &(addrlist->addr), 0, backendNetworkSideCB))
687
return (CUPS_BACKEND_OK);
688
else if ((bytes = read(0, buffer, sizeof(buffer))) <= 0)
689
return (CUPS_BACKEND_OK);
690
}
691
692
/*
693
* Try connecting to the remote server...
694
*/
695
696
delay = _cupsNextDelay(0, &prev_delay);
697
698
do
699
{
700
fprintf(stderr, "DEBUG: Connecting to %s:%d\n", hostname, port);
701
_cupsLangPrintFilter(stderr, "INFO", _("Connecting to printer."));
702
703
if (httpReconnect2(http, 30000, NULL))
704
{
705
int error = errno; /* Connection error */
706
707
if (http->status == HTTP_STATUS_CUPS_PKI_ERROR)
708
update_reasons(NULL, "+cups-certificate-error");
709
710
if (job_canceled)
711
break;
712
713
if (getenv("CLASS") != NULL)
714
{
715
/*
716
* If the CLASS environment variable is set, the job was submitted
717
* to a class and not to a specific queue. In this case, we want
718
* to abort immediately so that the job can be requeued on the next
719
* available printer in the class.
720
*/
721
722
_cupsLangPrintFilter(stderr, "INFO",
723
_("Unable to contact printer, queuing on next "
724
"printer in class."));
725
726
/*
727
* Sleep 5 seconds to keep the job from requeuing too rapidly...
728
*/
729
730
sleep(5);
731
732
update_reasons(NULL, "-connecting-to-device");
733
734
return (CUPS_BACKEND_FAILED);
735
}
736
737
fprintf(stderr, "DEBUG: Connection error: %s\n", strerror(errno));
738
739
if (errno == ECONNREFUSED || errno == EHOSTDOWN || errno == EHOSTUNREACH || errno == ETIMEDOUT || errno == ENOTCONN)
740
{
741
if (contimeout && (time(NULL) - start_time) > contimeout)
742
{
743
_cupsLangPrintFilter(stderr, "ERROR",
744
_("The printer is not responding."));
745
update_reasons(NULL, "-connecting-to-device");
746
return (CUPS_BACKEND_FAILED);
747
}
748
749
switch (error)
750
{
751
case EHOSTDOWN :
752
_cupsLangPrintFilter(stderr, "WARNING",
753
_("The printer may not exist or "
754
"is unavailable at this time."));
755
break;
756
757
case EHOSTUNREACH :
758
default :
759
_cupsLangPrintFilter(stderr, "WARNING",
760
_("The printer is unreachable at this "
761
"time."));
762
break;
763
764
case ECONNREFUSED :
765
_cupsLangPrintFilter(stderr, "WARNING",
766
_("The printer is in use."));
767
break;
768
}
769
770
sleep((unsigned)delay);
771
772
delay = _cupsNextDelay(delay, &prev_delay);
773
}
774
else
775
{
776
_cupsLangPrintFilter(stderr, "ERROR",
777
_("The printer is not responding."));
778
sleep(30);
779
}
780
781
if (job_canceled)
782
break;
783
}
784
else
785
update_reasons(NULL, "-cups-certificate-error");
786
}
787
while (http->fd < 0);
788
789
if (job_canceled)
790
return (CUPS_BACKEND_OK);
791
792
if (httpIsEncrypted(http))
793
{
794
/*
795
* Validate TLS credentials...
796
*/
797
798
cups_array_t *creds; /* TLS credentials */
799
cups_array_t *lcreds = NULL; /* Loaded credentials */
800
http_trust_t trust; /* Trust level */
801
char credinfo[1024], /* Information on credentials */
802
lcredinfo[1024];/* Information on saved credentials */
803
static const char * const trusts[] = { NULL, "+cups-pki-invalid", "+cups-pki-changed", "+cups-pki-expired", NULL, "+cups-pki-unknown" };
804
/* Trust keywords */
805
static const char * const trust_msgs[] =
806
{
807
"Credentials are OK/trusted",
808
"Credentials are invalid",
809
"Credentials have changed",
810
"Credentials are expired",
811
"Credentials have been renewed",
812
"Credentials are unknown/new"
813
};
814
815
fputs("DEBUG: Connection is encrypted.\n", stderr);
816
817
if (!httpCopyCredentials(http, &creds))
818
{
819
trust = httpCredentialsGetTrust(creds, hostname);
820
httpCredentialsString(creds, credinfo, sizeof(credinfo));
821
822
fprintf(stderr, "DEBUG: %s (%s)\n", trust_msgs[trust], cupsLastErrorString());
823
fprintf(stderr, "DEBUG: Printer credentials: %s\n", credinfo);
824
825
if (!httpLoadCredentials(NULL, &lcreds, hostname))
826
{
827
httpCredentialsString(lcreds, lcredinfo, sizeof(lcredinfo));
828
fprintf(stderr, "DEBUG: Stored credentials: %s\n", lcredinfo);
829
}
830
else
831
fputs("DEBUG: No stored credentials.\n", stderr);
832
833
update_reasons(NULL, "-cups-pki-invalid,cups-pki-changed,cups-pki-expired,cups-pki-unknown");
834
if (trusts[trust])
835
{
836
update_reasons(NULL, trusts[trust]);
837
return (CUPS_BACKEND_STOP);
838
}
839
840
if (!lcreds)
841
{
842
/*
843
* Could not load the credentials, let's save the ones we have so we
844
* can detect changes...
845
*/
846
847
httpSaveCredentials(NULL, creds, hostname);
848
}
849
850
httpFreeCredentials(lcreds);
851
httpFreeCredentials(creds);
852
}
853
else
854
{
855
fputs("DEBUG: No printer credentials.\n", stderr);
856
857
update_reasons(NULL, "cups-pki-unknown");
858
return (CUPS_BACKEND_STOP);
859
}
860
}
861
862
update_reasons(NULL, "-connecting-to-device");
863
_cupsLangPrintFilter(stderr, "INFO", _("Connected to printer."));
864
865
fprintf(stderr, "DEBUG: Connected to %s:%d...\n",
866
httpAddrString(http->hostaddr, addrname, sizeof(addrname)),
867
httpAddrPort(http->hostaddr));
868
869
/*
870
* Build a URI for the printer and fill the standard IPP attributes for
871
* an IPP_PRINT_FILE request. We can't use the URI in argv[0] because it
872
* might contain username:password information...
873
*/
874
875
httpAssembleURI(HTTP_URI_CODING_ALL, uri, sizeof(uri), scheme, NULL, hostname,
876
port, resource);
877
878
/*
879
* First validate the destination and see if the device supports multiple
880
* copies...
881
*/
882
883
#ifdef HAVE_LIBZ
884
compression_sup = NULL;
885
#endif /* HAVE_LIBZ */
886
copies_sup = NULL;
887
cups_version = NULL;
888
encryption_sup = NULL;
889
format_sup = NULL;
890
media_col_sup = NULL;
891
supported = NULL;
892
operations_sup = NULL;
893
doc_handling_sup = NULL;
894
print_color_mode_sup = NULL;
895
896
do
897
{
898
/*
899
* Check for side-channel requests...
900
*/
901
902
backendCheckSideChannel(snmp_fd, http->hostaddr);
903
904
/*
905
* Build the IPP request...
906
*/
907
908
request = ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES);
909
ippSetVersion(request, version / 10, version % 10);
910
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
911
NULL, uri);
912
913
ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
914
"requested-attributes", sizeof(pattrs) / sizeof(pattrs[0]),
915
NULL, pattrs);
916
917
/*
918
* Do the request...
919
*/
920
921
fputs("DEBUG: Getting supported attributes...\n", stderr);
922
923
if (http->version < HTTP_VERSION_1_1)
924
{
925
fprintf(stderr, "DEBUG: Printer responded with HTTP version %d.%d.\n",
926
http->version / 100, http->version % 100);
927
update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
928
"cups-ipp-wrong-http-version");
929
}
930
931
supported = cupsDoRequest(http, request, resource);
932
ipp_status = cupsLastError();
933
934
fprintf(stderr, "DEBUG: Get-Printer-Attributes: %s (%s)\n",
935
ippErrorString(ipp_status), cupsLastErrorString());
936
937
if (ipp_status <= IPP_STATUS_OK_CONFLICTING)
938
password_tries = 0;
939
else
940
{
941
fprintf(stderr, "DEBUG: Get-Printer-Attributes returned %s.\n",
942
ippErrorString(ipp_status));
943
944
if (ipp_status == IPP_STATUS_ERROR_BUSY ||
945
ipp_status == IPP_STATUS_ERROR_SERVICE_UNAVAILABLE)
946
{
947
if (contimeout && (time(NULL) - start_time) > contimeout)
948
{
949
_cupsLangPrintFilter(stderr, "ERROR",
950
_("The printer is not responding."));
951
return (CUPS_BACKEND_FAILED);
952
}
953
954
_cupsLangPrintFilter(stderr, "INFO", _("The printer is in use."));
955
956
report_printer_state(supported);
957
958
sleep((unsigned)delay);
959
960
delay = _cupsNextDelay(delay, &prev_delay);
961
}
962
else if ((ipp_status == IPP_STATUS_ERROR_BAD_REQUEST ||
963
ipp_status == IPP_STATUS_ERROR_VERSION_NOT_SUPPORTED) && version > 10)
964
{
965
/*
966
* Switch to IPP/1.1 or IPP/1.0...
967
*/
968
969
if (version >= 20)
970
{
971
_cupsLangPrintFilter(stderr, "INFO", _("Preparing to print."));
972
fprintf(stderr,
973
"DEBUG: The printer does not support IPP/%d.%d, trying "
974
"IPP/1.1.\n", version / 10, version % 10);
975
version = 11;
976
}
977
else
978
{
979
_cupsLangPrintFilter(stderr, "INFO", _("Preparing to print."));
980
fprintf(stderr,
981
"DEBUG: The printer does not support IPP/%d.%d, trying "
982
"IPP/1.0.\n", version / 10, version % 10);
983
version = 10;
984
}
985
986
httpReconnect2(http, 30000, NULL);
987
}
988
else if (ipp_status == IPP_STATUS_ERROR_NOT_FOUND)
989
{
990
_cupsLangPrintFilter(stderr, "ERROR",
991
_("The printer configuration is incorrect or the "
992
"printer no longer exists."));
993
994
ippDelete(supported);
995
996
return (CUPS_BACKEND_STOP);
997
}
998
else if (ipp_status == IPP_STATUS_ERROR_FORBIDDEN ||
999
ipp_status == IPP_STATUS_ERROR_CUPS_AUTHENTICATION_CANCELED)
1000
{
1001
const char *www_auth = httpGetField(http, HTTP_FIELD_WWW_AUTHENTICATE);
1002
/* WWW-Authenticate field value */
1003
1004
if (!strncmp(www_auth, "Negotiate", 9))
1005
auth_info_required = "negotiate";
1006
else if (www_auth[0])
1007
auth_info_required = "username,password";
1008
1009
fprintf(stderr, "ATTR: auth-info-required=%s\n", auth_info_required);
1010
return (CUPS_BACKEND_AUTH_REQUIRED);
1011
}
1012
else if (ipp_status != IPP_STATUS_ERROR_NOT_AUTHORIZED)
1013
{
1014
_cupsLangPrintFilter(stderr, "ERROR",
1015
_("Unable to get printer status."));
1016
sleep(10);
1017
1018
httpReconnect2(http, 30000, NULL);
1019
}
1020
1021
ippDelete(supported);
1022
supported = NULL;
1023
continue;
1024
}
1025
1026
if (!getenv("CLASS"))
1027
{
1028
/*
1029
* Check printer-is-accepting-jobs = false and printer-state-reasons for the
1030
* "spool-area-full" keyword...
1031
*/
1032
1033
int busy = 0;
1034
1035
if ((printer_accepting = ippFindAttribute(supported,
1036
"printer-is-accepting-jobs",
1037
IPP_TAG_BOOLEAN)) != NULL &&
1038
!printer_accepting->values[0].boolean)
1039
busy = 1;
1040
else if (!printer_accepting)
1041
update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
1042
"cups-ipp-missing-printer-is-accepting-jobs");
1043
1044
if ((printer_state = ippFindAttribute(supported,
1045
"printer-state-reasons",
1046
IPP_TAG_KEYWORD)) == NULL)
1047
{
1048
update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
1049
"cups-ipp-missing-printer-state-reasons");
1050
}
1051
else if (!busy)
1052
{
1053
for (i = 0; i < printer_state->num_values; i ++)
1054
{
1055
if (!strcmp(printer_state->values[0].string.text,
1056
"spool-area-full") ||
1057
!strncmp(printer_state->values[0].string.text, "spool-area-full-",
1058
16))
1059
{
1060
busy = 1;
1061
break;
1062
}
1063
}
1064
}
1065
1066
if (busy)
1067
{
1068
_cupsLangPrintFilter(stderr, "INFO", _("The printer is in use."));
1069
1070
report_printer_state(supported);
1071
1072
sleep((unsigned)delay);
1073
1074
delay = _cupsNextDelay(delay, &prev_delay);
1075
1076
ippDelete(supported);
1077
supported = NULL;
1078
ipp_status = IPP_STATUS_ERROR_BUSY;
1079
continue;
1080
}
1081
}
1082
1083
/*
1084
* Check for supported attributes...
1085
*/
1086
1087
#ifdef HAVE_LIBZ
1088
if ((compression_sup = ippFindAttribute(supported, "compression-supported",
1089
IPP_TAG_KEYWORD)) != NULL)
1090
{
1091
/*
1092
* Check whether the requested compression is supported and/or default to
1093
* compression if supported...
1094
*/
1095
1096
if (compression && !ippContainsString(compression_sup, compression))
1097
{
1098
fprintf(stderr, "DEBUG: Printer does not support the requested "
1099
"compression value \"%s\".\n", compression);
1100
compression = NULL;
1101
}
1102
else if (!compression && (!strcmp(final_content_type, "image/pwg-raster") || !strcmp(final_content_type, "image/urf")))
1103
{
1104
if (ippContainsString(compression_sup, "gzip"))
1105
compression = "gzip";
1106
else if (ippContainsString(compression_sup, "deflate"))
1107
compression = "deflate";
1108
1109
if (compression)
1110
fprintf(stderr, "DEBUG: Automatically using \"%s\" compression.\n",
1111
compression);
1112
}
1113
}
1114
#endif /* HAVE_LIBZ */
1115
1116
if ((copies_sup = ippFindAttribute(supported, "copies-supported",
1117
IPP_TAG_RANGE)) != NULL)
1118
{
1119
/*
1120
* Has the "copies-supported" attribute - does it have an upper
1121
* bound > 1?
1122
*/
1123
1124
fprintf(stderr, "DEBUG: copies-supported=%d-%d\n",
1125
copies_sup->values[0].range.lower,
1126
copies_sup->values[0].range.upper);
1127
1128
if (copies_sup->values[0].range.upper <= 1)
1129
copies_sup = NULL; /* No */
1130
}
1131
1132
if ((cups_version = ippFindAttribute(supported, "cups-version", IPP_TAG_TEXT)) != NULL)
1133
{
1134
const char *val = ippGetString(cups_version, 0, NULL);
1135
1136
fprintf(stderr, "DEBUG: cups-version = \"%s\"\n", val);
1137
if (!strcmp(val, "cups-version"))
1138
cups_version = NULL; /* Bogus cups-version value returned by buggy printers! */
1139
}
1140
1141
encryption_sup = ippFindAttribute(supported, "job-password-encryption-supported", IPP_TAG_KEYWORD);
1142
1143
if ((format_sup = ippFindAttribute(supported, "document-format-supported",
1144
IPP_TAG_MIMETYPE)) != NULL)
1145
{
1146
fprintf(stderr, "DEBUG: document-format-supported (%d values)\n",
1147
format_sup->num_values);
1148
for (i = 0; i < format_sup->num_values; i ++)
1149
fprintf(stderr, "DEBUG: [%d] = \"%s\"\n", i,
1150
format_sup->values[i].string.text);
1151
}
1152
1153
if ((media_col_sup = ippFindAttribute(supported, "media-col-supported",
1154
IPP_TAG_KEYWORD)) != NULL)
1155
{
1156
fprintf(stderr, "DEBUG: media-col-supported (%d values)\n",
1157
media_col_sup->num_values);
1158
for (i = 0; i < media_col_sup->num_values; i ++)
1159
fprintf(stderr, "DEBUG: [%d] = \"%s\"\n", i,
1160
media_col_sup->values[i].string.text);
1161
}
1162
1163
print_color_mode_sup = ippFindAttribute(supported, "print-color-mode-supported", IPP_TAG_KEYWORD);
1164
1165
if ((operations_sup = ippFindAttribute(supported, "operations-supported",
1166
IPP_TAG_ENUM)) != NULL)
1167
{
1168
fprintf(stderr, "DEBUG: operations-supported (%d values)\n",
1169
operations_sup->num_values);
1170
for (i = 0; i < operations_sup->num_values; i ++)
1171
fprintf(stderr, "DEBUG: [%d] = %s\n", i,
1172
ippOpString(operations_sup->values[i].integer));
1173
1174
for (i = 0; i < operations_sup->num_values; i ++)
1175
if (operations_sup->values[i].integer == IPP_OP_PRINT_JOB)
1176
break;
1177
1178
if (i >= operations_sup->num_values)
1179
update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
1180
"cups-ipp-missing-print-job");
1181
1182
for (i = 0; i < operations_sup->num_values; i ++)
1183
if (operations_sup->values[i].integer == IPP_OP_CANCEL_JOB)
1184
break;
1185
1186
if (i >= operations_sup->num_values)
1187
update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
1188
"cups-ipp-missing-cancel-job");
1189
1190
for (i = 0; i < operations_sup->num_values; i ++)
1191
if (operations_sup->values[i].integer == IPP_OP_GET_JOB_ATTRIBUTES)
1192
break;
1193
1194
if (i >= operations_sup->num_values)
1195
update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
1196
"cups-ipp-missing-get-job-attributes");
1197
1198
for (i = 0; i < operations_sup->num_values; i ++)
1199
if (operations_sup->values[i].integer == IPP_OP_GET_PRINTER_ATTRIBUTES)
1200
break;
1201
1202
if (i >= operations_sup->num_values)
1203
update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
1204
"cups-ipp-missing-get-printer-attributes");
1205
1206
for (i = 0; i < operations_sup->num_values; i ++)
1207
{
1208
if (operations_sup->values[i].integer == IPP_OP_VALIDATE_JOB)
1209
validate_job = 1;
1210
else if (operations_sup->values[i].integer == IPP_OP_CREATE_JOB)
1211
create_job = 1;
1212
else if (operations_sup->values[i].integer == IPP_OP_SEND_DOCUMENT)
1213
send_document = 1;
1214
else if (operations_sup->values[i].integer == IPP_OP_GET_JOB_ATTRIBUTES)
1215
get_job_attrs = 1;
1216
}
1217
1218
if (create_job && !send_document)
1219
{
1220
fputs("DEBUG: Printer supports Create-Job but not Send-Document.\n",
1221
stderr);
1222
create_job = 0;
1223
1224
update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
1225
"cups-ipp-missing-send-document");
1226
}
1227
1228
if (!validate_job)
1229
update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
1230
"cups-ipp-missing-validate-job");
1231
}
1232
else
1233
update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
1234
"cups-ipp-missing-operations-supported");
1235
1236
doc_handling_sup = ippFindAttribute(supported,
1237
"multiple-document-handling-supported",
1238
IPP_TAG_KEYWORD);
1239
1240
report_printer_state(supported);
1241
}
1242
while (!job_canceled && ipp_status > IPP_STATUS_OK_CONFLICTING);
1243
1244
if (job_canceled)
1245
return (CUPS_BACKEND_OK);
1246
1247
/*
1248
* See if the printer is accepting jobs and is not stopped; if either
1249
* condition is true and we are printing to a class, requeue the job...
1250
*/
1251
1252
if (getenv("CLASS") != NULL)
1253
{
1254
printer_state = ippFindAttribute(supported, "printer-state",
1255
IPP_TAG_ENUM);
1256
printer_accepting = ippFindAttribute(supported, "printer-is-accepting-jobs",
1257
IPP_TAG_BOOLEAN);
1258
1259
if (printer_state == NULL ||
1260
(printer_state->values[0].integer > IPP_PSTATE_PROCESSING &&
1261
waitprinter) ||
1262
printer_accepting == NULL ||
1263
!printer_accepting->values[0].boolean)
1264
{
1265
/*
1266
* If the CLASS environment variable is set, the job was submitted
1267
* to a class and not to a specific queue. In this case, we want
1268
* to abort immediately so that the job can be requeued on the next
1269
* available printer in the class.
1270
*/
1271
1272
_cupsLangPrintFilter(stderr, "INFO",
1273
_("Unable to contact printer, queuing on next "
1274
"printer in class."));
1275
1276
ippDelete(supported);
1277
httpClose(http);
1278
1279
/*
1280
* Sleep 5 seconds to keep the job from requeuing too rapidly...
1281
*/
1282
1283
sleep(5);
1284
1285
return (CUPS_BACKEND_FAILED);
1286
}
1287
}
1288
1289
/*
1290
* See if the printer supports multiple copies...
1291
*/
1292
1293
copies = atoi(argv[4]);
1294
1295
if (copies_sup || argc < 7)
1296
copies_remaining = 1;
1297
else
1298
copies_remaining = copies;
1299
1300
/*
1301
* Prepare remaining printing options...
1302
*/
1303
1304
options = NULL;
1305
1306
if (send_options)
1307
{
1308
num_options = cupsParseOptions(argv[5], 0, &options);
1309
1310
if (!cups_version && media_col_sup)
1311
{
1312
/*
1313
* Load the PPD file and generate PWG attribute mapping information...
1314
*/
1315
1316
ppd_attr_t *mandatory; /* cupsMandatory value */
1317
1318
ppd = ppdOpenFile(getenv("PPD"));
1319
pc = _ppdCacheCreateWithPPD(ppd);
1320
1321
ppdMarkDefaults(ppd);
1322
cupsMarkOptions(ppd, num_options, options);
1323
1324
if ((mandatory = ppdFindAttr(ppd, "cupsMandatory", NULL)) != NULL)
1325
strlcpy(mandatory_attrs, mandatory->value, sizeof(mandatory_attrs));
1326
}
1327
1328
/*
1329
* Validate job-password/-encryption...
1330
*/
1331
1332
if (cupsGetOption("job-password", num_options, options))
1333
{
1334
const char *keyword; /* job-password-encryption value */
1335
static const char * const hashes[] =
1336
{ /* List of supported hash algorithms, in order of preference */
1337
"sha-512",
1338
"sha-384",
1339
"sha-512_256",
1340
"sha-512-224",
1341
"sha-256",
1342
"sha-224",
1343
"sha",
1344
"none"
1345
};
1346
1347
if ((keyword = cupsGetOption("job-password-encryption", num_options, options)) == NULL || !ippContainsString(encryption_sup, keyword))
1348
{
1349
/*
1350
* Either no job-password-encryption or the value isn't supported by
1351
* the printer...
1352
*/
1353
1354
size_t j;
1355
for (j = 0; j < (sizeof(hashes) / sizeof(hashes[0])); j ++)
1356
if (ippContainsString(encryption_sup, hashes[j]))
1357
{
1358
num_options = cupsAddOption("job-password-encryption", hashes[j], num_options, &options);
1359
break;
1360
}
1361
}
1362
}
1363
}
1364
else
1365
num_options = 0;
1366
1367
document_format = NULL;
1368
1369
if (format_sup != NULL)
1370
{
1371
if (ippContainsString(format_sup, final_content_type))
1372
document_format = final_content_type;
1373
else if (ippContainsString(format_sup, "application/octet-stream"))
1374
document_format = "application/octet-stream";
1375
}
1376
1377
fprintf(stderr, "DEBUG: final_content_type=\"%s\", document_format=\"%s\"\n",
1378
final_content_type, document_format ? document_format : "(null)");
1379
1380
/*
1381
* If the printer does not support HTTP/1.1 (which IPP requires), copy stdin
1382
* to a temporary file so that we can do a HTTP/1.0 submission...
1383
*
1384
* (I hate compatibility hacks!)
1385
*/
1386
1387
if (http->version < HTTP_VERSION_1_1 && num_files == 0)
1388
{
1389
if ((fd = cupsTempFd(tmpfilename, sizeof(tmpfilename))) < 0)
1390
{
1391
perror("DEBUG: Unable to create temporary file");
1392
return (CUPS_BACKEND_FAILED);
1393
}
1394
1395
_cupsLangPrintFilter(stderr, "INFO", _("Copying print data."));
1396
1397
if ((compatsize = write(fd, buffer, (size_t)bytes)) < 0)
1398
{
1399
perror("DEBUG: Unable to write temporary file");
1400
return (CUPS_BACKEND_FAILED);
1401
}
1402
1403
if ((bytes = backendRunLoop(-1, fd, snmp_fd, &(addrlist->addr), 0, 0,
1404
backendNetworkSideCB)) < 0)
1405
return (CUPS_BACKEND_FAILED);
1406
1407
compatsize += bytes;
1408
1409
close(fd);
1410
1411
compatfile = tmpfilename;
1412
files = &compatfile;
1413
num_files = 1;
1414
}
1415
else if (http->version < HTTP_VERSION_1_1 && num_files == 1)
1416
{
1417
struct stat fileinfo; /* File information */
1418
1419
if (!stat(files[0], &fileinfo))
1420
compatsize = fileinfo.st_size;
1421
}
1422
1423
/*
1424
* If the printer only claims to support IPP/1.0, or if the user specifically
1425
* included version=1.0 in the URI, then do not try to use Create-Job or
1426
* Send-Document. This is another dreaded compatibility hack, but
1427
* unfortunately there are enough broken printers out there that we need
1428
* this for now...
1429
*/
1430
1431
if (version == 10)
1432
create_job = 0;
1433
1434
/*
1435
* Start monitoring the printer in the background...
1436
*/
1437
1438
monitor.uri = uri;
1439
monitor.hostname = hostname;
1440
monitor.user = argv[2];
1441
monitor.resource = resource;
1442
monitor.port = port;
1443
monitor.version = version;
1444
monitor.job_id = 0;
1445
monitor.create_job = create_job;
1446
monitor.get_job_attrs = get_job_attrs;
1447
monitor.encryption = cupsEncryption();
1448
monitor.job_state = IPP_JSTATE_PENDING;
1449
monitor.printer_state = IPP_PSTATE_IDLE;
1450
monitor.retryable = argc == 6 && document_format && strcmp(document_format, "image/pwg-raster") && strcmp(document_format, "image/urf");
1451
1452
fprintf(stderr, "DEBUG: retryable=%d\n", monitor.retryable);
1453
1454
if (create_job)
1455
{
1456
monitor.job_name = argv[3];
1457
}
1458
else
1459
{
1460
snprintf(print_job_name, sizeof(print_job_name), "%s - %s", argv[1],
1461
argv[3]);
1462
monitor.job_name = print_job_name;
1463
}
1464
1465
_cupsThreadCreate((_cups_thread_func_t)monitor_printer, &monitor);
1466
1467
/*
1468
* Validate access to the printer...
1469
*/
1470
1471
while (!job_canceled && validate_job)
1472
{
1473
request = new_request(IPP_OP_VALIDATE_JOB, version, uri, argv[2],
1474
monitor.job_name, num_options, options, compression,
1475
copies_sup ? copies : 1, document_format, pc, ppd,
1476
media_col_sup, doc_handling_sup, print_color_mode_sup);
1477
1478
response = cupsDoRequest(http, request, resource);
1479
1480
ipp_status = cupsLastError();
1481
1482
fprintf(stderr, "DEBUG: Validate-Job: %s (%s)\n",
1483
ippErrorString(ipp_status), cupsLastErrorString());
1484
debug_attributes(response);
1485
1486
if ((job_auth = ippFindAttribute(response, "job-authorization-uri",
1487
IPP_TAG_URI)) != NULL)
1488
num_options = cupsAddOption("job-authorization-uri",
1489
ippGetString(job_auth, 0, NULL), num_options,
1490
&options);
1491
1492
if (ipp_status == IPP_STATUS_OK_IGNORED_OR_SUBSTITUTED || ipp_status == IPP_STATUS_OK_CONFLICTING)
1493
{
1494
/*
1495
* One or more options are not supported...
1496
*/
1497
1498
if (ippFindAttribute(response, "sides", IPP_TAG_ZERO))
1499
{
1500
/*
1501
* The sides value is not supported, revert to one-sided as needed...
1502
*/
1503
1504
const char *sides = cupsGetOption("sides", num_options, options);
1505
1506
if (!sides || !strncmp(sides, "two-sided-", 10))
1507
{
1508
fputs("DEBUG: Unable to do two-sided printing, setting sides to 'one-sided'.\n", stderr);
1509
num_options = cupsAddOption("sides", "one-sided", num_options, &options);
1510
}
1511
}
1512
}
1513
1514
ippDelete(response);
1515
1516
if (job_canceled)
1517
break;
1518
1519
if (ipp_status == IPP_STATUS_ERROR_SERVICE_UNAVAILABLE ||
1520
ipp_status == IPP_STATUS_ERROR_BUSY)
1521
{
1522
_cupsLangPrintFilter(stderr, "INFO", _("The printer is in use."));
1523
sleep(10);
1524
}
1525
else if (ipp_status == IPP_STATUS_ERROR_DOCUMENT_FORMAT_NOT_SUPPORTED ||
1526
ipp_status == IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES ||
1527
ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_INFO_NEEDED ||
1528
ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_CLOSED ||
1529
ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_LIMIT_REACHED ||
1530
ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_AUTHORIZATION_FAILED)
1531
goto cleanup;
1532
else if (ipp_status == IPP_STATUS_ERROR_FORBIDDEN ||
1533
ipp_status == IPP_STATUS_ERROR_NOT_AUTHORIZED ||
1534
ipp_status == IPP_STATUS_ERROR_CUPS_AUTHENTICATION_CANCELED)
1535
{
1536
const char *www_auth = httpGetField(http, HTTP_FIELD_WWW_AUTHENTICATE);
1537
/* WWW-Authenticate field value */
1538
1539
if (!strncmp(www_auth, "Negotiate", 9))
1540
auth_info_required = "negotiate";
1541
else if (www_auth[0])
1542
auth_info_required = "username,password";
1543
1544
goto cleanup;
1545
}
1546
else if (ipp_status == IPP_STATUS_ERROR_OPERATION_NOT_SUPPORTED)
1547
{
1548
/*
1549
* This is all too common...
1550
*/
1551
1552
update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
1553
"cups-ipp-missing-validate-job");
1554
break;
1555
}
1556
else if (ipp_status < IPP_STATUS_REDIRECTION_OTHER_SITE ||
1557
ipp_status == IPP_STATUS_ERROR_BAD_REQUEST)
1558
break;
1559
else if (job_auth == NULL && ipp_status > IPP_STATUS_ERROR_BAD_REQUEST)
1560
{
1561
if (!validate_retried)
1562
{
1563
// Retry Validate-Job operation once, to work around known printer bug...
1564
validate_retried = 1;
1565
sleep(10);
1566
continue;
1567
}
1568
1569
goto cleanup;
1570
}
1571
}
1572
1573
/*
1574
* Then issue the print-job request...
1575
*/
1576
1577
job_id = 0;
1578
1579
while (!job_canceled && copies_remaining > 0)
1580
{
1581
/*
1582
* Check for side-channel requests...
1583
*/
1584
1585
backendCheckSideChannel(snmp_fd, http->hostaddr);
1586
1587
/*
1588
* Build the IPP job creation request...
1589
*/
1590
1591
if (job_canceled)
1592
break;
1593
1594
request = new_request((num_files > 1 || create_job) ? IPP_OP_CREATE_JOB :
1595
IPP_OP_PRINT_JOB,
1596
version, uri, argv[2], monitor.job_name, num_options,
1597
options, compression, copies_sup ? copies : 1,
1598
document_format, pc, ppd, media_col_sup,
1599
doc_handling_sup, print_color_mode_sup);
1600
1601
/*
1602
* Do the request...
1603
*/
1604
1605
if (num_files > 1 || create_job)
1606
response = cupsDoRequest(http, request, resource);
1607
else
1608
{
1609
size_t length = 0; /* Length of request */
1610
1611
if (compatsize > 0)
1612
{
1613
fputs("DEBUG: Sending file using HTTP/1.0 Content-Length...\n", stderr);
1614
length = ippLength(request) + (size_t)compatsize;
1615
}
1616
else
1617
fputs("DEBUG: Sending file using HTTP/1.1 chunking...\n", stderr);
1618
1619
http_status = cupsSendRequest(http, request, resource, length);
1620
if (http_status == HTTP_STATUS_CONTINUE && request->state == IPP_STATE_DATA)
1621
{
1622
if (compression && strcmp(compression, "none"))
1623
httpSetField(http, HTTP_FIELD_CONTENT_ENCODING, compression);
1624
1625
if (num_files == 1)
1626
{
1627
if ((fd = open(files[0], O_RDONLY)) < 0)
1628
{
1629
_cupsLangPrintError("ERROR", _("Unable to open print file"));
1630
return (CUPS_BACKEND_FAILED);
1631
}
1632
}
1633
else
1634
{
1635
fd = 0;
1636
http_status = cupsWriteRequestData(http, buffer, (size_t)bytes);
1637
}
1638
1639
while (http_status == HTTP_STATUS_CONTINUE &&
1640
(!job_canceled || compatsize > 0))
1641
{
1642
/*
1643
* Check for side-channel requests and more print data...
1644
*/
1645
1646
FD_ZERO(&input);
1647
FD_SET(fd, &input);
1648
FD_SET(snmp_fd, &input);
1649
FD_SET(CUPS_SC_FD, &input);
1650
1651
while (select(fd > snmp_fd ? fd + 1 : snmp_fd + 1, &input, NULL, NULL,
1652
NULL) <= 0 && !job_canceled);
1653
1654
if (FD_ISSET(snmp_fd, &input))
1655
backendCheckSideChannel(snmp_fd, http->hostaddr);
1656
1657
if (FD_ISSET(fd, &input))
1658
{
1659
if ((bytes = read(fd, buffer, sizeof(buffer))) > 0)
1660
{
1661
fprintf(stderr, "DEBUG: Read %d bytes...\n", (int)bytes);
1662
1663
if ((http_status = cupsWriteRequestData(http, buffer, (size_t)bytes))
1664
!= HTTP_STATUS_CONTINUE)
1665
break;
1666
}
1667
else if (bytes == 0 || (errno != EINTR && errno != EAGAIN))
1668
break;
1669
}
1670
}
1671
1672
if (http_status == HTTP_STATUS_ERROR)
1673
fprintf(stderr, "DEBUG: Error writing document data for "
1674
"Print-Job: %s\n", strerror(httpError(http)));
1675
1676
if (num_files == 1)
1677
close(fd);
1678
}
1679
1680
response = cupsGetResponse(http, resource);
1681
ippDelete(request);
1682
}
1683
1684
ipp_status = cupsLastError();
1685
1686
fprintf(stderr, "DEBUG: %s: %s (%s)\n",
1687
(num_files > 1 || create_job) ? "Create-Job" : "Print-Job",
1688
ippErrorString(ipp_status), cupsLastErrorString());
1689
debug_attributes(response);
1690
1691
if (ipp_status > IPP_STATUS_OK_CONFLICTING)
1692
{
1693
job_id = 0;
1694
1695
if (job_canceled)
1696
break;
1697
1698
if (ipp_status == IPP_STATUS_ERROR_SERVICE_UNAVAILABLE ||
1699
ipp_status == IPP_STATUS_ERROR_NOT_POSSIBLE ||
1700
ipp_status == IPP_STATUS_ERROR_BUSY)
1701
{
1702
_cupsLangPrintFilter(stderr, "INFO", _("The printer is in use."));
1703
sleep(10);
1704
1705
if (num_files == 0)
1706
{
1707
/*
1708
* We can't re-submit when we have no files to print, so exit
1709
* immediately with the right status code...
1710
*/
1711
1712
goto cleanup;
1713
}
1714
}
1715
else if (ipp_status == IPP_STATUS_ERROR_JOB_CANCELED ||
1716
ipp_status == IPP_STATUS_ERROR_NOT_AUTHORIZED ||
1717
ipp_status == IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES ||
1718
ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_INFO_NEEDED ||
1719
ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_CLOSED ||
1720
ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_LIMIT_REACHED ||
1721
ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_AUTHORIZATION_FAILED)
1722
goto cleanup;
1723
else
1724
{
1725
/*
1726
* Update auth-info-required as needed...
1727
*/
1728
1729
_cupsLangPrintFilter(stderr, "ERROR",
1730
_("Print job was not accepted."));
1731
1732
if (ipp_status == IPP_STATUS_ERROR_FORBIDDEN ||
1733
ipp_status == IPP_STATUS_ERROR_CUPS_AUTHENTICATION_CANCELED)
1734
{
1735
const char *www_auth = httpGetField(http, HTTP_FIELD_WWW_AUTHENTICATE);
1736
/* WWW-Authenticate field value */
1737
1738
if (!strncmp(www_auth, "Negotiate", 9))
1739
auth_info_required = "negotiate";
1740
else if (www_auth[0])
1741
auth_info_required = "username,password";
1742
}
1743
else if (ipp_status == IPP_STATUS_ERROR_REQUEST_VALUE)
1744
{
1745
/*
1746
* Print file is too large, abort this job...
1747
*/
1748
1749
goto cleanup;
1750
}
1751
else
1752
sleep(10);
1753
1754
if (num_files == 0)
1755
{
1756
/*
1757
* We can't re-submit when we have no files to print, so exit
1758
* immediately with the right status code...
1759
*/
1760
1761
goto cleanup;
1762
}
1763
}
1764
}
1765
else if ((job_id_attr = ippFindAttribute(response, "job-id",
1766
IPP_TAG_INTEGER)) == NULL)
1767
{
1768
fputs("DEBUG: Print job accepted - job ID unknown.\n", stderr);
1769
update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
1770
"cups-ipp-missing-job-id");
1771
job_id = 0;
1772
}
1773
else
1774
{
1775
password_tries = 0;
1776
monitor.job_id = job_id = job_id_attr->values[0].integer;
1777
fprintf(stderr, "DEBUG: Print job accepted - job ID %d.\n", job_id);
1778
}
1779
1780
ippDelete(response);
1781
1782
if (job_canceled)
1783
break;
1784
1785
if (job_id && (num_files > 1 || create_job))
1786
{
1787
for (i = 0; num_files == 0 || i < num_files; i ++)
1788
{
1789
/*
1790
* Check for side-channel requests...
1791
*/
1792
1793
backendCheckSideChannel(snmp_fd, http->hostaddr);
1794
1795
/*
1796
* Send the next file in the job...
1797
*/
1798
1799
request = ippNewRequest(IPP_OP_SEND_DOCUMENT);
1800
ippSetVersion(request, version / 10, version % 10);
1801
1802
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
1803
NULL, uri);
1804
1805
ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id",
1806
job_id);
1807
1808
if (argv[2][0])
1809
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
1810
"requesting-user-name", NULL, argv[2]);
1811
1812
ippAddBoolean(request, IPP_TAG_OPERATION, "last-document",
1813
(i + 1) >= num_files);
1814
1815
if (document_format)
1816
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE,
1817
"document-format", NULL, document_format);
1818
1819
if (compression)
1820
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
1821
"compression", NULL, compression);
1822
1823
fprintf(stderr, "DEBUG: Sending file %d using chunking...\n", i + 1);
1824
fprintf(stderr, "DEBUG: IPP/%d.%d %s #%d\n", version / 10, version % 10, ippOpString(ippGetOperation(request)), ippGetRequestId(request));
1825
debug_attributes(request);
1826
1827
http_status = cupsSendRequest(http, request, resource, 0);
1828
if (http_status == HTTP_STATUS_CONTINUE && request->state == IPP_STATE_DATA)
1829
{
1830
if (compression && strcmp(compression, "none"))
1831
httpSetField(http, HTTP_FIELD_CONTENT_ENCODING, compression);
1832
1833
if (num_files == 0)
1834
{
1835
fd = 0;
1836
http_status = cupsWriteRequestData(http, buffer, (size_t)bytes);
1837
}
1838
else
1839
{
1840
if ((fd = open(files[i], O_RDONLY)) < 0)
1841
{
1842
_cupsLangPrintError("ERROR", _("Unable to open print file"));
1843
return (CUPS_BACKEND_FAILED);
1844
}
1845
}
1846
}
1847
else
1848
fd = -1;
1849
1850
if (fd >= 0)
1851
{
1852
while (!job_canceled && http_status == HTTP_STATUS_CONTINUE &&
1853
(bytes = read(fd, buffer, sizeof(buffer))) > 0)
1854
{
1855
if ((http_status = cupsWriteRequestData(http, buffer, (size_t)bytes))
1856
!= HTTP_STATUS_CONTINUE)
1857
break;
1858
else
1859
{
1860
/*
1861
* Check for side-channel requests...
1862
*/
1863
1864
backendCheckSideChannel(snmp_fd, http->hostaddr);
1865
}
1866
}
1867
1868
if (fd > 0)
1869
close(fd);
1870
}
1871
1872
if (http_status == HTTP_STATUS_ERROR)
1873
fprintf(stderr, "DEBUG: Error writing document data for "
1874
"Send-Document: %s\n", strerror(httpError(http)));
1875
1876
response = cupsGetResponse(http, resource);
1877
ippDelete(request);
1878
1879
fprintf(stderr, "DEBUG: Send-Document: %s (%s)\n", ippErrorString(cupsLastError()), cupsLastErrorString());
1880
debug_attributes(response);
1881
1882
if (cupsLastError() > IPP_STATUS_OK_CONFLICTING && !job_canceled)
1883
{
1884
ipp_attribute_t *reasons = ippFindAttribute(response, "job-state-reasons", IPP_TAG_KEYWORD);
1885
/* job-state-reasons values */
1886
1887
ipp_status = cupsLastError();
1888
1889
if (ippContainsString(reasons, "document-format-error"))
1890
ipp_status = IPP_STATUS_ERROR_DOCUMENT_FORMAT_ERROR;
1891
else if (ippContainsString(reasons, "document-unprintable"))
1892
ipp_status = IPP_STATUS_ERROR_DOCUMENT_UNPRINTABLE;
1893
1894
ippDelete(response);
1895
_cupsLangPrintFilter(stderr, "ERROR", _("Unable to add document to print job."));
1896
break;
1897
}
1898
else
1899
{
1900
ippDelete(response);
1901
1902
password_tries = 0;
1903
1904
if (num_files == 0 || fd < 0)
1905
break;
1906
}
1907
}
1908
}
1909
1910
if (job_canceled)
1911
break;
1912
1913
if (ipp_status <= IPP_STATUS_OK_CONFLICTING && argc > 6)
1914
{
1915
fprintf(stderr, "PAGE: 1 %d\n", copies_sup ? atoi(argv[4]) : 1);
1916
copies_remaining --;
1917
}
1918
else if ((ipp_status == IPP_STATUS_ERROR_DOCUMENT_FORMAT_NOT_SUPPORTED || ipp_status == IPP_STATUS_ERROR_DOCUMENT_FORMAT_ERROR || ipp_status == IPP_STATUS_ERROR_DOCUMENT_UNPRINTABLE) &&
1919
argc == 6 &&
1920
document_format && strcmp(document_format, "image/pwg-raster") && strcmp(document_format, "image/urf"))
1921
{
1922
/*
1923
* Need to reprocess the job as raster...
1924
*/
1925
1926
fputs("JOBSTATE: cups-retry-as-raster\n", stderr);
1927
if (job_id > 0)
1928
cancel_job(http, uri, job_id, resource, argv[2], version);
1929
1930
goto cleanup;
1931
}
1932
else if (ipp_status == IPP_STATUS_ERROR_SERVICE_UNAVAILABLE ||
1933
ipp_status == IPP_STATUS_ERROR_NOT_POSSIBLE ||
1934
ipp_status == IPP_STATUS_ERROR_BUSY)
1935
{
1936
if (argc == 6)
1937
{
1938
/*
1939
* Need to reprocess the entire job; if we have a job ID, cancel the
1940
* job first...
1941
*/
1942
1943
if (job_id > 0)
1944
cancel_job(http, uri, job_id, resource, argv[2], version);
1945
1946
goto cleanup;
1947
}
1948
continue;
1949
}
1950
else if (ipp_status == IPP_STATUS_ERROR_REQUEST_VALUE ||
1951
ipp_status == IPP_STATUS_ERROR_JOB_CANCELED ||
1952
ipp_status == IPP_STATUS_ERROR_NOT_AUTHORIZED ||
1953
ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_INFO_NEEDED ||
1954
ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_CLOSED ||
1955
ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_LIMIT_REACHED ||
1956
ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_AUTHORIZATION_FAILED ||
1957
ipp_status == IPP_STATUS_ERROR_INTERNAL)
1958
{
1959
/*
1960
* Print file is too large, job was canceled, we need new
1961
* authentication data, or we had some sort of error...
1962
*/
1963
1964
goto cleanup;
1965
}
1966
else if (ipp_status == IPP_STATUS_ERROR_CUPS_UPGRADE_REQUIRED)
1967
{
1968
/*
1969
* Server is configured incorrectly; the policy for Create-Job and
1970
* Send-Document has to be the same (auth or no auth, encryption or
1971
* no encryption). Force the queue to stop since printing will never
1972
* work.
1973
*/
1974
1975
fputs("DEBUG: The server or printer is configured incorrectly.\n",
1976
stderr);
1977
fputs("DEBUG: The policy for Create-Job and Send-Document must have the "
1978
"same authentication and encryption requirements.\n", stderr);
1979
1980
ipp_status = IPP_STATUS_ERROR_INTERNAL;
1981
1982
if (job_id > 0)
1983
cancel_job(http, uri, job_id, resource, argv[2], version);
1984
1985
goto cleanup;
1986
}
1987
else if (ipp_status == IPP_STATUS_ERROR_NOT_FOUND)
1988
{
1989
/*
1990
* Printer does not actually implement support for Create-Job/
1991
* Send-Document, so log the conformance issue and stop the printer.
1992
*/
1993
1994
fputs("DEBUG: This printer claims to support Create-Job and "
1995
"Send-Document, but those operations failed.\n", stderr);
1996
fputs("DEBUG: Add '?version=1.0' to the device URI to use legacy "
1997
"compatibility mode.\n", stderr);
1998
update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
1999
"cups-ipp-missing-send-document");
2000
2001
ipp_status = IPP_STATUS_ERROR_INTERNAL; /* Force queue to stop */
2002
2003
goto cleanup;
2004
}
2005
else
2006
copies_remaining --;
2007
2008
/*
2009
* Wait for the job to complete...
2010
*/
2011
2012
if (!job_id || !waitjob || !get_job_attrs)
2013
continue;
2014
2015
fputs("STATE: +cups-waiting-for-job-completed\n", stderr);
2016
2017
_cupsLangPrintFilter(stderr, "INFO", _("Waiting for job to complete."));
2018
2019
for (delay = _cupsNextDelay(0, &prev_delay); !job_canceled;)
2020
{
2021
/*
2022
* Check for side-channel requests...
2023
*/
2024
2025
backendCheckSideChannel(snmp_fd, http->hostaddr);
2026
2027
/*
2028
* Check printer state...
2029
*/
2030
2031
check_printer_state(http, uri, resource, argv[2], version);
2032
2033
if (cupsLastError() <= IPP_STATUS_OK_CONFLICTING)
2034
password_tries = 0;
2035
2036
/*
2037
* Build an IPP_OP_GET_JOB_ATTRIBUTES request...
2038
*/
2039
2040
request = ippNewRequest(IPP_OP_GET_JOB_ATTRIBUTES);
2041
ippSetVersion(request, version / 10, version % 10);
2042
2043
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
2044
NULL, uri);
2045
2046
ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id",
2047
job_id);
2048
2049
if (argv[2][0])
2050
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
2051
"requesting-user-name", NULL, argv[2]);
2052
2053
ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
2054
"requested-attributes", sizeof(jattrs) / sizeof(jattrs[0]),
2055
NULL, jattrs);
2056
2057
fprintf(stderr, "DEBUG: IPP/%d.%d %s #%d\n", version / 10, version % 10, ippOpString(ippGetOperation(request)), ippGetRequestId(request));
2058
debug_attributes(request);
2059
2060
/*
2061
* Do the request...
2062
*/
2063
2064
httpReconnect2(http, 30000, NULL);
2065
response = cupsDoRequest(http, request, resource);
2066
ipp_status = cupsLastError();
2067
2068
if (ipp_status == IPP_STATUS_ERROR_NOT_FOUND || ipp_status == IPP_STATUS_ERROR_NOT_POSSIBLE)
2069
{
2070
/*
2071
* Job has gone away and/or the server has no job history...
2072
*/
2073
2074
update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
2075
"cups-ipp-missing-job-history");
2076
ippDelete(response);
2077
2078
ipp_status = IPP_STATUS_OK;
2079
break;
2080
}
2081
2082
fprintf(stderr, "DEBUG: Get-Job-Attributes: %s (%s)\n",
2083
ippErrorString(ipp_status), cupsLastErrorString());
2084
debug_attributes(response);
2085
2086
if (ipp_status <= IPP_STATUS_OK_CONFLICTING)
2087
password_tries = 0;
2088
else
2089
{
2090
if (ipp_status != IPP_STATUS_ERROR_SERVICE_UNAVAILABLE &&
2091
ipp_status != IPP_STATUS_ERROR_BUSY)
2092
{
2093
ippDelete(response);
2094
ipp_status = IPP_STATUS_OK;
2095
break;
2096
}
2097
else if (ipp_status == IPP_STATUS_ERROR_INTERNAL)
2098
{
2099
waitjob_tries ++;
2100
2101
if (waitjob_tries > 4)
2102
{
2103
ippDelete(response);
2104
ipp_status = IPP_STATUS_OK;
2105
break;
2106
}
2107
}
2108
}
2109
2110
if (response)
2111
{
2112
if ((job_state = ippFindAttribute(response, "job-state",
2113
IPP_TAG_ENUM)) != NULL)
2114
{
2115
/*
2116
* Reflect the remote job state in the local queue...
2117
*/
2118
2119
if (cups_version &&
2120
job_state->values[0].integer >= IPP_JSTATE_PENDING &&
2121
job_state->values[0].integer <= IPP_JSTATE_COMPLETED)
2122
update_reasons(NULL,
2123
remote_job_states[job_state->values[0].integer -
2124
IPP_JSTATE_PENDING]);
2125
2126
if ((job_sheets = ippFindAttribute(response, "job-impressions-completed", IPP_TAG_INTEGER)) == NULL)
2127
job_sheets = ippFindAttribute(response, "job-media-sheets-completed", IPP_TAG_INTEGER);
2128
2129
if (job_sheets)
2130
fprintf(stderr, "PAGE: total %d\n",
2131
job_sheets->values[0].integer);
2132
2133
/*
2134
* Stop polling if the job is finished or pending-held...
2135
*/
2136
2137
if (job_state->values[0].integer > IPP_JSTATE_STOPPED || job_state->values[0].integer == IPP_JSTATE_HELD)
2138
{
2139
ippDelete(response);
2140
break;
2141
}
2142
}
2143
else if (ipp_status != IPP_STATUS_ERROR_SERVICE_UNAVAILABLE &&
2144
ipp_status != IPP_STATUS_ERROR_NOT_POSSIBLE &&
2145
ipp_status != IPP_STATUS_ERROR_BUSY)
2146
{
2147
/*
2148
* If the printer does not return a job-state attribute, it does not
2149
* conform to the IPP specification - break out immediately and fail
2150
* the job...
2151
*/
2152
2153
update_reasons(NULL, "+cups-ipp-conformance-failure-report,"
2154
"cups-ipp-missing-job-state");
2155
ipp_status = IPP_STATUS_ERROR_INTERNAL;
2156
break;
2157
}
2158
}
2159
2160
ippDelete(response);
2161
2162
/*
2163
* Wait before polling again...
2164
*/
2165
2166
sleep((unsigned)delay);
2167
2168
delay = _cupsNextDelay(delay, &prev_delay);
2169
}
2170
}
2171
2172
/*
2173
* Cancel the job as needed...
2174
*/
2175
2176
if (job_canceled > 0 && job_id > 0)
2177
{
2178
cancel_job(http, uri, job_id, resource, argv[2], version);
2179
2180
if (cupsLastError() > IPP_STATUS_OK_CONFLICTING)
2181
_cupsLangPrintFilter(stderr, "ERROR", _("Unable to cancel print job."));
2182
}
2183
2184
/*
2185
* Check the printer state and report it if necessary...
2186
*/
2187
2188
check_printer_state(http, uri, resource, argv[2], version);
2189
2190
if (cupsLastError() <= IPP_STATUS_OK_CONFLICTING)
2191
password_tries = 0;
2192
2193
/*
2194
* Collect the final page count as needed...
2195
*/
2196
2197
if (have_supplies &&
2198
!backendSNMPSupplies(snmp_fd, &(http->addrlist->addr), &page_count,
2199
NULL) &&
2200
page_count > start_count)
2201
fprintf(stderr, "PAGE: total %d\n", page_count - start_count);
2202
2203
#ifdef HAVE_GSSAPI
2204
/*
2205
* See if we used Kerberos at all...
2206
*/
2207
2208
if (http->gssctx)
2209
auth_info_required = "negotiate";
2210
#endif /* HAVE_GSSAPI */
2211
2212
/*
2213
* Free memory...
2214
*/
2215
2216
cleanup:
2217
2218
cupsFreeOptions(num_options, options);
2219
_ppdCacheDestroy(pc);
2220
ppdClose(ppd);
2221
2222
httpClose(http);
2223
2224
ippDelete(supported);
2225
2226
/*
2227
* Remove the temporary file(s) if necessary...
2228
*/
2229
2230
if (tmpfilename[0])
2231
unlink(tmpfilename);
2232
2233
/*
2234
* Return the queue status...
2235
*/
2236
2237
if (ipp_status == IPP_STATUS_ERROR_NOT_AUTHORIZED || ipp_status == IPP_STATUS_ERROR_FORBIDDEN ||
2238
ipp_status == IPP_STATUS_ERROR_CUPS_AUTHENTICATION_CANCELED ||
2239
ipp_status <= IPP_STATUS_OK_CONFLICTING)
2240
fprintf(stderr, "ATTR: auth-info-required=%s\n", auth_info_required);
2241
2242
if (ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_INFO_NEEDED)
2243
fputs("JOBSTATE: account-info-needed\n", stderr);
2244
else if (ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_CLOSED)
2245
fputs("JOBSTATE: account-closed\n", stderr);
2246
else if (ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_LIMIT_REACHED)
2247
fputs("JOBSTATE: account-limit-reached\n", stderr);
2248
else if (ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_AUTHORIZATION_FAILED)
2249
fputs("JOBSTATE: account-authorization-failed\n", stderr);
2250
2251
// job_canceled can be -1 which should not be treated as CUPS_BACKEND_OK
2252
if (job_canceled > 0)
2253
return (CUPS_BACKEND_OK);
2254
else if (ipp_status == IPP_STATUS_ERROR_NOT_AUTHORIZED || ipp_status == IPP_STATUS_ERROR_FORBIDDEN || ipp_status == IPP_STATUS_ERROR_CUPS_AUTHENTICATION_CANCELED)
2255
return (CUPS_BACKEND_AUTH_REQUIRED);
2256
else if (ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_LIMIT_REACHED ||
2257
ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_INFO_NEEDED ||
2258
ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_CLOSED ||
2259
ipp_status == IPP_STATUS_ERROR_CUPS_ACCOUNT_AUTHORIZATION_FAILED)
2260
return (CUPS_BACKEND_HOLD);
2261
else if (ipp_status == IPP_STATUS_ERROR_INTERNAL)
2262
return (CUPS_BACKEND_STOP);
2263
else if (ipp_status == IPP_STATUS_ERROR_CONFLICTING || ipp_status == IPP_STATUS_ERROR_REQUEST_ENTITY || ipp_status == IPP_STATUS_ERROR_REQUEST_VALUE)
2264
return (CUPS_BACKEND_FAILED);
2265
else if (ipp_status == IPP_STATUS_ERROR_REQUEST_VALUE ||
2266
ipp_status == IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES ||
2267
ipp_status == IPP_STATUS_ERROR_DOCUMENT_FORMAT_NOT_SUPPORTED || job_canceled < 0)
2268
{
2269
if (ipp_status == IPP_STATUS_ERROR_REQUEST_VALUE)
2270
_cupsLangPrintFilter(stderr, "ERROR", _("Print job too large."));
2271
else if (ipp_status == IPP_STATUS_ERROR_DOCUMENT_FORMAT_NOT_SUPPORTED)
2272
_cupsLangPrintFilter(stderr, "ERROR",
2273
_("Printer cannot print supplied content."));
2274
else if (ipp_status == IPP_STATUS_ERROR_ATTRIBUTES_OR_VALUES)
2275
_cupsLangPrintFilter(stderr, "ERROR",
2276
_("Printer cannot print with supplied options."));
2277
else
2278
_cupsLangPrintFilter(stderr, "ERROR", _("Print job canceled at printer."));
2279
2280
return (CUPS_BACKEND_CANCEL);
2281
}
2282
else if (ipp_status > IPP_STATUS_OK_CONFLICTING && ipp_status != IPP_STATUS_ERROR_JOB_CANCELED)
2283
return (CUPS_BACKEND_RETRY_CURRENT);
2284
else
2285
return (CUPS_BACKEND_OK);
2286
}
2287
2288
2289
/*
2290
* 'cancel_job()' - Cancel a print job.
2291
*/
2292
2293
static void
2294
cancel_job(http_t *http, /* I - HTTP connection */
2295
const char *uri, /* I - printer-uri */
2296
int id, /* I - job-id */
2297
const char *resource, /* I - Resource path */
2298
const char *user, /* I - requesting-user-name */
2299
int version) /* I - IPP version */
2300
{
2301
ipp_t *request; /* Cancel-Job request */
2302
2303
2304
_cupsLangPrintFilter(stderr, "INFO", _("Canceling print job."));
2305
2306
request = ippNewRequest(IPP_OP_CANCEL_JOB);
2307
ippSetVersion(request, version / 10, version % 10);
2308
2309
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
2310
NULL, uri);
2311
ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", id);
2312
2313
if (user && user[0])
2314
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
2315
"requesting-user-name", NULL, user);
2316
2317
/*
2318
* Do the request...
2319
*/
2320
2321
ippDelete(cupsDoRequest(http, request, resource));
2322
}
2323
2324
2325
/*
2326
* 'check_printer_state()' - Check the printer state.
2327
*/
2328
2329
static ipp_pstate_t /* O - Current printer-state */
2330
check_printer_state(
2331
http_t *http, /* I - HTTP connection */
2332
const char *uri, /* I - Printer URI */
2333
const char *resource, /* I - Resource path */
2334
const char *user, /* I - Username, if any */
2335
int version) /* I - IPP version */
2336
{
2337
ipp_t *request, /* IPP request */
2338
*response; /* IPP response */
2339
ipp_attribute_t *attr; /* Attribute in response */
2340
ipp_pstate_t printer_state = IPP_PSTATE_STOPPED;
2341
/* Current printer-state */
2342
2343
2344
/*
2345
* Send a Get-Printer-Attributes request and log the results...
2346
*/
2347
2348
request = ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES);
2349
ippSetVersion(request, version / 10, version % 10);
2350
2351
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
2352
NULL, uri);
2353
2354
if (user && user[0])
2355
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
2356
"requesting-user-name", NULL, user);
2357
2358
ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
2359
"requested-attributes",
2360
(int)(sizeof(pattrs) / sizeof(pattrs[0])), NULL, pattrs);
2361
2362
fprintf(stderr, "DEBUG: IPP/%d.%d %s #%d\n", version / 10, version % 10, ippOpString(ippGetOperation(request)), ippGetRequestId(request));
2363
debug_attributes(request);
2364
2365
if ((response = cupsDoRequest(http, request, resource)) != NULL)
2366
{
2367
report_printer_state(response);
2368
2369
if ((attr = ippFindAttribute(response, "printer-state",
2370
IPP_TAG_ENUM)) != NULL)
2371
printer_state = (ipp_pstate_t)attr->values[0].integer;
2372
}
2373
2374
fprintf(stderr, "DEBUG: Get-Printer-Attributes: %s (%s)\n",
2375
ippErrorString(cupsLastError()), cupsLastErrorString());
2376
debug_attributes(response);
2377
ippDelete(response);
2378
2379
/*
2380
* Return the printer-state value...
2381
*/
2382
2383
return (printer_state);
2384
}
2385
2386
2387
/*
2388
* 'debug_attributes()' - Print out the request or response attributes as DEBUG
2389
* messages...
2390
*/
2391
2392
static void
2393
debug_attributes(ipp_t *ipp) /* I - Request or response message */
2394
{
2395
ipp_tag_t group; /* Current group */
2396
ipp_attribute_t *attr; /* Current attribute */
2397
char buffer[1024]; /* Value buffer */
2398
2399
2400
for (group = IPP_TAG_ZERO, attr = ippFirstAttribute(ipp);
2401
attr;
2402
attr = ippNextAttribute(ipp))
2403
{
2404
const char *name = ippGetName(attr);
2405
2406
if (!name)
2407
{
2408
group = IPP_TAG_ZERO;
2409
continue;
2410
}
2411
2412
if (group != ippGetGroupTag(attr))
2413
{
2414
group = ippGetGroupTag(attr);
2415
fprintf(stderr, "DEBUG: ---- %s ----\n", ippTagString(group));
2416
}
2417
2418
if (!strcmp(name, "job-password"))
2419
strlcpy(buffer, "---", sizeof(buffer));
2420
else
2421
ippAttributeString(attr, buffer, sizeof(buffer));
2422
2423
fprintf(stderr, "DEBUG: %s %s%s %s\n", name,
2424
ippGetCount(attr) > 1 ? "1setOf " : "",
2425
ippTagString(ippGetValueTag(attr)), buffer);
2426
}
2427
2428
fprintf(stderr, "DEBUG: ---- %s ----\n", ippTagString(IPP_TAG_END));
2429
}
2430
2431
2432
/*
2433
* 'monitor_printer()' - Monitor the printer state.
2434
*/
2435
2436
static void * /* O - Thread exit code */
2437
monitor_printer(
2438
_cups_monitor_t *monitor) /* I - Monitoring data */
2439
{
2440
http_t *http; /* Connection to printer */
2441
ipp_t *request, /* IPP request */
2442
*response; /* IPP response */
2443
ipp_attribute_t *attr; /* Attribute in response */
2444
int delay, /* Current delay */
2445
prev_delay; /* Previous delay */
2446
ipp_op_t job_op; /* Operation to use */
2447
int job_id; /* Job ID */
2448
const char *job_name; /* Job name */
2449
ipp_jstate_t job_state; /* Job state */
2450
const char *job_user; /* Job originating user name */
2451
int password_tries = 0; /* Password tries */
2452
2453
2454
/*
2455
* Make a copy of the printer connection...
2456
*/
2457
2458
http = httpConnect2(monitor->hostname, monitor->port, NULL, AF_UNSPEC,
2459
monitor->encryption, 1, 0, NULL);
2460
httpSetTimeout(http, 30.0, timeout_cb, NULL);
2461
if (username[0])
2462
cupsSetUser(username);
2463
2464
cupsSetPasswordCB2((cups_password_cb2_t)password_cb, &password_tries);
2465
2466
/*
2467
* Loop until the job is canceled, aborted, or completed.
2468
*/
2469
2470
delay = _cupsNextDelay(0, &prev_delay);
2471
2472
monitor->job_reasons = 0;
2473
2474
while (monitor->job_state < IPP_JSTATE_CANCELED && !job_canceled)
2475
{
2476
/*
2477
* Reconnect to the printer as needed...
2478
*/
2479
2480
if (httpGetFd(http) < 0)
2481
httpReconnect2(http, 30000, NULL);
2482
2483
if (httpGetFd(http) >= 0)
2484
{
2485
/*
2486
* Connected, so check on the printer state...
2487
*/
2488
2489
monitor->printer_state = check_printer_state(http, monitor->uri,
2490
monitor->resource,
2491
monitor->user,
2492
monitor->version);
2493
if (cupsLastError() <= IPP_STATUS_OK_CONFLICTING)
2494
password_tries = 0;
2495
2496
if (monitor->job_id == 0 && monitor->create_job)
2497
{
2498
/*
2499
* No job-id yet, so continue...
2500
*/
2501
2502
goto monitor_sleep;
2503
}
2504
2505
/*
2506
* Check the status of the job itself...
2507
*/
2508
2509
job_op = (monitor->job_id > 0 && monitor->get_job_attrs) ?
2510
IPP_OP_GET_JOB_ATTRIBUTES : IPP_OP_GET_JOBS;
2511
request = ippNewRequest(job_op);
2512
ippSetVersion(request, monitor->version / 10, monitor->version % 10);
2513
2514
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
2515
NULL, monitor->uri);
2516
if (job_op == IPP_OP_GET_JOB_ATTRIBUTES)
2517
ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id",
2518
monitor->job_id);
2519
2520
if (monitor->user && monitor->user[0])
2521
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
2522
"requesting-user-name", NULL, monitor->user);
2523
2524
ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
2525
"requested-attributes",
2526
(int)(sizeof(jattrs) / sizeof(jattrs[0])), NULL, jattrs);
2527
2528
/*
2529
* Do the request...
2530
*/
2531
2532
response = cupsDoRequest(http, request, monitor->resource);
2533
2534
fprintf(stderr, "DEBUG: (monitor) %s: %s (%s)\n", ippOpString(job_op),
2535
ippErrorString(cupsLastError()), cupsLastErrorString());
2536
2537
if (cupsLastError() <= IPP_STATUS_OK_CONFLICTING)
2538
password_tries = 0;
2539
2540
if (job_op == IPP_OP_GET_JOB_ATTRIBUTES)
2541
{
2542
if ((attr = ippFindAttribute(response, "job-state",
2543
IPP_TAG_ENUM)) != NULL)
2544
monitor->job_state = (ipp_jstate_t)attr->values[0].integer;
2545
else
2546
monitor->job_state = IPP_JSTATE_COMPLETED;
2547
}
2548
else if (response)
2549
{
2550
for (attr = response->attrs; attr; attr = attr->next)
2551
{
2552
job_id = 0;
2553
job_name = NULL;
2554
job_state = IPP_JSTATE_PENDING;
2555
job_user = NULL;
2556
2557
while (attr && attr->group_tag != IPP_TAG_JOB)
2558
attr = attr->next;
2559
2560
if (!attr)
2561
break;
2562
2563
while (attr && attr->group_tag == IPP_TAG_JOB)
2564
{
2565
if (!strcmp(attr->name, "job-id") &&
2566
attr->value_tag == IPP_TAG_INTEGER)
2567
job_id = attr->values[0].integer;
2568
else if (!strcmp(attr->name, "job-name") &&
2569
(attr->value_tag == IPP_TAG_NAME ||
2570
attr->value_tag == IPP_TAG_NAMELANG))
2571
job_name = attr->values[0].string.text;
2572
else if (!strcmp(attr->name, "job-state") &&
2573
attr->value_tag == IPP_TAG_ENUM)
2574
job_state = (ipp_jstate_t)attr->values[0].integer;
2575
else if (!strcmp(attr->name, "job-originating-user-name") &&
2576
(attr->value_tag == IPP_TAG_NAME ||
2577
attr->value_tag == IPP_TAG_NAMELANG))
2578
job_user = attr->values[0].string.text;
2579
2580
attr = attr->next;
2581
}
2582
2583
if (job_id > 0 && job_name && !strcmp(job_name, monitor->job_name) &&
2584
job_user && monitor->user && !strcmp(job_user, monitor->user))
2585
{
2586
monitor->job_id = job_id;
2587
monitor->job_state = job_state;
2588
break;
2589
}
2590
2591
if (!attr)
2592
break;
2593
}
2594
}
2595
2596
fprintf(stderr, "DEBUG: (monitor) job-state = %s\n", ippEnumString("job-state", (int)monitor->job_state));
2597
2598
if (!job_canceled &&
2599
(monitor->job_state == IPP_JSTATE_CANCELED ||
2600
monitor->job_state == IPP_JSTATE_ABORTED))
2601
{
2602
job_canceled = -1;
2603
fprintf(stderr, "DEBUG: (monitor) job_canceled = -1\n");
2604
}
2605
2606
if ((attr = ippFindAttribute(response, "job-state-reasons",
2607
IPP_TAG_KEYWORD)) != NULL)
2608
{
2609
int i, new_reasons = 0; /* Looping var, new reasons */
2610
2611
for (i = 0; i < attr->num_values; i ++)
2612
{
2613
if (!strcmp(attr->values[i].string.text, "account-authorization-failed"))
2614
new_reasons |= _CUPS_JSR_ACCOUNT_AUTHORIZATION_FAILED;
2615
else if (!strcmp(attr->values[i].string.text, "account-closed"))
2616
new_reasons |= _CUPS_JSR_ACCOUNT_CLOSED;
2617
else if (!strcmp(attr->values[i].string.text, "account-info-needed"))
2618
new_reasons |= _CUPS_JSR_ACCOUNT_INFO_NEEDED;
2619
else if (!strcmp(attr->values[i].string.text, "account-limit-reached"))
2620
new_reasons |= _CUPS_JSR_ACCOUNT_LIMIT_REACHED;
2621
else if (!strcmp(attr->values[i].string.text, "job-password-wait"))
2622
new_reasons |= _CUPS_JSR_JOB_PASSWORD_WAIT;
2623
else if (!strcmp(attr->values[i].string.text, "job-release-wait"))
2624
new_reasons |= _CUPS_JSR_JOB_RELEASE_WAIT;
2625
else if (!strcmp(attr->values[i].string.text, "document-format-error"))
2626
new_reasons |= _CUPS_JSR_DOCUMENT_FORMAT_ERROR;
2627
else if (!strcmp(attr->values[i].string.text, "document-unprintable"))
2628
new_reasons |= _CUPS_JSR_DOCUMENT_UNPRINTABLE;
2629
2630
if (!job_canceled && (!strncmp(attr->values[i].string.text, "job-canceled-", 13) || !strcmp(attr->values[i].string.text, "aborted-by-system")))
2631
job_canceled = 1;
2632
}
2633
2634
if (new_reasons != monitor->job_reasons)
2635
{
2636
if (new_reasons & _CUPS_JSR_ACCOUNT_AUTHORIZATION_FAILED)
2637
fputs("JOBSTATE: account-authorization-failed\n", stderr);
2638
else if (new_reasons & _CUPS_JSR_ACCOUNT_CLOSED)
2639
fputs("JOBSTATE: account-closed\n", stderr);
2640
else if (new_reasons & _CUPS_JSR_ACCOUNT_INFO_NEEDED)
2641
fputs("JOBSTATE: account-info-needed\n", stderr);
2642
else if (new_reasons & _CUPS_JSR_ACCOUNT_LIMIT_REACHED)
2643
fputs("JOBSTATE: account-limit-reached\n", stderr);
2644
else if (new_reasons & _CUPS_JSR_JOB_PASSWORD_WAIT)
2645
fputs("JOBSTATE: job-password-wait\n", stderr);
2646
else if (new_reasons & _CUPS_JSR_JOB_RELEASE_WAIT)
2647
fputs("JOBSTATE: job-release-wait\n", stderr);
2648
else if (new_reasons & (_CUPS_JSR_DOCUMENT_FORMAT_ERROR | _CUPS_JSR_DOCUMENT_UNPRINTABLE))
2649
{
2650
if (monitor->retryable)
2651
{
2652
/*
2653
* Can't print this, so retry as raster...
2654
*/
2655
2656
job_canceled = 1;
2657
fputs("JOBSTATE: cups-retry-as-raster\n", stderr);
2658
}
2659
else if (new_reasons & _CUPS_JSR_DOCUMENT_FORMAT_ERROR)
2660
{
2661
fputs("JOBSTATE: document-format-error\n", stderr);
2662
}
2663
else
2664
{
2665
fputs("JOBSTATE: document-unprintable\n", stderr);
2666
}
2667
}
2668
else
2669
fputs("JOBSTATE: job-printing\n", stderr);
2670
2671
monitor->job_reasons = new_reasons;
2672
}
2673
}
2674
2675
ippDelete(response);
2676
2677
fprintf(stderr, "DEBUG: (monitor) job-state = %s\n", ippEnumString("job-state", (int)monitor->job_state));
2678
2679
if (!job_canceled &&
2680
(monitor->job_state == IPP_JSTATE_CANCELED ||
2681
monitor->job_state == IPP_JSTATE_ABORTED))
2682
job_canceled = -1;
2683
}
2684
2685
/*
2686
* Sleep for N seconds...
2687
*/
2688
2689
monitor_sleep:
2690
2691
sleep((unsigned)delay);
2692
2693
delay = _cupsNextDelay(delay, &prev_delay);
2694
}
2695
2696
/*
2697
* Cancel the job if necessary...
2698
*/
2699
2700
if (job_canceled > 0 && monitor->job_id > 0)
2701
{
2702
if (httpGetFd(http) < 0)
2703
httpReconnect2(http, 30000, NULL);
2704
2705
if (httpGetFd(http) >= 0)
2706
{
2707
cancel_job(http, monitor->uri, monitor->job_id, monitor->resource,
2708
monitor->user, monitor->version);
2709
2710
if (cupsLastError() > IPP_STATUS_OK_CONFLICTING)
2711
{
2712
fprintf(stderr, "DEBUG: (monitor) cancel_job() = %s\n", cupsLastErrorString());
2713
_cupsLangPrintFilter(stderr, "ERROR", _("Unable to cancel print job."));
2714
}
2715
}
2716
}
2717
2718
/*
2719
* Cleanup and return...
2720
*/
2721
2722
httpClose(http);
2723
2724
return (NULL);
2725
}
2726
2727
2728
/*
2729
* 'new_request()' - Create a new print creation or validation request.
2730
*/
2731
2732
static ipp_t * /* O - Request data */
2733
new_request(
2734
ipp_op_t op, /* I - IPP operation code */
2735
int version, /* I - IPP version number */
2736
const char *uri, /* I - printer-uri value */
2737
const char *user, /* I - requesting-user-name value */
2738
const char *title, /* I - job-name value */
2739
int num_options, /* I - Number of options to send */
2740
cups_option_t *options, /* I - Options to send */
2741
const char *compression, /* I - compression value or NULL */
2742
int copies, /* I - copies value or 0 */
2743
const char *format, /* I - document-format value or NULL */
2744
_ppd_cache_t *pc, /* I - PPD cache and mapping data */
2745
ppd_file_t *ppd, /* I - PPD file data */
2746
ipp_attribute_t *media_col_sup, /* I - media-col-supported values */
2747
ipp_attribute_t *doc_handling_sup, /* I - multiple-document-handling-supported values */
2748
ipp_attribute_t *print_color_mode_sup)
2749
/* I - Printer supports print-color-mode */
2750
{
2751
ipp_t *request; /* Request data */
2752
const char *keyword; /* PWG keyword */
2753
2754
2755
/*
2756
* Create the IPP request...
2757
*/
2758
2759
request = ippNewRequest(op);
2760
ippSetVersion(request, version / 10, version % 10);
2761
2762
fprintf(stderr, "DEBUG: %s IPP/%d.%d\n",
2763
ippOpString(request->request.op.operation_id),
2764
request->request.op.version[0],
2765
request->request.op.version[1]);
2766
2767
/*
2768
* Add standard attributes...
2769
*/
2770
2771
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
2772
fprintf(stderr, "DEBUG: printer-uri=\"%s\"\n", uri);
2773
2774
if (user && *user)
2775
{
2776
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", NULL, user);
2777
fprintf(stderr, "DEBUG: requesting-user-name=\"%s\"\n", user);
2778
}
2779
2780
if (title && *title)
2781
{
2782
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL, title);
2783
fprintf(stderr, "DEBUG: job-name=\"%s\"\n", title);
2784
}
2785
2786
if (format && op != IPP_OP_CREATE_JOB)
2787
{
2788
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE, "document-format", NULL, format);
2789
fprintf(stderr, "DEBUG: document-format=\"%s\"\n", format);
2790
}
2791
2792
#ifdef HAVE_LIBZ
2793
if (compression && op != IPP_OP_CREATE_JOB && op != IPP_OP_VALIDATE_JOB)
2794
{
2795
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "compression", NULL, compression);
2796
fprintf(stderr, "DEBUG: compression=\"%s\"\n", compression);
2797
}
2798
#endif /* HAVE_LIBZ */
2799
2800
/*
2801
* Handle options on the command-line...
2802
*/
2803
2804
if (num_options > 0)
2805
{
2806
if (pc)
2807
{
2808
/*
2809
* Send standard IPP attributes...
2810
*/
2811
2812
fputs("DEBUG: Adding standard IPP operation/job attributes.\n", stderr);
2813
2814
copies = _cupsConvertOptions(request, ppd, pc, media_col_sup, doc_handling_sup, print_color_mode_sup, user, format, copies, num_options, options);
2815
2816
/*
2817
* Map FaxOut options...
2818
*/
2819
2820
if ((keyword = cupsGetOption("phone", num_options, options)) != NULL)
2821
{
2822
ipp_t *destination; /* destination collection */
2823
char phone[1024], /* Phone number string */
2824
*ptr, /* Pointer into string */
2825
tel_uri[1024]; /* tel: URI */
2826
static const char * const allowed = "0123456789#*-+.()pw";
2827
/* Allowed characters */
2828
2829
/*
2830
* Unescape and filter out spaces and other characters that are not
2831
* allowed in a tel: URI.
2832
*/
2833
2834
_httpDecodeURI(phone, keyword, sizeof(phone));
2835
ptr = phone;
2836
2837
/*
2838
* Weed out "Custom." in the beginning, this allows to put the
2839
* "phone" option as custom string option into the PPD so that
2840
* print dialogs not supporting fax display the option and
2841
* allow entering the phone number. Print dialogs also send "None"
2842
* if no phone number got entered, filter this, too.
2843
*/
2844
if (!_cups_strcasecmp(phone, "None"))
2845
*ptr = '\0';
2846
if (!_cups_strncasecmp(phone, "Custom.", 7))
2847
_cups_strcpy(ptr, ptr + 7);
2848
2849
for (; *ptr;)
2850
{
2851
if (*ptr == ',')
2852
*ptr = 'p';
2853
else if (!strchr(allowed, *ptr))
2854
_cups_strcpy(ptr, ptr + 1);
2855
else
2856
ptr ++;
2857
}
2858
2859
if (strlen(phone) > 0)
2860
{
2861
destination = ippNew();
2862
2863
httpAssembleURI(HTTP_URI_CODING_ALL, tel_uri, sizeof(tel_uri), "tel", NULL, NULL, 0, phone);
2864
ippAddString(destination, IPP_TAG_JOB, IPP_TAG_URI, "destination-uri", NULL, tel_uri);
2865
fprintf(stderr, "DEBUG: Faxing to phone %s; destination-uri: %s\n", phone, tel_uri);
2866
2867
if ((keyword = cupsGetOption("faxPrefix", num_options, options)) != NULL && *keyword)
2868
{
2869
char predial[1024]; /* Pre-dial string */
2870
2871
_httpDecodeURI(predial, keyword, sizeof(predial));
2872
ptr = predial;
2873
if (!_cups_strcasecmp(ptr, "None"))
2874
*ptr = '\0';
2875
if (!_cups_strncasecmp(ptr, "Custom.", 7))
2876
ptr += 7;
2877
if (strlen(ptr) > 0)
2878
{
2879
ippAddString(destination, IPP_TAG_JOB, IPP_TAG_TEXT, "pre-dial-string", NULL, ptr);
2880
fprintf(stderr, "DEBUG: Pre-dialing %s; pre-dial-string: %s\n", ptr, ptr);
2881
}
2882
else
2883
fprintf(stderr, "WARNING: Pre-dial number for fax not valid! Sending fax without pre-dial number.\n");
2884
}
2885
2886
ippAddCollection(request, IPP_TAG_JOB, "destination-uris", destination);
2887
ippDelete(destination);
2888
}
2889
else
2890
fprintf(stderr, "ERROR: Phone number for fax not valid! Fax cannot be sent.\n");
2891
}
2892
}
2893
else
2894
{
2895
/*
2896
* When talking to another CUPS server, send all options...
2897
*/
2898
2899
fputs("DEBUG: Adding all operation/job attributes.\n", stderr);
2900
cupsEncodeOptions2(request, num_options, options, IPP_TAG_OPERATION);
2901
cupsEncodeOptions2(request, num_options, options, IPP_TAG_JOB);
2902
}
2903
2904
if (copies > 1 && (!pc || copies <= pc->max_copies))
2905
ippAddInteger(request, IPP_TAG_JOB, IPP_TAG_INTEGER, "copies", copies);
2906
}
2907
2908
fprintf(stderr, "DEBUG: IPP/%d.%d %s #%d\n", version / 10, version % 10, ippOpString(ippGetOperation(request)), ippGetRequestId(request));
2909
debug_attributes(request);
2910
2911
return (request);
2912
}
2913
2914
2915
/*
2916
* 'password_cb()' - Disable the password prompt for cupsDoFileRequest().
2917
*/
2918
2919
static const char * /* O - Password */
2920
password_cb(const char *prompt, /* I - Prompt (not used) */
2921
http_t *http, /* I - Connection */
2922
const char *method, /* I - Request method (not used) */
2923
const char *resource, /* I - Resource path (not used) */
2924
int *password_tries) /* I - Password tries */
2925
{
2926
char def_username[HTTP_MAX_VALUE]; /* Default username */
2927
2928
2929
fprintf(stderr, "DEBUG: password_cb(prompt=\"%s\", http=%p, method=\"%s\", "
2930
"resource=\"%s\", password_tries=%p(%d)), password=%p\n",
2931
prompt, http, method, resource, password_tries, *password_tries,
2932
password);
2933
2934
(void)prompt;
2935
(void)method;
2936
(void)resource;
2937
2938
if (!uri_credentials)
2939
{
2940
/*
2941
* Remember that we need to authenticate...
2942
*/
2943
2944
if (httpGetSubField(http, HTTP_FIELD_WWW_AUTHENTICATE, "username",
2945
def_username))
2946
{
2947
char quoted[HTTP_MAX_VALUE * 2 + 4];
2948
/* Quoted string */
2949
2950
fprintf(stderr, "ATTR: auth-info-default=%s,\n",
2951
quote_string(def_username, quoted, sizeof(quoted)));
2952
}
2953
}
2954
2955
if (password && *password && *password_tries < 3)
2956
{
2957
(*password_tries) ++;
2958
2959
cupsSetUser(username);
2960
2961
return (password);
2962
}
2963
else
2964
{
2965
/*
2966
* Give up after 3 tries or if we don't have a password to begin with...
2967
*/
2968
2969
return (NULL);
2970
}
2971
}
2972
2973
2974
/*
2975
* 'quote_string()' - Quote a string value.
2976
*/
2977
2978
static const char * /* O - Quoted string */
2979
quote_string(const char *s, /* I - String */
2980
char *q, /* I - Quoted string buffer */
2981
size_t qsize) /* I - Size of quoted string buffer */
2982
{
2983
char *qptr, /* Pointer into string buffer */
2984
*qend; /* End of string buffer */
2985
2986
2987
qptr = q;
2988
qend = q + qsize - 5;
2989
2990
if (qend < q)
2991
{
2992
*q = '\0';
2993
return (q);
2994
}
2995
2996
*qptr++ = '\'';
2997
*qptr++ = '\"';
2998
2999
while (*s && qptr < qend)
3000
{
3001
if (*s == '\\' || *s == '\"' || *s == '\'')
3002
{
3003
if (qptr < (qend - 4))
3004
{
3005
*qptr++ = '\\';
3006
*qptr++ = '\\';
3007
*qptr++ = '\\';
3008
}
3009
else
3010
break;
3011
}
3012
3013
*qptr++ = *s++;
3014
}
3015
3016
*qptr++ = '\"';
3017
*qptr++ = '\'';
3018
*qptr = '\0';
3019
3020
return (q);
3021
}
3022
3023
3024
/*
3025
* 'report_attr()' - Report an IPP attribute value.
3026
*/
3027
3028
static void
3029
report_attr(ipp_attribute_t *attr) /* I - Attribute */
3030
{
3031
int i; /* Looping var */
3032
char value[1024], /* Value string */
3033
*valptr; /* Pointer into value string */
3034
const char *cached; /* Cached attribute */
3035
3036
3037
/*
3038
* Convert the attribute values into quoted strings...
3039
*/
3040
3041
for (i = 0, valptr = value;
3042
i < attr->num_values && valptr < (value + sizeof(value) - 10);
3043
i ++)
3044
{
3045
if (i > 0)
3046
*valptr++ = ',';
3047
3048
switch (attr->value_tag)
3049
{
3050
case IPP_TAG_INTEGER :
3051
case IPP_TAG_ENUM :
3052
snprintf(valptr, sizeof(value) - (size_t)(valptr - value), "%d", attr->values[i].integer);
3053
valptr += strlen(valptr);
3054
break;
3055
3056
case IPP_TAG_TEXT :
3057
case IPP_TAG_NAME :
3058
case IPP_TAG_KEYWORD :
3059
quote_string(attr->values[i].string.text, valptr, (size_t)(value + sizeof(value) - valptr));
3060
valptr += strlen(valptr);
3061
break;
3062
3063
default :
3064
/*
3065
* Unsupported value type...
3066
*/
3067
3068
return;
3069
}
3070
}
3071
3072
*valptr = '\0';
3073
3074
_cupsMutexLock(&report_mutex);
3075
3076
if ((cached = cupsGetOption(attr->name, num_attr_cache,
3077
attr_cache)) == NULL || strcmp(cached, value))
3078
{
3079
/*
3080
* Tell the scheduler about the new values...
3081
*/
3082
3083
num_attr_cache = cupsAddOption(attr->name, value, num_attr_cache,
3084
&attr_cache);
3085
fprintf(stderr, "ATTR: %s=%s\n", attr->name, value);
3086
}
3087
3088
_cupsMutexUnlock(&report_mutex);
3089
}
3090
3091
3092
/*
3093
* 'report_printer_state()' - Report the printer state.
3094
*/
3095
3096
static void
3097
report_printer_state(ipp_t *ipp) /* I - IPP response */
3098
{
3099
ipp_attribute_t *pa, /* printer-alert */
3100
*pam, /* printer-alert-message */
3101
*pmja, /* printer-mandatory-job-attributes */
3102
*psm, /* printer-state-message */
3103
*reasons, /* printer-state-reasons */
3104
*marker; /* marker-* attributes */
3105
char value[1024], /* State/message string */
3106
*valptr; /* Pointer into string */
3107
static int ipp_supplies = -1;
3108
/* Report supply levels? */
3109
3110
3111
/*
3112
* Report alerts and messages...
3113
*/
3114
3115
if ((pa = ippFindAttribute(ipp, "printer-alert", IPP_TAG_STRING)) != NULL)
3116
report_attr(pa);
3117
3118
if ((pam = ippFindAttribute(ipp, "printer-alert-message",
3119
IPP_TAG_TEXT)) != NULL)
3120
report_attr(pam);
3121
3122
if ((pmja = ippFindAttribute(ipp, "printer-mandatory-job-attributes", IPP_TAG_KEYWORD)) != NULL)
3123
{
3124
int i, /* Looping var */
3125
count = ippGetCount(pmja); /* Number of values */
3126
3127
for (i = 0, valptr = value; i < count; i ++, valptr += strlen(valptr))
3128
{
3129
if (i)
3130
snprintf(valptr, sizeof(value) - (size_t)(valptr - value), " %s", ippGetString(pmja, i, NULL));
3131
else
3132
strlcpy(value, ippGetString(pmja, i, NULL), sizeof(value));
3133
}
3134
3135
if (strcmp(value, mandatory_attrs))
3136
{
3137
strlcpy(mandatory_attrs, value, sizeof(mandatory_attrs));
3138
fprintf(stderr, "PPD: cupsMandatory=\"%s\"\n", value);
3139
}
3140
}
3141
3142
if ((psm = ippFindAttribute(ipp, "printer-state-message",
3143
IPP_TAG_TEXT)) != NULL)
3144
{
3145
char *ptr; /* Pointer into message */
3146
3147
3148
strlcpy(value, "INFO: ", sizeof(value));
3149
for (ptr = psm->values[0].string.text, valptr = value + 6;
3150
*ptr && valptr < (value + sizeof(value) - 6);
3151
ptr ++)
3152
{
3153
if (*ptr < ' ' && *ptr > 0 && *ptr != '\t')
3154
{
3155
/*
3156
* Substitute "<XX>" for the control character...
3157
*/
3158
3159
snprintf(valptr, sizeof(value) - (size_t)(valptr - value), "<%02X>", *ptr);
3160
valptr += 4;
3161
}
3162
else
3163
*valptr++ = *ptr;
3164
}
3165
3166
*valptr++ = '\n';
3167
*valptr = '\0';
3168
3169
fputs(value, stderr);
3170
}
3171
3172
/*
3173
* Now report printer-state-reasons, filtering out some of the reasons we never
3174
* want to set...
3175
*/
3176
3177
if ((reasons = ippFindAttribute(ipp, "printer-state-reasons",
3178
IPP_TAG_KEYWORD)) == NULL)
3179
return;
3180
3181
update_reasons(reasons, NULL);
3182
3183
/*
3184
* Relay the current marker-* attribute values...
3185
*/
3186
3187
if (ipp_supplies < 0)
3188
{
3189
ppd_file_t *ppd; /* PPD file */
3190
ppd_attr_t *ppdattr; /* Attribute in PPD file */
3191
3192
if ((ppd = ppdOpenFile(getenv("PPD"))) != NULL &&
3193
(ppdattr = ppdFindAttr(ppd, "cupsIPPSupplies", NULL)) != NULL &&
3194
ppdattr->value && _cups_strcasecmp(ppdattr->value, "true"))
3195
ipp_supplies = 0;
3196
else
3197
ipp_supplies = 1;
3198
3199
ppdClose(ppd);
3200
}
3201
3202
if (ipp_supplies > 0)
3203
{
3204
if ((marker = ippFindAttribute(ipp, "marker-colors", IPP_TAG_NAME)) != NULL)
3205
report_attr(marker);
3206
if ((marker = ippFindAttribute(ipp, "marker-high-levels",
3207
IPP_TAG_INTEGER)) != NULL)
3208
report_attr(marker);
3209
if ((marker = ippFindAttribute(ipp, "marker-levels",
3210
IPP_TAG_INTEGER)) != NULL)
3211
report_attr(marker);
3212
if ((marker = ippFindAttribute(ipp, "marker-low-levels",
3213
IPP_TAG_INTEGER)) != NULL)
3214
report_attr(marker);
3215
if ((marker = ippFindAttribute(ipp, "marker-message",
3216
IPP_TAG_TEXT)) != NULL)
3217
report_attr(marker);
3218
if ((marker = ippFindAttribute(ipp, "marker-names", IPP_TAG_NAME)) != NULL)
3219
report_attr(marker);
3220
if ((marker = ippFindAttribute(ipp, "marker-types",
3221
IPP_TAG_KEYWORD)) != NULL)
3222
report_attr(marker);
3223
}
3224
}
3225
3226
3227
#if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
3228
/*
3229
* 'run_as_user()' - Run the IPP backend as the printing user.
3230
*
3231
* This function uses an XPC-based user agent to run the backend as the printing
3232
* user. We need to do this in order to have access to the user's Kerberos
3233
* credentials.
3234
*/
3235
3236
static int /* O - Exit status */
3237
run_as_user(char *argv[], /* I - Command-line arguments */
3238
uid_t uid, /* I - User ID */
3239
const char *device_uri, /* I - Device URI */
3240
int fd) /* I - File to print */
3241
{
3242
const char *auth_negotiate,/* AUTH_NEGOTIATE env var */
3243
*content_type, /* [FINAL_]CONTENT_TYPE env vars */
3244
*auth_info_required; /* New auth-info-required value */
3245
xpc_connection_t conn; /* Connection to XPC service */
3246
xpc_object_t request; /* Request message dictionary */
3247
__block xpc_object_t response; /* Response message dictionary */
3248
int status = CUPS_BACKEND_FAILED;
3249
/* Status of request */
3250
3251
3252
fprintf(stderr, "DEBUG: Running IPP backend as UID %d.\n", (int)uid);
3253
3254
/*
3255
* Connect to the user agent for the specified UID...
3256
*/
3257
3258
conn = xpc_connection_create_mach_service(kPMPrintUIToolAgent,
3259
dispatch_get_global_queue(0, 0), 0);
3260
if (!conn)
3261
{
3262
_cupsLangPrintFilter(stderr, "ERROR",
3263
_("Unable to start backend process."));
3264
fputs("DEBUG: Unable to create connection to agent.\n", stderr);
3265
goto cleanup;
3266
}
3267
3268
xpc_connection_set_event_handler(conn,
3269
^(xpc_object_t event)
3270
{
3271
xpc_type_t messageType = xpc_get_type(event);
3272
3273
if (messageType == XPC_TYPE_ERROR)
3274
{
3275
if (event == XPC_ERROR_CONNECTION_INTERRUPTED)
3276
fprintf(stderr, "DEBUG: Interrupted connection to service %s.\n",
3277
xpc_connection_get_name(conn));
3278
else if (event == XPC_ERROR_CONNECTION_INVALID)
3279
fprintf(stderr, "DEBUG: Connection invalid for service %s.\n",
3280
xpc_connection_get_name(conn));
3281
else
3282
fprintf(stderr, "DEBUG: Unexpected error for service %s: %s\n",
3283
xpc_connection_get_name(conn),
3284
xpc_dictionary_get_string(event, XPC_ERROR_KEY_DESCRIPTION));
3285
}
3286
});
3287
xpc_connection_set_target_uid(conn, uid);
3288
xpc_connection_resume(conn);
3289
3290
/*
3291
* Try starting the backend...
3292
*/
3293
3294
request = xpc_dictionary_create(NULL, NULL, 0);
3295
xpc_dictionary_set_int64(request, "command", kPMStartJob);
3296
xpc_dictionary_set_string(request, "device-uri", device_uri);
3297
xpc_dictionary_set_string(request, "job-id", argv[1]);
3298
xpc_dictionary_set_string(request, "user", argv[2]);
3299
xpc_dictionary_set_string(request, "title", argv[3]);
3300
xpc_dictionary_set_string(request, "copies", argv[4]);
3301
xpc_dictionary_set_string(request, "options", argv[5]);
3302
if ((auth_info_required = getenv("AUTH_INFO_REQUIRED")) != NULL)
3303
xpc_dictionary_set_string(request, "auth-info-required",
3304
auth_info_required);
3305
if ((auth_negotiate = getenv("AUTH_NEGOTIATE")) != NULL)
3306
xpc_dictionary_set_string(request, "auth-negotiate", auth_negotiate);
3307
if ((content_type = getenv("CONTENT_TYPE")) != NULL)
3308
xpc_dictionary_set_string(request, "content-type", content_type);
3309
if ((content_type = getenv("FINAL_CONTENT_TYPE")) != NULL)
3310
xpc_dictionary_set_string(request, "final-content-type", content_type);
3311
xpc_dictionary_set_fd(request, "stdin", fd);
3312
xpc_dictionary_set_fd(request, "stderr", 2);
3313
xpc_dictionary_set_fd(request, "side-channel", CUPS_SC_FD);
3314
3315
response = xpc_connection_send_message_with_reply_sync(conn, request);
3316
3317
xpc_release(request);
3318
3319
if (response)
3320
{
3321
child_pid = (pid_t)xpc_dictionary_get_int64(response, "child-pid");
3322
3323
xpc_release(response);
3324
3325
if (child_pid)
3326
fprintf(stderr, "DEBUG: Child PID=%d.\n", (int)child_pid);
3327
else
3328
{
3329
_cupsLangPrintFilter(stderr, "ERROR",
3330
_("Unable to start backend process."));
3331
fputs("DEBUG: No child PID.\n", stderr);
3332
goto cleanup;
3333
}
3334
}
3335
else
3336
{
3337
_cupsLangPrintFilter(stderr, "ERROR",
3338
_("Unable to start backend process."));
3339
fputs("DEBUG: No reply from agent.\n", stderr);
3340
goto cleanup;
3341
}
3342
3343
/*
3344
* Then wait for the backend to finish...
3345
*/
3346
3347
request = xpc_dictionary_create(NULL, NULL, 0);
3348
xpc_dictionary_set_int64(request, "command", kPMWaitForJob);
3349
xpc_dictionary_set_fd(request, "stderr", 2);
3350
3351
response = xpc_connection_send_message_with_reply_sync(conn, request);
3352
xpc_release(request);
3353
3354
if (response)
3355
{
3356
status = (int)xpc_dictionary_get_int64(response, "status");
3357
3358
if (status == SIGTERM || status == SIGKILL || status == SIGPIPE)
3359
{
3360
fprintf(stderr, "DEBUG: Child terminated on signal %d.\n", status);
3361
status = CUPS_BACKEND_FAILED;
3362
}
3363
else if (WIFSIGNALED(status))
3364
{
3365
fprintf(stderr, "DEBUG: Child crashed on signal %d.\n", status);
3366
status = CUPS_BACKEND_STOP;
3367
}
3368
else if (WIFEXITED(status))
3369
{
3370
status = WEXITSTATUS(status);
3371
fprintf(stderr, "DEBUG: Child exited with status %d.\n", status);
3372
}
3373
3374
xpc_release(response);
3375
}
3376
else
3377
_cupsLangPrintFilter(stderr, "ERROR",
3378
_("Unable to get backend exit status."));
3379
3380
cleanup:
3381
3382
if (conn)
3383
{
3384
xpc_connection_cancel(conn);
3385
xpc_release(conn);
3386
}
3387
3388
return (status);
3389
}
3390
#endif /* HAVE_GSSAPI && HAVE_XPC */
3391
3392
3393
/*
3394
* 'sigterm_handler()' - Handle 'terminate' signals that stop the backend.
3395
*/
3396
3397
static void
3398
sigterm_handler(int sig) /* I - Signal */
3399
{
3400
(void)sig; /* remove compiler warnings... */
3401
3402
backendMessage("DEBUG: Got SIGTERM.\n");
3403
3404
#if defined(HAVE_GSSAPI) && defined(HAVE_XPC)
3405
if (child_pid)
3406
{
3407
kill(child_pid, sig);
3408
child_pid = 0;
3409
}
3410
#endif /* HAVE_GSSAPI && HAVE_XPC */
3411
3412
if (!job_canceled)
3413
{
3414
/*
3415
* Flag that the job should be canceled...
3416
*/
3417
3418
backendMessage("DEBUG: sigterm_handler: job_canceled = 1.\n");
3419
3420
job_canceled = 1;
3421
return;
3422
}
3423
3424
/*
3425
* The scheduler already tried to cancel us once, now just terminate
3426
* after removing our temp file!
3427
*/
3428
3429
if (tmpfilename[0])
3430
unlink(tmpfilename);
3431
3432
_exit(1);
3433
}
3434
3435
3436
/*
3437
* 'timeout_cb()' - Handle HTTP timeouts.
3438
*/
3439
3440
static int /* O - 1 to continue, 0 to cancel */
3441
timeout_cb(http_t *http, /* I - Connection to server (unused) */
3442
void *user_data) /* I - User data (unused) */
3443
{
3444
(void)http;
3445
(void)user_data;
3446
3447
return (!job_canceled);
3448
}
3449
3450
3451
/*
3452
* 'update_reasons()' - Update the printer-state-reasons values.
3453
*/
3454
3455
static void
3456
update_reasons(ipp_attribute_t *attr, /* I - printer-state-reasons or NULL */
3457
const char *s) /* I - STATE: string or NULL */
3458
{
3459
char op; /* Add (+), remove (-), replace (\0) */
3460
cups_array_t *new_reasons; /* New reasons array */
3461
char *reason, /* Current reason */
3462
add[2048], /* Reasons added string */
3463
*addptr, /* Pointer into add string */
3464
rem[2048], /* Reasons removed string */
3465
*remptr; /* Pointer into remove string */
3466
const char *addprefix, /* Current add string prefix */
3467
*remprefix; /* Current remove string prefix */
3468
3469
3470
fprintf(stderr, "DEBUG: update_reasons(attr=%d(%s%s), s=\"%s\")\n",
3471
attr ? attr->num_values : 0, attr ? attr->values[0].string.text : "",
3472
attr && attr->num_values > 1 ? ",..." : "", s ? s : "(null)");
3473
3474
/*
3475
* Create an array of new reason keyword strings...
3476
*/
3477
3478
if (attr)
3479
{
3480
int i; /* Looping var */
3481
3482
new_reasons = cupsArrayNew((cups_array_func_t)strcmp, NULL);
3483
op = '\0';
3484
3485
for (i = 0; i < attr->num_values; i ++)
3486
{
3487
reason = attr->values[i].string.text;
3488
3489
if (strcmp(reason, "none") &&
3490
strcmp(reason, "none-report") &&
3491
strcmp(reason, "paused") &&
3492
strncmp(reason, "spool-area-full", 15) &&
3493
strcmp(reason, "com.apple.print.recoverable-warning") &&
3494
strncmp(reason, "cups-", 5))
3495
cupsArrayAdd(new_reasons, reason);
3496
}
3497
}
3498
else if (s)
3499
{
3500
if (*s == '+' || *s == '-')
3501
op = *s++;
3502
else
3503
op = '\0';
3504
3505
new_reasons = _cupsArrayNewStrings(s, ',');
3506
}
3507
else
3508
return;
3509
3510
/*
3511
* Compute the changes...
3512
*/
3513
3514
add[0] = '\0';
3515
addprefix = "STATE: +";
3516
addptr = add;
3517
rem[0] = '\0';
3518
remprefix = "STATE: -";
3519
remptr = rem;
3520
3521
fprintf(stderr, "DEBUG2: op='%c', new_reasons=%d, state_reasons=%d\n",
3522
op ? op : ' ', cupsArrayCount(new_reasons),
3523
cupsArrayCount(state_reasons));
3524
3525
_cupsMutexLock(&report_mutex);
3526
3527
if (op == '+')
3528
{
3529
/*
3530
* Add reasons...
3531
*/
3532
3533
for (reason = (char *)cupsArrayFirst(new_reasons);
3534
reason;
3535
reason = (char *)cupsArrayNext(new_reasons))
3536
{
3537
if (!cupsArrayFind(state_reasons, reason))
3538
{
3539
if (!strncmp(reason, "cups-remote-", 12))
3540
{
3541
/*
3542
* If we are setting cups-remote-xxx, remove all other cups-remote-xxx
3543
* keywords...
3544
*/
3545
3546
char *temp; /* Current reason in state_reasons */
3547
3548
cupsArraySave(state_reasons);
3549
3550
for (temp = (char *)cupsArrayFirst(state_reasons);
3551
temp;
3552
temp = (char *)cupsArrayNext(state_reasons))
3553
if (!strncmp(temp, "cups-remote-", 12))
3554
{
3555
snprintf(remptr, sizeof(rem) - (size_t)(remptr - rem), "%s%s", remprefix, temp);
3556
remptr += strlen(remptr);
3557
remprefix = ",";
3558
3559
cupsArrayRemove(state_reasons, temp);
3560
break;
3561
}
3562
3563
cupsArrayRestore(state_reasons);
3564
}
3565
3566
cupsArrayAdd(state_reasons, reason);
3567
3568
snprintf(addptr, sizeof(add) - (size_t)(addptr - add), "%s%s", addprefix, reason);
3569
addptr += strlen(addptr);
3570
addprefix = ",";
3571
}
3572
}
3573
}
3574
else if (op == '-')
3575
{
3576
/*
3577
* Remove reasons...
3578
*/
3579
3580
for (reason = (char *)cupsArrayFirst(new_reasons);
3581
reason;
3582
reason = (char *)cupsArrayNext(new_reasons))
3583
{
3584
if (cupsArrayFind(state_reasons, reason))
3585
{
3586
snprintf(remptr, sizeof(rem) - (size_t)(remptr - rem), "%s%s", remprefix, reason);
3587
remptr += strlen(remptr);
3588
remprefix = ",";
3589
3590
cupsArrayRemove(state_reasons, reason);
3591
}
3592
}
3593
}
3594
else
3595
{
3596
/*
3597
* Replace reasons...
3598
*/
3599
3600
for (reason = (char *)cupsArrayFirst(state_reasons);
3601
reason;
3602
reason = (char *)cupsArrayNext(state_reasons))
3603
{
3604
if (strncmp(reason, "cups-", 5) && !cupsArrayFind(new_reasons, reason))
3605
{
3606
snprintf(remptr, sizeof(rem) - (size_t)(remptr - rem), "%s%s", remprefix, reason);
3607
remptr += strlen(remptr);
3608
remprefix = ",";
3609
3610
cupsArrayRemove(state_reasons, reason);
3611
}
3612
}
3613
3614
for (reason = (char *)cupsArrayFirst(new_reasons);
3615
reason;
3616
reason = (char *)cupsArrayNext(new_reasons))
3617
{
3618
if (!cupsArrayFind(state_reasons, reason))
3619
{
3620
cupsArrayAdd(state_reasons, reason);
3621
3622
snprintf(addptr, sizeof(add) - (size_t)(addptr - add), "%s%s", addprefix, reason);
3623
addptr += strlen(addptr);
3624
addprefix = ",";
3625
}
3626
}
3627
}
3628
3629
cupsArrayDelete(new_reasons);
3630
3631
_cupsMutexUnlock(&report_mutex);
3632
3633
/*
3634
* Report changes and return...
3635
*/
3636
3637
if (add[0] && rem[0])
3638
fprintf(stderr, "%s\n%s\n", add, rem);
3639
else if (add[0])
3640
fprintf(stderr, "%s\n", add);
3641
else if (rem[0])
3642
fprintf(stderr, "%s\n", rem);
3643
}
3644
3645