#include "cups-private.h"
#include "ppd-private.h"
#include <fcntl.h>
#include <sys/stat.h>
#if defined(WIN32) || defined(__EMX__)
# include <io.h>
#else
# include <unistd.h>
#endif
static int cups_get_printer_uri(http_t *http, const char *name,
char *host, int hostsize, int *port,
char *resource, int resourcesize,
int depth);
const char *
cupsGetPPD(const char *name)
{
_ppd_globals_t *pg = _ppdGlobals();
time_t modtime = 0;
pg->ppd_filename[0] = '\0';
if (cupsGetPPD3(CUPS_HTTP_DEFAULT, name, &modtime, pg->ppd_filename,
sizeof(pg->ppd_filename)) == HTTP_STATUS_OK)
return (pg->ppd_filename);
else
return (NULL);
}
const char *
cupsGetPPD2(http_t *http,
const char *name)
{
_ppd_globals_t *pg = _ppdGlobals();
time_t modtime = 0;
pg->ppd_filename[0] = '\0';
if (cupsGetPPD3(http, name, &modtime, pg->ppd_filename,
sizeof(pg->ppd_filename)) == HTTP_STATUS_OK)
return (pg->ppd_filename);
else
return (NULL);
}
http_status_t
cupsGetPPD3(http_t *http,
const char *name,
time_t *modtime,
char *buffer,
size_t bufsize)
{
int http_port;
char http_hostname[HTTP_MAX_HOST];
http_t *http2;
int fd;
char localhost[HTTP_MAX_URI],
hostname[HTTP_MAX_URI],
resource[HTTP_MAX_URI];
int port;
http_status_t status;
char tempfile[1024] = "";
_cups_globals_t *cg = _cupsGlobals();
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_STATUS_ERROR_INTERNAL, _("No printer name"), 1);
return (HTTP_STATUS_NOT_ACCEPTABLE);
}
if (!modtime)
{
_cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("No modification time"), 1);
return (HTTP_STATUS_NOT_ACCEPTABLE);
}
if (!buffer || bufsize <= 1)
{
_cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad filename buffer"), 1);
return (HTTP_STATUS_NOT_ACCEPTABLE);
}
#ifndef WIN32
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];
struct stat ppdinfo;
snprintf(ppdname, sizeof(ppdname), "%s/ppd/%s.ppd", cg->cups_serverroot,
name);
if (!stat(ppdname, &ppdinfo) && !access(ppdname, R_OK))
{
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;
const char *tmpdir;
struct timeval curtime;
if ((tmpdir = getenv("TMPDIR")) == NULL)
# ifdef __APPLE__
tmpdir = "/private/tmp";
# else
tmpdir = "/tmp";
# endif
tries = 0;
do
{
gettimeofday(&curtime, NULL);
snprintf(buffer, bufsize, "%s/%08lx%05lx", tmpdir,
(unsigned long)curtime.tv_sec,
(unsigned long)curtime.tv_usec);
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
if (!http)
if ((http = _cupsConnect()) == NULL)
return (HTTP_STATUS_SERVICE_UNAVAILABLE);
if (!cups_get_printer_uri(http, name, hostname, sizeof(hostname), &port,
resource, sizeof(resource), 0))
return (HTTP_STATUS_NOT_FOUND);
DEBUG_printf(("2cupsGetPPD3: Printer hostname=\"%s\", port=%d", hostname,
port));
if (cupsServer()[0] == '/' && !_cups_strcasecmp(hostname, "localhost") && port == ippPort())
{
strlcpy(hostname, cupsServer(), sizeof(hostname));
port = 0;
DEBUG_printf(("2cupsGetPPD3: Redirecting to \"%s\".", hostname));
}
httpGetHostname(NULL, localhost, sizeof(localhost));
DEBUG_printf(("2cupsGetPPD3: Local hostname=\"%s\"", localhost));
if (!_cups_strcasecmp(localhost, hostname))
strlcpy(hostname, "localhost", sizeof(hostname));
httpGetHostname(http, http_hostname, sizeof(http_hostname));
http_port = httpAddrPort(http->hostaddr);
DEBUG_printf(("2cupsGetPPD3: Connection hostname=\"%s\", port=%d",
http_hostname, http_port));
if (!_cups_strcasecmp(http_hostname, hostname) && port == http_port)
http2 = http;
else if ((http2 = httpConnect2(hostname, port, NULL, AF_UNSPEC,
cupsEncryption(), 1, 30000, NULL)) == NULL)
{
DEBUG_puts("1cupsGetPPD3: Unable to connect to server");
return (HTTP_STATUS_SERVICE_UNAVAILABLE);
}
if (buffer[0])
fd = open(buffer, O_CREAT | O_TRUNC | O_WRONLY, 0600);
else
fd = cupsTempFd(tempfile, sizeof(tempfile));
if (fd < 0)
{
_cupsSetError(IPP_STATUS_ERROR_INTERNAL, NULL, 0);
if (http2 != http)
httpClose(http2);
return (HTTP_STATUS_SERVER_ERROR);
}
strlcat(resource, ".ppd", sizeof(resource));
if (*modtime > 0)
httpSetField(http2, HTTP_FIELD_IF_MODIFIED_SINCE,
httpGetDateString(*modtime));
status = cupsGetFd(http2, resource, fd);
close(fd);
if (status == HTTP_STATUS_OK)
{
*modtime = httpGetDateTime(httpGetField(http2, HTTP_FIELD_DATE));
if (tempfile[0])
strlcpy(buffer, tempfile, bufsize);
}
else if (status != HTTP_STATUS_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);
DEBUG_printf(("1cupsGetPPD3: Returning status %d", status));
return (status);
}
char *
cupsGetServerPPD(http_t *http,
const char *name)
{
int fd;
ipp_t *request;
_ppd_globals_t *pg = _ppdGlobals();
if (!name)
{
_cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("No PPD name"), 1);
return (NULL);
}
if (!http)
if ((http = _cupsConnect()) == NULL)
return (NULL);
if ((fd = cupsTempFd(pg->ppd_filename, sizeof(pg->ppd_filename))) < 0)
{
_cupsSetError(IPP_STATUS_ERROR_INTERNAL, NULL, 0);
return (NULL);
}
request = ippNewRequest(IPP_OP_CUPS_GET_PPD);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "ppd-name", NULL,
name);
ippDelete(cupsDoIORequest(http, request, "/", -1, fd));
close(fd);
if (cupsLastError() != IPP_STATUS_OK)
{
unlink(pg->ppd_filename);
return (NULL);
}
else
return (pg->ppd_filename);
}
static int
cups_get_printer_uri(
http_t *http,
const char *name,
char *host,
int hostsize,
int *port,
char *resource,
int resourcesize,
int depth)
{
int i;
int http_port;
http_t *http2;
ipp_t *request,
*response;
ipp_attribute_t *attr;
char uri[HTTP_MAX_URI],
scheme[HTTP_MAX_URI],
username[HTTP_MAX_URI],
classname[255],
http_hostname[HTTP_MAX_HOST];
static const char * const requested_attrs[] =
{
"device-uri",
"member-uris",
"printer-uri-supported",
"printer-type"
};
DEBUG_printf(("4cups_get_printer_uri(http=%p, name=\"%s\", host=%p, hostsize=%d, resource=%p, resourcesize=%d, depth=%d)", http, name, host, hostsize, resource, resourcesize, depth));
if (httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, "localhost", 0, "/printers/%s", name) < HTTP_URI_STATUS_OK)
{
_cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unable to create printer-uri"), 1);
*host = '\0';
*resource = '\0';
return (0);
}
DEBUG_printf(("5cups_get_printer_uri: printer-uri=\"%s\"", uri));
httpGetHostname(http, http_hostname, sizeof(http_hostname));
http_port = httpAddrPort(http->hostaddr);
DEBUG_printf(("5cups_get_printer_uri: http_hostname=\"%s\"", http_hostname));
request = ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "requested-attributes", sizeof(requested_attrs) / sizeof(requested_attrs[0]), NULL, requested_attrs);
snprintf(resource, (size_t)resourcesize, "/printers/%s", name);
if ((response = cupsDoRequest(http, request, resource)) != NULL)
{
const char *device_uri = NULL;
if ((attr = ippFindAttribute(response, "device-uri", IPP_TAG_URI)) != NULL)
{
device_uri = attr->values[0].string.text;
DEBUG_printf(("5cups_get_printer_uri: device-uri=\"%s\"", device_uri));
}
if (device_uri &&
(((!strncmp(device_uri, "ipp://", 6) || !strncmp(device_uri, "ipps://", 7)) &&
(strstr(device_uri, "/printers/") != NULL || strstr(device_uri, "/classes/") != NULL)) ||
((strstr(device_uri, "._ipp.") != NULL || strstr(device_uri, "._ipps.") != NULL) &&
!strcmp(device_uri + strlen(device_uri) - 5, "/cups"))))
{
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);
DEBUG_printf(("5cups_get_printer_uri: Resolved to host=\"%s\", port=%d, resource=\"%s\"", host, *port, resource));
return (1);
}
else if ((attr = ippFindAttribute(response, "member-uris", IPP_TAG_URI)) != NULL)
{
DEBUG_printf(("5cups_get_printer_uri: Got member-uris with %d values.", ippGetCount(attr)));
for (i = 0; i < attr->num_values; i ++)
{
DEBUG_printf(("5cups_get_printer_uri: member-uris[%d]=\"%s\"", i, ippGetString(attr, i, NULL)));
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))
{
ippDelete(response);
DEBUG_printf(("5cups_get_printer_uri: Found printer member with host=\"%s\", port=%d, resource=\"%s\"", host, *port, resource));
return (1);
}
}
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))
{
if (!_cups_strcasecmp(http_hostname, host) && *port == http_port)
http2 = http;
else if ((http2 = httpConnect2(host, *port, NULL, AF_UNSPEC, cupsEncryption(), 1, 30000, NULL)) == NULL)
{
DEBUG_puts("8cups_get_printer_uri: Unable to connect to server");
continue;
}
strlcpy(classname, resource + 9, sizeof(classname));
cups_get_printer_uri(http2, classname, host, hostsize, port,
resource, resourcesize, depth + 1);
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, _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);
DEBUG_printf(("5cups_get_printer_uri: Resolved to host=\"%s\", port=%d, resource=\"%s\"", host, *port, resource));
if (!strncmp(resource, "/classes/", 9))
{
_cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("No printer-uri found for class"), 1);
*host = '\0';
*resource = '\0';
DEBUG_puts("5cups_get_printer_uri: Not returning class.");
return (0);
}
return (1);
}
ippDelete(response);
}
if (cupsLastError() != IPP_STATUS_ERROR_NOT_FOUND)
_cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("No printer-uri found"), 1);
*host = '\0';
*resource = '\0';
DEBUG_puts("5cups_get_printer_uri: Printer URI not found.");
return (0);
}