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