57cfbd
diff -up cups-2.2.10/backend/failover.c.failover cups-2.2.10/backend/failover.c
57cfbd
--- cups-2.2.10/backend/failover.c.failover	2019-03-15 13:49:11.072784214 +0100
57cfbd
+++ cups-2.2.10/backend/failover.c	2019-03-15 13:49:11.072784214 +0100
57cfbd
@@ -0,0 +1,837 @@
57cfbd
+/*
57cfbd
+ * Failover Backend for the Common UNIX Printing System (CUPS).
57cfbd
+ *
57cfbd
+ * Copyright (c) 2014, Red Hat, Inc.
57cfbd
+ * All rights reserved.
57cfbd
+ *
57cfbd
+ * Redistribution and use in source and binary forms, with or without
57cfbd
+ * modification, are permitted provided that the following conditions
57cfbd
+ * are met:
57cfbd
+ *
57cfbd
+ * * Redistributions of source code must retain the above copyright
57cfbd
+ *   notice, this list of conditions and the following disclaimer.
57cfbd
+ * * Redistributions in binary form must reproduce the above copyright
57cfbd
+ *   notice, this list of conditions and the following disclaimer in the
57cfbd
+ *   documentation and/or other materials provided with the distribution.
57cfbd
+ * * Neither the name of Red Hat, Inc. nor the names of its contributors
57cfbd
+ *   may be used to endorse or promote products derived from this software
57cfbd
+ *   without specific prior written permission.
57cfbd
+ *
57cfbd
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
57cfbd
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
57cfbd
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
57cfbd
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT,
57cfbd
+ * INC.  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
57cfbd
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
57cfbd
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
57cfbd
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
57cfbd
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57cfbd
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
57cfbd
+ * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
57cfbd
+ * DAMAGE.
57cfbd
+ *
57cfbd
+ * Original version by Clark Hale, Red Hat, Inc.
57cfbd
+ *
57cfbd
+ * This backend presents a fake printer that will choose the first
57cfbd
+ * available printer from a list of IPP URIs.
57cfbd
+ *
57cfbd
+ * Option failover contains a comma separated list of IPP URIs.  The
57cfbd
+ * URIs are attempted in-order.
57cfbd
+ *
57cfbd
+ * Option failover-retries contains an integer that indicates how many
57cfbd
+ * times to iterate through the failover list before completely
57cfbd
+ * failing.
57cfbd
+ *
57cfbd
+ * Contents:
57cfbd
+ *   main()                   - Checks each printer in a failover list, and
57cfbd
+ *                              sends job data to the first available printer
57cfbd
+ *   move_job()               - Sends and IPP Move-Job request
57cfbd
+ *   check_printer()          - Checks a printer's attributes to see
57cfbd
+ *                              if it's enabled and accepting jobs
57cfbd
+ *   read_config()            - Read the backends configuration from
57cfbd
+ *                              options
57cfbd
+ *   get_printer_attributes() - Sends an IPP Get-Attributes request to
57cfbd
+ *                              a URI
57cfbd
+ *   sigterm_handler()        - Handle SIGTERM that cancels the job
57cfbd
+ *   password_cb()            - Password call back used to disable password
57cfbd
+ *                              prompt
57cfbd
+ */
57cfbd
+#include <stdlib.h>
57cfbd
+#include <stdio.h>
57cfbd
+#include <string.h>
57cfbd
+#include <sys wait.h="">
57cfbd
+#include <cups http-private.h="">
57cfbd
+#include <cups http.h="">
57cfbd
+#include "backend-private.h"
57cfbd
+
57cfbd
+/*
57cfbd
+ * Return Values
57cfbd
+ */
57cfbd
+typedef enum fo_state_e
57cfbd
+{
57cfbd
+  FO_PRINTER_GOOD = 0,
57cfbd
+  FO_PRINTER_BAD,
57cfbd
+  FO_PRINTER_BUSY,
57cfbd
+  FO_AUTH_REQUIRED
57cfbd
+} fo_state_t;
57cfbd
+
57cfbd
+/*
57cfbd
+ * Constants
57cfbd
+ */
57cfbd
+#define FAILOVER_DEFAULT_RETRIES        (3)
57cfbd
+#define FAILOVER_PASSWORD_RETRIES_MAX   (3)
57cfbd
+
57cfbd
+/*
57cfbd
+ * Local Functions
57cfbd
+ */
57cfbd
+static       int   check_printer(const char *device_uri);
57cfbd
+static       int   read_config(cups_array_t *printer_array, int *retries,
57cfbd
+			       const char *options);
57cfbd
+static       int   get_printer_attributes(const char *device_uri,
57cfbd
+					  ipp_t **attributes);
57cfbd
+static       int   move_job(int jobid, const char *dest);              
57cfbd
+static       void  sigterm_handler(int sig);
57cfbd
+static const char *password_cb(const char *);
57cfbd
+
57cfbd
+/*
57cfbd
+ * Global Variables
57cfbd
+ */
57cfbd
+static int         job_canceled = 0;     /* Job canceled */
57cfbd
+static char       *password = NULL;      /* password for device */
57cfbd
+static int         password_retries = 0;
57cfbd
+static const char *auth_info_required = "none";
57cfbd
+
57cfbd
+/*
57cfbd
+ * 'main()' - Checks each printer in a failover list, and
57cfbd
+ *            sends job data to the first available printer
57cfbd
+ * Usage:
57cfbd
+ *    printer-uri job-id user title copies options [file]
57cfbd
+ *
57cfbd
+ *    The printer-uri option is not used, but it still required to fit
57cfbd
+ *    to the backend(7) standards.  
57cfbd
+ */
57cfbd
+int
57cfbd
+main(int argc, char *argv[])
57cfbd
+{
57cfbd
+  const char   *selected_uri = NULL;    /* URI of selected printer     */
57cfbd
+  const char   *tmp_device_uri;         /* Device URI to check         */
57cfbd
+  cups_array_t *printer_array;          /* Array of available printers */
57cfbd
+  int           printer_count = 0;      /* current printer array index */
57cfbd
+  int           retry_max = 1;          /* maximum retries before exit */
57cfbd
+  int           retry_count = 0;        /* current retry number        */
57cfbd
+  int           auth_failed_count = 0;  /* auth failures per loop      */
57cfbd
+  int           rc = CUPS_BACKEND_OK; 
57cfbd
+#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
57cfbd
+  struct        sigaction action;       /* Actions for POSIX signals */
57cfbd
+#endif /* HAVE_SIGACTION && !HAVE_SIGSET */
57cfbd
+
57cfbd
+ /*
57cfbd
+  * Check args
57cfbd
+  */
57cfbd
+  if (argc == 1)
57cfbd
+  {
57cfbd
+   /*
57cfbd
+    * print out discovery data
57cfbd
+    */
57cfbd
+    char *backendName;
57cfbd
+
57cfbd
+    if ((backendName = strrchr(argv[0], '/')) != NULL)
57cfbd
+      backendName++;
57cfbd
+    else
57cfbd
+      backendName = argv[0];
57cfbd
+    
57cfbd
+    _cupsLangPrintf(stderr,"network %s \"Unknown\" \"%s (%s)\"\n",
57cfbd
+		    backendName,
57cfbd
+		    _cupsLangString(cupsLangDefault(), _("Failover Printer")),
57cfbd
+		    backendName);
57cfbd
+    
57cfbd
+    return (CUPS_BACKEND_OK);
57cfbd
+  }
57cfbd
+  else if (argc < 6)
57cfbd
+  {
57cfbd
+    _cupsLangPrintf(stderr,
57cfbd
+                    _("Usage: %s job-id user title copies options [file]"),
57cfbd
+		    argv[0]);
57cfbd
+    return (CUPS_BACKEND_STOP);
57cfbd
+  }
57cfbd
+
57cfbd
+  fprintf(stderr, "DEBUG: Failover backend starting up.\n");
57cfbd
+
57cfbd
+ /*
57cfbd
+  * Don't buffer status messages
57cfbd
+  */
57cfbd
+  setbuf(stderr, NULL);
57cfbd
+
57cfbd
+ /*
57cfbd
+  * Ignore SIGPIPE and catch SIGTERM signals...
57cfbd
+  */
57cfbd
+#ifdef HAVE_SIGSET
57cfbd
+  sigset(SIGPIPE, SIG_IGN);
57cfbd
+  sigset(SIGTERM, sigterm_handler);
57cfbd
+#elif defined(HAVE_SIGACTION)
57cfbd
+  memset(&action, 0, sizeof(action));
57cfbd
+  action.sa_handler = SIG_IGN;
57cfbd
+  sigaction(SIGPIPE, &action, NULL);
57cfbd
+
57cfbd
+  sigemptyset(&action.sa_mask);
57cfbd
+  sigaddset(&action.sa_mask, SIGTERM);
57cfbd
+  action.sa_handler = sigterm_handler;
57cfbd
+  sigaction(SIGTERM, &action, NULL);
57cfbd
+#else
57cfbd
+  signal(SIGPIPE, SIG_IGN);
57cfbd
+  signal(SIGTERM, sigterm_handler);
57cfbd
+#endif /* HAVE_SIGSET */
57cfbd
+  
57cfbd
+  printer_array = cupsArrayNew(NULL, NULL);
57cfbd
+
57cfbd
+ /*
57cfbd
+  * Read Configuration
57cfbd
+  */
57cfbd
+  if ((rc = read_config(printer_array, &retry_max,
57cfbd
+			argv[5])) != CUPS_BACKEND_OK)
57cfbd
+  {
57cfbd
+    fprintf(stderr, "ERROR: Failed to read configuration options!\n");
57cfbd
+    goto cleanup;
57cfbd
+  }
57cfbd
+
57cfbd
+ /*
57cfbd
+  * Main Retry Loop
57cfbd
+  */
57cfbd
+  for (retry_count = 0; retry_count < retry_max; retry_count++)
57cfbd
+  {
57cfbd
+    fprintf(stderr, "DEBUG: Retry loop #%d\n", retry_count + 1);
57cfbd
+
57cfbd
+   /*
57cfbd
+    * Reset Counters
57cfbd
+    */
57cfbd
+    printer_count = 0;
57cfbd
+    auth_failed_count = 0;
57cfbd
+        
57cfbd
+    tmp_device_uri = (char *)cupsArrayFirst(printer_array);
57cfbd
+
57cfbd
+    do
57cfbd
+    {
57cfbd
+      if (job_canceled)
57cfbd
+      {
57cfbd
+	fprintf(stderr, "DEBUG: Job Canceled\n");
57cfbd
+	goto cleanup;
57cfbd
+      }
57cfbd
+      
57cfbd
+      fprintf(stderr,"DEBUG: Checking printer #%d: %s\n",
57cfbd
+	      printer_count+1, tmp_device_uri);
57cfbd
+
57cfbd
+      rc = check_printer(tmp_device_uri);
57cfbd
+
57cfbd
+      // Printer is available and not busy.
57cfbd
+      if ( rc == FO_PRINTER_GOOD )      
57cfbd
+      {
57cfbd
+	selected_uri = tmp_device_uri;
57cfbd
+	break;
57cfbd
+      }
57cfbd
+      // Printer is busy
57cfbd
+      else if ( rc == FO_PRINTER_BUSY )
57cfbd
+      {
57cfbd
+        fprintf(stderr, "DEBUG: Waiting for job to complete.\n");
57cfbd
+        sleep(2);
57cfbd
+        continue;
57cfbd
+      }
57cfbd
+      // Authorization is required to access the printer.
57cfbd
+      else if (rc == FO_AUTH_REQUIRED)
57cfbd
+      {
57cfbd
+	auth_failed_count++;
57cfbd
+	fprintf(stderr, "DEBUG: auth_failed_count = %d\n", auth_failed_count);
57cfbd
+      }
57cfbd
+      // Printer is stopped or not accepting jobs
57cfbd
+      else
57cfbd
+      {
57cfbd
+        if (!printer_count) 
57cfbd
+          fprintf(stderr, "INFO: Primary Printer, %s, not available.  "
57cfbd
+                  "Attempting Failovers...\n",
57cfbd
+                  tmp_device_uri);
57cfbd
+        else
57cfbd
+          fprintf(stderr, "INFO: Failover Printer, %s, not available.  "
57cfbd
+                  "Attempting Failovers..\n",
57cfbd
+                  tmp_device_uri);
57cfbd
+        printer_count++;
57cfbd
+        tmp_device_uri = (char *)cupsArrayNext(printer_array);
57cfbd
+      }
57cfbd
+    } while (tmp_device_uri != NULL);
57cfbd
+
57cfbd
+    if (selected_uri && !printer_count)
57cfbd
+      fprintf(stderr, "STATE: -primary-printer-failed\n");
57cfbd
+    else
57cfbd
+      fprintf(stderr, "STATE: +primary-printer-failed\n");
57cfbd
+
57cfbd
+    if (job_canceled)
57cfbd
+    {
57cfbd
+      fprintf(stderr, "DEBUG: Job Canceled\n");
57cfbd
+      goto cleanup;
57cfbd
+    }
57cfbd
+
57cfbd
+    if (!selected_uri && auth_failed_count == printer_count)
57cfbd
+    {
57cfbd
+      fprintf(stderr, "ERROR:  All failover printers failed with "
57cfbd
+	      "authorization issues.\n");
57cfbd
+      rc = CUPS_BACKEND_AUTH_REQUIRED;
57cfbd
+      fprintf(stderr, "ATTR: auth-info-required=%s\n", auth_info_required);
57cfbd
+      goto cleanup;
57cfbd
+    }
57cfbd
+    else if (!selected_uri && retry_count + 1 < retry_max)
57cfbd
+    {
57cfbd
+      fprintf(stderr, "INFO: No suitable printer found...retrying...\n");
57cfbd
+      sleep(2);
57cfbd
+      continue;
57cfbd
+    }
57cfbd
+    else if (selected_uri)
57cfbd
+    {
57cfbd
+      fprintf(stderr, "DEBUG: Using printer, %s.\n", selected_uri);
57cfbd
+      break;
57cfbd
+    }
57cfbd
+  }
57cfbd
+
57cfbd
+  if (!selected_uri)
57cfbd
+  {
57cfbd
+    fprintf(stderr, "ERROR: No suitable printer found.  Aborting print\n");
57cfbd
+    rc = CUPS_BACKEND_FAILED;
57cfbd
+    goto cleanup;
57cfbd
+  }
57cfbd
+
57cfbd
+  rc = move_job(atoi(argv[1]), selected_uri);
57cfbd
+
57cfbd
+  if (job_canceled)
57cfbd
+    rc = CUPS_BACKEND_OK;
57cfbd
+  
57cfbd
+cleanup :
57cfbd
+  if (job_canceled)
57cfbd
+    rc = CUPS_BACKEND_OK;
57cfbd
+
57cfbd
+  tmp_device_uri = (char *)cupsArrayFirst(printer_array);
57cfbd
+  do
57cfbd
+  {
57cfbd
+    free((void *)tmp_device_uri);
57cfbd
+  } while ((tmp_device_uri = (char *)cupsArrayNext(printer_array)) != NULL);
57cfbd
+      
57cfbd
+  cupsArrayDelete(printer_array);
57cfbd
+  sleep(2);
57cfbd
+  return (rc);
57cfbd
+}
57cfbd
+
57cfbd
+/*
57cfbd
+ * 'check_printer()' - Checks the status of a remote printer and returns
57cfbd
+ *                     back a good/bad/busy status.
57cfbd
+ */
57cfbd
+int
57cfbd
+check_printer(const char *device_uri)
57cfbd
+{
57cfbd
+  ipp_t           *attributes = NULL;     /* attributes for device_uri */
57cfbd
+  ipp_attribute_t *tmp_attribute;         /* for examining attribs     */
57cfbd
+  int              rc = FO_PRINTER_GOOD;  /* return code               */
57cfbd
+  char            *reason;                /* printer state reason */
57cfbd
+  int              i;
57cfbd
+
57cfbd
+  fprintf(stderr, "DEBUG: Checking printer %s\n",device_uri);
57cfbd
+  
57cfbd
+  rc = get_printer_attributes(device_uri, &attributes);
57cfbd
+  if ( rc != CUPS_BACKEND_OK )
57cfbd
+  {
57cfbd
+    fprintf(stderr, "DEBUG: Failed to get attributes from printer: %s\n",
57cfbd
+	    device_uri);
57cfbd
+    if ( rc == CUPS_BACKEND_AUTH_REQUIRED )
57cfbd
+      return (FO_AUTH_REQUIRED);
57cfbd
+    else
57cfbd
+      return (FO_PRINTER_BAD);
57cfbd
+  }
57cfbd
+
57cfbd
+ /*
57cfbd
+  * Check if printer is accepting jobs
57cfbd
+  */ 
57cfbd
+  if ((tmp_attribute = ippFindAttribute(attributes,
57cfbd
+					"printer-is-accepting-jobs",
57cfbd
+					IPP_TAG_BOOLEAN)) != NULL &&
57cfbd
+      !tmp_attribute->values[0].boolean)
57cfbd
+  {
57cfbd
+    fprintf(stderr,
57cfbd
+	    "DEBUG: Printer, %s, is not accepting jobs.\n",
57cfbd
+	    device_uri);
57cfbd
+     
57cfbd
+    rc = FO_PRINTER_BAD;
57cfbd
+  }
57cfbd
+
57cfbd
+ /*
57cfbd
+  * Check if printer is stopped or busy processing
57cfbd
+  */
57cfbd
+  if ((tmp_attribute = ippFindAttribute(attributes,
57cfbd
+					"printer-state",
57cfbd
+					IPP_TAG_ENUM)) != NULL)
57cfbd
+  {
57cfbd
+    // Printer Stopped
57cfbd
+    if ( tmp_attribute->values[0].integer == IPP_PRINTER_STOPPED )
57cfbd
+    {
57cfbd
+      fprintf(stderr, "DEBUG: Printer, %s, stopped.\n", device_uri);
57cfbd
+      rc = FO_PRINTER_BAD;
57cfbd
+    }
57cfbd
+    // Printer Busy
57cfbd
+    else if ( tmp_attribute->values[0].integer == IPP_PRINTER_PROCESSING )
57cfbd
+    {
57cfbd
+      fprintf(stderr, "DEBUG: Printer %s is busy.\n", device_uri);
57cfbd
+      rc = FO_PRINTER_BUSY;
57cfbd
+    }
57cfbd
+  }
57cfbd
+
57cfbd
+  /*
57cfbd
+   * Parse through the printer-state-reasons
57cfbd
+   */
57cfbd
+  if ((tmp_attribute = ippFindAttribute(attributes, "printer-state-reasons",
57cfbd
+					IPP_TAG_KEYWORD)) != NULL)
57cfbd
+  {
57cfbd
+    for (i = 0; i < tmp_attribute->num_values; i++)
57cfbd
+    {
57cfbd
+      reason = tmp_attribute->values[i].string.text;
57cfbd
+      int len = strlen(reason);
57cfbd
+      
57cfbd
+      if (len > 8 && !strcmp(reason + len - 8, "-warning"))
57cfbd
+      {
57cfbd
+	fprintf(stderr, "DEBUG: Printer Supply Warning, %s\n", reason);
57cfbd
+	rc = FO_PRINTER_BAD;
57cfbd
+      }
57cfbd
+      else if (len > 6 && !strcmp(reason + len - 6, "-error"))
57cfbd
+      {
57cfbd
+	fprintf(stderr, "DEBUG: Printer Supply Error, %s\n", reason);
57cfbd
+	rc = FO_PRINTER_BAD;
57cfbd
+      }
57cfbd
+    }
57cfbd
+  }
57cfbd
+  
57cfbd
+  return (rc);  
57cfbd
+}
57cfbd
+
57cfbd
+/*
57cfbd
+ * 'read_config()' - Parses the failover and failover-retries options
57cfbd
+ *                   
57cfbd
+ */
57cfbd
+static int
57cfbd
+read_config(cups_array_t *printer_array, int *retries, const char *options)
57cfbd
+{
57cfbd
+
57cfbd
+  const char    *tmp;                   /* temporary ptr                     */
57cfbd
+  char          *tok_tmp;               /* temporary ptr for option  parsing */
57cfbd
+  int            jobopts_count = 0;     /* number of options                 */
57cfbd
+  cups_option_t	*jobopts = NULL;	/* job options                       */
57cfbd
+
57cfbd
+
57cfbd
+  fprintf(stderr, "DEBUG: Reading Configuration.\n");
57cfbd
+  jobopts_count = cupsParseOptions(options, 0, &jobopts);
57cfbd
+
57cfbd
+  if (!jobopts_count)
57cfbd
+  {
57cfbd
+    fprintf(stderr,
57cfbd
+	    "ERROR: No job options!  Cannot find failover options!\n");
57cfbd
+    return (CUPS_BACKEND_STOP);
57cfbd
+  }
57cfbd
+  
57cfbd
+ /*
57cfbd
+  * Get attributes from the primary printer
57cfbd
+  */
57cfbd
+  fprintf(stderr, "DEBUG: Searching for failover option.\n");
57cfbd
+
57cfbd
+  if ((tmp = cupsGetOption("failover", jobopts_count, jobopts)) != NULL)
57cfbd
+  {
57cfbd
+    fprintf(stderr, "DEBUG: Failover option contents: %s.\n", tmp);
57cfbd
+
57cfbd
+    tok_tmp = strdup(tmp);
57cfbd
+
57cfbd
+    tmp = strtok(tok_tmp, ",");
57cfbd
+    do
57cfbd
+    {
57cfbd
+      cupsArrayAdd(printer_array, strdup(tmp));
57cfbd
+    } while ((tmp = strtok(NULL,",")) != NULL);
57cfbd
+
57cfbd
+    free(tok_tmp);
57cfbd
+  }
57cfbd
+  else
57cfbd
+  {
57cfbd
+   /*
57cfbd
+    * The queue is misconfigured, so return back CUPS_BACKEND_STOP
57cfbd
+    */
57cfbd
+    fprintf(stderr, "ERROR: failover option not specified!\n");
57cfbd
+    return (CUPS_BACKEND_STOP);
57cfbd
+  }
57cfbd
+
57cfbd
+ /*
57cfbd
+  * Get the failover-retries value, if it exists.
57cfbd
+  */
57cfbd
+  fprintf(stderr, "DEBUG: Searching for failover-retries option.\n");
57cfbd
+    
57cfbd
+  if ((tmp = cupsGetOption("failover-retries",
57cfbd
+			   jobopts_count, jobopts)) != NULL)
57cfbd
+  {
57cfbd
+    fprintf(stderr, "DEBUG: failover-retries option contents: %s.\n", tmp);
57cfbd
+    *retries = atoi(tmp);
57cfbd
+  }
57cfbd
+  else
57cfbd
+  {
57cfbd
+    *retries = FAILOVER_DEFAULT_RETRIES;
57cfbd
+    fprintf(stderr, "DEBUG: Failed to get failover-retries option\n");
57cfbd
+    fprintf(stderr, "DEBUG: Defaulted to %d retries\n", *retries);
57cfbd
+  }
57cfbd
+
57cfbd
+  return (CUPS_BACKEND_OK);
57cfbd
+}
57cfbd
+
57cfbd
+/*
57cfbd
+ * 'get_printer_attributes()' - Sends an IPP Get-Attributes request to
57cfbd
+ *                              a URI
57cfbd
+ */
57cfbd
+int
57cfbd
+get_printer_attributes(const char *device_uri, ipp_t **attributes)
57cfbd
+{
57cfbd
+  char		     uri[HTTP_MAX_URI];	        /* Updated URI without login */
57cfbd
+  int		     version;     		/* IPP version */
57cfbd
+  char               scheme[256];	       	/* Scheme in URI */
57cfbd
+  ipp_status_t	     ipp_status;    		/* Status of IPP request */
57cfbd
+  char               hostname[1024];		/* Hostname */
57cfbd
+  char               resource[1024];		/* Resource infoo */
57cfbd
+  char               addrname[256];		/* Address name */
57cfbd
+  int                port;                      /* IPP Port number */
57cfbd
+  char		     portname[255];		/* Port as string */
57cfbd
+  http_t	    *http;		        /* HTTP connection */
57cfbd
+  ipp_t		    *request;		        /* IPP request */
57cfbd
+  int                rc = CUPS_BACKEND_OK;      /* Return Code */
57cfbd
+  char		     username[256];             /* Username for device URI */
57cfbd
+  char              *option_ptr;                /* for parsing resource opts */
57cfbd
+  const char * const pattrs[] =	                /* Printer attributes wanted */
57cfbd
+    {
57cfbd
+      "printer-is-accepting-jobs",
57cfbd
+      "printer-state",
57cfbd
+      "printer-state-reasons"
57cfbd
+    };
57cfbd
+
57cfbd
+  if (job_canceled)
57cfbd
+    return (CUPS_BACKEND_OK);
57cfbd
+
57cfbd
+  fprintf(stderr, "DEBUG: Getting Printer Attributes.\n");
57cfbd
+  fprintf(stderr, "DEBUG: Device URL %s.\n", device_uri);
57cfbd
+
57cfbd
+ /*
57cfbd
+  * Parse device_uri
57cfbd
+  */  
57cfbd
+  if (httpSeparateURI(HTTP_URI_CODING_ALL, device_uri, scheme, sizeof(scheme),
57cfbd
+		     username, sizeof(username), hostname, sizeof(hostname),
57cfbd
+		     &port, resource, sizeof(resource)) != HTTP_URI_OK)
57cfbd
+  {
57cfbd
+    fprintf(stderr, "ERROR: Problem parsing device_uri, %s\n", device_uri);
57cfbd
+    return (CUPS_BACKEND_STOP);
57cfbd
+  }
57cfbd
+
57cfbd
+  if (!port)
57cfbd
+    port = IPP_PORT;
57cfbd
+
57cfbd
+  sprintf(portname, "%d", port);
57cfbd
+
57cfbd
+  fprintf(stderr, "DEBUG: Getting Printer Attributes.\n");
57cfbd
+
57cfbd
+ /*
57cfbd
+  * Configure password
57cfbd
+  */
57cfbd
+  cupsSetPasswordCB(password_cb);
57cfbd
+
57cfbd
+ /*
57cfbd
+  * reset, in case a previous attempt for
57cfbd
+  * another printer left residue
57cfbd
+  */
57cfbd
+  cupsSetUser(NULL);
57cfbd
+  password = NULL;
57cfbd
+  password_retries = 0;
57cfbd
+
57cfbd
+  if (*username)
57cfbd
+  {
57cfbd
+    if ((password = strchr(username, ':')) != NULL)
57cfbd
+    {
57cfbd
+      *password = '\0';
57cfbd
+      password++;
57cfbd
+    }
57cfbd
+
57cfbd
+    cupsSetUser(username);
57cfbd
+  }
57cfbd
+  else if (!getuid())
57cfbd
+  {
57cfbd
+    const char *username_env;
57cfbd
+
57cfbd
+    if ((username_env = getenv("AUTH_USERNAME")) != NULL)
57cfbd
+    {
57cfbd
+      cupsSetUser(username_env);
57cfbd
+      password = getenv("AUTH_PASSWORD");
57cfbd
+    }
57cfbd
+  }
57cfbd
+      
57cfbd
+ /*
57cfbd
+  * Try connecting to the remote server...
57cfbd
+  */
57cfbd
+  fprintf(stderr, "DEBUG: Connecting to %s:%d\n", hostname, port);
57cfbd
+  _cupsLangPuts(stderr, _("INFO: Connecting to printer...\n"));
57cfbd
+  
57cfbd
+  http = httpConnectEncrypt(hostname, port, cupsEncryption());
57cfbd
+
57cfbd
+ /*
57cfbd
+  * Deal the socket not being open.
57cfbd
+  */
57cfbd
+  if (!http)
57cfbd
+  {
57cfbd
+    int error = errno;		/* Connection error */
57cfbd
+
57cfbd
+    switch (error)
57cfbd
+    {
57cfbd
+    case EHOSTDOWN :
57cfbd
+      _cupsLangPuts(stderr, _("WARNING: "
57cfbd
+			      "The printer may not exist or "
57cfbd
+			      "is unavailable at this time.\n"));
57cfbd
+      break;
57cfbd
+    case EHOSTUNREACH :
57cfbd
+      _cupsLangPuts(stderr, _("WARNING: "
57cfbd
+			      "The printer is unreachable at this "
57cfbd
+			      "time.\n"));
57cfbd
+      break;
57cfbd
+    case ECONNREFUSED :
57cfbd
+      _cupsLangPuts(stderr, _("WARNING: "
57cfbd
+			      "Connection Refused.\n"));
57cfbd
+      break;
57cfbd
+    default :
57cfbd
+      fprintf(stderr, "DEBUG: Connection error: %s\n", strerror(errno));
57cfbd
+      break;
57cfbd
+    }
57cfbd
+
57cfbd
+    rc = CUPS_BACKEND_FAILED;
57cfbd
+    sleep(5);
57cfbd
+    goto prt_available_cleanup;
57cfbd
+  }
57cfbd
+
57cfbd
+
57cfbd
+#ifdef AF_INET6
57cfbd
+  if (http->hostaddr->addr.sa_family == AF_INET6)
57cfbd
+    fprintf(stderr, "DEBUG: Connected to [%s]:%d (IPv6)...\n",
57cfbd
+	    httpAddrString(http->hostaddr, addrname, sizeof(addrname)),
57cfbd
+	    ntohs(http->hostaddr->ipv6.sin6_port));
57cfbd
+  else
57cfbd
+#endif /* AF_INET6 */
57cfbd
+  if (http->hostaddr->addr.sa_family == AF_INET)
57cfbd
+    fprintf(stderr, "DEBUG: Connected to %s:%d (IPv4)...\n",
57cfbd
+	    httpAddrString(http->hostaddr, addrname, sizeof(addrname)),
57cfbd
+	    ntohs(http->hostaddr->ipv4.sin_port));
57cfbd
+ 
57cfbd
+ /*
57cfbd
+  * Search the resource string for options.
57cfbd
+  * We only care about version, for the moment.
57cfbd
+  */
57cfbd
+  version = 11;
57cfbd
+  
57cfbd
+  if ((option_ptr = strchr(resource, '?')) != NULL)
57cfbd
+  {
57cfbd
+    *option_ptr++ = '\0';
57cfbd
+    
57cfbd
+    if ((option_ptr = strstr(option_ptr, "version="))!=NULL)
57cfbd
+    {
57cfbd
+      int   minor;       /* minor version from URI */
57cfbd
+      int   major;       /* major version from URI */
57cfbd
+      char *version_str; /* ipp version */
57cfbd
+
57cfbd
+      option_ptr += 8;
57cfbd
+      version_str = option_ptr;
57cfbd
+
57cfbd
+      while (*option_ptr && *option_ptr != '&' && *option_ptr != '+')
57cfbd
+	option_ptr++;
57cfbd
+
57cfbd
+      if (*option_ptr)
57cfbd
+	*option_ptr = '\0';
57cfbd
+
57cfbd
+      sscanf(version_str, "%d.%d", &major, &minor);
57cfbd
+
57cfbd
+      version = (major * 10) + minor;
57cfbd
+
57cfbd
+      switch(version)
57cfbd
+      {
57cfbd
+      case 10 :
57cfbd
+      case 11 :
57cfbd
+      case 20 :
57cfbd
+      case 21 :
57cfbd
+	fprintf(stderr,
57cfbd
+		"DEBUG: Set version to %d from URI\n",
57cfbd
+		version);
57cfbd
+	break;
57cfbd
+      default :
57cfbd
+	_cupsLangPrintf(stderr,
57cfbd
+			_("DEBUG: Invalid version, %d, from URI.  "
57cfbd
+			  "Using default of 1.1 \n"),
57cfbd
+			version);
57cfbd
+	version = 11;
57cfbd
+      }
57cfbd
+    }
57cfbd
+  }
57cfbd
+
57cfbd
+
57cfbd
+ /*
57cfbd
+  * Build a URI for the printer.  We can't use the URI in argv[0]
57cfbd
+  * because it might contain username:password information...
57cfbd
+  */
57cfbd
+  if (httpAssembleURI(HTTP_URI_CODING_ALL, uri, sizeof(uri), scheme, NULL,
57cfbd
+		      hostname, port, resource) != HTTP_URI_OK)
57cfbd
+  {
57cfbd
+    fprintf(stderr, "ERROR: Problem assembling printer URI from host %s, "
57cfbd
+	    "port %d, resource %s\n", hostname, port, resource);
57cfbd
+    return (CUPS_BACKEND_STOP);
57cfbd
+  }
57cfbd
+
57cfbd
+ /*
57cfbd
+  * Build the IPP request...
57cfbd
+  */
57cfbd
+  request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES);
57cfbd
+  request->request.op.version[0] = version / 10;
57cfbd
+  request->request.op.version[1] = version % 10;
57cfbd
+
57cfbd
+  ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
57cfbd
+	       NULL, uri);
57cfbd
+
57cfbd
+  ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
57cfbd
+		"requested-attributes", sizeof(pattrs) / sizeof(pattrs[0]),
57cfbd
+		NULL, pattrs);
57cfbd
+
57cfbd
+ /*
57cfbd
+  * Do the request...
57cfbd
+  */
57cfbd
+  fputs("DEBUG: Getting supported attributes...\n", stderr);
57cfbd
+
57cfbd
+  fprintf(stderr, "DEBUG: IPP Request Structure Built.\n");
57cfbd
+
57cfbd
+  *attributes = cupsDoRequest(http, request, resource);
57cfbd
+  ipp_status = cupsLastError();
57cfbd
+
57cfbd
+  fprintf(stderr, "DEBUG: Get-Printer-Attributes: %s (%s)\n",
57cfbd
+	  ippErrorString(ipp_status), cupsLastErrorString());
57cfbd
+
57cfbd
+  if (ipp_status > IPP_OK_CONFLICT)
57cfbd
+  {
57cfbd
+    fprintf(stderr, "DEBUG: Get-Printer-Attributes returned %s.\n",
57cfbd
+	    ippErrorString(ipp_status));
57cfbd
+    switch(ipp_status)
57cfbd
+    {
57cfbd
+    case IPP_FORBIDDEN :
57cfbd
+    case IPP_NOT_AUTHORIZED :
57cfbd
+      _cupsLangPuts(stderr, _("ERROR: Not Authorized.\n"));
57cfbd
+      rc = CUPS_BACKEND_AUTH_REQUIRED;
57cfbd
+      break;
57cfbd
+    case IPP_PRINTER_BUSY :
57cfbd
+    case IPP_SERVICE_UNAVAILABLE :
57cfbd
+      _cupsLangPuts(stderr, _("ERROR: "
57cfbd
+			      "The printer is not responding.\n"));
57cfbd
+      rc = CUPS_BACKEND_FAILED;
57cfbd
+      break;
57cfbd
+    case IPP_BAD_REQUEST :
57cfbd
+    case IPP_VERSION_NOT_SUPPORTED :
57cfbd
+      fprintf(stderr, "ERROR: Destination does not support IPP version %d\n",
57cfbd
+	      version);
57cfbd
+    case IPP_NOT_FOUND :
57cfbd
+      _cupsLangPuts(stderr, _("ERROR: "
57cfbd
+			      "The printer configuration is incorrect or the "
57cfbd
+			      "printer no longer exists.\n"));
57cfbd
+      rc = CUPS_BACKEND_STOP;
57cfbd
+      break;
57cfbd
+    default :
57cfbd
+      rc = CUPS_BACKEND_FAILED;
57cfbd
+    }
57cfbd
+    goto prt_available_cleanup;
57cfbd
+  }
57cfbd
+
57cfbd
+prt_available_cleanup :
57cfbd
+  httpClose(http);
57cfbd
+  return (rc);
57cfbd
+}
57cfbd
+
57cfbd
+static int                              
57cfbd
+move_job(int        jobid,              /* Job ID */
57cfbd
+         const char *dest)              /* Destination ipp address */
57cfbd
+{
57cfbd
+  ipp_t *request;                       /* IPP Request */
57cfbd
+  char  job_uri[HTTP_MAX_URI];          /* job-uri */
57cfbd
+
57cfbd
+  http_t* http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
57cfbd
+
57cfbd
+  if (!http)
57cfbd
+  {
57cfbd
+    _cupsLangPrintf(stderr,
57cfbd
+                    _("failover: Unable to connect to server: %s\n"),
57cfbd
+                    strerror(errno));
57cfbd
+    return (CUPS_BACKEND_FAILED);
57cfbd
+  }
57cfbd
+
57cfbd
+ /*
57cfbd
+  * Build a CUPS_MOVE_JOB request, which requires the following
57cfbd
+  * attributes:
57cfbd
+  *
57cfbd
+  *    job-uri/printer-uri
57cfbd
+  *    job-printer-uri
57cfbd
+  *    requesting-user-name
57cfbd
+  */
57cfbd
+
57cfbd
+  request = ippNewRequest(CUPS_MOVE_JOB);
57cfbd
+
57cfbd
+  snprintf(job_uri, sizeof(job_uri), "ipp://localhost/jobs/%d", jobid);
57cfbd
+  ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL,
57cfbd
+               job_uri);
57cfbd
+
57cfbd
+  ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, 
57cfbd
+               "requesting-user-name",
57cfbd
+               NULL, cupsUser());
57cfbd
+
57cfbd
+  ippAddString(request, IPP_TAG_JOB, IPP_TAG_URI, "job-printer-uri",
57cfbd
+               NULL, dest);
57cfbd
+
57cfbd
+ /*
57cfbd
+  * Do the request and get back a response...
57cfbd
+  */
57cfbd
+
57cfbd
+  ippDelete(cupsDoRequest(http, request, "/jobs"));
57cfbd
+
57cfbd
+  httpClose(http);
57cfbd
+
57cfbd
+  if (cupsLastError() > IPP_OK_CONFLICT)
57cfbd
+  {
57cfbd
+    _cupsLangPrintf(stderr, "failover: %s\n", cupsLastErrorString());
57cfbd
+    return (CUPS_BACKEND_FAILED);
57cfbd
+  }
57cfbd
+  else
57cfbd
+    return (CUPS_BACKEND_OK);
57cfbd
+}
57cfbd
+
57cfbd
+/*
57cfbd
+ * 'sigterm_handler()' - handles a sigterm, i.e. job canceled
57cfbd
+ */
57cfbd
+static void
57cfbd
+sigterm_handler(int sig)
57cfbd
+{
57cfbd
+  if (!job_canceled)
57cfbd
+  {
57cfbd
+    write(2, "DEBUG: Got SIGTERM.\n", 20);
57cfbd
+    job_canceled = 1;
57cfbd
+  }
57cfbd
+  else
57cfbd
+  {
57cfbd
+   /*
57cfbd
+    * Job has already been canceled, so just exit
57cfbd
+    */
57cfbd
+    exit(1);
57cfbd
+  }
57cfbd
+}
57cfbd
+
57cfbd
+/*
57cfbd
+ * 'password_cb()' - Disable the password prompt for cupsDoFileRequest().
57cfbd
+ */
57cfbd
+static const char *			/* O - Password  */
57cfbd
+password_cb(const char *prompt)		/* I - Prompt (not used) */
57cfbd
+{
57cfbd
+  auth_info_required = "username,password";
57cfbd
+  password_retries++;
57cfbd
+
57cfbd
+  if(password_retries < FAILOVER_PASSWORD_RETRIES_MAX) 
57cfbd
+    return (password);
57cfbd
+  else
57cfbd
+    return (NULL);
57cfbd
+}
57cfbd
diff -up cups-2.2.10/backend/Makefile.failover cups-2.2.10/backend/Makefile
57cfbd
--- cups-2.2.10/backend/Makefile.failover	2019-03-15 13:49:11.042784468 +0100
57cfbd
+++ cups-2.2.10/backend/Makefile	2019-03-15 13:50:39.563035149 +0100
57cfbd
@@ -28,6 +28,7 @@ include ../Makedefs
57cfbd
 RBACKENDS =	\
