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 cupsdStopPrinter(p
, update
);
698 * If this printer is the next for browsing, point to the next one...
703 cupsArrayFind(Printers
, p
);
704 BrowseNext
= (cupsd_printer_t
*)cupsArrayNext(Printers
);
708 * Remove the printer from the list...
711 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
712 "cupsdDeletePrinter: Removing %s from Printers", p
->name
);
713 cupsArrayRemove(Printers
, p
);
715 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
717 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
718 "cupsdDeletePrinter: Removing %s from ImplicitPrinters",
720 cupsArrayRemove(ImplicitPrinters
, p
);
724 * Remove the dummy interface/icon/option files under IRIX...
728 snprintf(filename
, sizeof(filename
), "/var/spool/lp/interface/%s", p
->name
);
731 snprintf(filename
, sizeof(filename
), "/var/spool/lp/gui_interface/ELF/%s.gui",
735 snprintf(filename
, sizeof(filename
), "/var/spool/lp/activeicons/%s", p
->name
);
738 snprintf(filename
, sizeof(filename
), "/var/spool/lp/pod/%s.config", p
->name
);
741 snprintf(filename
, sizeof(filename
), "/var/spool/lp/pod/%s.status", p
->name
);
744 snprintf(filename
, sizeof(filename
), "/var/spool/lp/member/%s", p
->name
);
749 * If p is the default printer, assign a different one...
752 if (p
== DefaultPrinter
)
754 DefaultPrinter
= NULL
;
756 if (UseNetworkDefault
)
759 * Find the first network default printer and use it...
762 cupsd_printer_t
*dp
; /* New default printer */
765 for (dp
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
767 dp
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
768 if (dp
!= p
&& (dp
->type
& CUPS_PRINTER_DEFAULT
))
777 * Remove this printer from any classes...
780 if (!(p
->type
& CUPS_PRINTER_IMPLICIT
))
782 cupsdDeletePrinterFromClasses(p
);
785 * Deregister from any browse protocols...
788 cupsdDeregisterPrinter(p
, 1);
792 * Free all memory used by the printer...
795 if (p
->printers
!= NULL
)
798 if (MaxPrinterHistory
)
800 for (i
= 0; i
< p
->num_history
; i
++)
801 ippDelete(p
->history
[i
]);
806 for (i
= 0; i
< p
->num_reasons
; i
++)
807 _cupsStrFree(p
->reasons
[i
]);
810 ippDelete(p
->ppd_attrs
);
812 delete_printer_filters(p
);
814 mimeDeleteType(MimeDatabase
, p
->filetype
);
815 mimeDeleteType(MimeDatabase
, p
->prefiltertype
);
817 delete_string_array(&(p
->filters
));
818 delete_string_array(&(p
->pre_filters
));
820 cupsdFreePrinterUsers(p
);
823 cupsdClearString(&p
->uri
);
824 cupsdClearString(&p
->hostname
);
825 cupsdClearString(&p
->name
);
826 cupsdClearString(&p
->location
);
827 cupsdClearString(&p
->make_model
);
828 cupsdClearString(&p
->info
);
829 cupsdClearString(&p
->job_sheets
[0]);
830 cupsdClearString(&p
->job_sheets
[1]);
831 cupsdClearString(&p
->device_uri
);
832 cupsdClearString(&p
->sanitized_device_uri
);
833 cupsdClearString(&p
->port_monitor
);
834 cupsdClearString(&p
->op_policy
);
835 cupsdClearString(&p
->error_policy
);
837 cupsdClearString(&p
->alert
);
838 cupsdClearString(&p
->alert_description
);
841 cupsdClearString(&p
->product
);
842 cupsdClearString(&p
->pdl
);
843 #endif /* HAVE_DNSSD */
845 cupsArrayDelete(p
->filetypes
);
848 free(p
->browse_attrs
);
851 cupsdClearString(&p
->recoverable
);
852 #endif /* __APPLE__ */
854 cupsFreeOptions(p
->num_options
, p
->options
);
859 * Restore the previous position in the Printers array...
862 cupsArrayRestore(Printers
);
867 * 'cupsdFindDest()' - Find a destination in the list.
870 cupsd_printer_t
* /* O - Destination in list */
871 cupsdFindDest(const char *name
) /* I - Name of printer or class to find */
873 cupsd_printer_t key
; /* Search key */
876 key
.name
= (char *)name
;
877 return ((cupsd_printer_t
*)cupsArrayFind(Printers
, &key
));
882 * 'cupsdFindPrinter()' - Find a printer in the list.
885 cupsd_printer_t
* /* O - Printer in list */
886 cupsdFindPrinter(const char *name
) /* I - Name of printer to find */
888 cupsd_printer_t
*p
; /* Printer in list */
891 if ((p
= cupsdFindDest(name
)) != NULL
&& (p
->type
& CUPS_PRINTER_CLASS
))
899 * 'cupsdFreePrinterUsers()' - Free allow/deny users.
903 cupsdFreePrinterUsers(
904 cupsd_printer_t
*p
) /* I - Printer */
906 int i
; /* Looping var */
909 if (!p
|| !p
->num_users
)
912 for (i
= 0; i
< p
->num_users
; i
++)
913 free((void *)p
->users
[i
]);
923 * 'cupsdLoadAllPrinters()' - Load printers from the printers.conf file.
927 cupsdLoadAllPrinters(void)
929 int i
; /* Looping var */
930 cups_file_t
*fp
; /* printers.conf file */
931 int linenum
; /* Current line number */
932 char line
[4096], /* Line from file */
933 *value
, /* Pointer to value */
934 *valueptr
; /* Pointer into value */
935 cupsd_printer_t
*p
; /* Current printer */
939 * Open the printers.conf file...
942 snprintf(line
, sizeof(line
), "%s/printers.conf", ServerRoot
);
943 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
946 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open %s - %s", line
,
952 * Read printer configurations until we hit EOF...
958 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
961 * Decode the directive...
964 if (!strcasecmp(line
, "<Printer") ||
965 !strcasecmp(line
, "<DefaultPrinter"))
968 * <Printer name> or <DefaultPrinter name>
971 if (p
== NULL
&& value
)
974 * Add the printer and a base file type...
977 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Loading printer %s...", value
);
979 p
= cupsdAddPrinter(value
);
981 p
->state
= IPP_PRINTER_IDLE
;
984 * Set the default printer as needed...
987 if (!strcasecmp(line
, "<DefaultPrinter"))
991 cupsdLogMessage(CUPSD_LOG_ERROR
,
992 "Syntax error on line %d of printers.conf.", linenum
);
994 else if (!strcasecmp(line
, "</Printer>"))
999 * Close out the current printer...
1002 cupsdSetPrinterAttrs(p
);
1003 cupsdAddPrinterHistory(p
);
1005 if (strncmp(p
->device_uri
, "file:", 5) &&
1006 p
->state
!= IPP_PRINTER_STOPPED
)
1009 * See if the backend exists...
1012 snprintf(line
, sizeof(line
), "%s/backend/%s", ServerBin
,
1015 if ((valueptr
= strchr(line
+ strlen(ServerBin
), ':')) != NULL
)
1016 *valueptr
= '\0'; /* Chop everything but URI scheme */
1018 if (access(line
, 0))
1021 * Backend does not exist, stop printer...
1024 p
->state
= IPP_PRINTER_STOPPED
;
1025 snprintf(p
->state_message
, sizeof(p
->state_message
),
1026 "Backend %s does not exist!", line
);
1033 cupsdLogMessage(CUPSD_LOG_ERROR
,
1034 "Syntax error on line %d of printers.conf.", linenum
);
1038 cupsdLogMessage(CUPSD_LOG_ERROR
,
1039 "Syntax error on line %d of printers.conf.", linenum
);
1041 else if (!strcasecmp(line
, "AuthInfoRequired"))
1043 if (!cupsdSetAuthInfoRequired(p
, value
, NULL
))
1044 cupsdLogMessage(CUPSD_LOG_ERROR
,
1045 "Bad AuthInfoRequired on line %d of printers.conf.",
1048 else if (!strcasecmp(line
, "Info"))
1051 cupsdSetString(&p
->info
, value
);
1053 else if (!strcasecmp(line
, "MakeModel"))
1056 cupsdSetString(&p
->make_model
, value
);
1058 else if (!strcasecmp(line
, "Location"))
1061 cupsdSetString(&p
->location
, value
);
1063 else if (!strcasecmp(line
, "DeviceURI"))
1066 cupsdSetDeviceURI(p
, value
);
1068 cupsdLogMessage(CUPSD_LOG_ERROR
,
1069 "Syntax error on line %d of printers.conf.", linenum
);
1071 else if (!strcasecmp(line
, "Option") && value
)
1077 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
1080 cupsdLogMessage(CUPSD_LOG_ERROR
,
1081 "Syntax error on line %d of printers.conf.", linenum
);
1084 for (; *valueptr
&& isspace(*valueptr
& 255); *valueptr
++ = '\0');
1086 p
->num_options
= cupsAddOption(value
, valueptr
, p
->num_options
,
1090 else if (!strcasecmp(line
, "PortMonitor"))
1092 if (value
&& strcmp(value
, "none"))
1093 cupsdSetString(&p
->port_monitor
, value
);
1095 cupsdClearString(&p
->port_monitor
);
1097 cupsdLogMessage(CUPSD_LOG_ERROR
,
1098 "Syntax error on line %d of printers.conf.", linenum
);
1100 else if (!strcasecmp(line
, "Reason"))
1104 for (i
= 0 ; i
< p
->num_reasons
; i
++)
1105 if (!strcmp(value
, p
->reasons
[i
]))
1108 if (i
>= p
->num_reasons
&&
1109 p
->num_reasons
< (int)(sizeof(p
->reasons
) / sizeof(p
->reasons
[0])))
1111 p
->reasons
[p
->num_reasons
] = _cupsStrAlloc(value
);
1116 cupsdLogMessage(CUPSD_LOG_ERROR
,
1117 "Syntax error on line %d of printers.conf.", linenum
);
1119 else if (!strcasecmp(line
, "State"))
1122 * Set the initial queue state...
1125 if (value
&& !strcasecmp(value
, "idle"))
1126 p
->state
= IPP_PRINTER_IDLE
;
1127 else if (value
&& !strcasecmp(value
, "stopped"))
1129 p
->state
= IPP_PRINTER_STOPPED
;
1130 cupsdSetPrinterReasons(p
, "+paused");
1133 cupsdLogMessage(CUPSD_LOG_ERROR
,
1134 "Syntax error on line %d of printers.conf.", linenum
);
1136 else if (!strcasecmp(line
, "StateMessage"))
1139 * Set the initial queue state message...
1143 strlcpy(p
->state_message
, value
, sizeof(p
->state_message
));
1145 else if (!strcasecmp(line
, "StateTime"))
1148 * Set the state time...
1152 p
->state_time
= atoi(value
);
1154 else if (!strcasecmp(line
, "Accepting"))
1157 * Set the initial accepting state...
1161 (!strcasecmp(value
, "yes") ||
1162 !strcasecmp(value
, "on") ||
1163 !strcasecmp(value
, "true")))
1166 (!strcasecmp(value
, "no") ||
1167 !strcasecmp(value
, "off") ||
1168 !strcasecmp(value
, "false")))
1171 cupsdLogMessage(CUPSD_LOG_ERROR
,
1172 "Syntax error on line %d of printers.conf.", linenum
);
1174 else if (!strcasecmp(line
, "Type"))
1177 p
->type
= atoi(value
);
1179 cupsdLogMessage(CUPSD_LOG_ERROR
,
1180 "Syntax error on line %d of printers.conf.", linenum
);
1182 else if (!strcasecmp(line
, "Product"))
1187 p
->product
= _cupsStrAlloc(value
);
1188 #endif /* HAVE_DNSSD */
1191 cupsdLogMessage(CUPSD_LOG_ERROR
,
1192 "Syntax error on line %d of printers.conf.", linenum
);
1194 else if (!strcasecmp(line
, "Filter"))
1199 p
->filters
= cupsArrayNew(NULL
, NULL
);
1201 cupsArrayAdd(p
->filters
, _cupsStrAlloc(value
));
1204 cupsdLogMessage(CUPSD_LOG_ERROR
,
1205 "Syntax error on line %d of printers.conf.", linenum
);
1207 else if (!strcasecmp(line
, "PreFilter"))
1211 if (!p
->pre_filters
)
1212 p
->pre_filters
= cupsArrayNew(NULL
, NULL
);
1214 cupsArrayAdd(p
->pre_filters
, _cupsStrAlloc(value
));
1217 cupsdLogMessage(CUPSD_LOG_ERROR
,
1218 "Syntax error on line %d of printers.conf.", linenum
);
1220 else if (!strcasecmp(line
, "Shared"))
1223 * Set the initial shared state...
1227 (!strcasecmp(value
, "yes") ||
1228 !strcasecmp(value
, "on") ||
1229 !strcasecmp(value
, "true")))
1232 (!strcasecmp(value
, "no") ||
1233 !strcasecmp(value
, "off") ||
1234 !strcasecmp(value
, "false")))
1237 cupsdLogMessage(CUPSD_LOG_ERROR
,
1238 "Syntax error on line %d of printers.conf.", linenum
);
1240 else if (!strcasecmp(line
, "JobSheets"))
1243 * Set the initial job sheets...
1248 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
1253 cupsdSetString(&p
->job_sheets
[0], value
);
1255 while (isspace(*valueptr
& 255))
1260 for (value
= valueptr
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
1265 cupsdSetString(&p
->job_sheets
[1], value
);
1269 cupsdLogMessage(CUPSD_LOG_ERROR
,
1270 "Syntax error on line %d of printers.conf.", linenum
);
1272 else if (!strcasecmp(line
, "AllowUser"))
1277 cupsdAddPrinterUser(p
, value
);
1280 cupsdLogMessage(CUPSD_LOG_ERROR
,
1281 "Syntax error on line %d of printers.conf.", linenum
);
1283 else if (!strcasecmp(line
, "DenyUser"))
1288 cupsdAddPrinterUser(p
, value
);
1291 cupsdLogMessage(CUPSD_LOG_ERROR
,
1292 "Syntax error on line %d of printers.conf.", linenum
);
1294 else if (!strcasecmp(line
, "QuotaPeriod"))
1297 p
->quota_period
= atoi(value
);
1299 cupsdLogMessage(CUPSD_LOG_ERROR
,
1300 "Syntax error on line %d of printers.conf.", linenum
);
1302 else if (!strcasecmp(line
, "PageLimit"))
1305 p
->page_limit
= atoi(value
);
1307 cupsdLogMessage(CUPSD_LOG_ERROR
,
1308 "Syntax error on line %d of printers.conf.", linenum
);
1310 else if (!strcasecmp(line
, "KLimit"))
1313 p
->k_limit
= atoi(value
);
1315 cupsdLogMessage(CUPSD_LOG_ERROR
,
1316 "Syntax error on line %d of printers.conf.", linenum
);
1318 else if (!strcasecmp(line
, "OpPolicy"))
1322 cupsd_policy_t
*pol
; /* Policy */
1325 if ((pol
= cupsdFindPolicy(value
)) != NULL
)
1327 cupsdSetString(&p
->op_policy
, value
);
1328 p
->op_policy_ptr
= pol
;
1331 cupsdLogMessage(CUPSD_LOG_ERROR
,
1332 "Bad policy \"%s\" on line %d of printers.conf",
1336 cupsdLogMessage(CUPSD_LOG_ERROR
,
1337 "Syntax error on line %d of printers.conf.", linenum
);
1339 else if (!strcasecmp(line
, "ErrorPolicy"))
1342 cupsdSetString(&p
->error_policy
, value
);
1344 cupsdLogMessage(CUPSD_LOG_ERROR
,
1345 "Syntax error on line %d of printers.conf.", linenum
);
1347 else if (!strcasecmp(line
, "Attribute") && value
)
1349 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
1352 cupsdLogMessage(CUPSD_LOG_ERROR
,
1353 "Syntax error on line %d of printers.conf.", linenum
);
1356 for (; *valueptr
&& isspace(*valueptr
& 255); *valueptr
++ = '\0');
1359 cupsdSetPrinterAttrs(p
);
1361 if (!strcmp(value
, "marker-change-time"))
1362 p
->marker_time
= atoi(valueptr
);
1364 cupsdSetPrinterAttr(p
, value
, valueptr
);
1370 * Something else we don't understand...
1373 cupsdLogMessage(CUPSD_LOG_ERROR
,
1374 "Unknown configuration directive %s on line %d of printers.conf.",
1384 * 'cupsdRenamePrinter()' - Rename a printer.
1389 cupsd_printer_t
*p
, /* I - Printer */
1390 const char *name
) /* I - New name */
1393 * Remove the printer from the array(s) first...
1396 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1397 "cupsdRenamePrinter: Removing %s from Printers", p
->name
);
1398 cupsArrayRemove(Printers
, p
);
1400 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
1402 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1403 "cupsdRenamePrinter: Removing %s from ImplicitPrinters",
1405 cupsArrayRemove(ImplicitPrinters
, p
);
1409 * Rename the printer type...
1412 mimeDeleteType(MimeDatabase
, p
->filetype
);
1413 p
->filetype
= mimeAddType(MimeDatabase
, "printer", name
);
1415 mimeDeleteType(MimeDatabase
, p
->prefiltertype
);
1416 p
->prefiltertype
= mimeAddType(MimeDatabase
, "prefilter", name
);
1419 * Rename the printer...
1422 cupsdSetString(&p
->name
, name
);
1425 * Reset printer attributes...
1428 cupsdSetPrinterAttrs(p
);
1431 * Add the printer back to the printer array(s)...
1434 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1435 "cupsdRenamePrinter: Adding %s to Printers", p
->name
);
1436 cupsArrayAdd(Printers
, p
);
1438 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
1440 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1441 "cupsdRenamePrinter: Adding %s to ImplicitPrinters",
1443 cupsArrayAdd(ImplicitPrinters
, p
);
1449 * 'cupsdSaveAllPrinters()' - Save all printer definitions to the printers.conf
1454 cupsdSaveAllPrinters(void)
1456 int i
; /* Looping var */
1457 cups_file_t
*fp
; /* printers.conf file */
1458 char temp
[1024], /* Temporary string */
1459 backup
[1024], /* printers.conf.O file */
1460 value
[2048], /* Value string */
1461 *ptr
; /* Pointer into value */
1462 cupsd_printer_t
*printer
; /* Current printer class */
1463 time_t curtime
; /* Current time */
1464 struct tm
*curdate
; /* Current date */
1465 cups_option_t
*option
; /* Current option */
1466 ipp_attribute_t
*marker
; /* Current marker attribute */
1470 * Create the printers.conf file...
1473 snprintf(temp
, sizeof(temp
), "%s/printers.conf", ServerRoot
);
1474 snprintf(backup
, sizeof(backup
), "%s/printers.conf.O", ServerRoot
);
1476 if (rename(temp
, backup
))
1478 if (errno
!= ENOENT
)
1479 cupsdLogMessage(CUPSD_LOG_ERROR
,
1480 "Unable to backup printers.conf - %s", strerror(errno
));
1483 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
1485 cupsdLogMessage(CUPSD_LOG_ERROR
,
1486 "Unable to save printers.conf - %s", strerror(errno
));
1488 if (rename(backup
, temp
))
1489 cupsdLogMessage(CUPSD_LOG_ERROR
,
1490 "Unable to restore printers.conf - %s", strerror(errno
));
1494 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving printers.conf...");
1497 * Restrict access to the file...
1500 fchown(cupsFileNumber(fp
), getuid(), Group
);
1501 fchmod(cupsFileNumber(fp
), 0600);
1504 * Write a small header to the file...
1507 curtime
= time(NULL
);
1508 curdate
= localtime(&curtime
);
1509 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
1511 cupsFilePuts(fp
, "# Printer configuration file for " CUPS_SVERSION
"\n");
1512 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
1515 * Write each local printer known to the system...
1518 for (printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1520 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1523 * Skip remote destinations and printer classes...
1526 if ((printer
->type
& CUPS_PRINTER_DISCOVERED
) ||
1527 (printer
->type
& CUPS_PRINTER_CLASS
) ||
1528 (printer
->type
& CUPS_PRINTER_IMPLICIT
))
1532 * Write printers as needed...
1535 if (printer
== DefaultPrinter
)
1536 cupsFilePrintf(fp
, "<DefaultPrinter %s>\n", printer
->name
);
1538 cupsFilePrintf(fp
, "<Printer %s>\n", printer
->name
);
1540 if (printer
->num_auth_info_required
> 0)
1542 switch (printer
->num_auth_info_required
)
1545 strlcpy(value
, printer
->auth_info_required
[0], sizeof(value
));
1549 snprintf(value
, sizeof(value
), "%s,%s",
1550 printer
->auth_info_required
[0],
1551 printer
->auth_info_required
[1]);
1556 snprintf(value
, sizeof(value
), "%s,%s,%s",
1557 printer
->auth_info_required
[0],
1558 printer
->auth_info_required
[1],
1559 printer
->auth_info_required
[2]);
1563 cupsFilePutConf(fp
, "AuthInfoRequired", value
);
1567 cupsFilePutConf(fp
, "Info", printer
->info
);
1569 if (printer
->location
)
1570 cupsFilePutConf(fp
, "Location", printer
->location
);
1572 if (printer
->make_model
)
1573 cupsFilePutConf(fp
, "MakeModel", printer
->make_model
);
1575 cupsFilePutConf(fp
, "DeviceURI", printer
->device_uri
);
1577 if (printer
->port_monitor
)
1578 cupsFilePutConf(fp
, "PortMonitor", printer
->port_monitor
);
1580 if (printer
->state
== IPP_PRINTER_STOPPED
)
1582 cupsFilePuts(fp
, "State Stopped\n");
1584 if (printer
->state_message
)
1585 cupsFilePutConf(fp
, "StateMessage", printer
->state_message
);
1588 cupsFilePuts(fp
, "State Idle\n");
1590 cupsFilePrintf(fp
, "StateTime %d\n", (int)printer
->state_time
);
1592 for (i
= 0; i
< printer
->num_reasons
; i
++)
1593 if (strcmp(printer
->reasons
[i
], "connecting-to-device") &&
1594 strcmp(printer
->reasons
[i
], "cups-missing-filter-error"))
1595 cupsFilePutConf(fp
, "Reason", printer
->reasons
[i
]);
1597 cupsFilePrintf(fp
, "Type %d\n", printer
->type
);
1600 if (printer
->product
)
1601 cupsFilePutConf(fp
, "Product", printer
->product
);
1602 #endif /* HAVE_DNSSD */
1604 for (ptr
= (char *)cupsArrayFirst(printer
->filters
);
1606 ptr
= (char *)cupsArrayNext(printer
->filters
))
1607 cupsFilePutConf(fp
, "Filter", ptr
);
1609 for (ptr
= (char *)cupsArrayFirst(printer
->pre_filters
);
1611 ptr
= (char *)cupsArrayNext(printer
->pre_filters
))
1612 cupsFilePutConf(fp
, "PreFilter", ptr
);
1614 if (printer
->accepting
)
1615 cupsFilePuts(fp
, "Accepting Yes\n");
1617 cupsFilePuts(fp
, "Accepting No\n");
1619 if (printer
->shared
)
1620 cupsFilePuts(fp
, "Shared Yes\n");
1622 cupsFilePuts(fp
, "Shared No\n");
1624 snprintf(value
, sizeof(value
), "%s %s", printer
->job_sheets
[0],
1625 printer
->job_sheets
[1]);
1626 cupsFilePutConf(fp
, "JobSheets", value
);
1628 cupsFilePrintf(fp
, "QuotaPeriod %d\n", printer
->quota_period
);
1629 cupsFilePrintf(fp
, "PageLimit %d\n", printer
->page_limit
);
1630 cupsFilePrintf(fp
, "KLimit %d\n", printer
->k_limit
);
1632 for (i
= 0; i
< printer
->num_users
; i
++)
1633 cupsFilePutConf(fp
, printer
->deny_users
? "DenyUser" : "AllowUser",
1636 if (printer
->op_policy
)
1637 cupsFilePutConf(fp
, "OpPolicy", printer
->op_policy
);
1638 if (printer
->error_policy
)
1639 cupsFilePutConf(fp
, "ErrorPolicy", printer
->error_policy
);
1641 for (i
= printer
->num_options
, option
= printer
->options
;
1645 snprintf(value
, sizeof(value
), "%s %s", option
->name
, option
->value
);
1646 cupsFilePutConf(fp
, "Option", value
);
1649 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-colors",
1650 IPP_TAG_NAME
)) != NULL
)
1652 snprintf(value
, sizeof(value
), "%s ", marker
->name
);
1654 for (i
= 0, ptr
= value
+ strlen(value
);
1655 i
< marker
->num_values
&& ptr
< (value
+ sizeof(value
) - 1);
1661 strlcpy(ptr
, marker
->values
[i
].string
.text
,
1662 value
+ sizeof(value
) - ptr
);
1667 cupsFilePutConf(fp
, "Attribute", value
);
1670 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-levels",
1671 IPP_TAG_INTEGER
)) != NULL
)
1673 cupsFilePrintf(fp
, "Attribute %s %d", marker
->name
,
1674 marker
->values
[0].integer
);
1675 for (i
= 1; i
< marker
->num_values
; i
++)
1676 cupsFilePrintf(fp
, ",%d", marker
->values
[i
].integer
);
1677 cupsFilePuts(fp
, "\n");
1680 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-low-levels",
1681 IPP_TAG_INTEGER
)) != NULL
)
1683 cupsFilePrintf(fp
, "Attribute %s %d", marker
->name
,
1684 marker
->values
[0].integer
);
1685 for (i
= 1; i
< marker
->num_values
; i
++)
1686 cupsFilePrintf(fp
, ",%d", marker
->values
[i
].integer
);
1687 cupsFilePuts(fp
, "\n");
1690 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-high-levels",
1691 IPP_TAG_INTEGER
)) != NULL
)
1693 cupsFilePrintf(fp
, "Attribute %s %d", marker
->name
,
1694 marker
->values
[0].integer
);
1695 for (i
= 1; i
< marker
->num_values
; i
++)
1696 cupsFilePrintf(fp
, ",%d", marker
->values
[i
].integer
);
1697 cupsFilePuts(fp
, "\n");
1700 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-message",
1701 IPP_TAG_TEXT
)) != NULL
)
1703 snprintf(value
, sizeof(value
), "%s %s", marker
->name
,
1704 marker
->values
[0].string
.text
);
1706 cupsFilePutConf(fp
, "Attribute", value
);
1709 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-names",
1710 IPP_TAG_NAME
)) != NULL
)
1712 snprintf(value
, sizeof(value
), "%s ", marker
->name
);
1714 for (i
= 0, ptr
= value
+ strlen(value
);
1715 i
< marker
->num_values
&& ptr
< (value
+ sizeof(value
) - 1);
1721 strlcpy(ptr
, marker
->values
[i
].string
.text
,
1722 value
+ sizeof(value
) - ptr
);
1727 cupsFilePutConf(fp
, "Attribute", value
);
1730 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-types",
1731 IPP_TAG_KEYWORD
)) != NULL
)
1733 snprintf(value
, sizeof(value
), "%s ", marker
->name
);
1735 for (i
= 0, ptr
= value
+ strlen(value
);
1736 i
< marker
->num_values
&& ptr
< (value
+ sizeof(value
) - 1);
1742 strlcpy(ptr
, marker
->values
[i
].string
.text
,
1743 value
+ sizeof(value
) - ptr
);
1748 cupsFilePutConf(fp
, "Attribute", value
);
1751 if (printer
->marker_time
)
1752 cupsFilePrintf(fp
, "Attribute marker-change-time %ld\n",
1753 (long)printer
->marker_time
);
1755 cupsFilePuts(fp
, "</Printer>\n");
1759 * Make IRIX desktop & printer status happy
1762 write_irix_state(printer
);
1771 * 'cupsdSetAuthInfoRequired()' - Set the required authentication info.
1774 int /* O - 1 if value OK, 0 otherwise */
1775 cupsdSetAuthInfoRequired(
1776 cupsd_printer_t
*p
, /* I - Printer */
1777 const char *values
, /* I - Plain text value (or NULL) */
1778 ipp_attribute_t
*attr
) /* I - IPP attribute value (or NULL) */
1780 int i
; /* Looping var */
1783 p
->num_auth_info_required
= 0;
1786 * Do we have a plain text value?
1792 * Yes, grab the keywords...
1795 const char *end
; /* End of current value */
1798 while (*values
&& p
->num_auth_info_required
< 4)
1800 if ((end
= strchr(values
, ',')) == NULL
)
1801 end
= values
+ strlen(values
);
1803 if ((end
- values
) == 4 && !strncmp(values
, "none", 4))
1805 if (p
->num_auth_info_required
!= 0 || *end
)
1808 p
->auth_info_required
[p
->num_auth_info_required
] = "none";
1809 p
->num_auth_info_required
++;
1813 else if ((end
- values
) == 9 && !strncmp(values
, "negotiate", 9))
1815 if (p
->num_auth_info_required
!= 0 || *end
)
1818 p
->auth_info_required
[p
->num_auth_info_required
] = "negotiate";
1819 p
->num_auth_info_required
++;
1821 else if ((end
- values
) == 6 && !strncmp(values
, "domain", 6))
1823 p
->auth_info_required
[p
->num_auth_info_required
] = "domain";
1824 p
->num_auth_info_required
++;
1826 else if ((end
- values
) == 8 && !strncmp(values
, "password", 8))
1828 p
->auth_info_required
[p
->num_auth_info_required
] = "password";
1829 p
->num_auth_info_required
++;
1831 else if ((end
- values
) == 8 && !strncmp(values
, "username", 8))
1833 p
->auth_info_required
[p
->num_auth_info_required
] = "username";
1834 p
->num_auth_info_required
++;
1839 values
= (*end
) ? end
+ 1 : end
;
1842 if (p
->num_auth_info_required
== 0)
1844 p
->auth_info_required
[0] = "none";
1845 p
->num_auth_info_required
= 1;
1849 * Update the printer-type value as needed...
1852 if (p
->num_auth_info_required
> 1 ||
1853 strcmp(p
->auth_info_required
[0], "none"))
1854 p
->type
|= CUPS_PRINTER_AUTHENTICATED
;
1856 p
->type
&= ~CUPS_PRINTER_AUTHENTICATED
;
1862 * Grab values from an attribute instead...
1865 if (!attr
|| attr
->num_values
> 4)
1869 * Update the printer-type value as needed...
1872 if (attr
->num_values
> 1 ||
1873 strcmp(attr
->values
[0].string
.text
, "none"))
1874 p
->type
|= CUPS_PRINTER_AUTHENTICATED
;
1876 p
->type
&= ~CUPS_PRINTER_AUTHENTICATED
;
1878 for (i
= 0; i
< attr
->num_values
; i
++)
1880 if (!strcmp(attr
->values
[i
].string
.text
, "none"))
1882 if (p
->num_auth_info_required
!= 0 || attr
->num_values
!= 1)
1885 p
->auth_info_required
[p
->num_auth_info_required
] = "none";
1886 p
->num_auth_info_required
++;
1890 else if (!strcmp(attr
->values
[i
].string
.text
, "negotiate"))
1892 if (p
->num_auth_info_required
!= 0 || attr
->num_values
!= 1)
1895 p
->auth_info_required
[p
->num_auth_info_required
] = "negotiate";
1896 p
->num_auth_info_required
++;
1900 else if (!strcmp(attr
->values
[i
].string
.text
, "domain"))
1902 p
->auth_info_required
[p
->num_auth_info_required
] = "domain";
1903 p
->num_auth_info_required
++;
1905 else if (!strcmp(attr
->values
[i
].string
.text
, "password"))
1907 p
->auth_info_required
[p
->num_auth_info_required
] = "password";
1908 p
->num_auth_info_required
++;
1910 else if (!strcmp(attr
->values
[i
].string
.text
, "username"))
1912 p
->auth_info_required
[p
->num_auth_info_required
] = "username";
1913 p
->num_auth_info_required
++;
1924 * 'cupsdSetDeviceURI()' - Set the device URI for a printer.
1928 cupsdSetDeviceURI(cupsd_printer_t
*p
, /* I - Printer */
1929 const char *uri
) /* I - Device URI */
1931 char buffer
[1024], /* URI buffer */
1932 *start
, /* Start of data after scheme */
1933 *slash
, /* First slash after scheme:// */
1934 *ptr
; /* Pointer into user@host:port part */
1938 * Set the full device URI..
1941 cupsdSetString(&(p
->device_uri
), uri
);
1944 * Copy the device URI to a temporary buffer so we can sanitize any auth
1948 strlcpy(buffer
, uri
, sizeof(buffer
));
1951 * Find the end of the scheme:// part...
1954 if ((ptr
= strchr(buffer
, ':')) != NULL
)
1956 for (start
= ptr
+ 1; *start
; start
++)
1961 * Find the next slash (/) in the URI...
1964 if ((slash
= strchr(start
, '/')) == NULL
)
1965 slash
= start
+ strlen(start
); /* No slash, point to the end */
1968 * Check for an @ sign before the slash...
1971 if ((ptr
= strchr(start
, '@')) != NULL
&& ptr
< slash
)
1974 * Found an @ sign and it is before the resource part, so we have
1975 * an authentication string. Copy the remaining URI over the
1976 * authentication string...
1979 _cups_strcpy(start
, ptr
+ 1);
1984 * Save the sanitized URI...
1987 cupsdSetString(&(p
->sanitized_device_uri
), buffer
);
1992 * 'cupsdSetPrinterAttr()' - Set a printer attribute.
1996 cupsdSetPrinterAttr(
1997 cupsd_printer_t
*p
, /* I - Printer */
1998 const char *name
, /* I - Attribute name */
1999 char *value
) /* I - Attribute value string */
2001 ipp_attribute_t
*attr
; /* Attribute */
2002 int i
, /* Looping var */
2003 count
; /* Number of values */
2004 char *ptr
; /* Pointer into value */
2005 ipp_tag_t value_tag
; /* Value tag for this attribute */
2009 * Don't allow empty values...
2014 cupsdLogMessage(CUPSD_LOG_ERROR
, "Ignoring empty \"%s\" attribute", name
);
2019 * Count the number of values...
2022 for (count
= 1, ptr
= value
;
2023 (ptr
= strchr(ptr
, ',')) != NULL
;
2027 * Then add or update the attribute as needed...
2030 if (!strcmp(name
, "marker-levels") || !strcmp(name
, "marker-low-levels") ||
2031 !strcmp(name
, "marker-high-levels"))
2037 if ((attr
= ippFindAttribute(p
->attrs
, name
, IPP_TAG_INTEGER
)) != NULL
&&
2038 attr
->num_values
< count
)
2040 ippDeleteAttribute(p
->attrs
, attr
);
2045 attr
->num_values
= count
;
2047 attr
= ippAddIntegers(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, name
,
2052 cupsdLogMessage(CUPSD_LOG_ERROR
,
2053 "Unable to allocate memory for printer attribute "
2054 "(%d values)", count
);
2058 for (i
= 0; i
< count
; i
++)
2060 if ((ptr
= strchr(value
, ',')) != NULL
)
2063 attr
->values
[i
].integer
= strtol(value
, NULL
, 10);
2072 * Name or keyword values...
2075 if (!strcmp(name
, "marker-types"))
2076 value_tag
= IPP_TAG_KEYWORD
;
2077 else if (!strcmp(name
, "marker-message"))
2078 value_tag
= IPP_TAG_TEXT
;
2080 value_tag
= IPP_TAG_NAME
;
2082 if ((attr
= ippFindAttribute(p
->attrs
, name
, value_tag
)) != NULL
&&
2083 attr
->num_values
< count
)
2085 ippDeleteAttribute(p
->attrs
, attr
);
2091 for (i
= 0; i
< attr
->num_values
; i
++)
2092 _cupsStrFree(attr
->values
[i
].string
.text
);
2094 attr
->num_values
= count
;
2097 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, value_tag
, name
,
2102 cupsdLogMessage(CUPSD_LOG_ERROR
,
2103 "Unable to allocate memory for printer attribute "
2104 "(%d values)", count
);
2108 for (i
= 0; i
< count
; i
++)
2110 if ((ptr
= strchr(value
, ',')) != NULL
)
2113 attr
->values
[i
].string
.text
= _cupsStrAlloc(value
);
2123 * 'cupsdSetPrinterAttrs()' - Set printer attributes based upon the PPD file.
2127 cupsdSetPrinterAttrs(cupsd_printer_t
*p
)/* I - Printer to setup */
2129 int i
, /* Looping var */
2130 length
; /* Length of browse attributes */
2131 char resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2132 int num_air
; /* Number of auth-info-required values */
2133 const char * const *air
; /* auth-info-required values */
2134 cupsd_location_t
*auth
; /* Pointer to authentication element */
2135 const char *auth_supported
; /* Authentication supported */
2136 ipp_t
*oldattrs
; /* Old printer attributes */
2137 ipp_attribute_t
*attr
; /* Attribute data */
2138 cups_option_t
*option
; /* Current printer option */
2139 char *filter
; /* Current filter */
2140 static const char * const air_userpass
[] =
2141 { /* Basic/Digest authentication */
2146 static const char * const air_negotiate
[] =
2147 { /* Kerberos authentication */
2150 #endif /* HAVE_GSSAPI */
2151 static const char * const air_none
[] =
2152 { /* No authentication */
2157 DEBUG_printf(("cupsdSetPrinterAttrs: entering name = %s, type = %x\n", p
->name
,
2161 * Make sure that we have the common attributes defined...
2165 cupsdCreateCommonData();
2168 * Clear out old filters, if any...
2171 delete_printer_filters(p
);
2174 * Figure out the authentication that is required for the printer.
2177 auth_supported
= "requesting-user-name";
2181 if (p
->num_auth_info_required
> 0 && strcmp(p
->auth_info_required
[0], "none"))
2183 num_air
= p
->num_auth_info_required
;
2184 air
= p
->auth_info_required
;
2186 if (!strcmp(air
[0], "username"))
2187 auth_supported
= "basic";
2189 auth_supported
= "negotiate";
2191 else if (!(p
->type
& CUPS_PRINTER_DISCOVERED
))
2193 if (p
->type
& CUPS_PRINTER_CLASS
)
2194 snprintf(resource
, sizeof(resource
), "/classes/%s", p
->name
);
2196 snprintf(resource
, sizeof(resource
), "/printers/%s", p
->name
);
2198 if ((auth
= cupsdFindBest(resource
, HTTP_POST
)) == NULL
||
2199 auth
->type
== CUPSD_AUTH_NONE
)
2200 auth
= cupsdFindPolicyOp(p
->op_policy_ptr
, IPP_PRINT_JOB
);
2204 int auth_type
; /* Authentication type */
2207 if ((auth_type
= auth
->type
) == CUPSD_AUTH_DEFAULT
)
2208 auth_type
= DefaultAuthType
;
2210 if (auth_type
== CUPSD_AUTH_BASIC
|| auth_type
== CUPSD_AUTH_BASICDIGEST
)
2212 auth_supported
= "basic";
2216 else if (auth_type
== CUPSD_AUTH_DIGEST
)
2218 auth_supported
= "digest";
2223 else if (auth_type
== CUPSD_AUTH_NEGOTIATE
)
2225 auth_supported
= "negotiate";
2227 air
= air_negotiate
;
2229 #endif /* HAVE_GSSAPI */
2231 if (auth_type
!= CUPSD_AUTH_NONE
)
2232 p
->type
|= CUPS_PRINTER_AUTHENTICATED
;
2234 p
->type
&= ~CUPS_PRINTER_AUTHENTICATED
;
2237 p
->type
&= ~CUPS_PRINTER_AUTHENTICATED
;
2239 else if (p
->type
& CUPS_PRINTER_AUTHENTICATED
)
2246 * Create the required IPP attributes for a printer...
2249 oldattrs
= p
->attrs
;
2250 p
->attrs
= ippNew();
2252 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2253 "uri-authentication-supported", NULL
, auth_supported
);
2254 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2255 "uri-security-supported", NULL
, "none");
2256 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "printer-name", NULL
,
2258 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-location",
2259 NULL
, p
->location
? p
->location
: "");
2260 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-info",
2261 NULL
, p
->info
? p
->info
: "");
2266 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2267 "requesting-user-name-denied", p
->num_users
, NULL
,
2270 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2271 "requesting-user-name-allowed", p
->num_users
, NULL
,
2275 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2276 "job-quota-period", p
->quota_period
);
2277 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2278 "job-k-limit", p
->k_limit
);
2279 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2280 "job-page-limit", p
->page_limit
);
2281 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2282 "auth-info-required", num_air
, NULL
, air
);
2284 if (cupsArrayCount(Banners
) > 0 && !(p
->type
& CUPS_PRINTER_DISCOVERED
))
2287 * Setup the job-sheets-default attribute...
2290 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2291 "job-sheets-default", 2, NULL
, NULL
);
2295 attr
->values
[0].string
.text
= _cupsStrAlloc(Classification
?
2296 Classification
: p
->job_sheets
[0]);
2297 attr
->values
[1].string
.text
= _cupsStrAlloc(Classification
?
2298 Classification
: p
->job_sheets
[1]);
2305 if (p
->type
& CUPS_PRINTER_DISCOVERED
)
2308 * Tell the client this is a remote printer of some type...
2311 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
2312 "printer-uri-supported", NULL
, p
->uri
);
2314 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "printer-more-info",
2318 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
2319 "printer-make-and-model", NULL
, p
->make_model
);
2321 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri", NULL
,
2330 * Assign additional attributes depending on whether this is a printer
2334 if (p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
2337 p
->type
&= ~CUPS_PRINTER_OPTIONS
;
2340 * Add class-specific attributes...
2343 if ((p
->type
& CUPS_PRINTER_IMPLICIT
) && p
->num_printers
> 0 &&
2344 p
->printers
[0]->make_model
)
2345 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
2346 "printer-make-and-model", NULL
, p
->printers
[0]->make_model
);
2348 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
2349 "printer-make-and-model", NULL
, "Local Printer Class");
2351 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri", NULL
,
2352 "file:///dev/null");
2354 if (p
->num_printers
> 0)
2357 * Add a list of member names; URIs are added in copy_printer_attrs...
2360 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2361 "member-names", p
->num_printers
, NULL
, NULL
);
2362 p
->type
|= CUPS_PRINTER_OPTIONS
;
2364 for (i
= 0; i
< p
->num_printers
; i
++)
2367 attr
->values
[i
].string
.text
= _cupsStrRetain(p
->printers
[i
]->name
);
2369 p
->type
&= ~CUPS_PRINTER_OPTIONS
| p
->printers
[i
]->type
;
2376 * Add printer-specific attributes...
2379 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri", NULL
,
2380 p
->sanitized_device_uri
);
2383 * Assign additional attributes from the PPD file (if any)...
2389 * Add filters for printer...
2392 cupsdSetPrinterReasons(p
, "-cups-missing-filter-error");
2394 for (filter
= (char *)cupsArrayFirst(p
->filters
);
2396 filter
= (char *)cupsArrayNext(p
->filters
))
2397 add_printer_filter(p
, p
->filetype
, filter
);
2401 p
->prefiltertype
= mimeAddType(MimeDatabase
, "prefilter", p
->name
);
2403 for (filter
= (char *)cupsArrayFirst(p
->pre_filters
);
2405 filter
= (char *)cupsArrayNext(p
->pre_filters
))
2406 add_printer_filter(p
, p
->prefiltertype
, filter
);
2412 * Copy marker attributes as needed...
2417 ipp_attribute_t
*oldattr
; /* Old attribute */
2420 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-colors",
2421 IPP_TAG_NAME
)) != NULL
)
2423 if ((attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2424 "marker-colors", oldattr
->num_values
, NULL
,
2427 for (i
= 0; i
< oldattr
->num_values
; i
++)
2428 attr
->values
[i
].string
.text
=
2429 _cupsStrRetain(oldattr
->values
[i
].string
.text
);
2433 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-levels",
2434 IPP_TAG_INTEGER
)) != NULL
)
2436 if ((attr
= ippAddIntegers(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2437 "marker-levels", oldattr
->num_values
,
2440 for (i
= 0; i
< oldattr
->num_values
; i
++)
2441 attr
->values
[i
].integer
= oldattr
->values
[i
].integer
;
2445 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-names",
2446 IPP_TAG_NAME
)) != NULL
)
2448 if ((attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2449 "marker-names", oldattr
->num_values
, NULL
,
2452 for (i
= 0; i
< oldattr
->num_values
; i
++)
2453 attr
->values
[i
].string
.text
=
2454 _cupsStrRetain(oldattr
->values
[i
].string
.text
);
2458 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-types",
2459 IPP_TAG_KEYWORD
)) != NULL
)
2461 if ((attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2462 "marker-types", oldattr
->num_values
, NULL
,
2465 for (i
= 0; i
< oldattr
->num_values
; i
++)
2466 attr
->values
[i
].string
.text
=
2467 _cupsStrRetain(oldattr
->values
[i
].string
.text
);
2471 ippDelete(oldattrs
);
2475 * Force sharing off for remote queues...
2478 if (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
))
2483 * Copy the printer options into a browse attributes string we can re-use.
2486 const char *valptr
; /* Pointer into value */
2487 char *attrptr
; /* Pointer into attribute string */
2491 * Free the old browse attributes as needed...
2494 if (p
->browse_attrs
)
2495 free(p
->browse_attrs
);
2498 * Compute the length of all attributes + job-sheets, lease-duration,
2499 * and BrowseLocalOptions.
2502 for (length
= 1, i
= p
->num_options
, option
= p
->options
;
2506 length
+= strlen(option
->name
) + 2;
2510 for (valptr
= option
->value
; *valptr
; valptr
++)
2511 if (strchr(" \"\'\\", *valptr
))
2518 length
+= 13 + strlen(p
->job_sheets
[0]) + strlen(p
->job_sheets
[1]);
2520 if (BrowseLocalOptions
)
2521 length
+= 12 + strlen(BrowseLocalOptions
);
2523 if (p
->num_auth_info_required
> 0)
2525 length
+= 18; /* auth-info-required */
2527 for (i
= 0; i
< p
->num_auth_info_required
; i
++)
2528 length
+= strlen(p
->auth_info_required
[i
]) + 1;
2532 * Allocate the new string...
2535 if ((p
->browse_attrs
= calloc(1, length
)) == NULL
)
2536 cupsdLogMessage(CUPSD_LOG_ERROR
,
2537 "Unable to allocate %d bytes for browse data!",
2542 * Got the allocated string, now copy the options and attributes over...
2545 sprintf(p
->browse_attrs
, "job-sheets=%s,%s lease-duration=%d",
2546 p
->job_sheets
[0], p
->job_sheets
[1], BrowseTimeout
);
2547 attrptr
= p
->browse_attrs
+ strlen(p
->browse_attrs
);
2549 if (BrowseLocalOptions
)
2551 sprintf(attrptr
, " ipp-options=%s", BrowseLocalOptions
);
2552 attrptr
+= strlen(attrptr
);
2555 for (i
= p
->num_options
, option
= p
->options
;
2560 strcpy(attrptr
, option
->name
);
2561 attrptr
+= strlen(attrptr
);
2567 for (valptr
= option
->value
; *valptr
; valptr
++)
2569 if (strchr(" \"\'\\", *valptr
))
2572 *attrptr
++ = *valptr
;
2577 if (p
->num_auth_info_required
> 0)
2579 strcpy(attrptr
, "auth-info-required");
2582 for (i
= 0; i
< p
->num_auth_info_required
; i
++)
2584 *attrptr
++ = i
? ',' : '=';
2585 strcpy(attrptr
, p
->auth_info_required
[i
]);
2586 attrptr
+= strlen(attrptr
);
2595 * Populate the document-format-supported attribute...
2598 add_printer_formats(p
);
2600 DEBUG_printf(("cupsdSetPrinterAttrs: leaving name = %s, type = %x\n", p
->name
,
2604 * Add name-default attributes...
2607 add_printer_defaults(p
);
2611 * Write the IRIX printer config and status files...
2614 write_irix_config(p
);
2615 write_irix_state(p
);
2619 * Let the browse protocols reflect the change
2622 cupsdRegisterPrinter(p
);
2627 * 'cupsdSetPrinterReasons()' - Set/update the reasons strings.
2631 cupsdSetPrinterReasons(
2632 cupsd_printer_t
*p
, /* I - Printer */
2633 const char *s
) /* I - Reasons strings */
2635 int i
; /* Looping var */
2636 const char *sptr
; /* Pointer into reasons */
2637 char reason
[255], /* Reason string */
2638 *rptr
; /* Pointer into reason */
2643 cupsdLogMessage(CUPSD_LOG_EMERG
,
2644 "cupsdSetPrinterReasons called with p=%p and s=%p!", p
, s
);
2648 if (LogLevel
== CUPSD_LOG_DEBUG2
)
2650 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2651 "cupsdSetPrinterReasons(p=%p(%s),s=\"%s\"", p
, p
->name
, s
);
2652 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdSetPrinterReasons: num_reasons=%d",
2654 for (i
= 0; i
< p
->num_reasons
; i
++)
2655 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2656 "cupsdSetPrinterReasons: reasons[%d]=%p(\"%s\")", i
,
2657 p
->reasons
[i
], p
->reasons
[i
]);
2660 if (s
[0] == '-' || s
[0] == '+')
2663 * Add/remove reasons...
2671 * Replace reasons...
2676 for (i
= 0; i
< p
->num_reasons
; i
++)
2677 _cupsStrFree(p
->reasons
[i
]);
2681 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
2683 if (PrintcapFormat
== PRINTCAP_PLIST
)
2684 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2687 if (!strcmp(s
, "none"))
2691 * Loop through all of the reasons...
2697 * Skip leading whitespace and commas...
2700 while (isspace(*sptr
& 255) || *sptr
== ',')
2703 for (rptr
= reason
; *sptr
&& !isspace(*sptr
& 255) && *sptr
!= ','; sptr
++)
2704 if (rptr
< (reason
+ sizeof(reason
) - 1))
2718 for (i
= 0; i
< p
->num_reasons
; i
++)
2719 if (!strcmp(reason
, p
->reasons
[i
]))
2722 * Found a match, so remove it...
2725 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2726 "cupsdSetPrinterReasons: Removing \"%s\" at index %d",
2730 _cupsStrFree(p
->reasons
[i
]);
2732 if (i
< p
->num_reasons
)
2733 memmove(p
->reasons
+ i
, p
->reasons
+ i
+ 1,
2734 (p
->num_reasons
- i
) * sizeof(char *));
2736 if (!strcmp(reason
, "paused") && p
->state
== IPP_PRINTER_STOPPED
)
2737 cupsdSetPrinterState(p
, IPP_PRINTER_IDLE
, 1);
2739 if (strcmp(reason
, "connecting-to-device"))
2740 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
2742 if (PrintcapFormat
== PRINTCAP_PLIST
)
2743 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2748 else if (p
->num_reasons
< (int)(sizeof(p
->reasons
) / sizeof(p
->reasons
[0])))
2754 for (i
= 0; i
< p
->num_reasons
; i
++)
2755 if (!strcmp(reason
, p
->reasons
[i
]))
2758 if (i
>= p
->num_reasons
)
2760 if (i
>= (int)(sizeof(p
->reasons
) / sizeof(p
->reasons
[0])))
2762 cupsdLogMessage(CUPSD_LOG_ALERT
,
2763 "Too many printer-state-reasons values for %s (%d)",
2768 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2769 "cupsdSetPrinterReasons: Adding \"%s\" at index %d",
2772 p
->reasons
[i
] = _cupsStrAlloc(reason
);
2775 if (!strcmp(reason
, "paused") && p
->state
!= IPP_PRINTER_STOPPED
)
2776 cupsdSetPrinterState(p
, IPP_PRINTER_STOPPED
, 1);
2778 if (strcmp(reason
, "connecting-to-device"))
2779 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
2781 if (PrintcapFormat
== PRINTCAP_PLIST
)
2782 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2787 if (LogLevel
== CUPSD_LOG_DEBUG2
)
2789 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2790 "cupsdSetPrinterReasons: NEW num_reasons=%d",
2792 for (i
= 0; i
< p
->num_reasons
; i
++)
2793 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2794 "cupsdSetPrinterReasons: NEW reasons[%d]=%p(\"%s\")", i
,
2795 p
->reasons
[i
], p
->reasons
[i
]);
2801 * 'cupsdSetPrinterState()' - Update the current state of a printer.
2805 cupsdSetPrinterState(
2806 cupsd_printer_t
*p
, /* I - Printer to change */
2807 ipp_pstate_t s
, /* I - New state */
2808 int update
) /* I - Update printers.conf? */
2810 ipp_pstate_t old_state
; /* Old printer state */
2814 * Can't set status of remote printers...
2817 if (p
->type
& CUPS_PRINTER_DISCOVERED
)
2821 * Set the new state...
2824 if (PrintcapFormat
== PRINTCAP_PLIST
)
2825 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2827 old_state
= p
->state
;
2832 cupsdAddEvent(s
== IPP_PRINTER_STOPPED
? CUPSD_EVENT_PRINTER_STOPPED
:
2833 CUPSD_EVENT_PRINTER_STATE
, p
, NULL
,
2834 "%s \"%s\" state changed.",
2835 (p
->type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
2839 * Let the browse code know this needs to be updated...
2843 p
->state_time
= time(NULL
);
2847 write_irix_state(p
);
2851 if (s
== IPP_PRINTER_STOPPED
)
2852 cupsdSetPrinterReasons(p
, "+paused");
2854 cupsdSetPrinterReasons(p
, "-paused");
2856 cupsdAddPrinterHistory(p
);
2859 * Let the browse protocols reflect the change...
2863 cupsdRegisterPrinter(p
);
2866 * Save the printer configuration if a printer goes from idle or processing
2867 * to stopped (or visa-versa)...
2870 if ((old_state
== IPP_PRINTER_STOPPED
) != (s
== IPP_PRINTER_STOPPED
) &&
2873 if (p
->type
& CUPS_PRINTER_CLASS
)
2874 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
2876 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
2882 * 'cupsdStopPrinter()' - Stop a printer from printing any jobs...
2886 cupsdStopPrinter(cupsd_printer_t
*p
, /* I - Printer to stop */
2887 int update
)/* I - Update printers.conf? */
2889 cupsd_job_t
*job
; /* Active print job */
2893 * Set the printer state...
2896 cupsdSetPrinterState(p
, IPP_PRINTER_STOPPED
, update
);
2899 * See if we have a job printing on this printer...
2905 * Get pointer to job...
2908 job
= (cupsd_job_t
*)p
->job
;
2914 cupsdStopJob(job
, 0);
2917 * Reset the state to pending...
2920 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
2921 job
->state_value
= IPP_JOB_PENDING
;
2924 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2926 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, p
, job
,
2927 "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 snprintf(p
->state_message
, sizeof(p
->state_message
),
3593 "Filter \"%s\" for printer \"%s\" not available: %s",
3594 filename
, p
->name
, strerror(errno
));
3595 cupsdSetPrinterReasons(p
, "+cups-missing-filter-error");
3597 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s", p
->state_message
);
3602 * When running as root, do additional security checks...
3608 * Only use filters that are owned by root and do not have world write
3612 if (fileinfo
.st_uid
|| (fileinfo
.st_mode
& S_IWOTH
) != 0)
3614 if (fileinfo
.st_uid
)
3615 snprintf(p
->state_message
, sizeof(p
->state_message
),
3616 "Filter \"%s\" for printer \"%s\" not owned by root",
3619 snprintf(p
->state_message
, sizeof(p
->state_message
),
3620 "Filter \"%s\" for printer \"%s\" has insecure permissions "
3621 "(0%o)", filename
, p
->name
, fileinfo
.st_mode
);
3623 cupsdSetPrinterReasons(p
, "+cups-insecure-filter-error");
3625 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s", p
->state_message
);
3630 * Similarly, check that the parent directory is also owned by root and
3631 * does not have world write permissions.
3634 if ((dirsep
= strrchr(filename
, '/')) != NULL
)
3637 if (stat(filename
, &fileinfo
))
3639 snprintf(p
->state_message
, sizeof(p
->state_message
),
3640 "Filter directory \"%s\" for printer \"%s\" not accessible: %s",
3641 filename
, p
->name
, strerror(errno
));
3642 cupsdSetPrinterReasons(p
, "+cups-missing-filter-error");
3644 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s", p
->state_message
);
3647 else if (fileinfo
.st_uid
|| (fileinfo
.st_mode
& S_IWOTH
) != 0)
3649 if (fileinfo
.st_uid
)
3650 snprintf(p
->state_message
, sizeof(p
->state_message
),
3651 "Filter directory \"%s\" for printer \"%s\" not owned by "
3652 "root", filename
, p
->name
);
3654 snprintf(p
->state_message
, sizeof(p
->state_message
),
3655 "Filter directory \"%s\" for printer \"%s\" has insecure "
3656 "permissions (0%o)", filename
, p
->name
, fileinfo
.st_mode
);
3658 cupsdSetPrinterReasons(p
, "+cups-insecure-filter-error");
3660 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s", p
->state_message
);
3667 * Add the filter to the MIME database, supporting wildcards as needed...
3670 for (temptype
= mimeFirstType(MimeDatabase
);
3672 temptype
= mimeNextType(MimeDatabase
))
3673 if (((super
[0] == '*' && strcasecmp(temptype
->super
, "printer")) ||
3674 !strcasecmp(temptype
->super
, super
)) &&
3675 (type
[0] == '*' || !strcasecmp(temptype
->type
, type
)))
3677 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3678 "add_printer_filter: %s: adding filter %s/%s %s/%s %d %s",
3679 p
->name
, temptype
->super
, temptype
->type
,
3680 filtertype
->super
, filtertype
->type
,
3682 mimeAddFilter(MimeDatabase
, temptype
, filtertype
, cost
, program
);
3688 * 'add_printer_formats()' - Add document-format-supported values for a printer.
3692 add_printer_formats(cupsd_printer_t
*p
) /* I - Printer */
3694 int i
; /* Looping var */
3695 mime_type_t
*type
; /* Current MIME type */
3696 cups_array_t
*filters
; /* Filters */
3697 ipp_attribute_t
*attr
; /* document-format-supported attribute */
3698 char mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
3699 /* MIME type name */
3703 * Raw (and remote) queues advertise all of the supported MIME
3707 cupsArrayDelete(p
->filetypes
);
3708 p
->filetypes
= NULL
;
3712 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
,
3713 (ipp_tag_t
)(IPP_TAG_MIMETYPE
| IPP_TAG_COPY
),
3714 "document-format-supported", NumMimeTypes
, NULL
, MimeTypes
);
3719 * Otherwise, loop through the supported MIME types and see if there
3720 * are filters for them...
3723 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_printer_formats: %d types, %d filters",
3724 mimeNumTypes(MimeDatabase
), mimeNumFilters(MimeDatabase
));
3726 p
->filetypes
= cupsArrayNew(NULL
, NULL
);
3728 for (type
= mimeFirstType(MimeDatabase
);
3730 type
= mimeNextType(MimeDatabase
))
3732 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", type
->super
, type
->type
);
3734 if ((filters
= mimeFilter(MimeDatabase
, type
, p
->filetype
, NULL
)) != NULL
)
3736 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3737 "add_printer_formats: %s: %s needs %d filters",
3738 p
->name
, mimetype
, cupsArrayCount(filters
));
3740 cupsArrayDelete(filters
);
3741 cupsArrayAdd(p
->filetypes
, type
);
3744 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3745 "add_printer_formats: %s: %s not supported",
3749 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3750 "add_printer_formats: %s: %d supported types",
3751 p
->name
, cupsArrayCount(p
->filetypes
) + 1);
3754 * Add the file formats that can be filtered...
3757 if ((type
= mimeType(MimeDatabase
, "application", "octet-stream")) == NULL
||
3758 !cupsArrayFind(p
->filetypes
, type
))
3763 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_MIMETYPE
,
3764 "document-format-supported",
3765 cupsArrayCount(p
->filetypes
) + 1, NULL
, NULL
);
3768 attr
->values
[0].string
.text
= _cupsStrAlloc("application/octet-stream");
3770 for (type
= (mime_type_t
*)cupsArrayFirst(p
->filetypes
);
3772 i
++, type
= (mime_type_t
*)cupsArrayNext(p
->filetypes
))
3774 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", type
->super
, type
->type
);
3776 attr
->values
[i
].string
.text
= _cupsStrAlloc(mimetype
);
3781 char pdl
[1024]; /* Buffer to build pdl list */
3782 mime_filter_t
*filter
; /* MIME filter looping var */
3787 if (mimeType(MimeDatabase
, "application", "pdf"))
3788 strlcat(pdl
, "application/pdf,", sizeof(pdl
));
3790 if (mimeType(MimeDatabase
, "application", "postscript"))
3791 strlcat(pdl
, "application/postscript,", sizeof(pdl
));
3793 if (mimeType(MimeDatabase
, "application", "vnd.cups-raster"))
3794 strlcat(pdl
, "application/vnd.cups-raster,", sizeof(pdl
));
3797 * Determine if this is a Tioga PrintJobMgr based queue...
3800 for (filter
= (mime_filter_t
*)cupsArrayFirst(MimeDatabase
->filters
);
3802 filter
= (mime_filter_t
*)cupsArrayNext(MimeDatabase
->filters
))
3804 if (filter
->dst
== p
->filetype
&& filter
->filter
&&
3805 strstr(filter
->filter
, "PrintJobMgr"))
3810 * We only support raw printing if this is not a Tioga PrintJobMgr based
3811 * queue and if application/octet-stream is a known conversion...
3814 if (!filter
&& mimeType(MimeDatabase
, "application", "octet-stream"))
3815 strlcat(pdl
, "application/octet-stream,", sizeof(pdl
));
3817 if (mimeType(MimeDatabase
, "image", "png"))
3818 strlcat(pdl
, "image/png,", sizeof(pdl
));
3821 pdl
[strlen(pdl
) - 1] = '\0'; /* Remove trailing comma */
3823 cupsdSetString(&p
->pdl
, pdl
);
3825 #endif /* HAVE_DNSSD */
3830 * 'add_string_array()' - Add a string to an array of CUPS strings.
3834 add_string_array(cups_array_t
**a
, /* I - Array */
3835 const char *s
) /* I - String */
3838 *a
= cupsArrayNew(NULL
, NULL
);
3840 cupsArrayAdd(*a
, _cupsStrAlloc(s
));
3845 * 'compare_printers()' - Compare two printers.
3848 static int /* O - Result of comparison */
3849 compare_printers(void *first
, /* I - First printer */
3850 void *second
, /* I - Second printer */
3851 void *data
) /* I - App data (not used) */
3853 return (strcasecmp(((cupsd_printer_t
*)first
)->name
,
3854 ((cupsd_printer_t
*)second
)->name
));
3859 * 'delete_printer_filters()' - Delete all MIME filters for a printer.
3863 delete_printer_filters(
3864 cupsd_printer_t
*p
) /* I - Printer to remove from */
3866 mime_filter_t
*filter
; /* MIME filter looping var */
3870 * Range check input...
3877 * Remove all filters from the MIME database that have a destination
3878 * type == printer...
3881 for (filter
= mimeFirstFilter(MimeDatabase
);
3883 filter
= mimeNextFilter(MimeDatabase
))
3884 if (filter
->dst
== p
->filetype
)
3887 * Delete the current filter...
3890 mimeDeleteFilter(MimeDatabase
, filter
);
3896 * 'delete_string_array()' - Delete an array of CUPS strings.
3900 delete_string_array(cups_array_t
**a
) /* I - Array */
3902 char *ptr
; /* Current string */
3905 for (ptr
= (char *)cupsArrayFirst(*a
);
3907 ptr
= (char *)cupsArrayNext(*a
))
3910 cupsArrayDelete(*a
);
3916 * 'load_ppd()' - Load a cached PPD file, updating the cache as needed.
3920 load_ppd(cupsd_printer_t
*p
) /* I - Printer */
3922 int i
; /* Looping var */
3923 cups_file_t
*cache
; /* Cache file */
3924 char cache_name
[1024]; /* Cache filename */
3925 struct stat cache_info
; /* Cache file info */
3926 ppd_file_t
*ppd
; /* PPD file */
3927 char ppd_name
[1024]; /* PPD filename */
3928 struct stat ppd_info
; /* PPD file info */
3929 int num_media
; /* Number of media options */
3930 char custom_in
[256], /* Custom size name in inches */
3931 custom_mm
[256]; /* Custom size name in millimeters */
3932 ppd_size_t
*size
; /* Current size */
3933 ppd_option_t
*output_bin
, /* OutputBin options */
3934 *duplex
; /* Duplex options */
3935 ppd_attr_t
*ppd_attr
; /* PPD attribute */
3936 _cups_pwg_media_t
*pwgmedia
; /* Matching PWG size name */
3937 ipp_attribute_t
*attr
; /* Attribute data */
3938 ipp_t
*media_col_default
, /* media-col-default collection value */
3939 *media_size
; /* media-size collection value */
3940 ipp_value_t
*val
; /* Attribute value */
3941 int num_finishings
; /* Number of finishings */
3942 int finishings
[5]; /* finishings-supported values */
3943 static const char * const sides
[3] = /* sides-supported values */
3946 "two-sided-long-edge",
3947 "two-sided-short-edge"
3949 static const char * const standard_commands
[] =
3950 { /* Standard CUPS commands */
3958 * Check to see if the cache is up-to-date...
3961 snprintf(cache_name
, sizeof(cache_name
), "%s/%s.ipp", CacheDir
, p
->name
);
3962 if (stat(cache_name
, &cache_info
))
3963 cache_info
.st_mtime
= 0;
3965 snprintf(ppd_name
, sizeof(ppd_name
), "%s/ppd/%s.ppd", ServerRoot
, p
->name
);
3966 if (stat(ppd_name
, &ppd_info
))
3967 ppd_info
.st_mtime
= 1;
3969 ippDelete(p
->ppd_attrs
);
3970 p
->ppd_attrs
= ippNew();
3972 if (cache_info
.st_mtime
>= ppd_info
.st_mtime
&&
3973 (cache
= cupsFileOpen(cache_name
, "r")) != NULL
)
3976 * Load cached information and return...
3979 cupsdLogMessage(CUPSD_LOG_DEBUG
, "load_ppd: Loading %s...", cache_name
);
3981 if (ippReadIO(cache
, (ipp_iocb_t
)cupsFileRead
, 1, NULL
,
3982 p
->ppd_attrs
) == IPP_DATA
)
3984 cupsFileClose(cache
);
3988 cupsFileClose(cache
);
3992 * Reload PPD attributes from disk...
3995 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3997 cupsdLogMessage(CUPSD_LOG_DEBUG
, "load_ppd: Loading %s...", ppd_name
);
3999 delete_string_array(&(p
->filters
));
4000 delete_string_array(&(p
->pre_filters
));
4002 p
->type
&= ~CUPS_PRINTER_OPTIONS
;
4003 p
->type
|= CUPS_PRINTER_BW
;
4005 finishings
[0] = IPP_FINISHINGS_NONE
;
4008 if ((ppd
= ppdOpenFile(ppd_name
)) != NULL
)
4011 * Add make/model and other various attributes...
4014 ppdMarkDefaults(ppd
);
4016 if (ppd
->color_device
)
4017 p
->type
|= CUPS_PRINTER_COLOR
;
4018 if (ppd
->variable_sizes
)
4019 p
->type
|= CUPS_PRINTER_VARIABLE
;
4020 if (!ppd
->manual_copies
)
4021 p
->type
|= CUPS_PRINTER_COPIES
;
4022 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsFax", NULL
)) != NULL
)
4023 if (ppd_attr
->value
&& !strcasecmp(ppd_attr
->value
, "true"))
4024 p
->type
|= CUPS_PRINTER_FAX
;
4026 ippAddBoolean(p
->ppd_attrs
, IPP_TAG_PRINTER
, "color-supported",
4028 if (ppd
->throughput
)
4029 ippAddInteger(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4030 "pages-per-minute", ppd
->throughput
);
4035 * The NickName can be localized in the character set specified
4036 * by the LanugageEncoding attribute. However, ppdOpen2() has
4037 * already converted the ppd->nickname member to UTF-8 for us
4038 * (the original attribute value is available separately)
4041 cupsdSetString(&p
->make_model
, ppd
->nickname
);
4043 else if (ppd
->modelname
)
4046 * Model name can only contain specific characters...
4049 cupsdSetString(&p
->make_model
, ppd
->modelname
);
4052 cupsdSetString(&p
->make_model
, "Bad PPD File");
4054 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4055 "printer-make-and-model", NULL
, p
->make_model
);
4058 * Add media options from the PPD file...
4061 if (ppd
->num_sizes
== 0)
4063 cupsdLogMessage(CUPSD_LOG_CRIT
,
4064 "The PPD file for printer %s contains no media "
4065 "options and is therefore invalid!", p
->name
);
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 attr
= 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
;
4241 * Add a filter from application/vnd.cups-raw to printer/name to
4242 * handle "raw" printing by users.
4245 add_string_array(&(p
->filters
), "application/vnd.cups-raw 0 -");
4248 * Add any pre-filters in the PPD file...
4251 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsPreFilter", NULL
)) != NULL
)
4253 for (; ppd_attr
; ppd_attr
= ppdFindNextAttr(ppd
, "cupsPreFilter", NULL
))
4254 if (ppd_attr
->value
)
4255 add_string_array(&(p
->pre_filters
), ppd_attr
->value
);
4259 * Add any filters in the PPD file...
4262 DEBUG_printf(("ppd->num_filters = %d\n", ppd
->num_filters
));
4263 for (i
= 0; i
< ppd
->num_filters
; i
++)
4265 DEBUG_printf(("ppd->filters[%d] = \"%s\"\n", i
, ppd
->filters
[i
]));
4266 add_string_array(&(p
->filters
), ppd
->filters
[i
]);
4268 if (!strncasecmp(ppd
->filters
[i
], "application/vnd.cups-command", 28) &&
4269 isspace(ppd
->filters
[i
][28] & 255))
4270 p
->type
|= CUPS_PRINTER_COMMANDS
;
4273 if (ppd
->num_filters
== 0)
4276 * If there are no filters, add PostScript printing filters.
4279 add_string_array(&(p
->filters
),
4280 "application/vnd.cups-command 0 commandtops");
4281 add_string_array(&(p
->filters
),
4282 "application/vnd.cups-postscript 0 -");
4284 p
->type
|= CUPS_PRINTER_COMMANDS
;
4286 else if (!(p
->type
& CUPS_PRINTER_COMMANDS
))
4289 * See if this is a PostScript device without a command filter...
4292 for (i
= 0; i
< ppd
->num_filters
; i
++)
4293 if (!strncasecmp(ppd
->filters
[i
],
4294 "application/vnd.cups-postscript", 31) &&
4295 isspace(ppd
->filters
[i
][31] & 255))
4298 if (i
< ppd
->num_filters
)
4301 * Add the generic PostScript command filter...
4304 add_string_array(&(p
->filters
),
4305 "application/vnd.cups-command 0 commandtops");
4306 p
->type
|= CUPS_PRINTER_COMMANDS
;
4310 if (p
->type
& CUPS_PRINTER_COMMANDS
)
4312 char *commands
, /* Copy of commands */
4313 *start
, /* Start of name */
4314 *end
; /* End of name */
4315 int count
; /* Number of commands */
4318 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsCommands", NULL
)) != NULL
&&
4319 ppd_attr
->value
&& ppd_attr
->value
[0])
4321 for (count
= 0, start
= ppd_attr
->value
; *start
; count
++)
4323 while (isspace(*start
& 255))
4329 while (*start
&& !isspace(*start
& 255))
4339 * Make a copy of the commands string and count how many ...
4342 attr
= ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4343 "printer-commands", count
, NULL
, NULL
);
4345 commands
= strdup(ppd_attr
->value
);
4347 for (count
= 0, start
= commands
; *start
; count
++)
4349 while (isspace(*start
& 255))
4356 while (*end
&& !isspace(*end
& 255))
4362 attr
->values
[count
].string
.text
= _cupsStrAlloc(start
);
4372 * Add the standard list of commands...
4375 ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4377 (int)(sizeof(standard_commands
) /
4378 sizeof(standard_commands
[0])), NULL
,
4385 * No commands supported...
4388 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4389 "printer-commands", NULL
, "none");
4393 * Show current and available port monitors for this printer...
4396 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "port-monitor",
4397 NULL
, p
->port_monitor
? p
->port_monitor
: "none");
4399 for (i
= 1, ppd_attr
= ppdFindAttr(ppd
, "cupsPortMonitor", NULL
);
4401 i
++, ppd_attr
= ppdFindNextAttr(ppd
, "cupsPortMonitor", NULL
));
4405 if (strstr(ppd
->protocols
, "TBCP"))
4407 else if (strstr(ppd
->protocols
, "BCP"))
4411 attr
= ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
4412 "port-monitor-supported", i
, NULL
, NULL
);
4414 attr
->values
[0].string
.text
= _cupsStrAlloc("none");
4416 for (i
= 1, ppd_attr
= ppdFindAttr(ppd
, "cupsPortMonitor", NULL
);
4418 i
++, ppd_attr
= ppdFindNextAttr(ppd
, "cupsPortMonitor", NULL
))
4419 attr
->values
[i
].string
.text
= _cupsStrAlloc(ppd_attr
->value
);
4423 if (strstr(ppd
->protocols
, "TBCP"))
4424 attr
->values
[i
].string
.text
= _cupsStrAlloc("tbcp");
4425 else if (strstr(ppd
->protocols
, "BCP"))
4426 attr
->values
[i
].string
.text
= _cupsStrAlloc("bcp");
4430 cupsdSetString(&p
->product
, ppd
->product
);
4431 #endif /* HAVE_DNSSD */
4433 if (ppdFindAttr(ppd
, "APRemoteQueueID", NULL
))
4434 p
->type
|= CUPS_PRINTER_REMOTE
;
4437 * Close the PPD and set the type...
4442 else if (!access(ppd_name
, 0))
4444 int pline
; /* PPD line number */
4445 ppd_status_t pstatus
; /* PPD load status */
4448 pstatus
= ppdLastError(&pline
);
4450 cupsdLogMessage(CUPSD_LOG_ERROR
, "PPD file for %s cannot be loaded!",
4453 if (pstatus
<= PPD_ALLOC_ERROR
)
4454 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s", strerror(errno
));
4456 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s on line %d.",
4457 ppdErrorString(pstatus
), pline
);
4459 cupsdLogMessage(CUPSD_LOG_INFO
,
4460 "Hint: Run \"cupstestppd %s\" and fix any errors.",
4464 * Add a filter from application/vnd.cups-raw to printer/name to
4465 * handle "raw" printing by users.
4468 add_string_array(&(p
->filters
), "application/vnd.cups-raw 0 -");
4471 * Add a PostScript filter, since this is still possibly PS printer.
4474 add_string_array(&(p
->filters
), "application/vnd.cups-postscript 0 -");
4479 * If we have an interface script, add a filter entry for it...
4482 char interface
[1024]; /* Interface script */
4485 snprintf(interface
, sizeof(interface
), "%s/interfaces/%s", ServerRoot
,
4487 if (!access(interface
, X_OK
))
4490 * Yes, we have a System V style interface script; use it!
4493 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4494 "printer-make-and-model", NULL
,
4495 "Local System V Printer");
4497 snprintf(interface
, sizeof(interface
), "*/* 0 %s/interfaces/%s",
4498 ServerRoot
, p
->name
);
4499 add_string_array(&(p
->filters
), interface
);
4501 else if (!strncmp(p
->device_uri
, "ipp://", 6) &&
4502 (strstr(p
->device_uri
, "/printers/") != NULL
||
4503 strstr(p
->device_uri
, "/classes/") != NULL
||
4504 (strstr(p
->device_uri
, "._ipp.") != NULL
&&
4505 !strcmp(p
->device_uri
+ strlen(p
->device_uri
) - 5,
4509 * Tell the client this is really a hard-wired remote printer.
4512 p
->type
|= CUPS_PRINTER_REMOTE
;
4515 * Point the printer-uri-supported attribute to the
4519 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
4520 "printer-uri-supported", NULL
, p
->device_uri
);
4523 * Then set the make-and-model accordingly...
4526 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4527 "printer-make-and-model", NULL
, "Remote Printer");
4530 * Print all files directly...
4539 * Otherwise we have neither - treat this as a "dumb" printer
4540 * with no PPD file...
4543 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4544 "printer-make-and-model", NULL
, "Local Raw Printer");
4550 ippAddIntegers(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
4551 "finishings-supported", num_finishings
, finishings
);
4552 ippAddInteger(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
4553 "finishings-default", IPP_FINISHINGS_NONE
);
4555 if (ppd
&& (cache
= cupsFileOpen(cache_name
, "w")) != NULL
)
4558 * Save cached PPD attributes to disk...
4561 cupsdLogMessage(CUPSD_LOG_DEBUG
, "load_ppd: Saving %s...", cache_name
);
4563 p
->ppd_attrs
->state
= IPP_IDLE
;
4565 if (ippWriteIO(cache
, (ipp_iocb_t
)cupsFileWrite
, 1, NULL
,
4566 p
->ppd_attrs
) != IPP_DATA
)
4568 cupsdLogMessage(CUPSD_LOG_ERROR
,
4569 "Unable to save PPD cache file \"%s\" - %s", cache_name
,
4574 cupsFileClose(cache
);
4576 else if (cache_info
.st_mtime
)
4579 * Remove cache file...
4589 * 'write_irix_config()' - Update the config files used by the IRIX
4594 write_irix_config(cupsd_printer_t
*p
) /* I - Printer to update */
4596 char filename
[1024]; /* Interface script filename */
4597 cups_file_t
*fp
; /* Interface script file */
4598 ipp_attribute_t
*attr
; /* Attribute data */
4602 * Add dummy interface and GUI scripts to fool SGI's "challenged" printing
4603 * tools. First the interface script that tells the tools what kind of
4604 * printer we have...
4607 snprintf(filename
, sizeof(filename
), "/var/spool/lp/interface/%s", p
->name
);
4609 if (p
->type
& CUPS_PRINTER_CLASS
)
4611 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4613 cupsFilePuts(fp
, "#!/bin/sh\n");
4615 if ((attr
= ippFindAttribute(p
->attrs
, "printer-make-and-model",
4616 IPP_TAG_TEXT
)) != NULL
)
4617 cupsFilePrintf(fp
, "NAME=\"%s\"\n", attr
->values
[0].string
.text
);
4618 else if (p
->type
& CUPS_PRINTER_CLASS
)
4619 cupsFilePuts(fp
, "NAME=\"Printer Class\"\n");
4621 cupsFilePuts(fp
, "NAME=\"Remote Destination\"\n");
4623 if (p
->type
& CUPS_PRINTER_COLOR
)
4624 cupsFilePuts(fp
, "TYPE=ColorPostScript\n");
4626 cupsFilePuts(fp
, "TYPE=MonoPostScript\n");
4628 cupsFilePrintf(fp
, "HOSTNAME=%s\n", ServerName
);
4629 cupsFilePrintf(fp
, "HOSTPRINTER=%s\n", p
->name
);
4633 chmod(filename
, 0755);
4634 chown(filename
, User
, Group
);
4638 * Then the member file that tells which device file the queue is connected
4639 * to... Networked printers use "/dev/null" in this file, so that's what
4640 * we use (the actual device URI can confuse some apps...)
4643 snprintf(filename
, sizeof(filename
), "/var/spool/lp/member/%s", p
->name
);
4645 if (p
->type
& CUPS_PRINTER_CLASS
)
4647 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4649 cupsFilePuts(fp
, "/dev/null\n");
4653 chmod(filename
, 0644);
4654 chown(filename
, User
, Group
);
4658 * The gui_interface file is a script or program that launches a GUI
4659 * option panel for the printer, using options specified on the
4660 * command-line in the third argument. The option panel must send
4661 * any printing options to stdout on a single line when the user
4662 * accepts them, or nothing if the user cancels the dialog.
4664 * The default options panel program is /usr/bin/glpoptions, from
4665 * the ESP Print Pro software. You can select another using the
4666 * PrintcapGUI option.
4669 snprintf(filename
, sizeof(filename
), "/var/spool/lp/gui_interface/ELF/%s.gui", p
->name
);
4671 if (p
->type
& CUPS_PRINTER_CLASS
)
4673 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4675 cupsFilePuts(fp
, "#!/bin/sh\n");
4676 cupsFilePrintf(fp
, "%s -d %s -o \"$3\"\n", PrintcapGUI
, p
->name
);
4680 chmod(filename
, 0755);
4681 chown(filename
, User
, Group
);
4685 * The POD config file is needed by the printstatus command to show
4686 * the printer location and device.
4689 snprintf(filename
, sizeof(filename
), "/var/spool/lp/pod/%s.config", p
->name
);
4691 if (p
->type
& CUPS_PRINTER_CLASS
)
4693 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4695 cupsFilePrintf(fp
, "Printer Class | %s\n",
4696 (p
->type
& CUPS_PRINTER_COLOR
) ? "ColorPostScript" : "MonoPostScript");
4697 cupsFilePrintf(fp
, "Printer Model | %s\n", p
->make_model
? p
->make_model
: "");
4698 cupsFilePrintf(fp
, "Location Code | %s\n", p
->location
? p
->location
: "");
4699 cupsFilePrintf(fp
, "Physical Location | %s\n", p
->info
? p
->info
: "");
4700 cupsFilePrintf(fp
, "Port Path | %s\n", p
->device_uri
);
4701 cupsFilePrintf(fp
, "Config Path | /var/spool/lp/pod/%s.config\n", p
->name
);
4702 cupsFilePrintf(fp
, "Active Status Path | /var/spool/lp/pod/%s.status\n", p
->name
);
4703 cupsFilePuts(fp
, "Status Update Wait | 10 seconds\n");
4707 chmod(filename
, 0664);
4708 chown(filename
, User
, Group
);
4714 * 'write_irix_state()' - Update the status files used by IRIX printing
4719 write_irix_state(cupsd_printer_t
*p
) /* I - Printer to update */
4721 char filename
[1024]; /* Interface script filename */
4722 cups_file_t
*fp
; /* Interface script file */
4723 int tag
; /* Status tag value */
4729 * The POD status file is needed for the printstatus window to
4730 * provide the current status of the printer.
4733 snprintf(filename
, sizeof(filename
), "/var/spool/lp/pod/%s.status", p
->name
);
4735 if (p
->type
& CUPS_PRINTER_CLASS
)
4737 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4739 cupsFilePrintf(fp
, "Operational Status | %s\n",
4740 (p
->state
== IPP_PRINTER_IDLE
) ? "Idle" :
4741 (p
->state
== IPP_PRINTER_PROCESSING
) ? "Busy" :
4743 cupsFilePrintf(fp
, "Information | 01 00 00 | %s\n", CUPS_SVERSION
);
4744 cupsFilePrintf(fp
, "Information | 02 00 00 | Device URI: %s\n",
4746 cupsFilePrintf(fp
, "Information | 03 00 00 | %s jobs\n",
4747 p
->accepting
? "Accepting" : "Not accepting");
4748 cupsFilePrintf(fp
, "Information | 04 00 00 | %s\n", p
->state_message
);
4752 chmod(filename
, 0664);
4753 chown(filename
, User
, Group
);
4757 * The activeicons file is needed to provide desktop icons for printers:
4759 * [ quoted from /usr/lib/print/tagit ]
4761 * --- Type of printer tags (base values)
4763 * Dumb=66048 # 0x10200
4764 * DumbColor=66080 # 0x10220
4765 * Raster=66112 # 0x10240
4766 * ColorRaster=66144 # 0x10260
4767 * Plotter=66176 # 0x10280
4768 * PostScript=66208 # 0x102A0
4769 * ColorPostScript=66240 # 0x102C0
4770 * MonoPostScript=66272 # 0x102E0
4772 * --- Printer state modifiers for local printers
4777 * Unknown=3 # 0x3 (Faulted due to unknown reason)
4779 * --- Printer state modifiers for network printers
4783 * NetFaulted=10 # 0xA
4784 * NetUnknown=11 # 0xB (Faulted due to unknown reason)
4787 snprintf(filename
, sizeof(filename
), "/var/spool/lp/activeicons/%s", p
->name
);
4789 if (p
->type
& CUPS_PRINTER_CLASS
)
4791 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4793 if (p
->type
& CUPS_PRINTER_COLOR
)
4798 if (p
->type
& CUPS_PRINTER_REMOTE
)
4801 if (p
->state
== IPP_PRINTER_PROCESSING
)
4804 else if (p
->state
== IPP_PRINTER_STOPPED
)
4807 cupsFilePuts(fp
, "#!/bin/sh\n");
4808 cupsFilePrintf(fp
, "#Tag %d\n", tag
);
4812 chmod(filename
, 0755);
4813 chown(filename
, User
, Group
);
4818 * The default file is needed by the printers window to show
4819 * the default printer.
4822 snprintf(filename
, sizeof(filename
), "/var/spool/lp/default");
4824 if (DefaultPrinter
!= NULL
)
4826 if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4828 cupsFilePrintf(fp
, "%s\n", DefaultPrinter
->name
);
4832 chmod(filename
, 0644);
4833 chown(filename
, User
, Group
);
4843 * 'write_xml_string()' - Write a string with XML escaping.
4847 write_xml_string(cups_file_t
*fp
, /* I - File to write to */
4848 const char *s
) /* I - String to write */
4850 const char *start
; /* Start of current sequence */
4856 for (start
= s
; *s
; s
++)
4861 cupsFileWrite(fp
, start
, s
- start
);
4863 cupsFilePuts(fp
, "&");
4869 cupsFileWrite(fp
, start
, s
- start
);
4871 cupsFilePuts(fp
, "<");
4877 cupsFilePuts(fp
, start
);
4882 * End of "$Id: printers.c 7968 2008-09-19 23:03:01Z mike $".