#include "cupsd.h"
#include <cups/dir.h>
static void add_printer_defaults(cupsd_printer_t *p);
static void add_printer_filter(cupsd_printer_t *p, mime_type_t *type,
const char *filter);
static void add_printer_formats(cupsd_printer_t *p);
static int compare_printers(void *first, void *second, void *data);
static void delete_printer_filters(cupsd_printer_t *p);
#ifdef __sgi
static void write_irix_config(cupsd_printer_t *p);
static void write_irix_state(cupsd_printer_t *p);
#endif
cupsd_printer_t *
cupsdAddPrinter(const char *name)
{
cupsd_printer_t *p;
cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdAddPrinter(\"%s\")", name);
if ((p = calloc(1, sizeof(cupsd_printer_t))) == NULL)
{
cupsdLogMessage(CUPSD_LOG_CRIT, "Unable to allocate memory for printer - %s",
strerror(errno));
return (NULL);
}
cupsdSetString(&p->name, name);
cupsdSetString(&p->info, name);
cupsdSetString(&p->hostname, ServerName);
cupsdSetStringf(&p->uri, "ipp://%s:%d/printers/%s", ServerName, LocalPort, name);
cupsdSetStringf(&p->device_uri, "file:/dev/null");
p->state = IPP_PRINTER_STOPPED;
p->state_time = time(NULL);
p->accepting = 0;
p->shared = DefaultShared;
p->filetype = mimeAddType(MimeDatabase, "printer", name);
cupsdSetString(&p->job_sheets[0], "none");
cupsdSetString(&p->job_sheets[1], "none");
cupsdSetString(&p->error_policy, ErrorPolicy);
cupsdSetString(&p->op_policy, DefaultPolicy);
p->op_policy_ptr = DefaultPolicyPtr;
if (MaxPrinterHistory)
p->history = calloc(MaxPrinterHistory, sizeof(ipp_t *));
if (!Printers)
Printers = cupsArrayNew(compare_printers, NULL);
cupsdLogMessage(CUPSD_LOG_DEBUG2,
"cupsdAddPrinter: Adding %s to Printers", p->name);
cupsArrayAdd(Printers, p);
if (!ImplicitPrinters)
ImplicitPrinters = cupsArrayNew(compare_printers, NULL);
return (p);
}
void
cupsdAddPrinterHistory(
cupsd_printer_t *p)
{
ipp_t *history;
if (MaxPrinterHistory <= 0)
return;
p->sequence_number ++;
if (p->num_history >= MaxPrinterHistory)
{
p->num_history --;
ippDelete(p->history[0]);
memmove(p->history, p->history + 1, p->num_history * sizeof(ipp_t *));
}
history = ippNew();
ippAddInteger(history, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-state",
p->state);
ippAddBoolean(history, IPP_TAG_PRINTER, "printer-is-accepting-jobs",
p->accepting);
ippAddBoolean(history, IPP_TAG_PRINTER, "printer-is-shared", p->shared);
ippAddString(history, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-state-message",
NULL, p->state_message);
#ifdef __APPLE__
if (p->recoverable)
ippAddString(history, IPP_TAG_PRINTER, IPP_TAG_TEXT,
"com.apple.print.recoverable-message", NULL, p->recoverable);
#endif
if (p->num_reasons == 0)
ippAddString(history, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
"printer-state-reasons", NULL,
p->state == IPP_PRINTER_STOPPED ? "paused" : "none");
else
ippAddStrings(history, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
"printer-state-reasons", p->num_reasons, NULL,
(const char * const *)p->reasons);
ippAddInteger(history, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
"printer-state-change-time", p->state_time);
ippAddInteger(history, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
"printer-state-sequence-number", p->sequence_number);
p->history[p->num_history] = history;
p->num_history ++;
}
void
cupsdAddPrinterUser(
cupsd_printer_t *p,
const char *username)
{
const char **temp;
if (!p || !username)
return;
if (p->num_users == 0)
temp = malloc(sizeof(char **));
else
temp = realloc(p->users, sizeof(char **) * (p->num_users + 1));
if (!temp)
return;
p->users = temp;
temp += p->num_users;
if ((*temp = strdup(username)) != NULL)
p->num_users ++;
}
void
cupsdCreateCommonData(void)
{
int i;
ipp_attribute_t *attr;
cups_dir_t *dir;
cups_dentry_t *dent;
cups_array_t *notifiers;
char filename[1024],
*notifier;
static const int nups[] =
{ 1, 2, 4, 6, 9, 16 };
static const int orients[4] =
{
IPP_PORTRAIT,
IPP_LANDSCAPE,
IPP_REVERSE_LANDSCAPE,
IPP_REVERSE_PORTRAIT
};
static const char * const holds[] =
{
"no-hold",
"indefinite",
"day-time",
"evening",
"night",
"second-shift",
"third-shift",
"weekend"
};
static const char * const versions[] =
{
"1.0",
"1.1"
};
static const int ops[] =
{
IPP_PRINT_JOB,
IPP_VALIDATE_JOB,
IPP_CREATE_JOB,
IPP_SEND_DOCUMENT,
IPP_CANCEL_JOB,
IPP_GET_JOB_ATTRIBUTES,
IPP_GET_JOBS,
IPP_GET_PRINTER_ATTRIBUTES,
IPP_HOLD_JOB,
IPP_RELEASE_JOB,
IPP_PAUSE_PRINTER,
IPP_RESUME_PRINTER,
IPP_PURGE_JOBS,
IPP_SET_JOB_ATTRIBUTES,
IPP_CREATE_PRINTER_SUBSCRIPTION,
IPP_CREATE_JOB_SUBSCRIPTION,
IPP_GET_SUBSCRIPTION_ATTRIBUTES,
IPP_GET_SUBSCRIPTIONS,
IPP_RENEW_SUBSCRIPTION,
IPP_CANCEL_SUBSCRIPTION,
IPP_GET_NOTIFICATIONS,
IPP_ENABLE_PRINTER,
IPP_DISABLE_PRINTER,
CUPS_GET_DEFAULT,
CUPS_GET_PRINTERS,
CUPS_ADD_PRINTER,
CUPS_DELETE_PRINTER,
CUPS_GET_CLASSES,
CUPS_ADD_CLASS,
CUPS_DELETE_CLASS,
CUPS_ACCEPT_JOBS,
CUPS_REJECT_JOBS,
CUPS_SET_DEFAULT,
CUPS_GET_DEVICES,
CUPS_GET_PPDS,
CUPS_MOVE_JOB,
CUPS_AUTHENTICATE_JOB,
IPP_RESTART_JOB
};
static const char * const charsets[] =
{
"us-ascii",
"utf-8"
};
static const char * const compressions[] =
{
"none"
#ifdef HAVE_LIBZ
,"gzip"
#endif
};
static const char * const multiple_document_handling[] =
{
"separate-documents-uncollated-copies",
"separate-documents-collated-copies"
};
static const char * const errors[] =
{
"abort-job",
"retry-job",
"stop-printer"
};
static const char * const notify_attrs[] =
{
"printer-state-change-time",
"notify-lease-expiration-time",
"notify-subscriber-user-name"
};
static const char * const notify_events[] =
{
"job-completed",
"job-config-changed",
"job-created",
"job-progress",
"job-state-changed",
"job-stopped",
"printer-added",
"printer-changed",
"printer-config-changed",
"printer-deleted",
"printer-finishings-changed",
"printer-media-changed",
"printer-modified",
"printer-restarted",
"printer-shutdown",
"printer-state-changed",
"printer-stopped",
"server-audit",
"server-restarted",
"server-started",
"server-stopped"
};
if (CommonData)
ippDelete(CommonData);
CommonData = ippNew();
ippAddString(CommonData, IPP_TAG_PRINTER, IPP_TAG_CHARSET,
"charset-configured", NULL, DefaultCharset);
ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_CHARSET,
"charset-supported", sizeof(charsets) / sizeof(charsets[0]),
NULL, charsets);
ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
"compression-supported",
sizeof(compressions) / sizeof(compressions[0]),
NULL, compressions);
ippAddRange(CommonData, IPP_TAG_PRINTER, "copies-supported", 1, MaxCopies);
ippAddString(CommonData, IPP_TAG_PRINTER, IPP_TAG_TEXT, "cups-version",
NULL, CUPS_SVERSION + 6);
ippAddString(CommonData, IPP_TAG_PRINTER, IPP_TAG_LANGUAGE,
"generated-natural-language-supported", NULL, DefaultLanguage);
ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
"ipp-versions-supported", sizeof(versions) / sizeof(versions[0]),
NULL, versions);
ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
"job-hold-until-supported", sizeof(holds) / sizeof(holds[0]),
NULL, holds);
ippAddInteger(CommonData, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
"job-priority-supported", 100);
if (cupsArrayCount(Banners) > 0)
{
if (Classification && !ClassifyOverride)
attr = ippAddString(CommonData, IPP_TAG_PRINTER, IPP_TAG_NAME,
"job-sheets-supported", NULL, Classification);
else
attr = ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_NAME,
"job-sheets-supported", cupsArrayCount(Banners) + 1,
NULL, NULL);
if (attr == NULL)
cupsdLogMessage(CUPSD_LOG_EMERG,
"Unable to allocate memory for "
"job-sheets-supported attribute: %s!", strerror(errno));
else if (!Classification || ClassifyOverride)
{
cupsd_banner_t *banner;
attr->values[0].string.text = _cupsStrAlloc("none");
for (i = 1, banner = (cupsd_banner_t *)cupsArrayFirst(Banners);
banner;
i ++, banner = (cupsd_banner_t *)cupsArrayNext(Banners))
attr->values[i].string.text = _cupsStrAlloc(banner->name);
}
}
else
ippAddString(CommonData, IPP_TAG_PRINTER, IPP_TAG_NAME,
"job-sheets-supported", NULL, "none");
ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
"multiple-document-handling-supported",
sizeof(multiple_document_handling) /
sizeof(multiple_document_handling[0]), NULL,
multiple_document_handling);
ippAddBoolean(CommonData, IPP_TAG_PRINTER,
"multiple-document-jobs-supported", 1);
ippAddInteger(CommonData, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
"multiple-operation-time-out", 60);
ippAddString(CommonData, IPP_TAG_PRINTER, IPP_TAG_LANGUAGE,
"natural-language-configured", NULL, DefaultLanguage);
ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
"notify-attributes-supported",
(int)(sizeof(notify_attrs) / sizeof(notify_attrs[0])),
NULL, notify_attrs);
ippAddRange(CommonData, IPP_TAG_PRINTER,
"notify-lease-duration-supported", 0,
MaxLeaseDuration ? MaxLeaseDuration : 2147483647);
ippAddInteger(CommonData, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
"notify-max-events-supported", MaxEvents);
ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
"notify-events-supported",
(int)(sizeof(notify_events) / sizeof(notify_events[0])),
NULL, notify_events);
ippAddString(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
"notify-pull-method-supported", NULL, "ippget");
snprintf(filename, sizeof(filename), "%s/notifier", ServerBin);
if ((dir = cupsDirOpen(filename)) != NULL)
{
notifiers = cupsArrayNew((cups_array_func_t)strcmp, NULL);
while ((dent = cupsDirRead(dir)) != NULL)
if (S_ISREG(dent->fileinfo.st_mode) &&
(dent->fileinfo.st_mode & S_IXOTH) != 0)
cupsArrayAdd(notifiers, _cupsStrAlloc(dent->filename));
if (cupsArrayCount(notifiers) > 0)
{
attr = ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
"notify-schemes-supported",
cupsArrayCount(notifiers), NULL, NULL);
for (i = 0, notifier = (char *)cupsArrayFirst(notifiers);
notifier;
i ++, notifier = (char *)cupsArrayNext(notifiers))
attr->values[i].string.text = notifier;
}
cupsArrayDelete(notifiers);
cupsDirClose(dir);
}
ippAddIntegers(CommonData, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
"number-up-supported", sizeof(nups) / sizeof(nups[0]), nups);
ippAddIntegers(CommonData, IPP_TAG_PRINTER, IPP_TAG_ENUM,
"operations-supported",
sizeof(ops) / sizeof(ops[0]) + JobFiles - 1, ops);
ippAddIntegers(CommonData, IPP_TAG_PRINTER, IPP_TAG_ENUM,
"orientation-requested-supported", 4, orients);
ippAddBoolean(CommonData, IPP_TAG_PRINTER, "page-ranges-supported", 1);
ippAddString(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
"pdl-override-supported", NULL, "not-attempted");
ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_NAME,
"printer-error-policy-supported",
sizeof(errors) / sizeof(errors[0]), NULL, errors);
attr = ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_NAME,
"printer-op-policy-supported", NumPolicies, NULL, NULL);
for (i = 0; i < NumPolicies; i ++)
attr->values[i].string.text = _cupsStrAlloc(Policies[i]->name);
}
void
cupsdDeleteAllPrinters(void)
{
cupsd_printer_t *p;
for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
p;
p = (cupsd_printer_t *)cupsArrayNext(Printers))
if (!(p->type & CUPS_PRINTER_CLASS))
cupsdDeletePrinter(p, 0);
}
void
cupsdDeletePrinter(
cupsd_printer_t *p,
int update)
{
int i;
#ifdef __sgi
char filename[1024];
#endif
cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdDeletePrinter(p=%p(%s), update=%d)",
p, p->name, update);
cupsArraySave(Printers);
cupsdStopPrinter(p, update);
if (p == BrowseNext)
{
cupsArrayFind(Printers, p);
BrowseNext = (cupsd_printer_t *)cupsArrayNext(Printers);
}
cupsdLogMessage(CUPSD_LOG_DEBUG2,
"cupsdDeletePrinter: Removing %s from Printers", p->name);
cupsArrayRemove(Printers, p);
if (p->type & CUPS_PRINTER_IMPLICIT)
{
cupsdLogMessage(CUPSD_LOG_DEBUG2,
"cupsdDeletePrinter: Removing %s from ImplicitPrinters",
p->name);
cupsArrayRemove(ImplicitPrinters, p);
}
#ifdef __sgi
snprintf(filename, sizeof(filename), "/var/spool/lp/interface/%s", p->name);
unlink(filename);
snprintf(filename, sizeof(filename), "/var/spool/lp/gui_interface/ELF/%s.gui",
p->name);
unlink(filename);
snprintf(filename, sizeof(filename), "/var/spool/lp/activeicons/%s", p->name);
unlink(filename);
snprintf(filename, sizeof(filename), "/var/spool/lp/pod/%s.config", p->name);
unlink(filename);
snprintf(filename, sizeof(filename), "/var/spool/lp/pod/%s.status", p->name);
unlink(filename);
snprintf(filename, sizeof(filename), "/var/spool/lp/member/%s", p->name);
unlink(filename);
#endif
if (p == DefaultPrinter)
{
DefaultPrinter = NULL;
if (UseNetworkDefault)
{
cupsd_printer_t *dp;
for (dp = (cupsd_printer_t *)cupsArrayFirst(Printers);
dp;
dp = (cupsd_printer_t *)cupsArrayNext(Printers))
if (dp != p && (dp->type & CUPS_PRINTER_DEFAULT))
{
DefaultPrinter = dp;
break;
}
}
}
if (!(p->type & CUPS_PRINTER_IMPLICIT))
{
cupsdDeletePrinterFromClasses(p);
cupsdDeregisterPrinter(p, 1);
}
if (p->printers != NULL)
free(p->printers);
if (MaxPrinterHistory)
{
for (i = 0; i < p->num_history; i ++)
ippDelete(p->history[i]);
free(p->history);
}
for (i = 0; i < p->num_reasons; i ++)
free(p->reasons[i]);
ippDelete(p->attrs);
delete_printer_filters(p);
mimeDeleteType(MimeDatabase, p->filetype);
mimeDeleteType(MimeDatabase, p->prefiltertype);
cupsdFreePrinterUsers(p);
cupsdFreeQuotas(p);
cupsdClearString(&p->uri);
cupsdClearString(&p->hostname);
cupsdClearString(&p->name);
cupsdClearString(&p->location);
cupsdClearString(&p->make_model);
cupsdClearString(&p->info);
cupsdClearString(&p->job_sheets[0]);
cupsdClearString(&p->job_sheets[1]);
cupsdClearString(&p->device_uri);
cupsdClearString(&p->port_monitor);
cupsdClearString(&p->op_policy);
cupsdClearString(&p->error_policy);
cupsdClearString(&p->alert);
cupsdClearString(&p->alert_description);
#ifdef HAVE_DNSSD
cupsdClearString(&p->product);
cupsdClearString(&p->pdl);
#endif
cupsArrayDelete(p->filetypes);
if (p->browse_attrs)
free(p->browse_attrs);
#ifdef __APPLE__
cupsdClearString(&p->recoverable);
#endif
cupsFreeOptions(p->num_options, p->options);
free(p);
cupsArrayRestore(Printers);
}
cupsd_printer_t *
cupsdFindDest(const char *name)
{
cupsd_printer_t key;
key.name = (char *)name;
return ((cupsd_printer_t *)cupsArrayFind(Printers, &key));
}
cupsd_printer_t *
cupsdFindPrinter(const char *name)
{
cupsd_printer_t *p;
if ((p = cupsdFindDest(name)) != NULL && (p->type & CUPS_PRINTER_CLASS))
return (NULL);
else
return (p);
}
void
cupsdFreePrinterUsers(
cupsd_printer_t *p)
{
int i;
if (!p || !p->num_users)
return;
for (i = 0; i < p->num_users; i ++)
free((void *)p->users[i]);
free(p->users);
p->num_users = 0;
p->users = NULL;
}
void
cupsdLoadAllPrinters(void)
{
cups_file_t *fp;
int linenum;
char line[1024],
*value,
*valueptr;
cupsd_printer_t *p;
snprintf(line, sizeof(line), "%s/printers.conf", ServerRoot);
if ((fp = cupsFileOpen(line, "r")) == NULL)
{
if (errno != ENOENT)
cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to open %s - %s", line,
strerror(errno));
return;
}
linenum = 0;
p = NULL;
while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
{
if (!strcasecmp(line, "<Printer") ||
!strcasecmp(line, "<DefaultPrinter"))
{
if (p == NULL && value)
{
cupsdLogMessage(CUPSD_LOG_DEBUG, "Loading printer %s...", value);
p = cupsdAddPrinter(value);
p->accepting = 1;
p->state = IPP_PRINTER_IDLE;
if (!strcasecmp(line, "<DefaultPrinter"))
DefaultPrinter = p;
}
else
{
cupsdLogMessage(CUPSD_LOG_ERROR,
"Syntax error on line %d of printers.conf.", linenum);
break;
}
}
else if (!strcasecmp(line, "</Printer>"))
{
if (p != NULL)
{
cupsdSetPrinterAttrs(p);
cupsdAddPrinterHistory(p);
if (p->device_uri && strncmp(p->device_uri, "file:", 5) &&
p->state != IPP_PRINTER_STOPPED)
{
snprintf(line, sizeof(line), "%s/backend/%s", ServerBin,
p->device_uri);
if ((valueptr = strchr(line + strlen(ServerBin), ':')) != NULL)
*valueptr = '\0';
if (access(line, 0))
{
p->state = IPP_PRINTER_STOPPED;
snprintf(p->state_message, sizeof(p->state_message),
"Backend %s does not exist!", line);
}
}
p = NULL;
}
else
{
cupsdLogMessage(CUPSD_LOG_ERROR,
"Syntax error on line %d of printers.conf.", linenum);
break;
}
}
else if (!p)
{
cupsdLogMessage(CUPSD_LOG_ERROR,
"Syntax error on line %d of printers.conf.", linenum);
break;
}
else if (!strcasecmp(line, "AuthInfoRequired"))
{
if (!cupsdSetAuthInfoRequired(p, value, NULL))
cupsdLogMessage(CUPSD_LOG_ERROR,
"Bad AuthInfoRequired on line %d of printers.conf.",
linenum);
}
else if (!strcasecmp(line, "Info"))
{
if (value)
cupsdSetString(&p->info, value);
}
else if (!strcasecmp(line, "Location"))
{
if (value)
cupsdSetString(&p->location, value);
}
else if (!strcasecmp(line, "DeviceURI"))
{
if (value)
cupsdSetString(&p->device_uri, value);
else
{
cupsdLogMessage(CUPSD_LOG_ERROR,
"Syntax error on line %d of printers.conf.", linenum);
break;
}
}
else if (!strcasecmp(line, "Option") && value)
{
for (valueptr = value; *valueptr && !isspace(*valueptr & 255); valueptr ++);
if (!*valueptr)
cupsdLogMessage(CUPSD_LOG_ERROR,
"Syntax error on line %d of printers.conf.", linenum);
else
{
for (; *valueptr && isspace(*valueptr & 255); *valueptr++ = '\0');
p->num_options = cupsAddOption(value, valueptr, p->num_options,
&(p->options));
}
}
else if (!strcasecmp(line, "PortMonitor"))
{
if (value && strcmp(value, "none"))
cupsdSetString(&p->port_monitor, value);
else if (value)
cupsdClearString(&p->port_monitor);
else
{
cupsdLogMessage(CUPSD_LOG_ERROR,
"Syntax error on line %d of printers.conf.", linenum);
break;
}
}
else if (!strcasecmp(line, "State"))
{
if (value && !strcasecmp(value, "idle"))
p->state = IPP_PRINTER_IDLE;
else if (value && !strcasecmp(value, "stopped"))
p->state = IPP_PRINTER_STOPPED;
else
{
cupsdLogMessage(CUPSD_LOG_ERROR,
"Syntax error on line %d of printers.conf.", linenum);
break;
}
}
else if (!strcasecmp(line, "StateMessage"))
{
if (value)
strlcpy(p->state_message, value, sizeof(p->state_message));
}
else if (!strcasecmp(line, "StateTime"))
{
if (value)
p->state_time = atoi(value);
}
else if (!strcasecmp(line, "Accepting"))
{
if (value &&
(!strcasecmp(value, "yes") ||
!strcasecmp(value, "on") ||
!strcasecmp(value, "true")))
p->accepting = 1;
else if (value &&
(!strcasecmp(value, "no") ||
!strcasecmp(value, "off") ||
!strcasecmp(value, "false")))
p->accepting = 0;
else
{
cupsdLogMessage(CUPSD_LOG_ERROR,
"Syntax error on line %d of printers.conf.", linenum);
break;
}
}
else if (!strcasecmp(line, "Shared"))
{
if (value &&
(!strcasecmp(value, "yes") ||
!strcasecmp(value, "on") ||
!strcasecmp(value, "true")))
p->shared = 1;
else if (value &&
(!strcasecmp(value, "no") ||
!strcasecmp(value, "off") ||
!strcasecmp(value, "false")))
p->shared = 0;
else
{
cupsdLogMessage(CUPSD_LOG_ERROR,
"Syntax error on line %d of printers.conf.", linenum);
break;
}
}
else if (!strcasecmp(line, "JobSheets"))
{
if (value)
{
for (valueptr = value; *valueptr && !isspace(*valueptr & 255); valueptr ++);
if (*valueptr)
*valueptr++ = '\0';
cupsdSetString(&p->job_sheets[0], value);
while (isspace(*valueptr & 255))
valueptr ++;
if (*valueptr)
{
for (value = valueptr; *valueptr && !isspace(*valueptr & 255); valueptr ++);
if (*valueptr)
*valueptr = '\0';
cupsdSetString(&p->job_sheets[1], value);
}
}
else
{
cupsdLogMessage(CUPSD_LOG_ERROR,
"Syntax error on line %d of printers.conf.", linenum);
break;
}
}
else if (!strcasecmp(line, "AllowUser"))
{
if (value)
{
p->deny_users = 0;
cupsdAddPrinterUser(p, value);
}
else
{
cupsdLogMessage(CUPSD_LOG_ERROR,
"Syntax error on line %d of printers.conf.", linenum);
break;
}
}
else if (!strcasecmp(line, "DenyUser"))
{
if (value)
{
p->deny_users = 1;
cupsdAddPrinterUser(p, value);
}
else
{
cupsdLogMessage(CUPSD_LOG_ERROR,
"Syntax error on line %d of printers.conf.", linenum);
break;
}
}
else if (!strcasecmp(line, "QuotaPeriod"))
{
if (value)
p->quota_period = atoi(value);
else
{
cupsdLogMessage(CUPSD_LOG_ERROR,
"Syntax error on line %d of printers.conf.", linenum);
break;
}
}
else if (!strcasecmp(line, "PageLimit"))
{
if (value)
p->page_limit = atoi(value);
else
{
cupsdLogMessage(CUPSD_LOG_ERROR,
"Syntax error on line %d of printers.conf.", linenum);
break;
}
}
else if (!strcasecmp(line, "KLimit"))
{
if (value)
p->k_limit = atoi(value);
else
{
cupsdLogMessage(CUPSD_LOG_ERROR,
"Syntax error on line %d of printers.conf.", linenum);
break;
}
}
else if (!strcasecmp(line, "OpPolicy"))
{
if (value)
{
cupsd_policy_t *pol;
if ((pol = cupsdFindPolicy(value)) != NULL)
{
cupsdSetString(&p->op_policy, value);
p->op_policy_ptr = pol;
}
else
cupsdLogMessage(CUPSD_LOG_ERROR,
"Bad policy \"%s\" on line %d of printers.conf",
value, linenum);
}
else
{
cupsdLogMessage(CUPSD_LOG_ERROR,
"Syntax error on line %d of printers.conf.", linenum);
break;
}
}
else if (!strcasecmp(line, "ErrorPolicy"))
{
if (value)
cupsdSetString(&p->error_policy, value);
else
{
cupsdLogMessage(CUPSD_LOG_ERROR,
"Syntax error on line %d of printers.conf.", linenum);
break;
}
}
else
{
cupsdLogMessage(CUPSD_LOG_ERROR,
"Unknown configuration directive %s on line %d of printers.conf.",
line, linenum);
}
}
cupsFileClose(fp);
}
void
cupsdRenamePrinter(
cupsd_printer_t *p,
const char *name)
{
cupsdLogMessage(CUPSD_LOG_DEBUG2,
"cupsdRenamePrinter: Removing %s from Printers", p->name);
cupsArrayRemove(Printers, p);
if (p->type & CUPS_PRINTER_IMPLICIT)
{
cupsdLogMessage(CUPSD_LOG_DEBUG2,
"cupsdRenamePrinter: Removing %s from ImplicitPrinters",
p->name);
cupsArrayRemove(ImplicitPrinters, p);
}
mimeDeleteType(MimeDatabase, p->filetype);
p->filetype = mimeAddType(MimeDatabase, "printer", name);
mimeDeleteType(MimeDatabase, p->prefiltertype);
p->prefiltertype = mimeAddType(MimeDatabase, "prefilter", name);
cupsdSetString(&p->name, name);
cupsdSetPrinterAttrs(p);
cupsdLogMessage(CUPSD_LOG_DEBUG2,
"cupsdRenamePrinter: Adding %s to Printers", p->name);
cupsArrayAdd(Printers, p);
if (p->type & CUPS_PRINTER_IMPLICIT)
{
cupsdLogMessage(CUPSD_LOG_DEBUG2,
"cupsdRenamePrinter: Adding %s to ImplicitPrinters",
p->name);
cupsArrayAdd(ImplicitPrinters, p);
}
}
void
cupsdSaveAllPrinters(void)
{
int i;
cups_file_t *fp;
char temp[1024];
char backup[1024];
cupsd_printer_t *printer;
time_t curtime;
struct tm *curdate;
cups_option_t *option;
const char *ptr;
snprintf(temp, sizeof(temp), "%s/printers.conf", ServerRoot);
snprintf(backup, sizeof(backup), "%s/printers.conf.O", ServerRoot);
if (rename(temp, backup))
{
if (errno != ENOENT)
cupsdLogMessage(CUPSD_LOG_ERROR,
"Unable to backup printers.conf - %s", strerror(errno));
}
if ((fp = cupsFileOpen(temp, "w")) == NULL)
{
cupsdLogMessage(CUPSD_LOG_ERROR,
"Unable to save printers.conf - %s", strerror(errno));
if (rename(backup, temp))
cupsdLogMessage(CUPSD_LOG_ERROR,
"Unable to restore printers.conf - %s", strerror(errno));
return;
}
else
cupsdLogMessage(CUPSD_LOG_INFO, "Saving printers.conf...");
fchown(cupsFileNumber(fp), getuid(), Group);
fchmod(cupsFileNumber(fp), 0600);
curtime = time(NULL);
curdate = localtime(&curtime);
strftime(temp, sizeof(temp) - 1, "%Y-%m-%d %H:%M", curdate);
cupsFilePuts(fp, "# Printer configuration file for " CUPS_SVERSION "\n");
cupsFilePrintf(fp, "# Written by cupsd on %s\n", temp);
for (printer = (cupsd_printer_t *)cupsArrayFirst(Printers);
printer;
printer = (cupsd_printer_t *)cupsArrayNext(Printers))
{
if ((printer->type & CUPS_PRINTER_DISCOVERED) ||
(printer->type & CUPS_PRINTER_CLASS) ||
(printer->type & CUPS_PRINTER_IMPLICIT))
continue;
if (printer == DefaultPrinter)
cupsFilePrintf(fp, "<DefaultPrinter %s>\n", printer->name);
else
cupsFilePrintf(fp, "<Printer %s>\n", printer->name);
if (printer->num_auth_info_required > 0)
{
cupsFilePrintf(fp, "AuthInfoRequired %s", printer->auth_info_required[0]);
for (i = 1; i < printer->num_auth_info_required; i ++)
cupsFilePrintf(fp, ",%s", printer->auth_info_required[i]);
cupsFilePutChar(fp, '\n');
}
if (printer->info)
{
if ((ptr = strchr(printer->info, '#')) != NULL)
{
cupsFilePuts(fp, "Info ");
cupsFileWrite(fp, printer->info, ptr - printer->info);
cupsFilePutChar(fp, '\\');
cupsFilePuts(fp, ptr);
cupsFilePutChar(fp, '\n');
}
else
cupsFilePrintf(fp, "Info %s\n", printer->info);
}
if (printer->location)
{
if ((ptr = strchr(printer->info, '#')) != NULL)
{
cupsFilePuts(fp, "Location ");
cupsFileWrite(fp, printer->location, ptr - printer->location);
cupsFilePutChar(fp, '\\');
cupsFilePuts(fp, ptr);
cupsFilePutChar(fp, '\n');
}
else
cupsFilePrintf(fp, "Location %s\n", printer->location);
}
if (printer->device_uri)
cupsFilePrintf(fp, "DeviceURI %s\n", printer->device_uri);
if (printer->port_monitor)
cupsFilePrintf(fp, "PortMonitor %s\n", printer->port_monitor);
if (printer->state == IPP_PRINTER_STOPPED)
{
cupsFilePuts(fp, "State Stopped\n");
cupsFilePrintf(fp, "StateMessage %s\n", printer->state_message);
}
else
cupsFilePuts(fp, "State Idle\n");
cupsFilePrintf(fp, "StateTime %d\n", (int)printer->state_time);
if (printer->accepting)
cupsFilePuts(fp, "Accepting Yes\n");
else
cupsFilePuts(fp, "Accepting No\n");
if (printer->shared)
cupsFilePuts(fp, "Shared Yes\n");
else
cupsFilePuts(fp, "Shared No\n");
cupsFilePrintf(fp, "JobSheets %s %s\n", printer->job_sheets[0],
printer->job_sheets[1]);
cupsFilePrintf(fp, "QuotaPeriod %d\n", printer->quota_period);
cupsFilePrintf(fp, "PageLimit %d\n", printer->page_limit);
cupsFilePrintf(fp, "KLimit %d\n", printer->k_limit);
for (i = 0; i < printer->num_users; i ++)
cupsFilePrintf(fp, "%sUser %s\n", printer->deny_users ? "Deny" : "Allow",
printer->users[i]);
if (printer->op_policy)
cupsFilePrintf(fp, "OpPolicy %s\n", printer->op_policy);
if (printer->error_policy)
cupsFilePrintf(fp, "ErrorPolicy %s\n", printer->error_policy);
for (i = printer->num_options, option = printer->options;
i > 0;
i --, option ++)
cupsFilePrintf(fp, "Option %s %s\n", option->name, option->value);
cupsFilePuts(fp, "</Printer>\n");
#ifdef __sgi
write_irix_state(printer);
#endif
}
cupsFileClose(fp);
}
int
cupsdSetAuthInfoRequired(
cupsd_printer_t *p,
const char *values,
ipp_attribute_t *attr)
{
int i;
p->num_auth_info_required = 0;
if (values)
{
const char *end;
while (*values && p->num_auth_info_required < 4)
{
if ((end = strchr(values, ',')) == NULL)
end = values + strlen(values);
if ((end - values) == 4 && !strncmp(values, "none", 4))
{
if (p->num_auth_info_required != 0 || *end)
return (0);
p->auth_info_required[p->num_auth_info_required] = "none";
p->num_auth_info_required ++;
return (1);
}
else if ((end - values) == 9 && !strncmp(values, "negotiate", 9))
{
if (p->num_auth_info_required != 0 || *end)
return (0);
p->auth_info_required[p->num_auth_info_required] = "negotiate";
p->num_auth_info_required ++;
}
else if ((end - values) == 6 && !strncmp(values, "domain", 6))
{
p->auth_info_required[p->num_auth_info_required] = "domain";
p->num_auth_info_required ++;
}
else if ((end - values) == 8 && !strncmp(values, "password", 8))
{
p->auth_info_required[p->num_auth_info_required] = "password";
p->num_auth_info_required ++;
}
else if ((end - values) == 8 && !strncmp(values, "username", 8))
{
p->auth_info_required[p->num_auth_info_required] = "username";
p->num_auth_info_required ++;
}
else
return (0);
values = (*end) ? end + 1 : end;
}
if (p->num_auth_info_required == 0)
{
p->auth_info_required[0] = "none";
p->num_auth_info_required = 1;
}
if (p->num_auth_info_required > 1 ||
strcmp(p->auth_info_required[0], "none"))
p->type |= CUPS_PRINTER_AUTHENTICATED;
else
p->type &= ~CUPS_PRINTER_AUTHENTICATED;
return (1);
}
if (!attr || attr->num_values > 4)
return (0);
if (attr->num_values > 1 ||
strcmp(attr->values[0].string.text, "none"))
p->type |= CUPS_PRINTER_AUTHENTICATED;
else
p->type &= ~CUPS_PRINTER_AUTHENTICATED;
for (i = 0; i < attr->num_values; i ++)
{
if (!strcmp(attr->values[i].string.text, "none"))
{
if (p->num_auth_info_required != 0 || attr->num_values != 1)
return (0);
p->auth_info_required[p->num_auth_info_required] = "none";
p->num_auth_info_required ++;
return (1);
}
else if (!strcmp(attr->values[i].string.text, "negotiate"))
{
if (p->num_auth_info_required != 0 || attr->num_values != 1)
return (0);
p->auth_info_required[p->num_auth_info_required] = "negotiate";
p->num_auth_info_required ++;
return (1);
}
else if (!strcmp(attr->values[i].string.text, "domain"))
{
p->auth_info_required[p->num_auth_info_required] = "domain";
p->num_auth_info_required ++;
}
else if (!strcmp(attr->values[i].string.text, "password"))
{
p->auth_info_required[p->num_auth_info_required] = "password";
p->num_auth_info_required ++;
}
else if (!strcmp(attr->values[i].string.text, "username"))
{
p->auth_info_required[p->num_auth_info_required] = "username";
p->num_auth_info_required ++;
}
else
return (0);
}
return (1);
}
void
cupsdSetPrinterAttr(
cupsd_printer_t *p,
const char *name,
char *value)
{
ipp_attribute_t *attr;
int i,
count;
char *ptr;
ipp_tag_t value_tag;
for (count = 1, ptr = value;
(ptr = strchr(ptr, ',')) != NULL;
ptr ++, count ++);
if (!strcmp(name, "marker-levels"))
{
if ((attr = ippFindAttribute(p->attrs, name, IPP_TAG_INTEGER)) != NULL &&
attr->num_values < count)
{
ippDeleteAttribute(p->attrs, attr);
attr = NULL;
}
if (attr)
attr->num_values = count;
else
attr = ippAddIntegers(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER, name,
count, NULL);
if (!attr)
{
cupsdLogMessage(CUPSD_LOG_ERROR,
"Unable to allocate memory for printer attribute "
"(%d values)", count);
return;
}
for (i = 0; i < count; i ++)
{
if ((ptr = strchr(value, ',')) != NULL)
*ptr++ = '\0';
attr->values[i].integer = strtol(value, NULL, 10);
if (ptr)
value = ptr;
}
}
else
{
if (!strcmp(name, "marker-types"))
value_tag = IPP_TAG_KEYWORD;
else if (!strcmp(name, "marker-message"))
value_tag = IPP_TAG_TEXT;
else
value_tag = IPP_TAG_NAME;
if ((attr = ippFindAttribute(p->attrs, name, value_tag)) != NULL &&
attr->num_values < count)
{
ippDeleteAttribute(p->attrs, attr);
attr = NULL;
}
if (attr)
{
for (i = 0; i < attr->num_values; i ++)
_cupsStrFree(attr->values[i].string.text);
attr->num_values = count;
}
else
attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, value_tag, name,
count, NULL, NULL);
if (!attr)
{
cupsdLogMessage(CUPSD_LOG_ERROR,
"Unable to allocate memory for printer attribute "
"(%d values)", count);
return;
}
for (i = 0; i < count; i ++)
{
if ((ptr = strchr(value, ',')) != NULL)
*ptr++ = '\0';
attr->values[i].string.text = _cupsStrAlloc(value);
if (ptr)
value = ptr;
}
}
}
void
cupsdSetPrinterAttrs(cupsd_printer_t *p)
{
int i,
length;
char uri[HTTP_MAX_URI];
char resource[HTTP_MAX_URI];
char filename[1024];
int num_air;
const char * const *air;
int num_media;
cupsd_location_t *auth;
const char *auth_supported;
ppd_file_t *ppd;
ppd_option_t *input_slot,
*media_type,
*page_size,
*output_bin,
*media_quality,
*duplex;
ppd_attr_t *ppdattr;
ipp_attribute_t *attr;
ipp_value_t *val;
int num_finishings;
int finishings[5];
cups_option_t *option;
static const char * const sides[3] =
{
"one-sided",
"two-sided-long-edge",
"two-sided-short-edge"
};
static const char * const air_userpass[] =
{
"username",
"password"
};
#ifdef HAVE_GSSAPI
static const char * const air_negotiate[] =
{
"negotiate"
};
#endif
static const char * const air_none[] =
{
"none"
};
DEBUG_printf(("cupsdSetPrinterAttrs: entering name = %s, type = %x\n", p->name,
p->type));
if (!CommonData)
cupsdCreateCommonData();
delete_printer_filters(p);
auth_supported = "requesting-user-name";
num_air = 1;
air = air_none;
if (p->num_auth_info_required > 0 && strcmp(p->auth_info_required[0], "none"))
{
num_air = p->num_auth_info_required;
air = p->auth_info_required;
if (!strcmp(air[0], "username"))
auth_supported = "basic";
else
auth_supported = "negotiate";
}
else if (!(p->type & CUPS_PRINTER_DISCOVERED))
{
if (p->type & CUPS_PRINTER_CLASS)
snprintf(resource, sizeof(resource), "/classes/%s", p->name);
else
snprintf(resource, sizeof(resource), "/printers/%s", p->name);
if ((auth = cupsdFindBest(resource, HTTP_POST)) == NULL ||
auth->type == CUPSD_AUTH_NONE)
auth = cupsdFindPolicyOp(p->op_policy_ptr, IPP_PRINT_JOB);
if (auth)
{
if (auth->type == CUPSD_AUTH_BASIC || auth->type == CUPSD_AUTH_BASICDIGEST)
{
auth_supported = "basic";
num_air = 2;
air = air_userpass;
}
else if (auth->type == CUPSD_AUTH_DIGEST)
{
auth_supported = "digest";
num_air = 2;
air = air_userpass;
}
#ifdef HAVE_GSSAPI
else if (auth->type == CUPSD_AUTH_NEGOTIATE)
{
auth_supported = "negotiate";
num_air = 1;
air = air_negotiate;
}
#endif
if (auth->type != CUPSD_AUTH_NONE)
p->type |= CUPS_PRINTER_AUTHENTICATED;
else
p->type &= ~CUPS_PRINTER_AUTHENTICATED;
}
else
p->type &= ~CUPS_PRINTER_AUTHENTICATED;
}
else if (p->type & CUPS_PRINTER_AUTHENTICATED)
{
num_air = 2;
air = air_userpass;
}
if (p->attrs)
ippDelete(p->attrs);
p->attrs = ippNew();
ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
"uri-authentication-supported", NULL, auth_supported);
ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
"uri-security-supported", NULL, "none");
ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME, "printer-name", NULL,
p->name);
ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-location",
NULL, p->location ? p->location : "");
ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-info",
NULL, p->info ? p->info : "");
ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI, "printer-more-info",
NULL, p->uri);
if (p->num_users)
{
if (p->deny_users)
ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
"requesting-user-name-denied", p->num_users, NULL,
p->users);
else
ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
"requesting-user-name-allowed", p->num_users, NULL,
p->users);
}
ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
"job-quota-period", p->quota_period);
ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
"job-k-limit", p->k_limit);
ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
"job-page-limit", p->page_limit);
ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
"auth-info-required", num_air, NULL, air);
if (cupsArrayCount(Banners) > 0 && !(p->type & CUPS_PRINTER_DISCOVERED))
{
attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
"job-sheets-default", 2, NULL, NULL);
if (attr != NULL)
{
attr->values[0].string.text = _cupsStrAlloc(Classification ?
Classification : p->job_sheets[0]);
attr->values[1].string.text = _cupsStrAlloc(Classification ?
Classification : p->job_sheets[1]);
}
}
p->raw = 0;
p->remote = 0;
if (p->type & CUPS_PRINTER_DISCOVERED)
{
ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI,
"printer-uri-supported", NULL, p->uri);
if (p->make_model)
ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
"printer-make-and-model", NULL, p->make_model);
ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI, "device-uri", NULL,
p->uri);
p->raw = 1;
p->remote = 1;
}
else
{
p->type &= ~CUPS_PRINTER_OPTIONS;
if (p->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT))
{
p->raw = 1;
if ((p->type & CUPS_PRINTER_IMPLICIT) && p->num_printers > 0 &&
p->printers[0]->make_model)
ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
"printer-make-and-model", NULL, p->printers[0]->make_model);
else
ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
"printer-make-and-model", NULL, "Local Printer Class");
ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI, "device-uri", NULL,
"file:///dev/null");
if (p->num_printers > 0)
{
attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
"member-names", p->num_printers, NULL, NULL);
p->type |= CUPS_PRINTER_OPTIONS;
for (i = 0; i < p->num_printers; i ++)
{
if (attr != NULL)
attr->values[i].string.text = _cupsStrAlloc(p->printers[i]->name);
p->type &= ~CUPS_PRINTER_OPTIONS | p->printers[i]->type;
}
}
}
else
{
if (!p->device_uri)
strcpy(uri, "file:/dev/null");
else if (strstr(p->device_uri, "://") != NULL)
{
cupsdSanitizeURI(p->device_uri, uri, sizeof(uri));
}
else
{
strlcpy(uri, p->device_uri, sizeof(uri));
}
ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI, "device-uri", NULL,
uri);
p->type |= CUPS_PRINTER_BW;
finishings[0] = IPP_FINISHINGS_NONE;
num_finishings = 1;
snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot,
p->name);
if ((ppd = ppdOpenFile(filename)) != NULL)
{
if (ppd->color_device)
p->type |= CUPS_PRINTER_COLOR;
if (ppd->variable_sizes)
p->type |= CUPS_PRINTER_VARIABLE;
if (!ppd->manual_copies)
p->type |= CUPS_PRINTER_COPIES;
if ((ppdattr = ppdFindAttr(ppd, "cupsFax", NULL)) != NULL)
if (ppdattr->value && !strcasecmp(ppdattr->value, "true"))
p->type |= CUPS_PRINTER_FAX;
ippAddBoolean(p->attrs, IPP_TAG_PRINTER, "color-supported",
ppd->color_device);
if (ppd->throughput)
ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
"pages-per-minute", ppd->throughput);
if (ppd->nickname)
{
cupsdSetString(&p->make_model, ppd->nickname);
}
else if (ppd->modelname)
{
cupsdSetString(&p->make_model, ppd->modelname);
}
else
cupsdSetString(&p->make_model, "Bad PPD File");
ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
"printer-make-and-model", NULL, p->make_model);
if ((input_slot = ppdFindOption(ppd, "InputSlot")) != NULL)
num_media = input_slot->num_choices;
else
num_media = 0;
if ((media_type = ppdFindOption(ppd, "MediaType")) != NULL)
num_media += media_type->num_choices;
if ((page_size = ppdFindOption(ppd, "PageSize")) != NULL)
num_media += page_size->num_choices;
if ((media_quality = ppdFindOption(ppd, "EFMediaQualityMode")) != NULL)
num_media += media_quality->num_choices;
if (num_media == 0)
{
cupsdLogMessage(CUPSD_LOG_CRIT,
"The PPD file for printer %s contains no media "
"options and is therefore invalid!", p->name);
}
else
{
attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
"media-supported", num_media, NULL, NULL);
if (attr != NULL)
{
val = attr->values;
if (input_slot != NULL)
for (i = 0; i < input_slot->num_choices; i ++, val ++)
val->string.text = _cupsStrAlloc(input_slot->choices[i].choice);
if (media_type != NULL)
for (i = 0; i < media_type->num_choices; i ++, val ++)
val->string.text = _cupsStrAlloc(media_type->choices[i].choice);
if (media_quality != NULL)
for (i = 0; i < media_quality->num_choices; i ++, val ++)
val->string.text = _cupsStrAlloc(media_quality->choices[i].choice);
if (page_size != NULL)
{
for (i = 0; i < page_size->num_choices; i ++, val ++)
val->string.text = _cupsStrAlloc(page_size->choices[i].choice);
ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
"media-default", NULL, page_size->defchoice);
}
else if (input_slot != NULL)
ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
"media-default", NULL, input_slot->defchoice);
else if (media_type != NULL)
ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
"media-default", NULL, media_type->defchoice);
else if (media_quality != NULL)
ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
"media-default", NULL, media_quality->defchoice);
else
ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
"media-default", NULL, "none");
}
}
if ((output_bin = ppdFindOption(ppd, "OutputBin")) != NULL)
{
attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
"output-bin-supported", output_bin->num_choices,
NULL, NULL);
if (attr != NULL)
{
for (i = 0, val = attr->values;
i < output_bin->num_choices;
i ++, val ++)
val->string.text = _cupsStrAlloc(output_bin->choices[i].choice);
}
}
if ((duplex = ppdFindOption(ppd, "Duplex")) == NULL)
if ((duplex = ppdFindOption(ppd, "EFDuplex")) == NULL)
if ((duplex = ppdFindOption(ppd, "EFDuplexing")) == NULL)
if ((duplex = ppdFindOption(ppd, "KD03Duplex")) == NULL)
duplex = ppdFindOption(ppd, "JCLDuplex");
if (duplex && duplex->num_choices > 1)
{
p->type |= CUPS_PRINTER_DUPLEX;
ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
"sides-supported", 3, NULL, sides);
if (!strcasecmp(duplex->defchoice, "DuplexTumble"))
ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
"sides-default", NULL, "two-sided-short-edge");
else if (!strcasecmp(duplex->defchoice, "DuplexNoTumble"))
ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
"sides-default", NULL, "two-sided-long-edge");
else
ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
"sides-default", NULL, "one-sided");
}
if (ppdFindOption(ppd, "Collate") != NULL)
p->type |= CUPS_PRINTER_COLLATE;
if (ppdFindOption(ppd, "StapleLocation") != NULL)
{
p->type |= CUPS_PRINTER_STAPLE;
finishings[num_finishings++] = IPP_FINISHINGS_STAPLE;
}
if (ppdFindOption(ppd, "BindEdge") != NULL)
{
p->type |= CUPS_PRINTER_BIND;
finishings[num_finishings++] = IPP_FINISHINGS_BIND;
}
for (i = 0; i < ppd->num_sizes; i ++)
if (ppd->sizes[i].length > 1728)
p->type |= CUPS_PRINTER_LARGE;
else if (ppd->sizes[i].length > 1008)
p->type |= CUPS_PRINTER_MEDIUM;
else
p->type |= CUPS_PRINTER_SMALL;
add_printer_filter(p, p->filetype, "application/vnd.cups-raw 0 -");
if ((ppdattr = ppdFindAttr(ppd, "cupsPreFilter", NULL)) != NULL)
{
p->prefiltertype = mimeAddType(MimeDatabase, "prefilter", p->name);
for (; ppdattr; ppdattr = ppdFindNextAttr(ppd, "cupsPreFilter", NULL))
if (ppdattr->value)
add_printer_filter(p, p->prefiltertype, ppdattr->value);
}
DEBUG_printf(("ppd->num_filters = %d\n", ppd->num_filters));
for (i = 0; i < ppd->num_filters; i ++)
{
DEBUG_printf(("ppd->filters[%d] = \"%s\"\n", i, ppd->filters[i]));
add_printer_filter(p, p->filetype, ppd->filters[i]);
}
if (ppd->num_filters == 0)
{
add_printer_filter(p, p->filetype,
"application/vnd.cups-postscript 0 -");
}
ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME, "port-monitor",
NULL, p->port_monitor ? p->port_monitor : "none");
for (i = 1, ppdattr = ppdFindAttr(ppd, "cupsPortMonitor", NULL);
ppdattr;
i ++, ppdattr = ppdFindNextAttr(ppd, "cupsPortMonitor", NULL));
if (ppd->protocols)
{
if (strstr(ppd->protocols, "TBCP"))
i ++;
else if (strstr(ppd->protocols, "BCP"))
i ++;
}
attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
"port-monitor-supported", i, NULL, NULL);
attr->values[0].string.text = _cupsStrAlloc("none");
for (i = 1, ppdattr = ppdFindAttr(ppd, "cupsPortMonitor", NULL);
ppdattr;
i ++, ppdattr = ppdFindNextAttr(ppd, "cupsPortMonitor", NULL))
attr->values[i].string.text = _cupsStrAlloc(ppdattr->value);
if (ppd->protocols)
{
if (strstr(ppd->protocols, "TBCP"))
attr->values[i].string.text = _cupsStrAlloc("tbcp");
else if (strstr(ppd->protocols, "BCP"))
attr->values[i].string.text = _cupsStrAlloc("bcp");
}
#ifdef HAVE_DNSSD
cupsdSetString(&p->product, ppd->product);
#endif
if (ppdFindAttr(ppd, "APRemoteQueueID", NULL))
p->type |= CUPS_PRINTER_REMOTE;
ppdClose(ppd);
}
else if (!access(filename, 0))
{
int pline;
ppd_status_t pstatus;
pstatus = ppdLastError(&pline);
cupsdLogMessage(CUPSD_LOG_ERROR, "PPD file for %s cannot be loaded!",
p->name);
if (pstatus <= PPD_ALLOC_ERROR)
cupsdLogMessage(CUPSD_LOG_ERROR, "%s", strerror(errno));
else
cupsdLogMessage(CUPSD_LOG_ERROR, "%s on line %d.",
ppdErrorString(pstatus), pline);
cupsdLogMessage(CUPSD_LOG_INFO,
"Hint: Run \"cupstestppd %s\" and fix any errors.",
filename);
add_printer_filter(p, p->filetype, "application/vnd.cups-raw 0 -");
add_printer_filter(p, p->filetype,
"application/vnd.cups-postscript 0 -");
}
else
{
snprintf(filename, sizeof(filename), "%s/interfaces/%s", ServerRoot,
p->name);
if (!access(filename, X_OK))
{
ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
"printer-make-and-model", NULL,
"Local System V Printer");
snprintf(filename, sizeof(filename), "*/* 0 %s/interfaces/%s",
ServerRoot, p->name);
add_printer_filter(p, p->filetype, filename);
}
else if (p->device_uri &&
!strncmp(p->device_uri, "ipp://", 6) &&
(strstr(p->device_uri, "/printers/") != NULL ||
strstr(p->device_uri, "/classes/") != NULL))
{
p->type |= CUPS_PRINTER_REMOTE;
ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI,
"printer-uri-supported", NULL, p->device_uri);
ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
"printer-make-and-model", NULL, "Remote Printer");
p->raw = 1;
p->remote = 1;
}
else
{
ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
"printer-make-and-model", NULL, "Local Raw Printer");
p->raw = 1;
}
}
ippAddIntegers(p->attrs, IPP_TAG_PRINTER, IPP_TAG_ENUM,
"finishings-supported", num_finishings, finishings);
ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_ENUM,
"finishings-default", IPP_FINISHINGS_NONE);
}
}
if (p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT))
p->shared = 0;
else
{
const char *valptr;
char *attrptr;
if (p->browse_attrs)
free(p->browse_attrs);
for (length = 1, i = p->num_options, option = p->options;
i > 0;
i --, option ++)
{
length += strlen(option->name) + 2;
if (option->value)
{
for (valptr = option->value; *valptr; valptr ++)
if (strchr(" \"\'\\", *valptr))
length += 2;
else
length ++;
}
}
length += 13 + strlen(p->job_sheets[0]) + strlen(p->job_sheets[1]);
length += 32;
if (BrowseLocalOptions)
length += 12 + strlen(BrowseLocalOptions);
if (p->num_auth_info_required > 0)
{
length += 18;
for (i = 0; i < p->num_auth_info_required; i ++)
length += strlen(p->auth_info_required[i]) + 1;
}
if ((p->browse_attrs = calloc(1, length)) == NULL)
cupsdLogMessage(CUPSD_LOG_ERROR,
"Unable to allocate %d bytes for browse data!",
length);
else
{
sprintf(p->browse_attrs, "job-sheets=%s,%s lease-duration=%d",
p->job_sheets[0], p->job_sheets[1], BrowseTimeout);
attrptr = p->browse_attrs + strlen(p->browse_attrs);
if (BrowseLocalOptions)
{
sprintf(attrptr, " ipp-options=%s", BrowseLocalOptions);
attrptr += strlen(attrptr);
}
for (i = p->num_options, option = p->options;
i > 0;
i --, option ++)
{
*attrptr++ = ' ';
strcpy(attrptr, option->name);
attrptr += strlen(attrptr);
if (option->value)
{
*attrptr++ = '=';
for (valptr = option->value; *valptr; valptr ++)
{
if (strchr(" \"\'\\", *valptr))
*attrptr++ = '\\';
*attrptr++ = *valptr;
}
}
}
if (p->num_auth_info_required > 0)
{
strcpy(attrptr, "auth-info-required");
attrptr += 18;
for (i = 0; i < p->num_auth_info_required; i ++)
{
*attrptr++ = i ? ',' : '=';
strcpy(attrptr, p->auth_info_required[i]);
attrptr += strlen(attrptr);
}
}
else
*attrptr = '\0';
}
}
add_printer_formats(p);
DEBUG_printf(("cupsdSetPrinterAttrs: leaving name = %s, type = %x\n", p->name,
p->type));
add_printer_defaults(p);
#ifdef __sgi
write_irix_config(p);
write_irix_state(p);
#endif
cupsdRegisterPrinter(p);
}
void
cupsdSetPrinterReasons(
cupsd_printer_t *p,
const char *s)
{
int i;
const char *sptr;
char reason[255],
*rptr;
if (s[0] == '-' || s[0] == '+')
{
sptr = s + 1;
}
else
{
sptr = s;
for (i = 0; i < p->num_reasons; i ++)
free(p->reasons[i]);
p->num_reasons = 0;
}
if (!strcmp(s, "none"))
return;
while (*sptr)
{
while (isspace(*sptr & 255) || *sptr == ',')
sptr ++;
for (rptr = reason; *sptr && !isspace(*sptr & 255) && *sptr != ','; sptr ++)
if (rptr < (reason + sizeof(reason) - 1))
*rptr++ = *sptr;
if (rptr == reason)
break;
*rptr = '\0';
if (s[0] == '-')
{
for (i = 0; i < p->num_reasons; i ++)
if (!strcasecmp(reason, p->reasons[i]))
{
p->num_reasons --;
free(p->reasons[i]);
if (i < p->num_reasons)
memmove(p->reasons + i, p->reasons + i + 1,
(p->num_reasons - i) * sizeof(char *));
i --;
if (!strcmp(reason, "paused") && p->state == IPP_PRINTER_STOPPED)
cupsdSetPrinterState(p, IPP_PRINTER_IDLE, 1);
}
}
else if (p->num_reasons < (int)(sizeof(p->reasons) / sizeof(p->reasons[0])))
{
for (i = 0; i < p->num_reasons; i ++)
if (!strcasecmp(reason, p->reasons[i]))
break;
if (i >= p->num_reasons)
{
p->reasons[i] = strdup(reason);
p->num_reasons ++;
if (!strcmp(reason, "paused") && p->state != IPP_PRINTER_STOPPED)
cupsdSetPrinterState(p, IPP_PRINTER_STOPPED, 1);
}
}
}
}
void
cupsdSetPrinterState(
cupsd_printer_t *p,
ipp_pstate_t s,
int update)
{
ipp_pstate_t old_state;
if (p->type & CUPS_PRINTER_DISCOVERED)
return;
old_state = p->state;
p->state = s;
if (old_state != s)
{
cupsdAddEvent(s == IPP_PRINTER_STOPPED ? CUPSD_EVENT_PRINTER_STOPPED :
CUPSD_EVENT_PRINTER_STATE, p, NULL,
"%s \"%s\" state changed.",
(p->type & CUPS_PRINTER_CLASS) ? "Class" : "Printer",
p->name);
BrowseNext = p;
p->state_time = time(NULL);
p->browse_time = 0;
#ifdef __sgi
write_irix_state(p);
#endif
}
cupsdAddPrinterHistory(p);
if (update)
cupsdRegisterPrinter(p);
if ((old_state == IPP_PRINTER_STOPPED) != (s == IPP_PRINTER_STOPPED) &&
update)
{
if (p->type & CUPS_PRINTER_CLASS)
cupsdSaveAllClasses();
else
cupsdSaveAllPrinters();
}
}
void
cupsdStopPrinter(cupsd_printer_t *p,
int update)
{
cupsd_job_t *job;
cupsdSetPrinterState(p, IPP_PRINTER_STOPPED, update);
if (p->job)
{
job = (cupsd_job_t *)p->job;
cupsdStopJob(job, 0);
job->state->values[0].integer = IPP_JOB_PENDING;
job->state_value = IPP_JOB_PENDING;
cupsdSaveJob(job);
cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED, p, job,
"Job stopped due to printer being paused");
}
}
void
cupsdUpdatePrinters(void)
{
cupsd_printer_t *p;
for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
p;
p = (cupsd_printer_t *)cupsArrayNext(Printers))
{
if (!Browsing &&
(p->type & (CUPS_PRINTER_IMPLICIT | CUPS_PRINTER_DISCOVERED)))
{
if (p->type & CUPS_PRINTER_IMPLICIT)
cupsArrayRemove(ImplicitPrinters, p);
cupsArraySave(Printers);
cupsdDeletePrinter(p, 0);
cupsArrayRestore(Printers);
continue;
}
if ((p->op_policy_ptr = cupsdFindPolicy(p->op_policy)) == NULL)
p->op_policy_ptr = DefaultPolicyPtr;
if (!(p->type & CUPS_PRINTER_DISCOVERED))
cupsdSetPrinterAttrs(p);
}
}
const char *
cupsdValidateDest(
const char *uri,
cups_ptype_t *dtype,
cupsd_printer_t **printer)
{
cupsd_printer_t *p;
char localname[1024],
*lptr,
*sptr,
*rptr,
scheme[32],
username[64],
hostname[HTTP_MAX_HOST],
resource[HTTP_MAX_URI];
int port;
DEBUG_printf(("cupsdValidateDest(uri=\"%s\", dtype=%p, printer=%p)\n", uri,
dtype, printer));
if (printer)
*printer = NULL;
if (dtype)
*dtype = (cups_ptype_t)0;
httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, sizeof(scheme),
username, sizeof(username), hostname, sizeof(hostname),
&port, resource, sizeof(resource));
if (!strncmp(resource, "/classes/", 9))
{
rptr = resource + 9;
}
else if (!strncmp(resource, "/printers/", 10))
{
rptr = resource + 10;
}
else
{
return (NULL);
}
p = cupsdFindDest(rptr);
if (p == NULL && strchr(rptr, '@') == NULL)
return (NULL);
else if (p != NULL)
{
if (printer)
*printer = p;
if (dtype)
*dtype = p->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT |
CUPS_PRINTER_REMOTE | CUPS_PRINTER_DISCOVERED);
return (p->name);
}
if (!strcasecmp(hostname, "localhost"))
strlcpy(hostname, ServerName, sizeof(hostname));
strlcpy(localname, hostname, sizeof(localname));
if (!strcasecmp(hostname, ServerName))
{
lptr = strchr(localname, '.');
sptr = strchr(ServerName, '.');
if (sptr != NULL && lptr != NULL)
{
while (lptr != NULL)
{
if (!strcasecmp(lptr, sptr))
{
*lptr = '\0';
break;
}
else
lptr = strchr(lptr + 1, '.');
}
}
}
DEBUG_printf(("localized hostname is \"%s\"...\n", localname));
for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
p;
p = (cupsd_printer_t *)cupsArrayNext(Printers))
if (!strcasecmp(p->hostname, localname) &&
!strcasecmp(p->name, rptr))
{
if (printer)
*printer = p;
if (dtype)
*dtype = p->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT |
CUPS_PRINTER_REMOTE | CUPS_PRINTER_DISCOVERED);
return (p->name);
}
return (NULL);
}
void
cupsdWritePrintcap(void)
{
cups_file_t *fp;
cupsd_printer_t *p;
#ifdef __sgi
write_irix_state(DefaultPrinter);
#endif
if (!Printcap || !*Printcap)
return;
if ((fp = cupsFileOpen(Printcap, "w")) == NULL)
return;
cupsFilePuts(fp,
"# This file was automatically generated by cupsd(8) from the\n");
cupsFilePrintf(fp, "# %s/printers.conf file. All changes to this file\n",
ServerRoot);
cupsFilePuts(fp, "# will be lost.\n");
if (Printers)
{
switch (PrintcapFormat)
{
case PRINTCAP_BSD:
if (DefaultPrinter)
cupsFilePrintf(fp, "%s|%s:rm=%s:rp=%s:\n", DefaultPrinter->name,
DefaultPrinter->info, ServerName,
DefaultPrinter->name);
for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
p;
p = (cupsd_printer_t *)cupsArrayNext(Printers))
if (p != DefaultPrinter)
cupsFilePrintf(fp, "%s|%s:rm=%s:rp=%s:\n", p->name, p->info,
ServerName, p->name);
break;
case PRINTCAP_SOLARIS:
cupsFilePuts(fp, "_all:all=");
for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
p;
p = (cupsd_printer_t *)cupsArrayCurrent(Printers))
cupsFilePrintf(fp, "%s%c", p->name,
cupsArrayNext(Printers) ? ',' : '\n');
if (DefaultPrinter)
cupsFilePrintf(fp, "_default:use=%s\n", DefaultPrinter->name);
for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
p;
p = (cupsd_printer_t *)cupsArrayNext(Printers))
cupsFilePrintf(fp, "%s:\\\n"
"\t:bsdaddr=%s,%s:\\\n"
"\t:description=%s:\n",
p->name, ServerName, p->name,
p->info ? p->info : "");
break;
}
}
cupsFileClose(fp);
}
char *
cupsdSanitizeURI(const char *uri,
char *buffer,
int buflen)
{
char *start,
*slash,
*ptr;
if (!uri || !buffer || buflen < 2)
return (NULL);
strlcpy(buffer, uri, buflen);
if ((ptr = strchr(buffer, ':')) == NULL)
return (buffer);
for (start = ptr + 1; *start; start ++)
if (*start != '/')
break;
if ((slash = strchr(start, '/')) == NULL)
slash = start + strlen(start);
if ((ptr = strchr(start, '@')) != NULL && ptr < slash)
{
_cups_strcpy(start, ptr + 1);
}
return (buffer);
}
static void
add_printer_defaults(cupsd_printer_t *p)
{
int i;
int num_options;
cups_option_t *options,
*option;
char name[256];
if (!CommonDefaults)
{
CommonDefaults = cupsArrayNew((cups_array_func_t)strcmp, NULL);
cupsArrayAdd(CommonDefaults, _cupsStrAlloc("copies-default"));
cupsArrayAdd(CommonDefaults, _cupsStrAlloc("document-format-default"));
cupsArrayAdd(CommonDefaults, _cupsStrAlloc("finishings-default"));
cupsArrayAdd(CommonDefaults, _cupsStrAlloc("job-hold-until-default"));
cupsArrayAdd(CommonDefaults, _cupsStrAlloc("job-priority-default"));
cupsArrayAdd(CommonDefaults, _cupsStrAlloc("job-sheets-default"));
cupsArrayAdd(CommonDefaults, _cupsStrAlloc("media-default"));
cupsArrayAdd(CommonDefaults, _cupsStrAlloc("number-up-default"));
cupsArrayAdd(CommonDefaults,
_cupsStrAlloc("orientation-requested-default"));
cupsArrayAdd(CommonDefaults, _cupsStrAlloc("sides-default"));
}
for (num_options = 0, options = NULL, i = p->num_options, option = p->options;
i > 0;
i --, option ++)
{
if (strcmp(option->name, "ipp-options") &&
strcmp(option->name, "job-sheets") &&
strcmp(option->name, "lease-duration"))
{
snprintf(name, sizeof(name), "%s-default", option->name);
num_options = cupsAddOption(name, option->value, num_options, &options);
if (!cupsArrayFind(CommonDefaults, name))
cupsArrayAdd(CommonDefaults, _cupsStrAlloc(name));
}
}
cupsEncodeOptions2(p->attrs, num_options, options, IPP_TAG_PRINTER);
cupsFreeOptions(num_options, options);
if (!cupsGetOption("copies", p->num_options, p->options))
ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER, "copies-default",
1);
if (!cupsGetOption("document-format", p->num_options, p->options))
ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_MIMETYPE,
"document-format-default", NULL, "application/octet-stream");
if (!cupsGetOption("job-hold-until", p->num_options, p->options))
ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
"job-hold-until-default", NULL, "no-hold");
if (!cupsGetOption("job-priority", p->num_options, p->options))
ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
"job-priority-default", 50);
if (!cupsGetOption("number-up", p->num_options, p->options))
ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
"number-up-default", 1);
if (!cupsGetOption("orientation-requested", p->num_options, p->options))
ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NOVALUE,
"orientation-requested-default", NULL, NULL);
if (!cupsGetOption("notify-lease-duration", p->num_options, p->options))
ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
"notify-lease-duration-default", DefaultLeaseDuration);
if (!cupsGetOption("notify-events", p->num_options, p->options))
ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
"notify-events-default", NULL, "job-completed");
}
static void
add_printer_filter(
cupsd_printer_t *p,
mime_type_t *filtertype,
const char *filter)
{
char super[MIME_MAX_SUPER],
type[MIME_MAX_TYPE],
program[1024];
int cost;
mime_type_t *temptype;
char filename[1024];
if (sscanf(filter, "%15[^/]/%31s%d%*[ \t]%1023[^\n]", super, type, &cost,
program) != 4)
{
cupsdLogMessage(CUPSD_LOG_ERROR, "%s: invalid filter string \"%s\"!",
p->name, filter);
return;
}
if (strcmp(program, "-"))
{
if (program[0] == '/')
strlcpy(filename, program, sizeof(filename));
else
snprintf(filename, sizeof(filename), "%s/filter/%s", ServerBin, program);
if (access(filename, X_OK))
{
snprintf(p->state_message, sizeof(p->state_message),
"Filter \"%s\" for printer \"%s\" not available: %s",
program, p->name, strerror(errno));
cupsdSetPrinterReasons(p, "+cups-missing-filter-error");
cupsdSetPrinterState(p, IPP_PRINTER_STOPPED, 0);
cupsdLogMessage(CUPSD_LOG_ERROR, "%s", p->state_message);
}
}
if (!strcasecmp(super, "application") &&
!strcasecmp(type, "vnd.cups-command"))
p->type |= CUPS_PRINTER_COMMANDS;
for (temptype = mimeFirstType(MimeDatabase);
temptype;
temptype = mimeNextType(MimeDatabase))
if (((super[0] == '*' && strcasecmp(temptype->super, "printer")) ||
!strcasecmp(temptype->super, super)) &&
(type[0] == '*' || !strcasecmp(temptype->type, type)))
{
cupsdLogMessage(CUPSD_LOG_DEBUG2,
"add_printer_filter: %s: adding filter %s/%s %s/%s %d %s",
p->name, temptype->super, temptype->type,
filtertype->super, filtertype->type,
cost, program);
mimeAddFilter(MimeDatabase, temptype, filtertype, cost, program);
}
}
static void
add_printer_formats(cupsd_printer_t *p)
{
int i;
mime_type_t *type;
cups_array_t *filters;
ipp_attribute_t *attr;
char mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2];
cupsArrayDelete(p->filetypes);
p->filetypes = NULL;
if (p->raw)
{
ippAddStrings(p->attrs, IPP_TAG_PRINTER,
(ipp_tag_t)(IPP_TAG_MIMETYPE | IPP_TAG_COPY),
"document-format-supported", NumMimeTypes, NULL, MimeTypes);
return;
}
cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_printer_formats: %d types, %d filters",
mimeNumTypes(MimeDatabase), mimeNumFilters(MimeDatabase));
p->filetypes = cupsArrayNew(NULL, NULL);
for (type = mimeFirstType(MimeDatabase);
type;
type = mimeNextType(MimeDatabase))
{
snprintf(mimetype, sizeof(mimetype), "%s/%s", type->super, type->type);
if ((filters = mimeFilter(MimeDatabase, type, p->filetype, NULL)) != NULL)
{
cupsdLogMessage(CUPSD_LOG_DEBUG2,
"add_printer_formats: %s: %s needs %d filters",
p->name, mimetype, cupsArrayCount(filters));
cupsArrayDelete(filters);
cupsArrayAdd(p->filetypes, type);
}
else
cupsdLogMessage(CUPSD_LOG_DEBUG2,
"add_printer_formats: %s: %s not supported",
p->name, mimetype);
}
cupsdLogMessage(CUPSD_LOG_DEBUG2,
"add_printer_formats: %s: %d supported types",
p->name, cupsArrayCount(p->filetypes) + 1);
if ((type = mimeType(MimeDatabase, "application", "octet-stream")) == NULL ||
!cupsArrayFind(p->filetypes, type))
i = 1;
else
i = 0;
attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_MIMETYPE,
"document-format-supported",
cupsArrayCount(p->filetypes) + 1, NULL, NULL);
if (i)
attr->values[0].string.text = _cupsStrAlloc("application/octet-stream");
for (type = (mime_type_t *)cupsArrayFirst(p->filetypes);
type;
i ++, type = (mime_type_t *)cupsArrayNext(p->filetypes))
{
snprintf(mimetype, sizeof(mimetype), "%s/%s", type->super, type->type);
attr->values[i].string.text = _cupsStrAlloc(mimetype);
}
#ifdef HAVE_DNSSD
{
char pdl[1024];
mime_filter_t *filter;
pdl[0] = '\0';
if (mimeType(MimeDatabase, "application", "pdf"))
strlcat(pdl, "application/pdf,", sizeof(pdl));
if (mimeType(MimeDatabase, "application", "postscript"))
strlcat(pdl, "application/postscript,", sizeof(pdl));
if (mimeType(MimeDatabase, "application", "vnd.cups-raster"))
strlcat(pdl, "application/vnd.cups-raster,", sizeof(pdl));
for (filter = (mime_filter_t *)cupsArrayFirst(MimeDatabase->filters);
filter;
filter = (mime_filter_t *)cupsArrayNext(MimeDatabase->filters))
{
if (filter->dst == p->filetype && filter->filter &&
strstr(filter->filter, "PrintJobMgr"))
break;
}
if (!filter && mimeType(MimeDatabase, "application", "octet-stream"))
strlcat(pdl, "application/octet-stream,", sizeof(pdl));
if (mimeType(MimeDatabase, "image", "png"))
strlcat(pdl, "image/png,", sizeof(pdl));
if (pdl[0])
pdl[strlen(pdl) - 1] = '\0';
cupsdSetString(&p->pdl, pdl);
}
#endif
}
static int
compare_printers(void *first,
void *second,
void *data)
{
return (strcasecmp(((cupsd_printer_t *)first)->name,
((cupsd_printer_t *)second)->name));
}
static void
delete_printer_filters(
cupsd_printer_t *p)
{
mime_filter_t *filter;
if (p == NULL)
return;
for (filter = mimeFirstFilter(MimeDatabase);
filter;
filter = mimeNextFilter(MimeDatabase))
if (filter->dst == p->filetype)
{
mimeDeleteFilter(MimeDatabase, filter);
}
}
#ifdef __sgi
static void
write_irix_config(cupsd_printer_t *p)
{
char filename[1024];
cups_file_t *fp;
ipp_attribute_t *attr;
snprintf(filename, sizeof(filename), "/var/spool/lp/interface/%s", p->name);
if (p->type & CUPS_PRINTER_CLASS)
unlink(filename);
else if ((fp = cupsFileOpen(filename, "w")) != NULL)
{
cupsFilePuts(fp, "#!/bin/sh\n");
if ((attr = ippFindAttribute(p->attrs, "printer-make-and-model",
IPP_TAG_TEXT)) != NULL)
cupsFilePrintf(fp, "NAME=\"%s\"\n", attr->values[0].string.text);
else if (p->type & CUPS_PRINTER_CLASS)
cupsFilePuts(fp, "NAME=\"Printer Class\"\n");
else
cupsFilePuts(fp, "NAME=\"Remote Destination\"\n");
if (p->type & CUPS_PRINTER_COLOR)
cupsFilePuts(fp, "TYPE=ColorPostScript\n");
else
cupsFilePuts(fp, "TYPE=MonoPostScript\n");
cupsFilePrintf(fp, "HOSTNAME=%s\n", ServerName);
cupsFilePrintf(fp, "HOSTPRINTER=%s\n", p->name);
cupsFileClose(fp);
chmod(filename, 0755);
chown(filename, User, Group);
}
snprintf(filename, sizeof(filename), "/var/spool/lp/member/%s", p->name);
if (p->type & CUPS_PRINTER_CLASS)
unlink(filename);
else if ((fp = cupsFileOpen(filename, "w")) != NULL)
{
cupsFilePuts(fp, "/dev/null\n");
cupsFileClose(fp);
chmod(filename, 0644);
chown(filename, User, Group);
}
snprintf(filename, sizeof(filename), "/var/spool/lp/gui_interface/ELF/%s.gui", p->name);
if (p->type & CUPS_PRINTER_CLASS)
unlink(filename);
else if ((fp = cupsFileOpen(filename, "w")) != NULL)
{
cupsFilePuts(fp, "#!/bin/sh\n");
cupsFilePrintf(fp, "%s -d %s -o \"$3\"\n", PrintcapGUI, p->name);
cupsFileClose(fp);
chmod(filename, 0755);
chown(filename, User, Group);
}
snprintf(filename, sizeof(filename), "/var/spool/lp/pod/%s.config", p->name);
if (p->type & CUPS_PRINTER_CLASS)
unlink(filename);
else if ((fp = cupsFileOpen(filename, "w")) != NULL)
{
cupsFilePrintf(fp, "Printer Class | %s\n",
(p->type & CUPS_PRINTER_COLOR) ? "ColorPostScript" : "MonoPostScript");
cupsFilePrintf(fp, "Printer Model | %s\n", p->make_model ? p->make_model : "");
cupsFilePrintf(fp, "Location Code | %s\n", p->location ? p->location : "");
cupsFilePrintf(fp, "Physical Location | %s\n", p->info ? p->info : "");
cupsFilePrintf(fp, "Port Path | %s\n", p->device_uri ? p->device_uri : "");
cupsFilePrintf(fp, "Config Path | /var/spool/lp/pod/%s.config\n", p->name);
cupsFilePrintf(fp, "Active Status Path | /var/spool/lp/pod/%s.status\n", p->name);
cupsFilePuts(fp, "Status Update Wait | 10 seconds\n");
cupsFileClose(fp);
chmod(filename, 0664);
chown(filename, User, Group);
}
}
static void
write_irix_state(cupsd_printer_t *p)
{
char filename[1024];
cups_file_t *fp;
int tag;
if (p)
{
snprintf(filename, sizeof(filename), "/var/spool/lp/pod/%s.status", p->name);
if (p->type & CUPS_PRINTER_CLASS)
unlink(filename);
else if ((fp = cupsFileOpen(filename, "w")) != NULL)
{
cupsFilePrintf(fp, "Operational Status | %s\n",
(p->state == IPP_PRINTER_IDLE) ? "Idle" :
(p->state == IPP_PRINTER_PROCESSING) ? "Busy" :
"Faulted");
cupsFilePrintf(fp, "Information | 01 00 00 | %s\n", CUPS_SVERSION);
cupsFilePrintf(fp, "Information | 02 00 00 | Device URI: %s\n",
p->device_uri ? p->device_uri : "");
cupsFilePrintf(fp, "Information | 03 00 00 | %s jobs\n",
p->accepting ? "Accepting" : "Not accepting");
cupsFilePrintf(fp, "Information | 04 00 00 | %s\n", p->state_message);
cupsFileClose(fp);
chmod(filename, 0664);
chown(filename, User, Group);
}
snprintf(filename, sizeof(filename), "/var/spool/lp/activeicons/%s", p->name);
if (p->type & CUPS_PRINTER_CLASS)
unlink(filename);
else if ((fp = cupsFileOpen(filename, "w")) != NULL)
{
if (p->type & CUPS_PRINTER_COLOR)
tag = 66240;
else
tag = 66272;
if (p->type & CUPS_PRINTER_REMOTE)
tag |= 8;
if (p->state == IPP_PRINTER_PROCESSING)
tag |= 1;
else if (p->state == IPP_PRINTER_STOPPED)
tag |= 2;
cupsFilePuts(fp, "#!/bin/sh\n");
cupsFilePrintf(fp, "#Tag %d\n", tag);
cupsFileClose(fp);
chmod(filename, 0755);
chown(filename, User, Group);
}
}
snprintf(filename, sizeof(filename), "/var/spool/lp/default");
if (DefaultPrinter != NULL)
{
if ((fp = cupsFileOpen(filename, "w")) != NULL)
{
cupsFilePrintf(fp, "%s\n", DefaultPrinter->name);
cupsFileClose(fp);
chmod(filename, 0644);
chown(filename, User, Group);
}
}
else
unlink(filename);
}
#endif