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
);
210 ippAddString(history
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
211 "com.apple.print.recoverable-message", NULL
, p
->recoverable
);
212 #endif /* __APPLE__ */
213 if (p
->num_reasons
== 0)
214 ippAddString(history
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
215 "printer-state-reasons", NULL
, "none");
217 ippAddStrings(history
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
218 "printer-state-reasons", p
->num_reasons
, NULL
,
219 (const char * const *)p
->reasons
);
220 ippAddInteger(history
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
221 "printer-state-change-time", p
->state_time
);
222 ippAddInteger(history
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
223 "printer-state-sequence-number", p
->sequence_number
);
225 p
->history
[p
->num_history
] = history
;
231 * 'cupsdAddPrinterUser()' - Add a user to the ACL.
236 cupsd_printer_t
*p
, /* I - Printer */
237 const char *username
) /* I - User */
239 const char **temp
; /* Temporary array pointer */
245 if (p
->num_users
== 0)
246 temp
= malloc(sizeof(char **));
248 temp
= realloc(p
->users
, sizeof(char **) * (p
->num_users
+ 1));
254 temp
+= p
->num_users
;
256 if ((*temp
= strdup(username
)) != NULL
)
262 * 'cupsdCreateCommonData()' - Create the common printer data.
266 cupsdCreateCommonData(void)
268 int i
; /* Looping var */
269 ipp_attribute_t
*attr
; /* Attribute data */
270 cups_dir_t
*dir
; /* Notifier directory */
271 cups_dentry_t
*dent
; /* Notifier directory entry */
272 cups_array_t
*notifiers
; /* Notifier array */
273 char filename
[1024], /* Filename */
274 *notifier
; /* Current notifier */
275 cupsd_policy_t
*p
; /* Current policy */
276 static const int nups
[] = /* number-up-supported values */
277 { 1, 2, 4, 6, 9, 16 };
278 static const int orients
[4] =/* orientation-requested-supported values */
282 IPP_REVERSE_LANDSCAPE
,
285 static const char * const holds
[] = /* job-hold-until-supported values */
296 static const char * const versions
[] =/* ipp-versions-supported values */
303 static const int ops
[] = /* operations-supported values */
310 IPP_GET_JOB_ATTRIBUTES
,
312 IPP_GET_PRINTER_ATTRIBUTES
,
318 IPP_SET_PRINTER_ATTRIBUTES
,
319 IPP_SET_JOB_ATTRIBUTES
,
320 IPP_GET_PRINTER_SUPPORTED_VALUES
,
321 IPP_CREATE_PRINTER_SUBSCRIPTION
,
322 IPP_CREATE_JOB_SUBSCRIPTION
,
323 IPP_GET_SUBSCRIPTION_ATTRIBUTES
,
324 IPP_GET_SUBSCRIPTIONS
,
325 IPP_RENEW_SUBSCRIPTION
,
326 IPP_CANCEL_SUBSCRIPTION
,
327 IPP_GET_NOTIFICATIONS
,
331 IPP_RELEASE_HELD_NEW_JOBS
,
345 CUPS_AUTHENTICATE_JOB
,
350 static const char * const charsets
[] =/* charset-supported values */
355 static const char * const compressions
[] =
356 { /* document-compression-supported values */
360 #endif /* HAVE_LIBZ */
362 static const char * const media_col_supported
[] =
363 { /* media-col-supported values */
369 static const char * const multiple_document_handling
[] =
370 { /* multiple-document-handling-supported values */
371 "separate-documents-uncollated-copies",
372 "separate-documents-collated-copies"
374 static const char * const notify_attrs
[] =
375 { /* notify-attributes-supported values */
376 "printer-state-change-time",
377 "notify-lease-expiration-time",
378 "notify-subscriber-user-name"
380 static const char * const notify_events
[] =
381 { /* notify-events-supported values */
383 "job-config-changed",
390 "printer-config-changed",
392 "printer-finishings-changed",
393 "printer-media-changed",
397 "printer-state-changed",
404 static const char * const job_settable
[] =
405 { /* job-settable-attributes-supported */
411 "multiple-document-handling",
413 "orientation-requested",
416 "printer-resolution",
419 static const char * const printer_settable
[] =
420 { /* printer-settable-attributes-supported */
427 ippDelete(CommonData
);
429 CommonData
= ippNew();
432 * This list of attributes is sorted to improve performance when the
433 * client provides a requested-attributes attribute...
436 /* charset-configured */
437 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_CHARSET
| IPP_TAG_COPY
,
438 "charset-configured", NULL
, DefaultCharset
);
440 /* charset-supported */
441 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_CHARSET
| IPP_TAG_COPY
,
442 "charset-supported", sizeof(charsets
) / sizeof(charsets
[0]),
445 /* compression-supported */
446 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
447 "compression-supported",
448 sizeof(compressions
) / sizeof(compressions
[0]),
451 /* copies-supported */
452 ippAddRange(CommonData
, IPP_TAG_PRINTER
, "copies-supported", 1, MaxCopies
);
455 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
| IPP_TAG_COPY
,
456 "cups-version", NULL
, CUPS_SVERSION
+ 6);
458 /* generated-natural-language-supported */
459 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_LANGUAGE
| IPP_TAG_COPY
,
460 "generated-natural-language-supported", NULL
, DefaultLanguage
);
462 /* ipp-versions-supported */
463 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
464 "ipp-versions-supported", sizeof(versions
) / sizeof(versions
[0]),
467 /* job-hold-until-supported */
468 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
469 "job-hold-until-supported", sizeof(holds
) / sizeof(holds
[0]),
472 /* job-priority-supported */
473 ippAddInteger(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
474 "job-priority-supported", 100);
476 /* job-settable-attributes-supported */
477 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
478 "job-settable-attributes-supported",
479 sizeof(job_settable
) / sizeof(job_settable
[0]),
482 /* job-sheets-supported */
483 if (cupsArrayCount(Banners
) > 0)
486 * Setup the job-sheets-supported attribute...
489 if (Classification
&& !ClassifyOverride
)
490 attr
= ippAddString(CommonData
, IPP_TAG_PRINTER
,
491 IPP_TAG_NAME
| IPP_TAG_COPY
,
492 "job-sheets-supported", NULL
, Classification
);
494 attr
= ippAddStrings(CommonData
, IPP_TAG_PRINTER
,
495 IPP_TAG_NAME
| IPP_TAG_COPY
,
496 "job-sheets-supported", cupsArrayCount(Banners
) + 1,
500 cupsdLogMessage(CUPSD_LOG_EMERG
,
501 "Unable to allocate memory for "
502 "job-sheets-supported attribute: %s!", strerror(errno
));
503 else if (!Classification
|| ClassifyOverride
)
505 cupsd_banner_t
*banner
; /* Current banner */
508 attr
->values
[0].string
.text
= _cupsStrAlloc("none");
510 for (i
= 1, banner
= (cupsd_banner_t
*)cupsArrayFirst(Banners
);
512 i
++, banner
= (cupsd_banner_t
*)cupsArrayNext(Banners
))
513 attr
->values
[i
].string
.text
= banner
->name
;
517 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_NAME
| IPP_TAG_COPY
,
518 "job-sheets-supported", NULL
, "none");
520 /* media-col-supported */
521 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
522 "media-col-supported",
523 sizeof(media_col_supported
) /
524 sizeof(media_col_supported
[0]), NULL
,
525 media_col_supported
);
527 /* multiple-document-handling-supported */
528 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
529 "multiple-document-handling-supported",
530 sizeof(multiple_document_handling
) /
531 sizeof(multiple_document_handling
[0]), NULL
,
532 multiple_document_handling
);
534 /* multiple-document-jobs-supported */
535 ippAddBoolean(CommonData
, IPP_TAG_PRINTER
,
536 "multiple-document-jobs-supported", 1);
538 /* multiple-operation-time-out */
539 ippAddInteger(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
540 "multiple-operation-time-out", MultipleOperationTimeout
);
542 /* natural-language-configured */
543 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_LANGUAGE
| IPP_TAG_COPY
,
544 "natural-language-configured", NULL
, DefaultLanguage
);
546 /* notify-attributes-supported */
547 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
548 "notify-attributes-supported",
549 (int)(sizeof(notify_attrs
) / sizeof(notify_attrs
[0])),
552 /* notify-lease-duration-supported */
553 ippAddRange(CommonData
, IPP_TAG_PRINTER
,
554 "notify-lease-duration-supported", 0,
555 MaxLeaseDuration
? MaxLeaseDuration
: 2147483647);
557 /* notify-max-events-supported */
558 ippAddInteger(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
559 "notify-max-events-supported", MaxEvents
);
561 /* notify-events-supported */
562 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
563 "notify-events-supported",
564 (int)(sizeof(notify_events
) / sizeof(notify_events
[0])),
565 NULL
, notify_events
);
567 /* notify-pull-method-supported */
568 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
569 "notify-pull-method-supported", NULL
, "ippget");
571 /* notify-schemes-supported */
572 snprintf(filename
, sizeof(filename
), "%s/notifier", ServerBin
);
573 if ((dir
= cupsDirOpen(filename
)) != NULL
)
575 notifiers
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
577 while ((dent
= cupsDirRead(dir
)) != NULL
)
578 if (S_ISREG(dent
->fileinfo
.st_mode
) &&
579 (dent
->fileinfo
.st_mode
& S_IXOTH
) != 0)
580 cupsArrayAdd(notifiers
, _cupsStrAlloc(dent
->filename
));
582 if (cupsArrayCount(notifiers
) > 0)
584 attr
= ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
585 "notify-schemes-supported",
586 cupsArrayCount(notifiers
), NULL
, NULL
);
588 for (i
= 0, notifier
= (char *)cupsArrayFirst(notifiers
);
590 i
++, notifier
= (char *)cupsArrayNext(notifiers
))
591 attr
->values
[i
].string
.text
= notifier
;
594 cupsArrayDelete(notifiers
);
598 /* number-up-supported */
599 ippAddIntegers(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
600 "number-up-supported", sizeof(nups
) / sizeof(nups
[0]), nups
);
602 /* operations-supported */
603 ippAddIntegers(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
604 "operations-supported",
605 sizeof(ops
) / sizeof(ops
[0]) + JobFiles
- 1, ops
);
607 /* orientation-requested-supported */
608 ippAddIntegers(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
609 "orientation-requested-supported", 4, orients
);
611 /* page-ranges-supported */
612 ippAddBoolean(CommonData
, IPP_TAG_PRINTER
, "page-ranges-supported", 1);
614 /* pdf-override-supported */
615 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
616 "pdl-override-supported", NULL
, "not-attempted");
618 /* printer-op-policy-supported */
619 attr
= ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_NAME
| IPP_TAG_COPY
,
620 "printer-op-policy-supported", cupsArrayCount(Policies
),
622 for (i
= 0, p
= (cupsd_policy_t
*)cupsArrayFirst(Policies
);
624 i
++, p
= (cupsd_policy_t
*)cupsArrayNext(Policies
))
625 attr
->values
[i
].string
.text
= p
->name
;
627 /* printer-settable-attributes-supported */
628 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
629 "printer-settable-attributes-supported",
630 sizeof(printer_settable
) / sizeof(printer_settable
[0]),
631 NULL
, printer_settable
);
633 /* server-is-sharing-printers */
634 ippAddBoolean(CommonData
, IPP_TAG_PRINTER
, "server-is-sharing-printers",
635 BrowseLocalProtocols
!= 0 && Browsing
);
640 * 'cupsdDeleteAllPrinters()' - Delete all printers from the system.
644 cupsdDeleteAllPrinters(void)
646 cupsd_printer_t
*p
; /* Pointer to current printer/class */
649 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
651 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
652 cupsdDeletePrinter(p
, 0);
657 * 'cupsdDeletePrinter()' - Delete a printer from the system.
662 cupsd_printer_t
*p
, /* I - Printer to delete */
663 int update
) /* I - Update printers.conf? */
665 int i
; /* Looping var */
667 char filename
[1024]; /* Interface script filename */
671 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdDeletePrinter(p=%p(%s), update=%d)",
675 * Save the current position in the Printers array...
678 cupsArraySave(Printers
);
681 * Stop printing on this printer...
684 cupsdSetPrinterState(p
, IPP_PRINTER_STOPPED
, update
);
687 cupsdSetJobState(p
->job
, IPP_JOB_PENDING
, CUPSD_JOB_FORCE
,
688 update
? "Job stopped due to printer being deleted." :
692 * If this printer is the next for browsing, point to the next one...
697 cupsArrayFind(Printers
, p
);
698 BrowseNext
= (cupsd_printer_t
*)cupsArrayNext(Printers
);
702 * Remove the printer from the list...
705 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
706 "cupsdDeletePrinter: Removing %s from Printers", p
->name
);
707 cupsArrayRemove(Printers
, p
);
709 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
711 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
712 "cupsdDeletePrinter: Removing %s from ImplicitPrinters",
714 cupsArrayRemove(ImplicitPrinters
, p
);
718 * Remove the dummy interface/icon/option files under IRIX...
722 snprintf(filename
, sizeof(filename
), "/var/spool/lp/interface/%s", p
->name
);
725 snprintf(filename
, sizeof(filename
), "/var/spool/lp/gui_interface/ELF/%s.gui",
729 snprintf(filename
, sizeof(filename
), "/var/spool/lp/activeicons/%s", p
->name
);
732 snprintf(filename
, sizeof(filename
), "/var/spool/lp/pod/%s.config", p
->name
);
735 snprintf(filename
, sizeof(filename
), "/var/spool/lp/pod/%s.status", p
->name
);
738 snprintf(filename
, sizeof(filename
), "/var/spool/lp/member/%s", p
->name
);
743 * If p is the default printer, assign a different one...
746 if (p
== DefaultPrinter
)
748 DefaultPrinter
= NULL
;
750 if (UseNetworkDefault
)
753 * Find the first network default printer and use it...
756 cupsd_printer_t
*dp
; /* New default printer */
759 for (dp
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
761 dp
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
762 if (dp
!= p
&& (dp
->type
& CUPS_PRINTER_DEFAULT
))
771 * Remove this printer from any classes...
774 if (!(p
->type
& CUPS_PRINTER_IMPLICIT
))
776 cupsdDeletePrinterFromClasses(p
);
779 * Deregister from any browse protocols...
782 cupsdDeregisterPrinter(p
, 1);
786 * Free all memory used by the printer...
789 if (p
->printers
!= NULL
)
792 if (MaxPrinterHistory
)
794 for (i
= 0; i
< p
->num_history
; i
++)
795 ippDelete(p
->history
[i
]);
800 delete_printer_filters(p
);
802 for (i
= 0; i
< p
->num_reasons
; i
++)
803 _cupsStrFree(p
->reasons
[i
]);
806 ippDelete(p
->ppd_attrs
);
808 mimeDeleteType(MimeDatabase
, p
->filetype
);
809 mimeDeleteType(MimeDatabase
, p
->prefiltertype
);
811 delete_string_array(&(p
->filters
));
812 delete_string_array(&(p
->pre_filters
));
814 cupsdFreePrinterUsers(p
);
817 cupsdClearString(&p
->uri
);
818 cupsdClearString(&p
->hostname
);
819 cupsdClearString(&p
->name
);
820 cupsdClearString(&p
->location
);
821 cupsdClearString(&p
->make_model
);
822 cupsdClearString(&p
->info
);
823 cupsdClearString(&p
->job_sheets
[0]);
824 cupsdClearString(&p
->job_sheets
[1]);
825 cupsdClearString(&p
->device_uri
);
826 cupsdClearString(&p
->sanitized_device_uri
);
827 cupsdClearString(&p
->port_monitor
);
828 cupsdClearString(&p
->op_policy
);
829 cupsdClearString(&p
->error_policy
);
831 cupsdClearString(&p
->alert
);
832 cupsdClearString(&p
->alert_description
);
835 cupsdClearString(&p
->product
);
836 cupsdClearString(&p
->pdl
);
837 #endif /* HAVE_DNSSD */
839 cupsArrayDelete(p
->filetypes
);
842 free(p
->browse_attrs
);
845 cupsdClearString(&p
->recoverable
);
846 #endif /* __APPLE__ */
848 cupsFreeOptions(p
->num_options
, p
->options
);
853 * Restore the previous position in the Printers array...
856 cupsArrayRestore(Printers
);
861 * 'cupsdFindDest()' - Find a destination in the list.
864 cupsd_printer_t
* /* O - Destination in list */
865 cupsdFindDest(const char *name
) /* I - Name of printer or class to find */
867 cupsd_printer_t key
; /* Search key */
870 key
.name
= (char *)name
;
871 return ((cupsd_printer_t
*)cupsArrayFind(Printers
, &key
));
876 * 'cupsdFindPrinter()' - Find a printer in the list.
879 cupsd_printer_t
* /* O - Printer in list */
880 cupsdFindPrinter(const char *name
) /* I - Name of printer to find */
882 cupsd_printer_t
*p
; /* Printer in list */
885 if ((p
= cupsdFindDest(name
)) != NULL
&& (p
->type
& CUPS_PRINTER_CLASS
))
893 * 'cupsdFreePrinterUsers()' - Free allow/deny users.
897 cupsdFreePrinterUsers(
898 cupsd_printer_t
*p
) /* I - Printer */
900 int i
; /* Looping var */
903 if (!p
|| !p
->num_users
)
906 for (i
= 0; i
< p
->num_users
; i
++)
907 free((void *)p
->users
[i
]);
917 * 'cupsdLoadAllPrinters()' - Load printers from the printers.conf file.
921 cupsdLoadAllPrinters(void)
923 int i
; /* Looping var */
924 cups_file_t
*fp
; /* printers.conf file */
925 int linenum
; /* Current line number */
926 char line
[4096], /* Line from file */
927 *value
, /* Pointer to value */
928 *valueptr
; /* Pointer into value */
929 cupsd_printer_t
*p
; /* Current printer */
933 * Open the printers.conf file...
936 snprintf(line
, sizeof(line
), "%s/printers.conf", ServerRoot
);
937 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
940 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open %s - %s", line
,
946 * Read printer configurations until we hit EOF...
952 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
955 * Decode the directive...
958 if (!strcasecmp(line
, "<Printer") ||
959 !strcasecmp(line
, "<DefaultPrinter"))
962 * <Printer name> or <DefaultPrinter name>
965 if (p
== NULL
&& value
)
968 * Add the printer and a base file type...
971 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Loading printer %s...", value
);
973 p
= cupsdAddPrinter(value
);
975 p
->state
= IPP_PRINTER_IDLE
;
978 * Set the default printer as needed...
981 if (!strcasecmp(line
, "<DefaultPrinter"))
985 cupsdLogMessage(CUPSD_LOG_ERROR
,
986 "Syntax error on line %d of printers.conf.", linenum
);
988 else if (!strcasecmp(line
, "</Printer>"))
993 * Close out the current printer...
996 cupsdSetPrinterAttrs(p
);
997 cupsdAddPrinterHistory(p
);
999 if (strncmp(p
->device_uri
, "file:", 5) &&
1000 p
->state
!= IPP_PRINTER_STOPPED
)
1003 * See if the backend exists...
1006 snprintf(line
, sizeof(line
), "%s/backend/%s", ServerBin
,
1009 if ((valueptr
= strchr(line
+ strlen(ServerBin
), ':')) != NULL
)
1010 *valueptr
= '\0'; /* Chop everything but URI scheme */
1012 if (access(line
, 0))
1015 * Backend does not exist, stop printer...
1018 p
->state
= IPP_PRINTER_STOPPED
;
1019 snprintf(p
->state_message
, sizeof(p
->state_message
),
1020 "Backend %s does not exist!", line
);
1027 cupsdLogMessage(CUPSD_LOG_ERROR
,
1028 "Syntax error on line %d of printers.conf.", linenum
);
1032 cupsdLogMessage(CUPSD_LOG_ERROR
,
1033 "Syntax error on line %d of printers.conf.", linenum
);
1035 else if (!strcasecmp(line
, "AuthInfoRequired"))
1037 if (!cupsdSetAuthInfoRequired(p
, value
, NULL
))
1038 cupsdLogMessage(CUPSD_LOG_ERROR
,
1039 "Bad AuthInfoRequired on line %d of printers.conf.",
1042 else if (!strcasecmp(line
, "Info"))
1045 cupsdSetString(&p
->info
, value
);
1047 else if (!strcasecmp(line
, "MakeModel"))
1050 cupsdSetString(&p
->make_model
, value
);
1052 else if (!strcasecmp(line
, "Location"))
1055 cupsdSetString(&p
->location
, value
);
1057 else if (!strcasecmp(line
, "DeviceURI"))
1060 cupsdSetDeviceURI(p
, value
);
1062 cupsdLogMessage(CUPSD_LOG_ERROR
,
1063 "Syntax error on line %d of printers.conf.", linenum
);
1065 else if (!strcasecmp(line
, "Option") && value
)
1071 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
1074 cupsdLogMessage(CUPSD_LOG_ERROR
,
1075 "Syntax error on line %d of printers.conf.", linenum
);
1078 for (; *valueptr
&& isspace(*valueptr
& 255); *valueptr
++ = '\0');
1080 p
->num_options
= cupsAddOption(value
, valueptr
, p
->num_options
,
1084 else if (!strcasecmp(line
, "PortMonitor"))
1086 if (value
&& strcmp(value
, "none"))
1087 cupsdSetString(&p
->port_monitor
, value
);
1089 cupsdClearString(&p
->port_monitor
);
1091 cupsdLogMessage(CUPSD_LOG_ERROR
,
1092 "Syntax error on line %d of printers.conf.", linenum
);
1094 else if (!strcasecmp(line
, "Reason"))
1097 strcmp(value
, "com.apple.print.recoverable-warning") &&
1098 strcmp(value
, "connecting-to-device") &&
1099 strcmp(value
, "cups-insecure-filter-warning") &&
1100 strcmp(value
, "cups-missing-filter-warning"))
1102 for (i
= 0 ; i
< p
->num_reasons
; i
++)
1103 if (!strcmp(value
, p
->reasons
[i
]))
1106 if (i
>= p
->num_reasons
&&
1107 p
->num_reasons
< (int)(sizeof(p
->reasons
) / sizeof(p
->reasons
[0])))
1109 p
->reasons
[p
->num_reasons
] = _cupsStrAlloc(value
);
1114 cupsdLogMessage(CUPSD_LOG_ERROR
,
1115 "Syntax error on line %d of printers.conf.", linenum
);
1117 else if (!strcasecmp(line
, "State"))
1120 * Set the initial queue state...
1123 if (value
&& !strcasecmp(value
, "idle"))
1124 p
->state
= IPP_PRINTER_IDLE
;
1125 else if (value
&& !strcasecmp(value
, "stopped"))
1127 p
->state
= IPP_PRINTER_STOPPED
;
1129 for (i
= 0 ; i
< p
->num_reasons
; i
++)
1130 if (!strcmp("paused", p
->reasons
[i
]))
1133 if (i
>= p
->num_reasons
&&
1134 p
->num_reasons
< (int)(sizeof(p
->reasons
) / sizeof(p
->reasons
[0])))
1136 p
->reasons
[p
->num_reasons
] = _cupsStrAlloc("paused");
1141 cupsdLogMessage(CUPSD_LOG_ERROR
,
1142 "Syntax error on line %d of printers.conf.", linenum
);
1144 else if (!strcasecmp(line
, "StateMessage"))
1147 * Set the initial queue state message...
1151 strlcpy(p
->state_message
, value
, sizeof(p
->state_message
));
1153 else if (!strcasecmp(line
, "StateTime"))
1156 * Set the state time...
1160 p
->state_time
= atoi(value
);
1162 else if (!strcasecmp(line
, "Accepting"))
1165 * Set the initial accepting state...
1169 (!strcasecmp(value
, "yes") ||
1170 !strcasecmp(value
, "on") ||
1171 !strcasecmp(value
, "true")))
1174 (!strcasecmp(value
, "no") ||
1175 !strcasecmp(value
, "off") ||
1176 !strcasecmp(value
, "false")))
1179 cupsdLogMessage(CUPSD_LOG_ERROR
,
1180 "Syntax error on line %d of printers.conf.", linenum
);
1182 else if (!strcasecmp(line
, "Type"))
1185 p
->type
= atoi(value
);
1187 cupsdLogMessage(CUPSD_LOG_ERROR
,
1188 "Syntax error on line %d of printers.conf.", linenum
);
1190 else if (!strcasecmp(line
, "Product"))
1195 p
->product
= _cupsStrAlloc(value
);
1196 #endif /* HAVE_DNSSD */
1199 cupsdLogMessage(CUPSD_LOG_ERROR
,
1200 "Syntax error on line %d of printers.conf.", linenum
);
1202 else if (!strcasecmp(line
, "Filter"))
1207 p
->filters
= cupsArrayNew(NULL
, NULL
);
1209 cupsArrayAdd(p
->filters
, _cupsStrAlloc(value
));
1212 cupsdLogMessage(CUPSD_LOG_ERROR
,
1213 "Syntax error on line %d of printers.conf.", linenum
);
1215 else if (!strcasecmp(line
, "PreFilter"))
1219 if (!p
->pre_filters
)
1220 p
->pre_filters
= cupsArrayNew(NULL
, NULL
);
1222 cupsArrayAdd(p
->pre_filters
, _cupsStrAlloc(value
));
1225 cupsdLogMessage(CUPSD_LOG_ERROR
,
1226 "Syntax error on line %d of printers.conf.", linenum
);
1228 else if (!strcasecmp(line
, "Shared"))
1231 * Set the initial shared state...
1235 (!strcasecmp(value
, "yes") ||
1236 !strcasecmp(value
, "on") ||
1237 !strcasecmp(value
, "true")))
1240 (!strcasecmp(value
, "no") ||
1241 !strcasecmp(value
, "off") ||
1242 !strcasecmp(value
, "false")))
1245 cupsdLogMessage(CUPSD_LOG_ERROR
,
1246 "Syntax error on line %d of printers.conf.", linenum
);
1248 else if (!strcasecmp(line
, "JobSheets"))
1251 * Set the initial job sheets...
1256 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
1261 cupsdSetString(&p
->job_sheets
[0], value
);
1263 while (isspace(*valueptr
& 255))
1268 for (value
= valueptr
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
1273 cupsdSetString(&p
->job_sheets
[1], value
);
1277 cupsdLogMessage(CUPSD_LOG_ERROR
,
1278 "Syntax error on line %d of printers.conf.", linenum
);
1280 else if (!strcasecmp(line
, "AllowUser"))
1285 cupsdAddPrinterUser(p
, value
);
1288 cupsdLogMessage(CUPSD_LOG_ERROR
,
1289 "Syntax error on line %d of printers.conf.", linenum
);
1291 else if (!strcasecmp(line
, "DenyUser"))
1296 cupsdAddPrinterUser(p
, value
);
1299 cupsdLogMessage(CUPSD_LOG_ERROR
,
1300 "Syntax error on line %d of printers.conf.", linenum
);
1302 else if (!strcasecmp(line
, "QuotaPeriod"))
1305 p
->quota_period
= atoi(value
);
1307 cupsdLogMessage(CUPSD_LOG_ERROR
,
1308 "Syntax error on line %d of printers.conf.", linenum
);
1310 else if (!strcasecmp(line
, "PageLimit"))
1313 p
->page_limit
= atoi(value
);
1315 cupsdLogMessage(CUPSD_LOG_ERROR
,
1316 "Syntax error on line %d of printers.conf.", linenum
);
1318 else if (!strcasecmp(line
, "KLimit"))
1321 p
->k_limit
= atoi(value
);
1323 cupsdLogMessage(CUPSD_LOG_ERROR
,
1324 "Syntax error on line %d of printers.conf.", linenum
);
1326 else if (!strcasecmp(line
, "OpPolicy"))
1330 cupsd_policy_t
*pol
; /* Policy */
1333 if ((pol
= cupsdFindPolicy(value
)) != NULL
)
1335 cupsdSetString(&p
->op_policy
, value
);
1336 p
->op_policy_ptr
= pol
;
1339 cupsdLogMessage(CUPSD_LOG_ERROR
,
1340 "Bad policy \"%s\" on line %d of printers.conf",
1344 cupsdLogMessage(CUPSD_LOG_ERROR
,
1345 "Syntax error on line %d of printers.conf.", linenum
);
1347 else if (!strcasecmp(line
, "ErrorPolicy"))
1350 cupsdSetString(&p
->error_policy
, value
);
1352 cupsdLogMessage(CUPSD_LOG_ERROR
,
1353 "Syntax error on line %d of printers.conf.", linenum
);
1355 else if (!strcasecmp(line
, "Attribute") && value
)
1357 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
1360 cupsdLogMessage(CUPSD_LOG_ERROR
,
1361 "Syntax error on line %d of printers.conf.", linenum
);
1364 for (; *valueptr
&& isspace(*valueptr
& 255); *valueptr
++ = '\0');
1367 cupsdSetPrinterAttrs(p
);
1369 if (!strcmp(value
, "marker-change-time"))
1370 p
->marker_time
= atoi(valueptr
);
1372 cupsdSetPrinterAttr(p
, value
, valueptr
);
1378 * Something else we don't understand...
1381 cupsdLogMessage(CUPSD_LOG_ERROR
,
1382 "Unknown configuration directive %s on line %d of printers.conf.",
1392 * 'cupsdRenamePrinter()' - Rename a printer.
1397 cupsd_printer_t
*p
, /* I - Printer */
1398 const char *name
) /* I - New name */
1401 * Remove the printer from the array(s) first...
1404 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1405 "cupsdRenamePrinter: Removing %s from Printers", p
->name
);
1406 cupsArrayRemove(Printers
, p
);
1408 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
1410 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1411 "cupsdRenamePrinter: Removing %s from ImplicitPrinters",
1413 cupsArrayRemove(ImplicitPrinters
, p
);
1417 * Rename the printer type...
1420 mimeDeleteType(MimeDatabase
, p
->filetype
);
1421 p
->filetype
= mimeAddType(MimeDatabase
, "printer", name
);
1423 mimeDeleteType(MimeDatabase
, p
->prefiltertype
);
1424 p
->prefiltertype
= mimeAddType(MimeDatabase
, "prefilter", name
);
1427 * Rename the printer...
1430 cupsdSetString(&p
->name
, name
);
1433 * Reset printer attributes...
1436 cupsdSetPrinterAttrs(p
);
1439 * Add the printer back to the printer array(s)...
1442 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1443 "cupsdRenamePrinter: Adding %s to Printers", p
->name
);
1444 cupsArrayAdd(Printers
, p
);
1446 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
1448 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1449 "cupsdRenamePrinter: Adding %s to ImplicitPrinters",
1451 cupsArrayAdd(ImplicitPrinters
, p
);
1457 * 'cupsdSaveAllPrinters()' - Save all printer definitions to the printers.conf
1462 cupsdSaveAllPrinters(void)
1464 int i
; /* Looping var */
1465 cups_file_t
*fp
; /* printers.conf file */
1466 char temp
[1024], /* Temporary string */
1467 backup
[1024], /* printers.conf.O file */
1468 value
[2048], /* Value string */
1469 *ptr
; /* Pointer into value */
1470 cupsd_printer_t
*printer
; /* Current printer class */
1471 time_t curtime
; /* Current time */
1472 struct tm
*curdate
; /* Current date */
1473 cups_option_t
*option
; /* Current option */
1474 ipp_attribute_t
*marker
; /* Current marker attribute */
1478 * Create the printers.conf file...
1481 snprintf(temp
, sizeof(temp
), "%s/printers.conf", ServerRoot
);
1482 snprintf(backup
, sizeof(backup
), "%s/printers.conf.O", ServerRoot
);
1484 if (rename(temp
, backup
))
1486 if (errno
!= ENOENT
)
1487 cupsdLogMessage(CUPSD_LOG_ERROR
,
1488 "Unable to backup printers.conf - %s", strerror(errno
));
1491 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
1493 cupsdLogMessage(CUPSD_LOG_ERROR
,
1494 "Unable to save printers.conf - %s", strerror(errno
));
1496 if (rename(backup
, temp
))
1497 cupsdLogMessage(CUPSD_LOG_ERROR
,
1498 "Unable to restore printers.conf - %s", strerror(errno
));
1502 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving printers.conf...");
1505 * Restrict access to the file...
1508 fchown(cupsFileNumber(fp
), getuid(), Group
);
1509 fchmod(cupsFileNumber(fp
), 0600);
1512 * Write a small header to the file...
1515 curtime
= time(NULL
);
1516 curdate
= localtime(&curtime
);
1517 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
1519 cupsFilePuts(fp
, "# Printer configuration file for " CUPS_SVERSION
"\n");
1520 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
1521 cupsFilePuts(fp
, "# DO NOT EDIT THIS FILE WHEN CUPSD IS RUNNING\n");
1524 * Write each local printer known to the system...
1527 for (printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1529 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1532 * Skip remote destinations and printer classes...
1535 if ((printer
->type
& CUPS_PRINTER_DISCOVERED
) ||
1536 (printer
->type
& CUPS_PRINTER_CLASS
) ||
1537 (printer
->type
& CUPS_PRINTER_IMPLICIT
))
1541 * Write printers as needed...
1544 if (printer
== DefaultPrinter
)
1545 cupsFilePrintf(fp
, "<DefaultPrinter %s>\n", printer
->name
);
1547 cupsFilePrintf(fp
, "<Printer %s>\n", printer
->name
);
1549 if (printer
->num_auth_info_required
> 0)
1551 switch (printer
->num_auth_info_required
)
1554 strlcpy(value
, printer
->auth_info_required
[0], sizeof(value
));
1558 snprintf(value
, sizeof(value
), "%s,%s",
1559 printer
->auth_info_required
[0],
1560 printer
->auth_info_required
[1]);
1565 snprintf(value
, sizeof(value
), "%s,%s,%s",
1566 printer
->auth_info_required
[0],
1567 printer
->auth_info_required
[1],
1568 printer
->auth_info_required
[2]);
1572 cupsFilePutConf(fp
, "AuthInfoRequired", value
);
1576 cupsFilePutConf(fp
, "Info", printer
->info
);
1578 if (printer
->location
)
1579 cupsFilePutConf(fp
, "Location", printer
->location
);
1581 if (printer
->make_model
)
1582 cupsFilePutConf(fp
, "MakeModel", printer
->make_model
);
1584 cupsFilePutConf(fp
, "DeviceURI", printer
->device_uri
);
1586 if (printer
->port_monitor
)
1587 cupsFilePutConf(fp
, "PortMonitor", printer
->port_monitor
);
1589 if (printer
->state
== IPP_PRINTER_STOPPED
)
1591 cupsFilePuts(fp
, "State Stopped\n");
1593 if (printer
->state_message
)
1594 cupsFilePutConf(fp
, "StateMessage", printer
->state_message
);
1597 cupsFilePuts(fp
, "State Idle\n");
1599 cupsFilePrintf(fp
, "StateTime %d\n", (int)printer
->state_time
);
1601 for (i
= 0; i
< printer
->num_reasons
; i
++)
1602 if (strcmp(printer
->reasons
[i
], "com.apple.print.recoverable-warning") &&
1603 strcmp(printer
->reasons
[i
], "connecting-to-device") &&
1604 strcmp(printer
->reasons
[i
], "cups-insecure-filter-warning") &&
1605 strcmp(printer
->reasons
[i
], "cups-missing-filter-warning"))
1606 cupsFilePutConf(fp
, "Reason", printer
->reasons
[i
]);
1608 cupsFilePrintf(fp
, "Type %d\n", printer
->type
);
1611 if (printer
->product
)
1612 cupsFilePutConf(fp
, "Product", printer
->product
);
1613 #endif /* HAVE_DNSSD */
1615 for (ptr
= (char *)cupsArrayFirst(printer
->filters
);
1617 ptr
= (char *)cupsArrayNext(printer
->filters
))
1618 cupsFilePutConf(fp
, "Filter", ptr
);
1620 for (ptr
= (char *)cupsArrayFirst(printer
->pre_filters
);
1622 ptr
= (char *)cupsArrayNext(printer
->pre_filters
))
1623 cupsFilePutConf(fp
, "PreFilter", ptr
);
1625 if (printer
->accepting
)
1626 cupsFilePuts(fp
, "Accepting Yes\n");
1628 cupsFilePuts(fp
, "Accepting No\n");
1630 if (printer
->shared
)
1631 cupsFilePuts(fp
, "Shared Yes\n");
1633 cupsFilePuts(fp
, "Shared No\n");
1635 snprintf(value
, sizeof(value
), "%s %s", printer
->job_sheets
[0],
1636 printer
->job_sheets
[1]);
1637 cupsFilePutConf(fp
, "JobSheets", value
);
1639 cupsFilePrintf(fp
, "QuotaPeriod %d\n", printer
->quota_period
);
1640 cupsFilePrintf(fp
, "PageLimit %d\n", printer
->page_limit
);
1641 cupsFilePrintf(fp
, "KLimit %d\n", printer
->k_limit
);
1643 for (i
= 0; i
< printer
->num_users
; i
++)
1644 cupsFilePutConf(fp
, printer
->deny_users
? "DenyUser" : "AllowUser",
1647 if (printer
->op_policy
)
1648 cupsFilePutConf(fp
, "OpPolicy", printer
->op_policy
);
1649 if (printer
->error_policy
)
1650 cupsFilePutConf(fp
, "ErrorPolicy", printer
->error_policy
);
1652 for (i
= printer
->num_options
, option
= printer
->options
;
1656 snprintf(value
, sizeof(value
), "%s %s", option
->name
, option
->value
);
1657 cupsFilePutConf(fp
, "Option", value
);
1660 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-colors",
1661 IPP_TAG_NAME
)) != NULL
)
1663 snprintf(value
, sizeof(value
), "%s ", marker
->name
);
1665 for (i
= 0, ptr
= value
+ strlen(value
);
1666 i
< marker
->num_values
&& ptr
< (value
+ sizeof(value
) - 1);
1672 strlcpy(ptr
, marker
->values
[i
].string
.text
,
1673 value
+ sizeof(value
) - ptr
);
1678 cupsFilePutConf(fp
, "Attribute", value
);
1681 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-levels",
1682 IPP_TAG_INTEGER
)) != NULL
)
1684 cupsFilePrintf(fp
, "Attribute %s %d", marker
->name
,
1685 marker
->values
[0].integer
);
1686 for (i
= 1; i
< marker
->num_values
; i
++)
1687 cupsFilePrintf(fp
, ",%d", marker
->values
[i
].integer
);
1688 cupsFilePuts(fp
, "\n");
1691 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-low-levels",
1692 IPP_TAG_INTEGER
)) != NULL
)
1694 cupsFilePrintf(fp
, "Attribute %s %d", marker
->name
,
1695 marker
->values
[0].integer
);
1696 for (i
= 1; i
< marker
->num_values
; i
++)
1697 cupsFilePrintf(fp
, ",%d", marker
->values
[i
].integer
);
1698 cupsFilePuts(fp
, "\n");
1701 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-high-levels",
1702 IPP_TAG_INTEGER
)) != NULL
)
1704 cupsFilePrintf(fp
, "Attribute %s %d", marker
->name
,
1705 marker
->values
[0].integer
);
1706 for (i
= 1; i
< marker
->num_values
; i
++)
1707 cupsFilePrintf(fp
, ",%d", marker
->values
[i
].integer
);
1708 cupsFilePuts(fp
, "\n");
1711 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-message",
1712 IPP_TAG_TEXT
)) != NULL
)
1714 snprintf(value
, sizeof(value
), "%s %s", marker
->name
,
1715 marker
->values
[0].string
.text
);
1717 cupsFilePutConf(fp
, "Attribute", value
);
1720 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-names",
1721 IPP_TAG_NAME
)) != NULL
)
1723 snprintf(value
, sizeof(value
), "%s ", marker
->name
);
1725 for (i
= 0, ptr
= value
+ strlen(value
);
1726 i
< marker
->num_values
&& ptr
< (value
+ sizeof(value
) - 1);
1732 strlcpy(ptr
, marker
->values
[i
].string
.text
,
1733 value
+ sizeof(value
) - ptr
);
1738 cupsFilePutConf(fp
, "Attribute", value
);
1741 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-types",
1742 IPP_TAG_KEYWORD
)) != NULL
)
1744 snprintf(value
, sizeof(value
), "%s ", marker
->name
);
1746 for (i
= 0, ptr
= value
+ strlen(value
);
1747 i
< marker
->num_values
&& ptr
< (value
+ sizeof(value
) - 1);
1753 strlcpy(ptr
, marker
->values
[i
].string
.text
,
1754 value
+ sizeof(value
) - ptr
);
1759 cupsFilePutConf(fp
, "Attribute", value
);
1762 if (printer
->marker_time
)
1763 cupsFilePrintf(fp
, "Attribute marker-change-time %ld\n",
1764 (long)printer
->marker_time
);
1766 cupsFilePuts(fp
, "</Printer>\n");
1770 * Make IRIX desktop & printer status happy
1773 write_irix_state(printer
);
1782 * 'cupsdSetAuthInfoRequired()' - Set the required authentication info.
1785 int /* O - 1 if value OK, 0 otherwise */
1786 cupsdSetAuthInfoRequired(
1787 cupsd_printer_t
*p
, /* I - Printer */
1788 const char *values
, /* I - Plain text value (or NULL) */
1789 ipp_attribute_t
*attr
) /* I - IPP attribute value (or NULL) */
1791 int i
; /* Looping var */
1794 p
->num_auth_info_required
= 0;
1797 * Do we have a plain text value?
1803 * Yes, grab the keywords...
1806 const char *end
; /* End of current value */
1809 while (*values
&& p
->num_auth_info_required
< 4)
1811 if ((end
= strchr(values
, ',')) == NULL
)
1812 end
= values
+ strlen(values
);
1814 if ((end
- values
) == 4 && !strncmp(values
, "none", 4))
1816 if (p
->num_auth_info_required
!= 0 || *end
)
1819 p
->auth_info_required
[p
->num_auth_info_required
] = "none";
1820 p
->num_auth_info_required
++;
1824 else if ((end
- values
) == 9 && !strncmp(values
, "negotiate", 9))
1826 if (p
->num_auth_info_required
!= 0 || *end
)
1829 p
->auth_info_required
[p
->num_auth_info_required
] = "negotiate";
1830 p
->num_auth_info_required
++;
1832 else if ((end
- values
) == 6 && !strncmp(values
, "domain", 6))
1834 p
->auth_info_required
[p
->num_auth_info_required
] = "domain";
1835 p
->num_auth_info_required
++;
1837 else if ((end
- values
) == 8 && !strncmp(values
, "password", 8))
1839 p
->auth_info_required
[p
->num_auth_info_required
] = "password";
1840 p
->num_auth_info_required
++;
1842 else if ((end
- values
) == 8 && !strncmp(values
, "username", 8))
1844 p
->auth_info_required
[p
->num_auth_info_required
] = "username";
1845 p
->num_auth_info_required
++;
1850 values
= (*end
) ? end
+ 1 : end
;
1853 if (p
->num_auth_info_required
== 0)
1855 p
->auth_info_required
[0] = "none";
1856 p
->num_auth_info_required
= 1;
1860 * Update the printer-type value as needed...
1863 if (p
->num_auth_info_required
> 1 ||
1864 strcmp(p
->auth_info_required
[0], "none"))
1865 p
->type
|= CUPS_PRINTER_AUTHENTICATED
;
1867 p
->type
&= ~CUPS_PRINTER_AUTHENTICATED
;
1873 * Grab values from an attribute instead...
1876 if (!attr
|| attr
->num_values
> 4)
1880 * Update the printer-type value as needed...
1883 if (attr
->num_values
> 1 ||
1884 strcmp(attr
->values
[0].string
.text
, "none"))
1885 p
->type
|= CUPS_PRINTER_AUTHENTICATED
;
1887 p
->type
&= ~CUPS_PRINTER_AUTHENTICATED
;
1889 for (i
= 0; i
< attr
->num_values
; i
++)
1891 if (!strcmp(attr
->values
[i
].string
.text
, "none"))
1893 if (p
->num_auth_info_required
!= 0 || attr
->num_values
!= 1)
1896 p
->auth_info_required
[p
->num_auth_info_required
] = "none";
1897 p
->num_auth_info_required
++;
1901 else if (!strcmp(attr
->values
[i
].string
.text
, "negotiate"))
1903 if (p
->num_auth_info_required
!= 0 || attr
->num_values
!= 1)
1906 p
->auth_info_required
[p
->num_auth_info_required
] = "negotiate";
1907 p
->num_auth_info_required
++;
1911 else if (!strcmp(attr
->values
[i
].string
.text
, "domain"))
1913 p
->auth_info_required
[p
->num_auth_info_required
] = "domain";
1914 p
->num_auth_info_required
++;
1916 else if (!strcmp(attr
->values
[i
].string
.text
, "password"))
1918 p
->auth_info_required
[p
->num_auth_info_required
] = "password";
1919 p
->num_auth_info_required
++;
1921 else if (!strcmp(attr
->values
[i
].string
.text
, "username"))
1923 p
->auth_info_required
[p
->num_auth_info_required
] = "username";
1924 p
->num_auth_info_required
++;
1935 * 'cupsdSetDeviceURI()' - Set the device URI for a printer.
1939 cupsdSetDeviceURI(cupsd_printer_t
*p
, /* I - Printer */
1940 const char *uri
) /* I - Device URI */
1942 char buffer
[1024], /* URI buffer */
1943 *start
, /* Start of data after scheme */
1944 *slash
, /* First slash after scheme:// */
1945 *ptr
; /* Pointer into user@host:port part */
1949 * Set the full device URI..
1952 cupsdSetString(&(p
->device_uri
), uri
);
1955 * Copy the device URI to a temporary buffer so we can sanitize any auth
1959 strlcpy(buffer
, uri
, sizeof(buffer
));
1962 * Find the end of the scheme:// part...
1965 if ((ptr
= strchr(buffer
, ':')) != NULL
)
1967 for (start
= ptr
+ 1; *start
; start
++)
1972 * Find the next slash (/) in the URI...
1975 if ((slash
= strchr(start
, '/')) == NULL
)
1976 slash
= start
+ strlen(start
); /* No slash, point to the end */
1979 * Check for an @ sign before the slash...
1982 if ((ptr
= strchr(start
, '@')) != NULL
&& ptr
< slash
)
1985 * Found an @ sign and it is before the resource part, so we have
1986 * an authentication string. Copy the remaining URI over the
1987 * authentication string...
1990 _cups_strcpy(start
, ptr
+ 1);
1995 * Save the sanitized URI...
1998 cupsdSetString(&(p
->sanitized_device_uri
), buffer
);
2003 * 'cupsdSetPrinterAttr()' - Set a printer attribute.
2007 cupsdSetPrinterAttr(
2008 cupsd_printer_t
*p
, /* I - Printer */
2009 const char *name
, /* I - Attribute name */
2010 char *value
) /* I - Attribute value string */
2012 ipp_attribute_t
*attr
; /* Attribute */
2013 int i
, /* Looping var */
2014 count
; /* Number of values */
2015 char *ptr
; /* Pointer into value */
2016 ipp_tag_t value_tag
; /* Value tag for this attribute */
2020 * Don't allow empty values...
2023 if (!*value
&& strcmp(name
, "marker-message"))
2025 cupsdLogMessage(CUPSD_LOG_ERROR
, "Ignoring empty \"%s\" attribute", name
);
2030 * Count the number of values...
2033 for (count
= 1, ptr
= value
;
2034 (ptr
= strchr(ptr
, ',')) != NULL
;
2038 * Then add or update the attribute as needed...
2041 if (!strcmp(name
, "marker-levels") || !strcmp(name
, "marker-low-levels") ||
2042 !strcmp(name
, "marker-high-levels"))
2048 if ((attr
= ippFindAttribute(p
->attrs
, name
, IPP_TAG_INTEGER
)) != NULL
&&
2049 attr
->num_values
< count
)
2051 ippDeleteAttribute(p
->attrs
, attr
);
2056 attr
->num_values
= count
;
2058 attr
= ippAddIntegers(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, name
,
2063 cupsdLogMessage(CUPSD_LOG_ERROR
,
2064 "Unable to allocate memory for printer attribute "
2065 "(%d values)", count
);
2069 for (i
= 0; i
< count
; i
++)
2071 if ((ptr
= strchr(value
, ',')) != NULL
)
2074 attr
->values
[i
].integer
= strtol(value
, NULL
, 10);
2083 * Name or keyword values...
2086 if (!strcmp(name
, "marker-types"))
2087 value_tag
= IPP_TAG_KEYWORD
;
2088 else if (!strcmp(name
, "marker-message"))
2089 value_tag
= IPP_TAG_TEXT
;
2091 value_tag
= IPP_TAG_NAME
;
2093 if ((attr
= ippFindAttribute(p
->attrs
, name
, value_tag
)) != NULL
&&
2094 attr
->num_values
< count
)
2096 ippDeleteAttribute(p
->attrs
, attr
);
2102 for (i
= 0; i
< attr
->num_values
; i
++)
2103 _cupsStrFree(attr
->values
[i
].string
.text
);
2105 attr
->num_values
= count
;
2108 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, value_tag
, name
,
2113 cupsdLogMessage(CUPSD_LOG_ERROR
,
2114 "Unable to allocate memory for printer attribute "
2115 "(%d values)", count
);
2119 for (i
= 0; i
< count
; i
++)
2121 if ((ptr
= strchr(value
, ',')) != NULL
)
2124 attr
->values
[i
].string
.text
= _cupsStrAlloc(value
);
2134 * 'cupsdSetPrinterAttrs()' - Set printer attributes based upon the PPD file.
2138 cupsdSetPrinterAttrs(cupsd_printer_t
*p
)/* I - Printer to setup */
2140 int i
, /* Looping var */
2141 length
; /* Length of browse attributes */
2142 char resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2143 int num_air
; /* Number of auth-info-required values */
2144 const char * const *air
; /* auth-info-required values */
2145 cupsd_location_t
*auth
; /* Pointer to authentication element */
2146 const char *auth_supported
; /* Authentication supported */
2147 ipp_t
*oldattrs
; /* Old printer attributes */
2148 ipp_attribute_t
*attr
; /* Attribute data */
2149 cups_option_t
*option
; /* Current printer option */
2150 char *filter
; /* Current filter */
2151 static const char * const air_none
[] =
2152 { /* No authentication */
2155 static const char * const air_userpass
[] =
2156 { /* Basic/Digest authentication */
2162 DEBUG_printf(("cupsdSetPrinterAttrs: entering name = %s, type = %x\n", p
->name
,
2166 * Make sure that we have the common attributes defined...
2170 cupsdCreateCommonData();
2173 * Clear out old filters, if any...
2176 delete_printer_filters(p
);
2179 * Figure out the authentication that is required for the printer.
2182 auth_supported
= "requesting-user-name";
2186 if (p
->num_auth_info_required
> 0 && strcmp(p
->auth_info_required
[0], "none"))
2188 num_air
= p
->num_auth_info_required
;
2189 air
= p
->auth_info_required
;
2191 else if ((p
->type
& CUPS_PRINTER_AUTHENTICATED
) &&
2192 (p
->type
& CUPS_PRINTER_DISCOVERED
))
2198 if (p
->type
& CUPS_PRINTER_CLASS
)
2199 snprintf(resource
, sizeof(resource
), "/classes/%s", p
->name
);
2201 snprintf(resource
, sizeof(resource
), "/printers/%s", p
->name
);
2203 if ((auth
= cupsdFindBest(resource
, HTTP_POST
)) == NULL
||
2204 auth
->type
== CUPSD_AUTH_NONE
)
2205 auth
= cupsdFindPolicyOp(p
->op_policy_ptr
, IPP_PRINT_JOB
);
2209 int auth_type
; /* Authentication type */
2212 if ((auth_type
= auth
->type
) == CUPSD_AUTH_DEFAULT
)
2213 auth_type
= DefaultAuthType
;
2215 if (auth_type
== CUPSD_AUTH_BASIC
|| auth_type
== CUPSD_AUTH_BASICDIGEST
)
2216 auth_supported
= "basic";
2217 else if (auth_type
== CUPSD_AUTH_DIGEST
)
2218 auth_supported
= "digest";
2220 else if (auth_type
== CUPSD_AUTH_NEGOTIATE
)
2221 auth_supported
= "negotiate";
2222 #endif /* HAVE_GSSAPI */
2224 if (!(p
->type
& CUPS_PRINTER_DISCOVERED
))
2226 if (auth_type
!= CUPSD_AUTH_NONE
)
2227 p
->type
|= CUPS_PRINTER_AUTHENTICATED
;
2229 p
->type
&= ~CUPS_PRINTER_AUTHENTICATED
;
2232 else if (!(p
->type
& CUPS_PRINTER_DISCOVERED
))
2233 p
->type
&= ~CUPS_PRINTER_AUTHENTICATED
;
2236 * Create the required IPP attributes for a printer...
2239 oldattrs
= p
->attrs
;
2240 p
->attrs
= ippNew();
2242 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2243 "uri-authentication-supported", NULL
, auth_supported
);
2244 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2245 "uri-security-supported", NULL
, "none");
2246 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "printer-name", NULL
,
2248 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-location",
2249 NULL
, p
->location
? p
->location
: "");
2250 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-info",
2251 NULL
, p
->info
? p
->info
: "");
2256 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2257 "requesting-user-name-denied", p
->num_users
, NULL
,
2260 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2261 "requesting-user-name-allowed", p
->num_users
, NULL
,
2265 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2266 "job-quota-period", p
->quota_period
);
2267 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2268 "job-k-limit", p
->k_limit
);
2269 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2270 "job-page-limit", p
->page_limit
);
2271 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2272 "auth-info-required", num_air
, NULL
, air
);
2274 if (cupsArrayCount(Banners
) > 0 && !(p
->type
& CUPS_PRINTER_DISCOVERED
))
2277 * Setup the job-sheets-default attribute...
2280 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2281 "job-sheets-default", 2, NULL
, NULL
);
2285 attr
->values
[0].string
.text
= _cupsStrAlloc(Classification
?
2286 Classification
: p
->job_sheets
[0]);
2287 attr
->values
[1].string
.text
= _cupsStrAlloc(Classification
?
2288 Classification
: p
->job_sheets
[1]);
2295 if (p
->type
& CUPS_PRINTER_DISCOVERED
)
2298 * Tell the client this is a remote printer of some type...
2301 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
2302 "printer-uri-supported", NULL
, p
->uri
);
2304 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "printer-more-info",
2308 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
2309 "printer-make-and-model", NULL
, p
->make_model
);
2311 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri", NULL
,
2320 * Assign additional attributes depending on whether this is a printer
2324 if (p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
2327 p
->type
&= ~CUPS_PRINTER_OPTIONS
;
2330 * Add class-specific attributes...
2333 if ((p
->type
& CUPS_PRINTER_IMPLICIT
) && p
->num_printers
> 0 &&
2334 p
->printers
[0]->make_model
)
2335 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
2336 "printer-make-and-model", NULL
, p
->printers
[0]->make_model
);
2338 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
2339 "printer-make-and-model", NULL
, "Local Printer Class");
2341 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri", NULL
,
2342 "file:///dev/null");
2344 if (p
->num_printers
> 0)
2347 * Add a list of member names; URIs are added in copy_printer_attrs...
2350 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2351 "member-names", p
->num_printers
, NULL
, NULL
);
2352 p
->type
|= CUPS_PRINTER_OPTIONS
;
2354 for (i
= 0; i
< p
->num_printers
; i
++)
2357 attr
->values
[i
].string
.text
= _cupsStrRetain(p
->printers
[i
]->name
);
2359 p
->type
&= ~CUPS_PRINTER_OPTIONS
| p
->printers
[i
]->type
;
2366 * Add printer-specific attributes...
2369 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri", NULL
,
2370 p
->sanitized_device_uri
);
2373 * Assign additional attributes from the PPD file (if any)...
2379 * Add filters for printer...
2382 cupsdSetPrinterReasons(p
, "-cups-missing-filter-warning,"
2383 "cups-insecure-filter-warning");
2385 for (filter
= (char *)cupsArrayFirst(p
->filters
);
2387 filter
= (char *)cupsArrayNext(p
->filters
))
2388 add_printer_filter(p
, p
->filetype
, filter
);
2392 p
->prefiltertype
= mimeAddType(MimeDatabase
, "prefilter", p
->name
);
2394 for (filter
= (char *)cupsArrayFirst(p
->pre_filters
);
2396 filter
= (char *)cupsArrayNext(p
->pre_filters
))
2397 add_printer_filter(p
, p
->prefiltertype
, filter
);
2403 * Copy marker attributes as needed...
2408 ipp_attribute_t
*oldattr
; /* Old attribute */
2411 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-colors",
2412 IPP_TAG_NAME
)) != NULL
)
2414 if ((attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2415 "marker-colors", oldattr
->num_values
, NULL
,
2418 for (i
= 0; i
< oldattr
->num_values
; i
++)
2419 attr
->values
[i
].string
.text
=
2420 _cupsStrRetain(oldattr
->values
[i
].string
.text
);
2424 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-levels",
2425 IPP_TAG_INTEGER
)) != NULL
)
2427 if ((attr
= ippAddIntegers(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2428 "marker-levels", oldattr
->num_values
,
2431 for (i
= 0; i
< oldattr
->num_values
; i
++)
2432 attr
->values
[i
].integer
= oldattr
->values
[i
].integer
;
2436 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-message",
2437 IPP_TAG_TEXT
)) != NULL
)
2438 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "marker-message",
2439 NULL
, oldattr
->values
[0].string
.text
);
2441 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-low-levels",
2442 IPP_TAG_INTEGER
)) != NULL
)
2444 if ((attr
= ippAddIntegers(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2445 "marker-low-levels", oldattr
->num_values
,
2448 for (i
= 0; i
< oldattr
->num_values
; i
++)
2449 attr
->values
[i
].integer
= oldattr
->values
[i
].integer
;
2453 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-high-levels",
2454 IPP_TAG_INTEGER
)) != NULL
)
2456 if ((attr
= ippAddIntegers(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2457 "marker-high-levels", oldattr
->num_values
,
2460 for (i
= 0; i
< oldattr
->num_values
; i
++)
2461 attr
->values
[i
].integer
= oldattr
->values
[i
].integer
;
2465 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-names",
2466 IPP_TAG_NAME
)) != NULL
)
2468 if ((attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2469 "marker-names", oldattr
->num_values
, NULL
,
2472 for (i
= 0; i
< oldattr
->num_values
; i
++)
2473 attr
->values
[i
].string
.text
=
2474 _cupsStrRetain(oldattr
->values
[i
].string
.text
);
2478 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-types",
2479 IPP_TAG_KEYWORD
)) != NULL
)
2481 if ((attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2482 "marker-types", oldattr
->num_values
, NULL
,
2485 for (i
= 0; i
< oldattr
->num_values
; i
++)
2486 attr
->values
[i
].string
.text
=
2487 _cupsStrRetain(oldattr
->values
[i
].string
.text
);
2491 ippDelete(oldattrs
);
2495 * Force sharing off for remote queues...
2498 if (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
))
2503 * Copy the printer options into a browse attributes string we can re-use.
2506 const char *valptr
; /* Pointer into value */
2507 char *attrptr
; /* Pointer into attribute string */
2511 * Free the old browse attributes as needed...
2514 if (p
->browse_attrs
)
2515 free(p
->browse_attrs
);
2518 * Compute the length of all attributes + job-sheets, lease-duration,
2519 * and BrowseLocalOptions.
2522 for (length
= 1, i
= p
->num_options
, option
= p
->options
;
2526 length
+= strlen(option
->name
) + 2;
2530 for (valptr
= option
->value
; *valptr
; valptr
++)
2531 if (strchr(" \"\'\\", *valptr
))
2538 length
+= 13 + strlen(p
->job_sheets
[0]) + strlen(p
->job_sheets
[1]);
2540 if (BrowseLocalOptions
)
2541 length
+= 12 + strlen(BrowseLocalOptions
);
2544 * Allocate the new string...
2547 if ((p
->browse_attrs
= calloc(1, length
)) == NULL
)
2548 cupsdLogMessage(CUPSD_LOG_ERROR
,
2549 "Unable to allocate %d bytes for browse data!",
2554 * Got the allocated string, now copy the options and attributes over...
2557 sprintf(p
->browse_attrs
, "job-sheets=%s,%s lease-duration=%d",
2558 p
->job_sheets
[0], p
->job_sheets
[1], BrowseTimeout
);
2559 attrptr
= p
->browse_attrs
+ strlen(p
->browse_attrs
);
2561 if (BrowseLocalOptions
)
2563 sprintf(attrptr
, " ipp-options=%s", BrowseLocalOptions
);
2564 attrptr
+= strlen(attrptr
);
2567 for (i
= p
->num_options
, option
= p
->options
;
2572 strcpy(attrptr
, option
->name
);
2573 attrptr
+= strlen(attrptr
);
2579 for (valptr
= option
->value
; *valptr
; valptr
++)
2581 if (strchr(" \"\'\\", *valptr
))
2584 *attrptr
++ = *valptr
;
2592 * Populate the document-format-supported attribute...
2595 add_printer_formats(p
);
2597 DEBUG_printf(("cupsdSetPrinterAttrs: leaving name = %s, type = %x\n", p
->name
,
2601 * Add name-default attributes...
2604 add_printer_defaults(p
);
2608 * Write the IRIX printer config and status files...
2611 write_irix_config(p
);
2612 write_irix_state(p
);
2616 * Let the browse protocols reflect the change
2619 cupsdRegisterPrinter(p
);
2624 * 'cupsdSetPrinterReasons()' - Set/update the reasons strings.
2627 int /* O - 1 if something changed, 0 otherwise */
2628 cupsdSetPrinterReasons(
2629 cupsd_printer_t
*p
, /* I - Printer */
2630 const char *s
) /* I - Reasons strings */
2632 int i
, /* Looping var */
2633 changed
= 0; /* Did something change? */
2634 const char *sptr
; /* Pointer into reasons */
2635 char reason
[255], /* Reason string */
2636 *rptr
; /* Pointer into reason */
2639 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2640 "cupsdSetPrinterReasons(p=%p(%s),s=\"%s\"", p
, p
->name
, s
);
2642 if (s
[0] == '-' || s
[0] == '+')
2645 * Add/remove reasons...
2653 * Replace reasons...
2658 for (i
= 0; i
< p
->num_reasons
; i
++)
2659 _cupsStrFree(p
->reasons
[i
]);
2664 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
2666 if (PrintcapFormat
== PRINTCAP_PLIST
)
2667 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2670 if (!strcmp(s
, "none"))
2674 * Loop through all of the reasons...
2680 * Skip leading whitespace and commas...
2683 while (isspace(*sptr
& 255) || *sptr
== ',')
2686 for (rptr
= reason
; *sptr
&& !isspace(*sptr
& 255) && *sptr
!= ','; sptr
++)
2687 if (rptr
< (reason
+ sizeof(reason
) - 1))
2701 for (i
= 0; i
< p
->num_reasons
; i
++)
2702 if (!strcmp(reason
, p
->reasons
[i
]))
2705 * Found a match, so remove it...
2710 _cupsStrFree(p
->reasons
[i
]);
2712 if (i
< p
->num_reasons
)
2713 memmove(p
->reasons
+ i
, p
->reasons
+ i
+ 1,
2714 (p
->num_reasons
- i
) * sizeof(char *));
2716 if (!strcmp(reason
, "paused") && p
->state
== IPP_PRINTER_STOPPED
)
2717 cupsdSetPrinterState(p
, IPP_PRINTER_IDLE
, 1);
2719 if (strcmp(reason
, "connecting-to-device"))
2721 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
2723 if (PrintcapFormat
== PRINTCAP_PLIST
)
2724 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2729 else if (p
->num_reasons
< (int)(sizeof(p
->reasons
) / sizeof(p
->reasons
[0])))
2735 for (i
= 0; i
< p
->num_reasons
; i
++)
2736 if (!strcmp(reason
, p
->reasons
[i
]))
2739 if (i
>= p
->num_reasons
)
2741 if (i
>= (int)(sizeof(p
->reasons
) / sizeof(p
->reasons
[0])))
2743 cupsdLogMessage(CUPSD_LOG_ALERT
,
2744 "Too many printer-state-reasons values for %s (%d)",
2749 p
->reasons
[i
] = _cupsStrAlloc(reason
);
2753 if (!strcmp(reason
, "paused") && p
->state
!= IPP_PRINTER_STOPPED
)
2754 cupsdSetPrinterState(p
, IPP_PRINTER_STOPPED
, 1);
2756 if (strcmp(reason
, "connecting-to-device"))
2758 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
2760 if (PrintcapFormat
== PRINTCAP_PLIST
)
2761 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2772 * 'cupsdSetPrinterState()' - Update the current state of a printer.
2776 cupsdSetPrinterState(
2777 cupsd_printer_t
*p
, /* I - Printer to change */
2778 ipp_pstate_t s
, /* I - New state */
2779 int update
) /* I - Update printers.conf? */
2781 ipp_pstate_t old_state
; /* Old printer state */
2785 * Can't set status of remote printers...
2788 if (p
->type
& CUPS_PRINTER_DISCOVERED
)
2792 * Set the new state...
2795 if (PrintcapFormat
== PRINTCAP_PLIST
)
2796 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2798 old_state
= p
->state
;
2803 cupsdAddEvent(s
== IPP_PRINTER_STOPPED
? CUPSD_EVENT_PRINTER_STOPPED
:
2804 CUPSD_EVENT_PRINTER_STATE
, p
, NULL
,
2805 "%s \"%s\" state changed.",
2806 (p
->type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
2810 * Let the browse code know this needs to be updated...
2814 p
->state_time
= time(NULL
);
2818 write_irix_state(p
);
2823 * Set/clear the paused reason as needed...
2826 if (s
== IPP_PRINTER_STOPPED
)
2827 cupsdSetPrinterReasons(p
, "+paused");
2829 cupsdSetPrinterReasons(p
, "-paused");
2832 * Clear the message for the queue when going to processing...
2835 if (s
== IPP_PRINTER_PROCESSING
)
2836 p
->state_message
[0] = '\0';
2839 * Update the printer history...
2842 cupsdAddPrinterHistory(p
);
2845 * Let the browse protocols reflect the change...
2849 cupsdRegisterPrinter(p
);
2852 * Save the printer configuration if a printer goes from idle or processing
2853 * to stopped (or visa-versa)...
2857 (old_state
== IPP_PRINTER_STOPPED
) != (s
== IPP_PRINTER_STOPPED
))
2859 if (p
->type
& CUPS_PRINTER_CLASS
)
2860 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
2862 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
2868 * 'cupsdStopPrinter()' - Stop a printer from printing any jobs...
2872 cupsdStopPrinter(cupsd_printer_t
*p
, /* I - Printer to stop */
2873 int update
)/* I - Update printers.conf? */
2876 * Set the printer state...
2879 cupsdSetPrinterState(p
, IPP_PRINTER_STOPPED
, update
);
2882 * See if we have a job printing on this printer...
2885 if (p
->job
&& p
->job
->state_value
== IPP_JOB_PROCESSING
)
2886 cupsdSetJobState(p
->job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
2887 "Job stopped due to printer being paused.");
2892 * 'cupsdUpdatePrinterPPD()' - Update keywords in a printer's PPD file.
2895 int /* O - 1 if successful, 0 otherwise */
2896 cupsdUpdatePrinterPPD(
2897 cupsd_printer_t
*p
, /* I - Printer */
2898 int num_keywords
, /* I - Number of keywords */
2899 cups_option_t
*keywords
) /* I - Keywords */
2901 int i
; /* Looping var */
2902 cups_file_t
*src
, /* Original file */
2903 *dst
; /* New file */
2904 char srcfile
[1024], /* Original filename */
2905 dstfile
[1024], /* New filename */
2906 line
[1024], /* Line from file */
2907 keystring
[41]; /* Keyword from line */
2908 cups_option_t
*keyword
; /* Current keyword */
2911 cupsdLogMessage(CUPSD_LOG_INFO
, "Updating keywords in PPD file for %s...",
2915 * Get the old and new PPD filenames...
2918 snprintf(srcfile
, sizeof(srcfile
), "%s/ppd/%s.ppd.O", ServerRoot
, p
->name
);
2919 snprintf(dstfile
, sizeof(srcfile
), "%s/ppd/%s.ppd", ServerRoot
, p
->name
);
2922 * Rename the old file and open the old and new...
2925 if (rename(dstfile
, srcfile
))
2927 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to backup PPD file for %s: %s",
2928 p
->name
, strerror(errno
));
2932 if ((src
= cupsFileOpen(srcfile
, "r")) == NULL
)
2934 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open PPD file \"%s\": %s",
2935 srcfile
, strerror(errno
));
2936 rename(srcfile
, dstfile
);
2940 if ((dst
= cupsFileOpen(dstfile
, "w")) == NULL
)
2942 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create PPD file \"%s\": %s",
2943 dstfile
, strerror(errno
));
2945 rename(srcfile
, dstfile
);
2950 * Copy the first line and then write out all of the keywords...
2953 if (!cupsFileGets(src
, line
, sizeof(line
)))
2955 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to read PPD file \"%s\": %s",
2956 srcfile
, strerror(errno
));
2959 rename(srcfile
, dstfile
);
2963 cupsFilePrintf(dst
, "%s\n", line
);
2965 for (i
= num_keywords
, keyword
= keywords
; i
> 0; i
--, keyword
++)
2967 cupsdLogMessage(CUPSD_LOG_DEBUG
, "*%s: %s", keyword
->name
, keyword
->value
);
2968 cupsFilePrintf(dst
, "*%s: %s\n", keyword
->name
, keyword
->value
);
2972 * Then copy the rest of the PPD file, dropping any keywords we changed.
2975 while (cupsFileGets(src
, line
, sizeof(line
)))
2978 * Skip keywords we've already set...
2981 if (sscanf(line
, "*%40[^:]:", keystring
) == 1 &&
2982 cupsGetOption(keystring
, num_keywords
, keywords
))
2986 * Otherwise write the line...
2989 cupsFilePrintf(dst
, "%s\n", line
);
2993 * Close files and return...
3004 * 'cupsdUpdatePrinters()' - Update printers after a partial reload.
3008 cupsdUpdatePrinters(void)
3010 cupsd_printer_t
*p
; /* Current printer */
3014 * Loop through the printers and recreate the printer attributes
3015 * for any local printers since the policy and/or access control
3016 * stuff may have changed. Also, if browsing is disabled, remove
3017 * any remote printers...
3020 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3022 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3025 * Remove remote printers if we are no longer browsing...
3029 (p
->type
& (CUPS_PRINTER_IMPLICIT
| CUPS_PRINTER_DISCOVERED
)))
3031 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
3032 cupsArrayRemove(ImplicitPrinters
, p
);
3034 cupsArraySave(Printers
);
3035 cupsdDeletePrinter(p
, 0);
3036 cupsArrayRestore(Printers
);
3041 * Update the operation policy pointer...
3044 if ((p
->op_policy_ptr
= cupsdFindPolicy(p
->op_policy
)) == NULL
)
3045 p
->op_policy_ptr
= DefaultPolicyPtr
;
3048 * Update printer attributes as needed...
3051 if (!(p
->type
& CUPS_PRINTER_DISCOVERED
))
3052 cupsdSetPrinterAttrs(p
);
3058 * 'cupsdValidateDest()' - Validate a printer/class destination.
3061 const char * /* O - Printer or class name */
3063 const char *uri
, /* I - Printer URI */
3064 cups_ptype_t
*dtype
, /* O - Type (printer or class) */
3065 cupsd_printer_t
**printer
) /* O - Printer pointer */
3067 cupsd_printer_t
*p
; /* Current printer */
3068 char localname
[1024],/* Localized hostname */
3069 *lptr
, /* Pointer into localized hostname */
3070 *sptr
, /* Pointer into server name */
3071 *rptr
, /* Pointer into resource */
3072 scheme
[32], /* Scheme portion of URI */
3073 username
[64], /* Username portion of URI */
3074 hostname
[HTTP_MAX_HOST
],
3075 /* Host portion of URI */
3076 resource
[HTTP_MAX_URI
];
3077 /* Resource portion of URI */
3078 int port
; /* Port portion of URI */
3081 DEBUG_printf(("cupsdValidateDest(uri=\"%s\", dtype=%p, printer=%p)\n", uri
,
3085 * Initialize return values...
3092 *dtype
= (cups_ptype_t
)0;
3095 * Pull the hostname and resource from the URI...
3098 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
3099 username
, sizeof(username
), hostname
, sizeof(hostname
),
3100 &port
, resource
, sizeof(resource
));
3103 * See if the resource is a class or printer...
3106 if (!strncmp(resource
, "/classes/", 9))
3112 rptr
= resource
+ 9;
3114 else if (!strncmp(resource
, "/printers/", 10))
3120 rptr
= resource
+ 10;
3125 * Bad resource name...
3132 * See if the printer or class name exists...
3135 p
= cupsdFindDest(rptr
);
3137 if (p
== NULL
&& strchr(rptr
, '@') == NULL
)
3145 *dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
|
3146 CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
);
3152 * Change localhost to the server name...
3155 if (!strcasecmp(hostname
, "localhost"))
3156 strlcpy(hostname
, ServerName
, sizeof(hostname
));
3158 strlcpy(localname
, hostname
, sizeof(localname
));
3160 if (!strcasecmp(hostname
, ServerName
))
3163 * Localize the hostname...
3166 lptr
= strchr(localname
, '.');
3167 sptr
= strchr(ServerName
, '.');
3169 if (sptr
!= NULL
&& lptr
!= NULL
)
3172 * Strip the common domain name components...
3175 while (lptr
!= NULL
)
3177 if (!strcasecmp(lptr
, sptr
))
3183 lptr
= strchr(lptr
+ 1, '.');
3188 DEBUG_printf(("localized hostname is \"%s\"...\n", localname
));
3191 * Find a matching printer or class...
3194 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3196 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3197 if (!strcasecmp(p
->hostname
, localname
) &&
3198 !strcasecmp(p
->name
, rptr
))
3204 *dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
|
3205 CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
);
3215 * 'cupsdWritePrintcap()' - Write a pseudo-printcap file for older applications
3220 cupsdWritePrintcap(void)
3222 int i
; /* Looping var */
3223 cups_file_t
*fp
; /* Printcap file */
3224 cupsd_printer_t
*p
; /* Current printer */
3229 * Update the IRIX printer state for the default printer; if
3230 * no printers remain, then the default printer file will be
3234 write_irix_state(DefaultPrinter
);
3238 * See if we have a printcap file; if not, don't bother writing it.
3241 if (!Printcap
|| !*Printcap
)
3244 cupsdLogMessage(CUPSD_LOG_INFO
, "Generating printcap %s...", Printcap
);
3247 * Open the printcap file...
3250 if ((fp
= cupsFileOpen(Printcap
, "w")) == NULL
)
3254 * Put a comment header at the top so that users will know where the
3255 * data has come from...
3258 if (PrintcapFormat
!= PRINTCAP_PLIST
)
3259 cupsFilePrintf(fp
, "# This file was automatically generated by cupsd(8) "
3261 "# %s/printers.conf file. All changes to this file\n"
3262 "# will be lost.\n", ServerRoot
);
3265 * Write a new printcap with the current list of printers.
3268 switch (PrintcapFormat
)
3272 * Each printer is put in the file as:
3282 cupsFilePrintf(fp
, "%s|%s:rm=%s:rp=%s:\n", DefaultPrinter
->name
,
3283 DefaultPrinter
->info
, ServerName
,
3284 DefaultPrinter
->name
);
3286 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3288 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3289 if (p
!= DefaultPrinter
)
3290 cupsFilePrintf(fp
, "%s|%s:rm=%s:rp=%s:\n", p
->name
, p
->info
,
3291 ServerName
, p
->name
);
3294 case PRINTCAP_PLIST
:
3296 * Each printer is written as a dictionary in a plist file.
3297 * Currently the printer-name, printer-info, printer-is-accepting-jobs,
3298 * printer-location, printer-make-and-model, printer-state,
3299 * printer-state-reasons, printer-type, and (sanitized) device-uri.
3302 cupsFilePuts(fp
, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
3303 "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD "
3304 "PLIST 1.0//EN\" \"http://www.apple.com/DTDs/"
3305 "PropertyList-1.0.dtd\">\n"
3306 "<plist version=\"1.0\">\n"
3309 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3311 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3313 cupsFilePuts(fp
, "\t<dict>\n"
3314 "\t\t<key>printer-name</key>\n"
3316 write_xml_string(fp
, p
->name
);
3317 cupsFilePuts(fp
, "</string>\n"
3318 "\t\t<key>printer-info</key>\n"
3320 write_xml_string(fp
, p
->info
);
3321 cupsFilePrintf(fp
, "</string>\n"
3322 "\t\t<key>printer-is-accepting-jobs</key>\n"
3324 "\t\t<key>printer-location</key>\n"
3325 "\t\t<string>", p
->accepting
? "true" : "false");
3326 write_xml_string(fp
, p
->location
);
3327 cupsFilePuts(fp
, "</string>\n"
3328 "\t\t<key>printer-make-and-model</key>\n"
3330 write_xml_string(fp
, p
->make_model
);
3331 cupsFilePrintf(fp
, "</string>\n"
3332 "\t\t<key>printer-state</key>\n"
3333 "\t\t<integer>%d</integer>\n"
3334 "\t\t<key>printer-state-reasons</key>\n"
3335 "\t\t<array>\n", p
->state
);
3336 for (i
= 0; i
< p
->num_reasons
; i
++)
3338 cupsFilePuts(fp
, "\t\t\t<string>");
3339 write_xml_string(fp
, p
->reasons
[i
]);
3340 cupsFilePuts(fp
, "</string>\n");
3342 cupsFilePrintf(fp
, "\t\t</array>\n"
3343 "\t\t<key>printer-type</key>\n"
3344 "\t\t<integer>%d</integer>\n"
3345 "\t\t<key>device-uri</key>\n"
3346 "\t\t<string>", p
->type
);
3347 write_xml_string(fp
, p
->sanitized_device_uri
);
3348 cupsFilePuts(fp
, "</string>\n"
3351 cupsFilePuts(fp
, "</array>\n"
3355 case PRINTCAP_SOLARIS
:
3357 * Each printer is put in the file as:
3359 * _all:all=Printer1,Printer2,Printer3,...,PrinterN
3360 * _default:use=DefaultPrinter
3362 * :bsdaddr=ServerName,Printer1:\
3363 * :description=Description:
3365 * :bsdaddr=ServerName,Printer2:\
3366 * :description=Description:
3368 * :bsdaddr=ServerName,Printer3:\
3369 * :description=Description:
3372 * :bsdaddr=ServerName,PrinterN:\
3373 * :description=Description:
3376 cupsFilePuts(fp
, "_all:all=");
3377 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3379 p
= (cupsd_printer_t
*)cupsArrayCurrent(Printers
))
3380 cupsFilePrintf(fp
, "%s%c", p
->name
,
3381 cupsArrayNext(Printers
) ? ',' : '\n');
3384 cupsFilePrintf(fp
, "_default:use=%s\n", DefaultPrinter
->name
);
3386 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3388 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3389 cupsFilePrintf(fp
, "%s:\\\n"
3390 "\t:bsdaddr=%s,%s:\\\n"
3391 "\t:description=%s:\n",
3392 p
->name
, ServerName
, p
->name
,
3393 p
->info
? p
->info
: "");
3406 * 'add_printer_defaults()' - Add name-default attributes to the printer attributes.
3410 add_printer_defaults(cupsd_printer_t
*p
)/* I - Printer */
3412 int i
; /* Looping var */
3413 int num_options
; /* Number of default options */
3414 cups_option_t
*options
, /* Default options */
3415 *option
; /* Current option */
3416 char name
[256]; /* name-default */
3420 * Maintain a common array of default attribute names...
3423 if (!CommonDefaults
)
3425 CommonDefaults
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
3427 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("copies-default"));
3428 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("document-format-default"));
3429 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("finishings-default"));
3430 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("job-hold-until-default"));
3431 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("job-priority-default"));
3432 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("job-sheets-default"));
3433 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("media-default"));
3434 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("number-up-default"));
3435 cupsArrayAdd(CommonDefaults
,
3436 _cupsStrAlloc("orientation-requested-default"));
3437 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("sides-default"));
3441 * Add all of the default options from the .conf files...
3444 for (num_options
= 0, options
= NULL
, i
= p
->num_options
, option
= p
->options
;
3448 if (strcmp(option
->name
, "ipp-options") &&
3449 strcmp(option
->name
, "job-sheets") &&
3450 strcmp(option
->name
, "lease-duration"))
3452 snprintf(name
, sizeof(name
), "%s-default", option
->name
);
3453 num_options
= cupsAddOption(name
, option
->value
, num_options
, &options
);
3455 if (!cupsArrayFind(CommonDefaults
, name
))
3456 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc(name
));
3461 * Convert options to IPP attributes...
3464 cupsEncodeOptions2(p
->attrs
, num_options
, options
, IPP_TAG_PRINTER
);
3465 cupsFreeOptions(num_options
, options
);
3468 * Add standard -default attributes as needed...
3471 if (!cupsGetOption("copies", p
->num_options
, p
->options
))
3472 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "copies-default",
3475 if (!cupsGetOption("document-format", p
->num_options
, p
->options
))
3476 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_MIMETYPE
,
3477 "document-format-default", NULL
, "application/octet-stream");
3479 if (!cupsGetOption("job-hold-until", p
->num_options
, p
->options
))
3480 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
3481 "job-hold-until-default", NULL
, "no-hold");
3483 if (!cupsGetOption("job-priority", p
->num_options
, p
->options
))
3484 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
3485 "job-priority-default", 50);
3487 if (!cupsGetOption("number-up", p
->num_options
, p
->options
))
3488 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
3489 "number-up-default", 1);
3491 if (!cupsGetOption("orientation-requested", p
->num_options
, p
->options
))
3492 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NOVALUE
,
3493 "orientation-requested-default", NULL
, NULL
);
3495 if (!cupsGetOption("notify-lease-duration", p
->num_options
, p
->options
))
3496 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
3497 "notify-lease-duration-default", DefaultLeaseDuration
);
3499 if (!cupsGetOption("notify-events", p
->num_options
, p
->options
))
3500 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
3501 "notify-events-default", NULL
, "job-completed");
3506 * 'add_printer_filter()' - Add a MIME filter for a printer.
3511 cupsd_printer_t
*p
, /* I - Printer to add to */
3512 mime_type_t
*filtertype
, /* I - Filter or prefilter MIME type */
3513 const char *filter
) /* I - Filter to add */
3515 char super
[MIME_MAX_SUPER
], /* Super-type for filter */
3516 type
[MIME_MAX_TYPE
], /* Type for filter */
3517 program
[1024]; /* Program/filter name */
3518 int cost
; /* Cost of filter */
3519 mime_type_t
*temptype
; /* MIME type looping var */
3520 char filename
[1024], /* Full filter filename */
3521 *dirsep
; /* Pointer to directory separator */
3522 struct stat fileinfo
; /* File information */
3526 * Parse the filter string; it should be in the following format:
3528 * super/type cost program
3531 if (sscanf(filter
, "%15[^/]/%31s%d%*[ \t]%1023[^\n]", super
, type
, &cost
,
3534 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s: invalid filter string \"%s\"!",
3540 * See if the filter program exists; if not, stop the printer and flag
3544 if (strcmp(program
, "-"))
3546 if (program
[0] == '/')
3547 strlcpy(filename
, program
, sizeof(filename
));
3549 snprintf(filename
, sizeof(filename
), "%s/filter/%s", ServerBin
, program
);
3551 if (stat(filename
, &fileinfo
))
3553 memset(&fileinfo
, 0, sizeof(fileinfo
));
3555 snprintf(p
->state_message
, sizeof(p
->state_message
),
3556 "Filter \"%s\" for printer \"%s\" not available: %s",
3557 filename
, p
->name
, strerror(errno
));
3558 cupsdSetPrinterReasons(p
, "+cups-missing-filter-warning");
3560 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s", p
->state_message
);
3564 * When running as root, do additional security checks...
3570 * Only use filters that are owned by root and do not have world write
3574 if (fileinfo
.st_uid
|| (fileinfo
.st_mode
& (S_ISUID
| S_IWOTH
)) != 0)
3576 if (fileinfo
.st_uid
)
3577 snprintf(p
->state_message
, sizeof(p
->state_message
),
3578 "Filter \"%s\" for printer \"%s\" not owned by root",
3581 snprintf(p
->state_message
, sizeof(p
->state_message
),
3582 "Filter \"%s\" for printer \"%s\" has insecure permissions "
3583 "(0%o)", filename
, p
->name
, fileinfo
.st_mode
);
3585 cupsdSetPrinterReasons(p
, "+cups-insecure-filter-warning");
3587 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s", p
->state_message
);
3589 else if (fileinfo
.st_mode
)
3592 * Similarly, check that the parent directory is also owned by root and
3593 * does not have world write permissions.
3596 if ((dirsep
= strrchr(filename
, '/')) != NULL
)
3599 if (!stat(filename
, &fileinfo
) &&
3601 (fileinfo
.st_mode
& (S_ISUID
| S_IWOTH
)) != 0))
3603 if (fileinfo
.st_uid
)
3604 snprintf(p
->state_message
, sizeof(p
->state_message
),
3605 "Filter directory \"%s\" for printer \"%s\" not owned by "
3606 "root", filename
, p
->name
);
3608 snprintf(p
->state_message
, sizeof(p
->state_message
),
3609 "Filter directory \"%s\" for printer \"%s\" has insecure "
3610 "permissions (0%o)", filename
, p
->name
, fileinfo
.st_mode
);
3612 cupsdSetPrinterReasons(p
, "+cups-insecure-filter-warning");
3614 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s", p
->state_message
);
3621 * Add the filter to the MIME database, supporting wildcards as needed...
3624 for (temptype
= mimeFirstType(MimeDatabase
);
3626 temptype
= mimeNextType(MimeDatabase
))
3627 if (((super
[0] == '*' && strcasecmp(temptype
->super
, "printer")) ||
3628 !strcasecmp(temptype
->super
, super
)) &&
3629 (type
[0] == '*' || !strcasecmp(temptype
->type
, type
)))
3631 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3632 "add_printer_filter: %s: adding filter %s/%s %s/%s %d %s",
3633 p
->name
, temptype
->super
, temptype
->type
,
3634 filtertype
->super
, filtertype
->type
,
3636 mimeAddFilter(MimeDatabase
, temptype
, filtertype
, cost
, program
);
3642 * 'add_printer_formats()' - Add document-format-supported values for a printer.
3646 add_printer_formats(cupsd_printer_t
*p
) /* I - Printer */
3648 int i
; /* Looping var */
3649 mime_type_t
*type
; /* Current MIME type */
3650 cups_array_t
*filters
; /* Filters */
3651 ipp_attribute_t
*attr
; /* document-format-supported attribute */
3652 char mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
3653 /* MIME type name */
3657 * Raw (and remote) queues advertise all of the supported MIME
3661 cupsArrayDelete(p
->filetypes
);
3662 p
->filetypes
= NULL
;
3666 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
,
3667 (ipp_tag_t
)(IPP_TAG_MIMETYPE
| IPP_TAG_COPY
),
3668 "document-format-supported", NumMimeTypes
, NULL
, MimeTypes
);
3673 * Otherwise, loop through the supported MIME types and see if there
3674 * are filters for them...
3677 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_printer_formats: %d types, %d filters",
3678 mimeNumTypes(MimeDatabase
), mimeNumFilters(MimeDatabase
));
3680 p
->filetypes
= cupsArrayNew(NULL
, NULL
);
3682 for (type
= mimeFirstType(MimeDatabase
);
3684 type
= mimeNextType(MimeDatabase
))
3686 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", type
->super
, type
->type
);
3688 if ((filters
= mimeFilter(MimeDatabase
, type
, p
->filetype
, NULL
)) != NULL
)
3690 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3691 "add_printer_formats: %s: %s needs %d filters",
3692 p
->name
, mimetype
, cupsArrayCount(filters
));
3694 cupsArrayDelete(filters
);
3695 cupsArrayAdd(p
->filetypes
, type
);
3698 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3699 "add_printer_formats: %s: %s not supported",
3704 * Add the file formats that can be filtered...
3707 if ((type
= mimeType(MimeDatabase
, "application", "octet-stream")) == NULL
||
3708 !cupsArrayFind(p
->filetypes
, type
))
3713 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3714 "add_printer_formats: %s: %d supported types",
3715 p
->name
, cupsArrayCount(p
->filetypes
) + i
);
3717 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_MIMETYPE
,
3718 "document-format-supported",
3719 cupsArrayCount(p
->filetypes
) + i
, NULL
, NULL
);
3722 attr
->values
[0].string
.text
= _cupsStrAlloc("application/octet-stream");
3724 for (type
= (mime_type_t
*)cupsArrayFirst(p
->filetypes
);
3726 i
++, type
= (mime_type_t
*)cupsArrayNext(p
->filetypes
))
3728 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", type
->super
, type
->type
);
3730 attr
->values
[i
].string
.text
= _cupsStrAlloc(mimetype
);
3735 char pdl
[1024]; /* Buffer to build pdl list */
3736 mime_filter_t
*filter
; /* MIME filter looping var */
3741 if (mimeType(MimeDatabase
, "application", "pdf"))
3742 strlcat(pdl
, "application/pdf,", sizeof(pdl
));
3744 if (mimeType(MimeDatabase
, "application", "postscript"))
3745 strlcat(pdl
, "application/postscript,", sizeof(pdl
));
3747 if (mimeType(MimeDatabase
, "application", "vnd.cups-raster"))
3748 strlcat(pdl
, "application/vnd.cups-raster,", sizeof(pdl
));
3751 * Determine if this is a Tioga PrintJobMgr based queue...
3754 for (filter
= (mime_filter_t
*)cupsArrayFirst(MimeDatabase
->filters
);
3756 filter
= (mime_filter_t
*)cupsArrayNext(MimeDatabase
->filters
))
3758 if (filter
->dst
== p
->filetype
&& filter
->filter
&&
3759 strstr(filter
->filter
, "PrintJobMgr"))
3764 * We only support raw printing if this is not a Tioga PrintJobMgr based
3765 * queue and if application/octet-stream is a known conversion...
3768 if (!filter
&& mimeType(MimeDatabase
, "application", "octet-stream"))
3769 strlcat(pdl
, "application/octet-stream,", sizeof(pdl
));
3771 if (mimeType(MimeDatabase
, "image", "png"))
3772 strlcat(pdl
, "image/png,", sizeof(pdl
));
3775 pdl
[strlen(pdl
) - 1] = '\0'; /* Remove trailing comma */
3777 cupsdSetString(&p
->pdl
, pdl
);
3779 #endif /* HAVE_DNSSD */
3784 * 'add_string_array()' - Add a string to an array of CUPS strings.
3788 add_string_array(cups_array_t
**a
, /* I - Array */
3789 const char *s
) /* I - String */
3792 *a
= cupsArrayNew(NULL
, NULL
);
3794 cupsArrayAdd(*a
, _cupsStrAlloc(s
));
3799 * 'compare_printers()' - Compare two printers.
3802 static int /* O - Result of comparison */
3803 compare_printers(void *first
, /* I - First printer */
3804 void *second
, /* I - Second printer */
3805 void *data
) /* I - App data (not used) */
3807 return (strcasecmp(((cupsd_printer_t
*)first
)->name
,
3808 ((cupsd_printer_t
*)second
)->name
));
3813 * 'delete_printer_filters()' - Delete all MIME filters for a printer.
3817 delete_printer_filters(
3818 cupsd_printer_t
*p
) /* I - Printer to remove from */
3820 mime_filter_t
*filter
; /* MIME filter looping var */
3824 * Range check input...
3831 * Remove all filters from the MIME database that have a destination
3832 * type == printer...
3835 for (filter
= mimeFirstFilter(MimeDatabase
);
3837 filter
= mimeNextFilter(MimeDatabase
))
3838 if (filter
->dst
== p
->filetype
|| filter
->dst
== p
->prefiltertype
)
3841 * Delete the current filter...
3844 mimeDeleteFilter(MimeDatabase
, filter
);
3847 cupsdSetPrinterReasons(p
, "-cups-insecure-filter-warning"
3848 ",cups-missing-filter-warning");
3853 * 'delete_string_array()' - Delete an array of CUPS strings.
3857 delete_string_array(cups_array_t
**a
) /* I - Array */
3859 char *ptr
; /* Current string */
3862 for (ptr
= (char *)cupsArrayFirst(*a
);
3864 ptr
= (char *)cupsArrayNext(*a
))
3867 cupsArrayDelete(*a
);
3873 * 'load_ppd()' - Load a cached PPD file, updating the cache as needed.
3877 load_ppd(cupsd_printer_t
*p
) /* I - Printer */
3879 int i
; /* Looping var */
3880 cups_file_t
*cache
; /* Cache file */
3881 char cache_name
[1024]; /* Cache filename */
3882 struct stat cache_info
; /* Cache file info */
3883 ppd_file_t
*ppd
; /* PPD file */
3884 char ppd_name
[1024]; /* PPD filename */
3885 struct stat ppd_info
; /* PPD file info */
3886 int num_media
; /* Number of media options */
3887 char custom_in
[256], /* Custom size name in inches */
3888 custom_mm
[256]; /* Custom size name in millimeters */
3889 ppd_size_t
*size
; /* Current size */
3890 ppd_option_t
*output_bin
, /* OutputBin options */
3891 *duplex
; /* Duplex options */
3892 ppd_attr_t
*ppd_attr
; /* PPD attribute */
3893 _cups_pwg_media_t
*pwgmedia
; /* Matching PWG size name */
3894 ipp_attribute_t
*attr
; /* Attribute data */
3895 ipp_t
*media_col_default
, /* media-col-default collection value */
3896 *media_size
; /* media-size collection value */
3897 ipp_value_t
*val
; /* Attribute value */
3898 int num_finishings
; /* Number of finishings */
3899 int finishings
[5]; /* finishings-supported values */
3900 static const char * const sides
[3] = /* sides-supported values */
3903 "two-sided-long-edge",
3904 "two-sided-short-edge"
3906 static const char * const standard_commands
[] =
3907 { /* Standard CUPS commands */
3915 * Check to see if the cache is up-to-date...
3918 snprintf(cache_name
, sizeof(cache_name
), "%s/%s.ipp", CacheDir
, p
->name
);
3919 if (stat(cache_name
, &cache_info
))
3920 cache_info
.st_mtime
= 0;
3922 snprintf(ppd_name
, sizeof(ppd_name
), "%s/ppd/%s.ppd", ServerRoot
, p
->name
);
3923 if (stat(ppd_name
, &ppd_info
))
3924 ppd_info
.st_mtime
= 1;
3926 ippDelete(p
->ppd_attrs
);
3927 p
->ppd_attrs
= ippNew();
3929 if (cache_info
.st_mtime
>= ppd_info
.st_mtime
&&
3930 (cache
= cupsFileOpen(cache_name
, "r")) != NULL
)
3933 * Load cached information and return...
3936 cupsdLogMessage(CUPSD_LOG_DEBUG
, "load_ppd: Loading %s...", cache_name
);
3938 if (ippReadIO(cache
, (ipp_iocb_t
)cupsFileRead
, 1, NULL
,
3939 p
->ppd_attrs
) == IPP_DATA
)
3941 cupsFileClose(cache
);
3945 cupsFileClose(cache
);
3949 * Reload PPD attributes from disk...
3952 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3954 cupsdLogMessage(CUPSD_LOG_DEBUG
, "load_ppd: Loading %s...", ppd_name
);
3956 delete_string_array(&(p
->filters
));
3957 delete_string_array(&(p
->pre_filters
));
3959 p
->type
&= ~CUPS_PRINTER_OPTIONS
;
3960 p
->type
|= CUPS_PRINTER_BW
;
3962 finishings
[0] = IPP_FINISHINGS_NONE
;
3965 if ((ppd
= ppdOpenFile(ppd_name
)) != NULL
)
3968 * Add make/model and other various attributes...
3971 ppdMarkDefaults(ppd
);
3973 if (ppd
->color_device
)
3974 p
->type
|= CUPS_PRINTER_COLOR
;
3975 if (ppd
->variable_sizes
)
3976 p
->type
|= CUPS_PRINTER_VARIABLE
;
3977 if (!ppd
->manual_copies
)
3978 p
->type
|= CUPS_PRINTER_COPIES
;
3979 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsFax", NULL
)) != NULL
)
3980 if (ppd_attr
->value
&& !strcasecmp(ppd_attr
->value
, "true"))
3981 p
->type
|= CUPS_PRINTER_FAX
;
3983 ippAddBoolean(p
->ppd_attrs
, IPP_TAG_PRINTER
, "color-supported",
3985 if (ppd
->throughput
)
3986 ippAddInteger(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
3987 "pages-per-minute", ppd
->throughput
);
3992 * The NickName can be localized in the character set specified
3993 * by the LanugageEncoding attribute. However, ppdOpen2() has
3994 * already converted the ppd->nickname member to UTF-8 for us
3995 * (the original attribute value is available separately)
3998 cupsdSetString(&p
->make_model
, ppd
->nickname
);
4000 else if (ppd
->modelname
)
4003 * Model name can only contain specific characters...
4006 cupsdSetString(&p
->make_model
, ppd
->modelname
);
4009 cupsdSetString(&p
->make_model
, "Bad PPD File");
4011 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4012 "printer-make-and-model", NULL
, p
->make_model
);
4015 * Add media options from the PPD file...
4018 if (ppd
->num_sizes
== 0)
4020 if (!ppdFindAttr(ppd
, "APScannerOnly", NULL
))
4021 cupsdLogMessage(CUPSD_LOG_CRIT
,
4022 "The PPD file for printer %s contains no media "
4023 "options and is therefore invalid!", p
->name
);
4025 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4026 "media-supported", NULL
, "unknown");
4030 num_media
= ppd
->num_sizes
;
4031 if (ppd
->variable_sizes
)
4034 attr
= ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4035 "media-supported", num_media
, NULL
, NULL
);
4040 for (i
= ppd
->num_sizes
, size
= ppd
->sizes
; i
> 0; i
--, size
++)
4042 if (strcasecmp(size
->name
, "Custom"))
4044 if ((pwgmedia
= _cupsPWGMediaBySize(size
->width
,
4045 size
->length
)) != NULL
)
4047 val
->string
.text
= _cupsStrAlloc(pwgmedia
->pwg
);
4051 snprintf(custom_in
, sizeof(custom_in
), "adobe_%s_%gx%gin",
4052 size
->name
, size
->width
/ 72.0, size
->length
/ 72.0);
4053 snprintf(custom_mm
, sizeof(custom_mm
), "adobe_%s_%gx%gmm",
4054 size
->name
, size
->width
* 25.4 / 72.0,
4055 size
->length
* 25.4 / 72.0);
4056 if (strlen(custom_in
) < strlen(custom_mm
))
4057 val
->string
.text
= _cupsStrAlloc(custom_in
);
4059 val
->string
.text
= _cupsStrAlloc(custom_mm
);
4065 * Add media-default...
4068 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4069 "media-default", NULL
, val
->string
.text
);
4072 * Add media-col-default...
4075 media_size
= ippNew();
4076 ippAddInteger(media_size
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4077 "x-dimension", (int)(size
->width
* 2540.0 / 72.0));
4078 ippAddInteger(media_size
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4079 "y-dimension", (int)(size
->length
* 2540.0 / 72.0));
4081 media_col_default
= ippNew();
4082 ippAddString(media_col_default
, IPP_TAG_PRINTER
,
4083 IPP_TAG_KEYWORD
| IPP_TAG_COPY
, "media-color", NULL
,
4085 ippAddString(media_col_default
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4086 "media-key", NULL
,val
->string
.text
);
4087 ippAddCollection(media_col_default
, IPP_TAG_PRINTER
, "media-size",
4089 ippAddString(media_col_default
, IPP_TAG_PRINTER
,
4090 IPP_TAG_KEYWORD
| IPP_TAG_COPY
, "media-type", NULL
,
4093 ippAddCollection(p
->ppd_attrs
, IPP_TAG_PRINTER
,
4094 "media-col-default", media_col_default
);
4101 if (ppd
->variable_sizes
)
4103 snprintf(custom_in
, sizeof(custom_in
), "custom_min_%gx%gin",
4104 ppd
->custom_min
[0] / 72.0, ppd
->custom_min
[1] / 72.0);
4105 snprintf(custom_mm
, sizeof(custom_mm
), "custom_min_%gx%gmm",
4106 ppd
->custom_min
[0] * 25.4 / 72.0,
4107 ppd
->custom_min
[1] * 25.4 / 72.0);
4108 if (strlen(custom_in
) < strlen(custom_mm
))
4109 val
->string
.text
= _cupsStrAlloc(custom_in
);
4111 val
->string
.text
= _cupsStrAlloc(custom_mm
);
4114 snprintf(custom_in
, sizeof(custom_in
), "custom_max_%gx%gin",
4115 ppd
->custom_max
[0] / 72.0, ppd
->custom_max
[1] / 72.0);
4116 snprintf(custom_mm
, sizeof(custom_mm
), "custom_max_%gx%gmm",
4117 ppd
->custom_max
[0] * 25.4 / 72.0,
4118 ppd
->custom_max
[1] * 25.4 / 72.0);
4119 if (strlen(custom_in
) < strlen(custom_mm
))
4120 val
->string
.text
= _cupsStrAlloc(custom_in
);
4122 val
->string
.text
= _cupsStrAlloc(custom_mm
);
4131 if ((output_bin
= ppdFindOption(ppd
, "OutputBin")) != NULL
)
4133 attr
= ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4134 "output-bin-supported", output_bin
->num_choices
,
4139 for (i
= 0, val
= attr
->values
;
4140 i
< output_bin
->num_choices
;
4142 val
->string
.text
= _cupsStrAlloc(output_bin
->choices
[i
].choice
);
4145 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4146 "output-bin-default", NULL
, output_bin
->defchoice
);
4153 if ((duplex
= ppdFindOption(ppd
, "Duplex")) == NULL
)
4154 if ((duplex
= ppdFindOption(ppd
, "EFDuplex")) == NULL
)
4155 if ((duplex
= ppdFindOption(ppd
, "EFDuplexing")) == NULL
)
4156 if ((duplex
= ppdFindOption(ppd
, "KD03Duplex")) == NULL
)
4157 duplex
= ppdFindOption(ppd
, "JCLDuplex");
4159 if (duplex
&& duplex
->num_choices
> 1 &&
4160 !ppdInstallableConflict(ppd
, duplex
->keyword
, "DuplexTumble"))
4162 p
->type
|= CUPS_PRINTER_DUPLEX
;
4164 ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4165 "sides-supported", 3, NULL
, sides
);
4167 if (!strcasecmp(duplex
->defchoice
, "DuplexTumble"))
4168 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4169 "sides-default", NULL
, "two-sided-short-edge");
4170 else if (!strcasecmp(duplex
->defchoice
, "DuplexNoTumble"))
4171 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4172 "sides-default", NULL
, "two-sided-long-edge");
4174 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4175 "sides-default", NULL
, "one-sided");
4178 if (ppdFindOption(ppd
, "Collate") != NULL
)
4179 p
->type
|= CUPS_PRINTER_COLLATE
;
4181 if (ppdFindOption(ppd
, "StapleLocation") != NULL
)
4183 p
->type
|= CUPS_PRINTER_STAPLE
;
4184 finishings
[num_finishings
++] = IPP_FINISHINGS_STAPLE
;
4187 if (ppdFindOption(ppd
, "BindEdge") != NULL
)
4189 p
->type
|= CUPS_PRINTER_BIND
;
4190 finishings
[num_finishings
++] = IPP_FINISHINGS_BIND
;
4193 for (i
= 0; i
< ppd
->num_sizes
; i
++)
4194 if (ppd
->sizes
[i
].length
> 1728)
4195 p
->type
|= CUPS_PRINTER_LARGE
;
4196 else if (ppd
->sizes
[i
].length
> 1008)
4197 p
->type
|= CUPS_PRINTER_MEDIUM
;
4199 p
->type
|= CUPS_PRINTER_SMALL
;
4201 if ((ppd_attr
= ppdFindAttr(ppd
, "APICADriver", NULL
)) != NULL
&&
4202 ppd_attr
->value
&& !strcasecmp(ppd_attr
->value
, "true"))
4204 if ((ppd_attr
= ppdFindAttr(ppd
, "APScannerOnly", NULL
)) != NULL
&&
4205 ppd_attr
->value
&& !strcasecmp(ppd_attr
->value
, "true"))
4206 p
->type
|= CUPS_PRINTER_SCANNER
;
4208 p
->type
|= CUPS_PRINTER_MFP
;
4212 * Add a filter from application/vnd.cups-raw to printer/name to
4213 * handle "raw" printing by users.
4216 add_string_array(&(p
->filters
), "application/vnd.cups-raw 0 -");
4219 * Add any pre-filters in the PPD file...
4222 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsPreFilter", NULL
)) != NULL
)
4224 for (; ppd_attr
; ppd_attr
= ppdFindNextAttr(ppd
, "cupsPreFilter", NULL
))
4225 if (ppd_attr
->value
)
4226 add_string_array(&(p
->pre_filters
), ppd_attr
->value
);
4230 * Add any filters in the PPD file...
4233 DEBUG_printf(("ppd->num_filters = %d\n", ppd
->num_filters
));
4234 for (i
= 0; i
< ppd
->num_filters
; i
++)
4236 DEBUG_printf(("ppd->filters[%d] = \"%s\"\n", i
, ppd
->filters
[i
]));
4237 add_string_array(&(p
->filters
), ppd
->filters
[i
]);
4239 if (!strncasecmp(ppd
->filters
[i
], "application/vnd.cups-command", 28) &&
4240 isspace(ppd
->filters
[i
][28] & 255))
4241 p
->type
|= CUPS_PRINTER_COMMANDS
;
4244 if (ppd
->num_filters
== 0)
4247 * If there are no filters, add PostScript printing filters.
4250 add_string_array(&(p
->filters
),
4251 "application/vnd.cups-command 0 commandtops");
4252 add_string_array(&(p
->filters
),
4253 "application/vnd.cups-postscript 0 -");
4255 p
->type
|= CUPS_PRINTER_COMMANDS
;
4257 else if (!(p
->type
& CUPS_PRINTER_COMMANDS
))
4260 * See if this is a PostScript device without a command filter...
4263 for (i
= 0; i
< ppd
->num_filters
; i
++)
4264 if (!strncasecmp(ppd
->filters
[i
],
4265 "application/vnd.cups-postscript", 31) &&
4266 isspace(ppd
->filters
[i
][31] & 255))
4269 if (i
< ppd
->num_filters
)
4272 * Add the generic PostScript command filter...
4275 add_string_array(&(p
->filters
),
4276 "application/vnd.cups-command 0 commandtops");
4277 p
->type
|= CUPS_PRINTER_COMMANDS
;
4281 if (p
->type
& CUPS_PRINTER_COMMANDS
)
4283 char *commands
, /* Copy of commands */
4284 *start
, /* Start of name */
4285 *end
; /* End of name */
4286 int count
; /* Number of commands */
4289 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsCommands", NULL
)) != NULL
&&
4290 ppd_attr
->value
&& ppd_attr
->value
[0])
4292 for (count
= 0, start
= ppd_attr
->value
; *start
; count
++)
4294 while (isspace(*start
& 255))
4300 while (*start
&& !isspace(*start
& 255))
4310 * Make a copy of the commands string and count how many ...
4313 attr
= ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4314 "printer-commands", count
, NULL
, NULL
);
4316 commands
= strdup(ppd_attr
->value
);
4318 for (count
= 0, start
= commands
; *start
; count
++)
4320 while (isspace(*start
& 255))
4327 while (*end
&& !isspace(*end
& 255))
4333 attr
->values
[count
].string
.text
= _cupsStrAlloc(start
);
4343 * Add the standard list of commands...
4346 ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4348 (int)(sizeof(standard_commands
) /
4349 sizeof(standard_commands
[0])), NULL
,
4356 * No commands supported...
4359 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4360 "printer-commands", NULL
, "none");
4364 * Show current and available port monitors for this printer...
4367 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "port-monitor",
4368 NULL
, p
->port_monitor
? p
->port_monitor
: "none");
4370 for (i
= 1, ppd_attr
= ppdFindAttr(ppd
, "cupsPortMonitor", NULL
);
4372 i
++, ppd_attr
= ppdFindNextAttr(ppd
, "cupsPortMonitor", NULL
));
4376 if (strstr(ppd
->protocols
, "TBCP"))
4378 else if (strstr(ppd
->protocols
, "BCP"))
4382 attr
= ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
4383 "port-monitor-supported", i
, NULL
, NULL
);
4385 attr
->values
[0].string
.text
= _cupsStrAlloc("none");
4387 for (i
= 1, ppd_attr
= ppdFindAttr(ppd
, "cupsPortMonitor", NULL
);
4389 i
++, ppd_attr
= ppdFindNextAttr(ppd
, "cupsPortMonitor", NULL
))
4390 attr
->values
[i
].string
.text
= _cupsStrAlloc(ppd_attr
->value
);
4394 if (strstr(ppd
->protocols
, "TBCP"))
4395 attr
->values
[i
].string
.text
= _cupsStrAlloc("tbcp");
4396 else if (strstr(ppd
->protocols
, "BCP"))
4397 attr
->values
[i
].string
.text
= _cupsStrAlloc("bcp");
4401 cupsdSetString(&p
->product
, ppd
->product
);
4402 #endif /* HAVE_DNSSD */
4404 if (ppdFindAttr(ppd
, "APRemoteQueueID", NULL
))
4405 p
->type
|= CUPS_PRINTER_REMOTE
;
4408 * Close the PPD and set the type...
4413 else if (!access(ppd_name
, 0))
4415 int pline
; /* PPD line number */
4416 ppd_status_t pstatus
; /* PPD load status */
4419 pstatus
= ppdLastError(&pline
);
4421 cupsdLogMessage(CUPSD_LOG_ERROR
, "PPD file for %s cannot be loaded!",
4424 if (pstatus
<= PPD_ALLOC_ERROR
)
4425 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s", strerror(errno
));
4427 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s on line %d.",
4428 ppdErrorString(pstatus
), pline
);
4430 cupsdLogMessage(CUPSD_LOG_INFO
,
4431 "Hint: Run \"cupstestppd %s\" and fix any errors.",
4435 * Add a filter from application/vnd.cups-raw to printer/name to
4436 * handle "raw" printing by users.
4439 add_string_array(&(p
->filters
), "application/vnd.cups-raw 0 -");
4442 * Add a PostScript filter, since this is still possibly PS printer.
4445 add_string_array(&(p
->filters
), "application/vnd.cups-postscript 0 -");
4450 * If we have an interface script, add a filter entry for it...
4453 char interface
[1024]; /* Interface script */
4456 snprintf(interface
, sizeof(interface
), "%s/interfaces/%s", ServerRoot
,
4458 if (!access(interface
, X_OK
))
4461 * Yes, we have a System V style interface script; use it!
4464 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4465 "printer-make-and-model", NULL
,
4466 "Local System V Printer");
4468 snprintf(interface
, sizeof(interface
), "*/* 0 %s/interfaces/%s",
4469 ServerRoot
, p
->name
);
4470 add_string_array(&(p
->filters
), interface
);
4472 else if (!strncmp(p
->device_uri
, "ipp://", 6) &&
4473 (strstr(p
->device_uri
, "/printers/") != NULL
||
4474 strstr(p
->device_uri
, "/classes/") != NULL
||
4475 (strstr(p
->device_uri
, "._ipp.") != NULL
&&
4476 !strcmp(p
->device_uri
+ strlen(p
->device_uri
) - 5,
4480 * Tell the client this is really a hard-wired remote printer.
4483 p
->type
|= CUPS_PRINTER_REMOTE
;
4486 * Point the printer-uri-supported attribute to the
4490 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
4491 "printer-uri-supported", NULL
, p
->device_uri
);
4494 * Then set the make-and-model accordingly...
4497 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4498 "printer-make-and-model", NULL
, "Remote Printer");
4501 * Print all files directly...
4510 * Otherwise we have neither - treat this as a "dumb" printer
4511 * with no PPD file...
4514 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4515 "printer-make-and-model", NULL
, "Local Raw Printer");
4521 ippAddIntegers(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
4522 "finishings-supported", num_finishings
, finishings
);
4523 ippAddInteger(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
4524 "finishings-default", IPP_FINISHINGS_NONE
);
4526 if (ppd
&& (cache
= cupsFileOpen(cache_name
, "w")) != NULL
)
4529 * Save cached PPD attributes to disk...
4532 cupsdLogMessage(CUPSD_LOG_DEBUG
, "load_ppd: Saving %s...", cache_name
);
4534 p
->ppd_attrs
->state
= IPP_IDLE
;
4536 if (ippWriteIO(cache
, (ipp_iocb_t
)cupsFileWrite
, 1, NULL
,
4537 p
->ppd_attrs
) != IPP_DATA
)
4539 cupsdLogMessage(CUPSD_LOG_ERROR
,
4540 "Unable to save PPD cache file \"%s\" - %s", cache_name
,
4545 cupsFileClose(cache
);
4547 else if (cache_info
.st_mtime
)
4550 * Remove cache file...
4560 * 'write_irix_config()' - Update the config files used by the IRIX
4565 write_irix_config(cupsd_printer_t
*p
) /* I - Printer to update */
4567 char filename
[1024]; /* Interface script filename */
4568 cups_file_t
*fp
; /* Interface script file */
4569 ipp_attribute_t
*attr
; /* Attribute data */
4573 * Add dummy interface and GUI scripts to fool SGI's "challenged" printing
4574 * tools. First the interface script that tells the tools what kind of
4575 * printer we have...
4578 snprintf(filename
, sizeof(filename
), "/var/spool/lp/interface/%s", p
->name
);
4580 if (p
->type
& CUPS_PRINTER_CLASS
)
4582 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4584 cupsFilePuts(fp
, "#!/bin/sh\n");
4586 if ((attr
= ippFindAttribute(p
->attrs
, "printer-make-and-model",
4587 IPP_TAG_TEXT
)) != NULL
)
4588 cupsFilePrintf(fp
, "NAME=\"%s\"\n", attr
->values
[0].string
.text
);
4589 else if (p
->type
& CUPS_PRINTER_CLASS
)
4590 cupsFilePuts(fp
, "NAME=\"Printer Class\"\n");
4592 cupsFilePuts(fp
, "NAME=\"Remote Destination\"\n");
4594 if (p
->type
& CUPS_PRINTER_COLOR
)
4595 cupsFilePuts(fp
, "TYPE=ColorPostScript\n");
4597 cupsFilePuts(fp
, "TYPE=MonoPostScript\n");
4599 cupsFilePrintf(fp
, "HOSTNAME=%s\n", ServerName
);
4600 cupsFilePrintf(fp
, "HOSTPRINTER=%s\n", p
->name
);
4604 chmod(filename
, 0755);
4605 chown(filename
, User
, Group
);
4609 * Then the member file that tells which device file the queue is connected
4610 * to... Networked printers use "/dev/null" in this file, so that's what
4611 * we use (the actual device URI can confuse some apps...)
4614 snprintf(filename
, sizeof(filename
), "/var/spool/lp/member/%s", p
->name
);
4616 if (p
->type
& CUPS_PRINTER_CLASS
)
4618 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4620 cupsFilePuts(fp
, "/dev/null\n");
4624 chmod(filename
, 0644);
4625 chown(filename
, User
, Group
);
4629 * The gui_interface file is a script or program that launches a GUI
4630 * option panel for the printer, using options specified on the
4631 * command-line in the third argument. The option panel must send
4632 * any printing options to stdout on a single line when the user
4633 * accepts them, or nothing if the user cancels the dialog.
4635 * The default options panel program is /usr/bin/glpoptions, from
4636 * the ESP Print Pro software. You can select another using the
4637 * PrintcapGUI option.
4640 snprintf(filename
, sizeof(filename
), "/var/spool/lp/gui_interface/ELF/%s.gui", p
->name
);
4642 if (p
->type
& CUPS_PRINTER_CLASS
)
4644 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4646 cupsFilePuts(fp
, "#!/bin/sh\n");
4647 cupsFilePrintf(fp
, "%s -d %s -o \"$3\"\n", PrintcapGUI
, p
->name
);
4651 chmod(filename
, 0755);
4652 chown(filename
, User
, Group
);
4656 * The POD config file is needed by the printstatus command to show
4657 * the printer location and device.
4660 snprintf(filename
, sizeof(filename
), "/var/spool/lp/pod/%s.config", p
->name
);
4662 if (p
->type
& CUPS_PRINTER_CLASS
)
4664 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4666 cupsFilePrintf(fp
, "Printer Class | %s\n",
4667 (p
->type
& CUPS_PRINTER_COLOR
) ? "ColorPostScript" : "MonoPostScript");
4668 cupsFilePrintf(fp
, "Printer Model | %s\n", p
->make_model
? p
->make_model
: "");
4669 cupsFilePrintf(fp
, "Location Code | %s\n", p
->location
? p
->location
: "");
4670 cupsFilePrintf(fp
, "Physical Location | %s\n", p
->info
? p
->info
: "");
4671 cupsFilePrintf(fp
, "Port Path | %s\n", p
->device_uri
);
4672 cupsFilePrintf(fp
, "Config Path | /var/spool/lp/pod/%s.config\n", p
->name
);
4673 cupsFilePrintf(fp
, "Active Status Path | /var/spool/lp/pod/%s.status\n", p
->name
);
4674 cupsFilePuts(fp
, "Status Update Wait | 10 seconds\n");
4678 chmod(filename
, 0664);
4679 chown(filename
, User
, Group
);
4685 * 'write_irix_state()' - Update the status files used by IRIX printing
4690 write_irix_state(cupsd_printer_t
*p
) /* I - Printer to update */
4692 char filename
[1024]; /* Interface script filename */
4693 cups_file_t
*fp
; /* Interface script file */
4694 int tag
; /* Status tag value */
4700 * The POD status file is needed for the printstatus window to
4701 * provide the current status of the printer.
4704 snprintf(filename
, sizeof(filename
), "/var/spool/lp/pod/%s.status", p
->name
);
4706 if (p
->type
& CUPS_PRINTER_CLASS
)
4708 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4710 cupsFilePrintf(fp
, "Operational Status | %s\n",
4711 (p
->state
== IPP_PRINTER_IDLE
) ? "Idle" :
4712 (p
->state
== IPP_PRINTER_PROCESSING
) ? "Busy" :
4714 cupsFilePrintf(fp
, "Information | 01 00 00 | %s\n", CUPS_SVERSION
);
4715 cupsFilePrintf(fp
, "Information | 02 00 00 | Device URI: %s\n",
4717 cupsFilePrintf(fp
, "Information | 03 00 00 | %s jobs\n",
4718 p
->accepting
? "Accepting" : "Not accepting");
4719 cupsFilePrintf(fp
, "Information | 04 00 00 | %s\n", p
->state_message
);
4723 chmod(filename
, 0664);
4724 chown(filename
, User
, Group
);
4728 * The activeicons file is needed to provide desktop icons for printers:
4730 * [ quoted from /usr/lib/print/tagit ]
4732 * --- Type of printer tags (base values)
4734 * Dumb=66048 # 0x10200
4735 * DumbColor=66080 # 0x10220
4736 * Raster=66112 # 0x10240
4737 * ColorRaster=66144 # 0x10260
4738 * Plotter=66176 # 0x10280
4739 * PostScript=66208 # 0x102A0
4740 * ColorPostScript=66240 # 0x102C0
4741 * MonoPostScript=66272 # 0x102E0
4743 * --- Printer state modifiers for local printers
4748 * Unknown=3 # 0x3 (Faulted due to unknown reason)
4750 * --- Printer state modifiers for network printers
4754 * NetFaulted=10 # 0xA
4755 * NetUnknown=11 # 0xB (Faulted due to unknown reason)
4758 snprintf(filename
, sizeof(filename
), "/var/spool/lp/activeicons/%s", p
->name
);
4760 if (p
->type
& CUPS_PRINTER_CLASS
)
4762 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4764 if (p
->type
& CUPS_PRINTER_COLOR
)
4769 if (p
->type
& CUPS_PRINTER_REMOTE
)
4772 if (p
->state
== IPP_PRINTER_PROCESSING
)
4775 else if (p
->state
== IPP_PRINTER_STOPPED
)
4778 cupsFilePuts(fp
, "#!/bin/sh\n");
4779 cupsFilePrintf(fp
, "#Tag %d\n", tag
);
4783 chmod(filename
, 0755);
4784 chown(filename
, User
, Group
);
4789 * The default file is needed by the printers window to show
4790 * the default printer.
4793 snprintf(filename
, sizeof(filename
), "/var/spool/lp/default");
4795 if (DefaultPrinter
!= NULL
)
4797 if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4799 cupsFilePrintf(fp
, "%s\n", DefaultPrinter
->name
);
4803 chmod(filename
, 0644);
4804 chown(filename
, User
, Group
);
4814 * 'write_xml_string()' - Write a string with XML escaping.
4818 write_xml_string(cups_file_t
*fp
, /* I - File to write to */
4819 const char *s
) /* I - String to write */
4821 const char *start
; /* Start of current sequence */
4827 for (start
= s
; *s
; s
++)
4832 cupsFileWrite(fp
, start
, s
- start
);
4834 cupsFilePuts(fp
, "&");
4840 cupsFileWrite(fp
, start
, s
- start
);
4842 cupsFilePuts(fp
, "<");
4848 cupsFilePuts(fp
, start
);
4853 * End of "$Id: printers.c 7968 2008-09-19 23:03:01Z mike $".