Blame SOURCES/cups-failover-backend.patch

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