#include "cups.h"
#include "language.h"
#include "string.h"
#include "globals.h"
#include "debug.h"
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#ifdef HAVE_INTTYPES_H
# include <inttypes.h>
#endif
#if defined(WIN32) || defined(__EMX__)
# include <io.h>
#else
# include <unistd.h>
#endif
static char *cups_connect(const char *name, char *printer, char *hostname);
static int cups_getleadprinter(http_t *http, const char *hostname,
const char *className, char *printer, size_t bufferSize);
int
cupsCancelJob(const char *name,
int job)
{
char printer[HTTP_MAX_URI],
hostname[HTTP_MAX_URI],
uri[HTTP_MAX_URI];
ipp_t *request,
*response;
cups_lang_t *language;
cups_globals_t *cg = _cups_globals();
if (!cups_connect(name, printer, hostname))
{
DEBUG_puts("Unable to connect to server!");
cg->last_error = IPP_SERVICE_UNAVAILABLE;
return (0);
}
request = ippNew();
request->request.op.operation_id = IPP_CANCEL_JOB;
request->request.op.request_id = 1;
language = cupsLangDefault();
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
"attributes-charset", NULL, cupsLangEncoding(language));
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
"attributes-natural-language", NULL,
language != NULL ? language->language : "C");
cupsLangFree(language);
snprintf(uri, sizeof(uri), "ipp://%s:%d/printers/%s", hostname, ippPort(), printer);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri",
NULL, uri);
ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", job);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
NULL, cupsUser());
if ((response = cupsDoRequest(cg->http, request, "/jobs/")) != NULL)
ippDelete(response);
return (cg->last_error < IPP_REDIRECTION_OTHER_SITE);
}
ipp_t *
cupsDoFileRequest(http_t *http,
ipp_t *request,
const char *resource,
const char *filename)
{
ipp_t *response;
char length[255];
http_status_t status;
FILE *file;
struct stat fileinfo;
int bytes;
char buffer[32768];
cups_globals_t *cg = _cups_globals();
DEBUG_printf(("cupsDoFileRequest(%p, %p, \'%s\', \'%s\')\n",
http, request, resource ? resource : "(null)",
filename ? filename : "(null)"));
if (http == NULL || request == NULL || resource == NULL)
{
if (request != NULL)
ippDelete(request);
cg->last_error = IPP_INTERNAL_ERROR;
return (NULL);
}
if (filename != NULL)
{
if (stat(filename, &fileinfo))
{
ippDelete(request);
cg->last_error = IPP_NOT_FOUND;
return (NULL);
}
#ifdef WIN32
if (fileinfo.st_mode & _S_IFDIR)
#else
if (S_ISDIR(fileinfo.st_mode))
#endif
{
ippDelete(request);
cg->last_error = IPP_NOT_POSSIBLE;
return (NULL);
}
if ((file = fopen(filename, "rb")) == NULL)
{
ippDelete(request);
cg->last_error = IPP_NOT_FOUND;
return (NULL);
}
}
else
file = NULL;
response = NULL;
status = HTTP_ERROR;
while (response == NULL)
{
DEBUG_puts("cupsDoFileRequest: setup...");
if (filename != NULL)
sprintf(length, "%" PRIdMAX "", (intmax_t)(ippLength(request) +
fileinfo.st_size));
else
sprintf(length, "%" PRIdMAX "", (intmax_t)ippLength(request));
httpClearFields(http);
httpSetField(http, HTTP_FIELD_CONTENT_LENGTH, length);
httpSetField(http, HTTP_FIELD_CONTENT_TYPE, "application/ipp");
httpSetField(http, HTTP_FIELD_AUTHORIZATION, http->authstring);
DEBUG_printf(("cupsDoFileRequest: authstring=\"%s\"\n", http->authstring));
DEBUG_puts("cupsDoFileRequest: post...");
if (httpPost(http, resource))
{
if (httpReconnect(http))
{
status = HTTP_ERROR;
break;
}
else
continue;
}
DEBUG_puts("cupsDoFileRequest: ipp write...");
request->state = IPP_IDLE;
status = HTTP_CONTINUE;
if (ippWrite(http, request) != IPP_ERROR)
if (filename != NULL)
{
DEBUG_puts("cupsDoFileRequest: file write...");
rewind(file);
while ((bytes = fread(buffer, 1, sizeof(buffer), file)) > 0)
{
if (httpCheck(http))
{
if ((status = httpUpdate(http)) != HTTP_CONTINUE)
break;
}
if (httpWrite(http, buffer, bytes) < bytes)
break;
}
}
DEBUG_puts("cupsDoFileRequest: update...");
while (status == HTTP_CONTINUE)
status = httpUpdate(http);
DEBUG_printf(("cupsDoFileRequest: status = %d\n", status));
if (status == HTTP_UNAUTHORIZED)
{
DEBUG_puts("cupsDoFileRequest: unauthorized...");
httpFlush(http);
if (cupsDoAuthentication(http, "POST", resource))
break;
if (httpReconnect(http))
{
status = HTTP_ERROR;
break;
}
continue;
}
else if (status == HTTP_ERROR)
{
#ifdef WIN32
if (http->error != WSAENETDOWN && http->error != WSAENETUNREACH)
#else
if (http->error != ENETDOWN && http->error != ENETUNREACH)
#endif
continue;
else
break;
}
#ifdef HAVE_SSL
else if (status == HTTP_UPGRADE_REQUIRED)
{
httpFlush(http);
if (httpReconnect(http))
{
status = HTTP_ERROR;
break;
}
httpEncryption(http, HTTP_ENCRYPT_REQUIRED);
continue;
}
#endif
else if (status != HTTP_OK)
{
DEBUG_printf(("cupsDoFileRequest: error %d...\n", status));
httpFlush(http);
break;
}
else
{
DEBUG_puts("cupsDoFileRequest: response...");
response = ippNew();
if (ippRead(http, response) == IPP_ERROR)
{
DEBUG_puts("IPP read error!");
ippDelete(response);
response = NULL;
cg->last_error = IPP_SERVICE_UNAVAILABLE;
break;
}
}
}
if (filename != NULL)
fclose(file);
httpFlush(http);
ippDelete(request);
if (response)
cg->last_error = response->request.status.status_code;
else if (status != HTTP_OK)
{
switch (status)
{
case HTTP_NOT_FOUND :
cg->last_error = IPP_NOT_FOUND;
break;
case HTTP_UNAUTHORIZED :
cg->last_error = IPP_NOT_AUTHORIZED;
break;
case HTTP_FORBIDDEN :
cg->last_error = IPP_FORBIDDEN;
break;
case HTTP_BAD_REQUEST :
cg->last_error = IPP_BAD_REQUEST;
break;
case HTTP_REQUEST_TOO_LARGE :
cg->last_error = IPP_REQUEST_VALUE;
break;
case HTTP_NOT_IMPLEMENTED :
cg->last_error = IPP_OPERATION_NOT_SUPPORTED;
break;
case HTTP_NOT_SUPPORTED :
cg->last_error = IPP_VERSION_NOT_SUPPORTED;
break;
default :
DEBUG_printf(("HTTP error %d mapped to IPP_SERVICE_UNAVAILABLE!\n",
status));
cg->last_error = IPP_SERVICE_UNAVAILABLE;
break;
}
}
return (response);
}
void
cupsFreeJobs(int num_jobs,
cups_job_t *jobs)
{
int i;
if (num_jobs <= 0 || jobs == NULL)
return;
for (i = 0; i < num_jobs; i ++)
{
free(jobs[i].dest);
free(jobs[i].user);
free(jobs[i].format);
free(jobs[i].title);
}
free(jobs);
}
int
cupsGetClasses(char ***classes)
{
int n;
ipp_t *request,
*response;
ipp_attribute_t *attr;
cups_lang_t *language;
char **temp;
cups_globals_t *cg = _cups_globals();
if (classes == NULL)
{
cg->last_error = IPP_INTERNAL_ERROR;
return (0);
}
if (!cups_connect("default", NULL, NULL))
{
DEBUG_puts("Unable to connect to server!");
cg->last_error = IPP_SERVICE_UNAVAILABLE;
return (0);
}
request = ippNew();
request->request.op.operation_id = CUPS_GET_CLASSES;
request->request.op.request_id = 1;
language = cupsLangDefault();
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
"attributes-charset", NULL, cupsLangEncoding(language));
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
"attributes-natural-language", NULL, language->language);
cupsLangFree(language);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
"requested-attributes", NULL, "printer-name");
n = 0;
*classes = NULL;
if ((response = cupsDoRequest(cg->http, request, "/")) != NULL)
{
cg->last_error = response->request.status.status_code;
for (attr = response->attrs; attr != NULL; attr = attr->next)
if (attr->name != NULL &&
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);
}
else
cg->last_error = IPP_BAD_REQUEST;
return (n);
}
const char *
cupsGetDefault(void)
{
const char *var;
cups_globals_t *cg = _cups_globals();
if ((var = getenv("LPDEST")) != NULL)
return (var);
else if ((var = getenv("PRINTER")) != NULL && strcmp(var, "lp") != 0)
return (var);
if (!cups_connect("default", NULL, NULL))
{
DEBUG_puts("Unable to connect to server!");
cg->last_error = IPP_SERVICE_UNAVAILABLE;
return (NULL);
}
return (cupsGetDefault2(cg->http));
}
const char *
cupsGetDefault2(http_t *http)
{
ipp_t *request,
*response;
ipp_attribute_t *attr;
cups_lang_t *language;
const char *var;
cups_globals_t *cg = _cups_globals();
if ((var = getenv("LPDEST")) != NULL)
return (var);
else if ((var = getenv("PRINTER")) != NULL && strcmp(var, "lp") != 0)
return (var);
if (!http)
return (NULL);
request = ippNew();
request->request.op.operation_id = CUPS_GET_DEFAULT;
request->request.op.request_id = 1;
language = cupsLangDefault();
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
"attributes-charset", NULL, cupsLangEncoding(language));
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
"attributes-natural-language", NULL, language->language);
cupsLangFree(language);
if ((response = cupsDoRequest(http, request, "/")) != NULL)
{
cg->last_error = response->request.status.status_code;
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);
}
else
cg->last_error = IPP_BAD_REQUEST;
return (NULL);
}
int
cupsGetJobs(cups_job_t **jobs,
const char *mydest,
int myjobs,
int completed)
{
cups_globals_t *cg = _cups_globals();
if (!cups_connect("default", NULL, NULL))
{
DEBUG_puts("Unable to connect to server!");
cg->last_error = IPP_SERVICE_UNAVAILABLE;
return (-1);
}
return (cupsGetJobs2(cg->http, jobs, mydest, myjobs, completed));
}
int
cupsGetJobs2(http_t *http,
cups_job_t **jobs,
const char *mydest,
int myjobs,
int completed)
{
int n;
ipp_t *request,
*response;
ipp_attribute_t *attr;
cups_lang_t *language;
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 = _cups_globals();
static const char * const attrs[] =
{
"job-id",
"job-priority",
"job-k-octets",
"job-state",
"time-at-completed",
"time-at-creation",
"time-at-processing",
"job-printer-uri",
"document-format",
"job-name",
"job-originating-user-name"
};
if (!http || !jobs)
{
cg->last_error = IPP_INTERNAL_ERROR;
return (-1);
}
request = ippNew();
request->request.op.operation_id = IPP_GET_JOBS;
request->request.op.request_id = 1;
language = cupsLangDefault();
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
"attributes-charset", NULL, cupsLangEncoding(language));
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
"attributes-natural-language", NULL, language->language);
cupsLangFree(language);
if (mydest)
snprintf(uri, sizeof(uri), "ipp://localhost/printers/%s", mydest);
else
strcpy(uri, "ipp://localhost/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 (completed)
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
"which-jobs", NULL, "completed");
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)
{
cg->last_error = response->request.status.status_code;
for (attr = response->attrs; attr != NULL; attr = attr->next)
{
while (attr != NULL && attr->group_tag != IPP_TAG_JOB)
attr = attr->next;
if (attr == NULL)
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 != NULL && attr->group_tag == IPP_TAG_JOB)
{
if (strcmp(attr->name, "job-id") == 0 &&
attr->value_tag == IPP_TAG_INTEGER)
id = attr->values[0].integer;
else if (strcmp(attr->name, "job-state") == 0 &&
attr->value_tag == IPP_TAG_ENUM)
state = (ipp_jstate_t)attr->values[0].integer;
else if (strcmp(attr->name, "job-priority") == 0 &&
attr->value_tag == IPP_TAG_INTEGER)
priority = attr->values[0].integer;
else if (strcmp(attr->name, "job-k-octets") == 0 &&
attr->value_tag == IPP_TAG_INTEGER)
size = attr->values[0].integer;
else if (strcmp(attr->name, "time-at-completed") == 0 &&
attr->value_tag == IPP_TAG_INTEGER)
completed_time = attr->values[0].integer;
else if (strcmp(attr->name, "time-at-creation") == 0 &&
attr->value_tag == IPP_TAG_INTEGER)
creation_time = attr->values[0].integer;
else if (strcmp(attr->name, "time-at-processing") == 0 &&
attr->value_tag == IPP_TAG_INTEGER)
processing_time = attr->values[0].integer;
else if (strcmp(attr->name, "job-printer-uri") == 0 &&
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") == 0 &&
attr->value_tag == IPP_TAG_NAME)
user = attr->values[0].string.text;
else if (strcmp(attr->name, "document-format") == 0 &&
attr->value_tag == IPP_TAG_MIMETYPE)
format = attr->values[0].string.text;
else if (strcmp(attr->name, "job-name") == 0 &&
(attr->value_tag == IPP_TAG_TEXT ||
attr->value_tag == IPP_TAG_NAME))
title = attr->values[0].string.text;
attr = attr->next;
}
if (dest == NULL || id == 0)
{
if (attr == NULL)
break;
else
continue;
}
if (n == 0)
temp = malloc(sizeof(cups_job_t));
else
temp = realloc(*jobs, sizeof(cups_job_t) * (n + 1));
if (temp == NULL)
{
cupsFreeJobs(n, *jobs);
*jobs = NULL;
ippDelete(response);
return (0);
}
*jobs = temp;
temp += n;
n ++;
temp->dest = strdup(dest);
temp->user = strdup(user);
temp->format = strdup(format);
temp->title = strdup(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 == NULL)
break;
}
ippDelete(response);
}
else
cg->last_error = IPP_BAD_REQUEST;
if (n == 0 && cg->last_error >= IPP_BAD_REQUEST)
return (-1);
else
return (n);
}
const char *
cupsGetPPD(const char *name)
{
cups_globals_t *cg = _cups_globals();
if (!cups_connect(name, NULL, NULL))
{
DEBUG_puts("Unable to connect to server!");
cg->last_error = IPP_SERVICE_UNAVAILABLE;
return (NULL);
}
return (cupsGetPPD2(cg->http, name));
}
const char *
cupsGetPPD2(http_t *http,
const char *name)
{
int i;
http_t *http2;
ipp_t *request,
*response;
ipp_attribute_t *attr;
cups_lang_t *language;
int fd;
char uri[HTTP_MAX_URI],
printer[HTTP_MAX_URI],
method[HTTP_MAX_URI],
username[HTTP_MAX_URI],
hostname[HTTP_MAX_URI],
resource[HTTP_MAX_URI],
leadPrinter[HTTP_MAX_URI];
int isRemotePool = 0;
int port;
http_status_t status;
cups_globals_t *cg = _cups_globals();
static const char * const requested_attrs[] =
{
"printer-uri-supported",
"printer-type",
"member-uris"
};
DEBUG_printf(("cupsGetPPD2(http=%p, name=\"%s\")\n", http,
name ? name : "(null)"));
if (!http || !name)
{
cg->last_error = IPP_INTERNAL_ERROR;
return (NULL);
}
request = ippNew();
request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
request->request.op.request_id = 1;
language = cupsLangDefault();
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
"attributes-charset", NULL, cupsLangEncoding(language));
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
"attributes-natural-language", NULL, language->language);
cupsLangFree(language);
snprintf(uri, sizeof(uri), "ipp://localhost/printers/%s", name);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
"printer-uri", NULL, uri);
DEBUG_printf(("cupsGetPPD2: printer-uri=\"%s\"\n", uri));
ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
"requested-attributes",
sizeof(requested_attrs) / sizeof(requested_attrs[0]),
NULL, requested_attrs);
if ((response = cupsDoRequest(http, request, "/")) != NULL)
{
cg->last_error = response->request.status.status_code;
printer[0] = '\0';
hostname[0] = '\0';
if ((attr = ippFindAttribute(response, "member-uris", IPP_TAG_URI)) != NULL)
{
for (i = 0; i < attr->num_values; i ++)
{
httpSeparate(attr->values[0].string.text, method, username, hostname,
&port, resource);
if (strncmp(resource, "/printers/", 10) == 0)
{
strlcpy(printer, resource + 10, sizeof(printer));
break;
}
}
}
else if ((attr = ippFindAttribute(response, "printer-uri-supported",
IPP_TAG_URI)) != NULL)
{
httpSeparate(attr->values[0].string.text, method, username, hostname,
&port, resource);
strlcpy(printer, strrchr(resource, '/') + 1, sizeof(printer));
}
if ((attr = ippFindAttribute(response, "printer-type", IPP_TAG_ENUM)) != NULL)
{
isRemotePool = (attr->values[0].integer & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_CLASS))
== (CUPS_PRINTER_REMOTE | CUPS_PRINTER_CLASS);
}
ippDelete(response);
gethostname(uri, sizeof(uri));
if (strcasecmp(uri, hostname) == 0)
strcpy(hostname, "localhost");
}
if (!printer[0])
{
cg->last_error = IPP_NOT_FOUND;
return (NULL);
}
if (!strcasecmp(http->hostname, hostname) ||
(!strcasecmp(http->hostname, "localhost") && !strcasecmp(hostname, "127.0.0.1")) ||
(!strcasecmp(http->hostname, "127.0.0.1") && !strcasecmp(hostname, "localhost")))
http2 = http;
else if ((http2 = httpConnectEncrypt(hostname, ippPort(),
cupsEncryption())) == NULL)
{
DEBUG_puts("Unable to connect to server!");
cg->last_error = IPP_SERVICE_UNAVAILABLE;
return (NULL);
}
if (isRemotePool)
{
if (cups_getleadprinter(http2, hostname, printer, leadPrinter, sizeof(leadPrinter)))
{
DEBUG_puts("Unable to get lead printer!");
cg->last_error = IPP_SERVICE_UNAVAILABLE;
return (NULL);
}
strlcpy(printer, leadPrinter, sizeof(printer));
}
if ((fd = cupsTempFd(cg->ppd_filename, sizeof(cg->ppd_filename))) < 0)
{
cg->last_error = IPP_INTERNAL_ERROR;
if (http2 != http)
httpClose(http2);
return (NULL);
}
snprintf(resource, sizeof(resource), "/printers/%s.ppd", printer);
status = cupsGetFd(http2, resource, fd);
close(fd);
if (http2 != http)
httpClose(http2);
if (status != HTTP_OK)
{
switch (status)
{
case HTTP_NOT_FOUND :
cg->last_error = IPP_NOT_FOUND;
break;
case HTTP_ERROR :
DEBUG_puts("Mapping HTTP error to IPP_ERROR");
cg->last_error = IPP_ERROR;
break;
case HTTP_UNAUTHORIZED :
cg->last_error = IPP_NOT_AUTHORIZED;
break;
default :
cg->last_error = IPP_INTERNAL_ERROR;
break;
}
unlink(cg->ppd_filename);
return (NULL);
}
return (cg->ppd_filename);
}
int
cupsGetPrinters(char ***printers)
{
int n;
ipp_t *request,
*response;
ipp_attribute_t *attr;
cups_lang_t *language;
char **temp;
cups_globals_t *cg = _cups_globals();
if (printers == NULL)
{
cg->last_error = IPP_INTERNAL_ERROR;
return (0);
}
if (!cups_connect("default", NULL, NULL))
{
DEBUG_puts("Unable to connect to server!");
cg->last_error = IPP_SERVICE_UNAVAILABLE;
return (0);
}
request = ippNew();
request->request.op.operation_id = CUPS_GET_PRINTERS;
request->request.op.request_id = 1;
language = cupsLangDefault();
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
"attributes-charset", NULL, cupsLangEncoding(language));
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
"attributes-natural-language", NULL, language->language);
cupsLangFree(language);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD,
"requested-attributes", NULL, "printer-name");
n = 0;
*printers = NULL;
if ((response = cupsDoRequest(cg->http, request, "/")) != NULL)
{
cg->last_error = response->request.status.status_code;
for (attr = response->attrs; attr != NULL; attr = attr->next)
if (attr->name != NULL &&
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);
}
else
cg->last_error = IPP_BAD_REQUEST;
return (n);
}
ipp_status_t
cupsLastError(void)
{
return (_cups_globals()->last_error);
}
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)\n",
name, filename, title, num_options, options));
return (cupsPrintFiles(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)\n",
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)
{
cups_globals_t *cg = _cups_globals();
DEBUG_printf(("cupsPrintFiles(name=\"%s\", num_files=%d, "
"files=%p, title=\"%s\", num_options=%d, options=%p)\n",
name, num_files, files, title, num_options, options));
if (!cups_connect(name, NULL, NULL))
{
DEBUG_printf(("cupsPrintFiles: Unable to open connection - %s.\n",
strerror(errno)));
DEBUG_puts("Unable to connect to server!");
cg->last_error = IPP_SERVICE_UNAVAILABLE;
return (0);
}
return (cupsPrintFiles2(cg->http, 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;
const char *val;
ipp_t *request;
ipp_t *response;
ipp_attribute_t *attr;
char uri[HTTP_MAX_URI];
cups_lang_t *language;
int jobid;
cups_globals_t *cg = _cups_globals();
DEBUG_printf(("cupsPrintFiles(http=%p, name=\"%s\", num_files=%d, "
"files=%p, title=\"%s\", num_options=%d, options=%p)\n",
http, name, num_files, files, title, num_options, options));
if (!http || !name || num_files < 1 || files == NULL)
return (0);
language = cupsLangDefault();
if ((request = ippNew()) == NULL)
return (0);
request->request.op.operation_id = num_files == 1 ? IPP_PRINT_JOB :
IPP_CREATE_JOB;
request->request.op.request_id = 1;
snprintf(uri, sizeof(uri), "ipp://%s:%d/printers/%s", http->hostname,
ippPort(), name);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
"attributes-charset", NULL, cupsLangEncoding(language));
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
"attributes-natural-language", NULL,
language != NULL ? language->language : "C");
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 (title)
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "job-name", NULL,
title);
cupsEncodeOptions(request, num_options, options);
snprintf(uri, sizeof(uri), "/printers/%s", name);
if (num_files == 1)
response = cupsDoFileRequest(http, request, uri, *files);
else
response = cupsDoRequest(http, request, uri);
if (response == NULL)
jobid = 0;
else if (response->request.status.status_code > IPP_OK_CONFLICT)
{
DEBUG_printf(("IPP response code was 0x%x!\n",
response->request.status.status_code));
jobid = 0;
}
else if ((attr = ippFindAttribute(response, "job-id", IPP_TAG_INTEGER)) == NULL)
{
DEBUG_puts("No job ID!");
cg->last_error = IPP_SERVICE_UNAVAILABLE;
jobid = 0;
}
else
jobid = attr->values[0].integer;
if (response != NULL)
ippDelete(response);
if (jobid > 0 && num_files > 1)
for (i = 0; i < num_files; i ++)
{
if ((request = ippNew()) == NULL)
return (0);
request->request.op.operation_id = IPP_SEND_DOCUMENT;
request->request.op.request_id = 1;
snprintf(uri, sizeof(uri), "ipp://%s:%d/jobs/%d", http->hostname,
ippPort(), jobid);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
"attributes-charset", NULL, cupsLangEncoding(language));
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
"attributes-natural-language", NULL,
language != NULL ? language->language : "C");
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri",
NULL, uri);
if (cupsGetOption("raw", num_options, options))
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE, "document-format",
NULL, "application/vnd.cups-raw");
else if ((val = cupsGetOption("document-format", num_options, options)) != NULL)
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE, "document-format",
NULL, val);
else
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_MIMETYPE, "document-format",
NULL, "application/octet-stream");
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
NULL, cupsUser());
if (i == (num_files - 1))
ippAddBoolean(request, IPP_TAG_OPERATION, "last-document", 1);
snprintf(uri, sizeof(uri), "/printers/%s", name);
if ((response = cupsDoFileRequest(http, request, uri,
files[i])) != NULL)
ippDelete(response);
}
cupsLangFree(language);
return (jobid);
}
static char *
cups_connect(const char *name,
char *printer,
char *hostname)
{
char hostbuf[HTTP_MAX_URI];
cups_globals_t *cg = _cups_globals();
DEBUG_printf(("cups_connect(\"%s\", %p, %p)\n", name, printer, hostname));
if (name == NULL)
{
cg->last_error = IPP_BAD_REQUEST;
return (NULL);
}
strlcpy(hostbuf, cupsServer(), sizeof(hostbuf));
if (hostname != NULL)
strlcpy(hostname, hostbuf, HTTP_MAX_URI);
else
hostname = hostbuf;
if (printer != NULL)
strlcpy(printer, name, HTTP_MAX_URI);
else
printer = (char *)name;
if (cg->http != NULL)
{
if (strcasecmp(cg->http->hostname, hostname) == 0)
return (printer);
httpClose(cg->http);
}
DEBUG_printf(("connecting to %s on port %d...\n", hostname, ippPort()));
if ((cg->http = httpConnectEncrypt(hostname, ippPort(),
cupsEncryption())) == NULL)
{
DEBUG_puts("Unable to connect to server!");
cg->last_error = IPP_SERVICE_UNAVAILABLE;
return (NULL);
}
else
return (printer);
}
static int
cups_getleadprinter(http_t *http,
const char *hostname,
const char *className,
char *printer,
size_t bufferSize)
{
ipp_t *request;
ipp_t *response;
cups_lang_t *language;
char uri[HTTP_MAX_URI],
method[HTTP_MAX_URI],
user[HTTP_MAX_URI],
host[HTTP_MAX_URI],
resource[HTTP_MAX_URI];
int i,
port;
ipp_attribute_t *attr;
int result = 0;
static const char * const requested_attrs[] =
{
"member-uris"
};
if (!http || !hostname || !className || !printer)
{
return IPP_INTERNAL_ERROR;
}
printer[0] = '\0';
request = ippNew();
request->request.op.operation_id = IPP_GET_PRINTER_ATTRIBUTES;
request->request.op.request_id = 1;
language = cupsLangDefault();
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
"attributes-charset", NULL, cupsLangEncoding(language));
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
"attributes-natural-language", NULL, language->language);
cupsLangFree(language);
snprintf(uri, sizeof(uri), "ipp://%s/classes/%s", hostname, className);
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);
if ((response = cupsDoRequest(http, request, "/")) != NULL)
{
result = response->request.status.status_code;
if ((attr = ippFindAttribute(response, "member-uris", IPP_TAG_URI)) != NULL)
{
for (i = 0; i < attr->num_values; i ++)
{
httpSeparate(attr->values[0].string.text, method, user, host,
&port, resource);
if (strncmp(resource, "/printers/", 10) == 0)
{
strlcpy(printer, resource + 10, bufferSize);
break;
}
}
}
ippDelete(response);
}
else
{
ippDelete(request);
result = IPP_BAD_REQUEST;
}
return result;
}