#include "cups-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);
int
cupsCancelJob(const char *name,
int job_id)
{
return (cupsCancelJob2(CUPS_HTTP_DEFAULT, name, job_id, 0)
< IPP_REDIRECTION_OTHER_SITE);
}
ipp_status_t
cupsCancelJob2(http_t *http,
const char *name,
int job_id,
int purge)
{
char uri[HTTP_MAX_URI];
ipp_t *request;
if (job_id < -1 || (!name && job_id == 0))
{
_cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0);
return (0);
}
if (!http)
if ((http = _cupsConnect()) == NULL)
return (IPP_SERVICE_UNAVAILABLE);
request = ippNewRequest(job_id < 0 ? IPP_PURGE_JOBS : IPP_CANCEL_JOB);
if (name)
{
httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
"localhost", ippPort(), "/printers/%s", name);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL,
uri);
ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id",
job_id);
}
else if (job_id > 0)
{
snprintf(uri, sizeof(uri), "ipp://localhost/jobs/%d", job_id);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, uri);
}
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
NULL, cupsUser());
if (purge && job_id >= 0)
ippAddBoolean(request, IPP_TAG_OPERATION, "purge-job", 1);
else if (!purge && job_id < 0)
ippAddBoolean(request, IPP_TAG_OPERATION, "purge-jobs", 0);
ippDelete(cupsDoRequest(http, request, "/jobs/"));
return (cupsLastError());
}
int
cupsCreateJob(
http_t *http,
const char *name,
const char *title,
int num_options,
cups_option_t *options)
{
char printer_uri[1024],
resource[1024];
ipp_t *request,
*response;
ipp_attribute_t *attr;
int job_id = 0;
DEBUG_printf(("cupsCreateJob(http=%p, name=\"%s\", title=\"%s\", "
"num_options=%d, options=%p)",
http, name, title, num_options, options));
if (!name)
{
_cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0);
return (0);
}
if ((request = ippNewRequest(IPP_CREATE_JOB)) == NULL)
{
_cupsSetError(IPP_INTERNAL_ERROR, strerror(ENOMEM), 0);
return (0);
}
httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri), "ipp",
NULL, "localhost", ippPort(), "/printers/%s", name);
snprintf(resource, sizeof(resource), "/printers/%s", name);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
NULL, printer_uri);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
NULL, cupsUser());
if (title)
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL,
title);
cupsEncodeOptions2(request, num_options, options, IPP_TAG_JOB);
cupsEncodeOptions2(request, num_options, options, IPP_TAG_SUBSCRIPTION);
response = cupsDoRequest(http, request, resource);
if ((attr = ippFindAttribute(response, "job-id", IPP_TAG_INTEGER)) != NULL)
job_id = attr->values[0].integer;
ippDelete(response);
return (job_id);
}
ipp_status_t
cupsFinishDocument(http_t *http,
const char *name)
{
char resource[1024];
snprintf(resource, sizeof(resource), "/printers/%s", name);
ippDelete(cupsGetResponse(http, resource));
return (cupsLastError());
}
void
cupsFreeJobs(int num_jobs,
cups_job_t *jobs)
{
int i;
cups_job_t *job;
if (num_jobs <= 0 || !jobs)
return;
for (i = num_jobs, job = jobs; i > 0; i --, job ++)
{
_cupsStrFree(job->dest);
_cupsStrFree(job->user);
_cupsStrFree(job->format);
_cupsStrFree(job->title);
}
free(jobs);
}
int
cupsGetClasses(char ***classes)
{
int n;
ipp_t *request,
*response;
ipp_attribute_t *attr;
char **temp;
http_t *http;
if (!classes)
{
_cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0);
return (0);
}
*classes = NULL;
if ((http = _cupsConnect()) == NULL)
return (0);
request = ippNewRequest(CUPS_GET_CLASSES);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
"requested-attributes", NULL, "printer-name");
n = 0;
if ((response = cupsDoRequest(http, request, "/")) != NULL)
{
for (attr = response->attrs; attr != NULL; attr = attr->next)
if (attr->name != NULL &&
_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));
if (temp == NULL)
{
while (n > 0)
{
n --;
free((*classes)[n]);
}
free(*classes);
ippDelete(response);
return (0);
}
*classes = temp;
temp[n] = strdup(attr->values[0].string.text);
n ++;
}
ippDelete(response);
}
return (n);
}
const char *
cupsGetDefault(void)
{
return (cupsGetDefault2(CUPS_HTTP_DEFAULT));
}
const char *
cupsGetDefault2(http_t *http)
{
ipp_t *request,
*response;
ipp_attribute_t *attr;
_cups_globals_t *cg = _cupsGlobals();
if (_cupsUserDefault(cg->def_printer, sizeof(cg->def_printer)))
return (cg->def_printer);
if (!http)
if ((http = _cupsConnect()) == NULL)
return (NULL);
request = ippNewRequest(CUPS_GET_DEFAULT);
if ((response = cupsDoRequest(http, request, "/")) != NULL)
{
if ((attr = ippFindAttribute(response, "printer-name",
IPP_TAG_NAME)) != NULL)
{
strlcpy(cg->def_printer, attr->values[0].string.text,
sizeof(cg->def_printer));
ippDelete(response);
return (cg->def_printer);
}
ippDelete(response);
}
return (NULL);
}
int
cupsGetJobs(cups_job_t **jobs,
const char *name,
int myjobs,
int whichjobs)
{
return (cupsGetJobs2(CUPS_HTTP_DEFAULT, jobs, name, myjobs, whichjobs));
}
int
cupsGetJobs2(http_t *http,
cups_job_t **jobs,
const char *name,
int myjobs,
int whichjobs)
{
int n;
ipp_t *request,
*response;
ipp_attribute_t *attr;
cups_job_t *temp;
int id,
priority,
size;
ipp_jstate_t state;
time_t completed_time,
creation_time,
processing_time;
const char *dest,
*format,
*title,
*user;
char uri[HTTP_MAX_URI];
_cups_globals_t *cg = _cupsGlobals();
static const char * const attrs[] =
{
"document-format",
"job-id",
"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"
};
if (!jobs)
{
_cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0);
return (-1);
}
if (name)
{
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"), 1);
return (-1);
}
}
else
strcpy(uri, "ipp://localhost/");
if (!http)
if ((http = _cupsConnect()) == NULL)
return (-1);
request = ippNewRequest(IPP_GET_JOBS);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
"printer-uri", NULL, uri);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
"requesting-user-name", NULL, cupsUser());
if (myjobs)
ippAddBoolean(request, IPP_TAG_OPERATION, "my-jobs", 1);
if (whichjobs == CUPS_WHICHJOBS_COMPLETED)
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
"which-jobs", NULL, "completed");
else if (whichjobs == CUPS_WHICHJOBS_ALL)
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
"which-jobs", NULL, "all");
ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
"requested-attributes", sizeof(attrs) / sizeof(attrs[0]),
NULL, attrs);
n = 0;
*jobs = NULL;
if ((response = cupsDoRequest(http, request, "/")) != NULL)
{
for (attr = response->attrs; attr; attr = attr->next)
{
while (attr && attr->group_tag != IPP_TAG_JOB)
attr = attr->next;
if (!attr)
break;
id = 0;
size = 0;
priority = 50;
state = IPP_JOB_PENDING;
user = "unknown";
dest = NULL;
format = "application/octet-stream";
title = "untitled";
creation_time = 0;
completed_time = 0;
processing_time = 0;
while (attr && attr->group_tag == IPP_TAG_JOB)
{
if (!strcmp(attr->name, "job-id") &&
attr->value_tag == IPP_TAG_INTEGER)
id = attr->values[0].integer;
else if (!strcmp(attr->name, "job-state") &&
attr->value_tag == IPP_TAG_ENUM)
state = (ipp_jstate_t)attr->values[0].integer;
else if (!strcmp(attr->name, "job-priority") &&
attr->value_tag == IPP_TAG_INTEGER)
priority = attr->values[0].integer;
else if (!strcmp(attr->name, "job-k-octets") &&
attr->value_tag == IPP_TAG_INTEGER)
size = attr->values[0].integer;
else if (!strcmp(attr->name, "time-at-completed") &&
attr->value_tag == IPP_TAG_INTEGER)
completed_time = attr->values[0].integer;
else if (!strcmp(attr->name, "time-at-creation") &&
attr->value_tag == IPP_TAG_INTEGER)
creation_time = attr->values[0].integer;
else if (!strcmp(attr->name, "time-at-processing") &&
attr->value_tag == IPP_TAG_INTEGER)
processing_time = attr->values[0].integer;
else if (!strcmp(attr->name, "job-printer-uri") &&
attr->value_tag == IPP_TAG_URI)
{
if ((dest = strrchr(attr->values[0].string.text, '/')) != NULL)
dest ++;
}
else if (!strcmp(attr->name, "job-originating-user-name") &&
attr->value_tag == IPP_TAG_NAME)
user = attr->values[0].string.text;
else if (!strcmp(attr->name, "document-format") &&
attr->value_tag == IPP_TAG_MIMETYPE)
format = attr->values[0].string.text;
else if (!strcmp(attr->name, "job-name") &&
(attr->value_tag == IPP_TAG_TEXT ||
attr->value_tag == IPP_TAG_NAME))
title = attr->values[0].string.text;
attr = attr->next;
}
if (!dest || !id)
{
if (!attr)
break;
else
continue;
}
if (n == 0)
temp = malloc(sizeof(cups_job_t));
else
temp = realloc(*jobs, sizeof(cups_job_t) * (n + 1));
if (!temp)
{
_cupsSetError(IPP_INTERNAL_ERROR, NULL, 0);
cupsFreeJobs(n, *jobs);
*jobs = NULL;
ippDelete(response);
return (-1);
}
*jobs = temp;
temp += n;
n ++;
temp->dest = _cupsStrAlloc(dest);
temp->user = _cupsStrAlloc(user);
temp->format = _cupsStrAlloc(format);
temp->title = _cupsStrAlloc(title);
temp->id = id;
temp->priority = priority;
temp->state = state;
temp->size = size;
temp->completed_time = completed_time;
temp->creation_time = creation_time;
temp->processing_time = processing_time;
if (!attr)
break;
}
ippDelete(response);
}
if (n == 0 && cg->last_error >= IPP_BAD_REQUEST)
return (-1);
else
return (n);
}
const char *
cupsGetPPD(const char *name)
{
_cups_globals_t *cg = _cupsGlobals();
time_t modtime = 0;
cg->ppd_filename[0] = '\0';
if (cupsGetPPD3(CUPS_HTTP_DEFAULT, name, &modtime, cg->ppd_filename,
sizeof(cg->ppd_filename)) == HTTP_OK)
return (cg->ppd_filename);
else
return (NULL);
}
const char *
cupsGetPPD2(http_t *http,
const char *name)
{
_cups_globals_t *cg = _cupsGlobals();
time_t modtime = 0;
cg->ppd_filename[0] = '\0';
if (cupsGetPPD3(http, name, &modtime, cg->ppd_filename,
sizeof(cg->ppd_filename)) == HTTP_OK)
return (cg->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_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
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))
{
if (buffer[0])
{
unlink(buffer);
if (symlink(ppdname, buffer) && errno != EEXIST)
{
_cupsSetError(IPP_INTERNAL_ERROR, NULL, 0);
return (HTTP_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_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
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));
httpGetHostname(NULL, localhost, sizeof(localhost));
DEBUG_printf(("2cupsGetPPD3: Local hostname=\"%s\"", localhost));
if (!_cups_strcasecmp(localhost, hostname))
strcpy(hostname, "localhost");
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 = httpConnectEncrypt(hostname, port,
cupsEncryption())) == NULL)
{
DEBUG_puts("1cupsGetPPD3: Unable to connect to server");
return (HTTP_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_INTERNAL_ERROR, NULL, 0);
if (http2 != http)
httpClose(http2);
return (HTTP_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_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);
DEBUG_printf(("1cupsGetPPD3: Returning status %d", status));
return (status);
}
int
cupsGetPrinters(char ***printers)
{
int n;
ipp_t *request,
*response;
ipp_attribute_t *attr;
char **temp;
http_t *http;
if (!printers)
{
_cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0);
return (0);
}
*printers = NULL;
if ((http = _cupsConnect()) == NULL)
return (0);
request = ippNewRequest(CUPS_GET_PRINTERS);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
"requested-attributes", NULL, "printer-name");
ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM,
"printer-type", 0);
ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_ENUM,
"printer-type-mask", CUPS_PRINTER_CLASS);
n = 0;
if ((response = cupsDoRequest(http, request, "/")) != NULL)
{
for (attr = response->attrs; attr != NULL; attr = attr->next)
if (attr->name != NULL &&
_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));
if (temp == NULL)
{
while (n > 0)
{
n --;
free((*printers)[n]);
}
free(*printers);
ippDelete(response);
return (0);
}
*printers = temp;
temp[n] = strdup(attr->values[0].string.text);
n ++;
}
ippDelete(response);
}
return (n);
}
char *
cupsGetServerPPD(http_t *http,
const char *name)
{
int fd;
ipp_t *request;
_cups_globals_t *cg = _cupsGlobals();
if (!name)
{
_cupsSetError(IPP_INTERNAL_ERROR, _("No PPD name"), 1);
return (NULL);
}
if (!http)
if ((http = _cupsConnect()) == NULL)
return (NULL);
if ((fd = cupsTempFd(cg->ppd_filename, sizeof(cg->ppd_filename))) < 0)
{
_cupsSetError(IPP_INTERNAL_ERROR, NULL, 0);
return (NULL);
}
request = ippNewRequest(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_OK)
{
unlink(cg->ppd_filename);
return (NULL);
}
else
return (cg->ppd_filename);
}
int
cupsPrintFile(const char *name,
const char *filename,
const char *title,
int num_options,
cups_option_t *options)
{
DEBUG_printf(("cupsPrintFile(name=\"%s\", filename=\"%s\", "
"title=\"%s\", num_options=%d, options=%p)",
name, filename, title, num_options, options));
return (cupsPrintFiles2(CUPS_HTTP_DEFAULT, name, 1, &filename, title,
num_options, options));
}
int
cupsPrintFile2(
http_t *http,
const char *name,
const char *filename,
const char *title,
int num_options,
cups_option_t *options)
{
DEBUG_printf(("cupsPrintFile2(http=%p, name=\"%s\", filename=\"%s\", "
"title=\"%s\", num_options=%d, options=%p)",
http, name, filename, title, num_options, options));
return (cupsPrintFiles2(http, name, 1, &filename, title, num_options,
options));
}
int
cupsPrintFiles(
const char *name,
int num_files,
const char **files,
const char *title,
int num_options,
cups_option_t *options)
{
DEBUG_printf(("cupsPrintFiles(name=\"%s\", num_files=%d, "
"files=%p, title=\"%s\", num_options=%d, options=%p)",
name, num_files, files, title, num_options, options));
return (cupsPrintFiles2(CUPS_HTTP_DEFAULT, name, num_files, files, title,
num_options, options));
}
int
cupsPrintFiles2(
http_t *http,
const char *name,
int num_files,
const char **files,
const char *title,
int num_options,
cups_option_t *options)
{
int i;
int job_id;
const char *docname;
const char *format;
cups_file_t *fp;
char buffer[8192];
ssize_t bytes;
http_status_t status;
_cups_globals_t *cg = _cupsGlobals();
ipp_status_t cancel_status;
char *cancel_message;
DEBUG_printf(("cupsPrintFiles2(http=%p, name=\"%s\", num_files=%d, "
"files=%p, title=\"%s\", num_options=%d, options=%p)",
http, name, num_files, files, title, num_options, options));
if (!name || num_files < 1 || !files)
{
_cupsSetError(IPP_INTERNAL_ERROR, strerror(EINVAL), 0);
return (0);
}
if ((job_id = cupsCreateJob(http, name, title, num_options, options)) == 0)
return (0);
if (cupsGetOption("raw", num_options, options))
format = CUPS_FORMAT_RAW;
else if ((format = cupsGetOption("document-format", num_options,
options)) == NULL)
format = CUPS_FORMAT_AUTO;
for (i = 0; i < num_files; i ++)
{
if ((docname = strrchr(files[i], '/')) != NULL)
docname ++;
else
docname = files[i];
if ((fp = cupsFileOpen(files[i], "rb")) == NULL)
{
_cupsSetError(IPP_DOCUMENT_ACCESS_ERROR, NULL, 0);
goto cancel_job;
}
status = cupsStartDocument(http, name, job_id, docname, format,
i == (num_files - 1));
while (status == HTTP_CONTINUE &&
(bytes = cupsFileRead(fp, buffer, sizeof(buffer))) > 0)
status = cupsWriteRequestData(http, buffer, bytes);
cupsFileClose(fp);
if (status != HTTP_CONTINUE || cupsFinishDocument(http, name) != IPP_OK)
{
goto cancel_job;
}
}
return (job_id);
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);
}
http_status_t
cupsStartDocument(
http_t *http,
const char *name,
int job_id,
const char *docname,
const char *format,
int last_document)
{
char resource[1024],
printer_uri[1024];
ipp_t *request;
http_status_t status;
if ((request = ippNewRequest(IPP_SEND_DOCUMENT)) == NULL)
{
_cupsSetError(IPP_INTERNAL_ERROR, strerror(ENOMEM), 0);
return (HTTP_ERROR);
}
httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri), "ipp",
NULL, "localhost", ippPort(), "/printers/%s", name);
snprintf(resource, sizeof(resource), "/printers/%s", name);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
NULL, printer_uri);
ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", job_id);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
NULL, cupsUser());
if (docname)
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "document-name",
NULL, docname);
if (format)
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE,
"document-format", NULL, format);
ippAddBoolean(request, IPP_TAG_OPERATION, "last-document", last_document);
status = cupsSendRequest(http, request, resource, CUPS_LENGTH_VARIABLE);
ippDelete(request);
return (status);
}
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(("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));
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"), 1);
*host = '\0';
*resource = '\0';
return (0);
}
DEBUG_printf(("9cups_get_printer_uri: printer-uri=\"%s\"", uri));
httpGetHostname(http, http_hostname, sizeof(http_hostname));
http_port = _httpAddrPort(http->hostaddr);
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);
snprintf(resource, 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;
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"))))
{
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)
{
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))
{
ippDelete(response);
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 = httpConnectEncrypt(host, *port,
cupsEncryption())) == 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);
if (!strncmp(resource, "/classes/", 9))
{
_cupsSetError(IPP_INTERNAL_ERROR,
_("No printer-uri found for class"), 1);
*host = '\0';
*resource = '\0';
return (0);
}
return (1);
}
ippDelete(response);
}
if (cupsLastError() != IPP_NOT_FOUND)
_cupsSetError(IPP_INTERNAL_ERROR, _("No printer-uri found"), 1);
*host = '\0';
*resource = '\0';
return (0);
}