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
);
1523 * Write each local printer known to the system...
1526 for (printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1528 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1531 * Skip remote destinations and printer classes...
1534 if ((printer
->type
& CUPS_PRINTER_DISCOVERED
) ||
1535 (printer
->type
& CUPS_PRINTER_CLASS
) ||
1536 (printer
->type
& CUPS_PRINTER_IMPLICIT
))
1540 * Write printers as needed...
1543 if (printer
== DefaultPrinter
)
1544 cupsFilePrintf(fp
, "<DefaultPrinter %s>\n", printer
->name
);
1546 cupsFilePrintf(fp
, "<Printer %s>\n", printer
->name
);
1548 if (printer
->num_auth_info_required
> 0)
1550 switch (printer
->num_auth_info_required
)
1553 strlcpy(value
, printer
->auth_info_required
[0], sizeof(value
));
1557 snprintf(value
, sizeof(value
), "%s,%s",
1558 printer
->auth_info_required
[0],
1559 printer
->auth_info_required
[1]);
1564 snprintf(value
, sizeof(value
), "%s,%s,%s",
1565 printer
->auth_info_required
[0],
1566 printer
->auth_info_required
[1],
1567 printer
->auth_info_required
[2]);
1571 cupsFilePutConf(fp
, "AuthInfoRequired", value
);
1575 cupsFilePutConf(fp
, "Info", printer
->info
);
1577 if (printer
->location
)
1578 cupsFilePutConf(fp
, "Location", printer
->location
);
1580 if (printer
->make_model
)
1581 cupsFilePutConf(fp
, "MakeModel", printer
->make_model
);
1583 cupsFilePutConf(fp
, "DeviceURI", printer
->device_uri
);
1585 if (printer
->port_monitor
)
1586 cupsFilePutConf(fp
, "PortMonitor", printer
->port_monitor
);
1588 if (printer
->state
== IPP_PRINTER_STOPPED
)
1590 cupsFilePuts(fp
, "State Stopped\n");
1592 if (printer
->state_message
)
1593 cupsFilePutConf(fp
, "StateMessage", printer
->state_message
);
1596 cupsFilePuts(fp
, "State Idle\n");
1598 cupsFilePrintf(fp
, "StateTime %d\n", (int)printer
->state_time
);
1600 for (i
= 0; i
< printer
->num_reasons
; i
++)
1601 if (strcmp(printer
->reasons
[i
], "com.apple.print.recoverable-warning") &&
1602 strcmp(printer
->reasons
[i
], "connecting-to-device") &&
1603 strcmp(printer
->reasons
[i
], "cups-insecure-filter-warning") &&
1604 strcmp(printer
->reasons
[i
], "cups-missing-filter-warning"))
1605 cupsFilePutConf(fp
, "Reason", printer
->reasons
[i
]);
1607 cupsFilePrintf(fp
, "Type %d\n", printer
->type
);
1610 if (printer
->product
)
1611 cupsFilePutConf(fp
, "Product", printer
->product
);
1612 #endif /* HAVE_DNSSD */
1614 for (ptr
= (char *)cupsArrayFirst(printer
->filters
);
1616 ptr
= (char *)cupsArrayNext(printer
->filters
))
1617 cupsFilePutConf(fp
, "Filter", ptr
);
1619 for (ptr
= (char *)cupsArrayFirst(printer
->pre_filters
);
1621 ptr
= (char *)cupsArrayNext(printer
->pre_filters
))
1622 cupsFilePutConf(fp
, "PreFilter", ptr
);
1624 if (printer
->accepting
)
1625 cupsFilePuts(fp
, "Accepting Yes\n");
1627 cupsFilePuts(fp
, "Accepting No\n");
1629 if (printer
->shared
)
1630 cupsFilePuts(fp
, "Shared Yes\n");
1632 cupsFilePuts(fp
, "Shared No\n");
1634 snprintf(value
, sizeof(value
), "%s %s", printer
->job_sheets
[0],
1635 printer
->job_sheets
[1]);
1636 cupsFilePutConf(fp
, "JobSheets", value
);
1638 cupsFilePrintf(fp
, "QuotaPeriod %d\n", printer
->quota_period
);
1639 cupsFilePrintf(fp
, "PageLimit %d\n", printer
->page_limit
);
1640 cupsFilePrintf(fp
, "KLimit %d\n", printer
->k_limit
);
1642 for (i
= 0; i
< printer
->num_users
; i
++)
1643 cupsFilePutConf(fp
, printer
->deny_users
? "DenyUser" : "AllowUser",
1646 if (printer
->op_policy
)
1647 cupsFilePutConf(fp
, "OpPolicy", printer
->op_policy
);
1648 if (printer
->error_policy
)
1649 cupsFilePutConf(fp
, "ErrorPolicy", printer
->error_policy
);
1651 for (i
= printer
->num_options
, option
= printer
->options
;
1655 snprintf(value
, sizeof(value
), "%s %s", option
->name
, option
->value
);
1656 cupsFilePutConf(fp
, "Option", value
);
1659 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-colors",
1660 IPP_TAG_NAME
)) != NULL
)
1662 snprintf(value
, sizeof(value
), "%s ", marker
->name
);
1664 for (i
= 0, ptr
= value
+ strlen(value
);
1665 i
< marker
->num_values
&& ptr
< (value
+ sizeof(value
) - 1);
1671 strlcpy(ptr
, marker
->values
[i
].string
.text
,
1672 value
+ sizeof(value
) - ptr
);
1677 cupsFilePutConf(fp
, "Attribute", value
);
1680 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-levels",
1681 IPP_TAG_INTEGER
)) != NULL
)
1683 cupsFilePrintf(fp
, "Attribute %s %d", marker
->name
,
1684 marker
->values
[0].integer
);
1685 for (i
= 1; i
< marker
->num_values
; i
++)
1686 cupsFilePrintf(fp
, ",%d", marker
->values
[i
].integer
);
1687 cupsFilePuts(fp
, "\n");
1690 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-low-levels",
1691 IPP_TAG_INTEGER
)) != NULL
)
1693 cupsFilePrintf(fp
, "Attribute %s %d", marker
->name
,
1694 marker
->values
[0].integer
);
1695 for (i
= 1; i
< marker
->num_values
; i
++)
1696 cupsFilePrintf(fp
, ",%d", marker
->values
[i
].integer
);
1697 cupsFilePuts(fp
, "\n");
1700 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-high-levels",
1701 IPP_TAG_INTEGER
)) != NULL
)
1703 cupsFilePrintf(fp
, "Attribute %s %d", marker
->name
,
1704 marker
->values
[0].integer
);
1705 for (i
= 1; i
< marker
->num_values
; i
++)
1706 cupsFilePrintf(fp
, ",%d", marker
->values
[i
].integer
);
1707 cupsFilePuts(fp
, "\n");
1710 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-message",
1711 IPP_TAG_TEXT
)) != NULL
)
1713 snprintf(value
, sizeof(value
), "%s %s", marker
->name
,
1714 marker
->values
[0].string
.text
);
1716 cupsFilePutConf(fp
, "Attribute", value
);
1719 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-names",
1720 IPP_TAG_NAME
)) != NULL
)
1722 snprintf(value
, sizeof(value
), "%s ", marker
->name
);
1724 for (i
= 0, ptr
= value
+ strlen(value
);
1725 i
< marker
->num_values
&& ptr
< (value
+ sizeof(value
) - 1);
1731 strlcpy(ptr
, marker
->values
[i
].string
.text
,
1732 value
+ sizeof(value
) - ptr
);
1737 cupsFilePutConf(fp
, "Attribute", value
);
1740 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-types",
1741 IPP_TAG_KEYWORD
)) != NULL
)
1743 snprintf(value
, sizeof(value
), "%s ", marker
->name
);
1745 for (i
= 0, ptr
= value
+ strlen(value
);
1746 i
< marker
->num_values
&& ptr
< (value
+ sizeof(value
) - 1);
1752 strlcpy(ptr
, marker
->values
[i
].string
.text
,
1753 value
+ sizeof(value
) - ptr
);
1758 cupsFilePutConf(fp
, "Attribute", value
);
1761 if (printer
->marker_time
)
1762 cupsFilePrintf(fp
, "Attribute marker-change-time %ld\n",
1763 (long)printer
->marker_time
);
1765 cupsFilePuts(fp
, "</Printer>\n");
1769 * Make IRIX desktop & printer status happy
1772 write_irix_state(printer
);
1781 * 'cupsdSetAuthInfoRequired()' - Set the required authentication info.
1784 int /* O - 1 if value OK, 0 otherwise */
1785 cupsdSetAuthInfoRequired(
1786 cupsd_printer_t
*p
, /* I - Printer */
1787 const char *values
, /* I - Plain text value (or NULL) */
1788 ipp_attribute_t
*attr
) /* I - IPP attribute value (or NULL) */
1790 int i
; /* Looping var */
1793 p
->num_auth_info_required
= 0;
1796 * Do we have a plain text value?
1802 * Yes, grab the keywords...
1805 const char *end
; /* End of current value */
1808 while (*values
&& p
->num_auth_info_required
< 4)
1810 if ((end
= strchr(values
, ',')) == NULL
)
1811 end
= values
+ strlen(values
);
1813 if ((end
- values
) == 4 && !strncmp(values
, "none", 4))
1815 if (p
->num_auth_info_required
!= 0 || *end
)
1818 p
->auth_info_required
[p
->num_auth_info_required
] = "none";
1819 p
->num_auth_info_required
++;
1823 else if ((end
- values
) == 9 && !strncmp(values
, "negotiate", 9))
1825 if (p
->num_auth_info_required
!= 0 || *end
)
1828 p
->auth_info_required
[p
->num_auth_info_required
] = "negotiate";
1829 p
->num_auth_info_required
++;
1831 else if ((end
- values
) == 6 && !strncmp(values
, "domain", 6))
1833 p
->auth_info_required
[p
->num_auth_info_required
] = "domain";
1834 p
->num_auth_info_required
++;
1836 else if ((end
- values
) == 8 && !strncmp(values
, "password", 8))
1838 p
->auth_info_required
[p
->num_auth_info_required
] = "password";
1839 p
->num_auth_info_required
++;
1841 else if ((end
- values
) == 8 && !strncmp(values
, "username", 8))
1843 p
->auth_info_required
[p
->num_auth_info_required
] = "username";
1844 p
->num_auth_info_required
++;
1849 values
= (*end
) ? end
+ 1 : end
;
1852 if (p
->num_auth_info_required
== 0)
1854 p
->auth_info_required
[0] = "none";
1855 p
->num_auth_info_required
= 1;
1859 * Update the printer-type value as needed...
1862 if (p
->num_auth_info_required
> 1 ||
1863 strcmp(p
->auth_info_required
[0], "none"))
1864 p
->type
|= CUPS_PRINTER_AUTHENTICATED
;
1866 p
->type
&= ~CUPS_PRINTER_AUTHENTICATED
;
1872 * Grab values from an attribute instead...
1875 if (!attr
|| attr
->num_values
> 4)
1879 * Update the printer-type value as needed...
1882 if (attr
->num_values
> 1 ||
1883 strcmp(attr
->values
[0].string
.text
, "none"))
1884 p
->type
|= CUPS_PRINTER_AUTHENTICATED
;
1886 p
->type
&= ~CUPS_PRINTER_AUTHENTICATED
;
1888 for (i
= 0; i
< attr
->num_values
; i
++)
1890 if (!strcmp(attr
->values
[i
].string
.text
, "none"))
1892 if (p
->num_auth_info_required
!= 0 || attr
->num_values
!= 1)
1895 p
->auth_info_required
[p
->num_auth_info_required
] = "none";
1896 p
->num_auth_info_required
++;
1900 else if (!strcmp(attr
->values
[i
].string
.text
, "negotiate"))
1902 if (p
->num_auth_info_required
!= 0 || attr
->num_values
!= 1)
1905 p
->auth_info_required
[p
->num_auth_info_required
] = "negotiate";
1906 p
->num_auth_info_required
++;
1910 else if (!strcmp(attr
->values
[i
].string
.text
, "domain"))
1912 p
->auth_info_required
[p
->num_auth_info_required
] = "domain";
1913 p
->num_auth_info_required
++;
1915 else if (!strcmp(attr
->values
[i
].string
.text
, "password"))
1917 p
->auth_info_required
[p
->num_auth_info_required
] = "password";
1918 p
->num_auth_info_required
++;
1920 else if (!strcmp(attr
->values
[i
].string
.text
, "username"))
1922 p
->auth_info_required
[p
->num_auth_info_required
] = "username";
1923 p
->num_auth_info_required
++;
1934 * 'cupsdSetDeviceURI()' - Set the device URI for a printer.
1938 cupsdSetDeviceURI(cupsd_printer_t
*p
, /* I - Printer */
1939 const char *uri
) /* I - Device URI */
1941 char buffer
[1024], /* URI buffer */
1942 *start
, /* Start of data after scheme */
1943 *slash
, /* First slash after scheme:// */
1944 *ptr
; /* Pointer into user@host:port part */
1948 * Set the full device URI..
1951 cupsdSetString(&(p
->device_uri
), uri
);
1954 * Copy the device URI to a temporary buffer so we can sanitize any auth
1958 strlcpy(buffer
, uri
, sizeof(buffer
));
1961 * Find the end of the scheme:// part...
1964 if ((ptr
= strchr(buffer
, ':')) != NULL
)
1966 for (start
= ptr
+ 1; *start
; start
++)
1971 * Find the next slash (/) in the URI...
1974 if ((slash
= strchr(start
, '/')) == NULL
)
1975 slash
= start
+ strlen(start
); /* No slash, point to the end */
1978 * Check for an @ sign before the slash...
1981 if ((ptr
= strchr(start
, '@')) != NULL
&& ptr
< slash
)
1984 * Found an @ sign and it is before the resource part, so we have
1985 * an authentication string. Copy the remaining URI over the
1986 * authentication string...
1989 _cups_strcpy(start
, ptr
+ 1);
1994 * Save the sanitized URI...
1997 cupsdSetString(&(p
->sanitized_device_uri
), buffer
);
2002 * 'cupsdSetPrinterAttr()' - Set a printer attribute.
2006 cupsdSetPrinterAttr(
2007 cupsd_printer_t
*p
, /* I - Printer */
2008 const char *name
, /* I - Attribute name */
2009 char *value
) /* I - Attribute value string */
2011 ipp_attribute_t
*attr
; /* Attribute */
2012 int i
, /* Looping var */
2013 count
; /* Number of values */
2014 char *ptr
; /* Pointer into value */
2015 ipp_tag_t value_tag
; /* Value tag for this attribute */
2019 * Don't allow empty values...
2022 if (!*value
&& strcmp(name
, "marker-message"))
2024 cupsdLogMessage(CUPSD_LOG_ERROR
, "Ignoring empty \"%s\" attribute", name
);
2029 * Count the number of values...
2032 for (count
= 1, ptr
= value
;
2033 (ptr
= strchr(ptr
, ',')) != NULL
;
2037 * Then add or update the attribute as needed...
2040 if (!strcmp(name
, "marker-levels") || !strcmp(name
, "marker-low-levels") ||
2041 !strcmp(name
, "marker-high-levels"))
2047 if ((attr
= ippFindAttribute(p
->attrs
, name
, IPP_TAG_INTEGER
)) != NULL
&&
2048 attr
->num_values
< count
)
2050 ippDeleteAttribute(p
->attrs
, attr
);
2055 attr
->num_values
= count
;
2057 attr
= ippAddIntegers(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, name
,
2062 cupsdLogMessage(CUPSD_LOG_ERROR
,
2063 "Unable to allocate memory for printer attribute "
2064 "(%d values)", count
);
2068 for (i
= 0; i
< count
; i
++)
2070 if ((ptr
= strchr(value
, ',')) != NULL
)
2073 attr
->values
[i
].integer
= strtol(value
, NULL
, 10);
2082 * Name or keyword values...
2085 if (!strcmp(name
, "marker-types"))
2086 value_tag
= IPP_TAG_KEYWORD
;
2087 else if (!strcmp(name
, "marker-message"))
2088 value_tag
= IPP_TAG_TEXT
;
2090 value_tag
= IPP_TAG_NAME
;
2092 if ((attr
= ippFindAttribute(p
->attrs
, name
, value_tag
)) != NULL
&&
2093 attr
->num_values
< count
)
2095 ippDeleteAttribute(p
->attrs
, attr
);
2101 for (i
= 0; i
< attr
->num_values
; i
++)
2102 _cupsStrFree(attr
->values
[i
].string
.text
);
2104 attr
->num_values
= count
;
2107 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, value_tag
, name
,
2112 cupsdLogMessage(CUPSD_LOG_ERROR
,
2113 "Unable to allocate memory for printer attribute "
2114 "(%d values)", count
);
2118 for (i
= 0; i
< count
; i
++)
2120 if ((ptr
= strchr(value
, ',')) != NULL
)
2123 attr
->values
[i
].string
.text
= _cupsStrAlloc(value
);
2133 * 'cupsdSetPrinterAttrs()' - Set printer attributes based upon the PPD file.
2137 cupsdSetPrinterAttrs(cupsd_printer_t
*p
)/* I - Printer to setup */
2139 int i
, /* Looping var */
2140 length
; /* Length of browse attributes */
2141 char resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2142 int num_air
; /* Number of auth-info-required values */
2143 const char * const *air
; /* auth-info-required values */
2144 cupsd_location_t
*auth
; /* Pointer to authentication element */
2145 const char *auth_supported
; /* Authentication supported */
2146 ipp_t
*oldattrs
; /* Old printer attributes */
2147 ipp_attribute_t
*attr
; /* Attribute data */
2148 cups_option_t
*option
; /* Current printer option */
2149 char *filter
; /* Current filter */
2150 static const char * const air_none
[] =
2151 { /* No authentication */
2154 static const char * const air_userpass
[] =
2155 { /* Basic/Digest authentication */
2161 DEBUG_printf(("cupsdSetPrinterAttrs: entering name = %s, type = %x\n", p
->name
,
2165 * Make sure that we have the common attributes defined...
2169 cupsdCreateCommonData();
2172 * Clear out old filters, if any...
2175 delete_printer_filters(p
);
2178 * Figure out the authentication that is required for the printer.
2181 auth_supported
= "requesting-user-name";
2185 if (p
->num_auth_info_required
> 0 && strcmp(p
->auth_info_required
[0], "none"))
2187 num_air
= p
->num_auth_info_required
;
2188 air
= p
->auth_info_required
;
2190 else if ((p
->type
& CUPS_PRINTER_AUTHENTICATED
) &&
2191 (p
->type
& CUPS_PRINTER_DISCOVERED
))
2197 if (p
->type
& CUPS_PRINTER_CLASS
)
2198 snprintf(resource
, sizeof(resource
), "/classes/%s", p
->name
);
2200 snprintf(resource
, sizeof(resource
), "/printers/%s", p
->name
);
2202 if ((auth
= cupsdFindBest(resource
, HTTP_POST
)) == NULL
||
2203 auth
->type
== CUPSD_AUTH_NONE
)
2204 auth
= cupsdFindPolicyOp(p
->op_policy_ptr
, IPP_PRINT_JOB
);
2208 int auth_type
; /* Authentication type */
2211 if ((auth_type
= auth
->type
) == CUPSD_AUTH_DEFAULT
)
2212 auth_type
= DefaultAuthType
;
2214 if (auth_type
== CUPSD_AUTH_BASIC
|| auth_type
== CUPSD_AUTH_BASICDIGEST
)
2215 auth_supported
= "basic";
2216 else if (auth_type
== CUPSD_AUTH_DIGEST
)
2217 auth_supported
= "digest";
2219 else if (auth_type
== CUPSD_AUTH_NEGOTIATE
)
2220 auth_supported
= "negotiate";
2221 #endif /* HAVE_GSSAPI */
2223 if (!(p
->type
& CUPS_PRINTER_DISCOVERED
))
2225 if (auth_type
!= CUPSD_AUTH_NONE
)
2226 p
->type
|= CUPS_PRINTER_AUTHENTICATED
;
2228 p
->type
&= ~CUPS_PRINTER_AUTHENTICATED
;
2231 else if (!(p
->type
& CUPS_PRINTER_DISCOVERED
))
2232 p
->type
&= ~CUPS_PRINTER_AUTHENTICATED
;
2235 * Create the required IPP attributes for a printer...
2238 oldattrs
= p
->attrs
;
2239 p
->attrs
= ippNew();
2241 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2242 "uri-authentication-supported", NULL
, auth_supported
);
2243 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2244 "uri-security-supported", NULL
, "none");
2245 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "printer-name", NULL
,
2247 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-location",
2248 NULL
, p
->location
? p
->location
: "");
2249 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-info",
2250 NULL
, p
->info
? p
->info
: "");
2255 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2256 "requesting-user-name-denied", p
->num_users
, NULL
,
2259 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2260 "requesting-user-name-allowed", p
->num_users
, NULL
,
2264 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2265 "job-quota-period", p
->quota_period
);
2266 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2267 "job-k-limit", p
->k_limit
);
2268 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2269 "job-page-limit", p
->page_limit
);
2270 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2271 "auth-info-required", num_air
, NULL
, air
);
2273 if (cupsArrayCount(Banners
) > 0 && !(p
->type
& CUPS_PRINTER_DISCOVERED
))
2276 * Setup the job-sheets-default attribute...
2279 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2280 "job-sheets-default", 2, NULL
, NULL
);
2284 attr
->values
[0].string
.text
= _cupsStrAlloc(Classification
?
2285 Classification
: p
->job_sheets
[0]);
2286 attr
->values
[1].string
.text
= _cupsStrAlloc(Classification
?
2287 Classification
: p
->job_sheets
[1]);
2294 if (p
->type
& CUPS_PRINTER_DISCOVERED
)
2297 * Tell the client this is a remote printer of some type...
2300 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
2301 "printer-uri-supported", NULL
, p
->uri
);
2303 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "printer-more-info",
2307 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
2308 "printer-make-and-model", NULL
, p
->make_model
);
2310 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri", NULL
,
2319 * Assign additional attributes depending on whether this is a printer
2323 if (p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
2326 p
->type
&= ~CUPS_PRINTER_OPTIONS
;
2329 * Add class-specific attributes...
2332 if ((p
->type
& CUPS_PRINTER_IMPLICIT
) && p
->num_printers
> 0 &&
2333 p
->printers
[0]->make_model
)
2334 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
2335 "printer-make-and-model", NULL
, p
->printers
[0]->make_model
);
2337 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
2338 "printer-make-and-model", NULL
, "Local Printer Class");
2340 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri", NULL
,
2341 "file:///dev/null");
2343 if (p
->num_printers
> 0)
2346 * Add a list of member names; URIs are added in copy_printer_attrs...
2349 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2350 "member-names", p
->num_printers
, NULL
, NULL
);
2351 p
->type
|= CUPS_PRINTER_OPTIONS
;
2353 for (i
= 0; i
< p
->num_printers
; i
++)
2356 attr
->values
[i
].string
.text
= _cupsStrRetain(p
->printers
[i
]->name
);
2358 p
->type
&= ~CUPS_PRINTER_OPTIONS
| p
->printers
[i
]->type
;
2365 * Add printer-specific attributes...
2368 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri", NULL
,
2369 p
->sanitized_device_uri
);
2372 * Assign additional attributes from the PPD file (if any)...
2378 * Add filters for printer...
2381 cupsdSetPrinterReasons(p
, "-cups-missing-filter-warning,"
2382 "cups-insecure-filter-warning");
2384 for (filter
= (char *)cupsArrayFirst(p
->filters
);
2386 filter
= (char *)cupsArrayNext(p
->filters
))
2387 add_printer_filter(p
, p
->filetype
, filter
);
2391 p
->prefiltertype
= mimeAddType(MimeDatabase
, "prefilter", p
->name
);
2393 for (filter
= (char *)cupsArrayFirst(p
->pre_filters
);
2395 filter
= (char *)cupsArrayNext(p
->pre_filters
))
2396 add_printer_filter(p
, p
->prefiltertype
, filter
);
2402 * Copy marker attributes as needed...
2407 ipp_attribute_t
*oldattr
; /* Old attribute */
2410 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-colors",
2411 IPP_TAG_NAME
)) != NULL
)
2413 if ((attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2414 "marker-colors", oldattr
->num_values
, NULL
,
2417 for (i
= 0; i
< oldattr
->num_values
; i
++)
2418 attr
->values
[i
].string
.text
=
2419 _cupsStrRetain(oldattr
->values
[i
].string
.text
);
2423 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-levels",
2424 IPP_TAG_INTEGER
)) != NULL
)
2426 if ((attr
= ippAddIntegers(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2427 "marker-levels", oldattr
->num_values
,
2430 for (i
= 0; i
< oldattr
->num_values
; i
++)
2431 attr
->values
[i
].integer
= oldattr
->values
[i
].integer
;
2435 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-message",
2436 IPP_TAG_TEXT
)) != NULL
)
2437 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "marker-message",
2438 NULL
, oldattr
->values
[0].string
.text
);
2440 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-low-levels",
2441 IPP_TAG_INTEGER
)) != NULL
)
2443 if ((attr
= ippAddIntegers(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2444 "marker-low-levels", oldattr
->num_values
,
2447 for (i
= 0; i
< oldattr
->num_values
; i
++)
2448 attr
->values
[i
].integer
= oldattr
->values
[i
].integer
;
2452 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-high-levels",
2453 IPP_TAG_INTEGER
)) != NULL
)
2455 if ((attr
= ippAddIntegers(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2456 "marker-high-levels", oldattr
->num_values
,
2459 for (i
= 0; i
< oldattr
->num_values
; i
++)
2460 attr
->values
[i
].integer
= oldattr
->values
[i
].integer
;
2464 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-names",
2465 IPP_TAG_NAME
)) != NULL
)
2467 if ((attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2468 "marker-names", oldattr
->num_values
, NULL
,
2471 for (i
= 0; i
< oldattr
->num_values
; i
++)
2472 attr
->values
[i
].string
.text
=
2473 _cupsStrRetain(oldattr
->values
[i
].string
.text
);
2477 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-types",
2478 IPP_TAG_KEYWORD
)) != NULL
)
2480 if ((attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2481 "marker-types", oldattr
->num_values
, NULL
,
2484 for (i
= 0; i
< oldattr
->num_values
; i
++)
2485 attr
->values
[i
].string
.text
=
2486 _cupsStrRetain(oldattr
->values
[i
].string
.text
);
2490 ippDelete(oldattrs
);
2494 * Force sharing off for remote queues...
2497 if (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
))
2502 * Copy the printer options into a browse attributes string we can re-use.
2505 const char *valptr
; /* Pointer into value */
2506 char *attrptr
; /* Pointer into attribute string */
2510 * Free the old browse attributes as needed...
2513 if (p
->browse_attrs
)
2514 free(p
->browse_attrs
);
2517 * Compute the length of all attributes + job-sheets, lease-duration,
2518 * and BrowseLocalOptions.
2521 for (length
= 1, i
= p
->num_options
, option
= p
->options
;
2525 length
+= strlen(option
->name
) + 2;
2529 for (valptr
= option
->value
; *valptr
; valptr
++)
2530 if (strchr(" \"\'\\", *valptr
))
2537 length
+= 13 + strlen(p
->job_sheets
[0]) + strlen(p
->job_sheets
[1]);
2539 if (BrowseLocalOptions
)
2540 length
+= 12 + strlen(BrowseLocalOptions
);
2543 * Allocate the new string...
2546 if ((p
->browse_attrs
= calloc(1, length
)) == NULL
)
2547 cupsdLogMessage(CUPSD_LOG_ERROR
,
2548 "Unable to allocate %d bytes for browse data!",
2553 * Got the allocated string, now copy the options and attributes over...
2556 sprintf(p
->browse_attrs
, "job-sheets=%s,%s lease-duration=%d",
2557 p
->job_sheets
[0], p
->job_sheets
[1], BrowseTimeout
);
2558 attrptr
= p
->browse_attrs
+ strlen(p
->browse_attrs
);
2560 if (BrowseLocalOptions
)
2562 sprintf(attrptr
, " ipp-options=%s", BrowseLocalOptions
);
2563 attrptr
+= strlen(attrptr
);
2566 for (i
= p
->num_options
, option
= p
->options
;
2571 strcpy(attrptr
, option
->name
);
2572 attrptr
+= strlen(attrptr
);
2578 for (valptr
= option
->value
; *valptr
; valptr
++)
2580 if (strchr(" \"\'\\", *valptr
))
2583 *attrptr
++ = *valptr
;
2591 * Populate the document-format-supported attribute...
2594 add_printer_formats(p
);
2596 DEBUG_printf(("cupsdSetPrinterAttrs: leaving name = %s, type = %x\n", p
->name
,
2600 * Add name-default attributes...
2603 add_printer_defaults(p
);
2607 * Write the IRIX printer config and status files...
2610 write_irix_config(p
);
2611 write_irix_state(p
);
2615 * Let the browse protocols reflect the change
2618 cupsdRegisterPrinter(p
);
2623 * 'cupsdSetPrinterReasons()' - Set/update the reasons strings.
2626 int /* O - 1 if something changed, 0 otherwise */
2627 cupsdSetPrinterReasons(
2628 cupsd_printer_t
*p
, /* I - Printer */
2629 const char *s
) /* I - Reasons strings */
2631 int i
, /* Looping var */
2632 changed
= 0; /* Did something change? */
2633 const char *sptr
; /* Pointer into reasons */
2634 char reason
[255], /* Reason string */
2635 *rptr
; /* Pointer into reason */
2638 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2639 "cupsdSetPrinterReasons(p=%p(%s),s=\"%s\"", p
, p
->name
, s
);
2641 if (s
[0] == '-' || s
[0] == '+')
2644 * Add/remove reasons...
2652 * Replace reasons...
2657 for (i
= 0; i
< p
->num_reasons
; i
++)
2658 _cupsStrFree(p
->reasons
[i
]);
2663 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
2665 if (PrintcapFormat
== PRINTCAP_PLIST
)
2666 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2669 if (!strcmp(s
, "none"))
2673 * Loop through all of the reasons...
2679 * Skip leading whitespace and commas...
2682 while (isspace(*sptr
& 255) || *sptr
== ',')
2685 for (rptr
= reason
; *sptr
&& !isspace(*sptr
& 255) && *sptr
!= ','; sptr
++)
2686 if (rptr
< (reason
+ sizeof(reason
) - 1))
2700 for (i
= 0; i
< p
->num_reasons
; i
++)
2701 if (!strcmp(reason
, p
->reasons
[i
]))
2704 * Found a match, so remove it...
2709 _cupsStrFree(p
->reasons
[i
]);
2711 if (i
< p
->num_reasons
)
2712 memmove(p
->reasons
+ i
, p
->reasons
+ i
+ 1,
2713 (p
->num_reasons
- i
) * sizeof(char *));
2715 if (!strcmp(reason
, "paused") && p
->state
== IPP_PRINTER_STOPPED
)
2716 cupsdSetPrinterState(p
, IPP_PRINTER_IDLE
, 1);
2718 if (strcmp(reason
, "connecting-to-device"))
2720 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
2722 if (PrintcapFormat
== PRINTCAP_PLIST
)
2723 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2728 else if (p
->num_reasons
< (int)(sizeof(p
->reasons
) / sizeof(p
->reasons
[0])))
2734 for (i
= 0; i
< p
->num_reasons
; i
++)
2735 if (!strcmp(reason
, p
->reasons
[i
]))
2738 if (i
>= p
->num_reasons
)
2740 if (i
>= (int)(sizeof(p
->reasons
) / sizeof(p
->reasons
[0])))
2742 cupsdLogMessage(CUPSD_LOG_ALERT
,
2743 "Too many printer-state-reasons values for %s (%d)",
2748 p
->reasons
[i
] = _cupsStrAlloc(reason
);
2752 if (!strcmp(reason
, "paused") && p
->state
!= IPP_PRINTER_STOPPED
)
2753 cupsdSetPrinterState(p
, IPP_PRINTER_STOPPED
, 1);
2755 if (strcmp(reason
, "connecting-to-device"))
2757 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
2759 if (PrintcapFormat
== PRINTCAP_PLIST
)
2760 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2771 * 'cupsdSetPrinterState()' - Update the current state of a printer.
2775 cupsdSetPrinterState(
2776 cupsd_printer_t
*p
, /* I - Printer to change */
2777 ipp_pstate_t s
, /* I - New state */
2778 int update
) /* I - Update printers.conf? */
2780 ipp_pstate_t old_state
; /* Old printer state */
2784 * Can't set status of remote printers...
2787 if (p
->type
& CUPS_PRINTER_DISCOVERED
)
2791 * Set the new state...
2794 if (PrintcapFormat
== PRINTCAP_PLIST
)
2795 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2797 old_state
= p
->state
;
2802 cupsdAddEvent(s
== IPP_PRINTER_STOPPED
? CUPSD_EVENT_PRINTER_STOPPED
:
2803 CUPSD_EVENT_PRINTER_STATE
, p
, NULL
,
2804 "%s \"%s\" state changed.",
2805 (p
->type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
2809 * Let the browse code know this needs to be updated...
2813 p
->state_time
= time(NULL
);
2817 write_irix_state(p
);
2822 * Set/clear the paused reason as needed...
2825 if (s
== IPP_PRINTER_STOPPED
)
2826 cupsdSetPrinterReasons(p
, "+paused");
2828 cupsdSetPrinterReasons(p
, "-paused");
2831 * Clear the message for the queue when going to processing...
2834 if (s
== IPP_PRINTER_PROCESSING
)
2835 p
->state_message
[0] = '\0';
2838 * Update the printer history...
2841 cupsdAddPrinterHistory(p
);
2844 * Let the browse protocols reflect the change...
2848 cupsdRegisterPrinter(p
);
2851 * Save the printer configuration if a printer goes from idle or processing
2852 * to stopped (or visa-versa)...
2856 (old_state
== IPP_PRINTER_STOPPED
) != (s
== IPP_PRINTER_STOPPED
))
2858 if (p
->type
& CUPS_PRINTER_CLASS
)
2859 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
2861 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
2867 * 'cupsdStopPrinter()' - Stop a printer from printing any jobs...
2871 cupsdStopPrinter(cupsd_printer_t
*p
, /* I - Printer to stop */
2872 int update
)/* I - Update printers.conf? */
2875 * Set the printer state...
2878 cupsdSetPrinterState(p
, IPP_PRINTER_STOPPED
, update
);
2881 * See if we have a job printing on this printer...
2884 if (p
->job
&& p
->job
->state_value
== IPP_JOB_PROCESSING
)
2885 cupsdSetJobState(p
->job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
2886 "Job stopped due to printer being paused.");
2891 * 'cupsdUpdatePrinterPPD()' - Update keywords in a printer's PPD file.
2894 int /* O - 1 if successful, 0 otherwise */
2895 cupsdUpdatePrinterPPD(
2896 cupsd_printer_t
*p
, /* I - Printer */
2897 int num_keywords
, /* I - Number of keywords */
2898 cups_option_t
*keywords
) /* I - Keywords */
2900 int i
; /* Looping var */
2901 cups_file_t
*src
, /* Original file */
2902 *dst
; /* New file */
2903 char srcfile
[1024], /* Original filename */
2904 dstfile
[1024], /* New filename */
2905 line
[1024], /* Line from file */
2906 keystring
[41]; /* Keyword from line */
2907 cups_option_t
*keyword
; /* Current keyword */
2910 cupsdLogMessage(CUPSD_LOG_INFO
, "Updating keywords in PPD file for %s...",
2914 * Get the old and new PPD filenames...
2917 snprintf(srcfile
, sizeof(srcfile
), "%s/ppd/%s.ppd.O", ServerRoot
, p
->name
);
2918 snprintf(dstfile
, sizeof(srcfile
), "%s/ppd/%s.ppd", ServerRoot
, p
->name
);
2921 * Rename the old file and open the old and new...
2924 if (rename(dstfile
, srcfile
))
2926 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to backup PPD file for %s: %s",
2927 p
->name
, strerror(errno
));
2931 if ((src
= cupsFileOpen(srcfile
, "r")) == NULL
)
2933 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open PPD file \"%s\": %s",
2934 srcfile
, strerror(errno
));
2935 rename(srcfile
, dstfile
);
2939 if ((dst
= cupsFileOpen(dstfile
, "w")) == NULL
)
2941 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create PPD file \"%s\": %s",
2942 dstfile
, strerror(errno
));
2944 rename(srcfile
, dstfile
);
2949 * Copy the first line and then write out all of the keywords...
2952 if (!cupsFileGets(src
, line
, sizeof(line
)))
2954 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to read PPD file \"%s\": %s",
2955 srcfile
, strerror(errno
));
2958 rename(srcfile
, dstfile
);
2962 cupsFilePrintf(dst
, "%s\n", line
);
2964 for (i
= num_keywords
, keyword
= keywords
; i
> 0; i
--, keyword
++)
2966 cupsdLogMessage(CUPSD_LOG_DEBUG
, "*%s: %s", keyword
->name
, keyword
->value
);
2967 cupsFilePrintf(dst
, "*%s: %s\n", keyword
->name
, keyword
->value
);
2971 * Then copy the rest of the PPD file, dropping any keywords we changed.
2974 while (cupsFileGets(src
, line
, sizeof(line
)))
2977 * Skip keywords we've already set...
2980 if (sscanf(line
, "*%40[^:]:", keystring
) == 1 &&
2981 cupsGetOption(keystring
, num_keywords
, keywords
))
2985 * Otherwise write the line...
2988 cupsFilePrintf(dst
, "%s\n", line
);
2992 * Close files and return...
3003 * 'cupsdUpdatePrinters()' - Update printers after a partial reload.
3007 cupsdUpdatePrinters(void)
3009 cupsd_printer_t
*p
; /* Current printer */
3013 * Loop through the printers and recreate the printer attributes
3014 * for any local printers since the policy and/or access control
3015 * stuff may have changed. Also, if browsing is disabled, remove
3016 * any remote printers...
3019 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3021 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3024 * Remove remote printers if we are no longer browsing...
3028 (p
->type
& (CUPS_PRINTER_IMPLICIT
| CUPS_PRINTER_DISCOVERED
)))
3030 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
3031 cupsArrayRemove(ImplicitPrinters
, p
);
3033 cupsArraySave(Printers
);
3034 cupsdDeletePrinter(p
, 0);
3035 cupsArrayRestore(Printers
);
3040 * Update the operation policy pointer...
3043 if ((p
->op_policy_ptr
= cupsdFindPolicy(p
->op_policy
)) == NULL
)
3044 p
->op_policy_ptr
= DefaultPolicyPtr
;
3047 * Update printer attributes as needed...
3050 if (!(p
->type
& CUPS_PRINTER_DISCOVERED
))
3051 cupsdSetPrinterAttrs(p
);
3057 * 'cupsdValidateDest()' - Validate a printer/class destination.
3060 const char * /* O - Printer or class name */
3062 const char *uri
, /* I - Printer URI */
3063 cups_ptype_t
*dtype
, /* O - Type (printer or class) */
3064 cupsd_printer_t
**printer
) /* O - Printer pointer */
3066 cupsd_printer_t
*p
; /* Current printer */
3067 char localname
[1024],/* Localized hostname */
3068 *lptr
, /* Pointer into localized hostname */
3069 *sptr
, /* Pointer into server name */
3070 *rptr
, /* Pointer into resource */
3071 scheme
[32], /* Scheme portion of URI */
3072 username
[64], /* Username portion of URI */
3073 hostname
[HTTP_MAX_HOST
],
3074 /* Host portion of URI */
3075 resource
[HTTP_MAX_URI
];
3076 /* Resource portion of URI */
3077 int port
; /* Port portion of URI */
3080 DEBUG_printf(("cupsdValidateDest(uri=\"%s\", dtype=%p, printer=%p)\n", uri
,
3084 * Initialize return values...
3091 *dtype
= (cups_ptype_t
)0;
3094 * Pull the hostname and resource from the URI...
3097 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
3098 username
, sizeof(username
), hostname
, sizeof(hostname
),
3099 &port
, resource
, sizeof(resource
));
3102 * See if the resource is a class or printer...
3105 if (!strncmp(resource
, "/classes/", 9))
3111 rptr
= resource
+ 9;
3113 else if (!strncmp(resource
, "/printers/", 10))
3119 rptr
= resource
+ 10;
3124 * Bad resource name...
3131 * See if the printer or class name exists...
3134 p
= cupsdFindDest(rptr
);
3136 if (p
== NULL
&& strchr(rptr
, '@') == NULL
)
3144 *dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
|
3145 CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
);
3151 * Change localhost to the server name...
3154 if (!strcasecmp(hostname
, "localhost"))
3155 strlcpy(hostname
, ServerName
, sizeof(hostname
));
3157 strlcpy(localname
, hostname
, sizeof(localname
));
3159 if (!strcasecmp(hostname
, ServerName
))
3162 * Localize the hostname...
3165 lptr
= strchr(localname
, '.');
3166 sptr
= strchr(ServerName
, '.');
3168 if (sptr
!= NULL
&& lptr
!= NULL
)
3171 * Strip the common domain name components...
3174 while (lptr
!= NULL
)
3176 if (!strcasecmp(lptr
, sptr
))
3182 lptr
= strchr(lptr
+ 1, '.');
3187 DEBUG_printf(("localized hostname is \"%s\"...\n", localname
));
3190 * Find a matching printer or class...
3193 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3195 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3196 if (!strcasecmp(p
->hostname
, localname
) &&
3197 !strcasecmp(p
->name
, rptr
))
3203 *dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
|
3204 CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
);
3214 * 'cupsdWritePrintcap()' - Write a pseudo-printcap file for older applications
3219 cupsdWritePrintcap(void)
3221 int i
; /* Looping var */
3222 cups_file_t
*fp
; /* Printcap file */
3223 cupsd_printer_t
*p
; /* Current printer */
3228 * Update the IRIX printer state for the default printer; if
3229 * no printers remain, then the default printer file will be
3233 write_irix_state(DefaultPrinter
);
3237 * See if we have a printcap file; if not, don't bother writing it.
3240 if (!Printcap
|| !*Printcap
)
3243 cupsdLogMessage(CUPSD_LOG_INFO
, "Generating printcap %s...", Printcap
);
3246 * Open the printcap file...
3249 if ((fp
= cupsFileOpen(Printcap
, "w")) == NULL
)
3253 * Put a comment header at the top so that users will know where the
3254 * data has come from...
3257 if (PrintcapFormat
!= PRINTCAP_PLIST
)
3258 cupsFilePrintf(fp
, "# This file was automatically generated by cupsd(8) "
3260 "# %s/printers.conf file. All changes to this file\n"
3261 "# will be lost.\n", ServerRoot
);
3264 * Write a new printcap with the current list of printers.
3267 switch (PrintcapFormat
)
3271 * Each printer is put in the file as:
3281 cupsFilePrintf(fp
, "%s|%s:rm=%s:rp=%s:\n", DefaultPrinter
->name
,
3282 DefaultPrinter
->info
, ServerName
,
3283 DefaultPrinter
->name
);
3285 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3287 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3288 if (p
!= DefaultPrinter
)
3289 cupsFilePrintf(fp
, "%s|%s:rm=%s:rp=%s:\n", p
->name
, p
->info
,
3290 ServerName
, p
->name
);
3293 case PRINTCAP_PLIST
:
3295 * Each printer is written as a dictionary in a plist file.
3296 * Currently the printer-name, printer-info, printer-is-accepting-jobs,
3297 * printer-location, printer-make-and-model, printer-state,
3298 * printer-state-reasons, printer-type, and (sanitized) device-uri.
3301 cupsFilePuts(fp
, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
3302 "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD "
3303 "PLIST 1.0//EN\" \"http://www.apple.com/DTDs/"
3304 "PropertyList-1.0.dtd\">\n"
3305 "<plist version=\"1.0\">\n"
3308 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3310 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3312 cupsFilePuts(fp
, "\t<dict>\n"
3313 "\t\t<key>printer-name</key>\n"
3315 write_xml_string(fp
, p
->name
);
3316 cupsFilePuts(fp
, "</string>\n"
3317 "\t\t<key>printer-info</key>\n"
3319 write_xml_string(fp
, p
->info
);
3320 cupsFilePrintf(fp
, "</string>\n"
3321 "\t\t<key>printer-is-accepting-jobs</key>\n"
3323 "\t\t<key>printer-location</key>\n"
3324 "\t\t<string>", p
->accepting
? "true" : "false");
3325 write_xml_string(fp
, p
->location
);
3326 cupsFilePuts(fp
, "</string>\n"
3327 "\t\t<key>printer-make-and-model</key>\n"
3329 write_xml_string(fp
, p
->make_model
);
3330 cupsFilePrintf(fp
, "</string>\n"
3331 "\t\t<key>printer-state</key>\n"
3332 "\t\t<integer>%d</integer>\n"
3333 "\t\t<key>printer-state-reasons</key>\n"
3334 "\t\t<array>\n", p
->state
);
3335 for (i
= 0; i
< p
->num_reasons
; i
++)
3337 cupsFilePuts(fp
, "\t\t\t<string>");
3338 write_xml_string(fp
, p
->reasons
[i
]);
3339 cupsFilePuts(fp
, "</string>\n");
3341 cupsFilePrintf(fp
, "\t\t</array>\n"
3342 "\t\t<key>printer-type</key>\n"
3343 "\t\t<integer>%d</integer>\n"
3344 "\t\t<key>device-uri</key>\n"
3345 "\t\t<string>", p
->type
);
3346 write_xml_string(fp
, p
->sanitized_device_uri
);
3347 cupsFilePuts(fp
, "</string>\n"
3350 cupsFilePuts(fp
, "</array>\n"
3354 case PRINTCAP_SOLARIS
:
3356 * Each printer is put in the file as:
3358 * _all:all=Printer1,Printer2,Printer3,...,PrinterN
3359 * _default:use=DefaultPrinter
3361 * :bsdaddr=ServerName,Printer1:\
3362 * :description=Description:
3364 * :bsdaddr=ServerName,Printer2:\
3365 * :description=Description:
3367 * :bsdaddr=ServerName,Printer3:\
3368 * :description=Description:
3371 * :bsdaddr=ServerName,PrinterN:\
3372 * :description=Description:
3375 cupsFilePuts(fp
, "_all:all=");
3376 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3378 p
= (cupsd_printer_t
*)cupsArrayCurrent(Printers
))
3379 cupsFilePrintf(fp
, "%s%c", p
->name
,
3380 cupsArrayNext(Printers
) ? ',' : '\n');
3383 cupsFilePrintf(fp
, "_default:use=%s\n", DefaultPrinter
->name
);
3385 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3387 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3388 cupsFilePrintf(fp
, "%s:\\\n"
3389 "\t:bsdaddr=%s,%s:\\\n"
3390 "\t:description=%s:\n",
3391 p
->name
, ServerName
, p
->name
,
3392 p
->info
? p
->info
: "");
3405 * 'add_printer_defaults()' - Add name-default attributes to the printer attributes.
3409 add_printer_defaults(cupsd_printer_t
*p
)/* I - Printer */
3411 int i
; /* Looping var */
3412 int num_options
; /* Number of default options */
3413 cups_option_t
*options
, /* Default options */
3414 *option
; /* Current option */
3415 char name
[256]; /* name-default */
3419 * Maintain a common array of default attribute names...
3422 if (!CommonDefaults
)
3424 CommonDefaults
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
3426 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("copies-default"));
3427 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("document-format-default"));
3428 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("finishings-default"));
3429 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("job-hold-until-default"));
3430 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("job-priority-default"));
3431 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("job-sheets-default"));
3432 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("media-default"));
3433 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("number-up-default"));
3434 cupsArrayAdd(CommonDefaults
,
3435 _cupsStrAlloc("orientation-requested-default"));
3436 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("sides-default"));
3440 * Add all of the default options from the .conf files...
3443 for (num_options
= 0, options
= NULL
, i
= p
->num_options
, option
= p
->options
;
3447 if (strcmp(option
->name
, "ipp-options") &&
3448 strcmp(option
->name
, "job-sheets") &&
3449 strcmp(option
->name
, "lease-duration"))
3451 snprintf(name
, sizeof(name
), "%s-default", option
->name
);
3452 num_options
= cupsAddOption(name
, option
->value
, num_options
, &options
);
3454 if (!cupsArrayFind(CommonDefaults
, name
))
3455 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc(name
));
3460 * Convert options to IPP attributes...
3463 cupsEncodeOptions2(p
->attrs
, num_options
, options
, IPP_TAG_PRINTER
);
3464 cupsFreeOptions(num_options
, options
);
3467 * Add standard -default attributes as needed...
3470 if (!cupsGetOption("copies", p
->num_options
, p
->options
))
3471 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "copies-default",
3474 if (!cupsGetOption("document-format", p
->num_options
, p
->options
))
3475 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_MIMETYPE
,
3476 "document-format-default", NULL
, "application/octet-stream");
3478 if (!cupsGetOption("job-hold-until", p
->num_options
, p
->options
))
3479 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
3480 "job-hold-until-default", NULL
, "no-hold");
3482 if (!cupsGetOption("job-priority", p
->num_options
, p
->options
))
3483 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
3484 "job-priority-default", 50);
3486 if (!cupsGetOption("number-up", p
->num_options
, p
->options
))
3487 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
3488 "number-up-default", 1);
3490 if (!cupsGetOption("orientation-requested", p
->num_options
, p
->options
))
3491 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NOVALUE
,
3492 "orientation-requested-default", NULL
, NULL
);
3494 if (!cupsGetOption("notify-lease-duration", p
->num_options
, p
->options
))
3495 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
3496 "notify-lease-duration-default", DefaultLeaseDuration
);
3498 if (!cupsGetOption("notify-events", p
->num_options
, p
->options
))
3499 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
3500 "notify-events-default", NULL
, "job-completed");
3505 * 'add_printer_filter()' - Add a MIME filter for a printer.
3510 cupsd_printer_t
*p
, /* I - Printer to add to */
3511 mime_type_t
*filtertype
, /* I - Filter or prefilter MIME type */
3512 const char *filter
) /* I - Filter to add */
3514 char super
[MIME_MAX_SUPER
], /* Super-type for filter */
3515 type
[MIME_MAX_TYPE
], /* Type for filter */
3516 program
[1024]; /* Program/filter name */
3517 int cost
; /* Cost of filter */
3518 mime_type_t
*temptype
; /* MIME type looping var */
3519 char filename
[1024], /* Full filter filename */
3520 *dirsep
; /* Pointer to directory separator */
3521 struct stat fileinfo
; /* File information */
3525 * Parse the filter string; it should be in the following format:
3527 * super/type cost program
3530 if (sscanf(filter
, "%15[^/]/%31s%d%*[ \t]%1023[^\n]", super
, type
, &cost
,
3533 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s: invalid filter string \"%s\"!",
3539 * See if the filter program exists; if not, stop the printer and flag
3543 if (strcmp(program
, "-"))
3545 if (program
[0] == '/')
3546 strlcpy(filename
, program
, sizeof(filename
));
3548 snprintf(filename
, sizeof(filename
), "%s/filter/%s", ServerBin
, program
);
3550 if (stat(filename
, &fileinfo
))
3552 memset(&fileinfo
, 0, sizeof(fileinfo
));
3554 snprintf(p
->state_message
, sizeof(p
->state_message
),
3555 "Filter \"%s\" for printer \"%s\" not available: %s",
3556 filename
, p
->name
, strerror(errno
));
3557 cupsdSetPrinterReasons(p
, "+cups-missing-filter-warning");
3559 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s", p
->state_message
);
3563 * When running as root, do additional security checks...
3569 * Only use filters that are owned by root and do not have world write
3573 if (fileinfo
.st_uid
|| (fileinfo
.st_mode
& (S_ISUID
| S_IWOTH
)) != 0)
3575 if (fileinfo
.st_uid
)
3576 snprintf(p
->state_message
, sizeof(p
->state_message
),
3577 "Filter \"%s\" for printer \"%s\" not owned by root",
3580 snprintf(p
->state_message
, sizeof(p
->state_message
),
3581 "Filter \"%s\" for printer \"%s\" has insecure permissions "
3582 "(0%o)", filename
, p
->name
, fileinfo
.st_mode
);
3584 cupsdSetPrinterReasons(p
, "+cups-insecure-filter-warning");
3586 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s", p
->state_message
);
3588 else if (fileinfo
.st_mode
)
3591 * Similarly, check that the parent directory is also owned by root and
3592 * does not have world write permissions.
3595 if ((dirsep
= strrchr(filename
, '/')) != NULL
)
3598 if (!stat(filename
, &fileinfo
) &&
3600 (fileinfo
.st_mode
& (S_ISUID
| S_IWOTH
)) != 0))
3602 if (fileinfo
.st_uid
)
3603 snprintf(p
->state_message
, sizeof(p
->state_message
),
3604 "Filter directory \"%s\" for printer \"%s\" not owned by "
3605 "root", filename
, p
->name
);
3607 snprintf(p
->state_message
, sizeof(p
->state_message
),
3608 "Filter directory \"%s\" for printer \"%s\" has insecure "
3609 "permissions (0%o)", filename
, p
->name
, fileinfo
.st_mode
);
3611 cupsdSetPrinterReasons(p
, "+cups-insecure-filter-warning");
3613 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s", p
->state_message
);
3620 * Add the filter to the MIME database, supporting wildcards as needed...
3623 for (temptype
= mimeFirstType(MimeDatabase
);
3625 temptype
= mimeNextType(MimeDatabase
))
3626 if (((super
[0] == '*' && strcasecmp(temptype
->super
, "printer")) ||
3627 !strcasecmp(temptype
->super
, super
)) &&
3628 (type
[0] == '*' || !strcasecmp(temptype
->type
, type
)))
3630 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3631 "add_printer_filter: %s: adding filter %s/%s %s/%s %d %s",
3632 p
->name
, temptype
->super
, temptype
->type
,
3633 filtertype
->super
, filtertype
->type
,
3635 mimeAddFilter(MimeDatabase
, temptype
, filtertype
, cost
, program
);
3641 * 'add_printer_formats()' - Add document-format-supported values for a printer.
3645 add_printer_formats(cupsd_printer_t
*p
) /* I - Printer */
3647 int i
; /* Looping var */
3648 mime_type_t
*type
; /* Current MIME type */
3649 cups_array_t
*filters
; /* Filters */
3650 ipp_attribute_t
*attr
; /* document-format-supported attribute */
3651 char mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
3652 /* MIME type name */
3656 * Raw (and remote) queues advertise all of the supported MIME
3660 cupsArrayDelete(p
->filetypes
);
3661 p
->filetypes
= NULL
;
3665 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
,
3666 (ipp_tag_t
)(IPP_TAG_MIMETYPE
| IPP_TAG_COPY
),
3667 "document-format-supported", NumMimeTypes
, NULL
, MimeTypes
);
3672 * Otherwise, loop through the supported MIME types and see if there
3673 * are filters for them...
3676 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_printer_formats: %d types, %d filters",
3677 mimeNumTypes(MimeDatabase
), mimeNumFilters(MimeDatabase
));
3679 p
->filetypes
= cupsArrayNew(NULL
, NULL
);
3681 for (type
= mimeFirstType(MimeDatabase
);
3683 type
= mimeNextType(MimeDatabase
))
3685 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", type
->super
, type
->type
);
3687 if ((filters
= mimeFilter(MimeDatabase
, type
, p
->filetype
, NULL
)) != NULL
)
3689 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3690 "add_printer_formats: %s: %s needs %d filters",
3691 p
->name
, mimetype
, cupsArrayCount(filters
));
3693 cupsArrayDelete(filters
);
3694 cupsArrayAdd(p
->filetypes
, type
);
3697 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3698 "add_printer_formats: %s: %s not supported",
3703 * Add the file formats that can be filtered...
3706 if ((type
= mimeType(MimeDatabase
, "application", "octet-stream")) == NULL
||
3707 !cupsArrayFind(p
->filetypes
, type
))
3712 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3713 "add_printer_formats: %s: %d supported types",
3714 p
->name
, cupsArrayCount(p
->filetypes
) + i
);
3716 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_MIMETYPE
,
3717 "document-format-supported",
3718 cupsArrayCount(p
->filetypes
) + i
, NULL
, NULL
);
3721 attr
->values
[0].string
.text
= _cupsStrAlloc("application/octet-stream");
3723 for (type
= (mime_type_t
*)cupsArrayFirst(p
->filetypes
);
3725 i
++, type
= (mime_type_t
*)cupsArrayNext(p
->filetypes
))
3727 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", type
->super
, type
->type
);
3729 attr
->values
[i
].string
.text
= _cupsStrAlloc(mimetype
);
3734 char pdl
[1024]; /* Buffer to build pdl list */
3735 mime_filter_t
*filter
; /* MIME filter looping var */
3740 if (mimeType(MimeDatabase
, "application", "pdf"))
3741 strlcat(pdl
, "application/pdf,", sizeof(pdl
));
3743 if (mimeType(MimeDatabase
, "application", "postscript"))
3744 strlcat(pdl
, "application/postscript,", sizeof(pdl
));
3746 if (mimeType(MimeDatabase
, "application", "vnd.cups-raster"))
3747 strlcat(pdl
, "application/vnd.cups-raster,", sizeof(pdl
));
3750 * Determine if this is a Tioga PrintJobMgr based queue...
3753 for (filter
= (mime_filter_t
*)cupsArrayFirst(MimeDatabase
->filters
);
3755 filter
= (mime_filter_t
*)cupsArrayNext(MimeDatabase
->filters
))
3757 if (filter
->dst
== p
->filetype
&& filter
->filter
&&
3758 strstr(filter
->filter
, "PrintJobMgr"))
3763 * We only support raw printing if this is not a Tioga PrintJobMgr based
3764 * queue and if application/octet-stream is a known conversion...
3767 if (!filter
&& mimeType(MimeDatabase
, "application", "octet-stream"))
3768 strlcat(pdl
, "application/octet-stream,", sizeof(pdl
));
3770 if (mimeType(MimeDatabase
, "image", "png"))
3771 strlcat(pdl
, "image/png,", sizeof(pdl
));
3774 pdl
[strlen(pdl
) - 1] = '\0'; /* Remove trailing comma */
3776 cupsdSetString(&p
->pdl
, pdl
);
3778 #endif /* HAVE_DNSSD */
3783 * 'add_string_array()' - Add a string to an array of CUPS strings.
3787 add_string_array(cups_array_t
**a
, /* I - Array */
3788 const char *s
) /* I - String */
3791 *a
= cupsArrayNew(NULL
, NULL
);
3793 cupsArrayAdd(*a
, _cupsStrAlloc(s
));
3798 * 'compare_printers()' - Compare two printers.
3801 static int /* O - Result of comparison */
3802 compare_printers(void *first
, /* I - First printer */
3803 void *second
, /* I - Second printer */
3804 void *data
) /* I - App data (not used) */
3806 return (strcasecmp(((cupsd_printer_t
*)first
)->name
,
3807 ((cupsd_printer_t
*)second
)->name
));
3812 * 'delete_printer_filters()' - Delete all MIME filters for a printer.
3816 delete_printer_filters(
3817 cupsd_printer_t
*p
) /* I - Printer to remove from */
3819 mime_filter_t
*filter
; /* MIME filter looping var */
3823 * Range check input...
3830 * Remove all filters from the MIME database that have a destination
3831 * type == printer...
3834 for (filter
= mimeFirstFilter(MimeDatabase
);
3836 filter
= mimeNextFilter(MimeDatabase
))
3837 if (filter
->dst
== p
->filetype
|| filter
->dst
== p
->prefiltertype
)
3840 * Delete the current filter...
3843 mimeDeleteFilter(MimeDatabase
, filter
);
3846 cupsdSetPrinterReasons(p
, "-cups-insecure-filter-warning"
3847 ",cups-missing-filter-warning");
3852 * 'delete_string_array()' - Delete an array of CUPS strings.
3856 delete_string_array(cups_array_t
**a
) /* I - Array */
3858 char *ptr
; /* Current string */
3861 for (ptr
= (char *)cupsArrayFirst(*a
);
3863 ptr
= (char *)cupsArrayNext(*a
))
3866 cupsArrayDelete(*a
);
3872 * 'load_ppd()' - Load a cached PPD file, updating the cache as needed.
3876 load_ppd(cupsd_printer_t
*p
) /* I - Printer */
3878 int i
; /* Looping var */
3879 cups_file_t
*cache
; /* Cache file */
3880 char cache_name
[1024]; /* Cache filename */
3881 struct stat cache_info
; /* Cache file info */
3882 ppd_file_t
*ppd
; /* PPD file */
3883 char ppd_name
[1024]; /* PPD filename */
3884 struct stat ppd_info
; /* PPD file info */
3885 int num_media
; /* Number of media options */
3886 char custom_in
[256], /* Custom size name in inches */
3887 custom_mm
[256]; /* Custom size name in millimeters */
3888 ppd_size_t
*size
; /* Current size */
3889 ppd_option_t
*output_bin
, /* OutputBin options */
3890 *duplex
; /* Duplex options */
3891 ppd_attr_t
*ppd_attr
; /* PPD attribute */
3892 _cups_pwg_media_t
*pwgmedia
; /* Matching PWG size name */
3893 ipp_attribute_t
*attr
; /* Attribute data */
3894 ipp_t
*media_col_default
, /* media-col-default collection value */
3895 *media_size
; /* media-size collection value */
3896 ipp_value_t
*val
; /* Attribute value */
3897 int num_finishings
; /* Number of finishings */
3898 int finishings
[5]; /* finishings-supported values */
3899 static const char * const sides
[3] = /* sides-supported values */
3902 "two-sided-long-edge",
3903 "two-sided-short-edge"
3905 static const char * const standard_commands
[] =
3906 { /* Standard CUPS commands */
3914 * Check to see if the cache is up-to-date...
3917 snprintf(cache_name
, sizeof(cache_name
), "%s/%s.ipp", CacheDir
, p
->name
);
3918 if (stat(cache_name
, &cache_info
))
3919 cache_info
.st_mtime
= 0;
3921 snprintf(ppd_name
, sizeof(ppd_name
), "%s/ppd/%s.ppd", ServerRoot
, p
->name
);
3922 if (stat(ppd_name
, &ppd_info
))
3923 ppd_info
.st_mtime
= 1;
3925 ippDelete(p
->ppd_attrs
);
3926 p
->ppd_attrs
= ippNew();
3928 if (cache_info
.st_mtime
>= ppd_info
.st_mtime
&&
3929 (cache
= cupsFileOpen(cache_name
, "r")) != NULL
)
3932 * Load cached information and return...
3935 cupsdLogMessage(CUPSD_LOG_DEBUG
, "load_ppd: Loading %s...", cache_name
);
3937 if (ippReadIO(cache
, (ipp_iocb_t
)cupsFileRead
, 1, NULL
,
3938 p
->ppd_attrs
) == IPP_DATA
)
3940 cupsFileClose(cache
);
3944 cupsFileClose(cache
);
3948 * Reload PPD attributes from disk...
3951 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3953 cupsdLogMessage(CUPSD_LOG_DEBUG
, "load_ppd: Loading %s...", ppd_name
);
3955 delete_string_array(&(p
->filters
));
3956 delete_string_array(&(p
->pre_filters
));
3958 p
->type
&= ~CUPS_PRINTER_OPTIONS
;
3959 p
->type
|= CUPS_PRINTER_BW
;
3961 finishings
[0] = IPP_FINISHINGS_NONE
;
3964 if ((ppd
= ppdOpenFile(ppd_name
)) != NULL
)
3967 * Add make/model and other various attributes...
3970 ppdMarkDefaults(ppd
);
3972 if (ppd
->color_device
)
3973 p
->type
|= CUPS_PRINTER_COLOR
;
3974 if (ppd
->variable_sizes
)
3975 p
->type
|= CUPS_PRINTER_VARIABLE
;
3976 if (!ppd
->manual_copies
)
3977 p
->type
|= CUPS_PRINTER_COPIES
;
3978 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsFax", NULL
)) != NULL
)
3979 if (ppd_attr
->value
&& !strcasecmp(ppd_attr
->value
, "true"))
3980 p
->type
|= CUPS_PRINTER_FAX
;
3982 ippAddBoolean(p
->ppd_attrs
, IPP_TAG_PRINTER
, "color-supported",
3984 if (ppd
->throughput
)
3985 ippAddInteger(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
3986 "pages-per-minute", ppd
->throughput
);
3991 * The NickName can be localized in the character set specified
3992 * by the LanugageEncoding attribute. However, ppdOpen2() has
3993 * already converted the ppd->nickname member to UTF-8 for us
3994 * (the original attribute value is available separately)
3997 cupsdSetString(&p
->make_model
, ppd
->nickname
);
3999 else if (ppd
->modelname
)
4002 * Model name can only contain specific characters...
4005 cupsdSetString(&p
->make_model
, ppd
->modelname
);
4008 cupsdSetString(&p
->make_model
, "Bad PPD File");
4010 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4011 "printer-make-and-model", NULL
, p
->make_model
);
4014 * Add media options from the PPD file...
4017 if (ppd
->num_sizes
== 0)
4019 if (!ppdFindAttr(ppd
, "APScannerOnly", NULL
))
4020 cupsdLogMessage(CUPSD_LOG_CRIT
,
4021 "The PPD file for printer %s contains no media "
4022 "options and is therefore invalid!", p
->name
);
4024 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4025 "media-supported", NULL
, "unknown");
4029 num_media
= ppd
->num_sizes
;
4030 if (ppd
->variable_sizes
)
4033 attr
= ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4034 "media-supported", num_media
, NULL
, NULL
);
4039 for (i
= ppd
->num_sizes
, size
= ppd
->sizes
; i
> 0; i
--, size
++)
4041 if (strcasecmp(size
->name
, "Custom"))
4043 if ((pwgmedia
= _cupsPWGMediaBySize(size
->width
,
4044 size
->length
)) != NULL
)
4046 val
->string
.text
= _cupsStrAlloc(pwgmedia
->pwg
);
4050 snprintf(custom_in
, sizeof(custom_in
), "adobe_%s_%gx%gin",
4051 size
->name
, size
->width
/ 72.0, size
->length
/ 72.0);
4052 snprintf(custom_mm
, sizeof(custom_mm
), "adobe_%s_%gx%gmm",
4053 size
->name
, size
->width
* 25.4 / 72.0,
4054 size
->length
* 25.4 / 72.0);
4055 if (strlen(custom_in
) < strlen(custom_mm
))
4056 val
->string
.text
= _cupsStrAlloc(custom_in
);
4058 val
->string
.text
= _cupsStrAlloc(custom_mm
);
4064 * Add media-default...
4067 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4068 "media-default", NULL
, val
->string
.text
);
4071 * Add media-col-default...
4074 media_size
= ippNew();
4075 ippAddInteger(media_size
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4076 "x-dimension", (int)(size
->width
* 2540.0 / 72.0));
4077 ippAddInteger(media_size
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4078 "y-dimension", (int)(size
->length
* 2540.0 / 72.0));
4080 media_col_default
= ippNew();
4081 ippAddString(media_col_default
, IPP_TAG_PRINTER
,
4082 IPP_TAG_KEYWORD
| IPP_TAG_COPY
, "media-color", NULL
,
4084 ippAddString(media_col_default
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4085 "media-key", NULL
,val
->string
.text
);
4086 ippAddCollection(media_col_default
, IPP_TAG_PRINTER
, "media-size",
4088 ippAddString(media_col_default
, IPP_TAG_PRINTER
,
4089 IPP_TAG_KEYWORD
| IPP_TAG_COPY
, "media-type", NULL
,
4092 ippAddCollection(p
->ppd_attrs
, IPP_TAG_PRINTER
,
4093 "media-col-default", media_col_default
);
4100 if (ppd
->variable_sizes
)
4102 snprintf(custom_in
, sizeof(custom_in
), "custom_min_%gx%gin",
4103 ppd
->custom_min
[0] / 72.0, ppd
->custom_min
[1] / 72.0);
4104 snprintf(custom_mm
, sizeof(custom_mm
), "custom_min_%gx%gmm",
4105 ppd
->custom_min
[0] * 25.4 / 72.0,
4106 ppd
->custom_min
[1] * 25.4 / 72.0);
4107 if (strlen(custom_in
) < strlen(custom_mm
))
4108 val
->string
.text
= _cupsStrAlloc(custom_in
);
4110 val
->string
.text
= _cupsStrAlloc(custom_mm
);
4113 snprintf(custom_in
, sizeof(custom_in
), "custom_max_%gx%gin",
4114 ppd
->custom_max
[0] / 72.0, ppd
->custom_max
[1] / 72.0);
4115 snprintf(custom_mm
, sizeof(custom_mm
), "custom_max_%gx%gmm",
4116 ppd
->custom_max
[0] * 25.4 / 72.0,
4117 ppd
->custom_max
[1] * 25.4 / 72.0);
4118 if (strlen(custom_in
) < strlen(custom_mm
))
4119 val
->string
.text
= _cupsStrAlloc(custom_in
);
4121 val
->string
.text
= _cupsStrAlloc(custom_mm
);
4130 if ((output_bin
= ppdFindOption(ppd
, "OutputBin")) != NULL
)
4132 attr
= ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4133 "output-bin-supported", output_bin
->num_choices
,
4138 for (i
= 0, val
= attr
->values
;
4139 i
< output_bin
->num_choices
;
4141 val
->string
.text
= _cupsStrAlloc(output_bin
->choices
[i
].choice
);
4144 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4145 "output-bin-default", NULL
, output_bin
->defchoice
);
4152 if ((duplex
= ppdFindOption(ppd
, "Duplex")) == NULL
)
4153 if ((duplex
= ppdFindOption(ppd
, "EFDuplex")) == NULL
)
4154 if ((duplex
= ppdFindOption(ppd
, "EFDuplexing")) == NULL
)
4155 if ((duplex
= ppdFindOption(ppd
, "KD03Duplex")) == NULL
)
4156 duplex
= ppdFindOption(ppd
, "JCLDuplex");
4158 if (duplex
&& duplex
->num_choices
> 1 &&
4159 !ppdInstallableConflict(ppd
, duplex
->keyword
, "DuplexTumble"))
4161 p
->type
|= CUPS_PRINTER_DUPLEX
;
4163 ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4164 "sides-supported", 3, NULL
, sides
);
4166 if (!strcasecmp(duplex
->defchoice
, "DuplexTumble"))
4167 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4168 "sides-default", NULL
, "two-sided-short-edge");
4169 else if (!strcasecmp(duplex
->defchoice
, "DuplexNoTumble"))
4170 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4171 "sides-default", NULL
, "two-sided-long-edge");
4173 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4174 "sides-default", NULL
, "one-sided");
4177 if (ppdFindOption(ppd
, "Collate") != NULL
)
4178 p
->type
|= CUPS_PRINTER_COLLATE
;
4180 if (ppdFindOption(ppd
, "StapleLocation") != NULL
)
4182 p
->type
|= CUPS_PRINTER_STAPLE
;
4183 finishings
[num_finishings
++] = IPP_FINISHINGS_STAPLE
;
4186 if (ppdFindOption(ppd
, "BindEdge") != NULL
)
4188 p
->type
|= CUPS_PRINTER_BIND
;
4189 finishings
[num_finishings
++] = IPP_FINISHINGS_BIND
;
4192 for (i
= 0; i
< ppd
->num_sizes
; i
++)
4193 if (ppd
->sizes
[i
].length
> 1728)
4194 p
->type
|= CUPS_PRINTER_LARGE
;
4195 else if (ppd
->sizes
[i
].length
> 1008)
4196 p
->type
|= CUPS_PRINTER_MEDIUM
;
4198 p
->type
|= CUPS_PRINTER_SMALL
;
4200 if ((ppd_attr
= ppdFindAttr(ppd
, "APICADriver", NULL
)) != NULL
&&
4201 ppd_attr
->value
&& !strcasecmp(ppd_attr
->value
, "true"))
4203 if ((ppd_attr
= ppdFindAttr(ppd
, "APScannerOnly", NULL
)) != NULL
&&
4204 ppd_attr
->value
&& !strcasecmp(ppd_attr
->value
, "true"))
4205 p
->type
|= CUPS_PRINTER_SCANNER
;
4207 p
->type
|= CUPS_PRINTER_MFP
;
4211 * Add a filter from application/vnd.cups-raw to printer/name to
4212 * handle "raw" printing by users.
4215 add_string_array(&(p
->filters
), "application/vnd.cups-raw 0 -");
4218 * Add any pre-filters in the PPD file...
4221 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsPreFilter", NULL
)) != NULL
)
4223 for (; ppd_attr
; ppd_attr
= ppdFindNextAttr(ppd
, "cupsPreFilter", NULL
))
4224 if (ppd_attr
->value
)
4225 add_string_array(&(p
->pre_filters
), ppd_attr
->value
);
4229 * Add any filters in the PPD file...
4232 DEBUG_printf(("ppd->num_filters = %d\n", ppd
->num_filters
));
4233 for (i
= 0; i
< ppd
->num_filters
; i
++)
4235 DEBUG_printf(("ppd->filters[%d] = \"%s\"\n", i
, ppd
->filters
[i
]));
4236 add_string_array(&(p
->filters
), ppd
->filters
[i
]);
4238 if (!strncasecmp(ppd
->filters
[i
], "application/vnd.cups-command", 28) &&
4239 isspace(ppd
->filters
[i
][28] & 255))
4240 p
->type
|= CUPS_PRINTER_COMMANDS
;
4243 if (ppd
->num_filters
== 0)
4246 * If there are no filters, add PostScript printing filters.
4249 add_string_array(&(p
->filters
),
4250 "application/vnd.cups-command 0 commandtops");
4251 add_string_array(&(p
->filters
),
4252 "application/vnd.cups-postscript 0 -");
4254 p
->type
|= CUPS_PRINTER_COMMANDS
;
4256 else if (!(p
->type
& CUPS_PRINTER_COMMANDS
))
4259 * See if this is a PostScript device without a command filter...
4262 for (i
= 0; i
< ppd
->num_filters
; i
++)
4263 if (!strncasecmp(ppd
->filters
[i
],
4264 "application/vnd.cups-postscript", 31) &&
4265 isspace(ppd
->filters
[i
][31] & 255))
4268 if (i
< ppd
->num_filters
)
4271 * Add the generic PostScript command filter...
4274 add_string_array(&(p
->filters
),
4275 "application/vnd.cups-command 0 commandtops");
4276 p
->type
|= CUPS_PRINTER_COMMANDS
;
4280 if (p
->type
& CUPS_PRINTER_COMMANDS
)
4282 char *commands
, /* Copy of commands */
4283 *start
, /* Start of name */
4284 *end
; /* End of name */
4285 int count
; /* Number of commands */
4288 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsCommands", NULL
)) != NULL
&&
4289 ppd_attr
->value
&& ppd_attr
->value
[0])
4291 for (count
= 0, start
= ppd_attr
->value
; *start
; count
++)
4293 while (isspace(*start
& 255))
4299 while (*start
&& !isspace(*start
& 255))
4309 * Make a copy of the commands string and count how many ...
4312 attr
= ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4313 "printer-commands", count
, NULL
, NULL
);
4315 commands
= strdup(ppd_attr
->value
);
4317 for (count
= 0, start
= commands
; *start
; count
++)
4319 while (isspace(*start
& 255))
4326 while (*end
&& !isspace(*end
& 255))
4332 attr
->values
[count
].string
.text
= _cupsStrAlloc(start
);
4342 * Add the standard list of commands...
4345 ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4347 (int)(sizeof(standard_commands
) /
4348 sizeof(standard_commands
[0])), NULL
,
4355 * No commands supported...
4358 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4359 "printer-commands", NULL
, "none");
4363 * Show current and available port monitors for this printer...
4366 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "port-monitor",
4367 NULL
, p
->port_monitor
? p
->port_monitor
: "none");
4369 for (i
= 1, ppd_attr
= ppdFindAttr(ppd
, "cupsPortMonitor", NULL
);
4371 i
++, ppd_attr
= ppdFindNextAttr(ppd
, "cupsPortMonitor", NULL
));
4375 if (strstr(ppd
->protocols
, "TBCP"))
4377 else if (strstr(ppd
->protocols
, "BCP"))
4381 attr
= ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
4382 "port-monitor-supported", i
, NULL
, NULL
);
4384 attr
->values
[0].string
.text
= _cupsStrAlloc("none");
4386 for (i
= 1, ppd_attr
= ppdFindAttr(ppd
, "cupsPortMonitor", NULL
);
4388 i
++, ppd_attr
= ppdFindNextAttr(ppd
, "cupsPortMonitor", NULL
))
4389 attr
->values
[i
].string
.text
= _cupsStrAlloc(ppd_attr
->value
);
4393 if (strstr(ppd
->protocols
, "TBCP"))
4394 attr
->values
[i
].string
.text
= _cupsStrAlloc("tbcp");
4395 else if (strstr(ppd
->protocols
, "BCP"))
4396 attr
->values
[i
].string
.text
= _cupsStrAlloc("bcp");
4400 cupsdSetString(&p
->product
, ppd
->product
);
4401 #endif /* HAVE_DNSSD */
4403 if (ppdFindAttr(ppd
, "APRemoteQueueID", NULL
))
4404 p
->type
|= CUPS_PRINTER_REMOTE
;
4407 * Close the PPD and set the type...
4412 else if (!access(ppd_name
, 0))
4414 int pline
; /* PPD line number */
4415 ppd_status_t pstatus
; /* PPD load status */
4418 pstatus
= ppdLastError(&pline
);
4420 cupsdLogMessage(CUPSD_LOG_ERROR
, "PPD file for %s cannot be loaded!",
4423 if (pstatus
<= PPD_ALLOC_ERROR
)
4424 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s", strerror(errno
));
4426 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s on line %d.",
4427 ppdErrorString(pstatus
), pline
);
4429 cupsdLogMessage(CUPSD_LOG_INFO
,
4430 "Hint: Run \"cupstestppd %s\" and fix any errors.",
4434 * Add a filter from application/vnd.cups-raw to printer/name to
4435 * handle "raw" printing by users.
4438 add_string_array(&(p
->filters
), "application/vnd.cups-raw 0 -");
4441 * Add a PostScript filter, since this is still possibly PS printer.
4444 add_string_array(&(p
->filters
), "application/vnd.cups-postscript 0 -");
4449 * If we have an interface script, add a filter entry for it...
4452 char interface
[1024]; /* Interface script */
4455 snprintf(interface
, sizeof(interface
), "%s/interfaces/%s", ServerRoot
,
4457 if (!access(interface
, X_OK
))
4460 * Yes, we have a System V style interface script; use it!
4463 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4464 "printer-make-and-model", NULL
,
4465 "Local System V Printer");
4467 snprintf(interface
, sizeof(interface
), "*/* 0 %s/interfaces/%s",
4468 ServerRoot
, p
->name
);
4469 add_string_array(&(p
->filters
), interface
);
4471 else if (!strncmp(p
->device_uri
, "ipp://", 6) &&
4472 (strstr(p
->device_uri
, "/printers/") != NULL
||
4473 strstr(p
->device_uri
, "/classes/") != NULL
||
4474 (strstr(p
->device_uri
, "._ipp.") != NULL
&&
4475 !strcmp(p
->device_uri
+ strlen(p
->device_uri
) - 5,
4479 * Tell the client this is really a hard-wired remote printer.
4482 p
->type
|= CUPS_PRINTER_REMOTE
;
4485 * Point the printer-uri-supported attribute to the
4489 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
4490 "printer-uri-supported", NULL
, p
->device_uri
);
4493 * Then set the make-and-model accordingly...
4496 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4497 "printer-make-and-model", NULL
, "Remote Printer");
4500 * Print all files directly...
4509 * Otherwise we have neither - treat this as a "dumb" printer
4510 * with no PPD file...
4513 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4514 "printer-make-and-model", NULL
, "Local Raw Printer");
4520 ippAddIntegers(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
4521 "finishings-supported", num_finishings
, finishings
);
4522 ippAddInteger(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
4523 "finishings-default", IPP_FINISHINGS_NONE
);
4525 if (ppd
&& (cache
= cupsFileOpen(cache_name
, "w")) != NULL
)
4528 * Save cached PPD attributes to disk...
4531 cupsdLogMessage(CUPSD_LOG_DEBUG
, "load_ppd: Saving %s...", cache_name
);
4533 p
->ppd_attrs
->state
= IPP_IDLE
;
4535 if (ippWriteIO(cache
, (ipp_iocb_t
)cupsFileWrite
, 1, NULL
,
4536 p
->ppd_attrs
) != IPP_DATA
)
4538 cupsdLogMessage(CUPSD_LOG_ERROR
,
4539 "Unable to save PPD cache file \"%s\" - %s", cache_name
,
4544 cupsFileClose(cache
);
4546 else if (cache_info
.st_mtime
)
4549 * Remove cache file...
4559 * 'write_irix_config()' - Update the config files used by the IRIX
4564 write_irix_config(cupsd_printer_t
*p
) /* I - Printer to update */
4566 char filename
[1024]; /* Interface script filename */
4567 cups_file_t
*fp
; /* Interface script file */
4568 ipp_attribute_t
*attr
; /* Attribute data */
4572 * Add dummy interface and GUI scripts to fool SGI's "challenged" printing
4573 * tools. First the interface script that tells the tools what kind of
4574 * printer we have...
4577 snprintf(filename
, sizeof(filename
), "/var/spool/lp/interface/%s", p
->name
);
4579 if (p
->type
& CUPS_PRINTER_CLASS
)
4581 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4583 cupsFilePuts(fp
, "#!/bin/sh\n");
4585 if ((attr
= ippFindAttribute(p
->attrs
, "printer-make-and-model",
4586 IPP_TAG_TEXT
)) != NULL
)
4587 cupsFilePrintf(fp
, "NAME=\"%s\"\n", attr
->values
[0].string
.text
);
4588 else if (p
->type
& CUPS_PRINTER_CLASS
)
4589 cupsFilePuts(fp
, "NAME=\"Printer Class\"\n");
4591 cupsFilePuts(fp
, "NAME=\"Remote Destination\"\n");
4593 if (p
->type
& CUPS_PRINTER_COLOR
)
4594 cupsFilePuts(fp
, "TYPE=ColorPostScript\n");
4596 cupsFilePuts(fp
, "TYPE=MonoPostScript\n");
4598 cupsFilePrintf(fp
, "HOSTNAME=%s\n", ServerName
);
4599 cupsFilePrintf(fp
, "HOSTPRINTER=%s\n", p
->name
);
4603 chmod(filename
, 0755);
4604 chown(filename
, User
, Group
);
4608 * Then the member file that tells which device file the queue is connected
4609 * to... Networked printers use "/dev/null" in this file, so that's what
4610 * we use (the actual device URI can confuse some apps...)
4613 snprintf(filename
, sizeof(filename
), "/var/spool/lp/member/%s", p
->name
);
4615 if (p
->type
& CUPS_PRINTER_CLASS
)
4617 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4619 cupsFilePuts(fp
, "/dev/null\n");
4623 chmod(filename
, 0644);
4624 chown(filename
, User
, Group
);
4628 * The gui_interface file is a script or program that launches a GUI
4629 * option panel for the printer, using options specified on the
4630 * command-line in the third argument. The option panel must send
4631 * any printing options to stdout on a single line when the user
4632 * accepts them, or nothing if the user cancels the dialog.
4634 * The default options panel program is /usr/bin/glpoptions, from
4635 * the ESP Print Pro software. You can select another using the
4636 * PrintcapGUI option.
4639 snprintf(filename
, sizeof(filename
), "/var/spool/lp/gui_interface/ELF/%s.gui", p
->name
);
4641 if (p
->type
& CUPS_PRINTER_CLASS
)
4643 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4645 cupsFilePuts(fp
, "#!/bin/sh\n");
4646 cupsFilePrintf(fp
, "%s -d %s -o \"$3\"\n", PrintcapGUI
, p
->name
);
4650 chmod(filename
, 0755);
4651 chown(filename
, User
, Group
);
4655 * The POD config file is needed by the printstatus command to show
4656 * the printer location and device.
4659 snprintf(filename
, sizeof(filename
), "/var/spool/lp/pod/%s.config", p
->name
);
4661 if (p
->type
& CUPS_PRINTER_CLASS
)
4663 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4665 cupsFilePrintf(fp
, "Printer Class | %s\n",
4666 (p
->type
& CUPS_PRINTER_COLOR
) ? "ColorPostScript" : "MonoPostScript");
4667 cupsFilePrintf(fp
, "Printer Model | %s\n", p
->make_model
? p
->make_model
: "");
4668 cupsFilePrintf(fp
, "Location Code | %s\n", p
->location
? p
->location
: "");
4669 cupsFilePrintf(fp
, "Physical Location | %s\n", p
->info
? p
->info
: "");
4670 cupsFilePrintf(fp
, "Port Path | %s\n", p
->device_uri
);
4671 cupsFilePrintf(fp
, "Config Path | /var/spool/lp/pod/%s.config\n", p
->name
);
4672 cupsFilePrintf(fp
, "Active Status Path | /var/spool/lp/pod/%s.status\n", p
->name
);
4673 cupsFilePuts(fp
, "Status Update Wait | 10 seconds\n");
4677 chmod(filename
, 0664);
4678 chown(filename
, User
, Group
);
4684 * 'write_irix_state()' - Update the status files used by IRIX printing
4689 write_irix_state(cupsd_printer_t
*p
) /* I - Printer to update */
4691 char filename
[1024]; /* Interface script filename */
4692 cups_file_t
*fp
; /* Interface script file */
4693 int tag
; /* Status tag value */
4699 * The POD status file is needed for the printstatus window to
4700 * provide the current status of the printer.
4703 snprintf(filename
, sizeof(filename
), "/var/spool/lp/pod/%s.status", p
->name
);
4705 if (p
->type
& CUPS_PRINTER_CLASS
)
4707 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4709 cupsFilePrintf(fp
, "Operational Status | %s\n",
4710 (p
->state
== IPP_PRINTER_IDLE
) ? "Idle" :
4711 (p
->state
== IPP_PRINTER_PROCESSING
) ? "Busy" :
4713 cupsFilePrintf(fp
, "Information | 01 00 00 | %s\n", CUPS_SVERSION
);
4714 cupsFilePrintf(fp
, "Information | 02 00 00 | Device URI: %s\n",
4716 cupsFilePrintf(fp
, "Information | 03 00 00 | %s jobs\n",
4717 p
->accepting
? "Accepting" : "Not accepting");
4718 cupsFilePrintf(fp
, "Information | 04 00 00 | %s\n", p
->state_message
);
4722 chmod(filename
, 0664);
4723 chown(filename
, User
, Group
);
4727 * The activeicons file is needed to provide desktop icons for printers:
4729 * [ quoted from /usr/lib/print/tagit ]
4731 * --- Type of printer tags (base values)
4733 * Dumb=66048 # 0x10200
4734 * DumbColor=66080 # 0x10220
4735 * Raster=66112 # 0x10240
4736 * ColorRaster=66144 # 0x10260
4737 * Plotter=66176 # 0x10280
4738 * PostScript=66208 # 0x102A0
4739 * ColorPostScript=66240 # 0x102C0
4740 * MonoPostScript=66272 # 0x102E0
4742 * --- Printer state modifiers for local printers
4747 * Unknown=3 # 0x3 (Faulted due to unknown reason)
4749 * --- Printer state modifiers for network printers
4753 * NetFaulted=10 # 0xA
4754 * NetUnknown=11 # 0xB (Faulted due to unknown reason)
4757 snprintf(filename
, sizeof(filename
), "/var/spool/lp/activeicons/%s", p
->name
);
4759 if (p
->type
& CUPS_PRINTER_CLASS
)
4761 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4763 if (p
->type
& CUPS_PRINTER_COLOR
)
4768 if (p
->type
& CUPS_PRINTER_REMOTE
)
4771 if (p
->state
== IPP_PRINTER_PROCESSING
)
4774 else if (p
->state
== IPP_PRINTER_STOPPED
)
4777 cupsFilePuts(fp
, "#!/bin/sh\n");
4778 cupsFilePrintf(fp
, "#Tag %d\n", tag
);
4782 chmod(filename
, 0755);
4783 chown(filename
, User
, Group
);
4788 * The default file is needed by the printers window to show
4789 * the default printer.
4792 snprintf(filename
, sizeof(filename
), "/var/spool/lp/default");
4794 if (DefaultPrinter
!= NULL
)
4796 if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4798 cupsFilePrintf(fp
, "%s\n", DefaultPrinter
->name
);
4802 chmod(filename
, 0644);
4803 chown(filename
, User
, Group
);
4813 * 'write_xml_string()' - Write a string with XML escaping.
4817 write_xml_string(cups_file_t
*fp
, /* I - File to write to */
4818 const char *s
) /* I - String to write */
4820 const char *start
; /* Start of current sequence */
4826 for (start
= s
; *s
; s
++)
4831 cupsFileWrite(fp
, start
, s
- start
);
4833 cupsFilePuts(fp
, "&");
4839 cupsFileWrite(fp
, start
, s
- start
);
4841 cupsFilePuts(fp
, "<");
4847 cupsFilePuts(fp
, start
);
4852 * End of "$Id: printers.c 7968 2008-09-19 23:03:01Z mike $".