*
* IPP backend for the Common UNIX Printing System (CUPS).
*
- * Copyright 2007-2009 by Apple Inc.
+ * Copyright 2007-2010 by Apple Inc.
* Copyright 1997-2007 by Easy Software Products, all rights reserved.
*
* These coded instructions, statements, and computer programs are the
static char *password = NULL; /* Password for device URI */
static int password_tries = 0; /* Password tries */
+static const char *auth_info_required = "none";
+ /* New auth-info-required value */
#ifdef __APPLE__
static char pstmpname[1024] = ""; /* Temporary PostScript file name */
#endif /* __APPLE__ */
int num_options; /* Number of printer options */
cups_option_t *options; /* Printer options */
const char *device_uri; /* Device URI */
- char method[255], /* Method in URI */
+ char scheme[255], /* Scheme in URI */
hostname[1024], /* Hostname */
username[255], /* Username info */
resource[1024], /* Resource info (printer name) */
*response, /* IPP response */
*supported; /* get-printer-attributes response */
time_t start_time; /* Time of first connect */
- int recoverable; /* Recoverable error shown? */
int contimeout; /* Connection timeout */
int delay; /* Delay for retries... */
int compression, /* Do compression of the job data? */
int version; /* IPP version */
static const char * const pattrs[] =
{ /* Printer attributes we want */
- "com.apple.print.recoverable-message",
"copies-supported",
"document-format-supported",
"marker-colors",
+ "marker-high-levels",
"marker-levels",
+ "marker-low-levels",
"marker-message",
"marker-names",
"marker-types",
if ((device_uri = cupsBackendDeviceURI(argv)) == NULL)
return (CUPS_BACKEND_FAILED);
- if (httpSeparateURI(HTTP_URI_CODING_ALL, device_uri,
- method, sizeof(method), username, sizeof(username),
- hostname, sizeof(hostname), &port,
- resource, sizeof(resource)) < HTTP_URI_OK)
- {
- _cupsLangPuts(stderr,
- _("ERROR: Missing device URI on command-line and no "
- "DEVICE_URI environment variable!\n"));
- return (CUPS_BACKEND_STOP);
- }
+ httpSeparateURI(HTTP_URI_CODING_ALL, device_uri, scheme, sizeof(scheme),
+ username, sizeof(username), hostname, sizeof(hostname), &port,
+ resource, sizeof(resource));
if (!port)
port = IPP_PORT; /* Default to port 631 */
- if (!strcmp(method, "https"))
+ if (!strcmp(scheme, "https"))
cupsSetEncryption(HTTP_ENCRYPT_ALWAYS);
else
cupsSetEncryption(HTTP_ENCRYPT_IF_REQUESTED);
else
{
_cupsLangPrintf(stderr,
- _("ERROR: Unknown encryption option value \"%s\"!\n"),
+ _("ERROR: Unknown encryption option value \"%s\"\n"),
value);
}
}
else
{
_cupsLangPrintf(stderr,
- _("ERROR: Unknown version option value \"%s\"!\n"),
+ _("ERROR: Unknown version option value \"%s\"\n"),
value);
}
}
*/
_cupsLangPrintf(stderr,
- _("ERROR: Unknown option \"%s\" with value \"%s\"!\n"),
+ _("ERROR: Unknown option \"%s\" with value \"%s\"\n"),
name, value);
}
}
* Copy stdin to a temporary file...
*/
- int fd; /* File descriptor */
- char buffer[8192]; /* Buffer for copying */
- int bytes; /* Number of bytes read */
- off_t tbytes; /* Total bytes copied */
+ int fd; /* File descriptor */
+ http_addrlist_t *addrlist; /* Address list */
+ off_t tbytes; /* Total bytes copied */
+
+
+ fputs("STATE: +connecting-to-device\n", stderr);
+ fprintf(stderr, "DEBUG: Looking up \"%s\"...\n", hostname);
+ if ((addrlist = httpAddrGetList(hostname, AF_UNSPEC, "1")) == NULL)
+ {
+ _cupsLangPrintf(stderr, _("ERROR: Unable to locate printer \'%s\'\n"),
+ hostname);
+ return (CUPS_BACKEND_STOP);
+ }
+
+ snmp_fd = _cupsSNMPOpen(addrlist->addr.addr.sa_family);
if ((fd = cupsTempFd(tmpfilename, sizeof(tmpfilename))) < 0)
{
_cupsLangPuts(stderr, _("INFO: Copying print data...\n"));
- tbytes = 0;
+ tbytes = backendRunLoop(-1, fd, snmp_fd, &(addrlist->addr), 0, 0,
+ backendNetworkSideCB);
- while ((bytes = fread(buffer, 1, sizeof(buffer), stdin)) > 0)
- if (write(fd, buffer, bytes) < bytes)
- {
- _cupsLangPrintError(_("ERROR: Unable to write to temporary file"));
- close(fd);
- unlink(tmpfilename);
- return (CUPS_BACKEND_FAILED);
- }
- else
- tbytes += bytes;
+ if (snmp_fd >= 0)
+ _cupsSNMPClose(snmp_fd);
+
+ httpAddrFreeList(addrlist);
close(fd);
if (tbytes <= 1)
{
- _cupsLangPuts(stderr, _("ERROR: Empty print file!\n"));
+ _cupsLangPuts(stderr, _("ERROR: Empty print file\n"));
unlink(tmpfilename);
return (CUPS_BACKEND_FAILED);
}
* Try connecting to the remote server...
*/
- delay = 5;
- recoverable = 0;
- start_time = time(NULL);
+ delay = 5;
+ start_time = time(NULL);
fputs("STATE: +connecting-to-device\n", stderr);
do
{
- fprintf(stderr, "DEBUG: Connecting to %s:%d\n",
- hostname, port);
+ fprintf(stderr, "DEBUG: Connecting to %s:%d\n", hostname, port);
_cupsLangPuts(stderr, _("INFO: Connecting to printer...\n"));
if ((http = httpConnectEncrypt(hostname, port, cupsEncryption())) == NULL)
{
if (contimeout && (time(NULL) - start_time) > contimeout)
{
- _cupsLangPuts(stderr, _("ERROR: Printer not responding!\n"));
+ _cupsLangPuts(stderr, _("ERROR: Printer not responding\n"));
return (CUPS_BACKEND_FAILED);
}
- recoverable = 1;
-
_cupsLangPrintf(stderr,
- _("WARNING: recoverable: Network host \'%s\' is busy; "
+ _("WARNING: Network host \'%s\' is busy; "
"will retry in %d seconds...\n"),
hostname, delay);
}
else if (h_errno)
{
- _cupsLangPrintf(stderr, _("ERROR: Unable to locate printer \'%s\'!\n"),
+ _cupsLangPrintf(stderr, _("ERROR: Unable to locate printer \'%s\'\n"),
hostname);
return (CUPS_BACKEND_STOP);
}
else
{
- recoverable = 1;
-
fprintf(stderr, "DEBUG: Connection error: %s\n", strerror(errno));
_cupsLangPuts(stderr,
- _("ERROR: recoverable: Unable to connect to printer; will "
- "retry in 30 seconds...\n"));
+ _("ERROR: Unable to connect to printer; will retry in 30 "
+ "seconds...\n"));
sleep(30);
}
}
while (http == NULL);
- if (job_cancelled)
+ if (job_cancelled || !http)
{
if (tmpfilename[0])
unlink(tmpfilename);
* might contain username:password information...
*/
- snprintf(uri, sizeof(uri), "%s://%s:%d%s", method, hostname, port, resource);
+ httpAssembleURI(HTTP_URI_CODING_ALL, uri, sizeof(uri), scheme, NULL, hostname,
+ port, resource);
/*
* First validate the destination and see if the device supports multiple
fputs("DEBUG: Getting supported attributes...\n", stderr);
+ if (http->version < HTTP_1_1)
+ httpReconnect(http);
+
if ((supported = cupsDoRequest(http, request, resource)) == NULL)
ipp_status = cupsLastError();
else
{
if (contimeout && (time(NULL) - start_time) > contimeout)
{
- _cupsLangPuts(stderr, _("ERROR: Printer not responding!\n"));
+ _cupsLangPuts(stderr, _("ERROR: Printer not responding\n"));
return (CUPS_BACKEND_FAILED);
}
- recoverable = 1;
-
_cupsLangPrintf(stderr,
- _("WARNING: recoverable: Network host \'%s\' is busy; "
- "will retry in %d seconds...\n"),
- hostname, delay);
+ _("WARNING: Network host \'%s\' is busy; will retry in "
+ "%d seconds...\n"), hostname, delay);
report_printer_state(supported, 0);
}
else if (ipp_status == IPP_NOT_FOUND)
{
- _cupsLangPuts(stderr, _("ERROR: Destination printer does not exist!\n"));
+ _cupsLangPuts(stderr, _("ERROR: Destination printer does not exist\n"));
if (supported)
ippDelete(supported);
return (CUPS_BACKEND_STOP);
}
+ else if (ipp_status == IPP_NOT_AUTHORIZED || ipp_status == IPP_FORBIDDEN)
+ {
+ if (!strncmp(httpGetField(http, HTTP_FIELD_WWW_AUTHENTICATE),
+ "Negotiate", 9))
+ auth_info_required = "negotiate";
+
+ fprintf(stderr, "ATTR: auth-info-required=%s\n", auth_info_required);
+ return (CUPS_BACKEND_AUTH_REQUIRED);
+ }
else
{
_cupsLangPrintf(stderr,
- _("ERROR: Unable to get printer status (%s)!\n"),
+ _("ERROR: Unable to get printer status (%s)\n"),
cupsLastErrorString());
sleep(10);
}
}
}
- if (recoverable)
- {
- /*
- * If we've shown a recoverable error make sure the printer proxies
- * have a chance to see the recovered message. Not pretty but
- * necessary for now...
- */
-
- fputs("INFO: recovered: \n", stderr);
- sleep(5);
- }
-
/*
* See if the printer supports multiple copies...
*/
* Do the request...
*/
+ if (http->version < HTTP_1_1)
+ httpReconnect(http);
+
if (num_files > 1)
response = cupsDoRequest(http, request, resource);
else
httpReconnect(http);
}
else
- _cupsLangPrintf(stderr, _("ERROR: Print file was not accepted (%s)!\n"),
+ {
+ /*
+ * Update auth-info-required as needed...
+ */
+
+ _cupsLangPrintf(stderr, _("ERROR: Print file was not accepted (%s)\n"),
cupsLastErrorString());
+
+ if (ipp_status == IPP_NOT_AUTHORIZED || ipp_status == IPP_FORBIDDEN)
+ {
+ fprintf(stderr, "DEBUG: WWW-Authenticate=\"%s\"\n",
+ httpGetField(http, HTTP_FIELD_WWW_AUTHENTICATE));
+
+ /*
+ * Normal authentication goes through the password callback, which sets
+ * auth_info_required to "username,password". Kerberos goes directly
+ * through GSSAPI, so look for Negotiate in the WWW-Authenticate header
+ * here and set auth_info_required as needed...
+ */
+
+ if (!strncmp(httpGetField(http, HTTP_FIELD_WWW_AUTHENTICATE),
+ "Negotiate", 9))
+ auth_info_required = "negotiate";
+ }
+ }
}
else if ((job_id_attr = ippFindAttribute(response, "job-id",
IPP_TAG_INTEGER)) == NULL)
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE,
"document-format", NULL, content_type);
+ if (http->version < HTTP_1_1)
+ httpReconnect(http);
+
ippDelete(cupsDoFileRequest(http, request, resource, files[i]));
if (cupsLastError() > IPP_OK_CONFLICT)
* Do the request...
*/
- if (!copies_sup)
+ if (!copies_sup || http->version < HTTP_1_1)
httpReconnect(http);
response = cupsDoRequest(http, request, resource);
ippDelete(response);
_cupsLangPrintf(stderr,
- _("ERROR: Unable to get job %d attributes (%s)!\n"),
+ _("ERROR: Unable to get job %d attributes (%s)\n"),
job_id, cupsLastErrorString());
break;
}
break;
}
}
+ else
+ {
+ /*
+ * If the printer does not return a job-state attribute, it does not
+ * conform to the IPP specification - break out immediately and fail
+ * the job...
+ */
+
+ fputs("DEBUG: No job-state available from printer - stopping queue.\n",
+ stderr);
+ ipp_status = IPP_INTERNAL_ERROR;
+ break;
+ }
}
ippDelete(response);
page_count > start_count)
fprintf(stderr, "PAGE: total %d\n", page_count - start_count);
+#ifdef HAVE_GSSAPI
/*
- * Update auth-info-required as needed...
+ * See if we used Kerberos at all...
*/
- if (ipp_status == IPP_NOT_AUTHORIZED)
- {
- if (!strncmp(httpGetField(http, HTTP_FIELD_WWW_AUTHENTICATE),
- "Negotiate", 9))
- fputs("ATTR: auth-info-required=negotiate\n", stderr);
- else
- fputs("ATTR: auth-info-required=username,password\n", stderr);
- }
+ if (http->gssctx)
+ auth_info_required = "negotiate";
+#endif /* HAVE_GSSAPI */
/*
* Free memory...
* Return the queue status...
*/
- if (ipp_status == IPP_NOT_AUTHORIZED)
+ fprintf(stderr, "ATTR: auth-info-required=%s\n", auth_info_required);
+
+ if (ipp_status == IPP_NOT_AUTHORIZED || ipp_status == IPP_FORBIDDEN)
return (CUPS_BACKEND_AUTH_REQUIRED);
+ else if (ipp_status == IPP_INTERNAL_ERROR)
+ return (CUPS_BACKEND_STOP);
else if (ipp_status > IPP_OK_CONFLICT)
return (CUPS_BACKEND_FAILED);
else
+ {
+ _cupsLangPuts(stderr, _("INFO: Ready to print.\n"));
return (CUPS_BACKEND_OK);
+ }
}
* Do the request...
*/
+ if (http->version < HTTP_1_1)
+ httpReconnect(http);
+
ippDelete(cupsDoRequest(http, request, resource));
if (cupsLastError() > IPP_OK_CONFLICT)
*response; /* IPP response */
static const char * const attrs[] = /* Attributes we want */
{
- "com.apple.print.recoverable-message",
"marker-colors",
"marker-levels",
"marker-message",
* Do the request...
*/
+ if (http->version < HTTP_1_1)
+ httpReconnect(http);
+
if ((response = cupsDoRequest(http, request, resource)) != NULL)
{
report_printer_state(response, job_id);
{
(void)prompt;
+ /*
+ * Remember that we need to authenticate...
+ */
+
+ auth_info_required = "username,password";
+
if (password && *password && password_tries < 3)
{
password_tries ++;
else
{
/*
- * If there is no password set in the device URI, return the
- * "authentication required" exit code...
+ * Give up after 3 tries or if we don't have a password to begin with...
*/
- if (tmpfilename[0])
- unlink(tmpfilename);
-
-#ifdef __APPLE__
- if (pstmpname[0])
- unlink(pstmpname);
-#endif /* __APPLE__ */
-
- fputs("ATTR: auth-info-required=username,password\n", stderr);
-
- exit(CUPS_BACKEND_AUTH_REQUIRED);
-
- return (NULL); /* Eliminate compiler warning */
+ return (NULL);
}
}
{
int i; /* Looping var */
int count; /* Count of reasons shown... */
- ipp_attribute_t *caprm, /* com.apple.print.recoverable-message */
- *psm, /* printer-state-message */
+ ipp_attribute_t *psm, /* printer-state-message */
*reasons, /* printer-state-reasons */
*marker; /* marker-* attributes */
const char *reason; /* Current reason */
- const char *message; /* Message to show */
- char unknown[1024]; /* Unknown message string */
const char *prefix; /* Prefix for STATE: line */
char state[1024]; /* State string */
- cups_lang_t *language; /* Current localization */
- int saw_caprw; /* Saw com.apple.print.recoverable-warning state */
if ((psm = ippFindAttribute(ipp, "printer-state-message",
IPP_TAG_KEYWORD)) == NULL)
return (0);
- saw_caprw = 0;
- state[0] = '\0';
- prefix = "STATE: ";
- language = cupsLangDefault();
+ state[0] = '\0';
+ prefix = "STATE: ";
for (i = 0, count = 0; i < reasons->num_values; i ++)
{
reason = reasons->values[i].string.text;
- if (strcmp(reason, "paused"))
+ if (strcmp(reason, "paused") &&
+ strcmp(reason, "com.apple.print.recoverable-warning"))
{
strlcat(state, prefix, sizeof(state));
strlcat(state, reason, sizeof(state));
prefix = ",";
}
-
- message = "";
-
- if (!strncmp(reason, "media-needed", 12))
- message = _("Media tray needs to be filled.");
- else if (!strncmp(reason, "media-jam", 9))
- message = _("Media jam!");
- else if (!strncmp(reason, "moving-to-paused", 16) ||
- !strncmp(reason, "offline", 7) ||
- !strncmp(reason, "paused", 6) ||
- !strncmp(reason, "shutdown", 8))
- message = _("Printer offline.");
- else if (!strncmp(reason, "toner-low", 9))
- message = _("Toner low.");
- else if (!strncmp(reason, "toner-empty", 11))
- message = _("Out of toner!");
- else if (!strncmp(reason, "cover-open", 10))
- message = _("Cover open.");
- else if (!strncmp(reason, "interlock-open", 14))
- message = _("Interlock open.");
- else if (!strncmp(reason, "door-open", 9))
- message = _("Door open.");
- else if (!strncmp(reason, "input-tray-missing", 18))
- message = _("Media tray missing!");
- else if (!strncmp(reason, "media-low", 9))
- message = _("Media tray almost empty.");
- else if (!strncmp(reason, "media-empty", 11))
- message = _("Media tray empty!");
- else if (!strncmp(reason, "output-tray-missing", 19))
- message = _("Output tray missing!");
- else if (!strncmp(reason, "output-area-almost-full", 23))
- message = _("Output bin almost full.");
- else if (!strncmp(reason, "output-area-full", 16))
- message = _("Output bin full!");
- else if (!strncmp(reason, "marker-supply-low", 17))
- message = _("Ink/toner almost empty.");
- else if (!strncmp(reason, "marker-supply-empty", 19))
- message = _("Ink/toner empty!");
- else if (!strncmp(reason, "marker-waste-almost-full", 24))
- message = _("Ink/toner waste bin almost full.");
- else if (!strncmp(reason, "marker-waste-full", 17))
- message = _("Ink/toner waste bin full!");
- else if (!strncmp(reason, "fuser-over-temp", 15))
- message = _("Fuser temperature high!");
- else if (!strncmp(reason, "fuser-under-temp", 16))
- message = _("Fuser temperature low!");
- else if (!strncmp(reason, "opc-near-eol", 12))
- message = _("OPC almost at end-of-life.");
- else if (!strncmp(reason, "opc-life-over", 13))
- message = _("OPC at end-of-life!");
- else if (!strncmp(reason, "developer-low", 13))
- message = _("Developer almost empty.");
- else if (!strncmp(reason, "developer-empty", 15))
- message = _("Developer empty!");
- else if (!strcmp(reason, "com.apple.print.recoverable-warning"))
- saw_caprw = 1;
- else if (strstr(reason, "error") != NULL)
- {
- message = unknown;
-
- snprintf(unknown, sizeof(unknown), _("Unknown printer error (%s)!"),
- reason);
- }
-
- if (message[0])
- {
- count ++;
- if (strstr(reasons->values[i].string.text, "error"))
- fprintf(stderr, "ERROR: %s\n", _cupsLangString(language, message));
- else if (strstr(reasons->values[i].string.text, "warning"))
- fprintf(stderr, "WARNING: %s\n", _cupsLangString(language, message));
- else
- fprintf(stderr, "INFO: %s\n", _cupsLangString(language, message));
- }
}
- fprintf(stderr, "%s\n", state);
-
- /*
- * Relay com.apple.print.recoverable-message...
- */
-
- if ((caprm = ippFindAttribute(ipp, "com.apple.print.recoverable-message",
- IPP_TAG_TEXT)) != NULL)
- fprintf(stderr, "WARNING: %s: %s\n",
- saw_caprw ? "recoverable" : "recovered",
- caprm->values[0].string.text);
+ if (state[0])
+ fprintf(stderr, "%s\n", state);
/*
* Relay the current marker-* attribute values...
if ((marker = ippFindAttribute(ipp, "marker-colors", IPP_TAG_NAME)) != NULL)
report_attr(marker);
+ if ((marker = ippFindAttribute(ipp, "marker-high-levels",
+ IPP_TAG_INTEGER)) != NULL)
+ report_attr(marker);
if ((marker = ippFindAttribute(ipp, "marker-levels",
IPP_TAG_INTEGER)) != NULL)
report_attr(marker);
+ if ((marker = ippFindAttribute(ipp, "marker-low-levels",
+ IPP_TAG_INTEGER)) != NULL)
+ report_attr(marker);
if ((marker = ippFindAttribute(ipp, "marker-message", IPP_TAG_TEXT)) != NULL)
report_attr(marker);
if ((marker = ippFindAttribute(ipp, "marker-names", IPP_TAG_NAME)) != NULL)
if (!printer)
{
_cupsLangPuts(stderr,
- _("ERROR: PRINTER environment variable not defined!\n"));
+ _("ERROR: PRINTER environment variable not defined\n"));
return (-1);
}
* file...
*/
- close(1);
- dup(fd);
+ dup2(fd, 1);
close(fd);
if (!getuid())
* Change to an unpriviledged user...
*/
- setgid(fileinfo.st_gid);
- setuid(fileinfo.st_uid);
+ if (setgid(fileinfo.st_gid))
+ return (errno);
+
+ if (setuid(fileinfo.st_uid))
+ return (errno);
}
execlp("pictwpstops", printer, argv[1], argv[2], argv[3], argv[4], argv[5],
if (status)
{
if (status >= 256)
- _cupsLangPrintf(stderr, _("ERROR: pictwpstops exited with status %d!\n"),
+ _cupsLangPrintf(stderr, _("ERROR: pictwpstops exited with status %d\n"),
status / 256);
else
- _cupsLangPrintf(stderr, _("ERROR: pictwpstops exited on signal %d!\n"),
+ _cupsLangPrintf(stderr, _("ERROR: pictwpstops exited on signal %d\n"),
status);
return (status);