Blame SOURCES/cups-failover-backend.patch

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