57cfbd
 		ipp \
57cfbd
 		lpd \
57cfbd
+		failover \
57cfbd
 		$(DNSSD_BACKEND)
57cfbd
 UBACKENDS =	\
57cfbd
 		snmp \
57cfbd
@@ -51,6 +52,7 @@ LIBOBJS	=	\
57cfbd
 OBJS	=	\
57cfbd
 		ipp.o \
57cfbd
 		lpd.o \
57cfbd
+		failover.o \
57cfbd
 		dnssd.o \
57cfbd
 		snmp.o \
57cfbd
 		socket.o \
57cfbd
@@ -278,6 +280,13 @@ lpd:	lpd.o ../cups/$(LIBCUPS) libbackend
57cfbd
 	$(LD_CC) $(LDFLAGS) -o lpd lpd.o libbackend.a $(LIBS)
57cfbd
 	$(CODE_SIGN) -s "$(CODE_SIGN_IDENTITY)" $@
57cfbd
 
57cfbd
+#
57cfbd
+# failover
57cfbd
+#
57cfbd
+
57cfbd
+failover:	failover.o ../cups/$(LIBCUPS) libbackend.a
57cfbd
+	echo Linking $@...
57cfbd
+	$(CC) $(LDFLAGS) -o failover failover.o libbackend.a $(LIBS) $(CUPSDLIBS)
57cfbd
 
57cfbd
 #
57cfbd
 # snmp