X-Git-Url: http://git.ipfire.org/?p=thirdparty%2Fcups.git;a=blobdiff_plain;f=cups%2Futil.c;h=110142b65deac660fa1c1015665adc6c0abf92b3;hp=605cd89e80555fa406e2f34b2d98f2815de94d54;hb=076239869a921fff635f11ef9d459eea9a4d8b9f;hpb=75bd9771f6e44fdd887ee90faac46f403aefc0fc diff --git a/cups/util.c b/cups/util.c index 605cd89e8..110142b65 100644 --- a/cups/util.c +++ b/cups/util.c @@ -1,67 +1,25 @@ /* - * "$Id: util.c 7634 2008-06-10 23:22:28Z mike $" + * "$Id$" * - * Printing utilities for the Common UNIX Printing System (CUPS). + * Printing utilities for CUPS. * - * Copyright 2007-2008 by Apple Inc. - * Copyright 1997-2006 by Easy Software Products. + * Copyright 2007-2014 by Apple Inc. + * Copyright 1997-2006 by Easy Software Products. * - * These coded instructions, statements, and computer programs are the - * property of Apple Inc. and are protected by Federal copyright - * law. Distribution and use rights are outlined in the file "LICENSE.txt" - * which should have been included with this file. If this file is - * file is missing or damaged, see the license at "http://www.cups.org/". + * These coded instructions, statements, and computer programs are the + * property of Apple Inc. and are protected by Federal copyright + * law. Distribution and use rights are outlined in the file "LICENSE.txt" + * which should have been included with this file. If this file is + * file is missing or damaged, see the license at "http://www.cups.org/". * - * This file is subject to the Apple OS-Developed Software exception. - * - * Contents: - * - * cupsCancelJob() - Cancel a print job on the default server. - * cupsCancelJob2() - Cancel or purge a print job. - * cupsCreateJob() - Create an empty job. - * cupsFinishDocument() - Finish sending a document. - * cupsFreeJobs() - Free memory used by job data. - * cupsGetClasses() - Get a list of printer classes from the default - * server. - * cupsGetDefault() - Get the default printer or class for the default - * server. - * cupsGetDefault2() - Get the default printer or class for the specified - * server. - * cupsGetJobs() - Get the jobs from the default server. - * cupsGetJobs2() - Get the jobs from the specified server. - * cupsGetPPD() - Get the PPD file for a printer on the default - * server. - * cupsGetPPD2() - Get the PPD file for a printer from the specified - * server. - * cupsGetPPD3() - Get the PPD file for a printer on the specified - * server if it has changed. - * cupsGetPrinters() - Get a list of printers from the default server. - * cupsGetServerPPD() - Get an available PPD file from the server. - * cupsLastError() - Return the last IPP status code. - * cupsLastErrorString() - Return the last IPP status-message. - * cupsPrintFile() - Print a file to a printer or class on the default - * server. - * cupsPrintFile2() - Print a file to a printer or class on the - * specified server. - * cupsPrintFiles() - Print one or more files to a printer or class on - * the default server. - * cupsPrintFiles2() - Print one or more files to a printer or class on - * the specified server. - * cupsStartDocument() - Add a document to a job created with - * cupsCreateJob(). - * _cupsConnect() - Get the default server connection... - * cups_get_printer_uri() - Get the printer-uri-supported attribute for the - * first printer in a class. + * This file is subject to the Apple OS-Developed Software exception. */ /* * Include necessary headers... */ -#include "globals.h" -#include "debug.h" -#include -#include +#include "cups-private.h" #include #include #if defined(WIN32) || defined(__EMX__) @@ -96,7 +54,7 @@ cupsCancelJob(const char *name, /* I - Name of printer or class */ int job_id) /* I - Job ID, @code CUPS_JOBID_CURRENT@ for the current job, or @code CUPS_JOBID_ALL@ for all jobs */ { return (cupsCancelJob2(CUPS_HTTP_DEFAULT, name, job_id, 0) - < IPP_REDIRECTION_OTHER_SITE); + < IPP_STATUS_REDIRECTION_OTHER_SITE); } @@ -112,7 +70,7 @@ cupsCancelJob(const char *name, /* I - Name of printer or class */ * Use the @link cupsLastError@ and @link cupsLastErrorString@ functions to get * the cause of any failure. * - * @since CUPS 1.4@ + * @since CUPS 1.4/OS X 10.6@ */ ipp_status_t /* O - IPP status */ @@ -131,7 +89,7 @@ cupsCancelJob2(http_t *http, /* I - Connection to server or @code CUPS_HTTP_ if (job_id < -1 || (!name && job_id == 0)) { - _cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL)); + _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0); return (0); } @@ -141,7 +99,7 @@ cupsCancelJob2(http_t *http, /* I - Connection to server or @code CUPS_HTTP_ if (!http) if ((http = _cupsConnect()) == NULL) - return (IPP_SERVICE_UNAVAILABLE); + return (IPP_STATUS_ERROR_SERVICE_UNAVAILABLE); /* * Build an IPP_CANCEL_JOB or IPP_PURGE_JOBS request, which requires the following @@ -154,7 +112,7 @@ cupsCancelJob2(http_t *http, /* I - Connection to server or @code CUPS_HTTP_ * [purge-job] or [purge-jobs] */ - request = ippNewRequest(job_id < 0 ? IPP_PURGE_JOBS : IPP_CANCEL_JOB); + request = ippNewRequest(job_id < 0 ? IPP_OP_PURGE_JOBS : IPP_OP_CANCEL_JOB); if (name) { @@ -200,7 +158,7 @@ cupsCancelJob2(http_t *http, /* I - Connection to server or @code CUPS_HTTP_ * print, use the @link cupsPrintFile2@ or @link cupsPrintFiles2@ function * instead. * - * @since CUPS 1.4@ + * @since CUPS 1.4/OS X 10.6@ */ int /* O - Job ID or 0 on error */ @@ -220,7 +178,7 @@ cupsCreateJob( DEBUG_printf(("cupsCreateJob(http=%p, name=\"%s\", title=\"%s\", " - "num_options=%d, options=%p)\n", + "num_options=%d, options=%p)", http, name, title, num_options, options)); /* @@ -229,7 +187,7 @@ cupsCreateJob( if (!name) { - _cupsSetError(IPP_INTERNAL_ERROR, NULL); + _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0); return (0); } @@ -237,9 +195,9 @@ cupsCreateJob( * Build a Create-Job request... */ - if ((request = ippNewRequest(IPP_CREATE_JOB)) == NULL) + if ((request = ippNewRequest(IPP_OP_CREATE_JOB)) == NULL) { - _cupsSetError(IPP_INTERNAL_ERROR, NULL); + _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(ENOMEM), 0); return (0); } @@ -254,7 +212,9 @@ cupsCreateJob( if (title) ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL, title); - cupsEncodeOptions(request, num_options, options); + cupsEncodeOptions2(request, num_options, options, IPP_TAG_OPERATION); + cupsEncodeOptions2(request, num_options, options, IPP_TAG_JOB); + cupsEncodeOptions2(request, num_options, options, IPP_TAG_SUBSCRIPTION); /* * Send the request and get the job-id... @@ -280,7 +240,7 @@ cupsCreateJob( * * The document must have been started using @link cupsStartDocument@. * - * @since CUPS 1.4@ + * @since CUPS 1.4/OS X 10.6@ */ ipp_status_t /* O - Status of document submission */ @@ -346,7 +306,7 @@ cupsGetClasses(char ***classes) /* O - Classes */ if (!classes) { - _cupsSetError(IPP_INTERNAL_ERROR, NULL); + _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0); return (0); } @@ -365,7 +325,7 @@ cupsGetClasses(char ***classes) /* O - Classes */ * requested-attributes */ - request = ippNewRequest(CUPS_GET_CLASSES); + request = ippNewRequest(IPP_OP_CUPS_GET_CLASSES); ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "requested-attributes", NULL, "printer-name"); @@ -380,13 +340,13 @@ cupsGetClasses(char ***classes) /* O - Classes */ { for (attr = response->attrs; attr != NULL; attr = attr->next) if (attr->name != NULL && - strcasecmp(attr->name, "printer-name") == 0 && + _cups_strcasecmp(attr->name, "printer-name") == 0 && attr->value_tag == IPP_TAG_NAME) { if (n == 0) temp = malloc(sizeof(char *)); else - temp = realloc(*classes, sizeof(char *) * (n + 1)); + temp = realloc(*classes, sizeof(char *) * (size_t)(n + 1)); if (temp == NULL) { @@ -449,7 +409,7 @@ cupsGetDefault(void) * functions to get the user-defined default printer, as this function does * not support the lpoptions-defined default printer. * - * @since CUPS 1.1.21@ + * @since CUPS 1.1.21/OS X 10.4@ */ const char * /* O - Default printer or @code NULL@ */ @@ -458,21 +418,15 @@ cupsGetDefault2(http_t *http) /* I - Connection to server or @code CUPS_HTTP_DE ipp_t *request, /* IPP Request */ *response; /* IPP Response */ ipp_attribute_t *attr; /* Current attribute */ - const char *var; /* Environment variable */ _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ /* - * First see if the LPDEST or PRINTER environment variables are - * set... However, if PRINTER is set to "lp", ignore it to work - * around a "feature" in most Linux distributions - the default - * user login scripts set PRINTER to "lp"... + * See if we have a user default printer set... */ - if ((var = getenv("LPDEST")) != NULL) - return (var); - else if ((var = getenv("PRINTER")) != NULL && strcmp(var, "lp") != 0) - return (var); + if (_cupsUserDefault(cg->def_printer, sizeof(cg->def_printer))) + return (cg->def_printer); /* * Connect to the server as needed... @@ -490,7 +444,7 @@ cupsGetDefault2(http_t *http) /* I - Connection to server or @code CUPS_HTTP_DE * attributes-natural-language */ - request = ippNewRequest(CUPS_GET_DEFAULT); + request = ippNewRequest(IPP_OP_CUPS_GET_DEFAULT); /* * Do the request and get back a response... @@ -546,7 +500,7 @@ cupsGetJobs(cups_job_t **jobs, /* O - Job data */ * pending, processing, or held and @code CUPS_WHICHJOBS_COMPLETED@ returns * jobs that are stopped, canceled, aborted, or completed. * - * @since CUPS 1.1.21@ + * @since CUPS 1.1.21/OS X 10.4@ */ int /* O - Number of jobs */ @@ -576,17 +530,17 @@ cupsGetJobs2(http_t *http, /* I - Connection to server or @code CUPS_HTTP_D _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ static const char * const attrs[] = /* Requested attributes */ { + "document-format", "job-id", - "job-priority", "job-k-octets", + "job-name", + "job-originating-user-name", + "job-printer-uri", + "job-priority", "job-state", "time-at-completed", "time-at-creation", - "time-at-processing", - "job-printer-uri", - "document-format", - "job-name", - "job-originating-user-name" + "time-at-processing" }; @@ -596,7 +550,7 @@ cupsGetJobs2(http_t *http, /* I - Connection to server or @code CUPS_HTTP_D if (!jobs) { - _cupsSetError(IPP_INTERNAL_ERROR, NULL); + _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0); return (-1); } @@ -608,15 +562,17 @@ cupsGetJobs2(http_t *http, /* I - Connection to server or @code CUPS_HTTP_D if (name) { if (httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, - "localhost", 0, "/printers/%s", name) != HTTP_URI_OK) + "localhost", 0, "/printers/%s", + name) < HTTP_URI_STATUS_OK) { - _cupsSetError(IPP_INTERNAL_ERROR, NULL); + _cupsSetError(IPP_STATUS_ERROR_INTERNAL, + _("Unable to create printer-uri"), 1); return (-1); } } else - strcpy(uri, "ipp://localhost/jobs"); + strlcpy(uri, "ipp://localhost/", sizeof(uri)); if (!http) if ((http = _cupsConnect()) == NULL) @@ -635,7 +591,7 @@ cupsGetJobs2(http_t *http, /* I - Connection to server or @code CUPS_HTTP_D * requested-attributes */ - request = ippNewRequest(IPP_GET_JOBS); + request = ippNewRequest(IPP_OP_GET_JOBS); ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri); @@ -685,7 +641,7 @@ cupsGetJobs2(http_t *http, /* I - Connection to server or @code CUPS_HTTP_D id = 0; size = 0; priority = 50; - state = IPP_JOB_PENDING; + state = IPP_JSTATE_PENDING; user = "unknown"; dest = NULL; format = "application/octet-stream"; @@ -756,7 +712,7 @@ cupsGetJobs2(http_t *http, /* I - Connection to server or @code CUPS_HTTP_D if (n == 0) temp = malloc(sizeof(cups_job_t)); else - temp = realloc(*jobs, sizeof(cups_job_t) * (n + 1)); + temp = realloc(*jobs, sizeof(cups_job_t) * (size_t)(n + 1)); if (!temp) { @@ -764,7 +720,7 @@ cupsGetJobs2(http_t *http, /* I - Connection to server or @code CUPS_HTTP_D * Ran out of memory! */ - _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno)); + _cupsSetError(IPP_STATUS_ERROR_INTERNAL, NULL, 0); cupsFreeJobs(n, *jobs); *jobs = NULL; @@ -801,7 +757,7 @@ cupsGetJobs2(http_t *http, /* I - Connection to server or @code CUPS_HTTP_D ippDelete(response); } - if (n == 0 && cg->last_error >= IPP_BAD_REQUEST) + if (n == 0 && cg->last_error >= IPP_STATUS_ERROR_BAD_REQUEST) return (-1); else return (n); @@ -815,7 +771,8 @@ cupsGetJobs2(http_t *http, /* I - Connection to server or @code CUPS_HTTP_D * in the class. * * The returned filename is stored in a static buffer and is overwritten with - * each call to @code cupsGetPPD@ or @link cupsGetPPD2@. + * each call to @code cupsGetPPD@ or @link cupsGetPPD2@. The caller "owns" the + * file that is created and must @code unlink@ the returned filename. */ const char * /* O - Filename for PPD file */ @@ -832,7 +789,7 @@ cupsGetPPD(const char *name) /* I - Destination name */ cg->ppd_filename[0] = '\0'; if (cupsGetPPD3(CUPS_HTTP_DEFAULT, name, &modtime, cg->ppd_filename, - sizeof(cg->ppd_filename)) == HTTP_OK) + sizeof(cg->ppd_filename)) == HTTP_STATUS_OK) return (cg->ppd_filename); else return (NULL); @@ -846,9 +803,10 @@ cupsGetPPD(const char *name) /* I - Destination name */ * in the class. * * The returned filename is stored in a static buffer and is overwritten with - * each call to @link cupsGetPPD@ or @code cupsGetPPD2@. + * each call to @link cupsGetPPD@ or @code cupsGetPPD2@. The caller "owns" the + * file that is created and must @code unlink@ the returned filename. * - * @since CUPS 1.1.21@ + * @since CUPS 1.1.21/OS X 10.4@ */ const char * /* O - Filename for PPD file */ @@ -862,7 +820,7 @@ cupsGetPPD2(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DE cg->ppd_filename[0] = '\0'; if (cupsGetPPD3(http, name, &modtime, cg->ppd_filename, - sizeof(cg->ppd_filename)) == HTTP_OK) + sizeof(cg->ppd_filename)) == HTTP_STATUS_OK) return (cg->ppd_filename); else return (NULL); @@ -879,16 +837,17 @@ cupsGetPPD2(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DE * * The "buffer" parameter contains the local PPD filename. If it contains * the empty string, a new temporary file is created, otherwise the existing - * file will be overwritten as needed. + * file will be overwritten as needed. The caller "owns" the file that is + * created and must @code unlink@ the returned filename. * - * On success, @code HTTP_OK@ is returned for a new PPD file and - * @code HTTP_NOT_MODIFIED@ if the existing PPD file is up-to-date. Any other + * On success, @code HTTP_STATUS_OK@ is returned for a new PPD file and + * @code HTTP_STATUS_NOT_MODIFIED@ if the existing PPD file is up-to-date. Any other * status is an error. * * For classes, @code cupsGetPPD3@ returns the PPD file for the first printer * in the class. * - * @since CUPS 1.4@ + * @since CUPS 1.4/OS X 10.6@ */ http_status_t /* O - HTTP status */ @@ -908,6 +867,7 @@ cupsGetPPD3(http_t *http, /* I - HTTP connection or @code CUPS_HTTP_DEFAUL resource[HTTP_MAX_URI]; /* Resource name */ int port; /* Port number */ http_status_t status; /* HTTP status from server */ + char tempfile[1024] = ""; /* Temporary filename */ _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ @@ -916,26 +876,137 @@ cupsGetPPD3(http_t *http, /* I - HTTP connection or @code CUPS_HTTP_DEFAUL */ DEBUG_printf(("cupsGetPPD3(http=%p, name=\"%s\", modtime=%p(%d), buffer=%p, " - "bufsize=%d)\n", http, name ? name : "(null)", modtime, + "bufsize=%d)", http, name, modtime, modtime ? (int)*modtime : 0, buffer, (int)bufsize)); if (!name) { - _cupsSetError(IPP_INTERNAL_ERROR, "No printer name!"); - return (HTTP_NOT_ACCEPTABLE); + _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("No printer name"), 1); + return (HTTP_STATUS_NOT_ACCEPTABLE); } if (!modtime) { - _cupsSetError(IPP_INTERNAL_ERROR, "No modification time!"); - return (HTTP_NOT_ACCEPTABLE); + _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("No modification time"), 1); + return (HTTP_STATUS_NOT_ACCEPTABLE); } if (!buffer || bufsize <= 1) { - _cupsSetError(IPP_INTERNAL_ERROR, "Bad filename buffer!"); - return (HTTP_NOT_ACCEPTABLE); + _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad filename buffer"), 1); + return (HTTP_STATUS_NOT_ACCEPTABLE); + } + +#ifndef WIN32 + /* + * See if the PPD file is available locally... + */ + + if (http) + httpGetHostname(http, hostname, sizeof(hostname)); + else + { + strlcpy(hostname, cupsServer(), sizeof(hostname)); + if (hostname[0] == '/') + strlcpy(hostname, "localhost", sizeof(hostname)); + } + + if (!_cups_strcasecmp(hostname, "localhost")) + { + char ppdname[1024]; /* PPD filename */ + struct stat ppdinfo; /* PPD file information */ + + + snprintf(ppdname, sizeof(ppdname), "%s/ppd/%s.ppd", cg->cups_serverroot, + name); + if (!stat(ppdname, &ppdinfo)) + { + /* + * OK, the file exists, use it! + */ + + if (buffer[0]) + { + unlink(buffer); + + if (symlink(ppdname, buffer) && errno != EEXIST) + { + _cupsSetError(IPP_STATUS_ERROR_INTERNAL, NULL, 0); + + return (HTTP_STATUS_SERVER_ERROR); + } + } + else + { + int tries; /* Number of tries */ + const char *tmpdir; /* TMPDIR environment variable */ + struct timeval curtime; /* Current time */ + + /* + * Previously we put root temporary files in the default CUPS temporary + * directory under /var/spool/cups. However, since the scheduler cleans + * out temporary files there and runs independently of the user apps, we + * don't want to use it unless specifically told to by cupsd. + */ + + if ((tmpdir = getenv("TMPDIR")) == NULL) +# ifdef __APPLE__ + tmpdir = "/private/tmp"; /* /tmp is a symlink to /private/tmp */ +# else + tmpdir = "/tmp"; +# endif /* __APPLE__ */ + + /* + * Make the temporary name using the specified directory... + */ + + tries = 0; + + do + { + /* + * Get the current time of day... + */ + + gettimeofday(&curtime, NULL); + + /* + * Format a string using the hex time values... + */ + + snprintf(buffer, bufsize, "%s/%08lx%05lx", tmpdir, + (unsigned long)curtime.tv_sec, + (unsigned long)curtime.tv_usec); + + /* + * Try to make a symlink... + */ + + if (!symlink(ppdname, buffer)) + break; + + tries ++; + } + while (tries < 1000); + + if (tries >= 1000) + { + _cupsSetError(IPP_STATUS_ERROR_INTERNAL, NULL, 0); + + return (HTTP_STATUS_SERVER_ERROR); + } + } + + if (*modtime >= ppdinfo.st_mtime) + return (HTTP_STATUS_NOT_MODIFIED); + else + { + *modtime = ppdinfo.st_mtime; + return (HTTP_STATUS_OK); + } + } } +#endif /* !WIN32 */ /* * Try finding a printer URI for this printer... @@ -943,13 +1014,14 @@ cupsGetPPD3(http_t *http, /* I - HTTP connection or @code CUPS_HTTP_DEFAUL if (!http) if ((http = _cupsConnect()) == NULL) - return (HTTP_SERVICE_UNAVAILABLE); + return (HTTP_STATUS_SERVICE_UNAVAILABLE); if (!cups_get_printer_uri(http, name, hostname, sizeof(hostname), &port, resource, sizeof(resource), 0)) - return (HTTP_NOT_FOUND); + return (HTTP_STATUS_NOT_FOUND); - DEBUG_printf(("cupsGetPPD3: Printer hostname=\"%s\", port=%d\n", hostname, port)); + DEBUG_printf(("2cupsGetPPD3: Printer hostname=\"%s\", port=%d", hostname, + port)); /* * Remap local hostname to localhost... @@ -957,42 +1029,33 @@ cupsGetPPD3(http_t *http, /* I - HTTP connection or @code CUPS_HTTP_DEFAUL httpGetHostname(NULL, localhost, sizeof(localhost)); - DEBUG_printf(("Local hostname=\"%s\"\n", localhost)); + DEBUG_printf(("2cupsGetPPD3: Local hostname=\"%s\"", localhost)); - if (!strcasecmp(localhost, hostname)) - strcpy(hostname, "localhost"); + if (!_cups_strcasecmp(localhost, hostname)) + strlcpy(hostname, "localhost", sizeof(hostname)); /* * Get the hostname and port number we are connected to... */ httpGetHostname(http, http_hostname, sizeof(http_hostname)); + http_port = httpAddrPort(http->hostaddr); -#ifdef AF_INET6 - if (http->hostaddr->addr.sa_family == AF_INET6) - http_port = ntohs(http->hostaddr->ipv6.sin6_port); - else -#endif /* AF_INET6 */ - if (http->hostaddr->addr.sa_family == AF_INET) - http_port = ntohs(http->hostaddr->ipv4.sin_port); - else - http_port = ippPort(); - - DEBUG_printf(("Connection hostname=\"%s\", port=%d\n", http_hostname, - http_port)); + DEBUG_printf(("2cupsGetPPD3: Connection hostname=\"%s\", port=%d", + http_hostname, http_port)); /* * Reconnect to the correct server as needed... */ - if (!strcasecmp(http_hostname, hostname) && port == http_port) + if (!_cups_strcasecmp(http_hostname, hostname) && port == http_port) http2 = http; - else if ((http2 = httpConnectEncrypt(hostname, port, - cupsEncryption())) == NULL) + else if ((http2 = httpConnect2(hostname, port, NULL, AF_UNSPEC, + cupsEncryption(), 1, 30000, NULL)) == NULL) { - DEBUG_puts("Unable to connect to server!"); + DEBUG_puts("1cupsGetPPD3: Unable to connect to server"); - return (HTTP_SERVICE_UNAVAILABLE); + return (HTTP_STATUS_SERVICE_UNAVAILABLE); } /* @@ -1002,7 +1065,7 @@ cupsGetPPD3(http_t *http, /* I - HTTP connection or @code CUPS_HTTP_DEFAUL if (buffer[0]) fd = open(buffer, O_CREAT | O_TRUNC | O_WRONLY, 0600); else - fd = cupsTempFd(buffer, bufsize); + fd = cupsTempFd(tempfile, sizeof(tempfile)); if (fd < 0) { @@ -1010,12 +1073,12 @@ cupsGetPPD3(http_t *http, /* I - HTTP connection or @code CUPS_HTTP_DEFAUL * Can't open file; close the server connection and return NULL... */ - _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno)); + _cupsSetError(IPP_STATUS_ERROR_INTERNAL, NULL, 0); if (http2 != http) httpClose(http2); - return (HTTP_SERVER_ERROR); + return (HTTP_STATUS_SERVER_ERROR); } /* @@ -1036,29 +1099,24 @@ cupsGetPPD3(http_t *http, /* I - HTTP connection or @code CUPS_HTTP_DEFAUL * See if we actually got the file or an error... */ - if (status == HTTP_OK) - *modtime = httpGetDateTime(httpGetField(http2, HTTP_FIELD_DATE)); - else if (status != HTTP_NOT_MODIFIED) + if (status == HTTP_STATUS_OK) { - switch (status) - { - case HTTP_NOT_FOUND : - _cupsSetError(IPP_NOT_FOUND, httpStatus(status)); - break; - - case HTTP_UNAUTHORIZED : - _cupsSetError(IPP_NOT_AUTHORIZED, httpStatus(status)); - break; + *modtime = httpGetDateTime(httpGetField(http2, HTTP_FIELD_DATE)); - default : - DEBUG_printf(("HTTP error %d mapped to IPP_SERVICE_UNAVAILABLE!\n", - status)); - _cupsSetError(IPP_SERVICE_UNAVAILABLE, httpStatus(status)); - break; - } + if (tempfile[0]) + strlcpy(buffer, tempfile, bufsize); + } + else if (status != HTTP_STATUS_NOT_MODIFIED) + { + _cupsSetHTTPError(status); - unlink(cg->ppd_filename); + if (buffer[0]) + unlink(buffer); + else if (tempfile[0]) + unlink(tempfile); } + else if (tempfile[0]) + unlink(tempfile); if (http2 != http) httpClose(http2); @@ -1067,6 +1125,8 @@ cupsGetPPD3(http_t *http, /* I - HTTP connection or @code CUPS_HTTP_DEFAUL * Return the PPD file... */ + DEBUG_printf(("1cupsGetPPD3: Returning status %d", status)); + return (status); } @@ -1096,7 +1156,7 @@ cupsGetPrinters(char ***printers) /* O - Printers */ if (!printers) { - _cupsSetError(IPP_INTERNAL_ERROR, NULL); + _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0); return (0); } @@ -1119,7 +1179,7 @@ cupsGetPrinters(char ***printers) /* O - Printers */ * requested-attributes */ - request = ippNewRequest(CUPS_GET_PRINTERS); + request = ippNewRequest(IPP_OP_CUPS_GET_PRINTERS); ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "requested-attributes", NULL, "printer-name"); @@ -1140,13 +1200,13 @@ cupsGetPrinters(char ***printers) /* O - Printers */ { for (attr = response->attrs; attr != NULL; attr = attr->next) if (attr->name != NULL && - strcasecmp(attr->name, "printer-name") == 0 && + _cups_strcasecmp(attr->name, "printer-name") == 0 && attr->value_tag == IPP_TAG_NAME) { if (n == 0) temp = malloc(sizeof(char *)); else - temp = realloc(*printers, sizeof(char *) * (n + 1)); + temp = realloc(*printers, sizeof(char *) * (size_t)(n + 1)); if (temp == NULL) { @@ -1189,7 +1249,7 @@ cupsGetPrinters(char ***printers) /* O - Printers */ * overwritten on the next call to @link cupsGetPPD@, @link cupsGetPPD2@, * or @link cupsGetServerPPD@. * - * @since CUPS 1.3@ + * @since CUPS 1.3/OS X 10.5@ */ char * /* O - Name of PPD file or @code NULL@ on error */ @@ -1208,7 +1268,7 @@ cupsGetServerPPD(http_t *http, /* I - Connection to server or @code CUPS_HTT if (!name) { - _cupsSetError(IPP_INTERNAL_ERROR, "No PPD name!"); + _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("No PPD name"), 1); return (NULL); } @@ -1227,7 +1287,7 @@ cupsGetServerPPD(http_t *http, /* I - Connection to server or @code CUPS_HTT * Can't open file; close the server connection and return NULL... */ - _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno)); + _cupsSetError(IPP_STATUS_ERROR_INTERNAL, NULL, 0); return (NULL); } @@ -1236,7 +1296,7 @@ cupsGetServerPPD(http_t *http, /* I - Connection to server or @code CUPS_HTT * Get the PPD file... */ - request = ippNewRequest(CUPS_GET_PPD); + request = ippNewRequest(IPP_OP_CUPS_GET_PPD); ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "ppd-name", NULL, name); @@ -1244,7 +1304,7 @@ cupsGetServerPPD(http_t *http, /* I - Connection to server or @code CUPS_HTT close(fd); - if (cupsLastError() != IPP_OK) + if (cupsLastError() != IPP_STATUS_OK) { unlink(cg->ppd_filename); return (NULL); @@ -1254,30 +1314,6 @@ cupsGetServerPPD(http_t *http, /* I - Connection to server or @code CUPS_HTT } -/* - * 'cupsLastError()' - Return the last IPP status code. - */ - -ipp_status_t /* O - IPP status code from last request */ -cupsLastError(void) -{ - return (_cupsGlobals()->last_error); -} - - -/* - * 'cupsLastErrorString()' - Return the last IPP status-message. - * - * @since CUPS 1.2@ - */ - -const char * /* O - status-message text from last request */ -cupsLastErrorString(void) -{ - return (_cupsGlobals()->last_status_message); -} - - /* * 'cupsPrintFile()' - Print a file to a printer or class on the default server. */ @@ -1290,7 +1326,7 @@ cupsPrintFile(const char *name, /* I - Destination name */ cups_option_t *options) /* I - Options */ { DEBUG_printf(("cupsPrintFile(name=\"%s\", filename=\"%s\", " - "title=\"%s\", num_options=%d, options=%p)\n", + "title=\"%s\", num_options=%d, options=%p)", name, filename, title, num_options, options)); return (cupsPrintFiles2(CUPS_HTTP_DEFAULT, name, 1, &filename, title, @@ -1302,7 +1338,7 @@ cupsPrintFile(const char *name, /* I - Destination name */ * 'cupsPrintFile2()' - Print a file to a printer or class on the specified * server. * - * @since CUPS 1.1.21@ + * @since CUPS 1.1.21/OS X 10.4@ */ int /* O - Job ID or 0 on error */ @@ -1315,7 +1351,7 @@ cupsPrintFile2( cups_option_t *options) /* I - Options */ { DEBUG_printf(("cupsPrintFile2(http=%p, name=\"%s\", filename=\"%s\", " - "title=\"%s\", num_options=%d, options=%p)\n", + "title=\"%s\", num_options=%d, options=%p)", http, name, filename, title, num_options, options)); return (cupsPrintFiles2(http, name, 1, &filename, title, num_options, @@ -1338,10 +1374,9 @@ cupsPrintFiles( cups_option_t *options) /* I - Options */ { DEBUG_printf(("cupsPrintFiles(name=\"%s\", num_files=%d, " - "files=%p, title=\"%s\", num_options=%d, options=%p)\n", + "files=%p, title=\"%s\", num_options=%d, options=%p)", name, num_files, files, title, num_options, options)); - /* * Print the file(s)... */ @@ -1355,7 +1390,7 @@ cupsPrintFiles( * 'cupsPrintFiles2()' - Print one or more files to a printer or class on the * specified server. * - * @since CUPS 1.1.21@ + * @since CUPS 1.1.21/OS X 10.4@ */ int /* O - Job ID or 0 on error */ @@ -1376,10 +1411,13 @@ cupsPrintFiles2( char buffer[8192]; /* Copy buffer */ ssize_t bytes; /* Bytes in buffer */ http_status_t status; /* Status of write */ + _cups_globals_t *cg = _cupsGlobals(); /* Global data */ + ipp_status_t cancel_status; /* Status code to preserve */ + char *cancel_message; /* Error message to preserve */ DEBUG_printf(("cupsPrintFiles2(http=%p, name=\"%s\", num_files=%d, " - "files=%p, title=\"%s\", num_options=%d, options=%p)\n", + "files=%p, title=\"%s\", num_options=%d, options=%p)", http, name, num_files, files, title, num_options, options)); /* @@ -1388,7 +1426,7 @@ cupsPrintFiles2( if (!name || num_files < 1 || !files) { - _cupsSetError(IPP_INTERNAL_ERROR, NULL); + _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(EINVAL), 0); return (0); } @@ -1427,31 +1465,49 @@ cupsPrintFiles2( * Unable to open print file, cancel the job and return... */ - cupsCancelJob2(http, name, job_id, 0); - return (0); + _cupsSetError(IPP_STATUS_ERROR_DOCUMENT_ACCESS, NULL, 0); + goto cancel_job; } status = cupsStartDocument(http, name, job_id, docname, format, - i == (num_files - 1)); + i == (num_files - 1)); - while (status == HTTP_CONTINUE && - (bytes = cupsFileRead(fp, buffer, sizeof(buffer))) > 0) - status = cupsWriteRequestData(http, buffer, bytes); + while (status == HTTP_STATUS_CONTINUE && + (bytes = cupsFileRead(fp, buffer, sizeof(buffer))) > 0) + status = cupsWriteRequestData(http, buffer, (size_t)bytes); cupsFileClose(fp); - if (status != HTTP_CONTINUE || cupsFinishDocument(http, name) != IPP_OK) + if (status != HTTP_STATUS_CONTINUE || cupsFinishDocument(http, name) != IPP_STATUS_OK) { /* * Unable to queue, cancel the job and return... */ - cupsCancelJob2(http, name, job_id, 0); - return (0); + goto cancel_job; } } return (job_id); + + /* + * If we get here, something happened while sending the print job so we need + * to cancel the job without setting the last error (since we need to preserve + * the current error... + */ + + cancel_job: + + cancel_status = cg->last_error; + cancel_message = cg->last_status_message ? + _cupsStrRetain(cg->last_status_message) : NULL; + + cupsCancelJob2(http, name, job_id, 0); + + cg->last_error = cancel_status; + cg->last_status_message = cancel_message; + + return (0); } @@ -1466,7 +1522,7 @@ cupsPrintFiles2( * @code CUPS_FORMAT_TEXT@ are provided for the "format" argument, although * any supported MIME type string can be supplied. * - * @since CUPS 1.4@ + * @since CUPS 1.4/OS X 10.6@ */ http_status_t /* O - HTTP status of request */ @@ -1488,10 +1544,10 @@ cupsStartDocument( * Create a Send-Document request... */ - if ((request = ippNewRequest(IPP_SEND_DOCUMENT)) == NULL) + if ((request = ippNewRequest(IPP_OP_SEND_DOCUMENT)) == NULL) { - _cupsSetError(IPP_INTERNAL_ERROR, NULL); - return (0); + _cupsSetError(IPP_STATUS_ERROR_INTERNAL, strerror(ENOMEM), 0); + return (HTTP_STATUS_ERROR); } httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri), "ipp", @@ -1509,7 +1565,7 @@ cupsStartDocument( if (format) ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE, "document-format", NULL, format); - ippAddBoolean(request, IPP_TAG_OPERATION, "last-document", last_document); + ippAddBoolean(request, IPP_TAG_OPERATION, "last-document", (char)last_document); /* * Send and delete the request, then return the status... @@ -1524,79 +1580,8 @@ cupsStartDocument( /* - * '_cupsConnect()' - Get the default server connection... - */ - -http_t * /* O - HTTP connection */ -_cupsConnect(void) -{ - _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ - - - /* - * See if we are connected to the same server... - */ - - if (cg->http) - { - int port; /* Port for connection */ - - - /* - * Get the port associated with the current connection... - */ - -#ifdef AF_INET6 - if (cg->http->hostaddr->addr.sa_family == AF_INET6) - port = ntohs(cg->http->hostaddr->ipv6.sin6_port); - else -#endif /* AF_INET6 */ - if (cg->http->hostaddr->addr.sa_family == AF_INET) - port = ntohs(cg->http->hostaddr->ipv4.sin_port); - else - port = cg->ipp_port; - - /* - * Compare the connection hostname, port, and encryption settings to - * the cached defaults; these were initialized the first time we - * connected... - */ - - if (strcmp(cg->http->hostname, cg->server) || cg->ipp_port != port || - (cg->http->encryption != cg->encryption && - cg->http->encryption == HTTP_ENCRYPT_NEVER)) - { - /* - * Need to close the current connection because something has changed... - */ - - httpClose(cg->http); - cg->http = NULL; - } - } - - /* - * (Re)connect as needed... - */ - - if (!cg->http) - { - if ((cg->http = httpConnectEncrypt(cupsServer(), ippPort(), - cupsEncryption())) == NULL) - _cupsSetError(IPP_SERVICE_UNAVAILABLE, - errno ? strerror(errno) : "Unable to connect to host."); - } - - /* - * Return the cached connection... - */ - - return (cg->http); -} - - -/* - * 'cups_get_printer_uri()' - Get the printer-uri-supported attribute for the first printer in a class. + * 'cups_get_printer_uri()' - Get the printer-uri-supported attribute for the + * first printer in a class. */ static int /* O - 1 on success, 0 on failure */ @@ -1624,25 +1609,27 @@ cups_get_printer_uri( /* Hostname associated with connection */ static const char * const requested_attrs[] = { /* Requested attributes */ + "device-uri", + "member-uris", "printer-uri-supported", - "printer-type", - "member-uris" + "printer-type" }; - DEBUG_printf(("cups_get_printer_uri(http=%p, name=\"%s\", host=%p, " - "hostsize=%d, resource=%p, resourcesize=%d, depth=%d)\n", - http, name ? name : "(null)", host, hostsize, - resource, resourcesize, depth)); + DEBUG_printf(("7cups_get_printer_uri(http=%p, name=\"%s\", host=%p, " + "hostsize=%d, resource=%p, resourcesize=%d, depth=%d)", + http, name, host, hostsize, resource, resourcesize, depth)); /* * Setup the printer URI... */ if (httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, - "localhost", 0, "/printers/%s", name) != HTTP_URI_OK) + "localhost", 0, "/printers/%s", + name) < HTTP_URI_STATUS_OK) { - _cupsSetError(IPP_INTERNAL_ERROR, "Unable to create printer-uri!"); + _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unable to create printer-uri"), + 1); *host = '\0'; *resource = '\0'; @@ -1650,23 +1637,14 @@ cups_get_printer_uri( return (0); } - DEBUG_printf(("cups_get_printer_uri: printer-uri=\"%s\"\n", uri)); + DEBUG_printf(("9cups_get_printer_uri: printer-uri=\"%s\"", uri)); /* * Get the hostname and port number we are connected to... */ httpGetHostname(http, http_hostname, sizeof(http_hostname)); - -#ifdef AF_INET6 - if (http->hostaddr->addr.sa_family == AF_INET6) - http_port = ntohs(http->hostaddr->ipv6.sin6_port); - else -#endif /* AF_INET6 */ - if (http->hostaddr->addr.sa_family == AF_INET) - http_port = ntohs(http->hostaddr->ipv4.sin_port); - else - http_port = ippPort(); + http_port = httpAddrPort(http->hostaddr); /* * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following @@ -1678,7 +1656,7 @@ cups_get_printer_uri( * requested-attributes */ - request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES); + request = ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES); ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri); @@ -1692,9 +1670,38 @@ cups_get_printer_uri( * Do the request and get back a response... */ - if ((response = cupsDoRequest(http, request, "/")) != NULL) + snprintf(resource, (size_t)resourcesize, "/printers/%s", name); + + if ((response = cupsDoRequest(http, request, resource)) != NULL) { - if ((attr = ippFindAttribute(response, "member-uris", IPP_TAG_URI)) != NULL) + const char *device_uri = NULL; /* device-uri value */ + + if ((attr = ippFindAttribute(response, "device-uri", + IPP_TAG_URI)) != NULL) + device_uri = attr->values[0].string.text; + + if (device_uri && + (!strncmp(device_uri, "ipp://", 6) || + !strncmp(device_uri, "ipps://", 7) || + ((strstr(device_uri, "._ipp.") != NULL || + strstr(device_uri, "._ipps.") != NULL) && + !strcmp(device_uri + strlen(device_uri) - 5, "/cups")))) + { + /* + * Statically-configured shared printer. + */ + + httpSeparateURI(HTTP_URI_CODING_ALL, + _httpResolveURI(device_uri, uri, sizeof(uri), + _HTTP_RESOLVE_DEFAULT, NULL, NULL), + scheme, sizeof(scheme), username, sizeof(username), + host, hostsize, port, resource, resourcesize); + ippDelete(response); + + return (1); + } + else if ((attr = ippFindAttribute(response, "member-uris", + IPP_TAG_URI)) != NULL) { /* * Get the first actual printer name in the class... @@ -1735,12 +1742,13 @@ cups_get_printer_uri( * Found a class! Connect to the right server... */ - if (!strcasecmp(http_hostname, host) && *port == http_port) + if (!_cups_strcasecmp(http_hostname, host) && *port == http_port) http2 = http; - else if ((http2 = httpConnectEncrypt(host, *port, - cupsEncryption())) == NULL) + else if ((http2 = httpConnect2(host, *port, NULL, AF_UNSPEC, + cupsEncryption(), 1, 30000, + NULL)) == NULL) { - DEBUG_puts("Unable to connect to server!"); + DEBUG_puts("8cups_get_printer_uri: Unable to connect to server"); continue; } @@ -1770,14 +1778,18 @@ cups_get_printer_uri( else if ((attr = ippFindAttribute(response, "printer-uri-supported", IPP_TAG_URI)) != NULL) { - httpSeparateURI(HTTP_URI_CODING_ALL, attr->values[0].string.text, + httpSeparateURI(HTTP_URI_CODING_ALL, + _httpResolveURI(attr->values[0].string.text, uri, + sizeof(uri), _HTTP_RESOLVE_DEFAULT, + NULL, NULL), scheme, sizeof(scheme), username, sizeof(username), host, hostsize, port, resource, resourcesize); ippDelete(response); if (!strncmp(resource, "/classes/", 9)) { - _cupsSetError(IPP_INTERNAL_ERROR, _("No printer-uri found for class!")); + _cupsSetError(IPP_STATUS_ERROR_INTERNAL, + _("No printer-uri found for class"), 1); *host = '\0'; *resource = '\0'; @@ -1791,8 +1803,8 @@ cups_get_printer_uri( ippDelete(response); } - if (cupsLastError() != IPP_NOT_FOUND) - _cupsSetError(IPP_INTERNAL_ERROR, _("No printer-uri found!")); + if (cupsLastError() != IPP_STATUS_ERROR_NOT_FOUND) + _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("No printer-uri found"), 1); *host = '\0'; *resource = '\0'; @@ -1802,5 +1814,5 @@ cups_get_printer_uri( /* - * End of "$Id: util.c 7634 2008-06-10 23:22:28Z mike $". + * End of "$Id$". */