#include "cupsd.h"
#ifdef HAVE_LIBPAPER
# include <paper.h>
#endif
#ifdef __APPLE__
# ifdef HAVE_MEMBERSHIP_H
# include <membership.h>
# endif
# ifdef HAVE_MEMBERSHIPPRIV_H
# include <membershipPriv.h>
# else
extern int mbr_user_name_to_uuid(const char* name, uuid_t uu);
extern int mbr_group_name_to_uuid(const char* name, uuid_t uu);
extern int mbr_check_membership_by_id(uuid_t user, gid_t group, int* ismember);
# endif
#endif
static void accept_jobs(cupsd_client_t *con, ipp_attribute_t *uri);
static void add_class(cupsd_client_t *con, ipp_attribute_t *uri);
static int add_file(cupsd_client_t *con, cupsd_job_t *job,
mime_type_t *filetype, int compression);
static cupsd_job_t *add_job(cupsd_client_t *con, cupsd_printer_t *printer,
mime_type_t *filetype);
static void add_job_state_reasons(cupsd_client_t *con, cupsd_job_t *job);
static void add_job_subscriptions(cupsd_client_t *con, cupsd_job_t *job);
static void add_job_uuid(cupsd_client_t *con, cupsd_job_t *job);
static void add_printer(cupsd_client_t *con, ipp_attribute_t *uri);
static void add_printer_state_reasons(cupsd_client_t *con,
cupsd_printer_t *p);
static void add_queued_job_count(cupsd_client_t *con, cupsd_printer_t *p);
static void apply_printer_defaults(cupsd_printer_t *printer,
cupsd_job_t *job);
static void authenticate_job(cupsd_client_t *con, ipp_attribute_t *uri);
static void cancel_all_jobs(cupsd_client_t *con, ipp_attribute_t *uri);
static void cancel_job(cupsd_client_t *con, ipp_attribute_t *uri);
static void cancel_subscription(cupsd_client_t *con, int id);
static int check_quotas(cupsd_client_t *con, cupsd_printer_t *p);
static ipp_attribute_t *copy_attribute(ipp_t *to, ipp_attribute_t *attr,
int quickcopy);
static void copy_attrs(ipp_t *to, ipp_t *from, cups_array_t *ra,
ipp_tag_t group, int quickcopy);
static int copy_banner(cupsd_client_t *con, cupsd_job_t *job,
const char *name);
static int copy_file(const char *from, const char *to);
static int copy_model(cupsd_client_t *con, const char *from,
const char *to);
static void copy_job_attrs(cupsd_client_t *con,
cupsd_job_t *job,
cups_array_t *ra);
static void copy_printer_attrs(cupsd_client_t *con,
cupsd_printer_t *printer,
cups_array_t *ra);
static void copy_subscription_attrs(cupsd_client_t *con,
cupsd_subscription_t *sub,
cups_array_t *ra);
static void create_job(cupsd_client_t *con, ipp_attribute_t *uri);
static cups_array_t *create_requested_array(ipp_t *request);
static void create_subscription(cupsd_client_t *con, ipp_attribute_t *uri);
static void delete_printer(cupsd_client_t *con, ipp_attribute_t *uri);
static void get_default(cupsd_client_t *con);
static void get_devices(cupsd_client_t *con);
static void get_jobs(cupsd_client_t *con, ipp_attribute_t *uri);
static void get_job_attrs(cupsd_client_t *con, ipp_attribute_t *uri);
static void get_notifications(cupsd_client_t *con);
static void get_ppd(cupsd_client_t *con, ipp_attribute_t *uri);
static void get_ppds(cupsd_client_t *con);
static void get_printers(cupsd_client_t *con, int type);
static void get_printer_attrs(cupsd_client_t *con, ipp_attribute_t *uri);
static void get_subscription_attrs(cupsd_client_t *con, int sub_id);
static void get_subscriptions(cupsd_client_t *con, ipp_attribute_t *uri);
static const char *get_username(cupsd_client_t *con);
static void hold_job(cupsd_client_t *con, ipp_attribute_t *uri);
static void move_job(cupsd_client_t *con, ipp_attribute_t *uri);
static int ppd_parse_line(const char *line, char *option, int olen,
char *choice, int clen);
static void print_job(cupsd_client_t *con, ipp_attribute_t *uri);
static void read_ps_job_ticket(cupsd_client_t *con);
static void reject_jobs(cupsd_client_t *con, ipp_attribute_t *uri);
static void release_job(cupsd_client_t *con, ipp_attribute_t *uri);
static void renew_subscription(cupsd_client_t *con, int sub_id);
static void restart_job(cupsd_client_t *con, ipp_attribute_t *uri);
static void save_auth_info(cupsd_client_t *con, cupsd_job_t *job,
ipp_attribute_t *auth_info);
#if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
static void save_krb5_creds(cupsd_client_t *con, cupsd_job_t *job);
#endif
static void send_document(cupsd_client_t *con, ipp_attribute_t *uri);
static void send_http_error(cupsd_client_t *con, http_status_t status,
cupsd_printer_t *printer);
static void send_ipp_status(cupsd_client_t *con, ipp_status_t status,
const char *message, ...)
# ifdef __GNUC__
__attribute__ ((__format__ (__printf__, 3, 4)))
# endif
;
static void set_default(cupsd_client_t *con, ipp_attribute_t *uri);
static void set_job_attrs(cupsd_client_t *con, ipp_attribute_t *uri);
static void set_printer_defaults(cupsd_client_t *con,
cupsd_printer_t *printer);
static void start_printer(cupsd_client_t *con, ipp_attribute_t *uri);
static void stop_printer(cupsd_client_t *con, ipp_attribute_t *uri);
static void url_encode_attr(ipp_attribute_t *attr, char *buffer,
int bufsize);
static char *url_encode_string(const char *s, char *buffer, int bufsize);
static int user_allowed(cupsd_printer_t *p, const char *username);
static void validate_job(cupsd_client_t *con, ipp_attribute_t *uri);
static int validate_name(const char *name);
static int validate_user(cupsd_job_t *job, cupsd_client_t *con,
const char *owner, char *username,
int userlen);
int
cupsdProcessIPPRequest(
cupsd_client_t *con)
{
ipp_tag_t group;
ipp_attribute_t *attr;
ipp_attribute_t *charset;
ipp_attribute_t *language;
ipp_attribute_t *uri;
ipp_attribute_t *username;
int sub_id;
cupsdLogMessage(CUPSD_LOG_DEBUG2,
"cupsdProcessIPPRequest(%p[%d]): operation_id = %04x",
con, con->http.fd, con->request->request.op.operation_id);
con->response = ippNew();
con->response->request.status.version[0] =
con->request->request.op.version[0];
con->response->request.status.version[1] =
con->request->request.op.version[1];
con->response->request.status.request_id =
con->request->request.op.request_id;
if (con->request->request.any.version[0] != 1)
{
cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL,
"%04X %s Bad request version number %d.%d",
IPP_VERSION_NOT_SUPPORTED, con->http.hostname,
con->request->request.any.version[0],
con->request->request.any.version[1]);
send_ipp_status(con, IPP_VERSION_NOT_SUPPORTED,
_("Bad request version number %d.%d!"),
con->request->request.any.version[0],
con->request->request.any.version[1]);
}
else if (!con->request->attrs)
{
cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL,
"%04X %s No attributes in request",
IPP_BAD_REQUEST, con->http.hostname);
send_ipp_status(con, IPP_BAD_REQUEST, _("No attributes in request!"));
}
else
{
for (attr = con->request->attrs, group = attr->group_tag;
attr;
attr = attr->next)
if (attr->group_tag < group && attr->group_tag != IPP_TAG_ZERO)
{
cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL,
"%04X %s Attribute groups are out of order",
IPP_BAD_REQUEST, con->http.hostname);
send_ipp_status(con, IPP_BAD_REQUEST,
_("Attribute groups are out of order (%x < %x)!"),
attr->group_tag, group);
break;
}
else
group = attr->group_tag;
if (!attr)
{
attr = con->request->attrs;
if (attr && !strcmp(attr->name, "attributes-charset") &&
(attr->value_tag & IPP_TAG_MASK) == IPP_TAG_CHARSET)
charset = attr;
else
charset = NULL;
if (attr)
attr = attr->next;
if (attr && !strcmp(attr->name, "attributes-natural-language") &&
(attr->value_tag & IPP_TAG_MASK) == IPP_TAG_LANGUAGE)
language = attr;
else
language = NULL;
if ((attr = ippFindAttribute(con->request, "printer-uri",
IPP_TAG_URI)) != NULL)
uri = attr;
else if ((attr = ippFindAttribute(con->request, "job-uri",
IPP_TAG_URI)) != NULL)
uri = attr;
else if (con->request->request.op.operation_id == CUPS_GET_PPD)
uri = ippFindAttribute(con->request, "ppd-name", IPP_TAG_NAME);
else
uri = NULL;
if (charset)
ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
"attributes-charset", NULL,
charset->values[0].string.text);
else
ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
"attributes-charset", NULL, DefaultCharset);
if (language)
ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
"attributes-natural-language", NULL,
language->values[0].string.text);
else
ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
"attributes-natural-language", NULL, DefaultLanguage);
if (!charset || !language ||
(!uri &&
con->request->request.op.operation_id != CUPS_GET_DEFAULT &&
con->request->request.op.operation_id != CUPS_GET_PRINTERS &&
con->request->request.op.operation_id != CUPS_GET_CLASSES &&
con->request->request.op.operation_id != CUPS_GET_DEVICES &&
con->request->request.op.operation_id != CUPS_GET_PPDS))
{
if (!charset)
{
cupsdLogMessage(CUPSD_LOG_ERROR,
"Missing attributes-charset attribute!");
cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL,
"%04X %s Missing attributes-charset attribute",
IPP_BAD_REQUEST, con->http.hostname);
}
if (!language)
{
cupsdLogMessage(CUPSD_LOG_ERROR,
"Missing attributes-natural-language attribute!");
cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL,
"%04X %s Missing attributes-natural-language attribute",
IPP_BAD_REQUEST, con->http.hostname);
}
if (!uri)
{
cupsdLogMessage(CUPSD_LOG_ERROR,
"Missing printer-uri, job-uri, or ppd-name "
"attribute!");
cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL,
"%04X %s Missing printer-uri, job-uri, or ppd-name "
"attribute", IPP_BAD_REQUEST, con->http.hostname);
}
cupsdLogMessage(CUPSD_LOG_DEBUG, "Request attributes follow...");
for (attr = con->request->attrs; attr; attr = attr->next)
cupsdLogMessage(CUPSD_LOG_DEBUG,
"attr \"%s\": group_tag = %x, value_tag = %x",
attr->name ? attr->name : "(null)", attr->group_tag,
attr->value_tag);
cupsdLogMessage(CUPSD_LOG_DEBUG, "End of attributes...");
send_ipp_status(con, IPP_BAD_REQUEST,
_("Missing required attributes!"));
}
else
{
if ((username = ippFindAttribute(con->request, "requesting-user-name",
IPP_TAG_NAME)) != NULL)
{
if (!strcmp(username->values[0].string.text, "root") &&
strcasecmp(con->http.hostname, "localhost") &&
strcmp(con->username, "root"))
{
_cupsStrFree(username->values[0].string.text);
username->values[0].string.text = _cupsStrAlloc(RemoteRoot);
}
}
if ((attr = ippFindAttribute(con->request, "notify-subscription-id",
IPP_TAG_INTEGER)) != NULL)
sub_id = attr->values[0].integer;
else
sub_id = 0;
if (uri)
cupsdLogMessage(CUPSD_LOG_DEBUG, "%s %s",
ippOpString(con->request->request.op.operation_id),
uri->values[0].string.text);
else
cupsdLogMessage(CUPSD_LOG_DEBUG, "%s",
ippOpString(con->request->request.op.operation_id));
switch (con->request->request.op.operation_id)
{
case IPP_PRINT_JOB :
print_job(con, uri);
break;
case IPP_VALIDATE_JOB :
validate_job(con, uri);
break;
case IPP_CREATE_JOB :
create_job(con, uri);
break;
case IPP_SEND_DOCUMENT :
send_document(con, uri);
break;
case IPP_CANCEL_JOB :
cancel_job(con, uri);
break;
case IPP_GET_JOB_ATTRIBUTES :
get_job_attrs(con, uri);
break;
case IPP_GET_JOBS :
get_jobs(con, uri);
break;
case IPP_GET_PRINTER_ATTRIBUTES :
get_printer_attrs(con, uri);
break;
case IPP_HOLD_JOB :
hold_job(con, uri);
break;
case IPP_RELEASE_JOB :
release_job(con, uri);
break;
case IPP_RESTART_JOB :
restart_job(con, uri);
break;
case IPP_PAUSE_PRINTER :
stop_printer(con, uri);
break;
case IPP_RESUME_PRINTER :
start_printer(con, uri);
break;
case IPP_PURGE_JOBS :
cancel_all_jobs(con, uri);
break;
case IPP_SET_JOB_ATTRIBUTES :
set_job_attrs(con, uri);
break;
case CUPS_GET_DEFAULT :
get_default(con);
break;
case CUPS_GET_PRINTERS :
get_printers(con, 0);
break;
case CUPS_GET_CLASSES :
get_printers(con, CUPS_PRINTER_CLASS);
break;
case CUPS_ADD_PRINTER :
add_printer(con, uri);
break;
case CUPS_DELETE_PRINTER :
delete_printer(con, uri);
break;
case CUPS_ADD_CLASS :
add_class(con, uri);
break;
case CUPS_DELETE_CLASS :
delete_printer(con, uri);
break;
case CUPS_ACCEPT_JOBS :
case IPP_ENABLE_PRINTER :
accept_jobs(con, uri);
break;
case CUPS_REJECT_JOBS :
case IPP_DISABLE_PRINTER :
reject_jobs(con, uri);
break;
case CUPS_SET_DEFAULT :
set_default(con, uri);
break;
case CUPS_GET_DEVICES :
get_devices(con);
break;
case CUPS_GET_PPD :
get_ppd(con, uri);
break;
case CUPS_GET_PPDS :
get_ppds(con);
break;
case CUPS_MOVE_JOB :
move_job(con, uri);
break;
case CUPS_AUTHENTICATE_JOB :
authenticate_job(con, uri);
break;
case IPP_CREATE_PRINTER_SUBSCRIPTION :
case IPP_CREATE_JOB_SUBSCRIPTION :
create_subscription(con, uri);
break;
case IPP_GET_SUBSCRIPTION_ATTRIBUTES :
get_subscription_attrs(con, sub_id);
break;
case IPP_GET_SUBSCRIPTIONS :
get_subscriptions(con, uri);
break;
case IPP_RENEW_SUBSCRIPTION :
renew_subscription(con, sub_id);
break;
case IPP_CANCEL_SUBSCRIPTION :
cancel_subscription(con, sub_id);
break;
case IPP_GET_NOTIFICATIONS :
get_notifications(con);
break;
default :
cupsdAddEvent(CUPSD_EVENT_SERVER_AUDIT, NULL, NULL,
"%04X %s Operation %04X (%s) not supported",
IPP_OPERATION_NOT_SUPPORTED, con->http.hostname,
con->request->request.op.operation_id,
ippOpString(con->request->request.op.operation_id));
send_ipp_status(con, IPP_OPERATION_NOT_SUPPORTED,
_("%s not supported!"),
ippOpString(
con->request->request.op.operation_id));
break;
}
}
}
}
if (con->response)
{
cupsdLogMessage(CUPSD_LOG_DEBUG,
"cupsdProcessIPPRequest: %d status_code=%x (%s)",
con->http.fd, con->response->request.status.status_code,
ippErrorString(con->response->request.status.status_code));
if (cupsdSendHeader(con, HTTP_OK, "application/ipp", AUTH_NONE))
{
#ifdef CUPSD_USE_CHUNKING
if (con->http.version == HTTP_1_1)
{
if (httpPrintf(HTTP(con), "Transfer-Encoding: chunked\r\n\r\n") < 0)
return (0);
if (cupsdFlushHeader(con) < 0)
return (0);
con->http.data_encoding = HTTP_ENCODE_CHUNKED;
}
else
#endif
{
size_t length;
length = ippLength(con->response);
if (con->file >= 0 && !con->pipe_pid)
{
struct stat fileinfo;
if (!fstat(con->file, &fileinfo))
length += fileinfo.st_size;
}
if (httpPrintf(HTTP(con), "Content-Length: " CUPS_LLFMT "\r\n\r\n",
CUPS_LLCAST length) < 0)
return (0);
if (cupsdFlushHeader(con) < 0)
return (0);
con->http.data_encoding = HTTP_ENCODE_LENGTH;
con->http.data_remaining = length;
if (con->http.data_remaining <= INT_MAX)
con->http._data_remaining = con->http.data_remaining;
else
con->http._data_remaining = INT_MAX;
}
cupsdAddSelect(con->http.fd, (cupsd_selfunc_t)cupsdReadClient,
(cupsd_selfunc_t)cupsdWriteClient, con);
return (1);
}
else
{
return (0);
}
}
else
{
return (1);
}
}
void
cupsdTimeoutJob(cupsd_job_t *job)
{
cupsd_printer_t *printer;
ipp_attribute_t *attr;
int kbytes;
job->pending_timeout = 0;
printer = cupsdFindDest(job->dest);
attr = ippFindAttribute(job->attrs, "job-sheets", IPP_TAG_NAME);
if (printer &&
!(printer->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)) &&
attr && attr->num_values > 1)
{
cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Adding end banner page \"%s\".",
job->id, attr->values[1].string.text);
kbytes = copy_banner(NULL, job, attr->values[1].string.text);
cupsdUpdateQuota(printer, job->username, 0, kbytes);
}
}
static void
accept_jobs(cupsd_client_t *con,
ipp_attribute_t *uri)
{
http_status_t status;
cups_ptype_t dtype;
cupsd_printer_t *printer;
cupsdLogMessage(CUPSD_LOG_DEBUG2, "accept_jobs(%p[%d], %s)", con,
con->http.fd, uri->values[0].string.text);
if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
{
send_ipp_status(con, IPP_NOT_FOUND,
_("The printer or class was not found."));
return;
}
if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
{
send_http_error(con, status, printer);
return;
}
printer->accepting = 1;
printer->state_message[0] = '\0';
cupsdAddPrinterHistory(printer);
if (dtype & CUPS_PRINTER_CLASS)
{
cupsdSaveAllClasses();
cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" now accepting jobs (\"%s\").",
printer->name, get_username(con));
}
else
{
cupsdSaveAllPrinters();
cupsdLogMessage(CUPSD_LOG_INFO,
"Printer \"%s\" now accepting jobs (\"%s\").",
printer->name, get_username(con));
}
con->response->request.status.status_code = IPP_OK;
}
static void
add_class(cupsd_client_t *con,
ipp_attribute_t *uri)
{
http_status_t status;
int i;
char method[HTTP_MAX_URI],
username[HTTP_MAX_URI],
host[HTTP_MAX_URI],
resource[HTTP_MAX_URI];
int port;
cupsd_printer_t *pclass,
*member;
cups_ptype_t dtype;
ipp_attribute_t *attr;
int modify;
char newname[IPP_MAX_NAME];
int need_restart_job;
cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_class(%p[%d], %s)", con,
con->http.fd, uri->values[0].string.text);
httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method,
sizeof(method), username, sizeof(username), host,
sizeof(host), &port, resource, sizeof(resource));
if (strncmp(resource, "/classes/", 9) || strlen(resource) == 9)
{
send_ipp_status(con, IPP_BAD_REQUEST,
_("The printer-uri must be of the form "
"\"ipp://HOSTNAME/classes/CLASSNAME\"."));
return;
}
if (!validate_name(resource + 9))
{
send_ipp_status(con, IPP_BAD_REQUEST,
_("The printer-uri \"%s\" contains invalid characters."),
uri->values[0].string.text);
return;
}
if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
{
send_http_error(con, status, NULL);
return;
}
if ((pclass = cupsdFindClass(resource + 9)) == NULL)
{
if ((pclass = cupsdFindPrinter(resource + 9)) != NULL &&
!(pclass->type & CUPS_PRINTER_DISCOVERED))
{
send_ipp_status(con, IPP_NOT_POSSIBLE,
_("A printer named \"%s\" already exists!"),
resource + 9);
return;
}
pclass = cupsdAddClass(resource + 9);
modify = 0;
}
else if (pclass->type & CUPS_PRINTER_IMPLICIT)
{
if (ImplicitAnyClasses)
{
snprintf(newname, sizeof(newname), "Any%s", resource + 9);
cupsdRenamePrinter(pclass, newname);
}
else
cupsdDeletePrinter(pclass, 1);
pclass = cupsdAddClass(resource + 9);
modify = 0;
}
else if (pclass->type & CUPS_PRINTER_DISCOVERED)
{
snprintf(newname, sizeof(newname), "%s@%s", resource + 9, pclass->hostname);
cupsdRenamePrinter(pclass, newname);
pclass = cupsdAddClass(resource + 9);
modify = 0;
}
else
modify = 1;
need_restart_job = 0;
if ((attr = ippFindAttribute(con->request, "printer-location",
IPP_TAG_TEXT)) != NULL)
cupsdSetString(&pclass->location, attr->values[0].string.text);
if ((attr = ippFindAttribute(con->request, "printer-info",
IPP_TAG_TEXT)) != NULL)
cupsdSetString(&pclass->info, attr->values[0].string.text);
if ((attr = ippFindAttribute(con->request, "printer-is-accepting-jobs",
IPP_TAG_BOOLEAN)) != NULL)
{
cupsdLogMessage(CUPSD_LOG_INFO,
"Setting %s printer-is-accepting-jobs to %d (was %d.)",
pclass->name, attr->values[0].boolean, pclass->accepting);
pclass->accepting = attr->values[0].boolean;
cupsdAddPrinterHistory(pclass);
}
if ((attr = ippFindAttribute(con->request, "printer-is-shared",
IPP_TAG_BOOLEAN)) != NULL)
{
if (pclass->shared && !attr->values[0].boolean)
cupsdDeregisterPrinter(pclass, 1);
cupsdLogMessage(CUPSD_LOG_INFO,
"Setting %s printer-is-shared to %d (was %d.)",
pclass->name, attr->values[0].boolean, pclass->shared);
pclass->shared = attr->values[0].boolean;
}
if ((attr = ippFindAttribute(con->request, "printer-state",
IPP_TAG_ENUM)) != NULL)
{
if (attr->values[0].integer != IPP_PRINTER_IDLE &&
attr->values[0].integer != IPP_PRINTER_STOPPED)
{
send_ipp_status(con, IPP_BAD_REQUEST,
_("Attempt to set %s printer-state to bad value %d!"),
pclass->name, attr->values[0].integer);
return;
}
cupsdLogMessage(CUPSD_LOG_INFO, "Setting %s printer-state to %d (was %d.)",
pclass->name, attr->values[0].integer, pclass->state);
if (attr->values[0].integer == IPP_PRINTER_STOPPED)
cupsdStopPrinter(pclass, 0);
else
{
cupsdSetPrinterState(pclass, (ipp_pstate_t)(attr->values[0].integer), 0);
need_restart_job = 1;
}
}
if ((attr = ippFindAttribute(con->request, "printer-state-message",
IPP_TAG_TEXT)) != NULL)
{
strlcpy(pclass->state_message, attr->values[0].string.text,
sizeof(pclass->state_message));
cupsdAddPrinterHistory(pclass);
}
if ((attr = ippFindAttribute(con->request, "member-uris",
IPP_TAG_URI)) != NULL)
{
need_restart_job = 1;
if (pclass->num_printers > 0)
{
free(pclass->printers);
pclass->num_printers = 0;
}
for (i = 0; i < attr->num_values; i ++)
{
if (!cupsdValidateDest(attr->values[i].string.text, &dtype, &member))
{
send_ipp_status(con, IPP_NOT_FOUND,
_("The printer or class was not found."));
return;
}
cupsdAddPrinterToClass(pclass, member);
}
}
set_printer_defaults(con, pclass);
if ((attr = ippFindAttribute(con->request, "auth-info-required",
IPP_TAG_KEYWORD)) != NULL)
cupsdSetAuthInfoRequired(pclass, NULL, attr);
cupsdSetPrinterAttrs(pclass);
cupsdSaveAllClasses();
if (need_restart_job && pclass->job)
{
cupsd_job_t *job;
job = (cupsd_job_t *)pclass->job;
cupsdStopJob(job, 1);
job->state->values[0].integer = IPP_JOB_PENDING;
job->state_value = IPP_JOB_PENDING;
}
if (need_restart_job)
cupsdCheckJobs();
cupsdWritePrintcap();
if (modify)
{
cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED, pclass, NULL,
"Class \"%s\" modified by \"%s\".", pclass->name,
get_username(con));
cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" modified by \"%s\".",
pclass->name, get_username(con));
}
else
{
cupsdAddPrinterHistory(pclass);
cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, pclass, NULL,
"New class \"%s\" added by \"%s\".", pclass->name,
get_username(con));
cupsdLogMessage(CUPSD_LOG_INFO, "New class \"%s\" added by \"%s\".",
pclass->name, get_username(con));
}
con->response->request.status.status_code = IPP_OK;
}
static int
add_file(cupsd_client_t *con,
cupsd_job_t *job,
mime_type_t *filetype,
int compression)
{
mime_type_t **filetypes;
int *compressions;
cupsdLogMessage(CUPSD_LOG_DEBUG2,
"add_file(con=%p[%d], job=%d, filetype=%s/%s, "
"compression=%d)", con, con ? con->http.fd : -1, job->id,
filetype->super, filetype->type, compression);
if (job->num_files == 0)
{
compressions = (int *)malloc(sizeof(int));
filetypes = (mime_type_t **)malloc(sizeof(mime_type_t *));
}
else
{
compressions = (int *)realloc(job->compressions,
(job->num_files + 1) * sizeof(int));
filetypes = (mime_type_t **)realloc(job->filetypes,
(job->num_files + 1) *
sizeof(mime_type_t *));
}
if (!compressions || !filetypes)
{
cupsdCancelJob(job, 1, IPP_JOB_ABORTED);
if (con)
send_ipp_status(con, IPP_INTERNAL_ERROR,
_("Unable to allocate memory for file types!"));
return (-1);
}
job->compressions = compressions;
job->compressions[job->num_files] = compression;
job->filetypes = filetypes;
job->filetypes[job->num_files] = filetype;
job->num_files ++;
return (0);
}
static cupsd_job_t *
add_job(cupsd_client_t *con,
cupsd_printer_t *printer,
mime_type_t *filetype)
{
http_status_t status;
ipp_attribute_t *attr,
*auth_info;
const char *val;
int priority;
char *title;
cupsd_job_t *job;
char job_uri[HTTP_MAX_URI];
int kbytes;
int i;
int lowerpagerange;
cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_job(%p[%d], %p(%s), %p(%s/%s))",
con, con->http.fd, printer, printer->name,
filetype, filetype->super, filetype->type);
if (!printer->shared &&
strcasecmp(con->http.hostname, "localhost") &&
strcasecmp(con->http.hostname, ServerName))
{
send_ipp_status(con, IPP_NOT_AUTHORIZED,
_("The printer or class is not shared!"));
return (NULL);
}
auth_info = ippFindAttribute(con->request, "auth-info", IPP_TAG_TEXT);
if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
{
send_http_error(con, status, printer);
return (NULL);
}
else if ((printer->type & CUPS_PRINTER_AUTHENTICATED) &&
!con->username[0] && !auth_info)
{
send_http_error(con, HTTP_UNAUTHORIZED, printer);
return (NULL);
}
#ifdef HAVE_SSL
else if (auth_info && !con->http.tls &&
!httpAddrLocalhost(con->http.hostaddr))
{
send_http_error(con, HTTP_UPGRADE_REQUIRED, printer);
return (NULL);
}
#endif
if (!printer->accepting)
{
send_ipp_status(con, IPP_NOT_ACCEPTING,
_("Destination \"%s\" is not accepting jobs."),
printer->name);
return (NULL);
}
if (filetype && printer->filetypes &&
!cupsArrayFind(printer->filetypes, filetype))
{
char mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2];
snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super,
filetype->type);
send_ipp_status(con, IPP_DOCUMENT_FORMAT,
_("Unsupported format \'%s\'!"), mimetype);
ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE,
"document-format", NULL, mimetype);
return (NULL);
}
if ((attr = ippFindAttribute(con->request, "copies",
IPP_TAG_INTEGER)) != NULL)
{
if (attr->values[0].integer < 1 || attr->values[0].integer > MaxCopies)
{
send_ipp_status(con, IPP_ATTRIBUTES, _("Bad copies value %d."),
attr->values[0].integer);
ippAddInteger(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_INTEGER,
"copies", attr->values[0].integer);
return (NULL);
}
}
if ((attr = ippFindAttribute(con->request, "number-up",
IPP_TAG_INTEGER)) != NULL)
{
if (attr->values[0].integer != 1 &&
attr->values[0].integer != 2 &&
attr->values[0].integer != 4 &&
attr->values[0].integer != 6 &&
attr->values[0].integer != 9 &&
attr->values[0].integer != 16)
{
send_ipp_status(con, IPP_ATTRIBUTES, _("Bad number-up value %d."),
attr->values[0].integer);
ippAddInteger(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_INTEGER,
"number-up", attr->values[0].integer);
return (NULL);
}
}
if ((attr = ippFindAttribute(con->request, "page-ranges",
IPP_TAG_RANGE)) != NULL)
{
for (i = 0, lowerpagerange = 1; i < attr->num_values; i ++)
{
if (attr->values[i].range.lower < lowerpagerange ||
attr->values[i].range.lower > attr->values[i].range.upper)
{
send_ipp_status(con, IPP_BAD_REQUEST,
_("Bad page-ranges values %d-%d."),
attr->values[i].range.lower,
attr->values[i].range.upper);
return (NULL);
}
lowerpagerange = attr->values[i].range.upper + 1;
}
}
if (MaxJobs && cupsArrayCount(Jobs) >= MaxJobs)
cupsdCleanJobs();
if (MaxJobs && cupsArrayCount(Jobs) >= MaxJobs)
{
send_ipp_status(con, IPP_NOT_POSSIBLE,
_("Too many active jobs."));
return (NULL);
}
if ((i = check_quotas(con, printer)) < 0)
{
send_ipp_status(con, IPP_NOT_POSSIBLE, _("Quota limit reached."));
return (NULL);
}
else if (i == 0)
{
send_ipp_status(con, IPP_NOT_AUTHORIZED, _("Not allowed to print."));
return (NULL);
}
if ((attr = ippFindAttribute(con->request, "job-priority",
IPP_TAG_INTEGER)) != NULL)
priority = attr->values[0].integer;
else
{
if ((val = cupsGetOption("job-priority", printer->num_options,
printer->options)) != NULL)
priority = atoi(val);
else
priority = 50;
ippAddInteger(con->request, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-priority",
priority);
}
if ((attr = ippFindAttribute(con->request, "job-name",
IPP_TAG_NAME)) != NULL)
title = attr->values[0].string.text;
else
ippAddString(con->request, IPP_TAG_JOB, IPP_TAG_NAME, "job-name", NULL,
title = "Untitled");
if ((job = cupsdAddJob(priority, printer->name)) == NULL)
{
send_ipp_status(con, IPP_INTERNAL_ERROR,
_("Unable to add job for destination \"%s\"!"),
printer->name);
return (NULL);
}
job->dtype = printer->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT |
CUPS_PRINTER_REMOTE);
job->attrs = con->request;
con->request = ippNewRequest(job->attrs->request.op.operation_id);
add_job_uuid(con, job);
apply_printer_defaults(printer, job);
attr = ippFindAttribute(job->attrs, "requesting-user-name", IPP_TAG_NAME);
if (con->username[0])
{
cupsdSetString(&job->username, con->username);
if (attr)
cupsdSetString(&attr->values[0].string.text, con->username);
}
else if (attr)
{
cupsdLogMessage(CUPSD_LOG_DEBUG,
"add_job: requesting-user-name=\"%s\"",
attr->values[0].string.text);
cupsdSetString(&job->username, attr->values[0].string.text);
}
else
cupsdSetString(&job->username, "anonymous");
if (!attr)
ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME,
"job-originating-user-name", NULL, job->username);
else
{
attr->group_tag = IPP_TAG_JOB;
_cupsStrFree(attr->name);
attr->name = _cupsStrAlloc("job-originating-user-name");
}
if (con->username[0] || auth_info)
{
save_auth_info(con, job, auth_info);
if (auth_info)
{
if (job->attrs->prev)
job->attrs->prev->next = auth_info->next;
else
job->attrs->attrs = auth_info->next;
if (job->attrs->last == auth_info)
job->attrs->last = job->attrs->prev;
_ippFreeAttr(auth_info);
}
}
if ((attr = ippFindAttribute(job->attrs, "job-originating-host-name",
IPP_TAG_ZERO)) != NULL)
{
if (attr->value_tag != IPP_TAG_NAME ||
attr->num_values != 1 ||
strcmp(con->http.hostname, "localhost"))
{
switch (attr->value_tag)
{
case IPP_TAG_STRING :
case IPP_TAG_TEXTLANG :
case IPP_TAG_NAMELANG :
case IPP_TAG_TEXT :
case IPP_TAG_NAME :
case IPP_TAG_KEYWORD :
case IPP_TAG_URI :
case IPP_TAG_URISCHEME :
case IPP_TAG_CHARSET :
case IPP_TAG_LANGUAGE :
case IPP_TAG_MIMETYPE :
for (i = 0; i < attr->num_values; i ++)
{
_cupsStrFree(attr->values[i].string.text);
attr->values[i].string.text = NULL;
if (attr->values[i].string.charset)
{
_cupsStrFree(attr->values[i].string.charset);
attr->values[i].string.charset = NULL;
}
}
default :
break;
}
attr->value_tag = IPP_TAG_NAME;
attr->num_values = 1;
attr->values[0].string.text = _cupsStrAlloc(con->http.hostname);
}
attr->group_tag = IPP_TAG_JOB;
}
else
{
ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME,
"job-originating-host-name", NULL, con->http.hostname);
}
ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, "time-at-creation",
time(NULL));
attr = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER,
"time-at-processing", 0);
attr->value_tag = IPP_TAG_NOVALUE;
attr = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER,
"time-at-completed", 0);
attr->value_tag = IPP_TAG_NOVALUE;
ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", job->id);
job->state = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_ENUM,
"job-state", IPP_JOB_STOPPED);
job->state_value = (ipp_jstate_t)job->state->values[0].integer;
job->sheets = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER,
"job-media-sheets-completed", 0);
ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_URI, "job-printer-uri", NULL,
printer->uri);
ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME, "job-name", NULL,
title);
if ((attr = ippFindAttribute(job->attrs, "job-k-octets",
IPP_TAG_INTEGER)) != NULL)
attr->values[0].integer = 0;
else
attr = ippAddInteger(job->attrs, IPP_TAG_JOB, IPP_TAG_INTEGER,
"job-k-octets", 0);
if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
IPP_TAG_KEYWORD)) == NULL)
attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
if (!attr)
{
if ((val = cupsGetOption("job-hold-until", printer->num_options,
printer->options)) == NULL)
val = "no-hold";
attr = ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_KEYWORD,
"job-hold-until", NULL, val);
}
if (attr && strcmp(attr->values[0].string.text, "no-hold") &&
!(printer->type & CUPS_PRINTER_REMOTE))
{
cupsdSetJobHoldUntil(job, attr->values[0].string.text);
job->state->values[0].integer = IPP_JOB_HELD;
job->state_value = IPP_JOB_HELD;
}
else if (job->attrs->request.op.operation_id == IPP_CREATE_JOB)
{
job->hold_until = time(NULL) + 60;
job->state->values[0].integer = IPP_JOB_HELD;
job->state_value = IPP_JOB_HELD;
}
else
{
job->state->values[0].integer = IPP_JOB_PENDING;
job->state_value = IPP_JOB_PENDING;
}
if (!(printer->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)) ||
Classification)
{
if ((attr = ippFindAttribute(job->attrs, "job-sheets",
IPP_TAG_ZERO)) == NULL)
{
cupsdLogMessage(CUPSD_LOG_DEBUG,
"Adding default job-sheets values \"%s,%s\"...",
printer->job_sheets[0], printer->job_sheets[1]);
attr = ippAddStrings(job->attrs, IPP_TAG_JOB, IPP_TAG_NAME, "job-sheets",
2, NULL, NULL);
attr->values[0].string.text = _cupsStrAlloc(printer->job_sheets[0]);
attr->values[1].string.text = _cupsStrAlloc(printer->job_sheets[1]);
}
job->job_sheets = attr;
if (Classification)
{
cupsdLogMessage(CUPSD_LOG_INFO,
"Classification=\"%s\", ClassifyOverride=%d",
Classification ? Classification : "(null)",
ClassifyOverride);
if (ClassifyOverride)
{
if (!strcmp(attr->values[0].string.text, "none") &&
(attr->num_values == 1 ||
!strcmp(attr->values[1].string.text, "none")))
{
cupsdSetString(&attr->values[0].string.text, Classification);
cupsdLogMessage(CUPSD_LOG_NOTICE, "[Job %d] CLASSIFICATION FORCED "
"job-sheets=\"%s,none\", "
"job-originating-user-name=\"%s\"",
job->id, Classification, job->username);
}
else if (attr->num_values == 2 &&
strcmp(attr->values[0].string.text,
attr->values[1].string.text) &&
strcmp(attr->values[0].string.text, "none") &&
strcmp(attr->values[1].string.text, "none"))
{
cupsdSetString(&attr->values[1].string.text, attr->values[0].string.text);
cupsdLogMessage(CUPSD_LOG_NOTICE, "[Job %d] CLASSIFICATION FORCED "
"job-sheets=\"%s,%s\", "
"job-originating-user-name=\"%s\"",
job->id, attr->values[0].string.text,
attr->values[1].string.text, job->username);
}
else if (strcmp(attr->values[0].string.text, Classification) &&
strcmp(attr->values[0].string.text, "none") &&
(attr->num_values == 1 ||
(strcmp(attr->values[1].string.text, Classification) &&
strcmp(attr->values[1].string.text, "none"))))
{
if (attr->num_values == 1)
cupsdLogMessage(CUPSD_LOG_NOTICE,
"[Job %d] CLASSIFICATION OVERRIDDEN "
"job-sheets=\"%s\", "
"job-originating-user-name=\"%s\"",
job->id, attr->values[0].string.text, job->username);
else
cupsdLogMessage(CUPSD_LOG_NOTICE,
"[Job %d] CLASSIFICATION OVERRIDDEN "
"job-sheets=\"%s,%s\",fffff "
"job-originating-user-name=\"%s\"",
job->id, attr->values[0].string.text,
attr->values[1].string.text, job->username);
}
}
else if (strcmp(attr->values[0].string.text, Classification) &&
(attr->num_values == 1 ||
strcmp(attr->values[1].string.text, Classification)))
{
if (attr->num_values > 1 &&
!strcmp(attr->values[0].string.text, attr->values[1].string.text))
{
cupsdSetString(&(attr->values[0].string.text), Classification);
cupsdSetString(&(attr->values[1].string.text), Classification);
}
else
{
if (attr->num_values == 1 ||
strcmp(attr->values[0].string.text, "none"))
cupsdSetString(&(attr->values[0].string.text), Classification);
if (attr->num_values > 1 &&
strcmp(attr->values[1].string.text, "none"))
cupsdSetString(&(attr->values[1].string.text), Classification);
}
if (attr->num_values > 1)
cupsdLogMessage(CUPSD_LOG_NOTICE,
"[Job %d] CLASSIFICATION FORCED "
"job-sheets=\"%s,%s\", "
"job-originating-user-name=\"%s\"",
job->id, attr->values[0].string.text,
attr->values[1].string.text, job->username);
else
cupsdLogMessage(CUPSD_LOG_NOTICE,
"[Job %d] CLASSIFICATION FORCED "
"job-sheets=\"%s\", "
"job-originating-user-name=\"%s\"",
job->id, Classification, job->username);
}
}
if (!(printer->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT)))
{
cupsdLogMessage(CUPSD_LOG_INFO,
"[Job %d] Adding start banner page \"%s\".",
job->id, attr->values[0].string.text);
kbytes = copy_banner(con, job, attr->values[0].string.text);
cupsdUpdateQuota(printer, job->username, 0, kbytes);
}
}
else if ((attr = ippFindAttribute(job->attrs, "job-sheets",
IPP_TAG_ZERO)) != NULL)
job->sheets = attr;
snprintf(job_uri, sizeof(job_uri), "http://%s:%d/jobs/%d", ServerName,
LocalPort, job->id);
ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI, "job-uri", NULL,
job_uri);
ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", job->id);
ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_ENUM, "job-state",
job->state_value);
add_job_state_reasons(con, job);
con->response->request.status.status_code = IPP_OK;
add_job_subscriptions(con, job);
for (attr = job->attrs->attrs->next->next; attr; attr = attr->next)
attr->group_tag = IPP_TAG_JOB;
cupsdAddEvent(CUPSD_EVENT_JOB_CREATED, printer, job, "Job created.");
return (job);
}
static void
add_job_state_reasons(
cupsd_client_t *con,
cupsd_job_t *job)
{
cupsd_printer_t *dest;
cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_job_state_reasons(%p[%d], %d)",
con, con->http.fd, job ? job->id : 0);
switch (job ? job->state_value : IPP_JOB_CANCELED)
{
case IPP_JOB_PENDING :
dest = cupsdFindDest(job->dest);
if (dest && dest->state == IPP_PRINTER_STOPPED)
ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD,
"job-state-reasons", NULL, "printer-stopped");
else
ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD,
"job-state-reasons", NULL, "none");
break;
case IPP_JOB_HELD :
if (ippFindAttribute(job->attrs, "job-hold-until",
IPP_TAG_KEYWORD) != NULL ||
ippFindAttribute(job->attrs, "job-hold-until",
IPP_TAG_NAME) != NULL)
ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD,
"job-state-reasons", NULL, "job-hold-until-specified");
else
ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD,
"job-state-reasons", NULL, "job-incoming");
break;
case IPP_JOB_PROCESSING :
ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD,
"job-state-reasons", NULL, "job-printing");
break;
case IPP_JOB_STOPPED :
ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD,
"job-state-reasons", NULL, "job-stopped");
break;
case IPP_JOB_CANCELED :
ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD,
"job-state-reasons", NULL, "job-canceled-by-user");
break;
case IPP_JOB_ABORTED :
ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD,
"job-state-reasons", NULL, "aborted-by-system");
break;
case IPP_JOB_COMPLETED :
ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_KEYWORD,
"job-state-reasons", NULL, "job-completed-successfully");
break;
}
}
static void
add_job_subscriptions(
cupsd_client_t *con,
cupsd_job_t *job)
{
int i;
ipp_attribute_t *prev,
*next,
*attr;
cupsd_subscription_t *sub;
const char *recipient,
*pullmethod;
ipp_attribute_t *user_data;
int interval;
unsigned mask;
for (attr = job->attrs->attrs, prev = NULL;
attr;
prev = attr, attr = attr->next)
if (attr->group_tag == IPP_TAG_SUBSCRIPTION)
break;
if (!attr)
return;
while (attr)
{
recipient = NULL;
pullmethod = NULL;
user_data = NULL;
interval = 0;
mask = CUPSD_EVENT_NONE;
while (attr && attr->group_tag != IPP_TAG_ZERO)
{
if (!strcmp(attr->name, "notify-recipient-uri") &&
attr->value_tag == IPP_TAG_URI)
recipient = attr->values[0].string.text;
else if (!strcmp(attr->name, "notify-pull-method") &&
attr->value_tag == IPP_TAG_KEYWORD)
pullmethod = attr->values[0].string.text;
else if (!strcmp(attr->name, "notify-charset") &&
attr->value_tag == IPP_TAG_CHARSET &&
strcmp(attr->values[0].string.text, "us-ascii") &&
strcmp(attr->values[0].string.text, "utf-8"))
{
send_ipp_status(con, IPP_CHARSET,
_("Character set \"%s\" not supported!"),
attr->values[0].string.text);
return;
}
else if (!strcmp(attr->name, "notify-natural-language") &&
(attr->value_tag != IPP_TAG_LANGUAGE ||
strcmp(attr->values[0].string.text, DefaultLanguage)))
{
send_ipp_status(con, IPP_CHARSET,
_("Language \"%s\" not supported!"),
attr->values[0].string.text);
return;
}
else if (!strcmp(attr->name, "notify-user-data") &&
attr->value_tag == IPP_TAG_STRING)
{
if (attr->num_values > 1 || attr->values[0].unknown.length > 63)
{
send_ipp_status(con, IPP_REQUEST_VALUE,
_("The notify-user-data value is too large "
"(%d > 63 octets)!"),
attr->values[0].unknown.length);
return;
}
user_data = attr;
}
else if (!strcmp(attr->name, "notify-events") &&
attr->value_tag == IPP_TAG_KEYWORD)
{
for (i = 0; i < attr->num_values; i ++)
mask |= cupsdEventValue(attr->values[i].string.text);
}
else if (!strcmp(attr->name, "notify-lease-duration"))
{
send_ipp_status(con, IPP_BAD_REQUEST,
_("The notify-lease-duration attribute cannot be "
"used with job subscriptions."));
return;
}
else if (!strcmp(attr->name, "notify-time-interval") &&
attr->value_tag == IPP_TAG_INTEGER)
interval = attr->values[0].integer;
attr = attr->next;
}
if (!recipient && !pullmethod)
break;
if (mask == CUPSD_EVENT_NONE)
mask = CUPSD_EVENT_JOB_COMPLETED;
sub = cupsdAddSubscription(mask, cupsdFindDest(job->dest), job, recipient,
0);
sub->interval = interval;
cupsdSetString(&sub->owner, job->username);
if (user_data)
{
sub->user_data_len = user_data->values[0].unknown.length;
memcpy(sub->user_data, user_data->values[0].unknown.data,
sub->user_data_len);
}
ippAddSeparator(con->response);
ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
"notify-subscription-id", sub->id);
if (attr)
attr = attr->next;
}
cupsdSaveAllSubscriptions();
for (attr = job->attrs->attrs, prev = NULL; attr; attr = next)
{
next = attr->next;
if (attr->group_tag == IPP_TAG_SUBSCRIPTION ||
attr->group_tag == IPP_TAG_ZERO)
{
_ippFreeAttr(attr);
if (prev)
prev->next = next;
else
job->attrs->attrs = next;
}
else
prev = attr;
}
job->attrs->last = prev;
job->attrs->current = prev;
}
static void
add_job_uuid(cupsd_client_t *con,
cupsd_job_t *job)
{
char uuid[1024];
_cups_md5_state_t md5state;
unsigned char md5sum[16];
if (ippFindAttribute(job->attrs, "job-uuid", IPP_TAG_URI))
return;
snprintf(uuid, sizeof(uuid), "%s:%s:%d:%d", ServerName, con->servername,
con->serverport, job->id);
_cupsMD5Init(&md5state);
_cupsMD5Append(&md5state, (unsigned char *)uuid, strlen(uuid));
_cupsMD5Finish(&md5state, md5sum);
snprintf(uuid, sizeof(uuid),
"urn:uuid:%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-"
"%02x%02x%02x%02x%02x%02x",
md5sum[0], md5sum[1], md5sum[2], md5sum[3], md5sum[4], md5sum[5],
(md5sum[6] & 15) | 0x30, md5sum[7], (md5sum[8] & 0x3f) | 0x40,
md5sum[9], md5sum[10], md5sum[11], md5sum[12], md5sum[13],
md5sum[14], md5sum[15]);
ippAddString(job->attrs, IPP_TAG_JOB, IPP_TAG_URI, "job-uuid", NULL, uuid);
}
static void
add_printer(cupsd_client_t *con,
ipp_attribute_t *uri)
{
http_status_t status;
int i;
char scheme[HTTP_MAX_URI],
username[HTTP_MAX_URI],
host[HTTP_MAX_URI],
resource[HTTP_MAX_URI];
int port;
cupsd_printer_t *printer;
ipp_attribute_t *attr;
cups_file_t *fp;
char line[1024];
char srcfile[1024],
dstfile[1024];
int modify;
char newname[IPP_MAX_NAME];
int need_restart_job;
int set_device_uri,
set_port_monitor;
cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_printer(%p[%d], %s)", con,
con->http.fd, uri->values[0].string.text);
httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
sizeof(scheme), username, sizeof(username), host,
sizeof(host), &port, resource, sizeof(resource));
if (strncmp(resource, "/printers/", 10) || strlen(resource) == 10)
{
send_ipp_status(con, IPP_BAD_REQUEST,
_("The printer-uri must be of the form "
"\"ipp://HOSTNAME/printers/PRINTERNAME\"."));
return;
}
if (!validate_name(resource + 10))
{
send_ipp_status(con, IPP_BAD_REQUEST,
_("The printer-uri \"%s\" contains invalid characters."),
uri->values[0].string.text);
return;
}
if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
{
send_http_error(con, status, NULL);
return;
}
if ((printer = cupsdFindPrinter(resource + 10)) == NULL)
{
if ((printer = cupsdFindClass(resource + 10)) != NULL &&
!(printer->type & CUPS_PRINTER_DISCOVERED))
{
send_ipp_status(con, IPP_NOT_POSSIBLE,
_("A class named \"%s\" already exists!"),
resource + 10);
return;
}
printer = cupsdAddPrinter(resource + 10);
modify = 0;
}
else if (printer->type & CUPS_PRINTER_IMPLICIT)
{
if (ImplicitAnyClasses)
{
snprintf(newname, sizeof(newname), "Any%s", resource + 10);
cupsdRenamePrinter(printer, newname);
}
else
cupsdDeletePrinter(printer, 1);
printer = cupsdAddPrinter(resource + 10);
modify = 0;
}
else if (printer->type & CUPS_PRINTER_DISCOVERED)
{
snprintf(newname, sizeof(newname), "%s@%s", resource + 10,
printer->hostname);
cupsdRenamePrinter(printer, newname);
printer = cupsdAddPrinter(resource + 10);
modify = 0;
}
else
modify = 1;
need_restart_job = 0;
if ((attr = ippFindAttribute(con->request, "printer-location",
IPP_TAG_TEXT)) != NULL)
cupsdSetString(&printer->location, attr->values[0].string.text);
if ((attr = ippFindAttribute(con->request, "printer-info",
IPP_TAG_TEXT)) != NULL)
cupsdSetString(&printer->info, attr->values[0].string.text);
set_device_uri = 0;
if ((attr = ippFindAttribute(con->request, "device-uri",
IPP_TAG_URI)) != NULL)
{
need_restart_job = 1;
httpSeparateURI(HTTP_URI_CODING_ALL, attr->values[0].string.text, scheme,
sizeof(scheme), username, sizeof(username), host,
sizeof(host), &port, resource, sizeof(resource));
if (!strcmp(scheme, "file"))
{
if (!FileDevice && strcmp(resource, "/dev/null"))
{
send_ipp_status(con, IPP_NOT_POSSIBLE,
_("File device URIs have been disabled! "
"To enable, see the FileDevice directive in "
"\"%s/cupsd.conf\"."),
ServerRoot);
return;
}
}
else
{
snprintf(srcfile, sizeof(srcfile), "%s/backend/%s", ServerBin, scheme);
if (access(srcfile, X_OK))
{
send_ipp_status(con, IPP_NOT_POSSIBLE, _("Bad device-uri \"%s\"!"),
attr->values[0].string.text);
return;
}
}
cupsdLogMessage(CUPSD_LOG_INFO,
"Setting %s device-uri to \"%s\" (was \"%s\".)",
printer->name,
cupsdSanitizeURI(attr->values[0].string.text, line,
sizeof(line)),
cupsdSanitizeURI(printer->device_uri, resource,
sizeof(resource)));
cupsdSetString(&printer->device_uri, attr->values[0].string.text);
set_device_uri = 1;
}
set_port_monitor = 0;
if ((attr = ippFindAttribute(con->request, "port-monitor",
IPP_TAG_NAME)) != NULL)
{
ipp_attribute_t *supported;
need_restart_job = 1;
supported = ippFindAttribute(printer->attrs, "port-monitor-supported",
IPP_TAG_KEYWORD);
for (i = 0; i < supported->num_values; i ++)
if (!strcmp(supported->values[i].string.text,
attr->values[0].string.text))
break;
if (i >= supported->num_values)
{
send_ipp_status(con, IPP_NOT_POSSIBLE, _("Bad port-monitor \"%s\"!"),
attr->values[0].string.text);
return;
}
cupsdLogMessage(CUPSD_LOG_INFO,
"Setting %s port-monitor to \"%s\" (was \"%s\".)",
printer->name, attr->values[0].string.text,
printer->port_monitor ? printer->port_monitor : "none");
if (strcmp(attr->values[0].string.text, "none"))
cupsdSetString(&printer->port_monitor, attr->values[0].string.text);
else
cupsdClearString(&printer->port_monitor);
set_port_monitor = 1;
}
if ((attr = ippFindAttribute(con->request, "printer-is-accepting-jobs",
IPP_TAG_BOOLEAN)) != NULL)
{
cupsdLogMessage(CUPSD_LOG_INFO,
"Setting %s printer-is-accepting-jobs to %d (was %d.)",
printer->name, attr->values[0].boolean, printer->accepting);
printer->accepting = attr->values[0].boolean;
cupsdAddPrinterHistory(printer);
}
if ((attr = ippFindAttribute(con->request, "printer-is-shared",
IPP_TAG_BOOLEAN)) != NULL)
{
if (printer->shared && !attr->values[0].boolean)
cupsdDeregisterPrinter(printer, 1);
cupsdLogMessage(CUPSD_LOG_INFO,
"Setting %s printer-is-shared to %d (was %d.)",
printer->name, attr->values[0].boolean, printer->shared);
printer->shared = attr->values[0].boolean;
}
if ((attr = ippFindAttribute(con->request, "printer-state",
IPP_TAG_ENUM)) != NULL)
{
if (attr->values[0].integer != IPP_PRINTER_IDLE &&
attr->values[0].integer != IPP_PRINTER_STOPPED)
{
send_ipp_status(con, IPP_BAD_REQUEST, _("Bad printer-state value %d!"),
attr->values[0].integer);
return;
}
cupsdLogMessage(CUPSD_LOG_INFO, "Setting %s printer-state to %d (was %d.)",
printer->name, attr->values[0].integer, printer->state);
if (attr->values[0].integer == IPP_PRINTER_STOPPED)
cupsdStopPrinter(printer, 0);
else
{
need_restart_job = 1;
cupsdSetPrinterState(printer, (ipp_pstate_t)(attr->values[0].integer), 0);
}
}
if ((attr = ippFindAttribute(con->request, "printer-state-message",
IPP_TAG_TEXT)) != NULL)
{
strlcpy(printer->state_message, attr->values[0].string.text,
sizeof(printer->state_message));
cupsdAddPrinterHistory(printer);
}
set_printer_defaults(con, printer);
if ((attr = ippFindAttribute(con->request, "auth-info-required",
IPP_TAG_KEYWORD)) != NULL)
cupsdSetAuthInfoRequired(printer, NULL, attr);
if (!printer->device_uri)
cupsdSetString(&printer->device_uri, "file:///dev/null");
if (con->filename)
{
need_restart_job = 1;
strlcpy(srcfile, con->filename, sizeof(srcfile));
if ((fp = cupsFileOpen(srcfile, "rb")))
{
line[0] = '\0';
cupsFileGets(fp, line, sizeof(line));
cupsFileClose(fp);
snprintf(dstfile, sizeof(dstfile), "%s/interfaces/%s", ServerRoot,
printer->name);
if (!strncmp(line, "*PPD-Adobe", 10))
{
unlink(dstfile);
}
else
{
if (copy_file(srcfile, dstfile))
{
send_ipp_status(con, IPP_INTERNAL_ERROR,
_("Unable to copy interface script - %s!"),
strerror(errno));
return;
}
else
{
cupsdLogMessage(CUPSD_LOG_DEBUG,
"Copied interface script successfully!");
chmod(dstfile, 0755);
}
}
snprintf(dstfile, sizeof(dstfile), "%s/ppd/%s.ppd", ServerRoot,
printer->name);
if (!strncmp(line, "*PPD-Adobe", 10))
{
if (copy_file(srcfile, dstfile))
{
send_ipp_status(con, IPP_INTERNAL_ERROR,
_("Unable to copy PPD file - %s!"),
strerror(errno));
return;
}
else
{
cupsdLogMessage(CUPSD_LOG_DEBUG,
"Copied PPD file successfully!");
chmod(dstfile, 0644);
}
}
else
{
unlink(dstfile);
}
}
}
else if ((attr = ippFindAttribute(con->request, "ppd-name",
IPP_TAG_NAME)) != NULL)
{
need_restart_job = 1;
if (!strcmp(attr->values[0].string.text, "raw"))
{
snprintf(dstfile, sizeof(dstfile), "%s/interfaces/%s", ServerRoot,
printer->name);
unlink(dstfile);
snprintf(dstfile, sizeof(dstfile), "%s/ppd/%s.ppd", ServerRoot,
printer->name);
unlink(dstfile);
}
else
{
snprintf(dstfile, sizeof(dstfile), "%s/interfaces/%s", ServerRoot,
printer->name);
unlink(dstfile);
snprintf(dstfile, sizeof(dstfile), "%s/ppd/%s.ppd", ServerRoot,
printer->name);
if (copy_model(con, attr->values[0].string.text, dstfile))
{
send_ipp_status(con, IPP_INTERNAL_ERROR, _("Unable to copy PPD file!"));
return;
}
else
{
cupsdLogMessage(CUPSD_LOG_DEBUG,
"Copied PPD file successfully!");
chmod(dstfile, 0644);
}
}
}
if (set_device_uri && !set_port_monitor)
{
ppd_file_t *ppd;
ppd_attr_t *ppdattr;
httpSeparateURI(HTTP_URI_CODING_ALL, printer->device_uri, scheme,
sizeof(scheme), username, sizeof(username), host,
sizeof(host), &port, resource, sizeof(resource));
snprintf(srcfile, sizeof(srcfile), "%s/ppd/%s.ppd", ServerRoot,
printer->name);
if ((ppd = ppdOpenFile(srcfile)) != NULL)
{
for (ppdattr = ppdFindAttr(ppd, "cupsPortMonitor", NULL);
ppdattr;
ppdattr = ppdFindNextAttr(ppd, "cupsPortMonitor", NULL))
if (!strcmp(scheme, ppdattr->spec))
{
cupsdLogMessage(CUPSD_LOG_INFO,
"Setting %s port-monitor to \"%s\" (was \"%s\".)",
printer->name, ppdattr->value,
printer->port_monitor ? printer->port_monitor
: "none");
if (strcmp(ppdattr->value, "none"))
cupsdSetString(&printer->port_monitor, ppdattr->value);
else
cupsdClearString(&printer->port_monitor);
break;
}
ppdClose(ppd);
}
}
cupsdSetPrinterAttrs(printer);
cupsdSaveAllPrinters();
if (need_restart_job && printer->job)
{
cupsd_job_t *job;
job = (cupsd_job_t *)printer->job;
cupsdStopJob(job, 1);
job->state->values[0].integer = IPP_JOB_PENDING;
job->state_value = IPP_JOB_PENDING;
}
if (need_restart_job)
cupsdCheckJobs();
cupsdWritePrintcap();
if (modify)
{
cupsdAddEvent(CUPSD_EVENT_PRINTER_MODIFIED, printer, NULL,
"Printer \"%s\" modified by \"%s\".", printer->name,
get_username(con));
cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" modified by \"%s\".",
printer->name, get_username(con));
}
else
{
cupsdAddPrinterHistory(printer);
cupsdAddEvent(CUPSD_EVENT_PRINTER_ADDED, printer, NULL,
"New printer \"%s\" added by \"%s\".", printer->name,
get_username(con));
cupsdLogMessage(CUPSD_LOG_INFO, "New printer \"%s\" added by \"%s\".",
printer->name, get_username(con));
}
con->response->request.status.status_code = IPP_OK;
}
static void
add_printer_state_reasons(
cupsd_client_t *con,
cupsd_printer_t *p)
{
cupsdLogMessage(CUPSD_LOG_DEBUG2,
"add_printer_state_reasons(%p[%d], %p[%s])",
con, con->http.fd, p, p->name);
if (p->num_reasons == 0)
ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
"printer-state-reasons", NULL,
p->state == IPP_PRINTER_STOPPED ? "paused" : "none");
else
ippAddStrings(con->response, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
"printer-state-reasons", p->num_reasons, NULL,
(const char * const *)p->reasons);
}
static void
add_queued_job_count(
cupsd_client_t *con,
cupsd_printer_t *p)
{
int count;
cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_queued_job_count(%p[%d], %p[%s])",
con, con->http.fd, p, p->name);
count = cupsdGetPrinterJobCount(p->name);
ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
"queued-job-count", count);
}
static void
apply_printer_defaults(
cupsd_printer_t *printer,
cupsd_job_t *job)
{
int i,
num_options;
cups_option_t *options,
*option;
for (i = printer->num_options, num_options = 0, option = printer->options;
i > 0;
i --, option ++)
if (!ippFindAttribute(job->attrs, option->name, IPP_TAG_ZERO))
{
num_options = cupsAddOption(option->name, option->value, num_options,
&options);
}
cupsEncodeOptions2(job->attrs, num_options, options, IPP_TAG_JOB);
cupsFreeOptions(num_options, options);
}
static void
authenticate_job(cupsd_client_t *con,
ipp_attribute_t *uri)
{
ipp_attribute_t *attr,
*auth_info;
int jobid;
cupsd_job_t *job;
char method[HTTP_MAX_URI],
username[HTTP_MAX_URI],
host[HTTP_MAX_URI],
resource[HTTP_MAX_URI];
int port;
cupsdLogMessage(CUPSD_LOG_DEBUG2, "authenticate_job(%p[%d], %s)",
con, con->http.fd, uri->values[0].string.text);
con->response->request.status.status_code = IPP_OK;
if (!strcmp(uri->name, "printer-uri"))
{
if ((attr = ippFindAttribute(con->request, "job-id",
IPP_TAG_INTEGER)) == NULL)
{
send_ipp_status(con, IPP_BAD_REQUEST,
_("Got a printer-uri attribute but no job-id!"));
return;
}
jobid = attr->values[0].integer;
}
else
{
httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method,
sizeof(method), username, sizeof(username), host,
sizeof(host), &port, resource, sizeof(resource));
if (strncmp(resource, "/jobs/", 6))
{
send_ipp_status(con, IPP_BAD_REQUEST, _("Bad job-uri attribute \"%s\"!"),
uri->values[0].string.text);
return;
}
jobid = atoi(resource + 6);
}
if ((job = cupsdFindJob(jobid)) == NULL)
{
send_ipp_status(con, IPP_NOT_FOUND,
_("Job #%d does not exist!"), jobid);
return;
}
if (job->state_value != IPP_JOB_HELD)
{
send_ipp_status(con, IPP_NOT_POSSIBLE,
_("Job #%d is not held for authentication!"),
jobid);
return;
}
auth_info = ippFindAttribute(con->request, "auth-info", IPP_TAG_TEXT);
if (!con->username[0] && !auth_info)
{
cupsd_printer_t *printer;
printer = cupsdFindDest(job->dest);
if (printer && printer->num_auth_info_required > 0 &&
!strcmp(printer->auth_info_required[0], "negotiate"))
send_http_error(con, HTTP_UNAUTHORIZED, printer);
else
send_ipp_status(con, IPP_NOT_AUTHORIZED,
_("No authentication information provided!"));
return;
}
if (!validate_user(job, con, job->username, username, sizeof(username)))
{
send_http_error(con, HTTP_UNAUTHORIZED, NULL);
return;
}
save_auth_info(con, job, auth_info);
if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
IPP_TAG_KEYWORD)) == NULL)
attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
if (attr)
{
attr->value_tag = IPP_TAG_KEYWORD;
cupsdSetString(&(attr->values[0].string.text), "no-hold");
}
cupsdReleaseJob(job);
cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Authenticated by \"%s\".", jobid,
con->username);
}
static void
cancel_all_jobs(cupsd_client_t *con,
ipp_attribute_t *uri)
{
http_status_t status;
cups_ptype_t dtype;
char scheme[HTTP_MAX_URI],
userpass[HTTP_MAX_URI],
hostname[HTTP_MAX_URI],
resource[HTTP_MAX_URI];
int port;
ipp_attribute_t *attr;
const char *username;
int purge;
cupsd_printer_t *printer;
cupsdLogMessage(CUPSD_LOG_DEBUG2, "cancel_all_jobs(%p[%d], %s)", con,
con->http.fd, uri->values[0].string.text);
if (strcmp(uri->name, "printer-uri"))
{
send_ipp_status(con, IPP_BAD_REQUEST,
_("The printer-uri attribute is required!"));
return;
}
if ((attr = ippFindAttribute(con->request, "my-jobs",
IPP_TAG_BOOLEAN)) != NULL &&
attr->values[0].boolean)
{
if ((attr = ippFindAttribute(con->request, "requesting-user-name",
IPP_TAG_NAME)) != NULL)
username = attr->values[0].string.text;
else
{
send_ipp_status(con, IPP_BAD_REQUEST,
_("Missing requesting-user-name attribute!"));
return;
}
}
else
username = NULL;
if ((attr = ippFindAttribute(con->request, "purge-jobs",
IPP_TAG_BOOLEAN)) != NULL)
purge = attr->values[0].boolean;
else
purge = 1;
if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
{
httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text,
scheme, sizeof(scheme), userpass, sizeof(userpass),
hostname, sizeof(hostname), &port,
resource, sizeof(resource));
if ((!strncmp(resource, "/printers/", 10) && resource[10]) ||
(!strncmp(resource, "/classes/", 9) && resource[9]))
{
send_ipp_status(con, IPP_NOT_FOUND,
_("The printer or class was not found."));
return;
}
if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
{
send_http_error(con, status, NULL);
return;
}
cupsdCancelJobs(NULL, username, purge);
cupsdLogMessage(CUPSD_LOG_INFO, "All jobs were %s by \"%s\".",
purge ? "purged" : "canceled", get_username(con));
}
else
{
if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con,
NULL)) != HTTP_OK)
{
send_http_error(con, status, printer);
return;
}
cupsdCancelJobs(printer->name, username, purge);
cupsdLogMessage(CUPSD_LOG_INFO, "All jobs on \"%s\" were %s by \"%s\".",
printer->name, purge ? "purged" : "canceled",
get_username(con));
}
con->response->request.status.status_code = IPP_OK;
}
static void
cancel_job(cupsd_client_t *con,
ipp_attribute_t *uri)
{
ipp_attribute_t *attr;
int jobid;
char scheme[HTTP_MAX_URI],
username[HTTP_MAX_URI],
host[HTTP_MAX_URI],
resource[HTTP_MAX_URI];
int port;
cupsd_job_t *job;
cups_ptype_t dtype;
cupsd_printer_t *printer;
cupsdLogMessage(CUPSD_LOG_DEBUG2, "cancel_job(%p[%d], %s)", con,
con->http.fd, uri->values[0].string.text);
if (!strcmp(uri->name, "printer-uri"))
{
if ((attr = ippFindAttribute(con->request, "job-id",
IPP_TAG_INTEGER)) == NULL)
{
send_ipp_status(con, IPP_BAD_REQUEST,
_("Got a printer-uri attribute but no job-id!"));
return;
}
if ((jobid = attr->values[0].integer) == 0)
{
if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
{
send_ipp_status(con, IPP_NOT_FOUND,
_("The printer or class was not found."));
return;
}
if (printer->job)
jobid = ((cupsd_job_t *)printer->job)->id;
else
{
for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs);
job;
job = (cupsd_job_t *)cupsArrayNext(ActiveJobs))
if (job->state_value <= IPP_JOB_PROCESSING &&
!strcasecmp(job->dest, printer->name))
break;
if (job)
jobid = job->id;
else
{
for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs);
job;
job = (cupsd_job_t *)cupsArrayNext(ActiveJobs))
if (job->state_value == IPP_JOB_STOPPED &&
!strcasecmp(job->dest, printer->name))
break;
if (job)
jobid = job->id;
else
{
send_ipp_status(con, IPP_NOT_POSSIBLE, _("No active jobs on %s!"),
printer->name);
return;
}
}
}
}
}
else
{
httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
sizeof(scheme), username, sizeof(username), host,
sizeof(host), &port, resource, sizeof(resource));
if (strncmp(resource, "/jobs/", 6))
{
send_ipp_status(con, IPP_BAD_REQUEST,
_("Bad job-uri attribute \"%s\"!"),
uri->values[0].string.text);
return;
}
jobid = atoi(resource + 6);
}
if ((job = cupsdFindJob(jobid)) == NULL)
{
send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid);
return;
}
if (!validate_user(job, con, job->username, username, sizeof(username)))
{
send_http_error(con, HTTP_UNAUTHORIZED, NULL);
return;
}
if (job->state_value >= IPP_JOB_CANCELED)
{
switch (job->state_value)
{
case IPP_JOB_CANCELED :
send_ipp_status(con, IPP_NOT_POSSIBLE,
_("Job #%d is already canceled - can\'t cancel."),
jobid);
break;
case IPP_JOB_ABORTED :
send_ipp_status(con, IPP_NOT_POSSIBLE,
_("Job #%d is already aborted - can\'t cancel."),
jobid);
break;
default :
send_ipp_status(con, IPP_NOT_POSSIBLE,
_("Job #%d is already completed - can\'t cancel."),
jobid);
break;
}
return;
}
cupsdCancelJob(job, 0, IPP_JOB_CANCELED);
cupsdCheckJobs();
cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Canceled by \"%s\".", jobid,
username);
con->response->request.status.status_code = IPP_OK;
}
static void
cancel_subscription(
cupsd_client_t *con,
int sub_id)
{
http_status_t status;
cupsd_subscription_t *sub;
cupsdLogMessage(CUPSD_LOG_DEBUG2,
"cancel_subscription(con=%p[%d], sub_id=%d)",
con, con->http.fd, sub_id);
if ((sub = cupsdFindSubscription(sub_id)) == NULL)
{
send_ipp_status(con, IPP_NOT_FOUND,
_("notify-subscription-id %d no good!"), sub_id);
return;
}
if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr :
DefaultPolicyPtr,
con, sub->owner)) != HTTP_OK)
{
send_http_error(con, status, sub->dest);
return;
}
cupsdDeleteSubscription(sub, 1);
con->response->request.status.status_code = IPP_OK;
}
static int
check_quotas(cupsd_client_t *con,
cupsd_printer_t *p)
{
int i;
char username[33];
cupsd_quota_t *q;
#ifdef HAVE_MBR_UID_TO_UUID
uuid_t usr_uuid;
uuid_t usr2_uuid;
uuid_t grp_uuid;
int mbr_err;
int is_member;
#else
struct passwd *pw;
#endif
cupsdLogMessage(CUPSD_LOG_DEBUG2, "check_quotas(%p[%d], %p[%s])",
con, con->http.fd, p, p->name);
if (!con || !p)
return (0);
strlcpy(username, get_username(con), sizeof(username));
if (MaxJobsPerPrinter)
{
if (cupsdGetPrinterJobCount(p->name) >= MaxJobsPerPrinter)
{
cupsdLogMessage(CUPSD_LOG_INFO, "Too many jobs for printer \"%s\"...",
p->name);
return (0);
}
}
if (MaxJobsPerUser)
{
if (cupsdGetUserJobCount(username) >= MaxJobsPerUser)
{
cupsdLogMessage(CUPSD_LOG_INFO, "Too many jobs for user \"%s\"...",
username);
return (0);
}
}
if (p->num_users == 0 && p->k_limit == 0 && p->page_limit == 0)
return (1);
if (p->num_users)
{
#ifdef HAVE_MBR_UID_TO_UUID
if (mbr_user_name_to_uuid((char *)username, usr_uuid))
{
cupsdLogMessage(CUPSD_LOG_DEBUG,
"check_quotas: UUID lookup failed for user \"%s\"",
username);
cupsdLogMessage(CUPSD_LOG_INFO,
"Denying user \"%s\" access to printer \"%s\" "
"(unknown user)...",
username, p->name);
return (0);
}
#else
pw = getpwnam(username);
endpwent();
#endif
for (i = 0; i < p->num_users; i ++)
if (p->users[i][0] == '@')
{
#ifdef HAVE_MBR_UID_TO_UUID
if ((mbr_err = mbr_group_name_to_uuid((char *)p->users[i] + 1,
grp_uuid)) != 0)
{
cupsdLogMessage(CUPSD_LOG_DEBUG,
"check_quotas: UUID lookup failed for ACL entry "
"\"%s\" (err=%d)", p->users[i], mbr_err);
cupsdLogMessage(CUPSD_LOG_WARN,
"Access control entry \"%s\" not a valid group name; "
"entry ignored", p->users[i]);
}
else
{
if ((mbr_err = mbr_check_membership(usr_uuid, grp_uuid,
&is_member)) != 0)
{
cupsdLogMessage(CUPSD_LOG_DEBUG,
"check_quotas: group \"%s\" membership check "
"failed (err=%d)", p->users[i] + 1, mbr_err);
is_member = 0;
}
if (is_member)
break;
}
#else
if (cupsdCheckGroup(username, pw, p->users[i] + 1))
break;
#endif
}
#ifdef HAVE_MBR_UID_TO_UUID
else
{
if ((mbr_err = mbr_user_name_to_uuid((char *)p->users[i],
usr2_uuid)) != 0)
{
cupsdLogMessage(CUPSD_LOG_DEBUG,
"check_quotas: UUID lookup failed for ACL entry "
"\"%s\" (err=%d)", p->users[i], mbr_err);
cupsdLogMessage(CUPSD_LOG_WARN,
"Access control entry \"%s\" not a valid user name; "
"entry ignored", p->users[i]);
}
else
{
if ((mbr_err = mbr_check_membership(usr_uuid, usr2_uuid,
&is_member)) != 0)
{
cupsdLogMessage(CUPSD_LOG_DEBUG,
"check_quotas: User \"%s\" identity check failed "
"(err=%d)", p->users[i], mbr_err);
is_member = 0;
}
if (is_member)
break;
}
}
#else
else if (!strcasecmp(username, p->users[i]))
break;
#endif
if ((i < p->num_users) == p->deny_users)
{
cupsdLogMessage(CUPSD_LOG_INFO,
"Denying user \"%s\" access to printer \"%s\"...",
username, p->name);
return (0);
}
}
#ifdef __APPLE__
if (AppleQuotas && (q = cupsdFindQuota(p, username)) != NULL)
{
if (q->page_count == -4)
{
cupsdLogMessage(CUPSD_LOG_INFO,
"User \"%s\" request approved for printer %s (%s): "
"unlimited quota.",
username, p->name, p->info);
q->page_count = 0;
return (1);
}
else if (q->page_count == -3)
{
cupsdLogMessage(CUPSD_LOG_INFO,
"User \"%s\" request denied for printer %s (%s): "
"quota limit exceeded.",
username, p->name, p->info);
q->page_count = 2;
return (-1);
}
else if (q->page_count == -2)
{
cupsdLogMessage(CUPSD_LOG_INFO,
"User \"%s\" request denied for printer %s (%s): "
"printing disabled for user.",
username, p->name, p->info);
q->page_count = 2;
return (-1);
}
else if (q->page_count == -1)
{
cupsdLogMessage(CUPSD_LOG_INFO,
"User \"%s\" request denied for printer %s (%s): "
"unable to determine quota limit.",
username, p->name, p->info);
q->page_count = 2;
return (-1);
}
else if (q->page_count < 0)
{
cupsdLogMessage(CUPSD_LOG_INFO,
"User \"%s\" request denied for printer %s (%s): "
"user disabled / missing quota.",
username, p->name, p->info);
q->page_count = 2;
return (-1);
}
else
{
q->page_count = 0;
return (1);
}
}
else
#endif
if (p->k_limit || p->page_limit)
{
if ((q = cupsdUpdateQuota(p, username, 0, 0)) == NULL)
{
cupsdLogMessage(CUPSD_LOG_ERROR,
"Unable to allocate quota data for user \"%s\"!",
username);
return (-1);
}
if ((q->k_count >= p->k_limit && p->k_limit) ||
(q->page_count >= p->page_limit && p->page_limit))
{
cupsdLogMessage(CUPSD_LOG_INFO, "User \"%s\" is over the quota limit...",
username);
return (-1);
}
}
return (1);
}
static ipp_attribute_t *
copy_attribute(
ipp_t *to,
ipp_attribute_t *attr,
int quickcopy)
{
int i;
ipp_attribute_t *toattr;
cupsdLogMessage(CUPSD_LOG_DEBUG2,
"copy_attribute(%p, %p[%s,%x,%x])", to, attr,
attr->name ? attr->name : "(null)", attr->group_tag,
attr->value_tag);
switch (attr->value_tag & ~IPP_TAG_COPY)
{
case IPP_TAG_ZERO :
toattr = ippAddSeparator(to);
break;
case IPP_TAG_INTEGER :
case IPP_TAG_ENUM :
toattr = ippAddIntegers(to, attr->group_tag, attr->value_tag,
attr->name, attr->num_values, NULL);
for (i = 0; i < attr->num_values; i ++)
toattr->values[i].integer = attr->values[i].integer;
break;
case IPP_TAG_BOOLEAN :
toattr = ippAddBooleans(to, attr->group_tag, attr->name,
attr->num_values, NULL);
for (i = 0; i < attr->num_values; i ++)
toattr->values[i].boolean = attr->values[i].boolean;
break;
case IPP_TAG_STRING :
case IPP_TAG_TEXT :
case IPP_TAG_NAME :
case IPP_TAG_KEYWORD :
case IPP_TAG_URI :
case IPP_TAG_URISCHEME :
case IPP_TAG_CHARSET :
case IPP_TAG_LANGUAGE :
case IPP_TAG_MIMETYPE :
toattr = ippAddStrings(to, attr->group_tag,
(ipp_tag_t)(attr->value_tag | quickcopy),
attr->name, attr->num_values, NULL, NULL);
if (quickcopy)
{
for (i = 0; i < attr->num_values; i ++)
toattr->values[i].string.text = attr->values[i].string.text;
}
else
{
for (i = 0; i < attr->num_values; i ++)
toattr->values[i].string.text =
_cupsStrAlloc(attr->values[i].string.text);
}
break;
case IPP_TAG_DATE :
toattr = ippAddDate(to, attr->group_tag, attr->name,
attr->values[0].date);
break;
case IPP_TAG_RESOLUTION :
toattr = ippAddResolutions(to, attr->group_tag, attr->name,
attr->num_values, IPP_RES_PER_INCH,
NULL, NULL);
for (i = 0; i < attr->num_values; i ++)
{
toattr->values[i].resolution.xres = attr->values[i].resolution.xres;
toattr->values[i].resolution.yres = attr->values[i].resolution.yres;
toattr->values[i].resolution.units = attr->values[i].resolution.units;
}
break;
case IPP_TAG_RANGE :
toattr = ippAddRanges(to, attr->group_tag, attr->name,
attr->num_values, NULL, NULL);
for (i = 0; i < attr->num_values; i ++)
{
toattr->values[i].range.lower = attr->values[i].range.lower;
toattr->values[i].range.upper = attr->values[i].range.upper;
}
break;
case IPP_TAG_TEXTLANG :
case IPP_TAG_NAMELANG :
toattr = ippAddStrings(to, attr->group_tag,
(ipp_tag_t)(attr->value_tag | quickcopy),
attr->name, attr->num_values, NULL, NULL);
if (quickcopy)
{
for (i = 0; i < attr->num_values; i ++)
{
toattr->values[i].string.charset = attr->values[i].string.charset;
toattr->values[i].string.text = attr->values[i].string.text;
}
}
else
{
for (i = 0; i < attr->num_values; i ++)
{
if (!i)
toattr->values[i].string.charset =
_cupsStrAlloc(attr->values[i].string.charset);
else
toattr->values[i].string.charset =
toattr->values[0].string.charset;
toattr->values[i].string.text =
_cupsStrAlloc(attr->values[i].string.text);
}
}
break;
case IPP_TAG_BEGIN_COLLECTION :
toattr = ippAddCollections(to, attr->group_tag, attr->name,
attr->num_values, NULL);
for (i = 0; i < attr->num_values; i ++)
{
toattr->values[i].collection = ippNew();
copy_attrs(toattr->values[i].collection, attr->values[i].collection,
NULL, IPP_TAG_ZERO, 0);
}
break;
default :
toattr = ippAddIntegers(to, attr->group_tag, attr->value_tag,
attr->name, attr->num_values, NULL);
for (i = 0; i < attr->num_values; i ++)
{
toattr->values[i].unknown.length = attr->values[i].unknown.length;
if (toattr->values[i].unknown.length > 0)
{
if ((toattr->values[i].unknown.data =
malloc(toattr->values[i].unknown.length)) == NULL)
toattr->values[i].unknown.length = 0;
else
memcpy(toattr->values[i].unknown.data,
attr->values[i].unknown.data,
toattr->values[i].unknown.length);
}
}
break;
}
return (toattr);
}
static void
copy_attrs(ipp_t *to,
ipp_t *from,
cups_array_t *ra,
ipp_tag_t group,
int quickcopy)
{
ipp_attribute_t *fromattr;
cupsdLogMessage(CUPSD_LOG_DEBUG2,
"copy_attrs(to=%p, from=%p, ra=%p, group=%x, quickcopy=%d)",
to, from, ra, group, quickcopy);
if (!to || !from)
return;
for (fromattr = from->attrs; fromattr; fromattr = fromattr->next)
{
if ((group != IPP_TAG_ZERO && fromattr->group_tag != group &&
fromattr->group_tag != IPP_TAG_ZERO) || !fromattr->name)
continue;
if (!ra || cupsArrayFind(ra, fromattr->name))
copy_attribute(to, fromattr, quickcopy);
}
}
static int
copy_banner(cupsd_client_t *con,
cupsd_job_t *job,
const char *name)
{
int i;
int kbytes;
char filename[1024];
cupsd_banner_t *banner;
cups_file_t *in;
cups_file_t *out;
int ch;
char attrname[255],
*s;
ipp_attribute_t *attr;
cupsdLogMessage(CUPSD_LOG_DEBUG2, "copy_banner(%p[%d], %p[%d], %s)",
con, con ? con->http.fd : -1, job, job->id,
name ? name : "(null)");
if (!name || !strcmp(name, "none") ||
(banner = cupsdFindBanner(name)) == NULL)
return (0);
if (add_file(con, job, banner->filetype, 0))
return (0);
snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, job->id,
job->num_files);
if ((out = cupsFileOpen(filename, "w")) == NULL)
{
cupsdLogMessage(CUPSD_LOG_ERROR,
"copy_banner: Unable to create banner job file %s - %s",
filename, strerror(errno));
job->num_files --;
return (0);
}
fchmod(cupsFileNumber(out), 0640);
fchown(cupsFileNumber(out), RunUser, Group);
strlcpy(attrname, job->attrs->attrs->next->values[0].string.text,
sizeof(attrname));
if (strlen(attrname) > 2 && attrname[2] == '-')
{
attrname[2] = '_';
attrname[3] = toupper(attrname[3] & 255);
attrname[4] = toupper(attrname[4] & 255);
}
snprintf(filename, sizeof(filename), "%s/banners/%s/%s", DataDir,
attrname, name);
if (access(filename, 0) && strlen(attrname) > 2)
{
attrname[2] = '\0';
snprintf(filename, sizeof(filename), "%s/banners/%s/%s", DataDir,
attrname, name);
}
if (access(filename, 0))
{
snprintf(filename, sizeof(filename), "%s/banners/%s", DataDir, name);
}
if ((in = cupsFileOpen(filename, "r")) == NULL)
{
cupsFileClose(out);
unlink(filename);
cupsdLogMessage(CUPSD_LOG_ERROR,
"copy_banner: Unable to open banner template file %s - %s",
filename, strerror(errno));
job->num_files --;
return (0);
}
while ((ch = cupsFileGetChar(in)) != EOF)
if (ch == '{')
{
for (s = attrname; (ch = cupsFileGetChar(in)) != EOF;)
if (!isalpha(ch & 255) && ch != '-' && ch != '?')
break;
else if (s < (attrname + sizeof(attrname) - 1))
*s++ = ch;
else
break;
*s = '\0';
if (ch != '}')
{
cupsFilePrintf(out, "{%s%c", attrname, ch);
continue;
}
if (attrname[0] == '?')
s = attrname + 1;
else
s = attrname;
if (!strcmp(s, "printer-name"))
{
cupsFilePuts(out, job->dest);
continue;
}
else if ((attr = ippFindAttribute(job->attrs, s, IPP_TAG_ZERO)) == NULL)
{
if (attrname[0] != '?')
{
cupsFilePrintf(out, "{%s}", attrname);
}
continue;
}
for (i = 0; i < attr->num_values; i ++)
{
if (i)
cupsFilePutChar(out, ',');
switch (attr->value_tag)
{
case IPP_TAG_INTEGER :
case IPP_TAG_ENUM :
if (!strncmp(s, "time-at-", 8))
cupsFilePuts(out, cupsdGetDateTime(attr->values[i].integer));
else
cupsFilePrintf(out, "%d", attr->values[i].integer);
break;
case IPP_TAG_BOOLEAN :
cupsFilePrintf(out, "%d", attr->values[i].boolean);
break;
case IPP_TAG_NOVALUE :
cupsFilePuts(out, "novalue");
break;
case IPP_TAG_RANGE :
cupsFilePrintf(out, "%d-%d", attr->values[i].range.lower,
attr->values[i].range.upper);
break;
case IPP_TAG_RESOLUTION :
cupsFilePrintf(out, "%dx%d%s", attr->values[i].resolution.xres,
attr->values[i].resolution.yres,
attr->values[i].resolution.units == IPP_RES_PER_INCH ?
"dpi" : "dpc");
break;
case IPP_TAG_URI :
case IPP_TAG_STRING :
case IPP_TAG_TEXT :
case IPP_TAG_NAME :
case IPP_TAG_KEYWORD :
case IPP_TAG_CHARSET :
case IPP_TAG_LANGUAGE :
if (!strcasecmp(banner->filetype->type, "postscript"))
{
const char *p;
for (p = attr->values[i].string.text; *p; p ++)
{
if (*p == '(' || *p == ')' || *p == '\\')
{
cupsFilePutChar(out, '\\');
cupsFilePutChar(out, *p);
}
else if (*p < 32 || *p > 126)
cupsFilePrintf(out, "\\%03o", *p & 255);
else
cupsFilePutChar(out, *p);
}
}
else
cupsFilePuts(out, attr->values[i].string.text);
break;
default :
break;
}
}
}
else if (ch == '\\')
{
ch = cupsFileGetChar(in);
if (ch != '{')
cupsFilePutChar(out, '\\');
cupsFilePutChar(out, ch);
}
else
cupsFilePutChar(out, ch);
cupsFileClose(in);
kbytes = (cupsFileTell(out) + 1023) / 1024;
if ((attr = ippFindAttribute(job->attrs, "job-k-octets",
IPP_TAG_INTEGER)) != NULL)
attr->values[0].integer += kbytes;
cupsFileClose(out);
return (kbytes);
}
static int
copy_file(const char *from,
const char *to)
{
cups_file_t *src,
*dst;
int bytes;
char buffer[2048];
cupsdLogMessage(CUPSD_LOG_DEBUG2, "copy_file(\"%s\", \"%s\")", from, to);
if ((src = cupsFileOpen(from, "rb")) == NULL)
return (-1);
if ((dst = cupsFileOpen(to, "wb")) == NULL)
{
cupsFileClose(src);
return (-1);
}
while ((bytes = cupsFileRead(src, buffer, sizeof(buffer))) > 0)
if (cupsFileWrite(dst, buffer, bytes) < bytes)
{
cupsFileClose(src);
cupsFileClose(dst);
return (-1);
}
cupsFileClose(src);
return (cupsFileClose(dst));
}
static int
copy_model(cupsd_client_t *con,
const char *from,
const char *to)
{
fd_set input;
struct timeval timeout;
int maxfd;
char tempfile[1024];
int tempfd;
int temppid;
int temppipe[2];
char *argv[4],
*envp[MAX_ENV];
cups_file_t *src,
*dst;
ppd_file_t *ppd;
int bytes,
total;
char buffer[2048];
int i;
char option[PPD_MAX_NAME],
choice[PPD_MAX_NAME];
int num_defaults;
cups_option_t *defaults;
char cups_protocol[PPD_MAX_LINE];
int have_letter,
have_a4;
#ifdef HAVE_LIBPAPER
char *paper_result;
char system_paper[64];
#endif
cupsdLogMessage(CUPSD_LOG_DEBUG2,
"copy_model(con=%p, from=\"%s\", to=\"%s\")",
con, from, to);
argv[0] = "cups-driverd";
argv[1] = "cat";
argv[2] = (char *)from;
argv[3] = NULL;
cupsdLoadEnv(envp, (int)(sizeof(envp) / sizeof(envp[0])));
snprintf(buffer, sizeof(buffer), "%s/daemon/cups-driverd", ServerBin);
snprintf(tempfile, sizeof(tempfile), "%s/%d.ppd", TempDir, con->http.fd);
tempfd = open(tempfile, O_WRONLY | O_CREAT | O_TRUNC, 0600);
if (tempfd < 0)
return (-1);
cupsdOpenPipe(temppipe);
cupsdLogMessage(CUPSD_LOG_DEBUG,
"copy_model: Running \"cups-driverd cat %s\"...", from);
if (!cupsdStartProcess(buffer, argv, envp, -1, temppipe[1], CGIPipes[1],
-1, -1, 0, &temppid))
{
close(tempfd);
unlink(tempfile);
return (-1);
}
close(temppipe[1]);
total = 0;
if (temppipe[0] > CGIPipes[0])
maxfd = temppipe[0] + 1;
else
maxfd = CGIPipes[0] + 1;
for (;;)
{
bytes = 0;
FD_ZERO(&input);
FD_SET(temppipe[0], &input);
FD_SET(CGIPipes[0], &input);
timeout.tv_sec = 30;
timeout.tv_usec = 0;
if ((i = select(maxfd, &input, NULL, NULL, &timeout)) < 0)
{
if (errno == EINTR)
continue;
else
break;
}
else if (i == 0)
{
break;
}
if (FD_ISSET(temppipe[0], &input))
{
if ((bytes = read(temppipe[0], buffer, sizeof(buffer))) > 0)
{
if (write(tempfd, buffer, bytes) < bytes)
break;
total += bytes;
}
else
break;
}
if (FD_ISSET(CGIPipes[0], &input))
cupsdUpdateCGI();
}
close(temppipe[0]);
close(tempfd);
if (!total)
{
cupsdLogMessage(CUPSD_LOG_ERROR, "copy_model: empty PPD file!");
unlink(tempfile);
return (-1);
}
if ((ppd = ppdOpenFile(tempfile)) == NULL)
{
unlink(tempfile);
return (-1);
}
have_letter = ppdPageSize(ppd, "Letter") != NULL;
have_a4 = ppdPageSize(ppd, "A4") != NULL;
num_defaults = 0;
defaults = NULL;
cups_protocol[0] = '\0';
if ((dst = cupsFileOpen(to, "rb")) != NULL)
{
while (cupsFileGets(dst, buffer, sizeof(buffer)))
if (!strncmp(buffer, "*Default", 8))
{
if (!ppd_parse_line(buffer, option, sizeof(option),
choice, sizeof(choice)))
{
ppd_option_t *ppdo;
if (!cupsGetOption(option, num_defaults, defaults) &&
(ppdo = ppdFindOption(ppd, option)) != NULL &&
ppdFindChoice(ppdo, choice))
num_defaults = cupsAddOption(option, choice, num_defaults,
&defaults);
}
}
else if (!strncmp(buffer, "*cupsProtocol:", 14))
strlcpy(cups_protocol, buffer, sizeof(cups_protocol));
cupsFileClose(dst);
}
#ifdef HAVE_LIBPAPER
else if ((paper_result = systempapername()) != NULL)
{
strlcpy(system_paper, paper_result, sizeof(system_paper));
system_paper[0] = toupper(system_paper[0] & 255);
if ((!strcmp(system_paper, "Letter") && have_letter) ||
(!strcmp(system_paper, "A4") && have_a4))
{
num_defaults = cupsAddOption("PageSize", system_paper,
num_defaults, &defaults);
num_defaults = cupsAddOption("PageRegion", system_paper,
num_defaults, &defaults);
num_defaults = cupsAddOption("PaperDimension", system_paper,
num_defaults, &defaults);
num_defaults = cupsAddOption("ImageableArea", system_paper,
num_defaults, &defaults);
}
}
#endif
else
{
if (!DefaultLanguage ||
!strcasecmp(DefaultLanguage, "C") ||
!strcasecmp(DefaultLanguage, "POSIX") ||
!strcasecmp(DefaultLanguage, "en") ||
!strncasecmp(DefaultLanguage, "en.", 3) ||
!strncasecmp(DefaultLanguage, "en_US", 5) ||
!strncasecmp(DefaultLanguage, "en_CA", 5) ||
!strncasecmp(DefaultLanguage, "fr_CA", 5))
{
if (have_letter)
{
num_defaults = cupsAddOption("PageSize", "Letter", num_defaults,
&defaults);
num_defaults = cupsAddOption("PageRegion", "Letter", num_defaults,
&defaults);
num_defaults = cupsAddOption("PaperDimension", "Letter", num_defaults,
&defaults);
num_defaults = cupsAddOption("ImageableArea", "Letter", num_defaults,
&defaults);
}
}
else if (have_a4)
{
num_defaults = cupsAddOption("PageSize", "A4", num_defaults,
&defaults);
num_defaults = cupsAddOption("PageRegion", "A4", num_defaults,
&defaults);
num_defaults = cupsAddOption("PaperDimension", "A4", num_defaults,
&defaults);
num_defaults = cupsAddOption("ImageableArea", "A4", num_defaults,
&defaults);
}
}
ppdClose(ppd);
if ((src = cupsFileOpen(tempfile, "rb")) == NULL)
{
cupsFreeOptions(num_defaults, defaults);
unlink(tempfile);
return (-1);
}
if ((dst = cupsFileOpen(to, "wb")) == NULL)
{
cupsFreeOptions(num_defaults, defaults);
cupsFileClose(src);
unlink(tempfile);
return (-1);
}
while (cupsFileGets(src, buffer, sizeof(buffer)))
{
if (!strncmp(buffer, "*Default", 8))
{
if (!ppd_parse_line(buffer, option, sizeof(option),
choice, sizeof(choice)))
{
const char *val;
if ((val = cupsGetOption(option, num_defaults, defaults)) != NULL)
{
snprintf(buffer, sizeof(buffer), "*Default%s: %s", option, val);
}
}
}
cupsFilePrintf(dst, "%s\n", buffer);
}
if (cups_protocol[0])
cupsFilePrintf(dst, "%s\n", cups_protocol);
cupsFreeOptions(num_defaults, defaults);
cupsFileClose(src);
unlink(tempfile);
return (cupsFileClose(dst));
}
static void
copy_job_attrs(cupsd_client_t *con,
cupsd_job_t *job,
cups_array_t *ra)
{
char job_uri[HTTP_MAX_URI];
httpAssembleURIf(HTTP_URI_CODING_ALL, job_uri, sizeof(job_uri), "ipp", NULL,
con->servername, con->serverport, "/jobs/%d",
job->id);
if (!ra || cupsArrayFind(ra, "job-more-info"))
ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI,
"job-more-info", NULL, job_uri);
if (job->state_value > IPP_JOB_PROCESSING &&
(!ra || cupsArrayFind(ra, "job-preserved")))
ippAddBoolean(con->response, IPP_TAG_JOB, "job-preserved",
job->num_files > 0);
if (!ra || cupsArrayFind(ra, "job-printer-up-time"))
ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER,
"job-printer-up-time", time(NULL));
if (!ra || cupsArrayFind(ra, "job-state-reasons"))
add_job_state_reasons(con, job);
if (!ra || cupsArrayFind(ra, "job-uri"))
ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI,
"job-uri", NULL, job_uri);
copy_attrs(con->response, job->attrs, ra, IPP_TAG_JOB, 0);
}
static void
copy_printer_attrs(
cupsd_client_t *con,
cupsd_printer_t *printer,
cups_array_t *ra)
{
char printer_uri[HTTP_MAX_URI];
time_t curtime;
int i;
ipp_attribute_t *history;
curtime = time(NULL);
#ifdef __APPLE__
if ((!ra || cupsArrayFind(ra, "com.apple.print.recoverable-message")) &&
printer->recoverable)
ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_TEXT,
"com.apple.print.recoverable-message", NULL,
printer->recoverable);
#endif
if (printer->alert && (!ra || cupsArrayFind(ra, "printer-alert")))
ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_STRING,
"printer-alert", NULL, printer->alert);
if (printer->alert_description &&
(!ra || cupsArrayFind(ra, "printer-alert-description")))
ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_TEXT,
"printer-alert-description", NULL,
printer->alert_description);
if (!ra || cupsArrayFind(ra, "printer-current-time"))
ippAddDate(con->response, IPP_TAG_PRINTER, "printer-current-time",
ippTimeToDate(curtime));
if (!ra || cupsArrayFind(ra, "printer-error-policy"))
ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME,
"printer-error-policy", NULL, printer->error_policy);
if (!ra || cupsArrayFind(ra, "printer-is-accepting-jobs"))
ippAddBoolean(con->response, IPP_TAG_PRINTER, "printer-is-accepting-jobs",
printer->accepting);
if (!ra || cupsArrayFind(ra, "printer-is-shared"))
ippAddBoolean(con->response, IPP_TAG_PRINTER, "printer-is-shared",
printer->shared);
if (!ra || cupsArrayFind(ra, "printer-op-policy"))
ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_NAME,
"printer-op-policy", NULL, printer->op_policy);
if (!ra || cupsArrayFind(ra, "printer-state"))
ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-state",
printer->state);
if (!ra || cupsArrayFind(ra, "printer-state-change-time"))
ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
"printer-state-change-time", printer->state_time);
if (MaxPrinterHistory > 0 && printer->num_history > 0 &&
cupsArrayFind(ra, "printer-state-history"))
{
history = ippAddCollections(con->response, IPP_TAG_PRINTER,
"printer-state-history",
printer->num_history, NULL);
for (i = 0; i < printer->num_history; i ++)
copy_attrs(history->values[i].collection = ippNew(), printer->history[i],
NULL, IPP_TAG_ZERO, 0);
}
if (!ra || cupsArrayFind(ra, "printer-state-message"))
ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_TEXT,
"printer-state-message", NULL, printer->state_message);
if (!ra || cupsArrayFind(ra, "printer-state-reasons"))
add_printer_state_reasons(con, printer);
if (!ra || cupsArrayFind(ra, "printer-type"))
{
int type;
type = printer->type;
if (printer == DefaultPrinter)
type |= CUPS_PRINTER_DEFAULT;
if (!printer->accepting)
type |= CUPS_PRINTER_REJECTING;
if (!printer->shared)
type |= CUPS_PRINTER_NOT_SHARED;
ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-type",
type);
}
if (!ra || cupsArrayFind(ra, "printer-up-time"))
ippAddInteger(con->response, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
"printer-up-time", curtime);
if ((!ra || cupsArrayFind(ra, "printer-uri-supported")) &&
!ippFindAttribute(printer->attrs, "printer-uri-supported",
IPP_TAG_URI))
{
httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri),
"ipp", NULL, con->servername, con->serverport,
(printer->type & CUPS_PRINTER_CLASS) ?
"/classes/%s" : "/printers/%s", printer->name);
ippAddString(con->response, IPP_TAG_PRINTER, IPP_TAG_URI,
"printer-uri-supported", NULL, printer_uri);
cupsdLogMessage(CUPSD_LOG_DEBUG2, "printer-uri-supported=\"%s\"",
printer_uri);
}
if (!ra || cupsArrayFind(ra, "queued-job-count"))
add_queued_job_count(con, printer);
copy_attrs(con->response, printer->attrs, ra, IPP_TAG_ZERO, 0);
copy_attrs(con->response, CommonData, ra, IPP_TAG_ZERO, IPP_TAG_COPY);
}
static void
copy_subscription_attrs(
cupsd_client_t *con,
cupsd_subscription_t *sub,
cups_array_t *ra)
{
ipp_attribute_t *attr;
char printer_uri[HTTP_MAX_URI];
int count;
unsigned mask;
const char *name;
if (!ra || cupsArrayFind(ra, "notify-events"))
{
if ((name = cupsdEventName((cupsd_eventmask_t)sub->mask)) != NULL)
{
ippAddString(con->response, IPP_TAG_SUBSCRIPTION,
(ipp_tag_t)(IPP_TAG_KEYWORD | IPP_TAG_COPY),
"notify-events", NULL, name);
}
else
{
for (mask = 1, count = 0; mask < CUPSD_EVENT_ALL; mask <<= 1)
if (sub->mask & mask)
count ++;
attr = ippAddStrings(con->response, IPP_TAG_SUBSCRIPTION,
(ipp_tag_t)(IPP_TAG_KEYWORD | IPP_TAG_COPY),
"notify-events", count, NULL, NULL);
for (mask = 1, count = 0; mask < CUPSD_EVENT_ALL; mask <<= 1)
if (sub->mask & mask)
{
attr->values[count].string.text =
(char *)cupsdEventName((cupsd_eventmask_t)mask);
count ++;
}
}
}
if (sub->job && (!ra || cupsArrayFind(ra, "notify-job-id")))
ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
"notify-job-id", sub->job->id);
if (!sub->job && (!ra || cupsArrayFind(ra, "notify-lease-duration")))
ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
"notify-lease-duration", sub->lease);
if (sub->dest && (!ra || cupsArrayFind(ra, "notify-printer-uri")))
{
httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri),
"ipp", NULL, con->servername, con->serverport,
"/printers/%s", sub->dest->name);
ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_URI,
"notify-printer-uri", NULL, printer_uri);
}
if (sub->recipient && (!ra || cupsArrayFind(ra, "notify-recipient-uri")))
ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_URI,
"notify-recipient-uri", NULL, sub->recipient);
else if (!ra || cupsArrayFind(ra, "notify-pull-method"))
ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_KEYWORD,
"notify-pull-method", NULL, "ippget");
if (!ra || cupsArrayFind(ra, "notify-subscriber-user-name"))
ippAddString(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_NAME,
"notify-subscriber-user-name", NULL, sub->owner);
if (!ra || cupsArrayFind(ra, "notify-subscription-id"))
ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
"notify-subscription-id", sub->id);
if (!ra || cupsArrayFind(ra, "notify-time-interval"))
ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
"notify-time-interval", sub->interval);
if (sub->user_data_len > 0 && (!ra || cupsArrayFind(ra, "notify-user-data")))
ippAddOctetString(con->response, IPP_TAG_SUBSCRIPTION, "notify-user-data",
sub->user_data, sub->user_data_len);
}
static void
create_job(cupsd_client_t *con,
ipp_attribute_t *uri)
{
cupsd_printer_t *printer;
cupsd_job_t *job;
cupsdLogMessage(CUPSD_LOG_DEBUG2, "create_job(%p[%d], %s)", con,
con->http.fd, uri->values[0].string.text);
if (!cupsdValidateDest(uri->values[0].string.text, NULL, &printer))
{
send_ipp_status(con, IPP_NOT_FOUND,
_("The printer or class was not found."));
return;
}
if ((job = add_job(con, printer, NULL)) == NULL)
return;
job->pending_timeout = 1;
cupsdSaveJob(job);
cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Queued on \"%s\" by \"%s\".",
job->id, job->dest, job->username);
}
static cups_array_t *
create_requested_array(ipp_t *request)
{
int i;
ipp_attribute_t *requested;
cups_array_t *ra;
char *value;
if ((requested = ippFindAttribute(request, "requested-attributes",
IPP_TAG_KEYWORD)) == NULL)
return (NULL);
if (requested->num_values == 1 &&
!strcmp(requested->values[0].string.text, "all"))
return (NULL);
ra = cupsArrayNew((cups_array_func_t)strcmp, NULL);
for (i = 0; i < requested->num_values; i ++)
{
value = requested->values[i].string.text;
if (!strcmp(value, "job-template"))
{
cupsArrayAdd(ra, "copies");
cupsArrayAdd(ra, "copies-default");
cupsArrayAdd(ra, "copies-supported");
cupsArrayAdd(ra, "finishings");
cupsArrayAdd(ra, "finishings-default");
cupsArrayAdd(ra, "finishings-supported");
cupsArrayAdd(ra, "job-hold-until");
cupsArrayAdd(ra, "job-hold-until-default");
cupsArrayAdd(ra, "job-hold-until-supported");
cupsArrayAdd(ra, "job-priority");
cupsArrayAdd(ra, "job-priority-default");
cupsArrayAdd(ra, "job-priority-supported");
cupsArrayAdd(ra, "job-sheets");
cupsArrayAdd(ra, "job-sheets-default");
cupsArrayAdd(ra, "job-sheets-supported");
cupsArrayAdd(ra, "media");
cupsArrayAdd(ra, "media-default");
cupsArrayAdd(ra, "media-supported");
cupsArrayAdd(ra, "multiple-document-handling");
cupsArrayAdd(ra, "multiple-document-handling-default");
cupsArrayAdd(ra, "multiple-document-handling-supported");
cupsArrayAdd(ra, "number-up");
cupsArrayAdd(ra, "number-up-default");
cupsArrayAdd(ra, "number-up-supported");
cupsArrayAdd(ra, "orientation-requested");
cupsArrayAdd(ra, "orientation-requested-default");
cupsArrayAdd(ra, "orientation-requested-supported");
cupsArrayAdd(ra, "page-ranges");
cupsArrayAdd(ra, "page-ranges-supported");
cupsArrayAdd(ra, "printer-resolution");
cupsArrayAdd(ra, "printer-resolution-default");
cupsArrayAdd(ra, "printer-resolution-supported");
cupsArrayAdd(ra, "print-quality");
cupsArrayAdd(ra, "print-quality-default");
cupsArrayAdd(ra, "print-quality-supported");
cupsArrayAdd(ra, "sides");
cupsArrayAdd(ra, "sides-default");
cupsArrayAdd(ra, "sides-supported");
}
else if (!strcmp(value, "job-description"))
{
cupsArrayAdd(ra, "date-time-at-completed");
cupsArrayAdd(ra, "date-time-at-creation");
cupsArrayAdd(ra, "date-time-at-processing");
cupsArrayAdd(ra, "job-detailed-status-message");
cupsArrayAdd(ra, "job-document-access-errors");
cupsArrayAdd(ra, "job-id");
cupsArrayAdd(ra, "job-impressions");
cupsArrayAdd(ra, "job-impressions-completed");
cupsArrayAdd(ra, "job-k-octets");
cupsArrayAdd(ra, "job-k-octets-processed");
cupsArrayAdd(ra, "job-media-sheets");
cupsArrayAdd(ra, "job-media-sheets-completed");
cupsArrayAdd(ra, "job-message-from-operator");
cupsArrayAdd(ra, "job-more-info");
cupsArrayAdd(ra, "job-name");
cupsArrayAdd(ra, "job-originating-user-name");
cupsArrayAdd(ra, "job-printer-up-time");
cupsArrayAdd(ra, "job-printer-uri");
cupsArrayAdd(ra, "job-state");
cupsArrayAdd(ra, "job-state-message");
cupsArrayAdd(ra, "job-state-reasons");
cupsArrayAdd(ra, "job-uri");
cupsArrayAdd(ra, "number-of-documents");
cupsArrayAdd(ra, "number-of-intervening-jobs");
cupsArrayAdd(ra, "output-device-assigned");
cupsArrayAdd(ra, "time-at-completed");
cupsArrayAdd(ra, "time-at-creation");
cupsArrayAdd(ra, "time-at-processing");
}
else if (!strcmp(value, "printer-description"))
{
cupsArrayAdd(ra, "charset-configured");
cupsArrayAdd(ra, "charset-supported");
cupsArrayAdd(ra, "color-supported");
cupsArrayAdd(ra, "compression-supported");
cupsArrayAdd(ra, "document-format-default");
cupsArrayAdd(ra, "document-format-supported");
cupsArrayAdd(ra, "generated-natural-language-supported");
cupsArrayAdd(ra, "ipp-versions-supported");
cupsArrayAdd(ra, "job-impressions-supported");
cupsArrayAdd(ra, "job-k-octets-supported");
cupsArrayAdd(ra, "job-media-sheets-supported");
cupsArrayAdd(ra, "multiple-document-jobs-supported");
cupsArrayAdd(ra, "multiple-operation-time-out");
cupsArrayAdd(ra, "natural-language-configured");
cupsArrayAdd(ra, "notify-attributes-supported");
cupsArrayAdd(ra, "notify-lease-duration-default");
cupsArrayAdd(ra, "notify-lease-duration-supported");
cupsArrayAdd(ra, "notify-max-events-supported");
cupsArrayAdd(ra, "notify-events-default");
cupsArrayAdd(ra, "notify-events-supported");
cupsArrayAdd(ra, "notify-pull-method-supported");
cupsArrayAdd(ra, "notify-schemes-supported");
cupsArrayAdd(ra, "operations-supported");
cupsArrayAdd(ra, "pages-per-minute");
cupsArrayAdd(ra, "pages-per-minute-color");
cupsArrayAdd(ra, "pdl-override-supported");
cupsArrayAdd(ra, "printer-alert");
cupsArrayAdd(ra, "printer-alert-description");
cupsArrayAdd(ra, "printer-current-time");
cupsArrayAdd(ra, "printer-driver-installer");
cupsArrayAdd(ra, "printer-info");
cupsArrayAdd(ra, "printer-is-accepting-jobs");
cupsArrayAdd(ra, "printer-location");
cupsArrayAdd(ra, "printer-make-and-model");
cupsArrayAdd(ra, "printer-message-from-operator");
cupsArrayAdd(ra, "printer-more-info");
cupsArrayAdd(ra, "printer-more-info-manufacturer");
cupsArrayAdd(ra, "printer-name");
cupsArrayAdd(ra, "printer-state");
cupsArrayAdd(ra, "printer-state-message");
cupsArrayAdd(ra, "printer-state-reasons");
cupsArrayAdd(ra, "printer-up-time");
cupsArrayAdd(ra, "printer-uri-supported");
cupsArrayAdd(ra, "queued-job-count");
cupsArrayAdd(ra, "reference-uri-schemes-supported");
cupsArrayAdd(ra, "uri-authentication-supported");
cupsArrayAdd(ra, "uri-security-supported");
}
else if (!strcmp(value, "printer-defaults"))
{
char *name;
for (name = (char *)cupsArrayFirst(CommonDefaults);
name;
name = (char *)cupsArrayNext(CommonDefaults))
cupsArrayAdd(ra, name);
}
else if (!strcmp(value, "subscription-template"))
{
cupsArrayAdd(ra, "notify-attributes");
cupsArrayAdd(ra, "notify-charset");
cupsArrayAdd(ra, "notify-events");
cupsArrayAdd(ra, "notify-lease-duration");
cupsArrayAdd(ra, "notify-natural-language");
cupsArrayAdd(ra, "notify-pull-method");
cupsArrayAdd(ra, "notify-recipient-uri");
cupsArrayAdd(ra, "notify-time-interval");
cupsArrayAdd(ra, "notify-user-data");
}
else
cupsArrayAdd(ra, value);
}
return (ra);
}
static void
create_subscription(
cupsd_client_t *con,
ipp_attribute_t *uri)
{
http_status_t status;
int i;
ipp_attribute_t *attr;
cups_ptype_t dtype;
char scheme[HTTP_MAX_URI],
userpass[HTTP_MAX_URI],
host[HTTP_MAX_URI],
resource[HTTP_MAX_URI];
int port;
cupsd_printer_t *printer;
cupsd_job_t *job;
int jobid;
cupsd_subscription_t *sub;
const char *username,
*recipient,
*pullmethod;
ipp_attribute_t *user_data;
int interval,
lease;
unsigned mask;
ipp_attribute_t *notify_events,
*notify_lease;
#ifdef DEBUG
for (attr = con->request->attrs; attr; attr = attr->next)
{
if (attr->group_tag != IPP_TAG_ZERO)
cupsdLogMessage(CUPSD_LOG_DEBUG, "g%04x v%04x %s", attr->group_tag,
attr->value_tag, attr->name);
else
cupsdLogMessage(CUPSD_LOG_DEBUG, "----SEP----");
}
#endif
cupsdLogMessage(CUPSD_LOG_DEBUG,
"cupsdCreateSubscription(con=%p(%d), uri=\"%s\")",
con, con->http.fd, uri->values[0].string.text);
httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
sizeof(scheme), userpass, sizeof(userpass), host,
sizeof(host), &port, resource, sizeof(resource));
if (!strcmp(resource, "/"))
{
dtype = (cups_ptype_t)0;
printer = NULL;
}
else if (!strncmp(resource, "/printers", 9) && strlen(resource) <= 10)
{
dtype = (cups_ptype_t)0;
printer = NULL;
}
else if (!strncmp(resource, "/classes", 8) && strlen(resource) <= 9)
{
dtype = CUPS_PRINTER_CLASS;
printer = NULL;
}
else if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
{
send_ipp_status(con, IPP_NOT_FOUND,
_("The printer or class was not found."));
return;
}
if (printer)
{
if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con,
NULL)) != HTTP_OK)
{
send_http_error(con, status, printer);
return;
}
}
else if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
{
send_http_error(con, status, NULL);
return;
}
username = get_username(con);
for (attr = con->request->attrs; attr; attr = attr->next)
if (attr->group_tag == IPP_TAG_SUBSCRIPTION)
break;
if (!attr)
{
send_ipp_status(con, IPP_BAD_REQUEST,
_("No subscription attributes in request!"));
return;
}
con->response->request.status.status_code = IPP_BAD_REQUEST;
while (attr)
{
recipient = NULL;
pullmethod = NULL;
user_data = NULL;
interval = 0;
lease = DefaultLeaseDuration;
jobid = 0;
mask = CUPSD_EVENT_NONE;
if (printer)
{
notify_events = ippFindAttribute(printer->attrs, "notify-events-default",
IPP_TAG_KEYWORD);
notify_lease = ippFindAttribute(printer->attrs,
"notify-lease-duration-default",
IPP_TAG_INTEGER);
if (notify_lease)
lease = notify_lease->values[0].integer;
}
else
{
notify_events = NULL;
notify_lease = NULL;
}
while (attr && attr->group_tag != IPP_TAG_ZERO)
{
if (!strcmp(attr->name, "notify-recipient-uri") &&
attr->value_tag == IPP_TAG_URI)
{
char notifier[1024];
recipient = attr->values[0].string.text;
if (httpSeparateURI(HTTP_URI_CODING_ALL, recipient,
scheme, sizeof(scheme), userpass, sizeof(userpass),
host, sizeof(host), &port,
resource, sizeof(resource)) < HTTP_URI_OK)
{
send_ipp_status(con, IPP_NOT_POSSIBLE,
_("Bad notify-recipient-uri URI \"%s\"!"), recipient);
ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
"notify-status-code", IPP_URI_SCHEME);
return;
}
snprintf(notifier, sizeof(notifier), "%s/notifier/%s", ServerBin,
scheme);
if (access(notifier, X_OK))
{
send_ipp_status(con, IPP_NOT_POSSIBLE,
_("notify-recipient-uri URI \"%s\" uses unknown "
"scheme!"), recipient);
ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
"notify-status-code", IPP_URI_SCHEME);
return;
}
}
else if (!strcmp(attr->name, "notify-pull-method") &&
attr->value_tag == IPP_TAG_KEYWORD)
{
pullmethod = attr->values[0].string.text;
if (strcmp(pullmethod, "ippget"))
{
send_ipp_status(con, IPP_NOT_POSSIBLE,
_("Bad notify-pull-method \"%s\"!"), pullmethod);
ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_ENUM,
"notify-status-code", IPP_ATTRIBUTES);
return;
}
}
else if (!strcmp(attr->name, "notify-charset") &&
attr->value_tag == IPP_TAG_CHARSET &&
strcmp(attr->values[0].string.text, "us-ascii") &&
strcmp(attr->values[0].string.text, "utf-8"))
{
send_ipp_status(con, IPP_CHARSET,
_("Character set \"%s\" not supported!"),
attr->values[0].string.text);
return;
}
else if (!strcmp(attr->name, "notify-natural-language") &&
(attr->value_tag != IPP_TAG_LANGUAGE ||
strcmp(attr->values[0].string.text, DefaultLanguage)))
{
send_ipp_status(con, IPP_CHARSET,
_("Language \"%s\" not supported!"),
attr->values[0].string.text);
return;
}
else if (!strcmp(attr->name, "notify-user-data") &&
attr->value_tag == IPP_TAG_STRING)
{
if (attr->num_values > 1 || attr->values[0].unknown.length > 63)
{
send_ipp_status(con, IPP_REQUEST_VALUE,
_("The notify-user-data value is too large "
"(%d > 63 octets)!"),
attr->values[0].unknown.length);
return;
}
user_data = attr;
}
else if (!strcmp(attr->name, "notify-events") &&
attr->value_tag == IPP_TAG_KEYWORD)
notify_events = attr;
else if (!strcmp(attr->name, "notify-lease-duration") &&
attr->value_tag == IPP_TAG_INTEGER)
lease = attr->values[0].integer;
else if (!strcmp(attr->name, "notify-time-interval") &&
attr->value_tag == IPP_TAG_INTEGER)
interval = attr->values[0].integer;
else if (!strcmp(attr->name, "notify-job-id") &&
attr->value_tag == IPP_TAG_INTEGER)
jobid = attr->values[0].integer;
attr = attr->next;
}
if (notify_events)
{
for (i = 0; i < notify_events->num_values; i ++)
mask |= cupsdEventValue(notify_events->values[i].string.text);
}
if (recipient)
cupsdLogMessage(CUPSD_LOG_DEBUG, "recipient=\"%s\"", recipient);
if (pullmethod)
cupsdLogMessage(CUPSD_LOG_DEBUG, "pullmethod=\"%s\"", pullmethod);
cupsdLogMessage(CUPSD_LOG_DEBUG, "notify-lease-duration=%d", lease);
cupsdLogMessage(CUPSD_LOG_DEBUG, "notify-time-interval=%d", interval);
if (!recipient && !pullmethod)
break;
if (mask == CUPSD_EVENT_NONE)
{
if (jobid)
mask = CUPSD_EVENT_JOB_COMPLETED;
else if (printer)
mask = CUPSD_EVENT_PRINTER_STATE_CHANGED;
else
{
send_ipp_status(con, IPP_BAD_REQUEST,
_("notify-events not specified!"));
return;
}
}
if (MaxLeaseDuration && (lease == 0 || lease > MaxLeaseDuration))
{
cupsdLogMessage(CUPSD_LOG_INFO,
"create_subscription: Limiting notify-lease-duration to "
"%d seconds.",
MaxLeaseDuration);
lease = MaxLeaseDuration;
}
if (jobid)
{
if ((job = cupsdFindJob(jobid)) == NULL)
{
send_ipp_status(con, IPP_NOT_FOUND, _("Job %d not found!"), jobid);
return;
}
}
else
job = NULL;
sub = cupsdAddSubscription(mask, printer, job, recipient, 0);
if (job)
cupsdLogMessage(CUPSD_LOG_DEBUG, "Added subscription %d for job %d",
sub->id, job->id);
else if (printer)
cupsdLogMessage(CUPSD_LOG_DEBUG,
"Added subscription %d for printer \"%s\"",
sub->id, printer->name);
else
cupsdLogMessage(CUPSD_LOG_DEBUG, "Added subscription %d for server",
sub->id);
sub->interval = interval;
sub->lease = lease;
sub->expire = lease ? time(NULL) + lease : 0;
cupsdSetString(&sub->owner, username);
if (user_data)
{
sub->user_data_len = user_data->values[0].unknown.length;
memcpy(sub->user_data, user_data->values[0].unknown.data,
sub->user_data_len);
}
ippAddSeparator(con->response);
ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
"notify-subscription-id", sub->id);
con->response->request.status.status_code = IPP_OK;
if (attr)
attr = attr->next;
}
cupsdSaveAllSubscriptions();
}
static void
delete_printer(cupsd_client_t *con,
ipp_attribute_t *uri)
{
http_status_t status;
cups_ptype_t dtype;
cupsd_printer_t *printer;
char filename[1024];
cupsdLogMessage(CUPSD_LOG_DEBUG2, "delete_printer(%p[%d], %s)", con,
con->http.fd, uri->values[0].string.text);
if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
{
send_ipp_status(con, IPP_NOT_FOUND,
_("The printer or class was not found."));
return;
}
if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
{
send_http_error(con, status, NULL);
return;
}
cupsdCancelJobs(printer->name, NULL, 1);
cupsdAddEvent(CUPSD_EVENT_PRINTER_DELETED, printer, NULL,
"%s \"%s\" deleted by \"%s\".",
(dtype & CUPS_PRINTER_CLASS) ? "Class" : "Printer",
printer->name, get_username(con));
cupsdExpireSubscriptions(printer, NULL);
snprintf(filename, sizeof(filename), "%s/interfaces/%s", ServerRoot,
printer->name);
unlink(filename);
snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot,
printer->name);
unlink(filename);
if (dtype & CUPS_PRINTER_CLASS)
{
cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" deleted by \"%s\".",
printer->name, get_username(con));
cupsdDeletePrinter(printer, 0);
cupsdSaveAllClasses();
}
else
{
cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" deleted by \"%s\".",
printer->name, get_username(con));
cupsdDeletePrinter(printer, 0);
cupsdSaveAllPrinters();
}
cupsdWritePrintcap();
con->response->request.status.status_code = IPP_OK;
}
static void
get_default(cupsd_client_t *con)
{
http_status_t status;
cups_array_t *ra;
cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_default(%p[%d])", con, con->http.fd);
if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
{
send_http_error(con, status, NULL);
return;
}
if (DefaultPrinter)
{
ra = create_requested_array(con->request);
copy_printer_attrs(con, DefaultPrinter, ra);
cupsArrayDelete(ra);
con->response->request.status.status_code = IPP_OK;
}
else
send_ipp_status(con, IPP_NOT_FOUND, _("No default printer"));
}
static void
get_devices(cupsd_client_t *con)
{
http_status_t status;
ipp_attribute_t *limit,
*requested;
char command[1024],
options[1024],
requested_str[256];
cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_devices(%p[%d])", con, con->http.fd);
if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
{
send_http_error(con, status, NULL);
return;
}
limit = ippFindAttribute(con->request, "limit", IPP_TAG_INTEGER);
requested = ippFindAttribute(con->request, "requested-attributes",
IPP_TAG_KEYWORD);
if (requested)
url_encode_attr(requested, requested_str, sizeof(requested_str));
else
strlcpy(requested_str, "requested-attributes=all", sizeof(requested_str));
snprintf(command, sizeof(command), "%s/daemon/cups-deviced", ServerBin);
snprintf(options, sizeof(options),
"%d+%d+%d+%s",
con->request->request.op.request_id,
limit ? limit->values[0].integer : 0, (int)User,
requested_str);
if (cupsdSendCommand(con, command, options, 1))
{
ippDelete(con->response);
con->response = NULL;
}
else
{
send_ipp_status(con, IPP_INTERNAL_ERROR,
_("cups-deviced failed to execute."));
}
}
static void
get_job_attrs(cupsd_client_t *con,
ipp_attribute_t *uri)
{
http_status_t status;
ipp_attribute_t *attr;
int jobid;
cupsd_job_t *job;
char method[HTTP_MAX_URI],
username[HTTP_MAX_URI],
host[HTTP_MAX_URI],
resource[HTTP_MAX_URI];
int port;
cups_array_t *ra;
cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_job_attrs(%p[%d], %s)", con,
con->http.fd, uri->values[0].string.text);
if (!strcmp(uri->name, "printer-uri"))
{
if ((attr = ippFindAttribute(con->request, "job-id",
IPP_TAG_INTEGER)) == NULL)
{
send_ipp_status(con, IPP_BAD_REQUEST,
_("Got a printer-uri attribute but no job-id!"));
return;
}
jobid = attr->values[0].integer;
}
else
{
httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method,
sizeof(method), username, sizeof(username), host,
sizeof(host), &port, resource, sizeof(resource));
if (strncmp(resource, "/jobs/", 6))
{
send_ipp_status(con, IPP_BAD_REQUEST,
_("Bad job-uri attribute \"%s\"!"),
uri->values[0].string.text);
return;
}
jobid = atoi(resource + 6);
}
if ((job = cupsdFindJob(jobid)) == NULL)
{
send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid);
return;
}
if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
{
send_http_error(con, status, NULL);
return;
}
cupsdLoadJob(job);
ra = create_requested_array(con->request);
copy_job_attrs(con, job, ra);
cupsArrayDelete(ra);
con->response->request.status.status_code = IPP_OK;
}
static void
get_jobs(cupsd_client_t *con,
ipp_attribute_t *uri)
{
http_status_t status;
ipp_attribute_t *attr;
const char *dest;
cups_ptype_t dtype;
cups_ptype_t dmask;
char scheme[HTTP_MAX_URI],
username[HTTP_MAX_URI],
host[HTTP_MAX_URI],
resource[HTTP_MAX_URI];
int port;
int completed;
int first_job_id;
int limit;
int count;
cupsd_job_t *job;
cupsd_printer_t *printer;
cups_array_t *list;
cups_array_t *ra;
cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs(%p[%d], %s)", con, con->http.fd,
uri->values[0].string.text);
httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
sizeof(scheme), username, sizeof(username), host,
sizeof(host), &port, resource, sizeof(resource));
if (!strcmp(resource, "/") ||
(!strncmp(resource, "/jobs", 5) && strlen(resource) <= 6))
{
dest = NULL;
dtype = (cups_ptype_t)0;
dmask = (cups_ptype_t)0;
printer = NULL;
}
else if (!strncmp(resource, "/printers", 9) && strlen(resource) <= 10)
{
dest = NULL;
dtype = (cups_ptype_t)0;
dmask = CUPS_PRINTER_CLASS;
printer = NULL;
}
else if (!strncmp(resource, "/classes", 8) && strlen(resource) <= 9)
{
dest = NULL;
dtype = CUPS_PRINTER_CLASS;
dmask = CUPS_PRINTER_CLASS;
printer = NULL;
}
else if ((dest = cupsdValidateDest(uri->values[0].string.text, &dtype,
&printer)) == NULL)
{
send_ipp_status(con, IPP_NOT_FOUND,
_("The printer or class was not found."));
return;
}
else
{
dtype &= CUPS_PRINTER_CLASS;
dmask = CUPS_PRINTER_CLASS;
}
if (printer)
{
if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con,
NULL)) != HTTP_OK)
{
send_http_error(con, status, printer);
return;
}
}
else if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
{
send_http_error(con, status, NULL);
return;
}
if ((attr = ippFindAttribute(con->request, "which-jobs",
IPP_TAG_KEYWORD)) != NULL &&
!strcmp(attr->values[0].string.text, "completed"))
{
completed = 1;
list = Jobs;
}
else if (attr && !strcmp(attr->values[0].string.text, "all"))
{
completed = 0;
list = Jobs;
}
else
{
completed = 0;
list = ActiveJobs;
}
if ((attr = ippFindAttribute(con->request, "limit",
IPP_TAG_INTEGER)) != NULL)
limit = attr->values[0].integer;
else
limit = 1000000;
if ((attr = ippFindAttribute(con->request, "first-job-id",
IPP_TAG_INTEGER)) != NULL)
first_job_id = attr->values[0].integer;
else
first_job_id = 1;
if ((attr = ippFindAttribute(con->request, "my-jobs",
IPP_TAG_BOOLEAN)) != NULL &&
attr->values[0].boolean)
strlcpy(username, get_username(con), sizeof(username));
else
username[0] = '\0';
ra = create_requested_array(con->request);
for (count = 0, job = (cupsd_job_t *)cupsArrayFirst(list);
count < limit && job;
job = (cupsd_job_t *)cupsArrayNext(list))
{
cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs: job->id = %d", job->id);
if ((dest && strcmp(job->dest, dest)) &&
(!job->printer || !dest || strcmp(job->printer->name, dest)))
continue;
if ((job->dtype & dmask) != dtype &&
(!job->printer || (job->printer->type & dmask) != dtype))
continue;
if (completed && job->state_value <= IPP_JOB_STOPPED)
continue;
if (job->id < first_job_id)
continue;
cupsdLoadJob(job);
if (!job->attrs)
continue;
if (username[0] && strcasecmp(username, job->username))
continue;
if (count > 0)
ippAddSeparator(con->response);
count ++;
cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_jobs: count = %d", count);
copy_job_attrs(con, job, ra);
}
cupsArrayDelete(ra);
con->response->request.status.status_code = IPP_OK;
}
static void
get_notifications(cupsd_client_t *con)
{
int i, j;
http_status_t status;
cupsd_subscription_t *sub;
ipp_attribute_t *ids,
*sequences;
int min_seq;
int interval;
cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_notifications(con=%p[%d])",
con, con->http.fd);
ids = ippFindAttribute(con->request, "notify-subscription-ids",
IPP_TAG_INTEGER);
sequences = ippFindAttribute(con->request, "notify-sequence-numbers",
IPP_TAG_INTEGER);
if (!ids)
{
send_ipp_status(con, IPP_BAD_REQUEST,
_("Missing notify-subscription-ids attribute!"));
return;
}
for (i = 0, interval = 60; i < ids->num_values; i ++)
{
if ((sub = cupsdFindSubscription(ids->values[i].integer)) == NULL)
{
send_ipp_status(con, IPP_NOT_FOUND,
_("notify-subscription-id %d no good!"),
ids->values[i].integer);
return;
}
if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr :
DefaultPolicyPtr,
con, sub->owner)) != HTTP_OK)
{
send_http_error(con, status, sub->dest);
return;
}
if (sub->job && sub->job->state_value == IPP_JOB_PROCESSING &&
interval > 10)
interval = 10;
else if (sub->job && sub->job->state_value >= IPP_JOB_STOPPED)
interval = 0;
else if (sub->dest && sub->dest->state == IPP_PRINTER_PROCESSING &&
interval > 30)
interval = 30;
}
if (interval > 0)
ippAddInteger(con->response, IPP_TAG_OPERATION, IPP_TAG_INTEGER,
"notify-get-interval", interval);
ippAddInteger(con->response, IPP_TAG_OPERATION, IPP_TAG_INTEGER,
"printer-up-time", time(NULL));
con->response->request.status.status_code =
interval ? IPP_OK : IPP_OK_EVENTS_COMPLETE;
for (i = 0; i < ids->num_values; i ++)
{
sub = cupsdFindSubscription(ids->values[i].integer);
if (sequences && i < sequences->num_values)
min_seq = sequences->values[i].integer;
else
min_seq = 1;
if (min_seq > (sub->first_event_id + sub->num_events))
continue;
if (sub->first_event_id > min_seq)
j = 0;
else
j = min_seq - sub->first_event_id;
for (; j < sub->num_events; j ++)
{
ippAddSeparator(con->response);
copy_attrs(con->response, sub->events[j]->attrs, NULL,
IPP_TAG_EVENT_NOTIFICATION, 0);
}
}
}
static void
get_ppd(cupsd_client_t *con,
ipp_attribute_t *uri)
{
http_status_t status;
cupsd_printer_t *dest;
cups_ptype_t dtype;
cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_ppd(%p[%d], %p[%s=%s])", con,
con->http.fd, uri, uri->name, uri->values[0].string.text);
if (!strcmp(uri->name, "ppd-name"))
{
char command[1024],
options[1024],
ppd_name[1024];
if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
{
send_http_error(con, status, NULL);
return;
}
snprintf(command, sizeof(command), "%s/daemon/cups-driverd", ServerBin);
url_encode_string(uri->values[0].string.text, ppd_name, sizeof(ppd_name));
snprintf(options, sizeof(options), "get+%d+%s",
con->request->request.op.request_id, ppd_name);
if (cupsdSendCommand(con, command, options, 0))
{
ippDelete(con->response);
con->response = NULL;
}
else
{
send_ipp_status(con, IPP_INTERNAL_ERROR,
_("cups-driverd failed to execute."));
}
}
else if (!strcmp(uri->name, "printer-uri") &&
cupsdValidateDest(uri->values[0].string.text, &dtype, &dest))
{
int i;
char filename[1024];
if ((status = cupsdCheckPolicy(dest->op_policy_ptr, con, NULL)) != HTTP_OK)
{
send_http_error(con, status, NULL);
return;
}
snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot,
dest->name);
if ((dtype & CUPS_PRINTER_REMOTE) && access(filename, 0))
{
con->response->request.status.status_code = CUPS_SEE_OTHER;
ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_URI,
"printer-uri", NULL, dest->uri);
return;
}
else if (dtype & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT))
{
for (i = 0; i < dest->num_printers; i ++)
if (!(dest->printers[i]->type &
(CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT)))
{
snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot,
dest->printers[i]->name);
if (!access(filename, 0))
break;
}
if (i < dest->num_printers)
dest = dest->printers[i];
else
{
con->response->request.status.status_code = CUPS_SEE_OTHER;
ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_URI,
"printer-uri", NULL, dest->printers[0]->uri);
return;
}
}
if ((con->file = open(filename, O_RDONLY)) < 0)
{
send_ipp_status(con, IPP_NOT_FOUND,
_("The PPD file \"%s\" could not be opened: %s"),
uri->values[0].string.text, strerror(errno));
return;
}
fcntl(con->file, F_SETFD, fcntl(con->file, F_GETFD) | FD_CLOEXEC);
con->pipe_pid = 0;
con->response->request.status.status_code = IPP_OK;
}
else
send_ipp_status(con, IPP_NOT_FOUND,
_("The PPD file \"%s\" could not be found."),
uri->values[0].string.text);
}
static void
get_ppds(cupsd_client_t *con)
{
http_status_t status;
ipp_attribute_t *limit,
*device,
*language,
*make,
*model,
*model_number,
*product,
*psversion,
*type,
*requested;
char command[1024],
options[1024],
device_str[256],
language_str[256],
make_str[256],
model_str[256],
model_number_str[256],
product_str[256],
psversion_str[256],
type_str[256],
requested_str[256];
cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_ppds(%p[%d])", con, con->http.fd);
if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
{
send_http_error(con, status, NULL);
return;
}
limit = ippFindAttribute(con->request, "limit", IPP_TAG_INTEGER);
device = ippFindAttribute(con->request, "ppd-device-id", IPP_TAG_TEXT);
language = ippFindAttribute(con->request, "ppd-natural-language",
IPP_TAG_LANGUAGE);
make = ippFindAttribute(con->request, "ppd-make", IPP_TAG_TEXT);
model = ippFindAttribute(con->request, "ppd-make-and-model",
IPP_TAG_TEXT);
model_number = ippFindAttribute(con->request, "ppd-model-number",
IPP_TAG_INTEGER);
product = ippFindAttribute(con->request, "ppd-product", IPP_TAG_TEXT);
psversion = ippFindAttribute(con->request, "ppd-psversion", IPP_TAG_TEXT);
type = ippFindAttribute(con->request, "ppd-type", IPP_TAG_KEYWORD);
requested = ippFindAttribute(con->request, "requested-attributes",
IPP_TAG_KEYWORD);
if (requested)
url_encode_attr(requested, requested_str, sizeof(requested_str));
else
strlcpy(requested_str, "requested-attributes=all", sizeof(requested_str));
if (device)
url_encode_attr(device, device_str, sizeof(device_str));
else
device_str[0] = '\0';
if (language)
url_encode_attr(language, language_str, sizeof(language_str));
else
language_str[0] = '\0';
if (make)
url_encode_attr(make, make_str, sizeof(make_str));
else
make_str[0] = '\0';
if (model)
url_encode_attr(model, model_str, sizeof(model_str));
else
model_str[0] = '\0';
if (model_number)
snprintf(model_number_str, sizeof(model_number_str), "ppd-model-number=%d",
model_number->values[0].integer);
else
model_number_str[0] = '\0';
if (product)
url_encode_attr(product, product_str, sizeof(product_str));
else
product_str[0] = '\0';
if (psversion)
url_encode_attr(psversion, psversion_str, sizeof(psversion_str));
else
psversion_str[0] = '\0';
if (type)
url_encode_attr(type, type_str, sizeof(type_str));
else
type_str[0] = '\0';
snprintf(command, sizeof(command), "%s/daemon/cups-driverd", ServerBin);
snprintf(options, sizeof(options),
"list+%d+%d+%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
con->request->request.op.request_id,
limit ? limit->values[0].integer : 0,
requested_str,
device ? "%20" : "", device_str,
language ? "%20" : "", language_str,
make ? "%20" : "", make_str,
model ? "%20" : "", model_str,
model_number ? "%20" : "", model_number_str,
product ? "%20" : "", product_str,
psversion ? "%20" : "", psversion_str,
type ? "%20" : "", type_str);
if (cupsdSendCommand(con, command, options, 0))
{
ippDelete(con->response);
con->response = NULL;
}
else
{
send_ipp_status(con, IPP_INTERNAL_ERROR,
_("cups-driverd failed to execute."));
}
}
static void
get_printer_attrs(cupsd_client_t *con,
ipp_attribute_t *uri)
{
http_status_t status;
cups_ptype_t dtype;
cupsd_printer_t *printer;
cups_array_t *ra;
cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_printer_attrs(%p[%d], %s)", con,
con->http.fd, uri->values[0].string.text);
if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
{
send_ipp_status(con, IPP_NOT_FOUND,
_("The printer or class was not found."));
return;
}
if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
{
send_http_error(con, status, printer);
return;
}
ra = create_requested_array(con->request);
copy_printer_attrs(con, printer, ra);
cupsArrayDelete(ra);
con->response->request.status.status_code = IPP_OK;
}
static void
get_printers(cupsd_client_t *con,
int type)
{
http_status_t status;
ipp_attribute_t *attr;
int limit;
int count;
cupsd_printer_t *printer;
int printer_type,
printer_mask;
char *location;
const char *username;
char *first_printer_name;
cups_array_t *ra;
cupsdLogMessage(CUPSD_LOG_DEBUG2, "get_printers(%p[%d], %x)", con,
con->http.fd, type);
if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
{
send_http_error(con, status, NULL);
return;
}
if (!Printers || !cupsArrayCount(Printers))
{
send_ipp_status(con, IPP_NOT_FOUND, _("No destinations added."));
return;
}
if ((attr = ippFindAttribute(con->request, "limit",
IPP_TAG_INTEGER)) != NULL)
limit = attr->values[0].integer;
else
limit = 10000000;
if ((attr = ippFindAttribute(con->request, "first-printer-name",
IPP_TAG_NAME)) != NULL)
first_printer_name = attr->values[0].string.text;
else
first_printer_name = NULL;
if ((attr = ippFindAttribute(con->request, "printer-type",
IPP_TAG_ENUM)) != NULL)
printer_type = attr->values[0].integer;
else
printer_type = 0;
if ((attr = ippFindAttribute(con->request, "printer-type-mask",
IPP_TAG_ENUM)) != NULL)
printer_mask = attr->values[0].integer;
else
printer_mask = 0;
if ((attr = ippFindAttribute(con->request, "printer-location",
IPP_TAG_TEXT)) != NULL)
location = attr->values[0].string.text;
else
location = NULL;
if (con->username[0])
username = con->username;
else if ((attr = ippFindAttribute(con->request, "requesting-user-name",
IPP_TAG_NAME)) != NULL)
username = attr->values[0].string.text;
else
username = NULL;
ra = create_requested_array(con->request);
if (first_printer_name)
{
if ((printer = cupsdFindDest(first_printer_name)) == NULL)
printer = (cupsd_printer_t *)cupsArrayFirst(Printers);
}
else
printer = (cupsd_printer_t *)cupsArrayFirst(Printers);
for (count = 0;
count < limit && printer;
printer = (cupsd_printer_t *)cupsArrayNext(Printers))
{
if ((!type || (printer->type & CUPS_PRINTER_CLASS) == type) &&
(printer->type & printer_mask) == printer_type &&
(!location || !printer->location ||
!strcasecmp(printer->location, location)))
{
if (ImplicitClasses && HideImplicitMembers &&
printer->in_implicit_class)
continue;
if (printer->num_users && username && !user_allowed(printer, username))
continue;
if (count > 0)
ippAddSeparator(con->response);
count ++;
copy_printer_attrs(con, printer, ra);
}
}
cupsArrayDelete(ra);
con->response->request.status.status_code = IPP_OK;
}
static void
get_subscription_attrs(
cupsd_client_t *con,
int sub_id)
{
http_status_t status;
cupsd_subscription_t *sub;
cups_array_t *ra;
cupsdLogMessage(CUPSD_LOG_DEBUG2,
"get_subscription_attrs(con=%p[%d], sub_id=%d)",
con, con->http.fd, sub_id);
if ((sub = cupsdFindSubscription(sub_id)) == NULL)
{
send_ipp_status(con, IPP_NOT_FOUND,
_("notify-subscription-id %d no good!"), sub_id);
return;
}
if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr :
DefaultPolicyPtr,
con, sub->owner)) != HTTP_OK)
{
send_http_error(con, status, sub->dest);
return;
}
ra = create_requested_array(con->request);
copy_subscription_attrs(con, sub, ra);
cupsArrayDelete(ra);
con->response->request.status.status_code = IPP_OK;
}
static void
get_subscriptions(cupsd_client_t *con,
ipp_attribute_t *uri)
{
http_status_t status;
int count;
int limit;
cupsd_subscription_t *sub;
cups_array_t *ra;
ipp_attribute_t *attr;
cups_ptype_t dtype;
char scheme[HTTP_MAX_URI],
username[HTTP_MAX_URI],
host[HTTP_MAX_URI],
resource[HTTP_MAX_URI];
int port;
cupsd_job_t *job;
cupsd_printer_t *printer;
cupsdLogMessage(CUPSD_LOG_DEBUG2,
"get_subscriptions(con=%p[%d], uri=%s)",
con, con->http.fd, uri->values[0].string.text);
httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
sizeof(scheme), username, sizeof(username), host,
sizeof(host), &port, resource, sizeof(resource));
if (!strcmp(resource, "/") ||
(!strncmp(resource, "/jobs", 5) && strlen(resource) <= 6) ||
(!strncmp(resource, "/printers", 9) && strlen(resource) <= 10) ||
(!strncmp(resource, "/classes", 8) && strlen(resource) <= 9))
{
printer = NULL;
job = NULL;
}
else if (!strncmp(resource, "/jobs/", 6) && resource[6])
{
printer = NULL;
job = cupsdFindJob(atoi(resource + 6));
if (!job)
{
send_ipp_status(con, IPP_NOT_FOUND, _("Job #%s does not exist!"),
resource + 6);
return;
}
}
else if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
{
send_ipp_status(con, IPP_NOT_FOUND,
_("The printer or class was not found."));
return;
}
else if ((attr = ippFindAttribute(con->request, "notify-job-id",
IPP_TAG_INTEGER)) != NULL)
{
job = cupsdFindJob(attr->values[0].integer);
if (!job)
{
send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"),
attr->values[0].integer);
return;
}
}
else
job = NULL;
if ((status = cupsdCheckPolicy(printer ? printer->op_policy_ptr :
DefaultPolicyPtr,
con, NULL)) != HTTP_OK)
{
send_http_error(con, status, printer);
return;
}
ra = create_requested_array(con->request);
if ((attr = ippFindAttribute(con->request, "limit",
IPP_TAG_INTEGER)) != NULL)
limit = attr->values[0].integer;
else
limit = 0;
if ((attr = ippFindAttribute(con->request, "my-subscriptions",
IPP_TAG_BOOLEAN)) != NULL &&
attr->values[0].boolean)
strlcpy(username, get_username(con), sizeof(username));
else
username[0] = '\0';
for (sub = (cupsd_subscription_t *)cupsArrayFirst(Subscriptions), count = 0;
sub;
sub = (cupsd_subscription_t *)cupsArrayNext(Subscriptions))
if ((!printer || sub->dest == printer) && (!job || sub->job == job) &&
(!username[0] || !strcasecmp(username, sub->owner)))
{
ippAddSeparator(con->response);
copy_subscription_attrs(con, sub, ra);
count ++;
if (limit && count >= limit)
break;
}
cupsArrayDelete(ra);
if (count)
con->response->request.status.status_code = IPP_OK;
else
send_ipp_status(con, IPP_NOT_FOUND, _("No subscriptions found."));
}
static const char *
get_username(cupsd_client_t *con)
{
ipp_attribute_t *attr;
if (con->username[0])
return (con->username);
else if ((attr = ippFindAttribute(con->request, "requesting-user-name",
IPP_TAG_NAME)) != NULL)
return (attr->values[0].string.text);
else
return ("anonymous");
}
static void
hold_job(cupsd_client_t *con,
ipp_attribute_t *uri)
{
ipp_attribute_t *attr,
*newattr;
int jobid;
char method[HTTP_MAX_URI],
username[HTTP_MAX_URI],
host[HTTP_MAX_URI],
resource[HTTP_MAX_URI];
int port;
cupsd_job_t *job;
cupsdLogMessage(CUPSD_LOG_DEBUG2, "hold_job(%p[%d], %s)", con, con->http.fd,
uri->values[0].string.text);
if (!strcmp(uri->name, "printer-uri"))
{
if ((attr = ippFindAttribute(con->request, "job-id",
IPP_TAG_INTEGER)) == NULL)
{
send_ipp_status(con, IPP_BAD_REQUEST,
_("Got a printer-uri attribute but no job-id!"));
return;
}
jobid = attr->values[0].integer;
}
else
{
httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method,
sizeof(method), username, sizeof(username), host,
sizeof(host), &port, resource, sizeof(resource));
if (strncmp(resource, "/jobs/", 6))
{
send_ipp_status(con, IPP_BAD_REQUEST,
_("Bad job-uri attribute \"%s\"!"),
uri->values[0].string.text);
return;
}
jobid = atoi(resource + 6);
}
if ((job = cupsdFindJob(jobid)) == NULL)
{
send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid);
return;
}
if (!validate_user(job, con, job->username, username, sizeof(username)))
{
send_http_error(con, HTTP_UNAUTHORIZED, NULL);
return;
}
cupsdHoldJob(job);
cupsdAddEvent(CUPSD_EVENT_JOB_STATE, job->printer, job,
"Job held by user.");
if ((newattr = ippFindAttribute(con->request, "job-hold-until",
IPP_TAG_KEYWORD)) == NULL)
newattr = ippFindAttribute(con->request, "job-hold-until", IPP_TAG_NAME);
if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
IPP_TAG_KEYWORD)) == NULL)
attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
if (attr)
{
_cupsStrFree(attr->values[0].string.text);
if (newattr)
{
attr->value_tag = newattr->value_tag;
attr->values[0].string.text =
_cupsStrAlloc(newattr->values[0].string.text);
}
else
{
attr->value_tag = IPP_TAG_KEYWORD;
attr->values[0].string.text = _cupsStrAlloc("indefinite");
}
cupsdSetJobHoldUntil(job, attr->values[0].string.text);
cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED, job->printer, job,
"Job job-hold-until value changed by user.");
}
cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Held by \"%s\".", jobid,
username);
con->response->request.status.status_code = IPP_OK;
}
static void
move_job(cupsd_client_t *con,
ipp_attribute_t *uri)
{
http_status_t status;
ipp_attribute_t *attr;
int jobid;
cupsd_job_t *job;
const char *src;
cups_ptype_t stype,
dtype;
char scheme[HTTP_MAX_URI],
username[HTTP_MAX_URI],
host[HTTP_MAX_URI],
resource[HTTP_MAX_URI];
int port;
cupsd_printer_t *sprinter,
*dprinter;
cupsdLogMessage(CUPSD_LOG_DEBUG2, "move_job(%p[%d], %s)", con, con->http.fd,
uri->values[0].string.text);
if ((attr = ippFindAttribute(con->request, "job-printer-uri",
IPP_TAG_URI)) == NULL)
{
send_ipp_status(con, IPP_BAD_REQUEST,
_("job-printer-uri attribute missing!"));
return;
}
if (!cupsdValidateDest(attr->values[0].string.text, &dtype, &dprinter))
{
send_ipp_status(con, IPP_NOT_FOUND,
_("The printer or class was not found."));
return;
}
if ((status = cupsdCheckPolicy(dprinter->op_policy_ptr, con,
NULL)) != HTTP_OK)
{
send_http_error(con, status, dprinter);
return;
}
httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, scheme,
sizeof(scheme), username, sizeof(username), host,
sizeof(host), &port, resource, sizeof(resource));
if (!strcmp(uri->name, "printer-uri"))
{
if ((attr = ippFindAttribute(con->request, "job-id",
IPP_TAG_INTEGER)) == NULL)
{
if ((src = cupsdValidateDest(uri->values[0].string.text, &stype,
&sprinter)) == NULL)
{
send_ipp_status(con, IPP_NOT_FOUND,
_("The printer or class was not found."));
return;
}
job = NULL;
}
else
{
if ((job = cupsdFindJob(attr->values[0].integer)) == NULL)
{
send_ipp_status(con, IPP_NOT_FOUND,
_("Job #%d does not exist!"), attr->values[0].integer);
return;
}
else
{
src = NULL;
sprinter = NULL;
}
}
}
else
{
if (strncmp(resource, "/jobs/", 6))
{
send_ipp_status(con, IPP_BAD_REQUEST,
_("Bad job-uri attribute \"%s\"!"),
uri->values[0].string.text);
return;
}
jobid = atoi(resource + 6);
if ((job = cupsdFindJob(jobid)) == NULL)
{
send_ipp_status(con, IPP_NOT_FOUND,
_("Job #%d does not exist!"), jobid);
return;
}
else
{
src = NULL;
sprinter = NULL;
}
}
if (job)
{
if (job->state_value > IPP_JOB_STOPPED)
{
send_ipp_status(con, IPP_NOT_POSSIBLE,
_("Job #%d is finished and cannot be altered!"),
job->id);
return;
}
if (!validate_user(job, con, job->username, username, sizeof(username)))
{
send_http_error(con, HTTP_UNAUTHORIZED, NULL);
return;
}
cupsdMoveJob(job, dprinter);
}
else
{
for (job = (cupsd_job_t *)cupsArrayFirst(Jobs);
job;
job = (cupsd_job_t *)cupsArrayNext(Jobs))
{
if (strcasecmp(job->dest, src) ||
job->state_value > IPP_JOB_STOPPED)
continue;
if (!validate_user(job, con, job->username, username, sizeof(username)))
continue;
cupsdMoveJob(job, dprinter);
}
}
cupsdCheckJobs();
con->response->request.status.status_code = IPP_OK;
}
static int
ppd_parse_line(const char *line,
char *option,
int olen,
char *choice,
int clen)
{
if (strncmp(line, "*Default", 8))
return (-1);
for (line += 8, olen --; isalnum(*line & 255); line ++)
if (olen > 0)
{
*option++ = *line;
olen --;
}
*option = '\0';
while (*line && *line != ':')
line ++;
if (!*line)
return (-1);
line ++;
while (isspace(*line & 255))
line ++;
for (clen --; isalnum(*line & 255); line ++)
if (clen > 0)
{
*choice++ = *line;
clen --;
}
*choice = '\0';
return (0);
}
static void
print_job(cupsd_client_t *con,
ipp_attribute_t *uri)
{
ipp_attribute_t *attr;
ipp_attribute_t *format;
const char *default_format;
cupsd_job_t *job;
char filename[1024];
mime_type_t *filetype;
char super[MIME_MAX_SUPER],
type[MIME_MAX_TYPE],
mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2];
cupsd_printer_t *printer;
struct stat fileinfo;
int kbytes;
int compression;
cupsdLogMessage(CUPSD_LOG_DEBUG2, "print_job(%p[%d], %s)", con, con->http.fd,
uri->values[0].string.text);
compression = CUPS_FILE_NONE;
if ((attr = ippFindAttribute(con->request, "compression",
IPP_TAG_KEYWORD)) != NULL)
{
if (strcmp(attr->values[0].string.text, "none")
#ifdef HAVE_LIBZ
&& strcmp(attr->values[0].string.text, "gzip")
#endif
)
{
send_ipp_status(con, IPP_ATTRIBUTES,
_("Unsupported compression \"%s\"!"),
attr->values[0].string.text);
ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD,
"compression", NULL, attr->values[0].string.text);
return;
}
#ifdef HAVE_LIBZ
if (!strcmp(attr->values[0].string.text, "gzip"))
compression = CUPS_FILE_GZIP;
#endif
}
if (!con->filename)
{
send_ipp_status(con, IPP_BAD_REQUEST, _("No file!?!"));
return;
}
if (!cupsdValidateDest(uri->values[0].string.text, NULL, &printer))
{
send_ipp_status(con, IPP_NOT_FOUND,
_("The printer or class was not found."));
return;
}
if ((format = ippFindAttribute(con->request, "document-format",
IPP_TAG_MIMETYPE)) != NULL)
{
if (sscanf(format->values[0].string.text, "%15[^/]/%31[^;]", super,
type) != 2)
{
send_ipp_status(con, IPP_BAD_REQUEST,
_("Could not scan type \"%s\"!"),
format->values[0].string.text);
return;
}
}
else if ((default_format = cupsGetOption("document-format",
printer->num_options,
printer->options)) != NULL)
{
if (sscanf(default_format, "%15[^/]/%31[^;]", super, type) != 2)
{
send_ipp_status(con, IPP_BAD_REQUEST,
_("Could not scan type \"%s\"!"),
default_format);
return;
}
}
else
{
strcpy(super, "application");
strcpy(type, "octet-stream");
}
if (!strcmp(super, "application") && !strcmp(type, "octet-stream"))
{
ipp_attribute_t *doc_name;
cupsdLogMessage(CUPSD_LOG_DEBUG, "print_job: auto-typing file...");
doc_name = ippFindAttribute(con->request, "document-name", IPP_TAG_NAME);
filetype = mimeFileType(MimeDatabase, con->filename,
doc_name ? doc_name->values[0].string.text : NULL,
&compression);
if (!filetype)
filetype = mimeType(MimeDatabase, super, type);
}
else
filetype = mimeType(MimeDatabase, super, type);
if (filetype &&
(!format ||
(!strcmp(super, "application") && !strcmp(type, "octet-stream"))))
{
snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super,
filetype->type);
if (format)
{
_cupsStrFree(format->values[0].string.text);
format->values[0].string.text = _cupsStrAlloc(mimetype);
}
else
ippAddString(con->request, IPP_TAG_JOB, IPP_TAG_MIMETYPE,
"document-format", NULL, mimetype);
}
else if (!filetype)
{
send_ipp_status(con, IPP_DOCUMENT_FORMAT,
_("Unsupported format \'%s/%s\'!"), super, type);
cupsdLogMessage(CUPSD_LOG_INFO,
"Hint: Do you have the raw file printing rules enabled?");
if (format)
ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE,
"document-format", NULL, format->values[0].string.text);
return;
}
if (!strcasecmp(filetype->super, "application") &&
!strcasecmp(filetype->type, "postscript"))
read_ps_job_ticket(con);
if ((job = add_job(con, printer, filetype)) == NULL)
return;
cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Adding job file of type %s/%s.",
job->id, filetype->super, filetype->type);
if (stat(con->filename, &fileinfo))
kbytes = 0;
else
kbytes = (fileinfo.st_size + 1023) / 1024;
cupsdUpdateQuota(printer, job->username, 0, kbytes);
if ((attr = ippFindAttribute(job->attrs, "job-k-octets",
IPP_TAG_INTEGER)) != NULL)
attr->values[0].integer += kbytes;
if (add_file(con, job, filetype, compression))
return;
snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, job->id,
job->num_files);
rename(con->filename, filename);
cupsdClearString(&con->filename);
cupsdTimeoutJob(job);
cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Queued on \"%s\" by \"%s\".",
job->id, job->dest, job->username);
cupsdLogMessage(CUPSD_LOG_DEBUG, "[Job %d] hold_until = %d", job->id,
(int)job->hold_until);
cupsdSaveJob(job);
cupsdCheckJobs();
}
static void
read_ps_job_ticket(cupsd_client_t *con)
{
cups_file_t *fp;
char line[256];
int num_options;
cups_option_t *options;
ipp_t *ticket;
ipp_attribute_t *attr,
*attr2,
*prev2;
if ((fp = cupsFileOpen(con->filename, "rb")) == NULL)
{
cupsdLogMessage(CUPSD_LOG_ERROR,
"read_ps_job_ticket: Unable to open PostScript print file "
"- %s",
strerror(errno));
return;
}
if (cupsFileGets(fp, line, sizeof(line)) == NULL)
{
cupsdLogMessage(CUPSD_LOG_ERROR,
"read_ps_job_ticket: Unable to read from PostScript print "
"file - %s",
strerror(errno));
cupsFileClose(fp);
return;
}
if (strncmp(line, "%!PS-Adobe-", 11))
{
cupsFileClose(fp);
return;
}
num_options = 0;
options = NULL;
while (cupsFileGets(fp, line, sizeof(line)))
{
if (strncmp(line, "%cupsJobTicket:", 15))
break;
num_options = cupsParseOptions(line + 15, num_options, &options);
}
cupsFileClose(fp);
if (num_options == 0)
return;
ticket = ippNew();
cupsEncodeOptions(ticket, num_options, options);
for (attr = ticket->attrs; attr; attr = attr->next)
{
if (attr->group_tag != IPP_TAG_JOB || !attr->name)
continue;
if (!strcmp(attr->name, "job-originating-host-name") ||
!strcmp(attr->name, "job-originating-user-name") ||
!strcmp(attr->name, "job-media-sheets-completed") ||
!strcmp(attr->name, "job-k-octets") ||
!strcmp(attr->name, "job-id") ||
!strncmp(attr->name, "job-state", 9) ||
!strncmp(attr->name, "time-at-", 8))
continue;
if ((attr2 = ippFindAttribute(con->request, attr->name,
IPP_TAG_ZERO)) != NULL)
{
if (con->request->attrs == attr2)
{
con->request->attrs = attr2->next;
prev2 = NULL;
}
else
{
for (prev2 = con->request->attrs; prev2; prev2 = prev2->next)
if (prev2->next == attr2)
{
prev2->next = attr2->next;
break;
}
}
if (con->request->last == attr2)
con->request->last = prev2;
_ippFreeAttr(attr2);
}
copy_attribute(con->request, attr, 0);
}
ippDelete(ticket);
cupsFreeOptions(num_options, options);
}
static void
reject_jobs(cupsd_client_t *con,
ipp_attribute_t *uri)
{
http_status_t status;
cups_ptype_t dtype;
cupsd_printer_t *printer;
ipp_attribute_t *attr;
cupsdLogMessage(CUPSD_LOG_DEBUG2, "reject_jobs(%p[%d], %s)", con,
con->http.fd, uri->values[0].string.text);
if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
{
send_ipp_status(con, IPP_NOT_FOUND,
_("The printer or class was not found."));
return;
}
if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
{
send_http_error(con, status, printer);
return;
}
printer->accepting = 0;
if ((attr = ippFindAttribute(con->request, "printer-state-message",
IPP_TAG_TEXT)) == NULL)
strcpy(printer->state_message, "Rejecting Jobs");
else
strlcpy(printer->state_message, attr->values[0].string.text,
sizeof(printer->state_message));
cupsdAddPrinterHistory(printer);
if (dtype & CUPS_PRINTER_CLASS)
{
cupsdSaveAllClasses();
cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" rejecting jobs (\"%s\").",
printer->name, get_username(con));
}
else
{
cupsdSaveAllPrinters();
cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" rejecting jobs (\"%s\").",
printer->name, get_username(con));
}
con->response->request.status.status_code = IPP_OK;
}
static void
release_job(cupsd_client_t *con,
ipp_attribute_t *uri)
{
ipp_attribute_t *attr;
int jobid;
char method[HTTP_MAX_URI],
username[HTTP_MAX_URI],
host[HTTP_MAX_URI],
resource[HTTP_MAX_URI];
int port;
cupsd_job_t *job;
cupsdLogMessage(CUPSD_LOG_DEBUG2, "release_job(%p[%d], %s)", con,
con->http.fd, uri->values[0].string.text);
if (!strcmp(uri->name, "printer-uri"))
{
if ((attr = ippFindAttribute(con->request, "job-id",
IPP_TAG_INTEGER)) == NULL)
{
send_ipp_status(con, IPP_BAD_REQUEST,
_("Got a printer-uri attribute but no job-id!"));
return;
}
jobid = attr->values[0].integer;
}
else
{
httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method,
sizeof(method), username, sizeof(username), host,
sizeof(host), &port, resource, sizeof(resource));
if (strncmp(resource, "/jobs/", 6))
{
send_ipp_status(con, IPP_BAD_REQUEST,
_("Bad job-uri attribute \"%s\"!"),
uri->values[0].string.text);
return;
}
jobid = atoi(resource + 6);
}
if ((job = cupsdFindJob(jobid)) == NULL)
{
send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid);
return;
}
if (job->state_value != IPP_JOB_HELD)
{
send_ipp_status(con, IPP_NOT_POSSIBLE, _("Job #%d is not held!"), jobid);
return;
}
if (!validate_user(job, con, job->username, username, sizeof(username)))
{
send_http_error(con, HTTP_UNAUTHORIZED, NULL);
return;
}
if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
IPP_TAG_KEYWORD)) == NULL)
attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
if (attr)
{
_cupsStrFree(attr->values[0].string.text);
attr->value_tag = IPP_TAG_KEYWORD;
attr->values[0].string.text = _cupsStrAlloc("no-hold");
cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED, job->printer, job,
"Job job-hold-until value changed by user.");
}
cupsdReleaseJob(job);
cupsdAddEvent(CUPSD_EVENT_JOB_STATE, job->printer, job,
"Job released by user.");
cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Released by \"%s\".", jobid,
username);
con->response->request.status.status_code = IPP_OK;
}
static void
renew_subscription(
cupsd_client_t *con,
int sub_id)
{
http_status_t status;
cupsd_subscription_t *sub;
ipp_attribute_t *lease;
cupsdLogMessage(CUPSD_LOG_DEBUG2,
"renew_subscription(con=%p[%d], sub_id=%d)",
con, con->http.fd, sub_id);
if ((sub = cupsdFindSubscription(sub_id)) == NULL)
{
send_ipp_status(con, IPP_NOT_FOUND,
_("notify-subscription-id %d no good!"), sub_id);
return;
}
if (sub->job)
{
send_ipp_status(con, IPP_NOT_POSSIBLE,
_("Job subscriptions cannot be renewed!"));
return;
}
if ((status = cupsdCheckPolicy(sub->dest ? sub->dest->op_policy_ptr :
DefaultPolicyPtr,
con, sub->owner)) != HTTP_OK)
{
send_http_error(con, status, sub->dest);
return;
}
lease = ippFindAttribute(con->request, "notify-lease-duration",
IPP_TAG_INTEGER);
sub->lease = lease ? lease->values[0].integer : DefaultLeaseDuration;
if (MaxLeaseDuration && (sub->lease == 0 || sub->lease > MaxLeaseDuration))
{
cupsdLogMessage(CUPSD_LOG_INFO,
"renew_subscription: Limiting notify-lease-duration to "
"%d seconds.",
MaxLeaseDuration);
sub->lease = MaxLeaseDuration;
}
sub->expire = sub->lease ? time(NULL) + sub->lease : 0;
cupsdSaveAllSubscriptions();
con->response->request.status.status_code = IPP_OK;
ippAddInteger(con->response, IPP_TAG_SUBSCRIPTION, IPP_TAG_INTEGER,
"notify-lease-duration", sub->lease);
}
static void
restart_job(cupsd_client_t *con,
ipp_attribute_t *uri)
{
ipp_attribute_t *attr;
int jobid;
char method[HTTP_MAX_URI],
username[HTTP_MAX_URI],
host[HTTP_MAX_URI],
resource[HTTP_MAX_URI];
int port;
cupsd_job_t *job;
cupsdLogMessage(CUPSD_LOG_DEBUG2, "restart_job(%p[%d], %s)", con,
con->http.fd, uri->values[0].string.text);
if (!strcmp(uri->name, "printer-uri"))
{
if ((attr = ippFindAttribute(con->request, "job-id",
IPP_TAG_INTEGER)) == NULL)
{
send_ipp_status(con, IPP_BAD_REQUEST,
_("Got a printer-uri attribute but no job-id!"));
return;
}
jobid = attr->values[0].integer;
}
else
{
httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method,
sizeof(method), username, sizeof(username), host,
sizeof(host), &port, resource, sizeof(resource));
if (strncmp(resource, "/jobs/", 6))
{
send_ipp_status(con, IPP_BAD_REQUEST,
_("Bad job-uri attribute \"%s\"!"),
uri->values[0].string.text);
return;
}
jobid = atoi(resource + 6);
}
if ((job = cupsdFindJob(jobid)) == NULL)
{
send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid);
return;
}
if (job->state_value <= IPP_JOB_PROCESSING)
{
send_ipp_status(con, IPP_NOT_POSSIBLE, _("Job #%d is not complete!"),
jobid);
return;
}
cupsdLoadJob(job);
if (!job->attrs || job->num_files == 0)
{
send_ipp_status(con, IPP_NOT_POSSIBLE,
_("Job #%d cannot be restarted - no files!"), jobid);
return;
}
if (!validate_user(job, con, job->username, username, sizeof(username)))
{
send_http_error(con, HTTP_UNAUTHORIZED, NULL);
return;
}
cupsdRestartJob(job);
cupsdLogMessage(CUPSD_LOG_INFO, "[Job %d] Restarted by \"%s\".", jobid,
username);
con->response->request.status.status_code = IPP_OK;
}
static void
save_auth_info(
cupsd_client_t *con,
cupsd_job_t *job,
ipp_attribute_t *auth_info)
{
int i;
char filename[1024];
cups_file_t *fp;
char line[2048];
cupsd_printer_t *dest;
if (RunUser)
return;
if ((dest = cupsdFindDest(job->dest)) == NULL)
return;
snprintf(filename, sizeof(filename), "%s/a%05d", RequestRoot, job->id);
if ((fp = cupsFileOpen(filename, "w")) == NULL)
{
cupsdLogMessage(CUPSD_LOG_ERROR,
"Unable to save authentication info to \"%s\" - %s",
filename, strerror(errno));
return;
}
fchown(cupsFileNumber(fp), 0, 0);
fchmod(cupsFileNumber(fp), 0400);
if (auth_info && auth_info->num_values == dest->num_auth_info_required)
{
cupsdClearString(&job->auth_username);
cupsdClearString(&job->auth_domain);
cupsdClearString(&job->auth_password);
for (i = 0; i < auth_info->num_values; i ++)
{
httpEncode64_2(line, sizeof(line), auth_info->values[i].string.text,
strlen(auth_info->values[i].string.text));
cupsFilePrintf(fp, "%s\n", line);
if (!strcmp(dest->auth_info_required[i], "username"))
cupsdSetStringf(&job->auth_username, "AUTH_USERNAME=%s",
auth_info->values[i].string.text);
else if (!strcmp(dest->auth_info_required[i], "domain"))
cupsdSetStringf(&job->auth_domain, "AUTH_DOMAIN=%s",
auth_info->values[i].string.text);
else if (!strcmp(dest->auth_info_required[i], "password"))
cupsdSetStringf(&job->auth_password, "AUTH_PASSWORD=%s",
auth_info->values[i].string.text);
}
}
else if (con->username[0])
{
httpEncode64_2(line, sizeof(line), con->username, strlen(con->username));
cupsFilePrintf(fp, "%s\n", line);
cupsdSetStringf(&job->auth_username, "AUTH_USERNAME=%s", con->username);
cupsdClearString(&job->auth_domain);
httpEncode64_2(line, sizeof(line), con->password, strlen(con->password));
cupsFilePrintf(fp, "%s\n", line);
cupsdSetStringf(&job->auth_password, "AUTH_PASSWORD=%s", con->password);
}
for (i = (rand() % 1024); i >= 0; i --)
cupsFilePutChar(fp, '\n');
cupsFileClose(fp);
#if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
if (con->gss_have_creds)
save_krb5_creds(con, job);
else if (job->ccname)
cupsdClearString(&(job->ccname));
#endif
}
#if defined(HAVE_GSSAPI) && defined(HAVE_KRB5_H)
static void
save_krb5_creds(cupsd_client_t *con,
cupsd_job_t *job)
{
# if !defined(HAVE_KRB5_CC_NEW_UNIQUE) && !defined(HAVE_HEIMDAL)
cupsdLogMessage(CUPSD_LOG_INFO,
"Sorry, your version of Kerberos does not support delegated "
"credentials!");
return;
# else
krb5_error_code error;
OM_uint32 major_status,
minor_status;
# ifdef HAVE_KRB5_CC_NEW_UNIQUE
krb5_principal principal;
# endif
# ifdef __APPLE__
if (krb5_init_context == NULL)
return;
# endif
# ifdef HAVE_KRB5_CC_NEW_UNIQUE
if ((error = krb5_cc_new_unique(KerberosContext, "FILE", NULL,
&(job->ccache))) != 0)
# else
if ((error = krb5_cc_gen_new(krb_context, &krb5_fcc_ops,
&(job->ccache))) != 0)
# endif
{
cupsdLogMessage(CUPSD_LOG_ERROR,
"Unable to create new credentials cache (%d/%s)",
error, strerror(errno));
job->ccache = NULL;
return;
}
if ((error = krb5_parse_name(KerberosContext, con->username, &principal)) != 0)
{
cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to parse kerberos username (%d/%s)",
error, strerror(errno));
krb5_cc_destroy(KerberosContext, job->ccache);
job->ccache = NULL;
return;
}
if ((error = krb5_cc_initialize(KerberosContext, job->ccache, principal)))
{
cupsdLogMessage(CUPSD_LOG_ERROR,
"Unable to initialize credentials cache (%d/%s)", error,
strerror(errno));
krb5_cc_destroy(KerberosContext, job->ccache);
krb5_free_principal(KerberosContext, principal);
job->ccache = NULL;
return;
}
krb5_free_principal(KerberosContext, principal);
major_status = gss_krb5_copy_ccache(&minor_status, con->gss_delegated_cred,
job->ccache);
if (GSS_ERROR(major_status))
{
cupsdLogGSSMessage(CUPSD_LOG_ERROR, major_status, minor_status,
"Unable to import client credentials cache");
krb5_cc_destroy(KerberosContext, job->ccache);
job->ccache = NULL;
return;
}
cupsdSetStringf(&(job->ccname), "KRB5CCNAME=FILE:%s",
krb5_cc_get_name(KerberosContext, job->ccache));
cupsdLogMessage(CUPSD_LOG_DEBUG2, "[Job %d] save_krb5_creds: %s", job->id,
job->ccname);
# endif
}
#endif
static void
send_document(cupsd_client_t *con,
ipp_attribute_t *uri)
{
ipp_attribute_t *attr;
ipp_attribute_t *format;
const char *default_format;
int jobid;
cupsd_job_t *job;
char job_uri[HTTP_MAX_URI],
method[HTTP_MAX_URI],
username[HTTP_MAX_URI],
host[HTTP_MAX_URI],
resource[HTTP_MAX_URI];
int port;
mime_type_t *filetype;
char super[MIME_MAX_SUPER],
type[MIME_MAX_TYPE],
mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2];
char filename[1024];
cupsd_printer_t *printer;
struct stat fileinfo;
int kbytes;
int compression;
cupsdLogMessage(CUPSD_LOG_DEBUG2, "send_document(%p[%d], %s)", con,
con->http.fd, uri->values[0].string.text);
if (!strcmp(uri->name, "printer-uri"))
{
if ((attr = ippFindAttribute(con->request, "job-id",
IPP_TAG_INTEGER)) == NULL)
{
send_ipp_status(con, IPP_BAD_REQUEST,
_("Got a printer-uri attribute but no job-id!"));
return;
}
jobid = attr->values[0].integer;
}
else
{
httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method,
sizeof(method), username, sizeof(username), host,
sizeof(host), &port, resource, sizeof(resource));
if (strncmp(resource, "/jobs/", 6))
{
send_ipp_status(con, IPP_BAD_REQUEST,
_("Bad job-uri attribute \"%s\"!"),
uri->values[0].string.text);
return;
}
jobid = atoi(resource + 6);
}
if ((job = cupsdFindJob(jobid)) == NULL)
{
send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid);
return;
}
printer = cupsdFindDest(job->dest);
if (!validate_user(job, con, job->username, username, sizeof(username)))
{
send_http_error(con, HTTP_UNAUTHORIZED, NULL);
return;
}
compression = CUPS_FILE_NONE;
if ((attr = ippFindAttribute(con->request, "compression",
IPP_TAG_KEYWORD)) != NULL)
{
if (strcmp(attr->values[0].string.text, "none")
#ifdef HAVE_LIBZ
&& strcmp(attr->values[0].string.text, "gzip")
#endif
)
{
send_ipp_status(con, IPP_ATTRIBUTES, _("Unsupported compression \"%s\"!"),
attr->values[0].string.text);
ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD,
"compression", NULL, attr->values[0].string.text);
return;
}
#ifdef HAVE_LIBZ
if (!strcmp(attr->values[0].string.text, "gzip"))
compression = CUPS_FILE_GZIP;
#endif
}
if (!con->filename)
{
send_ipp_status(con, IPP_BAD_REQUEST, _("No file!?!"));
return;
}
if ((format = ippFindAttribute(con->request, "document-format",
IPP_TAG_MIMETYPE)) != NULL)
{
if (sscanf(format->values[0].string.text, "%15[^/]/%31[^;]",
super, type) != 2)
{
send_ipp_status(con, IPP_BAD_REQUEST, _("Bad document-format \"%s\"!"),
format->values[0].string.text);
return;
}
}
else if ((default_format = cupsGetOption("document-format",
printer->num_options,
printer->options)) != NULL)
{
if (sscanf(default_format, "%15[^/]/%31[^;]", super, type) != 2)
{
send_ipp_status(con, IPP_BAD_REQUEST,
_("Could not scan type \"%s\"!"),
default_format);
return;
}
}
else
{
strcpy(super, "application");
strcpy(type, "octet-stream");
}
if (!strcmp(super, "application") && !strcmp(type, "octet-stream"))
{
ipp_attribute_t *doc_name;
cupsdLogMessage(CUPSD_LOG_DEBUG, "send_document: auto-typing file...");
doc_name = ippFindAttribute(con->request, "document-name", IPP_TAG_NAME);
filetype = mimeFileType(MimeDatabase, con->filename,
doc_name ? doc_name->values[0].string.text : NULL,
&compression);
if (!filetype)
filetype = mimeType(MimeDatabase, super, type);
}
else
filetype = mimeType(MimeDatabase, super, type);
if (filetype &&
(!format ||
(!strcmp(super, "application") && !strcmp(type, "octet-stream"))))
{
snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super,
filetype->type);
if (format)
{
_cupsStrFree(format->values[0].string.text);
format->values[0].string.text = _cupsStrAlloc(mimetype);
}
else
ippAddString(con->request, IPP_TAG_JOB, IPP_TAG_MIMETYPE,
"document-format", NULL, mimetype);
}
else if (!filetype)
{
send_ipp_status(con, IPP_DOCUMENT_FORMAT,
_("Unsupported format \'%s/%s\'!"), super, type);
cupsdLogMessage(CUPSD_LOG_INFO,
"Hint: Do you have the raw file printing rules enabled?");
if (format)
ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE,
"document-format", NULL, format->values[0].string.text);
return;
}
if (printer->filetypes && !cupsArrayFind(printer->filetypes, filetype))
{
snprintf(mimetype, sizeof(mimetype), "%s/%s", filetype->super,
filetype->type);
send_ipp_status(con, IPP_DOCUMENT_FORMAT,
_("Unsupported format \'%s\'!"), mimetype);
ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE,
"document-format", NULL, mimetype);
return;
}
cupsdLogMessage(CUPSD_LOG_DEBUG,
"send_document: request file type is %s/%s.",
filetype->super, filetype->type);
cupsdLoadJob(job);
if (add_file(con, job, filetype, compression))
return;
if (stat(con->filename, &fileinfo))
kbytes = 0;
else
kbytes = (fileinfo.st_size + 1023) / 1024;
cupsdUpdateQuota(printer, job->username, 0, kbytes);
if ((attr = ippFindAttribute(job->attrs, "job-k-octets",
IPP_TAG_INTEGER)) != NULL)
attr->values[0].integer += kbytes;
snprintf(filename, sizeof(filename), "%s/d%05d-%03d", RequestRoot, job->id,
job->num_files);
rename(con->filename, filename);
cupsdClearString(&con->filename);
cupsdLogMessage(CUPSD_LOG_INFO,
"File of type %s/%s queued in job #%d by \"%s\".",
filetype->super, filetype->type, job->id, job->username);
if ((attr = ippFindAttribute(con->request, "last-document",
IPP_TAG_BOOLEAN)) != NULL &&
attr->values[0].boolean)
{
cupsdTimeoutJob(job);
if (job->state_value == IPP_JOB_STOPPED)
{
job->state->values[0].integer = IPP_JOB_PENDING;
job->state_value = IPP_JOB_PENDING;
}
else if (job->state_value == IPP_JOB_HELD)
{
if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
IPP_TAG_KEYWORD)) == NULL)
attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
if (!attr || !strcmp(attr->values[0].string.text, "no-hold"))
{
job->state->values[0].integer = IPP_JOB_PENDING;
job->state_value = IPP_JOB_PENDING;
}
}
cupsdSaveJob(job);
jobid = job->id;
cupsdCheckJobs();
job = cupsdFindJob(jobid);
}
else
{
if ((attr = ippFindAttribute(job->attrs, "job-hold-until",
IPP_TAG_KEYWORD)) == NULL)
attr = ippFindAttribute(job->attrs, "job-hold-until", IPP_TAG_NAME);
if (!attr || !strcmp(attr->values[0].string.text, "no-hold"))
{
job->state->values[0].integer = IPP_JOB_HELD;
job->state_value = IPP_JOB_HELD;
job->hold_until = time(NULL) + 60;
cupsdSaveJob(job);
}
}
snprintf(job_uri, sizeof(job_uri), "http://%s:%d/jobs/%d", ServerName,
LocalPort, jobid);
ippAddString(con->response, IPP_TAG_JOB, IPP_TAG_URI, "job-uri", NULL,
job_uri);
ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_INTEGER, "job-id", jobid);
ippAddInteger(con->response, IPP_TAG_JOB, IPP_TAG_ENUM, "job-state",
job ? job->state_value : IPP_JOB_CANCELED);
add_job_state_reasons(con, job);
con->response->request.status.status_code = IPP_OK;
}
static void
send_http_error(
cupsd_client_t *con,
http_status_t status,
cupsd_printer_t *printer)
{
cupsdLogMessage(CUPSD_LOG_ERROR, "%s: %s",
ippOpString(con->request->request.op.operation_id),
httpStatus(status));
if (status == HTTP_UNAUTHORIZED &&
printer && printer->num_auth_info_required > 0 &&
!strcmp(printer->auth_info_required[0], "negotiate"))
cupsdSendError(con, status, AUTH_NEGOTIATE);
else
cupsdSendError(con, status, AUTH_NONE);
ippDelete(con->response);
con->response = NULL;
return;
}
static void
send_ipp_status(cupsd_client_t *con,
ipp_status_t status,
const char *message,
...)
{
va_list ap;
char formatted[1024];
va_start(ap, message);
vsnprintf(formatted, sizeof(formatted),
_cupsLangString(con->language, message), ap);
va_end(ap);
cupsdLogMessage(CUPSD_LOG_DEBUG, "%s %s: %s",
ippOpString(con->request->request.op.operation_id),
ippErrorString(status), formatted);
con->response->request.status.status_code = status;
if (ippFindAttribute(con->response, "attributes-charset",
IPP_TAG_ZERO) == NULL)
ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
"attributes-charset", NULL, DefaultCharset);
if (ippFindAttribute(con->response, "attributes-natural-language",
IPP_TAG_ZERO) == NULL)
ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
"attributes-natural-language", NULL, DefaultLanguage);
ippAddString(con->response, IPP_TAG_OPERATION, IPP_TAG_TEXT,
"status-message", NULL, formatted);
}
static void
set_default(cupsd_client_t *con,
ipp_attribute_t *uri)
{
http_status_t status;
cups_ptype_t dtype;
cupsd_printer_t *printer;
cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_default(%p[%d], %s)", con,
con->http.fd, uri->values[0].string.text);
if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
{
send_ipp_status(con, IPP_NOT_FOUND,
_("The printer or class was not found."));
return;
}
if ((status = cupsdCheckPolicy(DefaultPolicyPtr, con, NULL)) != HTTP_OK)
{
send_http_error(con, status, NULL);
return;
}
DefaultPrinter = printer;
cupsdSaveAllPrinters();
cupsdSaveAllClasses();
cupsdWritePrintcap();
cupsdLogMessage(CUPSD_LOG_INFO,
"Default destination set to \"%s\" by \"%s\".",
printer->name, get_username(con));
con->response->request.status.status_code = IPP_OK;
}
static void
set_job_attrs(cupsd_client_t *con,
ipp_attribute_t *uri)
{
ipp_attribute_t *attr,
*attr2;
int jobid;
cupsd_job_t *job;
char method[HTTP_MAX_URI],
username[HTTP_MAX_URI],
host[HTTP_MAX_URI],
resource[HTTP_MAX_URI];
int port;
int event;
cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_job_attrs(%p[%d], %s)", con,
con->http.fd, uri->values[0].string.text);
con->response->request.status.status_code = IPP_OK;
if (!strcmp(uri->name, "printer-uri"))
{
if ((attr = ippFindAttribute(con->request, "job-id",
IPP_TAG_INTEGER)) == NULL)
{
send_ipp_status(con, IPP_BAD_REQUEST,
_("Got a printer-uri attribute but no job-id!"));
return;
}
jobid = attr->values[0].integer;
}
else
{
httpSeparateURI(HTTP_URI_CODING_ALL, uri->values[0].string.text, method,
sizeof(method), username, sizeof(username), host,
sizeof(host), &port, resource, sizeof(resource));
if (strncmp(resource, "/jobs/", 6))
{
send_ipp_status(con, IPP_BAD_REQUEST,
_("Bad job-uri attribute \"%s\"!"),
uri->values[0].string.text);
return;
}
jobid = atoi(resource + 6);
}
if ((job = cupsdFindJob(jobid)) == NULL)
{
send_ipp_status(con, IPP_NOT_FOUND, _("Job #%d does not exist!"), jobid);
return;
}
if (job->state_value > IPP_JOB_STOPPED)
{
send_ipp_status(con, IPP_NOT_POSSIBLE,
_("Job #%d is finished and cannot be altered!"), jobid);
return;
}
if (!validate_user(job, con, job->username, username, sizeof(username)))
{
send_http_error(con, HTTP_UNAUTHORIZED, NULL);
return;
}
cupsdLoadJob(job);
event = 0;
for (attr = con->request->attrs; attr; attr = attr->next)
{
if (attr->group_tag != IPP_TAG_JOB || !attr->name)
continue;
if (!strcmp(attr->name, "attributes-charset") ||
!strcmp(attr->name, "attributes-natural-language") ||
!strcmp(attr->name, "document-compression") ||
!strcmp(attr->name, "document-format") ||
!strcmp(attr->name, "job-detailed-status-messages") ||
!strcmp(attr->name, "job-document-access-errors") ||
!strcmp(attr->name, "job-id") ||
!strcmp(attr->name, "job-k-octets") ||
!strcmp(attr->name, "job-originating-host-name") ||
!strcmp(attr->name, "job-originating-user-name") ||
!strcmp(attr->name, "job-printer-up-time") ||
!strcmp(attr->name, "job-printer-uri") ||
!strcmp(attr->name, "job-sheets") ||
!strcmp(attr->name, "job-state-message") ||
!strcmp(attr->name, "job-state-reasons") ||
!strcmp(attr->name, "job-uri") ||
!strcmp(attr->name, "number-of-documents") ||
!strcmp(attr->name, "number-of-intervening-jobs") ||
!strcmp(attr->name, "output-device-assigned") ||
!strncmp(attr->name, "date-time-at-", 13) ||
!strncmp(attr->name, "job-impressions", 15) ||
!strncmp(attr->name, "job-k-octets", 12) ||
!strncmp(attr->name, "job-media-sheets", 16) ||
!strncmp(attr->name, "time-at-", 8))
{
send_ipp_status(con, IPP_ATTRIBUTES_NOT_SETTABLE,
_("%s cannot be changed."), attr->name);
if ((attr2 = copy_attribute(con->response, attr, 0)) != NULL)
attr2->group_tag = IPP_TAG_UNSUPPORTED_GROUP;
continue;
}
if (!strcmp(attr->name, "job-priority"))
{
if (attr->value_tag != IPP_TAG_INTEGER)
{
send_ipp_status(con, IPP_REQUEST_VALUE, _("Bad job-priority value!"));
if ((attr2 = copy_attribute(con->response, attr, 0)) != NULL)
attr2->group_tag = IPP_TAG_UNSUPPORTED_GROUP;
}
else if (job->state_value >= IPP_JOB_PROCESSING)
{
send_ipp_status(con, IPP_NOT_POSSIBLE,
_("Job is completed and cannot be changed."));
return;
}
else if (con->response->request.status.status_code == IPP_OK)
{
cupsdSetJobPriority(job, attr->values[0].integer);
event |= CUPSD_EVENT_JOB_CONFIG_CHANGED;
}
}
else if (!strcmp(attr->name, "job-state"))
{
if (attr->value_tag != IPP_TAG_ENUM)
{
send_ipp_status(con, IPP_REQUEST_VALUE, _("Bad job-state value!"));
if ((attr2 = copy_attribute(con->response, attr, 0)) != NULL)
attr2->group_tag = IPP_TAG_UNSUPPORTED_GROUP;
}
else
{
switch (attr->values[0].integer)
{
case IPP_JOB_PENDING :
case IPP_JOB_HELD :
if (job->state_value > IPP_JOB_HELD)
{
send_ipp_status(con, IPP_NOT_POSSIBLE,
_("Job state cannot be changed."));
return;
}
else if (con->response->request.status.status_code == IPP_OK)
{
job->state->values[0].integer = attr->values[0].integer;
job->state_value = (ipp_jstate_t)attr->values[0].integer;
event |= CUPSD_EVENT_JOB_STATE;
}
break;
case IPP_JOB_PROCESSING :
case IPP_JOB_STOPPED :
if (job->state_value != attr->values[0].integer)
{
send_ipp_status(con, IPP_NOT_POSSIBLE,
_("Job state cannot be changed."));
return;
}
break;
case IPP_JOB_CANCELED :
case IPP_JOB_ABORTED :
case IPP_JOB_COMPLETED :
if (job->state_value > IPP_JOB_PROCESSING)
{
send_ipp_status(con, IPP_NOT_POSSIBLE,
_("Job state cannot be changed."));
return;
}
else if (con->response->request.status.status_code == IPP_OK)
cupsdCancelJob(job, 0, (ipp_jstate_t)attr->values[0].integer);
break;
}
}
}
else if (con->response->request.status.status_code != IPP_OK)
continue;
else if ((attr2 = ippFindAttribute(job->attrs, attr->name,
IPP_TAG_ZERO)) != NULL)
{
if (job->attrs->prev)
job->attrs->prev->next = attr2->next;
else
job->attrs->attrs = attr2->next;
if (job->attrs->last == attr2)
job->attrs->last = job->attrs->prev;
_ippFreeAttr(attr2);
copy_attribute(job->attrs, attr, 0);
if (!strcmp(attr->name, "job-hold-until"))
{
cupsdSetJobHoldUntil(job, attr->values[0].string.text);
if (!strcmp(attr->values[0].string.text, "no-hold"))
cupsdReleaseJob(job);
else
cupsdHoldJob(job);
event |= CUPSD_EVENT_JOB_CONFIG_CHANGED | CUPSD_EVENT_JOB_STATE;
}
}
else if (attr->value_tag == IPP_TAG_DELETEATTR)
{
if ((attr2 = ippFindAttribute(job->attrs, attr->name,
IPP_TAG_ZERO)) != NULL)
{
if (job->attrs->prev)
job->attrs->prev->next = attr2->next;
else
job->attrs->attrs = attr2->next;
if (attr2 == job->attrs->last)
job->attrs->last = job->attrs->prev;
_ippFreeAttr(attr2);
event |= CUPSD_EVENT_JOB_CONFIG_CHANGED;
}
}
else
{
copy_attribute(job->attrs, attr, 0);
event |= CUPSD_EVENT_JOB_CONFIG_CHANGED;
}
}
cupsdSaveJob(job);
if (event & CUPSD_EVENT_JOB_STATE)
cupsdAddEvent(CUPSD_EVENT_JOB_STATE, job->printer, job,
job->state_value == IPP_JOB_HELD ?
"Job held by user." : "Job restarted by user.");
if (event & CUPSD_EVENT_JOB_CONFIG_CHANGED)
cupsdAddEvent(CUPSD_EVENT_JOB_CONFIG_CHANGED, job->printer, job,
"Job options changed by user.");
cupsdCheckJobs();
}
static void
set_printer_defaults(
cupsd_client_t *con,
cupsd_printer_t *printer)
{
int i;
ipp_attribute_t *attr;
int namelen;
char name[256],
value[256];
for (attr = con->request->attrs; attr; attr = attr->next)
{
if (attr->group_tag != IPP_TAG_PRINTER || !attr->name)
continue;
cupsdLogMessage(CUPSD_LOG_DEBUG2, "set_printer_defaults: %s", attr->name);
if (!strcmp(attr->name, "job-sheets-default"))
{
if (attr->value_tag != IPP_TAG_NAME && attr->value_tag != IPP_TAG_KEYWORD)
continue;
if (Classification)
continue;
cupsdSetString(&printer->job_sheets[0], attr->values[0].string.text);
if (attr->num_values > 1)
cupsdSetString(&printer->job_sheets[1], attr->values[1].string.text);
else
cupsdSetString(&printer->job_sheets[1], "none");
}
else if (!strcmp(attr->name, "requesting-user-name-allowed"))
{
cupsdFreePrinterUsers(printer);
printer->deny_users = 0;
if (attr->value_tag == IPP_TAG_NAME &&
(attr->num_values > 1 ||
strcmp(attr->values[0].string.text, "all")))
{
for (i = 0; i < attr->num_values; i ++)
cupsdAddPrinterUser(printer, attr->values[i].string.text);
}
}
else if (!strcmp(attr->name, "requesting-user-name-denied"))
{
cupsdFreePrinterUsers(printer);
printer->deny_users = 1;
if (attr->value_tag == IPP_TAG_NAME &&
(attr->num_values > 1 ||
strcmp(attr->values[0].string.text, "none")))
{
for (i = 0; i < attr->num_values; i ++)
cupsdAddPrinterUser(printer, attr->values[i].string.text);
}
}
else if (!strcmp(attr->name, "job-quota-period"))
{
if (attr->value_tag != IPP_TAG_INTEGER)
continue;
cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-quota-period to %d...",
attr->values[0].integer);
cupsdFreeQuotas(printer);
printer->quota_period = attr->values[0].integer;
}
else if (!strcmp(attr->name, "job-k-limit"))
{
if (attr->value_tag != IPP_TAG_INTEGER)
continue;
cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-k-limit to %d...",
attr->values[0].integer);
cupsdFreeQuotas(printer);
printer->k_limit = attr->values[0].integer;
}
else if (!strcmp(attr->name, "job-page-limit"))
{
if (attr->value_tag != IPP_TAG_INTEGER)
continue;
cupsdLogMessage(CUPSD_LOG_DEBUG, "Setting job-page-limit to %d...",
attr->values[0].integer);
cupsdFreeQuotas(printer);
printer->page_limit = attr->values[0].integer;
}
else if (!strcmp(attr->name, "printer-op-policy"))
{
cupsd_policy_t *p;
if (attr->value_tag != IPP_TAG_NAME)
continue;
if ((p = cupsdFindPolicy(attr->values[0].string.text)) != NULL)
{
cupsdLogMessage(CUPSD_LOG_DEBUG,
"Setting printer-op-policy to \"%s\"...",
attr->values[0].string.text);
cupsdSetString(&printer->op_policy, attr->values[0].string.text);
printer->op_policy_ptr = p;
}
else
{
send_ipp_status(con, IPP_NOT_POSSIBLE,
_("Unknown printer-op-policy \"%s\"."),
attr->values[0].string.text);
return;
}
}
else if (!strcmp(attr->name, "printer-error-policy"))
{
if (attr->value_tag != IPP_TAG_NAME && attr->value_tag != IPP_TAG_KEYWORD)
continue;
if (strcmp(attr->values[0].string.text, "abort-job") &&
strcmp(attr->values[0].string.text, "retry-job") &&
strcmp(attr->values[0].string.text, "stop-printer"))
{
send_ipp_status(con, IPP_NOT_POSSIBLE,
_("Unknown printer-error-policy \"%s\"."),
attr->values[0].string.text);
return;
}
cupsdLogMessage(CUPSD_LOG_DEBUG,
"Setting printer-error-policy to \"%s\"...",
attr->values[0].string.text);
cupsdSetString(&printer->error_policy, attr->values[0].string.text);
}
namelen = strlen(attr->name);
if (namelen < 9 || strcmp(attr->name + namelen - 8, "-default") ||
namelen > (sizeof(name) - 1) || attr->num_values != 1)
continue;
strlcpy(name, attr->name, sizeof(name));
name[namelen - 8] = '\0';
switch (attr->value_tag)
{
case IPP_TAG_DELETEATTR :
printer->num_options = cupsRemoveOption(name,
printer->num_options,
&(printer->options));
cupsdLogMessage(CUPSD_LOG_DEBUG,
"Deleting %s", attr->name);
break;
case IPP_TAG_NAME :
case IPP_TAG_KEYWORD :
case IPP_TAG_URI :
printer->num_options = cupsAddOption(name,
attr->values[0].string.text,
printer->num_options,
&(printer->options));
cupsdLogMessage(CUPSD_LOG_DEBUG,
"Setting %s to \"%s\"...", attr->name,
attr->values[0].string.text);
break;
case IPP_TAG_BOOLEAN :
printer->num_options = cupsAddOption(name,
attr->values[0].boolean ?
"true" : "false",
printer->num_options,
&(printer->options));
cupsdLogMessage(CUPSD_LOG_DEBUG,
"Setting %s to %s...", attr->name,
attr->values[0].boolean ? "true" : "false");
break;
case IPP_TAG_INTEGER :
case IPP_TAG_ENUM :
sprintf(value, "%d", attr->values[0].integer);
printer->num_options = cupsAddOption(name, value,
printer->num_options,
&(printer->options));
cupsdLogMessage(CUPSD_LOG_DEBUG,
"Setting %s to %s...", attr->name, value);
break;
case IPP_TAG_RANGE :
sprintf(value, "%d-%d", attr->values[0].range.lower,
attr->values[0].range.upper);
printer->num_options = cupsAddOption(name, value,
printer->num_options,
&(printer->options));
cupsdLogMessage(CUPSD_LOG_DEBUG,
"Setting %s to %s...", attr->name, value);
break;
case IPP_TAG_RESOLUTION :
sprintf(value, "%dx%d%s", attr->values[0].resolution.xres,
attr->values[0].resolution.yres,
attr->values[0].resolution.units == IPP_RES_PER_INCH ?
"dpi" : "dpc");
printer->num_options = cupsAddOption(name, value,
printer->num_options,
&(printer->options));
cupsdLogMessage(CUPSD_LOG_DEBUG,
"Setting %s to %s...", attr->name, value);
break;
default :
break;
}
}
}
static void
start_printer(cupsd_client_t *con,
ipp_attribute_t *uri)
{
http_status_t status;
cups_ptype_t dtype;
cupsd_printer_t *printer;
cupsdLogMessage(CUPSD_LOG_DEBUG2, "start_printer(%p[%d], %s)", con,
con->http.fd, uri->values[0].string.text);
if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
{
send_ipp_status(con, IPP_NOT_FOUND,
_("The printer or class was not found."));
return;
}
if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
{
send_http_error(con, status, printer);
return;
}
printer->state_message[0] = '\0';
cupsdStartPrinter(printer, 1);
if (dtype & CUPS_PRINTER_CLASS)
cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" started by \"%s\".",
printer->name, get_username(con));
else
cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" started by \"%s\".",
printer->name, get_username(con));
cupsdCheckJobs();
con->response->request.status.status_code = IPP_OK;
}
static void
stop_printer(cupsd_client_t *con,
ipp_attribute_t *uri)
{
http_status_t status;
cups_ptype_t dtype;
cupsd_printer_t *printer;
ipp_attribute_t *attr;
cupsdLogMessage(CUPSD_LOG_DEBUG2, "stop_printer(%p[%d], %s)", con,
con->http.fd, uri->values[0].string.text);
if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
{
send_ipp_status(con, IPP_NOT_FOUND,
_("The printer or class was not found."));
return;
}
if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
{
send_http_error(con, status, printer);
return;
}
if ((attr = ippFindAttribute(con->request, "printer-state-message",
IPP_TAG_TEXT)) == NULL)
strcpy(printer->state_message, "Paused");
else
{
strlcpy(printer->state_message, attr->values[0].string.text,
sizeof(printer->state_message));
}
cupsdStopPrinter(printer, 1);
if (dtype & CUPS_PRINTER_CLASS)
cupsdLogMessage(CUPSD_LOG_INFO, "Class \"%s\" stopped by \"%s\".",
printer->name, get_username(con));
else
cupsdLogMessage(CUPSD_LOG_INFO, "Printer \"%s\" stopped by \"%s\".",
printer->name, get_username(con));
con->response->request.status.status_code = IPP_OK;
}
static void
url_encode_attr(ipp_attribute_t *attr,
char *buffer,
int bufsize)
{
int i;
char *bufptr,
*bufend;
strlcpy(buffer, attr->name, bufsize);
bufptr = buffer + strlen(buffer);
bufend = buffer + bufsize - 1;
for (i = 0; i < attr->num_values; i ++)
{
if (bufptr >= bufend)
break;
if (i)
*bufptr++ = ',';
else
*bufptr++ = '=';
if (bufptr >= bufend)
break;
*bufptr++ = '\'';
bufptr = url_encode_string(attr->values[i].string.text,
bufptr, bufend - bufptr + 1);
if (bufptr >= bufend)
break;
*bufptr++ = '\'';
}
*bufptr = '\0';
}
static char *
url_encode_string(const char *s,
char *buffer,
int bufsize)
{
char *bufptr,
*bufend;
static const char *hex = "0123456789ABCDEF";
bufptr = buffer;
bufend = buffer + bufsize - 1;
while (*s && bufptr < bufend)
{
if (*s == ' ' || *s == '%')
{
if (bufptr >= (bufend - 2))
break;
*bufptr++ = '%';
*bufptr++ = hex[(*s >> 4) & 15];
*bufptr++ = hex[*s & 15];
s ++;
}
else if (*s == '\'' || *s == '\\')
{
if (bufptr >= (bufend - 1))
break;
*bufptr++ = '\\';
*bufptr++ = *s++;
}
else
*bufptr++ = *s++;
}
*bufptr = '\0';
return (bufptr);
}
static int
user_allowed(cupsd_printer_t *p,
const char *username)
{
int i;
struct passwd *pw;
if (p->num_users == 0)
return (1);
if (!strcmp(username, "root"))
return (1);
pw = getpwnam(username);
endpwent();
for (i = 0; i < p->num_users; i ++)
{
if (p->users[i][0] == '@')
{
if (cupsdCheckGroup(username, pw, p->users[i] + 1))
break;
}
else if (!strcasecmp(username, p->users[i]))
break;
}
return ((i < p->num_users) != p->deny_users);
}
static void
validate_job(cupsd_client_t *con,
ipp_attribute_t *uri)
{
http_status_t status;
ipp_attribute_t *attr;
ipp_attribute_t *format;
cups_ptype_t dtype;
char super[MIME_MAX_SUPER],
type[MIME_MAX_TYPE];
cupsd_printer_t *printer;
cupsdLogMessage(CUPSD_LOG_DEBUG2, "validate_job(%p[%d], %s)", con,
con->http.fd, uri->values[0].string.text);
if ((attr = ippFindAttribute(con->request, "compression",
IPP_TAG_KEYWORD)) != NULL &&
!strcmp(attr->values[0].string.text, "none"))
{
send_ipp_status(con, IPP_ATTRIBUTES,
_("Unsupported compression attribute %s!"),
attr->values[0].string.text);
ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_KEYWORD,
"compression", NULL, attr->values[0].string.text);
return;
}
if ((format = ippFindAttribute(con->request, "document-format",
IPP_TAG_MIMETYPE)) != NULL)
{
if (sscanf(format->values[0].string.text, "%15[^/]/%31[^;]",
super, type) != 2)
{
send_ipp_status(con, IPP_BAD_REQUEST, _("Bad document-format \"%s\"!"),
format->values[0].string.text);
return;
}
if ((strcmp(super, "application") || strcmp(type, "octet-stream")) &&
!mimeType(MimeDatabase, super, type))
{
cupsdLogMessage(CUPSD_LOG_INFO,
"Hint: Do you have the raw file printing rules enabled?");
send_ipp_status(con, IPP_DOCUMENT_FORMAT,
_("Unsupported format \"%s\"!"),
format->values[0].string.text);
ippAddString(con->response, IPP_TAG_UNSUPPORTED_GROUP, IPP_TAG_MIMETYPE,
"document-format", NULL, format->values[0].string.text);
return;
}
}
if (!cupsdValidateDest(uri->values[0].string.text, &dtype, &printer))
{
send_ipp_status(con, IPP_NOT_FOUND,
_("The printer or class was not found."));
return;
}
if ((status = cupsdCheckPolicy(printer->op_policy_ptr, con, NULL)) != HTTP_OK)
{
send_http_error(con, status, printer);
return;
}
con->response->request.status.status_code = IPP_OK;
}
static int
validate_name(const char *name)
{
const char *ptr;
for (ptr = name; *ptr; ptr ++)
if ((*ptr > 0 && *ptr <= ' ') || *ptr == 127 || *ptr == '/' || *ptr == '#')
return (0);
return ((ptr - name) < 128);
}
static int
validate_user(cupsd_job_t *job,
cupsd_client_t *con,
const char *owner,
char *username,
int userlen)
{
cupsd_printer_t *printer;
cupsdLogMessage(CUPSD_LOG_DEBUG2,
"validate_user(job=%d, con=%d, owner=\"%s\", username=%p, "
"userlen=%d)",
job ? job->id : 0, con->http.fd, owner ? owner : "(null)",
username, userlen);
if (!con || !owner || !username || userlen <= 0)
return (0);
strlcpy(username, get_username(con), userlen);
printer = cupsdFindDest(job->dest);
return (cupsdCheckPolicy(printer ? printer->op_policy_ptr : DefaultPolicyPtr,
con, owner) == HTTP_OK);
}