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-2010 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
17 * cupsdAddPrinter() - Add a printer to the system.
18 * cupsdAddPrinterHistory() - Add the current printer state to the history.
19 * cupsdAddPrinterUser() - Add a user to the ACL.
20 * cupsdCreateCommonData() - Create the common printer data.
21 * cupsdDeleteAllPrinters() - Delete all printers from the system.
22 * cupsdDeletePrinter() - Delete a printer from the system.
23 * cupsdFindDest() - Find a destination in the list.
24 * cupsdFindPrinter() - Find a printer in the list.
25 * cupsdFreePrinterUsers() - Free allow/deny users.
26 * cupsdLoadAllPrinters() - Load printers from the printers.conf file.
27 * cupsdRenamePrinter() - Rename a printer.
28 * cupsdSaveAllPrinters() - Save all printer definitions to the
30 * cupsdSetAuthInfoRequired() - Set the required authentication info.
31 * cupsdSetDeviceURI() - Set the device URI for a printer.
32 * cupsdSetPrinterAttr() - Set a printer attribute.
33 * cupsdSetPrinterAttrs() - Set printer attributes based upon the PPD
35 * cupsdSetPrinterReasons() - Set/update the reasons strings.
36 * cupsdSetPrinterState() - Update the current state of a printer.
37 * cupsdStopPrinter() - Stop a printer from printing any jobs...
38 * cupsdUpdatePrinterPPD() - Update keywords in a printer's PPD file.
39 * cupsdUpdatePrinters() - Update printers after a partial reload.
40 * cupsdValidateDest() - Validate a printer/class destination.
41 * cupsdWritePrintcap() - Write a pseudo-printcap file for older
42 * applications that need it...
43 * add_printer_defaults() - Add name-default attributes to the printer
45 * add_printer_filter() - Add a MIME filter for a printer.
46 * add_printer_formats() - Add document-format-supported values for a
48 * add_string_array() - Add a string to an array of CUPS strings.
49 * compare_printers() - Compare two printers.
50 * delete_printer_filters() - Delete all MIME filters for a printer.
51 * delete_string_array() - Delete an array of CUPS strings.
52 * load_ppd() - Load a cached PPD file, updating the cache as
54 * write_irix_config() - Update the config files used by the IRIX
56 * write_irix_state() - Update the status files used by IRIX printing
58 * write_xml_string() - Write a string with XML escaping.
62 * Include necessary headers...
67 #include <cups/pwgmedia.h>
74 static void add_printer_defaults(cupsd_printer_t
*p
);
75 static void add_printer_filter(cupsd_printer_t
*p
, mime_type_t
*type
,
77 static void add_printer_formats(cupsd_printer_t
*p
);
78 static void add_string_array(cups_array_t
**a
, const char *s
);
79 static int compare_printers(void *first
, void *second
, void *data
);
80 static void delete_printer_filters(cupsd_printer_t
*p
);
81 static void delete_string_array(cups_array_t
**a
);
82 static void load_ppd(cupsd_printer_t
*p
);
84 static void write_irix_config(cupsd_printer_t
*p
);
85 static void write_irix_state(cupsd_printer_t
*p
);
87 static void write_xml_string(cups_file_t
*fp
, const char *s
);
91 * 'cupsdAddPrinter()' - Add a printer to the system.
94 cupsd_printer_t
* /* O - New printer */
95 cupsdAddPrinter(const char *name
) /* I - Name of printer */
97 cupsd_printer_t
*p
; /* New printer */
101 * Range check input...
104 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdAddPrinter(\"%s\")", name
);
107 * Create a new printer entity...
110 if ((p
= calloc(1, sizeof(cupsd_printer_t
))) == NULL
)
112 cupsdLogMessage(CUPSD_LOG_CRIT
, "Unable to allocate memory for printer - %s",
117 cupsdSetString(&p
->name
, name
);
118 cupsdSetString(&p
->info
, name
);
119 cupsdSetString(&p
->hostname
, ServerName
);
121 cupsdSetStringf(&p
->uri
, "ipp://%s:%d/printers/%s", ServerName
, RemotePort
,
123 cupsdSetDeviceURI(p
, "file:///dev/null");
125 p
->state
= IPP_PRINTER_STOPPED
;
126 p
->state_time
= time(NULL
);
128 p
->shared
= DefaultShared
;
129 p
->filetype
= mimeAddType(MimeDatabase
, "printer", name
);
131 cupsdSetString(&p
->job_sheets
[0], "none");
132 cupsdSetString(&p
->job_sheets
[1], "none");
134 cupsdSetString(&p
->error_policy
, ErrorPolicy
);
135 cupsdSetString(&p
->op_policy
, DefaultPolicy
);
137 p
->op_policy_ptr
= DefaultPolicyPtr
;
139 if (MaxPrinterHistory
)
140 p
->history
= calloc(MaxPrinterHistory
, sizeof(ipp_t
*));
143 * Insert the printer in the printer list alphabetically...
147 Printers
= cupsArrayNew(compare_printers
, NULL
);
149 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
150 "cupsdAddPrinter: Adding %s to Printers", p
->name
);
151 cupsArrayAdd(Printers
, p
);
153 if (!ImplicitPrinters
)
154 ImplicitPrinters
= cupsArrayNew(compare_printers
, NULL
);
157 * Return the new printer...
165 * 'cupsdAddPrinterHistory()' - Add the current printer state to the history.
169 cupsdAddPrinterHistory(
170 cupsd_printer_t
*p
) /* I - Printer */
172 ipp_t
*history
; /* History collection */
176 * Stop early if we aren't keeping history data...
179 if (MaxPrinterHistory
<= 0)
183 * Retire old history data as needed...
186 p
->sequence_number
++;
188 if (p
->num_history
>= MaxPrinterHistory
)
191 ippDelete(p
->history
[0]);
192 memmove(p
->history
, p
->history
+ 1, p
->num_history
* sizeof(ipp_t
*));
196 * Create a collection containing the current printer-state, printer-up-time,
197 * printer-state-message, and printer-state-reasons attributes.
201 ippAddInteger(history
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
, "printer-state",
203 ippAddBoolean(history
, IPP_TAG_PRINTER
, "printer-is-accepting-jobs",
205 ippAddBoolean(history
, IPP_TAG_PRINTER
, "printer-is-shared", p
->shared
);
206 ippAddString(history
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-state-message",
207 NULL
, p
->state_message
);
208 if (p
->num_reasons
== 0)
209 ippAddString(history
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
210 "printer-state-reasons", NULL
, "none");
212 ippAddStrings(history
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
213 "printer-state-reasons", p
->num_reasons
, NULL
,
214 (const char * const *)p
->reasons
);
215 ippAddInteger(history
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
216 "printer-state-change-time", p
->state_time
);
217 ippAddInteger(history
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
218 "printer-state-sequence-number", p
->sequence_number
);
220 p
->history
[p
->num_history
] = history
;
226 * 'cupsdAddPrinterUser()' - Add a user to the ACL.
231 cupsd_printer_t
*p
, /* I - Printer */
232 const char *username
) /* I - User */
234 const char **temp
; /* Temporary array pointer */
240 if (p
->num_users
== 0)
241 temp
= malloc(sizeof(char **));
243 temp
= realloc(p
->users
, sizeof(char **) * (p
->num_users
+ 1));
249 temp
+= p
->num_users
;
251 if ((*temp
= strdup(username
)) != NULL
)
257 * 'cupsdCreateCommonData()' - Create the common printer data.
261 cupsdCreateCommonData(void)
263 int i
; /* Looping var */
264 ipp_attribute_t
*attr
; /* Attribute data */
265 cups_dir_t
*dir
; /* Notifier directory */
266 cups_dentry_t
*dent
; /* Notifier directory entry */
267 cups_array_t
*notifiers
; /* Notifier array */
268 char filename
[1024], /* Filename */
269 *notifier
; /* Current notifier */
270 cupsd_policy_t
*p
; /* Current policy */
271 static const int nups
[] = /* number-up-supported values */
272 { 1, 2, 4, 6, 9, 16 };
273 static const int orients
[4] =/* orientation-requested-supported values */
277 IPP_REVERSE_LANDSCAPE
,
280 static const char * const holds
[] = /* job-hold-until-supported values */
291 static const char * const versions
[] =/* ipp-versions-supported values */
298 static const int ops
[] = /* operations-supported values */
305 IPP_GET_JOB_ATTRIBUTES
,
307 IPP_GET_PRINTER_ATTRIBUTES
,
313 IPP_SET_PRINTER_ATTRIBUTES
,
314 IPP_SET_JOB_ATTRIBUTES
,
315 IPP_GET_PRINTER_SUPPORTED_VALUES
,
316 IPP_CREATE_PRINTER_SUBSCRIPTION
,
317 IPP_CREATE_JOB_SUBSCRIPTION
,
318 IPP_GET_SUBSCRIPTION_ATTRIBUTES
,
319 IPP_GET_SUBSCRIPTIONS
,
320 IPP_RENEW_SUBSCRIPTION
,
321 IPP_CANCEL_SUBSCRIPTION
,
322 IPP_GET_NOTIFICATIONS
,
326 IPP_RELEASE_HELD_NEW_JOBS
,
340 CUPS_AUTHENTICATE_JOB
,
345 static const char * const charsets
[] =/* charset-supported values */
350 static const char * const compressions
[] =
351 { /* document-compression-supported values */
355 #endif /* HAVE_LIBZ */
357 static const char * const media_col_supported
[] =
358 { /* media-col-supported values */
364 static const char * const multiple_document_handling
[] =
365 { /* multiple-document-handling-supported values */
366 "separate-documents-uncollated-copies",
367 "separate-documents-collated-copies"
369 static const char * const notify_attrs
[] =
370 { /* notify-attributes-supported values */
371 "printer-state-change-time",
372 "notify-lease-expiration-time",
373 "notify-subscriber-user-name"
375 static const char * const notify_events
[] =
376 { /* notify-events-supported values */
378 "job-config-changed",
385 "printer-config-changed",
387 "printer-finishings-changed",
388 "printer-media-changed",
392 "printer-state-changed",
399 static const char * const job_settable
[] =
400 { /* job-settable-attributes-supported */
406 "multiple-document-handling",
408 "orientation-requested",
411 "printer-resolution",
414 static const char * const printer_settable
[] =
415 { /* printer-settable-attributes-supported */
422 ippDelete(CommonData
);
424 CommonData
= ippNew();
427 * This list of attributes is sorted to improve performance when the
428 * client provides a requested-attributes attribute...
431 /* charset-configured */
432 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_CHARSET
| IPP_TAG_COPY
,
433 "charset-configured", NULL
, "utf-8");
435 /* charset-supported */
436 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_CHARSET
| IPP_TAG_COPY
,
437 "charset-supported", sizeof(charsets
) / sizeof(charsets
[0]),
440 /* compression-supported */
441 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
442 "compression-supported",
443 sizeof(compressions
) / sizeof(compressions
[0]),
446 /* copies-supported */
447 ippAddRange(CommonData
, IPP_TAG_PRINTER
, "copies-supported", 1, MaxCopies
);
450 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
| IPP_TAG_COPY
,
451 "cups-version", NULL
, CUPS_SVERSION
+ 6);
453 /* generated-natural-language-supported (no IPP_TAG_COPY) */
454 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_LANGUAGE
,
455 "generated-natural-language-supported", NULL
, DefaultLanguage
);
457 /* ipp-versions-supported */
458 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
459 "ipp-versions-supported", sizeof(versions
) / sizeof(versions
[0]),
462 /* job-hold-until-supported */
463 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
464 "job-hold-until-supported", sizeof(holds
) / sizeof(holds
[0]),
467 /* job-priority-supported */
468 ippAddInteger(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
469 "job-priority-supported", 100);
471 /* job-settable-attributes-supported */
472 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
473 "job-settable-attributes-supported",
474 sizeof(job_settable
) / sizeof(job_settable
[0]),
477 /* job-sheets-supported */
478 if (cupsArrayCount(Banners
) > 0)
481 * Setup the job-sheets-supported attribute...
484 if (Classification
&& !ClassifyOverride
)
485 attr
= ippAddString(CommonData
, IPP_TAG_PRINTER
,
486 IPP_TAG_NAME
| IPP_TAG_COPY
,
487 "job-sheets-supported", NULL
, Classification
);
489 attr
= ippAddStrings(CommonData
, IPP_TAG_PRINTER
,
490 IPP_TAG_NAME
| IPP_TAG_COPY
,
491 "job-sheets-supported", cupsArrayCount(Banners
) + 1,
495 cupsdLogMessage(CUPSD_LOG_EMERG
,
496 "Unable to allocate memory for "
497 "job-sheets-supported attribute: %s!", strerror(errno
));
498 else if (!Classification
|| ClassifyOverride
)
500 cupsd_banner_t
*banner
; /* Current banner */
503 attr
->values
[0].string
.text
= _cupsStrAlloc("none");
505 for (i
= 1, banner
= (cupsd_banner_t
*)cupsArrayFirst(Banners
);
507 i
++, banner
= (cupsd_banner_t
*)cupsArrayNext(Banners
))
508 attr
->values
[i
].string
.text
= banner
->name
;
512 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_NAME
| IPP_TAG_COPY
,
513 "job-sheets-supported", NULL
, "none");
515 /* media-col-supported */
516 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
517 "media-col-supported",
518 sizeof(media_col_supported
) /
519 sizeof(media_col_supported
[0]), NULL
,
520 media_col_supported
);
522 /* multiple-document-handling-supported */
523 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
524 "multiple-document-handling-supported",
525 sizeof(multiple_document_handling
) /
526 sizeof(multiple_document_handling
[0]), NULL
,
527 multiple_document_handling
);
529 /* multiple-document-jobs-supported */
530 ippAddBoolean(CommonData
, IPP_TAG_PRINTER
,
531 "multiple-document-jobs-supported", 1);
533 /* multiple-operation-time-out */
534 ippAddInteger(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
535 "multiple-operation-time-out", MultipleOperationTimeout
);
537 /* natural-language-configured (no IPP_TAG_COPY) */
538 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_LANGUAGE
,
539 "natural-language-configured", NULL
, DefaultLanguage
);
541 /* notify-attributes-supported */
542 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
543 "notify-attributes-supported",
544 (int)(sizeof(notify_attrs
) / sizeof(notify_attrs
[0])),
547 /* notify-lease-duration-supported */
548 ippAddRange(CommonData
, IPP_TAG_PRINTER
,
549 "notify-lease-duration-supported", 0,
550 MaxLeaseDuration
? MaxLeaseDuration
: 2147483647);
552 /* notify-max-events-supported */
553 ippAddInteger(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
554 "notify-max-events-supported", MaxEvents
);
556 /* notify-events-supported */
557 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
558 "notify-events-supported",
559 (int)(sizeof(notify_events
) / sizeof(notify_events
[0])),
560 NULL
, notify_events
);
562 /* notify-pull-method-supported */
563 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
564 "notify-pull-method-supported", NULL
, "ippget");
566 /* notify-schemes-supported */
567 snprintf(filename
, sizeof(filename
), "%s/notifier", ServerBin
);
568 if ((dir
= cupsDirOpen(filename
)) != NULL
)
570 notifiers
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
572 while ((dent
= cupsDirRead(dir
)) != NULL
)
573 if (S_ISREG(dent
->fileinfo
.st_mode
) &&
574 (dent
->fileinfo
.st_mode
& S_IXOTH
) != 0)
575 cupsArrayAdd(notifiers
, _cupsStrAlloc(dent
->filename
));
577 if (cupsArrayCount(notifiers
) > 0)
579 attr
= ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
580 "notify-schemes-supported",
581 cupsArrayCount(notifiers
), NULL
, NULL
);
583 for (i
= 0, notifier
= (char *)cupsArrayFirst(notifiers
);
585 i
++, notifier
= (char *)cupsArrayNext(notifiers
))
586 attr
->values
[i
].string
.text
= notifier
;
589 cupsArrayDelete(notifiers
);
593 /* number-up-supported */
594 ippAddIntegers(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
595 "number-up-supported", sizeof(nups
) / sizeof(nups
[0]), nups
);
597 /* operations-supported */
598 ippAddIntegers(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
599 "operations-supported",
600 sizeof(ops
) / sizeof(ops
[0]) + JobFiles
- 1, ops
);
602 /* orientation-requested-supported */
603 ippAddIntegers(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
604 "orientation-requested-supported", 4, orients
);
606 /* page-ranges-supported */
607 ippAddBoolean(CommonData
, IPP_TAG_PRINTER
, "page-ranges-supported", 1);
609 /* pdf-override-supported */
610 ippAddString(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
611 "pdl-override-supported", NULL
, "not-attempted");
613 /* printer-op-policy-supported */
614 attr
= ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_NAME
| IPP_TAG_COPY
,
615 "printer-op-policy-supported", cupsArrayCount(Policies
),
617 for (i
= 0, p
= (cupsd_policy_t
*)cupsArrayFirst(Policies
);
619 i
++, p
= (cupsd_policy_t
*)cupsArrayNext(Policies
))
620 attr
->values
[i
].string
.text
= p
->name
;
622 /* printer-settable-attributes-supported */
623 ippAddStrings(CommonData
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
| IPP_TAG_COPY
,
624 "printer-settable-attributes-supported",
625 sizeof(printer_settable
) / sizeof(printer_settable
[0]),
626 NULL
, printer_settable
);
628 /* server-is-sharing-printers */
629 ippAddBoolean(CommonData
, IPP_TAG_PRINTER
, "server-is-sharing-printers",
630 BrowseLocalProtocols
!= 0 && Browsing
);
635 * 'cupsdDeleteAllPrinters()' - Delete all printers from the system.
639 cupsdDeleteAllPrinters(void)
641 cupsd_printer_t
*p
; /* Pointer to current printer/class */
644 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
646 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
648 p
->op_policy_ptr
= DefaultPolicyPtr
;
649 cupsdDeletePrinter(p
, 0);
655 * 'cupsdDeletePrinter()' - Delete a printer from the system.
658 int /* O - 1 if classes affected, 0 otherwise */
660 cupsd_printer_t
*p
, /* I - Printer to delete */
661 int update
) /* I - Update printers.conf? */
663 int i
, /* Looping var */
664 changed
= 0; /* Class changed? */
666 char filename
[1024]; /* Interface script filename */
670 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "cupsdDeletePrinter(p=%p(%s), update=%d)",
674 * Save the current position in the Printers array...
677 cupsArraySave(Printers
);
680 * Stop printing on this printer...
683 cupsdSetPrinterState(p
, IPP_PRINTER_STOPPED
, update
);
686 cupsdSetJobState(p
->job
, IPP_JOB_PENDING
, CUPSD_JOB_FORCE
,
687 update
? "Job stopped due to printer being deleted." :
691 * If this printer is the next for browsing, point to the next one...
696 cupsArrayFind(Printers
, p
);
697 BrowseNext
= (cupsd_printer_t
*)cupsArrayNext(Printers
);
701 * Remove the printer from the list...
704 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
705 "cupsdDeletePrinter: Removing %s from Printers", p
->name
);
706 cupsArrayRemove(Printers
, p
);
708 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
710 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
711 "cupsdDeletePrinter: Removing %s from ImplicitPrinters",
713 cupsArrayRemove(ImplicitPrinters
, p
);
717 * Remove the dummy interface/icon/option files under IRIX...
721 snprintf(filename
, sizeof(filename
), "/var/spool/lp/interface/%s", p
->name
);
724 snprintf(filename
, sizeof(filename
), "/var/spool/lp/gui_interface/ELF/%s.gui",
728 snprintf(filename
, sizeof(filename
), "/var/spool/lp/activeicons/%s", p
->name
);
731 snprintf(filename
, sizeof(filename
), "/var/spool/lp/pod/%s.config", p
->name
);
734 snprintf(filename
, sizeof(filename
), "/var/spool/lp/pod/%s.status", p
->name
);
737 snprintf(filename
, sizeof(filename
), "/var/spool/lp/member/%s", p
->name
);
742 * If p is the default printer, assign a different one...
745 if (p
== DefaultPrinter
)
747 DefaultPrinter
= NULL
;
749 if (UseNetworkDefault
)
752 * Find the first network default printer and use it...
755 cupsd_printer_t
*dp
; /* New default printer */
758 for (dp
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
760 dp
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
761 if (dp
!= p
&& (dp
->type
& CUPS_PRINTER_DEFAULT
))
770 * Remove this printer from any classes...
773 if (!(p
->type
& CUPS_PRINTER_IMPLICIT
))
775 changed
= cupsdDeletePrinterFromClasses(p
);
778 * Deregister from any browse protocols...
781 cupsdDeregisterPrinter(p
, 1);
785 * Free all memory used by the printer...
788 if (p
->printers
!= NULL
)
791 if (MaxPrinterHistory
)
793 for (i
= 0; i
< p
->num_history
; i
++)
794 ippDelete(p
->history
[i
]);
799 delete_printer_filters(p
);
801 for (i
= 0; i
< p
->num_reasons
; i
++)
802 _cupsStrFree(p
->reasons
[i
]);
805 ippDelete(p
->ppd_attrs
);
807 mimeDeleteType(MimeDatabase
, p
->filetype
);
808 mimeDeleteType(MimeDatabase
, p
->prefiltertype
);
810 delete_string_array(&(p
->filters
));
811 delete_string_array(&(p
->pre_filters
));
813 cupsdFreePrinterUsers(p
);
816 cupsdClearString(&p
->uri
);
817 cupsdClearString(&p
->hostname
);
818 cupsdClearString(&p
->name
);
819 cupsdClearString(&p
->location
);
820 cupsdClearString(&p
->make_model
);
821 cupsdClearString(&p
->info
);
822 cupsdClearString(&p
->job_sheets
[0]);
823 cupsdClearString(&p
->job_sheets
[1]);
824 cupsdClearString(&p
->device_uri
);
825 cupsdClearString(&p
->sanitized_device_uri
);
826 cupsdClearString(&p
->port_monitor
);
827 cupsdClearString(&p
->op_policy
);
828 cupsdClearString(&p
->error_policy
);
830 cupsdClearString(&p
->alert
);
831 cupsdClearString(&p
->alert_description
);
834 cupsdClearString(&p
->product
);
835 cupsdClearString(&p
->pdl
);
836 #endif /* HAVE_DNSSD */
838 cupsArrayDelete(p
->filetypes
);
841 free(p
->browse_attrs
);
843 cupsFreeOptions(p
->num_options
, p
->options
);
848 * Restore the previous position in the Printers array...
851 cupsArrayRestore(Printers
);
858 * 'cupsdFindDest()' - Find a destination in the list.
861 cupsd_printer_t
* /* O - Destination in list */
862 cupsdFindDest(const char *name
) /* I - Name of printer or class to find */
864 cupsd_printer_t key
; /* Search key */
867 key
.name
= (char *)name
;
868 return ((cupsd_printer_t
*)cupsArrayFind(Printers
, &key
));
873 * 'cupsdFindPrinter()' - Find a printer in the list.
876 cupsd_printer_t
* /* O - Printer in list */
877 cupsdFindPrinter(const char *name
) /* I - Name of printer to find */
879 cupsd_printer_t
*p
; /* Printer in list */
882 if ((p
= cupsdFindDest(name
)) != NULL
&& (p
->type
& CUPS_PRINTER_CLASS
))
890 * 'cupsdFreePrinterUsers()' - Free allow/deny users.
894 cupsdFreePrinterUsers(
895 cupsd_printer_t
*p
) /* I - Printer */
897 int i
; /* Looping var */
900 if (!p
|| !p
->num_users
)
903 for (i
= 0; i
< p
->num_users
; i
++)
904 free((void *)p
->users
[i
]);
914 * 'cupsdLoadAllPrinters()' - Load printers from the printers.conf file.
918 cupsdLoadAllPrinters(void)
920 int i
; /* Looping var */
921 cups_file_t
*fp
; /* printers.conf file */
922 int linenum
; /* Current line number */
923 char line
[4096], /* Line from file */
924 *value
, /* Pointer to value */
925 *valueptr
; /* Pointer into value */
926 cupsd_printer_t
*p
; /* Current printer */
930 * Open the printers.conf file...
933 snprintf(line
, sizeof(line
), "%s/printers.conf", ServerRoot
);
934 if ((fp
= cupsFileOpen(line
, "r")) == NULL
)
937 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open %s - %s", line
,
943 * Read printer configurations until we hit EOF...
949 while (cupsFileGetConf(fp
, line
, sizeof(line
), &value
, &linenum
))
952 * Decode the directive...
955 if (!strcasecmp(line
, "<Printer") ||
956 !strcasecmp(line
, "<DefaultPrinter"))
959 * <Printer name> or <DefaultPrinter name>
962 if (p
== NULL
&& value
)
965 * Add the printer and a base file type...
968 cupsdLogMessage(CUPSD_LOG_DEBUG
, "Loading printer %s...", value
);
970 p
= cupsdAddPrinter(value
);
972 p
->state
= IPP_PRINTER_IDLE
;
975 * Set the default printer as needed...
978 if (!strcasecmp(line
, "<DefaultPrinter"))
982 cupsdLogMessage(CUPSD_LOG_ERROR
,
983 "Syntax error on line %d of printers.conf.", linenum
);
985 else if (!strcasecmp(line
, "</Printer>"))
990 * Close out the current printer...
993 cupsdSetPrinterAttrs(p
);
994 cupsdAddPrinterHistory(p
);
996 if (strncmp(p
->device_uri
, "file:", 5) &&
997 p
->state
!= IPP_PRINTER_STOPPED
)
1000 * See if the backend exists...
1003 snprintf(line
, sizeof(line
), "%s/backend/%s", ServerBin
,
1006 if ((valueptr
= strchr(line
+ strlen(ServerBin
), ':')) != NULL
)
1007 *valueptr
= '\0'; /* Chop everything but URI scheme */
1009 if (access(line
, 0))
1012 * Backend does not exist, stop printer...
1015 p
->state
= IPP_PRINTER_STOPPED
;
1016 snprintf(p
->state_message
, sizeof(p
->state_message
),
1017 "Backend %s does not exist!", line
);
1024 cupsdLogMessage(CUPSD_LOG_ERROR
,
1025 "Syntax error on line %d of printers.conf.", linenum
);
1029 cupsdLogMessage(CUPSD_LOG_ERROR
,
1030 "Syntax error on line %d of printers.conf.", linenum
);
1032 else if (!strcasecmp(line
, "AuthInfoRequired"))
1034 if (!cupsdSetAuthInfoRequired(p
, value
, NULL
))
1035 cupsdLogMessage(CUPSD_LOG_ERROR
,
1036 "Bad AuthInfoRequired on line %d of printers.conf.",
1039 else if (!strcasecmp(line
, "Info"))
1042 cupsdSetString(&p
->info
, value
);
1044 else if (!strcasecmp(line
, "MakeModel"))
1047 cupsdSetString(&p
->make_model
, value
);
1049 else if (!strcasecmp(line
, "Location"))
1052 cupsdSetString(&p
->location
, value
);
1054 else if (!strcasecmp(line
, "DeviceURI"))
1057 cupsdSetDeviceURI(p
, value
);
1059 cupsdLogMessage(CUPSD_LOG_ERROR
,
1060 "Syntax error on line %d of printers.conf.", linenum
);
1062 else if (!strcasecmp(line
, "Option") && value
)
1068 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
1071 cupsdLogMessage(CUPSD_LOG_ERROR
,
1072 "Syntax error on line %d of printers.conf.", linenum
);
1075 for (; *valueptr
&& isspace(*valueptr
& 255); *valueptr
++ = '\0');
1077 p
->num_options
= cupsAddOption(value
, valueptr
, p
->num_options
,
1081 else if (!strcasecmp(line
, "PortMonitor"))
1083 if (value
&& strcmp(value
, "none"))
1084 cupsdSetString(&p
->port_monitor
, value
);
1086 cupsdClearString(&p
->port_monitor
);
1088 cupsdLogMessage(CUPSD_LOG_ERROR
,
1089 "Syntax error on line %d of printers.conf.", linenum
);
1091 else if (!strcasecmp(line
, "Reason"))
1094 strcmp(value
, "connecting-to-device") &&
1095 strcmp(value
, "cups-insecure-filter-warning") &&
1096 strcmp(value
, "cups-missing-filter-warning"))
1098 for (i
= 0 ; i
< p
->num_reasons
; i
++)
1099 if (!strcmp(value
, p
->reasons
[i
]))
1102 if (i
>= p
->num_reasons
&&
1103 p
->num_reasons
< (int)(sizeof(p
->reasons
) / sizeof(p
->reasons
[0])))
1105 p
->reasons
[p
->num_reasons
] = _cupsStrAlloc(value
);
1110 cupsdLogMessage(CUPSD_LOG_ERROR
,
1111 "Syntax error on line %d of printers.conf.", linenum
);
1113 else if (!strcasecmp(line
, "State"))
1116 * Set the initial queue state...
1119 if (value
&& !strcasecmp(value
, "idle"))
1120 p
->state
= IPP_PRINTER_IDLE
;
1121 else if (value
&& !strcasecmp(value
, "stopped"))
1123 p
->state
= IPP_PRINTER_STOPPED
;
1125 for (i
= 0 ; i
< p
->num_reasons
; i
++)
1126 if (!strcmp("paused", p
->reasons
[i
]))
1129 if (i
>= p
->num_reasons
&&
1130 p
->num_reasons
< (int)(sizeof(p
->reasons
) / sizeof(p
->reasons
[0])))
1132 p
->reasons
[p
->num_reasons
] = _cupsStrAlloc("paused");
1137 cupsdLogMessage(CUPSD_LOG_ERROR
,
1138 "Syntax error on line %d of printers.conf.", linenum
);
1140 else if (!strcasecmp(line
, "StateMessage"))
1143 * Set the initial queue state message...
1147 strlcpy(p
->state_message
, value
, sizeof(p
->state_message
));
1149 else if (!strcasecmp(line
, "StateTime"))
1152 * Set the state time...
1156 p
->state_time
= atoi(value
);
1158 else if (!strcasecmp(line
, "Accepting"))
1161 * Set the initial accepting state...
1165 (!strcasecmp(value
, "yes") ||
1166 !strcasecmp(value
, "on") ||
1167 !strcasecmp(value
, "true")))
1170 (!strcasecmp(value
, "no") ||
1171 !strcasecmp(value
, "off") ||
1172 !strcasecmp(value
, "false")))
1175 cupsdLogMessage(CUPSD_LOG_ERROR
,
1176 "Syntax error on line %d of printers.conf.", linenum
);
1178 else if (!strcasecmp(line
, "Type"))
1181 p
->type
= atoi(value
);
1183 cupsdLogMessage(CUPSD_LOG_ERROR
,
1184 "Syntax error on line %d of printers.conf.", linenum
);
1186 else if (!strcasecmp(line
, "Product"))
1191 p
->product
= _cupsStrAlloc(value
);
1192 #endif /* HAVE_DNSSD */
1195 cupsdLogMessage(CUPSD_LOG_ERROR
,
1196 "Syntax error on line %d of printers.conf.", linenum
);
1198 else if (!strcasecmp(line
, "Filter"))
1203 p
->filters
= cupsArrayNew(NULL
, NULL
);
1205 cupsArrayAdd(p
->filters
, _cupsStrAlloc(value
));
1208 cupsdLogMessage(CUPSD_LOG_ERROR
,
1209 "Syntax error on line %d of printers.conf.", linenum
);
1211 else if (!strcasecmp(line
, "PreFilter"))
1215 if (!p
->pre_filters
)
1216 p
->pre_filters
= cupsArrayNew(NULL
, NULL
);
1218 cupsArrayAdd(p
->pre_filters
, _cupsStrAlloc(value
));
1221 cupsdLogMessage(CUPSD_LOG_ERROR
,
1222 "Syntax error on line %d of printers.conf.", linenum
);
1224 else if (!strcasecmp(line
, "Shared"))
1227 * Set the initial shared state...
1231 (!strcasecmp(value
, "yes") ||
1232 !strcasecmp(value
, "on") ||
1233 !strcasecmp(value
, "true")))
1236 (!strcasecmp(value
, "no") ||
1237 !strcasecmp(value
, "off") ||
1238 !strcasecmp(value
, "false")))
1241 cupsdLogMessage(CUPSD_LOG_ERROR
,
1242 "Syntax error on line %d of printers.conf.", linenum
);
1244 else if (!strcasecmp(line
, "JobSheets"))
1247 * Set the initial job sheets...
1252 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
1257 cupsdSetString(&p
->job_sheets
[0], value
);
1259 while (isspace(*valueptr
& 255))
1264 for (value
= valueptr
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
1269 cupsdSetString(&p
->job_sheets
[1], value
);
1273 cupsdLogMessage(CUPSD_LOG_ERROR
,
1274 "Syntax error on line %d of printers.conf.", linenum
);
1276 else if (!strcasecmp(line
, "AllowUser"))
1281 cupsdAddPrinterUser(p
, value
);
1284 cupsdLogMessage(CUPSD_LOG_ERROR
,
1285 "Syntax error on line %d of printers.conf.", linenum
);
1287 else if (!strcasecmp(line
, "DenyUser"))
1292 cupsdAddPrinterUser(p
, value
);
1295 cupsdLogMessage(CUPSD_LOG_ERROR
,
1296 "Syntax error on line %d of printers.conf.", linenum
);
1298 else if (!strcasecmp(line
, "QuotaPeriod"))
1301 p
->quota_period
= atoi(value
);
1303 cupsdLogMessage(CUPSD_LOG_ERROR
,
1304 "Syntax error on line %d of printers.conf.", linenum
);
1306 else if (!strcasecmp(line
, "PageLimit"))
1309 p
->page_limit
= atoi(value
);
1311 cupsdLogMessage(CUPSD_LOG_ERROR
,
1312 "Syntax error on line %d of printers.conf.", linenum
);
1314 else if (!strcasecmp(line
, "KLimit"))
1317 p
->k_limit
= atoi(value
);
1319 cupsdLogMessage(CUPSD_LOG_ERROR
,
1320 "Syntax error on line %d of printers.conf.", linenum
);
1322 else if (!strcasecmp(line
, "OpPolicy"))
1326 cupsd_policy_t
*pol
; /* Policy */
1329 if ((pol
= cupsdFindPolicy(value
)) != NULL
)
1331 cupsdSetString(&p
->op_policy
, value
);
1332 p
->op_policy_ptr
= pol
;
1335 cupsdLogMessage(CUPSD_LOG_ERROR
,
1336 "Bad policy \"%s\" on line %d of printers.conf",
1340 cupsdLogMessage(CUPSD_LOG_ERROR
,
1341 "Syntax error on line %d of printers.conf.", linenum
);
1343 else if (!strcasecmp(line
, "ErrorPolicy"))
1346 cupsdSetString(&p
->error_policy
, value
);
1348 cupsdLogMessage(CUPSD_LOG_ERROR
,
1349 "Syntax error on line %d of printers.conf.", linenum
);
1351 else if (!strcasecmp(line
, "Attribute") && value
)
1353 for (valueptr
= value
; *valueptr
&& !isspace(*valueptr
& 255); valueptr
++);
1356 cupsdLogMessage(CUPSD_LOG_ERROR
,
1357 "Syntax error on line %d of printers.conf.", linenum
);
1360 for (; *valueptr
&& isspace(*valueptr
& 255); *valueptr
++ = '\0');
1363 cupsdSetPrinterAttrs(p
);
1365 if (!strcmp(value
, "marker-change-time"))
1366 p
->marker_time
= atoi(valueptr
);
1368 cupsdSetPrinterAttr(p
, value
, valueptr
);
1374 * Something else we don't understand...
1377 cupsdLogMessage(CUPSD_LOG_ERROR
,
1378 "Unknown configuration directive %s on line %d of printers.conf.",
1388 * 'cupsdRenamePrinter()' - Rename a printer.
1393 cupsd_printer_t
*p
, /* I - Printer */
1394 const char *name
) /* I - New name */
1397 * Remove the printer from the array(s) first...
1400 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1401 "cupsdRenamePrinter: Removing %s from Printers", p
->name
);
1402 cupsArrayRemove(Printers
, p
);
1404 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
1406 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1407 "cupsdRenamePrinter: Removing %s from ImplicitPrinters",
1409 cupsArrayRemove(ImplicitPrinters
, p
);
1413 * Rename the printer type...
1416 mimeDeleteType(MimeDatabase
, p
->filetype
);
1417 p
->filetype
= mimeAddType(MimeDatabase
, "printer", name
);
1419 mimeDeleteType(MimeDatabase
, p
->prefiltertype
);
1420 p
->prefiltertype
= mimeAddType(MimeDatabase
, "prefilter", name
);
1423 * Rename the printer...
1426 cupsdSetString(&p
->name
, name
);
1429 * Reset printer attributes...
1432 cupsdSetPrinterAttrs(p
);
1435 * Add the printer back to the printer array(s)...
1438 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1439 "cupsdRenamePrinter: Adding %s to Printers", p
->name
);
1440 cupsArrayAdd(Printers
, p
);
1442 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
1444 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
1445 "cupsdRenamePrinter: Adding %s to ImplicitPrinters",
1447 cupsArrayAdd(ImplicitPrinters
, p
);
1453 * 'cupsdSaveAllPrinters()' - Save all printer definitions to the printers.conf
1458 cupsdSaveAllPrinters(void)
1460 int i
; /* Looping var */
1461 cups_file_t
*fp
; /* printers.conf file */
1462 char temp
[1024], /* Temporary string */
1463 backup
[1024], /* printers.conf.O file */
1464 value
[2048], /* Value string */
1465 *ptr
; /* Pointer into value */
1466 cupsd_printer_t
*printer
; /* Current printer class */
1467 time_t curtime
; /* Current time */
1468 struct tm
*curdate
; /* Current date */
1469 cups_option_t
*option
; /* Current option */
1470 ipp_attribute_t
*marker
; /* Current marker attribute */
1474 * Create the printers.conf file...
1477 snprintf(temp
, sizeof(temp
), "%s/printers.conf", ServerRoot
);
1478 snprintf(backup
, sizeof(backup
), "%s/printers.conf.O", ServerRoot
);
1480 if (rename(temp
, backup
))
1482 if (errno
!= ENOENT
)
1483 cupsdLogMessage(CUPSD_LOG_ERROR
,
1484 "Unable to backup printers.conf - %s", strerror(errno
));
1487 if ((fp
= cupsFileOpen(temp
, "w")) == NULL
)
1489 cupsdLogMessage(CUPSD_LOG_ERROR
,
1490 "Unable to save printers.conf - %s", strerror(errno
));
1492 if (rename(backup
, temp
))
1493 cupsdLogMessage(CUPSD_LOG_ERROR
,
1494 "Unable to restore printers.conf - %s", strerror(errno
));
1498 cupsdLogMessage(CUPSD_LOG_INFO
, "Saving printers.conf...");
1501 * Restrict access to the file...
1504 fchown(cupsFileNumber(fp
), getuid(), Group
);
1505 fchmod(cupsFileNumber(fp
), 0600);
1508 * Write a small header to the file...
1511 curtime
= time(NULL
);
1512 curdate
= localtime(&curtime
);
1513 strftime(temp
, sizeof(temp
) - 1, "%Y-%m-%d %H:%M", curdate
);
1515 cupsFilePuts(fp
, "# Printer configuration file for " CUPS_SVERSION
"\n");
1516 cupsFilePrintf(fp
, "# Written by cupsd on %s\n", temp
);
1517 cupsFilePuts(fp
, "# DO NOT EDIT THIS FILE WHEN CUPSD IS RUNNING\n");
1520 * Write each local printer known to the system...
1523 for (printer
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
1525 printer
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
1528 * Skip remote destinations and printer classes...
1531 if ((printer
->type
& CUPS_PRINTER_DISCOVERED
) ||
1532 (printer
->type
& CUPS_PRINTER_CLASS
) ||
1533 (printer
->type
& CUPS_PRINTER_IMPLICIT
))
1537 * Write printers as needed...
1540 if (printer
== DefaultPrinter
)
1541 cupsFilePrintf(fp
, "<DefaultPrinter %s>\n", printer
->name
);
1543 cupsFilePrintf(fp
, "<Printer %s>\n", printer
->name
);
1545 if (printer
->num_auth_info_required
> 0)
1547 switch (printer
->num_auth_info_required
)
1550 strlcpy(value
, printer
->auth_info_required
[0], sizeof(value
));
1554 snprintf(value
, sizeof(value
), "%s,%s",
1555 printer
->auth_info_required
[0],
1556 printer
->auth_info_required
[1]);
1561 snprintf(value
, sizeof(value
), "%s,%s,%s",
1562 printer
->auth_info_required
[0],
1563 printer
->auth_info_required
[1],
1564 printer
->auth_info_required
[2]);
1568 cupsFilePutConf(fp
, "AuthInfoRequired", value
);
1572 cupsFilePutConf(fp
, "Info", printer
->info
);
1574 if (printer
->location
)
1575 cupsFilePutConf(fp
, "Location", printer
->location
);
1577 if (printer
->make_model
)
1578 cupsFilePutConf(fp
, "MakeModel", printer
->make_model
);
1580 cupsFilePutConf(fp
, "DeviceURI", printer
->device_uri
);
1582 if (printer
->port_monitor
)
1583 cupsFilePutConf(fp
, "PortMonitor", printer
->port_monitor
);
1585 if (printer
->state
== IPP_PRINTER_STOPPED
)
1587 cupsFilePuts(fp
, "State Stopped\n");
1589 if (printer
->state_message
)
1590 cupsFilePutConf(fp
, "StateMessage", printer
->state_message
);
1593 cupsFilePuts(fp
, "State Idle\n");
1595 cupsFilePrintf(fp
, "StateTime %d\n", (int)printer
->state_time
);
1597 for (i
= 0; i
< printer
->num_reasons
; i
++)
1598 if (strcmp(printer
->reasons
[i
], "connecting-to-device") &&
1599 strcmp(printer
->reasons
[i
], "cups-insecure-filter-warning") &&
1600 strcmp(printer
->reasons
[i
], "cups-missing-filter-warning"))
1601 cupsFilePutConf(fp
, "Reason", printer
->reasons
[i
]);
1603 cupsFilePrintf(fp
, "Type %d\n", printer
->type
);
1606 if (printer
->product
)
1607 cupsFilePutConf(fp
, "Product", printer
->product
);
1608 #endif /* HAVE_DNSSD */
1610 for (ptr
= (char *)cupsArrayFirst(printer
->filters
);
1612 ptr
= (char *)cupsArrayNext(printer
->filters
))
1613 cupsFilePutConf(fp
, "Filter", ptr
);
1615 for (ptr
= (char *)cupsArrayFirst(printer
->pre_filters
);
1617 ptr
= (char *)cupsArrayNext(printer
->pre_filters
))
1618 cupsFilePutConf(fp
, "PreFilter", ptr
);
1620 if (printer
->accepting
)
1621 cupsFilePuts(fp
, "Accepting Yes\n");
1623 cupsFilePuts(fp
, "Accepting No\n");
1625 if (printer
->shared
)
1626 cupsFilePuts(fp
, "Shared Yes\n");
1628 cupsFilePuts(fp
, "Shared No\n");
1630 snprintf(value
, sizeof(value
), "%s %s", printer
->job_sheets
[0],
1631 printer
->job_sheets
[1]);
1632 cupsFilePutConf(fp
, "JobSheets", value
);
1634 cupsFilePrintf(fp
, "QuotaPeriod %d\n", printer
->quota_period
);
1635 cupsFilePrintf(fp
, "PageLimit %d\n", printer
->page_limit
);
1636 cupsFilePrintf(fp
, "KLimit %d\n", printer
->k_limit
);
1638 for (i
= 0; i
< printer
->num_users
; i
++)
1639 cupsFilePutConf(fp
, printer
->deny_users
? "DenyUser" : "AllowUser",
1642 if (printer
->op_policy
)
1643 cupsFilePutConf(fp
, "OpPolicy", printer
->op_policy
);
1644 if (printer
->error_policy
)
1645 cupsFilePutConf(fp
, "ErrorPolicy", printer
->error_policy
);
1647 for (i
= printer
->num_options
, option
= printer
->options
;
1651 snprintf(value
, sizeof(value
), "%s %s", option
->name
, option
->value
);
1652 cupsFilePutConf(fp
, "Option", value
);
1655 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-colors",
1656 IPP_TAG_NAME
)) != NULL
)
1658 snprintf(value
, sizeof(value
), "%s ", marker
->name
);
1660 for (i
= 0, ptr
= value
+ strlen(value
);
1661 i
< marker
->num_values
&& ptr
< (value
+ sizeof(value
) - 1);
1667 strlcpy(ptr
, marker
->values
[i
].string
.text
,
1668 value
+ sizeof(value
) - ptr
);
1673 cupsFilePutConf(fp
, "Attribute", value
);
1676 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-levels",
1677 IPP_TAG_INTEGER
)) != NULL
)
1679 cupsFilePrintf(fp
, "Attribute %s %d", marker
->name
,
1680 marker
->values
[0].integer
);
1681 for (i
= 1; i
< marker
->num_values
; i
++)
1682 cupsFilePrintf(fp
, ",%d", marker
->values
[i
].integer
);
1683 cupsFilePuts(fp
, "\n");
1686 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-low-levels",
1687 IPP_TAG_INTEGER
)) != NULL
)
1689 cupsFilePrintf(fp
, "Attribute %s %d", marker
->name
,
1690 marker
->values
[0].integer
);
1691 for (i
= 1; i
< marker
->num_values
; i
++)
1692 cupsFilePrintf(fp
, ",%d", marker
->values
[i
].integer
);
1693 cupsFilePuts(fp
, "\n");
1696 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-high-levels",
1697 IPP_TAG_INTEGER
)) != NULL
)
1699 cupsFilePrintf(fp
, "Attribute %s %d", marker
->name
,
1700 marker
->values
[0].integer
);
1701 for (i
= 1; i
< marker
->num_values
; i
++)
1702 cupsFilePrintf(fp
, ",%d", marker
->values
[i
].integer
);
1703 cupsFilePuts(fp
, "\n");
1706 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-message",
1707 IPP_TAG_TEXT
)) != NULL
)
1709 snprintf(value
, sizeof(value
), "%s %s", marker
->name
,
1710 marker
->values
[0].string
.text
);
1712 cupsFilePutConf(fp
, "Attribute", value
);
1715 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-names",
1716 IPP_TAG_NAME
)) != NULL
)
1718 snprintf(value
, sizeof(value
), "%s ", marker
->name
);
1720 for (i
= 0, ptr
= value
+ strlen(value
);
1721 i
< marker
->num_values
&& ptr
< (value
+ sizeof(value
) - 1);
1727 strlcpy(ptr
, marker
->values
[i
].string
.text
,
1728 value
+ sizeof(value
) - ptr
);
1733 cupsFilePutConf(fp
, "Attribute", value
);
1736 if ((marker
= ippFindAttribute(printer
->attrs
, "marker-types",
1737 IPP_TAG_KEYWORD
)) != NULL
)
1739 snprintf(value
, sizeof(value
), "%s ", marker
->name
);
1741 for (i
= 0, ptr
= value
+ strlen(value
);
1742 i
< marker
->num_values
&& ptr
< (value
+ sizeof(value
) - 1);
1748 strlcpy(ptr
, marker
->values
[i
].string
.text
,
1749 value
+ sizeof(value
) - ptr
);
1754 cupsFilePutConf(fp
, "Attribute", value
);
1757 if (printer
->marker_time
)
1758 cupsFilePrintf(fp
, "Attribute marker-change-time %ld\n",
1759 (long)printer
->marker_time
);
1761 cupsFilePuts(fp
, "</Printer>\n");
1765 * Make IRIX desktop & printer status happy
1768 write_irix_state(printer
);
1777 * 'cupsdSetAuthInfoRequired()' - Set the required authentication info.
1780 int /* O - 1 if value OK, 0 otherwise */
1781 cupsdSetAuthInfoRequired(
1782 cupsd_printer_t
*p
, /* I - Printer */
1783 const char *values
, /* I - Plain text value (or NULL) */
1784 ipp_attribute_t
*attr
) /* I - IPP attribute value (or NULL) */
1786 int i
; /* Looping var */
1789 p
->num_auth_info_required
= 0;
1792 * Do we have a plain text value?
1798 * Yes, grab the keywords...
1801 const char *end
; /* End of current value */
1804 while (*values
&& p
->num_auth_info_required
< 4)
1806 if ((end
= strchr(values
, ',')) == NULL
)
1807 end
= values
+ strlen(values
);
1809 if ((end
- values
) == 4 && !strncmp(values
, "none", 4))
1811 if (p
->num_auth_info_required
!= 0 || *end
)
1814 p
->auth_info_required
[p
->num_auth_info_required
] = "none";
1815 p
->num_auth_info_required
++;
1819 else if ((end
- values
) == 9 && !strncmp(values
, "negotiate", 9))
1821 if (p
->num_auth_info_required
!= 0 || *end
)
1824 p
->auth_info_required
[p
->num_auth_info_required
] = "negotiate";
1825 p
->num_auth_info_required
++;
1827 else if ((end
- values
) == 6 && !strncmp(values
, "domain", 6))
1829 p
->auth_info_required
[p
->num_auth_info_required
] = "domain";
1830 p
->num_auth_info_required
++;
1832 else if ((end
- values
) == 8 && !strncmp(values
, "password", 8))
1834 p
->auth_info_required
[p
->num_auth_info_required
] = "password";
1835 p
->num_auth_info_required
++;
1837 else if ((end
- values
) == 8 && !strncmp(values
, "username", 8))
1839 p
->auth_info_required
[p
->num_auth_info_required
] = "username";
1840 p
->num_auth_info_required
++;
1845 values
= (*end
) ? end
+ 1 : end
;
1848 if (p
->num_auth_info_required
== 0)
1850 p
->auth_info_required
[0] = "none";
1851 p
->num_auth_info_required
= 1;
1855 * Update the printer-type value as needed...
1858 if (p
->num_auth_info_required
> 1 ||
1859 strcmp(p
->auth_info_required
[0], "none"))
1860 p
->type
|= CUPS_PRINTER_AUTHENTICATED
;
1862 p
->type
&= ~CUPS_PRINTER_AUTHENTICATED
;
1868 * Grab values from an attribute instead...
1871 if (!attr
|| attr
->num_values
> 4)
1875 * Update the printer-type value as needed...
1878 if (attr
->num_values
> 1 ||
1879 strcmp(attr
->values
[0].string
.text
, "none"))
1880 p
->type
|= CUPS_PRINTER_AUTHENTICATED
;
1882 p
->type
&= ~CUPS_PRINTER_AUTHENTICATED
;
1884 for (i
= 0; i
< attr
->num_values
; i
++)
1886 if (!strcmp(attr
->values
[i
].string
.text
, "none"))
1888 if (p
->num_auth_info_required
!= 0 || attr
->num_values
!= 1)
1891 p
->auth_info_required
[p
->num_auth_info_required
] = "none";
1892 p
->num_auth_info_required
++;
1896 else if (!strcmp(attr
->values
[i
].string
.text
, "negotiate"))
1898 if (p
->num_auth_info_required
!= 0 || attr
->num_values
!= 1)
1901 p
->auth_info_required
[p
->num_auth_info_required
] = "negotiate";
1902 p
->num_auth_info_required
++;
1906 else if (!strcmp(attr
->values
[i
].string
.text
, "domain"))
1908 p
->auth_info_required
[p
->num_auth_info_required
] = "domain";
1909 p
->num_auth_info_required
++;
1911 else if (!strcmp(attr
->values
[i
].string
.text
, "password"))
1913 p
->auth_info_required
[p
->num_auth_info_required
] = "password";
1914 p
->num_auth_info_required
++;
1916 else if (!strcmp(attr
->values
[i
].string
.text
, "username"))
1918 p
->auth_info_required
[p
->num_auth_info_required
] = "username";
1919 p
->num_auth_info_required
++;
1930 * 'cupsdSetDeviceURI()' - Set the device URI for a printer.
1934 cupsdSetDeviceURI(cupsd_printer_t
*p
, /* I - Printer */
1935 const char *uri
) /* I - Device URI */
1937 char buffer
[1024], /* URI buffer */
1938 *start
, /* Start of data after scheme */
1939 *slash
, /* First slash after scheme:// */
1940 *ptr
; /* Pointer into user@host:port part */
1944 * Set the full device URI..
1947 cupsdSetString(&(p
->device_uri
), uri
);
1950 * Copy the device URI to a temporary buffer so we can sanitize any auth
1954 strlcpy(buffer
, uri
, sizeof(buffer
));
1957 * Find the end of the scheme:// part...
1960 if ((ptr
= strchr(buffer
, ':')) != NULL
)
1962 for (start
= ptr
+ 1; *start
; start
++)
1967 * Find the next slash (/) in the URI...
1970 if ((slash
= strchr(start
, '/')) == NULL
)
1971 slash
= start
+ strlen(start
); /* No slash, point to the end */
1974 * Check for an @ sign before the slash...
1977 if ((ptr
= strchr(start
, '@')) != NULL
&& ptr
< slash
)
1980 * Found an @ sign and it is before the resource part, so we have
1981 * an authentication string. Copy the remaining URI over the
1982 * authentication string...
1985 _cups_strcpy(start
, ptr
+ 1);
1990 * Save the sanitized URI...
1993 cupsdSetString(&(p
->sanitized_device_uri
), buffer
);
1998 * 'cupsdSetPrinterAttr()' - Set a printer attribute.
2002 cupsdSetPrinterAttr(
2003 cupsd_printer_t
*p
, /* I - Printer */
2004 const char *name
, /* I - Attribute name */
2005 char *value
) /* I - Attribute value string */
2007 ipp_attribute_t
*attr
; /* Attribute */
2008 int i
, /* Looping var */
2009 count
; /* Number of values */
2010 char *ptr
; /* Pointer into value */
2011 ipp_tag_t value_tag
; /* Value tag for this attribute */
2015 * Don't allow empty values...
2018 if (!*value
&& strcmp(name
, "marker-message"))
2020 cupsdLogMessage(CUPSD_LOG_ERROR
, "Ignoring empty \"%s\" attribute", name
);
2025 * Count the number of values...
2028 for (count
= 1, ptr
= value
;
2029 (ptr
= strchr(ptr
, ',')) != NULL
;
2033 * Then add or update the attribute as needed...
2036 if (!strcmp(name
, "marker-levels") || !strcmp(name
, "marker-low-levels") ||
2037 !strcmp(name
, "marker-high-levels"))
2043 if ((attr
= ippFindAttribute(p
->attrs
, name
, IPP_TAG_INTEGER
)) != NULL
&&
2044 attr
->num_values
< count
)
2046 ippDeleteAttribute(p
->attrs
, attr
);
2051 attr
->num_values
= count
;
2053 attr
= ippAddIntegers(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, name
,
2058 cupsdLogMessage(CUPSD_LOG_ERROR
,
2059 "Unable to allocate memory for printer attribute "
2060 "(%d values)", count
);
2064 for (i
= 0; i
< count
; i
++)
2066 if ((ptr
= strchr(value
, ',')) != NULL
)
2069 attr
->values
[i
].integer
= strtol(value
, NULL
, 10);
2078 * Name or keyword values...
2081 if (!strcmp(name
, "marker-types"))
2082 value_tag
= IPP_TAG_KEYWORD
;
2083 else if (!strcmp(name
, "marker-message"))
2084 value_tag
= IPP_TAG_TEXT
;
2086 value_tag
= IPP_TAG_NAME
;
2088 if ((attr
= ippFindAttribute(p
->attrs
, name
, value_tag
)) != NULL
&&
2089 attr
->num_values
< count
)
2091 ippDeleteAttribute(p
->attrs
, attr
);
2097 for (i
= 0; i
< attr
->num_values
; i
++)
2098 _cupsStrFree(attr
->values
[i
].string
.text
);
2100 attr
->num_values
= count
;
2103 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, value_tag
, name
,
2108 cupsdLogMessage(CUPSD_LOG_ERROR
,
2109 "Unable to allocate memory for printer attribute "
2110 "(%d values)", count
);
2114 for (i
= 0; i
< count
; i
++)
2116 if ((ptr
= strchr(value
, ',')) != NULL
)
2119 attr
->values
[i
].string
.text
= _cupsStrAlloc(value
);
2129 * 'cupsdSetPrinterAttrs()' - Set printer attributes based upon the PPD file.
2133 cupsdSetPrinterAttrs(cupsd_printer_t
*p
)/* I - Printer to setup */
2135 int i
, /* Looping var */
2136 length
; /* Length of browse attributes */
2137 char resource
[HTTP_MAX_URI
]; /* Resource portion of URI */
2138 int num_air
; /* Number of auth-info-required values */
2139 const char * const *air
; /* auth-info-required values */
2140 cupsd_location_t
*auth
; /* Pointer to authentication element */
2141 const char *auth_supported
; /* Authentication supported */
2142 ipp_t
*oldattrs
; /* Old printer attributes */
2143 ipp_attribute_t
*attr
; /* Attribute data */
2144 cups_option_t
*option
; /* Current printer option */
2145 char *filter
; /* Current filter */
2146 static const char * const air_none
[] =
2147 { /* No authentication */
2150 static const char * const air_userpass
[] =
2151 { /* Basic/Digest 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 else if ((p
->type
& CUPS_PRINTER_AUTHENTICATED
) &&
2187 (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
)
2211 auth_supported
= "basic";
2212 else if (auth_type
== CUPSD_AUTH_DIGEST
)
2213 auth_supported
= "digest";
2215 else if (auth_type
== CUPSD_AUTH_NEGOTIATE
)
2216 auth_supported
= "negotiate";
2217 #endif /* HAVE_GSSAPI */
2219 if (!(p
->type
& CUPS_PRINTER_DISCOVERED
))
2221 if (auth_type
!= CUPSD_AUTH_NONE
)
2222 p
->type
|= CUPS_PRINTER_AUTHENTICATED
;
2224 p
->type
&= ~CUPS_PRINTER_AUTHENTICATED
;
2227 else if (!(p
->type
& CUPS_PRINTER_DISCOVERED
))
2228 p
->type
&= ~CUPS_PRINTER_AUTHENTICATED
;
2231 * Create the required IPP attributes for a printer...
2234 oldattrs
= p
->attrs
;
2235 p
->attrs
= ippNew();
2237 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2238 "uri-authentication-supported", NULL
, auth_supported
);
2239 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2240 "uri-security-supported", NULL
, "none");
2241 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "printer-name", NULL
,
2243 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-location",
2244 NULL
, p
->location
? p
->location
: "");
2245 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "printer-info",
2246 NULL
, p
->info
? p
->info
: "");
2251 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2252 "requesting-user-name-denied", p
->num_users
, NULL
,
2255 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2256 "requesting-user-name-allowed", p
->num_users
, NULL
,
2260 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2261 "job-quota-period", p
->quota_period
);
2262 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2263 "job-k-limit", p
->k_limit
);
2264 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2265 "job-page-limit", p
->page_limit
);
2266 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2267 "auth-info-required", num_air
, NULL
, air
);
2269 if (cupsArrayCount(Banners
) > 0 && !(p
->type
& CUPS_PRINTER_DISCOVERED
))
2272 * Setup the job-sheets-default attribute...
2275 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2276 "job-sheets-default", 2, NULL
, NULL
);
2280 attr
->values
[0].string
.text
= _cupsStrAlloc(Classification
?
2281 Classification
: p
->job_sheets
[0]);
2282 attr
->values
[1].string
.text
= _cupsStrAlloc(Classification
?
2283 Classification
: p
->job_sheets
[1]);
2290 if (p
->type
& CUPS_PRINTER_DISCOVERED
)
2293 * Tell the client this is a remote printer of some type...
2296 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
2297 "printer-uri-supported", NULL
, p
->uri
);
2299 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "printer-more-info",
2303 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
2304 "printer-make-and-model", NULL
, p
->make_model
);
2306 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri", NULL
,
2315 * Assign additional attributes depending on whether this is a printer
2319 if (p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
))
2322 p
->type
&= ~CUPS_PRINTER_OPTIONS
;
2325 * Add class-specific attributes...
2328 if ((p
->type
& CUPS_PRINTER_IMPLICIT
) && p
->num_printers
> 0 &&
2329 p
->printers
[0]->make_model
)
2330 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
2331 "printer-make-and-model", NULL
, p
->printers
[0]->make_model
);
2333 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
2334 "printer-make-and-model", NULL
, "Local Printer Class");
2336 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri", NULL
,
2337 "file:///dev/null");
2339 if (p
->num_printers
> 0)
2342 * Add a list of member names; URIs are added in copy_printer_attrs...
2345 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2346 "member-names", p
->num_printers
, NULL
, NULL
);
2347 p
->type
|= CUPS_PRINTER_OPTIONS
;
2349 for (i
= 0; i
< p
->num_printers
; i
++)
2352 attr
->values
[i
].string
.text
= _cupsStrRetain(p
->printers
[i
]->name
);
2354 p
->type
&= ~CUPS_PRINTER_OPTIONS
| p
->printers
[i
]->type
;
2361 * Add printer-specific attributes...
2364 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
, "device-uri", NULL
,
2365 p
->sanitized_device_uri
);
2368 * Assign additional attributes from the PPD file (if any)...
2374 * Add filters for printer...
2377 cupsdSetPrinterReasons(p
, "-cups-missing-filter-warning,"
2378 "cups-insecure-filter-warning");
2380 for (filter
= (char *)cupsArrayFirst(p
->filters
);
2382 filter
= (char *)cupsArrayNext(p
->filters
))
2383 add_printer_filter(p
, p
->filetype
, filter
);
2387 p
->prefiltertype
= mimeAddType(MimeDatabase
, "prefilter", p
->name
);
2389 for (filter
= (char *)cupsArrayFirst(p
->pre_filters
);
2391 filter
= (char *)cupsArrayNext(p
->pre_filters
))
2392 add_printer_filter(p
, p
->prefiltertype
, filter
);
2398 * Copy marker attributes as needed...
2403 ipp_attribute_t
*oldattr
; /* Old attribute */
2406 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-colors",
2407 IPP_TAG_NAME
)) != NULL
)
2409 if ((attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2410 "marker-colors", oldattr
->num_values
, NULL
,
2413 for (i
= 0; i
< oldattr
->num_values
; i
++)
2414 attr
->values
[i
].string
.text
=
2415 _cupsStrRetain(oldattr
->values
[i
].string
.text
);
2419 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-levels",
2420 IPP_TAG_INTEGER
)) != NULL
)
2422 if ((attr
= ippAddIntegers(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2423 "marker-levels", oldattr
->num_values
,
2426 for (i
= 0; i
< oldattr
->num_values
; i
++)
2427 attr
->values
[i
].integer
= oldattr
->values
[i
].integer
;
2431 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-message",
2432 IPP_TAG_TEXT
)) != NULL
)
2433 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
, "marker-message",
2434 NULL
, oldattr
->values
[0].string
.text
);
2436 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-low-levels",
2437 IPP_TAG_INTEGER
)) != NULL
)
2439 if ((attr
= ippAddIntegers(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2440 "marker-low-levels", oldattr
->num_values
,
2443 for (i
= 0; i
< oldattr
->num_values
; i
++)
2444 attr
->values
[i
].integer
= oldattr
->values
[i
].integer
;
2448 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-high-levels",
2449 IPP_TAG_INTEGER
)) != NULL
)
2451 if ((attr
= ippAddIntegers(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
2452 "marker-high-levels", oldattr
->num_values
,
2455 for (i
= 0; i
< oldattr
->num_values
; i
++)
2456 attr
->values
[i
].integer
= oldattr
->values
[i
].integer
;
2460 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-names",
2461 IPP_TAG_NAME
)) != NULL
)
2463 if ((attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
2464 "marker-names", oldattr
->num_values
, NULL
,
2467 for (i
= 0; i
< oldattr
->num_values
; i
++)
2468 attr
->values
[i
].string
.text
=
2469 _cupsStrRetain(oldattr
->values
[i
].string
.text
);
2473 if ((oldattr
= ippFindAttribute(oldattrs
, "marker-types",
2474 IPP_TAG_KEYWORD
)) != NULL
)
2476 if ((attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
2477 "marker-types", oldattr
->num_values
, NULL
,
2480 for (i
= 0; i
< oldattr
->num_values
; i
++)
2481 attr
->values
[i
].string
.text
=
2482 _cupsStrRetain(oldattr
->values
[i
].string
.text
);
2486 ippDelete(oldattrs
);
2490 * Force sharing off for remote queues...
2493 if (p
->type
& (CUPS_PRINTER_REMOTE
| CUPS_PRINTER_IMPLICIT
))
2498 * Copy the printer options into a browse attributes string we can re-use.
2501 const char *valptr
; /* Pointer into value */
2502 char *attrptr
; /* Pointer into attribute string */
2506 * Free the old browse attributes as needed...
2509 if (p
->browse_attrs
)
2510 free(p
->browse_attrs
);
2513 * Compute the length of all attributes + job-sheets, lease-duration,
2514 * and BrowseLocalOptions.
2517 for (length
= 1, i
= p
->num_options
, option
= p
->options
;
2521 length
+= strlen(option
->name
) + 2;
2525 for (valptr
= option
->value
; *valptr
; valptr
++)
2526 if (strchr(" \"\'\\", *valptr
))
2533 length
+= 13 + strlen(p
->job_sheets
[0]) + strlen(p
->job_sheets
[1]);
2535 if (BrowseLocalOptions
)
2536 length
+= 12 + strlen(BrowseLocalOptions
);
2539 * Allocate the new string...
2542 if ((p
->browse_attrs
= calloc(1, length
)) == NULL
)
2543 cupsdLogMessage(CUPSD_LOG_ERROR
,
2544 "Unable to allocate %d bytes for browse data!",
2549 * Got the allocated string, now copy the options and attributes over...
2552 sprintf(p
->browse_attrs
, "job-sheets=%s,%s lease-duration=%d",
2553 p
->job_sheets
[0], p
->job_sheets
[1], BrowseTimeout
);
2554 attrptr
= p
->browse_attrs
+ strlen(p
->browse_attrs
);
2556 if (BrowseLocalOptions
)
2558 sprintf(attrptr
, " ipp-options=%s", BrowseLocalOptions
);
2559 attrptr
+= strlen(attrptr
);
2562 for (i
= p
->num_options
, option
= p
->options
;
2567 strcpy(attrptr
, option
->name
);
2568 attrptr
+= strlen(attrptr
);
2574 for (valptr
= option
->value
; *valptr
; valptr
++)
2576 if (strchr(" \"\'\\", *valptr
))
2579 *attrptr
++ = *valptr
;
2587 * Populate the document-format-supported attribute...
2590 add_printer_formats(p
);
2592 DEBUG_printf(("cupsdSetPrinterAttrs: leaving name = %s, type = %x\n", p
->name
,
2596 * Add name-default attributes...
2599 add_printer_defaults(p
);
2603 * Write the IRIX printer config and status files...
2606 write_irix_config(p
);
2607 write_irix_state(p
);
2611 * Let the browse protocols reflect the change
2614 cupsdRegisterPrinter(p
);
2619 * 'cupsdSetPrinterReasons()' - Set/update the reasons strings.
2622 int /* O - 1 if something changed, 0 otherwise */
2623 cupsdSetPrinterReasons(
2624 cupsd_printer_t
*p
, /* I - Printer */
2625 const char *s
) /* I - Reasons strings */
2627 int i
, /* Looping var */
2628 changed
= 0; /* Did something change? */
2629 const char *sptr
; /* Pointer into reasons */
2630 char reason
[255], /* Reason string */
2631 *rptr
; /* Pointer into reason */
2634 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
2635 "cupsdSetPrinterReasons(p=%p(%s),s=\"%s\"", p
, p
->name
, s
);
2637 if (s
[0] == '-' || s
[0] == '+')
2640 * Add/remove reasons...
2648 * Replace reasons...
2653 for (i
= 0; i
< p
->num_reasons
; i
++)
2654 _cupsStrFree(p
->reasons
[i
]);
2659 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
2661 if (PrintcapFormat
== PRINTCAP_PLIST
)
2662 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2665 if (!strcmp(s
, "none"))
2669 * Loop through all of the reasons...
2675 * Skip leading whitespace and commas...
2678 while (isspace(*sptr
& 255) || *sptr
== ',')
2681 for (rptr
= reason
; *sptr
&& !isspace(*sptr
& 255) && *sptr
!= ','; sptr
++)
2682 if (rptr
< (reason
+ sizeof(reason
) - 1))
2696 for (i
= 0; i
< p
->num_reasons
; i
++)
2697 if (!strcmp(reason
, p
->reasons
[i
]))
2700 * Found a match, so remove it...
2705 _cupsStrFree(p
->reasons
[i
]);
2707 if (i
< p
->num_reasons
)
2708 memmove(p
->reasons
+ i
, p
->reasons
+ i
+ 1,
2709 (p
->num_reasons
- i
) * sizeof(char *));
2711 if (!strcmp(reason
, "paused") && p
->state
== IPP_PRINTER_STOPPED
)
2712 cupsdSetPrinterState(p
, IPP_PRINTER_IDLE
, 1);
2714 if (strcmp(reason
, "connecting-to-device"))
2716 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
2718 if (PrintcapFormat
== PRINTCAP_PLIST
)
2719 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2724 else if (p
->num_reasons
< (int)(sizeof(p
->reasons
) / sizeof(p
->reasons
[0])))
2730 for (i
= 0; i
< p
->num_reasons
; i
++)
2731 if (!strcmp(reason
, p
->reasons
[i
]))
2734 if (i
>= p
->num_reasons
)
2736 if (i
>= (int)(sizeof(p
->reasons
) / sizeof(p
->reasons
[0])))
2738 cupsdLogMessage(CUPSD_LOG_ALERT
,
2739 "Too many printer-state-reasons values for %s (%d)",
2744 p
->reasons
[i
] = _cupsStrAlloc(reason
);
2748 if (!strcmp(reason
, "paused") && p
->state
!= IPP_PRINTER_STOPPED
)
2749 cupsdSetPrinterState(p
, IPP_PRINTER_STOPPED
, 1);
2751 if (strcmp(reason
, "connecting-to-device"))
2753 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
2755 if (PrintcapFormat
== PRINTCAP_PLIST
)
2756 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2767 * 'cupsdSetPrinterState()' - Update the current state of a printer.
2771 cupsdSetPrinterState(
2772 cupsd_printer_t
*p
, /* I - Printer to change */
2773 ipp_pstate_t s
, /* I - New state */
2774 int update
) /* I - Update printers.conf? */
2776 ipp_pstate_t old_state
; /* Old printer state */
2777 static const char * const printer_states
[] =
2778 { /* State strings */
2786 * Can't set status of remote printers...
2789 if (p
->type
& CUPS_PRINTER_DISCOVERED
)
2793 * Set the new state...
2796 if (PrintcapFormat
== PRINTCAP_PLIST
)
2797 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP
);
2799 old_state
= p
->state
;
2804 cupsdAddEvent(s
== IPP_PRINTER_STOPPED
? CUPSD_EVENT_PRINTER_STOPPED
:
2805 CUPSD_EVENT_PRINTER_STATE
, p
, NULL
,
2806 "%s \"%s\" state changed to %s.",
2807 (p
->type
& CUPS_PRINTER_CLASS
) ? "Class" : "Printer",
2808 p
->name
, printer_states
[p
->state
]);
2811 * Let the browse code know this needs to be updated...
2815 p
->state_time
= time(NULL
);
2819 write_irix_state(p
);
2824 * Set/clear the paused reason as needed...
2827 if (s
== IPP_PRINTER_STOPPED
)
2828 cupsdSetPrinterReasons(p
, "+paused");
2830 cupsdSetPrinterReasons(p
, "-paused");
2833 * Clear the message for the queue when going to processing...
2836 if (s
== IPP_PRINTER_PROCESSING
)
2837 p
->state_message
[0] = '\0';
2840 * Update the printer history...
2843 cupsdAddPrinterHistory(p
);
2846 * Let the browse protocols reflect the change...
2850 cupsdRegisterPrinter(p
);
2853 * Save the printer configuration if a printer goes from idle or processing
2854 * to stopped (or visa-versa)...
2858 (old_state
== IPP_PRINTER_STOPPED
) != (s
== IPP_PRINTER_STOPPED
))
2860 if (p
->type
& CUPS_PRINTER_CLASS
)
2861 cupsdMarkDirty(CUPSD_DIRTY_CLASSES
);
2863 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
2869 * 'cupsdStopPrinter()' - Stop a printer from printing any jobs...
2873 cupsdStopPrinter(cupsd_printer_t
*p
, /* I - Printer to stop */
2874 int update
)/* I - Update printers.conf? */
2877 * Set the printer state...
2880 cupsdSetPrinterState(p
, IPP_PRINTER_STOPPED
, update
);
2883 * See if we have a job printing on this printer...
2886 if (p
->job
&& p
->job
->state_value
== IPP_JOB_PROCESSING
)
2887 cupsdSetJobState(p
->job
, IPP_JOB_PENDING
, CUPSD_JOB_DEFAULT
,
2888 "Job stopped due to printer being paused.");
2893 * 'cupsdUpdatePrinterPPD()' - Update keywords in a printer's PPD file.
2896 int /* O - 1 if successful, 0 otherwise */
2897 cupsdUpdatePrinterPPD(
2898 cupsd_printer_t
*p
, /* I - Printer */
2899 int num_keywords
, /* I - Number of keywords */
2900 cups_option_t
*keywords
) /* I - Keywords */
2902 int i
; /* Looping var */
2903 cups_file_t
*src
, /* Original file */
2904 *dst
; /* New file */
2905 char srcfile
[1024], /* Original filename */
2906 dstfile
[1024], /* New filename */
2907 line
[1024], /* Line from file */
2908 keystring
[41]; /* Keyword from line */
2909 cups_option_t
*keyword
; /* Current keyword */
2912 cupsdLogMessage(CUPSD_LOG_INFO
, "Updating keywords in PPD file for %s...",
2916 * Get the old and new PPD filenames...
2919 snprintf(srcfile
, sizeof(srcfile
), "%s/ppd/%s.ppd.O", ServerRoot
, p
->name
);
2920 snprintf(dstfile
, sizeof(srcfile
), "%s/ppd/%s.ppd", ServerRoot
, p
->name
);
2923 * Rename the old file and open the old and new...
2926 if (rename(dstfile
, srcfile
))
2928 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to backup PPD file for %s: %s",
2929 p
->name
, strerror(errno
));
2933 if ((src
= cupsFileOpen(srcfile
, "r")) == NULL
)
2935 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to open PPD file \"%s\": %s",
2936 srcfile
, strerror(errno
));
2937 rename(srcfile
, dstfile
);
2941 if ((dst
= cupsFileOpen(dstfile
, "w")) == NULL
)
2943 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to create PPD file \"%s\": %s",
2944 dstfile
, strerror(errno
));
2946 rename(srcfile
, dstfile
);
2951 * Copy the first line and then write out all of the keywords...
2954 if (!cupsFileGets(src
, line
, sizeof(line
)))
2956 cupsdLogMessage(CUPSD_LOG_ERROR
, "Unable to read PPD file \"%s\": %s",
2957 srcfile
, strerror(errno
));
2960 rename(srcfile
, dstfile
);
2964 cupsFilePrintf(dst
, "%s\n", line
);
2966 for (i
= num_keywords
, keyword
= keywords
; i
> 0; i
--, keyword
++)
2968 cupsdLogMessage(CUPSD_LOG_DEBUG
, "*%s: %s", keyword
->name
, keyword
->value
);
2969 cupsFilePrintf(dst
, "*%s: %s\n", keyword
->name
, keyword
->value
);
2973 * Then copy the rest of the PPD file, dropping any keywords we changed.
2976 while (cupsFileGets(src
, line
, sizeof(line
)))
2979 * Skip keywords we've already set...
2982 if (sscanf(line
, "*%40[^:]:", keystring
) == 1 &&
2983 cupsGetOption(keystring
, num_keywords
, keywords
))
2987 * Otherwise write the line...
2990 cupsFilePrintf(dst
, "%s\n", line
);
2994 * Close files and return...
3005 * 'cupsdUpdatePrinters()' - Update printers after a partial reload.
3009 cupsdUpdatePrinters(void)
3011 cupsd_printer_t
*p
; /* Current printer */
3015 * Loop through the printers and recreate the printer attributes
3016 * for any local printers since the policy and/or access control
3017 * stuff may have changed. Also, if browsing is disabled, remove
3018 * any remote printers...
3021 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3023 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3026 * Remove remote printers if we are no longer browsing...
3030 (p
->type
& (CUPS_PRINTER_IMPLICIT
| CUPS_PRINTER_DISCOVERED
)))
3032 if (p
->type
& CUPS_PRINTER_IMPLICIT
)
3033 cupsArrayRemove(ImplicitPrinters
, p
);
3035 cupsArraySave(Printers
);
3036 cupsdDeletePrinter(p
, 0);
3037 cupsArrayRestore(Printers
);
3042 * Update the operation policy pointer...
3045 if ((p
->op_policy_ptr
= cupsdFindPolicy(p
->op_policy
)) == NULL
)
3046 p
->op_policy_ptr
= DefaultPolicyPtr
;
3049 * Update printer attributes as needed...
3052 if (!(p
->type
& CUPS_PRINTER_DISCOVERED
))
3053 cupsdSetPrinterAttrs(p
);
3059 * 'cupsdValidateDest()' - Validate a printer/class destination.
3062 const char * /* O - Printer or class name */
3064 const char *uri
, /* I - Printer URI */
3065 cups_ptype_t
*dtype
, /* O - Type (printer or class) */
3066 cupsd_printer_t
**printer
) /* O - Printer pointer */
3068 cupsd_printer_t
*p
; /* Current printer */
3069 char localname
[1024],/* Localized hostname */
3070 *lptr
, /* Pointer into localized hostname */
3071 *sptr
, /* Pointer into server name */
3072 *rptr
, /* Pointer into resource */
3073 scheme
[32], /* Scheme portion of URI */
3074 username
[64], /* Username portion of URI */
3075 hostname
[HTTP_MAX_HOST
],
3076 /* Host portion of URI */
3077 resource
[HTTP_MAX_URI
];
3078 /* Resource portion of URI */
3079 int port
; /* Port portion of URI */
3082 DEBUG_printf(("cupsdValidateDest(uri=\"%s\", dtype=%p, printer=%p)\n", uri
,
3086 * Initialize return values...
3093 *dtype
= (cups_ptype_t
)0;
3096 * Pull the hostname and resource from the URI...
3099 httpSeparateURI(HTTP_URI_CODING_ALL
, uri
, scheme
, sizeof(scheme
),
3100 username
, sizeof(username
), hostname
, sizeof(hostname
),
3101 &port
, resource
, sizeof(resource
));
3104 * See if the resource is a class or printer...
3107 if (!strncmp(resource
, "/classes/", 9))
3113 rptr
= resource
+ 9;
3115 else if (!strncmp(resource
, "/printers/", 10))
3121 rptr
= resource
+ 10;
3126 * Bad resource name...
3133 * See if the printer or class name exists...
3136 p
= cupsdFindDest(rptr
);
3138 if (p
== NULL
&& strchr(rptr
, '@') == NULL
)
3146 *dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
|
3147 CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
);
3153 * Change localhost to the server name...
3156 if (!strcasecmp(hostname
, "localhost"))
3157 strlcpy(hostname
, ServerName
, sizeof(hostname
));
3159 strlcpy(localname
, hostname
, sizeof(localname
));
3161 if (!strcasecmp(hostname
, ServerName
))
3164 * Localize the hostname...
3167 lptr
= strchr(localname
, '.');
3168 sptr
= strchr(ServerName
, '.');
3170 if (sptr
!= NULL
&& lptr
!= NULL
)
3173 * Strip the common domain name components...
3176 while (lptr
!= NULL
)
3178 if (!strcasecmp(lptr
, sptr
))
3184 lptr
= strchr(lptr
+ 1, '.');
3189 DEBUG_printf(("localized hostname is \"%s\"...\n", localname
));
3192 * Find a matching printer or class...
3195 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3197 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3198 if (!strcasecmp(p
->hostname
, localname
) &&
3199 !strcasecmp(p
->name
, rptr
))
3205 *dtype
= p
->type
& (CUPS_PRINTER_CLASS
| CUPS_PRINTER_IMPLICIT
|
3206 CUPS_PRINTER_REMOTE
| CUPS_PRINTER_DISCOVERED
);
3216 * 'cupsdWritePrintcap()' - Write a pseudo-printcap file for older applications
3221 cupsdWritePrintcap(void)
3223 int i
; /* Looping var */
3224 cups_file_t
*fp
; /* Printcap file */
3225 cupsd_printer_t
*p
; /* Current printer */
3230 * Update the IRIX printer state for the default printer; if
3231 * no printers remain, then the default printer file will be
3235 write_irix_state(DefaultPrinter
);
3239 * See if we have a printcap file; if not, don't bother writing it.
3242 if (!Printcap
|| !*Printcap
)
3245 cupsdLogMessage(CUPSD_LOG_INFO
, "Generating printcap %s...", Printcap
);
3248 * Open the printcap file...
3251 if ((fp
= cupsFileOpen(Printcap
, "w")) == NULL
)
3255 * Put a comment header at the top so that users will know where the
3256 * data has come from...
3259 if (PrintcapFormat
!= PRINTCAP_PLIST
)
3260 cupsFilePrintf(fp
, "# This file was automatically generated by cupsd(8) "
3262 "# %s/printers.conf file. All changes to this file\n"
3263 "# will be lost.\n", ServerRoot
);
3266 * Write a new printcap with the current list of printers.
3269 switch (PrintcapFormat
)
3273 * Each printer is put in the file as:
3283 cupsFilePrintf(fp
, "%s|%s:rm=%s:rp=%s:\n", DefaultPrinter
->name
,
3284 DefaultPrinter
->info
, ServerName
,
3285 DefaultPrinter
->name
);
3287 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3289 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3290 if (p
!= DefaultPrinter
)
3291 cupsFilePrintf(fp
, "%s|%s:rm=%s:rp=%s:\n", p
->name
, p
->info
,
3292 ServerName
, p
->name
);
3295 case PRINTCAP_PLIST
:
3297 * Each printer is written as a dictionary in a plist file.
3298 * Currently the printer-name, printer-info, printer-is-accepting-jobs,
3299 * printer-location, printer-make-and-model, printer-state,
3300 * printer-state-reasons, printer-type, and (sanitized) device-uri.
3303 cupsFilePuts(fp
, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
3304 "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD "
3305 "PLIST 1.0//EN\" \"http://www.apple.com/DTDs/"
3306 "PropertyList-1.0.dtd\">\n"
3307 "<plist version=\"1.0\">\n"
3310 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3312 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3314 cupsFilePuts(fp
, "\t<dict>\n"
3315 "\t\t<key>printer-name</key>\n"
3317 write_xml_string(fp
, p
->name
);
3318 cupsFilePuts(fp
, "</string>\n"
3319 "\t\t<key>printer-info</key>\n"
3321 write_xml_string(fp
, p
->info
);
3322 cupsFilePrintf(fp
, "</string>\n"
3323 "\t\t<key>printer-is-accepting-jobs</key>\n"
3325 "\t\t<key>printer-location</key>\n"
3326 "\t\t<string>", p
->accepting
? "true" : "false");
3327 write_xml_string(fp
, p
->location
);
3328 cupsFilePuts(fp
, "</string>\n"
3329 "\t\t<key>printer-make-and-model</key>\n"
3331 write_xml_string(fp
, p
->make_model
);
3332 cupsFilePrintf(fp
, "</string>\n"
3333 "\t\t<key>printer-state</key>\n"
3334 "\t\t<integer>%d</integer>\n"
3335 "\t\t<key>printer-state-reasons</key>\n"
3336 "\t\t<array>\n", p
->state
);
3337 for (i
= 0; i
< p
->num_reasons
; i
++)
3339 cupsFilePuts(fp
, "\t\t\t<string>");
3340 write_xml_string(fp
, p
->reasons
[i
]);
3341 cupsFilePuts(fp
, "</string>\n");
3343 cupsFilePrintf(fp
, "\t\t</array>\n"
3344 "\t\t<key>printer-type</key>\n"
3345 "\t\t<integer>%d</integer>\n"
3346 "\t\t<key>device-uri</key>\n"
3347 "\t\t<string>", p
->type
);
3348 write_xml_string(fp
, p
->sanitized_device_uri
);
3349 cupsFilePuts(fp
, "</string>\n"
3352 cupsFilePuts(fp
, "</array>\n"
3356 case PRINTCAP_SOLARIS
:
3358 * Each printer is put in the file as:
3360 * _all:all=Printer1,Printer2,Printer3,...,PrinterN
3361 * _default:use=DefaultPrinter
3363 * :bsdaddr=ServerName,Printer1:\
3364 * :description=Description:
3366 * :bsdaddr=ServerName,Printer2:\
3367 * :description=Description:
3369 * :bsdaddr=ServerName,Printer3:\
3370 * :description=Description:
3373 * :bsdaddr=ServerName,PrinterN:\
3374 * :description=Description:
3377 cupsFilePuts(fp
, "_all:all=");
3378 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3380 p
= (cupsd_printer_t
*)cupsArrayCurrent(Printers
))
3381 cupsFilePrintf(fp
, "%s%c", p
->name
,
3382 cupsArrayNext(Printers
) ? ',' : '\n');
3385 cupsFilePrintf(fp
, "_default:use=%s\n", DefaultPrinter
->name
);
3387 for (p
= (cupsd_printer_t
*)cupsArrayFirst(Printers
);
3389 p
= (cupsd_printer_t
*)cupsArrayNext(Printers
))
3390 cupsFilePrintf(fp
, "%s:\\\n"
3391 "\t:bsdaddr=%s,%s:\\\n"
3392 "\t:description=%s:\n",
3393 p
->name
, ServerName
, p
->name
,
3394 p
->info
? p
->info
: "");
3407 * 'add_printer_defaults()' - Add name-default attributes to the printer attributes.
3411 add_printer_defaults(cupsd_printer_t
*p
)/* I - Printer */
3413 int i
; /* Looping var */
3414 int num_options
; /* Number of default options */
3415 cups_option_t
*options
, /* Default options */
3416 *option
; /* Current option */
3417 char name
[256]; /* name-default */
3421 * Maintain a common array of default attribute names...
3424 if (!CommonDefaults
)
3426 CommonDefaults
= cupsArrayNew((cups_array_func_t
)strcmp
, NULL
);
3428 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("copies-default"));
3429 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("document-format-default"));
3430 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("finishings-default"));
3431 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("job-hold-until-default"));
3432 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("job-priority-default"));
3433 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("job-sheets-default"));
3434 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("media-default"));
3435 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("number-up-default"));
3436 cupsArrayAdd(CommonDefaults
,
3437 _cupsStrAlloc("orientation-requested-default"));
3438 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc("sides-default"));
3442 * Add all of the default options from the .conf files...
3445 for (num_options
= 0, options
= NULL
, i
= p
->num_options
, option
= p
->options
;
3449 if (strcmp(option
->name
, "ipp-options") &&
3450 strcmp(option
->name
, "job-sheets") &&
3451 strcmp(option
->name
, "lease-duration"))
3453 snprintf(name
, sizeof(name
), "%s-default", option
->name
);
3454 num_options
= cupsAddOption(name
, option
->value
, num_options
, &options
);
3456 if (!cupsArrayFind(CommonDefaults
, name
))
3457 cupsArrayAdd(CommonDefaults
, _cupsStrAlloc(name
));
3462 * Convert options to IPP attributes...
3465 cupsEncodeOptions2(p
->attrs
, num_options
, options
, IPP_TAG_PRINTER
);
3466 cupsFreeOptions(num_options
, options
);
3469 * Add standard -default attributes as needed...
3472 if (!cupsGetOption("copies", p
->num_options
, p
->options
))
3473 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
, "copies-default",
3476 if (!cupsGetOption("document-format", p
->num_options
, p
->options
))
3477 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_MIMETYPE
,
3478 "document-format-default", NULL
, "application/octet-stream");
3480 if (!cupsGetOption("job-hold-until", p
->num_options
, p
->options
))
3481 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
3482 "job-hold-until-default", NULL
, "no-hold");
3484 if (!cupsGetOption("job-priority", p
->num_options
, p
->options
))
3485 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
3486 "job-priority-default", 50);
3488 if (!cupsGetOption("number-up", p
->num_options
, p
->options
))
3489 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
3490 "number-up-default", 1);
3492 if (!cupsGetOption("orientation-requested", p
->num_options
, p
->options
))
3493 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_NOVALUE
,
3494 "orientation-requested-default", NULL
, NULL
);
3496 if (!cupsGetOption("notify-lease-duration", p
->num_options
, p
->options
))
3497 ippAddInteger(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
3498 "notify-lease-duration-default", DefaultLeaseDuration
);
3500 if (!cupsGetOption("notify-events", p
->num_options
, p
->options
))
3501 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
3502 "notify-events-default", NULL
, "job-completed");
3507 * 'add_printer_filter()' - Add a MIME filter for a printer.
3512 cupsd_printer_t
*p
, /* I - Printer to add to */
3513 mime_type_t
*filtertype
, /* I - Filter or prefilter MIME type */
3514 const char *filter
) /* I - Filter to add */
3516 char super
[MIME_MAX_SUPER
], /* Super-type for filter */
3517 type
[MIME_MAX_TYPE
], /* Type for filter */
3518 program
[1024]; /* Program/filter name */
3519 int cost
; /* Cost of filter */
3520 mime_type_t
*temptype
; /* MIME type looping var */
3521 char filename
[1024], /* Full filter filename */
3522 *dirsep
; /* Pointer to directory separator */
3523 struct stat fileinfo
; /* File information */
3527 * Parse the filter string; it should be in the following format:
3529 * super/type cost program
3532 if (sscanf(filter
, "%15[^/]/%31s%d%*[ \t]%1023[^\n]", super
, type
, &cost
,
3535 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s: invalid filter string \"%s\"!",
3541 * See if the filter program exists; if not, stop the printer and flag
3545 if (strcmp(program
, "-"))
3547 if (program
[0] == '/')
3548 strlcpy(filename
, program
, sizeof(filename
));
3550 snprintf(filename
, sizeof(filename
), "%s/filter/%s", ServerBin
, program
);
3552 if (stat(filename
, &fileinfo
))
3554 memset(&fileinfo
, 0, sizeof(fileinfo
));
3556 snprintf(p
->state_message
, sizeof(p
->state_message
),
3557 "Filter \"%s\" for printer \"%s\" not available: %s",
3558 filename
, p
->name
, strerror(errno
));
3559 cupsdSetPrinterReasons(p
, "+cups-missing-filter-warning");
3561 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s", p
->state_message
);
3565 * When running as root, do additional security checks...
3571 * Only use filters that are owned by root and do not have group or world
3572 * write permissions.
3575 if (fileinfo
.st_uid
||
3576 (fileinfo
.st_mode
& (S_ISUID
| S_IWGRP
| S_IWOTH
)) != 0)
3578 if (fileinfo
.st_uid
)
3579 snprintf(p
->state_message
, sizeof(p
->state_message
),
3580 "Filter \"%s\" for printer \"%s\" not owned by root",
3583 snprintf(p
->state_message
, sizeof(p
->state_message
),
3584 "Filter \"%s\" for printer \"%s\" has insecure permissions "
3585 "(0%o)", filename
, p
->name
, fileinfo
.st_mode
);
3587 cupsdSetPrinterReasons(p
, "+cups-insecure-filter-warning");
3589 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s", p
->state_message
);
3591 else if (fileinfo
.st_mode
)
3594 * Similarly, check that the parent directory is also owned by root and
3595 * does not have world write permissions.
3598 if ((dirsep
= strrchr(filename
, '/')) != NULL
)
3601 if (!stat(filename
, &fileinfo
) &&
3603 (fileinfo
.st_mode
& (S_ISUID
| S_IWGRP
| S_IWOTH
)) != 0))
3605 if (fileinfo
.st_uid
)
3606 snprintf(p
->state_message
, sizeof(p
->state_message
),
3607 "Filter directory \"%s\" for printer \"%s\" not owned by "
3608 "root", filename
, p
->name
);
3610 snprintf(p
->state_message
, sizeof(p
->state_message
),
3611 "Filter directory \"%s\" for printer \"%s\" has insecure "
3612 "permissions (0%o)", filename
, p
->name
, fileinfo
.st_mode
);
3614 cupsdSetPrinterReasons(p
, "+cups-insecure-filter-warning");
3616 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s", p
->state_message
);
3623 * Add the filter to the MIME database, supporting wildcards as needed...
3626 for (temptype
= mimeFirstType(MimeDatabase
);
3628 temptype
= mimeNextType(MimeDatabase
))
3629 if (((super
[0] == '*' && strcasecmp(temptype
->super
, "printer")) ||
3630 !strcasecmp(temptype
->super
, super
)) &&
3631 (type
[0] == '*' || !strcasecmp(temptype
->type
, type
)))
3633 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3634 "add_printer_filter: %s: adding filter %s/%s %s/%s %d %s",
3635 p
->name
, temptype
->super
, temptype
->type
,
3636 filtertype
->super
, filtertype
->type
,
3638 mimeAddFilter(MimeDatabase
, temptype
, filtertype
, cost
, program
);
3644 * 'add_printer_formats()' - Add document-format-supported values for a printer.
3648 add_printer_formats(cupsd_printer_t
*p
) /* I - Printer */
3650 int i
; /* Looping var */
3651 mime_type_t
*type
; /* Current MIME type */
3652 cups_array_t
*filters
; /* Filters */
3653 ipp_attribute_t
*attr
; /* document-format-supported attribute */
3654 char mimetype
[MIME_MAX_SUPER
+ MIME_MAX_TYPE
+ 2];
3655 /* MIME type name */
3659 * Raw (and remote) queues advertise all of the supported MIME
3663 cupsArrayDelete(p
->filetypes
);
3664 p
->filetypes
= NULL
;
3668 ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
,
3669 (ipp_tag_t
)(IPP_TAG_MIMETYPE
| IPP_TAG_COPY
),
3670 "document-format-supported", NumMimeTypes
, NULL
, MimeTypes
);
3675 * Otherwise, loop through the supported MIME types and see if there
3676 * are filters for them...
3679 cupsdLogMessage(CUPSD_LOG_DEBUG2
, "add_printer_formats: %d types, %d filters",
3680 mimeNumTypes(MimeDatabase
), mimeNumFilters(MimeDatabase
));
3682 p
->filetypes
= cupsArrayNew(NULL
, NULL
);
3684 for (type
= mimeFirstType(MimeDatabase
);
3686 type
= mimeNextType(MimeDatabase
))
3688 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", type
->super
, type
->type
);
3690 if ((filters
= mimeFilter(MimeDatabase
, type
, p
->filetype
, NULL
)) != NULL
)
3692 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3693 "add_printer_formats: %s: %s needs %d filters",
3694 p
->name
, mimetype
, cupsArrayCount(filters
));
3696 cupsArrayDelete(filters
);
3697 cupsArrayAdd(p
->filetypes
, type
);
3700 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3701 "add_printer_formats: %s: %s not supported",
3706 * Add the file formats that can be filtered...
3709 if ((type
= mimeType(MimeDatabase
, "application", "octet-stream")) == NULL
||
3710 !cupsArrayFind(p
->filetypes
, type
))
3715 cupsdLogMessage(CUPSD_LOG_DEBUG2
,
3716 "add_printer_formats: %s: %d supported types",
3717 p
->name
, cupsArrayCount(p
->filetypes
) + i
);
3719 attr
= ippAddStrings(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_MIMETYPE
,
3720 "document-format-supported",
3721 cupsArrayCount(p
->filetypes
) + i
, NULL
, NULL
);
3724 attr
->values
[0].string
.text
= _cupsStrAlloc("application/octet-stream");
3726 for (type
= (mime_type_t
*)cupsArrayFirst(p
->filetypes
);
3728 i
++, type
= (mime_type_t
*)cupsArrayNext(p
->filetypes
))
3730 snprintf(mimetype
, sizeof(mimetype
), "%s/%s", type
->super
, type
->type
);
3732 attr
->values
[i
].string
.text
= _cupsStrAlloc(mimetype
);
3737 char pdl
[1024]; /* Buffer to build pdl list */
3738 mime_filter_t
*filter
; /* MIME filter looping var */
3743 if (mimeType(MimeDatabase
, "application", "pdf"))
3744 strlcat(pdl
, "application/pdf,", sizeof(pdl
));
3746 if (mimeType(MimeDatabase
, "application", "postscript"))
3747 strlcat(pdl
, "application/postscript,", sizeof(pdl
));
3749 if (mimeType(MimeDatabase
, "application", "vnd.cups-raster"))
3750 strlcat(pdl
, "application/vnd.cups-raster,", sizeof(pdl
));
3753 * Determine if this is a Tioga PrintJobMgr based queue...
3756 for (filter
= (mime_filter_t
*)cupsArrayFirst(MimeDatabase
->filters
);
3758 filter
= (mime_filter_t
*)cupsArrayNext(MimeDatabase
->filters
))
3760 if (filter
->dst
== p
->filetype
&& filter
->filter
&&
3761 strstr(filter
->filter
, "PrintJobMgr"))
3766 * We only support raw printing if this is not a Tioga PrintJobMgr based
3767 * queue and if application/octet-stream is a known conversion...
3770 if (!filter
&& mimeType(MimeDatabase
, "application", "octet-stream"))
3771 strlcat(pdl
, "application/octet-stream,", sizeof(pdl
));
3773 if (mimeType(MimeDatabase
, "image", "png"))
3774 strlcat(pdl
, "image/png,", sizeof(pdl
));
3777 pdl
[strlen(pdl
) - 1] = '\0'; /* Remove trailing comma */
3779 cupsdSetString(&p
->pdl
, pdl
);
3781 #endif /* HAVE_DNSSD */
3786 * 'add_string_array()' - Add a string to an array of CUPS strings.
3790 add_string_array(cups_array_t
**a
, /* I - Array */
3791 const char *s
) /* I - String */
3794 *a
= cupsArrayNew(NULL
, NULL
);
3796 cupsArrayAdd(*a
, _cupsStrAlloc(s
));
3801 * 'compare_printers()' - Compare two printers.
3804 static int /* O - Result of comparison */
3805 compare_printers(void *first
, /* I - First printer */
3806 void *second
, /* I - Second printer */
3807 void *data
) /* I - App data (not used) */
3809 return (strcasecmp(((cupsd_printer_t
*)first
)->name
,
3810 ((cupsd_printer_t
*)second
)->name
));
3815 * 'delete_printer_filters()' - Delete all MIME filters for a printer.
3819 delete_printer_filters(
3820 cupsd_printer_t
*p
) /* I - Printer to remove from */
3822 mime_filter_t
*filter
; /* MIME filter looping var */
3826 * Range check input...
3833 * Remove all filters from the MIME database that have a destination
3834 * type == printer...
3837 for (filter
= mimeFirstFilter(MimeDatabase
);
3839 filter
= mimeNextFilter(MimeDatabase
))
3840 if (filter
->dst
== p
->filetype
|| filter
->dst
== p
->prefiltertype
)
3843 * Delete the current filter...
3846 mimeDeleteFilter(MimeDatabase
, filter
);
3849 cupsdSetPrinterReasons(p
, "-cups-insecure-filter-warning"
3850 ",cups-missing-filter-warning");
3855 * 'delete_string_array()' - Delete an array of CUPS strings.
3859 delete_string_array(cups_array_t
**a
) /* I - Array */
3861 char *ptr
; /* Current string */
3864 for (ptr
= (char *)cupsArrayFirst(*a
);
3866 ptr
= (char *)cupsArrayNext(*a
))
3869 cupsArrayDelete(*a
);
3875 * 'load_ppd()' - Load a cached PPD file, updating the cache as needed.
3879 load_ppd(cupsd_printer_t
*p
) /* I - Printer */
3881 int i
; /* Looping var */
3882 cups_file_t
*cache
; /* Cache file */
3883 char cache_name
[1024]; /* Cache filename */
3884 struct stat cache_info
; /* Cache file info */
3885 ppd_file_t
*ppd
; /* PPD file */
3886 char ppd_name
[1024]; /* PPD filename */
3887 struct stat ppd_info
; /* PPD file info */
3888 int num_media
; /* Number of media options */
3889 char custom_in
[256], /* Custom size name in inches */
3890 custom_mm
[256]; /* Custom size name in millimeters */
3891 ppd_size_t
*size
; /* Current size */
3892 ppd_option_t
*output_bin
, /* OutputBin options */
3893 *duplex
; /* Duplex options */
3894 ppd_attr_t
*ppd_attr
; /* PPD attribute */
3895 _cups_pwg_media_t
*pwgmedia
; /* Matching PWG size name */
3896 ipp_attribute_t
*attr
; /* Attribute data */
3897 ipp_t
*media_col_default
, /* media-col-default collection value */
3898 *media_size
; /* media-size collection value */
3899 ipp_value_t
*val
; /* Attribute value */
3900 int num_finishings
; /* Number of finishings */
3901 int finishings
[5]; /* finishings-supported values */
3902 static const char * const sides
[3] = /* sides-supported values */
3905 "two-sided-long-edge",
3906 "two-sided-short-edge"
3908 static const char * const standard_commands
[] =
3909 { /* Standard CUPS commands */
3917 * Check to see if the cache is up-to-date...
3920 snprintf(cache_name
, sizeof(cache_name
), "%s/%s.ipp", CacheDir
, p
->name
);
3921 if (stat(cache_name
, &cache_info
))
3922 cache_info
.st_mtime
= 0;
3924 snprintf(ppd_name
, sizeof(ppd_name
), "%s/ppd/%s.ppd", ServerRoot
, p
->name
);
3925 if (stat(ppd_name
, &ppd_info
))
3926 ppd_info
.st_mtime
= 1;
3928 ippDelete(p
->ppd_attrs
);
3929 p
->ppd_attrs
= ippNew();
3931 if (cache_info
.st_mtime
>= ppd_info
.st_mtime
&&
3932 (cache
= cupsFileOpen(cache_name
, "r")) != NULL
)
3935 * Load cached information and return...
3938 cupsdLogMessage(CUPSD_LOG_DEBUG
, "load_ppd: Loading %s...", cache_name
);
3940 if (ippReadIO(cache
, (ipp_iocb_t
)cupsFileRead
, 1, NULL
,
3941 p
->ppd_attrs
) == IPP_DATA
)
3943 cupsFileClose(cache
);
3947 cupsFileClose(cache
);
3951 * Reload PPD attributes from disk...
3954 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS
);
3956 cupsdLogMessage(CUPSD_LOG_DEBUG
, "load_ppd: Loading %s...", ppd_name
);
3958 delete_string_array(&(p
->filters
));
3959 delete_string_array(&(p
->pre_filters
));
3961 p
->type
&= ~CUPS_PRINTER_OPTIONS
;
3962 p
->type
|= CUPS_PRINTER_BW
;
3964 finishings
[0] = IPP_FINISHINGS_NONE
;
3967 if ((ppd
= ppdOpenFile(ppd_name
)) != NULL
)
3970 * Add make/model and other various attributes...
3973 ppdMarkDefaults(ppd
);
3975 if (ppd
->color_device
)
3976 p
->type
|= CUPS_PRINTER_COLOR
;
3977 if (ppd
->variable_sizes
)
3978 p
->type
|= CUPS_PRINTER_VARIABLE
;
3979 if (!ppd
->manual_copies
)
3980 p
->type
|= CUPS_PRINTER_COPIES
;
3981 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsFax", NULL
)) != NULL
)
3982 if (ppd_attr
->value
&& !strcasecmp(ppd_attr
->value
, "true"))
3983 p
->type
|= CUPS_PRINTER_FAX
;
3985 ippAddBoolean(p
->ppd_attrs
, IPP_TAG_PRINTER
, "color-supported",
3987 if (ppd
->throughput
)
3988 ippAddInteger(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
3989 "pages-per-minute", ppd
->throughput
);
3994 * The NickName can be localized in the character set specified
3995 * by the LanugageEncoding attribute. However, ppdOpen2() has
3996 * already converted the ppd->nickname member to UTF-8 for us
3997 * (the original attribute value is available separately)
4000 cupsdSetString(&p
->make_model
, ppd
->nickname
);
4002 else if (ppd
->modelname
)
4005 * Model name can only contain specific characters...
4008 cupsdSetString(&p
->make_model
, ppd
->modelname
);
4011 cupsdSetString(&p
->make_model
, "Bad PPD File");
4013 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4014 "printer-make-and-model", NULL
, p
->make_model
);
4017 * Add media options from the PPD file...
4020 if (ppd
->num_sizes
== 0)
4022 if (!ppdFindAttr(ppd
, "APScannerOnly", NULL
))
4023 cupsdLogMessage(CUPSD_LOG_CRIT
,
4024 "The PPD file for printer %s contains no media "
4025 "options and is therefore invalid!", p
->name
);
4027 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4028 "media-supported", NULL
, "unknown");
4032 num_media
= ppd
->num_sizes
;
4033 if (ppd
->variable_sizes
)
4036 attr
= ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4037 "media-supported", num_media
, NULL
, NULL
);
4042 for (i
= ppd
->num_sizes
, size
= ppd
->sizes
; i
> 0; i
--, size
++)
4044 if (strcasecmp(size
->name
, "Custom"))
4046 if ((pwgmedia
= _cupsPWGMediaBySize(size
->width
,
4047 size
->length
)) != NULL
)
4049 val
->string
.text
= _cupsStrAlloc(pwgmedia
->pwg
);
4053 snprintf(custom_in
, sizeof(custom_in
), "adobe_%s_%gx%gin",
4054 size
->name
, size
->width
/ 72.0, size
->length
/ 72.0);
4055 snprintf(custom_mm
, sizeof(custom_mm
), "adobe_%s_%gx%gmm",
4056 size
->name
, size
->width
* 25.4 / 72.0,
4057 size
->length
* 25.4 / 72.0);
4058 if (strlen(custom_in
) < strlen(custom_mm
))
4059 val
->string
.text
= _cupsStrAlloc(custom_in
);
4061 val
->string
.text
= _cupsStrAlloc(custom_mm
);
4067 * Add media-default...
4070 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4071 "media-default", NULL
, val
->string
.text
);
4074 * Add media-col-default...
4077 media_size
= ippNew();
4078 ippAddInteger(media_size
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4079 "x-dimension", (int)(size
->width
* 2540.0 / 72.0));
4080 ippAddInteger(media_size
, IPP_TAG_PRINTER
, IPP_TAG_INTEGER
,
4081 "y-dimension", (int)(size
->length
* 2540.0 / 72.0));
4083 media_col_default
= ippNew();
4084 ippAddString(media_col_default
, IPP_TAG_PRINTER
,
4085 IPP_TAG_KEYWORD
| IPP_TAG_COPY
, "media-color", NULL
,
4087 ippAddString(media_col_default
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4088 "media-key", NULL
,val
->string
.text
);
4089 ippAddCollection(media_col_default
, IPP_TAG_PRINTER
, "media-size",
4091 ippAddString(media_col_default
, IPP_TAG_PRINTER
,
4092 IPP_TAG_KEYWORD
| IPP_TAG_COPY
, "media-type", NULL
,
4095 ippAddCollection(p
->ppd_attrs
, IPP_TAG_PRINTER
,
4096 "media-col-default", media_col_default
);
4103 if (ppd
->variable_sizes
)
4105 snprintf(custom_in
, sizeof(custom_in
), "custom_min_%gx%gin",
4106 ppd
->custom_min
[0] / 72.0, ppd
->custom_min
[1] / 72.0);
4107 snprintf(custom_mm
, sizeof(custom_mm
), "custom_min_%gx%gmm",
4108 ppd
->custom_min
[0] * 25.4 / 72.0,
4109 ppd
->custom_min
[1] * 25.4 / 72.0);
4110 if (strlen(custom_in
) < strlen(custom_mm
))
4111 val
->string
.text
= _cupsStrAlloc(custom_in
);
4113 val
->string
.text
= _cupsStrAlloc(custom_mm
);
4116 snprintf(custom_in
, sizeof(custom_in
), "custom_max_%gx%gin",
4117 ppd
->custom_max
[0] / 72.0, ppd
->custom_max
[1] / 72.0);
4118 snprintf(custom_mm
, sizeof(custom_mm
), "custom_max_%gx%gmm",
4119 ppd
->custom_max
[0] * 25.4 / 72.0,
4120 ppd
->custom_max
[1] * 25.4 / 72.0);
4121 if (strlen(custom_in
) < strlen(custom_mm
))
4122 val
->string
.text
= _cupsStrAlloc(custom_in
);
4124 val
->string
.text
= _cupsStrAlloc(custom_mm
);
4133 if ((output_bin
= ppdFindOption(ppd
, "OutputBin")) != NULL
)
4135 attr
= ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4136 "output-bin-supported", output_bin
->num_choices
,
4141 for (i
= 0, val
= attr
->values
;
4142 i
< output_bin
->num_choices
;
4144 val
->string
.text
= _cupsStrAlloc(output_bin
->choices
[i
].choice
);
4147 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4148 "output-bin-default", NULL
, output_bin
->defchoice
);
4155 if ((duplex
= ppdFindOption(ppd
, "Duplex")) == NULL
)
4156 if ((duplex
= ppdFindOption(ppd
, "EFDuplex")) == NULL
)
4157 if ((duplex
= ppdFindOption(ppd
, "EFDuplexing")) == NULL
)
4158 if ((duplex
= ppdFindOption(ppd
, "KD03Duplex")) == NULL
)
4159 duplex
= ppdFindOption(ppd
, "JCLDuplex");
4161 if (duplex
&& duplex
->num_choices
> 1 &&
4162 !ppdInstallableConflict(ppd
, duplex
->keyword
, "DuplexTumble"))
4164 p
->type
|= CUPS_PRINTER_DUPLEX
;
4166 ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4167 "sides-supported", 3, NULL
, sides
);
4169 if (!strcasecmp(duplex
->defchoice
, "DuplexTumble"))
4170 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4171 "sides-default", NULL
, "two-sided-short-edge");
4172 else if (!strcasecmp(duplex
->defchoice
, "DuplexNoTumble"))
4173 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4174 "sides-default", NULL
, "two-sided-long-edge");
4176 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4177 "sides-default", NULL
, "one-sided");
4180 if (ppdFindOption(ppd
, "Collate") != NULL
)
4181 p
->type
|= CUPS_PRINTER_COLLATE
;
4183 if (ppdFindOption(ppd
, "StapleLocation") != NULL
)
4185 p
->type
|= CUPS_PRINTER_STAPLE
;
4186 finishings
[num_finishings
++] = IPP_FINISHINGS_STAPLE
;
4189 if (ppdFindOption(ppd
, "BindEdge") != NULL
)
4191 p
->type
|= CUPS_PRINTER_BIND
;
4192 finishings
[num_finishings
++] = IPP_FINISHINGS_BIND
;
4195 for (i
= 0; i
< ppd
->num_sizes
; i
++)
4196 if (ppd
->sizes
[i
].length
> 1728)
4197 p
->type
|= CUPS_PRINTER_LARGE
;
4198 else if (ppd
->sizes
[i
].length
> 1008)
4199 p
->type
|= CUPS_PRINTER_MEDIUM
;
4201 p
->type
|= CUPS_PRINTER_SMALL
;
4203 if ((ppd_attr
= ppdFindAttr(ppd
, "APICADriver", NULL
)) != NULL
&&
4204 ppd_attr
->value
&& !strcasecmp(ppd_attr
->value
, "true"))
4206 if ((ppd_attr
= ppdFindAttr(ppd
, "APScannerOnly", NULL
)) != NULL
&&
4207 ppd_attr
->value
&& !strcasecmp(ppd_attr
->value
, "true"))
4208 p
->type
|= CUPS_PRINTER_SCANNER
;
4210 p
->type
|= CUPS_PRINTER_MFP
;
4214 * Add a filter from application/vnd.cups-raw to printer/name to
4215 * handle "raw" printing by users.
4218 add_string_array(&(p
->filters
), "application/vnd.cups-raw 0 -");
4221 * Add any pre-filters in the PPD file...
4224 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsPreFilter", NULL
)) != NULL
)
4226 for (; ppd_attr
; ppd_attr
= ppdFindNextAttr(ppd
, "cupsPreFilter", NULL
))
4227 if (ppd_attr
->value
)
4228 add_string_array(&(p
->pre_filters
), ppd_attr
->value
);
4232 * Add any filters in the PPD file...
4235 DEBUG_printf(("ppd->num_filters = %d\n", ppd
->num_filters
));
4236 for (i
= 0; i
< ppd
->num_filters
; i
++)
4238 DEBUG_printf(("ppd->filters[%d] = \"%s\"\n", i
, ppd
->filters
[i
]));
4239 add_string_array(&(p
->filters
), ppd
->filters
[i
]);
4241 if (!strncasecmp(ppd
->filters
[i
], "application/vnd.cups-command", 28) &&
4242 isspace(ppd
->filters
[i
][28] & 255))
4243 p
->type
|= CUPS_PRINTER_COMMANDS
;
4246 if (ppd
->num_filters
== 0)
4249 * If there are no filters, add PostScript printing filters.
4252 add_string_array(&(p
->filters
),
4253 "application/vnd.cups-command 0 commandtops");
4254 add_string_array(&(p
->filters
),
4255 "application/vnd.cups-postscript 0 -");
4257 p
->type
|= CUPS_PRINTER_COMMANDS
;
4259 else if (!(p
->type
& CUPS_PRINTER_COMMANDS
))
4262 * See if this is a PostScript device without a command filter...
4265 for (i
= 0; i
< ppd
->num_filters
; i
++)
4266 if (!strncasecmp(ppd
->filters
[i
],
4267 "application/vnd.cups-postscript", 31) &&
4268 isspace(ppd
->filters
[i
][31] & 255))
4271 if (i
< ppd
->num_filters
)
4274 * Add the generic PostScript command filter...
4277 add_string_array(&(p
->filters
),
4278 "application/vnd.cups-command 0 commandtops");
4279 p
->type
|= CUPS_PRINTER_COMMANDS
;
4283 if (p
->type
& CUPS_PRINTER_COMMANDS
)
4285 char *commands
, /* Copy of commands */
4286 *start
, /* Start of name */
4287 *end
; /* End of name */
4288 int count
; /* Number of commands */
4291 if ((ppd_attr
= ppdFindAttr(ppd
, "cupsCommands", NULL
)) != NULL
&&
4292 ppd_attr
->value
&& ppd_attr
->value
[0])
4294 for (count
= 0, start
= ppd_attr
->value
; *start
; count
++)
4296 while (isspace(*start
& 255))
4302 while (*start
&& !isspace(*start
& 255))
4312 * Make a copy of the commands string and count how many ...
4315 attr
= ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4316 "printer-commands", count
, NULL
, NULL
);
4318 commands
= strdup(ppd_attr
->value
);
4320 for (count
= 0, start
= commands
; *start
; count
++)
4322 while (isspace(*start
& 255))
4329 while (*end
&& !isspace(*end
& 255))
4335 attr
->values
[count
].string
.text
= _cupsStrAlloc(start
);
4345 * Add the standard list of commands...
4348 ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4350 (int)(sizeof(standard_commands
) /
4351 sizeof(standard_commands
[0])), NULL
,
4358 * No commands supported...
4361 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_KEYWORD
,
4362 "printer-commands", NULL
, "none");
4366 * Show current and available port monitors for this printer...
4369 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
, "port-monitor",
4370 NULL
, p
->port_monitor
? p
->port_monitor
: "none");
4372 for (i
= 1, ppd_attr
= ppdFindAttr(ppd
, "cupsPortMonitor", NULL
);
4374 i
++, ppd_attr
= ppdFindNextAttr(ppd
, "cupsPortMonitor", NULL
));
4378 if (strstr(ppd
->protocols
, "TBCP"))
4380 else if (strstr(ppd
->protocols
, "BCP"))
4384 attr
= ippAddStrings(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_NAME
,
4385 "port-monitor-supported", i
, NULL
, NULL
);
4387 attr
->values
[0].string
.text
= _cupsStrAlloc("none");
4389 for (i
= 1, ppd_attr
= ppdFindAttr(ppd
, "cupsPortMonitor", NULL
);
4391 i
++, ppd_attr
= ppdFindNextAttr(ppd
, "cupsPortMonitor", NULL
))
4392 attr
->values
[i
].string
.text
= _cupsStrAlloc(ppd_attr
->value
);
4396 if (strstr(ppd
->protocols
, "TBCP"))
4397 attr
->values
[i
].string
.text
= _cupsStrAlloc("tbcp");
4398 else if (strstr(ppd
->protocols
, "BCP"))
4399 attr
->values
[i
].string
.text
= _cupsStrAlloc("bcp");
4403 cupsdSetString(&p
->product
, ppd
->product
);
4404 #endif /* HAVE_DNSSD */
4406 if (ppdFindAttr(ppd
, "APRemoteQueueID", NULL
))
4407 p
->type
|= CUPS_PRINTER_REMOTE
;
4410 * Close the PPD and set the type...
4415 else if (!access(ppd_name
, 0))
4417 int pline
; /* PPD line number */
4418 ppd_status_t pstatus
; /* PPD load status */
4421 pstatus
= ppdLastError(&pline
);
4423 cupsdLogMessage(CUPSD_LOG_ERROR
, "PPD file for %s cannot be loaded!",
4426 if (pstatus
<= PPD_ALLOC_ERROR
)
4427 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s", strerror(errno
));
4429 cupsdLogMessage(CUPSD_LOG_ERROR
, "%s on line %d.",
4430 ppdErrorString(pstatus
), pline
);
4432 cupsdLogMessage(CUPSD_LOG_INFO
,
4433 "Hint: Run \"cupstestppd %s\" and fix any errors.",
4437 * Add a filter from application/vnd.cups-raw to printer/name to
4438 * handle "raw" printing by users.
4441 add_string_array(&(p
->filters
), "application/vnd.cups-raw 0 -");
4444 * Add a PostScript filter, since this is still possibly PS printer.
4447 add_string_array(&(p
->filters
), "application/vnd.cups-postscript 0 -");
4452 * If we have an interface script, add a filter entry for it...
4455 char interface
[1024]; /* Interface script */
4458 snprintf(interface
, sizeof(interface
), "%s/interfaces/%s", ServerRoot
,
4460 if (!access(interface
, X_OK
))
4463 * Yes, we have a System V style interface script; use it!
4466 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4467 "printer-make-and-model", NULL
,
4468 "Local System V Printer");
4470 snprintf(interface
, sizeof(interface
), "*/* 0 %s/interfaces/%s",
4471 ServerRoot
, p
->name
);
4472 add_string_array(&(p
->filters
), interface
);
4474 else if (!strncmp(p
->device_uri
, "ipp://", 6) &&
4475 (strstr(p
->device_uri
, "/printers/") != NULL
||
4476 strstr(p
->device_uri
, "/classes/") != NULL
||
4477 (strstr(p
->device_uri
, "._ipp.") != NULL
&&
4478 !strcmp(p
->device_uri
+ strlen(p
->device_uri
) - 5,
4482 * Tell the client this is really a hard-wired remote printer.
4485 p
->type
|= CUPS_PRINTER_REMOTE
;
4488 * Point the printer-uri-supported attribute to the
4492 ippAddString(p
->attrs
, IPP_TAG_PRINTER
, IPP_TAG_URI
,
4493 "printer-uri-supported", NULL
, p
->device_uri
);
4496 * Then set the make-and-model accordingly...
4499 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4500 "printer-make-and-model", NULL
, "Remote Printer");
4503 * Print all files directly...
4512 * Otherwise we have neither - treat this as a "dumb" printer
4513 * with no PPD file...
4516 ippAddString(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_TEXT
,
4517 "printer-make-and-model", NULL
, "Local Raw Printer");
4523 ippAddIntegers(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
4524 "finishings-supported", num_finishings
, finishings
);
4525 ippAddInteger(p
->ppd_attrs
, IPP_TAG_PRINTER
, IPP_TAG_ENUM
,
4526 "finishings-default", IPP_FINISHINGS_NONE
);
4528 if (ppd
&& (cache
= cupsFileOpen(cache_name
, "w")) != NULL
)
4531 * Save cached PPD attributes to disk...
4534 cupsdLogMessage(CUPSD_LOG_DEBUG
, "load_ppd: Saving %s...", cache_name
);
4536 p
->ppd_attrs
->state
= IPP_IDLE
;
4538 if (ippWriteIO(cache
, (ipp_iocb_t
)cupsFileWrite
, 1, NULL
,
4539 p
->ppd_attrs
) != IPP_DATA
)
4541 cupsdLogMessage(CUPSD_LOG_ERROR
,
4542 "Unable to save PPD cache file \"%s\" - %s", cache_name
,
4547 cupsFileClose(cache
);
4549 else if (cache_info
.st_mtime
)
4552 * Remove cache file...
4562 * 'write_irix_config()' - Update the config files used by the IRIX
4567 write_irix_config(cupsd_printer_t
*p
) /* I - Printer to update */
4569 char filename
[1024]; /* Interface script filename */
4570 cups_file_t
*fp
; /* Interface script file */
4571 ipp_attribute_t
*attr
; /* Attribute data */
4575 * Add dummy interface and GUI scripts to fool SGI's "challenged" printing
4576 * tools. First the interface script that tells the tools what kind of
4577 * printer we have...
4580 snprintf(filename
, sizeof(filename
), "/var/spool/lp/interface/%s", p
->name
);
4582 if (p
->type
& CUPS_PRINTER_CLASS
)
4584 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4586 cupsFilePuts(fp
, "#!/bin/sh\n");
4588 if ((attr
= ippFindAttribute(p
->attrs
, "printer-make-and-model",
4589 IPP_TAG_TEXT
)) != NULL
)
4590 cupsFilePrintf(fp
, "NAME=\"%s\"\n", attr
->values
[0].string
.text
);
4591 else if (p
->type
& CUPS_PRINTER_CLASS
)
4592 cupsFilePuts(fp
, "NAME=\"Printer Class\"\n");
4594 cupsFilePuts(fp
, "NAME=\"Remote Destination\"\n");
4596 if (p
->type
& CUPS_PRINTER_COLOR
)
4597 cupsFilePuts(fp
, "TYPE=ColorPostScript\n");
4599 cupsFilePuts(fp
, "TYPE=MonoPostScript\n");
4601 cupsFilePrintf(fp
, "HOSTNAME=%s\n", ServerName
);
4602 cupsFilePrintf(fp
, "HOSTPRINTER=%s\n", p
->name
);
4606 chmod(filename
, 0755);
4607 chown(filename
, User
, Group
);
4611 * Then the member file that tells which device file the queue is connected
4612 * to... Networked printers use "/dev/null" in this file, so that's what
4613 * we use (the actual device URI can confuse some apps...)
4616 snprintf(filename
, sizeof(filename
), "/var/spool/lp/member/%s", p
->name
);
4618 if (p
->type
& CUPS_PRINTER_CLASS
)
4620 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4622 cupsFilePuts(fp
, "/dev/null\n");
4626 chmod(filename
, 0644);
4627 chown(filename
, User
, Group
);
4631 * The gui_interface file is a script or program that launches a GUI
4632 * option panel for the printer, using options specified on the
4633 * command-line in the third argument. The option panel must send
4634 * any printing options to stdout on a single line when the user
4635 * accepts them, or nothing if the user cancels the dialog.
4637 * The default options panel program is /usr/bin/glpoptions, from
4638 * the ESP Print Pro software. You can select another using the
4639 * PrintcapGUI option.
4642 snprintf(filename
, sizeof(filename
), "/var/spool/lp/gui_interface/ELF/%s.gui", p
->name
);
4644 if (p
->type
& CUPS_PRINTER_CLASS
)
4646 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4648 cupsFilePuts(fp
, "#!/bin/sh\n");
4649 cupsFilePrintf(fp
, "%s -d %s -o \"$3\"\n", PrintcapGUI
, p
->name
);
4653 chmod(filename
, 0755);
4654 chown(filename
, User
, Group
);
4658 * The POD config file is needed by the printstatus command to show
4659 * the printer location and device.
4662 snprintf(filename
, sizeof(filename
), "/var/spool/lp/pod/%s.config", p
->name
);
4664 if (p
->type
& CUPS_PRINTER_CLASS
)
4666 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4668 cupsFilePrintf(fp
, "Printer Class | %s\n",
4669 (p
->type
& CUPS_PRINTER_COLOR
) ? "ColorPostScript" : "MonoPostScript");
4670 cupsFilePrintf(fp
, "Printer Model | %s\n", p
->make_model
? p
->make_model
: "");
4671 cupsFilePrintf(fp
, "Location Code | %s\n", p
->location
? p
->location
: "");
4672 cupsFilePrintf(fp
, "Physical Location | %s\n", p
->info
? p
->info
: "");
4673 cupsFilePrintf(fp
, "Port Path | %s\n", p
->device_uri
);
4674 cupsFilePrintf(fp
, "Config Path | /var/spool/lp/pod/%s.config\n", p
->name
);
4675 cupsFilePrintf(fp
, "Active Status Path | /var/spool/lp/pod/%s.status\n", p
->name
);
4676 cupsFilePuts(fp
, "Status Update Wait | 10 seconds\n");
4680 chmod(filename
, 0664);
4681 chown(filename
, User
, Group
);
4687 * 'write_irix_state()' - Update the status files used by IRIX printing
4692 write_irix_state(cupsd_printer_t
*p
) /* I - Printer to update */
4694 char filename
[1024]; /* Interface script filename */
4695 cups_file_t
*fp
; /* Interface script file */
4696 int tag
; /* Status tag value */
4702 * The POD status file is needed for the printstatus window to
4703 * provide the current status of the printer.
4706 snprintf(filename
, sizeof(filename
), "/var/spool/lp/pod/%s.status", p
->name
);
4708 if (p
->type
& CUPS_PRINTER_CLASS
)
4710 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4712 cupsFilePrintf(fp
, "Operational Status | %s\n",
4713 (p
->state
== IPP_PRINTER_IDLE
) ? "Idle" :
4714 (p
->state
== IPP_PRINTER_PROCESSING
) ? "Busy" :
4716 cupsFilePrintf(fp
, "Information | 01 00 00 | %s\n", CUPS_SVERSION
);
4717 cupsFilePrintf(fp
, "Information | 02 00 00 | Device URI: %s\n",
4719 cupsFilePrintf(fp
, "Information | 03 00 00 | %s jobs\n",
4720 p
->accepting
? "Accepting" : "Not accepting");
4721 cupsFilePrintf(fp
, "Information | 04 00 00 | %s\n", p
->state_message
);
4725 chmod(filename
, 0664);
4726 chown(filename
, User
, Group
);
4730 * The activeicons file is needed to provide desktop icons for printers:
4732 * [ quoted from /usr/lib/print/tagit ]
4734 * --- Type of printer tags (base values)
4736 * Dumb=66048 # 0x10200
4737 * DumbColor=66080 # 0x10220
4738 * Raster=66112 # 0x10240
4739 * ColorRaster=66144 # 0x10260
4740 * Plotter=66176 # 0x10280
4741 * PostScript=66208 # 0x102A0
4742 * ColorPostScript=66240 # 0x102C0
4743 * MonoPostScript=66272 # 0x102E0
4745 * --- Printer state modifiers for local printers
4750 * Unknown=3 # 0x3 (Faulted due to unknown reason)
4752 * --- Printer state modifiers for network printers
4756 * NetFaulted=10 # 0xA
4757 * NetUnknown=11 # 0xB (Faulted due to unknown reason)
4760 snprintf(filename
, sizeof(filename
), "/var/spool/lp/activeicons/%s", p
->name
);
4762 if (p
->type
& CUPS_PRINTER_CLASS
)
4764 else if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4766 if (p
->type
& CUPS_PRINTER_COLOR
)
4771 if (p
->type
& CUPS_PRINTER_REMOTE
)
4774 if (p
->state
== IPP_PRINTER_PROCESSING
)
4777 else if (p
->state
== IPP_PRINTER_STOPPED
)
4780 cupsFilePuts(fp
, "#!/bin/sh\n");
4781 cupsFilePrintf(fp
, "#Tag %d\n", tag
);
4785 chmod(filename
, 0755);
4786 chown(filename
, User
, Group
);
4791 * The default file is needed by the printers window to show
4792 * the default printer.
4795 snprintf(filename
, sizeof(filename
), "/var/spool/lp/default");
4797 if (DefaultPrinter
!= NULL
)
4799 if ((fp
= cupsFileOpen(filename
, "w")) != NULL
)
4801 cupsFilePrintf(fp
, "%s\n", DefaultPrinter
->name
);
4805 chmod(filename
, 0644);
4806 chown(filename
, User
, Group
);
4816 * 'write_xml_string()' - Write a string with XML escaping.
4820 write_xml_string(cups_file_t
*fp
, /* I - File to write to */
4821 const char *s
) /* I - String to write */
4823 const char *start
; /* Start of current sequence */
4829 for (start
= s
; *s
; s
++)
4834 cupsFileWrite(fp
, start
, s
- start
);
4836 cupsFilePuts(fp
, "&");
4842 cupsFileWrite(fp
, start
, s
- start
);
4844 cupsFilePuts(fp
, "<");
4850 cupsFilePuts(fp
, start
);
4855 * End of "$Id: printers.c 7968 2008-09-19 23:03:01Z mike $".