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
;
1128 cupsdSetPrinterReasons(p
, "+paused");
1131 cupsdLogMessage(CUPSD_LOG_ERROR
,
1132 "Syntax error on line %d of printers.conf.", linenum
);
1134 else if (!strcasecmp(line
, "StateMessage"))
1137 * Set the initial queue state message...
1141 strlcpy(p
->state_message
, value
, sizeof(p
->state_message
));
1143 else if (!strcasecmp(line
, "StateTime"))
1146 * Set the state time...
1150 p
->state_time
= atoi(value
);
1152 else if (!strcasecmp(line
, "Accepting"))
1155 * Set the initial accepting state...
1159 (!strcasecmp(value
, "yes") ||
1160 !strcasecmp(value
, "on") ||
1161 !strcasecmp(value
, "true")))
1164 (!strcasecmp(value
, "no") ||
1165 !strcasecmp(value
, "off") ||
1166 !strcasecmp(value
, "false")))
1169 cupsdLogMessage(CUPSD_LOG_ERROR
,
1170 "Syntax error on line %d of printers.conf.", linenum
);
1172 else if (!strcasecmp(line
, "Type"))
1175 p
->type
= atoi(value
);
1177 cupsdLogMessage(CUPSD_LOG_ERROR
,
1178 "Syntax error on line %d of printers.conf.", linenum
);
1180 else if (!strcasecmp(line
, "Product"))
1185 p
->product
= _cupsStrAlloc(value
);
1186 #endif /* HAVE_DNSSD */
1189 cupsdLogMessage(CUPSD_LOG_ERROR
,
1190 "Syntax error on line %d of printers.conf.", linenum
);
1192 else if (!strcasecmp(line
, "Filter"))
1197 p
->filters
= cupsArrayNew(NULL
, NULL
);
1199 cupsArrayAdd(p
->filters
, _cupsStrAlloc(value
));
1202 cupsdLogMessage(CUPSD_LOG_ERROR
,
1203 "Syntax error on line %d of printers.conf.", linenum
);
1205 else if (!strcasecmp(line
, "PreFilter"))
1209 if (!p
->pre_filters
)
1210 p
->pre_filters
= cupsArrayNew(NULL
, NULL
);
1212 cupsArrayAdd(p
->pre_filters
, _cupsStrAlloc(value
));
1215 cupsdLogMessage(CUPSD_LOG_ERROR
,
1216 "Syntax error on line %d of printers.conf.", linenum
);
1218 else if (!strcasecmp(line
, "Shared"))
1221 * Set the initial shared state...
1225 (!strcasecmp(value
, "yes") ||
1226 !strcasecmp(value
, "on") ||
1227 !strcasecmp(value
, "true")))
1230 (!strcasecmp(value
, "no") ||
1231 !strcasecmp(value
, "off") ||
1232 !strcasecmp(value
, "false")))
1235 cupsdLogMessage(CUPSD_LOG_ERROR
,
1236 "Syntax error on line %d of printers.conf.", linenum
);
1238 else if (!strcasecmp(line
, "JobSheets"))
1241 * Set the initial job sheets...
1246 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
1251 cupsdSetString(&p
->job_sheets
[0], value
);
1253 while (isspace(*valueptr
& 255))
1258 for (value
= valueptr
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
1263 cupsdSetString(&p
->job_sheets
[1], value
);
1267 cupsdLogMessage(CUPSD_LOG_ERROR
,
1268 "Syntax error on line %d of printers.conf.", linenum
);
1270 else if (!strcasecmp(line
, "AllowUser"))
1275 cupsdAddPrinterUser(p
, value
);
1278 cupsdLogMessage(CUPSD_LOG_ERROR
,
1279 "Syntax error on line %d of printers.conf.", linenum
);
1281 else if (!strcasecmp(line
, "DenyUser"))
1286 cupsdAddPrinterUser(p
, value
);
1289 cupsdLogMessage(CUPSD_LOG_ERROR
,
1290 "Syntax error on line %d of printers.conf.", linenum
);
1292 else if (!strcasecmp(line
, "QuotaPeriod"))
1295 p
->quota_period
= atoi(value
);
1297 cupsdLogMessage(CUPSD_LOG_ERROR
,
1298 "Syntax error on line %d of printers.conf.", linenum
);
1300 else if (!strcasecmp(line
, "PageLimit"))
1303 p
->page_limit
= atoi(value
);
1305 cupsdLogMessage(CUPSD_LOG_ERROR
,
1306 "Syntax error on line %d of printers.conf.", linenum
);
1308 else if (!strcasecmp(line
, "KLimit"))
1311 p
->k_limit
= atoi(value
);
1313 cupsdLogMessage(CUPSD_LOG_ERROR
,
1314 "Syntax error on line %d of printers.conf.", linenum
);
1316 else if (!strcasecmp(line
, "OpPolicy"))
1320 cupsd_policy_t
*pol
; /* Policy */
1323 if ((pol
= cupsdFindPolicy(value
)) != NULL
)
1325 cupsdSetString(&p
->op_policy
, value
);
1326 p
->op_policy_ptr
= pol
;
1329 cupsdLogMessage(CUPSD_LOG_ERROR
,
1330 "Bad policy \"%s\" on line %d of printers.conf",
1334 cupsdLogMessage(CUPSD_LOG_ERROR
,
1335 "Syntax error on line %d of printers.conf.", linenum
);
1337 else if (!strcasecmp(line
, "ErrorPolicy"))
1340 cupsdSetString(&p
->error_policy
, value
);
1342 cupsdLogMessage(CUPSD_LOG_ERROR
,
1343 "Syntax error on line %d of printers.conf.", linenum
);
1345 else if (!strcasecmp(line
, "Attribute") && value
)
1347 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
1350 cupsdLogMessage(CUPSD_LOG_ERROR
,
1351 "Syntax error on line %d of printers.conf.", linenum
);
1354 for (; *valueptr
&& isspace(*valueptr
& 255); *valueptr
++ = '\0');
1357 cupsdSetPrinterAttrs(p
);
1359 if (!strcmp(value
, "marker-change-time"))
1360 p
->marker_time
= atoi(valueptr
);
1362 cupsdSetPrinterAttr(p
, value
, valueptr
);
1368 * Something else we don't understand...
1371 cupsdLogMessage(CUPSD_LOG_ERROR
,
1372 "Unknown configuration directive %s on line %d of printers.conf.",
1382 * 'cupsdRenamePrinter()' - Rename a printer.
1387 cupsd_printer_t
*p
, /* I - Printer */
1388 const char *name
) /* I - New name */
1391 * Remove the printer from the array(s) first...
1394 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1395 "cupsdRenamePrinter: Removing %s from Printers", p
->name
);
1396 cupsArrayRemove(Printers
, p
);
1398 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
1400 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1401 "cupsdRenamePrinter: Removing %s from ImplicitPrinters",
1403 cupsArrayRemove(ImplicitPrinters
, p
);
1407 * Rename the printer type...
1410 mimeDeleteType(MimeDatabase
, p
->filetype
);
1411 p
->filetype
= mimeAddType(MimeDatabase
, "printer", name
);
1413 mimeDeleteType(MimeDatabase
, p
->prefiltertype
);
1414 p
->prefiltertype
= mimeAddType(MimeDatabase
, "prefilter", name
);
1417 * Rename the printer...
1420 cupsdSetString(&p
->name
, name
);
1423 * Reset printer attributes...
1426 cupsdSetPrinterAttrs(p
);
1429 * Add the printer back to the printer array(s)...
1432 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1433 "cupsdRenamePrinter: Adding %s to Printers", p
->name
);
1434 cupsArrayAdd(Printers
, p
);
1436 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
1438 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1439 "cupsdRenamePrinter: Adding %s to ImplicitPrinters",
1441 cupsArrayAdd(ImplicitPrinters
, p
);
1447 * 'cupsdSaveAllPrinters()' - Save all printer definitions to the printers.conf
1452 cupsdSaveAllPrinters(void)
1454 int i
; /* Looping var */
1455 cups_file_t
*fp
; /* printers.conf file */
1456 char temp
[1024], /* Temporary string */
1457 backup
[1024], /* printers.conf.O file */
1458 value
[2048], /* Value string */
1459 *ptr
; /* Pointer into value */
1460 cupsd_printer_t
*printer
; /* Current printer class */
1461 time_t curtime
; /* Current time */
1462 struct tm
*curdate
; /* Current date */
1463 cups_option_t
*option
; /* Current option */
1464 ipp_attribute_t
*marker
; /* Current marker attribute */
1468 * Create the printers.conf file...
1471 snprintf(temp
, sizeof(temp
), "%s/printers.conf", ServerRoot
);
1472 snprintf(backup
, sizeof(backup
), "%s/printers.conf.O", ServerRoot
);
1474 if (rename(temp
, backup
))
1476 if (errno
!= ENOENT
)
1477 cupsdLogMessage(CUPSD_LOG_ERROR
,
1478 "Unable to backup printers.conf - %s", strerror(errno
));
1481 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
1483 cupsdLogMessage(CUPSD_LOG_ERROR
,
1484 "Unable to save printers.conf - %s", strerror(errno
));
1486 if (rename(backup
, temp
))
1487 cupsdLogMessage(CUPSD_LOG_ERROR
,
1488 "Unable to restore printers.conf - %s", strerror(errno
));
1492 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving printers.conf...");
1495 * Restrict access to the file...
1498 fchown(cupsFileNumber(fp
), getuid(), Group
);
1499 fchmod(cupsFileNumber(fp
), 0600);
1502 * Write a small header to the file...
1505 curtime
= time(NULL
);
1506 curdate
= localtime(&curtime
);
1507 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
1509 cupsFilePuts(fp
, "# Printer configuration file for " CUPS_SVERSION
"\n");
1510 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
1513 * Write each local printer known to the system...
1516 for (printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1518 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1521 * Skip remote destinations and printer classes...
1524 if ((printer
->type
& CUPS_PRINTER_DISCOVERED
) ||
1525 (printer
->type
& CUPS_PRINTER_CLASS
) ||
1526 (printer
->type
& CUPS_PRINTER_IMPLICIT
))
1530 * Write printers as needed...
1533 if (printer
== DefaultPrinter
)
1534 cupsFilePrintf(fp
, "<DefaultPrinter %s>\n", printer
->name
);
1536 cupsFilePrintf(fp
, "<Printer %s>\n", printer
->name
);
1538 if (printer
->num_auth_info_required
> 0)
1540 switch (printer
->num_auth_info_required
)
1543 strlcpy(value
, printer
->auth_info_required
[0], sizeof(value
));
1547 snprintf(value
, sizeof(value
), "%s,%s",
1548 printer
->auth_info_required
[0],
1549 printer
->auth_info_required
[1]);
1554 snprintf(value
, sizeof(value
), "%s,%s,%s",
1555 printer
->auth_info_required
[0],
1556 printer
->auth_info_required
[1],
1557 printer
->auth_info_required
[2]);
1561 cupsFilePutConf(fp
, "AuthInfoRequired", value
);
1565 cupsFilePutConf(fp
, "Info", printer
->info
);
1567 if (printer
->location
)
1568 cupsFilePutConf(fp
, "Location", printer
->location
);
1570 if (printer
->make_model
)
1571 cupsFilePutConf(fp
, "MakeModel", printer
->make_model
);
1573 cupsFilePutConf(fp
, "DeviceURI", printer
->device_uri
);
1575 if (printer
->port_monitor
)
1576 cupsFilePutConf(fp
, "PortMonitor", printer
->port_monitor
);
1578 if (printer
->state
== IPP_PRINTER_STOPPED
)
1580 cupsFilePuts(fp
, "State Stopped\n");
1582 if (printer
->state_message
)
1583 cupsFilePutConf(fp
, "StateMessage", printer
->state_message
);
1586 cupsFilePuts(fp
, "State Idle\n");
1588 cupsFilePrintf(fp
, "StateTime %d\n", (int)printer
->state_time
);
1590 for (i
= 0; i
< printer
->num_reasons
; i
++)
1591 if (strcmp(printer
->reasons
[i
], "com.apple.print.recoverable-warning") &&
1592 strcmp(printer
->reasons
[i
], "connecting-to-device") &&
1593 strcmp(printer
->reasons
[i
], "cups-insecure-filter-warning") &&
1594 strcmp(printer
->reasons
[i
], "cups-missing-filter-warning"))
1595 cupsFilePutConf(fp
, "Reason", printer
->reasons
[i
]);
1597 cupsFilePrintf(fp
, "Type %d\n", printer
->type
);
1600 if (printer
->product
)
1601 cupsFilePutConf(fp
, "Product", printer
->product
);
1602 #endif /* HAVE_DNSSD */
1604 for (ptr
= (char *)cupsArrayFirst(printer
->filters
);
1606 ptr
= (char *)cupsArrayNext(printer
->filters
))
1607 cupsFilePutConf(fp
, "Filter", ptr
);
1609 for (ptr
= (char *)cupsArrayFirst(printer
->pre_filters
);
1611 ptr
= (char *)cupsArrayNext(printer
->pre_filters
))
1612 cupsFilePutConf(fp
, "PreFilter", ptr
);
1614 if (printer
->accepting
)
1615 cupsFilePuts(fp
, "Accepting Yes\n");
1617 cupsFilePuts(fp
, "Accepting No\n");
1619 if (printer
->shared
)
1620 cupsFilePuts(fp
, "Shared Yes\n");
1622 cupsFilePuts(fp
, "Shared No\n");
1624 snprintf(value
, sizeof(value
), "%s %s", printer
->job_sheets
[0],
1625 printer
->job_sheets
[1]);
1626 cupsFilePutConf(fp
, "JobSheets", value
);
1628 cupsFilePrintf(fp
, "QuotaPeriod %d\n", printer
->quota_period
);
1629 cupsFilePrintf(fp
, "PageLimit %d\n", printer
->page_limit
);
1630 cupsFilePrintf(fp
, "KLimit %d\n", printer
->k_limit
);
1632 for (i
= 0; i
< printer
->num_users
; i
++)
1633 cupsFilePutConf(fp
, printer
->deny_users
? "DenyUser" : "AllowUser",
1636 if (printer
->op_policy
)
1637 cupsFilePutConf(fp
, "OpPolicy", printer
->op_policy
);
1638 if (printer
->error_policy
)
1639 cupsFilePutConf(fp
, "ErrorPolicy", printer
->error_policy
);
1641 for (i
= printer
->num_options
, option
= printer
->options
;
1645 snprintf(value
, sizeof(value
), "%s %s", option
->name
, option
->value
);
1646 cupsFilePutConf(fp
, "Option", value
);
1649 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-colors",
1650 IPP_TAG_NAME
)) != NULL
)
1652 snprintf(value
, sizeof(value
), "%s ", marker
->name
);
1654 for (i
= 0, ptr
= value
+ strlen(value
);
1655 i
< marker
->num_values
&& ptr
< (value
+ sizeof(value
) - 1);
1661 strlcpy(ptr
, marker
->values
[i
].string
.text
,
1662 value
+ sizeof(value
) - ptr
);
1667 cupsFilePutConf(fp
, "Attribute", value
);
1670 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-levels",
1671 IPP_TAG_INTEGER
)) != NULL
)
1673 cupsFilePrintf(fp
, "Attribute %s %d", marker
->name
,
1674 marker
->values
[0].integer
);
1675 for (i
= 1; i
< marker
->num_values
; i
++)
1676 cupsFilePrintf(fp
, ",%d", marker
->values
[i
].integer
);
1677 cupsFilePuts(fp
, "\n");
1680 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-low-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-high-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-message",
1701 IPP_TAG_TEXT
)) != NULL
)
1703 snprintf(value
, sizeof(value
), "%s %s", marker
->name
,
1704 marker
->values
[0].string
.text
);
1706 cupsFilePutConf(fp
, "Attribute", value
);
1709 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-names",
1710 IPP_TAG_NAME
)) != NULL
)
1712 snprintf(value
, sizeof(value
), "%s ", marker
->name
);
1714 for (i
= 0, ptr
= value
+ strlen(value
);
1715 i
< marker
->num_values
&& ptr
< (value
+ sizeof(value
) - 1);
1721 strlcpy(ptr
, marker
->values
[i
].string
.text
,
1722 value
+ sizeof(value
) - ptr
);
1727 cupsFilePutConf(fp
, "Attribute", value
);
1730 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-types",
1731 IPP_TAG_KEYWORD
)) != NULL
)
1733 snprintf(value
, sizeof(value
), "%s ", marker
->name
);
1735 for (i
= 0, ptr
= value
+ strlen(value
);
1736 i
< marker
->num_values
&& ptr
< (value
+ sizeof(value
) - 1);
1742 strlcpy(ptr
, marker
->values
[i
].string
.text
,
1743 value
+ sizeof(value
) - ptr
);
1748 cupsFilePutConf(fp
, "Attribute", value
);
1751 if (printer
->marker_time
)
1752 cupsFilePrintf(fp
, "Attribute marker-change-time %ld\n",
1753 (long)printer
->marker_time
);
1755 cupsFilePuts(fp
, "</Printer>\n");
1759 * Make IRIX desktop & printer status happy
1762 write_irix_state(printer
);
1771 * 'cupsdSetAuthInfoRequired()' - Set the required authentication info.
1774 int /* O - 1 if value OK, 0 otherwise */
1775 cupsdSetAuthInfoRequired(
1776 cupsd_printer_t
*p
, /* I - Printer */
1777 const char *values
, /* I - Plain text value (or NULL) */
1778 ipp_attribute_t
*attr
) /* I - IPP attribute value (or NULL) */
1780 int i
; /* Looping var */
1783 p
->num_auth_info_required
= 0;
1786 * Do we have a plain text value?
1792 * Yes, grab the keywords...
1795 const char *end
; /* End of current value */
1798 while (*values
&& p
->num_auth_info_required
< 4)
1800 if ((end
= strchr(values
, ',')) == NULL
)
1801 end
= values
+ strlen(values
);
1803 if ((end
- values
) == 4 && !strncmp(values
, "none", 4))
1805 if (p
->num_auth_info_required
!= 0 || *end
)
1808 p
->auth_info_required
[p
->num_auth_info_required
] = "none";
1809 p
->num_auth_info_required
++;
1813 else if ((end
- values
) == 9 && !strncmp(values
, "negotiate", 9))
1815 if (p
->num_auth_info_required
!= 0 || *end
)
1818 p
->auth_info_required
[p
->num_auth_info_required
] = "negotiate";
1819 p
->num_auth_info_required
++;
1821 else if ((end
- values
) == 6 && !strncmp(values
, "domain", 6))
1823 p
->auth_info_required
[p
->num_auth_info_required
] = "domain";
1824 p
->num_auth_info_required
++;
1826 else if ((end
- values
) == 8 && !strncmp(values
, "password", 8))
1828 p
->auth_info_required
[p
->num_auth_info_required
] = "password";
1829 p
->num_auth_info_required
++;
1831 else if ((end
- values
) == 8 && !strncmp(values
, "username", 8))
1833 p
->auth_info_required
[p
->num_auth_info_required
] = "username";
1834 p
->num_auth_info_required
++;
1839 values
= (*end
) ? end
+ 1 : end
;
1842 if (p
->num_auth_info_required
== 0)
1844 p
->auth_info_required
[0] = "none";
1845 p
->num_auth_info_required
= 1;
1849 * Update the printer-type value as needed...
1852 if (p
->num_auth_info_required
> 1 ||
1853 strcmp(p
->auth_info_required
[0], "none"))
1854 p
->type
|= CUPS_PRINTER_AUTHENTICATED
;
1856 p
->type
&= ~CUPS_PRINTER_AUTHENTICATED
;
1862 * Grab values from an attribute instead...
1865 if (!attr
|| attr
->num_values
> 4)
1869 * Update the printer-type value as needed...
1872 if (attr
->num_values
> 1 ||
1873 strcmp(attr
->values
[0].string
.text
, "none"))
1874 p
->type
|= CUPS_PRINTER_AUTHENTICATED
;
1876 p
->type
&= ~CUPS_PRINTER_AUTHENTICATED
;
1878 for (i
= 0; i
< attr
->num_values
; i
++)
1880 if (!strcmp(attr
->values
[i
].string
.text
, "none"))
1882 if (p
->num_auth_info_required
!= 0 || attr
->num_values
!= 1)
1885 p
->auth_info_required
[p
->num_auth_info_required
] = "none";
1886 p
->num_auth_info_required
++;
1890 else if (!strcmp(attr
->values
[i
].string
.text
, "negotiate"))
1892 if (p
->num_auth_info_required
!= 0 || attr
->num_values
!= 1)
1895 p
->auth_info_required
[p
->num_auth_info_required
] = "negotiate";
1896 p
->num_auth_info_required
++;
1900 else if (!strcmp(attr
->values
[i
].string
.text
, "domain"))
1902 p
->auth_info_required
[p
->num_auth_info_required
] = "domain";
1903 p
->num_auth_info_required
++;
1905 else if (!strcmp(attr
->values
[i
].string
.text
, "password"))
1907 p
->auth_info_required
[p
->num_auth_info_required
] = "password";
1908 p
->num_auth_info_required
++;
1910 else if (!strcmp(attr
->values
[i
].string
.text
, "username"))
1912 p
->auth_info_required
[p
->num_auth_info_required
] = "username";
1913 p
->num_auth_info_required
++;
1924 * 'cupsdSetDeviceURI()' - Set the device URI for a printer.
1928 cupsdSetDeviceURI(cupsd_printer_t
*p
, /* I - Printer */
1929 const char *uri
) /* I - Device URI */
1931 char buffer
[1024], /* URI buffer */
1932 *start
, /* Start of data after scheme */
1933 *slash
, /* First slash after scheme:// */
1934 *ptr
; /* Pointer into user@host:port part */
1938 * Set the full device URI..
1941 cupsdSetString(&(p
->device_uri
), uri
);
1944 * Copy the device URI to a temporary buffer so we can sanitize any auth
1948 strlcpy(buffer
, uri
, sizeof(buffer
));
1951 * Find the end of the scheme:// part...
1954 if ((ptr
= strchr(buffer
, ':')) != NULL
)
1956 for (start
= ptr
+ 1; *start
; start
++)
1961 * Find the next slash (/) in the URI...
1964 if ((slash
= strchr(start
, '/')) == NULL
)
1965 slash
= start
+ strlen(start
); /* No slash, point to the end */
1968 * Check for an @ sign before the slash...
1971 if ((ptr
= strchr(start
, '@')) != NULL
&& ptr
< slash
)
1974 * Found an @ sign and it is before the resource part, so we have
1975 * an authentication string. Copy the remaining URI over the
1976 * authentication string...
1979 _cups_strcpy(start
, ptr
+ 1);
1984 * Save the sanitized URI...
1987 cupsdSetString(&(p
->sanitized_device_uri
), buffer
);
1992 * 'cupsdSetPrinterAttr()' - Set a printer attribute.
1996 cupsdSetPrinterAttr(
1997 cupsd_printer_t
*p
, /* I - Printer */
1998 const char *name
, /* I - Attribute name */
1999 char *value
) /* I - Attribute value string */
2001 ipp_attribute_t
*attr
; /* Attribute */
2002 int i
, /* Looping var */
2003 count
; /* Number of values */
2004 char *ptr
; /* Pointer into value */
2005 ipp_tag_t value_tag
; /* Value tag for this attribute */
2009 * Don't allow empty values...
2012 if (!*value
&& strcmp(name
, "marker-message"))
2014 cupsdLogMessage(CUPSD_LOG_ERROR
, "Ignoring empty \"%s\" attribute", name
);
2019 * Count the number of values...
2022 for (count
= 1, ptr
= value
;
2023 (ptr
= strchr(ptr
, ',')) != NULL
;
2027 * Then add or update the attribute as needed...
2030 if (!strcmp(name
, "marker-levels") || !strcmp(name
, "marker-low-levels") ||
2031 !strcmp(name
, "marker-high-levels"))
2037 if ((attr
= ippFindAttribute(p
->attrs
, name
, IPP_TAG_INTEGER
)) != NULL
&&
2038 attr
->num_values
< count
)
2040 ippDeleteAttribute(p
->attrs
, attr
);
2045 attr
->num_values
= count
;
2047 attr
= ippAddIntegers(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, name
,
2052 cupsdLogMessage(CUPSD_LOG_ERROR
,
2053 "Unable to allocate memory for printer attribute "
2054 "(%d values)", count
);
2058 for (i
= 0; i
< count
; i
++)
2060 if ((ptr
= strchr(value
, ',')) != NULL
)
2063 attr
->values
[i
].integer
= strtol(value
, NULL
, 10);
2072 * Name or keyword values...
2075 if (!strcmp(name
, "marker-types"))
2076 value_tag
= IPP_TAG_KEYWORD
;
2077 else if (!strcmp(name
, "marker-message"))
2078 value_tag
= IPP_TAG_TEXT
;
2080 value_tag
= IPP_TAG_NAME
;
2082 if ((attr
= ippFindAttribute(p
->attrs
, name
, value_tag
)) != NULL
&&
2083 attr
->num_values
< count
)
2085 ippDeleteAttribute(p
->attrs
, attr
);
2091 for (i
= 0; i
< attr
->num_values
; i
++)
2092 _cupsStrFree(attr
->values
[i
].string
.text
);
2094 attr
->num_values
= count
;
2097 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, value_tag
, name
,
2102 cupsdLogMessage(CUPSD_LOG_ERROR
,
2103 "Unable to allocate memory for printer attribute "
2104 "(%d values)", count
);
2108 for (i
= 0; i
< count
; i
++)
2110 if ((ptr
= strchr(value
, ',')) != NULL
)
2113 attr
->values
[i
].string
.text
= _cupsStrAlloc(value
);
2123 * 'cupsdSetPrinterAttrs()' - Set printer attributes based upon the PPD file.
2127 cupsdSetPrinterAttrs(cupsd_printer_t
*p
)/* I - Printer to setup */
2129 int i
, /* Looping var */
2130 length
; /* Length of browse attributes */
2131 char resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2132 int num_air
; /* Number of auth-info-required values */
2133 const char * const *air
; /* auth-info-required values */
2134 cupsd_location_t
*auth
; /* Pointer to authentication element */
2135 const char *auth_supported
; /* Authentication supported */
2136 ipp_t
*oldattrs
; /* Old printer attributes */
2137 ipp_attribute_t
*attr
; /* Attribute data */
2138 cups_option_t
*option
; /* Current printer option */
2139 char *filter
; /* Current filter */
2140 static const char * const air_userpass
[] =
2141 { /* Basic/Digest authentication */
2146 static const char * const air_negotiate
[] =
2147 { /* Kerberos authentication */
2150 #endif /* HAVE_GSSAPI */
2151 static const char * const air_none
[] =
2152 { /* No authentication */
2157 DEBUG_printf(("cupsdSetPrinterAttrs: entering name = %s, type = %x\n", p
->name
,
2161 * Make sure that we have the common attributes defined...
2165 cupsdCreateCommonData();
2168 * Clear out old filters, if any...
2171 delete_printer_filters(p
);
2174 * Figure out the authentication that is required for the printer.
2177 auth_supported
= "requesting-user-name";
2181 if (p
->num_auth_info_required
> 0 && strcmp(p
->auth_info_required
[0], "none"))
2183 num_air
= p
->num_auth_info_required
;
2184 air
= p
->auth_info_required
;
2186 if (!strcmp(air
[0], "username"))
2187 auth_supported
= "basic";
2189 auth_supported
= "negotiate";
2191 else if (!(p
->type
& CUPS_PRINTER_DISCOVERED
))
2193 if (p
->type
& CUPS_PRINTER_CLASS
)
2194 snprintf(resource
, sizeof(resource
), "/classes/%s", p
->name
);
2196 snprintf(resource
, sizeof(resource
), "/printers/%s", p
->name
);
2198 if ((auth
= cupsdFindBest(resource
, HTTP_POST
)) == NULL
||
2199 auth
->type
== CUPSD_AUTH_NONE
)
2200 auth
= cupsdFindPolicyOp(p
->op_policy_ptr
, IPP_PRINT_JOB
);
2204 int auth_type
; /* Authentication type */
2207 if ((auth_type
= auth
->type
) == CUPSD_AUTH_DEFAULT
)
2208 auth_type
= DefaultAuthType
;
2210 if (auth_type
== CUPSD_AUTH_BASIC
|| auth_type
== CUPSD_AUTH_BASICDIGEST
)
2212 auth_supported
= "basic";
2216 else if (auth_type
== CUPSD_AUTH_DIGEST
)
2218 auth_supported
= "digest";
2223 else if (auth_type
== CUPSD_AUTH_NEGOTIATE
)
2225 auth_supported
= "negotiate";
2227 air
= air_negotiate
;
2229 #endif /* HAVE_GSSAPI */
2231 if (auth_type
!= CUPSD_AUTH_NONE
)
2232 p
->type
|= CUPS_PRINTER_AUTHENTICATED
;
2234 p
->type
&= ~CUPS_PRINTER_AUTHENTICATED
;
2237 p
->type
&= ~CUPS_PRINTER_AUTHENTICATED
;
2239 else if (p
->type
& CUPS_PRINTER_AUTHENTICATED
)
2246 * Create the required IPP attributes for a printer...
2249 oldattrs
= p
->attrs
;
2250 p
->attrs
= ippNew();
2252 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2253 "uri-authentication-supported", NULL
, auth_supported
);
2254 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2255 "uri-security-supported", NULL
, "none");
2256 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "printer-name", NULL
,
2258 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-location",
2259 NULL
, p
->location
? p
->location
: "");
2260 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-info",
2261 NULL
, p
->info
? p
->info
: "");
2266 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2267 "requesting-user-name-denied", p
->num_users
, NULL
,
2270 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2271 "requesting-user-name-allowed", p
->num_users
, NULL
,
2275 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2276 "job-quota-period", p
->quota_period
);
2277 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2278 "job-k-limit", p
->k_limit
);
2279 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2280 "job-page-limit", p
->page_limit
);
2281 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2282 "auth-info-required", num_air
, NULL
, air
);
2284 if (cupsArrayCount(Banners
) > 0 && !(p
->type
& CUPS_PRINTER_DISCOVERED
))
2287 * Setup the job-sheets-default attribute...
2290 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2291 "job-sheets-default", 2, NULL
, NULL
);
2295 attr
->values
[0].string
.text
= _cupsStrAlloc(Classification
?
2296 Classification
: p
->job_sheets
[0]);
2297 attr
->values
[1].string
.text
= _cupsStrAlloc(Classification
?
2298 Classification
: p
->job_sheets
[1]);
2305 if (p
->type
& CUPS_PRINTER_DISCOVERED
)
2308 * Tell the client this is a remote printer of some type...
2311 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
2312 "printer-uri-supported", NULL
, p
->uri
);
2314 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "printer-more-info",
2318 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
2319 "printer-make-and-model", NULL
, p
->make_model
);
2321 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri", NULL
,
2330 * Assign additional attributes depending on whether this is a printer
2334 if (p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
2337 p
->type
&= ~CUPS_PRINTER_OPTIONS
;
2340 * Add class-specific attributes...
2343 if ((p
->type
& CUPS_PRINTER_IMPLICIT
) && p
->num_printers
> 0 &&
2344 p
->printers
[0]->make_model
)
2345 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
2346 "printer-make-and-model", NULL
, p
->printers
[0]->make_model
);
2348 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
2349 "printer-make-and-model", NULL
, "Local Printer Class");
2351 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri", NULL
,
2352 "file:///dev/null");
2354 if (p
->num_printers
> 0)
2357 * Add a list of member names; URIs are added in copy_printer_attrs...
2360 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2361 "member-names", p
->num_printers
, NULL
, NULL
);
2362 p
->type
|= CUPS_PRINTER_OPTIONS
;
2364 for (i
= 0; i
< p
->num_printers
; i
++)
2367 attr
->values
[i
].string
.text
= _cupsStrRetain(p
->printers
[i
]->name
);
2369 p
->type
&= ~CUPS_PRINTER_OPTIONS
| p
->printers
[i
]->type
;
2376 * Add printer-specific attributes...
2379 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri", NULL
,
2380 p
->sanitized_device_uri
);
2383 * Assign additional attributes from the PPD file (if any)...
2389 * Add filters for printer...
2392 cupsdSetPrinterReasons(p
, "-cups-missing-filter-warning,"
2393 "cups-insecure-filter-warning");
2395 for (filter
= (char *)cupsArrayFirst(p
->filters
);
2397 filter
= (char *)cupsArrayNext(p
->filters
))
2398 add_printer_filter(p
, p
->filetype
, filter
);
2402 p
->prefiltertype
= mimeAddType(MimeDatabase
, "prefilter", p
->name
);
2404 for (filter
= (char *)cupsArrayFirst(p
->pre_filters
);
2406 filter
= (char *)cupsArrayNext(p
->pre_filters
))
2407 add_printer_filter(p
, p
->prefiltertype
, filter
);
2413 * Copy marker attributes as needed...
2418 ipp_attribute_t
*oldattr
; /* Old attribute */
2421 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-colors",
2422 IPP_TAG_NAME
)) != NULL
)
2424 if ((attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2425 "marker-colors", oldattr
->num_values
, NULL
,
2428 for (i
= 0; i
< oldattr
->num_values
; i
++)
2429 attr
->values
[i
].string
.text
=
2430 _cupsStrRetain(oldattr
->values
[i
].string
.text
);
2434 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-levels",
2435 IPP_TAG_INTEGER
)) != NULL
)
2437 if ((attr
= ippAddIntegers(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2438 "marker-levels", oldattr
->num_values
,
2441 for (i
= 0; i
< oldattr
->num_values
; i
++)
2442 attr
->values
[i
].integer
= oldattr
->values
[i
].integer
;
2446 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-message",
2447 IPP_TAG_TEXT
)) != NULL
)
2448 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "marker-message",
2449 NULL
, oldattr
->values
[0].string
.text
);
2451 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-low-levels",
2452 IPP_TAG_INTEGER
)) != NULL
)
2454 if ((attr
= ippAddIntegers(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2455 "marker-low-levels", oldattr
->num_values
,
2458 for (i
= 0; i
< oldattr
->num_values
; i
++)
2459 attr
->values
[i
].integer
= oldattr
->values
[i
].integer
;
2463 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-high-levels",
2464 IPP_TAG_INTEGER
)) != NULL
)
2466 if ((attr
= ippAddIntegers(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2467 "marker-high-levels", oldattr
->num_values
,
2470 for (i
= 0; i
< oldattr
->num_values
; i
++)
2471 attr
->values
[i
].integer
= oldattr
->values
[i
].integer
;
2475 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-names",
2476 IPP_TAG_NAME
)) != NULL
)
2478 if ((attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2479 "marker-names", oldattr
->num_values
, NULL
,
2482 for (i
= 0; i
< oldattr
->num_values
; i
++)
2483 attr
->values
[i
].string
.text
=
2484 _cupsStrRetain(oldattr
->values
[i
].string
.text
);
2488 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-types",
2489 IPP_TAG_KEYWORD
)) != NULL
)
2491 if ((attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2492 "marker-types", oldattr
->num_values
, NULL
,
2495 for (i
= 0; i
< oldattr
->num_values
; i
++)
2496 attr
->values
[i
].string
.text
=
2497 _cupsStrRetain(oldattr
->values
[i
].string
.text
);
2501 ippDelete(oldattrs
);
2505 * Force sharing off for remote queues...
2508 if (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
))
2513 * Copy the printer options into a browse attributes string we can re-use.
2516 const char *valptr
; /* Pointer into value */
2517 char *attrptr
; /* Pointer into attribute string */
2521 * Free the old browse attributes as needed...
2524 if (p
->browse_attrs
)
2525 free(p
->browse_attrs
);
2528 * Compute the length of all attributes + job-sheets, lease-duration,
2529 * and BrowseLocalOptions.
2532 for (length
= 1, i
= p
->num_options
, option
= p
->options
;
2536 length
+= strlen(option
->name
) + 2;
2540 for (valptr
= option
->value
; *valptr
; valptr
++)
2541 if (strchr(" \"\'\\", *valptr
))
2548 length
+= 13 + strlen(p
->job_sheets
[0]) + strlen(p
->job_sheets
[1]);
2550 if (BrowseLocalOptions
)
2551 length
+= 12 + strlen(BrowseLocalOptions
);
2553 if (p
->num_auth_info_required
> 0)
2555 length
+= 18; /* auth-info-required */
2557 for (i
= 0; i
< p
->num_auth_info_required
; i
++)
2558 length
+= strlen(p
->auth_info_required
[i
]) + 1;
2562 * Allocate the new string...
2565 if ((p
->browse_attrs
= calloc(1, length
)) == NULL
)
2566 cupsdLogMessage(CUPSD_LOG_ERROR
,
2567 "Unable to allocate %d bytes for browse data!",
2572 * Got the allocated string, now copy the options and attributes over...
2575 sprintf(p
->browse_attrs
, "job-sheets=%s,%s lease-duration=%d",
2576 p
->job_sheets
[0], p
->job_sheets
[1], BrowseTimeout
);
2577 attrptr
= p
->browse_attrs
+ strlen(p
->browse_attrs
);
2579 if (BrowseLocalOptions
)
2581 sprintf(attrptr
, " ipp-options=%s", BrowseLocalOptions
);
2582 attrptr
+= strlen(attrptr
);
2585 for (i
= p
->num_options
, option
= p
->options
;
2590 strcpy(attrptr
, option
->name
);
2591 attrptr
+= strlen(attrptr
);
2597 for (valptr
= option
->value
; *valptr
; valptr
++)
2599 if (strchr(" \"\'\\", *valptr
))
2602 *attrptr
++ = *valptr
;
2607 if (p
->num_auth_info_required
> 0)
2609 strcpy(attrptr
, "auth-info-required");
2612 for (i
= 0; i
< p
->num_auth_info_required
; i
++)
2614 *attrptr
++ = i
? ',' : '=';
2615 strcpy(attrptr
, p
->auth_info_required
[i
]);
2616 attrptr
+= strlen(attrptr
);
2625 * Populate the document-format-supported attribute...
2628 add_printer_formats(p
);
2630 DEBUG_printf(("cupsdSetPrinterAttrs: leaving name = %s, type = %x\n", p
->name
,
2634 * Add name-default attributes...
2637 add_printer_defaults(p
);
2641 * Write the IRIX printer config and status files...
2644 write_irix_config(p
);
2645 write_irix_state(p
);
2649 * Let the browse protocols reflect the change
2652 cupsdRegisterPrinter(p
);
2657 * 'cupsdSetPrinterReasons()' - Set/update the reasons strings.
2660 int /* O - 1 if something changed, 0 otherwise */
2661 cupsdSetPrinterReasons(
2662 cupsd_printer_t
*p
, /* I - Printer */
2663 const char *s
) /* I - Reasons strings */
2665 int i
, /* Looping var */
2666 changed
= 0; /* Did something change? */
2667 const char *sptr
; /* Pointer into reasons */
2668 char reason
[255], /* Reason string */
2669 *rptr
; /* Pointer into reason */
2672 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2673 "cupsdSetPrinterReasons(p=%p(%s),s=\"%s\"", p
, p
->name
, s
);
2675 if (s
[0] == '-' || s
[0] == '+')
2678 * Add/remove reasons...
2686 * Replace reasons...
2691 for (i
= 0; i
< p
->num_reasons
; i
++)
2692 _cupsStrFree(p
->reasons
[i
]);
2697 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
2699 if (PrintcapFormat
== PRINTCAP_PLIST
)
2700 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2703 if (!strcmp(s
, "none"))
2707 * Loop through all of the reasons...
2713 * Skip leading whitespace and commas...
2716 while (isspace(*sptr
& 255) || *sptr
== ',')
2719 for (rptr
= reason
; *sptr
&& !isspace(*sptr
& 255) && *sptr
!= ','; sptr
++)
2720 if (rptr
< (reason
+ sizeof(reason
) - 1))
2734 for (i
= 0; i
< p
->num_reasons
; i
++)
2735 if (!strcmp(reason
, p
->reasons
[i
]))
2738 * Found a match, so remove it...
2743 _cupsStrFree(p
->reasons
[i
]);
2745 if (i
< p
->num_reasons
)
2746 memmove(p
->reasons
+ i
, p
->reasons
+ i
+ 1,
2747 (p
->num_reasons
- i
) * sizeof(char *));
2749 if (!strcmp(reason
, "paused") && p
->state
== IPP_PRINTER_STOPPED
)
2750 cupsdSetPrinterState(p
, IPP_PRINTER_IDLE
, 1);
2752 if (strcmp(reason
, "connecting-to-device"))
2753 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
2755 if (PrintcapFormat
== PRINTCAP_PLIST
)
2756 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2761 else if (p
->num_reasons
< (int)(sizeof(p
->reasons
) / sizeof(p
->reasons
[0])))
2767 for (i
= 0; i
< p
->num_reasons
; i
++)
2768 if (!strcmp(reason
, p
->reasons
[i
]))
2771 if (i
>= p
->num_reasons
)
2773 if (i
>= (int)(sizeof(p
->reasons
) / sizeof(p
->reasons
[0])))
2775 cupsdLogMessage(CUPSD_LOG_ALERT
,
2776 "Too many printer-state-reasons values for %s (%d)",
2781 p
->reasons
[i
] = _cupsStrAlloc(reason
);
2785 if (!strcmp(reason
, "paused") && p
->state
!= IPP_PRINTER_STOPPED
)
2786 cupsdSetPrinterState(p
, IPP_PRINTER_STOPPED
, 1);
2788 if (strcmp(reason
, "connecting-to-device"))
2789 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
2791 if (PrintcapFormat
== PRINTCAP_PLIST
)
2792 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2802 * 'cupsdSetPrinterState()' - Update the current state of a printer.
2806 cupsdSetPrinterState(
2807 cupsd_printer_t
*p
, /* I - Printer to change */
2808 ipp_pstate_t s
, /* I - New state */
2809 int update
) /* I - Update printers.conf? */
2811 ipp_pstate_t old_state
; /* Old printer state */
2815 * Can't set status of remote printers...
2818 if (p
->type
& CUPS_PRINTER_DISCOVERED
)
2822 * Set the new state...
2825 if (PrintcapFormat
== PRINTCAP_PLIST
)
2826 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2828 old_state
= p
->state
;
2833 cupsdAddEvent(s
== IPP_PRINTER_STOPPED
? CUPSD_EVENT_PRINTER_STOPPED
:
2834 CUPSD_EVENT_PRINTER_STATE
, p
, NULL
,
2835 "%s \"%s\" state changed.",
2836 (p
->type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
2840 * Let the browse code know this needs to be updated...
2844 p
->state_time
= time(NULL
);
2848 write_irix_state(p
);
2853 * Set/clear the paused reason as needed...
2856 if (s
== IPP_PRINTER_STOPPED
)
2857 cupsdSetPrinterReasons(p
, "+paused");
2859 cupsdSetPrinterReasons(p
, "-paused");
2862 * Clear the message for the queue when going to processing...
2865 if (s
== IPP_PRINTER_PROCESSING
)
2866 p
->state_message
[0] = '\0';
2869 * Update the printer history...
2872 cupsdAddPrinterHistory(p
);
2875 * Let the browse protocols reflect the change...
2879 cupsdRegisterPrinter(p
);
2882 * Save the printer configuration if a printer goes from idle or processing
2883 * to stopped (or visa-versa)...
2887 (old_state
== IPP_PRINTER_STOPPED
) != (s
== IPP_PRINTER_STOPPED
))
2889 if (p
->type
& CUPS_PRINTER_CLASS
)
2890 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
2892 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
2898 * 'cupsdStopPrinter()' - Stop a printer from printing any jobs...
2902 cupsdStopPrinter(cupsd_printer_t
*p
, /* I - Printer to stop */
2903 int update
)/* I - Update printers.conf? */
2906 * Set the printer state...
2909 cupsdSetPrinterState(p
, IPP_PRINTER_STOPPED
, update
);
2912 * See if we have a job printing on this printer...
2915 if (p
->job
&& p
->job
->state_value
== IPP_JOB_PROCESSING
)
2916 cupsdSetJobState(p
->job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
2917 "Job stopped due to printer being paused.");
2922 * 'cupsdUpdatePrinterPPD()' - Update keywords in a printer's PPD file.
2925 int /* O - 1 if successful, 0 otherwise */
2926 cupsdUpdatePrinterPPD(
2927 cupsd_printer_t
*p
, /* I - Printer */
2928 int num_keywords
, /* I - Number of keywords */
2929 cups_option_t
*keywords
) /* I - Keywords */
2931 int i
; /* Looping var */
2932 cups_file_t
*src
, /* Original file */
2933 *dst
; /* New file */
2934 char srcfile
[1024], /* Original filename */
2935 dstfile
[1024], /* New filename */
2936 line
[1024], /* Line from file */
2937 keystring
[41]; /* Keyword from line */
2938 cups_option_t
*keyword
; /* Current keyword */
2941 cupsdLogMessage(CUPSD_LOG_INFO
, "Updating keywords in PPD file for %s...",
2945 * Get the old and new PPD filenames...
2948 snprintf(srcfile
, sizeof(srcfile
), "%s/ppd/%s.ppd.O", ServerRoot
, p
->name
);
2949 snprintf(dstfile
, sizeof(srcfile
), "%s/ppd/%s.ppd", ServerRoot
, p
->name
);
2952 * Rename the old file and open the old and new...
2955 if (rename(dstfile
, srcfile
))
2957 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to backup PPD file for %s: %s",
2958 p
->name
, strerror(errno
));
2962 if ((src
= cupsFileOpen(srcfile
, "r")) == NULL
)
2964 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open PPD file \"%s\": %s",
2965 srcfile
, strerror(errno
));
2966 rename(srcfile
, dstfile
);
2970 if ((dst
= cupsFileOpen(dstfile
, "w")) == NULL
)
2972 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create PPD file \"%s\": %s",
2973 dstfile
, strerror(errno
));
2975 rename(srcfile
, dstfile
);
2980 * Copy the first line and then write out all of the keywords...
2983 if (!cupsFileGets(src
, line
, sizeof(line
)))
2985 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to read PPD file \"%s\": %s",
2986 srcfile
, strerror(errno
));
2989 rename(srcfile
, dstfile
);
2993 cupsFilePrintf(dst
, "%s\n", line
);
2995 for (i
= num_keywords
, keyword
= keywords
; i
> 0; i
--, keyword
++)
2997 cupsdLogMessage(CUPSD_LOG_DEBUG
, "*%s: %s", keyword
->name
, keyword
->value
);
2998 cupsFilePrintf(dst
, "*%s: %s\n", keyword
->name
, keyword
->value
);
3002 * Then copy the rest of the PPD file, dropping any keywords we changed.
3005 while (cupsFileGets(src
, line
, sizeof(line
)))
3008 * Skip keywords we've already set...
3011 if (sscanf(line
, "*%40[^:]:", keystring
) == 1 &&
3012 cupsGetOption(keystring
, num_keywords
, keywords
))
3016 * Otherwise write the line...
3019 cupsFilePrintf(dst
, "%s\n", line
);
3023 * Close files and return...
3034 * 'cupsdUpdatePrinters()' - Update printers after a partial reload.
3038 cupsdUpdatePrinters(void)
3040 cupsd_printer_t
*p
; /* Current printer */
3044 * Loop through the printers and recreate the printer attributes
3045 * for any local printers since the policy and/or access control
3046 * stuff may have changed. Also, if browsing is disabled, remove
3047 * any remote printers...
3050 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3052 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3055 * Remove remote printers if we are no longer browsing...
3059 (p
->type
& (CUPS_PRINTER_IMPLICIT
| CUPS_PRINTER_DISCOVERED
)))
3061 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
3062 cupsArrayRemove(ImplicitPrinters
, p
);
3064 cupsArraySave(Printers
);
3065 cupsdDeletePrinter(p
, 0);
3066 cupsArrayRestore(Printers
);
3071 * Update the operation policy pointer...
3074 if ((p
->op_policy_ptr
= cupsdFindPolicy(p
->op_policy
)) == NULL
)
3075 p
->op_policy_ptr
= DefaultPolicyPtr
;
3078 * Update printer attributes as needed...
3081 if (!(p
->type
& CUPS_PRINTER_DISCOVERED
))
3082 cupsdSetPrinterAttrs(p
);
3088 * 'cupsdValidateDest()' - Validate a printer/class destination.
3091 const char * /* O - Printer or class name */
3093 const char *uri
, /* I - Printer URI */
3094 cups_ptype_t
*dtype
, /* O - Type (printer or class) */
3095 cupsd_printer_t
**printer
) /* O - Printer pointer */
3097 cupsd_printer_t
*p
; /* Current printer */
3098 char localname
[1024],/* Localized hostname */
3099 *lptr
, /* Pointer into localized hostname */
3100 *sptr
, /* Pointer into server name */
3101 *rptr
, /* Pointer into resource */
3102 scheme
[32], /* Scheme portion of URI */
3103 username
[64], /* Username portion of URI */
3104 hostname
[HTTP_MAX_HOST
],
3105 /* Host portion of URI */
3106 resource
[HTTP_MAX_URI
];
3107 /* Resource portion of URI */
3108 int port
; /* Port portion of URI */
3111 DEBUG_printf(("cupsdValidateDest(uri=\"%s\", dtype=%p, printer=%p)\n", uri
,
3115 * Initialize return values...
3122 *dtype
= (cups_ptype_t
)0;
3125 * Pull the hostname and resource from the URI...
3128 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
3129 username
, sizeof(username
), hostname
, sizeof(hostname
),
3130 &port
, resource
, sizeof(resource
));
3133 * See if the resource is a class or printer...
3136 if (!strncmp(resource
, "/classes/", 9))
3142 rptr
= resource
+ 9;
3144 else if (!strncmp(resource
, "/printers/", 10))
3150 rptr
= resource
+ 10;
3155 * Bad resource name...
3162 * See if the printer or class name exists...
3165 p
= cupsdFindDest(rptr
);
3167 if (p
== NULL
&& strchr(rptr
, '@') == NULL
)
3175 *dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
|
3176 CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
);
3182 * Change localhost to the server name...
3185 if (!strcasecmp(hostname
, "localhost"))
3186 strlcpy(hostname
, ServerName
, sizeof(hostname
));
3188 strlcpy(localname
, hostname
, sizeof(localname
));
3190 if (!strcasecmp(hostname
, ServerName
))
3193 * Localize the hostname...
3196 lptr
= strchr(localname
, '.');
3197 sptr
= strchr(ServerName
, '.');
3199 if (sptr
!= NULL
&& lptr
!= NULL
)
3202 * Strip the common domain name components...
3205 while (lptr
!= NULL
)
3207 if (!strcasecmp(lptr
, sptr
))
3213 lptr
= strchr(lptr
+ 1, '.');
3218 DEBUG_printf(("localized hostname is \"%s\"...\n", localname
));
3221 * Find a matching printer or class...
3224 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3226 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3227 if (!strcasecmp(p
->hostname
, localname
) &&
3228 !strcasecmp(p
->name
, rptr
))
3234 *dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
|
3235 CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
);
3245 * 'cupsdWritePrintcap()' - Write a pseudo-printcap file for older applications
3250 cupsdWritePrintcap(void)
3252 int i
; /* Looping var */
3253 cups_file_t
*fp
; /* Printcap file */
3254 cupsd_printer_t
*p
; /* Current printer */
3259 * Update the IRIX printer state for the default printer; if
3260 * no printers remain, then the default printer file will be
3264 write_irix_state(DefaultPrinter
);
3268 * See if we have a printcap file; if not, don't bother writing it.
3271 if (!Printcap
|| !*Printcap
)
3275 * Open the printcap file...
3278 if ((fp
= cupsFileOpen(Printcap
, "w")) == NULL
)
3282 * Put a comment header at the top so that users will know where the
3283 * data has come from...
3286 if (PrintcapFormat
!= PRINTCAP_PLIST
)
3287 cupsFilePrintf(fp
, "# This file was automatically generated by cupsd(8) "
3289 "# %s/printers.conf file. All changes to this file\n"
3290 "# will be lost.\n", ServerRoot
);
3293 * Write a new printcap with the current list of printers.
3296 switch (PrintcapFormat
)
3300 * Each printer is put in the file as:
3310 cupsFilePrintf(fp
, "%s|%s:rm=%s:rp=%s:\n", DefaultPrinter
->name
,
3311 DefaultPrinter
->info
, ServerName
,
3312 DefaultPrinter
->name
);
3314 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3316 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3317 if (p
!= DefaultPrinter
)
3318 cupsFilePrintf(fp
, "%s|%s:rm=%s:rp=%s:\n", p
->name
, p
->info
,
3319 ServerName
, p
->name
);
3322 case PRINTCAP_PLIST
:
3324 * Each printer is written as a dictionary in a plist file.
3325 * Currently the printer-name, printer-info, printer-is-accepting-jobs,
3326 * printer-location, printer-make-and-model, printer-state,
3327 * printer-state-reasons, printer-type, and (sanitized) device-uri.
3330 cupsFilePuts(fp
, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
3331 "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD "
3332 "PLIST 1.0//EN\" \"http://www.apple.com/DTDs/"
3333 "PropertyList-1.0.dtd\">\n"
3334 "<plist version=\"1.0\">\n"
3337 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3339 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3341 cupsFilePuts(fp
, "\t<dict>\n"
3342 "\t\t<key>printer-name</key>\n"
3344 write_xml_string(fp
, p
->name
);
3345 cupsFilePuts(fp
, "</string>\n"
3346 "\t\t<key>printer-info</key>\n"
3348 write_xml_string(fp
, p
->info
);
3349 cupsFilePrintf(fp
, "</string>\n"
3350 "\t\t<key>printer-is-accepting-jobs</key>\n"
3352 "\t\t<key>printer-location</key>\n"
3353 "\t\t<string>", p
->accepting
? "true" : "false");
3354 write_xml_string(fp
, p
->location
);
3355 cupsFilePuts(fp
, "</string>\n"
3356 "\t\t<key>printer-make-and-model</key>\n"
3358 write_xml_string(fp
, p
->make_model
);
3359 cupsFilePrintf(fp
, "</string>\n"
3360 "\t\t<key>printer-state</key>\n"
3361 "\t\t<integer>%d</integer>\n"
3362 "\t\t<key>printer-state-reasons</key>\n"
3363 "\t\t<array>\n", p
->state
);
3364 for (i
= 0; i
< p
->num_reasons
; i
++)
3366 cupsFilePuts(fp
, "\t\t\t<string>");
3367 write_xml_string(fp
, p
->reasons
[i
]);
3368 cupsFilePuts(fp
, "</string>\n");
3370 cupsFilePrintf(fp
, "\t\t</array>\n"
3371 "\t\t<key>printer-type</key>\n"
3372 "\t\t<integer>%d</integer>\n"
3373 "\t\t<key>device-uri</key>\n"
3374 "\t\t<string>", p
->type
);
3375 write_xml_string(fp
, p
->sanitized_device_uri
);
3376 cupsFilePuts(fp
, "</string>\n"
3379 cupsFilePuts(fp
, "</array>\n"
3383 case PRINTCAP_SOLARIS
:
3385 * Each printer is put in the file as:
3387 * _all:all=Printer1,Printer2,Printer3,...,PrinterN
3388 * _default:use=DefaultPrinter
3390 * :bsdaddr=ServerName,Printer1:\
3391 * :description=Description:
3393 * :bsdaddr=ServerName,Printer2:\
3394 * :description=Description:
3396 * :bsdaddr=ServerName,Printer3:\
3397 * :description=Description:
3400 * :bsdaddr=ServerName,PrinterN:\
3401 * :description=Description:
3404 cupsFilePuts(fp
, "_all:all=");
3405 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3407 p
= (cupsd_printer_t
*)cupsArrayCurrent(Printers
))
3408 cupsFilePrintf(fp
, "%s%c", p
->name
,
3409 cupsArrayNext(Printers
) ? ',' : '\n');
3412 cupsFilePrintf(fp
, "_default:use=%s\n", DefaultPrinter
->name
);
3414 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3416 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3417 cupsFilePrintf(fp
, "%s:\\\n"
3418 "\t:bsdaddr=%s,%s:\\\n"
3419 "\t:description=%s:\n",
3420 p
->name
, ServerName
, p
->name
,
3421 p
->info
? p
->info
: "");
3434 * 'add_printer_defaults()' - Add name-default attributes to the printer attributes.
3438 add_printer_defaults(cupsd_printer_t
*p
)/* I - Printer */
3440 int i
; /* Looping var */
3441 int num_options
; /* Number of default options */
3442 cups_option_t
*options
, /* Default options */
3443 *option
; /* Current option */
3444 char name
[256]; /* name-default */
3448 * Maintain a common array of default attribute names...
3451 if (!CommonDefaults
)
3453 CommonDefaults
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
3455 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("copies-default"));
3456 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("document-format-default"));
3457 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("finishings-default"));
3458 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("job-hold-until-default"));
3459 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("job-priority-default"));
3460 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("job-sheets-default"));
3461 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("media-default"));
3462 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("number-up-default"));
3463 cupsArrayAdd(CommonDefaults
,
3464 _cupsStrAlloc("orientation-requested-default"));
3465 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("sides-default"));
3469 * Add all of the default options from the .conf files...
3472 for (num_options
= 0, options
= NULL
, i
= p
->num_options
, option
= p
->options
;
3476 if (strcmp(option
->name
, "ipp-options") &&
3477 strcmp(option
->name
, "job-sheets") &&
3478 strcmp(option
->name
, "lease-duration"))
3480 snprintf(name
, sizeof(name
), "%s-default", option
->name
);
3481 num_options
= cupsAddOption(name
, option
->value
, num_options
, &options
);
3483 if (!cupsArrayFind(CommonDefaults
, name
))
3484 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc(name
));
3489 * Convert options to IPP attributes...
3492 cupsEncodeOptions2(p
->attrs
, num_options
, options
, IPP_TAG_PRINTER
);
3493 cupsFreeOptions(num_options
, options
);
3496 * Add standard -default attributes as needed...
3499 if (!cupsGetOption("copies", p
->num_options
, p
->options
))
3500 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "copies-default",
3503 if (!cupsGetOption("document-format", p
->num_options
, p
->options
))
3504 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_MIMETYPE
,
3505 "document-format-default", NULL
, "application/octet-stream");
3507 if (!cupsGetOption("job-hold-until", p
->num_options
, p
->options
))
3508 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
3509 "job-hold-until-default", NULL
, "no-hold");
3511 if (!cupsGetOption("job-priority", p
->num_options
, p
->options
))
3512 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
3513 "job-priority-default", 50);
3515 if (!cupsGetOption("number-up", p
->num_options
, p
->options
))
3516 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
3517 "number-up-default", 1);
3519 if (!cupsGetOption("orientation-requested", p
->num_options
, p
->options
))
3520 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NOVALUE
,
3521 "orientation-requested-default", NULL
, NULL
);
3523 if (!cupsGetOption("notify-lease-duration", p
->num_options
, p
->options
))
3524 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
3525 "notify-lease-duration-default", DefaultLeaseDuration
);
3527 if (!cupsGetOption("notify-events", p
->num_options
, p
->options
))
3528 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
3529 "notify-events-default", NULL
, "job-completed");
3534 * 'add_printer_filter()' - Add a MIME filter for a printer.
3539 cupsd_printer_t
*p
, /* I - Printer to add to */
3540 mime_type_t
*filtertype
, /* I - Filter or prefilter MIME type */
3541 const char *filter
) /* I - Filter to add */
3543 char super
[MIME_MAX_SUPER
], /* Super-type for filter */
3544 type
[MIME_MAX_TYPE
], /* Type for filter */
3545 program
[1024]; /* Program/filter name */
3546 int cost
; /* Cost of filter */
3547 mime_type_t
*temptype
; /* MIME type looping var */
3548 char filename
[1024], /* Full filter filename */
3549 *dirsep
; /* Pointer to directory separator */
3550 struct stat fileinfo
; /* File information */
3554 * Parse the filter string; it should be in the following format:
3556 * super/type cost program
3559 if (sscanf(filter
, "%15[^/]/%31s%d%*[ \t]%1023[^\n]", super
, type
, &cost
,
3562 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s: invalid filter string \"%s\"!",
3568 * See if the filter program exists; if not, stop the printer and flag
3572 if (strcmp(program
, "-"))
3574 if (program
[0] == '/')
3575 strlcpy(filename
, program
, sizeof(filename
));
3577 snprintf(filename
, sizeof(filename
), "%s/filter/%s", ServerBin
, program
);
3579 if (stat(filename
, &fileinfo
))
3581 memset(&fileinfo
, 0, sizeof(fileinfo
));
3583 snprintf(p
->state_message
, sizeof(p
->state_message
),
3584 "Filter \"%s\" for printer \"%s\" not available: %s",
3585 filename
, p
->name
, strerror(errno
));
3586 cupsdSetPrinterReasons(p
, "+cups-missing-filter-warning");
3588 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s", p
->state_message
);
3592 * When running as root, do additional security checks...
3598 * Only use filters that are owned by root and do not have world write
3602 if (fileinfo
.st_uid
|| (fileinfo
.st_mode
& (S_ISUID
| S_IWOTH
)) != 0)
3604 if (fileinfo
.st_uid
)
3605 snprintf(p
->state_message
, sizeof(p
->state_message
),
3606 "Filter \"%s\" for printer \"%s\" not owned by root",
3609 snprintf(p
->state_message
, sizeof(p
->state_message
),
3610 "Filter \"%s\" for printer \"%s\" has insecure permissions "
3611 "(0%o)", filename
, p
->name
, fileinfo
.st_mode
);
3613 cupsdSetPrinterReasons(p
, "+cups-insecure-filter-warning");
3615 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s", p
->state_message
);
3617 else if (fileinfo
.st_mode
)
3620 * Similarly, check that the parent directory is also owned by root and
3621 * does not have world write permissions.
3624 if ((dirsep
= strrchr(filename
, '/')) != NULL
)
3627 if (!stat(filename
, &fileinfo
) &&
3629 (fileinfo
.st_mode
& (S_ISUID
| S_IWOTH
)) != 0))
3631 if (fileinfo
.st_uid
)
3632 snprintf(p
->state_message
, sizeof(p
->state_message
),
3633 "Filter directory \"%s\" for printer \"%s\" not owned by "
3634 "root", filename
, p
->name
);
3636 snprintf(p
->state_message
, sizeof(p
->state_message
),
3637 "Filter directory \"%s\" for printer \"%s\" has insecure "
3638 "permissions (0%o)", filename
, p
->name
, fileinfo
.st_mode
);
3640 cupsdSetPrinterReasons(p
, "+cups-insecure-filter-warning");
3642 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s", p
->state_message
);
3649 * Add the filter to the MIME database, supporting wildcards as needed...
3652 for (temptype
= mimeFirstType(MimeDatabase
);
3654 temptype
= mimeNextType(MimeDatabase
))
3655 if (((super
[0] == '*' && strcasecmp(temptype
->super
, "printer")) ||
3656 !strcasecmp(temptype
->super
, super
)) &&
3657 (type
[0] == '*' || !strcasecmp(temptype
->type
, type
)))
3659 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3660 "add_printer_filter: %s: adding filter %s/%s %s/%s %d %s",
3661 p
->name
, temptype
->super
, temptype
->type
,
3662 filtertype
->super
, filtertype
->type
,
3664 mimeAddFilter(MimeDatabase
, temptype
, filtertype
, cost
, program
);
3670 * 'add_printer_formats()' - Add document-format-supported values for a printer.
3674 add_printer_formats(cupsd_printer_t
*p
) /* I - Printer */
3676 int i
; /* Looping var */
3677 mime_type_t
*type
; /* Current MIME type */
3678 cups_array_t
*filters
; /* Filters */
3679 ipp_attribute_t
*attr
; /* document-format-supported attribute */
3680 char mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
3681 /* MIME type name */
3685 * Raw (and remote) queues advertise all of the supported MIME
3689 cupsArrayDelete(p
->filetypes
);
3690 p
->filetypes
= NULL
;
3694 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
,
3695 (ipp_tag_t
)(IPP_TAG_MIMETYPE
| IPP_TAG_COPY
),
3696 "document-format-supported", NumMimeTypes
, NULL
, MimeTypes
);
3701 * Otherwise, loop through the supported MIME types and see if there
3702 * are filters for them...
3705 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_printer_formats: %d types, %d filters",
3706 mimeNumTypes(MimeDatabase
), mimeNumFilters(MimeDatabase
));
3708 p
->filetypes
= cupsArrayNew(NULL
, NULL
);
3710 for (type
= mimeFirstType(MimeDatabase
);
3712 type
= mimeNextType(MimeDatabase
))
3714 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", type
->super
, type
->type
);
3716 if ((filters
= mimeFilter(MimeDatabase
, type
, p
->filetype
, NULL
)) != NULL
)
3718 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3719 "add_printer_formats: %s: %s needs %d filters",
3720 p
->name
, mimetype
, cupsArrayCount(filters
));
3722 cupsArrayDelete(filters
);
3723 cupsArrayAdd(p
->filetypes
, type
);
3726 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3727 "add_printer_formats: %s: %s not supported",
3731 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3732 "add_printer_formats: %s: %d supported types",
3733 p
->name
, cupsArrayCount(p
->filetypes
) + 1);
3736 * Add the file formats that can be filtered...
3739 if ((type
= mimeType(MimeDatabase
, "application", "octet-stream")) == NULL
||
3740 !cupsArrayFind(p
->filetypes
, type
))
3745 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_MIMETYPE
,
3746 "document-format-supported",
3747 cupsArrayCount(p
->filetypes
) + 1, NULL
, NULL
);
3750 attr
->values
[0].string
.text
= _cupsStrAlloc("application/octet-stream");
3752 for (type
= (mime_type_t
*)cupsArrayFirst(p
->filetypes
);
3754 i
++, type
= (mime_type_t
*)cupsArrayNext(p
->filetypes
))
3756 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", type
->super
, type
->type
);
3758 attr
->values
[i
].string
.text
= _cupsStrAlloc(mimetype
);
3763 char pdl
[1024]; /* Buffer to build pdl list */
3764 mime_filter_t
*filter
; /* MIME filter looping var */
3769 if (mimeType(MimeDatabase
, "application", "pdf"))
3770 strlcat(pdl
, "application/pdf,", sizeof(pdl
));
3772 if (mimeType(MimeDatabase
, "application", "postscript"))
3773 strlcat(pdl
, "application/postscript,", sizeof(pdl
));
3775 if (mimeType(MimeDatabase
, "application", "vnd.cups-raster"))
3776 strlcat(pdl
, "application/vnd.cups-raster,", sizeof(pdl
));
3779 * Determine if this is a Tioga PrintJobMgr based queue...
3782 for (filter
= (mime_filter_t
*)cupsArrayFirst(MimeDatabase
->filters
);
3784 filter
= (mime_filter_t
*)cupsArrayNext(MimeDatabase
->filters
))
3786 if (filter
->dst
== p
->filetype
&& filter
->filter
&&
3787 strstr(filter
->filter
, "PrintJobMgr"))
3792 * We only support raw printing if this is not a Tioga PrintJobMgr based
3793 * queue and if application/octet-stream is a known conversion...
3796 if (!filter
&& mimeType(MimeDatabase
, "application", "octet-stream"))
3797 strlcat(pdl
, "application/octet-stream,", sizeof(pdl
));
3799 if (mimeType(MimeDatabase
, "image", "png"))
3800 strlcat(pdl
, "image/png,", sizeof(pdl
));
3803 pdl
[strlen(pdl
) - 1] = '\0'; /* Remove trailing comma */
3805 cupsdSetString(&p
->pdl
, pdl
);
3807 #endif /* HAVE_DNSSD */
3812 * 'add_string_array()' - Add a string to an array of CUPS strings.
3816 add_string_array(cups_array_t
**a
, /* I - Array */
3817 const char *s
) /* I - String */
3820 *a
= cupsArrayNew(NULL
, NULL
);
3822 cupsArrayAdd(*a
, _cupsStrAlloc(s
));
3827 * 'compare_printers()' - Compare two printers.
3830 static int /* O - Result of comparison */
3831 compare_printers(void *first
, /* I - First printer */
3832 void *second
, /* I - Second printer */
3833 void *data
) /* I - App data (not used) */
3835 return (strcasecmp(((cupsd_printer_t
*)first
)->name
,
3836 ((cupsd_printer_t
*)second
)->name
));
3841 * 'delete_printer_filters()' - Delete all MIME filters for a printer.
3845 delete_printer_filters(
3846 cupsd_printer_t
*p
) /* I - Printer to remove from */
3848 mime_filter_t
*filter
; /* MIME filter looping var */
3852 * Range check input...
3859 * Remove all filters from the MIME database that have a destination
3860 * type == printer...
3863 for (filter
= mimeFirstFilter(MimeDatabase
);
3865 filter
= mimeNextFilter(MimeDatabase
))
3866 if (filter
->dst
== p
->filetype
)
3869 * Delete the current filter...
3872 mimeDeleteFilter(MimeDatabase
, filter
);
3875 cupsdSetPrinterReasons(p
, "-cups-insecure-filter-warning"
3876 ",cups-missing-filter-warning");
3881 * 'delete_string_array()' - Delete an array of CUPS strings.
3885 delete_string_array(cups_array_t
**a
) /* I - Array */
3887 char *ptr
; /* Current string */
3890 for (ptr
= (char *)cupsArrayFirst(*a
);
3892 ptr
= (char *)cupsArrayNext(*a
))
3895 cupsArrayDelete(*a
);
3901 * 'load_ppd()' - Load a cached PPD file, updating the cache as needed.
3905 load_ppd(cupsd_printer_t
*p
) /* I - Printer */
3907 int i
; /* Looping var */
3908 cups_file_t
*cache
; /* Cache file */
3909 char cache_name
[1024]; /* Cache filename */
3910 struct stat cache_info
; /* Cache file info */
3911 ppd_file_t
*ppd
; /* PPD file */
3912 char ppd_name
[1024]; /* PPD filename */
3913 struct stat ppd_info
; /* PPD file info */
3914 int num_media
; /* Number of media options */
3915 char custom_in
[256], /* Custom size name in inches */
3916 custom_mm
[256]; /* Custom size name in millimeters */
3917 ppd_size_t
*size
; /* Current size */
3918 ppd_option_t
*output_bin
, /* OutputBin options */
3919 *duplex
; /* Duplex options */
3920 ppd_attr_t
*ppd_attr
; /* PPD attribute */
3921 _cups_pwg_media_t
*pwgmedia
; /* Matching PWG size name */
3922 ipp_attribute_t
*attr
; /* Attribute data */
3923 ipp_t
*media_col_default
, /* media-col-default collection value */
3924 *media_size
; /* media-size collection value */
3925 ipp_value_t
*val
; /* Attribute value */
3926 int num_finishings
; /* Number of finishings */
3927 int finishings
[5]; /* finishings-supported values */
3928 static const char * const sides
[3] = /* sides-supported values */
3931 "two-sided-long-edge",
3932 "two-sided-short-edge"
3934 static const char * const standard_commands
[] =
3935 { /* Standard CUPS commands */
3943 * Check to see if the cache is up-to-date...
3946 snprintf(cache_name
, sizeof(cache_name
), "%s/%s.ipp", CacheDir
, p
->name
);
3947 if (stat(cache_name
, &cache_info
))
3948 cache_info
.st_mtime
= 0;
3950 snprintf(ppd_name
, sizeof(ppd_name
), "%s/ppd/%s.ppd", ServerRoot
, p
->name
);
3951 if (stat(ppd_name
, &ppd_info
))
3952 ppd_info
.st_mtime
= 1;
3954 ippDelete(p
->ppd_attrs
);
3955 p
->ppd_attrs
= ippNew();
3957 if (cache_info
.st_mtime
>= ppd_info
.st_mtime
&&
3958 (cache
= cupsFileOpen(cache_name
, "r")) != NULL
)
3961 * Load cached information and return...
3964 cupsdLogMessage(CUPSD_LOG_DEBUG
, "load_ppd: Loading %s...", cache_name
);
3966 if (ippReadIO(cache
, (ipp_iocb_t
)cupsFileRead
, 1, NULL
,
3967 p
->ppd_attrs
) == IPP_DATA
)
3969 cupsFileClose(cache
);
3973 cupsFileClose(cache
);
3977 * Reload PPD attributes from disk...
3980 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3982 cupsdLogMessage(CUPSD_LOG_DEBUG
, "load_ppd: Loading %s...", ppd_name
);
3984 delete_string_array(&(p
->filters
));
3985 delete_string_array(&(p
->pre_filters
));
3987 p
->type
&= ~CUPS_PRINTER_OPTIONS
;
3988 p
->type
|= CUPS_PRINTER_BW
;
3990 finishings
[0] = IPP_FINISHINGS_NONE
;
3993 if ((ppd
= ppdOpenFile(ppd_name
)) != NULL
)
3996 * Add make/model and other various attributes...
3999 ppdMarkDefaults(ppd
);
4001 if (ppd
->color_device
)
4002 p
->type
|= CUPS_PRINTER_COLOR
;
4003 if (ppd
->variable_sizes
)
4004 p
->type
|= CUPS_PRINTER_VARIABLE
;
4005 if (!ppd
->manual_copies
)
4006 p
->type
|= CUPS_PRINTER_COPIES
;
4007 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsFax", NULL
)) != NULL
)
4008 if (ppd_attr
->value
&& !strcasecmp(ppd_attr
->value
, "true"))
4009 p
->type
|= CUPS_PRINTER_FAX
;
4011 ippAddBoolean(p
->ppd_attrs
, IPP_TAG_PRINTER
, "color-supported",
4013 if (ppd
->throughput
)
4014 ippAddInteger(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4015 "pages-per-minute", ppd
->throughput
);
4020 * The NickName can be localized in the character set specified
4021 * by the LanugageEncoding attribute. However, ppdOpen2() has
4022 * already converted the ppd->nickname member to UTF-8 for us
4023 * (the original attribute value is available separately)
4026 cupsdSetString(&p
->make_model
, ppd
->nickname
);
4028 else if (ppd
->modelname
)
4031 * Model name can only contain specific characters...
4034 cupsdSetString(&p
->make_model
, ppd
->modelname
);
4037 cupsdSetString(&p
->make_model
, "Bad PPD File");
4039 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4040 "printer-make-and-model", NULL
, p
->make_model
);
4043 * Add media options from the PPD file...
4046 if (ppd
->num_sizes
== 0)
4048 if (!ppdFindAttr(ppd
, "APScannerOnly", NULL
))
4049 cupsdLogMessage(CUPSD_LOG_CRIT
,
4050 "The PPD file for printer %s contains no media "
4051 "options and is therefore invalid!", p
->name
);
4053 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4054 "media-supported", NULL
, "unknown");
4058 num_media
= ppd
->num_sizes
;
4059 if (ppd
->variable_sizes
)
4062 attr
= ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4063 "media-supported", num_media
, NULL
, NULL
);
4068 for (i
= ppd
->num_sizes
, size
= ppd
->sizes
; i
> 0; i
--, size
++)
4070 if (strcasecmp(size
->name
, "Custom"))
4072 if ((pwgmedia
= _cupsPWGMediaBySize(size
->width
,
4073 size
->length
)) != NULL
)
4075 val
->string
.text
= _cupsStrAlloc(pwgmedia
->pwg
);
4079 snprintf(custom_in
, sizeof(custom_in
), "adobe_%s_%gx%gin",
4080 size
->name
, size
->width
/ 72.0, size
->length
/ 72.0);
4081 snprintf(custom_mm
, sizeof(custom_mm
), "adobe_%s_%gx%gmm",
4082 size
->name
, size
->width
* 25.4 / 72.0,
4083 size
->length
* 25.4 / 72.0);
4084 if (strlen(custom_in
) < strlen(custom_mm
))
4085 val
->string
.text
= _cupsStrAlloc(custom_in
);
4087 val
->string
.text
= _cupsStrAlloc(custom_mm
);
4093 * Add media-default...
4096 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4097 "media-default", NULL
, val
->string
.text
);
4100 * Add media-col-default...
4103 media_size
= ippNew();
4104 ippAddInteger(media_size
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4105 "x-dimension", (int)(size
->width
* 2540.0 / 72.0));
4106 ippAddInteger(media_size
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4107 "y-dimension", (int)(size
->length
* 2540.0 / 72.0));
4109 media_col_default
= ippNew();
4110 ippAddString(media_col_default
, IPP_TAG_PRINTER
,
4111 IPP_TAG_KEYWORD
| IPP_TAG_COPY
, "media-color", NULL
,
4113 ippAddString(media_col_default
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4114 "media-key", NULL
,val
->string
.text
);
4115 ippAddCollection(media_col_default
, IPP_TAG_PRINTER
, "media-size",
4117 ippAddString(media_col_default
, IPP_TAG_PRINTER
,
4118 IPP_TAG_KEYWORD
| IPP_TAG_COPY
, "media-type", NULL
,
4121 ippAddCollection(p
->ppd_attrs
, IPP_TAG_PRINTER
,
4122 "media-col-default", media_col_default
);
4129 if (ppd
->variable_sizes
)
4131 snprintf(custom_in
, sizeof(custom_in
), "custom_min_%gx%gin",
4132 ppd
->custom_min
[0] / 72.0, ppd
->custom_min
[1] / 72.0);
4133 snprintf(custom_mm
, sizeof(custom_mm
), "custom_min_%gx%gmm",
4134 ppd
->custom_min
[0] * 25.4 / 72.0,
4135 ppd
->custom_min
[1] * 25.4 / 72.0);
4136 if (strlen(custom_in
) < strlen(custom_mm
))
4137 val
->string
.text
= _cupsStrAlloc(custom_in
);
4139 val
->string
.text
= _cupsStrAlloc(custom_mm
);
4142 snprintf(custom_in
, sizeof(custom_in
), "custom_max_%gx%gin",
4143 ppd
->custom_max
[0] / 72.0, ppd
->custom_max
[1] / 72.0);
4144 snprintf(custom_mm
, sizeof(custom_mm
), "custom_max_%gx%gmm",
4145 ppd
->custom_max
[0] * 25.4 / 72.0,
4146 ppd
->custom_max
[1] * 25.4 / 72.0);
4147 if (strlen(custom_in
) < strlen(custom_mm
))
4148 val
->string
.text
= _cupsStrAlloc(custom_in
);
4150 val
->string
.text
= _cupsStrAlloc(custom_mm
);
4159 if ((output_bin
= ppdFindOption(ppd
, "OutputBin")) != NULL
)
4161 attr
= ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4162 "output-bin-supported", output_bin
->num_choices
,
4167 for (i
= 0, val
= attr
->values
;
4168 i
< output_bin
->num_choices
;
4170 val
->string
.text
= _cupsStrAlloc(output_bin
->choices
[i
].choice
);
4173 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4174 "output-bin-default", NULL
, output_bin
->defchoice
);
4181 if ((duplex
= ppdFindOption(ppd
, "Duplex")) == NULL
)
4182 if ((duplex
= ppdFindOption(ppd
, "EFDuplex")) == NULL
)
4183 if ((duplex
= ppdFindOption(ppd
, "EFDuplexing")) == NULL
)
4184 if ((duplex
= ppdFindOption(ppd
, "KD03Duplex")) == NULL
)
4185 duplex
= ppdFindOption(ppd
, "JCLDuplex");
4187 if (duplex
&& duplex
->num_choices
> 1 &&
4188 !ppdInstallableConflict(ppd
, duplex
->keyword
, "DuplexTumble"))
4190 p
->type
|= CUPS_PRINTER_DUPLEX
;
4192 ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4193 "sides-supported", 3, NULL
, sides
);
4195 if (!strcasecmp(duplex
->defchoice
, "DuplexTumble"))
4196 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4197 "sides-default", NULL
, "two-sided-short-edge");
4198 else if (!strcasecmp(duplex
->defchoice
, "DuplexNoTumble"))
4199 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4200 "sides-default", NULL
, "two-sided-long-edge");
4202 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4203 "sides-default", NULL
, "one-sided");
4206 if (ppdFindOption(ppd
, "Collate") != NULL
)
4207 p
->type
|= CUPS_PRINTER_COLLATE
;
4209 if (ppdFindOption(ppd
, "StapleLocation") != NULL
)
4211 p
->type
|= CUPS_PRINTER_STAPLE
;
4212 finishings
[num_finishings
++] = IPP_FINISHINGS_STAPLE
;
4215 if (ppdFindOption(ppd
, "BindEdge") != NULL
)
4217 p
->type
|= CUPS_PRINTER_BIND
;
4218 finishings
[num_finishings
++] = IPP_FINISHINGS_BIND
;
4221 for (i
= 0; i
< ppd
->num_sizes
; i
++)
4222 if (ppd
->sizes
[i
].length
> 1728)
4223 p
->type
|= CUPS_PRINTER_LARGE
;
4224 else if (ppd
->sizes
[i
].length
> 1008)
4225 p
->type
|= CUPS_PRINTER_MEDIUM
;
4227 p
->type
|= CUPS_PRINTER_SMALL
;
4229 if ((ppd_attr
= ppdFindAttr(ppd
, "APICADriver", NULL
)) != NULL
&&
4230 ppd_attr
->value
&& !strcasecmp(ppd_attr
->value
, "true"))
4232 if ((ppd_attr
= ppdFindAttr(ppd
, "APScannerOnly", NULL
)) != NULL
&&
4233 ppd_attr
->value
&& !strcasecmp(ppd_attr
->value
, "true"))
4234 p
->type
|= CUPS_PRINTER_SCANNER
;
4236 p
->type
|= CUPS_PRINTER_MFP
;
4240 * Add a filter from application/vnd.cups-raw to printer/name to
4241 * handle "raw" printing by users.
4244 add_string_array(&(p
->filters
), "application/vnd.cups-raw 0 -");
4247 * Add any pre-filters in the PPD file...
4250 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsPreFilter", NULL
)) != NULL
)
4252 for (; ppd_attr
; ppd_attr
= ppdFindNextAttr(ppd
, "cupsPreFilter", NULL
))
4253 if (ppd_attr
->value
)
4254 add_string_array(&(p
->pre_filters
), ppd_attr
->value
);
4258 * Add any filters in the PPD file...
4261 DEBUG_printf(("ppd->num_filters = %d\n", ppd
->num_filters
));
4262 for (i
= 0; i
< ppd
->num_filters
; i
++)
4264 DEBUG_printf(("ppd->filters[%d] = \"%s\"\n", i
, ppd
->filters
[i
]));
4265 add_string_array(&(p
->filters
), ppd
->filters
[i
]);
4267 if (!strncasecmp(ppd
->filters
[i
], "application/vnd.cups-command", 28) &&
4268 isspace(ppd
->filters
[i
][28] & 255))
4269 p
->type
|= CUPS_PRINTER_COMMANDS
;
4272 if (ppd
->num_filters
== 0)
4275 * If there are no filters, add PostScript printing filters.
4278 add_string_array(&(p
->filters
),
4279 "application/vnd.cups-command 0 commandtops");
4280 add_string_array(&(p
->filters
),
4281 "application/vnd.cups-postscript 0 -");
4283 p
->type
|= CUPS_PRINTER_COMMANDS
;
4285 else if (!(p
->type
& CUPS_PRINTER_COMMANDS
))
4288 * See if this is a PostScript device without a command filter...
4291 for (i
= 0; i
< ppd
->num_filters
; i
++)
4292 if (!strncasecmp(ppd
->filters
[i
],
4293 "application/vnd.cups-postscript", 31) &&
4294 isspace(ppd
->filters
[i
][31] & 255))
4297 if (i
< ppd
->num_filters
)
4300 * Add the generic PostScript command filter...
4303 add_string_array(&(p
->filters
),
4304 "application/vnd.cups-command 0 commandtops");
4305 p
->type
|= CUPS_PRINTER_COMMANDS
;
4309 if (p
->type
& CUPS_PRINTER_COMMANDS
)
4311 char *commands
, /* Copy of commands */
4312 *start
, /* Start of name */
4313 *end
; /* End of name */
4314 int count
; /* Number of commands */
4317 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsCommands", NULL
)) != NULL
&&
4318 ppd_attr
->value
&& ppd_attr
->value
[0])
4320 for (count
= 0, start
= ppd_attr
->value
; *start
; count
++)
4322 while (isspace(*start
& 255))
4328 while (*start
&& !isspace(*start
& 255))
4338 * Make a copy of the commands string and count how many ...
4341 attr
= ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4342 "printer-commands", count
, NULL
, NULL
);
4344 commands
= strdup(ppd_attr
->value
);
4346 for (count
= 0, start
= commands
; *start
; count
++)
4348 while (isspace(*start
& 255))
4355 while (*end
&& !isspace(*end
& 255))
4361 attr
->values
[count
].string
.text
= _cupsStrAlloc(start
);
4371 * Add the standard list of commands...
4374 ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4376 (int)(sizeof(standard_commands
) /
4377 sizeof(standard_commands
[0])), NULL
,
4384 * No commands supported...
4387 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4388 "printer-commands", NULL
, "none");
4392 * Show current and available port monitors for this printer...
4395 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "port-monitor",
4396 NULL
, p
->port_monitor
? p
->port_monitor
: "none");
4398 for (i
= 1, ppd_attr
= ppdFindAttr(ppd
, "cupsPortMonitor", NULL
);
4400 i
++, ppd_attr
= ppdFindNextAttr(ppd
, "cupsPortMonitor", NULL
));
4404 if (strstr(ppd
->protocols
, "TBCP"))
4406 else if (strstr(ppd
->protocols
, "BCP"))
4410 attr
= ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
4411 "port-monitor-supported", i
, NULL
, NULL
);
4413 attr
->values
[0].string
.text
= _cupsStrAlloc("none");
4415 for (i
= 1, ppd_attr
= ppdFindAttr(ppd
, "cupsPortMonitor", NULL
);
4417 i
++, ppd_attr
= ppdFindNextAttr(ppd
, "cupsPortMonitor", NULL
))
4418 attr
->values
[i
].string
.text
= _cupsStrAlloc(ppd_attr
->value
);
4422 if (strstr(ppd
->protocols
, "TBCP"))
4423 attr
->values
[i
].string
.text
= _cupsStrAlloc("tbcp");
4424 else if (strstr(ppd
->protocols
, "BCP"))
4425 attr
->values
[i
].string
.text
= _cupsStrAlloc("bcp");
4429 cupsdSetString(&p
->product
, ppd
->product
);
4430 #endif /* HAVE_DNSSD */
4432 if (ppdFindAttr(ppd
, "APRemoteQueueID", NULL
))
4433 p
->type
|= CUPS_PRINTER_REMOTE
;
4436 * Close the PPD and set the type...
4441 else if (!access(ppd_name
, 0))
4443 int pline
; /* PPD line number */
4444 ppd_status_t pstatus
; /* PPD load status */
4447 pstatus
= ppdLastError(&pline
);
4449 cupsdLogMessage(CUPSD_LOG_ERROR
, "PPD file for %s cannot be loaded!",
4452 if (pstatus
<= PPD_ALLOC_ERROR
)
4453 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s", strerror(errno
));
4455 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s on line %d.",
4456 ppdErrorString(pstatus
), pline
);
4458 cupsdLogMessage(CUPSD_LOG_INFO
,
4459 "Hint: Run \"cupstestppd %s\" and fix any errors.",
4463 * Add a filter from application/vnd.cups-raw to printer/name to
4464 * handle "raw" printing by users.
4467 add_string_array(&(p
->filters
), "application/vnd.cups-raw 0 -");
4470 * Add a PostScript filter, since this is still possibly PS printer.
4473 add_string_array(&(p
->filters
), "application/vnd.cups-postscript 0 -");
4478 * If we have an interface script, add a filter entry for it...
4481 char interface
[1024]; /* Interface script */
4484 snprintf(interface
, sizeof(interface
), "%s/interfaces/%s", ServerRoot
,
4486 if (!access(interface
, X_OK
))
4489 * Yes, we have a System V style interface script; use it!
4492 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4493 "printer-make-and-model", NULL
,
4494 "Local System V Printer");
4496 snprintf(interface
, sizeof(interface
), "*/* 0 %s/interfaces/%s",
4497 ServerRoot
, p
->name
);
4498 add_string_array(&(p
->filters
), interface
);
4500 else if (!strncmp(p
->device_uri
, "ipp://", 6) &&
4501 (strstr(p
->device_uri
, "/printers/") != NULL
||
4502 strstr(p
->device_uri
, "/classes/") != NULL
||
4503 (strstr(p
->device_uri
, "._ipp.") != NULL
&&
4504 !strcmp(p
->device_uri
+ strlen(p
->device_uri
) - 5,
4508 * Tell the client this is really a hard-wired remote printer.
4511 p
->type
|= CUPS_PRINTER_REMOTE
;
4514 * Point the printer-uri-supported attribute to the
4518 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
4519 "printer-uri-supported", NULL
, p
->device_uri
);
4522 * Then set the make-and-model accordingly...
4525 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4526 "printer-make-and-model", NULL
, "Remote Printer");
4529 * Print all files directly...
4538 * Otherwise we have neither - treat this as a "dumb" printer
4539 * with no PPD file...
4542 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4543 "printer-make-and-model", NULL
, "Local Raw Printer");
4549 ippAddIntegers(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
4550 "finishings-supported", num_finishings
, finishings
);
4551 ippAddInteger(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
4552 "finishings-default", IPP_FINISHINGS_NONE
);
4554 if (ppd
&& (cache
= cupsFileOpen(cache_name
, "w")) != NULL
)
4557 * Save cached PPD attributes to disk...
4560 cupsdLogMessage(CUPSD_LOG_DEBUG
, "load_ppd: Saving %s...", cache_name
);
4562 p
->ppd_attrs
->state
= IPP_IDLE
;
4564 if (ippWriteIO(cache
, (ipp_iocb_t
)cupsFileWrite
, 1, NULL
,
4565 p
->ppd_attrs
) != IPP_DATA
)
4567 cupsdLogMessage(CUPSD_LOG_ERROR
,
4568 "Unable to save PPD cache file \"%s\" - %s", cache_name
,
4573 cupsFileClose(cache
);
4575 else if (cache_info
.st_mtime
)
4578 * Remove cache file...
4588 * 'write_irix_config()' - Update the config files used by the IRIX
4593 write_irix_config(cupsd_printer_t
*p
) /* I - Printer to update */
4595 char filename
[1024]; /* Interface script filename */
4596 cups_file_t
*fp
; /* Interface script file */
4597 ipp_attribute_t
*attr
; /* Attribute data */
4601 * Add dummy interface and GUI scripts to fool SGI's "challenged" printing
4602 * tools. First the interface script that tells the tools what kind of
4603 * printer we have...
4606 snprintf(filename
, sizeof(filename
), "/var/spool/lp/interface/%s", p
->name
);
4608 if (p
->type
& CUPS_PRINTER_CLASS
)
4610 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4612 cupsFilePuts(fp
, "#!/bin/sh\n");
4614 if ((attr
= ippFindAttribute(p
->attrs
, "printer-make-and-model",
4615 IPP_TAG_TEXT
)) != NULL
)
4616 cupsFilePrintf(fp
, "NAME=\"%s\"\n", attr
->values
[0].string
.text
);
4617 else if (p
->type
& CUPS_PRINTER_CLASS
)
4618 cupsFilePuts(fp
, "NAME=\"Printer Class\"\n");
4620 cupsFilePuts(fp
, "NAME=\"Remote Destination\"\n");
4622 if (p
->type
& CUPS_PRINTER_COLOR
)
4623 cupsFilePuts(fp
, "TYPE=ColorPostScript\n");
4625 cupsFilePuts(fp
, "TYPE=MonoPostScript\n");
4627 cupsFilePrintf(fp
, "HOSTNAME=%s\n", ServerName
);
4628 cupsFilePrintf(fp
, "HOSTPRINTER=%s\n", p
->name
);
4632 chmod(filename
, 0755);
4633 chown(filename
, User
, Group
);
4637 * Then the member file that tells which device file the queue is connected
4638 * to... Networked printers use "/dev/null" in this file, so that's what
4639 * we use (the actual device URI can confuse some apps...)
4642 snprintf(filename
, sizeof(filename
), "/var/spool/lp/member/%s", p
->name
);
4644 if (p
->type
& CUPS_PRINTER_CLASS
)
4646 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4648 cupsFilePuts(fp
, "/dev/null\n");
4652 chmod(filename
, 0644);
4653 chown(filename
, User
, Group
);
4657 * The gui_interface file is a script or program that launches a GUI
4658 * option panel for the printer, using options specified on the
4659 * command-line in the third argument. The option panel must send
4660 * any printing options to stdout on a single line when the user
4661 * accepts them, or nothing if the user cancels the dialog.
4663 * The default options panel program is /usr/bin/glpoptions, from
4664 * the ESP Print Pro software. You can select another using the
4665 * PrintcapGUI option.
4668 snprintf(filename
, sizeof(filename
), "/var/spool/lp/gui_interface/ELF/%s.gui", p
->name
);
4670 if (p
->type
& CUPS_PRINTER_CLASS
)
4672 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4674 cupsFilePuts(fp
, "#!/bin/sh\n");
4675 cupsFilePrintf(fp
, "%s -d %s -o \"$3\"\n", PrintcapGUI
, p
->name
);
4679 chmod(filename
, 0755);
4680 chown(filename
, User
, Group
);
4684 * The POD config file is needed by the printstatus command to show
4685 * the printer location and device.
4688 snprintf(filename
, sizeof(filename
), "/var/spool/lp/pod/%s.config", p
->name
);
4690 if (p
->type
& CUPS_PRINTER_CLASS
)
4692 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4694 cupsFilePrintf(fp
, "Printer Class | %s\n",
4695 (p
->type
& CUPS_PRINTER_COLOR
) ? "ColorPostScript" : "MonoPostScript");
4696 cupsFilePrintf(fp
, "Printer Model | %s\n", p
->make_model
? p
->make_model
: "");
4697 cupsFilePrintf(fp
, "Location Code | %s\n", p
->location
? p
->location
: "");
4698 cupsFilePrintf(fp
, "Physical Location | %s\n", p
->info
? p
->info
: "");
4699 cupsFilePrintf(fp
, "Port Path | %s\n", p
->device_uri
);
4700 cupsFilePrintf(fp
, "Config Path | /var/spool/lp/pod/%s.config\n", p
->name
);
4701 cupsFilePrintf(fp
, "Active Status Path | /var/spool/lp/pod/%s.status\n", p
->name
);
4702 cupsFilePuts(fp
, "Status Update Wait | 10 seconds\n");
4706 chmod(filename
, 0664);
4707 chown(filename
, User
, Group
);
4713 * 'write_irix_state()' - Update the status files used by IRIX printing
4718 write_irix_state(cupsd_printer_t
*p
) /* I - Printer to update */
4720 char filename
[1024]; /* Interface script filename */
4721 cups_file_t
*fp
; /* Interface script file */
4722 int tag
; /* Status tag value */
4728 * The POD status file is needed for the printstatus window to
4729 * provide the current status of the printer.
4732 snprintf(filename
, sizeof(filename
), "/var/spool/lp/pod/%s.status", p
->name
);
4734 if (p
->type
& CUPS_PRINTER_CLASS
)
4736 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4738 cupsFilePrintf(fp
, "Operational Status | %s\n",
4739 (p
->state
== IPP_PRINTER_IDLE
) ? "Idle" :
4740 (p
->state
== IPP_PRINTER_PROCESSING
) ? "Busy" :
4742 cupsFilePrintf(fp
, "Information | 01 00 00 | %s\n", CUPS_SVERSION
);
4743 cupsFilePrintf(fp
, "Information | 02 00 00 | Device URI: %s\n",
4745 cupsFilePrintf(fp
, "Information | 03 00 00 | %s jobs\n",
4746 p
->accepting
? "Accepting" : "Not accepting");
4747 cupsFilePrintf(fp
, "Information | 04 00 00 | %s\n", p
->state_message
);
4751 chmod(filename
, 0664);
4752 chown(filename
, User
, Group
);
4756 * The activeicons file is needed to provide desktop icons for printers:
4758 * [ quoted from /usr/lib/print/tagit ]
4760 * --- Type of printer tags (base values)
4762 * Dumb=66048 # 0x10200
4763 * DumbColor=66080 # 0x10220
4764 * Raster=66112 # 0x10240
4765 * ColorRaster=66144 # 0x10260
4766 * Plotter=66176 # 0x10280
4767 * PostScript=66208 # 0x102A0
4768 * ColorPostScript=66240 # 0x102C0
4769 * MonoPostScript=66272 # 0x102E0
4771 * --- Printer state modifiers for local printers
4776 * Unknown=3 # 0x3 (Faulted due to unknown reason)
4778 * --- Printer state modifiers for network printers
4782 * NetFaulted=10 # 0xA
4783 * NetUnknown=11 # 0xB (Faulted due to unknown reason)
4786 snprintf(filename
, sizeof(filename
), "/var/spool/lp/activeicons/%s", p
->name
);
4788 if (p
->type
& CUPS_PRINTER_CLASS
)
4790 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4792 if (p
->type
& CUPS_PRINTER_COLOR
)
4797 if (p
->type
& CUPS_PRINTER_REMOTE
)
4800 if (p
->state
== IPP_PRINTER_PROCESSING
)
4803 else if (p
->state
== IPP_PRINTER_STOPPED
)
4806 cupsFilePuts(fp
, "#!/bin/sh\n");
4807 cupsFilePrintf(fp
, "#Tag %d\n", tag
);
4811 chmod(filename
, 0755);
4812 chown(filename
, User
, Group
);
4817 * The default file is needed by the printers window to show
4818 * the default printer.
4821 snprintf(filename
, sizeof(filename
), "/var/spool/lp/default");
4823 if (DefaultPrinter
!= NULL
)
4825 if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4827 cupsFilePrintf(fp
, "%s\n", DefaultPrinter
->name
);
4831 chmod(filename
, 0644);
4832 chown(filename
, User
, Group
);
4842 * 'write_xml_string()' - Write a string with XML escaping.
4846 write_xml_string(cups_file_t
*fp
, /* I - File to write to */
4847 const char *s
) /* I - String to write */
4849 const char *start
; /* Start of current sequence */
4855 for (start
= s
; *s
; s
++)
4860 cupsFileWrite(fp
, start
, s
- start
);
4862 cupsFilePuts(fp
, "&");
4868 cupsFileWrite(fp
, start
, s
- start
);
4870 cupsFilePuts(fp
, "<");
4876 cupsFilePuts(fp
, start
);
4881 * End of "$Id: printers.c 7968 2008-09-19 23:03:01Z mike $".