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 delete_printer_filters(p
);
808 for (i
= 0; i
< p
->num_reasons
; i
++)
809 _cupsStrFree(p
->reasons
[i
]);
812 ippDelete(p
->ppd_attrs
);
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"))
1103 strcmp(value
, "com.apple.print.recoverable-warning") &&
1104 strcmp(value
, "connecting-to-device") &&
1105 strcmp(value
, "cups-insecure-filter-error") &&
1106 strcmp(value
, "cups-missing-filter-error"))
1108 for (i
= 0 ; i
< p
->num_reasons
; i
++)
1109 if (!strcmp(value
, p
->reasons
[i
]))
1112 if (i
>= p
->num_reasons
&&
1113 p
->num_reasons
< (int)(sizeof(p
->reasons
) / sizeof(p
->reasons
[0])))
1115 p
->reasons
[p
->num_reasons
] = _cupsStrAlloc(value
);
1120 cupsdLogMessage(CUPSD_LOG_ERROR
,
1121 "Syntax error on line %d of printers.conf.", linenum
);
1123 else if (!strcasecmp(line
, "State"))
1126 * Set the initial queue state...
1129 if (value
&& !strcasecmp(value
, "idle"))
1130 p
->state
= IPP_PRINTER_IDLE
;
1131 else if (value
&& !strcasecmp(value
, "stopped"))
1133 p
->state
= IPP_PRINTER_STOPPED
;
1134 cupsdSetPrinterReasons(p
, "+paused");
1137 cupsdLogMessage(CUPSD_LOG_ERROR
,
1138 "Syntax error on line %d of printers.conf.", linenum
);
1140 else if (!strcasecmp(line
, "StateMessage"))
1143 * Set the initial queue state message...
1147 strlcpy(p
->state_message
, value
, sizeof(p
->state_message
));
1149 else if (!strcasecmp(line
, "StateTime"))
1152 * Set the state time...
1156 p
->state_time
= atoi(value
);
1158 else if (!strcasecmp(line
, "Accepting"))
1161 * Set the initial accepting state...
1165 (!strcasecmp(value
, "yes") ||
1166 !strcasecmp(value
, "on") ||
1167 !strcasecmp(value
, "true")))
1170 (!strcasecmp(value
, "no") ||
1171 !strcasecmp(value
, "off") ||
1172 !strcasecmp(value
, "false")))
1175 cupsdLogMessage(CUPSD_LOG_ERROR
,
1176 "Syntax error on line %d of printers.conf.", linenum
);
1178 else if (!strcasecmp(line
, "Type"))
1181 p
->type
= atoi(value
);
1183 cupsdLogMessage(CUPSD_LOG_ERROR
,
1184 "Syntax error on line %d of printers.conf.", linenum
);
1186 else if (!strcasecmp(line
, "Product"))
1191 p
->product
= _cupsStrAlloc(value
);
1192 #endif /* HAVE_DNSSD */
1195 cupsdLogMessage(CUPSD_LOG_ERROR
,
1196 "Syntax error on line %d of printers.conf.", linenum
);
1198 else if (!strcasecmp(line
, "Filter"))
1203 p
->filters
= cupsArrayNew(NULL
, NULL
);
1205 cupsArrayAdd(p
->filters
, _cupsStrAlloc(value
));
1208 cupsdLogMessage(CUPSD_LOG_ERROR
,
1209 "Syntax error on line %d of printers.conf.", linenum
);
1211 else if (!strcasecmp(line
, "PreFilter"))
1215 if (!p
->pre_filters
)
1216 p
->pre_filters
= cupsArrayNew(NULL
, NULL
);
1218 cupsArrayAdd(p
->pre_filters
, _cupsStrAlloc(value
));
1221 cupsdLogMessage(CUPSD_LOG_ERROR
,
1222 "Syntax error on line %d of printers.conf.", linenum
);
1224 else if (!strcasecmp(line
, "Shared"))
1227 * Set the initial shared state...
1231 (!strcasecmp(value
, "yes") ||
1232 !strcasecmp(value
, "on") ||
1233 !strcasecmp(value
, "true")))
1236 (!strcasecmp(value
, "no") ||
1237 !strcasecmp(value
, "off") ||
1238 !strcasecmp(value
, "false")))
1241 cupsdLogMessage(CUPSD_LOG_ERROR
,
1242 "Syntax error on line %d of printers.conf.", linenum
);
1244 else if (!strcasecmp(line
, "JobSheets"))
1247 * Set the initial job sheets...
1252 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
1257 cupsdSetString(&p
->job_sheets
[0], value
);
1259 while (isspace(*valueptr
& 255))
1264 for (value
= valueptr
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
1269 cupsdSetString(&p
->job_sheets
[1], value
);
1273 cupsdLogMessage(CUPSD_LOG_ERROR
,
1274 "Syntax error on line %d of printers.conf.", linenum
);
1276 else if (!strcasecmp(line
, "AllowUser"))
1281 cupsdAddPrinterUser(p
, value
);
1284 cupsdLogMessage(CUPSD_LOG_ERROR
,
1285 "Syntax error on line %d of printers.conf.", linenum
);
1287 else if (!strcasecmp(line
, "DenyUser"))
1292 cupsdAddPrinterUser(p
, value
);
1295 cupsdLogMessage(CUPSD_LOG_ERROR
,
1296 "Syntax error on line %d of printers.conf.", linenum
);
1298 else if (!strcasecmp(line
, "QuotaPeriod"))
1301 p
->quota_period
= atoi(value
);
1303 cupsdLogMessage(CUPSD_LOG_ERROR
,
1304 "Syntax error on line %d of printers.conf.", linenum
);
1306 else if (!strcasecmp(line
, "PageLimit"))
1309 p
->page_limit
= atoi(value
);
1311 cupsdLogMessage(CUPSD_LOG_ERROR
,
1312 "Syntax error on line %d of printers.conf.", linenum
);
1314 else if (!strcasecmp(line
, "KLimit"))
1317 p
->k_limit
= atoi(value
);
1319 cupsdLogMessage(CUPSD_LOG_ERROR
,
1320 "Syntax error on line %d of printers.conf.", linenum
);
1322 else if (!strcasecmp(line
, "OpPolicy"))
1326 cupsd_policy_t
*pol
; /* Policy */
1329 if ((pol
= cupsdFindPolicy(value
)) != NULL
)
1331 cupsdSetString(&p
->op_policy
, value
);
1332 p
->op_policy_ptr
= pol
;
1335 cupsdLogMessage(CUPSD_LOG_ERROR
,
1336 "Bad policy \"%s\" on line %d of printers.conf",
1340 cupsdLogMessage(CUPSD_LOG_ERROR
,
1341 "Syntax error on line %d of printers.conf.", linenum
);
1343 else if (!strcasecmp(line
, "ErrorPolicy"))
1346 cupsdSetString(&p
->error_policy
, value
);
1348 cupsdLogMessage(CUPSD_LOG_ERROR
,
1349 "Syntax error on line %d of printers.conf.", linenum
);
1351 else if (!strcasecmp(line
, "Attribute") && value
)
1353 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
1356 cupsdLogMessage(CUPSD_LOG_ERROR
,
1357 "Syntax error on line %d of printers.conf.", linenum
);
1360 for (; *valueptr
&& isspace(*valueptr
& 255); *valueptr
++ = '\0');
1363 cupsdSetPrinterAttrs(p
);
1365 if (!strcmp(value
, "marker-change-time"))
1366 p
->marker_time
= atoi(valueptr
);
1368 cupsdSetPrinterAttr(p
, value
, valueptr
);
1374 * Something else we don't understand...
1377 cupsdLogMessage(CUPSD_LOG_ERROR
,
1378 "Unknown configuration directive %s on line %d of printers.conf.",
1388 * 'cupsdRenamePrinter()' - Rename a printer.
1393 cupsd_printer_t
*p
, /* I - Printer */
1394 const char *name
) /* I - New name */
1397 * Remove the printer from the array(s) first...
1400 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1401 "cupsdRenamePrinter: Removing %s from Printers", p
->name
);
1402 cupsArrayRemove(Printers
, p
);
1404 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
1406 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1407 "cupsdRenamePrinter: Removing %s from ImplicitPrinters",
1409 cupsArrayRemove(ImplicitPrinters
, p
);
1413 * Rename the printer type...
1416 mimeDeleteType(MimeDatabase
, p
->filetype
);
1417 p
->filetype
= mimeAddType(MimeDatabase
, "printer", name
);
1419 mimeDeleteType(MimeDatabase
, p
->prefiltertype
);
1420 p
->prefiltertype
= mimeAddType(MimeDatabase
, "prefilter", name
);
1423 * Rename the printer...
1426 cupsdSetString(&p
->name
, name
);
1429 * Reset printer attributes...
1432 cupsdSetPrinterAttrs(p
);
1435 * Add the printer back to the printer array(s)...
1438 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1439 "cupsdRenamePrinter: Adding %s to Printers", p
->name
);
1440 cupsArrayAdd(Printers
, p
);
1442 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
1444 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1445 "cupsdRenamePrinter: Adding %s to ImplicitPrinters",
1447 cupsArrayAdd(ImplicitPrinters
, p
);
1453 * 'cupsdSaveAllPrinters()' - Save all printer definitions to the printers.conf
1458 cupsdSaveAllPrinters(void)
1460 int i
; /* Looping var */
1461 cups_file_t
*fp
; /* printers.conf file */
1462 char temp
[1024], /* Temporary string */
1463 backup
[1024], /* printers.conf.O file */
1464 value
[2048], /* Value string */
1465 *ptr
; /* Pointer into value */
1466 cupsd_printer_t
*printer
; /* Current printer class */
1467 time_t curtime
; /* Current time */
1468 struct tm
*curdate
; /* Current date */
1469 cups_option_t
*option
; /* Current option */
1470 ipp_attribute_t
*marker
; /* Current marker attribute */
1474 * Create the printers.conf file...
1477 snprintf(temp
, sizeof(temp
), "%s/printers.conf", ServerRoot
);
1478 snprintf(backup
, sizeof(backup
), "%s/printers.conf.O", ServerRoot
);
1480 if (rename(temp
, backup
))
1482 if (errno
!= ENOENT
)
1483 cupsdLogMessage(CUPSD_LOG_ERROR
,
1484 "Unable to backup printers.conf - %s", strerror(errno
));
1487 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
1489 cupsdLogMessage(CUPSD_LOG_ERROR
,
1490 "Unable to save printers.conf - %s", strerror(errno
));
1492 if (rename(backup
, temp
))
1493 cupsdLogMessage(CUPSD_LOG_ERROR
,
1494 "Unable to restore printers.conf - %s", strerror(errno
));
1498 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving printers.conf...");
1501 * Restrict access to the file...
1504 fchown(cupsFileNumber(fp
), getuid(), Group
);
1505 fchmod(cupsFileNumber(fp
), 0600);
1508 * Write a small header to the file...
1511 curtime
= time(NULL
);
1512 curdate
= localtime(&curtime
);
1513 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
1515 cupsFilePuts(fp
, "# Printer configuration file for " CUPS_SVERSION
"\n");
1516 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
1519 * Write each local printer known to the system...
1522 for (printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1524 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1527 * Skip remote destinations and printer classes...
1530 if ((printer
->type
& CUPS_PRINTER_DISCOVERED
) ||
1531 (printer
->type
& CUPS_PRINTER_CLASS
) ||
1532 (printer
->type
& CUPS_PRINTER_IMPLICIT
))
1536 * Write printers as needed...
1539 if (printer
== DefaultPrinter
)
1540 cupsFilePrintf(fp
, "<DefaultPrinter %s>\n", printer
->name
);
1542 cupsFilePrintf(fp
, "<Printer %s>\n", printer
->name
);
1544 if (printer
->num_auth_info_required
> 0)
1546 switch (printer
->num_auth_info_required
)
1549 strlcpy(value
, printer
->auth_info_required
[0], sizeof(value
));
1553 snprintf(value
, sizeof(value
), "%s,%s",
1554 printer
->auth_info_required
[0],
1555 printer
->auth_info_required
[1]);
1560 snprintf(value
, sizeof(value
), "%s,%s,%s",
1561 printer
->auth_info_required
[0],
1562 printer
->auth_info_required
[1],
1563 printer
->auth_info_required
[2]);
1567 cupsFilePutConf(fp
, "AuthInfoRequired", value
);
1571 cupsFilePutConf(fp
, "Info", printer
->info
);
1573 if (printer
->location
)
1574 cupsFilePutConf(fp
, "Location", printer
->location
);
1576 if (printer
->make_model
)
1577 cupsFilePutConf(fp
, "MakeModel", printer
->make_model
);
1579 cupsFilePutConf(fp
, "DeviceURI", printer
->device_uri
);
1581 if (printer
->port_monitor
)
1582 cupsFilePutConf(fp
, "PortMonitor", printer
->port_monitor
);
1584 if (printer
->state
== IPP_PRINTER_STOPPED
)
1586 cupsFilePuts(fp
, "State Stopped\n");
1588 if (printer
->state_message
)
1589 cupsFilePutConf(fp
, "StateMessage", printer
->state_message
);
1592 cupsFilePuts(fp
, "State Idle\n");
1594 cupsFilePrintf(fp
, "StateTime %d\n", (int)printer
->state_time
);
1596 for (i
= 0; i
< printer
->num_reasons
; i
++)
1597 if (strcmp(printer
->reasons
[i
], "com.apple.print.recoverable-warning") &&
1598 strcmp(printer
->reasons
[i
], "connecting-to-device") &&
1599 strcmp(printer
->reasons
[i
], "cups-insecure-filter-error") &&
1600 strcmp(printer
->reasons
[i
], "cups-missing-filter-error"))
1601 cupsFilePutConf(fp
, "Reason", printer
->reasons
[i
]);
1603 cupsFilePrintf(fp
, "Type %d\n", printer
->type
);
1606 if (printer
->product
)
1607 cupsFilePutConf(fp
, "Product", printer
->product
);
1608 #endif /* HAVE_DNSSD */
1610 for (ptr
= (char *)cupsArrayFirst(printer
->filters
);
1612 ptr
= (char *)cupsArrayNext(printer
->filters
))
1613 cupsFilePutConf(fp
, "Filter", ptr
);
1615 for (ptr
= (char *)cupsArrayFirst(printer
->pre_filters
);
1617 ptr
= (char *)cupsArrayNext(printer
->pre_filters
))
1618 cupsFilePutConf(fp
, "PreFilter", ptr
);
1620 if (printer
->accepting
)
1621 cupsFilePuts(fp
, "Accepting Yes\n");
1623 cupsFilePuts(fp
, "Accepting No\n");
1625 if (printer
->shared
)
1626 cupsFilePuts(fp
, "Shared Yes\n");
1628 cupsFilePuts(fp
, "Shared No\n");
1630 snprintf(value
, sizeof(value
), "%s %s", printer
->job_sheets
[0],
1631 printer
->job_sheets
[1]);
1632 cupsFilePutConf(fp
, "JobSheets", value
);
1634 cupsFilePrintf(fp
, "QuotaPeriod %d\n", printer
->quota_period
);
1635 cupsFilePrintf(fp
, "PageLimit %d\n", printer
->page_limit
);
1636 cupsFilePrintf(fp
, "KLimit %d\n", printer
->k_limit
);
1638 for (i
= 0; i
< printer
->num_users
; i
++)
1639 cupsFilePutConf(fp
, printer
->deny_users
? "DenyUser" : "AllowUser",
1642 if (printer
->op_policy
)
1643 cupsFilePutConf(fp
, "OpPolicy", printer
->op_policy
);
1644 if (printer
->error_policy
)
1645 cupsFilePutConf(fp
, "ErrorPolicy", printer
->error_policy
);
1647 for (i
= printer
->num_options
, option
= printer
->options
;
1651 snprintf(value
, sizeof(value
), "%s %s", option
->name
, option
->value
);
1652 cupsFilePutConf(fp
, "Option", value
);
1655 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-colors",
1656 IPP_TAG_NAME
)) != NULL
)
1658 snprintf(value
, sizeof(value
), "%s ", marker
->name
);
1660 for (i
= 0, ptr
= value
+ strlen(value
);
1661 i
< marker
->num_values
&& ptr
< (value
+ sizeof(value
) - 1);
1667 strlcpy(ptr
, marker
->values
[i
].string
.text
,
1668 value
+ sizeof(value
) - ptr
);
1673 cupsFilePutConf(fp
, "Attribute", value
);
1676 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-levels",
1677 IPP_TAG_INTEGER
)) != NULL
)
1679 cupsFilePrintf(fp
, "Attribute %s %d", marker
->name
,
1680 marker
->values
[0].integer
);
1681 for (i
= 1; i
< marker
->num_values
; i
++)
1682 cupsFilePrintf(fp
, ",%d", marker
->values
[i
].integer
);
1683 cupsFilePuts(fp
, "\n");
1686 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-low-levels",
1687 IPP_TAG_INTEGER
)) != NULL
)
1689 cupsFilePrintf(fp
, "Attribute %s %d", marker
->name
,
1690 marker
->values
[0].integer
);
1691 for (i
= 1; i
< marker
->num_values
; i
++)
1692 cupsFilePrintf(fp
, ",%d", marker
->values
[i
].integer
);
1693 cupsFilePuts(fp
, "\n");
1696 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-high-levels",
1697 IPP_TAG_INTEGER
)) != NULL
)
1699 cupsFilePrintf(fp
, "Attribute %s %d", marker
->name
,
1700 marker
->values
[0].integer
);
1701 for (i
= 1; i
< marker
->num_values
; i
++)
1702 cupsFilePrintf(fp
, ",%d", marker
->values
[i
].integer
);
1703 cupsFilePuts(fp
, "\n");
1706 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-message",
1707 IPP_TAG_TEXT
)) != NULL
)
1709 snprintf(value
, sizeof(value
), "%s %s", marker
->name
,
1710 marker
->values
[0].string
.text
);
1712 cupsFilePutConf(fp
, "Attribute", value
);
1715 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-names",
1716 IPP_TAG_NAME
)) != NULL
)
1718 snprintf(value
, sizeof(value
), "%s ", marker
->name
);
1720 for (i
= 0, ptr
= value
+ strlen(value
);
1721 i
< marker
->num_values
&& ptr
< (value
+ sizeof(value
) - 1);
1727 strlcpy(ptr
, marker
->values
[i
].string
.text
,
1728 value
+ sizeof(value
) - ptr
);
1733 cupsFilePutConf(fp
, "Attribute", value
);
1736 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-types",
1737 IPP_TAG_KEYWORD
)) != NULL
)
1739 snprintf(value
, sizeof(value
), "%s ", marker
->name
);
1741 for (i
= 0, ptr
= value
+ strlen(value
);
1742 i
< marker
->num_values
&& ptr
< (value
+ sizeof(value
) - 1);
1748 strlcpy(ptr
, marker
->values
[i
].string
.text
,
1749 value
+ sizeof(value
) - ptr
);
1754 cupsFilePutConf(fp
, "Attribute", value
);
1757 if (printer
->marker_time
)
1758 cupsFilePrintf(fp
, "Attribute marker-change-time %ld\n",
1759 (long)printer
->marker_time
);
1761 cupsFilePuts(fp
, "</Printer>\n");
1765 * Make IRIX desktop & printer status happy
1768 write_irix_state(printer
);
1777 * 'cupsdSetAuthInfoRequired()' - Set the required authentication info.
1780 int /* O - 1 if value OK, 0 otherwise */
1781 cupsdSetAuthInfoRequired(
1782 cupsd_printer_t
*p
, /* I - Printer */
1783 const char *values
, /* I - Plain text value (or NULL) */
1784 ipp_attribute_t
*attr
) /* I - IPP attribute value (or NULL) */
1786 int i
; /* Looping var */
1789 p
->num_auth_info_required
= 0;
1792 * Do we have a plain text value?
1798 * Yes, grab the keywords...
1801 const char *end
; /* End of current value */
1804 while (*values
&& p
->num_auth_info_required
< 4)
1806 if ((end
= strchr(values
, ',')) == NULL
)
1807 end
= values
+ strlen(values
);
1809 if ((end
- values
) == 4 && !strncmp(values
, "none", 4))
1811 if (p
->num_auth_info_required
!= 0 || *end
)
1814 p
->auth_info_required
[p
->num_auth_info_required
] = "none";
1815 p
->num_auth_info_required
++;
1819 else if ((end
- values
) == 9 && !strncmp(values
, "negotiate", 9))
1821 if (p
->num_auth_info_required
!= 0 || *end
)
1824 p
->auth_info_required
[p
->num_auth_info_required
] = "negotiate";
1825 p
->num_auth_info_required
++;
1827 else if ((end
- values
) == 6 && !strncmp(values
, "domain", 6))
1829 p
->auth_info_required
[p
->num_auth_info_required
] = "domain";
1830 p
->num_auth_info_required
++;
1832 else if ((end
- values
) == 8 && !strncmp(values
, "password", 8))
1834 p
->auth_info_required
[p
->num_auth_info_required
] = "password";
1835 p
->num_auth_info_required
++;
1837 else if ((end
- values
) == 8 && !strncmp(values
, "username", 8))
1839 p
->auth_info_required
[p
->num_auth_info_required
] = "username";
1840 p
->num_auth_info_required
++;
1845 values
= (*end
) ? end
+ 1 : end
;
1848 if (p
->num_auth_info_required
== 0)
1850 p
->auth_info_required
[0] = "none";
1851 p
->num_auth_info_required
= 1;
1855 * Update the printer-type value as needed...
1858 if (p
->num_auth_info_required
> 1 ||
1859 strcmp(p
->auth_info_required
[0], "none"))
1860 p
->type
|= CUPS_PRINTER_AUTHENTICATED
;
1862 p
->type
&= ~CUPS_PRINTER_AUTHENTICATED
;
1868 * Grab values from an attribute instead...
1871 if (!attr
|| attr
->num_values
> 4)
1875 * Update the printer-type value as needed...
1878 if (attr
->num_values
> 1 ||
1879 strcmp(attr
->values
[0].string
.text
, "none"))
1880 p
->type
|= CUPS_PRINTER_AUTHENTICATED
;
1882 p
->type
&= ~CUPS_PRINTER_AUTHENTICATED
;
1884 for (i
= 0; i
< attr
->num_values
; i
++)
1886 if (!strcmp(attr
->values
[i
].string
.text
, "none"))
1888 if (p
->num_auth_info_required
!= 0 || attr
->num_values
!= 1)
1891 p
->auth_info_required
[p
->num_auth_info_required
] = "none";
1892 p
->num_auth_info_required
++;
1896 else if (!strcmp(attr
->values
[i
].string
.text
, "negotiate"))
1898 if (p
->num_auth_info_required
!= 0 || attr
->num_values
!= 1)
1901 p
->auth_info_required
[p
->num_auth_info_required
] = "negotiate";
1902 p
->num_auth_info_required
++;
1906 else if (!strcmp(attr
->values
[i
].string
.text
, "domain"))
1908 p
->auth_info_required
[p
->num_auth_info_required
] = "domain";
1909 p
->num_auth_info_required
++;
1911 else if (!strcmp(attr
->values
[i
].string
.text
, "password"))
1913 p
->auth_info_required
[p
->num_auth_info_required
] = "password";
1914 p
->num_auth_info_required
++;
1916 else if (!strcmp(attr
->values
[i
].string
.text
, "username"))
1918 p
->auth_info_required
[p
->num_auth_info_required
] = "username";
1919 p
->num_auth_info_required
++;
1930 * 'cupsdSetDeviceURI()' - Set the device URI for a printer.
1934 cupsdSetDeviceURI(cupsd_printer_t
*p
, /* I - Printer */
1935 const char *uri
) /* I - Device URI */
1937 char buffer
[1024], /* URI buffer */
1938 *start
, /* Start of data after scheme */
1939 *slash
, /* First slash after scheme:// */
1940 *ptr
; /* Pointer into user@host:port part */
1944 * Set the full device URI..
1947 cupsdSetString(&(p
->device_uri
), uri
);
1950 * Copy the device URI to a temporary buffer so we can sanitize any auth
1954 strlcpy(buffer
, uri
, sizeof(buffer
));
1957 * Find the end of the scheme:// part...
1960 if ((ptr
= strchr(buffer
, ':')) != NULL
)
1962 for (start
= ptr
+ 1; *start
; start
++)
1967 * Find the next slash (/) in the URI...
1970 if ((slash
= strchr(start
, '/')) == NULL
)
1971 slash
= start
+ strlen(start
); /* No slash, point to the end */
1974 * Check for an @ sign before the slash...
1977 if ((ptr
= strchr(start
, '@')) != NULL
&& ptr
< slash
)
1980 * Found an @ sign and it is before the resource part, so we have
1981 * an authentication string. Copy the remaining URI over the
1982 * authentication string...
1985 _cups_strcpy(start
, ptr
+ 1);
1990 * Save the sanitized URI...
1993 cupsdSetString(&(p
->sanitized_device_uri
), buffer
);
1998 * 'cupsdSetPrinterAttr()' - Set a printer attribute.
2002 cupsdSetPrinterAttr(
2003 cupsd_printer_t
*p
, /* I - Printer */
2004 const char *name
, /* I - Attribute name */
2005 char *value
) /* I - Attribute value string */
2007 ipp_attribute_t
*attr
; /* Attribute */
2008 int i
, /* Looping var */
2009 count
; /* Number of values */
2010 char *ptr
; /* Pointer into value */
2011 ipp_tag_t value_tag
; /* Value tag for this attribute */
2015 * Don't allow empty values...
2020 cupsdLogMessage(CUPSD_LOG_ERROR
, "Ignoring empty \"%s\" attribute", name
);
2025 * Count the number of values...
2028 for (count
= 1, ptr
= value
;
2029 (ptr
= strchr(ptr
, ',')) != NULL
;
2033 * Then add or update the attribute as needed...
2036 if (!strcmp(name
, "marker-levels") || !strcmp(name
, "marker-low-levels") ||
2037 !strcmp(name
, "marker-high-levels"))
2043 if ((attr
= ippFindAttribute(p
->attrs
, name
, IPP_TAG_INTEGER
)) != NULL
&&
2044 attr
->num_values
< count
)
2046 ippDeleteAttribute(p
->attrs
, attr
);
2051 attr
->num_values
= count
;
2053 attr
= ippAddIntegers(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, name
,
2058 cupsdLogMessage(CUPSD_LOG_ERROR
,
2059 "Unable to allocate memory for printer attribute "
2060 "(%d values)", count
);
2064 for (i
= 0; i
< count
; i
++)
2066 if ((ptr
= strchr(value
, ',')) != NULL
)
2069 attr
->values
[i
].integer
= strtol(value
, NULL
, 10);
2078 * Name or keyword values...
2081 if (!strcmp(name
, "marker-types"))
2082 value_tag
= IPP_TAG_KEYWORD
;
2083 else if (!strcmp(name
, "marker-message"))
2084 value_tag
= IPP_TAG_TEXT
;
2086 value_tag
= IPP_TAG_NAME
;
2088 if ((attr
= ippFindAttribute(p
->attrs
, name
, value_tag
)) != NULL
&&
2089 attr
->num_values
< count
)
2091 ippDeleteAttribute(p
->attrs
, attr
);
2097 for (i
= 0; i
< attr
->num_values
; i
++)
2098 _cupsStrFree(attr
->values
[i
].string
.text
);
2100 attr
->num_values
= count
;
2103 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, value_tag
, name
,
2108 cupsdLogMessage(CUPSD_LOG_ERROR
,
2109 "Unable to allocate memory for printer attribute "
2110 "(%d values)", count
);
2114 for (i
= 0; i
< count
; i
++)
2116 if ((ptr
= strchr(value
, ',')) != NULL
)
2119 attr
->values
[i
].string
.text
= _cupsStrAlloc(value
);
2129 * 'cupsdSetPrinterAttrs()' - Set printer attributes based upon the PPD file.
2133 cupsdSetPrinterAttrs(cupsd_printer_t
*p
)/* I - Printer to setup */
2135 int i
, /* Looping var */
2136 length
; /* Length of browse attributes */
2137 char resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2138 int num_air
; /* Number of auth-info-required values */
2139 const char * const *air
; /* auth-info-required values */
2140 cupsd_location_t
*auth
; /* Pointer to authentication element */
2141 const char *auth_supported
; /* Authentication supported */
2142 ipp_t
*oldattrs
; /* Old printer attributes */
2143 ipp_attribute_t
*attr
; /* Attribute data */
2144 cups_option_t
*option
; /* Current printer option */
2145 char *filter
; /* Current filter */
2146 static const char * const air_userpass
[] =
2147 { /* Basic/Digest authentication */
2152 static const char * const air_negotiate
[] =
2153 { /* Kerberos authentication */
2156 #endif /* HAVE_GSSAPI */
2157 static const char * const air_none
[] =
2158 { /* No authentication */
2163 DEBUG_printf(("cupsdSetPrinterAttrs: entering name = %s, type = %x\n", p
->name
,
2167 * Make sure that we have the common attributes defined...
2171 cupsdCreateCommonData();
2174 * Clear out old filters, if any...
2177 delete_printer_filters(p
);
2180 * Figure out the authentication that is required for the printer.
2183 auth_supported
= "requesting-user-name";
2187 if (p
->num_auth_info_required
> 0 && strcmp(p
->auth_info_required
[0], "none"))
2189 num_air
= p
->num_auth_info_required
;
2190 air
= p
->auth_info_required
;
2192 if (!strcmp(air
[0], "username"))
2193 auth_supported
= "basic";
2195 auth_supported
= "negotiate";
2197 else if (!(p
->type
& CUPS_PRINTER_DISCOVERED
))
2199 if (p
->type
& CUPS_PRINTER_CLASS
)
2200 snprintf(resource
, sizeof(resource
), "/classes/%s", p
->name
);
2202 snprintf(resource
, sizeof(resource
), "/printers/%s", p
->name
);
2204 if ((auth
= cupsdFindBest(resource
, HTTP_POST
)) == NULL
||
2205 auth
->type
== CUPSD_AUTH_NONE
)
2206 auth
= cupsdFindPolicyOp(p
->op_policy_ptr
, IPP_PRINT_JOB
);
2210 int auth_type
; /* Authentication type */
2213 if ((auth_type
= auth
->type
) == CUPSD_AUTH_DEFAULT
)
2214 auth_type
= DefaultAuthType
;
2216 if (auth_type
== CUPSD_AUTH_BASIC
|| auth_type
== CUPSD_AUTH_BASICDIGEST
)
2218 auth_supported
= "basic";
2222 else if (auth_type
== CUPSD_AUTH_DIGEST
)
2224 auth_supported
= "digest";
2229 else if (auth_type
== CUPSD_AUTH_NEGOTIATE
)
2231 auth_supported
= "negotiate";
2233 air
= air_negotiate
;
2235 #endif /* HAVE_GSSAPI */
2237 if (auth_type
!= CUPSD_AUTH_NONE
)
2238 p
->type
|= CUPS_PRINTER_AUTHENTICATED
;
2240 p
->type
&= ~CUPS_PRINTER_AUTHENTICATED
;
2243 p
->type
&= ~CUPS_PRINTER_AUTHENTICATED
;
2245 else if (p
->type
& CUPS_PRINTER_AUTHENTICATED
)
2252 * Create the required IPP attributes for a printer...
2255 oldattrs
= p
->attrs
;
2256 p
->attrs
= ippNew();
2258 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2259 "uri-authentication-supported", NULL
, auth_supported
);
2260 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2261 "uri-security-supported", NULL
, "none");
2262 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "printer-name", NULL
,
2264 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-location",
2265 NULL
, p
->location
? p
->location
: "");
2266 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-info",
2267 NULL
, p
->info
? p
->info
: "");
2272 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2273 "requesting-user-name-denied", p
->num_users
, NULL
,
2276 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2277 "requesting-user-name-allowed", p
->num_users
, NULL
,
2281 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2282 "job-quota-period", p
->quota_period
);
2283 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2284 "job-k-limit", p
->k_limit
);
2285 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2286 "job-page-limit", p
->page_limit
);
2287 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2288 "auth-info-required", num_air
, NULL
, air
);
2290 if (cupsArrayCount(Banners
) > 0 && !(p
->type
& CUPS_PRINTER_DISCOVERED
))
2293 * Setup the job-sheets-default attribute...
2296 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2297 "job-sheets-default", 2, NULL
, NULL
);
2301 attr
->values
[0].string
.text
= _cupsStrAlloc(Classification
?
2302 Classification
: p
->job_sheets
[0]);
2303 attr
->values
[1].string
.text
= _cupsStrAlloc(Classification
?
2304 Classification
: p
->job_sheets
[1]);
2311 if (p
->type
& CUPS_PRINTER_DISCOVERED
)
2314 * Tell the client this is a remote printer of some type...
2317 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
2318 "printer-uri-supported", NULL
, p
->uri
);
2320 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "printer-more-info",
2324 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
2325 "printer-make-and-model", NULL
, p
->make_model
);
2327 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri", NULL
,
2336 * Assign additional attributes depending on whether this is a printer
2340 if (p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
2343 p
->type
&= ~CUPS_PRINTER_OPTIONS
;
2346 * Add class-specific attributes...
2349 if ((p
->type
& CUPS_PRINTER_IMPLICIT
) && p
->num_printers
> 0 &&
2350 p
->printers
[0]->make_model
)
2351 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
2352 "printer-make-and-model", NULL
, p
->printers
[0]->make_model
);
2354 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
2355 "printer-make-and-model", NULL
, "Local Printer Class");
2357 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri", NULL
,
2358 "file:///dev/null");
2360 if (p
->num_printers
> 0)
2363 * Add a list of member names; URIs are added in copy_printer_attrs...
2366 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2367 "member-names", p
->num_printers
, NULL
, NULL
);
2368 p
->type
|= CUPS_PRINTER_OPTIONS
;
2370 for (i
= 0; i
< p
->num_printers
; i
++)
2373 attr
->values
[i
].string
.text
= _cupsStrRetain(p
->printers
[i
]->name
);
2375 p
->type
&= ~CUPS_PRINTER_OPTIONS
| p
->printers
[i
]->type
;
2382 * Add printer-specific attributes...
2385 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri", NULL
,
2386 p
->sanitized_device_uri
);
2389 * Assign additional attributes from the PPD file (if any)...
2395 * Add filters for printer...
2398 cupsdSetPrinterReasons(p
, "-cups-missing-filter-error");
2400 for (filter
= (char *)cupsArrayFirst(p
->filters
);
2402 filter
= (char *)cupsArrayNext(p
->filters
))
2403 add_printer_filter(p
, p
->filetype
, filter
);
2407 p
->prefiltertype
= mimeAddType(MimeDatabase
, "prefilter", p
->name
);
2409 for (filter
= (char *)cupsArrayFirst(p
->pre_filters
);
2411 filter
= (char *)cupsArrayNext(p
->pre_filters
))
2412 add_printer_filter(p
, p
->prefiltertype
, filter
);
2418 * Copy marker attributes as needed...
2423 ipp_attribute_t
*oldattr
; /* Old attribute */
2426 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-colors",
2427 IPP_TAG_NAME
)) != NULL
)
2429 if ((attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2430 "marker-colors", oldattr
->num_values
, NULL
,
2433 for (i
= 0; i
< oldattr
->num_values
; i
++)
2434 attr
->values
[i
].string
.text
=
2435 _cupsStrRetain(oldattr
->values
[i
].string
.text
);
2439 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-levels",
2440 IPP_TAG_INTEGER
)) != NULL
)
2442 if ((attr
= ippAddIntegers(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2443 "marker-levels", oldattr
->num_values
,
2446 for (i
= 0; i
< oldattr
->num_values
; i
++)
2447 attr
->values
[i
].integer
= oldattr
->values
[i
].integer
;
2451 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-low-levels",
2452 IPP_TAG_INTEGER
)) != NULL
)
2454 if ((attr
= ippAddIntegers(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2455 "marker-low-levels", oldattr
->num_values
,
2458 for (i
= 0; i
< oldattr
->num_values
; i
++)
2459 attr
->values
[i
].integer
= oldattr
->values
[i
].integer
;
2463 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-high-levels",
2464 IPP_TAG_INTEGER
)) != NULL
)
2466 if ((attr
= ippAddIntegers(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2467 "marker-high-levels", oldattr
->num_values
,
2470 for (i
= 0; i
< oldattr
->num_values
; i
++)
2471 attr
->values
[i
].integer
= oldattr
->values
[i
].integer
;
2475 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-names",
2476 IPP_TAG_NAME
)) != NULL
)
2478 if ((attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2479 "marker-names", oldattr
->num_values
, NULL
,
2482 for (i
= 0; i
< oldattr
->num_values
; i
++)
2483 attr
->values
[i
].string
.text
=
2484 _cupsStrRetain(oldattr
->values
[i
].string
.text
);
2488 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-types",
2489 IPP_TAG_KEYWORD
)) != NULL
)
2491 if ((attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2492 "marker-types", oldattr
->num_values
, NULL
,
2495 for (i
= 0; i
< oldattr
->num_values
; i
++)
2496 attr
->values
[i
].string
.text
=
2497 _cupsStrRetain(oldattr
->values
[i
].string
.text
);
2501 ippDelete(oldattrs
);
2505 * Force sharing off for remote queues...
2508 if (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
))
2513 * Copy the printer options into a browse attributes string we can re-use.
2516 const char *valptr
; /* Pointer into value */
2517 char *attrptr
; /* Pointer into attribute string */
2521 * Free the old browse attributes as needed...
2524 if (p
->browse_attrs
)
2525 free(p
->browse_attrs
);
2528 * Compute the length of all attributes + job-sheets, lease-duration,
2529 * and BrowseLocalOptions.
2532 for (length
= 1, i
= p
->num_options
, option
= p
->options
;
2536 length
+= strlen(option
->name
) + 2;
2540 for (valptr
= option
->value
; *valptr
; valptr
++)
2541 if (strchr(" \"\'\\", *valptr
))
2548 length
+= 13 + strlen(p
->job_sheets
[0]) + strlen(p
->job_sheets
[1]);
2550 if (BrowseLocalOptions
)
2551 length
+= 12 + strlen(BrowseLocalOptions
);
2553 if (p
->num_auth_info_required
> 0)
2555 length
+= 18; /* auth-info-required */
2557 for (i
= 0; i
< p
->num_auth_info_required
; i
++)
2558 length
+= strlen(p
->auth_info_required
[i
]) + 1;
2562 * Allocate the new string...
2565 if ((p
->browse_attrs
= calloc(1, length
)) == NULL
)
2566 cupsdLogMessage(CUPSD_LOG_ERROR
,
2567 "Unable to allocate %d bytes for browse data!",
2572 * Got the allocated string, now copy the options and attributes over...
2575 sprintf(p
->browse_attrs
, "job-sheets=%s,%s lease-duration=%d",
2576 p
->job_sheets
[0], p
->job_sheets
[1], BrowseTimeout
);
2577 attrptr
= p
->browse_attrs
+ strlen(p
->browse_attrs
);
2579 if (BrowseLocalOptions
)
2581 sprintf(attrptr
, " ipp-options=%s", BrowseLocalOptions
);
2582 attrptr
+= strlen(attrptr
);
2585 for (i
= p
->num_options
, option
= p
->options
;
2590 strcpy(attrptr
, option
->name
);
2591 attrptr
+= strlen(attrptr
);
2597 for (valptr
= option
->value
; *valptr
; valptr
++)
2599 if (strchr(" \"\'\\", *valptr
))
2602 *attrptr
++ = *valptr
;
2607 if (p
->num_auth_info_required
> 0)
2609 strcpy(attrptr
, "auth-info-required");
2612 for (i
= 0; i
< p
->num_auth_info_required
; i
++)
2614 *attrptr
++ = i
? ',' : '=';
2615 strcpy(attrptr
, p
->auth_info_required
[i
]);
2616 attrptr
+= strlen(attrptr
);
2625 * Populate the document-format-supported attribute...
2628 add_printer_formats(p
);
2630 DEBUG_printf(("cupsdSetPrinterAttrs: leaving name = %s, type = %x\n", p
->name
,
2634 * Add name-default attributes...
2637 add_printer_defaults(p
);
2641 * Write the IRIX printer config and status files...
2644 write_irix_config(p
);
2645 write_irix_state(p
);
2649 * Let the browse protocols reflect the change
2652 cupsdRegisterPrinter(p
);
2657 * 'cupsdSetPrinterReasons()' - Set/update the reasons strings.
2661 cupsdSetPrinterReasons(
2662 cupsd_printer_t
*p
, /* I - Printer */
2663 const char *s
) /* I - Reasons strings */
2665 int i
; /* Looping var */
2666 const char *sptr
; /* Pointer into reasons */
2667 char reason
[255], /* Reason string */
2668 *rptr
; /* Pointer into reason */
2671 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2672 "cupsdSetPrinterReasons(p=%p(%s),s=\"%s\"", p
, p
->name
, s
);
2674 if (s
[0] == '-' || s
[0] == '+')
2677 * Add/remove reasons...
2685 * Replace reasons...
2690 for (i
= 0; i
< p
->num_reasons
; i
++)
2691 _cupsStrFree(p
->reasons
[i
]);
2695 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
2697 if (PrintcapFormat
== PRINTCAP_PLIST
)
2698 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2701 if (!strcmp(s
, "none"))
2705 * Loop through all of the reasons...
2711 * Skip leading whitespace and commas...
2714 while (isspace(*sptr
& 255) || *sptr
== ',')
2717 for (rptr
= reason
; *sptr
&& !isspace(*sptr
& 255) && *sptr
!= ','; sptr
++)
2718 if (rptr
< (reason
+ sizeof(reason
) - 1))
2732 for (i
= 0; i
< p
->num_reasons
; i
++)
2733 if (!strcmp(reason
, p
->reasons
[i
]))
2736 * Found a match, so remove it...
2739 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2740 "cupsdSetPrinterReasons: Removing \"%s\" at index %d",
2744 _cupsStrFree(p
->reasons
[i
]);
2746 if (i
< p
->num_reasons
)
2747 memmove(p
->reasons
+ i
, p
->reasons
+ i
+ 1,
2748 (p
->num_reasons
- i
) * sizeof(char *));
2750 if (!strcmp(reason
, "paused") && p
->state
== IPP_PRINTER_STOPPED
)
2751 cupsdSetPrinterState(p
, IPP_PRINTER_IDLE
, 1);
2753 if (strcmp(reason
, "connecting-to-device"))
2754 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
2756 if (PrintcapFormat
== PRINTCAP_PLIST
)
2757 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2762 else if (p
->num_reasons
< (int)(sizeof(p
->reasons
) / sizeof(p
->reasons
[0])))
2768 for (i
= 0; i
< p
->num_reasons
; i
++)
2769 if (!strcmp(reason
, p
->reasons
[i
]))
2772 if (i
>= p
->num_reasons
)
2774 if (i
>= (int)(sizeof(p
->reasons
) / sizeof(p
->reasons
[0])))
2776 cupsdLogMessage(CUPSD_LOG_ALERT
,
2777 "Too many printer-state-reasons values for %s (%d)",
2782 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2783 "cupsdSetPrinterReasons: Adding \"%s\" at index %d",
2786 p
->reasons
[i
] = _cupsStrAlloc(reason
);
2789 if (!strcmp(reason
, "paused") && p
->state
!= IPP_PRINTER_STOPPED
)
2790 cupsdSetPrinterState(p
, IPP_PRINTER_STOPPED
, 1);
2792 if (strcmp(reason
, "connecting-to-device"))
2793 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
2795 if (PrintcapFormat
== PRINTCAP_PLIST
)
2796 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2804 * 'cupsdSetPrinterState()' - Update the current state of a printer.
2808 cupsdSetPrinterState(
2809 cupsd_printer_t
*p
, /* I - Printer to change */
2810 ipp_pstate_t s
, /* I - New state */
2811 int update
) /* I - Update printers.conf? */
2813 ipp_pstate_t old_state
; /* Old printer state */
2817 * Can't set status of remote printers...
2820 if (p
->type
& CUPS_PRINTER_DISCOVERED
)
2824 * Set the new state...
2827 if (PrintcapFormat
== PRINTCAP_PLIST
)
2828 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2830 old_state
= p
->state
;
2835 cupsdAddEvent(s
== IPP_PRINTER_STOPPED
? CUPSD_EVENT_PRINTER_STOPPED
:
2836 CUPSD_EVENT_PRINTER_STATE
, p
, NULL
,
2837 "%s \"%s\" state changed.",
2838 (p
->type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
2842 * Let the browse code know this needs to be updated...
2846 p
->state_time
= time(NULL
);
2850 write_irix_state(p
);
2855 * Set/clear the paused reason as needed...
2858 if (s
== IPP_PRINTER_STOPPED
)
2859 cupsdSetPrinterReasons(p
, "+paused");
2861 cupsdSetPrinterReasons(p
, "-paused");
2864 * Clear the message for the queue when going to processing...
2867 if (s
== IPP_PRINTER_PROCESSING
)
2868 p
->state_message
[0] = '\0';
2871 * Update the printer history...
2874 cupsdAddPrinterHistory(p
);
2877 * Let the browse protocols reflect the change...
2881 cupsdRegisterPrinter(p
);
2884 * Save the printer configuration if a printer goes from idle or processing
2885 * to stopped (or visa-versa)...
2889 (old_state
== IPP_PRINTER_STOPPED
) != (s
== IPP_PRINTER_STOPPED
))
2891 if (p
->type
& CUPS_PRINTER_CLASS
)
2892 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
2894 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
2900 * 'cupsdStopPrinter()' - Stop a printer from printing any jobs...
2904 cupsdStopPrinter(cupsd_printer_t
*p
, /* I - Printer to stop */
2905 int update
)/* I - Update printers.conf? */
2908 * Set the printer state...
2911 cupsdSetPrinterState(p
, IPP_PRINTER_STOPPED
, update
);
2914 * See if we have a job printing on this printer...
2918 cupsdSetJobState(p
->job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
2919 "Job stopped due to printer being paused.");
2924 * 'cupsdUpdatePrinterPPD()' - Update keywords in a printer's PPD file.
2927 int /* O - 1 if successful, 0 otherwise */
2928 cupsdUpdatePrinterPPD(
2929 cupsd_printer_t
*p
, /* I - Printer */
2930 int num_keywords
, /* I - Number of keywords */
2931 cups_option_t
*keywords
) /* I - Keywords */
2933 int i
; /* Looping var */
2934 cups_file_t
*src
, /* Original file */
2935 *dst
; /* New file */
2936 char srcfile
[1024], /* Original filename */
2937 dstfile
[1024], /* New filename */
2938 line
[1024], /* Line from file */
2939 keystring
[41]; /* Keyword from line */
2940 cups_option_t
*keyword
; /* Current keyword */
2943 cupsdLogMessage(CUPSD_LOG_INFO
, "Updating keywords in PPD file for %s...",
2947 * Get the old and new PPD filenames...
2950 snprintf(srcfile
, sizeof(srcfile
), "%s/ppd/%s.ppd.O", ServerRoot
, p
->name
);
2951 snprintf(dstfile
, sizeof(srcfile
), "%s/ppd/%s.ppd", ServerRoot
, p
->name
);
2954 * Rename the old file and open the old and new...
2957 if (rename(dstfile
, srcfile
))
2959 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to backup PPD file for %s: %s",
2960 p
->name
, strerror(errno
));
2964 if ((src
= cupsFileOpen(srcfile
, "r")) == NULL
)
2966 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open PPD file \"%s\": %s",
2967 srcfile
, strerror(errno
));
2968 rename(srcfile
, dstfile
);
2972 if ((dst
= cupsFileOpen(dstfile
, "w")) == NULL
)
2974 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create PPD file \"%s\": %s",
2975 dstfile
, strerror(errno
));
2977 rename(srcfile
, dstfile
);
2982 * Copy the first line and then write out all of the keywords...
2985 if (!cupsFileGets(src
, line
, sizeof(line
)))
2987 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to read PPD file \"%s\": %s",
2988 srcfile
, strerror(errno
));
2991 rename(srcfile
, dstfile
);
2995 cupsFilePrintf(dst
, "%s\n", line
);
2997 for (i
= num_keywords
, keyword
= keywords
; i
> 0; i
--, keyword
++)
2999 cupsdLogMessage(CUPSD_LOG_DEBUG
, "*%s: %s", keyword
->name
, keyword
->value
);
3000 cupsFilePrintf(dst
, "*%s: %s\n", keyword
->name
, keyword
->value
);
3004 * Then copy the rest of the PPD file, dropping any keywords we changed.
3007 while (cupsFileGets(src
, line
, sizeof(line
)))
3010 * Skip keywords we've already set...
3013 if (sscanf(line
, "*%40[^:]:", keystring
) == 1 &&
3014 cupsGetOption(keystring
, num_keywords
, keywords
))
3018 * Otherwise write the line...
3021 cupsFilePrintf(dst
, "%s\n", line
);
3025 * Close files and return...
3036 * 'cupsdUpdatePrinters()' - Update printers after a partial reload.
3040 cupsdUpdatePrinters(void)
3042 cupsd_printer_t
*p
; /* Current printer */
3046 * Loop through the printers and recreate the printer attributes
3047 * for any local printers since the policy and/or access control
3048 * stuff may have changed. Also, if browsing is disabled, remove
3049 * any remote printers...
3052 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3054 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3057 * Remove remote printers if we are no longer browsing...
3061 (p
->type
& (CUPS_PRINTER_IMPLICIT
| CUPS_PRINTER_DISCOVERED
)))
3063 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
3064 cupsArrayRemove(ImplicitPrinters
, p
);
3066 cupsArraySave(Printers
);
3067 cupsdDeletePrinter(p
, 0);
3068 cupsArrayRestore(Printers
);
3073 * Update the operation policy pointer...
3076 if ((p
->op_policy_ptr
= cupsdFindPolicy(p
->op_policy
)) == NULL
)
3077 p
->op_policy_ptr
= DefaultPolicyPtr
;
3080 * Update printer attributes as needed...
3083 if (!(p
->type
& CUPS_PRINTER_DISCOVERED
))
3084 cupsdSetPrinterAttrs(p
);
3090 * 'cupsdValidateDest()' - Validate a printer/class destination.
3093 const char * /* O - Printer or class name */
3095 const char *uri
, /* I - Printer URI */
3096 cups_ptype_t
*dtype
, /* O - Type (printer or class) */
3097 cupsd_printer_t
**printer
) /* O - Printer pointer */
3099 cupsd_printer_t
*p
; /* Current printer */
3100 char localname
[1024],/* Localized hostname */
3101 *lptr
, /* Pointer into localized hostname */
3102 *sptr
, /* Pointer into server name */
3103 *rptr
, /* Pointer into resource */
3104 scheme
[32], /* Scheme portion of URI */
3105 username
[64], /* Username portion of URI */
3106 hostname
[HTTP_MAX_HOST
],
3107 /* Host portion of URI */
3108 resource
[HTTP_MAX_URI
];
3109 /* Resource portion of URI */
3110 int port
; /* Port portion of URI */
3113 DEBUG_printf(("cupsdValidateDest(uri=\"%s\", dtype=%p, printer=%p)\n", uri
,
3117 * Initialize return values...
3124 *dtype
= (cups_ptype_t
)0;
3127 * Pull the hostname and resource from the URI...
3130 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
3131 username
, sizeof(username
), hostname
, sizeof(hostname
),
3132 &port
, resource
, sizeof(resource
));
3135 * See if the resource is a class or printer...
3138 if (!strncmp(resource
, "/classes/", 9))
3144 rptr
= resource
+ 9;
3146 else if (!strncmp(resource
, "/printers/", 10))
3152 rptr
= resource
+ 10;
3157 * Bad resource name...
3164 * See if the printer or class name exists...
3167 p
= cupsdFindDest(rptr
);
3169 if (p
== NULL
&& strchr(rptr
, '@') == NULL
)
3177 *dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
|
3178 CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
);
3184 * Change localhost to the server name...
3187 if (!strcasecmp(hostname
, "localhost"))
3188 strlcpy(hostname
, ServerName
, sizeof(hostname
));
3190 strlcpy(localname
, hostname
, sizeof(localname
));
3192 if (!strcasecmp(hostname
, ServerName
))
3195 * Localize the hostname...
3198 lptr
= strchr(localname
, '.');
3199 sptr
= strchr(ServerName
, '.');
3201 if (sptr
!= NULL
&& lptr
!= NULL
)
3204 * Strip the common domain name components...
3207 while (lptr
!= NULL
)
3209 if (!strcasecmp(lptr
, sptr
))
3215 lptr
= strchr(lptr
+ 1, '.');
3220 DEBUG_printf(("localized hostname is \"%s\"...\n", localname
));
3223 * Find a matching printer or class...
3226 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3228 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3229 if (!strcasecmp(p
->hostname
, localname
) &&
3230 !strcasecmp(p
->name
, rptr
))
3236 *dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
|
3237 CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
);
3247 * 'cupsdWritePrintcap()' - Write a pseudo-printcap file for older applications
3252 cupsdWritePrintcap(void)
3254 int i
; /* Looping var */
3255 cups_file_t
*fp
; /* Printcap file */
3256 cupsd_printer_t
*p
; /* Current printer */
3261 * Update the IRIX printer state for the default printer; if
3262 * no printers remain, then the default printer file will be
3266 write_irix_state(DefaultPrinter
);
3270 * See if we have a printcap file; if not, don't bother writing it.
3273 if (!Printcap
|| !*Printcap
)
3277 * Open the printcap file...
3280 if ((fp
= cupsFileOpen(Printcap
, "w")) == NULL
)
3284 * Put a comment header at the top so that users will know where the
3285 * data has come from...
3288 if (PrintcapFormat
!= PRINTCAP_PLIST
)
3289 cupsFilePrintf(fp
, "# This file was automatically generated by cupsd(8) "
3291 "# %s/printers.conf file. All changes to this file\n"
3292 "# will be lost.\n", ServerRoot
);
3295 * Write a new printcap with the current list of printers.
3298 switch (PrintcapFormat
)
3302 * Each printer is put in the file as:
3312 cupsFilePrintf(fp
, "%s|%s:rm=%s:rp=%s:\n", DefaultPrinter
->name
,
3313 DefaultPrinter
->info
, ServerName
,
3314 DefaultPrinter
->name
);
3316 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3318 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3319 if (p
!= DefaultPrinter
)
3320 cupsFilePrintf(fp
, "%s|%s:rm=%s:rp=%s:\n", p
->name
, p
->info
,
3321 ServerName
, p
->name
);
3324 case PRINTCAP_PLIST
:
3326 * Each printer is written as a dictionary in a plist file.
3327 * Currently the printer-name, printer-info, printer-is-accepting-jobs,
3328 * printer-location, printer-make-and-model, printer-state,
3329 * printer-state-reasons, printer-type, and (sanitized) device-uri.
3332 cupsFilePuts(fp
, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
3333 "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD "
3334 "PLIST 1.0//EN\" \"http://www.apple.com/DTDs/"
3335 "PropertyList-1.0.dtd\">\n"
3336 "<plist version=\"1.0\">\n"
3339 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3341 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3343 cupsFilePuts(fp
, "\t<dict>\n"
3344 "\t\t<key>printer-name</key>\n"
3346 write_xml_string(fp
, p
->name
);
3347 cupsFilePuts(fp
, "</string>\n"
3348 "\t\t<key>printer-info</key>\n"
3350 write_xml_string(fp
, p
->info
);
3351 cupsFilePrintf(fp
, "</string>\n"
3352 "\t\t<key>printer-is-accepting-jobs</key>\n"
3354 "\t\t<key>printer-location</key>\n"
3355 "\t\t<string>", p
->accepting
? "true" : "false");
3356 write_xml_string(fp
, p
->location
);
3357 cupsFilePuts(fp
, "</string>\n"
3358 "\t\t<key>printer-make-and-model</key>\n"
3360 write_xml_string(fp
, p
->make_model
);
3361 cupsFilePrintf(fp
, "</string>\n"
3362 "\t\t<key>printer-state</key>\n"
3363 "\t\t<integer>%d</integer>\n"
3364 "\t\t<key>printer-state-reasons</key>\n"
3365 "\t\t<array>\n", p
->state
);
3366 for (i
= 0; i
< p
->num_reasons
; i
++)
3368 cupsFilePuts(fp
, "\t\t\t<string>");
3369 write_xml_string(fp
, p
->reasons
[i
]);
3370 cupsFilePuts(fp
, "</string>\n");
3372 cupsFilePrintf(fp
, "\t\t</array>\n"
3373 "\t\t<key>printer-type</key>\n"
3374 "\t\t<integer>%d</integer>\n"
3375 "\t\t<key>device-uri</key>\n"
3376 "\t\t<string>", p
->type
);
3377 write_xml_string(fp
, p
->sanitized_device_uri
);
3378 cupsFilePuts(fp
, "</string>\n"
3381 cupsFilePuts(fp
, "</array>\n"
3385 case PRINTCAP_SOLARIS
:
3387 * Each printer is put in the file as:
3389 * _all:all=Printer1,Printer2,Printer3,...,PrinterN
3390 * _default:use=DefaultPrinter
3392 * :bsdaddr=ServerName,Printer1:\
3393 * :description=Description:
3395 * :bsdaddr=ServerName,Printer2:\
3396 * :description=Description:
3398 * :bsdaddr=ServerName,Printer3:\
3399 * :description=Description:
3402 * :bsdaddr=ServerName,PrinterN:\
3403 * :description=Description:
3406 cupsFilePuts(fp
, "_all:all=");
3407 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3409 p
= (cupsd_printer_t
*)cupsArrayCurrent(Printers
))
3410 cupsFilePrintf(fp
, "%s%c", p
->name
,
3411 cupsArrayNext(Printers
) ? ',' : '\n');
3414 cupsFilePrintf(fp
, "_default:use=%s\n", DefaultPrinter
->name
);
3416 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3418 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3419 cupsFilePrintf(fp
, "%s:\\\n"
3420 "\t:bsdaddr=%s,%s:\\\n"
3421 "\t:description=%s:\n",
3422 p
->name
, ServerName
, p
->name
,
3423 p
->info
? p
->info
: "");
3436 * 'add_printer_defaults()' - Add name-default attributes to the printer attributes.
3440 add_printer_defaults(cupsd_printer_t
*p
)/* I - Printer */
3442 int i
; /* Looping var */
3443 int num_options
; /* Number of default options */
3444 cups_option_t
*options
, /* Default options */
3445 *option
; /* Current option */
3446 char name
[256]; /* name-default */
3450 * Maintain a common array of default attribute names...
3453 if (!CommonDefaults
)
3455 CommonDefaults
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
3457 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("copies-default"));
3458 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("document-format-default"));
3459 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("finishings-default"));
3460 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("job-hold-until-default"));
3461 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("job-priority-default"));
3462 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("job-sheets-default"));
3463 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("media-default"));
3464 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("number-up-default"));
3465 cupsArrayAdd(CommonDefaults
,
3466 _cupsStrAlloc("orientation-requested-default"));
3467 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("sides-default"));
3471 * Add all of the default options from the .conf files...
3474 for (num_options
= 0, options
= NULL
, i
= p
->num_options
, option
= p
->options
;
3478 if (strcmp(option
->name
, "ipp-options") &&
3479 strcmp(option
->name
, "job-sheets") &&
3480 strcmp(option
->name
, "lease-duration"))
3482 snprintf(name
, sizeof(name
), "%s-default", option
->name
);
3483 num_options
= cupsAddOption(name
, option
->value
, num_options
, &options
);
3485 if (!cupsArrayFind(CommonDefaults
, name
))
3486 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc(name
));
3491 * Convert options to IPP attributes...
3494 cupsEncodeOptions2(p
->attrs
, num_options
, options
, IPP_TAG_PRINTER
);
3495 cupsFreeOptions(num_options
, options
);
3498 * Add standard -default attributes as needed...
3501 if (!cupsGetOption("copies", p
->num_options
, p
->options
))
3502 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "copies-default",
3505 if (!cupsGetOption("document-format", p
->num_options
, p
->options
))
3506 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_MIMETYPE
,
3507 "document-format-default", NULL
, "application/octet-stream");
3509 if (!cupsGetOption("job-hold-until", p
->num_options
, p
->options
))
3510 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
3511 "job-hold-until-default", NULL
, "no-hold");
3513 if (!cupsGetOption("job-priority", p
->num_options
, p
->options
))
3514 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
3515 "job-priority-default", 50);
3517 if (!cupsGetOption("number-up", p
->num_options
, p
->options
))
3518 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
3519 "number-up-default", 1);
3521 if (!cupsGetOption("orientation-requested", p
->num_options
, p
->options
))
3522 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NOVALUE
,
3523 "orientation-requested-default", NULL
, NULL
);
3525 if (!cupsGetOption("notify-lease-duration", p
->num_options
, p
->options
))
3526 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
3527 "notify-lease-duration-default", DefaultLeaseDuration
);
3529 if (!cupsGetOption("notify-events", p
->num_options
, p
->options
))
3530 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
3531 "notify-events-default", NULL
, "job-completed");
3536 * 'add_printer_filter()' - Add a MIME filter for a printer.
3541 cupsd_printer_t
*p
, /* I - Printer to add to */
3542 mime_type_t
*filtertype
, /* I - Filter or prefilter MIME type */
3543 const char *filter
) /* I - Filter to add */
3545 char super
[MIME_MAX_SUPER
], /* Super-type for filter */
3546 type
[MIME_MAX_TYPE
], /* Type for filter */
3547 program
[1024]; /* Program/filter name */
3548 int cost
; /* Cost of filter */
3549 mime_type_t
*temptype
; /* MIME type looping var */
3550 char filename
[1024], /* Full filter filename */
3551 *dirsep
; /* Pointer to directory separator */
3552 struct stat fileinfo
; /* File information */
3556 * Parse the filter string; it should be in the following format:
3558 * super/type cost program
3561 if (sscanf(filter
, "%15[^/]/%31s%d%*[ \t]%1023[^\n]", super
, type
, &cost
,
3564 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s: invalid filter string \"%s\"!",
3570 * See if the filter program exists; if not, stop the printer and flag
3574 if (strcmp(program
, "-"))
3576 if (program
[0] == '/')
3577 strlcpy(filename
, program
, sizeof(filename
));
3579 snprintf(filename
, sizeof(filename
), "%s/filter/%s", ServerBin
, program
);
3581 if (stat(filename
, &fileinfo
))
3583 snprintf(p
->state_message
, sizeof(p
->state_message
),
3584 "Filter \"%s\" for printer \"%s\" not available: %s",
3585 filename
, p
->name
, strerror(errno
));
3586 cupsdSetPrinterReasons(p
, "+cups-missing-filter-error");
3588 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s", p
->state_message
);
3593 * When running as root, do additional security checks...
3599 * Only use filters that are owned by root and do not have world write
3603 if (fileinfo
.st_uid
|| (fileinfo
.st_mode
& S_IWOTH
) != 0)
3605 if (fileinfo
.st_uid
)
3606 snprintf(p
->state_message
, sizeof(p
->state_message
),
3607 "Filter \"%s\" for printer \"%s\" not owned by root",
3610 snprintf(p
->state_message
, sizeof(p
->state_message
),
3611 "Filter \"%s\" for printer \"%s\" has insecure permissions "
3612 "(0%o)", filename
, p
->name
, fileinfo
.st_mode
);
3614 cupsdSetPrinterReasons(p
, "+cups-insecure-filter-error");
3616 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s", p
->state_message
);
3621 * Similarly, check that the parent directory is also owned by root and
3622 * does not have world write permissions.
3625 if ((dirsep
= strrchr(filename
, '/')) != NULL
)
3628 if (stat(filename
, &fileinfo
))
3630 snprintf(p
->state_message
, sizeof(p
->state_message
),
3631 "Filter directory \"%s\" for printer \"%s\" not accessible: %s",
3632 filename
, p
->name
, strerror(errno
));
3633 cupsdSetPrinterReasons(p
, "+cups-missing-filter-error");
3635 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s", p
->state_message
);
3638 else if (fileinfo
.st_uid
|| (fileinfo
.st_mode
& S_IWOTH
) != 0)
3640 if (fileinfo
.st_uid
)
3641 snprintf(p
->state_message
, sizeof(p
->state_message
),
3642 "Filter directory \"%s\" for printer \"%s\" not owned by "
3643 "root", filename
, p
->name
);
3645 snprintf(p
->state_message
, sizeof(p
->state_message
),
3646 "Filter directory \"%s\" for printer \"%s\" has insecure "
3647 "permissions (0%o)", filename
, p
->name
, fileinfo
.st_mode
);
3649 cupsdSetPrinterReasons(p
, "+cups-insecure-filter-error");
3651 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s", p
->state_message
);
3658 * Add the filter to the MIME database, supporting wildcards as needed...
3661 for (temptype
= mimeFirstType(MimeDatabase
);
3663 temptype
= mimeNextType(MimeDatabase
))
3664 if (((super
[0] == '*' && strcasecmp(temptype
->super
, "printer")) ||
3665 !strcasecmp(temptype
->super
, super
)) &&
3666 (type
[0] == '*' || !strcasecmp(temptype
->type
, type
)))
3668 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3669 "add_printer_filter: %s: adding filter %s/%s %s/%s %d %s",
3670 p
->name
, temptype
->super
, temptype
->type
,
3671 filtertype
->super
, filtertype
->type
,
3673 mimeAddFilter(MimeDatabase
, temptype
, filtertype
, cost
, program
);
3679 * 'add_printer_formats()' - Add document-format-supported values for a printer.
3683 add_printer_formats(cupsd_printer_t
*p
) /* I - Printer */
3685 int i
; /* Looping var */
3686 mime_type_t
*type
; /* Current MIME type */
3687 cups_array_t
*filters
; /* Filters */
3688 ipp_attribute_t
*attr
; /* document-format-supported attribute */
3689 char mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
3690 /* MIME type name */
3694 * Raw (and remote) queues advertise all of the supported MIME
3698 cupsArrayDelete(p
->filetypes
);
3699 p
->filetypes
= NULL
;
3703 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
,
3704 (ipp_tag_t
)(IPP_TAG_MIMETYPE
| IPP_TAG_COPY
),
3705 "document-format-supported", NumMimeTypes
, NULL
, MimeTypes
);
3710 * Otherwise, loop through the supported MIME types and see if there
3711 * are filters for them...
3714 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_printer_formats: %d types, %d filters",
3715 mimeNumTypes(MimeDatabase
), mimeNumFilters(MimeDatabase
));
3717 p
->filetypes
= cupsArrayNew(NULL
, NULL
);
3719 for (type
= mimeFirstType(MimeDatabase
);
3721 type
= mimeNextType(MimeDatabase
))
3723 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", type
->super
, type
->type
);
3725 if ((filters
= mimeFilter(MimeDatabase
, type
, p
->filetype
, NULL
)) != NULL
)
3727 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3728 "add_printer_formats: %s: %s needs %d filters",
3729 p
->name
, mimetype
, cupsArrayCount(filters
));
3731 cupsArrayDelete(filters
);
3732 cupsArrayAdd(p
->filetypes
, type
);
3735 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3736 "add_printer_formats: %s: %s not supported",
3740 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3741 "add_printer_formats: %s: %d supported types",
3742 p
->name
, cupsArrayCount(p
->filetypes
) + 1);
3745 * Add the file formats that can be filtered...
3748 if ((type
= mimeType(MimeDatabase
, "application", "octet-stream")) == NULL
||
3749 !cupsArrayFind(p
->filetypes
, type
))
3754 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_MIMETYPE
,
3755 "document-format-supported",
3756 cupsArrayCount(p
->filetypes
) + 1, NULL
, NULL
);
3759 attr
->values
[0].string
.text
= _cupsStrAlloc("application/octet-stream");
3761 for (type
= (mime_type_t
*)cupsArrayFirst(p
->filetypes
);
3763 i
++, type
= (mime_type_t
*)cupsArrayNext(p
->filetypes
))
3765 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", type
->super
, type
->type
);
3767 attr
->values
[i
].string
.text
= _cupsStrAlloc(mimetype
);
3772 char pdl
[1024]; /* Buffer to build pdl list */
3773 mime_filter_t
*filter
; /* MIME filter looping var */
3778 if (mimeType(MimeDatabase
, "application", "pdf"))
3779 strlcat(pdl
, "application/pdf,", sizeof(pdl
));
3781 if (mimeType(MimeDatabase
, "application", "postscript"))
3782 strlcat(pdl
, "application/postscript,", sizeof(pdl
));
3784 if (mimeType(MimeDatabase
, "application", "vnd.cups-raster"))
3785 strlcat(pdl
, "application/vnd.cups-raster,", sizeof(pdl
));
3788 * Determine if this is a Tioga PrintJobMgr based queue...
3791 for (filter
= (mime_filter_t
*)cupsArrayFirst(MimeDatabase
->filters
);
3793 filter
= (mime_filter_t
*)cupsArrayNext(MimeDatabase
->filters
))
3795 if (filter
->dst
== p
->filetype
&& filter
->filter
&&
3796 strstr(filter
->filter
, "PrintJobMgr"))
3801 * We only support raw printing if this is not a Tioga PrintJobMgr based
3802 * queue and if application/octet-stream is a known conversion...
3805 if (!filter
&& mimeType(MimeDatabase
, "application", "octet-stream"))
3806 strlcat(pdl
, "application/octet-stream,", sizeof(pdl
));
3808 if (mimeType(MimeDatabase
, "image", "png"))
3809 strlcat(pdl
, "image/png,", sizeof(pdl
));
3812 pdl
[strlen(pdl
) - 1] = '\0'; /* Remove trailing comma */
3814 cupsdSetString(&p
->pdl
, pdl
);
3816 #endif /* HAVE_DNSSD */
3821 * 'add_string_array()' - Add a string to an array of CUPS strings.
3825 add_string_array(cups_array_t
**a
, /* I - Array */
3826 const char *s
) /* I - String */
3829 *a
= cupsArrayNew(NULL
, NULL
);
3831 cupsArrayAdd(*a
, _cupsStrAlloc(s
));
3836 * 'compare_printers()' - Compare two printers.
3839 static int /* O - Result of comparison */
3840 compare_printers(void *first
, /* I - First printer */
3841 void *second
, /* I - Second printer */
3842 void *data
) /* I - App data (not used) */
3844 return (strcasecmp(((cupsd_printer_t
*)first
)->name
,
3845 ((cupsd_printer_t
*)second
)->name
));
3850 * 'delete_printer_filters()' - Delete all MIME filters for a printer.
3854 delete_printer_filters(
3855 cupsd_printer_t
*p
) /* I - Printer to remove from */
3857 mime_filter_t
*filter
; /* MIME filter looping var */
3861 * Range check input...
3868 * Remove all filters from the MIME database that have a destination
3869 * type == printer...
3872 for (filter
= mimeFirstFilter(MimeDatabase
);
3874 filter
= mimeNextFilter(MimeDatabase
))
3875 if (filter
->dst
== p
->filetype
)
3878 * Delete the current filter...
3881 mimeDeleteFilter(MimeDatabase
, filter
);
3884 cupsdSetPrinterReasons(p
, "-cups-insecure-filter-error"
3885 ",cups-missing-filter-error");
3890 * 'delete_string_array()' - Delete an array of CUPS strings.
3894 delete_string_array(cups_array_t
**a
) /* I - Array */
3896 char *ptr
; /* Current string */
3899 for (ptr
= (char *)cupsArrayFirst(*a
);
3901 ptr
= (char *)cupsArrayNext(*a
))
3904 cupsArrayDelete(*a
);
3910 * 'load_ppd()' - Load a cached PPD file, updating the cache as needed.
3914 load_ppd(cupsd_printer_t
*p
) /* I - Printer */
3916 int i
; /* Looping var */
3917 cups_file_t
*cache
; /* Cache file */
3918 char cache_name
[1024]; /* Cache filename */
3919 struct stat cache_info
; /* Cache file info */
3920 ppd_file_t
*ppd
; /* PPD file */
3921 char ppd_name
[1024]; /* PPD filename */
3922 struct stat ppd_info
; /* PPD file info */
3923 int num_media
; /* Number of media options */
3924 char custom_in
[256], /* Custom size name in inches */
3925 custom_mm
[256]; /* Custom size name in millimeters */
3926 ppd_size_t
*size
; /* Current size */
3927 ppd_option_t
*output_bin
, /* OutputBin options */
3928 *duplex
; /* Duplex options */
3929 ppd_attr_t
*ppd_attr
; /* PPD attribute */
3930 _cups_pwg_media_t
*pwgmedia
; /* Matching PWG size name */
3931 ipp_attribute_t
*attr
; /* Attribute data */
3932 ipp_t
*media_col_default
, /* media-col-default collection value */
3933 *media_size
; /* media-size collection value */
3934 ipp_value_t
*val
; /* Attribute value */
3935 int num_finishings
; /* Number of finishings */
3936 int finishings
[5]; /* finishings-supported values */
3937 static const char * const sides
[3] = /* sides-supported values */
3940 "two-sided-long-edge",
3941 "two-sided-short-edge"
3943 static const char * const standard_commands
[] =
3944 { /* Standard CUPS commands */
3952 * Check to see if the cache is up-to-date...
3955 snprintf(cache_name
, sizeof(cache_name
), "%s/%s.ipp", CacheDir
, p
->name
);
3956 if (stat(cache_name
, &cache_info
))
3957 cache_info
.st_mtime
= 0;
3959 snprintf(ppd_name
, sizeof(ppd_name
), "%s/ppd/%s.ppd", ServerRoot
, p
->name
);
3960 if (stat(ppd_name
, &ppd_info
))
3961 ppd_info
.st_mtime
= 1;
3963 ippDelete(p
->ppd_attrs
);
3964 p
->ppd_attrs
= ippNew();
3966 if (cache_info
.st_mtime
>= ppd_info
.st_mtime
&&
3967 (cache
= cupsFileOpen(cache_name
, "r")) != NULL
)
3970 * Load cached information and return...
3973 cupsdLogMessage(CUPSD_LOG_DEBUG
, "load_ppd: Loading %s...", cache_name
);
3975 if (ippReadIO(cache
, (ipp_iocb_t
)cupsFileRead
, 1, NULL
,
3976 p
->ppd_attrs
) == IPP_DATA
)
3978 cupsFileClose(cache
);
3982 cupsFileClose(cache
);
3986 * Reload PPD attributes from disk...
3989 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3991 cupsdLogMessage(CUPSD_LOG_DEBUG
, "load_ppd: Loading %s...", ppd_name
);
3993 delete_string_array(&(p
->filters
));
3994 delete_string_array(&(p
->pre_filters
));
3996 p
->type
&= ~CUPS_PRINTER_OPTIONS
;
3997 p
->type
|= CUPS_PRINTER_BW
;
3999 finishings
[0] = IPP_FINISHINGS_NONE
;
4002 if ((ppd
= ppdOpenFile(ppd_name
)) != NULL
)
4005 * Add make/model and other various attributes...
4008 ppdMarkDefaults(ppd
);
4010 if (ppd
->color_device
)
4011 p
->type
|= CUPS_PRINTER_COLOR
;
4012 if (ppd
->variable_sizes
)
4013 p
->type
|= CUPS_PRINTER_VARIABLE
;
4014 if (!ppd
->manual_copies
)
4015 p
->type
|= CUPS_PRINTER_COPIES
;
4016 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsFax", NULL
)) != NULL
)
4017 if (ppd_attr
->value
&& !strcasecmp(ppd_attr
->value
, "true"))
4018 p
->type
|= CUPS_PRINTER_FAX
;
4020 ippAddBoolean(p
->ppd_attrs
, IPP_TAG_PRINTER
, "color-supported",
4022 if (ppd
->throughput
)
4023 ippAddInteger(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4024 "pages-per-minute", ppd
->throughput
);
4029 * The NickName can be localized in the character set specified
4030 * by the LanugageEncoding attribute. However, ppdOpen2() has
4031 * already converted the ppd->nickname member to UTF-8 for us
4032 * (the original attribute value is available separately)
4035 cupsdSetString(&p
->make_model
, ppd
->nickname
);
4037 else if (ppd
->modelname
)
4040 * Model name can only contain specific characters...
4043 cupsdSetString(&p
->make_model
, ppd
->modelname
);
4046 cupsdSetString(&p
->make_model
, "Bad PPD File");
4048 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4049 "printer-make-and-model", NULL
, p
->make_model
);
4052 * Add media options from the PPD file...
4055 if (ppd
->num_sizes
== 0)
4057 if (!ppdFindAttr(ppd
, "APScannerOnly", NULL
))
4058 cupsdLogMessage(CUPSD_LOG_CRIT
,
4059 "The PPD file for printer %s contains no media "
4060 "options and is therefore invalid!", p
->name
);
4062 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4063 "media-supported", NULL
, "unknown");
4067 num_media
= ppd
->num_sizes
;
4068 if (ppd
->variable_sizes
)
4071 attr
= ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4072 "media-supported", num_media
, NULL
, NULL
);
4077 for (i
= ppd
->num_sizes
, size
= ppd
->sizes
; i
> 0; i
--, size
++)
4079 if (strcasecmp(size
->name
, "Custom"))
4081 if ((pwgmedia
= _cupsPWGMediaBySize(size
->width
,
4082 size
->length
)) != NULL
)
4084 val
->string
.text
= _cupsStrAlloc(pwgmedia
->pwg
);
4088 snprintf(custom_in
, sizeof(custom_in
), "adobe_%s_%gx%gin",
4089 size
->name
, size
->width
/ 72.0, size
->length
/ 72.0);
4090 snprintf(custom_mm
, sizeof(custom_mm
), "adobe_%s_%gx%gmm",
4091 size
->name
, size
->width
* 25.4 / 72.0,
4092 size
->length
* 25.4 / 72.0);
4093 if (strlen(custom_in
) < strlen(custom_mm
))
4094 val
->string
.text
= _cupsStrAlloc(custom_in
);
4096 val
->string
.text
= _cupsStrAlloc(custom_mm
);
4102 * Add media-default...
4105 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4106 "media-default", NULL
, val
->string
.text
);
4109 * Add media-col-default...
4112 media_size
= ippNew();
4113 ippAddInteger(media_size
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4114 "x-dimension", (int)(size
->width
* 2540.0 / 72.0));
4115 ippAddInteger(media_size
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4116 "y-dimension", (int)(size
->length
* 2540.0 / 72.0));
4118 media_col_default
= ippNew();
4119 ippAddString(media_col_default
, IPP_TAG_PRINTER
,
4120 IPP_TAG_KEYWORD
| IPP_TAG_COPY
, "media-color", NULL
,
4122 ippAddString(media_col_default
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4123 "media-key", NULL
,val
->string
.text
);
4124 ippAddCollection(media_col_default
, IPP_TAG_PRINTER
, "media-size",
4126 ippAddString(media_col_default
, IPP_TAG_PRINTER
,
4127 IPP_TAG_KEYWORD
| IPP_TAG_COPY
, "media-type", NULL
,
4130 ippAddCollection(p
->ppd_attrs
, IPP_TAG_PRINTER
,
4131 "media-col-default", media_col_default
);
4138 if (ppd
->variable_sizes
)
4140 snprintf(custom_in
, sizeof(custom_in
), "custom_min_%gx%gin",
4141 ppd
->custom_min
[0] / 72.0, ppd
->custom_min
[1] / 72.0);
4142 snprintf(custom_mm
, sizeof(custom_mm
), "custom_min_%gx%gmm",
4143 ppd
->custom_min
[0] * 25.4 / 72.0,
4144 ppd
->custom_min
[1] * 25.4 / 72.0);
4145 if (strlen(custom_in
) < strlen(custom_mm
))
4146 val
->string
.text
= _cupsStrAlloc(custom_in
);
4148 val
->string
.text
= _cupsStrAlloc(custom_mm
);
4151 snprintf(custom_in
, sizeof(custom_in
), "custom_max_%gx%gin",
4152 ppd
->custom_max
[0] / 72.0, ppd
->custom_max
[1] / 72.0);
4153 snprintf(custom_mm
, sizeof(custom_mm
), "custom_max_%gx%gmm",
4154 ppd
->custom_max
[0] * 25.4 / 72.0,
4155 ppd
->custom_max
[1] * 25.4 / 72.0);
4156 if (strlen(custom_in
) < strlen(custom_mm
))
4157 val
->string
.text
= _cupsStrAlloc(custom_in
);
4159 val
->string
.text
= _cupsStrAlloc(custom_mm
);
4168 if ((output_bin
= ppdFindOption(ppd
, "OutputBin")) != NULL
)
4170 attr
= ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4171 "output-bin-supported", output_bin
->num_choices
,
4176 for (i
= 0, val
= attr
->values
;
4177 i
< output_bin
->num_choices
;
4179 val
->string
.text
= _cupsStrAlloc(output_bin
->choices
[i
].choice
);
4182 attr
= ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4183 "output-bin-default", NULL
, output_bin
->defchoice
);
4190 if ((duplex
= ppdFindOption(ppd
, "Duplex")) == NULL
)
4191 if ((duplex
= ppdFindOption(ppd
, "EFDuplex")) == NULL
)
4192 if ((duplex
= ppdFindOption(ppd
, "EFDuplexing")) == NULL
)
4193 if ((duplex
= ppdFindOption(ppd
, "KD03Duplex")) == NULL
)
4194 duplex
= ppdFindOption(ppd
, "JCLDuplex");
4196 if (duplex
&& duplex
->num_choices
> 1 &&
4197 !ppdInstallableConflict(ppd
, duplex
->keyword
, "DuplexTumble"))
4199 p
->type
|= CUPS_PRINTER_DUPLEX
;
4201 ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4202 "sides-supported", 3, NULL
, sides
);
4204 if (!strcasecmp(duplex
->defchoice
, "DuplexTumble"))
4205 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4206 "sides-default", NULL
, "two-sided-short-edge");
4207 else if (!strcasecmp(duplex
->defchoice
, "DuplexNoTumble"))
4208 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4209 "sides-default", NULL
, "two-sided-long-edge");
4211 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4212 "sides-default", NULL
, "one-sided");
4215 if (ppdFindOption(ppd
, "Collate") != NULL
)
4216 p
->type
|= CUPS_PRINTER_COLLATE
;
4218 if (ppdFindOption(ppd
, "StapleLocation") != NULL
)
4220 p
->type
|= CUPS_PRINTER_STAPLE
;
4221 finishings
[num_finishings
++] = IPP_FINISHINGS_STAPLE
;
4224 if (ppdFindOption(ppd
, "BindEdge") != NULL
)
4226 p
->type
|= CUPS_PRINTER_BIND
;
4227 finishings
[num_finishings
++] = IPP_FINISHINGS_BIND
;
4230 for (i
= 0; i
< ppd
->num_sizes
; i
++)
4231 if (ppd
->sizes
[i
].length
> 1728)
4232 p
->type
|= CUPS_PRINTER_LARGE
;
4233 else if (ppd
->sizes
[i
].length
> 1008)
4234 p
->type
|= CUPS_PRINTER_MEDIUM
;
4236 p
->type
|= CUPS_PRINTER_SMALL
;
4238 if ((ppd_attr
= ppdFindAttr(ppd
, "APICADriver", NULL
)) != NULL
&&
4239 ppd_attr
->value
&& !strcasecmp(ppd_attr
->value
, "true"))
4241 if ((ppd_attr
= ppdFindAttr(ppd
, "APScannerOnly", NULL
)) != NULL
&&
4242 ppd_attr
->value
&& !strcasecmp(ppd_attr
->value
, "true"))
4243 p
->type
|= CUPS_PRINTER_SCANNER
;
4245 p
->type
|= CUPS_PRINTER_MFP
;
4249 * Add a filter from application/vnd.cups-raw to printer/name to
4250 * handle "raw" printing by users.
4253 add_string_array(&(p
->filters
), "application/vnd.cups-raw 0 -");
4256 * Add any pre-filters in the PPD file...
4259 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsPreFilter", NULL
)) != NULL
)
4261 for (; ppd_attr
; ppd_attr
= ppdFindNextAttr(ppd
, "cupsPreFilter", NULL
))
4262 if (ppd_attr
->value
)
4263 add_string_array(&(p
->pre_filters
), ppd_attr
->value
);
4267 * Add any filters in the PPD file...
4270 DEBUG_printf(("ppd->num_filters = %d\n", ppd
->num_filters
));
4271 for (i
= 0; i
< ppd
->num_filters
; i
++)
4273 DEBUG_printf(("ppd->filters[%d] = \"%s\"\n", i
, ppd
->filters
[i
]));
4274 add_string_array(&(p
->filters
), ppd
->filters
[i
]);
4276 if (!strncasecmp(ppd
->filters
[i
], "application/vnd.cups-command", 28) &&
4277 isspace(ppd
->filters
[i
][28] & 255))
4278 p
->type
|= CUPS_PRINTER_COMMANDS
;
4281 if (ppd
->num_filters
== 0)
4284 * If there are no filters, add PostScript printing filters.
4287 add_string_array(&(p
->filters
),
4288 "application/vnd.cups-command 0 commandtops");
4289 add_string_array(&(p
->filters
),
4290 "application/vnd.cups-postscript 0 -");
4292 p
->type
|= CUPS_PRINTER_COMMANDS
;
4294 else if (!(p
->type
& CUPS_PRINTER_COMMANDS
))
4297 * See if this is a PostScript device without a command filter...
4300 for (i
= 0; i
< ppd
->num_filters
; i
++)
4301 if (!strncasecmp(ppd
->filters
[i
],
4302 "application/vnd.cups-postscript", 31) &&
4303 isspace(ppd
->filters
[i
][31] & 255))
4306 if (i
< ppd
->num_filters
)
4309 * Add the generic PostScript command filter...
4312 add_string_array(&(p
->filters
),
4313 "application/vnd.cups-command 0 commandtops");
4314 p
->type
|= CUPS_PRINTER_COMMANDS
;
4318 if (p
->type
& CUPS_PRINTER_COMMANDS
)
4320 char *commands
, /* Copy of commands */
4321 *start
, /* Start of name */
4322 *end
; /* End of name */
4323 int count
; /* Number of commands */
4326 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsCommands", NULL
)) != NULL
&&
4327 ppd_attr
->value
&& ppd_attr
->value
[0])
4329 for (count
= 0, start
= ppd_attr
->value
; *start
; count
++)
4331 while (isspace(*start
& 255))
4337 while (*start
&& !isspace(*start
& 255))
4347 * Make a copy of the commands string and count how many ...
4350 attr
= ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4351 "printer-commands", count
, NULL
, NULL
);
4353 commands
= strdup(ppd_attr
->value
);
4355 for (count
= 0, start
= commands
; *start
; count
++)
4357 while (isspace(*start
& 255))
4364 while (*end
&& !isspace(*end
& 255))
4370 attr
->values
[count
].string
.text
= _cupsStrAlloc(start
);
4380 * Add the standard list of commands...
4383 ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4385 (int)(sizeof(standard_commands
) /
4386 sizeof(standard_commands
[0])), NULL
,
4393 * No commands supported...
4396 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4397 "printer-commands", NULL
, "none");
4401 * Show current and available port monitors for this printer...
4404 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "port-monitor",
4405 NULL
, p
->port_monitor
? p
->port_monitor
: "none");
4407 for (i
= 1, ppd_attr
= ppdFindAttr(ppd
, "cupsPortMonitor", NULL
);
4409 i
++, ppd_attr
= ppdFindNextAttr(ppd
, "cupsPortMonitor", NULL
));
4413 if (strstr(ppd
->protocols
, "TBCP"))
4415 else if (strstr(ppd
->protocols
, "BCP"))
4419 attr
= ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
4420 "port-monitor-supported", i
, NULL
, NULL
);
4422 attr
->values
[0].string
.text
= _cupsStrAlloc("none");
4424 for (i
= 1, ppd_attr
= ppdFindAttr(ppd
, "cupsPortMonitor", NULL
);
4426 i
++, ppd_attr
= ppdFindNextAttr(ppd
, "cupsPortMonitor", NULL
))
4427 attr
->values
[i
].string
.text
= _cupsStrAlloc(ppd_attr
->value
);
4431 if (strstr(ppd
->protocols
, "TBCP"))
4432 attr
->values
[i
].string
.text
= _cupsStrAlloc("tbcp");
4433 else if (strstr(ppd
->protocols
, "BCP"))
4434 attr
->values
[i
].string
.text
= _cupsStrAlloc("bcp");
4438 cupsdSetString(&p
->product
, ppd
->product
);
4439 #endif /* HAVE_DNSSD */
4441 if (ppdFindAttr(ppd
, "APRemoteQueueID", NULL
))
4442 p
->type
|= CUPS_PRINTER_REMOTE
;
4445 * Close the PPD and set the type...
4450 else if (!access(ppd_name
, 0))
4452 int pline
; /* PPD line number */
4453 ppd_status_t pstatus
; /* PPD load status */
4456 pstatus
= ppdLastError(&pline
);
4458 cupsdLogMessage(CUPSD_LOG_ERROR
, "PPD file for %s cannot be loaded!",
4461 if (pstatus
<= PPD_ALLOC_ERROR
)
4462 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s", strerror(errno
));
4464 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s on line %d.",
4465 ppdErrorString(pstatus
), pline
);
4467 cupsdLogMessage(CUPSD_LOG_INFO
,
4468 "Hint: Run \"cupstestppd %s\" and fix any errors.",
4472 * Add a filter from application/vnd.cups-raw to printer/name to
4473 * handle "raw" printing by users.
4476 add_string_array(&(p
->filters
), "application/vnd.cups-raw 0 -");
4479 * Add a PostScript filter, since this is still possibly PS printer.
4482 add_string_array(&(p
->filters
), "application/vnd.cups-postscript 0 -");
4487 * If we have an interface script, add a filter entry for it...
4490 char interface
[1024]; /* Interface script */
4493 snprintf(interface
, sizeof(interface
), "%s/interfaces/%s", ServerRoot
,
4495 if (!access(interface
, X_OK
))
4498 * Yes, we have a System V style interface script; use it!
4501 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4502 "printer-make-and-model", NULL
,
4503 "Local System V Printer");
4505 snprintf(interface
, sizeof(interface
), "*/* 0 %s/interfaces/%s",
4506 ServerRoot
, p
->name
);
4507 add_string_array(&(p
->filters
), interface
);
4509 else if (!strncmp(p
->device_uri
, "ipp://", 6) &&
4510 (strstr(p
->device_uri
, "/printers/") != NULL
||
4511 strstr(p
->device_uri
, "/classes/") != NULL
||
4512 (strstr(p
->device_uri
, "._ipp.") != NULL
&&
4513 !strcmp(p
->device_uri
+ strlen(p
->device_uri
) - 5,
4517 * Tell the client this is really a hard-wired remote printer.
4520 p
->type
|= CUPS_PRINTER_REMOTE
;
4523 * Point the printer-uri-supported attribute to the
4527 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
4528 "printer-uri-supported", NULL
, p
->device_uri
);
4531 * Then set the make-and-model accordingly...
4534 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4535 "printer-make-and-model", NULL
, "Remote Printer");
4538 * Print all files directly...
4547 * Otherwise we have neither - treat this as a "dumb" printer
4548 * with no PPD file...
4551 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4552 "printer-make-and-model", NULL
, "Local Raw Printer");
4558 ippAddIntegers(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
4559 "finishings-supported", num_finishings
, finishings
);
4560 ippAddInteger(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
4561 "finishings-default", IPP_FINISHINGS_NONE
);
4563 if (ppd
&& (cache
= cupsFileOpen(cache_name
, "w")) != NULL
)
4566 * Save cached PPD attributes to disk...
4569 cupsdLogMessage(CUPSD_LOG_DEBUG
, "load_ppd: Saving %s...", cache_name
);
4571 p
->ppd_attrs
->state
= IPP_IDLE
;
4573 if (ippWriteIO(cache
, (ipp_iocb_t
)cupsFileWrite
, 1, NULL
,
4574 p
->ppd_attrs
) != IPP_DATA
)
4576 cupsdLogMessage(CUPSD_LOG_ERROR
,
4577 "Unable to save PPD cache file \"%s\" - %s", cache_name
,
4582 cupsFileClose(cache
);
4584 else if (cache_info
.st_mtime
)
4587 * Remove cache file...
4597 * 'write_irix_config()' - Update the config files used by the IRIX
4602 write_irix_config(cupsd_printer_t
*p
) /* I - Printer to update */
4604 char filename
[1024]; /* Interface script filename */
4605 cups_file_t
*fp
; /* Interface script file */
4606 ipp_attribute_t
*attr
; /* Attribute data */
4610 * Add dummy interface and GUI scripts to fool SGI's "challenged" printing
4611 * tools. First the interface script that tells the tools what kind of
4612 * printer we have...
4615 snprintf(filename
, sizeof(filename
), "/var/spool/lp/interface/%s", p
->name
);
4617 if (p
->type
& CUPS_PRINTER_CLASS
)
4619 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4621 cupsFilePuts(fp
, "#!/bin/sh\n");
4623 if ((attr
= ippFindAttribute(p
->attrs
, "printer-make-and-model",
4624 IPP_TAG_TEXT
)) != NULL
)
4625 cupsFilePrintf(fp
, "NAME=\"%s\"\n", attr
->values
[0].string
.text
);
4626 else if (p
->type
& CUPS_PRINTER_CLASS
)
4627 cupsFilePuts(fp
, "NAME=\"Printer Class\"\n");
4629 cupsFilePuts(fp
, "NAME=\"Remote Destination\"\n");
4631 if (p
->type
& CUPS_PRINTER_COLOR
)
4632 cupsFilePuts(fp
, "TYPE=ColorPostScript\n");
4634 cupsFilePuts(fp
, "TYPE=MonoPostScript\n");
4636 cupsFilePrintf(fp
, "HOSTNAME=%s\n", ServerName
);
4637 cupsFilePrintf(fp
, "HOSTPRINTER=%s\n", p
->name
);
4641 chmod(filename
, 0755);
4642 chown(filename
, User
, Group
);
4646 * Then the member file that tells which device file the queue is connected
4647 * to... Networked printers use "/dev/null" in this file, so that's what
4648 * we use (the actual device URI can confuse some apps...)
4651 snprintf(filename
, sizeof(filename
), "/var/spool/lp/member/%s", p
->name
);
4653 if (p
->type
& CUPS_PRINTER_CLASS
)
4655 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4657 cupsFilePuts(fp
, "/dev/null\n");
4661 chmod(filename
, 0644);
4662 chown(filename
, User
, Group
);
4666 * The gui_interface file is a script or program that launches a GUI
4667 * option panel for the printer, using options specified on the
4668 * command-line in the third argument. The option panel must send
4669 * any printing options to stdout on a single line when the user
4670 * accepts them, or nothing if the user cancels the dialog.
4672 * The default options panel program is /usr/bin/glpoptions, from
4673 * the ESP Print Pro software. You can select another using the
4674 * PrintcapGUI option.
4677 snprintf(filename
, sizeof(filename
), "/var/spool/lp/gui_interface/ELF/%s.gui", p
->name
);
4679 if (p
->type
& CUPS_PRINTER_CLASS
)
4681 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4683 cupsFilePuts(fp
, "#!/bin/sh\n");
4684 cupsFilePrintf(fp
, "%s -d %s -o \"$3\"\n", PrintcapGUI
, p
->name
);
4688 chmod(filename
, 0755);
4689 chown(filename
, User
, Group
);
4693 * The POD config file is needed by the printstatus command to show
4694 * the printer location and device.
4697 snprintf(filename
, sizeof(filename
), "/var/spool/lp/pod/%s.config", p
->name
);
4699 if (p
->type
& CUPS_PRINTER_CLASS
)
4701 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4703 cupsFilePrintf(fp
, "Printer Class | %s\n",
4704 (p
->type
& CUPS_PRINTER_COLOR
) ? "ColorPostScript" : "MonoPostScript");
4705 cupsFilePrintf(fp
, "Printer Model | %s\n", p
->make_model
? p
->make_model
: "");
4706 cupsFilePrintf(fp
, "Location Code | %s\n", p
->location
? p
->location
: "");
4707 cupsFilePrintf(fp
, "Physical Location | %s\n", p
->info
? p
->info
: "");
4708 cupsFilePrintf(fp
, "Port Path | %s\n", p
->device_uri
);
4709 cupsFilePrintf(fp
, "Config Path | /var/spool/lp/pod/%s.config\n", p
->name
);
4710 cupsFilePrintf(fp
, "Active Status Path | /var/spool/lp/pod/%s.status\n", p
->name
);
4711 cupsFilePuts(fp
, "Status Update Wait | 10 seconds\n");
4715 chmod(filename
, 0664);
4716 chown(filename
, User
, Group
);
4722 * 'write_irix_state()' - Update the status files used by IRIX printing
4727 write_irix_state(cupsd_printer_t
*p
) /* I - Printer to update */
4729 char filename
[1024]; /* Interface script filename */
4730 cups_file_t
*fp
; /* Interface script file */
4731 int tag
; /* Status tag value */
4737 * The POD status file is needed for the printstatus window to
4738 * provide the current status of the printer.
4741 snprintf(filename
, sizeof(filename
), "/var/spool/lp/pod/%s.status", p
->name
);
4743 if (p
->type
& CUPS_PRINTER_CLASS
)
4745 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4747 cupsFilePrintf(fp
, "Operational Status | %s\n",
4748 (p
->state
== IPP_PRINTER_IDLE
) ? "Idle" :
4749 (p
->state
== IPP_PRINTER_PROCESSING
) ? "Busy" :
4751 cupsFilePrintf(fp
, "Information | 01 00 00 | %s\n", CUPS_SVERSION
);
4752 cupsFilePrintf(fp
, "Information | 02 00 00 | Device URI: %s\n",
4754 cupsFilePrintf(fp
, "Information | 03 00 00 | %s jobs\n",
4755 p
->accepting
? "Accepting" : "Not accepting");
4756 cupsFilePrintf(fp
, "Information | 04 00 00 | %s\n", p
->state_message
);
4760 chmod(filename
, 0664);
4761 chown(filename
, User
, Group
);
4765 * The activeicons file is needed to provide desktop icons for printers:
4767 * [ quoted from /usr/lib/print/tagit ]
4769 * --- Type of printer tags (base values)
4771 * Dumb=66048 # 0x10200
4772 * DumbColor=66080 # 0x10220
4773 * Raster=66112 # 0x10240
4774 * ColorRaster=66144 # 0x10260
4775 * Plotter=66176 # 0x10280
4776 * PostScript=66208 # 0x102A0
4777 * ColorPostScript=66240 # 0x102C0
4778 * MonoPostScript=66272 # 0x102E0
4780 * --- Printer state modifiers for local printers
4785 * Unknown=3 # 0x3 (Faulted due to unknown reason)
4787 * --- Printer state modifiers for network printers
4791 * NetFaulted=10 # 0xA
4792 * NetUnknown=11 # 0xB (Faulted due to unknown reason)
4795 snprintf(filename
, sizeof(filename
), "/var/spool/lp/activeicons/%s", p
->name
);
4797 if (p
->type
& CUPS_PRINTER_CLASS
)
4799 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4801 if (p
->type
& CUPS_PRINTER_COLOR
)
4806 if (p
->type
& CUPS_PRINTER_REMOTE
)
4809 if (p
->state
== IPP_PRINTER_PROCESSING
)
4812 else if (p
->state
== IPP_PRINTER_STOPPED
)
4815 cupsFilePuts(fp
, "#!/bin/sh\n");
4816 cupsFilePrintf(fp
, "#Tag %d\n", tag
);
4820 chmod(filename
, 0755);
4821 chown(filename
, User
, Group
);
4826 * The default file is needed by the printers window to show
4827 * the default printer.
4830 snprintf(filename
, sizeof(filename
), "/var/spool/lp/default");
4832 if (DefaultPrinter
!= NULL
)
4834 if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4836 cupsFilePrintf(fp
, "%s\n", DefaultPrinter
->name
);
4840 chmod(filename
, 0644);
4841 chown(filename
, User
, Group
);
4851 * 'write_xml_string()' - Write a string with XML escaping.
4855 write_xml_string(cups_file_t
*fp
, /* I - File to write to */
4856 const char *s
) /* I - String to write */
4858 const char *start
; /* Start of current sequence */
4864 for (start
= s
; *s
; s
++)
4869 cupsFileWrite(fp
, start
, s
- start
);
4871 cupsFilePuts(fp
, "&");
4877 cupsFileWrite(fp
, start
, s
- start
);
4879 cupsFilePuts(fp
, "<");
4885 cupsFilePuts(fp
, start
);
4890 * End of "$Id: printers.c 7968 2008-09-19 23:03:01Z mike $".