static int /* O - 1 on success, 0 on failure */ cups_get_printer_uri( http_t *http, /* I - HTTP connection */ const char *name, /* I - Name of printer or class */ char *host, /* I - Hostname buffer */ int hostsize, /* I - Size of hostname buffer */ int *port, /* O - Port number */ char *resource, /* I - Resource buffer */ int resourcesize, /* I - Size of resource buffer */ int depth) /* I - Depth of query */ { int i; /* Looping var */ int http_port; /* Port number */ http_t *http2; /* Alternate HTTP connection */ ipp_t *request, /* IPP request */ *response; /* IPP response */ ipp_attribute_t *attr; /* Current attribute */ char uri[HTTP_MAX_URI], /* printer-uri attribute */ scheme[HTTP_MAX_URI], /* Scheme name */ username[HTTP_MAX_URI], /* Username:password */ classname[255], /* Temporary class name */ http_hostname[HTTP_MAX_HOST]; /* Hostname associated with connection */ static const char * const requested_attrs[] = { /* Requested attributes */ "printer-uri-supported", "printer-type", "member-uris" }; 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)); /* * Setup the printer URI... */ if (httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, "localhost", 0, "/printers/%s", name) != HTTP_URI_OK) { _cupsSetError(IPP_INTERNAL_ERROR, "Unable to create printer-uri!"); *host = '\0'; *resource = '\0'; return (0); } DEBUG_printf(("cups_get_printer_uri: printer-uri=\"%s\"\n", 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(); /* * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following * attributes: * * attributes-charset * attributes-natural-language * printer-uri * requested-attributes */ request = ippNewRequest(IPP_GET_PRINTER_ATTRIBUTES); ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri); ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requested-attributes", sizeof(requested_attrs) / sizeof(requested_attrs[0]), NULL, requested_attrs); /* * Do the request and get back a response... */ if ((response = cupsDoRequest(http, request, "/")) != NULL) { if ((attr = ippFindAttribute(response, "member-uris", IPP_TAG_URI)) != NULL) { /* * Get the first actual printer name in the class... */ for (i = 0; i < attr->num_values; i ++) { httpSeparateURI(HTTP_URI_CODING_ALL, attr->values[i].string.text, scheme, sizeof(scheme), username, sizeof(username), host, hostsize, port, resource, resourcesize); if (!strncmp(resource, "/printers/", 10)) { /* * Found a printer! */ ippDelete(response); return (1); } } /* * No printers in this class - try recursively looking for a printer, * but not more than 3 levels deep... */ if (depth < 3) { for (i = 0; i < attr->num_values; i ++) { httpSeparateURI(HTTP_URI_CODING_ALL, attr->values[i].string.text, scheme, sizeof(scheme), username, sizeof(username), host, hostsize, port, resource, resourcesize); if (!strncmp(resource, "/classes/", 9)) { /* * Found a class! Connect to the right server... */ if (!strcasecmp(http_hostname, host) && *port == http_port) http2 = http; else if ((http2 = httpConnectEncrypt(host, *port, cupsEncryption())) == NULL) { DEBUG_puts("Unable to connect to server!"); continue; } /* * Look up printers on that server... */ strlcpy(classname, resource + 9, sizeof(classname)); cups_get_printer_uri(http2, classname, host, hostsize, port, resource, resourcesize, depth + 1); /* * Close the connection as needed... */ if (http2 != http) httpClose(http2); if (*host) return (1); } } } } else if ((attr = ippFindAttribute(response, "printer-uri-supported", IPP_TAG_URI)) != NULL) { httpSeparateURI(HTTP_URI_CODING_ALL, attr->values[0].string.text, 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!")); *host = '\0'; *resource = '\0'; return (0); } return (1); } ippDelete(response); } if (cupsLastError() != IPP_NOT_FOUND) _cupsSetError(IPP_INTERNAL_ERROR, _("No printer-uri found!")); *host = '\0'; *resource = '\0'; return (0); }
const char * /* O - Filename for PPD file */ cupsGetPPD2(http_t *http, /* I - HTTP connection */ const char *name) /* I - Printer name */ { int http_port; /* Port number */ char http_hostname[HTTP_MAX_HOST]; /* Hostname associated with connection */ http_t *http2; /* Alternate HTTP connection */ int fd; /* PPD file */ char localhost[HTTP_MAX_URI],/* Local hostname */ hostname[HTTP_MAX_URI], /* Hostname */ resource[HTTP_MAX_URI]; /* Resource name */ int port; /* Port number */ http_status_t status; /* HTTP status from server */ _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */ /* * Range check input... */ DEBUG_printf(("cupsGetPPD2(http=%p, name=\"%s\")\n", http, name ? name : "(null)")); if (!http || !name) { if (!http) _cupsSetError(IPP_INTERNAL_ERROR, "No HTTP connection!"); else _cupsSetError(IPP_INTERNAL_ERROR, "No printer name!"); return (NULL); } /* * Try finding a printer URI for this printer... */ if (!cups_get_printer_uri(http, name, hostname, sizeof(hostname), &port, resource, sizeof(resource), 0)) return (NULL); DEBUG_printf(("cupsGetPPD3: Printer hostname=\"%s\", port=%d\n", hostname, port)); /* * Remap local hostname to localhost... */ httpGetHostname(NULL, localhost, sizeof(localhost)); DEBUG_printf(("Local hostname=\"%s\"\n", localhost)); if (!strcasecmp(localhost, hostname)) strcpy(hostname, "localhost"); /* * 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(); DEBUG_printf(("Connection hostname=\"%s\", port=%d\n", http_hostname, http_port)); /* * Reconnect to the correct server as needed... */ if (!strcasecmp(http_hostname, hostname) && port == http_port) http2 = http; else if ((http2 = httpConnectEncrypt(hostname, port, cupsEncryption())) == NULL) { DEBUG_puts("Unable to connect to server!"); return (NULL); } /* * Get a temp file... */ if ((fd = cupsTempFd(cg->ppd_filename, sizeof(cg->ppd_filename))) < 0) { /* * Can't open file; close the server connection and return NULL... */ _cupsSetError(IPP_INTERNAL_ERROR, strerror(errno)); if (http2 != http) httpClose(http2); return (NULL); } /* * And send a request to the HTTP server... */ strlcat(resource, ".ppd", sizeof(resource)); status = cupsGetFd(http2, resource, fd); close(fd); /* * See if we actually got the file or an error... */ if (status != HTTP_OK) { switch (status) { case HTTP_NOT_FOUND : _cupsSetError(IPP_NOT_FOUND, httpStatus(status)); break; case HTTP_UNAUTHORIZED : _cupsSetError(IPP_NOT_AUTHORIZED, httpStatus(status)); break; default : DEBUG_printf(("HTTP error %d mapped to IPP_SERVICE_UNAVAILABLE!\n", status)); _cupsSetError(IPP_SERVICE_UNAVAILABLE, httpStatus(status)); break; } unlink(cg->ppd_filename); return (NULL); } if (http2 != http) httpClose(http2); /* * Return the PPD file... */ return (cg->ppd_filename); }
http_status_t /* O - HTTP status */ cupsGetPPD3(http_t *http, /* I - HTTP connection or @code CUPS_HTTP_DEFAULT@ */ const char *name, /* I - Destination name */ time_t *modtime, /* IO - Modification time */ char *buffer, /* I - Filename buffer */ size_t bufsize) /* I - Size of filename buffer */ { int http_port; /* Port number */ char http_hostname[HTTP_MAX_HOST]; /* Hostname associated with connection */ http_t *http2; /* Alternate HTTP connection */ int fd; /* PPD file */ char localhost[HTTP_MAX_URI],/* Local hostname */ hostname[HTTP_MAX_URI], /* Hostname */ 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 */ /* * Range check input... */ DEBUG_printf(("cupsGetPPD3(http=%p, name=\"%s\", modtime=%p(%d), buffer=%p, " "bufsize=%d)", http, name, modtime, modtime ? (int)*modtime : 0, buffer, (int)bufsize)); if (!name) { _cupsSetError(IPP_INTERNAL_ERROR, _("No printer name"), 1); return (HTTP_NOT_ACCEPTABLE); } if (!modtime) { _cupsSetError(IPP_INTERNAL_ERROR, _("No modification time"), 1); return (HTTP_NOT_ACCEPTABLE); } if (!buffer || bufsize <= 1) { _cupsSetError(IPP_INTERNAL_ERROR, _("Bad filename buffer"), 1); return (HTTP_NOT_ACCEPTABLE); } #ifndef WIN32 /* * See if the PPD file is available locally... */ if (!cg->servername[0]) cupsServer(); if (!_cups_strcasecmp(cg->servername, "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_INTERNAL_ERROR, NULL, 0); return (HTTP_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_INTERNAL_ERROR, NULL, 0); return (HTTP_SERVER_ERROR); } } if (*modtime >= ppdinfo.st_mtime) return (HTTP_NOT_MODIFIED); else { *modtime = ppdinfo.st_mtime; return (HTTP_OK); } } } #endif /* !WIN32 */ /* * Try finding a printer URI for this printer... */ if (!http) if ((http = _cupsConnect()) == NULL) return (HTTP_SERVICE_UNAVAILABLE); if (!cups_get_printer_uri(http, name, hostname, sizeof(hostname), &port, resource, sizeof(resource), 0)) return (HTTP_NOT_FOUND); DEBUG_printf(("2cupsGetPPD3: Printer hostname=\"%s\", port=%d", hostname, port)); /* * Remap local hostname to localhost... */ httpGetHostname(NULL, localhost, sizeof(localhost)); DEBUG_printf(("2cupsGetPPD3: Local hostname=\"%s\"", localhost)); if (!_cups_strcasecmp(localhost, hostname)) strcpy(hostname, "localhost"); /* * Get the hostname and port number we are connected to... */ httpGetHostname(http, http_hostname, sizeof(http_hostname)); http_port = _httpAddrPort(http->hostaddr); DEBUG_printf(("2cupsGetPPD3: Connection hostname=\"%s\", port=%d", http_hostname, http_port)); /* * Reconnect to the correct server as needed... */ if (!_cups_strcasecmp(http_hostname, hostname) && port == http_port) http2 = http; else if ((http2 = httpConnectEncrypt(hostname, port, cupsEncryption())) == NULL) { DEBUG_puts("1cupsGetPPD3: Unable to connect to server"); return (HTTP_SERVICE_UNAVAILABLE); } /* * Get a temp file... */ if (buffer[0]) fd = open(buffer, O_CREAT | O_TRUNC | O_WRONLY, 0600); else fd = cupsTempFd(tempfile, sizeof(tempfile)); if (fd < 0) { /* * Can't open file; close the server connection and return NULL... */ _cupsSetError(IPP_INTERNAL_ERROR, NULL, 0); if (http2 != http) httpClose(http2); return (HTTP_SERVER_ERROR); } /* * And send a request to the HTTP server... */ strlcat(resource, ".ppd", sizeof(resource)); if (*modtime > 0) httpSetField(http2, HTTP_FIELD_IF_MODIFIED_SINCE, httpGetDateString(*modtime)); status = cupsGetFd(http2, resource, fd); close(fd); /* * See if we actually got the file or an error... */ if (status == HTTP_OK) { *modtime = httpGetDateTime(httpGetField(http2, HTTP_FIELD_DATE)); if (tempfile[0]) strlcpy(buffer, tempfile, bufsize); } else if (status != HTTP_NOT_MODIFIED) { _cupsSetHTTPError(status); if (buffer[0]) unlink(buffer); else if (tempfile[0]) unlink(tempfile); } else if (tempfile[0]) unlink(tempfile); if (http2 != http) httpClose(http2); /* * Return the PPD file... */ DEBUG_printf(("1cupsGetPPD3: Returning status %d", status)); return (status); }