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-2008 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 * cupsdFindPrinter() - Find a printer in the list.
24 * cupsdFreePrinterUsers() - Free allow/deny users.
25 * cupsdLoadAllPrinters() - Load printers from the printers.conf file.
26 * cupsdRenamePrinter() - Rename a printer.
27 * cupsdSaveAllPrinters() - Save all printer definitions to the
29 * cupsdSetAuthInfoRequired() - Set the required authentication info.
30 * cupsdSetDeviceURI() - Set the device URI for a printer.
31 * cupsdSetPrinterAttr() - Set a printer attribute.
32 * cupsdSetPrinterAttrs() - Set printer attributes based upon the PPD
34 * cupsdSetPrinterReasons() - Set/update the reasons strings.
35 * cupsdSetPrinterState() - Update the current state of a printer.
36 * cupsdStopPrinter() - Stop a printer from printing any jobs...
37 * cupsdUpdatePrinters() - Update printers after a partial reload.
38 * cupsdValidateDest() - Validate a printer/class destination.
39 * cupsdWritePrintcap() - Write a pseudo-printcap file for older
40 * applications that need it...
41 * add_printer_defaults() - Add name-default attributes to the printer
43 * add_printer_filter() - Add a MIME filter for a printer.
44 * add_printer_formats() - Add document-format-supported values for
46 * compare_printers() - Compare two printers.
47 * delete_printer_filters() - Delete all MIME filters for a printer.
48 * write_irix_config() - Update the config files used by the IRIX
50 * write_irix_state() - Update the status files used by IRIX
51 * printing desktop tools.
52 * write_xml_string() - Write a string with XML escaping.
56 * Include necessary headers...
67 static void add_printer_defaults(cupsd_printer_t
*p
);
68 static void add_printer_filter(cupsd_printer_t
*p
, mime_type_t
*type
,
70 static void add_printer_formats(cupsd_printer_t
*p
);
71 static void add_string_array(cups_array_t
**a
, const char *s
);
72 static int compare_printers(void *first
, void *second
, void *data
);
73 static void delete_printer_filters(cupsd_printer_t
*p
);
74 static void delete_string_array(cups_array_t
**a
);
75 static void load_ppd(cupsd_printer_t
*p
);
77 static void write_irix_config(cupsd_printer_t
*p
);
78 static void write_irix_state(cupsd_printer_t
*p
);
80 static void write_xml_string(cups_file_t
*fp
, const char *s
);
84 * 'cupsdAddPrinter()' - Add a printer to the system.
87 cupsd_printer_t
* /* O - New printer */
88 cupsdAddPrinter(const char *name
) /* I - Name of printer */
90 cupsd_printer_t
*p
; /* New printer */
94 * Range check input...
97 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAddPrinter(\"%s\")", name
);
100 * Create a new printer entity...
103 if ((p
= calloc(1, sizeof(cupsd_printer_t
))) == NULL
)
105 cupsdLogMessage(CUPSD_LOG_CRIT
, "Unable to allocate memory for printer - %s",
110 cupsdSetString(&p
->name
, name
);
111 cupsdSetString(&p
->info
, name
);
112 cupsdSetString(&p
->hostname
, ServerName
);
114 cupsdSetStringf(&p
->uri
, "ipp://%s:%d/printers/%s", ServerName
, LocalPort
, name
);
115 cupsdSetDeviceURI(p
, "file:/dev/null");
117 p
->state
= IPP_PRINTER_STOPPED
;
118 p
->state_time
= time(NULL
);
120 p
->shared
= DefaultShared
;
121 p
->filetype
= mimeAddType(MimeDatabase
, "printer", name
);
123 cupsdSetString(&p
->job_sheets
[0], "none");
124 cupsdSetString(&p
->job_sheets
[1], "none");
126 cupsdSetString(&p
->error_policy
, ErrorPolicy
);
127 cupsdSetString(&p
->op_policy
, DefaultPolicy
);
129 p
->op_policy_ptr
= DefaultPolicyPtr
;
131 if (MaxPrinterHistory
)
132 p
->history
= calloc(MaxPrinterHistory
, sizeof(ipp_t
*));
135 * Insert the printer in the printer list alphabetically...
139 Printers
= cupsArrayNew(compare_printers
, NULL
);
141 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
142 "cupsdAddPrinter: Adding %s to Printers", p
->name
);
143 cupsArrayAdd(Printers
, p
);
145 if (!ImplicitPrinters
)
146 ImplicitPrinters
= cupsArrayNew(compare_printers
, NULL
);
149 * Return the new printer...
157 * 'cupsdAddPrinterHistory()' - Add the current printer state to the history.
161 cupsdAddPrinterHistory(
162 cupsd_printer_t
*p
) /* I - Printer */
164 ipp_t
*history
; /* History collection */
168 * Stop early if we aren't keeping history data...
171 if (MaxPrinterHistory
<= 0)
175 * Retire old history data as needed...
178 p
->sequence_number
++;
180 if (p
->num_history
>= MaxPrinterHistory
)
183 ippDelete(p
->history
[0]);
184 memmove(p
->history
, p
->history
+ 1, p
->num_history
* sizeof(ipp_t
*));
188 * Create a collection containing the current printer-state, printer-up-time,
189 * printer-state-message, and printer-state-reasons attributes.
193 ippAddInteger(history
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
195 ippAddBoolean(history
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs",
197 ippAddBoolean(history
, IPP_TAG_PRINTER
, "printer-is-shared", p
->shared
);
198 ippAddString(history
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-state-message",
199 NULL
, p
->state_message
);
202 ippAddString(history
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
203 "com.apple.print.recoverable-message", NULL
, p
->recoverable
);
204 #endif /* __APPLE__ */
205 if (p
->num_reasons
== 0)
206 ippAddString(history
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
207 "printer-state-reasons", NULL
,
208 p
->state
== IPP_PRINTER_STOPPED
? "paused" : "none");
210 ippAddStrings(history
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
211 "printer-state-reasons", p
->num_reasons
, NULL
,
212 (const char * const *)p
->reasons
);
213 ippAddInteger(history
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
214 "printer-state-change-time", p
->state_time
);
215 ippAddInteger(history
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
216 "printer-state-sequence-number", p
->sequence_number
);
218 p
->history
[p
->num_history
] = history
;
224 * 'cupsdAddPrinterUser()' - Add a user to the ACL.
229 cupsd_printer_t
*p
, /* I - Printer */
230 const char *username
) /* I - User */
232 const char **temp
; /* Temporary array pointer */
238 if (p
->num_users
== 0)
239 temp
= malloc(sizeof(char **));
241 temp
= realloc(p
->users
, sizeof(char **) * (p
->num_users
+ 1));
247 temp
+= p
->num_users
;
249 if ((*temp
= strdup(username
)) != NULL
)
255 * 'cupsdCreateCommonData()' - Create the common printer data.
259 cupsdCreateCommonData(void)
261 int i
; /* Looping var */
262 ipp_attribute_t
*attr
; /* Attribute data */
263 cups_dir_t
*dir
; /* Notifier directory */
264 cups_dentry_t
*dent
; /* Notifier directory entry */
265 cups_array_t
*notifiers
; /* Notifier array */
266 char filename
[1024], /* Filename */
267 *notifier
; /* Current notifier */
268 cupsd_policy_t
*p
; /* Current policy */
269 static const int nups
[] = /* number-up-supported values */
270 { 1, 2, 4, 6, 9, 16 };
271 static const int orients
[4] =/* orientation-requested-supported values */
275 IPP_REVERSE_LANDSCAPE
,
278 static const char * const holds
[] = /* job-hold-until-supported values */
289 static const char * const versions
[] =/* ipp-versions-supported values */
294 static const int ops
[] = /* operations-supported values */
301 IPP_GET_JOB_ATTRIBUTES
,
303 IPP_GET_PRINTER_ATTRIBUTES
,
309 IPP_SET_JOB_ATTRIBUTES
,
310 IPP_CREATE_PRINTER_SUBSCRIPTION
,
311 IPP_CREATE_JOB_SUBSCRIPTION
,
312 IPP_GET_SUBSCRIPTION_ATTRIBUTES
,
313 IPP_GET_SUBSCRIPTIONS
,
314 IPP_RENEW_SUBSCRIPTION
,
315 IPP_CANCEL_SUBSCRIPTION
,
316 IPP_GET_NOTIFICATIONS
,
320 IPP_RELEASE_HELD_NEW_JOBS
,
334 CUPS_AUTHENTICATE_JOB
,
339 static const char * const charsets
[] =/* charset-supported values */
344 static const char * const compressions
[] =
345 { /* document-compression-supported values */
349 #endif /* HAVE_LIBZ */
351 static const char * const multiple_document_handling
[] =
352 { /* multiple-document-handling-supported values */
353 "separate-documents-uncollated-copies",
354 "separate-documents-collated-copies"
356 static const char * const errors
[] = /* printer-error-policy-supported values */
363 static const char * const notify_attrs
[] =
364 { /* notify-attributes-supported values */
365 "printer-state-change-time",
366 "notify-lease-expiration-time",
367 "notify-subscriber-user-name"
369 static const char * const notify_events
[] =
370 { /* notify-events-supported values */
372 "job-config-changed",
379 "printer-config-changed",
381 "printer-finishings-changed",
382 "printer-media-changed",
386 "printer-state-changed",
393 static const char * const job_settable
[] =
394 { /* job-settable-attributes-supported */
400 "multiple-document-handling",
402 "orientation-requested",
405 "printer-resolution",
411 ippDelete(CommonData
);
413 CommonData
= ippNew();
416 * This list of attributes is sorted to improve performance when the
417 * client provides a requested-attributes attribute...
420 /* charset-configured */
421 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_CHARSET
| IPP_TAG_COPY
,
422 "charset-configured", NULL
, DefaultCharset
);
424 /* charset-supported */
425 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_CHARSET
| IPP_TAG_COPY
,
426 "charset-supported", sizeof(charsets
) / sizeof(charsets
[0]),
429 /* compression-supported */
430 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
431 "compression-supported",
432 sizeof(compressions
) / sizeof(compressions
[0]),
435 /* copies-supported */
436 ippAddRange(CommonData
, IPP_TAG_PRINTER
, "copies-supported", 1, MaxCopies
);
439 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
| IPP_TAG_COPY
,
440 "cups-version", NULL
, CUPS_SVERSION
+ 6);
442 /* generated-natural-language-supported */
443 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_LANGUAGE
| IPP_TAG_COPY
,
444 "generated-natural-language-supported", NULL
, DefaultLanguage
);
446 /* ipp-versions-supported */
447 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
448 "ipp-versions-supported", sizeof(versions
) / sizeof(versions
[0]),
451 /* job-hold-until-supported */
452 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
453 "job-hold-until-supported", sizeof(holds
) / sizeof(holds
[0]),
456 /* job-priority-supported */
457 ippAddInteger(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
458 "job-priority-supported", 100);
460 /* job-settable-attributes-supported */
461 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
462 "job-settable-attributes-supported",
463 sizeof(job_settable
) / sizeof(job_settable
[0]),
466 /* job-sheets-supported */
467 if (cupsArrayCount(Banners
) > 0)
470 * Setup the job-sheets-supported attribute...
473 if (Classification
&& !ClassifyOverride
)
474 attr
= ippAddString(CommonData
, IPP_TAG_PRINTER
,
475 IPP_TAG_NAME
| IPP_TAG_COPY
,
476 "job-sheets-supported", NULL
, Classification
);
478 attr
= ippAddStrings(CommonData
, IPP_TAG_PRINTER
,
479 IPP_TAG_NAME
| IPP_TAG_COPY
,
480 "job-sheets-supported", cupsArrayCount(Banners
) + 1,
484 cupsdLogMessage(CUPSD_LOG_EMERG
,
485 "Unable to allocate memory for "
486 "job-sheets-supported attribute: %s!", strerror(errno
));
487 else if (!Classification
|| ClassifyOverride
)
489 cupsd_banner_t
*banner
; /* Current banner */
492 attr
->values
[0].string
.text
= _cupsStrAlloc("none");
494 for (i
= 1, banner
= (cupsd_banner_t
*)cupsArrayFirst(Banners
);
496 i
++, banner
= (cupsd_banner_t
*)cupsArrayNext(Banners
))
497 attr
->values
[i
].string
.text
= banner
->name
;
501 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_NAME
| IPP_TAG_COPY
,
502 "job-sheets-supported", NULL
, "none");
504 /* multiple-document-handling-supported */
505 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
506 "multiple-document-handling-supported",
507 sizeof(multiple_document_handling
) /
508 sizeof(multiple_document_handling
[0]), NULL
,
509 multiple_document_handling
);
511 /* multiple-document-jobs-supported */
512 ippAddBoolean(CommonData
, IPP_TAG_PRINTER
,
513 "multiple-document-jobs-supported", 1);
515 /* multiple-operation-time-out */
516 ippAddInteger(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
517 "multiple-operation-time-out", 60);
519 /* natural-language-configured */
520 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_LANGUAGE
| IPP_TAG_COPY
,
521 "natural-language-configured", NULL
, DefaultLanguage
);
523 /* notify-attributes-supported */
524 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
525 "notify-attributes-supported",
526 (int)(sizeof(notify_attrs
) / sizeof(notify_attrs
[0])),
529 /* notify-lease-duration-supported */
530 ippAddRange(CommonData
, IPP_TAG_PRINTER
,
531 "notify-lease-duration-supported", 0,
532 MaxLeaseDuration
? MaxLeaseDuration
: 2147483647);
534 /* notify-max-events-supported */
535 ippAddInteger(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
536 "notify-max-events-supported", MaxEvents
);
538 /* notify-events-supported */
539 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
540 "notify-events-supported",
541 (int)(sizeof(notify_events
) / sizeof(notify_events
[0])),
542 NULL
, notify_events
);
544 /* notify-pull-method-supported */
545 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
546 "notify-pull-method-supported", NULL
, "ippget");
548 /* notify-schemes-supported */
549 snprintf(filename
, sizeof(filename
), "%s/notifier", ServerBin
);
550 if ((dir
= cupsDirOpen(filename
)) != NULL
)
552 notifiers
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
554 while ((dent
= cupsDirRead(dir
)) != NULL
)
555 if (S_ISREG(dent
->fileinfo
.st_mode
) &&
556 (dent
->fileinfo
.st_mode
& S_IXOTH
) != 0)
557 cupsArrayAdd(notifiers
, _cupsStrAlloc(dent
->filename
));
559 if (cupsArrayCount(notifiers
) > 0)
561 attr
= ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
562 "notify-schemes-supported",
563 cupsArrayCount(notifiers
), NULL
, NULL
);
565 for (i
= 0, notifier
= (char *)cupsArrayFirst(notifiers
);
567 i
++, notifier
= (char *)cupsArrayNext(notifiers
))
568 attr
->values
[i
].string
.text
= notifier
;
571 cupsArrayDelete(notifiers
);
575 /* number-up-supported */
576 ippAddIntegers(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
577 "number-up-supported", sizeof(nups
) / sizeof(nups
[0]), nups
);
579 /* operations-supported */
580 ippAddIntegers(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
581 "operations-supported",
582 sizeof(ops
) / sizeof(ops
[0]) + JobFiles
- 1, ops
);
584 /* orientation-requested-supported */
585 ippAddIntegers(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
586 "orientation-requested-supported", 4, orients
);
588 /* page-ranges-supported */
589 ippAddBoolean(CommonData
, IPP_TAG_PRINTER
, "page-ranges-supported", 1);
591 /* pdf-override-supported */
592 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
593 "pdl-override-supported", NULL
, "not-attempted");
595 /* printer-error-policy-supported */
596 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_NAME
| IPP_TAG_COPY
,
597 "printer-error-policy-supported",
598 sizeof(errors
) / sizeof(errors
[0]), NULL
, errors
);
600 /* printer-op-policy-supported */
601 attr
= ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_NAME
| IPP_TAG_COPY
,
602 "printer-op-policy-supported", cupsArrayCount(Policies
),
604 for (i
= 0, p
= (cupsd_policy_t
*)cupsArrayFirst(Policies
);
606 i
++, p
= (cupsd_policy_t
*)cupsArrayNext(Policies
))
607 attr
->values
[i
].string
.text
= p
->name
;
609 /* server-is-sharing-printers */
610 ippAddBoolean(CommonData
, IPP_TAG_PRINTER
, "server-is-sharing-printers",
611 BrowseLocalProtocols
!= 0 && Browsing
);
616 * 'cupsdDeleteAllPrinters()' - Delete all printers from the system.
620 cupsdDeleteAllPrinters(void)
622 cupsd_printer_t
*p
; /* Pointer to current printer/class */
625 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
627 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
628 cupsdDeletePrinter(p
, 0);
633 * 'cupsdDeletePrinter()' - Delete a printer from the system.
638 cupsd_printer_t
*p
, /* I - Printer to delete */
639 int update
) /* I - Update printers.conf? */
641 int i
; /* Looping var */
643 char filename
[1024]; /* Interface script filename */
647 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdDeletePrinter(p=%p(%s), update=%d)",
651 * Save the current position in the Printers array...
654 cupsArraySave(Printers
);
657 * Stop printing on this printer...
660 cupsdStopPrinter(p
, update
);
663 * If this printer is the next for browsing, point to the next one...
668 cupsArrayFind(Printers
, p
);
669 BrowseNext
= (cupsd_printer_t
*)cupsArrayNext(Printers
);
673 * Remove the printer from the list...
676 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
677 "cupsdDeletePrinter: Removing %s from Printers", p
->name
);
678 cupsArrayRemove(Printers
, p
);
680 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
682 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
683 "cupsdDeletePrinter: Removing %s from ImplicitPrinters",
685 cupsArrayRemove(ImplicitPrinters
, p
);
689 * Remove the dummy interface/icon/option files under IRIX...
693 snprintf(filename
, sizeof(filename
), "/var/spool/lp/interface/%s", p
->name
);
696 snprintf(filename
, sizeof(filename
), "/var/spool/lp/gui_interface/ELF/%s.gui",
700 snprintf(filename
, sizeof(filename
), "/var/spool/lp/activeicons/%s", p
->name
);
703 snprintf(filename
, sizeof(filename
), "/var/spool/lp/pod/%s.config", p
->name
);
706 snprintf(filename
, sizeof(filename
), "/var/spool/lp/pod/%s.status", p
->name
);
709 snprintf(filename
, sizeof(filename
), "/var/spool/lp/member/%s", p
->name
);
714 * If p is the default printer, assign a different one...
717 if (p
== DefaultPrinter
)
719 DefaultPrinter
= NULL
;
721 if (UseNetworkDefault
)
724 * Find the first network default printer and use it...
727 cupsd_printer_t
*dp
; /* New default printer */
730 for (dp
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
732 dp
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
733 if (dp
!= p
&& (dp
->type
& CUPS_PRINTER_DEFAULT
))
742 * Remove this printer from any classes...
745 if (!(p
->type
& CUPS_PRINTER_IMPLICIT
))
747 cupsdDeletePrinterFromClasses(p
);
750 * Deregister from any browse protocols...
753 cupsdDeregisterPrinter(p
, 1);
757 * Free all memory used by the printer...
760 if (p
->printers
!= NULL
)
763 if (MaxPrinterHistory
)
765 for (i
= 0; i
< p
->num_history
; i
++)
766 ippDelete(p
->history
[i
]);
771 for (i
= 0; i
< p
->num_reasons
; i
++)
772 _cupsStrFree(p
->reasons
[i
]);
775 ippDelete(p
->ppd_attrs
);
777 delete_printer_filters(p
);
779 mimeDeleteType(MimeDatabase
, p
->filetype
);
780 mimeDeleteType(MimeDatabase
, p
->prefiltertype
);
782 delete_string_array(&(p
->filters
));
783 delete_string_array(&(p
->pre_filters
));
785 cupsdFreePrinterUsers(p
);
788 cupsdClearString(&p
->uri
);
789 cupsdClearString(&p
->hostname
);
790 cupsdClearString(&p
->name
);
791 cupsdClearString(&p
->location
);
792 cupsdClearString(&p
->make_model
);
793 cupsdClearString(&p
->info
);
794 cupsdClearString(&p
->job_sheets
[0]);
795 cupsdClearString(&p
->job_sheets
[1]);
796 cupsdClearString(&p
->device_uri
);
797 cupsdClearString(&p
->sanitized_device_uri
);
798 cupsdClearString(&p
->port_monitor
);
799 cupsdClearString(&p
->op_policy
);
800 cupsdClearString(&p
->error_policy
);
802 cupsdClearString(&p
->alert
);
803 cupsdClearString(&p
->alert_description
);
806 cupsdClearString(&p
->product
);
807 cupsdClearString(&p
->pdl
);
808 #endif /* HAVE_DNSSD */
810 cupsArrayDelete(p
->filetypes
);
813 free(p
->browse_attrs
);
816 cupsdClearString(&p
->recoverable
);
817 #endif /* __APPLE__ */
819 cupsFreeOptions(p
->num_options
, p
->options
);
824 * Restore the previous position in the Printers array...
827 cupsArrayRestore(Printers
);
832 * 'cupsdFindDest()' - Find a destination in the list.
835 cupsd_printer_t
* /* O - Destination in list */
836 cupsdFindDest(const char *name
) /* I - Name of printer or class to find */
838 cupsd_printer_t key
; /* Search key */
841 key
.name
= (char *)name
;
842 return ((cupsd_printer_t
*)cupsArrayFind(Printers
, &key
));
847 * 'cupsdFindPrinter()' - Find a printer in the list.
850 cupsd_printer_t
* /* O - Printer in list */
851 cupsdFindPrinter(const char *name
) /* I - Name of printer to find */
853 cupsd_printer_t
*p
; /* Printer in list */
856 if ((p
= cupsdFindDest(name
)) != NULL
&& (p
->type
& CUPS_PRINTER_CLASS
))
864 * 'cupsdFreePrinterUsers()' - Free allow/deny users.
868 cupsdFreePrinterUsers(
869 cupsd_printer_t
*p
) /* I - Printer */
871 int i
; /* Looping var */
874 if (!p
|| !p
->num_users
)
877 for (i
= 0; i
< p
->num_users
; i
++)
878 free((void *)p
->users
[i
]);
888 * 'cupsdLoadAllPrinters()' - Load printers from the printers.conf file.
892 cupsdLoadAllPrinters(void)
894 cups_file_t
*fp
; /* printers.conf file */
895 int linenum
; /* Current line number */
896 char line
[1024], /* Line from file */
897 *value
, /* Pointer to value */
898 *valueptr
; /* Pointer into value */
899 cupsd_printer_t
*p
; /* Current printer */
903 * Open the printers.conf file...
906 snprintf(line
, sizeof(line
), "%s/printers.conf", ServerRoot
);
907 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
910 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open %s - %s", line
,
916 * Read printer configurations until we hit EOF...
922 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
925 * Decode the directive...
928 if (!strcasecmp(line
, "<Printer") ||
929 !strcasecmp(line
, "<DefaultPrinter"))
932 * <Printer name> or <DefaultPrinter name>
935 if (p
== NULL
&& value
)
938 * Add the printer and a base file type...
941 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Loading printer %s...", value
);
943 p
= cupsdAddPrinter(value
);
945 p
->state
= IPP_PRINTER_IDLE
;
948 * Set the default printer as needed...
951 if (!strcasecmp(line
, "<DefaultPrinter"))
955 cupsdLogMessage(CUPSD_LOG_ERROR
,
956 "Syntax error on line %d of printers.conf.", linenum
);
958 else if (!strcasecmp(line
, "</Printer>"))
963 * Close out the current printer...
966 cupsdSetPrinterAttrs(p
);
967 cupsdAddPrinterHistory(p
);
969 if (strncmp(p
->device_uri
, "file:", 5) &&
970 p
->state
!= IPP_PRINTER_STOPPED
)
973 * See if the backend exists...
976 snprintf(line
, sizeof(line
), "%s/backend/%s", ServerBin
,
979 if ((valueptr
= strchr(line
+ strlen(ServerBin
), ':')) != NULL
)
980 *valueptr
= '\0'; /* Chop everything but URI scheme */
985 * Backend does not exist, stop printer...
988 p
->state
= IPP_PRINTER_STOPPED
;
989 snprintf(p
->state_message
, sizeof(p
->state_message
),
990 "Backend %s does not exist!", line
);
997 cupsdLogMessage(CUPSD_LOG_ERROR
,
998 "Syntax error on line %d of printers.conf.", linenum
);
1002 cupsdLogMessage(CUPSD_LOG_ERROR
,
1003 "Syntax error on line %d of printers.conf.", linenum
);
1005 else if (!strcasecmp(line
, "AuthInfoRequired"))
1007 if (!cupsdSetAuthInfoRequired(p
, value
, NULL
))
1008 cupsdLogMessage(CUPSD_LOG_ERROR
,
1009 "Bad AuthInfoRequired on line %d of printers.conf.",
1012 else if (!strcasecmp(line
, "Info"))
1015 cupsdSetString(&p
->info
, value
);
1017 else if (!strcasecmp(line
, "MakeModel"))
1020 cupsdSetString(&p
->make_model
, value
);
1022 else if (!strcasecmp(line
, "Location"))
1025 cupsdSetString(&p
->location
, value
);
1027 else if (!strcasecmp(line
, "DeviceURI"))
1030 cupsdSetDeviceURI(p
, value
);
1032 cupsdLogMessage(CUPSD_LOG_ERROR
,
1033 "Syntax error on line %d of printers.conf.", linenum
);
1035 else if (!strcasecmp(line
, "Option") && value
)
1041 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
1044 cupsdLogMessage(CUPSD_LOG_ERROR
,
1045 "Syntax error on line %d of printers.conf.", linenum
);
1048 for (; *valueptr
&& isspace(*valueptr
& 255); *valueptr
++ = '\0');
1050 p
->num_options
= cupsAddOption(value
, valueptr
, p
->num_options
,
1054 else if (!strcasecmp(line
, "PortMonitor"))
1056 if (value
&& strcmp(value
, "none"))
1057 cupsdSetString(&p
->port_monitor
, value
);
1059 cupsdClearString(&p
->port_monitor
);
1061 cupsdLogMessage(CUPSD_LOG_ERROR
,
1062 "Syntax error on line %d of printers.conf.", linenum
);
1064 else if (!strcasecmp(line
, "Reason"))
1067 p
->num_reasons
< (int)(sizeof(p
->reasons
) / sizeof(p
->reasons
[0])))
1069 p
->reasons
[p
->num_reasons
] = _cupsStrAlloc(value
);
1073 cupsdLogMessage(CUPSD_LOG_ERROR
,
1074 "Syntax error on line %d of printers.conf.", linenum
);
1076 else if (!strcasecmp(line
, "State"))
1079 * Set the initial queue state...
1082 if (value
&& !strcasecmp(value
, "idle"))
1083 p
->state
= IPP_PRINTER_IDLE
;
1084 else if (value
&& !strcasecmp(value
, "stopped"))
1085 p
->state
= IPP_PRINTER_STOPPED
;
1087 cupsdLogMessage(CUPSD_LOG_ERROR
,
1088 "Syntax error on line %d of printers.conf.", linenum
);
1090 else if (!strcasecmp(line
, "StateMessage"))
1093 * Set the initial queue state message...
1097 strlcpy(p
->state_message
, value
, sizeof(p
->state_message
));
1099 else if (!strcasecmp(line
, "StateTime"))
1102 * Set the state time...
1106 p
->state_time
= atoi(value
);
1108 else if (!strcasecmp(line
, "Accepting"))
1111 * Set the initial accepting state...
1115 (!strcasecmp(value
, "yes") ||
1116 !strcasecmp(value
, "on") ||
1117 !strcasecmp(value
, "true")))
1120 (!strcasecmp(value
, "no") ||
1121 !strcasecmp(value
, "off") ||
1122 !strcasecmp(value
, "false")))
1125 cupsdLogMessage(CUPSD_LOG_ERROR
,
1126 "Syntax error on line %d of printers.conf.", linenum
);
1128 else if (!strcasecmp(line
, "Type"))
1131 p
->type
= atoi(value
);
1133 cupsdLogMessage(CUPSD_LOG_ERROR
,
1134 "Syntax error on line %d of printers.conf.", linenum
);
1136 else if (!strcasecmp(line
, "Product"))
1141 p
->product
= _cupsStrAlloc(value
);
1142 #endif /* HAVE_DNSSD */
1145 cupsdLogMessage(CUPSD_LOG_ERROR
,
1146 "Syntax error on line %d of printers.conf.", linenum
);
1148 else if (!strcasecmp(line
, "Filter"))
1153 p
->filters
= cupsArrayNew(NULL
, NULL
);
1155 cupsArrayAdd(p
->filters
, _cupsStrAlloc(value
));
1158 cupsdLogMessage(CUPSD_LOG_ERROR
,
1159 "Syntax error on line %d of printers.conf.", linenum
);
1161 else if (!strcasecmp(line
, "PreFilter"))
1165 if (!p
->pre_filters
)
1166 p
->pre_filters
= cupsArrayNew(NULL
, NULL
);
1168 cupsArrayAdd(p
->pre_filters
, _cupsStrAlloc(value
));
1171 cupsdLogMessage(CUPSD_LOG_ERROR
,
1172 "Syntax error on line %d of printers.conf.", linenum
);
1174 else if (!strcasecmp(line
, "Shared"))
1177 * Set the initial shared state...
1181 (!strcasecmp(value
, "yes") ||
1182 !strcasecmp(value
, "on") ||
1183 !strcasecmp(value
, "true")))
1186 (!strcasecmp(value
, "no") ||
1187 !strcasecmp(value
, "off") ||
1188 !strcasecmp(value
, "false")))
1191 cupsdLogMessage(CUPSD_LOG_ERROR
,
1192 "Syntax error on line %d of printers.conf.", linenum
);
1194 else if (!strcasecmp(line
, "JobSheets"))
1197 * Set the initial job sheets...
1202 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
1207 cupsdSetString(&p
->job_sheets
[0], value
);
1209 while (isspace(*valueptr
& 255))
1214 for (value
= valueptr
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
1219 cupsdSetString(&p
->job_sheets
[1], value
);
1223 cupsdLogMessage(CUPSD_LOG_ERROR
,
1224 "Syntax error on line %d of printers.conf.", linenum
);
1226 else if (!strcasecmp(line
, "AllowUser"))
1231 cupsdAddPrinterUser(p
, value
);
1234 cupsdLogMessage(CUPSD_LOG_ERROR
,
1235 "Syntax error on line %d of printers.conf.", linenum
);
1237 else if (!strcasecmp(line
, "DenyUser"))
1242 cupsdAddPrinterUser(p
, value
);
1245 cupsdLogMessage(CUPSD_LOG_ERROR
,
1246 "Syntax error on line %d of printers.conf.", linenum
);
1248 else if (!strcasecmp(line
, "QuotaPeriod"))
1251 p
->quota_period
= atoi(value
);
1253 cupsdLogMessage(CUPSD_LOG_ERROR
,
1254 "Syntax error on line %d of printers.conf.", linenum
);
1256 else if (!strcasecmp(line
, "PageLimit"))
1259 p
->page_limit
= atoi(value
);
1261 cupsdLogMessage(CUPSD_LOG_ERROR
,
1262 "Syntax error on line %d of printers.conf.", linenum
);
1264 else if (!strcasecmp(line
, "KLimit"))
1267 p
->k_limit
= atoi(value
);
1269 cupsdLogMessage(CUPSD_LOG_ERROR
,
1270 "Syntax error on line %d of printers.conf.", linenum
);
1272 else if (!strcasecmp(line
, "OpPolicy"))
1276 cupsd_policy_t
*pol
; /* Policy */
1279 if ((pol
= cupsdFindPolicy(value
)) != NULL
)
1281 cupsdSetString(&p
->op_policy
, value
);
1282 p
->op_policy_ptr
= pol
;
1285 cupsdLogMessage(CUPSD_LOG_ERROR
,
1286 "Bad policy \"%s\" on line %d of printers.conf",
1290 cupsdLogMessage(CUPSD_LOG_ERROR
,
1291 "Syntax error on line %d of printers.conf.", linenum
);
1293 else if (!strcasecmp(line
, "ErrorPolicy"))
1296 cupsdSetString(&p
->error_policy
, value
);
1298 cupsdLogMessage(CUPSD_LOG_ERROR
,
1299 "Syntax error on line %d of printers.conf.", linenum
);
1301 else if (!strcasecmp(line
, "Attribute") && value
)
1303 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
1306 cupsdLogMessage(CUPSD_LOG_ERROR
,
1307 "Syntax error on line %d of printers.conf.", linenum
);
1310 for (; *valueptr
&& isspace(*valueptr
& 255); *valueptr
++ = '\0');
1313 cupsdSetPrinterAttrs(p
);
1315 if (!strcmp(value
, "marker-change-time"))
1316 p
->marker_time
= atoi(valueptr
);
1318 cupsdSetPrinterAttr(p
, value
, valueptr
);
1324 * Something else we don't understand...
1327 cupsdLogMessage(CUPSD_LOG_ERROR
,
1328 "Unknown configuration directive %s on line %d of printers.conf.",
1338 * 'cupsdRenamePrinter()' - Rename a printer.
1343 cupsd_printer_t
*p
, /* I - Printer */
1344 const char *name
) /* I - New name */
1347 * Remove the printer from the array(s) first...
1350 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1351 "cupsdRenamePrinter: Removing %s from Printers", p
->name
);
1352 cupsArrayRemove(Printers
, p
);
1354 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
1356 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1357 "cupsdRenamePrinter: Removing %s from ImplicitPrinters",
1359 cupsArrayRemove(ImplicitPrinters
, p
);
1363 * Rename the printer type...
1366 mimeDeleteType(MimeDatabase
, p
->filetype
);
1367 p
->filetype
= mimeAddType(MimeDatabase
, "printer", name
);
1369 mimeDeleteType(MimeDatabase
, p
->prefiltertype
);
1370 p
->prefiltertype
= mimeAddType(MimeDatabase
, "prefilter", name
);
1373 * Rename the printer...
1376 cupsdSetString(&p
->name
, name
);
1379 * Reset printer attributes...
1382 cupsdSetPrinterAttrs(p
);
1385 * Add the printer back to the printer array(s)...
1388 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1389 "cupsdRenamePrinter: Adding %s to Printers", p
->name
);
1390 cupsArrayAdd(Printers
, p
);
1392 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
1394 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1395 "cupsdRenamePrinter: Adding %s to ImplicitPrinters",
1397 cupsArrayAdd(ImplicitPrinters
, p
);
1403 * 'cupsdSaveAllPrinters()' - Save all printer definitions to the printers.conf
1408 cupsdSaveAllPrinters(void)
1410 int i
; /* Looping var */
1411 cups_file_t
*fp
; /* printers.conf file */
1412 char temp
[1024], /* Temporary string */
1413 backup
[1024], /* printers.conf.O file */
1414 value
[2048], /* Value string */
1415 *ptr
; /* Pointer into value */
1416 cupsd_printer_t
*printer
; /* Current printer class */
1417 time_t curtime
; /* Current time */
1418 struct tm
*curdate
; /* Current date */
1419 cups_option_t
*option
; /* Current option */
1420 ipp_attribute_t
*marker
; /* Current marker attribute */
1424 * Create the printers.conf file...
1427 snprintf(temp
, sizeof(temp
), "%s/printers.conf", ServerRoot
);
1428 snprintf(backup
, sizeof(backup
), "%s/printers.conf.O", ServerRoot
);
1430 if (rename(temp
, backup
))
1432 if (errno
!= ENOENT
)
1433 cupsdLogMessage(CUPSD_LOG_ERROR
,
1434 "Unable to backup printers.conf - %s", strerror(errno
));
1437 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
1439 cupsdLogMessage(CUPSD_LOG_ERROR
,
1440 "Unable to save printers.conf - %s", strerror(errno
));
1442 if (rename(backup
, temp
))
1443 cupsdLogMessage(CUPSD_LOG_ERROR
,
1444 "Unable to restore printers.conf - %s", strerror(errno
));
1448 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving printers.conf...");
1451 * Restrict access to the file...
1454 fchown(cupsFileNumber(fp
), getuid(), Group
);
1455 fchmod(cupsFileNumber(fp
), 0600);
1458 * Write a small header to the file...
1461 curtime
= time(NULL
);
1462 curdate
= localtime(&curtime
);
1463 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
1465 cupsFilePuts(fp
, "# Printer configuration file for " CUPS_SVERSION
"\n");
1466 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
1469 * Write each local printer known to the system...
1472 for (printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1474 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1477 * Skip remote destinations and printer classes...
1480 if ((printer
->type
& CUPS_PRINTER_DISCOVERED
) ||
1481 (printer
->type
& CUPS_PRINTER_CLASS
) ||
1482 (printer
->type
& CUPS_PRINTER_IMPLICIT
))
1486 * Write printers as needed...
1489 if (printer
== DefaultPrinter
)
1490 cupsFilePrintf(fp
, "<DefaultPrinter %s>\n", printer
->name
);
1492 cupsFilePrintf(fp
, "<Printer %s>\n", printer
->name
);
1494 if (printer
->num_auth_info_required
> 0)
1496 switch (printer
->num_auth_info_required
)
1499 strlcpy(value
, printer
->auth_info_required
[0], sizeof(value
));
1503 snprintf(value
, sizeof(value
), "%s,%s",
1504 printer
->auth_info_required
[0],
1505 printer
->auth_info_required
[1]);
1510 snprintf(value
, sizeof(value
), "%s,%s,%s",
1511 printer
->auth_info_required
[0],
1512 printer
->auth_info_required
[1],
1513 printer
->auth_info_required
[2]);
1517 cupsFilePutConf(fp
, "AuthInfoRequired", value
);
1521 cupsFilePutConf(fp
, "Info", printer
->info
);
1523 if (printer
->location
)
1524 cupsFilePutConf(fp
, "Location", printer
->location
);
1526 if (printer
->make_model
)
1527 cupsFilePutConf(fp
, "MakeModel", printer
->make_model
);
1529 cupsFilePutConf(fp
, "DeviceURI", printer
->device_uri
);
1531 if (printer
->port_monitor
)
1532 cupsFilePutConf(fp
, "PortMonitor", printer
->port_monitor
);
1534 if (printer
->state
== IPP_PRINTER_STOPPED
)
1536 cupsFilePuts(fp
, "State Stopped\n");
1538 if (printer
->state_message
)
1539 cupsFilePutConf(fp
, "StateMessage", printer
->state_message
);
1542 cupsFilePuts(fp
, "State Idle\n");
1544 cupsFilePrintf(fp
, "StateTime %d\n", (int)printer
->state_time
);
1546 for (i
= 0; i
< printer
->num_reasons
; i
++)
1547 if (strcmp(printer
->reasons
[i
], "connecting-to-device"))
1548 cupsFilePutConf(fp
, "Reason", printer
->reasons
[i
]);
1550 cupsFilePrintf(fp
, "Type %d\n", printer
->type
);
1553 if (printer
->product
)
1554 cupsFilePutConf(fp
, "Product", printer
->product
);
1555 #endif /* HAVE_DNSSD */
1557 for (ptr
= (char *)cupsArrayFirst(printer
->filters
);
1559 ptr
= (char *)cupsArrayNext(printer
->filters
))
1560 cupsFilePutConf(fp
, "Filter", ptr
);
1562 for (ptr
= (char *)cupsArrayFirst(printer
->pre_filters
);
1564 ptr
= (char *)cupsArrayNext(printer
->pre_filters
))
1565 cupsFilePutConf(fp
, "PreFilter", ptr
);
1567 if (printer
->accepting
)
1568 cupsFilePuts(fp
, "Accepting Yes\n");
1570 cupsFilePuts(fp
, "Accepting No\n");
1572 if (printer
->shared
)
1573 cupsFilePuts(fp
, "Shared Yes\n");
1575 cupsFilePuts(fp
, "Shared No\n");
1577 snprintf(value
, sizeof(value
), "%s %s", printer
->job_sheets
[0],
1578 printer
->job_sheets
[1]);
1579 cupsFilePutConf(fp
, "JobSheets", value
);
1581 cupsFilePrintf(fp
, "QuotaPeriod %d\n", printer
->quota_period
);
1582 cupsFilePrintf(fp
, "PageLimit %d\n", printer
->page_limit
);
1583 cupsFilePrintf(fp
, "KLimit %d\n", printer
->k_limit
);
1585 for (i
= 0; i
< printer
->num_users
; i
++)
1586 cupsFilePutConf(fp
, printer
->deny_users
? "DenyUser" : "AllowUser",
1589 if (printer
->op_policy
)
1590 cupsFilePutConf(fp
, "OpPolicy", printer
->op_policy
);
1591 if (printer
->error_policy
)
1592 cupsFilePutConf(fp
, "ErrorPolicy", printer
->error_policy
);
1594 for (i
= printer
->num_options
, option
= printer
->options
;
1598 snprintf(value
, sizeof(value
), "%s %s", option
->name
, option
->value
);
1599 cupsFilePutConf(fp
, "Option", value
);
1602 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-colors",
1603 IPP_TAG_NAME
)) != NULL
)
1605 snprintf(value
, sizeof(value
), "%s ", marker
->name
);
1607 for (i
= 0, ptr
= value
+ strlen(value
);
1608 i
< marker
->num_values
&& ptr
< (value
+ sizeof(value
) - 1);
1614 strlcpy(ptr
, marker
->values
[i
].string
.text
,
1615 value
+ sizeof(value
) - ptr
);
1620 cupsFilePutConf(fp
, "Attribute", value
);
1623 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-levels",
1624 IPP_TAG_INTEGER
)) != NULL
)
1626 cupsFilePrintf(fp
, "Attribute %s %d", marker
->name
,
1627 marker
->values
[0].integer
);
1628 for (i
= 1; i
< marker
->num_values
; i
++)
1629 cupsFilePrintf(fp
, ",%d", marker
->values
[i
].integer
);
1630 cupsFilePuts(fp
, "\n");
1633 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-low-levels",
1634 IPP_TAG_INTEGER
)) != NULL
)
1636 cupsFilePrintf(fp
, "Attribute %s %d", marker
->name
,
1637 marker
->values
[0].integer
);
1638 for (i
= 1; i
< marker
->num_values
; i
++)
1639 cupsFilePrintf(fp
, ",%d", marker
->values
[i
].integer
);
1640 cupsFilePuts(fp
, "\n");
1643 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-high-levels",
1644 IPP_TAG_INTEGER
)) != NULL
)
1646 cupsFilePrintf(fp
, "Attribute %s %d", marker
->name
,
1647 marker
->values
[0].integer
);
1648 for (i
= 1; i
< marker
->num_values
; i
++)
1649 cupsFilePrintf(fp
, ",%d", marker
->values
[i
].integer
);
1650 cupsFilePuts(fp
, "\n");
1653 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-message",
1654 IPP_TAG_TEXT
)) != NULL
)
1656 snprintf(value
, sizeof(value
), "%s %s", marker
->name
,
1657 marker
->values
[0].string
.text
);
1659 cupsFilePutConf(fp
, "Attribute", value
);
1662 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-names",
1663 IPP_TAG_NAME
)) != NULL
)
1665 snprintf(value
, sizeof(value
), "%s ", marker
->name
);
1667 for (i
= 0, ptr
= value
+ strlen(value
);
1668 i
< marker
->num_values
&& ptr
< (value
+ sizeof(value
) - 1);
1674 strlcpy(ptr
, marker
->values
[i
].string
.text
,
1675 value
+ sizeof(value
) - ptr
);
1680 cupsFilePutConf(fp
, "Attribute", value
);
1683 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-types",
1684 IPP_TAG_KEYWORD
)) != NULL
)
1686 snprintf(value
, sizeof(value
), "%s ", marker
->name
);
1688 for (i
= 0, ptr
= value
+ strlen(value
);
1689 i
< marker
->num_values
&& ptr
< (value
+ sizeof(value
) - 1);
1695 strlcpy(ptr
, marker
->values
[i
].string
.text
,
1696 value
+ sizeof(value
) - ptr
);
1701 cupsFilePutConf(fp
, "Attribute", value
);
1704 if (printer
->marker_time
)
1705 cupsFilePrintf(fp
, "Attribute marker-change-time %ld\n",
1706 (long)printer
->marker_time
);
1708 cupsFilePuts(fp
, "</Printer>\n");
1712 * Make IRIX desktop & printer status happy
1715 write_irix_state(printer
);
1724 * 'cupsdSetAuthInfoRequired()' - Set the required authentication info.
1727 int /* O - 1 if value OK, 0 otherwise */
1728 cupsdSetAuthInfoRequired(
1729 cupsd_printer_t
*p
, /* I - Printer */
1730 const char *values
, /* I - Plain text value (or NULL) */
1731 ipp_attribute_t
*attr
) /* I - IPP attribute value (or NULL) */
1733 int i
; /* Looping var */
1736 p
->num_auth_info_required
= 0;
1739 * Do we have a plain text value?
1745 * Yes, grab the keywords...
1748 const char *end
; /* End of current value */
1751 while (*values
&& p
->num_auth_info_required
< 4)
1753 if ((end
= strchr(values
, ',')) == NULL
)
1754 end
= values
+ strlen(values
);
1756 if ((end
- values
) == 4 && !strncmp(values
, "none", 4))
1758 if (p
->num_auth_info_required
!= 0 || *end
)
1761 p
->auth_info_required
[p
->num_auth_info_required
] = "none";
1762 p
->num_auth_info_required
++;
1766 else if ((end
- values
) == 9 && !strncmp(values
, "negotiate", 9))
1768 if (p
->num_auth_info_required
!= 0 || *end
)
1771 p
->auth_info_required
[p
->num_auth_info_required
] = "negotiate";
1772 p
->num_auth_info_required
++;
1774 else if ((end
- values
) == 6 && !strncmp(values
, "domain", 6))
1776 p
->auth_info_required
[p
->num_auth_info_required
] = "domain";
1777 p
->num_auth_info_required
++;
1779 else if ((end
- values
) == 8 && !strncmp(values
, "password", 8))
1781 p
->auth_info_required
[p
->num_auth_info_required
] = "password";
1782 p
->num_auth_info_required
++;
1784 else if ((end
- values
) == 8 && !strncmp(values
, "username", 8))
1786 p
->auth_info_required
[p
->num_auth_info_required
] = "username";
1787 p
->num_auth_info_required
++;
1792 values
= (*end
) ? end
+ 1 : end
;
1795 if (p
->num_auth_info_required
== 0)
1797 p
->auth_info_required
[0] = "none";
1798 p
->num_auth_info_required
= 1;
1802 * Update the printer-type value as needed...
1805 if (p
->num_auth_info_required
> 1 ||
1806 strcmp(p
->auth_info_required
[0], "none"))
1807 p
->type
|= CUPS_PRINTER_AUTHENTICATED
;
1809 p
->type
&= ~CUPS_PRINTER_AUTHENTICATED
;
1815 * Grab values from an attribute instead...
1818 if (!attr
|| attr
->num_values
> 4)
1822 * Update the printer-type value as needed...
1825 if (attr
->num_values
> 1 ||
1826 strcmp(attr
->values
[0].string
.text
, "none"))
1827 p
->type
|= CUPS_PRINTER_AUTHENTICATED
;
1829 p
->type
&= ~CUPS_PRINTER_AUTHENTICATED
;
1831 for (i
= 0; i
< attr
->num_values
; i
++)
1833 if (!strcmp(attr
->values
[i
].string
.text
, "none"))
1835 if (p
->num_auth_info_required
!= 0 || attr
->num_values
!= 1)
1838 p
->auth_info_required
[p
->num_auth_info_required
] = "none";
1839 p
->num_auth_info_required
++;
1843 else if (!strcmp(attr
->values
[i
].string
.text
, "negotiate"))
1845 if (p
->num_auth_info_required
!= 0 || attr
->num_values
!= 1)
1848 p
->auth_info_required
[p
->num_auth_info_required
] = "negotiate";
1849 p
->num_auth_info_required
++;
1853 else if (!strcmp(attr
->values
[i
].string
.text
, "domain"))
1855 p
->auth_info_required
[p
->num_auth_info_required
] = "domain";
1856 p
->num_auth_info_required
++;
1858 else if (!strcmp(attr
->values
[i
].string
.text
, "password"))
1860 p
->auth_info_required
[p
->num_auth_info_required
] = "password";
1861 p
->num_auth_info_required
++;
1863 else if (!strcmp(attr
->values
[i
].string
.text
, "username"))
1865 p
->auth_info_required
[p
->num_auth_info_required
] = "username";
1866 p
->num_auth_info_required
++;
1877 * 'cupsdSetDeviceURI()' - Set the device URI for a printer.
1881 cupsdSetDeviceURI(cupsd_printer_t
*p
, /* I - Printer */
1882 const char *uri
) /* I - Device URI */
1884 char buffer
[1024], /* URI buffer */
1885 *start
, /* Start of data after scheme */
1886 *slash
, /* First slash after scheme:// */
1887 *ptr
; /* Pointer into user@host:port part */
1891 * Set the full device URI..
1894 cupsdSetString(&(p
->device_uri
), uri
);
1897 * Copy the device URI to a temporary buffer so we can sanitize any auth
1901 strlcpy(buffer
, uri
, sizeof(buffer
));
1904 * Find the end of the scheme:// part...
1907 if ((ptr
= strchr(buffer
, ':')) != NULL
)
1909 for (start
= ptr
+ 1; *start
; start
++)
1914 * Find the next slash (/) in the URI...
1917 if ((slash
= strchr(start
, '/')) == NULL
)
1918 slash
= start
+ strlen(start
); /* No slash, point to the end */
1921 * Check for an @ sign before the slash...
1924 if ((ptr
= strchr(start
, '@')) != NULL
&& ptr
< slash
)
1927 * Found an @ sign and it is before the resource part, so we have
1928 * an authentication string. Copy the remaining URI over the
1929 * authentication string...
1932 _cups_strcpy(start
, ptr
+ 1);
1937 * Save the sanitized URI...
1940 cupsdSetString(&(p
->sanitized_device_uri
), buffer
);
1945 * 'cupsdSetPrinterAttr()' - Set a printer attribute.
1949 cupsdSetPrinterAttr(
1950 cupsd_printer_t
*p
, /* I - Printer */
1951 const char *name
, /* I - Attribute name */
1952 char *value
) /* I - Attribute value string */
1954 ipp_attribute_t
*attr
; /* Attribute */
1955 int i
, /* Looping var */
1956 count
; /* Number of values */
1957 char *ptr
; /* Pointer into value */
1958 ipp_tag_t value_tag
; /* Value tag for this attribute */
1962 * Don't allow empty values...
1967 cupsdLogMessage(CUPSD_LOG_ERROR
, "Ignoring empty \"%s\" attribute", name
);
1972 * Count the number of values...
1975 for (count
= 1, ptr
= value
;
1976 (ptr
= strchr(ptr
, ',')) != NULL
;
1980 * Then add or update the attribute as needed...
1983 if (!strcmp(name
, "marker-levels") || !strcmp(name
, "marker-low-levels") ||
1984 !strcmp(name
, "marker-high-levels"))
1990 if ((attr
= ippFindAttribute(p
->attrs
, name
, IPP_TAG_INTEGER
)) != NULL
&&
1991 attr
->num_values
< count
)
1993 ippDeleteAttribute(p
->attrs
, attr
);
1998 attr
->num_values
= count
;
2000 attr
= ippAddIntegers(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, name
,
2005 cupsdLogMessage(CUPSD_LOG_ERROR
,
2006 "Unable to allocate memory for printer attribute "
2007 "(%d values)", count
);
2011 for (i
= 0; i
< count
; i
++)
2013 if ((ptr
= strchr(value
, ',')) != NULL
)
2016 attr
->values
[i
].integer
= strtol(value
, NULL
, 10);
2025 * Name or keyword values...
2028 if (!strcmp(name
, "marker-types"))
2029 value_tag
= IPP_TAG_KEYWORD
;
2030 else if (!strcmp(name
, "marker-message"))
2031 value_tag
= IPP_TAG_TEXT
;
2033 value_tag
= IPP_TAG_NAME
;
2035 if ((attr
= ippFindAttribute(p
->attrs
, name
, value_tag
)) != NULL
&&
2036 attr
->num_values
< count
)
2038 ippDeleteAttribute(p
->attrs
, attr
);
2044 for (i
= 0; i
< attr
->num_values
; i
++)
2045 _cupsStrFree(attr
->values
[i
].string
.text
);
2047 attr
->num_values
= count
;
2050 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, value_tag
, name
,
2055 cupsdLogMessage(CUPSD_LOG_ERROR
,
2056 "Unable to allocate memory for printer attribute "
2057 "(%d values)", count
);
2061 for (i
= 0; i
< count
; i
++)
2063 if ((ptr
= strchr(value
, ',')) != NULL
)
2066 attr
->values
[i
].string
.text
= _cupsStrAlloc(value
);
2076 * 'cupsdSetPrinterAttrs()' - Set printer attributes based upon the PPD file.
2080 cupsdSetPrinterAttrs(cupsd_printer_t
*p
)/* I - Printer to setup */
2082 int i
, /* Looping var */
2083 length
; /* Length of browse attributes */
2084 char resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2085 int num_air
; /* Number of auth-info-required values */
2086 const char * const *air
; /* auth-info-required values */
2087 cupsd_location_t
*auth
; /* Pointer to authentication element */
2088 const char *auth_supported
; /* Authentication supported */
2089 ipp_t
*oldattrs
; /* Old printer attributes */
2090 ipp_attribute_t
*attr
; /* Attribute data */
2091 cups_option_t
*option
; /* Current printer option */
2092 char *filter
; /* Current filter */
2093 static const char * const air_userpass
[] =
2094 { /* Basic/Digest authentication */
2099 static const char * const air_negotiate
[] =
2100 { /* Kerberos authentication */
2103 #endif /* HAVE_GSSAPI */
2104 static const char * const air_none
[] =
2105 { /* No authentication */
2110 DEBUG_printf(("cupsdSetPrinterAttrs: entering name = %s, type = %x\n", p
->name
,
2114 * Make sure that we have the common attributes defined...
2118 cupsdCreateCommonData();
2121 * Clear out old filters, if any...
2124 delete_printer_filters(p
);
2127 * Figure out the authentication that is required for the printer.
2130 auth_supported
= "requesting-user-name";
2134 if (p
->num_auth_info_required
> 0 && strcmp(p
->auth_info_required
[0], "none"))
2136 num_air
= p
->num_auth_info_required
;
2137 air
= p
->auth_info_required
;
2139 if (!strcmp(air
[0], "username"))
2140 auth_supported
= "basic";
2142 auth_supported
= "negotiate";
2144 else if (!(p
->type
& CUPS_PRINTER_DISCOVERED
))
2146 if (p
->type
& CUPS_PRINTER_CLASS
)
2147 snprintf(resource
, sizeof(resource
), "/classes/%s", p
->name
);
2149 snprintf(resource
, sizeof(resource
), "/printers/%s", p
->name
);
2151 if ((auth
= cupsdFindBest(resource
, HTTP_POST
)) == NULL
||
2152 auth
->type
== CUPSD_AUTH_NONE
)
2153 auth
= cupsdFindPolicyOp(p
->op_policy_ptr
, IPP_PRINT_JOB
);
2157 int auth_type
; /* Authentication type */
2160 if ((auth_type
= auth
->type
) == CUPSD_AUTH_DEFAULT
)
2161 auth_type
= DefaultAuthType
;
2163 if (auth_type
== CUPSD_AUTH_BASIC
|| auth_type
== CUPSD_AUTH_BASICDIGEST
)
2165 auth_supported
= "basic";
2169 else if (auth_type
== CUPSD_AUTH_DIGEST
)
2171 auth_supported
= "digest";
2176 else if (auth_type
== CUPSD_AUTH_NEGOTIATE
)
2178 auth_supported
= "negotiate";
2180 air
= air_negotiate
;
2182 #endif /* HAVE_GSSAPI */
2184 if (auth_type
!= CUPSD_AUTH_NONE
)
2185 p
->type
|= CUPS_PRINTER_AUTHENTICATED
;
2187 p
->type
&= ~CUPS_PRINTER_AUTHENTICATED
;
2190 p
->type
&= ~CUPS_PRINTER_AUTHENTICATED
;
2192 else if (p
->type
& CUPS_PRINTER_AUTHENTICATED
)
2199 * Create the required IPP attributes for a printer...
2202 oldattrs
= p
->attrs
;
2203 p
->attrs
= ippNew();
2205 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2206 "uri-authentication-supported", NULL
, auth_supported
);
2207 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2208 "uri-security-supported", NULL
, "none");
2209 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "printer-name", NULL
,
2211 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-location",
2212 NULL
, p
->location
? p
->location
: "");
2213 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-info",
2214 NULL
, p
->info
? p
->info
: "");
2219 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2220 "requesting-user-name-denied", p
->num_users
, NULL
,
2223 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2224 "requesting-user-name-allowed", p
->num_users
, NULL
,
2228 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2229 "job-quota-period", p
->quota_period
);
2230 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2231 "job-k-limit", p
->k_limit
);
2232 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2233 "job-page-limit", p
->page_limit
);
2234 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2235 "auth-info-required", num_air
, NULL
, air
);
2237 if (cupsArrayCount(Banners
) > 0 && !(p
->type
& CUPS_PRINTER_DISCOVERED
))
2240 * Setup the job-sheets-default attribute...
2243 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2244 "job-sheets-default", 2, NULL
, NULL
);
2248 attr
->values
[0].string
.text
= _cupsStrAlloc(Classification
?
2249 Classification
: p
->job_sheets
[0]);
2250 attr
->values
[1].string
.text
= _cupsStrAlloc(Classification
?
2251 Classification
: p
->job_sheets
[1]);
2258 if (p
->type
& CUPS_PRINTER_DISCOVERED
)
2261 * Tell the client this is a remote printer of some type...
2264 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
2265 "printer-uri-supported", NULL
, p
->uri
);
2267 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "printer-more-info",
2271 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
2272 "printer-make-and-model", NULL
, p
->make_model
);
2274 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri", NULL
,
2283 * Assign additional attributes depending on whether this is a printer
2287 if (p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
2290 p
->type
&= ~CUPS_PRINTER_OPTIONS
;
2293 * Add class-specific attributes...
2296 if ((p
->type
& CUPS_PRINTER_IMPLICIT
) && p
->num_printers
> 0 &&
2297 p
->printers
[0]->make_model
)
2298 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
2299 "printer-make-and-model", NULL
, p
->printers
[0]->make_model
);
2301 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
2302 "printer-make-and-model", NULL
, "Local Printer Class");
2304 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri", NULL
,
2305 "file:///dev/null");
2307 if (p
->num_printers
> 0)
2310 * Add a list of member names; URIs are added in copy_printer_attrs...
2313 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2314 "member-names", p
->num_printers
, NULL
, NULL
);
2315 p
->type
|= CUPS_PRINTER_OPTIONS
;
2317 for (i
= 0; i
< p
->num_printers
; i
++)
2320 attr
->values
[i
].string
.text
= _cupsStrAlloc(p
->printers
[i
]->name
);
2322 p
->type
&= ~CUPS_PRINTER_OPTIONS
| p
->printers
[i
]->type
;
2329 * Add printer-specific attributes...
2332 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri", NULL
,
2333 p
->sanitized_device_uri
);
2336 * Assign additional attributes from the PPD file (if any)...
2342 * Add filters for printer...
2345 for (filter
= (char *)cupsArrayFirst(p
->filters
);
2347 filter
= (char *)cupsArrayNext(p
->filters
))
2348 add_printer_filter(p
, p
->filetype
, filter
);
2352 p
->prefiltertype
= mimeAddType(MimeDatabase
, "prefilter", p
->name
);
2354 for (filter
= (char *)cupsArrayFirst(p
->pre_filters
);
2356 filter
= (char *)cupsArrayNext(p
->pre_filters
))
2357 add_printer_filter(p
, p
->prefiltertype
, filter
);
2363 * Copy marker attributes as needed...
2368 ipp_attribute_t
*oldattr
; /* Old attribute */
2371 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-colors",
2372 IPP_TAG_NAME
)) != NULL
)
2374 if ((attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2375 "marker-colors", oldattr
->num_values
, NULL
,
2378 for (i
= 0; i
< oldattr
->num_values
; i
++)
2379 attr
->values
[i
].string
.text
=
2380 _cupsStrAlloc(oldattr
->values
[i
].string
.text
);
2384 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-levels",
2385 IPP_TAG_INTEGER
)) != NULL
)
2387 if ((attr
= ippAddIntegers(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2388 "marker-levels", oldattr
->num_values
,
2391 for (i
= 0; i
< oldattr
->num_values
; i
++)
2392 attr
->values
[i
].integer
= oldattr
->values
[i
].integer
;
2396 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-names",
2397 IPP_TAG_NAME
)) != NULL
)
2399 if ((attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2400 "marker-names", oldattr
->num_values
, NULL
,
2403 for (i
= 0; i
< oldattr
->num_values
; i
++)
2404 attr
->values
[i
].string
.text
=
2405 _cupsStrAlloc(oldattr
->values
[i
].string
.text
);
2409 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-types",
2410 IPP_TAG_KEYWORD
)) != NULL
)
2412 if ((attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2413 "marker-types", oldattr
->num_values
, NULL
,
2416 for (i
= 0; i
< oldattr
->num_values
; i
++)
2417 attr
->values
[i
].string
.text
=
2418 _cupsStrAlloc(oldattr
->values
[i
].string
.text
);
2422 ippDelete(oldattrs
);
2426 * Force sharing off for remote queues...
2429 if (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
))
2434 * Copy the printer options into a browse attributes string we can re-use.
2437 const char *valptr
; /* Pointer into value */
2438 char *attrptr
; /* Pointer into attribute string */
2442 * Free the old browse attributes as needed...
2445 if (p
->browse_attrs
)
2446 free(p
->browse_attrs
);
2449 * Compute the length of all attributes + job-sheets, lease-duration,
2450 * and BrowseLocalOptions.
2453 for (length
= 1, i
= p
->num_options
, option
= p
->options
;
2457 length
+= strlen(option
->name
) + 2;
2461 for (valptr
= option
->value
; *valptr
; valptr
++)
2462 if (strchr(" \"\'\\", *valptr
))
2469 length
+= 13 + strlen(p
->job_sheets
[0]) + strlen(p
->job_sheets
[1]);
2471 if (BrowseLocalOptions
)
2472 length
+= 12 + strlen(BrowseLocalOptions
);
2474 if (p
->num_auth_info_required
> 0)
2476 length
+= 18; /* auth-info-required */
2478 for (i
= 0; i
< p
->num_auth_info_required
; i
++)
2479 length
+= strlen(p
->auth_info_required
[i
]) + 1;
2483 * Allocate the new string...
2486 if ((p
->browse_attrs
= calloc(1, length
)) == NULL
)
2487 cupsdLogMessage(CUPSD_LOG_ERROR
,
2488 "Unable to allocate %d bytes for browse data!",
2493 * Got the allocated string, now copy the options and attributes over...
2496 sprintf(p
->browse_attrs
, "job-sheets=%s,%s lease-duration=%d",
2497 p
->job_sheets
[0], p
->job_sheets
[1], BrowseTimeout
);
2498 attrptr
= p
->browse_attrs
+ strlen(p
->browse_attrs
);
2500 if (BrowseLocalOptions
)
2502 sprintf(attrptr
, " ipp-options=%s", BrowseLocalOptions
);
2503 attrptr
+= strlen(attrptr
);
2506 for (i
= p
->num_options
, option
= p
->options
;
2511 strcpy(attrptr
, option
->name
);
2512 attrptr
+= strlen(attrptr
);
2518 for (valptr
= option
->value
; *valptr
; valptr
++)
2520 if (strchr(" \"\'\\", *valptr
))
2523 *attrptr
++ = *valptr
;
2528 if (p
->num_auth_info_required
> 0)
2530 strcpy(attrptr
, "auth-info-required");
2533 for (i
= 0; i
< p
->num_auth_info_required
; i
++)
2535 *attrptr
++ = i
? ',' : '=';
2536 strcpy(attrptr
, p
->auth_info_required
[i
]);
2537 attrptr
+= strlen(attrptr
);
2546 * Populate the document-format-supported attribute...
2549 add_printer_formats(p
);
2551 DEBUG_printf(("cupsdSetPrinterAttrs: leaving name = %s, type = %x\n", p
->name
,
2555 * Add name-default attributes...
2558 add_printer_defaults(p
);
2562 * Write the IRIX printer config and status files...
2565 write_irix_config(p
);
2566 write_irix_state(p
);
2570 * Let the browse protocols reflect the change
2573 cupsdRegisterPrinter(p
);
2578 * 'cupsdSetPrinterReasons()' - Set/update the reasons strings.
2582 cupsdSetPrinterReasons(
2583 cupsd_printer_t
*p
, /* I - Printer */
2584 const char *s
) /* I - Reasons strings */
2586 int i
; /* Looping var */
2587 const char *sptr
; /* Pointer into reasons */
2588 char reason
[255], /* Reason string */
2589 *rptr
; /* Pointer into reason */
2592 if (s
[0] == '-' || s
[0] == '+')
2595 * Add/remove reasons...
2603 * Replace reasons...
2608 for (i
= 0; i
< p
->num_reasons
; i
++)
2609 _cupsStrFree(p
->reasons
[i
]);
2613 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
2615 if (PrintcapFormat
== PRINTCAP_PLIST
)
2616 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2619 if (!strcmp(s
, "none"))
2623 * Loop through all of the reasons...
2629 * Skip leading whitespace and commas...
2632 while (isspace(*sptr
& 255) || *sptr
== ',')
2635 for (rptr
= reason
; *sptr
&& !isspace(*sptr
& 255) && *sptr
!= ','; sptr
++)
2636 if (rptr
< (reason
+ sizeof(reason
) - 1))
2650 for (i
= 0; i
< p
->num_reasons
; i
++)
2651 if (!strcasecmp(reason
, p
->reasons
[i
]))
2654 * Found a match, so remove it...
2658 _cupsStrFree(p
->reasons
[i
]);
2660 if (i
< p
->num_reasons
)
2661 memmove(p
->reasons
+ i
, p
->reasons
+ i
+ 1,
2662 (p
->num_reasons
- i
) * sizeof(char *));
2666 if (!strcmp(reason
, "paused") && p
->state
== IPP_PRINTER_STOPPED
)
2667 cupsdSetPrinterState(p
, IPP_PRINTER_IDLE
, 1);
2669 if (strcmp(reason
, "connecting-to-device"))
2670 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
2672 if (PrintcapFormat
== PRINTCAP_PLIST
)
2673 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2676 else if (p
->num_reasons
< (int)(sizeof(p
->reasons
) / sizeof(p
->reasons
[0])))
2682 for (i
= 0; i
< p
->num_reasons
; i
++)
2683 if (!strcasecmp(reason
, p
->reasons
[i
]))
2686 if (i
>= p
->num_reasons
)
2688 p
->reasons
[i
] = _cupsStrAlloc(reason
);
2691 if (!strcmp(reason
, "paused") && p
->state
!= IPP_PRINTER_STOPPED
)
2692 cupsdSetPrinterState(p
, IPP_PRINTER_STOPPED
, 1);
2694 if (strcmp(reason
, "connecting-to-device"))
2695 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
2697 if (PrintcapFormat
== PRINTCAP_PLIST
)
2698 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2706 * 'cupsdSetPrinterState()' - Update the current state of a printer.
2710 cupsdSetPrinterState(
2711 cupsd_printer_t
*p
, /* I - Printer to change */
2712 ipp_pstate_t s
, /* I - New state */
2713 int update
) /* I - Update printers.conf? */
2715 ipp_pstate_t old_state
; /* Old printer state */
2719 * Can't set status of remote printers...
2722 if (p
->type
& CUPS_PRINTER_DISCOVERED
)
2726 * Set the new state...
2729 if (PrintcapFormat
== PRINTCAP_PLIST
)
2730 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2732 old_state
= p
->state
;
2737 cupsdAddEvent(s
== IPP_PRINTER_STOPPED
? CUPSD_EVENT_PRINTER_STOPPED
:
2738 CUPSD_EVENT_PRINTER_STATE
, p
, NULL
,
2739 "%s \"%s\" state changed.",
2740 (p
->type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
2744 * Let the browse code know this needs to be updated...
2748 p
->state_time
= time(NULL
);
2752 write_irix_state(p
);
2756 cupsdAddPrinterHistory(p
);
2759 * Let the browse protocols reflect the change...
2763 cupsdRegisterPrinter(p
);
2766 * Save the printer configuration if a printer goes from idle or processing
2767 * to stopped (or visa-versa)...
2770 if ((old_state
== IPP_PRINTER_STOPPED
) != (s
== IPP_PRINTER_STOPPED
) &&
2773 if (p
->type
& CUPS_PRINTER_CLASS
)
2774 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
2776 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
2782 * 'cupsdStopPrinter()' - Stop a printer from printing any jobs...
2786 cupsdStopPrinter(cupsd_printer_t
*p
, /* I - Printer to stop */
2787 int update
)/* I - Update printers.conf? */
2789 cupsd_job_t
*job
; /* Active print job */
2793 * Set the printer state...
2796 cupsdSetPrinterState(p
, IPP_PRINTER_STOPPED
, update
);
2799 * See if we have a job printing on this printer...
2805 * Get pointer to job...
2808 job
= (cupsd_job_t
*)p
->job
;
2814 cupsdStopJob(job
, 0);
2817 * Reset the state to pending...
2820 job
->state
->values
[0].integer
= IPP_JOB_PENDING
;
2821 job
->state_value
= IPP_JOB_PENDING
;
2824 cupsdMarkDirty(CUPSD_DIRTY_JOBS
);
2826 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED
, p
, job
,
2827 "Job stopped due to printer being paused");
2833 * 'cupsdUpdatePrinterPPD()' - Update keywords in a printer's PPD file.
2836 int /* O - 1 if successful, 0 otherwise */
2837 cupsdUpdatePrinterPPD(
2838 cupsd_printer_t
*p
, /* I - Printer */
2839 int num_keywords
, /* I - Number of keywords */
2840 cups_option_t
*keywords
) /* I - Keywords */
2842 int i
; /* Looping var */
2843 cups_file_t
*src
, /* Original file */
2844 *dst
; /* New file */
2845 char srcfile
[1024], /* Original filename */
2846 dstfile
[1024], /* New filename */
2847 line
[1024], /* Line from file */
2848 keystring
[41]; /* Keyword from line */
2849 cups_option_t
*keyword
; /* Current keyword */
2852 cupsdLogMessage(CUPSD_LOG_INFO
, "Updating keywords in PPD file for %s...",
2856 * Get the old and new PPD filenames...
2859 snprintf(srcfile
, sizeof(srcfile
), "%s/ppd/%s.ppd.O", ServerRoot
, p
->name
);
2860 snprintf(dstfile
, sizeof(srcfile
), "%s/ppd/%s.ppd", ServerRoot
, p
->name
);
2863 * Rename the old file and open the old and new...
2866 if (rename(dstfile
, srcfile
))
2868 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to backup PPD file for %s: %s",
2869 p
->name
, strerror(errno
));
2873 if ((src
= cupsFileOpen(srcfile
, "r")) == NULL
)
2875 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open PPD file \"%s\": %s",
2876 srcfile
, strerror(errno
));
2877 rename(srcfile
, dstfile
);
2881 if ((dst
= cupsFileOpen(dstfile
, "w")) == NULL
)
2883 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create PPD file \"%s\": %s",
2884 dstfile
, strerror(errno
));
2886 rename(srcfile
, dstfile
);
2891 * Copy the first line and then write out all of the keywords...
2894 if (!cupsFileGets(src
, line
, sizeof(line
)))
2896 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to read PPD file \"%s\": %s",
2897 srcfile
, strerror(errno
));
2900 rename(srcfile
, dstfile
);
2904 cupsFilePrintf(dst
, "%s\n", line
);
2906 for (i
= num_keywords
, keyword
= keywords
; i
> 0; i
--, keyword
++)
2908 cupsdLogMessage(CUPSD_LOG_DEBUG
, "*%s: %s", keyword
->name
, keyword
->value
);
2909 cupsFilePrintf(dst
, "*%s: %s\n", keyword
->name
, keyword
->value
);
2913 * Then copy the rest of the PPD file, dropping any keywords we changed.
2916 while (cupsFileGets(src
, line
, sizeof(line
)))
2919 * Skip keywords we've already set...
2922 if (sscanf(line
, "*%40[^:]:", keystring
) == 1 &&
2923 cupsGetOption(keystring
, num_keywords
, keywords
))
2927 * Otherwise write the line...
2930 cupsFilePrintf(dst
, "%s\n", line
);
2934 * Close files and return...
2945 * 'cupsdUpdatePrinters()' - Update printers after a partial reload.
2949 cupsdUpdatePrinters(void)
2951 cupsd_printer_t
*p
; /* Current printer */
2955 * Loop through the printers and recreate the printer attributes
2956 * for any local printers since the policy and/or access control
2957 * stuff may have changed. Also, if browsing is disabled, remove
2958 * any remote printers...
2961 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
2963 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
2966 * Remove remote printers if we are no longer browsing...
2970 (p
->type
& (CUPS_PRINTER_IMPLICIT
| CUPS_PRINTER_DISCOVERED
)))
2972 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
2973 cupsArrayRemove(ImplicitPrinters
, p
);
2975 cupsArraySave(Printers
);
2976 cupsdDeletePrinter(p
, 0);
2977 cupsArrayRestore(Printers
);
2982 * Update the operation policy pointer...
2985 if ((p
->op_policy_ptr
= cupsdFindPolicy(p
->op_policy
)) == NULL
)
2986 p
->op_policy_ptr
= DefaultPolicyPtr
;
2989 * Update printer attributes as needed...
2992 if (!(p
->type
& CUPS_PRINTER_DISCOVERED
))
2993 cupsdSetPrinterAttrs(p
);
2999 * 'cupsdValidateDest()' - Validate a printer/class destination.
3002 const char * /* O - Printer or class name */
3004 const char *uri
, /* I - Printer URI */
3005 cups_ptype_t
*dtype
, /* O - Type (printer or class) */
3006 cupsd_printer_t
**printer
) /* O - Printer pointer */
3008 cupsd_printer_t
*p
; /* Current printer */
3009 char localname
[1024],/* Localized hostname */
3010 *lptr
, /* Pointer into localized hostname */
3011 *sptr
, /* Pointer into server name */
3012 *rptr
, /* Pointer into resource */
3013 scheme
[32], /* Scheme portion of URI */
3014 username
[64], /* Username portion of URI */
3015 hostname
[HTTP_MAX_HOST
],
3016 /* Host portion of URI */
3017 resource
[HTTP_MAX_URI
];
3018 /* Resource portion of URI */
3019 int port
; /* Port portion of URI */
3022 DEBUG_printf(("cupsdValidateDest(uri=\"%s\", dtype=%p, printer=%p)\n", uri
,
3026 * Initialize return values...
3033 *dtype
= (cups_ptype_t
)0;
3036 * Pull the hostname and resource from the URI...
3039 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
3040 username
, sizeof(username
), hostname
, sizeof(hostname
),
3041 &port
, resource
, sizeof(resource
));
3044 * See if the resource is a class or printer...
3047 if (!strncmp(resource
, "/classes/", 9))
3053 rptr
= resource
+ 9;
3055 else if (!strncmp(resource
, "/printers/", 10))
3061 rptr
= resource
+ 10;
3066 * Bad resource name...
3073 * See if the printer or class name exists...
3076 p
= cupsdFindDest(rptr
);
3078 if (p
== NULL
&& strchr(rptr
, '@') == NULL
)
3086 *dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
|
3087 CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
);
3093 * Change localhost to the server name...
3096 if (!strcasecmp(hostname
, "localhost"))
3097 strlcpy(hostname
, ServerName
, sizeof(hostname
));
3099 strlcpy(localname
, hostname
, sizeof(localname
));
3101 if (!strcasecmp(hostname
, ServerName
))
3104 * Localize the hostname...
3107 lptr
= strchr(localname
, '.');
3108 sptr
= strchr(ServerName
, '.');
3110 if (sptr
!= NULL
&& lptr
!= NULL
)
3113 * Strip the common domain name components...
3116 while (lptr
!= NULL
)
3118 if (!strcasecmp(lptr
, sptr
))
3124 lptr
= strchr(lptr
+ 1, '.');
3129 DEBUG_printf(("localized hostname is \"%s\"...\n", localname
));
3132 * Find a matching printer or class...
3135 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3137 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3138 if (!strcasecmp(p
->hostname
, localname
) &&
3139 !strcasecmp(p
->name
, rptr
))
3145 *dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
|
3146 CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
);
3156 * 'cupsdWritePrintcap()' - Write a pseudo-printcap file for older applications
3161 cupsdWritePrintcap(void)
3163 int i
; /* Looping var */
3164 cups_file_t
*fp
; /* Printcap file */
3165 cupsd_printer_t
*p
; /* Current printer */
3170 * Update the IRIX printer state for the default printer; if
3171 * no printers remain, then the default printer file will be
3175 write_irix_state(DefaultPrinter
);
3179 * See if we have a printcap file; if not, don't bother writing it.
3182 if (!Printcap
|| !*Printcap
)
3186 * Open the printcap file...
3189 if ((fp
= cupsFileOpen(Printcap
, "w")) == NULL
)
3193 * Put a comment header at the top so that users will know where the
3194 * data has come from...
3197 if (PrintcapFormat
!= PRINTCAP_PLIST
)
3198 cupsFilePrintf(fp
, "# This file was automatically generated by cupsd(8) "
3200 "# %s/printers.conf file. All changes to this file\n"
3201 "# will be lost.\n", ServerRoot
);
3206 * Write a new printcap with the current list of printers.
3209 switch (PrintcapFormat
)
3213 * Each printer is put in the file as:
3223 cupsFilePrintf(fp
, "%s|%s:rm=%s:rp=%s:\n", DefaultPrinter
->name
,
3224 DefaultPrinter
->info
, ServerName
,
3225 DefaultPrinter
->name
);
3227 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3229 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3230 if (p
!= DefaultPrinter
)
3231 cupsFilePrintf(fp
, "%s|%s:rm=%s:rp=%s:\n", p
->name
, p
->info
,
3232 ServerName
, p
->name
);
3235 case PRINTCAP_PLIST
:
3237 * Each printer is written as a dictionary in a plist file.
3238 * Currently the printer-name, printer-info, printer-is-accepting-jobs,
3239 * printer-location, printer-make-and-model, printer-state,
3240 * printer-state-reasons, printer-type, and (sanitized) device-uri.
3243 cupsFilePuts(fp
, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
3244 "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD "
3245 "PLIST 1.0//EN\" \"http://www.apple.com/DTDs/"
3246 "PropertyList-1.0.dtd\">\n"
3247 "<plist version=\"1.0\">\n"
3250 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3252 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3254 cupsFilePuts(fp
, "\t<dict>\n"
3255 "\t\t<key>printer-name</key>\n"
3257 write_xml_string(fp
, p
->name
);
3258 cupsFilePuts(fp
, "</string>\n"
3259 "\t\t<key>printer-info</key>\n"
3261 write_xml_string(fp
, p
->info
);
3262 cupsFilePrintf(fp
, "</string>\n"
3263 "\t\t<key>printer-is-accepting-jobs</key>\n"
3265 "\t\t<key>printer-location</key>\n"
3266 "\t\t<string>", p
->accepting
? "true" : "false");
3267 write_xml_string(fp
, p
->location
);
3268 cupsFilePuts(fp
, "</string>\n"
3269 "\t\t<key>printer-make-and-model</key>\n"
3271 write_xml_string(fp
, p
->make_model
);
3272 cupsFilePrintf(fp
, "</string>\n"
3273 "\t\t<key>printer-state</key>\n"
3274 "\t\t<integer>%d</integer>\n"
3275 "\t\t<key>printer-state-reasons</key>\n"
3276 "\t\t<array>\n", p
->state
);
3277 for (i
= 0; i
< p
->num_reasons
; i
++)
3279 cupsFilePuts(fp
, "\t\t\t<string>");
3280 write_xml_string(fp
, p
->reasons
[i
]);
3281 cupsFilePuts(fp
, "</string>\n");
3283 cupsFilePrintf(fp
, "\t\t</array>\n"
3284 "\t\t<key>printer-type</key>\n"
3285 "\t\t<integer>%d</integer>\n"
3286 "\t\t<key>device-uri</key>\n"
3287 "\t\t<string>", p
->type
);
3288 write_xml_string(fp
, p
->sanitized_device_uri
);
3289 cupsFilePuts(fp
, "</string>\n"
3292 cupsFilePuts(fp
, "</array>\n"
3296 case PRINTCAP_SOLARIS
:
3298 * Each printer is put in the file as:
3300 * _all:all=Printer1,Printer2,Printer3,...,PrinterN
3301 * _default:use=DefaultPrinter
3303 * :bsdaddr=ServerName,Printer1:\
3304 * :description=Description:
3306 * :bsdaddr=ServerName,Printer2:\
3307 * :description=Description:
3309 * :bsdaddr=ServerName,Printer3:\
3310 * :description=Description:
3313 * :bsdaddr=ServerName,PrinterN:\
3314 * :description=Description:
3317 cupsFilePuts(fp
, "_all:all=");
3318 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3320 p
= (cupsd_printer_t
*)cupsArrayCurrent(Printers
))
3321 cupsFilePrintf(fp
, "%s%c", p
->name
,
3322 cupsArrayNext(Printers
) ? ',' : '\n');
3325 cupsFilePrintf(fp
, "_default:use=%s\n", DefaultPrinter
->name
);
3327 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3329 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3330 cupsFilePrintf(fp
, "%s:\\\n"
3331 "\t:bsdaddr=%s,%s:\\\n"
3332 "\t:description=%s:\n",
3333 p
->name
, ServerName
, p
->name
,
3334 p
->info
? p
->info
: "");
3348 * 'add_printer_defaults()' - Add name-default attributes to the printer attributes.
3352 add_printer_defaults(cupsd_printer_t
*p
)/* I - Printer */
3354 int i
; /* Looping var */
3355 int num_options
; /* Number of default options */
3356 cups_option_t
*options
, /* Default options */
3357 *option
; /* Current option */
3358 char name
[256]; /* name-default */
3362 * Maintain a common array of default attribute names...
3365 if (!CommonDefaults
)
3367 CommonDefaults
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
3369 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("copies-default"));
3370 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("document-format-default"));
3371 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("finishings-default"));
3372 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("job-hold-until-default"));
3373 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("job-priority-default"));
3374 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("job-sheets-default"));
3375 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("media-default"));
3376 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("number-up-default"));
3377 cupsArrayAdd(CommonDefaults
,
3378 _cupsStrAlloc("orientation-requested-default"));
3379 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("sides-default"));
3383 * Add all of the default options from the .conf files...
3386 for (num_options
= 0, options
= NULL
, i
= p
->num_options
, option
= p
->options
;
3390 if (strcmp(option
->name
, "ipp-options") &&
3391 strcmp(option
->name
, "job-sheets") &&
3392 strcmp(option
->name
, "lease-duration"))
3394 snprintf(name
, sizeof(name
), "%s-default", option
->name
);
3395 num_options
= cupsAddOption(name
, option
->value
, num_options
, &options
);
3397 if (!cupsArrayFind(CommonDefaults
, name
))
3398 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc(name
));
3403 * Convert options to IPP attributes...
3406 cupsEncodeOptions2(p
->attrs
, num_options
, options
, IPP_TAG_PRINTER
);
3407 cupsFreeOptions(num_options
, options
);
3410 * Add standard -default attributes as needed...
3413 if (!cupsGetOption("copies", p
->num_options
, p
->options
))
3414 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "copies-default",
3417 if (!cupsGetOption("document-format", p
->num_options
, p
->options
))
3418 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_MIMETYPE
,
3419 "document-format-default", NULL
, "application/octet-stream");
3421 if (!cupsGetOption("job-hold-until", p
->num_options
, p
->options
))
3422 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
3423 "job-hold-until-default", NULL
, "no-hold");
3425 if (!cupsGetOption("job-priority", p
->num_options
, p
->options
))
3426 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
3427 "job-priority-default", 50);
3429 if (!cupsGetOption("number-up", p
->num_options
, p
->options
))
3430 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
3431 "number-up-default", 1);
3433 if (!cupsGetOption("orientation-requested", p
->num_options
, p
->options
))
3434 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NOVALUE
,
3435 "orientation-requested-default", NULL
, NULL
);
3437 if (!cupsGetOption("notify-lease-duration", p
->num_options
, p
->options
))
3438 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
3439 "notify-lease-duration-default", DefaultLeaseDuration
);
3441 if (!cupsGetOption("notify-events", p
->num_options
, p
->options
))
3442 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
3443 "notify-events-default", NULL
, "job-completed");
3448 * 'add_printer_filter()' - Add a MIME filter for a printer.
3453 cupsd_printer_t
*p
, /* I - Printer to add to */
3454 mime_type_t
*filtertype
, /* I - Filter or prefilter MIME type */
3455 const char *filter
) /* I - Filter to add */
3457 char super
[MIME_MAX_SUPER
], /* Super-type for filter */
3458 type
[MIME_MAX_TYPE
], /* Type for filter */
3459 program
[1024]; /* Program/filter name */
3460 int cost
; /* Cost of filter */
3461 mime_type_t
*temptype
; /* MIME type looping var */
3462 char filename
[1024]; /* Full filter filename */
3466 * Parse the filter string; it should be in the following format:
3468 * super/type cost program
3471 if (sscanf(filter
, "%15[^/]/%31s%d%*[ \t]%1023[^\n]", super
, type
, &cost
,
3474 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s: invalid filter string \"%s\"!",
3480 * See if the filter program exists; if not, stop the printer and flag
3484 if (strcmp(program
, "-"))
3486 if (program
[0] == '/')
3487 strlcpy(filename
, program
, sizeof(filename
));
3489 snprintf(filename
, sizeof(filename
), "%s/filter/%s", ServerBin
, program
);
3491 if (access(filename
, X_OK
))
3493 snprintf(p
->state_message
, sizeof(p
->state_message
),
3494 "Filter \"%s\" for printer \"%s\" not available: %s",
3495 program
, p
->name
, strerror(errno
));
3496 cupsdSetPrinterReasons(p
, "+cups-missing-filter-error");
3497 cupsdSetPrinterState(p
, IPP_PRINTER_STOPPED
, 0);
3499 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s", p
->state_message
);
3504 * Mark the CUPS_PRINTER_COMMANDS bit if we have a filter for
3505 * application/vnd.cups-command...
3508 if (!strcasecmp(super
, "application") &&
3509 !strcasecmp(type
, "vnd.cups-command"))
3510 p
->type
|= CUPS_PRINTER_COMMANDS
;
3513 * Add the filter to the MIME database, supporting wildcards as needed...
3516 for (temptype
= mimeFirstType(MimeDatabase
);
3518 temptype
= mimeNextType(MimeDatabase
))
3519 if (((super
[0] == '*' && strcasecmp(temptype
->super
, "printer")) ||
3520 !strcasecmp(temptype
->super
, super
)) &&
3521 (type
[0] == '*' || !strcasecmp(temptype
->type
, type
)))
3523 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3524 "add_printer_filter: %s: adding filter %s/%s %s/%s %d %s",
3525 p
->name
, temptype
->super
, temptype
->type
,
3526 filtertype
->super
, filtertype
->type
,
3528 mimeAddFilter(MimeDatabase
, temptype
, filtertype
, cost
, program
);
3534 * 'add_printer_formats()' - Add document-format-supported values for a printer.
3538 add_printer_formats(cupsd_printer_t
*p
) /* I - Printer */
3540 int i
; /* Looping var */
3541 mime_type_t
*type
; /* Current MIME type */
3542 cups_array_t
*filters
; /* Filters */
3543 ipp_attribute_t
*attr
; /* document-format-supported attribute */
3544 char mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
3545 /* MIME type name */
3549 * Raw (and remote) queues advertise all of the supported MIME
3553 cupsArrayDelete(p
->filetypes
);
3554 p
->filetypes
= NULL
;
3558 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
,
3559 (ipp_tag_t
)(IPP_TAG_MIMETYPE
| IPP_TAG_COPY
),
3560 "document-format-supported", NumMimeTypes
, NULL
, MimeTypes
);
3565 * Otherwise, loop through the supported MIME types and see if there
3566 * are filters for them...
3569 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_printer_formats: %d types, %d filters",
3570 mimeNumTypes(MimeDatabase
), mimeNumFilters(MimeDatabase
));
3572 p
->filetypes
= cupsArrayNew(NULL
, NULL
);
3574 for (type
= mimeFirstType(MimeDatabase
);
3576 type
= mimeNextType(MimeDatabase
))
3578 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", type
->super
, type
->type
);
3580 if ((filters
= mimeFilter(MimeDatabase
, type
, p
->filetype
, NULL
)) != NULL
)
3582 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3583 "add_printer_formats: %s: %s needs %d filters",
3584 p
->name
, mimetype
, cupsArrayCount(filters
));
3586 cupsArrayDelete(filters
);
3587 cupsArrayAdd(p
->filetypes
, type
);
3590 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3591 "add_printer_formats: %s: %s not supported",
3595 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3596 "add_printer_formats: %s: %d supported types",
3597 p
->name
, cupsArrayCount(p
->filetypes
) + 1);
3600 * Add the file formats that can be filtered...
3603 if ((type
= mimeType(MimeDatabase
, "application", "octet-stream")) == NULL
||
3604 !cupsArrayFind(p
->filetypes
, type
))
3609 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_MIMETYPE
,
3610 "document-format-supported",
3611 cupsArrayCount(p
->filetypes
) + 1, NULL
, NULL
);
3614 attr
->values
[0].string
.text
= _cupsStrAlloc("application/octet-stream");
3616 for (type
= (mime_type_t
*)cupsArrayFirst(p
->filetypes
);
3618 i
++, type
= (mime_type_t
*)cupsArrayNext(p
->filetypes
))
3620 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", type
->super
, type
->type
);
3622 attr
->values
[i
].string
.text
= _cupsStrAlloc(mimetype
);
3627 char pdl
[1024]; /* Buffer to build pdl list */
3628 mime_filter_t
*filter
; /* MIME filter looping var */
3633 if (mimeType(MimeDatabase
, "application", "pdf"))
3634 strlcat(pdl
, "application/pdf,", sizeof(pdl
));
3636 if (mimeType(MimeDatabase
, "application", "postscript"))
3637 strlcat(pdl
, "application/postscript,", sizeof(pdl
));
3639 if (mimeType(MimeDatabase
, "application", "vnd.cups-raster"))
3640 strlcat(pdl
, "application/vnd.cups-raster,", sizeof(pdl
));
3643 * Determine if this is a Tioga PrintJobMgr based queue...
3646 for (filter
= (mime_filter_t
*)cupsArrayFirst(MimeDatabase
->filters
);
3648 filter
= (mime_filter_t
*)cupsArrayNext(MimeDatabase
->filters
))
3650 if (filter
->dst
== p
->filetype
&& filter
->filter
&&
3651 strstr(filter
->filter
, "PrintJobMgr"))
3656 * We only support raw printing if this is not a Tioga PrintJobMgr based
3657 * queue and if application/octet-stream is a known conversion...
3660 if (!filter
&& mimeType(MimeDatabase
, "application", "octet-stream"))
3661 strlcat(pdl
, "application/octet-stream,", sizeof(pdl
));
3663 if (mimeType(MimeDatabase
, "image", "png"))
3664 strlcat(pdl
, "image/png,", sizeof(pdl
));
3667 pdl
[strlen(pdl
) - 1] = '\0'; /* Remove trailing comma */
3669 cupsdSetString(&p
->pdl
, pdl
);
3671 #endif /* HAVE_DNSSD */
3676 * 'add_string_array()' - Add a string to an array of CUPS strings.
3680 add_string_array(cups_array_t
**a
, /* I - Array */
3681 const char *s
) /* I - String */
3684 *a
= cupsArrayNew(NULL
, NULL
);
3686 cupsArrayAdd(*a
, _cupsStrAlloc(s
));
3691 * 'compare_printers()' - Compare two printers.
3694 static int /* O - Result of comparison */
3695 compare_printers(void *first
, /* I - First printer */
3696 void *second
, /* I - Second printer */
3697 void *data
) /* I - App data (not used) */
3699 return (strcasecmp(((cupsd_printer_t
*)first
)->name
,
3700 ((cupsd_printer_t
*)second
)->name
));
3705 * 'delete_printer_filters()' - Delete all MIME filters for a printer.
3709 delete_printer_filters(
3710 cupsd_printer_t
*p
) /* I - Printer to remove from */
3712 mime_filter_t
*filter
; /* MIME filter looping var */
3716 * Range check input...
3723 * Remove all filters from the MIME database that have a destination
3724 * type == printer...
3727 for (filter
= mimeFirstFilter(MimeDatabase
);
3729 filter
= mimeNextFilter(MimeDatabase
))
3730 if (filter
->dst
== p
->filetype
)
3733 * Delete the current filter...
3736 mimeDeleteFilter(MimeDatabase
, filter
);
3742 * 'delete_string_array()' - Delete an array of CUPS strings.
3746 delete_string_array(cups_array_t
**a
) /* I - Array */
3748 char *ptr
; /* Current string */
3751 for (ptr
= (char *)cupsArrayFirst(*a
);
3753 ptr
= (char *)cupsArrayNext(*a
))
3756 cupsArrayDelete(*a
);
3762 * 'load_ppd()' - Load a cached PPD file, updating the cache as needed.
3766 load_ppd(cupsd_printer_t
*p
) /* I - Printer */
3768 int i
; /* Looping var */
3769 cups_file_t
*cache
; /* Cache file */
3770 char cache_name
[1024]; /* Cache filename */
3771 struct stat cache_info
; /* Cache file info */
3772 ppd_file_t
*ppd
; /* PPD file */
3773 char ppd_name
[1024]; /* PPD filename */
3774 struct stat ppd_info
; /* PPD file info */
3775 int num_media
; /* Number of media options */
3776 ppd_option_t
*input_slot
, /* InputSlot options */
3777 *media_type
, /* MediaType options */
3778 *page_size
, /* PageSize options */
3779 *output_bin
, /* OutputBin options */
3780 *media_quality
, /* EFMediaQualityMode options */
3781 *duplex
; /* Duplex options */
3782 ppd_attr_t
*ppd_attr
; /* PPD attribute */
3783 ipp_attribute_t
*attr
; /* Attribute data */
3784 ipp_value_t
*val
; /* Attribute value */
3785 int num_finishings
; /* Number of finishings */
3786 int finishings
[5]; /* finishings-supported values */
3787 static const char * const sides
[3] = /* sides-supported values */
3790 "two-sided-long-edge",
3791 "two-sided-short-edge"
3793 static const char * const standard_commands
[] =
3794 { /* Standard CUPS commands */
3797 "PrintSelfTestPage",
3803 * Check to see if the cache is up-to-date...
3806 snprintf(cache_name
, sizeof(cache_name
), "%s/%s.ipp", CacheDir
, p
->name
);
3807 if (stat(cache_name
, &cache_info
))
3808 cache_info
.st_mtime
= 0;
3810 snprintf(ppd_name
, sizeof(ppd_name
), "%s/ppd/%s.ppd", ServerRoot
, p
->name
);
3811 if (stat(ppd_name
, &ppd_info
))
3812 ppd_info
.st_mtime
= 1;
3814 ippDelete(p
->ppd_attrs
);
3815 p
->ppd_attrs
= ippNew();
3817 if (cache_info
.st_mtime
>= ppd_info
.st_mtime
&&
3818 (cache
= cupsFileOpen(cache_name
, "r")) != NULL
)
3821 * Load cached information and return...
3824 cupsdLogMessage(CUPSD_LOG_DEBUG
, "load_ppd: Loading %s...", cache_name
);
3826 if (ippReadIO(cache
, (ipp_iocb_t
)cupsFileRead
, 1, NULL
,
3827 p
->ppd_attrs
) == IPP_DATA
)
3829 cupsFileClose(cache
);
3833 cupsFileClose(cache
);
3837 * Reload PPD attributes from disk...
3840 cupsdLogMessage(CUPSD_LOG_DEBUG
, "load_ppd: Loading %s...", ppd_name
);
3842 delete_string_array(&(p
->filters
));
3843 delete_string_array(&(p
->pre_filters
));
3845 p
->type
&= ~CUPS_PRINTER_OPTIONS
;
3846 p
->type
|= CUPS_PRINTER_BW
;
3848 finishings
[0] = IPP_FINISHINGS_NONE
;
3851 if ((ppd
= ppdOpenFile(ppd_name
)) != NULL
)
3854 * Add make/model and other various attributes...
3857 if (ppd
->color_device
)
3858 p
->type
|= CUPS_PRINTER_COLOR
;
3859 if (ppd
->variable_sizes
)
3860 p
->type
|= CUPS_PRINTER_VARIABLE
;
3861 if (!ppd
->manual_copies
)
3862 p
->type
|= CUPS_PRINTER_COPIES
;
3863 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsFax", NULL
)) != NULL
)
3864 if (ppd_attr
->value
&& !strcasecmp(ppd_attr
->value
, "true"))
3865 p
->type
|= CUPS_PRINTER_FAX
;
3867 ippAddBoolean(p
->ppd_attrs
, IPP_TAG_PRINTER
, "color-supported",
3869 if (ppd
->throughput
)
3870 ippAddInteger(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
3871 "pages-per-minute", ppd
->throughput
);
3876 * The NickName can be localized in the character set specified
3877 * by the LanugageEncoding attribute. However, ppdOpen2() has
3878 * already converted the ppd->nickname member to UTF-8 for us
3879 * (the original attribute value is available separately)
3882 cupsdSetString(&p
->make_model
, ppd
->nickname
);
3884 else if (ppd
->modelname
)
3887 * Model name can only contain specific characters...
3890 cupsdSetString(&p
->make_model
, ppd
->modelname
);
3893 cupsdSetString(&p
->make_model
, "Bad PPD File");
3895 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
3896 "printer-make-and-model", NULL
, p
->make_model
);
3899 * Add media options from the PPD file...
3902 if ((input_slot
= ppdFindOption(ppd
, "InputSlot")) != NULL
)
3903 num_media
= input_slot
->num_choices
;
3907 if ((media_type
= ppdFindOption(ppd
, "MediaType")) != NULL
)
3908 num_media
+= media_type
->num_choices
;
3910 if ((page_size
= ppdFindOption(ppd
, "PageSize")) != NULL
)
3911 num_media
+= page_size
->num_choices
;
3913 if ((media_quality
= ppdFindOption(ppd
, "EFMediaQualityMode")) != NULL
)
3914 num_media
+= media_quality
->num_choices
;
3918 cupsdLogMessage(CUPSD_LOG_CRIT
,
3919 "The PPD file for printer %s contains no media "
3920 "options and is therefore invalid!", p
->name
);
3924 attr
= ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
3925 "media-supported", num_media
, NULL
, NULL
);
3930 if (input_slot
!= NULL
)
3931 for (i
= 0; i
< input_slot
->num_choices
; i
++, val
++)
3932 val
->string
.text
= _cupsStrAlloc(input_slot
->choices
[i
].choice
);
3934 if (media_type
!= NULL
)
3935 for (i
= 0; i
< media_type
->num_choices
; i
++, val
++)
3936 val
->string
.text
= _cupsStrAlloc(media_type
->choices
[i
].choice
);
3938 if (media_quality
!= NULL
)
3939 for (i
= 0; i
< media_quality
->num_choices
; i
++, val
++)
3940 val
->string
.text
= _cupsStrAlloc(media_quality
->choices
[i
].choice
);
3942 if (page_size
!= NULL
)
3944 for (i
= 0; i
< page_size
->num_choices
; i
++, val
++)
3945 val
->string
.text
= _cupsStrAlloc(page_size
->choices
[i
].choice
);
3947 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
3948 "media-default", NULL
, page_size
->defchoice
);
3950 else if (input_slot
!= NULL
)
3951 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
3952 "media-default", NULL
, input_slot
->defchoice
);
3953 else if (media_type
!= NULL
)
3954 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
3955 "media-default", NULL
, media_type
->defchoice
);
3956 else if (media_quality
!= NULL
)
3957 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
3958 "media-default", NULL
, media_quality
->defchoice
);
3960 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
3961 "media-default", NULL
, "none");
3969 if ((output_bin
= ppdFindOption(ppd
, "OutputBin")) != NULL
)
3971 attr
= ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
3972 "output-bin-supported", output_bin
->num_choices
,
3977 for (i
= 0, val
= attr
->values
;
3978 i
< output_bin
->num_choices
;
3980 val
->string
.text
= _cupsStrAlloc(output_bin
->choices
[i
].choice
);
3988 if ((duplex
= ppdFindOption(ppd
, "Duplex")) == NULL
)
3989 if ((duplex
= ppdFindOption(ppd
, "EFDuplex")) == NULL
)
3990 if ((duplex
= ppdFindOption(ppd
, "EFDuplexing")) == NULL
)
3991 if ((duplex
= ppdFindOption(ppd
, "KD03Duplex")) == NULL
)
3992 duplex
= ppdFindOption(ppd
, "JCLDuplex");
3994 if (duplex
&& duplex
->num_choices
> 1 &&
3995 !ppdInstallableConflict(ppd
, duplex
->keyword
, "DuplexTumble"))
3997 p
->type
|= CUPS_PRINTER_DUPLEX
;
3999 ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4000 "sides-supported", 3, NULL
, sides
);
4002 if (!strcasecmp(duplex
->defchoice
, "DuplexTumble"))
4003 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4004 "sides-default", NULL
, "two-sided-short-edge");
4005 else if (!strcasecmp(duplex
->defchoice
, "DuplexNoTumble"))
4006 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4007 "sides-default", NULL
, "two-sided-long-edge");
4009 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4010 "sides-default", NULL
, "one-sided");
4013 if (ppdFindOption(ppd
, "Collate") != NULL
)
4014 p
->type
|= CUPS_PRINTER_COLLATE
;
4016 if (ppdFindOption(ppd
, "StapleLocation") != NULL
)
4018 p
->type
|= CUPS_PRINTER_STAPLE
;
4019 finishings
[num_finishings
++] = IPP_FINISHINGS_STAPLE
;
4022 if (ppdFindOption(ppd
, "BindEdge") != NULL
)
4024 p
->type
|= CUPS_PRINTER_BIND
;
4025 finishings
[num_finishings
++] = IPP_FINISHINGS_BIND
;
4028 for (i
= 0; i
< ppd
->num_sizes
; i
++)
4029 if (ppd
->sizes
[i
].length
> 1728)
4030 p
->type
|= CUPS_PRINTER_LARGE
;
4031 else if (ppd
->sizes
[i
].length
> 1008)
4032 p
->type
|= CUPS_PRINTER_MEDIUM
;
4034 p
->type
|= CUPS_PRINTER_SMALL
;
4037 * Add a filter from application/vnd.cups-raw to printer/name to
4038 * handle "raw" printing by users.
4041 add_string_array(&(p
->filters
), "application/vnd.cups-raw 0 -");
4044 * Add any pre-filters in the PPD file...
4047 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsPreFilter", NULL
)) != NULL
)
4049 for (; ppd_attr
; ppd_attr
= ppdFindNextAttr(ppd
, "cupsPreFilter", NULL
))
4050 if (ppd_attr
->value
)
4051 add_string_array(&(p
->pre_filters
), ppd_attr
->value
);
4055 * Add any filters in the PPD file...
4058 DEBUG_printf(("ppd->num_filters = %d\n", ppd
->num_filters
));
4059 for (i
= 0; i
< ppd
->num_filters
; i
++)
4061 DEBUG_printf(("ppd->filters[%d] = \"%s\"\n", i
, ppd
->filters
[i
]));
4062 add_string_array(&(p
->filters
), ppd
->filters
[i
]);
4065 if (ppd
->num_filters
== 0)
4068 * If there are no filters, add PostScript printing filters.
4071 add_string_array(&(p
->filters
),
4072 "application/vnd.cups-command 0 commandtops");
4073 add_string_array(&(p
->filters
),
4074 "application/vnd.cups-postscript 0 -");
4076 p
->type
|= CUPS_PRINTER_COMMANDS
;
4078 else if (!(p
->type
& CUPS_PRINTER_COMMANDS
))
4081 * See if this is a PostScript device without a command filter...
4084 for (i
= 0; i
< ppd
->num_filters
; i
++)
4085 if (!strncasecmp(ppd
->filters
[i
],
4086 "application/vnd.cups-postscript", 31))
4089 if (i
< ppd
->num_filters
)
4092 * Add the generic PostScript command filter...
4095 add_string_array(&(p
->filters
),
4096 "application/vnd.cups-command 0 commandtops");
4097 p
->type
|= CUPS_PRINTER_COMMANDS
;
4101 if (p
->type
& CUPS_PRINTER_COMMANDS
)
4103 char *commands
, /* Copy of commands */
4104 *start
, /* Start of name */
4105 *end
; /* End of name */
4106 int count
; /* Number of commands */
4109 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsCommands", NULL
)) != NULL
&&
4110 ppd_attr
->value
&& ppd_attr
->value
[0])
4112 for (count
= 0, start
= ppd_attr
->value
; *start
; count
++)
4114 while (isspace(*start
& 255))
4120 while (*start
&& !isspace(*start
& 255))
4130 * Make a copy of the commands string and count how many ...
4133 attr
= ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4134 "printer-commands", count
, NULL
, NULL
);
4136 commands
= strdup(ppd_attr
->value
);
4138 for (count
= 0, start
= commands
; *start
; count
++)
4140 while (isspace(*start
& 255))
4147 while (*end
&& !isspace(*end
& 255))
4153 attr
->values
[count
].string
.text
= _cupsStrAlloc(start
);
4163 * Add the standard list of commands...
4166 ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4168 (int)(sizeof(standard_commands
) /
4169 sizeof(standard_commands
[0])), NULL
,
4176 * No commands supported...
4179 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4180 "printer-commands", NULL
, "none");
4184 * Show current and available port monitors for this printer...
4187 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "port-monitor",
4188 NULL
, p
->port_monitor
? p
->port_monitor
: "none");
4190 for (i
= 1, ppd_attr
= ppdFindAttr(ppd
, "cupsPortMonitor", NULL
);
4192 i
++, ppd_attr
= ppdFindNextAttr(ppd
, "cupsPortMonitor", NULL
));
4196 if (strstr(ppd
->protocols
, "TBCP"))
4198 else if (strstr(ppd
->protocols
, "BCP"))
4202 attr
= ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
4203 "port-monitor-supported", i
, NULL
, NULL
);
4205 attr
->values
[0].string
.text
= _cupsStrAlloc("none");
4207 for (i
= 1, ppd_attr
= ppdFindAttr(ppd
, "cupsPortMonitor", NULL
);
4209 i
++, ppd_attr
= ppdFindNextAttr(ppd
, "cupsPortMonitor", NULL
))
4210 attr
->values
[i
].string
.text
= _cupsStrAlloc(ppd_attr
->value
);
4214 if (strstr(ppd
->protocols
, "TBCP"))
4215 attr
->values
[i
].string
.text
= _cupsStrAlloc("tbcp");
4216 else if (strstr(ppd
->protocols
, "BCP"))
4217 attr
->values
[i
].string
.text
= _cupsStrAlloc("bcp");
4221 cupsdSetString(&p
->product
, ppd
->product
);
4222 #endif /* HAVE_DNSSD */
4224 if (ppdFindAttr(ppd
, "APRemoteQueueID", NULL
))
4225 p
->type
|= CUPS_PRINTER_REMOTE
;
4228 * Close the PPD and set the type...
4233 else if (!access(ppd_name
, 0))
4235 int pline
; /* PPD line number */
4236 ppd_status_t pstatus
; /* PPD load status */
4239 pstatus
= ppdLastError(&pline
);
4241 cupsdLogMessage(CUPSD_LOG_ERROR
, "PPD file for %s cannot be loaded!",
4244 if (pstatus
<= PPD_ALLOC_ERROR
)
4245 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s", strerror(errno
));
4247 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s on line %d.",
4248 ppdErrorString(pstatus
), pline
);
4250 cupsdLogMessage(CUPSD_LOG_INFO
,
4251 "Hint: Run \"cupstestppd %s\" and fix any errors.",
4255 * Add a filter from application/vnd.cups-raw to printer/name to
4256 * handle "raw" printing by users.
4259 add_string_array(&(p
->filters
), "application/vnd.cups-raw 0 -");
4262 * Add a PostScript filter, since this is still possibly PS printer.
4265 add_string_array(&(p
->filters
), "application/vnd.cups-postscript 0 -");
4270 * If we have an interface script, add a filter entry for it...
4273 char interface
[1024]; /* Interface script */
4276 snprintf(interface
, sizeof(interface
), "%s/interfaces/%s", ServerRoot
,
4278 if (!access(interface
, X_OK
))
4281 * Yes, we have a System V style interface script; use it!
4284 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4285 "printer-make-and-model", NULL
,
4286 "Local System V Printer");
4288 snprintf(interface
, sizeof(interface
), "*/* 0 %s/interfaces/%s",
4289 ServerRoot
, p
->name
);
4290 add_string_array(&(p
->filters
), interface
);
4292 else if (!strncmp(p
->device_uri
, "ipp://", 6) &&
4293 (strstr(p
->device_uri
, "/printers/") != NULL
||
4294 strstr(p
->device_uri
, "/classes/") != NULL
||
4295 (strstr(p
->device_uri
, "._ipp.") != NULL
&&
4296 !strcmp(p
->device_uri
+ strlen(p
->device_uri
) - 5,
4300 * Tell the client this is really a hard-wired remote printer.
4303 p
->type
|= CUPS_PRINTER_REMOTE
;
4306 * Point the printer-uri-supported attribute to the
4310 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
4311 "printer-uri-supported", NULL
, p
->device_uri
);
4314 * Then set the make-and-model accordingly...
4317 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4318 "printer-make-and-model", NULL
, "Remote Printer");
4321 * Print all files directly...
4330 * Otherwise we have neither - treat this as a "dumb" printer
4331 * with no PPD file...
4334 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4335 "printer-make-and-model", NULL
, "Local Raw Printer");
4341 ippAddIntegers(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
4342 "finishings-supported", num_finishings
, finishings
);
4343 ippAddInteger(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
4344 "finishings-default", IPP_FINISHINGS_NONE
);
4346 if (ppd
&& (cache
= cupsFileOpen(cache_name
, "w")) != NULL
)
4349 * Save cached PPD attributes to disk...
4352 cupsdLogMessage(CUPSD_LOG_DEBUG
, "load_ppd: Saving %s...", cache_name
);
4354 p
->ppd_attrs
->state
= IPP_IDLE
;
4356 if (ippWriteIO(cache
, (ipp_iocb_t
)cupsFileWrite
, 1, NULL
,
4357 p
->ppd_attrs
) != IPP_DATA
)
4359 cupsdLogMessage(CUPSD_LOG_ERROR
,
4360 "Unable to save PPD cache file \"%s\" - %s", cache_name
,
4365 cupsFileClose(cache
);
4367 else if (cache_info
.st_mtime
)
4370 * Remove cache file...
4380 * 'write_irix_config()' - Update the config files used by the IRIX
4385 write_irix_config(cupsd_printer_t
*p
) /* I - Printer to update */
4387 char filename
[1024]; /* Interface script filename */
4388 cups_file_t
*fp
; /* Interface script file */
4389 ipp_attribute_t
*attr
; /* Attribute data */
4393 * Add dummy interface and GUI scripts to fool SGI's "challenged" printing
4394 * tools. First the interface script that tells the tools what kind of
4395 * printer we have...
4398 snprintf(filename
, sizeof(filename
), "/var/spool/lp/interface/%s", p
->name
);
4400 if (p
->type
& CUPS_PRINTER_CLASS
)
4402 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4404 cupsFilePuts(fp
, "#!/bin/sh\n");
4406 if ((attr
= ippFindAttribute(p
->attrs
, "printer-make-and-model",
4407 IPP_TAG_TEXT
)) != NULL
)
4408 cupsFilePrintf(fp
, "NAME=\"%s\"\n", attr
->values
[0].string
.text
);
4409 else if (p
->type
& CUPS_PRINTER_CLASS
)
4410 cupsFilePuts(fp
, "NAME=\"Printer Class\"\n");
4412 cupsFilePuts(fp
, "NAME=\"Remote Destination\"\n");
4414 if (p
->type
& CUPS_PRINTER_COLOR
)
4415 cupsFilePuts(fp
, "TYPE=ColorPostScript\n");
4417 cupsFilePuts(fp
, "TYPE=MonoPostScript\n");
4419 cupsFilePrintf(fp
, "HOSTNAME=%s\n", ServerName
);
4420 cupsFilePrintf(fp
, "HOSTPRINTER=%s\n", p
->name
);
4424 chmod(filename
, 0755);
4425 chown(filename
, User
, Group
);
4429 * Then the member file that tells which device file the queue is connected
4430 * to... Networked printers use "/dev/null" in this file, so that's what
4431 * we use (the actual device URI can confuse some apps...)
4434 snprintf(filename
, sizeof(filename
), "/var/spool/lp/member/%s", p
->name
);
4436 if (p
->type
& CUPS_PRINTER_CLASS
)
4438 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4440 cupsFilePuts(fp
, "/dev/null\n");
4444 chmod(filename
, 0644);
4445 chown(filename
, User
, Group
);
4449 * The gui_interface file is a script or program that launches a GUI
4450 * option panel for the printer, using options specified on the
4451 * command-line in the third argument. The option panel must send
4452 * any printing options to stdout on a single line when the user
4453 * accepts them, or nothing if the user cancels the dialog.
4455 * The default options panel program is /usr/bin/glpoptions, from
4456 * the ESP Print Pro software. You can select another using the
4457 * PrintcapGUI option.
4460 snprintf(filename
, sizeof(filename
), "/var/spool/lp/gui_interface/ELF/%s.gui", p
->name
);
4462 if (p
->type
& CUPS_PRINTER_CLASS
)
4464 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4466 cupsFilePuts(fp
, "#!/bin/sh\n");
4467 cupsFilePrintf(fp
, "%s -d %s -o \"$3\"\n", PrintcapGUI
, p
->name
);
4471 chmod(filename
, 0755);
4472 chown(filename
, User
, Group
);
4476 * The POD config file is needed by the printstatus command to show
4477 * the printer location and device.
4480 snprintf(filename
, sizeof(filename
), "/var/spool/lp/pod/%s.config", p
->name
);
4482 if (p
->type
& CUPS_PRINTER_CLASS
)
4484 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4486 cupsFilePrintf(fp
, "Printer Class | %s\n",
4487 (p
->type
& CUPS_PRINTER_COLOR
) ? "ColorPostScript" : "MonoPostScript");
4488 cupsFilePrintf(fp
, "Printer Model | %s\n", p
->make_model
? p
->make_model
: "");
4489 cupsFilePrintf(fp
, "Location Code | %s\n", p
->location
? p
->location
: "");
4490 cupsFilePrintf(fp
, "Physical Location | %s\n", p
->info
? p
->info
: "");
4491 cupsFilePrintf(fp
, "Port Path | %s\n", p
->device_uri
);
4492 cupsFilePrintf(fp
, "Config Path | /var/spool/lp/pod/%s.config\n", p
->name
);
4493 cupsFilePrintf(fp
, "Active Status Path | /var/spool/lp/pod/%s.status\n", p
->name
);
4494 cupsFilePuts(fp
, "Status Update Wait | 10 seconds\n");
4498 chmod(filename
, 0664);
4499 chown(filename
, User
, Group
);
4505 * 'write_irix_state()' - Update the status files used by IRIX printing
4510 write_irix_state(cupsd_printer_t
*p
) /* I - Printer to update */
4512 char filename
[1024]; /* Interface script filename */
4513 cups_file_t
*fp
; /* Interface script file */
4514 int tag
; /* Status tag value */
4520 * The POD status file is needed for the printstatus window to
4521 * provide the current status of the printer.
4524 snprintf(filename
, sizeof(filename
), "/var/spool/lp/pod/%s.status", p
->name
);
4526 if (p
->type
& CUPS_PRINTER_CLASS
)
4528 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4530 cupsFilePrintf(fp
, "Operational Status | %s\n",
4531 (p
->state
== IPP_PRINTER_IDLE
) ? "Idle" :
4532 (p
->state
== IPP_PRINTER_PROCESSING
) ? "Busy" :
4534 cupsFilePrintf(fp
, "Information | 01 00 00 | %s\n", CUPS_SVERSION
);
4535 cupsFilePrintf(fp
, "Information | 02 00 00 | Device URI: %s\n",
4537 cupsFilePrintf(fp
, "Information | 03 00 00 | %s jobs\n",
4538 p
->accepting
? "Accepting" : "Not accepting");
4539 cupsFilePrintf(fp
, "Information | 04 00 00 | %s\n", p
->state_message
);
4543 chmod(filename
, 0664);
4544 chown(filename
, User
, Group
);
4548 * The activeicons file is needed to provide desktop icons for printers:
4550 * [ quoted from /usr/lib/print/tagit ]
4552 * --- Type of printer tags (base values)
4554 * Dumb=66048 # 0x10200
4555 * DumbColor=66080 # 0x10220
4556 * Raster=66112 # 0x10240
4557 * ColorRaster=66144 # 0x10260
4558 * Plotter=66176 # 0x10280
4559 * PostScript=66208 # 0x102A0
4560 * ColorPostScript=66240 # 0x102C0
4561 * MonoPostScript=66272 # 0x102E0
4563 * --- Printer state modifiers for local printers
4568 * Unknown=3 # 0x3 (Faulted due to unknown reason)
4570 * --- Printer state modifiers for network printers
4574 * NetFaulted=10 # 0xA
4575 * NetUnknown=11 # 0xB (Faulted due to unknown reason)
4578 snprintf(filename
, sizeof(filename
), "/var/spool/lp/activeicons/%s", p
->name
);
4580 if (p
->type
& CUPS_PRINTER_CLASS
)
4582 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4584 if (p
->type
& CUPS_PRINTER_COLOR
)
4589 if (p
->type
& CUPS_PRINTER_REMOTE
)
4592 if (p
->state
== IPP_PRINTER_PROCESSING
)
4595 else if (p
->state
== IPP_PRINTER_STOPPED
)
4598 cupsFilePuts(fp
, "#!/bin/sh\n");
4599 cupsFilePrintf(fp
, "#Tag %d\n", tag
);
4603 chmod(filename
, 0755);
4604 chown(filename
, User
, Group
);
4609 * The default file is needed by the printers window to show
4610 * the default printer.
4613 snprintf(filename
, sizeof(filename
), "/var/spool/lp/default");
4615 if (DefaultPrinter
!= NULL
)
4617 if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4619 cupsFilePrintf(fp
, "%s\n", DefaultPrinter
->name
);
4623 chmod(filename
, 0644);
4624 chown(filename
, User
, Group
);
4634 * 'write_xml_string()' - Write a string with XML escaping.
4638 write_xml_string(cups_file_t
*fp
, /* I - File to write to */
4639 const char *s
) /* I - String to write */
4641 const char *start
; /* Start of current sequence */
4647 for (start
= s
; *s
; s
++)
4652 cupsFileWrite(fp
, start
, s
- start
);
4654 cupsFilePuts(fp
, "&");
4660 cupsFileWrite(fp
, start
, s
- start
);
4662 cupsFilePuts(fp
, "<");
4668 cupsFilePuts(fp
, start
);
4673 * End of "$Id: printers.c 7968 2008-09-19 23:03:01Z mike $".