2 * "$Id: printers.c 7968 2008-09-19 23:03:01Z mike $"
4 * Printer routines for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2009 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
17 * cupsdAddPrinter() - Add a printer to the system.
18 * cupsdAddPrinterHistory() - Add the current printer state to the history.
19 * cupsdAddPrinterUser() - Add a user to the ACL.
20 * cupsdCreateCommonData() - Create the common printer data.
21 * cupsdDeleteAllPrinters() - Delete all printers from the system.
22 * cupsdDeletePrinter() - Delete a printer from the system.
23 * cupsdFindDest() - Find a destination in the list.
24 * cupsdFindPrinter() - Find a printer in the list.
25 * cupsdFreePrinterUsers() - Free allow/deny users.
26 * cupsdLoadAllPrinters() - Load printers from the printers.conf file.
27 * cupsdRenamePrinter() - Rename a printer.
28 * cupsdSaveAllPrinters() - Save all printer definitions to the
30 * cupsdSetAuthInfoRequired() - Set the required authentication info.
31 * cupsdSetDeviceURI() - Set the device URI for a printer.
32 * cupsdSetPrinterAttr() - Set a printer attribute.
33 * cupsdSetPrinterAttrs() - Set printer attributes based upon the PPD
35 * cupsdSetPrinterReasons() - Set/update the reasons strings.
36 * cupsdSetPrinterState() - Update the current state of a printer.
37 * cupsdStopPrinter() - Stop a printer from printing any jobs...
38 * cupsdUpdatePrinterPPD() - Update keywords in a printer's PPD file.
39 * cupsdUpdatePrinters() - Update printers after a partial reload.
40 * cupsdValidateDest() - Validate a printer/class destination.
41 * cupsdWritePrintcap() - Write a pseudo-printcap file for older
42 * applications that need it...
43 * add_printer_defaults() - Add name-default attributes to the printer
45 * add_printer_filter() - Add a MIME filter for a printer.
46 * add_printer_formats() - Add document-format-supported values for a
48 * add_string_array() - Add a string to an array of CUPS strings.
49 * compare_printers() - Compare two printers.
50 * delete_printer_filters() - Delete all MIME filters for a printer.
51 * delete_string_array() - Delete an array of CUPS strings.
52 * load_ppd() - Load a cached PPD file, updating the cache as
54 * write_irix_config() - Update the config files used by the IRIX
56 * write_irix_state() - Update the status files used by IRIX printing
58 * write_xml_string() - Write a string with XML escaping.
62 * Include necessary headers...
67 #include <cups/pwgmedia.h>
74 static void add_printer_defaults(cupsd_printer_t
*p
);
75 static void add_printer_filter(cupsd_printer_t
*p
, mime_type_t
*type
,
77 static void add_printer_formats(cupsd_printer_t
*p
);
78 static void add_string_array(cups_array_t
**a
, const char *s
);
79 static int compare_printers(void *first
, void *second
, void *data
);
80 static void delete_printer_filters(cupsd_printer_t
*p
);
81 static void delete_string_array(cups_array_t
**a
);
82 static void load_ppd(cupsd_printer_t
*p
);
84 static void write_irix_config(cupsd_printer_t
*p
);
85 static void write_irix_state(cupsd_printer_t
*p
);
87 static void write_xml_string(cups_file_t
*fp
, const char *s
);
91 * 'cupsdAddPrinter()' - Add a printer to the system.
94 cupsd_printer_t
* /* O - New printer */
95 cupsdAddPrinter(const char *name
) /* I - Name of printer */
97 cupsd_printer_t
*p
; /* New printer */
101 * Range check input...
104 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAddPrinter(\"%s\")", name
);
107 * Create a new printer entity...
110 if ((p
= calloc(1, sizeof(cupsd_printer_t
))) == NULL
)
112 cupsdLogMessage(CUPSD_LOG_CRIT
, "Unable to allocate memory for printer - %s",
117 cupsdSetString(&p
->name
, name
);
118 cupsdSetString(&p
->info
, name
);
119 cupsdSetString(&p
->hostname
, ServerName
);
121 cupsdSetStringf(&p
->uri
, "ipp://%s:%d/printers/%s", ServerName
, LocalPort
, name
);
122 cupsdSetDeviceURI(p
, "file:///dev/null");
124 p
->state
= IPP_PRINTER_STOPPED
;
125 p
->state_time
= time(NULL
);
127 p
->shared
= DefaultShared
;
128 p
->filetype
= mimeAddType(MimeDatabase
, "printer", name
);
130 cupsdSetString(&p
->job_sheets
[0], "none");
131 cupsdSetString(&p
->job_sheets
[1], "none");
133 cupsdSetString(&p
->error_policy
, ErrorPolicy
);
134 cupsdSetString(&p
->op_policy
, DefaultPolicy
);
136 p
->op_policy_ptr
= DefaultPolicyPtr
;
138 if (MaxPrinterHistory
)
139 p
->history
= calloc(MaxPrinterHistory
, sizeof(ipp_t
*));
142 * Insert the printer in the printer list alphabetically...
146 Printers
= cupsArrayNew(compare_printers
, NULL
);
148 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
149 "cupsdAddPrinter: Adding %s to Printers", p
->name
);
150 cupsArrayAdd(Printers
, p
);
152 if (!ImplicitPrinters
)
153 ImplicitPrinters
= cupsArrayNew(compare_printers
, NULL
);
156 * Return the new printer...
164 * 'cupsdAddPrinterHistory()' - Add the current printer state to the history.
168 cupsdAddPrinterHistory(
169 cupsd_printer_t
*p
) /* I - Printer */
171 ipp_t
*history
; /* History collection */
175 * Stop early if we aren't keeping history data...
178 if (MaxPrinterHistory
<= 0)
182 * Retire old history data as needed...
185 p
->sequence_number
++;
187 if (p
->num_history
>= MaxPrinterHistory
)
190 ippDelete(p
->history
[0]);
191 memmove(p
->history
, p
->history
+ 1, p
->num_history
* sizeof(ipp_t
*));
195 * Create a collection containing the current printer-state, printer-up-time,
196 * printer-state-message, and printer-state-reasons attributes.
200 ippAddInteger(history
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
202 ippAddBoolean(history
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs",
204 ippAddBoolean(history
, IPP_TAG_PRINTER
, "printer-is-shared", p
->shared
);
205 ippAddString(history
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-state-message",
206 NULL
, p
->state_message
);
209 ippAddString(history
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
210 "com.apple.print.recoverable-message", NULL
, p
->recoverable
);
211 #endif /* __APPLE__ */
212 if (p
->num_reasons
== 0)
213 ippAddString(history
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
214 "printer-state-reasons", NULL
, "none");
216 ippAddStrings(history
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
217 "printer-state-reasons", p
->num_reasons
, NULL
,
218 (const char * const *)p
->reasons
);
219 ippAddInteger(history
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
220 "printer-state-change-time", p
->state_time
);
221 ippAddInteger(history
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
222 "printer-state-sequence-number", p
->sequence_number
);
224 p
->history
[p
->num_history
] = history
;
230 * 'cupsdAddPrinterUser()' - Add a user to the ACL.
235 cupsd_printer_t
*p
, /* I - Printer */
236 const char *username
) /* I - User */
238 const char **temp
; /* Temporary array pointer */
244 if (p
->num_users
== 0)
245 temp
= malloc(sizeof(char **));
247 temp
= realloc(p
->users
, sizeof(char **) * (p
->num_users
+ 1));
253 temp
+= p
->num_users
;
255 if ((*temp
= strdup(username
)) != NULL
)
261 * 'cupsdCreateCommonData()' - Create the common printer data.
265 cupsdCreateCommonData(void)
267 int i
; /* Looping var */
268 ipp_attribute_t
*attr
; /* Attribute data */
269 cups_dir_t
*dir
; /* Notifier directory */
270 cups_dentry_t
*dent
; /* Notifier directory entry */
271 cups_array_t
*notifiers
; /* Notifier array */
272 char filename
[1024], /* Filename */
273 *notifier
; /* Current notifier */
274 cupsd_policy_t
*p
; /* Current policy */
275 static const int nups
[] = /* number-up-supported values */
276 { 1, 2, 4, 6, 9, 16 };
277 static const int orients
[4] =/* orientation-requested-supported values */
281 IPP_REVERSE_LANDSCAPE
,
284 static const char * const holds
[] = /* job-hold-until-supported values */
295 static const char * const versions
[] =/* ipp-versions-supported values */
302 static const int ops
[] = /* operations-supported values */
309 IPP_GET_JOB_ATTRIBUTES
,
311 IPP_GET_PRINTER_ATTRIBUTES
,
317 IPP_SET_PRINTER_ATTRIBUTES
,
318 IPP_SET_JOB_ATTRIBUTES
,
319 IPP_GET_PRINTER_SUPPORTED_VALUES
,
320 IPP_CREATE_PRINTER_SUBSCRIPTION
,
321 IPP_CREATE_JOB_SUBSCRIPTION
,
322 IPP_GET_SUBSCRIPTION_ATTRIBUTES
,
323 IPP_GET_SUBSCRIPTIONS
,
324 IPP_RENEW_SUBSCRIPTION
,
325 IPP_CANCEL_SUBSCRIPTION
,
326 IPP_GET_NOTIFICATIONS
,
330 IPP_RELEASE_HELD_NEW_JOBS
,
344 CUPS_AUTHENTICATE_JOB
,
349 static const char * const charsets
[] =/* charset-supported values */
354 static const char * const compressions
[] =
355 { /* document-compression-supported values */
359 #endif /* HAVE_LIBZ */
361 static const char * const media_col_supported
[] =
362 { /* media-col-supported values */
368 static const char * const multiple_document_handling
[] =
369 { /* multiple-document-handling-supported values */
370 "separate-documents-uncollated-copies",
371 "separate-documents-collated-copies"
373 static const char * const errors
[] = /* printer-error-policy-supported values */
380 static const char * const notify_attrs
[] =
381 { /* notify-attributes-supported values */
382 "printer-state-change-time",
383 "notify-lease-expiration-time",
384 "notify-subscriber-user-name"
386 static const char * const notify_events
[] =
387 { /* notify-events-supported values */
389 "job-config-changed",
396 "printer-config-changed",
398 "printer-finishings-changed",
399 "printer-media-changed",
403 "printer-state-changed",
410 static const char * const job_settable
[] =
411 { /* job-settable-attributes-supported */
417 "multiple-document-handling",
419 "orientation-requested",
422 "printer-resolution",
425 static const char * const printer_settable
[] =
426 { /* printer-settable-attributes-supported */
433 ippDelete(CommonData
);
435 CommonData
= ippNew();
438 * This list of attributes is sorted to improve performance when the
439 * client provides a requested-attributes attribute...
442 /* charset-configured */
443 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_CHARSET
| IPP_TAG_COPY
,
444 "charset-configured", NULL
, DefaultCharset
);
446 /* charset-supported */
447 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_CHARSET
| IPP_TAG_COPY
,
448 "charset-supported", sizeof(charsets
) / sizeof(charsets
[0]),
451 /* compression-supported */
452 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
453 "compression-supported",
454 sizeof(compressions
) / sizeof(compressions
[0]),
457 /* copies-supported */
458 ippAddRange(CommonData
, IPP_TAG_PRINTER
, "copies-supported", 1, MaxCopies
);
461 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
| IPP_TAG_COPY
,
462 "cups-version", NULL
, CUPS_SVERSION
+ 6);
464 /* generated-natural-language-supported */
465 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_LANGUAGE
| IPP_TAG_COPY
,
466 "generated-natural-language-supported", NULL
, DefaultLanguage
);
468 /* ipp-versions-supported */
469 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
470 "ipp-versions-supported", sizeof(versions
) / sizeof(versions
[0]),
473 /* job-hold-until-supported */
474 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
475 "job-hold-until-supported", sizeof(holds
) / sizeof(holds
[0]),
478 /* job-priority-supported */
479 ippAddInteger(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
480 "job-priority-supported", 100);
482 /* job-settable-attributes-supported */
483 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
484 "job-settable-attributes-supported",
485 sizeof(job_settable
) / sizeof(job_settable
[0]),
488 /* job-sheets-supported */
489 if (cupsArrayCount(Banners
) > 0)
492 * Setup the job-sheets-supported attribute...
495 if (Classification
&& !ClassifyOverride
)
496 attr
= ippAddString(CommonData
, IPP_TAG_PRINTER
,
497 IPP_TAG_NAME
| IPP_TAG_COPY
,
498 "job-sheets-supported", NULL
, Classification
);
500 attr
= ippAddStrings(CommonData
, IPP_TAG_PRINTER
,
501 IPP_TAG_NAME
| IPP_TAG_COPY
,
502 "job-sheets-supported", cupsArrayCount(Banners
) + 1,
506 cupsdLogMessage(CUPSD_LOG_EMERG
,
507 "Unable to allocate memory for "
508 "job-sheets-supported attribute: %s!", strerror(errno
));
509 else if (!Classification
|| ClassifyOverride
)
511 cupsd_banner_t
*banner
; /* Current banner */
514 attr
->values
[0].string
.text
= _cupsStrAlloc("none");
516 for (i
= 1, banner
= (cupsd_banner_t
*)cupsArrayFirst(Banners
);
518 i
++, banner
= (cupsd_banner_t
*)cupsArrayNext(Banners
))
519 attr
->values
[i
].string
.text
= banner
->name
;
523 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_NAME
| IPP_TAG_COPY
,
524 "job-sheets-supported", NULL
, "none");
526 /* media-col-supported */
527 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
528 "media-col-supported",
529 sizeof(media_col_supported
) /
530 sizeof(media_col_supported
[0]), NULL
,
531 media_col_supported
);
533 /* multiple-document-handling-supported */
534 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
535 "multiple-document-handling-supported",
536 sizeof(multiple_document_handling
) /
537 sizeof(multiple_document_handling
[0]), NULL
,
538 multiple_document_handling
);
540 /* multiple-document-jobs-supported */
541 ippAddBoolean(CommonData
, IPP_TAG_PRINTER
,
542 "multiple-document-jobs-supported", 1);
544 /* multiple-operation-time-out */
545 ippAddInteger(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
546 "multiple-operation-time-out", MultipleOperationTimeout
);
548 /* natural-language-configured */
549 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_LANGUAGE
| IPP_TAG_COPY
,
550 "natural-language-configured", NULL
, DefaultLanguage
);
552 /* notify-attributes-supported */
553 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
554 "notify-attributes-supported",
555 (int)(sizeof(notify_attrs
) / sizeof(notify_attrs
[0])),
558 /* notify-lease-duration-supported */
559 ippAddRange(CommonData
, IPP_TAG_PRINTER
,
560 "notify-lease-duration-supported", 0,
561 MaxLeaseDuration
? MaxLeaseDuration
: 2147483647);
563 /* notify-max-events-supported */
564 ippAddInteger(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
565 "notify-max-events-supported", MaxEvents
);
567 /* notify-events-supported */
568 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
569 "notify-events-supported",
570 (int)(sizeof(notify_events
) / sizeof(notify_events
[0])),
571 NULL
, notify_events
);
573 /* notify-pull-method-supported */
574 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
575 "notify-pull-method-supported", NULL
, "ippget");
577 /* notify-schemes-supported */
578 snprintf(filename
, sizeof(filename
), "%s/notifier", ServerBin
);
579 if ((dir
= cupsDirOpen(filename
)) != NULL
)
581 notifiers
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
583 while ((dent
= cupsDirRead(dir
)) != NULL
)
584 if (S_ISREG(dent
->fileinfo
.st_mode
) &&
585 (dent
->fileinfo
.st_mode
& S_IXOTH
) != 0)
586 cupsArrayAdd(notifiers
, _cupsStrAlloc(dent
->filename
));
588 if (cupsArrayCount(notifiers
) > 0)
590 attr
= ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
591 "notify-schemes-supported",
592 cupsArrayCount(notifiers
), NULL
, NULL
);
594 for (i
= 0, notifier
= (char *)cupsArrayFirst(notifiers
);
596 i
++, notifier
= (char *)cupsArrayNext(notifiers
))
597 attr
->values
[i
].string
.text
= notifier
;
600 cupsArrayDelete(notifiers
);
604 /* number-up-supported */
605 ippAddIntegers(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
606 "number-up-supported", sizeof(nups
) / sizeof(nups
[0]), nups
);
608 /* operations-supported */
609 ippAddIntegers(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
610 "operations-supported",
611 sizeof(ops
) / sizeof(ops
[0]) + JobFiles
- 1, ops
);
613 /* orientation-requested-supported */
614 ippAddIntegers(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
615 "orientation-requested-supported", 4, orients
);
617 /* page-ranges-supported */
618 ippAddBoolean(CommonData
, IPP_TAG_PRINTER
, "page-ranges-supported", 1);
620 /* pdf-override-supported */
621 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
622 "pdl-override-supported", NULL
, "not-attempted");
624 /* printer-error-policy-supported */
625 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_NAME
| IPP_TAG_COPY
,
626 "printer-error-policy-supported",
627 sizeof(errors
) / sizeof(errors
[0]), NULL
, errors
);
629 /* printer-op-policy-supported */
630 attr
= ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_NAME
| IPP_TAG_COPY
,
631 "printer-op-policy-supported", cupsArrayCount(Policies
),
633 for (i
= 0, p
= (cupsd_policy_t
*)cupsArrayFirst(Policies
);
635 i
++, p
= (cupsd_policy_t
*)cupsArrayNext(Policies
))
636 attr
->values
[i
].string
.text
= p
->name
;
638 /* printer-settable-attributes-supported */
639 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
640 "printer-settable-attributes-supported",
641 sizeof(printer_settable
) / sizeof(printer_settable
[0]),
642 NULL
, printer_settable
);
644 /* server-is-sharing-printers */
645 ippAddBoolean(CommonData
, IPP_TAG_PRINTER
, "server-is-sharing-printers",
646 BrowseLocalProtocols
!= 0 && Browsing
);
651 * 'cupsdDeleteAllPrinters()' - Delete all printers from the system.
655 cupsdDeleteAllPrinters(void)
657 cupsd_printer_t
*p
; /* Pointer to current printer/class */
660 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
662 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
663 cupsdDeletePrinter(p
, 0);
668 * 'cupsdDeletePrinter()' - Delete a printer from the system.
673 cupsd_printer_t
*p
, /* I - Printer to delete */
674 int update
) /* I - Update printers.conf? */
676 int i
; /* Looping var */
678 char filename
[1024]; /* Interface script filename */
682 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdDeletePrinter(p=%p(%s), update=%d)",
686 * Save the current position in the Printers array...
689 cupsArraySave(Printers
);
692 * Stop printing on this printer...
695 cupsdSetPrinterState(p
, IPP_PRINTER_STOPPED
, update
);
698 cupsdSetJobState(p
->job
, IPP_JOB_PENDING
, CUPSD_JOB_FORCE
,
699 update
? "Job stopped due to printer being deleted." :
703 * If this printer is the next for browsing, point to the next one...
708 cupsArrayFind(Printers
, p
);
709 BrowseNext
= (cupsd_printer_t
*)cupsArrayNext(Printers
);
713 * Remove the printer from the list...
716 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
717 "cupsdDeletePrinter: Removing %s from Printers", p
->name
);
718 cupsArrayRemove(Printers
, p
);
720 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
722 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
723 "cupsdDeletePrinter: Removing %s from ImplicitPrinters",
725 cupsArrayRemove(ImplicitPrinters
, p
);
729 * Remove the dummy interface/icon/option files under IRIX...
733 snprintf(filename
, sizeof(filename
), "/var/spool/lp/interface/%s", p
->name
);
736 snprintf(filename
, sizeof(filename
), "/var/spool/lp/gui_interface/ELF/%s.gui",
740 snprintf(filename
, sizeof(filename
), "/var/spool/lp/activeicons/%s", p
->name
);
743 snprintf(filename
, sizeof(filename
), "/var/spool/lp/pod/%s.config", p
->name
);
746 snprintf(filename
, sizeof(filename
), "/var/spool/lp/pod/%s.status", p
->name
);
749 snprintf(filename
, sizeof(filename
), "/var/spool/lp/member/%s", p
->name
);
754 * If p is the default printer, assign a different one...
757 if (p
== DefaultPrinter
)
759 DefaultPrinter
= NULL
;
761 if (UseNetworkDefault
)
764 * Find the first network default printer and use it...
767 cupsd_printer_t
*dp
; /* New default printer */
770 for (dp
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
772 dp
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
773 if (dp
!= p
&& (dp
->type
& CUPS_PRINTER_DEFAULT
))
782 * Remove this printer from any classes...
785 if (!(p
->type
& CUPS_PRINTER_IMPLICIT
))
787 cupsdDeletePrinterFromClasses(p
);
790 * Deregister from any browse protocols...
793 cupsdDeregisterPrinter(p
, 1);
797 * Free all memory used by the printer...
800 if (p
->printers
!= NULL
)
803 if (MaxPrinterHistory
)
805 for (i
= 0; i
< p
->num_history
; i
++)
806 ippDelete(p
->history
[i
]);
811 delete_printer_filters(p
);
813 for (i
= 0; i
< p
->num_reasons
; i
++)
814 _cupsStrFree(p
->reasons
[i
]);
817 ippDelete(p
->ppd_attrs
);
819 mimeDeleteType(MimeDatabase
, p
->filetype
);
820 mimeDeleteType(MimeDatabase
, p
->prefiltertype
);
822 delete_string_array(&(p
->filters
));
823 delete_string_array(&(p
->pre_filters
));
825 cupsdFreePrinterUsers(p
);
828 cupsdClearString(&p
->uri
);
829 cupsdClearString(&p
->hostname
);
830 cupsdClearString(&p
->name
);
831 cupsdClearString(&p
->location
);
832 cupsdClearString(&p
->make_model
);
833 cupsdClearString(&p
->info
);
834 cupsdClearString(&p
->job_sheets
[0]);
835 cupsdClearString(&p
->job_sheets
[1]);
836 cupsdClearString(&p
->device_uri
);
837 cupsdClearString(&p
->sanitized_device_uri
);
838 cupsdClearString(&p
->port_monitor
);
839 cupsdClearString(&p
->op_policy
);
840 cupsdClearString(&p
->error_policy
);
842 cupsdClearString(&p
->alert
);
843 cupsdClearString(&p
->alert_description
);
846 cupsdClearString(&p
->product
);
847 cupsdClearString(&p
->pdl
);
848 #endif /* HAVE_DNSSD */
850 cupsArrayDelete(p
->filetypes
);
853 free(p
->browse_attrs
);
856 cupsdClearString(&p
->recoverable
);
857 #endif /* __APPLE__ */
859 cupsFreeOptions(p
->num_options
, p
->options
);
864 * Restore the previous position in the Printers array...
867 cupsArrayRestore(Printers
);
872 * 'cupsdFindDest()' - Find a destination in the list.
875 cupsd_printer_t
* /* O - Destination in list */
876 cupsdFindDest(const char *name
) /* I - Name of printer or class to find */
878 cupsd_printer_t key
; /* Search key */
881 key
.name
= (char *)name
;
882 return ((cupsd_printer_t
*)cupsArrayFind(Printers
, &key
));
887 * 'cupsdFindPrinter()' - Find a printer in the list.
890 cupsd_printer_t
* /* O - Printer in list */
891 cupsdFindPrinter(const char *name
) /* I - Name of printer to find */
893 cupsd_printer_t
*p
; /* Printer in list */
896 if ((p
= cupsdFindDest(name
)) != NULL
&& (p
->type
& CUPS_PRINTER_CLASS
))
904 * 'cupsdFreePrinterUsers()' - Free allow/deny users.
908 cupsdFreePrinterUsers(
909 cupsd_printer_t
*p
) /* I - Printer */
911 int i
; /* Looping var */
914 if (!p
|| !p
->num_users
)
917 for (i
= 0; i
< p
->num_users
; i
++)
918 free((void *)p
->users
[i
]);
928 * 'cupsdLoadAllPrinters()' - Load printers from the printers.conf file.
932 cupsdLoadAllPrinters(void)
934 int i
; /* Looping var */
935 cups_file_t
*fp
; /* printers.conf file */
936 int linenum
; /* Current line number */
937 char line
[4096], /* Line from file */
938 *value
, /* Pointer to value */
939 *valueptr
; /* Pointer into value */
940 cupsd_printer_t
*p
; /* Current printer */
944 * Open the printers.conf file...
947 snprintf(line
, sizeof(line
), "%s/printers.conf", ServerRoot
);
948 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
951 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open %s - %s", line
,
957 * Read printer configurations until we hit EOF...
963 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
966 * Decode the directive...
969 if (!strcasecmp(line
, "<Printer") ||
970 !strcasecmp(line
, "<DefaultPrinter"))
973 * <Printer name> or <DefaultPrinter name>
976 if (p
== NULL
&& value
)
979 * Add the printer and a base file type...
982 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Loading printer %s...", value
);
984 p
= cupsdAddPrinter(value
);
986 p
->state
= IPP_PRINTER_IDLE
;
989 * Set the default printer as needed...
992 if (!strcasecmp(line
, "<DefaultPrinter"))
996 cupsdLogMessage(CUPSD_LOG_ERROR
,
997 "Syntax error on line %d of printers.conf.", linenum
);
999 else if (!strcasecmp(line
, "</Printer>"))
1004 * Close out the current printer...
1007 cupsdSetPrinterAttrs(p
);
1008 cupsdAddPrinterHistory(p
);
1010 if (strncmp(p
->device_uri
, "file:", 5) &&
1011 p
->state
!= IPP_PRINTER_STOPPED
)
1014 * See if the backend exists...
1017 snprintf(line
, sizeof(line
), "%s/backend/%s", ServerBin
,
1020 if ((valueptr
= strchr(line
+ strlen(ServerBin
), ':')) != NULL
)
1021 *valueptr
= '\0'; /* Chop everything but URI scheme */
1023 if (access(line
, 0))
1026 * Backend does not exist, stop printer...
1029 p
->state
= IPP_PRINTER_STOPPED
;
1030 snprintf(p
->state_message
, sizeof(p
->state_message
),
1031 "Backend %s does not exist!", line
);
1038 cupsdLogMessage(CUPSD_LOG_ERROR
,
1039 "Syntax error on line %d of printers.conf.", linenum
);
1043 cupsdLogMessage(CUPSD_LOG_ERROR
,
1044 "Syntax error on line %d of printers.conf.", linenum
);
1046 else if (!strcasecmp(line
, "AuthInfoRequired"))
1048 if (!cupsdSetAuthInfoRequired(p
, value
, NULL
))
1049 cupsdLogMessage(CUPSD_LOG_ERROR
,
1050 "Bad AuthInfoRequired on line %d of printers.conf.",
1053 else if (!strcasecmp(line
, "Info"))
1056 cupsdSetString(&p
->info
, value
);
1058 else if (!strcasecmp(line
, "MakeModel"))
1061 cupsdSetString(&p
->make_model
, value
);
1063 else if (!strcasecmp(line
, "Location"))
1066 cupsdSetString(&p
->location
, value
);
1068 else if (!strcasecmp(line
, "DeviceURI"))
1071 cupsdSetDeviceURI(p
, value
);
1073 cupsdLogMessage(CUPSD_LOG_ERROR
,
1074 "Syntax error on line %d of printers.conf.", linenum
);
1076 else if (!strcasecmp(line
, "Option") && value
)
1082 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
1085 cupsdLogMessage(CUPSD_LOG_ERROR
,
1086 "Syntax error on line %d of printers.conf.", linenum
);
1089 for (; *valueptr
&& isspace(*valueptr
& 255); *valueptr
++ = '\0');
1091 p
->num_options
= cupsAddOption(value
, valueptr
, p
->num_options
,
1095 else if (!strcasecmp(line
, "PortMonitor"))
1097 if (value
&& strcmp(value
, "none"))
1098 cupsdSetString(&p
->port_monitor
, value
);
1100 cupsdClearString(&p
->port_monitor
);
1102 cupsdLogMessage(CUPSD_LOG_ERROR
,
1103 "Syntax error on line %d of printers.conf.", linenum
);
1105 else if (!strcasecmp(line
, "Reason"))
1108 strcmp(value
, "com.apple.print.recoverable-warning") &&
1109 strcmp(value
, "connecting-to-device") &&
1110 strcmp(value
, "cups-insecure-filter-warning") &&
1111 strcmp(value
, "cups-missing-filter-warning"))
1113 for (i
= 0 ; i
< p
->num_reasons
; i
++)
1114 if (!strcmp(value
, p
->reasons
[i
]))
1117 if (i
>= p
->num_reasons
&&
1118 p
->num_reasons
< (int)(sizeof(p
->reasons
) / sizeof(p
->reasons
[0])))
1120 p
->reasons
[p
->num_reasons
] = _cupsStrAlloc(value
);
1125 cupsdLogMessage(CUPSD_LOG_ERROR
,
1126 "Syntax error on line %d of printers.conf.", linenum
);
1128 else if (!strcasecmp(line
, "State"))
1131 * Set the initial queue state...
1134 if (value
&& !strcasecmp(value
, "idle"))
1135 p
->state
= IPP_PRINTER_IDLE
;
1136 else if (value
&& !strcasecmp(value
, "stopped"))
1138 p
->state
= IPP_PRINTER_STOPPED
;
1139 cupsdSetPrinterReasons(p
, "+paused");
1142 cupsdLogMessage(CUPSD_LOG_ERROR
,
1143 "Syntax error on line %d of printers.conf.", linenum
);
1145 else if (!strcasecmp(line
, "StateMessage"))
1148 * Set the initial queue state message...
1152 strlcpy(p
->state_message
, value
, sizeof(p
->state_message
));
1154 else if (!strcasecmp(line
, "StateTime"))
1157 * Set the state time...
1161 p
->state_time
= atoi(value
);
1163 else if (!strcasecmp(line
, "Accepting"))
1166 * Set the initial accepting state...
1170 (!strcasecmp(value
, "yes") ||
1171 !strcasecmp(value
, "on") ||
1172 !strcasecmp(value
, "true")))
1175 (!strcasecmp(value
, "no") ||
1176 !strcasecmp(value
, "off") ||
1177 !strcasecmp(value
, "false")))
1180 cupsdLogMessage(CUPSD_LOG_ERROR
,
1181 "Syntax error on line %d of printers.conf.", linenum
);
1183 else if (!strcasecmp(line
, "Type"))
1186 p
->type
= atoi(value
);
1188 cupsdLogMessage(CUPSD_LOG_ERROR
,
1189 "Syntax error on line %d of printers.conf.", linenum
);
1191 else if (!strcasecmp(line
, "Product"))
1196 p
->product
= _cupsStrAlloc(value
);
1197 #endif /* HAVE_DNSSD */
1200 cupsdLogMessage(CUPSD_LOG_ERROR
,
1201 "Syntax error on line %d of printers.conf.", linenum
);
1203 else if (!strcasecmp(line
, "Filter"))
1208 p
->filters
= cupsArrayNew(NULL
, NULL
);
1210 cupsArrayAdd(p
->filters
, _cupsStrAlloc(value
));
1213 cupsdLogMessage(CUPSD_LOG_ERROR
,
1214 "Syntax error on line %d of printers.conf.", linenum
);
1216 else if (!strcasecmp(line
, "PreFilter"))
1220 if (!p
->pre_filters
)
1221 p
->pre_filters
= cupsArrayNew(NULL
, NULL
);
1223 cupsArrayAdd(p
->pre_filters
, _cupsStrAlloc(value
));
1226 cupsdLogMessage(CUPSD_LOG_ERROR
,
1227 "Syntax error on line %d of printers.conf.", linenum
);
1229 else if (!strcasecmp(line
, "Shared"))
1232 * Set the initial shared state...
1236 (!strcasecmp(value
, "yes") ||
1237 !strcasecmp(value
, "on") ||
1238 !strcasecmp(value
, "true")))
1241 (!strcasecmp(value
, "no") ||
1242 !strcasecmp(value
, "off") ||
1243 !strcasecmp(value
, "false")))
1246 cupsdLogMessage(CUPSD_LOG_ERROR
,
1247 "Syntax error on line %d of printers.conf.", linenum
);
1249 else if (!strcasecmp(line
, "JobSheets"))
1252 * Set the initial job sheets...
1257 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
1262 cupsdSetString(&p
->job_sheets
[0], value
);
1264 while (isspace(*valueptr
& 255))
1269 for (value
= valueptr
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
1274 cupsdSetString(&p
->job_sheets
[1], value
);
1278 cupsdLogMessage(CUPSD_LOG_ERROR
,
1279 "Syntax error on line %d of printers.conf.", linenum
);
1281 else if (!strcasecmp(line
, "AllowUser"))
1286 cupsdAddPrinterUser(p
, value
);
1289 cupsdLogMessage(CUPSD_LOG_ERROR
,
1290 "Syntax error on line %d of printers.conf.", linenum
);
1292 else if (!strcasecmp(line
, "DenyUser"))
1297 cupsdAddPrinterUser(p
, value
);
1300 cupsdLogMessage(CUPSD_LOG_ERROR
,
1301 "Syntax error on line %d of printers.conf.", linenum
);
1303 else if (!strcasecmp(line
, "QuotaPeriod"))
1306 p
->quota_period
= atoi(value
);
1308 cupsdLogMessage(CUPSD_LOG_ERROR
,
1309 "Syntax error on line %d of printers.conf.", linenum
);
1311 else if (!strcasecmp(line
, "PageLimit"))
1314 p
->page_limit
= atoi(value
);
1316 cupsdLogMessage(CUPSD_LOG_ERROR
,
1317 "Syntax error on line %d of printers.conf.", linenum
);
1319 else if (!strcasecmp(line
, "KLimit"))
1322 p
->k_limit
= atoi(value
);
1324 cupsdLogMessage(CUPSD_LOG_ERROR
,
1325 "Syntax error on line %d of printers.conf.", linenum
);
1327 else if (!strcasecmp(line
, "OpPolicy"))
1331 cupsd_policy_t
*pol
; /* Policy */
1334 if ((pol
= cupsdFindPolicy(value
)) != NULL
)
1336 cupsdSetString(&p
->op_policy
, value
);
1337 p
->op_policy_ptr
= pol
;
1340 cupsdLogMessage(CUPSD_LOG_ERROR
,
1341 "Bad policy \"%s\" on line %d of printers.conf",
1345 cupsdLogMessage(CUPSD_LOG_ERROR
,
1346 "Syntax error on line %d of printers.conf.", linenum
);
1348 else if (!strcasecmp(line
, "ErrorPolicy"))
1351 cupsdSetString(&p
->error_policy
, value
);
1353 cupsdLogMessage(CUPSD_LOG_ERROR
,
1354 "Syntax error on line %d of printers.conf.", linenum
);
1356 else if (!strcasecmp(line
, "Attribute") && value
)
1358 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
1361 cupsdLogMessage(CUPSD_LOG_ERROR
,
1362 "Syntax error on line %d of printers.conf.", linenum
);
1365 for (; *valueptr
&& isspace(*valueptr
& 255); *valueptr
++ = '\0');
1368 cupsdSetPrinterAttrs(p
);
1370 if (!strcmp(value
, "marker-change-time"))
1371 p
->marker_time
= atoi(valueptr
);
1373 cupsdSetPrinterAttr(p
, value
, valueptr
);
1379 * Something else we don't understand...
1382 cupsdLogMessage(CUPSD_LOG_ERROR
,
1383 "Unknown configuration directive %s on line %d of printers.conf.",
1393 * 'cupsdRenamePrinter()' - Rename a printer.
1398 cupsd_printer_t
*p
, /* I - Printer */
1399 const char *name
) /* I - New name */
1402 * Remove the printer from the array(s) first...
1405 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1406 "cupsdRenamePrinter: Removing %s from Printers", p
->name
);
1407 cupsArrayRemove(Printers
, p
);
1409 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
1411 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1412 "cupsdRenamePrinter: Removing %s from ImplicitPrinters",
1414 cupsArrayRemove(ImplicitPrinters
, p
);
1418 * Rename the printer type...
1421 mimeDeleteType(MimeDatabase
, p
->filetype
);
1422 p
->filetype
= mimeAddType(MimeDatabase
, "printer", name
);
1424 mimeDeleteType(MimeDatabase
, p
->prefiltertype
);
1425 p
->prefiltertype
= mimeAddType(MimeDatabase
, "prefilter", name
);
1428 * Rename the printer...
1431 cupsdSetString(&p
->name
, name
);
1434 * Reset printer attributes...
1437 cupsdSetPrinterAttrs(p
);
1440 * Add the printer back to the printer array(s)...
1443 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1444 "cupsdRenamePrinter: Adding %s to Printers", p
->name
);
1445 cupsArrayAdd(Printers
, p
);
1447 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
1449 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1450 "cupsdRenamePrinter: Adding %s to ImplicitPrinters",
1452 cupsArrayAdd(ImplicitPrinters
, p
);
1458 * 'cupsdSaveAllPrinters()' - Save all printer definitions to the printers.conf
1463 cupsdSaveAllPrinters(void)
1465 int i
; /* Looping var */
1466 cups_file_t
*fp
; /* printers.conf file */
1467 char temp
[1024], /* Temporary string */
1468 backup
[1024], /* printers.conf.O file */
1469 value
[2048], /* Value string */
1470 *ptr
; /* Pointer into value */
1471 cupsd_printer_t
*printer
; /* Current printer class */
1472 time_t curtime
; /* Current time */
1473 struct tm
*curdate
; /* Current date */
1474 cups_option_t
*option
; /* Current option */
1475 ipp_attribute_t
*marker
; /* Current marker attribute */
1479 * Create the printers.conf file...
1482 snprintf(temp
, sizeof(temp
), "%s/printers.conf", ServerRoot
);
1483 snprintf(backup
, sizeof(backup
), "%s/printers.conf.O", ServerRoot
);
1485 if (rename(temp
, backup
))
1487 if (errno
!= ENOENT
)
1488 cupsdLogMessage(CUPSD_LOG_ERROR
,
1489 "Unable to backup printers.conf - %s", strerror(errno
));
1492 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
1494 cupsdLogMessage(CUPSD_LOG_ERROR
,
1495 "Unable to save printers.conf - %s", strerror(errno
));
1497 if (rename(backup
, temp
))
1498 cupsdLogMessage(CUPSD_LOG_ERROR
,
1499 "Unable to restore printers.conf - %s", strerror(errno
));
1503 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving printers.conf...");
1506 * Restrict access to the file...
1509 fchown(cupsFileNumber(fp
), getuid(), Group
);
1510 fchmod(cupsFileNumber(fp
), 0600);
1513 * Write a small header to the file...
1516 curtime
= time(NULL
);
1517 curdate
= localtime(&curtime
);
1518 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
1520 cupsFilePuts(fp
, "# Printer configuration file for " CUPS_SVERSION
"\n");
1521 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
1524 * Write each local printer known to the system...
1527 for (printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1529 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1532 * Skip remote destinations and printer classes...
1535 if ((printer
->type
& CUPS_PRINTER_DISCOVERED
) ||
1536 (printer
->type
& CUPS_PRINTER_CLASS
) ||
1537 (printer
->type
& CUPS_PRINTER_IMPLICIT
))
1541 * Write printers as needed...
1544 if (printer
== DefaultPrinter
)
1545 cupsFilePrintf(fp
, "<DefaultPrinter %s>\n", printer
->name
);
1547 cupsFilePrintf(fp
, "<Printer %s>\n", printer
->name
);
1549 if (printer
->num_auth_info_required
> 0)
1551 switch (printer
->num_auth_info_required
)
1554 strlcpy(value
, printer
->auth_info_required
[0], sizeof(value
));
1558 snprintf(value
, sizeof(value
), "%s,%s",
1559 printer
->auth_info_required
[0],
1560 printer
->auth_info_required
[1]);
1565 snprintf(value
, sizeof(value
), "%s,%s,%s",
1566 printer
->auth_info_required
[0],
1567 printer
->auth_info_required
[1],
1568 printer
->auth_info_required
[2]);
1572 cupsFilePutConf(fp
, "AuthInfoRequired", value
);
1576 cupsFilePutConf(fp
, "Info", printer
->info
);
1578 if (printer
->location
)
1579 cupsFilePutConf(fp
, "Location", printer
->location
);
1581 if (printer
->make_model
)
1582 cupsFilePutConf(fp
, "MakeModel", printer
->make_model
);
1584 cupsFilePutConf(fp
, "DeviceURI", printer
->device_uri
);
1586 if (printer
->port_monitor
)
1587 cupsFilePutConf(fp
, "PortMonitor", printer
->port_monitor
);
1589 if (printer
->state
== IPP_PRINTER_STOPPED
)
1591 cupsFilePuts(fp
, "State Stopped\n");
1593 if (printer
->state_message
)
1594 cupsFilePutConf(fp
, "StateMessage", printer
->state_message
);
1597 cupsFilePuts(fp
, "State Idle\n");
1599 cupsFilePrintf(fp
, "StateTime %d\n", (int)printer
->state_time
);
1601 for (i
= 0; i
< printer
->num_reasons
; i
++)
1602 if (strcmp(printer
->reasons
[i
], "com.apple.print.recoverable-warning") &&
1603 strcmp(printer
->reasons
[i
], "connecting-to-device") &&
1604 strcmp(printer
->reasons
[i
], "cups-insecure-filter-warning") &&
1605 strcmp(printer
->reasons
[i
], "cups-missing-filter-warning"))
1606 cupsFilePutConf(fp
, "Reason", printer
->reasons
[i
]);
1608 cupsFilePrintf(fp
, "Type %d\n", printer
->type
);
1611 if (printer
->product
)
1612 cupsFilePutConf(fp
, "Product", printer
->product
);
1613 #endif /* HAVE_DNSSD */
1615 for (ptr
= (char *)cupsArrayFirst(printer
->filters
);
1617 ptr
= (char *)cupsArrayNext(printer
->filters
))
1618 cupsFilePutConf(fp
, "Filter", ptr
);
1620 for (ptr
= (char *)cupsArrayFirst(printer
->pre_filters
);
1622 ptr
= (char *)cupsArrayNext(printer
->pre_filters
))
1623 cupsFilePutConf(fp
, "PreFilter", ptr
);
1625 if (printer
->accepting
)
1626 cupsFilePuts(fp
, "Accepting Yes\n");
1628 cupsFilePuts(fp
, "Accepting No\n");
1630 if (printer
->shared
)
1631 cupsFilePuts(fp
, "Shared Yes\n");
1633 cupsFilePuts(fp
, "Shared No\n");
1635 snprintf(value
, sizeof(value
), "%s %s", printer
->job_sheets
[0],
1636 printer
->job_sheets
[1]);
1637 cupsFilePutConf(fp
, "JobSheets", value
);
1639 cupsFilePrintf(fp
, "QuotaPeriod %d\n", printer
->quota_period
);
1640 cupsFilePrintf(fp
, "PageLimit %d\n", printer
->page_limit
);
1641 cupsFilePrintf(fp
, "KLimit %d\n", printer
->k_limit
);
1643 for (i
= 0; i
< printer
->num_users
; i
++)
1644 cupsFilePutConf(fp
, printer
->deny_users
? "DenyUser" : "AllowUser",
1647 if (printer
->op_policy
)
1648 cupsFilePutConf(fp
, "OpPolicy", printer
->op_policy
);
1649 if (printer
->error_policy
)
1650 cupsFilePutConf(fp
, "ErrorPolicy", printer
->error_policy
);
1652 for (i
= printer
->num_options
, option
= printer
->options
;
1656 snprintf(value
, sizeof(value
), "%s %s", option
->name
, option
->value
);
1657 cupsFilePutConf(fp
, "Option", value
);
1660 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-colors",
1661 IPP_TAG_NAME
)) != NULL
)
1663 snprintf(value
, sizeof(value
), "%s ", marker
->name
);
1665 for (i
= 0, ptr
= value
+ strlen(value
);
1666 i
< marker
->num_values
&& ptr
< (value
+ sizeof(value
) - 1);
1672 strlcpy(ptr
, marker
->values
[i
].string
.text
,
1673 value
+ sizeof(value
) - ptr
);
1678 cupsFilePutConf(fp
, "Attribute", value
);
1681 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-levels",
1682 IPP_TAG_INTEGER
)) != NULL
)
1684 cupsFilePrintf(fp
, "Attribute %s %d", marker
->name
,
1685 marker
->values
[0].integer
);
1686 for (i
= 1; i
< marker
->num_values
; i
++)
1687 cupsFilePrintf(fp
, ",%d", marker
->values
[i
].integer
);
1688 cupsFilePuts(fp
, "\n");
1691 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-low-levels",
1692 IPP_TAG_INTEGER
)) != NULL
)
1694 cupsFilePrintf(fp
, "Attribute %s %d", marker
->name
,
1695 marker
->values
[0].integer
);
1696 for (i
= 1; i
< marker
->num_values
; i
++)
1697 cupsFilePrintf(fp
, ",%d", marker
->values
[i
].integer
);
1698 cupsFilePuts(fp
, "\n");
1701 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-high-levels",
1702 IPP_TAG_INTEGER
)) != NULL
)
1704 cupsFilePrintf(fp
, "Attribute %s %d", marker
->name
,
1705 marker
->values
[0].integer
);
1706 for (i
= 1; i
< marker
->num_values
; i
++)
1707 cupsFilePrintf(fp
, ",%d", marker
->values
[i
].integer
);
1708 cupsFilePuts(fp
, "\n");
1711 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-message",
1712 IPP_TAG_TEXT
)) != NULL
)
1714 snprintf(value
, sizeof(value
), "%s %s", marker
->name
,
1715 marker
->values
[0].string
.text
);
1717 cupsFilePutConf(fp
, "Attribute", value
);
1720 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-names",
1721 IPP_TAG_NAME
)) != NULL
)
1723 snprintf(value
, sizeof(value
), "%s ", marker
->name
);
1725 for (i
= 0, ptr
= value
+ strlen(value
);
1726 i
< marker
->num_values
&& ptr
< (value
+ sizeof(value
) - 1);
1732 strlcpy(ptr
, marker
->values
[i
].string
.text
,
1733 value
+ sizeof(value
) - ptr
);
1738 cupsFilePutConf(fp
, "Attribute", value
);
1741 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-types",
1742 IPP_TAG_KEYWORD
)) != NULL
)
1744 snprintf(value
, sizeof(value
), "%s ", marker
->name
);
1746 for (i
= 0, ptr
= value
+ strlen(value
);
1747 i
< marker
->num_values
&& ptr
< (value
+ sizeof(value
) - 1);
1753 strlcpy(ptr
, marker
->values
[i
].string
.text
,
1754 value
+ sizeof(value
) - ptr
);
1759 cupsFilePutConf(fp
, "Attribute", value
);
1762 if (printer
->marker_time
)
1763 cupsFilePrintf(fp
, "Attribute marker-change-time %ld\n",
1764 (long)printer
->marker_time
);
1766 cupsFilePuts(fp
, "</Printer>\n");
1770 * Make IRIX desktop & printer status happy
1773 write_irix_state(printer
);
1782 * 'cupsdSetAuthInfoRequired()' - Set the required authentication info.
1785 int /* O - 1 if value OK, 0 otherwise */
1786 cupsdSetAuthInfoRequired(
1787 cupsd_printer_t
*p
, /* I - Printer */
1788 const char *values
, /* I - Plain text value (or NULL) */
1789 ipp_attribute_t
*attr
) /* I - IPP attribute value (or NULL) */
1791 int i
; /* Looping var */
1794 p
->num_auth_info_required
= 0;
1797 * Do we have a plain text value?
1803 * Yes, grab the keywords...
1806 const char *end
; /* End of current value */
1809 while (*values
&& p
->num_auth_info_required
< 4)
1811 if ((end
= strchr(values
, ',')) == NULL
)
1812 end
= values
+ strlen(values
);
1814 if ((end
- values
) == 4 && !strncmp(values
, "none", 4))
1816 if (p
->num_auth_info_required
!= 0 || *end
)
1819 p
->auth_info_required
[p
->num_auth_info_required
] = "none";
1820 p
->num_auth_info_required
++;
1824 else if ((end
- values
) == 9 && !strncmp(values
, "negotiate", 9))
1826 if (p
->num_auth_info_required
!= 0 || *end
)
1829 p
->auth_info_required
[p
->num_auth_info_required
] = "negotiate";
1830 p
->num_auth_info_required
++;
1832 else if ((end
- values
) == 6 && !strncmp(values
, "domain", 6))
1834 p
->auth_info_required
[p
->num_auth_info_required
] = "domain";
1835 p
->num_auth_info_required
++;
1837 else if ((end
- values
) == 8 && !strncmp(values
, "password", 8))
1839 p
->auth_info_required
[p
->num_auth_info_required
] = "password";
1840 p
->num_auth_info_required
++;
1842 else if ((end
- values
) == 8 && !strncmp(values
, "username", 8))
1844 p
->auth_info_required
[p
->num_auth_info_required
] = "username";
1845 p
->num_auth_info_required
++;
1850 values
= (*end
) ? end
+ 1 : end
;
1853 if (p
->num_auth_info_required
== 0)
1855 p
->auth_info_required
[0] = "none";
1856 p
->num_auth_info_required
= 1;
1860 * Update the printer-type value as needed...
1863 if (p
->num_auth_info_required
> 1 ||
1864 strcmp(p
->auth_info_required
[0], "none"))
1865 p
->type
|= CUPS_PRINTER_AUTHENTICATED
;
1867 p
->type
&= ~CUPS_PRINTER_AUTHENTICATED
;
1873 * Grab values from an attribute instead...
1876 if (!attr
|| attr
->num_values
> 4)
1880 * Update the printer-type value as needed...
1883 if (attr
->num_values
> 1 ||
1884 strcmp(attr
->values
[0].string
.text
, "none"))
1885 p
->type
|= CUPS_PRINTER_AUTHENTICATED
;
1887 p
->type
&= ~CUPS_PRINTER_AUTHENTICATED
;
1889 for (i
= 0; i
< attr
->num_values
; i
++)
1891 if (!strcmp(attr
->values
[i
].string
.text
, "none"))
1893 if (p
->num_auth_info_required
!= 0 || attr
->num_values
!= 1)
1896 p
->auth_info_required
[p
->num_auth_info_required
] = "none";
1897 p
->num_auth_info_required
++;
1901 else if (!strcmp(attr
->values
[i
].string
.text
, "negotiate"))
1903 if (p
->num_auth_info_required
!= 0 || attr
->num_values
!= 1)
1906 p
->auth_info_required
[p
->num_auth_info_required
] = "negotiate";
1907 p
->num_auth_info_required
++;
1911 else if (!strcmp(attr
->values
[i
].string
.text
, "domain"))
1913 p
->auth_info_required
[p
->num_auth_info_required
] = "domain";
1914 p
->num_auth_info_required
++;
1916 else if (!strcmp(attr
->values
[i
].string
.text
, "password"))
1918 p
->auth_info_required
[p
->num_auth_info_required
] = "password";
1919 p
->num_auth_info_required
++;
1921 else if (!strcmp(attr
->values
[i
].string
.text
, "username"))
1923 p
->auth_info_required
[p
->num_auth_info_required
] = "username";
1924 p
->num_auth_info_required
++;
1935 * 'cupsdSetDeviceURI()' - Set the device URI for a printer.
1939 cupsdSetDeviceURI(cupsd_printer_t
*p
, /* I - Printer */
1940 const char *uri
) /* I - Device URI */
1942 char buffer
[1024], /* URI buffer */
1943 *start
, /* Start of data after scheme */
1944 *slash
, /* First slash after scheme:// */
1945 *ptr
; /* Pointer into user@host:port part */
1949 * Set the full device URI..
1952 cupsdSetString(&(p
->device_uri
), uri
);
1955 * Copy the device URI to a temporary buffer so we can sanitize any auth
1959 strlcpy(buffer
, uri
, sizeof(buffer
));
1962 * Find the end of the scheme:// part...
1965 if ((ptr
= strchr(buffer
, ':')) != NULL
)
1967 for (start
= ptr
+ 1; *start
; start
++)
1972 * Find the next slash (/) in the URI...
1975 if ((slash
= strchr(start
, '/')) == NULL
)
1976 slash
= start
+ strlen(start
); /* No slash, point to the end */
1979 * Check for an @ sign before the slash...
1982 if ((ptr
= strchr(start
, '@')) != NULL
&& ptr
< slash
)
1985 * Found an @ sign and it is before the resource part, so we have
1986 * an authentication string. Copy the remaining URI over the
1987 * authentication string...
1990 _cups_strcpy(start
, ptr
+ 1);
1995 * Save the sanitized URI...
1998 cupsdSetString(&(p
->sanitized_device_uri
), buffer
);
2003 * 'cupsdSetPrinterAttr()' - Set a printer attribute.
2007 cupsdSetPrinterAttr(
2008 cupsd_printer_t
*p
, /* I - Printer */
2009 const char *name
, /* I - Attribute name */
2010 char *value
) /* I - Attribute value string */
2012 ipp_attribute_t
*attr
; /* Attribute */
2013 int i
, /* Looping var */
2014 count
; /* Number of values */
2015 char *ptr
; /* Pointer into value */
2016 ipp_tag_t value_tag
; /* Value tag for this attribute */
2020 * Don't allow empty values...
2023 if (!*value
&& strcmp(name
, "marker-message"))
2025 cupsdLogMessage(CUPSD_LOG_ERROR
, "Ignoring empty \"%s\" attribute", name
);
2030 * Count the number of values...
2033 for (count
= 1, ptr
= value
;
2034 (ptr
= strchr(ptr
, ',')) != NULL
;
2038 * Then add or update the attribute as needed...
2041 if (!strcmp(name
, "marker-levels") || !strcmp(name
, "marker-low-levels") ||
2042 !strcmp(name
, "marker-high-levels"))
2048 if ((attr
= ippFindAttribute(p
->attrs
, name
, IPP_TAG_INTEGER
)) != NULL
&&
2049 attr
->num_values
< count
)
2051 ippDeleteAttribute(p
->attrs
, attr
);
2056 attr
->num_values
= count
;
2058 attr
= ippAddIntegers(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, name
,
2063 cupsdLogMessage(CUPSD_LOG_ERROR
,
2064 "Unable to allocate memory for printer attribute "
2065 "(%d values)", count
);
2069 for (i
= 0; i
< count
; i
++)
2071 if ((ptr
= strchr(value
, ',')) != NULL
)
2074 attr
->values
[i
].integer
= strtol(value
, NULL
, 10);
2083 * Name or keyword values...
2086 if (!strcmp(name
, "marker-types"))
2087 value_tag
= IPP_TAG_KEYWORD
;
2088 else if (!strcmp(name
, "marker-message"))
2089 value_tag
= IPP_TAG_TEXT
;
2091 value_tag
= IPP_TAG_NAME
;
2093 if ((attr
= ippFindAttribute(p
->attrs
, name
, value_tag
)) != NULL
&&
2094 attr
->num_values
< count
)
2096 ippDeleteAttribute(p
->attrs
, attr
);
2102 for (i
= 0; i
< attr
->num_values
; i
++)
2103 _cupsStrFree(attr
->values
[i
].string
.text
);
2105 attr
->num_values
= count
;
2108 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, value_tag
, name
,
2113 cupsdLogMessage(CUPSD_LOG_ERROR
,
2114 "Unable to allocate memory for printer attribute "
2115 "(%d values)", count
);
2119 for (i
= 0; i
< count
; i
++)
2121 if ((ptr
= strchr(value
, ',')) != NULL
)
2124 attr
->values
[i
].string
.text
= _cupsStrAlloc(value
);
2134 * 'cupsdSetPrinterAttrs()' - Set printer attributes based upon the PPD file.
2138 cupsdSetPrinterAttrs(cupsd_printer_t
*p
)/* I - Printer to setup */
2140 int i
, /* Looping var */
2141 length
; /* Length of browse attributes */
2142 char resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2143 int num_air
; /* Number of auth-info-required values */
2144 const char * const *air
; /* auth-info-required values */
2145 cupsd_location_t
*auth
; /* Pointer to authentication element */
2146 const char *auth_supported
; /* Authentication supported */
2147 ipp_t
*oldattrs
; /* Old printer attributes */
2148 ipp_attribute_t
*attr
; /* Attribute data */
2149 cups_option_t
*option
; /* Current printer option */
2150 char *filter
; /* Current filter */
2151 static const char * const air_userpass
[] =
2152 { /* Basic/Digest authentication */
2157 static const char * const air_negotiate
[] =
2158 { /* Kerberos authentication */
2161 #endif /* HAVE_GSSAPI */
2162 static const char * const air_none
[] =
2163 { /* No authentication */
2168 DEBUG_printf(("cupsdSetPrinterAttrs: entering name = %s, type = %x\n", p
->name
,
2172 * Make sure that we have the common attributes defined...
2176 cupsdCreateCommonData();
2179 * Clear out old filters, if any...
2182 delete_printer_filters(p
);
2185 * Figure out the authentication that is required for the printer.
2188 auth_supported
= "requesting-user-name";
2192 if (p
->num_auth_info_required
> 0 && strcmp(p
->auth_info_required
[0], "none"))
2194 num_air
= p
->num_auth_info_required
;
2195 air
= p
->auth_info_required
;
2197 if (!strcmp(air
[0], "username"))
2198 auth_supported
= "basic";
2200 auth_supported
= "negotiate";
2202 else if (!(p
->type
& CUPS_PRINTER_DISCOVERED
))
2204 if (p
->type
& CUPS_PRINTER_CLASS
)
2205 snprintf(resource
, sizeof(resource
), "/classes/%s", p
->name
);
2207 snprintf(resource
, sizeof(resource
), "/printers/%s", p
->name
);
2209 if ((auth
= cupsdFindBest(resource
, HTTP_POST
)) == NULL
||
2210 auth
->type
== CUPSD_AUTH_NONE
)
2211 auth
= cupsdFindPolicyOp(p
->op_policy_ptr
, IPP_PRINT_JOB
);
2215 int auth_type
; /* Authentication type */
2218 if ((auth_type
= auth
->type
) == CUPSD_AUTH_DEFAULT
)
2219 auth_type
= DefaultAuthType
;
2221 if (auth_type
== CUPSD_AUTH_BASIC
|| auth_type
== CUPSD_AUTH_BASICDIGEST
)
2223 auth_supported
= "basic";
2227 else if (auth_type
== CUPSD_AUTH_DIGEST
)
2229 auth_supported
= "digest";
2234 else if (auth_type
== CUPSD_AUTH_NEGOTIATE
)
2236 auth_supported
= "negotiate";
2238 air
= air_negotiate
;
2240 #endif /* HAVE_GSSAPI */
2242 if (auth_type
!= CUPSD_AUTH_NONE
)
2243 p
->type
|= CUPS_PRINTER_AUTHENTICATED
;
2245 p
->type
&= ~CUPS_PRINTER_AUTHENTICATED
;
2248 p
->type
&= ~CUPS_PRINTER_AUTHENTICATED
;
2250 else if (p
->type
& CUPS_PRINTER_AUTHENTICATED
)
2257 * Create the required IPP attributes for a printer...
2260 oldattrs
= p
->attrs
;
2261 p
->attrs
= ippNew();
2263 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2264 "uri-authentication-supported", NULL
, auth_supported
);
2265 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2266 "uri-security-supported", NULL
, "none");
2267 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "printer-name", NULL
,
2269 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-location",
2270 NULL
, p
->location
? p
->location
: "");
2271 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-info",
2272 NULL
, p
->info
? p
->info
: "");
2277 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2278 "requesting-user-name-denied", p
->num_users
, NULL
,
2281 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2282 "requesting-user-name-allowed", p
->num_users
, NULL
,
2286 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2287 "job-quota-period", p
->quota_period
);
2288 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2289 "job-k-limit", p
->k_limit
);
2290 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2291 "job-page-limit", p
->page_limit
);
2292 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2293 "auth-info-required", num_air
, NULL
, air
);
2295 if (cupsArrayCount(Banners
) > 0 && !(p
->type
& CUPS_PRINTER_DISCOVERED
))
2298 * Setup the job-sheets-default attribute...
2301 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2302 "job-sheets-default", 2, NULL
, NULL
);
2306 attr
->values
[0].string
.text
= _cupsStrAlloc(Classification
?
2307 Classification
: p
->job_sheets
[0]);
2308 attr
->values
[1].string
.text
= _cupsStrAlloc(Classification
?
2309 Classification
: p
->job_sheets
[1]);
2316 if (p
->type
& CUPS_PRINTER_DISCOVERED
)
2319 * Tell the client this is a remote printer of some type...
2322 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
2323 "printer-uri-supported", NULL
, p
->uri
);
2325 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "printer-more-info",
2329 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
2330 "printer-make-and-model", NULL
, p
->make_model
);
2332 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri", NULL
,
2341 * Assign additional attributes depending on whether this is a printer
2345 if (p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
2348 p
->type
&= ~CUPS_PRINTER_OPTIONS
;
2351 * Add class-specific attributes...
2354 if ((p
->type
& CUPS_PRINTER_IMPLICIT
) && p
->num_printers
> 0 &&
2355 p
->printers
[0]->make_model
)
2356 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
2357 "printer-make-and-model", NULL
, p
->printers
[0]->make_model
);
2359 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
2360 "printer-make-and-model", NULL
, "Local Printer Class");
2362 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri", NULL
,
2363 "file:///dev/null");
2365 if (p
->num_printers
> 0)
2368 * Add a list of member names; URIs are added in copy_printer_attrs...
2371 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2372 "member-names", p
->num_printers
, NULL
, NULL
);
2373 p
->type
|= CUPS_PRINTER_OPTIONS
;
2375 for (i
= 0; i
< p
->num_printers
; i
++)
2378 attr
->values
[i
].string
.text
= _cupsStrRetain(p
->printers
[i
]->name
);
2380 p
->type
&= ~CUPS_PRINTER_OPTIONS
| p
->printers
[i
]->type
;
2387 * Add printer-specific attributes...
2390 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri", NULL
,
2391 p
->sanitized_device_uri
);
2394 * Assign additional attributes from the PPD file (if any)...
2400 * Add filters for printer...
2403 cupsdSetPrinterReasons(p
, "-cups-missing-filter-warning,"
2404 "cups-insecure-filter-warning");
2406 for (filter
= (char *)cupsArrayFirst(p
->filters
);
2408 filter
= (char *)cupsArrayNext(p
->filters
))
2409 add_printer_filter(p
, p
->filetype
, filter
);
2413 p
->prefiltertype
= mimeAddType(MimeDatabase
, "prefilter", p
->name
);
2415 for (filter
= (char *)cupsArrayFirst(p
->pre_filters
);
2417 filter
= (char *)cupsArrayNext(p
->pre_filters
))
2418 add_printer_filter(p
, p
->prefiltertype
, filter
);
2424 * Copy marker attributes as needed...
2429 ipp_attribute_t
*oldattr
; /* Old attribute */
2432 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-colors",
2433 IPP_TAG_NAME
)) != NULL
)
2435 if ((attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2436 "marker-colors", oldattr
->num_values
, NULL
,
2439 for (i
= 0; i
< oldattr
->num_values
; i
++)
2440 attr
->values
[i
].string
.text
=
2441 _cupsStrRetain(oldattr
->values
[i
].string
.text
);
2445 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-levels",
2446 IPP_TAG_INTEGER
)) != NULL
)
2448 if ((attr
= ippAddIntegers(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2449 "marker-levels", oldattr
->num_values
,
2452 for (i
= 0; i
< oldattr
->num_values
; i
++)
2453 attr
->values
[i
].integer
= oldattr
->values
[i
].integer
;
2457 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-message",
2458 IPP_TAG_TEXT
)) != NULL
)
2459 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "marker-message",
2460 NULL
, oldattr
->values
[0].string
.text
);
2462 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-low-levels",
2463 IPP_TAG_INTEGER
)) != NULL
)
2465 if ((attr
= ippAddIntegers(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2466 "marker-low-levels", oldattr
->num_values
,
2469 for (i
= 0; i
< oldattr
->num_values
; i
++)
2470 attr
->values
[i
].integer
= oldattr
->values
[i
].integer
;
2474 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-high-levels",
2475 IPP_TAG_INTEGER
)) != NULL
)
2477 if ((attr
= ippAddIntegers(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2478 "marker-high-levels", oldattr
->num_values
,
2481 for (i
= 0; i
< oldattr
->num_values
; i
++)
2482 attr
->values
[i
].integer
= oldattr
->values
[i
].integer
;
2486 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-names",
2487 IPP_TAG_NAME
)) != NULL
)
2489 if ((attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2490 "marker-names", oldattr
->num_values
, NULL
,
2493 for (i
= 0; i
< oldattr
->num_values
; i
++)
2494 attr
->values
[i
].string
.text
=
2495 _cupsStrRetain(oldattr
->values
[i
].string
.text
);
2499 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-types",
2500 IPP_TAG_KEYWORD
)) != NULL
)
2502 if ((attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2503 "marker-types", oldattr
->num_values
, NULL
,
2506 for (i
= 0; i
< oldattr
->num_values
; i
++)
2507 attr
->values
[i
].string
.text
=
2508 _cupsStrRetain(oldattr
->values
[i
].string
.text
);
2512 ippDelete(oldattrs
);
2516 * Force sharing off for remote queues...
2519 if (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
))
2524 * Copy the printer options into a browse attributes string we can re-use.
2527 const char *valptr
; /* Pointer into value */
2528 char *attrptr
; /* Pointer into attribute string */
2532 * Free the old browse attributes as needed...
2535 if (p
->browse_attrs
)
2536 free(p
->browse_attrs
);
2539 * Compute the length of all attributes + job-sheets, lease-duration,
2540 * and BrowseLocalOptions.
2543 for (length
= 1, i
= p
->num_options
, option
= p
->options
;
2547 length
+= strlen(option
->name
) + 2;
2551 for (valptr
= option
->value
; *valptr
; valptr
++)
2552 if (strchr(" \"\'\\", *valptr
))
2559 length
+= 13 + strlen(p
->job_sheets
[0]) + strlen(p
->job_sheets
[1]);
2561 if (BrowseLocalOptions
)
2562 length
+= 12 + strlen(BrowseLocalOptions
);
2564 if (p
->num_auth_info_required
> 0)
2566 length
+= 18; /* auth-info-required */
2568 for (i
= 0; i
< p
->num_auth_info_required
; i
++)
2569 length
+= strlen(p
->auth_info_required
[i
]) + 1;
2573 * Allocate the new string...
2576 if ((p
->browse_attrs
= calloc(1, length
)) == NULL
)
2577 cupsdLogMessage(CUPSD_LOG_ERROR
,
2578 "Unable to allocate %d bytes for browse data!",
2583 * Got the allocated string, now copy the options and attributes over...
2586 sprintf(p
->browse_attrs
, "job-sheets=%s,%s lease-duration=%d",
2587 p
->job_sheets
[0], p
->job_sheets
[1], BrowseTimeout
);
2588 attrptr
= p
->browse_attrs
+ strlen(p
->browse_attrs
);
2590 if (BrowseLocalOptions
)
2592 sprintf(attrptr
, " ipp-options=%s", BrowseLocalOptions
);
2593 attrptr
+= strlen(attrptr
);
2596 for (i
= p
->num_options
, option
= p
->options
;
2601 strcpy(attrptr
, option
->name
);
2602 attrptr
+= strlen(attrptr
);
2608 for (valptr
= option
->value
; *valptr
; valptr
++)
2610 if (strchr(" \"\'\\", *valptr
))
2613 *attrptr
++ = *valptr
;
2618 if (p
->num_auth_info_required
> 0)
2620 strcpy(attrptr
, "auth-info-required");
2623 for (i
= 0; i
< p
->num_auth_info_required
; i
++)
2625 *attrptr
++ = i
? ',' : '=';
2626 strcpy(attrptr
, p
->auth_info_required
[i
]);
2627 attrptr
+= strlen(attrptr
);
2636 * Populate the document-format-supported attribute...
2639 add_printer_formats(p
);
2641 DEBUG_printf(("cupsdSetPrinterAttrs: leaving name = %s, type = %x\n", p
->name
,
2645 * Add name-default attributes...
2648 add_printer_defaults(p
);
2652 * Write the IRIX printer config and status files...
2655 write_irix_config(p
);
2656 write_irix_state(p
);
2660 * Let the browse protocols reflect the change
2663 cupsdRegisterPrinter(p
);
2668 * 'cupsdSetPrinterReasons()' - Set/update the reasons strings.
2671 int /* O - 1 if something changed, 0 otherwise */
2672 cupsdSetPrinterReasons(
2673 cupsd_printer_t
*p
, /* I - Printer */
2674 const char *s
) /* I - Reasons strings */
2676 int i
, /* Looping var */
2677 changed
= 0; /* Did something change? */
2678 const char *sptr
; /* Pointer into reasons */
2679 char reason
[255], /* Reason string */
2680 *rptr
; /* Pointer into reason */
2683 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2684 "cupsdSetPrinterReasons(p=%p(%s),s=\"%s\"", p
, p
->name
, s
);
2686 if (s
[0] == '-' || s
[0] == '+')
2689 * Add/remove reasons...
2697 * Replace reasons...
2702 for (i
= 0; i
< p
->num_reasons
; i
++)
2703 _cupsStrFree(p
->reasons
[i
]);
2708 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
2710 if (PrintcapFormat
== PRINTCAP_PLIST
)
2711 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2714 if (!strcmp(s
, "none"))
2718 * Loop through all of the reasons...
2724 * Skip leading whitespace and commas...
2727 while (isspace(*sptr
& 255) || *sptr
== ',')
2730 for (rptr
= reason
; *sptr
&& !isspace(*sptr
& 255) && *sptr
!= ','; sptr
++)
2731 if (rptr
< (reason
+ sizeof(reason
) - 1))
2745 for (i
= 0; i
< p
->num_reasons
; i
++)
2746 if (!strcmp(reason
, p
->reasons
[i
]))
2749 * Found a match, so remove it...
2754 _cupsStrFree(p
->reasons
[i
]);
2756 if (i
< p
->num_reasons
)
2757 memmove(p
->reasons
+ i
, p
->reasons
+ i
+ 1,
2758 (p
->num_reasons
- i
) * sizeof(char *));
2760 if (!strcmp(reason
, "paused") && p
->state
== IPP_PRINTER_STOPPED
)
2761 cupsdSetPrinterState(p
, IPP_PRINTER_IDLE
, 1);
2763 if (strcmp(reason
, "connecting-to-device"))
2764 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
2766 if (PrintcapFormat
== PRINTCAP_PLIST
)
2767 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2772 else if (p
->num_reasons
< (int)(sizeof(p
->reasons
) / sizeof(p
->reasons
[0])))
2778 for (i
= 0; i
< p
->num_reasons
; i
++)
2779 if (!strcmp(reason
, p
->reasons
[i
]))
2782 if (i
>= p
->num_reasons
)
2784 if (i
>= (int)(sizeof(p
->reasons
) / sizeof(p
->reasons
[0])))
2786 cupsdLogMessage(CUPSD_LOG_ALERT
,
2787 "Too many printer-state-reasons values for %s (%d)",
2792 p
->reasons
[i
] = _cupsStrAlloc(reason
);
2796 if (!strcmp(reason
, "paused") && p
->state
!= IPP_PRINTER_STOPPED
)
2797 cupsdSetPrinterState(p
, IPP_PRINTER_STOPPED
, 1);
2799 if (strcmp(reason
, "connecting-to-device"))
2800 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
2802 if (PrintcapFormat
== PRINTCAP_PLIST
)
2803 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2813 * 'cupsdSetPrinterState()' - Update the current state of a printer.
2817 cupsdSetPrinterState(
2818 cupsd_printer_t
*p
, /* I - Printer to change */
2819 ipp_pstate_t s
, /* I - New state */
2820 int update
) /* I - Update printers.conf? */
2822 ipp_pstate_t old_state
; /* Old printer state */
2826 * Can't set status of remote printers...
2829 if (p
->type
& CUPS_PRINTER_DISCOVERED
)
2833 * Set the new state...
2836 if (PrintcapFormat
== PRINTCAP_PLIST
)
2837 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2839 old_state
= p
->state
;
2844 cupsdAddEvent(s
== IPP_PRINTER_STOPPED
? CUPSD_EVENT_PRINTER_STOPPED
:
2845 CUPSD_EVENT_PRINTER_STATE
, p
, NULL
,
2846 "%s \"%s\" state changed.",
2847 (p
->type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
2851 * Let the browse code know this needs to be updated...
2855 p
->state_time
= time(NULL
);
2859 write_irix_state(p
);
2864 * Set/clear the paused reason as needed...
2867 if (s
== IPP_PRINTER_STOPPED
)
2868 cupsdSetPrinterReasons(p
, "+paused");
2870 cupsdSetPrinterReasons(p
, "-paused");
2873 * Clear the message for the queue when going to processing...
2876 if (s
== IPP_PRINTER_PROCESSING
)
2877 p
->state_message
[0] = '\0';
2880 * Update the printer history...
2883 cupsdAddPrinterHistory(p
);
2886 * Let the browse protocols reflect the change...
2890 cupsdRegisterPrinter(p
);
2893 * Save the printer configuration if a printer goes from idle or processing
2894 * to stopped (or visa-versa)...
2898 (old_state
== IPP_PRINTER_STOPPED
) != (s
== IPP_PRINTER_STOPPED
))
2900 if (p
->type
& CUPS_PRINTER_CLASS
)
2901 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
2903 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
2909 * 'cupsdStopPrinter()' - Stop a printer from printing any jobs...
2913 cupsdStopPrinter(cupsd_printer_t
*p
, /* I - Printer to stop */
2914 int update
)/* I - Update printers.conf? */
2917 * Set the printer state...
2920 cupsdSetPrinterState(p
, IPP_PRINTER_STOPPED
, update
);
2923 * See if we have a job printing on this printer...
2927 cupsdSetJobState(p
->job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
2928 "Job stopped due to printer being paused.");
2933 * 'cupsdUpdatePrinterPPD()' - Update keywords in a printer's PPD file.
2936 int /* O - 1 if successful, 0 otherwise */
2937 cupsdUpdatePrinterPPD(
2938 cupsd_printer_t
*p
, /* I - Printer */
2939 int num_keywords
, /* I - Number of keywords */
2940 cups_option_t
*keywords
) /* I - Keywords */
2942 int i
; /* Looping var */
2943 cups_file_t
*src
, /* Original file */
2944 *dst
; /* New file */
2945 char srcfile
[1024], /* Original filename */
2946 dstfile
[1024], /* New filename */
2947 line
[1024], /* Line from file */
2948 keystring
[41]; /* Keyword from line */
2949 cups_option_t
*keyword
; /* Current keyword */
2952 cupsdLogMessage(CUPSD_LOG_INFO
, "Updating keywords in PPD file for %s...",
2956 * Get the old and new PPD filenames...
2959 snprintf(srcfile
, sizeof(srcfile
), "%s/ppd/%s.ppd.O", ServerRoot
, p
->name
);
2960 snprintf(dstfile
, sizeof(srcfile
), "%s/ppd/%s.ppd", ServerRoot
, p
->name
);
2963 * Rename the old file and open the old and new...
2966 if (rename(dstfile
, srcfile
))
2968 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to backup PPD file for %s: %s",
2969 p
->name
, strerror(errno
));
2973 if ((src
= cupsFileOpen(srcfile
, "r")) == NULL
)
2975 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open PPD file \"%s\": %s",
2976 srcfile
, strerror(errno
));
2977 rename(srcfile
, dstfile
);
2981 if ((dst
= cupsFileOpen(dstfile
, "w")) == NULL
)
2983 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create PPD file \"%s\": %s",
2984 dstfile
, strerror(errno
));
2986 rename(srcfile
, dstfile
);
2991 * Copy the first line and then write out all of the keywords...
2994 if (!cupsFileGets(src
, line
, sizeof(line
)))
2996 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to read PPD file \"%s\": %s",
2997 srcfile
, strerror(errno
));
3000 rename(srcfile
, dstfile
);
3004 cupsFilePrintf(dst
, "%s\n", line
);
3006 for (i
= num_keywords
, keyword
= keywords
; i
> 0; i
--, keyword
++)
3008 cupsdLogMessage(CUPSD_LOG_DEBUG
, "*%s: %s", keyword
->name
, keyword
->value
);
3009 cupsFilePrintf(dst
, "*%s: %s\n", keyword
->name
, keyword
->value
);
3013 * Then copy the rest of the PPD file, dropping any keywords we changed.
3016 while (cupsFileGets(src
, line
, sizeof(line
)))
3019 * Skip keywords we've already set...
3022 if (sscanf(line
, "*%40[^:]:", keystring
) == 1 &&
3023 cupsGetOption(keystring
, num_keywords
, keywords
))
3027 * Otherwise write the line...
3030 cupsFilePrintf(dst
, "%s\n", line
);
3034 * Close files and return...
3045 * 'cupsdUpdatePrinters()' - Update printers after a partial reload.
3049 cupsdUpdatePrinters(void)
3051 cupsd_printer_t
*p
; /* Current printer */
3055 * Loop through the printers and recreate the printer attributes
3056 * for any local printers since the policy and/or access control
3057 * stuff may have changed. Also, if browsing is disabled, remove
3058 * any remote printers...
3061 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3063 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3066 * Remove remote printers if we are no longer browsing...
3070 (p
->type
& (CUPS_PRINTER_IMPLICIT
| CUPS_PRINTER_DISCOVERED
)))
3072 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
3073 cupsArrayRemove(ImplicitPrinters
, p
);
3075 cupsArraySave(Printers
);
3076 cupsdDeletePrinter(p
, 0);
3077 cupsArrayRestore(Printers
);
3082 * Update the operation policy pointer...
3085 if ((p
->op_policy_ptr
= cupsdFindPolicy(p
->op_policy
)) == NULL
)
3086 p
->op_policy_ptr
= DefaultPolicyPtr
;
3089 * Update printer attributes as needed...
3092 if (!(p
->type
& CUPS_PRINTER_DISCOVERED
))
3093 cupsdSetPrinterAttrs(p
);
3099 * 'cupsdValidateDest()' - Validate a printer/class destination.
3102 const char * /* O - Printer or class name */
3104 const char *uri
, /* I - Printer URI */
3105 cups_ptype_t
*dtype
, /* O - Type (printer or class) */
3106 cupsd_printer_t
**printer
) /* O - Printer pointer */
3108 cupsd_printer_t
*p
; /* Current printer */
3109 char localname
[1024],/* Localized hostname */
3110 *lptr
, /* Pointer into localized hostname */
3111 *sptr
, /* Pointer into server name */
3112 *rptr
, /* Pointer into resource */
3113 scheme
[32], /* Scheme portion of URI */
3114 username
[64], /* Username portion of URI */
3115 hostname
[HTTP_MAX_HOST
],
3116 /* Host portion of URI */
3117 resource
[HTTP_MAX_URI
];
3118 /* Resource portion of URI */
3119 int port
; /* Port portion of URI */
3122 DEBUG_printf(("cupsdValidateDest(uri=\"%s\", dtype=%p, printer=%p)\n", uri
,
3126 * Initialize return values...
3133 *dtype
= (cups_ptype_t
)0;
3136 * Pull the hostname and resource from the URI...
3139 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
3140 username
, sizeof(username
), hostname
, sizeof(hostname
),
3141 &port
, resource
, sizeof(resource
));
3144 * See if the resource is a class or printer...
3147 if (!strncmp(resource
, "/classes/", 9))
3153 rptr
= resource
+ 9;
3155 else if (!strncmp(resource
, "/printers/", 10))
3161 rptr
= resource
+ 10;
3166 * Bad resource name...
3173 * See if the printer or class name exists...
3176 p
= cupsdFindDest(rptr
);
3178 if (p
== NULL
&& strchr(rptr
, '@') == NULL
)
3186 *dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
|
3187 CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
);
3193 * Change localhost to the server name...
3196 if (!strcasecmp(hostname
, "localhost"))
3197 strlcpy(hostname
, ServerName
, sizeof(hostname
));
3199 strlcpy(localname
, hostname
, sizeof(localname
));
3201 if (!strcasecmp(hostname
, ServerName
))
3204 * Localize the hostname...
3207 lptr
= strchr(localname
, '.');
3208 sptr
= strchr(ServerName
, '.');
3210 if (sptr
!= NULL
&& lptr
!= NULL
)
3213 * Strip the common domain name components...
3216 while (lptr
!= NULL
)
3218 if (!strcasecmp(lptr
, sptr
))
3224 lptr
= strchr(lptr
+ 1, '.');
3229 DEBUG_printf(("localized hostname is \"%s\"...\n", localname
));
3232 * Find a matching printer or class...
3235 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3237 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3238 if (!strcasecmp(p
->hostname
, localname
) &&
3239 !strcasecmp(p
->name
, rptr
))
3245 *dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
|
3246 CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
);
3256 * 'cupsdWritePrintcap()' - Write a pseudo-printcap file for older applications
3261 cupsdWritePrintcap(void)
3263 int i
; /* Looping var */
3264 cups_file_t
*fp
; /* Printcap file */
3265 cupsd_printer_t
*p
; /* Current printer */
3270 * Update the IRIX printer state for the default printer; if
3271 * no printers remain, then the default printer file will be
3275 write_irix_state(DefaultPrinter
);
3279 * See if we have a printcap file; if not, don't bother writing it.
3282 if (!Printcap
|| !*Printcap
)
3286 * Open the printcap file...
3289 if ((fp
= cupsFileOpen(Printcap
, "w")) == NULL
)
3293 * Put a comment header at the top so that users will know where the
3294 * data has come from...
3297 if (PrintcapFormat
!= PRINTCAP_PLIST
)
3298 cupsFilePrintf(fp
, "# This file was automatically generated by cupsd(8) "
3300 "# %s/printers.conf file. All changes to this file\n"
3301 "# will be lost.\n", ServerRoot
);
3304 * Write a new printcap with the current list of printers.
3307 switch (PrintcapFormat
)
3311 * Each printer is put in the file as:
3321 cupsFilePrintf(fp
, "%s|%s:rm=%s:rp=%s:\n", DefaultPrinter
->name
,
3322 DefaultPrinter
->info
, ServerName
,
3323 DefaultPrinter
->name
);
3325 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3327 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3328 if (p
!= DefaultPrinter
)
3329 cupsFilePrintf(fp
, "%s|%s:rm=%s:rp=%s:\n", p
->name
, p
->info
,
3330 ServerName
, p
->name
);
3333 case PRINTCAP_PLIST
:
3335 * Each printer is written as a dictionary in a plist file.
3336 * Currently the printer-name, printer-info, printer-is-accepting-jobs,
3337 * printer-location, printer-make-and-model, printer-state,
3338 * printer-state-reasons, printer-type, and (sanitized) device-uri.
3341 cupsFilePuts(fp
, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
3342 "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD "
3343 "PLIST 1.0//EN\" \"http://www.apple.com/DTDs/"
3344 "PropertyList-1.0.dtd\">\n"
3345 "<plist version=\"1.0\">\n"
3348 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3350 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3352 cupsFilePuts(fp
, "\t<dict>\n"
3353 "\t\t<key>printer-name</key>\n"
3355 write_xml_string(fp
, p
->name
);
3356 cupsFilePuts(fp
, "</string>\n"
3357 "\t\t<key>printer-info</key>\n"
3359 write_xml_string(fp
, p
->info
);
3360 cupsFilePrintf(fp
, "</string>\n"
3361 "\t\t<key>printer-is-accepting-jobs</key>\n"
3363 "\t\t<key>printer-location</key>\n"
3364 "\t\t<string>", p
->accepting
? "true" : "false");
3365 write_xml_string(fp
, p
->location
);
3366 cupsFilePuts(fp
, "</string>\n"
3367 "\t\t<key>printer-make-and-model</key>\n"
3369 write_xml_string(fp
, p
->make_model
);
3370 cupsFilePrintf(fp
, "</string>\n"
3371 "\t\t<key>printer-state</key>\n"
3372 "\t\t<integer>%d</integer>\n"
3373 "\t\t<key>printer-state-reasons</key>\n"
3374 "\t\t<array>\n", p
->state
);
3375 for (i
= 0; i
< p
->num_reasons
; i
++)
3377 cupsFilePuts(fp
, "\t\t\t<string>");
3378 write_xml_string(fp
, p
->reasons
[i
]);
3379 cupsFilePuts(fp
, "</string>\n");
3381 cupsFilePrintf(fp
, "\t\t</array>\n"
3382 "\t\t<key>printer-type</key>\n"
3383 "\t\t<integer>%d</integer>\n"
3384 "\t\t<key>device-uri</key>\n"
3385 "\t\t<string>", p
->type
);
3386 write_xml_string(fp
, p
->sanitized_device_uri
);
3387 cupsFilePuts(fp
, "</string>\n"
3390 cupsFilePuts(fp
, "</array>\n"
3394 case PRINTCAP_SOLARIS
:
3396 * Each printer is put in the file as:
3398 * _all:all=Printer1,Printer2,Printer3,...,PrinterN
3399 * _default:use=DefaultPrinter
3401 * :bsdaddr=ServerName,Printer1:\
3402 * :description=Description:
3404 * :bsdaddr=ServerName,Printer2:\
3405 * :description=Description:
3407 * :bsdaddr=ServerName,Printer3:\
3408 * :description=Description:
3411 * :bsdaddr=ServerName,PrinterN:\
3412 * :description=Description:
3415 cupsFilePuts(fp
, "_all:all=");
3416 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3418 p
= (cupsd_printer_t
*)cupsArrayCurrent(Printers
))
3419 cupsFilePrintf(fp
, "%s%c", p
->name
,
3420 cupsArrayNext(Printers
) ? ',' : '\n');
3423 cupsFilePrintf(fp
, "_default:use=%s\n", DefaultPrinter
->name
);
3425 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3427 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3428 cupsFilePrintf(fp
, "%s:\\\n"
3429 "\t:bsdaddr=%s,%s:\\\n"
3430 "\t:description=%s:\n",
3431 p
->name
, ServerName
, p
->name
,
3432 p
->info
? p
->info
: "");
3445 * 'add_printer_defaults()' - Add name-default attributes to the printer attributes.
3449 add_printer_defaults(cupsd_printer_t
*p
)/* I - Printer */
3451 int i
; /* Looping var */
3452 int num_options
; /* Number of default options */
3453 cups_option_t
*options
, /* Default options */
3454 *option
; /* Current option */
3455 char name
[256]; /* name-default */
3459 * Maintain a common array of default attribute names...
3462 if (!CommonDefaults
)
3464 CommonDefaults
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
3466 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("copies-default"));
3467 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("document-format-default"));
3468 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("finishings-default"));
3469 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("job-hold-until-default"));
3470 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("job-priority-default"));
3471 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("job-sheets-default"));
3472 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("media-default"));
3473 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("number-up-default"));
3474 cupsArrayAdd(CommonDefaults
,
3475 _cupsStrAlloc("orientation-requested-default"));
3476 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("sides-default"));
3480 * Add all of the default options from the .conf files...
3483 for (num_options
= 0, options
= NULL
, i
= p
->num_options
, option
= p
->options
;
3487 if (strcmp(option
->name
, "ipp-options") &&
3488 strcmp(option
->name
, "job-sheets") &&
3489 strcmp(option
->name
, "lease-duration"))
3491 snprintf(name
, sizeof(name
), "%s-default", option
->name
);
3492 num_options
= cupsAddOption(name
, option
->value
, num_options
, &options
);
3494 if (!cupsArrayFind(CommonDefaults
, name
))
3495 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc(name
));
3500 * Convert options to IPP attributes...
3503 cupsEncodeOptions2(p
->attrs
, num_options
, options
, IPP_TAG_PRINTER
);
3504 cupsFreeOptions(num_options
, options
);
3507 * Add standard -default attributes as needed...
3510 if (!cupsGetOption("copies", p
->num_options
, p
->options
))
3511 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "copies-default",
3514 if (!cupsGetOption("document-format", p
->num_options
, p
->options
))
3515 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_MIMETYPE
,
3516 "document-format-default", NULL
, "application/octet-stream");
3518 if (!cupsGetOption("job-hold-until", p
->num_options
, p
->options
))
3519 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
3520 "job-hold-until-default", NULL
, "no-hold");
3522 if (!cupsGetOption("job-priority", p
->num_options
, p
->options
))
3523 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
3524 "job-priority-default", 50);
3526 if (!cupsGetOption("number-up", p
->num_options
, p
->options
))
3527 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
3528 "number-up-default", 1);
3530 if (!cupsGetOption("orientation-requested", p
->num_options
, p
->options
))
3531 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NOVALUE
,
3532 "orientation-requested-default", NULL
, NULL
);
3534 if (!cupsGetOption("notify-lease-duration", p
->num_options
, p
->options
))
3535 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
3536 "notify-lease-duration-default", DefaultLeaseDuration
);
3538 if (!cupsGetOption("notify-events", p
->num_options
, p
->options
))
3539 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
3540 "notify-events-default", NULL
, "job-completed");
3545 * 'add_printer_filter()' - Add a MIME filter for a printer.
3550 cupsd_printer_t
*p
, /* I - Printer to add to */
3551 mime_type_t
*filtertype
, /* I - Filter or prefilter MIME type */
3552 const char *filter
) /* I - Filter to add */
3554 char super
[MIME_MAX_SUPER
], /* Super-type for filter */
3555 type
[MIME_MAX_TYPE
], /* Type for filter */
3556 program
[1024]; /* Program/filter name */
3557 int cost
; /* Cost of filter */
3558 mime_type_t
*temptype
; /* MIME type looping var */
3559 char filename
[1024], /* Full filter filename */
3560 *dirsep
; /* Pointer to directory separator */
3561 struct stat fileinfo
; /* File information */
3565 * Parse the filter string; it should be in the following format:
3567 * super/type cost program
3570 if (sscanf(filter
, "%15[^/]/%31s%d%*[ \t]%1023[^\n]", super
, type
, &cost
,
3573 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s: invalid filter string \"%s\"!",
3579 * See if the filter program exists; if not, stop the printer and flag
3583 if (strcmp(program
, "-"))
3585 if (program
[0] == '/')
3586 strlcpy(filename
, program
, sizeof(filename
));
3588 snprintf(filename
, sizeof(filename
), "%s/filter/%s", ServerBin
, program
);
3590 if (stat(filename
, &fileinfo
))
3592 memset(&fileinfo
, 0, sizeof(fileinfo
));
3594 snprintf(p
->state_message
, sizeof(p
->state_message
),
3595 "Filter \"%s\" for printer \"%s\" not available: %s",
3596 filename
, p
->name
, strerror(errno
));
3597 cupsdSetPrinterReasons(p
, "+cups-missing-filter-warning");
3599 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s", p
->state_message
);
3603 * When running as root, do additional security checks...
3609 * Only use filters that are owned by root and do not have world write
3613 if (fileinfo
.st_uid
|| (fileinfo
.st_mode
& (S_ISUID
| S_IWOTH
)) != 0)
3615 if (fileinfo
.st_uid
)
3616 snprintf(p
->state_message
, sizeof(p
->state_message
),
3617 "Filter \"%s\" for printer \"%s\" not owned by root",
3620 snprintf(p
->state_message
, sizeof(p
->state_message
),
3621 "Filter \"%s\" for printer \"%s\" has insecure permissions "
3622 "(0%o)", filename
, p
->name
, fileinfo
.st_mode
);
3624 cupsdSetPrinterReasons(p
, "+cups-insecure-filter-warning");
3626 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s", p
->state_message
);
3628 else if (fileinfo
.st_mode
)
3631 * Similarly, check that the parent directory is also owned by root and
3632 * does not have world write permissions.
3635 if ((dirsep
= strrchr(filename
, '/')) != NULL
)
3638 if (!stat(filename
, &fileinfo
) &&
3640 (fileinfo
.st_mode
& (S_ISUID
| S_IWOTH
)) != 0))
3642 if (fileinfo
.st_uid
)
3643 snprintf(p
->state_message
, sizeof(p
->state_message
),
3644 "Filter directory \"%s\" for printer \"%s\" not owned by "
3645 "root", filename
, p
->name
);
3647 snprintf(p
->state_message
, sizeof(p
->state_message
),
3648 "Filter directory \"%s\" for printer \"%s\" has insecure "
3649 "permissions (0%o)", filename
, p
->name
, fileinfo
.st_mode
);
3651 cupsdSetPrinterReasons(p
, "+cups-insecure-filter-warning");
3653 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s", p
->state_message
);
3660 * Add the filter to the MIME database, supporting wildcards as needed...
3663 for (temptype
= mimeFirstType(MimeDatabase
);
3665 temptype
= mimeNextType(MimeDatabase
))
3666 if (((super
[0] == '*' && strcasecmp(temptype
->super
, "printer")) ||
3667 !strcasecmp(temptype
->super
, super
)) &&
3668 (type
[0] == '*' || !strcasecmp(temptype
->type
, type
)))
3670 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3671 "add_printer_filter: %s: adding filter %s/%s %s/%s %d %s",
3672 p
->name
, temptype
->super
, temptype
->type
,
3673 filtertype
->super
, filtertype
->type
,
3675 mimeAddFilter(MimeDatabase
, temptype
, filtertype
, cost
, program
);
3681 * 'add_printer_formats()' - Add document-format-supported values for a printer.
3685 add_printer_formats(cupsd_printer_t
*p
) /* I - Printer */
3687 int i
; /* Looping var */
3688 mime_type_t
*type
; /* Current MIME type */
3689 cups_array_t
*filters
; /* Filters */
3690 ipp_attribute_t
*attr
; /* document-format-supported attribute */
3691 char mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
3692 /* MIME type name */
3696 * Raw (and remote) queues advertise all of the supported MIME
3700 cupsArrayDelete(p
->filetypes
);
3701 p
->filetypes
= NULL
;
3705 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
,
3706 (ipp_tag_t
)(IPP_TAG_MIMETYPE
| IPP_TAG_COPY
),
3707 "document-format-supported", NumMimeTypes
, NULL
, MimeTypes
);
3712 * Otherwise, loop through the supported MIME types and see if there
3713 * are filters for them...
3716 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_printer_formats: %d types, %d filters",
3717 mimeNumTypes(MimeDatabase
), mimeNumFilters(MimeDatabase
));
3719 p
->filetypes
= cupsArrayNew(NULL
, NULL
);
3721 for (type
= mimeFirstType(MimeDatabase
);
3723 type
= mimeNextType(MimeDatabase
))
3725 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", type
->super
, type
->type
);
3727 if ((filters
= mimeFilter(MimeDatabase
, type
, p
->filetype
, NULL
)) != NULL
)
3729 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3730 "add_printer_formats: %s: %s needs %d filters",
3731 p
->name
, mimetype
, cupsArrayCount(filters
));
3733 cupsArrayDelete(filters
);
3734 cupsArrayAdd(p
->filetypes
, type
);
3737 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3738 "add_printer_formats: %s: %s not supported",
3742 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3743 "add_printer_formats: %s: %d supported types",
3744 p
->name
, cupsArrayCount(p
->filetypes
) + 1);
3747 * Add the file formats that can be filtered...
3750 if ((type
= mimeType(MimeDatabase
, "application", "octet-stream")) == NULL
||
3751 !cupsArrayFind(p
->filetypes
, type
))
3756 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_MIMETYPE
,
3757 "document-format-supported",
3758 cupsArrayCount(p
->filetypes
) + 1, NULL
, NULL
);
3761 attr
->values
[0].string
.text
= _cupsStrAlloc("application/octet-stream");
3763 for (type
= (mime_type_t
*)cupsArrayFirst(p
->filetypes
);
3765 i
++, type
= (mime_type_t
*)cupsArrayNext(p
->filetypes
))
3767 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", type
->super
, type
->type
);
3769 attr
->values
[i
].string
.text
= _cupsStrAlloc(mimetype
);
3774 char pdl
[1024]; /* Buffer to build pdl list */
3775 mime_filter_t
*filter
; /* MIME filter looping var */
3780 if (mimeType(MimeDatabase
, "application", "pdf"))
3781 strlcat(pdl
, "application/pdf,", sizeof(pdl
));
3783 if (mimeType(MimeDatabase
, "application", "postscript"))
3784 strlcat(pdl
, "application/postscript,", sizeof(pdl
));
3786 if (mimeType(MimeDatabase
, "application", "vnd.cups-raster"))
3787 strlcat(pdl
, "application/vnd.cups-raster,", sizeof(pdl
));
3790 * Determine if this is a Tioga PrintJobMgr based queue...
3793 for (filter
= (mime_filter_t
*)cupsArrayFirst(MimeDatabase
->filters
);
3795 filter
= (mime_filter_t
*)cupsArrayNext(MimeDatabase
->filters
))
3797 if (filter
->dst
== p
->filetype
&& filter
->filter
&&
3798 strstr(filter
->filter
, "PrintJobMgr"))
3803 * We only support raw printing if this is not a Tioga PrintJobMgr based
3804 * queue and if application/octet-stream is a known conversion...
3807 if (!filter
&& mimeType(MimeDatabase
, "application", "octet-stream"))
3808 strlcat(pdl
, "application/octet-stream,", sizeof(pdl
));
3810 if (mimeType(MimeDatabase
, "image", "png"))
3811 strlcat(pdl
, "image/png,", sizeof(pdl
));
3814 pdl
[strlen(pdl
) - 1] = '\0'; /* Remove trailing comma */
3816 cupsdSetString(&p
->pdl
, pdl
);
3818 #endif /* HAVE_DNSSD */
3823 * 'add_string_array()' - Add a string to an array of CUPS strings.
3827 add_string_array(cups_array_t
**a
, /* I - Array */
3828 const char *s
) /* I - String */
3831 *a
= cupsArrayNew(NULL
, NULL
);
3833 cupsArrayAdd(*a
, _cupsStrAlloc(s
));
3838 * 'compare_printers()' - Compare two printers.
3841 static int /* O - Result of comparison */
3842 compare_printers(void *first
, /* I - First printer */
3843 void *second
, /* I - Second printer */
3844 void *data
) /* I - App data (not used) */
3846 return (strcasecmp(((cupsd_printer_t
*)first
)->name
,
3847 ((cupsd_printer_t
*)second
)->name
));
3852 * 'delete_printer_filters()' - Delete all MIME filters for a printer.
3856 delete_printer_filters(
3857 cupsd_printer_t
*p
) /* I - Printer to remove from */
3859 mime_filter_t
*filter
; /* MIME filter looping var */
3863 * Range check input...
3870 * Remove all filters from the MIME database that have a destination
3871 * type == printer...
3874 for (filter
= mimeFirstFilter(MimeDatabase
);
3876 filter
= mimeNextFilter(MimeDatabase
))
3877 if (filter
->dst
== p
->filetype
)
3880 * Delete the current filter...
3883 mimeDeleteFilter(MimeDatabase
, filter
);
3886 cupsdSetPrinterReasons(p
, "-cups-insecure-filter-warning"
3887 ",cups-missing-filter-warning");
3892 * 'delete_string_array()' - Delete an array of CUPS strings.
3896 delete_string_array(cups_array_t
**a
) /* I - Array */
3898 char *ptr
; /* Current string */
3901 for (ptr
= (char *)cupsArrayFirst(*a
);
3903 ptr
= (char *)cupsArrayNext(*a
))
3906 cupsArrayDelete(*a
);
3912 * 'load_ppd()' - Load a cached PPD file, updating the cache as needed.
3916 load_ppd(cupsd_printer_t
*p
) /* I - Printer */
3918 int i
; /* Looping var */
3919 cups_file_t
*cache
; /* Cache file */
3920 char cache_name
[1024]; /* Cache filename */
3921 struct stat cache_info
; /* Cache file info */
3922 ppd_file_t
*ppd
; /* PPD file */
3923 char ppd_name
[1024]; /* PPD filename */
3924 struct stat ppd_info
; /* PPD file info */
3925 int num_media
; /* Number of media options */
3926 char custom_in
[256], /* Custom size name in inches */
3927 custom_mm
[256]; /* Custom size name in millimeters */
3928 ppd_size_t
*size
; /* Current size */
3929 ppd_option_t
*output_bin
, /* OutputBin options */
3930 *duplex
; /* Duplex options */
3931 ppd_attr_t
*ppd_attr
; /* PPD attribute */
3932 _cups_pwg_media_t
*pwgmedia
; /* Matching PWG size name */
3933 ipp_attribute_t
*attr
; /* Attribute data */
3934 ipp_t
*media_col_default
, /* media-col-default collection value */
3935 *media_size
; /* media-size collection value */
3936 ipp_value_t
*val
; /* Attribute value */
3937 int num_finishings
; /* Number of finishings */
3938 int finishings
[5]; /* finishings-supported values */
3939 static const char * const sides
[3] = /* sides-supported values */
3942 "two-sided-long-edge",
3943 "two-sided-short-edge"
3945 static const char * const standard_commands
[] =
3946 { /* Standard CUPS commands */
3954 * Check to see if the cache is up-to-date...
3957 snprintf(cache_name
, sizeof(cache_name
), "%s/%s.ipp", CacheDir
, p
->name
);
3958 if (stat(cache_name
, &cache_info
))
3959 cache_info
.st_mtime
= 0;
3961 snprintf(ppd_name
, sizeof(ppd_name
), "%s/ppd/%s.ppd", ServerRoot
, p
->name
);
3962 if (stat(ppd_name
, &ppd_info
))
3963 ppd_info
.st_mtime
= 1;
3965 ippDelete(p
->ppd_attrs
);
3966 p
->ppd_attrs
= ippNew();
3968 if (cache_info
.st_mtime
>= ppd_info
.st_mtime
&&
3969 (cache
= cupsFileOpen(cache_name
, "r")) != NULL
)
3972 * Load cached information and return...
3975 cupsdLogMessage(CUPSD_LOG_DEBUG
, "load_ppd: Loading %s...", cache_name
);
3977 if (ippReadIO(cache
, (ipp_iocb_t
)cupsFileRead
, 1, NULL
,
3978 p
->ppd_attrs
) == IPP_DATA
)
3980 cupsFileClose(cache
);
3984 cupsFileClose(cache
);
3988 * Reload PPD attributes from disk...
3991 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3993 cupsdLogMessage(CUPSD_LOG_DEBUG
, "load_ppd: Loading %s...", ppd_name
);
3995 delete_string_array(&(p
->filters
));
3996 delete_string_array(&(p
->pre_filters
));
3998 p
->type
&= ~CUPS_PRINTER_OPTIONS
;
3999 p
->type
|= CUPS_PRINTER_BW
;
4001 finishings
[0] = IPP_FINISHINGS_NONE
;
4004 if ((ppd
= ppdOpenFile(ppd_name
)) != NULL
)
4007 * Add make/model and other various attributes...
4010 ppdMarkDefaults(ppd
);
4012 if (ppd
->color_device
)
4013 p
->type
|= CUPS_PRINTER_COLOR
;
4014 if (ppd
->variable_sizes
)
4015 p
->type
|= CUPS_PRINTER_VARIABLE
;
4016 if (!ppd
->manual_copies
)
4017 p
->type
|= CUPS_PRINTER_COPIES
;
4018 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsFax", NULL
)) != NULL
)
4019 if (ppd_attr
->value
&& !strcasecmp(ppd_attr
->value
, "true"))
4020 p
->type
|= CUPS_PRINTER_FAX
;
4022 ippAddBoolean(p
->ppd_attrs
, IPP_TAG_PRINTER
, "color-supported",
4024 if (ppd
->throughput
)
4025 ippAddInteger(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4026 "pages-per-minute", ppd
->throughput
);
4031 * The NickName can be localized in the character set specified
4032 * by the LanugageEncoding attribute. However, ppdOpen2() has
4033 * already converted the ppd->nickname member to UTF-8 for us
4034 * (the original attribute value is available separately)
4037 cupsdSetString(&p
->make_model
, ppd
->nickname
);
4039 else if (ppd
->modelname
)
4042 * Model name can only contain specific characters...
4045 cupsdSetString(&p
->make_model
, ppd
->modelname
);
4048 cupsdSetString(&p
->make_model
, "Bad PPD File");
4050 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4051 "printer-make-and-model", NULL
, p
->make_model
);
4054 * Add media options from the PPD file...
4057 if (ppd
->num_sizes
== 0)
4059 if (!ppdFindAttr(ppd
, "APScannerOnly", NULL
))
4060 cupsdLogMessage(CUPSD_LOG_CRIT
,
4061 "The PPD file for printer %s contains no media "
4062 "options and is therefore invalid!", p
->name
);
4064 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4065 "media-supported", NULL
, "unknown");
4069 num_media
= ppd
->num_sizes
;
4070 if (ppd
->variable_sizes
)
4073 attr
= ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4074 "media-supported", num_media
, NULL
, NULL
);
4079 for (i
= ppd
->num_sizes
, size
= ppd
->sizes
; i
> 0; i
--, size
++)
4081 if (strcasecmp(size
->name
, "Custom"))
4083 if ((pwgmedia
= _cupsPWGMediaBySize(size
->width
,
4084 size
->length
)) != NULL
)
4086 val
->string
.text
= _cupsStrAlloc(pwgmedia
->pwg
);
4090 snprintf(custom_in
, sizeof(custom_in
), "adobe_%s_%gx%gin",
4091 size
->name
, size
->width
/ 72.0, size
->length
/ 72.0);
4092 snprintf(custom_mm
, sizeof(custom_mm
), "adobe_%s_%gx%gmm",
4093 size
->name
, size
->width
* 25.4 / 72.0,
4094 size
->length
* 25.4 / 72.0);
4095 if (strlen(custom_in
) < strlen(custom_mm
))
4096 val
->string
.text
= _cupsStrAlloc(custom_in
);
4098 val
->string
.text
= _cupsStrAlloc(custom_mm
);
4104 * Add media-default...
4107 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4108 "media-default", NULL
, val
->string
.text
);
4111 * Add media-col-default...
4114 media_size
= ippNew();
4115 ippAddInteger(media_size
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4116 "x-dimension", (int)(size
->width
* 2540.0 / 72.0));
4117 ippAddInteger(media_size
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4118 "y-dimension", (int)(size
->length
* 2540.0 / 72.0));
4120 media_col_default
= ippNew();
4121 ippAddString(media_col_default
, IPP_TAG_PRINTER
,
4122 IPP_TAG_KEYWORD
| IPP_TAG_COPY
, "media-color", NULL
,
4124 ippAddString(media_col_default
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4125 "media-key", NULL
,val
->string
.text
);
4126 ippAddCollection(media_col_default
, IPP_TAG_PRINTER
, "media-size",
4128 ippAddString(media_col_default
, IPP_TAG_PRINTER
,
4129 IPP_TAG_KEYWORD
| IPP_TAG_COPY
, "media-type", NULL
,
4132 ippAddCollection(p
->ppd_attrs
, IPP_TAG_PRINTER
,
4133 "media-col-default", media_col_default
);
4140 if (ppd
->variable_sizes
)
4142 snprintf(custom_in
, sizeof(custom_in
), "custom_min_%gx%gin",
4143 ppd
->custom_min
[0] / 72.0, ppd
->custom_min
[1] / 72.0);
4144 snprintf(custom_mm
, sizeof(custom_mm
), "custom_min_%gx%gmm",
4145 ppd
->custom_min
[0] * 25.4 / 72.0,
4146 ppd
->custom_min
[1] * 25.4 / 72.0);
4147 if (strlen(custom_in
) < strlen(custom_mm
))
4148 val
->string
.text
= _cupsStrAlloc(custom_in
);
4150 val
->string
.text
= _cupsStrAlloc(custom_mm
);
4153 snprintf(custom_in
, sizeof(custom_in
), "custom_max_%gx%gin",
4154 ppd
->custom_max
[0] / 72.0, ppd
->custom_max
[1] / 72.0);
4155 snprintf(custom_mm
, sizeof(custom_mm
), "custom_max_%gx%gmm",
4156 ppd
->custom_max
[0] * 25.4 / 72.0,
4157 ppd
->custom_max
[1] * 25.4 / 72.0);
4158 if (strlen(custom_in
) < strlen(custom_mm
))
4159 val
->string
.text
= _cupsStrAlloc(custom_in
);
4161 val
->string
.text
= _cupsStrAlloc(custom_mm
);
4170 if ((output_bin
= ppdFindOption(ppd
, "OutputBin")) != NULL
)
4172 attr
= ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4173 "output-bin-supported", output_bin
->num_choices
,
4178 for (i
= 0, val
= attr
->values
;
4179 i
< output_bin
->num_choices
;
4181 val
->string
.text
= _cupsStrAlloc(output_bin
->choices
[i
].choice
);
4184 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4185 "output-bin-default", NULL
, output_bin
->defchoice
);
4192 if ((duplex
= ppdFindOption(ppd
, "Duplex")) == NULL
)
4193 if ((duplex
= ppdFindOption(ppd
, "EFDuplex")) == NULL
)
4194 if ((duplex
= ppdFindOption(ppd
, "EFDuplexing")) == NULL
)
4195 if ((duplex
= ppdFindOption(ppd
, "KD03Duplex")) == NULL
)
4196 duplex
= ppdFindOption(ppd
, "JCLDuplex");
4198 if (duplex
&& duplex
->num_choices
> 1 &&
4199 !ppdInstallableConflict(ppd
, duplex
->keyword
, "DuplexTumble"))
4201 p
->type
|= CUPS_PRINTER_DUPLEX
;
4203 ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4204 "sides-supported", 3, NULL
, sides
);
4206 if (!strcasecmp(duplex
->defchoice
, "DuplexTumble"))
4207 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4208 "sides-default", NULL
, "two-sided-short-edge");
4209 else if (!strcasecmp(duplex
->defchoice
, "DuplexNoTumble"))
4210 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4211 "sides-default", NULL
, "two-sided-long-edge");
4213 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4214 "sides-default", NULL
, "one-sided");
4217 if (ppdFindOption(ppd
, "Collate") != NULL
)
4218 p
->type
|= CUPS_PRINTER_COLLATE
;
4220 if (ppdFindOption(ppd
, "StapleLocation") != NULL
)
4222 p
->type
|= CUPS_PRINTER_STAPLE
;
4223 finishings
[num_finishings
++] = IPP_FINISHINGS_STAPLE
;
4226 if (ppdFindOption(ppd
, "BindEdge") != NULL
)
4228 p
->type
|= CUPS_PRINTER_BIND
;
4229 finishings
[num_finishings
++] = IPP_FINISHINGS_BIND
;
4232 for (i
= 0; i
< ppd
->num_sizes
; i
++)
4233 if (ppd
->sizes
[i
].length
> 1728)
4234 p
->type
|= CUPS_PRINTER_LARGE
;
4235 else if (ppd
->sizes
[i
].length
> 1008)
4236 p
->type
|= CUPS_PRINTER_MEDIUM
;
4238 p
->type
|= CUPS_PRINTER_SMALL
;
4240 if ((ppd_attr
= ppdFindAttr(ppd
, "APICADriver", NULL
)) != NULL
&&
4241 ppd_attr
->value
&& !strcasecmp(ppd_attr
->value
, "true"))
4243 if ((ppd_attr
= ppdFindAttr(ppd
, "APScannerOnly", NULL
)) != NULL
&&
4244 ppd_attr
->value
&& !strcasecmp(ppd_attr
->value
, "true"))
4245 p
->type
|= CUPS_PRINTER_SCANNER
;
4247 p
->type
|= CUPS_PRINTER_MFP
;
4251 * Add a filter from application/vnd.cups-raw to printer/name to
4252 * handle "raw" printing by users.
4255 add_string_array(&(p
->filters
), "application/vnd.cups-raw 0 -");
4258 * Add any pre-filters in the PPD file...
4261 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsPreFilter", NULL
)) != NULL
)
4263 for (; ppd_attr
; ppd_attr
= ppdFindNextAttr(ppd
, "cupsPreFilter", NULL
))
4264 if (ppd_attr
->value
)
4265 add_string_array(&(p
->pre_filters
), ppd_attr
->value
);
4269 * Add any filters in the PPD file...
4272 DEBUG_printf(("ppd->num_filters = %d\n", ppd
->num_filters
));
4273 for (i
= 0; i
< ppd
->num_filters
; i
++)
4275 DEBUG_printf(("ppd->filters[%d] = \"%s\"\n", i
, ppd
->filters
[i
]));
4276 add_string_array(&(p
->filters
), ppd
->filters
[i
]);
4278 if (!strncasecmp(ppd
->filters
[i
], "application/vnd.cups-command", 28) &&
4279 isspace(ppd
->filters
[i
][28] & 255))
4280 p
->type
|= CUPS_PRINTER_COMMANDS
;
4283 if (ppd
->num_filters
== 0)
4286 * If there are no filters, add PostScript printing filters.
4289 add_string_array(&(p
->filters
),
4290 "application/vnd.cups-command 0 commandtops");
4291 add_string_array(&(p
->filters
),
4292 "application/vnd.cups-postscript 0 -");
4294 p
->type
|= CUPS_PRINTER_COMMANDS
;
4296 else if (!(p
->type
& CUPS_PRINTER_COMMANDS
))
4299 * See if this is a PostScript device without a command filter...
4302 for (i
= 0; i
< ppd
->num_filters
; i
++)
4303 if (!strncasecmp(ppd
->filters
[i
],
4304 "application/vnd.cups-postscript", 31) &&
4305 isspace(ppd
->filters
[i
][31] & 255))
4308 if (i
< ppd
->num_filters
)
4311 * Add the generic PostScript command filter...
4314 add_string_array(&(p
->filters
),
4315 "application/vnd.cups-command 0 commandtops");
4316 p
->type
|= CUPS_PRINTER_COMMANDS
;
4320 if (p
->type
& CUPS_PRINTER_COMMANDS
)
4322 char *commands
, /* Copy of commands */
4323 *start
, /* Start of name */
4324 *end
; /* End of name */
4325 int count
; /* Number of commands */
4328 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsCommands", NULL
)) != NULL
&&
4329 ppd_attr
->value
&& ppd_attr
->value
[0])
4331 for (count
= 0, start
= ppd_attr
->value
; *start
; count
++)
4333 while (isspace(*start
& 255))
4339 while (*start
&& !isspace(*start
& 255))
4349 * Make a copy of the commands string and count how many ...
4352 attr
= ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4353 "printer-commands", count
, NULL
, NULL
);
4355 commands
= strdup(ppd_attr
->value
);
4357 for (count
= 0, start
= commands
; *start
; count
++)
4359 while (isspace(*start
& 255))
4366 while (*end
&& !isspace(*end
& 255))
4372 attr
->values
[count
].string
.text
= _cupsStrAlloc(start
);
4382 * Add the standard list of commands...
4385 ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4387 (int)(sizeof(standard_commands
) /
4388 sizeof(standard_commands
[0])), NULL
,
4395 * No commands supported...
4398 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4399 "printer-commands", NULL
, "none");
4403 * Show current and available port monitors for this printer...
4406 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "port-monitor",
4407 NULL
, p
->port_monitor
? p
->port_monitor
: "none");
4409 for (i
= 1, ppd_attr
= ppdFindAttr(ppd
, "cupsPortMonitor", NULL
);
4411 i
++, ppd_attr
= ppdFindNextAttr(ppd
, "cupsPortMonitor", NULL
));
4415 if (strstr(ppd
->protocols
, "TBCP"))
4417 else if (strstr(ppd
->protocols
, "BCP"))
4421 attr
= ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
4422 "port-monitor-supported", i
, NULL
, NULL
);
4424 attr
->values
[0].string
.text
= _cupsStrAlloc("none");
4426 for (i
= 1, ppd_attr
= ppdFindAttr(ppd
, "cupsPortMonitor", NULL
);
4428 i
++, ppd_attr
= ppdFindNextAttr(ppd
, "cupsPortMonitor", NULL
))
4429 attr
->values
[i
].string
.text
= _cupsStrAlloc(ppd_attr
->value
);
4433 if (strstr(ppd
->protocols
, "TBCP"))
4434 attr
->values
[i
].string
.text
= _cupsStrAlloc("tbcp");
4435 else if (strstr(ppd
->protocols
, "BCP"))
4436 attr
->values
[i
].string
.text
= _cupsStrAlloc("bcp");
4440 cupsdSetString(&p
->product
, ppd
->product
);
4441 #endif /* HAVE_DNSSD */
4443 if (ppdFindAttr(ppd
, "APRemoteQueueID", NULL
))
4444 p
->type
|= CUPS_PRINTER_REMOTE
;
4447 * Close the PPD and set the type...
4452 else if (!access(ppd_name
, 0))
4454 int pline
; /* PPD line number */
4455 ppd_status_t pstatus
; /* PPD load status */
4458 pstatus
= ppdLastError(&pline
);
4460 cupsdLogMessage(CUPSD_LOG_ERROR
, "PPD file for %s cannot be loaded!",
4463 if (pstatus
<= PPD_ALLOC_ERROR
)
4464 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s", strerror(errno
));
4466 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s on line %d.",
4467 ppdErrorString(pstatus
), pline
);
4469 cupsdLogMessage(CUPSD_LOG_INFO
,
4470 "Hint: Run \"cupstestppd %s\" and fix any errors.",
4474 * Add a filter from application/vnd.cups-raw to printer/name to
4475 * handle "raw" printing by users.
4478 add_string_array(&(p
->filters
), "application/vnd.cups-raw 0 -");
4481 * Add a PostScript filter, since this is still possibly PS printer.
4484 add_string_array(&(p
->filters
), "application/vnd.cups-postscript 0 -");
4489 * If we have an interface script, add a filter entry for it...
4492 char interface
[1024]; /* Interface script */
4495 snprintf(interface
, sizeof(interface
), "%s/interfaces/%s", ServerRoot
,
4497 if (!access(interface
, X_OK
))
4500 * Yes, we have a System V style interface script; use it!
4503 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4504 "printer-make-and-model", NULL
,
4505 "Local System V Printer");
4507 snprintf(interface
, sizeof(interface
), "*/* 0 %s/interfaces/%s",
4508 ServerRoot
, p
->name
);
4509 add_string_array(&(p
->filters
), interface
);
4511 else if (!strncmp(p
->device_uri
, "ipp://", 6) &&
4512 (strstr(p
->device_uri
, "/printers/") != NULL
||
4513 strstr(p
->device_uri
, "/classes/") != NULL
||
4514 (strstr(p
->device_uri
, "._ipp.") != NULL
&&
4515 !strcmp(p
->device_uri
+ strlen(p
->device_uri
) - 5,
4519 * Tell the client this is really a hard-wired remote printer.
4522 p
->type
|= CUPS_PRINTER_REMOTE
;
4525 * Point the printer-uri-supported attribute to the
4529 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
4530 "printer-uri-supported", NULL
, p
->device_uri
);
4533 * Then set the make-and-model accordingly...
4536 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4537 "printer-make-and-model", NULL
, "Remote Printer");
4540 * Print all files directly...
4549 * Otherwise we have neither - treat this as a "dumb" printer
4550 * with no PPD file...
4553 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4554 "printer-make-and-model", NULL
, "Local Raw Printer");
4560 ippAddIntegers(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
4561 "finishings-supported", num_finishings
, finishings
);
4562 ippAddInteger(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
4563 "finishings-default", IPP_FINISHINGS_NONE
);
4565 if (ppd
&& (cache
= cupsFileOpen(cache_name
, "w")) != NULL
)
4568 * Save cached PPD attributes to disk...
4571 cupsdLogMessage(CUPSD_LOG_DEBUG
, "load_ppd: Saving %s...", cache_name
);
4573 p
->ppd_attrs
->state
= IPP_IDLE
;
4575 if (ippWriteIO(cache
, (ipp_iocb_t
)cupsFileWrite
, 1, NULL
,
4576 p
->ppd_attrs
) != IPP_DATA
)
4578 cupsdLogMessage(CUPSD_LOG_ERROR
,
4579 "Unable to save PPD cache file \"%s\" - %s", cache_name
,
4584 cupsFileClose(cache
);
4586 else if (cache_info
.st_mtime
)
4589 * Remove cache file...
4599 * 'write_irix_config()' - Update the config files used by the IRIX
4604 write_irix_config(cupsd_printer_t
*p
) /* I - Printer to update */
4606 char filename
[1024]; /* Interface script filename */
4607 cups_file_t
*fp
; /* Interface script file */
4608 ipp_attribute_t
*attr
; /* Attribute data */
4612 * Add dummy interface and GUI scripts to fool SGI's "challenged" printing
4613 * tools. First the interface script that tells the tools what kind of
4614 * printer we have...
4617 snprintf(filename
, sizeof(filename
), "/var/spool/lp/interface/%s", p
->name
);
4619 if (p
->type
& CUPS_PRINTER_CLASS
)
4621 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4623 cupsFilePuts(fp
, "#!/bin/sh\n");
4625 if ((attr
= ippFindAttribute(p
->attrs
, "printer-make-and-model",
4626 IPP_TAG_TEXT
)) != NULL
)
4627 cupsFilePrintf(fp
, "NAME=\"%s\"\n", attr
->values
[0].string
.text
);
4628 else if (p
->type
& CUPS_PRINTER_CLASS
)
4629 cupsFilePuts(fp
, "NAME=\"Printer Class\"\n");
4631 cupsFilePuts(fp
, "NAME=\"Remote Destination\"\n");
4633 if (p
->type
& CUPS_PRINTER_COLOR
)
4634 cupsFilePuts(fp
, "TYPE=ColorPostScript\n");
4636 cupsFilePuts(fp
, "TYPE=MonoPostScript\n");
4638 cupsFilePrintf(fp
, "HOSTNAME=%s\n", ServerName
);
4639 cupsFilePrintf(fp
, "HOSTPRINTER=%s\n", p
->name
);
4643 chmod(filename
, 0755);
4644 chown(filename
, User
, Group
);
4648 * Then the member file that tells which device file the queue is connected
4649 * to... Networked printers use "/dev/null" in this file, so that's what
4650 * we use (the actual device URI can confuse some apps...)
4653 snprintf(filename
, sizeof(filename
), "/var/spool/lp/member/%s", p
->name
);
4655 if (p
->type
& CUPS_PRINTER_CLASS
)
4657 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4659 cupsFilePuts(fp
, "/dev/null\n");
4663 chmod(filename
, 0644);
4664 chown(filename
, User
, Group
);
4668 * The gui_interface file is a script or program that launches a GUI
4669 * option panel for the printer, using options specified on the
4670 * command-line in the third argument. The option panel must send
4671 * any printing options to stdout on a single line when the user
4672 * accepts them, or nothing if the user cancels the dialog.
4674 * The default options panel program is /usr/bin/glpoptions, from
4675 * the ESP Print Pro software. You can select another using the
4676 * PrintcapGUI option.
4679 snprintf(filename
, sizeof(filename
), "/var/spool/lp/gui_interface/ELF/%s.gui", p
->name
);
4681 if (p
->type
& CUPS_PRINTER_CLASS
)
4683 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4685 cupsFilePuts(fp
, "#!/bin/sh\n");
4686 cupsFilePrintf(fp
, "%s -d %s -o \"$3\"\n", PrintcapGUI
, p
->name
);
4690 chmod(filename
, 0755);
4691 chown(filename
, User
, Group
);
4695 * The POD config file is needed by the printstatus command to show
4696 * the printer location and device.
4699 snprintf(filename
, sizeof(filename
), "/var/spool/lp/pod/%s.config", p
->name
);
4701 if (p
->type
& CUPS_PRINTER_CLASS
)
4703 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4705 cupsFilePrintf(fp
, "Printer Class | %s\n",
4706 (p
->type
& CUPS_PRINTER_COLOR
) ? "ColorPostScript" : "MonoPostScript");
4707 cupsFilePrintf(fp
, "Printer Model | %s\n", p
->make_model
? p
->make_model
: "");
4708 cupsFilePrintf(fp
, "Location Code | %s\n", p
->location
? p
->location
: "");
4709 cupsFilePrintf(fp
, "Physical Location | %s\n", p
->info
? p
->info
: "");
4710 cupsFilePrintf(fp
, "Port Path | %s\n", p
->device_uri
);
4711 cupsFilePrintf(fp
, "Config Path | /var/spool/lp/pod/%s.config\n", p
->name
);
4712 cupsFilePrintf(fp
, "Active Status Path | /var/spool/lp/pod/%s.status\n", p
->name
);
4713 cupsFilePuts(fp
, "Status Update Wait | 10 seconds\n");
4717 chmod(filename
, 0664);
4718 chown(filename
, User
, Group
);
4724 * 'write_irix_state()' - Update the status files used by IRIX printing
4729 write_irix_state(cupsd_printer_t
*p
) /* I - Printer to update */
4731 char filename
[1024]; /* Interface script filename */
4732 cups_file_t
*fp
; /* Interface script file */
4733 int tag
; /* Status tag value */
4739 * The POD status file is needed for the printstatus window to
4740 * provide the current status of the printer.
4743 snprintf(filename
, sizeof(filename
), "/var/spool/lp/pod/%s.status", p
->name
);
4745 if (p
->type
& CUPS_PRINTER_CLASS
)
4747 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4749 cupsFilePrintf(fp
, "Operational Status | %s\n",
4750 (p
->state
== IPP_PRINTER_IDLE
) ? "Idle" :
4751 (p
->state
== IPP_PRINTER_PROCESSING
) ? "Busy" :
4753 cupsFilePrintf(fp
, "Information | 01 00 00 | %s\n", CUPS_SVERSION
);
4754 cupsFilePrintf(fp
, "Information | 02 00 00 | Device URI: %s\n",
4756 cupsFilePrintf(fp
, "Information | 03 00 00 | %s jobs\n",
4757 p
->accepting
? "Accepting" : "Not accepting");
4758 cupsFilePrintf(fp
, "Information | 04 00 00 | %s\n", p
->state_message
);
4762 chmod(filename
, 0664);
4763 chown(filename
, User
, Group
);
4767 * The activeicons file is needed to provide desktop icons for printers:
4769 * [ quoted from /usr/lib/print/tagit ]
4771 * --- Type of printer tags (base values)
4773 * Dumb=66048 # 0x10200
4774 * DumbColor=66080 # 0x10220
4775 * Raster=66112 # 0x10240
4776 * ColorRaster=66144 # 0x10260
4777 * Plotter=66176 # 0x10280
4778 * PostScript=66208 # 0x102A0
4779 * ColorPostScript=66240 # 0x102C0
4780 * MonoPostScript=66272 # 0x102E0
4782 * --- Printer state modifiers for local printers
4787 * Unknown=3 # 0x3 (Faulted due to unknown reason)
4789 * --- Printer state modifiers for network printers
4793 * NetFaulted=10 # 0xA
4794 * NetUnknown=11 # 0xB (Faulted due to unknown reason)
4797 snprintf(filename
, sizeof(filename
), "/var/spool/lp/activeicons/%s", p
->name
);
4799 if (p
->type
& CUPS_PRINTER_CLASS
)
4801 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4803 if (p
->type
& CUPS_PRINTER_COLOR
)
4808 if (p
->type
& CUPS_PRINTER_REMOTE
)
4811 if (p
->state
== IPP_PRINTER_PROCESSING
)
4814 else if (p
->state
== IPP_PRINTER_STOPPED
)
4817 cupsFilePuts(fp
, "#!/bin/sh\n");
4818 cupsFilePrintf(fp
, "#Tag %d\n", tag
);
4822 chmod(filename
, 0755);
4823 chown(filename
, User
, Group
);
4828 * The default file is needed by the printers window to show
4829 * the default printer.
4832 snprintf(filename
, sizeof(filename
), "/var/spool/lp/default");
4834 if (DefaultPrinter
!= NULL
)
4836 if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4838 cupsFilePrintf(fp
, "%s\n", DefaultPrinter
->name
);
4842 chmod(filename
, 0644);
4843 chown(filename
, User
, Group
);
4853 * 'write_xml_string()' - Write a string with XML escaping.
4857 write_xml_string(cups_file_t
*fp
, /* I - File to write to */
4858 const char *s
) /* I - String to write */
4860 const char *start
; /* Start of current sequence */
4866 for (start
= s
; *s
; s
++)
4871 cupsFileWrite(fp
, start
, s
- start
);
4873 cupsFilePuts(fp
, "&");
4879 cupsFileWrite(fp
, start
, s
- start
);
4881 cupsFilePuts(fp
, "<");
4887 cupsFilePuts(fp
, start
);
4892 * End of "$Id: printers.c 7968 2008-09-19 23:03:01Z mike $".