2 * "$Id: printers.c 7968 2008-09-19 23:03:01Z mike $"
4 * Printer routines for the Common UNIX Printing System (CUPS).
6 * Copyright 2007-2009 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
17 * cupsdAddPrinter() - Add a printer to the system.
18 * cupsdAddPrinterHistory() - Add the current printer state to the history.
19 * cupsdAddPrinterUser() - Add a user to the ACL.
20 * cupsdCreateCommonData() - Create the common printer data.
21 * cupsdDeleteAllPrinters() - Delete all printers from the system.
22 * cupsdDeletePrinter() - Delete a printer from the system.
23 * cupsdFindDest() - Find a destination in the list.
24 * cupsdFindPrinter() - Find a printer in the list.
25 * cupsdFreePrinterUsers() - Free allow/deny users.
26 * cupsdLoadAllPrinters() - Load printers from the printers.conf file.
27 * cupsdRenamePrinter() - Rename a printer.
28 * cupsdSaveAllPrinters() - Save all printer definitions to the
30 * cupsdSetAuthInfoRequired() - Set the required authentication info.
31 * cupsdSetDeviceURI() - Set the device URI for a printer.
32 * cupsdSetPrinterAttr() - Set a printer attribute.
33 * cupsdSetPrinterAttrs() - Set printer attributes based upon the PPD
35 * cupsdSetPrinterReasons() - Set/update the reasons strings.
36 * cupsdSetPrinterState() - Update the current state of a printer.
37 * cupsdStopPrinter() - Stop a printer from printing any jobs...
38 * cupsdUpdatePrinterPPD() - Update keywords in a printer's PPD file.
39 * cupsdUpdatePrinters() - Update printers after a partial reload.
40 * cupsdValidateDest() - Validate a printer/class destination.
41 * cupsdWritePrintcap() - Write a pseudo-printcap file for older
42 * applications that need it...
43 * add_printer_defaults() - Add name-default attributes to the printer
45 * add_printer_filter() - Add a MIME filter for a printer.
46 * add_printer_formats() - Add document-format-supported values for a
48 * add_string_array() - Add a string to an array of CUPS strings.
49 * compare_printers() - Compare two printers.
50 * delete_printer_filters() - Delete all MIME filters for a printer.
51 * delete_string_array() - Delete an array of CUPS strings.
52 * load_ppd() - Load a cached PPD file, updating the cache as
54 * write_irix_config() - Update the config files used by the IRIX
56 * write_irix_state() - Update the status files used by IRIX printing
58 * write_xml_string() - Write a string with XML escaping.
62 * Include necessary headers...
67 #include <cups/pwgmedia.h>
74 static void add_printer_defaults(cupsd_printer_t
*p
);
75 static void add_printer_filter(cupsd_printer_t
*p
, mime_type_t
*type
,
77 static void add_printer_formats(cupsd_printer_t
*p
);
78 static void add_string_array(cups_array_t
**a
, const char *s
);
79 static int compare_printers(void *first
, void *second
, void *data
);
80 static void delete_printer_filters(cupsd_printer_t
*p
);
81 static void delete_string_array(cups_array_t
**a
);
82 static void load_ppd(cupsd_printer_t
*p
);
84 static void write_irix_config(cupsd_printer_t
*p
);
85 static void write_irix_state(cupsd_printer_t
*p
);
87 static void write_xml_string(cups_file_t
*fp
, const char *s
);
91 * 'cupsdAddPrinter()' - Add a printer to the system.
94 cupsd_printer_t
* /* O - New printer */
95 cupsdAddPrinter(const char *name
) /* I - Name of printer */
97 cupsd_printer_t
*p
; /* New printer */
101 * Range check input...
104 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAddPrinter(\"%s\")", name
);
107 * Create a new printer entity...
110 if ((p
= calloc(1, sizeof(cupsd_printer_t
))) == NULL
)
112 cupsdLogMessage(CUPSD_LOG_CRIT
, "Unable to allocate memory for printer - %s",
117 cupsdSetString(&p
->name
, name
);
118 cupsdSetString(&p
->info
, name
);
119 cupsdSetString(&p
->hostname
, ServerName
);
121 cupsdSetStringf(&p
->uri
, "ipp://%s:%d/printers/%s", ServerName
, RemotePort
,
123 cupsdSetDeviceURI(p
, "file:///dev/null");
125 p
->state
= IPP_PRINTER_STOPPED
;
126 p
->state_time
= time(NULL
);
128 p
->shared
= DefaultShared
;
129 p
->filetype
= mimeAddType(MimeDatabase
, "printer", name
);
131 cupsdSetString(&p
->job_sheets
[0], "none");
132 cupsdSetString(&p
->job_sheets
[1], "none");
134 cupsdSetString(&p
->error_policy
, ErrorPolicy
);
135 cupsdSetString(&p
->op_policy
, DefaultPolicy
);
137 p
->op_policy_ptr
= DefaultPolicyPtr
;
139 if (MaxPrinterHistory
)
140 p
->history
= calloc(MaxPrinterHistory
, sizeof(ipp_t
*));
143 * Insert the printer in the printer list alphabetically...
147 Printers
= cupsArrayNew(compare_printers
, NULL
);
149 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
150 "cupsdAddPrinter: Adding %s to Printers", p
->name
);
151 cupsArrayAdd(Printers
, p
);
153 if (!ImplicitPrinters
)
154 ImplicitPrinters
= cupsArrayNew(compare_printers
, NULL
);
157 * Return the new printer...
165 * 'cupsdAddPrinterHistory()' - Add the current printer state to the history.
169 cupsdAddPrinterHistory(
170 cupsd_printer_t
*p
) /* I - Printer */
172 ipp_t
*history
; /* History collection */
176 * Stop early if we aren't keeping history data...
179 if (MaxPrinterHistory
<= 0)
183 * Retire old history data as needed...
186 p
->sequence_number
++;
188 if (p
->num_history
>= MaxPrinterHistory
)
191 ippDelete(p
->history
[0]);
192 memmove(p
->history
, p
->history
+ 1, p
->num_history
* sizeof(ipp_t
*));
196 * Create a collection containing the current printer-state, printer-up-time,
197 * printer-state-message, and printer-state-reasons attributes.
201 ippAddInteger(history
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
203 ippAddBoolean(history
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs",
205 ippAddBoolean(history
, IPP_TAG_PRINTER
, "printer-is-shared", p
->shared
);
206 ippAddString(history
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-state-message",
207 NULL
, p
->state_message
);
208 if (p
->num_reasons
== 0)
209 ippAddString(history
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
210 "printer-state-reasons", NULL
, "none");
212 ippAddStrings(history
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
213 "printer-state-reasons", p
->num_reasons
, NULL
,
214 (const char * const *)p
->reasons
);
215 ippAddInteger(history
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
216 "printer-state-change-time", p
->state_time
);
217 ippAddInteger(history
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
218 "printer-state-sequence-number", p
->sequence_number
);
220 p
->history
[p
->num_history
] = history
;
226 * 'cupsdAddPrinterUser()' - Add a user to the ACL.
231 cupsd_printer_t
*p
, /* I - Printer */
232 const char *username
) /* I - User */
234 const char **temp
; /* Temporary array pointer */
240 if (p
->num_users
== 0)
241 temp
= malloc(sizeof(char **));
243 temp
= realloc(p
->users
, sizeof(char **) * (p
->num_users
+ 1));
249 temp
+= p
->num_users
;
251 if ((*temp
= strdup(username
)) != NULL
)
257 * 'cupsdCreateCommonData()' - Create the common printer data.
261 cupsdCreateCommonData(void)
263 int i
; /* Looping var */
264 ipp_attribute_t
*attr
; /* Attribute data */
265 cups_dir_t
*dir
; /* Notifier directory */
266 cups_dentry_t
*dent
; /* Notifier directory entry */
267 cups_array_t
*notifiers
; /* Notifier array */
268 char filename
[1024], /* Filename */
269 *notifier
; /* Current notifier */
270 cupsd_policy_t
*p
; /* Current policy */
271 static const int nups
[] = /* number-up-supported values */
272 { 1, 2, 4, 6, 9, 16 };
273 static const int orients
[4] =/* orientation-requested-supported values */
277 IPP_REVERSE_LANDSCAPE
,
280 static const char * const holds
[] = /* job-hold-until-supported values */
291 static const char * const versions
[] =/* ipp-versions-supported values */
298 static const int ops
[] = /* operations-supported values */
305 IPP_GET_JOB_ATTRIBUTES
,
307 IPP_GET_PRINTER_ATTRIBUTES
,
313 IPP_SET_PRINTER_ATTRIBUTES
,
314 IPP_SET_JOB_ATTRIBUTES
,
315 IPP_GET_PRINTER_SUPPORTED_VALUES
,
316 IPP_CREATE_PRINTER_SUBSCRIPTION
,
317 IPP_CREATE_JOB_SUBSCRIPTION
,
318 IPP_GET_SUBSCRIPTION_ATTRIBUTES
,
319 IPP_GET_SUBSCRIPTIONS
,
320 IPP_RENEW_SUBSCRIPTION
,
321 IPP_CANCEL_SUBSCRIPTION
,
322 IPP_GET_NOTIFICATIONS
,
326 IPP_RELEASE_HELD_NEW_JOBS
,
340 CUPS_AUTHENTICATE_JOB
,
345 static const char * const charsets
[] =/* charset-supported values */
350 static const char * const compressions
[] =
351 { /* document-compression-supported values */
355 #endif /* HAVE_LIBZ */
357 static const char * const media_col_supported
[] =
358 { /* media-col-supported values */
364 static const char * const multiple_document_handling
[] =
365 { /* multiple-document-handling-supported values */
366 "separate-documents-uncollated-copies",
367 "separate-documents-collated-copies"
369 static const char * const notify_attrs
[] =
370 { /* notify-attributes-supported values */
371 "printer-state-change-time",
372 "notify-lease-expiration-time",
373 "notify-subscriber-user-name"
375 static const char * const notify_events
[] =
376 { /* notify-events-supported values */
378 "job-config-changed",
385 "printer-config-changed",
387 "printer-finishings-changed",
388 "printer-media-changed",
392 "printer-state-changed",
399 static const char * const job_settable
[] =
400 { /* job-settable-attributes-supported */
406 "multiple-document-handling",
408 "orientation-requested",
411 "printer-resolution",
414 static const char * const printer_settable
[] =
415 { /* printer-settable-attributes-supported */
422 ippDelete(CommonData
);
424 CommonData
= ippNew();
427 * This list of attributes is sorted to improve performance when the
428 * client provides a requested-attributes attribute...
431 /* charset-configured */
432 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_CHARSET
| IPP_TAG_COPY
,
433 "charset-configured", NULL
, DefaultCharset
);
435 /* charset-supported */
436 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_CHARSET
| IPP_TAG_COPY
,
437 "charset-supported", sizeof(charsets
) / sizeof(charsets
[0]),
440 /* compression-supported */
441 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
442 "compression-supported",
443 sizeof(compressions
) / sizeof(compressions
[0]),
446 /* copies-supported */
447 ippAddRange(CommonData
, IPP_TAG_PRINTER
, "copies-supported", 1, MaxCopies
);
450 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
| IPP_TAG_COPY
,
451 "cups-version", NULL
, CUPS_SVERSION
+ 6);
453 /* generated-natural-language-supported */
454 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_LANGUAGE
| IPP_TAG_COPY
,
455 "generated-natural-language-supported", NULL
, DefaultLanguage
);
457 /* ipp-versions-supported */
458 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
459 "ipp-versions-supported", sizeof(versions
) / sizeof(versions
[0]),
462 /* job-hold-until-supported */
463 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
464 "job-hold-until-supported", sizeof(holds
) / sizeof(holds
[0]),
467 /* job-priority-supported */
468 ippAddInteger(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
469 "job-priority-supported", 100);
471 /* job-settable-attributes-supported */
472 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
473 "job-settable-attributes-supported",
474 sizeof(job_settable
) / sizeof(job_settable
[0]),
477 /* job-sheets-supported */
478 if (cupsArrayCount(Banners
) > 0)
481 * Setup the job-sheets-supported attribute...
484 if (Classification
&& !ClassifyOverride
)
485 attr
= ippAddString(CommonData
, IPP_TAG_PRINTER
,
486 IPP_TAG_NAME
| IPP_TAG_COPY
,
487 "job-sheets-supported", NULL
, Classification
);
489 attr
= ippAddStrings(CommonData
, IPP_TAG_PRINTER
,
490 IPP_TAG_NAME
| IPP_TAG_COPY
,
491 "job-sheets-supported", cupsArrayCount(Banners
) + 1,
495 cupsdLogMessage(CUPSD_LOG_EMERG
,
496 "Unable to allocate memory for "
497 "job-sheets-supported attribute: %s!", strerror(errno
));
498 else if (!Classification
|| ClassifyOverride
)
500 cupsd_banner_t
*banner
; /* Current banner */
503 attr
->values
[0].string
.text
= _cupsStrAlloc("none");
505 for (i
= 1, banner
= (cupsd_banner_t
*)cupsArrayFirst(Banners
);
507 i
++, banner
= (cupsd_banner_t
*)cupsArrayNext(Banners
))
508 attr
->values
[i
].string
.text
= banner
->name
;
512 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_NAME
| IPP_TAG_COPY
,
513 "job-sheets-supported", NULL
, "none");
515 /* media-col-supported */
516 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
517 "media-col-supported",
518 sizeof(media_col_supported
) /
519 sizeof(media_col_supported
[0]), NULL
,
520 media_col_supported
);
522 /* multiple-document-handling-supported */
523 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
524 "multiple-document-handling-supported",
525 sizeof(multiple_document_handling
) /
526 sizeof(multiple_document_handling
[0]), NULL
,
527 multiple_document_handling
);
529 /* multiple-document-jobs-supported */
530 ippAddBoolean(CommonData
, IPP_TAG_PRINTER
,
531 "multiple-document-jobs-supported", 1);
533 /* multiple-operation-time-out */
534 ippAddInteger(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
535 "multiple-operation-time-out", MultipleOperationTimeout
);
537 /* natural-language-configured */
538 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_LANGUAGE
| IPP_TAG_COPY
,
539 "natural-language-configured", NULL
, DefaultLanguage
);
541 /* notify-attributes-supported */
542 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
543 "notify-attributes-supported",
544 (int)(sizeof(notify_attrs
) / sizeof(notify_attrs
[0])),
547 /* notify-lease-duration-supported */
548 ippAddRange(CommonData
, IPP_TAG_PRINTER
,
549 "notify-lease-duration-supported", 0,
550 MaxLeaseDuration
? MaxLeaseDuration
: 2147483647);
552 /* notify-max-events-supported */
553 ippAddInteger(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
554 "notify-max-events-supported", MaxEvents
);
556 /* notify-events-supported */
557 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
558 "notify-events-supported",
559 (int)(sizeof(notify_events
) / sizeof(notify_events
[0])),
560 NULL
, notify_events
);
562 /* notify-pull-method-supported */
563 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
564 "notify-pull-method-supported", NULL
, "ippget");
566 /* notify-schemes-supported */
567 snprintf(filename
, sizeof(filename
), "%s/notifier", ServerBin
);
568 if ((dir
= cupsDirOpen(filename
)) != NULL
)
570 notifiers
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
572 while ((dent
= cupsDirRead(dir
)) != NULL
)
573 if (S_ISREG(dent
->fileinfo
.st_mode
) &&
574 (dent
->fileinfo
.st_mode
& S_IXOTH
) != 0)
575 cupsArrayAdd(notifiers
, _cupsStrAlloc(dent
->filename
));
577 if (cupsArrayCount(notifiers
) > 0)
579 attr
= ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
580 "notify-schemes-supported",
581 cupsArrayCount(notifiers
), NULL
, NULL
);
583 for (i
= 0, notifier
= (char *)cupsArrayFirst(notifiers
);
585 i
++, notifier
= (char *)cupsArrayNext(notifiers
))
586 attr
->values
[i
].string
.text
= notifier
;
589 cupsArrayDelete(notifiers
);
593 /* number-up-supported */
594 ippAddIntegers(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
595 "number-up-supported", sizeof(nups
) / sizeof(nups
[0]), nups
);
597 /* operations-supported */
598 ippAddIntegers(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
599 "operations-supported",
600 sizeof(ops
) / sizeof(ops
[0]) + JobFiles
- 1, ops
);
602 /* orientation-requested-supported */
603 ippAddIntegers(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
604 "orientation-requested-supported", 4, orients
);
606 /* page-ranges-supported */
607 ippAddBoolean(CommonData
, IPP_TAG_PRINTER
, "page-ranges-supported", 1);
609 /* pdf-override-supported */
610 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
611 "pdl-override-supported", NULL
, "not-attempted");
613 /* printer-op-policy-supported */
614 attr
= ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_NAME
| IPP_TAG_COPY
,
615 "printer-op-policy-supported", cupsArrayCount(Policies
),
617 for (i
= 0, p
= (cupsd_policy_t
*)cupsArrayFirst(Policies
);
619 i
++, p
= (cupsd_policy_t
*)cupsArrayNext(Policies
))
620 attr
->values
[i
].string
.text
= p
->name
;
622 /* printer-settable-attributes-supported */
623 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
624 "printer-settable-attributes-supported",
625 sizeof(printer_settable
) / sizeof(printer_settable
[0]),
626 NULL
, printer_settable
);
628 /* server-is-sharing-printers */
629 ippAddBoolean(CommonData
, IPP_TAG_PRINTER
, "server-is-sharing-printers",
630 BrowseLocalProtocols
!= 0 && Browsing
);
635 * 'cupsdDeleteAllPrinters()' - Delete all printers from the system.
639 cupsdDeleteAllPrinters(void)
641 cupsd_printer_t
*p
; /* Pointer to current printer/class */
644 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
646 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
648 p
->op_policy_ptr
= DefaultPolicyPtr
;
649 cupsdDeletePrinter(p
, 0);
655 * 'cupsdDeletePrinter()' - Delete a printer from the system.
660 cupsd_printer_t
*p
, /* I - Printer to delete */
661 int update
) /* I - Update printers.conf? */
663 int i
; /* Looping var */
665 char filename
[1024]; /* Interface script filename */
669 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdDeletePrinter(p=%p(%s), update=%d)",
673 * Save the current position in the Printers array...
676 cupsArraySave(Printers
);
679 * Stop printing on this printer...
682 cupsdSetPrinterState(p
, IPP_PRINTER_STOPPED
, update
);
685 cupsdSetJobState(p
->job
, IPP_JOB_PENDING
, CUPSD_JOB_FORCE
,
686 update
? "Job stopped due to printer being deleted." :
690 * If this printer is the next for browsing, point to the next one...
695 cupsArrayFind(Printers
, p
);
696 BrowseNext
= (cupsd_printer_t
*)cupsArrayNext(Printers
);
700 * Remove the printer from the list...
703 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
704 "cupsdDeletePrinter: Removing %s from Printers", p
->name
);
705 cupsArrayRemove(Printers
, p
);
707 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
709 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
710 "cupsdDeletePrinter: Removing %s from ImplicitPrinters",
712 cupsArrayRemove(ImplicitPrinters
, p
);
716 * Remove the dummy interface/icon/option files under IRIX...
720 snprintf(filename
, sizeof(filename
), "/var/spool/lp/interface/%s", p
->name
);
723 snprintf(filename
, sizeof(filename
), "/var/spool/lp/gui_interface/ELF/%s.gui",
727 snprintf(filename
, sizeof(filename
), "/var/spool/lp/activeicons/%s", p
->name
);
730 snprintf(filename
, sizeof(filename
), "/var/spool/lp/pod/%s.config", p
->name
);
733 snprintf(filename
, sizeof(filename
), "/var/spool/lp/pod/%s.status", p
->name
);
736 snprintf(filename
, sizeof(filename
), "/var/spool/lp/member/%s", p
->name
);
741 * If p is the default printer, assign a different one...
744 if (p
== DefaultPrinter
)
746 DefaultPrinter
= NULL
;
748 if (UseNetworkDefault
)
751 * Find the first network default printer and use it...
754 cupsd_printer_t
*dp
; /* New default printer */
757 for (dp
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
759 dp
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
760 if (dp
!= p
&& (dp
->type
& CUPS_PRINTER_DEFAULT
))
769 * Remove this printer from any classes...
772 if (!(p
->type
& CUPS_PRINTER_IMPLICIT
))
774 cupsdDeletePrinterFromClasses(p
);
777 * Deregister from any browse protocols...
780 cupsdDeregisterPrinter(p
, 1);
784 * Free all memory used by the printer...
787 if (p
->printers
!= NULL
)
790 if (MaxPrinterHistory
)
792 for (i
= 0; i
< p
->num_history
; i
++)
793 ippDelete(p
->history
[i
]);
798 delete_printer_filters(p
);
800 for (i
= 0; i
< p
->num_reasons
; i
++)
801 _cupsStrFree(p
->reasons
[i
]);
804 ippDelete(p
->ppd_attrs
);
806 mimeDeleteType(MimeDatabase
, p
->filetype
);
807 mimeDeleteType(MimeDatabase
, p
->prefiltertype
);
809 delete_string_array(&(p
->filters
));
810 delete_string_array(&(p
->pre_filters
));
812 cupsdFreePrinterUsers(p
);
815 cupsdClearString(&p
->uri
);
816 cupsdClearString(&p
->hostname
);
817 cupsdClearString(&p
->name
);
818 cupsdClearString(&p
->location
);
819 cupsdClearString(&p
->make_model
);
820 cupsdClearString(&p
->info
);
821 cupsdClearString(&p
->job_sheets
[0]);
822 cupsdClearString(&p
->job_sheets
[1]);
823 cupsdClearString(&p
->device_uri
);
824 cupsdClearString(&p
->sanitized_device_uri
);
825 cupsdClearString(&p
->port_monitor
);
826 cupsdClearString(&p
->op_policy
);
827 cupsdClearString(&p
->error_policy
);
829 cupsdClearString(&p
->alert
);
830 cupsdClearString(&p
->alert_description
);
833 cupsdClearString(&p
->product
);
834 cupsdClearString(&p
->pdl
);
835 #endif /* HAVE_DNSSD */
837 cupsArrayDelete(p
->filetypes
);
840 free(p
->browse_attrs
);
842 cupsFreeOptions(p
->num_options
, p
->options
);
847 * Restore the previous position in the Printers array...
850 cupsArrayRestore(Printers
);
855 * 'cupsdFindDest()' - Find a destination in the list.
858 cupsd_printer_t
* /* O - Destination in list */
859 cupsdFindDest(const char *name
) /* I - Name of printer or class to find */
861 cupsd_printer_t key
; /* Search key */
864 key
.name
= (char *)name
;
865 return ((cupsd_printer_t
*)cupsArrayFind(Printers
, &key
));
870 * 'cupsdFindPrinter()' - Find a printer in the list.
873 cupsd_printer_t
* /* O - Printer in list */
874 cupsdFindPrinter(const char *name
) /* I - Name of printer to find */
876 cupsd_printer_t
*p
; /* Printer in list */
879 if ((p
= cupsdFindDest(name
)) != NULL
&& (p
->type
& CUPS_PRINTER_CLASS
))
887 * 'cupsdFreePrinterUsers()' - Free allow/deny users.
891 cupsdFreePrinterUsers(
892 cupsd_printer_t
*p
) /* I - Printer */
894 int i
; /* Looping var */
897 if (!p
|| !p
->num_users
)
900 for (i
= 0; i
< p
->num_users
; i
++)
901 free((void *)p
->users
[i
]);
911 * 'cupsdLoadAllPrinters()' - Load printers from the printers.conf file.
915 cupsdLoadAllPrinters(void)
917 int i
; /* Looping var */
918 cups_file_t
*fp
; /* printers.conf file */
919 int linenum
; /* Current line number */
920 char line
[4096], /* Line from file */
921 *value
, /* Pointer to value */
922 *valueptr
; /* Pointer into value */
923 cupsd_printer_t
*p
; /* Current printer */
927 * Open the printers.conf file...
930 snprintf(line
, sizeof(line
), "%s/printers.conf", ServerRoot
);
931 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
934 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open %s - %s", line
,
940 * Read printer configurations until we hit EOF...
946 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
949 * Decode the directive...
952 if (!strcasecmp(line
, "<Printer") ||
953 !strcasecmp(line
, "<DefaultPrinter"))
956 * <Printer name> or <DefaultPrinter name>
959 if (p
== NULL
&& value
)
962 * Add the printer and a base file type...
965 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Loading printer %s...", value
);
967 p
= cupsdAddPrinter(value
);
969 p
->state
= IPP_PRINTER_IDLE
;
972 * Set the default printer as needed...
975 if (!strcasecmp(line
, "<DefaultPrinter"))
979 cupsdLogMessage(CUPSD_LOG_ERROR
,
980 "Syntax error on line %d of printers.conf.", linenum
);
982 else if (!strcasecmp(line
, "</Printer>"))
987 * Close out the current printer...
990 cupsdSetPrinterAttrs(p
);
991 cupsdAddPrinterHistory(p
);
993 if (strncmp(p
->device_uri
, "file:", 5) &&
994 p
->state
!= IPP_PRINTER_STOPPED
)
997 * See if the backend exists...
1000 snprintf(line
, sizeof(line
), "%s/backend/%s", ServerBin
,
1003 if ((valueptr
= strchr(line
+ strlen(ServerBin
), ':')) != NULL
)
1004 *valueptr
= '\0'; /* Chop everything but URI scheme */
1006 if (access(line
, 0))
1009 * Backend does not exist, stop printer...
1012 p
->state
= IPP_PRINTER_STOPPED
;
1013 snprintf(p
->state_message
, sizeof(p
->state_message
),
1014 "Backend %s does not exist!", line
);
1021 cupsdLogMessage(CUPSD_LOG_ERROR
,
1022 "Syntax error on line %d of printers.conf.", linenum
);
1026 cupsdLogMessage(CUPSD_LOG_ERROR
,
1027 "Syntax error on line %d of printers.conf.", linenum
);
1029 else if (!strcasecmp(line
, "AuthInfoRequired"))
1031 if (!cupsdSetAuthInfoRequired(p
, value
, NULL
))
1032 cupsdLogMessage(CUPSD_LOG_ERROR
,
1033 "Bad AuthInfoRequired on line %d of printers.conf.",
1036 else if (!strcasecmp(line
, "Info"))
1039 cupsdSetString(&p
->info
, value
);
1041 else if (!strcasecmp(line
, "MakeModel"))
1044 cupsdSetString(&p
->make_model
, value
);
1046 else if (!strcasecmp(line
, "Location"))
1049 cupsdSetString(&p
->location
, value
);
1051 else if (!strcasecmp(line
, "DeviceURI"))
1054 cupsdSetDeviceURI(p
, value
);
1056 cupsdLogMessage(CUPSD_LOG_ERROR
,
1057 "Syntax error on line %d of printers.conf.", linenum
);
1059 else if (!strcasecmp(line
, "Option") && value
)
1065 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
1068 cupsdLogMessage(CUPSD_LOG_ERROR
,
1069 "Syntax error on line %d of printers.conf.", linenum
);
1072 for (; *valueptr
&& isspace(*valueptr
& 255); *valueptr
++ = '\0');
1074 p
->num_options
= cupsAddOption(value
, valueptr
, p
->num_options
,
1078 else if (!strcasecmp(line
, "PortMonitor"))
1080 if (value
&& strcmp(value
, "none"))
1081 cupsdSetString(&p
->port_monitor
, value
);
1083 cupsdClearString(&p
->port_monitor
);
1085 cupsdLogMessage(CUPSD_LOG_ERROR
,
1086 "Syntax error on line %d of printers.conf.", linenum
);
1088 else if (!strcasecmp(line
, "Reason"))
1091 strcmp(value
, "connecting-to-device") &&
1092 strcmp(value
, "cups-insecure-filter-warning") &&
1093 strcmp(value
, "cups-missing-filter-warning"))
1095 for (i
= 0 ; i
< p
->num_reasons
; i
++)
1096 if (!strcmp(value
, p
->reasons
[i
]))
1099 if (i
>= p
->num_reasons
&&
1100 p
->num_reasons
< (int)(sizeof(p
->reasons
) / sizeof(p
->reasons
[0])))
1102 p
->reasons
[p
->num_reasons
] = _cupsStrAlloc(value
);
1107 cupsdLogMessage(CUPSD_LOG_ERROR
,
1108 "Syntax error on line %d of printers.conf.", linenum
);
1110 else if (!strcasecmp(line
, "State"))
1113 * Set the initial queue state...
1116 if (value
&& !strcasecmp(value
, "idle"))
1117 p
->state
= IPP_PRINTER_IDLE
;
1118 else if (value
&& !strcasecmp(value
, "stopped"))
1120 p
->state
= IPP_PRINTER_STOPPED
;
1122 for (i
= 0 ; i
< p
->num_reasons
; i
++)
1123 if (!strcmp("paused", p
->reasons
[i
]))
1126 if (i
>= p
->num_reasons
&&
1127 p
->num_reasons
< (int)(sizeof(p
->reasons
) / sizeof(p
->reasons
[0])))
1129 p
->reasons
[p
->num_reasons
] = _cupsStrAlloc("paused");
1134 cupsdLogMessage(CUPSD_LOG_ERROR
,
1135 "Syntax error on line %d of printers.conf.", linenum
);
1137 else if (!strcasecmp(line
, "StateMessage"))
1140 * Set the initial queue state message...
1144 strlcpy(p
->state_message
, value
, sizeof(p
->state_message
));
1146 else if (!strcasecmp(line
, "StateTime"))
1149 * Set the state time...
1153 p
->state_time
= atoi(value
);
1155 else if (!strcasecmp(line
, "Accepting"))
1158 * Set the initial accepting state...
1162 (!strcasecmp(value
, "yes") ||
1163 !strcasecmp(value
, "on") ||
1164 !strcasecmp(value
, "true")))
1167 (!strcasecmp(value
, "no") ||
1168 !strcasecmp(value
, "off") ||
1169 !strcasecmp(value
, "false")))
1172 cupsdLogMessage(CUPSD_LOG_ERROR
,
1173 "Syntax error on line %d of printers.conf.", linenum
);
1175 else if (!strcasecmp(line
, "Type"))
1178 p
->type
= atoi(value
);
1180 cupsdLogMessage(CUPSD_LOG_ERROR
,
1181 "Syntax error on line %d of printers.conf.", linenum
);
1183 else if (!strcasecmp(line
, "Product"))
1188 p
->product
= _cupsStrAlloc(value
);
1189 #endif /* HAVE_DNSSD */
1192 cupsdLogMessage(CUPSD_LOG_ERROR
,
1193 "Syntax error on line %d of printers.conf.", linenum
);
1195 else if (!strcasecmp(line
, "Filter"))
1200 p
->filters
= cupsArrayNew(NULL
, NULL
);
1202 cupsArrayAdd(p
->filters
, _cupsStrAlloc(value
));
1205 cupsdLogMessage(CUPSD_LOG_ERROR
,
1206 "Syntax error on line %d of printers.conf.", linenum
);
1208 else if (!strcasecmp(line
, "PreFilter"))
1212 if (!p
->pre_filters
)
1213 p
->pre_filters
= cupsArrayNew(NULL
, NULL
);
1215 cupsArrayAdd(p
->pre_filters
, _cupsStrAlloc(value
));
1218 cupsdLogMessage(CUPSD_LOG_ERROR
,
1219 "Syntax error on line %d of printers.conf.", linenum
);
1221 else if (!strcasecmp(line
, "Shared"))
1224 * Set the initial shared state...
1228 (!strcasecmp(value
, "yes") ||
1229 !strcasecmp(value
, "on") ||
1230 !strcasecmp(value
, "true")))
1233 (!strcasecmp(value
, "no") ||
1234 !strcasecmp(value
, "off") ||
1235 !strcasecmp(value
, "false")))
1238 cupsdLogMessage(CUPSD_LOG_ERROR
,
1239 "Syntax error on line %d of printers.conf.", linenum
);
1241 else if (!strcasecmp(line
, "JobSheets"))
1244 * Set the initial job sheets...
1249 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
1254 cupsdSetString(&p
->job_sheets
[0], value
);
1256 while (isspace(*valueptr
& 255))
1261 for (value
= valueptr
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
1266 cupsdSetString(&p
->job_sheets
[1], value
);
1270 cupsdLogMessage(CUPSD_LOG_ERROR
,
1271 "Syntax error on line %d of printers.conf.", linenum
);
1273 else if (!strcasecmp(line
, "AllowUser"))
1278 cupsdAddPrinterUser(p
, value
);
1281 cupsdLogMessage(CUPSD_LOG_ERROR
,
1282 "Syntax error on line %d of printers.conf.", linenum
);
1284 else if (!strcasecmp(line
, "DenyUser"))
1289 cupsdAddPrinterUser(p
, value
);
1292 cupsdLogMessage(CUPSD_LOG_ERROR
,
1293 "Syntax error on line %d of printers.conf.", linenum
);
1295 else if (!strcasecmp(line
, "QuotaPeriod"))
1298 p
->quota_period
= atoi(value
);
1300 cupsdLogMessage(CUPSD_LOG_ERROR
,
1301 "Syntax error on line %d of printers.conf.", linenum
);
1303 else if (!strcasecmp(line
, "PageLimit"))
1306 p
->page_limit
= atoi(value
);
1308 cupsdLogMessage(CUPSD_LOG_ERROR
,
1309 "Syntax error on line %d of printers.conf.", linenum
);
1311 else if (!strcasecmp(line
, "KLimit"))
1314 p
->k_limit
= atoi(value
);
1316 cupsdLogMessage(CUPSD_LOG_ERROR
,
1317 "Syntax error on line %d of printers.conf.", linenum
);
1319 else if (!strcasecmp(line
, "OpPolicy"))
1323 cupsd_policy_t
*pol
; /* Policy */
1326 if ((pol
= cupsdFindPolicy(value
)) != NULL
)
1328 cupsdSetString(&p
->op_policy
, value
);
1329 p
->op_policy_ptr
= pol
;
1332 cupsdLogMessage(CUPSD_LOG_ERROR
,
1333 "Bad policy \"%s\" on line %d of printers.conf",
1337 cupsdLogMessage(CUPSD_LOG_ERROR
,
1338 "Syntax error on line %d of printers.conf.", linenum
);
1340 else if (!strcasecmp(line
, "ErrorPolicy"))
1343 cupsdSetString(&p
->error_policy
, value
);
1345 cupsdLogMessage(CUPSD_LOG_ERROR
,
1346 "Syntax error on line %d of printers.conf.", linenum
);
1348 else if (!strcasecmp(line
, "Attribute") && value
)
1350 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
1353 cupsdLogMessage(CUPSD_LOG_ERROR
,
1354 "Syntax error on line %d of printers.conf.", linenum
);
1357 for (; *valueptr
&& isspace(*valueptr
& 255); *valueptr
++ = '\0');
1360 cupsdSetPrinterAttrs(p
);
1362 if (!strcmp(value
, "marker-change-time"))
1363 p
->marker_time
= atoi(valueptr
);
1365 cupsdSetPrinterAttr(p
, value
, valueptr
);
1371 * Something else we don't understand...
1374 cupsdLogMessage(CUPSD_LOG_ERROR
,
1375 "Unknown configuration directive %s on line %d of printers.conf.",
1385 * 'cupsdRenamePrinter()' - Rename a printer.
1390 cupsd_printer_t
*p
, /* I - Printer */
1391 const char *name
) /* I - New name */
1394 * Remove the printer from the array(s) first...
1397 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1398 "cupsdRenamePrinter: Removing %s from Printers", p
->name
);
1399 cupsArrayRemove(Printers
, p
);
1401 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
1403 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1404 "cupsdRenamePrinter: Removing %s from ImplicitPrinters",
1406 cupsArrayRemove(ImplicitPrinters
, p
);
1410 * Rename the printer type...
1413 mimeDeleteType(MimeDatabase
, p
->filetype
);
1414 p
->filetype
= mimeAddType(MimeDatabase
, "printer", name
);
1416 mimeDeleteType(MimeDatabase
, p
->prefiltertype
);
1417 p
->prefiltertype
= mimeAddType(MimeDatabase
, "prefilter", name
);
1420 * Rename the printer...
1423 cupsdSetString(&p
->name
, name
);
1426 * Reset printer attributes...
1429 cupsdSetPrinterAttrs(p
);
1432 * Add the printer back to the printer array(s)...
1435 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1436 "cupsdRenamePrinter: Adding %s to Printers", p
->name
);
1437 cupsArrayAdd(Printers
, p
);
1439 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
1441 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1442 "cupsdRenamePrinter: Adding %s to ImplicitPrinters",
1444 cupsArrayAdd(ImplicitPrinters
, p
);
1450 * 'cupsdSaveAllPrinters()' - Save all printer definitions to the printers.conf
1455 cupsdSaveAllPrinters(void)
1457 int i
; /* Looping var */
1458 cups_file_t
*fp
; /* printers.conf file */
1459 char temp
[1024], /* Temporary string */
1460 backup
[1024], /* printers.conf.O file */
1461 value
[2048], /* Value string */
1462 *ptr
; /* Pointer into value */
1463 cupsd_printer_t
*printer
; /* Current printer class */
1464 time_t curtime
; /* Current time */
1465 struct tm
*curdate
; /* Current date */
1466 cups_option_t
*option
; /* Current option */
1467 ipp_attribute_t
*marker
; /* Current marker attribute */
1471 * Create the printers.conf file...
1474 snprintf(temp
, sizeof(temp
), "%s/printers.conf", ServerRoot
);
1475 snprintf(backup
, sizeof(backup
), "%s/printers.conf.O", ServerRoot
);
1477 if (rename(temp
, backup
))
1479 if (errno
!= ENOENT
)
1480 cupsdLogMessage(CUPSD_LOG_ERROR
,
1481 "Unable to backup printers.conf - %s", strerror(errno
));
1484 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
1486 cupsdLogMessage(CUPSD_LOG_ERROR
,
1487 "Unable to save printers.conf - %s", strerror(errno
));
1489 if (rename(backup
, temp
))
1490 cupsdLogMessage(CUPSD_LOG_ERROR
,
1491 "Unable to restore printers.conf - %s", strerror(errno
));
1495 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving printers.conf...");
1498 * Restrict access to the file...
1501 fchown(cupsFileNumber(fp
), getuid(), Group
);
1502 fchmod(cupsFileNumber(fp
), 0600);
1505 * Write a small header to the file...
1508 curtime
= time(NULL
);
1509 curdate
= localtime(&curtime
);
1510 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
1512 cupsFilePuts(fp
, "# Printer configuration file for " CUPS_SVERSION
"\n");
1513 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
1514 cupsFilePuts(fp
, "# DO NOT EDIT THIS FILE WHEN CUPSD IS RUNNING\n");
1517 * Write each local printer known to the system...
1520 for (printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1522 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1525 * Skip remote destinations and printer classes...
1528 if ((printer
->type
& CUPS_PRINTER_DISCOVERED
) ||
1529 (printer
->type
& CUPS_PRINTER_CLASS
) ||
1530 (printer
->type
& CUPS_PRINTER_IMPLICIT
))
1534 * Write printers as needed...
1537 if (printer
== DefaultPrinter
)
1538 cupsFilePrintf(fp
, "<DefaultPrinter %s>\n", printer
->name
);
1540 cupsFilePrintf(fp
, "<Printer %s>\n", printer
->name
);
1542 if (printer
->num_auth_info_required
> 0)
1544 switch (printer
->num_auth_info_required
)
1547 strlcpy(value
, printer
->auth_info_required
[0], sizeof(value
));
1551 snprintf(value
, sizeof(value
), "%s,%s",
1552 printer
->auth_info_required
[0],
1553 printer
->auth_info_required
[1]);
1558 snprintf(value
, sizeof(value
), "%s,%s,%s",
1559 printer
->auth_info_required
[0],
1560 printer
->auth_info_required
[1],
1561 printer
->auth_info_required
[2]);
1565 cupsFilePutConf(fp
, "AuthInfoRequired", value
);
1569 cupsFilePutConf(fp
, "Info", printer
->info
);
1571 if (printer
->location
)
1572 cupsFilePutConf(fp
, "Location", printer
->location
);
1574 if (printer
->make_model
)
1575 cupsFilePutConf(fp
, "MakeModel", printer
->make_model
);
1577 cupsFilePutConf(fp
, "DeviceURI", printer
->device_uri
);
1579 if (printer
->port_monitor
)
1580 cupsFilePutConf(fp
, "PortMonitor", printer
->port_monitor
);
1582 if (printer
->state
== IPP_PRINTER_STOPPED
)
1584 cupsFilePuts(fp
, "State Stopped\n");
1586 if (printer
->state_message
)
1587 cupsFilePutConf(fp
, "StateMessage", printer
->state_message
);
1590 cupsFilePuts(fp
, "State Idle\n");
1592 cupsFilePrintf(fp
, "StateTime %d\n", (int)printer
->state_time
);
1594 for (i
= 0; i
< printer
->num_reasons
; i
++)
1595 if (strcmp(printer
->reasons
[i
], "connecting-to-device") &&
1596 strcmp(printer
->reasons
[i
], "cups-insecure-filter-warning") &&
1597 strcmp(printer
->reasons
[i
], "cups-missing-filter-warning"))
1598 cupsFilePutConf(fp
, "Reason", printer
->reasons
[i
]);
1600 cupsFilePrintf(fp
, "Type %d\n", printer
->type
);
1603 if (printer
->product
)
1604 cupsFilePutConf(fp
, "Product", printer
->product
);
1605 #endif /* HAVE_DNSSD */
1607 for (ptr
= (char *)cupsArrayFirst(printer
->filters
);
1609 ptr
= (char *)cupsArrayNext(printer
->filters
))
1610 cupsFilePutConf(fp
, "Filter", ptr
);
1612 for (ptr
= (char *)cupsArrayFirst(printer
->pre_filters
);
1614 ptr
= (char *)cupsArrayNext(printer
->pre_filters
))
1615 cupsFilePutConf(fp
, "PreFilter", ptr
);
1617 if (printer
->accepting
)
1618 cupsFilePuts(fp
, "Accepting Yes\n");
1620 cupsFilePuts(fp
, "Accepting No\n");
1622 if (printer
->shared
)
1623 cupsFilePuts(fp
, "Shared Yes\n");
1625 cupsFilePuts(fp
, "Shared No\n");
1627 snprintf(value
, sizeof(value
), "%s %s", printer
->job_sheets
[0],
1628 printer
->job_sheets
[1]);
1629 cupsFilePutConf(fp
, "JobSheets", value
);
1631 cupsFilePrintf(fp
, "QuotaPeriod %d\n", printer
->quota_period
);
1632 cupsFilePrintf(fp
, "PageLimit %d\n", printer
->page_limit
);
1633 cupsFilePrintf(fp
, "KLimit %d\n", printer
->k_limit
);
1635 for (i
= 0; i
< printer
->num_users
; i
++)
1636 cupsFilePutConf(fp
, printer
->deny_users
? "DenyUser" : "AllowUser",
1639 if (printer
->op_policy
)
1640 cupsFilePutConf(fp
, "OpPolicy", printer
->op_policy
);
1641 if (printer
->error_policy
)
1642 cupsFilePutConf(fp
, "ErrorPolicy", printer
->error_policy
);
1644 for (i
= printer
->num_options
, option
= printer
->options
;
1648 snprintf(value
, sizeof(value
), "%s %s", option
->name
, option
->value
);
1649 cupsFilePutConf(fp
, "Option", value
);
1652 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-colors",
1653 IPP_TAG_NAME
)) != NULL
)
1655 snprintf(value
, sizeof(value
), "%s ", marker
->name
);
1657 for (i
= 0, ptr
= value
+ strlen(value
);
1658 i
< marker
->num_values
&& ptr
< (value
+ sizeof(value
) - 1);
1664 strlcpy(ptr
, marker
->values
[i
].string
.text
,
1665 value
+ sizeof(value
) - ptr
);
1670 cupsFilePutConf(fp
, "Attribute", value
);
1673 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-levels",
1674 IPP_TAG_INTEGER
)) != NULL
)
1676 cupsFilePrintf(fp
, "Attribute %s %d", marker
->name
,
1677 marker
->values
[0].integer
);
1678 for (i
= 1; i
< marker
->num_values
; i
++)
1679 cupsFilePrintf(fp
, ",%d", marker
->values
[i
].integer
);
1680 cupsFilePuts(fp
, "\n");
1683 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-low-levels",
1684 IPP_TAG_INTEGER
)) != NULL
)
1686 cupsFilePrintf(fp
, "Attribute %s %d", marker
->name
,
1687 marker
->values
[0].integer
);
1688 for (i
= 1; i
< marker
->num_values
; i
++)
1689 cupsFilePrintf(fp
, ",%d", marker
->values
[i
].integer
);
1690 cupsFilePuts(fp
, "\n");
1693 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-high-levels",
1694 IPP_TAG_INTEGER
)) != NULL
)
1696 cupsFilePrintf(fp
, "Attribute %s %d", marker
->name
,
1697 marker
->values
[0].integer
);
1698 for (i
= 1; i
< marker
->num_values
; i
++)
1699 cupsFilePrintf(fp
, ",%d", marker
->values
[i
].integer
);
1700 cupsFilePuts(fp
, "\n");
1703 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-message",
1704 IPP_TAG_TEXT
)) != NULL
)
1706 snprintf(value
, sizeof(value
), "%s %s", marker
->name
,
1707 marker
->values
[0].string
.text
);
1709 cupsFilePutConf(fp
, "Attribute", value
);
1712 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-names",
1713 IPP_TAG_NAME
)) != NULL
)
1715 snprintf(value
, sizeof(value
), "%s ", marker
->name
);
1717 for (i
= 0, ptr
= value
+ strlen(value
);
1718 i
< marker
->num_values
&& ptr
< (value
+ sizeof(value
) - 1);
1724 strlcpy(ptr
, marker
->values
[i
].string
.text
,
1725 value
+ sizeof(value
) - ptr
);
1730 cupsFilePutConf(fp
, "Attribute", value
);
1733 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-types",
1734 IPP_TAG_KEYWORD
)) != NULL
)
1736 snprintf(value
, sizeof(value
), "%s ", marker
->name
);
1738 for (i
= 0, ptr
= value
+ strlen(value
);
1739 i
< marker
->num_values
&& ptr
< (value
+ sizeof(value
) - 1);
1745 strlcpy(ptr
, marker
->values
[i
].string
.text
,
1746 value
+ sizeof(value
) - ptr
);
1751 cupsFilePutConf(fp
, "Attribute", value
);
1754 if (printer
->marker_time
)
1755 cupsFilePrintf(fp
, "Attribute marker-change-time %ld\n",
1756 (long)printer
->marker_time
);
1758 cupsFilePuts(fp
, "</Printer>\n");
1762 * Make IRIX desktop & printer status happy
1765 write_irix_state(printer
);
1774 * 'cupsdSetAuthInfoRequired()' - Set the required authentication info.
1777 int /* O - 1 if value OK, 0 otherwise */
1778 cupsdSetAuthInfoRequired(
1779 cupsd_printer_t
*p
, /* I - Printer */
1780 const char *values
, /* I - Plain text value (or NULL) */
1781 ipp_attribute_t
*attr
) /* I - IPP attribute value (or NULL) */
1783 int i
; /* Looping var */
1786 p
->num_auth_info_required
= 0;
1789 * Do we have a plain text value?
1795 * Yes, grab the keywords...
1798 const char *end
; /* End of current value */
1801 while (*values
&& p
->num_auth_info_required
< 4)
1803 if ((end
= strchr(values
, ',')) == NULL
)
1804 end
= values
+ strlen(values
);
1806 if ((end
- values
) == 4 && !strncmp(values
, "none", 4))
1808 if (p
->num_auth_info_required
!= 0 || *end
)
1811 p
->auth_info_required
[p
->num_auth_info_required
] = "none";
1812 p
->num_auth_info_required
++;
1816 else if ((end
- values
) == 9 && !strncmp(values
, "negotiate", 9))
1818 if (p
->num_auth_info_required
!= 0 || *end
)
1821 p
->auth_info_required
[p
->num_auth_info_required
] = "negotiate";
1822 p
->num_auth_info_required
++;
1824 else if ((end
- values
) == 6 && !strncmp(values
, "domain", 6))
1826 p
->auth_info_required
[p
->num_auth_info_required
] = "domain";
1827 p
->num_auth_info_required
++;
1829 else if ((end
- values
) == 8 && !strncmp(values
, "password", 8))
1831 p
->auth_info_required
[p
->num_auth_info_required
] = "password";
1832 p
->num_auth_info_required
++;
1834 else if ((end
- values
) == 8 && !strncmp(values
, "username", 8))
1836 p
->auth_info_required
[p
->num_auth_info_required
] = "username";
1837 p
->num_auth_info_required
++;
1842 values
= (*end
) ? end
+ 1 : end
;
1845 if (p
->num_auth_info_required
== 0)
1847 p
->auth_info_required
[0] = "none";
1848 p
->num_auth_info_required
= 1;
1852 * Update the printer-type value as needed...
1855 if (p
->num_auth_info_required
> 1 ||
1856 strcmp(p
->auth_info_required
[0], "none"))
1857 p
->type
|= CUPS_PRINTER_AUTHENTICATED
;
1859 p
->type
&= ~CUPS_PRINTER_AUTHENTICATED
;
1865 * Grab values from an attribute instead...
1868 if (!attr
|| attr
->num_values
> 4)
1872 * Update the printer-type value as needed...
1875 if (attr
->num_values
> 1 ||
1876 strcmp(attr
->values
[0].string
.text
, "none"))
1877 p
->type
|= CUPS_PRINTER_AUTHENTICATED
;
1879 p
->type
&= ~CUPS_PRINTER_AUTHENTICATED
;
1881 for (i
= 0; i
< attr
->num_values
; i
++)
1883 if (!strcmp(attr
->values
[i
].string
.text
, "none"))
1885 if (p
->num_auth_info_required
!= 0 || attr
->num_values
!= 1)
1888 p
->auth_info_required
[p
->num_auth_info_required
] = "none";
1889 p
->num_auth_info_required
++;
1893 else if (!strcmp(attr
->values
[i
].string
.text
, "negotiate"))
1895 if (p
->num_auth_info_required
!= 0 || attr
->num_values
!= 1)
1898 p
->auth_info_required
[p
->num_auth_info_required
] = "negotiate";
1899 p
->num_auth_info_required
++;
1903 else if (!strcmp(attr
->values
[i
].string
.text
, "domain"))
1905 p
->auth_info_required
[p
->num_auth_info_required
] = "domain";
1906 p
->num_auth_info_required
++;
1908 else if (!strcmp(attr
->values
[i
].string
.text
, "password"))
1910 p
->auth_info_required
[p
->num_auth_info_required
] = "password";
1911 p
->num_auth_info_required
++;
1913 else if (!strcmp(attr
->values
[i
].string
.text
, "username"))
1915 p
->auth_info_required
[p
->num_auth_info_required
] = "username";
1916 p
->num_auth_info_required
++;
1927 * 'cupsdSetDeviceURI()' - Set the device URI for a printer.
1931 cupsdSetDeviceURI(cupsd_printer_t
*p
, /* I - Printer */
1932 const char *uri
) /* I - Device URI */
1934 char buffer
[1024], /* URI buffer */
1935 *start
, /* Start of data after scheme */
1936 *slash
, /* First slash after scheme:// */
1937 *ptr
; /* Pointer into user@host:port part */
1941 * Set the full device URI..
1944 cupsdSetString(&(p
->device_uri
), uri
);
1947 * Copy the device URI to a temporary buffer so we can sanitize any auth
1951 strlcpy(buffer
, uri
, sizeof(buffer
));
1954 * Find the end of the scheme:// part...
1957 if ((ptr
= strchr(buffer
, ':')) != NULL
)
1959 for (start
= ptr
+ 1; *start
; start
++)
1964 * Find the next slash (/) in the URI...
1967 if ((slash
= strchr(start
, '/')) == NULL
)
1968 slash
= start
+ strlen(start
); /* No slash, point to the end */
1971 * Check for an @ sign before the slash...
1974 if ((ptr
= strchr(start
, '@')) != NULL
&& ptr
< slash
)
1977 * Found an @ sign and it is before the resource part, so we have
1978 * an authentication string. Copy the remaining URI over the
1979 * authentication string...
1982 _cups_strcpy(start
, ptr
+ 1);
1987 * Save the sanitized URI...
1990 cupsdSetString(&(p
->sanitized_device_uri
), buffer
);
1995 * 'cupsdSetPrinterAttr()' - Set a printer attribute.
1999 cupsdSetPrinterAttr(
2000 cupsd_printer_t
*p
, /* I - Printer */
2001 const char *name
, /* I - Attribute name */
2002 char *value
) /* I - Attribute value string */
2004 ipp_attribute_t
*attr
; /* Attribute */
2005 int i
, /* Looping var */
2006 count
; /* Number of values */
2007 char *ptr
; /* Pointer into value */
2008 ipp_tag_t value_tag
; /* Value tag for this attribute */
2012 * Don't allow empty values...
2015 if (!*value
&& strcmp(name
, "marker-message"))
2017 cupsdLogMessage(CUPSD_LOG_ERROR
, "Ignoring empty \"%s\" attribute", name
);
2022 * Count the number of values...
2025 for (count
= 1, ptr
= value
;
2026 (ptr
= strchr(ptr
, ',')) != NULL
;
2030 * Then add or update the attribute as needed...
2033 if (!strcmp(name
, "marker-levels") || !strcmp(name
, "marker-low-levels") ||
2034 !strcmp(name
, "marker-high-levels"))
2040 if ((attr
= ippFindAttribute(p
->attrs
, name
, IPP_TAG_INTEGER
)) != NULL
&&
2041 attr
->num_values
< count
)
2043 ippDeleteAttribute(p
->attrs
, attr
);
2048 attr
->num_values
= count
;
2050 attr
= ippAddIntegers(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, 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
].integer
= strtol(value
, NULL
, 10);
2075 * Name or keyword values...
2078 if (!strcmp(name
, "marker-types"))
2079 value_tag
= IPP_TAG_KEYWORD
;
2080 else if (!strcmp(name
, "marker-message"))
2081 value_tag
= IPP_TAG_TEXT
;
2083 value_tag
= IPP_TAG_NAME
;
2085 if ((attr
= ippFindAttribute(p
->attrs
, name
, value_tag
)) != NULL
&&
2086 attr
->num_values
< count
)
2088 ippDeleteAttribute(p
->attrs
, attr
);
2094 for (i
= 0; i
< attr
->num_values
; i
++)
2095 _cupsStrFree(attr
->values
[i
].string
.text
);
2097 attr
->num_values
= count
;
2100 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, value_tag
, name
,
2105 cupsdLogMessage(CUPSD_LOG_ERROR
,
2106 "Unable to allocate memory for printer attribute "
2107 "(%d values)", count
);
2111 for (i
= 0; i
< count
; i
++)
2113 if ((ptr
= strchr(value
, ',')) != NULL
)
2116 attr
->values
[i
].string
.text
= _cupsStrAlloc(value
);
2126 * 'cupsdSetPrinterAttrs()' - Set printer attributes based upon the PPD file.
2130 cupsdSetPrinterAttrs(cupsd_printer_t
*p
)/* I - Printer to setup */
2132 int i
, /* Looping var */
2133 length
; /* Length of browse attributes */
2134 char resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2135 int num_air
; /* Number of auth-info-required values */
2136 const char * const *air
; /* auth-info-required values */
2137 cupsd_location_t
*auth
; /* Pointer to authentication element */
2138 const char *auth_supported
; /* Authentication supported */
2139 ipp_t
*oldattrs
; /* Old printer attributes */
2140 ipp_attribute_t
*attr
; /* Attribute data */
2141 cups_option_t
*option
; /* Current printer option */
2142 char *filter
; /* Current filter */
2143 static const char * const air_none
[] =
2144 { /* No authentication */
2147 static const char * const air_userpass
[] =
2148 { /* Basic/Digest authentication */
2154 DEBUG_printf(("cupsdSetPrinterAttrs: entering name = %s, type = %x\n", p
->name
,
2158 * Make sure that we have the common attributes defined...
2162 cupsdCreateCommonData();
2165 * Clear out old filters, if any...
2168 delete_printer_filters(p
);
2171 * Figure out the authentication that is required for the printer.
2174 auth_supported
= "requesting-user-name";
2178 if (p
->num_auth_info_required
> 0 && strcmp(p
->auth_info_required
[0], "none"))
2180 num_air
= p
->num_auth_info_required
;
2181 air
= p
->auth_info_required
;
2183 else if ((p
->type
& CUPS_PRINTER_AUTHENTICATED
) &&
2184 (p
->type
& CUPS_PRINTER_DISCOVERED
))
2190 if (p
->type
& CUPS_PRINTER_CLASS
)
2191 snprintf(resource
, sizeof(resource
), "/classes/%s", p
->name
);
2193 snprintf(resource
, sizeof(resource
), "/printers/%s", p
->name
);
2195 if ((auth
= cupsdFindBest(resource
, HTTP_POST
)) == NULL
||
2196 auth
->type
== CUPSD_AUTH_NONE
)
2197 auth
= cupsdFindPolicyOp(p
->op_policy_ptr
, IPP_PRINT_JOB
);
2201 int auth_type
; /* Authentication type */
2204 if ((auth_type
= auth
->type
) == CUPSD_AUTH_DEFAULT
)
2205 auth_type
= DefaultAuthType
;
2207 if (auth_type
== CUPSD_AUTH_BASIC
|| auth_type
== CUPSD_AUTH_BASICDIGEST
)
2208 auth_supported
= "basic";
2209 else if (auth_type
== CUPSD_AUTH_DIGEST
)
2210 auth_supported
= "digest";
2212 else if (auth_type
== CUPSD_AUTH_NEGOTIATE
)
2213 auth_supported
= "negotiate";
2214 #endif /* HAVE_GSSAPI */
2216 if (!(p
->type
& CUPS_PRINTER_DISCOVERED
))
2218 if (auth_type
!= CUPSD_AUTH_NONE
)
2219 p
->type
|= CUPS_PRINTER_AUTHENTICATED
;
2221 p
->type
&= ~CUPS_PRINTER_AUTHENTICATED
;
2224 else if (!(p
->type
& CUPS_PRINTER_DISCOVERED
))
2225 p
->type
&= ~CUPS_PRINTER_AUTHENTICATED
;
2228 * Create the required IPP attributes for a printer...
2231 oldattrs
= p
->attrs
;
2232 p
->attrs
= ippNew();
2234 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2235 "uri-authentication-supported", NULL
, auth_supported
);
2236 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2237 "uri-security-supported", NULL
, "none");
2238 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "printer-name", NULL
,
2240 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-location",
2241 NULL
, p
->location
? p
->location
: "");
2242 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-info",
2243 NULL
, p
->info
? p
->info
: "");
2248 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2249 "requesting-user-name-denied", p
->num_users
, NULL
,
2252 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2253 "requesting-user-name-allowed", p
->num_users
, NULL
,
2257 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2258 "job-quota-period", p
->quota_period
);
2259 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2260 "job-k-limit", p
->k_limit
);
2261 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2262 "job-page-limit", p
->page_limit
);
2263 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2264 "auth-info-required", num_air
, NULL
, air
);
2266 if (cupsArrayCount(Banners
) > 0 && !(p
->type
& CUPS_PRINTER_DISCOVERED
))
2269 * Setup the job-sheets-default attribute...
2272 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2273 "job-sheets-default", 2, NULL
, NULL
);
2277 attr
->values
[0].string
.text
= _cupsStrAlloc(Classification
?
2278 Classification
: p
->job_sheets
[0]);
2279 attr
->values
[1].string
.text
= _cupsStrAlloc(Classification
?
2280 Classification
: p
->job_sheets
[1]);
2287 if (p
->type
& CUPS_PRINTER_DISCOVERED
)
2290 * Tell the client this is a remote printer of some type...
2293 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
2294 "printer-uri-supported", NULL
, p
->uri
);
2296 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "printer-more-info",
2300 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
2301 "printer-make-and-model", NULL
, p
->make_model
);
2303 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri", NULL
,
2312 * Assign additional attributes depending on whether this is a printer
2316 if (p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
2319 p
->type
&= ~CUPS_PRINTER_OPTIONS
;
2322 * Add class-specific attributes...
2325 if ((p
->type
& CUPS_PRINTER_IMPLICIT
) && p
->num_printers
> 0 &&
2326 p
->printers
[0]->make_model
)
2327 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
2328 "printer-make-and-model", NULL
, p
->printers
[0]->make_model
);
2330 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
2331 "printer-make-and-model", NULL
, "Local Printer Class");
2333 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri", NULL
,
2334 "file:///dev/null");
2336 if (p
->num_printers
> 0)
2339 * Add a list of member names; URIs are added in copy_printer_attrs...
2342 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2343 "member-names", p
->num_printers
, NULL
, NULL
);
2344 p
->type
|= CUPS_PRINTER_OPTIONS
;
2346 for (i
= 0; i
< p
->num_printers
; i
++)
2349 attr
->values
[i
].string
.text
= _cupsStrRetain(p
->printers
[i
]->name
);
2351 p
->type
&= ~CUPS_PRINTER_OPTIONS
| p
->printers
[i
]->type
;
2358 * Add printer-specific attributes...
2361 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri", NULL
,
2362 p
->sanitized_device_uri
);
2365 * Assign additional attributes from the PPD file (if any)...
2371 * Add filters for printer...
2374 cupsdSetPrinterReasons(p
, "-cups-missing-filter-warning,"
2375 "cups-insecure-filter-warning");
2377 for (filter
= (char *)cupsArrayFirst(p
->filters
);
2379 filter
= (char *)cupsArrayNext(p
->filters
))
2380 add_printer_filter(p
, p
->filetype
, filter
);
2384 p
->prefiltertype
= mimeAddType(MimeDatabase
, "prefilter", p
->name
);
2386 for (filter
= (char *)cupsArrayFirst(p
->pre_filters
);
2388 filter
= (char *)cupsArrayNext(p
->pre_filters
))
2389 add_printer_filter(p
, p
->prefiltertype
, filter
);
2395 * Copy marker attributes as needed...
2400 ipp_attribute_t
*oldattr
; /* Old attribute */
2403 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-colors",
2404 IPP_TAG_NAME
)) != NULL
)
2406 if ((attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2407 "marker-colors", oldattr
->num_values
, NULL
,
2410 for (i
= 0; i
< oldattr
->num_values
; i
++)
2411 attr
->values
[i
].string
.text
=
2412 _cupsStrRetain(oldattr
->values
[i
].string
.text
);
2416 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-levels",
2417 IPP_TAG_INTEGER
)) != NULL
)
2419 if ((attr
= ippAddIntegers(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2420 "marker-levels", oldattr
->num_values
,
2423 for (i
= 0; i
< oldattr
->num_values
; i
++)
2424 attr
->values
[i
].integer
= oldattr
->values
[i
].integer
;
2428 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-message",
2429 IPP_TAG_TEXT
)) != NULL
)
2430 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "marker-message",
2431 NULL
, oldattr
->values
[0].string
.text
);
2433 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-low-levels",
2434 IPP_TAG_INTEGER
)) != NULL
)
2436 if ((attr
= ippAddIntegers(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2437 "marker-low-levels", oldattr
->num_values
,
2440 for (i
= 0; i
< oldattr
->num_values
; i
++)
2441 attr
->values
[i
].integer
= oldattr
->values
[i
].integer
;
2445 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-high-levels",
2446 IPP_TAG_INTEGER
)) != NULL
)
2448 if ((attr
= ippAddIntegers(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2449 "marker-high-levels", oldattr
->num_values
,
2452 for (i
= 0; i
< oldattr
->num_values
; i
++)
2453 attr
->values
[i
].integer
= oldattr
->values
[i
].integer
;
2457 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-names",
2458 IPP_TAG_NAME
)) != NULL
)
2460 if ((attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2461 "marker-names", oldattr
->num_values
, NULL
,
2464 for (i
= 0; i
< oldattr
->num_values
; i
++)
2465 attr
->values
[i
].string
.text
=
2466 _cupsStrRetain(oldattr
->values
[i
].string
.text
);
2470 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-types",
2471 IPP_TAG_KEYWORD
)) != NULL
)
2473 if ((attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2474 "marker-types", oldattr
->num_values
, NULL
,
2477 for (i
= 0; i
< oldattr
->num_values
; i
++)
2478 attr
->values
[i
].string
.text
=
2479 _cupsStrRetain(oldattr
->values
[i
].string
.text
);
2483 ippDelete(oldattrs
);
2487 * Force sharing off for remote queues...
2490 if (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
))
2495 * Copy the printer options into a browse attributes string we can re-use.
2498 const char *valptr
; /* Pointer into value */
2499 char *attrptr
; /* Pointer into attribute string */
2503 * Free the old browse attributes as needed...
2506 if (p
->browse_attrs
)
2507 free(p
->browse_attrs
);
2510 * Compute the length of all attributes + job-sheets, lease-duration,
2511 * and BrowseLocalOptions.
2514 for (length
= 1, i
= p
->num_options
, option
= p
->options
;
2518 length
+= strlen(option
->name
) + 2;
2522 for (valptr
= option
->value
; *valptr
; valptr
++)
2523 if (strchr(" \"\'\\", *valptr
))
2530 length
+= 13 + strlen(p
->job_sheets
[0]) + strlen(p
->job_sheets
[1]);
2532 if (BrowseLocalOptions
)
2533 length
+= 12 + strlen(BrowseLocalOptions
);
2536 * Allocate the new string...
2539 if ((p
->browse_attrs
= calloc(1, length
)) == NULL
)
2540 cupsdLogMessage(CUPSD_LOG_ERROR
,
2541 "Unable to allocate %d bytes for browse data!",
2546 * Got the allocated string, now copy the options and attributes over...
2549 sprintf(p
->browse_attrs
, "job-sheets=%s,%s lease-duration=%d",
2550 p
->job_sheets
[0], p
->job_sheets
[1], BrowseTimeout
);
2551 attrptr
= p
->browse_attrs
+ strlen(p
->browse_attrs
);
2553 if (BrowseLocalOptions
)
2555 sprintf(attrptr
, " ipp-options=%s", BrowseLocalOptions
);
2556 attrptr
+= strlen(attrptr
);
2559 for (i
= p
->num_options
, option
= p
->options
;
2564 strcpy(attrptr
, option
->name
);
2565 attrptr
+= strlen(attrptr
);
2571 for (valptr
= option
->value
; *valptr
; valptr
++)
2573 if (strchr(" \"\'\\", *valptr
))
2576 *attrptr
++ = *valptr
;
2584 * Populate the document-format-supported attribute...
2587 add_printer_formats(p
);
2589 DEBUG_printf(("cupsdSetPrinterAttrs: leaving name = %s, type = %x\n", p
->name
,
2593 * Add name-default attributes...
2596 add_printer_defaults(p
);
2600 * Write the IRIX printer config and status files...
2603 write_irix_config(p
);
2604 write_irix_state(p
);
2608 * Let the browse protocols reflect the change
2611 cupsdRegisterPrinter(p
);
2616 * 'cupsdSetPrinterReasons()' - Set/update the reasons strings.
2619 int /* O - 1 if something changed, 0 otherwise */
2620 cupsdSetPrinterReasons(
2621 cupsd_printer_t
*p
, /* I - Printer */
2622 const char *s
) /* I - Reasons strings */
2624 int i
, /* Looping var */
2625 changed
= 0; /* Did something change? */
2626 const char *sptr
; /* Pointer into reasons */
2627 char reason
[255], /* Reason string */
2628 *rptr
; /* Pointer into reason */
2631 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2632 "cupsdSetPrinterReasons(p=%p(%s),s=\"%s\"", p
, p
->name
, s
);
2634 if (s
[0] == '-' || s
[0] == '+')
2637 * Add/remove reasons...
2645 * Replace reasons...
2650 for (i
= 0; i
< p
->num_reasons
; i
++)
2651 _cupsStrFree(p
->reasons
[i
]);
2656 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
2658 if (PrintcapFormat
== PRINTCAP_PLIST
)
2659 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2662 if (!strcmp(s
, "none"))
2666 * Loop through all of the reasons...
2672 * Skip leading whitespace and commas...
2675 while (isspace(*sptr
& 255) || *sptr
== ',')
2678 for (rptr
= reason
; *sptr
&& !isspace(*sptr
& 255) && *sptr
!= ','; sptr
++)
2679 if (rptr
< (reason
+ sizeof(reason
) - 1))
2693 for (i
= 0; i
< p
->num_reasons
; i
++)
2694 if (!strcmp(reason
, p
->reasons
[i
]))
2697 * Found a match, so remove it...
2702 _cupsStrFree(p
->reasons
[i
]);
2704 if (i
< p
->num_reasons
)
2705 memmove(p
->reasons
+ i
, p
->reasons
+ i
+ 1,
2706 (p
->num_reasons
- i
) * sizeof(char *));
2708 if (!strcmp(reason
, "paused") && p
->state
== IPP_PRINTER_STOPPED
)
2709 cupsdSetPrinterState(p
, IPP_PRINTER_IDLE
, 1);
2711 if (strcmp(reason
, "connecting-to-device"))
2713 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
2715 if (PrintcapFormat
== PRINTCAP_PLIST
)
2716 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2721 else if (p
->num_reasons
< (int)(sizeof(p
->reasons
) / sizeof(p
->reasons
[0])))
2727 for (i
= 0; i
< p
->num_reasons
; i
++)
2728 if (!strcmp(reason
, p
->reasons
[i
]))
2731 if (i
>= p
->num_reasons
)
2733 if (i
>= (int)(sizeof(p
->reasons
) / sizeof(p
->reasons
[0])))
2735 cupsdLogMessage(CUPSD_LOG_ALERT
,
2736 "Too many printer-state-reasons values for %s (%d)",
2741 p
->reasons
[i
] = _cupsStrAlloc(reason
);
2745 if (!strcmp(reason
, "paused") && p
->state
!= IPP_PRINTER_STOPPED
)
2746 cupsdSetPrinterState(p
, IPP_PRINTER_STOPPED
, 1);
2748 if (strcmp(reason
, "connecting-to-device"))
2750 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
2752 if (PrintcapFormat
== PRINTCAP_PLIST
)
2753 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2764 * 'cupsdSetPrinterState()' - Update the current state of a printer.
2768 cupsdSetPrinterState(
2769 cupsd_printer_t
*p
, /* I - Printer to change */
2770 ipp_pstate_t s
, /* I - New state */
2771 int update
) /* I - Update printers.conf? */
2773 ipp_pstate_t old_state
; /* Old printer state */
2777 * Can't set status of remote printers...
2780 if (p
->type
& CUPS_PRINTER_DISCOVERED
)
2784 * Set the new state...
2787 if (PrintcapFormat
== PRINTCAP_PLIST
)
2788 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2790 old_state
= p
->state
;
2795 cupsdAddEvent(s
== IPP_PRINTER_STOPPED
? CUPSD_EVENT_PRINTER_STOPPED
:
2796 CUPSD_EVENT_PRINTER_STATE
, p
, NULL
,
2797 "%s \"%s\" state changed.",
2798 (p
->type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
2802 * Let the browse code know this needs to be updated...
2806 p
->state_time
= time(NULL
);
2810 write_irix_state(p
);
2815 * Set/clear the paused reason as needed...
2818 if (s
== IPP_PRINTER_STOPPED
)
2819 cupsdSetPrinterReasons(p
, "+paused");
2821 cupsdSetPrinterReasons(p
, "-paused");
2824 * Clear the message for the queue when going to processing...
2827 if (s
== IPP_PRINTER_PROCESSING
)
2828 p
->state_message
[0] = '\0';
2831 * Update the printer history...
2834 cupsdAddPrinterHistory(p
);
2837 * Let the browse protocols reflect the change...
2841 cupsdRegisterPrinter(p
);
2844 * Save the printer configuration if a printer goes from idle or processing
2845 * to stopped (or visa-versa)...
2849 (old_state
== IPP_PRINTER_STOPPED
) != (s
== IPP_PRINTER_STOPPED
))
2851 if (p
->type
& CUPS_PRINTER_CLASS
)
2852 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
2854 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
2860 * 'cupsdStopPrinter()' - Stop a printer from printing any jobs...
2864 cupsdStopPrinter(cupsd_printer_t
*p
, /* I - Printer to stop */
2865 int update
)/* I - Update printers.conf? */
2868 * Set the printer state...
2871 cupsdSetPrinterState(p
, IPP_PRINTER_STOPPED
, update
);
2874 * See if we have a job printing on this printer...
2877 if (p
->job
&& p
->job
->state_value
== IPP_JOB_PROCESSING
)
2878 cupsdSetJobState(p
->job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
2879 "Job stopped due to printer being paused.");
2884 * 'cupsdUpdatePrinterPPD()' - Update keywords in a printer's PPD file.
2887 int /* O - 1 if successful, 0 otherwise */
2888 cupsdUpdatePrinterPPD(
2889 cupsd_printer_t
*p
, /* I - Printer */
2890 int num_keywords
, /* I - Number of keywords */
2891 cups_option_t
*keywords
) /* I - Keywords */
2893 int i
; /* Looping var */
2894 cups_file_t
*src
, /* Original file */
2895 *dst
; /* New file */
2896 char srcfile
[1024], /* Original filename */
2897 dstfile
[1024], /* New filename */
2898 line
[1024], /* Line from file */
2899 keystring
[41]; /* Keyword from line */
2900 cups_option_t
*keyword
; /* Current keyword */
2903 cupsdLogMessage(CUPSD_LOG_INFO
, "Updating keywords in PPD file for %s...",
2907 * Get the old and new PPD filenames...
2910 snprintf(srcfile
, sizeof(srcfile
), "%s/ppd/%s.ppd.O", ServerRoot
, p
->name
);
2911 snprintf(dstfile
, sizeof(srcfile
), "%s/ppd/%s.ppd", ServerRoot
, p
->name
);
2914 * Rename the old file and open the old and new...
2917 if (rename(dstfile
, srcfile
))
2919 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to backup PPD file for %s: %s",
2920 p
->name
, strerror(errno
));
2924 if ((src
= cupsFileOpen(srcfile
, "r")) == NULL
)
2926 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open PPD file \"%s\": %s",
2927 srcfile
, strerror(errno
));
2928 rename(srcfile
, dstfile
);
2932 if ((dst
= cupsFileOpen(dstfile
, "w")) == NULL
)
2934 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create PPD file \"%s\": %s",
2935 dstfile
, strerror(errno
));
2937 rename(srcfile
, dstfile
);
2942 * Copy the first line and then write out all of the keywords...
2945 if (!cupsFileGets(src
, line
, sizeof(line
)))
2947 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to read PPD file \"%s\": %s",
2948 srcfile
, strerror(errno
));
2951 rename(srcfile
, dstfile
);
2955 cupsFilePrintf(dst
, "%s\n", line
);
2957 for (i
= num_keywords
, keyword
= keywords
; i
> 0; i
--, keyword
++)
2959 cupsdLogMessage(CUPSD_LOG_DEBUG
, "*%s: %s", keyword
->name
, keyword
->value
);
2960 cupsFilePrintf(dst
, "*%s: %s\n", keyword
->name
, keyword
->value
);
2964 * Then copy the rest of the PPD file, dropping any keywords we changed.
2967 while (cupsFileGets(src
, line
, sizeof(line
)))
2970 * Skip keywords we've already set...
2973 if (sscanf(line
, "*%40[^:]:", keystring
) == 1 &&
2974 cupsGetOption(keystring
, num_keywords
, keywords
))
2978 * Otherwise write the line...
2981 cupsFilePrintf(dst
, "%s\n", line
);
2985 * Close files and return...
2996 * 'cupsdUpdatePrinters()' - Update printers after a partial reload.
3000 cupsdUpdatePrinters(void)
3002 cupsd_printer_t
*p
; /* Current printer */
3006 * Loop through the printers and recreate the printer attributes
3007 * for any local printers since the policy and/or access control
3008 * stuff may have changed. Also, if browsing is disabled, remove
3009 * any remote printers...
3012 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3014 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3017 * Remove remote printers if we are no longer browsing...
3021 (p
->type
& (CUPS_PRINTER_IMPLICIT
| CUPS_PRINTER_DISCOVERED
)))
3023 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
3024 cupsArrayRemove(ImplicitPrinters
, p
);
3026 cupsArraySave(Printers
);
3027 cupsdDeletePrinter(p
, 0);
3028 cupsArrayRestore(Printers
);
3033 * Update the operation policy pointer...
3036 if ((p
->op_policy_ptr
= cupsdFindPolicy(p
->op_policy
)) == NULL
)
3037 p
->op_policy_ptr
= DefaultPolicyPtr
;
3040 * Update printer attributes as needed...
3043 if (!(p
->type
& CUPS_PRINTER_DISCOVERED
))
3044 cupsdSetPrinterAttrs(p
);
3050 * 'cupsdValidateDest()' - Validate a printer/class destination.
3053 const char * /* O - Printer or class name */
3055 const char *uri
, /* I - Printer URI */
3056 cups_ptype_t
*dtype
, /* O - Type (printer or class) */
3057 cupsd_printer_t
**printer
) /* O - Printer pointer */
3059 cupsd_printer_t
*p
; /* Current printer */
3060 char localname
[1024],/* Localized hostname */
3061 *lptr
, /* Pointer into localized hostname */
3062 *sptr
, /* Pointer into server name */
3063 *rptr
, /* Pointer into resource */
3064 scheme
[32], /* Scheme portion of URI */
3065 username
[64], /* Username portion of URI */
3066 hostname
[HTTP_MAX_HOST
],
3067 /* Host portion of URI */
3068 resource
[HTTP_MAX_URI
];
3069 /* Resource portion of URI */
3070 int port
; /* Port portion of URI */
3073 DEBUG_printf(("cupsdValidateDest(uri=\"%s\", dtype=%p, printer=%p)\n", uri
,
3077 * Initialize return values...
3084 *dtype
= (cups_ptype_t
)0;
3087 * Pull the hostname and resource from the URI...
3090 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
3091 username
, sizeof(username
), hostname
, sizeof(hostname
),
3092 &port
, resource
, sizeof(resource
));
3095 * See if the resource is a class or printer...
3098 if (!strncmp(resource
, "/classes/", 9))
3104 rptr
= resource
+ 9;
3106 else if (!strncmp(resource
, "/printers/", 10))
3112 rptr
= resource
+ 10;
3117 * Bad resource name...
3124 * See if the printer or class name exists...
3127 p
= cupsdFindDest(rptr
);
3129 if (p
== NULL
&& strchr(rptr
, '@') == NULL
)
3137 *dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
|
3138 CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
);
3144 * Change localhost to the server name...
3147 if (!strcasecmp(hostname
, "localhost"))
3148 strlcpy(hostname
, ServerName
, sizeof(hostname
));
3150 strlcpy(localname
, hostname
, sizeof(localname
));
3152 if (!strcasecmp(hostname
, ServerName
))
3155 * Localize the hostname...
3158 lptr
= strchr(localname
, '.');
3159 sptr
= strchr(ServerName
, '.');
3161 if (sptr
!= NULL
&& lptr
!= NULL
)
3164 * Strip the common domain name components...
3167 while (lptr
!= NULL
)
3169 if (!strcasecmp(lptr
, sptr
))
3175 lptr
= strchr(lptr
+ 1, '.');
3180 DEBUG_printf(("localized hostname is \"%s\"...\n", localname
));
3183 * Find a matching printer or class...
3186 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3188 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3189 if (!strcasecmp(p
->hostname
, localname
) &&
3190 !strcasecmp(p
->name
, rptr
))
3196 *dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
|
3197 CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
);
3207 * 'cupsdWritePrintcap()' - Write a pseudo-printcap file for older applications
3212 cupsdWritePrintcap(void)
3214 int i
; /* Looping var */
3215 cups_file_t
*fp
; /* Printcap file */
3216 cupsd_printer_t
*p
; /* Current printer */
3221 * Update the IRIX printer state for the default printer; if
3222 * no printers remain, then the default printer file will be
3226 write_irix_state(DefaultPrinter
);
3230 * See if we have a printcap file; if not, don't bother writing it.
3233 if (!Printcap
|| !*Printcap
)
3236 cupsdLogMessage(CUPSD_LOG_INFO
, "Generating printcap %s...", Printcap
);
3239 * Open the printcap file...
3242 if ((fp
= cupsFileOpen(Printcap
, "w")) == NULL
)
3246 * Put a comment header at the top so that users will know where the
3247 * data has come from...
3250 if (PrintcapFormat
!= PRINTCAP_PLIST
)
3251 cupsFilePrintf(fp
, "# This file was automatically generated by cupsd(8) "
3253 "# %s/printers.conf file. All changes to this file\n"
3254 "# will be lost.\n", ServerRoot
);
3257 * Write a new printcap with the current list of printers.
3260 switch (PrintcapFormat
)
3264 * Each printer is put in the file as:
3274 cupsFilePrintf(fp
, "%s|%s:rm=%s:rp=%s:\n", DefaultPrinter
->name
,
3275 DefaultPrinter
->info
, ServerName
,
3276 DefaultPrinter
->name
);
3278 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3280 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3281 if (p
!= DefaultPrinter
)
3282 cupsFilePrintf(fp
, "%s|%s:rm=%s:rp=%s:\n", p
->name
, p
->info
,
3283 ServerName
, p
->name
);
3286 case PRINTCAP_PLIST
:
3288 * Each printer is written as a dictionary in a plist file.
3289 * Currently the printer-name, printer-info, printer-is-accepting-jobs,
3290 * printer-location, printer-make-and-model, printer-state,
3291 * printer-state-reasons, printer-type, and (sanitized) device-uri.
3294 cupsFilePuts(fp
, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
3295 "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD "
3296 "PLIST 1.0//EN\" \"http://www.apple.com/DTDs/"
3297 "PropertyList-1.0.dtd\">\n"
3298 "<plist version=\"1.0\">\n"
3301 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3303 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3305 cupsFilePuts(fp
, "\t<dict>\n"
3306 "\t\t<key>printer-name</key>\n"
3308 write_xml_string(fp
, p
->name
);
3309 cupsFilePuts(fp
, "</string>\n"
3310 "\t\t<key>printer-info</key>\n"
3312 write_xml_string(fp
, p
->info
);
3313 cupsFilePrintf(fp
, "</string>\n"
3314 "\t\t<key>printer-is-accepting-jobs</key>\n"
3316 "\t\t<key>printer-location</key>\n"
3317 "\t\t<string>", p
->accepting
? "true" : "false");
3318 write_xml_string(fp
, p
->location
);
3319 cupsFilePuts(fp
, "</string>\n"
3320 "\t\t<key>printer-make-and-model</key>\n"
3322 write_xml_string(fp
, p
->make_model
);
3323 cupsFilePrintf(fp
, "</string>\n"
3324 "\t\t<key>printer-state</key>\n"
3325 "\t\t<integer>%d</integer>\n"
3326 "\t\t<key>printer-state-reasons</key>\n"
3327 "\t\t<array>\n", p
->state
);
3328 for (i
= 0; i
< p
->num_reasons
; i
++)
3330 cupsFilePuts(fp
, "\t\t\t<string>");
3331 write_xml_string(fp
, p
->reasons
[i
]);
3332 cupsFilePuts(fp
, "</string>\n");
3334 cupsFilePrintf(fp
, "\t\t</array>\n"
3335 "\t\t<key>printer-type</key>\n"
3336 "\t\t<integer>%d</integer>\n"
3337 "\t\t<key>device-uri</key>\n"
3338 "\t\t<string>", p
->type
);
3339 write_xml_string(fp
, p
->sanitized_device_uri
);
3340 cupsFilePuts(fp
, "</string>\n"
3343 cupsFilePuts(fp
, "</array>\n"
3347 case PRINTCAP_SOLARIS
:
3349 * Each printer is put in the file as:
3351 * _all:all=Printer1,Printer2,Printer3,...,PrinterN
3352 * _default:use=DefaultPrinter
3354 * :bsdaddr=ServerName,Printer1:\
3355 * :description=Description:
3357 * :bsdaddr=ServerName,Printer2:\
3358 * :description=Description:
3360 * :bsdaddr=ServerName,Printer3:\
3361 * :description=Description:
3364 * :bsdaddr=ServerName,PrinterN:\
3365 * :description=Description:
3368 cupsFilePuts(fp
, "_all:all=");
3369 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3371 p
= (cupsd_printer_t
*)cupsArrayCurrent(Printers
))
3372 cupsFilePrintf(fp
, "%s%c", p
->name
,
3373 cupsArrayNext(Printers
) ? ',' : '\n');
3376 cupsFilePrintf(fp
, "_default:use=%s\n", DefaultPrinter
->name
);
3378 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3380 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3381 cupsFilePrintf(fp
, "%s:\\\n"
3382 "\t:bsdaddr=%s,%s:\\\n"
3383 "\t:description=%s:\n",
3384 p
->name
, ServerName
, p
->name
,
3385 p
->info
? p
->info
: "");
3398 * 'add_printer_defaults()' - Add name-default attributes to the printer attributes.
3402 add_printer_defaults(cupsd_printer_t
*p
)/* I - Printer */
3404 int i
; /* Looping var */
3405 int num_options
; /* Number of default options */
3406 cups_option_t
*options
, /* Default options */
3407 *option
; /* Current option */
3408 char name
[256]; /* name-default */
3412 * Maintain a common array of default attribute names...
3415 if (!CommonDefaults
)
3417 CommonDefaults
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
3419 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("copies-default"));
3420 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("document-format-default"));
3421 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("finishings-default"));
3422 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("job-hold-until-default"));
3423 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("job-priority-default"));
3424 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("job-sheets-default"));
3425 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("media-default"));
3426 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("number-up-default"));
3427 cupsArrayAdd(CommonDefaults
,
3428 _cupsStrAlloc("orientation-requested-default"));
3429 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("sides-default"));
3433 * Add all of the default options from the .conf files...
3436 for (num_options
= 0, options
= NULL
, i
= p
->num_options
, option
= p
->options
;
3440 if (strcmp(option
->name
, "ipp-options") &&
3441 strcmp(option
->name
, "job-sheets") &&
3442 strcmp(option
->name
, "lease-duration"))
3444 snprintf(name
, sizeof(name
), "%s-default", option
->name
);
3445 num_options
= cupsAddOption(name
, option
->value
, num_options
, &options
);
3447 if (!cupsArrayFind(CommonDefaults
, name
))
3448 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc(name
));
3453 * Convert options to IPP attributes...
3456 cupsEncodeOptions2(p
->attrs
, num_options
, options
, IPP_TAG_PRINTER
);
3457 cupsFreeOptions(num_options
, options
);
3460 * Add standard -default attributes as needed...
3463 if (!cupsGetOption("copies", p
->num_options
, p
->options
))
3464 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "copies-default",
3467 if (!cupsGetOption("document-format", p
->num_options
, p
->options
))
3468 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_MIMETYPE
,
3469 "document-format-default", NULL
, "application/octet-stream");
3471 if (!cupsGetOption("job-hold-until", p
->num_options
, p
->options
))
3472 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
3473 "job-hold-until-default", NULL
, "no-hold");
3475 if (!cupsGetOption("job-priority", p
->num_options
, p
->options
))
3476 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
3477 "job-priority-default", 50);
3479 if (!cupsGetOption("number-up", p
->num_options
, p
->options
))
3480 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
3481 "number-up-default", 1);
3483 if (!cupsGetOption("orientation-requested", p
->num_options
, p
->options
))
3484 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NOVALUE
,
3485 "orientation-requested-default", NULL
, NULL
);
3487 if (!cupsGetOption("notify-lease-duration", p
->num_options
, p
->options
))
3488 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
3489 "notify-lease-duration-default", DefaultLeaseDuration
);
3491 if (!cupsGetOption("notify-events", p
->num_options
, p
->options
))
3492 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
3493 "notify-events-default", NULL
, "job-completed");
3498 * 'add_printer_filter()' - Add a MIME filter for a printer.
3503 cupsd_printer_t
*p
, /* I - Printer to add to */
3504 mime_type_t
*filtertype
, /* I - Filter or prefilter MIME type */
3505 const char *filter
) /* I - Filter to add */
3507 char super
[MIME_MAX_SUPER
], /* Super-type for filter */
3508 type
[MIME_MAX_TYPE
], /* Type for filter */
3509 program
[1024]; /* Program/filter name */
3510 int cost
; /* Cost of filter */
3511 mime_type_t
*temptype
; /* MIME type looping var */
3512 char filename
[1024], /* Full filter filename */
3513 *dirsep
; /* Pointer to directory separator */
3514 struct stat fileinfo
; /* File information */
3518 * Parse the filter string; it should be in the following format:
3520 * super/type cost program
3523 if (sscanf(filter
, "%15[^/]/%31s%d%*[ \t]%1023[^\n]", super
, type
, &cost
,
3526 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s: invalid filter string \"%s\"!",
3532 * See if the filter program exists; if not, stop the printer and flag
3536 if (strcmp(program
, "-"))
3538 if (program
[0] == '/')
3539 strlcpy(filename
, program
, sizeof(filename
));
3541 snprintf(filename
, sizeof(filename
), "%s/filter/%s", ServerBin
, program
);
3543 if (stat(filename
, &fileinfo
))
3545 memset(&fileinfo
, 0, sizeof(fileinfo
));
3547 snprintf(p
->state_message
, sizeof(p
->state_message
),
3548 "Filter \"%s\" for printer \"%s\" not available: %s",
3549 filename
, p
->name
, strerror(errno
));
3550 cupsdSetPrinterReasons(p
, "+cups-missing-filter-warning");
3552 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s", p
->state_message
);
3556 * When running as root, do additional security checks...
3562 * Only use filters that are owned by root and do not have group or world
3563 * write permissions.
3566 if (fileinfo
.st_uid
||
3567 (fileinfo
.st_mode
& (S_ISUID
| S_IWGRP
| S_IWOTH
)) != 0)
3569 if (fileinfo
.st_uid
)
3570 snprintf(p
->state_message
, sizeof(p
->state_message
),
3571 "Filter \"%s\" for printer \"%s\" not owned by root",
3574 snprintf(p
->state_message
, sizeof(p
->state_message
),
3575 "Filter \"%s\" for printer \"%s\" has insecure permissions "
3576 "(0%o)", filename
, p
->name
, fileinfo
.st_mode
);
3578 cupsdSetPrinterReasons(p
, "+cups-insecure-filter-warning");
3580 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s", p
->state_message
);
3582 else if (fileinfo
.st_mode
)
3585 * Similarly, check that the parent directory is also owned by root and
3586 * does not have world write permissions.
3589 if ((dirsep
= strrchr(filename
, '/')) != NULL
)
3592 if (!stat(filename
, &fileinfo
) &&
3594 (fileinfo
.st_mode
& (S_ISUID
| S_IWGRP
| S_IWOTH
)) != 0))
3596 if (fileinfo
.st_uid
)
3597 snprintf(p
->state_message
, sizeof(p
->state_message
),
3598 "Filter directory \"%s\" for printer \"%s\" not owned by "
3599 "root", filename
, p
->name
);
3601 snprintf(p
->state_message
, sizeof(p
->state_message
),
3602 "Filter directory \"%s\" for printer \"%s\" has insecure "
3603 "permissions (0%o)", filename
, p
->name
, fileinfo
.st_mode
);
3605 cupsdSetPrinterReasons(p
, "+cups-insecure-filter-warning");
3607 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s", p
->state_message
);
3614 * Add the filter to the MIME database, supporting wildcards as needed...
3617 for (temptype
= mimeFirstType(MimeDatabase
);
3619 temptype
= mimeNextType(MimeDatabase
))
3620 if (((super
[0] == '*' && strcasecmp(temptype
->super
, "printer")) ||
3621 !strcasecmp(temptype
->super
, super
)) &&
3622 (type
[0] == '*' || !strcasecmp(temptype
->type
, type
)))
3624 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3625 "add_printer_filter: %s: adding filter %s/%s %s/%s %d %s",
3626 p
->name
, temptype
->super
, temptype
->type
,
3627 filtertype
->super
, filtertype
->type
,
3629 mimeAddFilter(MimeDatabase
, temptype
, filtertype
, cost
, program
);
3635 * 'add_printer_formats()' - Add document-format-supported values for a printer.
3639 add_printer_formats(cupsd_printer_t
*p
) /* I - Printer */
3641 int i
; /* Looping var */
3642 mime_type_t
*type
; /* Current MIME type */
3643 cups_array_t
*filters
; /* Filters */
3644 ipp_attribute_t
*attr
; /* document-format-supported attribute */
3645 char mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
3646 /* MIME type name */
3650 * Raw (and remote) queues advertise all of the supported MIME
3654 cupsArrayDelete(p
->filetypes
);
3655 p
->filetypes
= NULL
;
3659 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
,
3660 (ipp_tag_t
)(IPP_TAG_MIMETYPE
| IPP_TAG_COPY
),
3661 "document-format-supported", NumMimeTypes
, NULL
, MimeTypes
);
3666 * Otherwise, loop through the supported MIME types and see if there
3667 * are filters for them...
3670 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_printer_formats: %d types, %d filters",
3671 mimeNumTypes(MimeDatabase
), mimeNumFilters(MimeDatabase
));
3673 p
->filetypes
= cupsArrayNew(NULL
, NULL
);
3675 for (type
= mimeFirstType(MimeDatabase
);
3677 type
= mimeNextType(MimeDatabase
))
3679 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", type
->super
, type
->type
);
3681 if ((filters
= mimeFilter(MimeDatabase
, type
, p
->filetype
, NULL
)) != NULL
)
3683 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3684 "add_printer_formats: %s: %s needs %d filters",
3685 p
->name
, mimetype
, cupsArrayCount(filters
));
3687 cupsArrayDelete(filters
);
3688 cupsArrayAdd(p
->filetypes
, type
);
3691 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3692 "add_printer_formats: %s: %s not supported",
3697 * Add the file formats that can be filtered...
3700 if ((type
= mimeType(MimeDatabase
, "application", "octet-stream")) == NULL
||
3701 !cupsArrayFind(p
->filetypes
, type
))
3706 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3707 "add_printer_formats: %s: %d supported types",
3708 p
->name
, cupsArrayCount(p
->filetypes
) + i
);
3710 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_MIMETYPE
,
3711 "document-format-supported",
3712 cupsArrayCount(p
->filetypes
) + i
, NULL
, NULL
);
3715 attr
->values
[0].string
.text
= _cupsStrAlloc("application/octet-stream");
3717 for (type
= (mime_type_t
*)cupsArrayFirst(p
->filetypes
);
3719 i
++, type
= (mime_type_t
*)cupsArrayNext(p
->filetypes
))
3721 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", type
->super
, type
->type
);
3723 attr
->values
[i
].string
.text
= _cupsStrAlloc(mimetype
);
3728 char pdl
[1024]; /* Buffer to build pdl list */
3729 mime_filter_t
*filter
; /* MIME filter looping var */
3734 if (mimeType(MimeDatabase
, "application", "pdf"))
3735 strlcat(pdl
, "application/pdf,", sizeof(pdl
));
3737 if (mimeType(MimeDatabase
, "application", "postscript"))
3738 strlcat(pdl
, "application/postscript,", sizeof(pdl
));
3740 if (mimeType(MimeDatabase
, "application", "vnd.cups-raster"))
3741 strlcat(pdl
, "application/vnd.cups-raster,", sizeof(pdl
));
3744 * Determine if this is a Tioga PrintJobMgr based queue...
3747 for (filter
= (mime_filter_t
*)cupsArrayFirst(MimeDatabase
->filters
);
3749 filter
= (mime_filter_t
*)cupsArrayNext(MimeDatabase
->filters
))
3751 if (filter
->dst
== p
->filetype
&& filter
->filter
&&
3752 strstr(filter
->filter
, "PrintJobMgr"))
3757 * We only support raw printing if this is not a Tioga PrintJobMgr based
3758 * queue and if application/octet-stream is a known conversion...
3761 if (!filter
&& mimeType(MimeDatabase
, "application", "octet-stream"))
3762 strlcat(pdl
, "application/octet-stream,", sizeof(pdl
));
3764 if (mimeType(MimeDatabase
, "image", "png"))
3765 strlcat(pdl
, "image/png,", sizeof(pdl
));
3768 pdl
[strlen(pdl
) - 1] = '\0'; /* Remove trailing comma */
3770 cupsdSetString(&p
->pdl
, pdl
);
3772 #endif /* HAVE_DNSSD */
3777 * 'add_string_array()' - Add a string to an array of CUPS strings.
3781 add_string_array(cups_array_t
**a
, /* I - Array */
3782 const char *s
) /* I - String */
3785 *a
= cupsArrayNew(NULL
, NULL
);
3787 cupsArrayAdd(*a
, _cupsStrAlloc(s
));
3792 * 'compare_printers()' - Compare two printers.
3795 static int /* O - Result of comparison */
3796 compare_printers(void *first
, /* I - First printer */
3797 void *second
, /* I - Second printer */
3798 void *data
) /* I - App data (not used) */
3800 return (strcasecmp(((cupsd_printer_t
*)first
)->name
,
3801 ((cupsd_printer_t
*)second
)->name
));
3806 * 'delete_printer_filters()' - Delete all MIME filters for a printer.
3810 delete_printer_filters(
3811 cupsd_printer_t
*p
) /* I - Printer to remove from */
3813 mime_filter_t
*filter
; /* MIME filter looping var */
3817 * Range check input...
3824 * Remove all filters from the MIME database that have a destination
3825 * type == printer...
3828 for (filter
= mimeFirstFilter(MimeDatabase
);
3830 filter
= mimeNextFilter(MimeDatabase
))
3831 if (filter
->dst
== p
->filetype
|| filter
->dst
== p
->prefiltertype
)
3834 * Delete the current filter...
3837 mimeDeleteFilter(MimeDatabase
, filter
);
3840 cupsdSetPrinterReasons(p
, "-cups-insecure-filter-warning"
3841 ",cups-missing-filter-warning");
3846 * 'delete_string_array()' - Delete an array of CUPS strings.
3850 delete_string_array(cups_array_t
**a
) /* I - Array */
3852 char *ptr
; /* Current string */
3855 for (ptr
= (char *)cupsArrayFirst(*a
);
3857 ptr
= (char *)cupsArrayNext(*a
))
3860 cupsArrayDelete(*a
);
3866 * 'load_ppd()' - Load a cached PPD file, updating the cache as needed.
3870 load_ppd(cupsd_printer_t
*p
) /* I - Printer */
3872 int i
; /* Looping var */
3873 cups_file_t
*cache
; /* Cache file */
3874 char cache_name
[1024]; /* Cache filename */
3875 struct stat cache_info
; /* Cache file info */
3876 ppd_file_t
*ppd
; /* PPD file */
3877 char ppd_name
[1024]; /* PPD filename */
3878 struct stat ppd_info
; /* PPD file info */
3879 int num_media
; /* Number of media options */
3880 char custom_in
[256], /* Custom size name in inches */
3881 custom_mm
[256]; /* Custom size name in millimeters */
3882 ppd_size_t
*size
; /* Current size */
3883 ppd_option_t
*output_bin
, /* OutputBin options */
3884 *duplex
; /* Duplex options */
3885 ppd_attr_t
*ppd_attr
; /* PPD attribute */
3886 _cups_pwg_media_t
*pwgmedia
; /* Matching PWG size name */
3887 ipp_attribute_t
*attr
; /* Attribute data */
3888 ipp_t
*media_col_default
, /* media-col-default collection value */
3889 *media_size
; /* media-size collection value */
3890 ipp_value_t
*val
; /* Attribute value */
3891 int num_finishings
; /* Number of finishings */
3892 int finishings
[5]; /* finishings-supported values */
3893 static const char * const sides
[3] = /* sides-supported values */
3896 "two-sided-long-edge",
3897 "two-sided-short-edge"
3899 static const char * const standard_commands
[] =
3900 { /* Standard CUPS commands */
3908 * Check to see if the cache is up-to-date...
3911 snprintf(cache_name
, sizeof(cache_name
), "%s/%s.ipp", CacheDir
, p
->name
);
3912 if (stat(cache_name
, &cache_info
))
3913 cache_info
.st_mtime
= 0;
3915 snprintf(ppd_name
, sizeof(ppd_name
), "%s/ppd/%s.ppd", ServerRoot
, p
->name
);
3916 if (stat(ppd_name
, &ppd_info
))
3917 ppd_info
.st_mtime
= 1;
3919 ippDelete(p
->ppd_attrs
);
3920 p
->ppd_attrs
= ippNew();
3922 if (cache_info
.st_mtime
>= ppd_info
.st_mtime
&&
3923 (cache
= cupsFileOpen(cache_name
, "r")) != NULL
)
3926 * Load cached information and return...
3929 cupsdLogMessage(CUPSD_LOG_DEBUG
, "load_ppd: Loading %s...", cache_name
);
3931 if (ippReadIO(cache
, (ipp_iocb_t
)cupsFileRead
, 1, NULL
,
3932 p
->ppd_attrs
) == IPP_DATA
)
3934 cupsFileClose(cache
);
3938 cupsFileClose(cache
);
3942 * Reload PPD attributes from disk...
3945 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3947 cupsdLogMessage(CUPSD_LOG_DEBUG
, "load_ppd: Loading %s...", ppd_name
);
3949 delete_string_array(&(p
->filters
));
3950 delete_string_array(&(p
->pre_filters
));
3952 p
->type
&= ~CUPS_PRINTER_OPTIONS
;
3953 p
->type
|= CUPS_PRINTER_BW
;
3955 finishings
[0] = IPP_FINISHINGS_NONE
;
3958 if ((ppd
= ppdOpenFile(ppd_name
)) != NULL
)
3961 * Add make/model and other various attributes...
3964 ppdMarkDefaults(ppd
);
3966 if (ppd
->color_device
)
3967 p
->type
|= CUPS_PRINTER_COLOR
;
3968 if (ppd
->variable_sizes
)
3969 p
->type
|= CUPS_PRINTER_VARIABLE
;
3970 if (!ppd
->manual_copies
)
3971 p
->type
|= CUPS_PRINTER_COPIES
;
3972 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsFax", NULL
)) != NULL
)
3973 if (ppd_attr
->value
&& !strcasecmp(ppd_attr
->value
, "true"))
3974 p
->type
|= CUPS_PRINTER_FAX
;
3976 ippAddBoolean(p
->ppd_attrs
, IPP_TAG_PRINTER
, "color-supported",
3978 if (ppd
->throughput
)
3979 ippAddInteger(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
3980 "pages-per-minute", ppd
->throughput
);
3985 * The NickName can be localized in the character set specified
3986 * by the LanugageEncoding attribute. However, ppdOpen2() has
3987 * already converted the ppd->nickname member to UTF-8 for us
3988 * (the original attribute value is available separately)
3991 cupsdSetString(&p
->make_model
, ppd
->nickname
);
3993 else if (ppd
->modelname
)
3996 * Model name can only contain specific characters...
3999 cupsdSetString(&p
->make_model
, ppd
->modelname
);
4002 cupsdSetString(&p
->make_model
, "Bad PPD File");
4004 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4005 "printer-make-and-model", NULL
, p
->make_model
);
4008 * Add media options from the PPD file...
4011 if (ppd
->num_sizes
== 0)
4013 if (!ppdFindAttr(ppd
, "APScannerOnly", NULL
))
4014 cupsdLogMessage(CUPSD_LOG_CRIT
,
4015 "The PPD file for printer %s contains no media "
4016 "options and is therefore invalid!", p
->name
);
4018 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4019 "media-supported", NULL
, "unknown");
4023 num_media
= ppd
->num_sizes
;
4024 if (ppd
->variable_sizes
)
4027 attr
= ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4028 "media-supported", num_media
, NULL
, NULL
);
4033 for (i
= ppd
->num_sizes
, size
= ppd
->sizes
; i
> 0; i
--, size
++)
4035 if (strcasecmp(size
->name
, "Custom"))
4037 if ((pwgmedia
= _cupsPWGMediaBySize(size
->width
,
4038 size
->length
)) != NULL
)
4040 val
->string
.text
= _cupsStrAlloc(pwgmedia
->pwg
);
4044 snprintf(custom_in
, sizeof(custom_in
), "adobe_%s_%gx%gin",
4045 size
->name
, size
->width
/ 72.0, size
->length
/ 72.0);
4046 snprintf(custom_mm
, sizeof(custom_mm
), "adobe_%s_%gx%gmm",
4047 size
->name
, size
->width
* 25.4 / 72.0,
4048 size
->length
* 25.4 / 72.0);
4049 if (strlen(custom_in
) < strlen(custom_mm
))
4050 val
->string
.text
= _cupsStrAlloc(custom_in
);
4052 val
->string
.text
= _cupsStrAlloc(custom_mm
);
4058 * Add media-default...
4061 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4062 "media-default", NULL
, val
->string
.text
);
4065 * Add media-col-default...
4068 media_size
= ippNew();
4069 ippAddInteger(media_size
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4070 "x-dimension", (int)(size
->width
* 2540.0 / 72.0));
4071 ippAddInteger(media_size
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4072 "y-dimension", (int)(size
->length
* 2540.0 / 72.0));
4074 media_col_default
= ippNew();
4075 ippAddString(media_col_default
, IPP_TAG_PRINTER
,
4076 IPP_TAG_KEYWORD
| IPP_TAG_COPY
, "media-color", NULL
,
4078 ippAddString(media_col_default
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4079 "media-key", NULL
,val
->string
.text
);
4080 ippAddCollection(media_col_default
, IPP_TAG_PRINTER
, "media-size",
4082 ippAddString(media_col_default
, IPP_TAG_PRINTER
,
4083 IPP_TAG_KEYWORD
| IPP_TAG_COPY
, "media-type", NULL
,
4086 ippAddCollection(p
->ppd_attrs
, IPP_TAG_PRINTER
,
4087 "media-col-default", media_col_default
);
4094 if (ppd
->variable_sizes
)
4096 snprintf(custom_in
, sizeof(custom_in
), "custom_min_%gx%gin",
4097 ppd
->custom_min
[0] / 72.0, ppd
->custom_min
[1] / 72.0);
4098 snprintf(custom_mm
, sizeof(custom_mm
), "custom_min_%gx%gmm",
4099 ppd
->custom_min
[0] * 25.4 / 72.0,
4100 ppd
->custom_min
[1] * 25.4 / 72.0);
4101 if (strlen(custom_in
) < strlen(custom_mm
))
4102 val
->string
.text
= _cupsStrAlloc(custom_in
);
4104 val
->string
.text
= _cupsStrAlloc(custom_mm
);
4107 snprintf(custom_in
, sizeof(custom_in
), "custom_max_%gx%gin",
4108 ppd
->custom_max
[0] / 72.0, ppd
->custom_max
[1] / 72.0);
4109 snprintf(custom_mm
, sizeof(custom_mm
), "custom_max_%gx%gmm",
4110 ppd
->custom_max
[0] * 25.4 / 72.0,
4111 ppd
->custom_max
[1] * 25.4 / 72.0);
4112 if (strlen(custom_in
) < strlen(custom_mm
))
4113 val
->string
.text
= _cupsStrAlloc(custom_in
);
4115 val
->string
.text
= _cupsStrAlloc(custom_mm
);
4124 if ((output_bin
= ppdFindOption(ppd
, "OutputBin")) != NULL
)
4126 attr
= ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4127 "output-bin-supported", output_bin
->num_choices
,
4132 for (i
= 0, val
= attr
->values
;
4133 i
< output_bin
->num_choices
;
4135 val
->string
.text
= _cupsStrAlloc(output_bin
->choices
[i
].choice
);
4138 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4139 "output-bin-default", NULL
, output_bin
->defchoice
);
4146 if ((duplex
= ppdFindOption(ppd
, "Duplex")) == NULL
)
4147 if ((duplex
= ppdFindOption(ppd
, "EFDuplex")) == NULL
)
4148 if ((duplex
= ppdFindOption(ppd
, "EFDuplexing")) == NULL
)
4149 if ((duplex
= ppdFindOption(ppd
, "KD03Duplex")) == NULL
)
4150 duplex
= ppdFindOption(ppd
, "JCLDuplex");
4152 if (duplex
&& duplex
->num_choices
> 1 &&
4153 !ppdInstallableConflict(ppd
, duplex
->keyword
, "DuplexTumble"))
4155 p
->type
|= CUPS_PRINTER_DUPLEX
;
4157 ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4158 "sides-supported", 3, NULL
, sides
);
4160 if (!strcasecmp(duplex
->defchoice
, "DuplexTumble"))
4161 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4162 "sides-default", NULL
, "two-sided-short-edge");
4163 else if (!strcasecmp(duplex
->defchoice
, "DuplexNoTumble"))
4164 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4165 "sides-default", NULL
, "two-sided-long-edge");
4167 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4168 "sides-default", NULL
, "one-sided");
4171 if (ppdFindOption(ppd
, "Collate") != NULL
)
4172 p
->type
|= CUPS_PRINTER_COLLATE
;
4174 if (ppdFindOption(ppd
, "StapleLocation") != NULL
)
4176 p
->type
|= CUPS_PRINTER_STAPLE
;
4177 finishings
[num_finishings
++] = IPP_FINISHINGS_STAPLE
;
4180 if (ppdFindOption(ppd
, "BindEdge") != NULL
)
4182 p
->type
|= CUPS_PRINTER_BIND
;
4183 finishings
[num_finishings
++] = IPP_FINISHINGS_BIND
;
4186 for (i
= 0; i
< ppd
->num_sizes
; i
++)
4187 if (ppd
->sizes
[i
].length
> 1728)
4188 p
->type
|= CUPS_PRINTER_LARGE
;
4189 else if (ppd
->sizes
[i
].length
> 1008)
4190 p
->type
|= CUPS_PRINTER_MEDIUM
;
4192 p
->type
|= CUPS_PRINTER_SMALL
;
4194 if ((ppd_attr
= ppdFindAttr(ppd
, "APICADriver", NULL
)) != NULL
&&
4195 ppd_attr
->value
&& !strcasecmp(ppd_attr
->value
, "true"))
4197 if ((ppd_attr
= ppdFindAttr(ppd
, "APScannerOnly", NULL
)) != NULL
&&
4198 ppd_attr
->value
&& !strcasecmp(ppd_attr
->value
, "true"))
4199 p
->type
|= CUPS_PRINTER_SCANNER
;
4201 p
->type
|= CUPS_PRINTER_MFP
;
4205 * Add a filter from application/vnd.cups-raw to printer/name to
4206 * handle "raw" printing by users.
4209 add_string_array(&(p
->filters
), "application/vnd.cups-raw 0 -");
4212 * Add any pre-filters in the PPD file...
4215 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsPreFilter", NULL
)) != NULL
)
4217 for (; ppd_attr
; ppd_attr
= ppdFindNextAttr(ppd
, "cupsPreFilter", NULL
))
4218 if (ppd_attr
->value
)
4219 add_string_array(&(p
->pre_filters
), ppd_attr
->value
);
4223 * Add any filters in the PPD file...
4226 DEBUG_printf(("ppd->num_filters = %d\n", ppd
->num_filters
));
4227 for (i
= 0; i
< ppd
->num_filters
; i
++)
4229 DEBUG_printf(("ppd->filters[%d] = \"%s\"\n", i
, ppd
->filters
[i
]));
4230 add_string_array(&(p
->filters
), ppd
->filters
[i
]);
4232 if (!strncasecmp(ppd
->filters
[i
], "application/vnd.cups-command", 28) &&
4233 isspace(ppd
->filters
[i
][28] & 255))
4234 p
->type
|= CUPS_PRINTER_COMMANDS
;
4237 if (ppd
->num_filters
== 0)
4240 * If there are no filters, add PostScript printing filters.
4243 add_string_array(&(p
->filters
),
4244 "application/vnd.cups-command 0 commandtops");
4245 add_string_array(&(p
->filters
),
4246 "application/vnd.cups-postscript 0 -");
4248 p
->type
|= CUPS_PRINTER_COMMANDS
;
4250 else if (!(p
->type
& CUPS_PRINTER_COMMANDS
))
4253 * See if this is a PostScript device without a command filter...
4256 for (i
= 0; i
< ppd
->num_filters
; i
++)
4257 if (!strncasecmp(ppd
->filters
[i
],
4258 "application/vnd.cups-postscript", 31) &&
4259 isspace(ppd
->filters
[i
][31] & 255))
4262 if (i
< ppd
->num_filters
)
4265 * Add the generic PostScript command filter...
4268 add_string_array(&(p
->filters
),
4269 "application/vnd.cups-command 0 commandtops");
4270 p
->type
|= CUPS_PRINTER_COMMANDS
;
4274 if (p
->type
& CUPS_PRINTER_COMMANDS
)
4276 char *commands
, /* Copy of commands */
4277 *start
, /* Start of name */
4278 *end
; /* End of name */
4279 int count
; /* Number of commands */
4282 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsCommands", NULL
)) != NULL
&&
4283 ppd_attr
->value
&& ppd_attr
->value
[0])
4285 for (count
= 0, start
= ppd_attr
->value
; *start
; count
++)
4287 while (isspace(*start
& 255))
4293 while (*start
&& !isspace(*start
& 255))
4303 * Make a copy of the commands string and count how many ...
4306 attr
= ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4307 "printer-commands", count
, NULL
, NULL
);
4309 commands
= strdup(ppd_attr
->value
);
4311 for (count
= 0, start
= commands
; *start
; count
++)
4313 while (isspace(*start
& 255))
4320 while (*end
&& !isspace(*end
& 255))
4326 attr
->values
[count
].string
.text
= _cupsStrAlloc(start
);
4336 * Add the standard list of commands...
4339 ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4341 (int)(sizeof(standard_commands
) /
4342 sizeof(standard_commands
[0])), NULL
,
4349 * No commands supported...
4352 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4353 "printer-commands", NULL
, "none");
4357 * Show current and available port monitors for this printer...
4360 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "port-monitor",
4361 NULL
, p
->port_monitor
? p
->port_monitor
: "none");
4363 for (i
= 1, ppd_attr
= ppdFindAttr(ppd
, "cupsPortMonitor", NULL
);
4365 i
++, ppd_attr
= ppdFindNextAttr(ppd
, "cupsPortMonitor", NULL
));
4369 if (strstr(ppd
->protocols
, "TBCP"))
4371 else if (strstr(ppd
->protocols
, "BCP"))
4375 attr
= ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
4376 "port-monitor-supported", i
, NULL
, NULL
);
4378 attr
->values
[0].string
.text
= _cupsStrAlloc("none");
4380 for (i
= 1, ppd_attr
= ppdFindAttr(ppd
, "cupsPortMonitor", NULL
);
4382 i
++, ppd_attr
= ppdFindNextAttr(ppd
, "cupsPortMonitor", NULL
))
4383 attr
->values
[i
].string
.text
= _cupsStrAlloc(ppd_attr
->value
);
4387 if (strstr(ppd
->protocols
, "TBCP"))
4388 attr
->values
[i
].string
.text
= _cupsStrAlloc("tbcp");
4389 else if (strstr(ppd
->protocols
, "BCP"))
4390 attr
->values
[i
].string
.text
= _cupsStrAlloc("bcp");
4394 cupsdSetString(&p
->product
, ppd
->product
);
4395 #endif /* HAVE_DNSSD */
4397 if (ppdFindAttr(ppd
, "APRemoteQueueID", NULL
))
4398 p
->type
|= CUPS_PRINTER_REMOTE
;
4401 * Close the PPD and set the type...
4406 else if (!access(ppd_name
, 0))
4408 int pline
; /* PPD line number */
4409 ppd_status_t pstatus
; /* PPD load status */
4412 pstatus
= ppdLastError(&pline
);
4414 cupsdLogMessage(CUPSD_LOG_ERROR
, "PPD file for %s cannot be loaded!",
4417 if (pstatus
<= PPD_ALLOC_ERROR
)
4418 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s", strerror(errno
));
4420 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s on line %d.",
4421 ppdErrorString(pstatus
), pline
);
4423 cupsdLogMessage(CUPSD_LOG_INFO
,
4424 "Hint: Run \"cupstestppd %s\" and fix any errors.",
4428 * Add a filter from application/vnd.cups-raw to printer/name to
4429 * handle "raw" printing by users.
4432 add_string_array(&(p
->filters
), "application/vnd.cups-raw 0 -");
4435 * Add a PostScript filter, since this is still possibly PS printer.
4438 add_string_array(&(p
->filters
), "application/vnd.cups-postscript 0 -");
4443 * If we have an interface script, add a filter entry for it...
4446 char interface
[1024]; /* Interface script */
4449 snprintf(interface
, sizeof(interface
), "%s/interfaces/%s", ServerRoot
,
4451 if (!access(interface
, X_OK
))
4454 * Yes, we have a System V style interface script; use it!
4457 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4458 "printer-make-and-model", NULL
,
4459 "Local System V Printer");
4461 snprintf(interface
, sizeof(interface
), "*/* 0 %s/interfaces/%s",
4462 ServerRoot
, p
->name
);
4463 add_string_array(&(p
->filters
), interface
);
4465 else if (!strncmp(p
->device_uri
, "ipp://", 6) &&
4466 (strstr(p
->device_uri
, "/printers/") != NULL
||
4467 strstr(p
->device_uri
, "/classes/") != NULL
||
4468 (strstr(p
->device_uri
, "._ipp.") != NULL
&&
4469 !strcmp(p
->device_uri
+ strlen(p
->device_uri
) - 5,
4473 * Tell the client this is really a hard-wired remote printer.
4476 p
->type
|= CUPS_PRINTER_REMOTE
;
4479 * Point the printer-uri-supported attribute to the
4483 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
4484 "printer-uri-supported", NULL
, p
->device_uri
);
4487 * Then set the make-and-model accordingly...
4490 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4491 "printer-make-and-model", NULL
, "Remote Printer");
4494 * Print all files directly...
4503 * Otherwise we have neither - treat this as a "dumb" printer
4504 * with no PPD file...
4507 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4508 "printer-make-and-model", NULL
, "Local Raw Printer");
4514 ippAddIntegers(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
4515 "finishings-supported", num_finishings
, finishings
);
4516 ippAddInteger(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
4517 "finishings-default", IPP_FINISHINGS_NONE
);
4519 if (ppd
&& (cache
= cupsFileOpen(cache_name
, "w")) != NULL
)
4522 * Save cached PPD attributes to disk...
4525 cupsdLogMessage(CUPSD_LOG_DEBUG
, "load_ppd: Saving %s...", cache_name
);
4527 p
->ppd_attrs
->state
= IPP_IDLE
;
4529 if (ippWriteIO(cache
, (ipp_iocb_t
)cupsFileWrite
, 1, NULL
,
4530 p
->ppd_attrs
) != IPP_DATA
)
4532 cupsdLogMessage(CUPSD_LOG_ERROR
,
4533 "Unable to save PPD cache file \"%s\" - %s", cache_name
,
4538 cupsFileClose(cache
);
4540 else if (cache_info
.st_mtime
)
4543 * Remove cache file...
4553 * 'write_irix_config()' - Update the config files used by the IRIX
4558 write_irix_config(cupsd_printer_t
*p
) /* I - Printer to update */
4560 char filename
[1024]; /* Interface script filename */
4561 cups_file_t
*fp
; /* Interface script file */
4562 ipp_attribute_t
*attr
; /* Attribute data */
4566 * Add dummy interface and GUI scripts to fool SGI's "challenged" printing
4567 * tools. First the interface script that tells the tools what kind of
4568 * printer we have...
4571 snprintf(filename
, sizeof(filename
), "/var/spool/lp/interface/%s", p
->name
);
4573 if (p
->type
& CUPS_PRINTER_CLASS
)
4575 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4577 cupsFilePuts(fp
, "#!/bin/sh\n");
4579 if ((attr
= ippFindAttribute(p
->attrs
, "printer-make-and-model",
4580 IPP_TAG_TEXT
)) != NULL
)
4581 cupsFilePrintf(fp
, "NAME=\"%s\"\n", attr
->values
[0].string
.text
);
4582 else if (p
->type
& CUPS_PRINTER_CLASS
)
4583 cupsFilePuts(fp
, "NAME=\"Printer Class\"\n");
4585 cupsFilePuts(fp
, "NAME=\"Remote Destination\"\n");
4587 if (p
->type
& CUPS_PRINTER_COLOR
)
4588 cupsFilePuts(fp
, "TYPE=ColorPostScript\n");
4590 cupsFilePuts(fp
, "TYPE=MonoPostScript\n");
4592 cupsFilePrintf(fp
, "HOSTNAME=%s\n", ServerName
);
4593 cupsFilePrintf(fp
, "HOSTPRINTER=%s\n", p
->name
);
4597 chmod(filename
, 0755);
4598 chown(filename
, User
, Group
);
4602 * Then the member file that tells which device file the queue is connected
4603 * to... Networked printers use "/dev/null" in this file, so that's what
4604 * we use (the actual device URI can confuse some apps...)
4607 snprintf(filename
, sizeof(filename
), "/var/spool/lp/member/%s", p
->name
);
4609 if (p
->type
& CUPS_PRINTER_CLASS
)
4611 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4613 cupsFilePuts(fp
, "/dev/null\n");
4617 chmod(filename
, 0644);
4618 chown(filename
, User
, Group
);
4622 * The gui_interface file is a script or program that launches a GUI
4623 * option panel for the printer, using options specified on the
4624 * command-line in the third argument. The option panel must send
4625 * any printing options to stdout on a single line when the user
4626 * accepts them, or nothing if the user cancels the dialog.
4628 * The default options panel program is /usr/bin/glpoptions, from
4629 * the ESP Print Pro software. You can select another using the
4630 * PrintcapGUI option.
4633 snprintf(filename
, sizeof(filename
), "/var/spool/lp/gui_interface/ELF/%s.gui", p
->name
);
4635 if (p
->type
& CUPS_PRINTER_CLASS
)
4637 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4639 cupsFilePuts(fp
, "#!/bin/sh\n");
4640 cupsFilePrintf(fp
, "%s -d %s -o \"$3\"\n", PrintcapGUI
, p
->name
);
4644 chmod(filename
, 0755);
4645 chown(filename
, User
, Group
);
4649 * The POD config file is needed by the printstatus command to show
4650 * the printer location and device.
4653 snprintf(filename
, sizeof(filename
), "/var/spool/lp/pod/%s.config", p
->name
);
4655 if (p
->type
& CUPS_PRINTER_CLASS
)
4657 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4659 cupsFilePrintf(fp
, "Printer Class | %s\n",
4660 (p
->type
& CUPS_PRINTER_COLOR
) ? "ColorPostScript" : "MonoPostScript");
4661 cupsFilePrintf(fp
, "Printer Model | %s\n", p
->make_model
? p
->make_model
: "");
4662 cupsFilePrintf(fp
, "Location Code | %s\n", p
->location
? p
->location
: "");
4663 cupsFilePrintf(fp
, "Physical Location | %s\n", p
->info
? p
->info
: "");
4664 cupsFilePrintf(fp
, "Port Path | %s\n", p
->device_uri
);
4665 cupsFilePrintf(fp
, "Config Path | /var/spool/lp/pod/%s.config\n", p
->name
);
4666 cupsFilePrintf(fp
, "Active Status Path | /var/spool/lp/pod/%s.status\n", p
->name
);
4667 cupsFilePuts(fp
, "Status Update Wait | 10 seconds\n");
4671 chmod(filename
, 0664);
4672 chown(filename
, User
, Group
);
4678 * 'write_irix_state()' - Update the status files used by IRIX printing
4683 write_irix_state(cupsd_printer_t
*p
) /* I - Printer to update */
4685 char filename
[1024]; /* Interface script filename */
4686 cups_file_t
*fp
; /* Interface script file */
4687 int tag
; /* Status tag value */
4693 * The POD status file is needed for the printstatus window to
4694 * provide the current status of the printer.
4697 snprintf(filename
, sizeof(filename
), "/var/spool/lp/pod/%s.status", p
->name
);
4699 if (p
->type
& CUPS_PRINTER_CLASS
)
4701 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4703 cupsFilePrintf(fp
, "Operational Status | %s\n",
4704 (p
->state
== IPP_PRINTER_IDLE
) ? "Idle" :
4705 (p
->state
== IPP_PRINTER_PROCESSING
) ? "Busy" :
4707 cupsFilePrintf(fp
, "Information | 01 00 00 | %s\n", CUPS_SVERSION
);
4708 cupsFilePrintf(fp
, "Information | 02 00 00 | Device URI: %s\n",
4710 cupsFilePrintf(fp
, "Information | 03 00 00 | %s jobs\n",
4711 p
->accepting
? "Accepting" : "Not accepting");
4712 cupsFilePrintf(fp
, "Information | 04 00 00 | %s\n", p
->state_message
);
4716 chmod(filename
, 0664);
4717 chown(filename
, User
, Group
);
4721 * The activeicons file is needed to provide desktop icons for printers:
4723 * [ quoted from /usr/lib/print/tagit ]
4725 * --- Type of printer tags (base values)
4727 * Dumb=66048 # 0x10200
4728 * DumbColor=66080 # 0x10220
4729 * Raster=66112 # 0x10240
4730 * ColorRaster=66144 # 0x10260
4731 * Plotter=66176 # 0x10280
4732 * PostScript=66208 # 0x102A0
4733 * ColorPostScript=66240 # 0x102C0
4734 * MonoPostScript=66272 # 0x102E0
4736 * --- Printer state modifiers for local printers
4741 * Unknown=3 # 0x3 (Faulted due to unknown reason)
4743 * --- Printer state modifiers for network printers
4747 * NetFaulted=10 # 0xA
4748 * NetUnknown=11 # 0xB (Faulted due to unknown reason)
4751 snprintf(filename
, sizeof(filename
), "/var/spool/lp/activeicons/%s", p
->name
);
4753 if (p
->type
& CUPS_PRINTER_CLASS
)
4755 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4757 if (p
->type
& CUPS_PRINTER_COLOR
)
4762 if (p
->type
& CUPS_PRINTER_REMOTE
)
4765 if (p
->state
== IPP_PRINTER_PROCESSING
)
4768 else if (p
->state
== IPP_PRINTER_STOPPED
)
4771 cupsFilePuts(fp
, "#!/bin/sh\n");
4772 cupsFilePrintf(fp
, "#Tag %d\n", tag
);
4776 chmod(filename
, 0755);
4777 chown(filename
, User
, Group
);
4782 * The default file is needed by the printers window to show
4783 * the default printer.
4786 snprintf(filename
, sizeof(filename
), "/var/spool/lp/default");
4788 if (DefaultPrinter
!= NULL
)
4790 if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4792 cupsFilePrintf(fp
, "%s\n", DefaultPrinter
->name
);
4796 chmod(filename
, 0644);
4797 chown(filename
, User
, Group
);
4807 * 'write_xml_string()' - Write a string with XML escaping.
4811 write_xml_string(cups_file_t
*fp
, /* I - File to write to */
4812 const char *s
) /* I - String to write */
4814 const char *start
; /* Start of current sequence */
4820 for (start
= s
; *s
; s
++)
4825 cupsFileWrite(fp
, start
, s
- start
);
4827 cupsFilePuts(fp
, "&");
4833 cupsFileWrite(fp
, start
, s
- start
);
4835 cupsFilePuts(fp
, "<");
4841 cupsFilePuts(fp
, start
);
4846 * End of "$Id: printers.c 7968 2008-09-19 23:03:01Z mike $".