Blame SOURCES/cups-failover-backend.patch

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