]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/printers.c
Merge changes from CUPS 1.5svn-r9491.
[thirdparty/cups.git] / scheduler / printers.c
1 /*
2 * "$Id: printers.c 7968 2008-09-19 23:03:01Z mike $"
3 *
4 * Printer routines for the CUPS scheduler.
5 *
6 * Copyright 2007-2011 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
8 *
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/".
14 *
15 * Contents:
16 *
17 * cupsdAddPrinter() - Add a printer to the system.
18 * cupsdAddPrinterHistory() - Add the current printer state to the history.
19 * cupsdCreateCommonData() - Create the common printer data.
20 * cupsdDeleteAllPrinters() - Delete all printers from the system.
21 * cupsdDeletePrinter() - Delete a printer from the system.
22 * cupsdFindDest() - Find a destination in the list.
23 * cupsdFindPrinter() - Find a printer in the list.
24 * cupsdLoadAllPrinters() - Load printers from the printers.conf file.
25 * cupsdRenamePrinter() - Rename a printer.
26 * cupsdSaveAllPrinters() - Save all printer definitions to the
27 * printers.conf file.
28 * cupsdSetAuthInfoRequired() - Set the required authentication info.
29 * cupsdSetDeviceURI() - Set the device URI for a printer.
30 * cupsdSetPrinterAttr() - Set a printer attribute.
31 * cupsdSetPrinterAttrs() - Set printer attributes based upon the PPD
32 * file.
33 * cupsdSetPrinterReasons() - Set/update the reasons strings.
34 * cupsdSetPrinterState() - Update the current state of a printer.
35 * cupsdStopPrinter() - Stop a printer from printing any jobs...
36 * cupsdUpdatePrinterPPD() - Update keywords in a printer's PPD file.
37 * cupsdUpdatePrinters() - Update printers after a partial reload.
38 * cupsdValidateDest() - Validate a printer/class destination.
39 * cupsdWritePrintcap() - Write a pseudo-printcap file for older
40 * applications that need it...
41 * add_printer_defaults() - Add name-default attributes to the printer
42 * attributes.
43 * add_printer_filter() - Add a MIME filter for a printer.
44 * add_printer_formats() - Add document-format-supported values for a
45 * printer.
46 * add_string_array() - Add a string to an array of CUPS strings.
47 * compare_printers() - Compare two printers.
48 * delete_printer_filters() - Delete all MIME filters for a printer.
49 * delete_string_array() - Delete an array of CUPS strings.
50 * dirty_printer() - Mark config and state files dirty for the
51 * specified printer.
52 * load_ppd() - Load a cached PPD file, updating the cache as
53 * needed.
54 * new_media_col() - Create a media-col collection value.
55 * write_irix_config() - Update the config files used by the IRIX
56 * desktop tools.
57 * write_irix_state() - Update the status files used by IRIX printing
58 * desktop tools.
59 * write_xml_string() - Write a string with XML escaping.
60 */
61
62 /*
63 * Include necessary headers...
64 */
65
66 #include "cupsd.h"
67 #include <cups/dir.h>
68 #ifdef HAVE_APPLICATIONSERVICES_H
69 # include <ApplicationServices/ApplicationServices.h>
70 #endif /* HAVE_APPLICATIONSERVICES_H */
71 #ifdef HAVE_SYS_MOUNT_H
72 # include <sys/mount.h>
73 #endif /* HAVE_SYS_MOUNT_H */
74 #ifdef HAVE_SYS_STATVFS_H
75 # include <sys/statvfs.h>
76 #elif defined(HAVE_SYS_STATFS_H)
77 # include <sys/statfs.h>
78 #endif /* HAVE_SYS_STATVFS_H */
79 #ifdef HAVE_SYS_VFS_H
80 # include <sys/vfs.h>
81 #endif /* HAVE_SYS_VFS_H */
82
83
84 /*
85 * Local functions...
86 */
87
88 static void add_printer_defaults(cupsd_printer_t *p);
89 static void add_printer_filter(cupsd_printer_t *p, mime_type_t *type,
90 const char *filter);
91 static void add_printer_formats(cupsd_printer_t *p);
92 static void add_string_array(cups_array_t **a, const char *s);
93 static int compare_printers(void *first, void *second, void *data);
94 static void delete_printer_filters(cupsd_printer_t *p);
95 static void delete_string_array(cups_array_t **a);
96 static void dirty_printer(cupsd_printer_t *p);
97 static void load_ppd(cupsd_printer_t *p);
98 static ipp_t *new_media_col(_pwg_size_t *size, const char *source,
99 const char *type);
100 #ifdef __sgi
101 static void write_irix_config(cupsd_printer_t *p);
102 static void write_irix_state(cupsd_printer_t *p);
103 #endif /* __sgi */
104 static void write_xml_string(cups_file_t *fp, const char *s);
105
106
107 /*
108 * 'cupsdAddPrinter()' - Add a printer to the system.
109 */
110
111 cupsd_printer_t * /* O - New printer */
112 cupsdAddPrinter(const char *name) /* I - Name of printer */
113 {
114 cupsd_printer_t *p; /* New printer */
115 char uri[1024]; /* Printer URI */
116
117
118 /*
119 * Range check input...
120 */
121
122 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdAddPrinter(\"%s\")", name);
123
124 /*
125 * Create a new printer entity...
126 */
127
128 if ((p = calloc(1, sizeof(cupsd_printer_t))) == NULL)
129 {
130 cupsdLogMessage(CUPSD_LOG_CRIT, "Unable to allocate memory for printer - %s",
131 strerror(errno));
132 return (NULL);
133 }
134
135 cupsdSetString(&p->name, name);
136 cupsdSetString(&p->info, name);
137 cupsdSetString(&p->hostname, ServerName);
138
139 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
140 ServerName, RemotePort, "/printers/%s", name);
141 cupsdSetString(&p->uri, uri);
142 cupsdSetDeviceURI(p, "file:///dev/null");
143
144 p->state = IPP_PRINTER_STOPPED;
145 p->state_time = time(NULL);
146 p->accepting = 0;
147 p->shared = DefaultShared;
148 p->filetype = mimeAddType(MimeDatabase, "printer", name);
149
150 cupsdSetString(&p->job_sheets[0], "none");
151 cupsdSetString(&p->job_sheets[1], "none");
152
153 cupsdSetString(&p->error_policy, ErrorPolicy);
154 cupsdSetString(&p->op_policy, DefaultPolicy);
155
156 p->op_policy_ptr = DefaultPolicyPtr;
157
158 if (MaxPrinterHistory)
159 p->history = calloc(MaxPrinterHistory, sizeof(ipp_t *));
160
161 /*
162 * Insert the printer in the printer list alphabetically...
163 */
164
165 if (!Printers)
166 Printers = cupsArrayNew(compare_printers, NULL);
167
168 cupsdLogMessage(CUPSD_LOG_DEBUG2,
169 "cupsdAddPrinter: Adding %s to Printers", p->name);
170 cupsArrayAdd(Printers, p);
171
172 if (!ImplicitPrinters)
173 ImplicitPrinters = cupsArrayNew(compare_printers, NULL);
174
175 /*
176 * Return the new printer...
177 */
178
179 return (p);
180 }
181
182
183 /*
184 * 'cupsdAddPrinterHistory()' - Add the current printer state to the history.
185 */
186
187 void
188 cupsdAddPrinterHistory(
189 cupsd_printer_t *p) /* I - Printer */
190 {
191 ipp_t *history; /* History collection */
192
193
194 /*
195 * Stop early if we aren't keeping history data...
196 */
197
198 if (MaxPrinterHistory <= 0)
199 return;
200
201 /*
202 * Retire old history data as needed...
203 */
204
205 p->sequence_number ++;
206
207 if (p->num_history >= MaxPrinterHistory)
208 {
209 p->num_history --;
210 ippDelete(p->history[0]);
211 memmove(p->history, p->history + 1, p->num_history * sizeof(ipp_t *));
212 }
213
214 /*
215 * Create a collection containing the current printer-state, printer-up-time,
216 * printer-state-message, and printer-state-reasons attributes.
217 */
218
219 history = ippNew();
220 ippAddInteger(history, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-state",
221 p->state);
222 ippAddBoolean(history, IPP_TAG_PRINTER, "printer-is-accepting-jobs",
223 p->accepting);
224 ippAddBoolean(history, IPP_TAG_PRINTER, "printer-is-shared", p->shared);
225 ippAddString(history, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-state-message",
226 NULL, p->state_message);
227 if (p->num_reasons == 0)
228 ippAddString(history, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
229 "printer-state-reasons", NULL, "none");
230 else
231 ippAddStrings(history, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
232 "printer-state-reasons", p->num_reasons, NULL,
233 (const char * const *)p->reasons);
234 ippAddInteger(history, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
235 "printer-state-change-time", p->state_time);
236 ippAddInteger(history, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
237 "printer-state-sequence-number", p->sequence_number);
238
239 p->history[p->num_history] = history;
240 p->num_history ++;
241 }
242
243
244 /*
245 * 'cupsdCreateCommonData()' - Create the common printer data.
246 */
247
248 void
249 cupsdCreateCommonData(void)
250 {
251 int i; /* Looping var */
252 ipp_attribute_t *attr; /* Attribute data */
253 cups_dir_t *dir; /* Notifier directory */
254 cups_dentry_t *dent; /* Notifier directory entry */
255 cups_array_t *notifiers; /* Notifier array */
256 char filename[1024], /* Filename */
257 *notifier; /* Current notifier */
258 cupsd_policy_t *p; /* Current policy */
259 int k_supported; /* Maximum file size supported */
260 #ifdef HAVE_STATVFS
261 struct statvfs spoolinfo; /* FS info for spool directory */
262 double spoolsize; /* FS size */
263 #elif defined(HAVE_STATFS)
264 struct statfs spoolinfo; /* FS info for spool directory */
265 double spoolsize; /* FS size */
266 #endif /* HAVE_STATVFS */
267 static const int nups[] = /* number-up-supported values */
268 { 1, 2, 4, 6, 9, 16 };
269 static const int orients[4] =/* orientation-requested-supported values */
270 {
271 IPP_PORTRAIT,
272 IPP_LANDSCAPE,
273 IPP_REVERSE_LANDSCAPE,
274 IPP_REVERSE_PORTRAIT
275 };
276 static const char * const holds[] = /* job-hold-until-supported values */
277 {
278 "no-hold",
279 "indefinite",
280 "day-time",
281 "evening",
282 "night",
283 "second-shift",
284 "third-shift",
285 "weekend"
286 };
287 static const char * const versions[] =/* ipp-versions-supported values */
288 {
289 "1.0",
290 "1.1",
291 "2.0",
292 "2.1"
293 };
294 static const int ops[] = /* operations-supported values */
295 {
296 IPP_PRINT_JOB,
297 IPP_VALIDATE_JOB,
298 IPP_CREATE_JOB,
299 IPP_SEND_DOCUMENT,
300 IPP_CANCEL_JOB,
301 IPP_GET_JOB_ATTRIBUTES,
302 IPP_GET_JOBS,
303 IPP_GET_PRINTER_ATTRIBUTES,
304 IPP_HOLD_JOB,
305 IPP_RELEASE_JOB,
306 IPP_RESTART_JOB,
307 IPP_PAUSE_PRINTER,
308 IPP_RESUME_PRINTER,
309 IPP_PURGE_JOBS,
310 IPP_SET_PRINTER_ATTRIBUTES,
311 IPP_SET_JOB_ATTRIBUTES,
312 IPP_GET_PRINTER_SUPPORTED_VALUES,
313 IPP_CREATE_PRINTER_SUBSCRIPTION,
314 IPP_CREATE_JOB_SUBSCRIPTION,
315 IPP_GET_SUBSCRIPTION_ATTRIBUTES,
316 IPP_GET_SUBSCRIPTIONS,
317 IPP_RENEW_SUBSCRIPTION,
318 IPP_CANCEL_SUBSCRIPTION,
319 IPP_GET_NOTIFICATIONS,
320 IPP_ENABLE_PRINTER,
321 IPP_DISABLE_PRINTER,
322 IPP_HOLD_NEW_JOBS,
323 IPP_RELEASE_HELD_NEW_JOBS,
324 IPP_CANCEL_JOBS,
325 IPP_CANCEL_MY_JOBS,
326 IPP_CLOSE_JOB,
327 CUPS_GET_DEFAULT,
328 CUPS_GET_PRINTERS,
329 CUPS_ADD_PRINTER,
330 CUPS_DELETE_PRINTER,
331 CUPS_GET_CLASSES,
332 CUPS_ADD_CLASS,
333 CUPS_DELETE_CLASS,
334 CUPS_ACCEPT_JOBS,
335 CUPS_REJECT_JOBS,
336 CUPS_SET_DEFAULT,
337 CUPS_GET_DEVICES,
338 CUPS_GET_PPDS,
339 CUPS_MOVE_JOB,
340 CUPS_AUTHENTICATE_JOB,
341 CUPS_GET_PPD,
342 CUPS_GET_DOCUMENT,
343 IPP_RESTART_JOB
344 };
345 static const char * const charsets[] =/* charset-supported values */
346 {
347 "us-ascii",
348 "utf-8"
349 };
350 static const char * const compressions[] =
351 { /* document-compression-supported values */
352 "none"
353 #ifdef HAVE_LIBZ
354 ,"gzip"
355 #endif /* HAVE_LIBZ */
356 };
357 static const char * const media_col_supported[] =
358 { /* media-col-supported values */
359 "media-bottom-margin",
360 "media-left-margin",
361 "media-right-margin",
362 "media-size",
363 "media-source",
364 "media-top-margin",
365 "media-type"
366 };
367 static const char * const multiple_document_handling[] =
368 { /* multiple-document-handling-supported values */
369 "separate-documents-uncollated-copies",
370 "separate-documents-collated-copies"
371 };
372 static const char * const notify_attrs[] =
373 { /* notify-attributes-supported values */
374 "printer-state-change-time",
375 "notify-lease-expiration-time",
376 "notify-subscriber-user-name"
377 };
378 static const char * const notify_events[] =
379 { /* notify-events-supported values */
380 "job-completed",
381 "job-config-changed",
382 "job-created",
383 "job-progress",
384 "job-state-changed",
385 "job-stopped",
386 "printer-added",
387 "printer-changed",
388 "printer-config-changed",
389 "printer-deleted",
390 "printer-finishings-changed",
391 "printer-media-changed",
392 "printer-modified",
393 "printer-restarted",
394 "printer-shutdown",
395 "printer-state-changed",
396 "printer-stopped",
397 "server-audit",
398 "server-restarted",
399 "server-started",
400 "server-stopped"
401 };
402 static const char * const job_creation[] =
403 { /* job-creation-attributes-supported */
404 "copies",
405 "finishings",
406 "ipp-attribute-fidelity",
407 "job-hold-until",
408 "job-name",
409 "job-priority",
410 "job-sheets",
411 "media",
412 "media-col",
413 "multiple-document-handling",
414 "number-up",
415 "output-bin",
416 "output-mode",
417 "orientation-requested",
418 "page-ranges",
419 "print-quality",
420 "printer-resolution",
421 "sides"
422 };
423 static const char * const job_settable[] =
424 { /* job-settable-attributes-supported */
425 "copies",
426 "finishings",
427 "job-hold-until",
428 "job-name",
429 "job-priority",
430 "media",
431 "media-col",
432 "multiple-document-handling",
433 "number-up",
434 "output-bin",
435 "output-mode",
436 "orientation-requested",
437 "page-ranges",
438 "print-quality",
439 "printer-resolution",
440 "sides"
441 };
442 static const char * const printer_settable[] =
443 { /* printer-settable-attributes-supported */
444 "printer-info",
445 "printer-location"
446 };
447 static const char * const which_jobs[] =
448 { /* which-jobs-supported values */
449 "completed",
450 "not-completed",
451 "aborted",
452 "all",
453 "canceled",
454 "pending",
455 "pending-held",
456 "processing",
457 "processing-stopped"
458 };
459
460
461 if (CommonData)
462 ippDelete(CommonData);
463
464 CommonData = ippNew();
465
466 /*
467 * Get the maximum spool size based on the size of the filesystem used for
468 * the RequestRoot directory. If the host OS doesn't support the statfs call
469 * or the filesystem is larger than 2TiB, always report INT_MAX.
470 */
471
472 #ifdef HAVE_STATVFS
473 if (statvfs(RequestRoot, &spoolinfo))
474 k_supported = INT_MAX;
475 else if ((spoolsize = (double)spoolinfo.f_frsize * spoolinfo.f_blocks / 1024) >
476 INT_MAX)
477 k_supported = INT_MAX;
478 else
479 k_supported = (int)spoolsize;
480
481 #elif defined(HAVE_STATFS)
482 if (statfs(RequestRoot, &spoolinfo))
483 k_supported = INT_MAX;
484 else if ((spoolsize = (double)spoolinfo.f_bsize * spoolinfo.f_blocks / 1024) >
485 INT_MAX)
486 k_supported = INT_MAX;
487 else
488 k_supported = (int)spoolsize;
489
490 #else
491 k_supported = INT_MAX;
492 #endif /* HAVE_STATVFS */
493
494 /*
495 * This list of attributes is sorted to improve performance when the
496 * client provides a requested-attributes attribute...
497 */
498
499 /* charset-configured */
500 ippAddString(CommonData, IPP_TAG_PRINTER, IPP_TAG_CHARSET | IPP_TAG_COPY,
501 "charset-configured", NULL, "utf-8");
502
503 /* charset-supported */
504 ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_CHARSET | IPP_TAG_COPY,
505 "charset-supported", sizeof(charsets) / sizeof(charsets[0]),
506 NULL, charsets);
507
508 /* compression-supported */
509 ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD | IPP_TAG_COPY,
510 "compression-supported",
511 sizeof(compressions) / sizeof(compressions[0]),
512 NULL, compressions);
513
514 /* copies-supported */
515 ippAddRange(CommonData, IPP_TAG_PRINTER, "copies-supported", 1, MaxCopies);
516
517 /* cups-version */
518 ippAddString(CommonData, IPP_TAG_PRINTER, IPP_TAG_TEXT | IPP_TAG_COPY,
519 "cups-version", NULL, CUPS_SVERSION + 6);
520
521 /* generated-natural-language-supported (no IPP_TAG_COPY) */
522 ippAddString(CommonData, IPP_TAG_PRINTER, IPP_TAG_LANGUAGE,
523 "generated-natural-language-supported", NULL, DefaultLanguage);
524
525 /* ipp-versions-supported */
526 ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD | IPP_TAG_COPY,
527 "ipp-versions-supported", sizeof(versions) / sizeof(versions[0]),
528 NULL, versions);
529
530 /* ippget-event-life */
531 ippAddInteger(CommonData, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
532 "ippget-event-life", 15);
533
534 /* job-creation-attributes-supported */
535 ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD | IPP_TAG_COPY,
536 "job-creation-attributes-supported",
537 sizeof(job_creation) / sizeof(job_creation[0]),
538 NULL, job_creation);
539
540 /* job-hold-until-supported */
541 ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD | IPP_TAG_COPY,
542 "job-hold-until-supported", sizeof(holds) / sizeof(holds[0]),
543 NULL, holds);
544
545 /* job-ids-supported */
546 ippAddBoolean(CommonData, IPP_TAG_PRINTER, "job-ids-supported", 1);
547
548 /* job-k-octets-supported */
549 ippAddInteger(CommonData, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
550 "job-k-octets-supported", k_supported);
551
552 /* job-priority-supported */
553 ippAddInteger(CommonData, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
554 "job-priority-supported", 100);
555
556 /* job-settable-attributes-supported */
557 ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD | IPP_TAG_COPY,
558 "job-settable-attributes-supported",
559 sizeof(job_settable) / sizeof(job_settable[0]),
560 NULL, job_settable);
561
562 /* job-sheets-supported */
563 if (cupsArrayCount(Banners) > 0)
564 {
565 /*
566 * Setup the job-sheets-supported attribute...
567 */
568
569 if (Classification && !ClassifyOverride)
570 attr = ippAddString(CommonData, IPP_TAG_PRINTER,
571 IPP_TAG_NAME | IPP_TAG_COPY,
572 "job-sheets-supported", NULL, Classification);
573 else
574 attr = ippAddStrings(CommonData, IPP_TAG_PRINTER,
575 IPP_TAG_NAME | IPP_TAG_COPY,
576 "job-sheets-supported", cupsArrayCount(Banners) + 1,
577 NULL, NULL);
578
579 if (attr == NULL)
580 cupsdLogMessage(CUPSD_LOG_EMERG,
581 "Unable to allocate memory for "
582 "job-sheets-supported attribute: %s!", strerror(errno));
583 else if (!Classification || ClassifyOverride)
584 {
585 cupsd_banner_t *banner; /* Current banner */
586
587
588 attr->values[0].string.text = _cupsStrAlloc("none");
589
590 for (i = 1, banner = (cupsd_banner_t *)cupsArrayFirst(Banners);
591 banner;
592 i ++, banner = (cupsd_banner_t *)cupsArrayNext(Banners))
593 attr->values[i].string.text = banner->name;
594 }
595 }
596 else
597 ippAddString(CommonData, IPP_TAG_PRINTER, IPP_TAG_NAME | IPP_TAG_COPY,
598 "job-sheets-supported", NULL, "none");
599
600 /* media-col-supported */
601 ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD | IPP_TAG_COPY,
602 "media-col-supported",
603 sizeof(media_col_supported) /
604 sizeof(media_col_supported[0]), NULL,
605 media_col_supported);
606
607 /* multiple-document-handling-supported */
608 ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD | IPP_TAG_COPY,
609 "multiple-document-handling-supported",
610 sizeof(multiple_document_handling) /
611 sizeof(multiple_document_handling[0]), NULL,
612 multiple_document_handling);
613
614 /* multiple-document-jobs-supported */
615 ippAddBoolean(CommonData, IPP_TAG_PRINTER,
616 "multiple-document-jobs-supported", 1);
617
618 /* multiple-operation-time-out */
619 ippAddInteger(CommonData, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
620 "multiple-operation-time-out", MultipleOperationTimeout);
621
622 /* natural-language-configured (no IPP_TAG_COPY) */
623 ippAddString(CommonData, IPP_TAG_PRINTER, IPP_TAG_LANGUAGE,
624 "natural-language-configured", NULL, DefaultLanguage);
625
626 /* notify-attributes-supported */
627 ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD | IPP_TAG_COPY,
628 "notify-attributes-supported",
629 (int)(sizeof(notify_attrs) / sizeof(notify_attrs[0])),
630 NULL, notify_attrs);
631
632 /* notify-lease-duration-supported */
633 ippAddRange(CommonData, IPP_TAG_PRINTER,
634 "notify-lease-duration-supported", 0,
635 MaxLeaseDuration ? MaxLeaseDuration : 2147483647);
636
637 /* notify-max-events-supported */
638 ippAddInteger(CommonData, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
639 "notify-max-events-supported", MaxEvents);
640
641 /* notify-events-supported */
642 ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD | IPP_TAG_COPY,
643 "notify-events-supported",
644 (int)(sizeof(notify_events) / sizeof(notify_events[0])),
645 NULL, notify_events);
646
647 /* notify-pull-method-supported */
648 ippAddString(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD | IPP_TAG_COPY,
649 "notify-pull-method-supported", NULL, "ippget");
650
651 /* notify-schemes-supported */
652 snprintf(filename, sizeof(filename), "%s/notifier", ServerBin);
653 if ((dir = cupsDirOpen(filename)) != NULL)
654 {
655 notifiers = cupsArrayNew((cups_array_func_t)strcmp, NULL);
656
657 while ((dent = cupsDirRead(dir)) != NULL)
658 if (S_ISREG(dent->fileinfo.st_mode) &&
659 (dent->fileinfo.st_mode & S_IXOTH) != 0)
660 cupsArrayAdd(notifiers, _cupsStrAlloc(dent->filename));
661
662 if (cupsArrayCount(notifiers) > 0)
663 {
664 attr = ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
665 "notify-schemes-supported",
666 cupsArrayCount(notifiers), NULL, NULL);
667
668 for (i = 0, notifier = (char *)cupsArrayFirst(notifiers);
669 notifier;
670 i ++, notifier = (char *)cupsArrayNext(notifiers))
671 attr->values[i].string.text = notifier;
672 }
673
674 cupsArrayDelete(notifiers);
675 cupsDirClose(dir);
676 }
677
678 /* number-up-supported */
679 ippAddIntegers(CommonData, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
680 "number-up-supported", sizeof(nups) / sizeof(nups[0]), nups);
681
682 /* operations-supported */
683 ippAddIntegers(CommonData, IPP_TAG_PRINTER, IPP_TAG_ENUM,
684 "operations-supported",
685 sizeof(ops) / sizeof(ops[0]) + JobFiles - 1, ops);
686
687 /* orientation-requested-supported */
688 ippAddIntegers(CommonData, IPP_TAG_PRINTER, IPP_TAG_ENUM,
689 "orientation-requested-supported", 4, orients);
690
691 /* page-ranges-supported */
692 ippAddBoolean(CommonData, IPP_TAG_PRINTER, "page-ranges-supported", 1);
693
694 /* pdl-override-supported */
695 ippAddString(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD | IPP_TAG_COPY,
696 "pdl-override-supported", NULL, "attempted");
697
698 /* printer-op-policy-supported */
699 attr = ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_NAME | IPP_TAG_COPY,
700 "printer-op-policy-supported", cupsArrayCount(Policies),
701 NULL, NULL);
702 for (i = 0, p = (cupsd_policy_t *)cupsArrayFirst(Policies);
703 p;
704 i ++, p = (cupsd_policy_t *)cupsArrayNext(Policies))
705 attr->values[i].string.text = p->name;
706
707 /* printer-settable-attributes-supported */
708 ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD | IPP_TAG_COPY,
709 "printer-settable-attributes-supported",
710 sizeof(printer_settable) / sizeof(printer_settable[0]),
711 NULL, printer_settable);
712
713 /* server-is-sharing-printers */
714 ippAddBoolean(CommonData, IPP_TAG_PRINTER, "server-is-sharing-printers",
715 BrowseLocalProtocols != 0 && Browsing);
716
717 /* which-jobs-supported */
718 ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD | IPP_TAG_COPY,
719 "which-jobs-supported",
720 sizeof(which_jobs) / sizeof(which_jobs[0]), NULL, which_jobs);
721 }
722
723
724 /*
725 * 'cupsdDeleteAllPrinters()' - Delete all printers from the system.
726 */
727
728 void
729 cupsdDeleteAllPrinters(void)
730 {
731 cupsd_printer_t *p; /* Pointer to current printer/class */
732
733
734 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
735 p;
736 p = (cupsd_printer_t *)cupsArrayNext(Printers))
737 {
738 p->op_policy_ptr = DefaultPolicyPtr;
739 cupsdDeletePrinter(p, 0);
740 }
741 }
742
743
744 /*
745 * 'cupsdDeletePrinter()' - Delete a printer from the system.
746 */
747
748 int /* O - 1 if classes affected, 0 otherwise */
749 cupsdDeletePrinter(
750 cupsd_printer_t *p, /* I - Printer to delete */
751 int update) /* I - Update printers.conf? */
752 {
753 int i, /* Looping var */
754 changed = 0; /* Class changed? */
755 #ifdef __sgi
756 char filename[1024]; /* Interface script filename */
757 #endif /* __sgi */
758
759
760 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdDeletePrinter(p=%p(%s), update=%d)",
761 p, p->name, update);
762
763 /*
764 * Save the current position in the Printers array...
765 */
766
767 cupsArraySave(Printers);
768
769 /*
770 * Stop printing on this printer...
771 */
772
773 cupsdSetPrinterState(p, IPP_PRINTER_STOPPED, update);
774
775 p->state = IPP_PRINTER_STOPPED; /* Force for browsed printers */
776
777 if (p->job)
778 cupsdSetJobState(p->job, IPP_JOB_PENDING, CUPSD_JOB_FORCE,
779 update ? "Job stopped due to printer being deleted." :
780 "Job stopped.");
781
782 /*
783 * If this printer is the next for browsing, point to the next one...
784 */
785
786 if (p == BrowseNext)
787 {
788 cupsArrayFind(Printers, p);
789 BrowseNext = (cupsd_printer_t *)cupsArrayNext(Printers);
790 }
791
792 /*
793 * Remove the printer from the list...
794 */
795
796 cupsdLogMessage(CUPSD_LOG_DEBUG2,
797 "cupsdDeletePrinter: Removing %s from Printers", p->name);
798 cupsArrayRemove(Printers, p);
799
800 if (p->type & CUPS_PRINTER_IMPLICIT)
801 {
802 cupsdLogMessage(CUPSD_LOG_DEBUG2,
803 "cupsdDeletePrinter: Removing %s from ImplicitPrinters",
804 p->name);
805 cupsArrayRemove(ImplicitPrinters, p);
806 }
807
808 /*
809 * Remove the dummy interface/icon/option files under IRIX...
810 */
811
812 #ifdef __sgi
813 snprintf(filename, sizeof(filename), "/var/spool/lp/interface/%s", p->name);
814 unlink(filename);
815
816 snprintf(filename, sizeof(filename), "/var/spool/lp/gui_interface/ELF/%s.gui",
817 p->name);
818 unlink(filename);
819
820 snprintf(filename, sizeof(filename), "/var/spool/lp/activeicons/%s", p->name);
821 unlink(filename);
822
823 snprintf(filename, sizeof(filename), "/var/spool/lp/pod/%s.config", p->name);
824 unlink(filename);
825
826 snprintf(filename, sizeof(filename), "/var/spool/lp/pod/%s.status", p->name);
827 unlink(filename);
828
829 snprintf(filename, sizeof(filename), "/var/spool/lp/member/%s", p->name);
830 unlink(filename);
831 #endif /* __sgi */
832
833 /*
834 * If p is the default printer, assign a different one...
835 */
836
837 if (p == DefaultPrinter)
838 {
839 DefaultPrinter = NULL;
840
841 if (UseNetworkDefault)
842 {
843 /*
844 * Find the first network default printer and use it...
845 */
846
847 cupsd_printer_t *dp; /* New default printer */
848
849
850 for (dp = (cupsd_printer_t *)cupsArrayFirst(Printers);
851 dp;
852 dp = (cupsd_printer_t *)cupsArrayNext(Printers))
853 if (dp != p && (dp->type & CUPS_PRINTER_DEFAULT))
854 {
855 DefaultPrinter = dp;
856 break;
857 }
858 }
859 }
860
861 /*
862 * Remove this printer from any classes...
863 */
864
865 if (!(p->type & CUPS_PRINTER_IMPLICIT))
866 {
867 changed = cupsdDeletePrinterFromClasses(p);
868
869 /*
870 * Deregister from any browse protocols...
871 */
872
873 cupsdDeregisterPrinter(p, 1);
874 }
875
876 /*
877 * Free all memory used by the printer...
878 */
879
880 if (p->printers != NULL)
881 free(p->printers);
882
883 if (MaxPrinterHistory)
884 {
885 for (i = 0; i < p->num_history; i ++)
886 ippDelete(p->history[i]);
887
888 free(p->history);
889 }
890
891 delete_printer_filters(p);
892
893 for (i = 0; i < p->num_reasons; i ++)
894 _cupsStrFree(p->reasons[i]);
895
896 ippDelete(p->attrs);
897 ippDelete(p->ppd_attrs);
898
899 mimeDeleteType(MimeDatabase, p->filetype);
900 mimeDeleteType(MimeDatabase, p->prefiltertype);
901
902 delete_string_array(&(p->filters));
903 delete_string_array(&(p->pre_filters));
904
905 cupsdFreeStrings(&(p->users));
906 cupsdFreeQuotas(p);
907
908 cupsdClearString(&p->uri);
909 cupsdClearString(&p->hostname);
910 cupsdClearString(&p->name);
911 cupsdClearString(&p->location);
912 cupsdClearString(&p->make_model);
913 cupsdClearString(&p->info);
914 cupsdClearString(&p->job_sheets[0]);
915 cupsdClearString(&p->job_sheets[1]);
916 cupsdClearString(&p->device_uri);
917 cupsdClearString(&p->sanitized_device_uri);
918 cupsdClearString(&p->port_monitor);
919 cupsdClearString(&p->op_policy);
920 cupsdClearString(&p->error_policy);
921
922 cupsdClearString(&p->alert);
923 cupsdClearString(&p->alert_description);
924
925 #ifdef HAVE_DNSSD
926 cupsdClearString(&p->product);
927 cupsdClearString(&p->pdl);
928 #endif /* HAVE_DNSSD */
929
930 cupsArrayDelete(p->filetypes);
931
932 if (p->browse_attrs)
933 free(p->browse_attrs);
934
935 cupsFreeOptions(p->num_options, p->options);
936
937 free(p);
938
939 /*
940 * Restore the previous position in the Printers array...
941 */
942
943 cupsArrayRestore(Printers);
944
945 return (changed);
946 }
947
948
949 /*
950 * 'cupsdFindDest()' - Find a destination in the list.
951 */
952
953 cupsd_printer_t * /* O - Destination in list */
954 cupsdFindDest(const char *name) /* I - Name of printer or class to find */
955 {
956 cupsd_printer_t key; /* Search key */
957
958
959 key.name = (char *)name;
960 return ((cupsd_printer_t *)cupsArrayFind(Printers, &key));
961 }
962
963
964 /*
965 * 'cupsdFindPrinter()' - Find a printer in the list.
966 */
967
968 cupsd_printer_t * /* O - Printer in list */
969 cupsdFindPrinter(const char *name) /* I - Name of printer to find */
970 {
971 cupsd_printer_t *p; /* Printer in list */
972
973
974 if ((p = cupsdFindDest(name)) != NULL && (p->type & CUPS_PRINTER_CLASS))
975 return (NULL);
976 else
977 return (p);
978 }
979
980
981 /*
982 * 'cupsdLoadAllPrinters()' - Load printers from the printers.conf file.
983 */
984
985 void
986 cupsdLoadAllPrinters(void)
987 {
988 int i; /* Looping var */
989 cups_file_t *fp; /* printers.conf file */
990 int linenum; /* Current line number */
991 char line[4096], /* Line from file */
992 *value, /* Pointer to value */
993 *valueptr; /* Pointer into value */
994 cupsd_printer_t *p; /* Current printer */
995
996
997 /*
998 * Open the printers.conf file...
999 */
1000
1001 snprintf(line, sizeof(line), "%s/printers.conf", ServerRoot);
1002 if ((fp = cupsFileOpen(line, "r")) == NULL)
1003 {
1004 if (errno != ENOENT)
1005 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to open %s - %s", line,
1006 strerror(errno));
1007 return;
1008 }
1009
1010 /*
1011 * Read printer configurations until we hit EOF...
1012 */
1013
1014 linenum = 0;
1015 p = NULL;
1016
1017 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
1018 {
1019 /*
1020 * Decode the directive...
1021 */
1022
1023 if (!strcasecmp(line, "<Printer") ||
1024 !strcasecmp(line, "<DefaultPrinter"))
1025 {
1026 /*
1027 * <Printer name> or <DefaultPrinter name>
1028 */
1029
1030 if (p == NULL && value)
1031 {
1032 /*
1033 * Add the printer and a base file type...
1034 */
1035
1036 cupsdLogMessage(CUPSD_LOG_DEBUG, "Loading printer %s...", value);
1037
1038 p = cupsdAddPrinter(value);
1039 p->accepting = 1;
1040 p->state = IPP_PRINTER_IDLE;
1041
1042 /*
1043 * Set the default printer as needed...
1044 */
1045
1046 if (!strcasecmp(line, "<DefaultPrinter"))
1047 DefaultPrinter = p;
1048 }
1049 else
1050 cupsdLogMessage(CUPSD_LOG_ERROR,
1051 "Syntax error on line %d of printers.conf.", linenum);
1052 }
1053 else if (!strcasecmp(line, "</Printer>"))
1054 {
1055 if (p != NULL)
1056 {
1057 /*
1058 * Close out the current printer...
1059 */
1060
1061 cupsdSetPrinterAttrs(p);
1062 cupsdAddPrinterHistory(p);
1063
1064 if (strncmp(p->device_uri, "file:", 5) &&
1065 p->state != IPP_PRINTER_STOPPED)
1066 {
1067 /*
1068 * See if the backend exists...
1069 */
1070
1071 snprintf(line, sizeof(line), "%s/backend/%s", ServerBin,
1072 p->device_uri);
1073
1074 if ((valueptr = strchr(line + strlen(ServerBin), ':')) != NULL)
1075 *valueptr = '\0'; /* Chop everything but URI scheme */
1076
1077 if (access(line, 0))
1078 {
1079 /*
1080 * Backend does not exist, stop printer...
1081 */
1082
1083 p->state = IPP_PRINTER_STOPPED;
1084 snprintf(p->state_message, sizeof(p->state_message),
1085 "Backend %s does not exist!", line);
1086 }
1087 }
1088
1089 p = NULL;
1090 }
1091 else
1092 cupsdLogMessage(CUPSD_LOG_ERROR,
1093 "Syntax error on line %d of printers.conf.", linenum);
1094 }
1095 else if (!p)
1096 {
1097 cupsdLogMessage(CUPSD_LOG_ERROR,
1098 "Syntax error on line %d of printers.conf.", linenum);
1099 }
1100 else if (!strcasecmp(line, "AuthInfoRequired"))
1101 {
1102 if (!cupsdSetAuthInfoRequired(p, value, NULL))
1103 cupsdLogMessage(CUPSD_LOG_ERROR,
1104 "Bad AuthInfoRequired on line %d of printers.conf.",
1105 linenum);
1106 }
1107 else if (!strcasecmp(line, "Info"))
1108 {
1109 if (value)
1110 cupsdSetString(&p->info, value);
1111 }
1112 else if (!strcasecmp(line, "MakeModel"))
1113 {
1114 if (value)
1115 cupsdSetString(&p->make_model, value);
1116 }
1117 else if (!strcasecmp(line, "Location"))
1118 {
1119 if (value)
1120 cupsdSetString(&p->location, value);
1121 }
1122 else if (!strcasecmp(line, "DeviceURI"))
1123 {
1124 if (value)
1125 cupsdSetDeviceURI(p, value);
1126 else
1127 cupsdLogMessage(CUPSD_LOG_ERROR,
1128 "Syntax error on line %d of printers.conf.", linenum);
1129 }
1130 else if (!strcasecmp(line, "Option") && value)
1131 {
1132 /*
1133 * Option name value
1134 */
1135
1136 for (valueptr = value; *valueptr && !isspace(*valueptr & 255); valueptr ++);
1137
1138 if (!*valueptr)
1139 cupsdLogMessage(CUPSD_LOG_ERROR,
1140 "Syntax error on line %d of printers.conf.", linenum);
1141 else
1142 {
1143 for (; *valueptr && isspace(*valueptr & 255); *valueptr++ = '\0');
1144
1145 p->num_options = cupsAddOption(value, valueptr, p->num_options,
1146 &(p->options));
1147 }
1148 }
1149 else if (!strcasecmp(line, "PortMonitor"))
1150 {
1151 if (value && strcmp(value, "none"))
1152 cupsdSetString(&p->port_monitor, value);
1153 else if (value)
1154 cupsdClearString(&p->port_monitor);
1155 else
1156 cupsdLogMessage(CUPSD_LOG_ERROR,
1157 "Syntax error on line %d of printers.conf.", linenum);
1158 }
1159 else if (!strcasecmp(line, "Reason"))
1160 {
1161 if (value &&
1162 strcmp(value, "connecting-to-device") &&
1163 strcmp(value, "cups-insecure-filter-warning") &&
1164 strcmp(value, "cups-missing-filter-warning"))
1165 {
1166 for (i = 0 ; i < p->num_reasons; i ++)
1167 if (!strcmp(value, p->reasons[i]))
1168 break;
1169
1170 if (i >= p->num_reasons &&
1171 p->num_reasons < (int)(sizeof(p->reasons) / sizeof(p->reasons[0])))
1172 {
1173 p->reasons[p->num_reasons] = _cupsStrAlloc(value);
1174 p->num_reasons ++;
1175 }
1176 }
1177 else
1178 cupsdLogMessage(CUPSD_LOG_ERROR,
1179 "Syntax error on line %d of printers.conf.", linenum);
1180 }
1181 else if (!strcasecmp(line, "State"))
1182 {
1183 /*
1184 * Set the initial queue state...
1185 */
1186
1187 if (value && !strcasecmp(value, "idle"))
1188 p->state = IPP_PRINTER_IDLE;
1189 else if (value && !strcasecmp(value, "stopped"))
1190 {
1191 p->state = IPP_PRINTER_STOPPED;
1192
1193 for (i = 0 ; i < p->num_reasons; i ++)
1194 if (!strcmp("paused", p->reasons[i]))
1195 break;
1196
1197 if (i >= p->num_reasons &&
1198 p->num_reasons < (int)(sizeof(p->reasons) / sizeof(p->reasons[0])))
1199 {
1200 p->reasons[p->num_reasons] = _cupsStrAlloc("paused");
1201 p->num_reasons ++;
1202 }
1203 }
1204 else
1205 cupsdLogMessage(CUPSD_LOG_ERROR,
1206 "Syntax error on line %d of printers.conf.", linenum);
1207 }
1208 else if (!strcasecmp(line, "StateMessage"))
1209 {
1210 /*
1211 * Set the initial queue state message...
1212 */
1213
1214 if (value)
1215 strlcpy(p->state_message, value, sizeof(p->state_message));
1216 }
1217 else if (!strcasecmp(line, "StateTime"))
1218 {
1219 /*
1220 * Set the state time...
1221 */
1222
1223 if (value)
1224 p->state_time = atoi(value);
1225 }
1226 else if (!strcasecmp(line, "Accepting"))
1227 {
1228 /*
1229 * Set the initial accepting state...
1230 */
1231
1232 if (value &&
1233 (!strcasecmp(value, "yes") ||
1234 !strcasecmp(value, "on") ||
1235 !strcasecmp(value, "true")))
1236 p->accepting = 1;
1237 else if (value &&
1238 (!strcasecmp(value, "no") ||
1239 !strcasecmp(value, "off") ||
1240 !strcasecmp(value, "false")))
1241 p->accepting = 0;
1242 else
1243 cupsdLogMessage(CUPSD_LOG_ERROR,
1244 "Syntax error on line %d of printers.conf.", linenum);
1245 }
1246 else if (!strcasecmp(line, "Type"))
1247 {
1248 if (value)
1249 p->type = atoi(value);
1250 else
1251 cupsdLogMessage(CUPSD_LOG_ERROR,
1252 "Syntax error on line %d of printers.conf.", linenum);
1253 }
1254 else if (!strcasecmp(line, "Product"))
1255 {
1256 if (value)
1257 {
1258 #ifdef HAVE_DNSSD
1259 p->product = _cupsStrAlloc(value);
1260 #endif /* HAVE_DNSSD */
1261 }
1262 else
1263 cupsdLogMessage(CUPSD_LOG_ERROR,
1264 "Syntax error on line %d of printers.conf.", linenum);
1265 }
1266 else if (!strcasecmp(line, "Filter"))
1267 {
1268 if (value)
1269 {
1270 if (!p->filters)
1271 p->filters = cupsArrayNew(NULL, NULL);
1272
1273 cupsArrayAdd(p->filters, _cupsStrAlloc(value));
1274 }
1275 else
1276 cupsdLogMessage(CUPSD_LOG_ERROR,
1277 "Syntax error on line %d of printers.conf.", linenum);
1278 }
1279 else if (!strcasecmp(line, "PreFilter"))
1280 {
1281 if (value)
1282 {
1283 if (!p->pre_filters)
1284 p->pre_filters = cupsArrayNew(NULL, NULL);
1285
1286 cupsArrayAdd(p->pre_filters, _cupsStrAlloc(value));
1287 }
1288 else
1289 cupsdLogMessage(CUPSD_LOG_ERROR,
1290 "Syntax error on line %d of printers.conf.", linenum);
1291 }
1292 else if (!strcasecmp(line, "Shared"))
1293 {
1294 /*
1295 * Set the initial shared state...
1296 */
1297
1298 if (value &&
1299 (!strcasecmp(value, "yes") ||
1300 !strcasecmp(value, "on") ||
1301 !strcasecmp(value, "true")))
1302 p->shared = 1;
1303 else if (value &&
1304 (!strcasecmp(value, "no") ||
1305 !strcasecmp(value, "off") ||
1306 !strcasecmp(value, "false")))
1307 p->shared = 0;
1308 else
1309 cupsdLogMessage(CUPSD_LOG_ERROR,
1310 "Syntax error on line %d of printers.conf.", linenum);
1311 }
1312 else if (!strcasecmp(line, "JobSheets"))
1313 {
1314 /*
1315 * Set the initial job sheets...
1316 */
1317
1318 if (value)
1319 {
1320 for (valueptr = value; *valueptr && !isspace(*valueptr & 255); valueptr ++);
1321
1322 if (*valueptr)
1323 *valueptr++ = '\0';
1324
1325 cupsdSetString(&p->job_sheets[0], value);
1326
1327 while (isspace(*valueptr & 255))
1328 valueptr ++;
1329
1330 if (*valueptr)
1331 {
1332 for (value = valueptr; *valueptr && !isspace(*valueptr & 255); valueptr ++);
1333
1334 if (*valueptr)
1335 *valueptr = '\0';
1336
1337 cupsdSetString(&p->job_sheets[1], value);
1338 }
1339 }
1340 else
1341 cupsdLogMessage(CUPSD_LOG_ERROR,
1342 "Syntax error on line %d of printers.conf.", linenum);
1343 }
1344 else if (!strcasecmp(line, "AllowUser"))
1345 {
1346 if (value)
1347 {
1348 p->deny_users = 0;
1349 cupsdAddString(&(p->users), value);
1350 }
1351 else
1352 cupsdLogMessage(CUPSD_LOG_ERROR,
1353 "Syntax error on line %d of printers.conf.", linenum);
1354 }
1355 else if (!strcasecmp(line, "DenyUser"))
1356 {
1357 if (value)
1358 {
1359 p->deny_users = 1;
1360 cupsdAddString(&(p->users), value);
1361 }
1362 else
1363 cupsdLogMessage(CUPSD_LOG_ERROR,
1364 "Syntax error on line %d of printers.conf.", linenum);
1365 }
1366 else if (!strcasecmp(line, "QuotaPeriod"))
1367 {
1368 if (value)
1369 p->quota_period = atoi(value);
1370 else
1371 cupsdLogMessage(CUPSD_LOG_ERROR,
1372 "Syntax error on line %d of printers.conf.", linenum);
1373 }
1374 else if (!strcasecmp(line, "PageLimit"))
1375 {
1376 if (value)
1377 p->page_limit = atoi(value);
1378 else
1379 cupsdLogMessage(CUPSD_LOG_ERROR,
1380 "Syntax error on line %d of printers.conf.", linenum);
1381 }
1382 else if (!strcasecmp(line, "KLimit"))
1383 {
1384 if (value)
1385 p->k_limit = atoi(value);
1386 else
1387 cupsdLogMessage(CUPSD_LOG_ERROR,
1388 "Syntax error on line %d of printers.conf.", linenum);
1389 }
1390 else if (!strcasecmp(line, "OpPolicy"))
1391 {
1392 if (value)
1393 {
1394 cupsd_policy_t *pol; /* Policy */
1395
1396
1397 if ((pol = cupsdFindPolicy(value)) != NULL)
1398 {
1399 cupsdSetString(&p->op_policy, value);
1400 p->op_policy_ptr = pol;
1401 }
1402 else
1403 cupsdLogMessage(CUPSD_LOG_ERROR,
1404 "Bad policy \"%s\" on line %d of printers.conf",
1405 value, linenum);
1406 }
1407 else
1408 cupsdLogMessage(CUPSD_LOG_ERROR,
1409 "Syntax error on line %d of printers.conf.", linenum);
1410 }
1411 else if (!strcasecmp(line, "ErrorPolicy"))
1412 {
1413 if (value)
1414 cupsdSetString(&p->error_policy, value);
1415 else
1416 cupsdLogMessage(CUPSD_LOG_ERROR,
1417 "Syntax error on line %d of printers.conf.", linenum);
1418 }
1419 else if (!strcasecmp(line, "Attribute") && value)
1420 {
1421 for (valueptr = value; *valueptr && !isspace(*valueptr & 255); valueptr ++);
1422
1423 if (!*valueptr)
1424 cupsdLogMessage(CUPSD_LOG_ERROR,
1425 "Syntax error on line %d of printers.conf.", linenum);
1426 else
1427 {
1428 for (; *valueptr && isspace(*valueptr & 255); *valueptr++ = '\0');
1429
1430 if (!p->attrs)
1431 cupsdSetPrinterAttrs(p);
1432
1433 if (!strcmp(value, "marker-change-time"))
1434 p->marker_time = atoi(valueptr);
1435 else
1436 cupsdSetPrinterAttr(p, value, valueptr);
1437 }
1438 }
1439 else
1440 {
1441 /*
1442 * Something else we don't understand...
1443 */
1444
1445 cupsdLogMessage(CUPSD_LOG_ERROR,
1446 "Unknown configuration directive %s on line %d of printers.conf.",
1447 line, linenum);
1448 }
1449 }
1450
1451 cupsFileClose(fp);
1452 }
1453
1454
1455 /*
1456 * 'cupsdRenamePrinter()' - Rename a printer.
1457 */
1458
1459 void
1460 cupsdRenamePrinter(
1461 cupsd_printer_t *p, /* I - Printer */
1462 const char *name) /* I - New name */
1463 {
1464 /*
1465 * Remove the printer from the array(s) first...
1466 */
1467
1468 cupsdLogMessage(CUPSD_LOG_DEBUG2,
1469 "cupsdRenamePrinter: Removing %s from Printers", p->name);
1470 cupsArrayRemove(Printers, p);
1471
1472 if (p->type & CUPS_PRINTER_IMPLICIT)
1473 {
1474 cupsdLogMessage(CUPSD_LOG_DEBUG2,
1475 "cupsdRenamePrinter: Removing %s from ImplicitPrinters",
1476 p->name);
1477 cupsArrayRemove(ImplicitPrinters, p);
1478 }
1479
1480 /*
1481 * Rename the printer type...
1482 */
1483
1484 mimeDeleteType(MimeDatabase, p->filetype);
1485 p->filetype = mimeAddType(MimeDatabase, "printer", name);
1486
1487 mimeDeleteType(MimeDatabase, p->prefiltertype);
1488 p->prefiltertype = mimeAddType(MimeDatabase, "prefilter", name);
1489
1490 /*
1491 * Rename the printer...
1492 */
1493
1494 cupsdSetString(&p->name, name);
1495
1496 /*
1497 * Reset printer attributes...
1498 */
1499
1500 cupsdSetPrinterAttrs(p);
1501
1502 /*
1503 * Add the printer back to the printer array(s)...
1504 */
1505
1506 cupsdLogMessage(CUPSD_LOG_DEBUG2,
1507 "cupsdRenamePrinter: Adding %s to Printers", p->name);
1508 cupsArrayAdd(Printers, p);
1509
1510 if (p->type & CUPS_PRINTER_IMPLICIT)
1511 {
1512 cupsdLogMessage(CUPSD_LOG_DEBUG2,
1513 "cupsdRenamePrinter: Adding %s to ImplicitPrinters",
1514 p->name);
1515 cupsArrayAdd(ImplicitPrinters, p);
1516 }
1517 }
1518
1519
1520 /*
1521 * 'cupsdSaveAllPrinters()' - Save all printer definitions to the printers.conf
1522 * file.
1523 */
1524
1525 void
1526 cupsdSaveAllPrinters(void)
1527 {
1528 int i; /* Looping var */
1529 cups_file_t *fp; /* printers.conf file */
1530 char temp[1024], /* Temporary string */
1531 backup[1024], /* printers.conf.O file */
1532 value[2048], /* Value string */
1533 *ptr, /* Pointer into value */
1534 *name; /* Current user/group name */
1535 cupsd_printer_t *printer; /* Current printer class */
1536 time_t curtime; /* Current time */
1537 struct tm *curdate; /* Current date */
1538 cups_option_t *option; /* Current option */
1539 ipp_attribute_t *marker; /* Current marker attribute */
1540
1541
1542 /*
1543 * Create the printers.conf file...
1544 */
1545
1546 snprintf(temp, sizeof(temp), "%s/printers.conf", ServerRoot);
1547 snprintf(backup, sizeof(backup), "%s/printers.conf.O", ServerRoot);
1548
1549 if (rename(temp, backup))
1550 {
1551 if (errno != ENOENT)
1552 cupsdLogMessage(CUPSD_LOG_ERROR,
1553 "Unable to backup printers.conf - %s", strerror(errno));
1554 }
1555
1556 if ((fp = cupsFileOpen(temp, "w")) == NULL)
1557 {
1558 cupsdLogMessage(CUPSD_LOG_ERROR,
1559 "Unable to save printers.conf - %s", strerror(errno));
1560
1561 if (rename(backup, temp))
1562 cupsdLogMessage(CUPSD_LOG_ERROR,
1563 "Unable to restore printers.conf - %s", strerror(errno));
1564 return;
1565 }
1566 else
1567 cupsdLogMessage(CUPSD_LOG_INFO, "Saving printers.conf...");
1568
1569 /*
1570 * Restrict access to the file...
1571 */
1572
1573 fchown(cupsFileNumber(fp), getuid(), Group);
1574 fchmod(cupsFileNumber(fp), ConfigFilePerm & 0600);
1575
1576 /*
1577 * Write a small header to the file...
1578 */
1579
1580 curtime = time(NULL);
1581 curdate = localtime(&curtime);
1582 strftime(temp, sizeof(temp) - 1, "%Y-%m-%d %H:%M", curdate);
1583
1584 cupsFilePuts(fp, "# Printer configuration file for " CUPS_SVERSION "\n");
1585 cupsFilePrintf(fp, "# Written by cupsd on %s\n", temp);
1586 cupsFilePuts(fp, "# DO NOT EDIT THIS FILE WHEN CUPSD IS RUNNING\n");
1587
1588 /*
1589 * Write each local printer known to the system...
1590 */
1591
1592 for (printer = (cupsd_printer_t *)cupsArrayFirst(Printers);
1593 printer;
1594 printer = (cupsd_printer_t *)cupsArrayNext(Printers))
1595 {
1596 /*
1597 * Skip remote destinations and printer classes...
1598 */
1599
1600 if ((printer->type & CUPS_PRINTER_DISCOVERED) ||
1601 (printer->type & CUPS_PRINTER_CLASS) ||
1602 (printer->type & CUPS_PRINTER_IMPLICIT))
1603 continue;
1604
1605 /*
1606 * Write printers as needed...
1607 */
1608
1609 if (printer == DefaultPrinter)
1610 cupsFilePrintf(fp, "<DefaultPrinter %s>\n", printer->name);
1611 else
1612 cupsFilePrintf(fp, "<Printer %s>\n", printer->name);
1613
1614 if (printer->num_auth_info_required > 0)
1615 {
1616 switch (printer->num_auth_info_required)
1617 {
1618 case 1 :
1619 strlcpy(value, printer->auth_info_required[0], sizeof(value));
1620 break;
1621
1622 case 2 :
1623 snprintf(value, sizeof(value), "%s,%s",
1624 printer->auth_info_required[0],
1625 printer->auth_info_required[1]);
1626 break;
1627
1628 case 3 :
1629 default :
1630 snprintf(value, sizeof(value), "%s,%s,%s",
1631 printer->auth_info_required[0],
1632 printer->auth_info_required[1],
1633 printer->auth_info_required[2]);
1634 break;
1635 }
1636
1637 cupsFilePutConf(fp, "AuthInfoRequired", value);
1638 }
1639
1640 if (printer->info)
1641 cupsFilePutConf(fp, "Info", printer->info);
1642
1643 if (printer->location)
1644 cupsFilePutConf(fp, "Location", printer->location);
1645
1646 if (printer->make_model)
1647 cupsFilePutConf(fp, "MakeModel", printer->make_model);
1648
1649 cupsFilePutConf(fp, "DeviceURI", printer->device_uri);
1650
1651 if (printer->port_monitor)
1652 cupsFilePutConf(fp, "PortMonitor", printer->port_monitor);
1653
1654 if (printer->state == IPP_PRINTER_STOPPED)
1655 {
1656 cupsFilePuts(fp, "State Stopped\n");
1657
1658 if (printer->state_message)
1659 cupsFilePutConf(fp, "StateMessage", printer->state_message);
1660 }
1661 else
1662 cupsFilePuts(fp, "State Idle\n");
1663
1664 cupsFilePrintf(fp, "StateTime %d\n", (int)printer->state_time);
1665
1666 for (i = 0; i < printer->num_reasons; i ++)
1667 if (strcmp(printer->reasons[i], "connecting-to-device") &&
1668 strcmp(printer->reasons[i], "cups-insecure-filter-warning") &&
1669 strcmp(printer->reasons[i], "cups-missing-filter-warning"))
1670 cupsFilePutConf(fp, "Reason", printer->reasons[i]);
1671
1672 cupsFilePrintf(fp, "Type %d\n", printer->type);
1673
1674 #ifdef HAVE_DNSSD
1675 if (printer->product)
1676 cupsFilePutConf(fp, "Product", printer->product);
1677 #endif /* HAVE_DNSSD */
1678
1679 for (ptr = (char *)cupsArrayFirst(printer->filters);
1680 ptr;
1681 ptr = (char *)cupsArrayNext(printer->filters))
1682 cupsFilePutConf(fp, "Filter", ptr);
1683
1684 for (ptr = (char *)cupsArrayFirst(printer->pre_filters);
1685 ptr;
1686 ptr = (char *)cupsArrayNext(printer->pre_filters))
1687 cupsFilePutConf(fp, "PreFilter", ptr);
1688
1689 if (printer->accepting)
1690 cupsFilePuts(fp, "Accepting Yes\n");
1691 else
1692 cupsFilePuts(fp, "Accepting No\n");
1693
1694 if (printer->shared)
1695 cupsFilePuts(fp, "Shared Yes\n");
1696 else
1697 cupsFilePuts(fp, "Shared No\n");
1698
1699 snprintf(value, sizeof(value), "%s %s", printer->job_sheets[0],
1700 printer->job_sheets[1]);
1701 cupsFilePutConf(fp, "JobSheets", value);
1702
1703 cupsFilePrintf(fp, "QuotaPeriod %d\n", printer->quota_period);
1704 cupsFilePrintf(fp, "PageLimit %d\n", printer->page_limit);
1705 cupsFilePrintf(fp, "KLimit %d\n", printer->k_limit);
1706
1707 for (name = (char *)cupsArrayFirst(printer->users);
1708 name;
1709 name = (char *)cupsArrayNext(printer->users))
1710 cupsFilePutConf(fp, printer->deny_users ? "DenyUser" : "AllowUser", name);
1711
1712 if (printer->op_policy)
1713 cupsFilePutConf(fp, "OpPolicy", printer->op_policy);
1714 if (printer->error_policy)
1715 cupsFilePutConf(fp, "ErrorPolicy", printer->error_policy);
1716
1717 for (i = printer->num_options, option = printer->options;
1718 i > 0;
1719 i --, option ++)
1720 {
1721 snprintf(value, sizeof(value), "%s %s", option->name, option->value);
1722 cupsFilePutConf(fp, "Option", value);
1723 }
1724
1725 if ((marker = ippFindAttribute(printer->attrs, "marker-colors",
1726 IPP_TAG_NAME)) != NULL)
1727 {
1728 snprintf(value, sizeof(value), "%s ", marker->name);
1729
1730 for (i = 0, ptr = value + strlen(value);
1731 i < marker->num_values && ptr < (value + sizeof(value) - 1);
1732 i ++)
1733 {
1734 if (i)
1735 *ptr++ = ',';
1736
1737 strlcpy(ptr, marker->values[i].string.text,
1738 value + sizeof(value) - ptr);
1739 ptr += strlen(ptr);
1740 }
1741
1742 *ptr = '\0';
1743 cupsFilePutConf(fp, "Attribute", value);
1744 }
1745
1746 if ((marker = ippFindAttribute(printer->attrs, "marker-levels",
1747 IPP_TAG_INTEGER)) != NULL)
1748 {
1749 cupsFilePrintf(fp, "Attribute %s %d", marker->name,
1750 marker->values[0].integer);
1751 for (i = 1; i < marker->num_values; i ++)
1752 cupsFilePrintf(fp, ",%d", marker->values[i].integer);
1753 cupsFilePuts(fp, "\n");
1754 }
1755
1756 if ((marker = ippFindAttribute(printer->attrs, "marker-low-levels",
1757 IPP_TAG_INTEGER)) != NULL)
1758 {
1759 cupsFilePrintf(fp, "Attribute %s %d", marker->name,
1760 marker->values[0].integer);
1761 for (i = 1; i < marker->num_values; i ++)
1762 cupsFilePrintf(fp, ",%d", marker->values[i].integer);
1763 cupsFilePuts(fp, "\n");
1764 }
1765
1766 if ((marker = ippFindAttribute(printer->attrs, "marker-high-levels",
1767 IPP_TAG_INTEGER)) != NULL)
1768 {
1769 cupsFilePrintf(fp, "Attribute %s %d", marker->name,
1770 marker->values[0].integer);
1771 for (i = 1; i < marker->num_values; i ++)
1772 cupsFilePrintf(fp, ",%d", marker->values[i].integer);
1773 cupsFilePuts(fp, "\n");
1774 }
1775
1776 if ((marker = ippFindAttribute(printer->attrs, "marker-message",
1777 IPP_TAG_TEXT)) != NULL)
1778 {
1779 snprintf(value, sizeof(value), "%s %s", marker->name,
1780 marker->values[0].string.text);
1781
1782 cupsFilePutConf(fp, "Attribute", value);
1783 }
1784
1785 if ((marker = ippFindAttribute(printer->attrs, "marker-names",
1786 IPP_TAG_NAME)) != NULL)
1787 {
1788 snprintf(value, sizeof(value), "%s ", marker->name);
1789
1790 for (i = 0, ptr = value + strlen(value);
1791 i < marker->num_values && ptr < (value + sizeof(value) - 1);
1792 i ++)
1793 {
1794 if (i)
1795 *ptr++ = ',';
1796
1797 strlcpy(ptr, marker->values[i].string.text,
1798 value + sizeof(value) - ptr);
1799 ptr += strlen(ptr);
1800 }
1801
1802 *ptr = '\0';
1803 cupsFilePutConf(fp, "Attribute", value);
1804 }
1805
1806 if ((marker = ippFindAttribute(printer->attrs, "marker-types",
1807 IPP_TAG_KEYWORD)) != NULL)
1808 {
1809 snprintf(value, sizeof(value), "%s ", marker->name);
1810
1811 for (i = 0, ptr = value + strlen(value);
1812 i < marker->num_values && ptr < (value + sizeof(value) - 1);
1813 i ++)
1814 {
1815 if (i)
1816 *ptr++ = ',';
1817
1818 strlcpy(ptr, marker->values[i].string.text,
1819 value + sizeof(value) - ptr);
1820 ptr += strlen(ptr);
1821 }
1822
1823 *ptr = '\0';
1824 cupsFilePutConf(fp, "Attribute", value);
1825 }
1826
1827 if (printer->marker_time)
1828 cupsFilePrintf(fp, "Attribute marker-change-time %ld\n",
1829 (long)printer->marker_time);
1830
1831 cupsFilePuts(fp, "</Printer>\n");
1832
1833 #ifdef __sgi
1834 /*
1835 * Make IRIX desktop & printer status happy
1836 */
1837
1838 write_irix_state(printer);
1839 #endif /* __sgi */
1840 }
1841
1842 cupsFileClose(fp);
1843 }
1844
1845
1846 /*
1847 * 'cupsdSetAuthInfoRequired()' - Set the required authentication info.
1848 */
1849
1850 int /* O - 1 if value OK, 0 otherwise */
1851 cupsdSetAuthInfoRequired(
1852 cupsd_printer_t *p, /* I - Printer */
1853 const char *values, /* I - Plain text value (or NULL) */
1854 ipp_attribute_t *attr) /* I - IPP attribute value (or NULL) */
1855 {
1856 int i; /* Looping var */
1857
1858
1859 p->num_auth_info_required = 0;
1860
1861 /*
1862 * Do we have a plain text value?
1863 */
1864
1865 if (values)
1866 {
1867 /*
1868 * Yes, grab the keywords...
1869 */
1870
1871 const char *end; /* End of current value */
1872
1873
1874 while (*values && p->num_auth_info_required < 4)
1875 {
1876 if ((end = strchr(values, ',')) == NULL)
1877 end = values + strlen(values);
1878
1879 if ((end - values) == 4 && !strncmp(values, "none", 4))
1880 {
1881 if (p->num_auth_info_required != 0 || *end)
1882 return (0);
1883
1884 p->auth_info_required[p->num_auth_info_required] = "none";
1885 p->num_auth_info_required ++;
1886
1887 return (1);
1888 }
1889 else if ((end - values) == 9 && !strncmp(values, "negotiate", 9))
1890 {
1891 if (p->num_auth_info_required != 0 || *end)
1892 return (0);
1893
1894 p->auth_info_required[p->num_auth_info_required] = "negotiate";
1895 p->num_auth_info_required ++;
1896 }
1897 else if ((end - values) == 6 && !strncmp(values, "domain", 6))
1898 {
1899 p->auth_info_required[p->num_auth_info_required] = "domain";
1900 p->num_auth_info_required ++;
1901 }
1902 else if ((end - values) == 8 && !strncmp(values, "password", 8))
1903 {
1904 p->auth_info_required[p->num_auth_info_required] = "password";
1905 p->num_auth_info_required ++;
1906 }
1907 else if ((end - values) == 8 && !strncmp(values, "username", 8))
1908 {
1909 p->auth_info_required[p->num_auth_info_required] = "username";
1910 p->num_auth_info_required ++;
1911 }
1912 else
1913 return (0);
1914
1915 values = (*end) ? end + 1 : end;
1916 }
1917
1918 if (p->num_auth_info_required == 0)
1919 {
1920 p->auth_info_required[0] = "none";
1921 p->num_auth_info_required = 1;
1922 }
1923
1924 /*
1925 * Update the printer-type value as needed...
1926 */
1927
1928 if (p->num_auth_info_required > 1 ||
1929 strcmp(p->auth_info_required[0], "none"))
1930 p->type |= CUPS_PRINTER_AUTHENTICATED;
1931 else
1932 p->type &= ~CUPS_PRINTER_AUTHENTICATED;
1933
1934 return (1);
1935 }
1936
1937 /*
1938 * Grab values from an attribute instead...
1939 */
1940
1941 if (!attr || attr->num_values > 4)
1942 return (0);
1943
1944 /*
1945 * Update the printer-type value as needed...
1946 */
1947
1948 if (attr->num_values > 1 ||
1949 strcmp(attr->values[0].string.text, "none"))
1950 p->type |= CUPS_PRINTER_AUTHENTICATED;
1951 else
1952 p->type &= ~CUPS_PRINTER_AUTHENTICATED;
1953
1954 for (i = 0; i < attr->num_values; i ++)
1955 {
1956 if (!strcmp(attr->values[i].string.text, "none"))
1957 {
1958 if (p->num_auth_info_required != 0 || attr->num_values != 1)
1959 return (0);
1960
1961 p->auth_info_required[p->num_auth_info_required] = "none";
1962 p->num_auth_info_required ++;
1963
1964 return (1);
1965 }
1966 else if (!strcmp(attr->values[i].string.text, "negotiate"))
1967 {
1968 if (p->num_auth_info_required != 0 || attr->num_values != 1)
1969 return (0);
1970
1971 p->auth_info_required[p->num_auth_info_required] = "negotiate";
1972 p->num_auth_info_required ++;
1973
1974 return (1);
1975 }
1976 else if (!strcmp(attr->values[i].string.text, "domain"))
1977 {
1978 p->auth_info_required[p->num_auth_info_required] = "domain";
1979 p->num_auth_info_required ++;
1980 }
1981 else if (!strcmp(attr->values[i].string.text, "password"))
1982 {
1983 p->auth_info_required[p->num_auth_info_required] = "password";
1984 p->num_auth_info_required ++;
1985 }
1986 else if (!strcmp(attr->values[i].string.text, "username"))
1987 {
1988 p->auth_info_required[p->num_auth_info_required] = "username";
1989 p->num_auth_info_required ++;
1990 }
1991 else
1992 return (0);
1993 }
1994
1995 return (1);
1996 }
1997
1998
1999 /*
2000 * 'cupsdSetDeviceURI()' - Set the device URI for a printer.
2001 */
2002
2003 void
2004 cupsdSetDeviceURI(cupsd_printer_t *p, /* I - Printer */
2005 const char *uri) /* I - Device URI */
2006 {
2007 char buffer[1024], /* URI buffer */
2008 *start, /* Start of data after scheme */
2009 *slash, /* First slash after scheme:// */
2010 *ptr; /* Pointer into user@host:port part */
2011
2012
2013 /*
2014 * Set the full device URI..
2015 */
2016
2017 cupsdSetString(&(p->device_uri), uri);
2018
2019 /*
2020 * Copy the device URI to a temporary buffer so we can sanitize any auth
2021 * info in it...
2022 */
2023
2024 strlcpy(buffer, uri, sizeof(buffer));
2025
2026 /*
2027 * Find the end of the scheme:// part...
2028 */
2029
2030 if ((ptr = strchr(buffer, ':')) != NULL)
2031 {
2032 for (start = ptr + 1; *start; start ++)
2033 if (*start != '/')
2034 break;
2035
2036 /*
2037 * Find the next slash (/) in the URI...
2038 */
2039
2040 if ((slash = strchr(start, '/')) == NULL)
2041 slash = start + strlen(start); /* No slash, point to the end */
2042
2043 /*
2044 * Check for an @ sign before the slash...
2045 */
2046
2047 if ((ptr = strchr(start, '@')) != NULL && ptr < slash)
2048 {
2049 /*
2050 * Found an @ sign and it is before the resource part, so we have
2051 * an authentication string. Copy the remaining URI over the
2052 * authentication string...
2053 */
2054
2055 _cups_strcpy(start, ptr + 1);
2056 }
2057 }
2058
2059 /*
2060 * Save the sanitized URI...
2061 */
2062
2063 cupsdSetString(&(p->sanitized_device_uri), buffer);
2064 }
2065
2066
2067 /*
2068 * 'cupsdSetPrinterAttr()' - Set a printer attribute.
2069 */
2070
2071 void
2072 cupsdSetPrinterAttr(
2073 cupsd_printer_t *p, /* I - Printer */
2074 const char *name, /* I - Attribute name */
2075 char *value) /* I - Attribute value string */
2076 {
2077 ipp_attribute_t *attr; /* Attribute */
2078 int i, /* Looping var */
2079 count; /* Number of values */
2080 char *ptr; /* Pointer into value */
2081 ipp_tag_t value_tag; /* Value tag for this attribute */
2082
2083
2084 /*
2085 * Don't allow empty values...
2086 */
2087
2088 if (!*value && strcmp(name, "marker-message"))
2089 {
2090 cupsdLogMessage(CUPSD_LOG_ERROR, "Ignoring empty \"%s\" attribute", name);
2091 return;
2092 }
2093
2094 /*
2095 * Count the number of values...
2096 */
2097
2098 for (count = 1, ptr = value;
2099 (ptr = strchr(ptr, ',')) != NULL;
2100 ptr ++, count ++);
2101
2102 /*
2103 * Then add or update the attribute as needed...
2104 */
2105
2106 if (!strcmp(name, "marker-levels") || !strcmp(name, "marker-low-levels") ||
2107 !strcmp(name, "marker-high-levels"))
2108 {
2109 /*
2110 * Integer values...
2111 */
2112
2113 if ((attr = ippFindAttribute(p->attrs, name, IPP_TAG_INTEGER)) != NULL &&
2114 attr->num_values < count)
2115 {
2116 ippDeleteAttribute(p->attrs, attr);
2117 attr = NULL;
2118 }
2119
2120 if (attr)
2121 attr->num_values = count;
2122 else
2123 attr = ippAddIntegers(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER, name,
2124 count, NULL);
2125
2126 if (!attr)
2127 {
2128 cupsdLogMessage(CUPSD_LOG_ERROR,
2129 "Unable to allocate memory for printer attribute "
2130 "(%d values)", count);
2131 return;
2132 }
2133
2134 for (i = 0; i < count; i ++)
2135 {
2136 if ((ptr = strchr(value, ',')) != NULL)
2137 *ptr++ = '\0';
2138
2139 attr->values[i].integer = strtol(value, NULL, 10);
2140
2141 if (ptr)
2142 value = ptr;
2143 }
2144 }
2145 else
2146 {
2147 /*
2148 * Name or keyword values...
2149 */
2150
2151 if (!strcmp(name, "marker-types"))
2152 value_tag = IPP_TAG_KEYWORD;
2153 else if (!strcmp(name, "marker-message"))
2154 value_tag = IPP_TAG_TEXT;
2155 else
2156 value_tag = IPP_TAG_NAME;
2157
2158 if ((attr = ippFindAttribute(p->attrs, name, value_tag)) != NULL &&
2159 attr->num_values < count)
2160 {
2161 ippDeleteAttribute(p->attrs, attr);
2162 attr = NULL;
2163 }
2164
2165 if (attr)
2166 {
2167 for (i = 0; i < attr->num_values; i ++)
2168 _cupsStrFree(attr->values[i].string.text);
2169
2170 attr->num_values = count;
2171 }
2172 else
2173 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, value_tag, name,
2174 count, NULL, NULL);
2175
2176 if (!attr)
2177 {
2178 cupsdLogMessage(CUPSD_LOG_ERROR,
2179 "Unable to allocate memory for printer attribute "
2180 "(%d values)", count);
2181 return;
2182 }
2183
2184 for (i = 0; i < count; i ++)
2185 {
2186 if ((ptr = strchr(value, ',')) != NULL)
2187 *ptr++ = '\0';
2188
2189 attr->values[i].string.text = _cupsStrAlloc(value);
2190
2191 if (ptr)
2192 value = ptr;
2193 }
2194 }
2195 }
2196
2197
2198 /*
2199 * 'cupsdSetPrinterAttrs()' - Set printer attributes based upon the PPD file.
2200 */
2201
2202 void
2203 cupsdSetPrinterAttrs(cupsd_printer_t *p)/* I - Printer to setup */
2204 {
2205 int i, /* Looping var */
2206 length; /* Length of browse attributes */
2207 char resource[HTTP_MAX_URI]; /* Resource portion of URI */
2208 int num_air; /* Number of auth-info-required values */
2209 const char * const *air; /* auth-info-required values */
2210 cupsd_location_t *auth; /* Pointer to authentication element */
2211 const char *auth_supported; /* Authentication supported */
2212 ipp_t *oldattrs; /* Old printer attributes */
2213 ipp_attribute_t *attr; /* Attribute data */
2214 cups_option_t *option; /* Current printer option */
2215 char *name, /* Current user/group name */
2216 *filter; /* Current filter */
2217 static const char * const air_none[] =
2218 { /* No authentication */
2219 "none"
2220 };
2221 static const char * const air_userpass[] =
2222 { /* Basic/Digest authentication */
2223 "username",
2224 "password"
2225 };
2226
2227
2228 DEBUG_printf(("cupsdSetPrinterAttrs: entering name = %s, type = %x\n", p->name,
2229 p->type));
2230
2231 /*
2232 * Make sure that we have the common attributes defined...
2233 */
2234
2235 if (!CommonData)
2236 cupsdCreateCommonData();
2237
2238 /*
2239 * Clear out old filters, if any...
2240 */
2241
2242 delete_printer_filters(p);
2243
2244 /*
2245 * Figure out the authentication that is required for the printer.
2246 */
2247
2248 auth_supported = "requesting-user-name";
2249 num_air = 1;
2250 air = air_none;
2251
2252 if (p->num_auth_info_required > 0 && strcmp(p->auth_info_required[0], "none"))
2253 {
2254 num_air = p->num_auth_info_required;
2255 air = p->auth_info_required;
2256 }
2257 else if ((p->type & CUPS_PRINTER_AUTHENTICATED) &&
2258 (p->type & CUPS_PRINTER_DISCOVERED))
2259 {
2260 num_air = 2;
2261 air = air_userpass;
2262 }
2263
2264 if (p->type & CUPS_PRINTER_CLASS)
2265 snprintf(resource, sizeof(resource), "/classes/%s", p->name);
2266 else
2267 snprintf(resource, sizeof(resource), "/printers/%s", p->name);
2268
2269 if ((auth = cupsdFindBest(resource, HTTP_POST)) == NULL ||
2270 auth->type == CUPSD_AUTH_NONE)
2271 auth = cupsdFindPolicyOp(p->op_policy_ptr, IPP_PRINT_JOB);
2272
2273 if (auth)
2274 {
2275 int auth_type; /* Authentication type */
2276
2277
2278 if ((auth_type = auth->type) == CUPSD_AUTH_DEFAULT)
2279 auth_type = DefaultAuthType;
2280
2281 if (auth_type == CUPSD_AUTH_BASIC || auth_type == CUPSD_AUTH_BASICDIGEST)
2282 auth_supported = "basic";
2283 else if (auth_type == CUPSD_AUTH_DIGEST)
2284 auth_supported = "digest";
2285 #ifdef HAVE_GSSAPI
2286 else if (auth_type == CUPSD_AUTH_NEGOTIATE)
2287 auth_supported = "negotiate";
2288 #endif /* HAVE_GSSAPI */
2289
2290 if (!(p->type & CUPS_PRINTER_DISCOVERED))
2291 {
2292 if (auth_type != CUPSD_AUTH_NONE)
2293 p->type |= CUPS_PRINTER_AUTHENTICATED;
2294 else
2295 p->type &= ~CUPS_PRINTER_AUTHENTICATED;
2296 }
2297 }
2298 else if (!(p->type & CUPS_PRINTER_DISCOVERED))
2299 p->type &= ~CUPS_PRINTER_AUTHENTICATED;
2300
2301 /*
2302 * Create the required IPP attributes for a printer...
2303 */
2304
2305 oldattrs = p->attrs;
2306 p->attrs = ippNew();
2307
2308 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2309 "uri-authentication-supported", NULL, auth_supported);
2310 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2311 "uri-security-supported", NULL, "none");
2312 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME, "printer-name", NULL,
2313 p->name);
2314 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-location",
2315 NULL, p->location ? p->location : "");
2316 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-info",
2317 NULL, p->info ? p->info : "");
2318
2319 if (cupsArrayCount(p->users) > 0)
2320 {
2321 if (p->deny_users)
2322 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
2323 "requesting-user-name-denied",
2324 cupsArrayCount(p->users), NULL, NULL);
2325 else
2326 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
2327 "requesting-user-name-allowed",
2328 cupsArrayCount(p->users), NULL, NULL);
2329
2330 for (i = 0, name = (char *)cupsArrayFirst(p->users);
2331 name;
2332 i ++, name = (char *)cupsArrayNext(p->users))
2333 attr->values[i].string.text = _cupsStrRetain(name);
2334 }
2335
2336 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
2337 "job-quota-period", p->quota_period);
2338 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
2339 "job-k-limit", p->k_limit);
2340 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
2341 "job-page-limit", p->page_limit);
2342 ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2343 "auth-info-required", num_air, NULL, air);
2344
2345 if (cupsArrayCount(Banners) > 0 && !(p->type & CUPS_PRINTER_DISCOVERED))
2346 {
2347 /*
2348 * Setup the job-sheets-default attribute...
2349 */
2350
2351 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
2352 "job-sheets-default", 2, NULL, NULL);
2353
2354 if (attr != NULL)
2355 {
2356 attr->values[0].string.text = _cupsStrAlloc(Classification ?
2357 Classification : p->job_sheets[0]);
2358 attr->values[1].string.text = _cupsStrAlloc(Classification ?
2359 Classification : p->job_sheets[1]);
2360 }
2361 }
2362
2363 p->raw = 0;
2364 p->remote = 0;
2365
2366 if (p->type & CUPS_PRINTER_DISCOVERED)
2367 {
2368 /*
2369 * Tell the client this is a remote printer of some type...
2370 */
2371
2372 if (strchr(p->uri, '?'))
2373 {
2374 /*
2375 * Strip trailing "?options" from URI...
2376 */
2377
2378 char *ptr; /* Pointer into URI */
2379
2380 strlcpy(resource, p->uri, sizeof(resource));
2381 if ((ptr = strchr(resource, '?')) != NULL)
2382 *ptr = '\0';
2383
2384 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI,
2385 "printer-uri-supported", NULL, resource);
2386 }
2387 else
2388 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI,
2389 "printer-uri-supported", NULL, p->uri);
2390
2391 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI, "printer-more-info",
2392 NULL, p->uri);
2393
2394 if (p->make_model)
2395 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
2396 "printer-make-and-model", NULL, p->make_model);
2397
2398 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI, "device-uri", NULL,
2399 p->uri);
2400
2401 p->raw = 1;
2402 p->remote = 1;
2403 }
2404 else
2405 {
2406 /*
2407 * Assign additional attributes depending on whether this is a printer
2408 * or class...
2409 */
2410
2411 if (p->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT))
2412 {
2413 p->raw = 1;
2414 p->type &= ~CUPS_PRINTER_OPTIONS;
2415
2416 /*
2417 * Add class-specific attributes...
2418 */
2419
2420 if ((p->type & CUPS_PRINTER_IMPLICIT) && p->num_printers > 0 &&
2421 p->printers[0]->make_model)
2422 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
2423 "printer-make-and-model", NULL, p->printers[0]->make_model);
2424 else
2425 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
2426 "printer-make-and-model", NULL, "Local Printer Class");
2427
2428 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI, "device-uri", NULL,
2429 "file:///dev/null");
2430
2431 if (p->num_printers > 0)
2432 {
2433 /*
2434 * Add a list of member names; URIs are added in copy_printer_attrs...
2435 */
2436
2437 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
2438 "member-names", p->num_printers, NULL, NULL);
2439 p->type |= CUPS_PRINTER_OPTIONS;
2440
2441 for (i = 0; i < p->num_printers; i ++)
2442 {
2443 if (attr != NULL)
2444 attr->values[i].string.text = _cupsStrRetain(p->printers[i]->name);
2445
2446 p->type &= ~CUPS_PRINTER_OPTIONS | p->printers[i]->type;
2447 }
2448 }
2449 }
2450 else
2451 {
2452 /*
2453 * Add printer-specific attributes...
2454 */
2455
2456 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI, "device-uri", NULL,
2457 p->sanitized_device_uri);
2458
2459 /*
2460 * Assign additional attributes from the PPD file (if any)...
2461 */
2462
2463 load_ppd(p);
2464
2465 /*
2466 * Add filters for printer...
2467 */
2468
2469 cupsdSetPrinterReasons(p, "-cups-missing-filter-warning,"
2470 "cups-insecure-filter-warning");
2471
2472 for (filter = (char *)cupsArrayFirst(p->filters);
2473 filter;
2474 filter = (char *)cupsArrayNext(p->filters))
2475 add_printer_filter(p, p->filetype, filter);
2476
2477 if (p->pre_filters)
2478 {
2479 p->prefiltertype = mimeAddType(MimeDatabase, "prefilter", p->name);
2480
2481 for (filter = (char *)cupsArrayFirst(p->pre_filters);
2482 filter;
2483 filter = (char *)cupsArrayNext(p->pre_filters))
2484 add_printer_filter(p, p->prefiltertype, filter);
2485 }
2486 }
2487 }
2488
2489 /*
2490 * Copy marker attributes as needed...
2491 */
2492
2493 if (oldattrs)
2494 {
2495 ipp_attribute_t *oldattr; /* Old attribute */
2496
2497
2498 if ((oldattr = ippFindAttribute(oldattrs, "marker-colors",
2499 IPP_TAG_NAME)) != NULL)
2500 {
2501 if ((attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
2502 "marker-colors", oldattr->num_values, NULL,
2503 NULL)) != NULL)
2504 {
2505 for (i = 0; i < oldattr->num_values; i ++)
2506 attr->values[i].string.text =
2507 _cupsStrRetain(oldattr->values[i].string.text);
2508 }
2509 }
2510
2511 if ((oldattr = ippFindAttribute(oldattrs, "marker-levels",
2512 IPP_TAG_INTEGER)) != NULL)
2513 {
2514 if ((attr = ippAddIntegers(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
2515 "marker-levels", oldattr->num_values,
2516 NULL)) != NULL)
2517 {
2518 for (i = 0; i < oldattr->num_values; i ++)
2519 attr->values[i].integer = oldattr->values[i].integer;
2520 }
2521 }
2522
2523 if ((oldattr = ippFindAttribute(oldattrs, "marker-message",
2524 IPP_TAG_TEXT)) != NULL)
2525 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT, "marker-message",
2526 NULL, oldattr->values[0].string.text);
2527
2528 if ((oldattr = ippFindAttribute(oldattrs, "marker-low-levels",
2529 IPP_TAG_INTEGER)) != NULL)
2530 {
2531 if ((attr = ippAddIntegers(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
2532 "marker-low-levels", oldattr->num_values,
2533 NULL)) != NULL)
2534 {
2535 for (i = 0; i < oldattr->num_values; i ++)
2536 attr->values[i].integer = oldattr->values[i].integer;
2537 }
2538 }
2539
2540 if ((oldattr = ippFindAttribute(oldattrs, "marker-high-levels",
2541 IPP_TAG_INTEGER)) != NULL)
2542 {
2543 if ((attr = ippAddIntegers(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
2544 "marker-high-levels", oldattr->num_values,
2545 NULL)) != NULL)
2546 {
2547 for (i = 0; i < oldattr->num_values; i ++)
2548 attr->values[i].integer = oldattr->values[i].integer;
2549 }
2550 }
2551
2552 if ((oldattr = ippFindAttribute(oldattrs, "marker-names",
2553 IPP_TAG_NAME)) != NULL)
2554 {
2555 if ((attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
2556 "marker-names", oldattr->num_values, NULL,
2557 NULL)) != NULL)
2558 {
2559 for (i = 0; i < oldattr->num_values; i ++)
2560 attr->values[i].string.text =
2561 _cupsStrRetain(oldattr->values[i].string.text);
2562 }
2563 }
2564
2565 if ((oldattr = ippFindAttribute(oldattrs, "marker-types",
2566 IPP_TAG_KEYWORD)) != NULL)
2567 {
2568 if ((attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2569 "marker-types", oldattr->num_values, NULL,
2570 NULL)) != NULL)
2571 {
2572 for (i = 0; i < oldattr->num_values; i ++)
2573 attr->values[i].string.text =
2574 _cupsStrRetain(oldattr->values[i].string.text);
2575 }
2576 }
2577
2578 ippDelete(oldattrs);
2579 }
2580
2581 /*
2582 * Force sharing off for remote queues...
2583 */
2584
2585 if (p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT))
2586 p->shared = 0;
2587 else
2588 {
2589 /*
2590 * Copy the printer options into a browse attributes string we can re-use.
2591 */
2592
2593 const char *valptr; /* Pointer into value */
2594 char *attrptr; /* Pointer into attribute string */
2595
2596
2597 /*
2598 * Free the old browse attributes as needed...
2599 */
2600
2601 if (p->browse_attrs)
2602 free(p->browse_attrs);
2603
2604 /*
2605 * Compute the length of all attributes + job-sheets, lease-duration,
2606 * and BrowseLocalOptions.
2607 */
2608
2609 for (length = 1, i = p->num_options, option = p->options;
2610 i > 0;
2611 i --, option ++)
2612 {
2613 length += strlen(option->name) + 2;
2614
2615 if (option->value)
2616 {
2617 for (valptr = option->value; *valptr; valptr ++)
2618 if (strchr(" \"\'\\", *valptr))
2619 length += 2;
2620 else
2621 length ++;
2622 }
2623 }
2624
2625 length += 13 + strlen(p->job_sheets[0]) + strlen(p->job_sheets[1]);
2626 length += 32;
2627 if (BrowseLocalOptions)
2628 length += 12 + strlen(BrowseLocalOptions);
2629
2630 /*
2631 * Allocate the new string...
2632 */
2633
2634 if ((p->browse_attrs = calloc(1, length)) == NULL)
2635 cupsdLogMessage(CUPSD_LOG_ERROR,
2636 "Unable to allocate %d bytes for browse data!",
2637 length);
2638 else
2639 {
2640 /*
2641 * Got the allocated string, now copy the options and attributes over...
2642 */
2643
2644 sprintf(p->browse_attrs, "job-sheets=%s,%s lease-duration=%d",
2645 p->job_sheets[0], p->job_sheets[1], BrowseTimeout);
2646 attrptr = p->browse_attrs + strlen(p->browse_attrs);
2647
2648 if (BrowseLocalOptions)
2649 {
2650 sprintf(attrptr, " ipp-options=%s", BrowseLocalOptions);
2651 attrptr += strlen(attrptr);
2652 }
2653
2654 for (i = p->num_options, option = p->options;
2655 i > 0;
2656 i --, option ++)
2657 {
2658 *attrptr++ = ' ';
2659 strcpy(attrptr, option->name);
2660 attrptr += strlen(attrptr);
2661
2662 if (option->value)
2663 {
2664 *attrptr++ = '=';
2665
2666 for (valptr = option->value; *valptr; valptr ++)
2667 {
2668 if (strchr(" \"\'\\", *valptr))
2669 *attrptr++ = '\\';
2670
2671 *attrptr++ = *valptr;
2672 }
2673 }
2674 }
2675 }
2676 }
2677
2678 /*
2679 * Populate the document-format-supported attribute...
2680 */
2681
2682 add_printer_formats(p);
2683
2684 DEBUG_printf(("cupsdSetPrinterAttrs: leaving name = %s, type = %x\n", p->name,
2685 p->type));
2686
2687 /*
2688 * Add name-default attributes...
2689 */
2690
2691 add_printer_defaults(p);
2692
2693 #ifdef __sgi
2694 /*
2695 * Write the IRIX printer config and status files...
2696 */
2697
2698 write_irix_config(p);
2699 write_irix_state(p);
2700 #endif /* __sgi */
2701
2702 /*
2703 * Let the browse protocols reflect the change
2704 */
2705
2706 cupsdRegisterPrinter(p);
2707 }
2708
2709
2710 /*
2711 * 'cupsdSetPrinterReasons()' - Set/update the reasons strings.
2712 */
2713
2714 int /* O - 1 if something changed, 0 otherwise */
2715 cupsdSetPrinterReasons(
2716 cupsd_printer_t *p, /* I - Printer */
2717 const char *s) /* I - Reasons strings */
2718 {
2719 int i, /* Looping var */
2720 changed = 0; /* Did something change? */
2721 const char *sptr; /* Pointer into reasons */
2722 char reason[255], /* Reason string */
2723 *rptr; /* Pointer into reason */
2724
2725
2726 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2727 "cupsdSetPrinterReasons(p=%p(%s),s=\"%s\"", p, p->name, s);
2728
2729 if (s[0] == '-' || s[0] == '+')
2730 {
2731 /*
2732 * Add/remove reasons...
2733 */
2734
2735 sptr = s + 1;
2736 }
2737 else
2738 {
2739 /*
2740 * Replace reasons...
2741 */
2742
2743 sptr = s;
2744
2745 for (i = 0; i < p->num_reasons; i ++)
2746 _cupsStrFree(p->reasons[i]);
2747
2748 p->num_reasons = 0;
2749 changed = 1;
2750
2751 dirty_printer(p);
2752 }
2753
2754 if (!strcmp(s, "none"))
2755 return (changed);
2756
2757 /*
2758 * Loop through all of the reasons...
2759 */
2760
2761 while (*sptr)
2762 {
2763 /*
2764 * Skip leading whitespace and commas...
2765 */
2766
2767 while (isspace(*sptr & 255) || *sptr == ',')
2768 sptr ++;
2769
2770 for (rptr = reason; *sptr && !isspace(*sptr & 255) && *sptr != ','; sptr ++)
2771 if (rptr < (reason + sizeof(reason) - 1))
2772 *rptr++ = *sptr;
2773
2774 if (rptr == reason)
2775 break;
2776
2777 *rptr = '\0';
2778
2779 if (s[0] == '-')
2780 {
2781 /*
2782 * Remove reason...
2783 */
2784
2785 for (i = 0; i < p->num_reasons; i ++)
2786 if (!strcmp(reason, p->reasons[i]))
2787 {
2788 /*
2789 * Found a match, so remove it...
2790 */
2791
2792 p->num_reasons --;
2793 changed = 1;
2794 _cupsStrFree(p->reasons[i]);
2795
2796 if (i < p->num_reasons)
2797 memmove(p->reasons + i, p->reasons + i + 1,
2798 (p->num_reasons - i) * sizeof(char *));
2799
2800 if (!strcmp(reason, "paused") && p->state == IPP_PRINTER_STOPPED)
2801 cupsdSetPrinterState(p, IPP_PRINTER_IDLE, 1);
2802
2803 if (strcmp(reason, "connecting-to-device"))
2804 dirty_printer(p);
2805 break;
2806 }
2807 }
2808 else if (p->num_reasons < (int)(sizeof(p->reasons) / sizeof(p->reasons[0])))
2809 {
2810 /*
2811 * Add reason...
2812 */
2813
2814 for (i = 0; i < p->num_reasons; i ++)
2815 if (!strcmp(reason, p->reasons[i]))
2816 break;
2817
2818 if (i >= p->num_reasons)
2819 {
2820 if (i >= (int)(sizeof(p->reasons) / sizeof(p->reasons[0])))
2821 {
2822 cupsdLogMessage(CUPSD_LOG_ALERT,
2823 "Too many printer-state-reasons values for %s (%d)",
2824 p->name, i + 1);
2825 return (changed);
2826 }
2827
2828 p->reasons[i] = _cupsStrAlloc(reason);
2829 p->num_reasons ++;
2830 changed = 1;
2831
2832 if (!strcmp(reason, "paused") && p->state != IPP_PRINTER_STOPPED)
2833 cupsdSetPrinterState(p, IPP_PRINTER_STOPPED, 1);
2834
2835 if (strcmp(reason, "connecting-to-device"))
2836 dirty_printer(p);
2837 }
2838 }
2839 }
2840
2841 return (changed);
2842 }
2843
2844
2845 /*
2846 * 'cupsdSetPrinterState()' - Update the current state of a printer.
2847 */
2848
2849 void
2850 cupsdSetPrinterState(
2851 cupsd_printer_t *p, /* I - Printer to change */
2852 ipp_pstate_t s, /* I - New state */
2853 int update) /* I - Update printers.conf? */
2854 {
2855 ipp_pstate_t old_state; /* Old printer state */
2856 static const char * const printer_states[] =
2857 { /* State strings */
2858 "idle",
2859 "processing",
2860 "stopped"
2861 };
2862
2863
2864 /*
2865 * Can't set status of remote printers...
2866 */
2867
2868 if (p->type & CUPS_PRINTER_DISCOVERED)
2869 return;
2870
2871 /*
2872 * Set the new state...
2873 */
2874
2875 old_state = p->state;
2876 p->state = s;
2877
2878 if (old_state != s)
2879 {
2880 cupsdAddEvent(s == IPP_PRINTER_STOPPED ? CUPSD_EVENT_PRINTER_STOPPED :
2881 CUPSD_EVENT_PRINTER_STATE, p, NULL,
2882 "%s \"%s\" state changed to %s.",
2883 (p->type & CUPS_PRINTER_CLASS) ? "Class" : "Printer",
2884 p->name, printer_states[p->state - IPP_PRINTER_IDLE]);
2885
2886 /*
2887 * Let the browse code know this needs to be updated...
2888 */
2889
2890 BrowseNext = p;
2891 p->state_time = time(NULL);
2892 p->browse_time = 0;
2893
2894 #ifdef __sgi
2895 write_irix_state(p);
2896 #endif /* __sgi */
2897 }
2898
2899 /*
2900 * Set/clear the paused reason as needed...
2901 */
2902
2903 if (s == IPP_PRINTER_STOPPED)
2904 cupsdSetPrinterReasons(p, "+paused");
2905 else
2906 cupsdSetPrinterReasons(p, "-paused");
2907
2908 /*
2909 * Clear the message for the queue when going to processing...
2910 */
2911
2912 if (s == IPP_PRINTER_PROCESSING)
2913 p->state_message[0] = '\0';
2914
2915 /*
2916 * Update the printer history...
2917 */
2918
2919 cupsdAddPrinterHistory(p);
2920
2921 /*
2922 * Let the browse protocols reflect the change...
2923 */
2924
2925 if (update)
2926 cupsdRegisterPrinter(p);
2927
2928 /*
2929 * Save the printer configuration if a printer goes from idle or processing
2930 * to stopped (or visa-versa)...
2931 */
2932
2933 if (update &&
2934 (old_state == IPP_PRINTER_STOPPED) != (s == IPP_PRINTER_STOPPED))
2935 dirty_printer(p);
2936 }
2937
2938
2939 /*
2940 * 'cupsdStopPrinter()' - Stop a printer from printing any jobs...
2941 */
2942
2943 void
2944 cupsdStopPrinter(cupsd_printer_t *p, /* I - Printer to stop */
2945 int update)/* I - Update printers.conf? */
2946 {
2947 /*
2948 * Set the printer state...
2949 */
2950
2951 cupsdSetPrinterState(p, IPP_PRINTER_STOPPED, update);
2952
2953 /*
2954 * See if we have a job printing on this printer...
2955 */
2956
2957 if (p->job && p->job->state_value == IPP_JOB_PROCESSING)
2958 cupsdSetJobState(p->job, IPP_JOB_PENDING, CUPSD_JOB_DEFAULT,
2959 "Job stopped due to printer being paused.");
2960 }
2961
2962
2963 /*
2964 * 'cupsdUpdatePrinterPPD()' - Update keywords in a printer's PPD file.
2965 */
2966
2967 int /* O - 1 if successful, 0 otherwise */
2968 cupsdUpdatePrinterPPD(
2969 cupsd_printer_t *p, /* I - Printer */
2970 int num_keywords, /* I - Number of keywords */
2971 cups_option_t *keywords) /* I - Keywords */
2972 {
2973 int i; /* Looping var */
2974 cups_file_t *src, /* Original file */
2975 *dst; /* New file */
2976 char srcfile[1024], /* Original filename */
2977 dstfile[1024], /* New filename */
2978 line[1024], /* Line from file */
2979 keystring[41]; /* Keyword from line */
2980 cups_option_t *keyword; /* Current keyword */
2981
2982
2983 cupsdLogMessage(CUPSD_LOG_INFO, "Updating keywords in PPD file for %s...",
2984 p->name);
2985
2986 /*
2987 * Get the old and new PPD filenames...
2988 */
2989
2990 snprintf(srcfile, sizeof(srcfile), "%s/ppd/%s.ppd.O", ServerRoot, p->name);
2991 snprintf(dstfile, sizeof(srcfile), "%s/ppd/%s.ppd", ServerRoot, p->name);
2992
2993 /*
2994 * Rename the old file and open the old and new...
2995 */
2996
2997 if (rename(dstfile, srcfile))
2998 {
2999 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to backup PPD file for %s: %s",
3000 p->name, strerror(errno));
3001 return (0);
3002 }
3003
3004 if ((src = cupsFileOpen(srcfile, "r")) == NULL)
3005 {
3006 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to open PPD file \"%s\": %s",
3007 srcfile, strerror(errno));
3008 rename(srcfile, dstfile);
3009 return (0);
3010 }
3011
3012 if ((dst = cupsFileOpen(dstfile, "w")) == NULL)
3013 {
3014 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create PPD file \"%s\": %s",
3015 dstfile, strerror(errno));
3016 cupsFileClose(src);
3017 rename(srcfile, dstfile);
3018 return (0);
3019 }
3020
3021 /*
3022 * Copy the first line and then write out all of the keywords...
3023 */
3024
3025 if (!cupsFileGets(src, line, sizeof(line)))
3026 {
3027 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to read PPD file \"%s\": %s",
3028 srcfile, strerror(errno));
3029 cupsFileClose(src);
3030 cupsFileClose(dst);
3031 rename(srcfile, dstfile);
3032 return (0);
3033 }
3034
3035 cupsFilePrintf(dst, "%s\n", line);
3036
3037 for (i = num_keywords, keyword = keywords; i > 0; i --, keyword ++)
3038 {
3039 cupsdLogMessage(CUPSD_LOG_DEBUG, "*%s: %s", keyword->name, keyword->value);
3040 cupsFilePrintf(dst, "*%s: %s\n", keyword->name, keyword->value);
3041 }
3042
3043 /*
3044 * Then copy the rest of the PPD file, dropping any keywords we changed.
3045 */
3046
3047 while (cupsFileGets(src, line, sizeof(line)))
3048 {
3049 /*
3050 * Skip keywords we've already set...
3051 */
3052
3053 if (sscanf(line, "*%40[^:]:", keystring) == 1 &&
3054 cupsGetOption(keystring, num_keywords, keywords))
3055 continue;
3056
3057 /*
3058 * Otherwise write the line...
3059 */
3060
3061 cupsFilePrintf(dst, "%s\n", line);
3062 }
3063
3064 /*
3065 * Close files and return...
3066 */
3067
3068 cupsFileClose(src);
3069 cupsFileClose(dst);
3070
3071 return (1);
3072 }
3073
3074
3075 /*
3076 * 'cupsdUpdatePrinters()' - Update printers after a partial reload.
3077 */
3078
3079 void
3080 cupsdUpdatePrinters(void)
3081 {
3082 cupsd_printer_t *p; /* Current printer */
3083
3084
3085 /*
3086 * Loop through the printers and recreate the printer attributes
3087 * for any local printers since the policy and/or access control
3088 * stuff may have changed. Also, if browsing is disabled, remove
3089 * any remote printers...
3090 */
3091
3092 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
3093 p;
3094 p = (cupsd_printer_t *)cupsArrayNext(Printers))
3095 {
3096 /*
3097 * Remove remote printers if we are no longer browsing...
3098 */
3099
3100 if (!Browsing &&
3101 (p->type & (CUPS_PRINTER_IMPLICIT | CUPS_PRINTER_DISCOVERED)))
3102 {
3103 if (p->type & CUPS_PRINTER_IMPLICIT)
3104 cupsArrayRemove(ImplicitPrinters, p);
3105
3106 cupsArraySave(Printers);
3107 cupsdDeletePrinter(p, 0);
3108 cupsArrayRestore(Printers);
3109 continue;
3110 }
3111
3112 /*
3113 * Update the operation policy pointer...
3114 */
3115
3116 if ((p->op_policy_ptr = cupsdFindPolicy(p->op_policy)) == NULL)
3117 p->op_policy_ptr = DefaultPolicyPtr;
3118
3119 /*
3120 * Update printer attributes as needed...
3121 */
3122
3123 if (!(p->type & CUPS_PRINTER_DISCOVERED))
3124 cupsdSetPrinterAttrs(p);
3125 }
3126 }
3127
3128
3129 /*
3130 * 'cupsdValidateDest()' - Validate a printer/class destination.
3131 */
3132
3133 const char * /* O - Printer or class name */
3134 cupsdValidateDest(
3135 const char *uri, /* I - Printer URI */
3136 cups_ptype_t *dtype, /* O - Type (printer or class) */
3137 cupsd_printer_t **printer) /* O - Printer pointer */
3138 {
3139 cupsd_printer_t *p; /* Current printer */
3140 char localname[1024],/* Localized hostname */
3141 *lptr, /* Pointer into localized hostname */
3142 *sptr, /* Pointer into server name */
3143 *rptr, /* Pointer into resource */
3144 scheme[32], /* Scheme portion of URI */
3145 username[64], /* Username portion of URI */
3146 hostname[HTTP_MAX_HOST],
3147 /* Host portion of URI */
3148 resource[HTTP_MAX_URI];
3149 /* Resource portion of URI */
3150 int port; /* Port portion of URI */
3151
3152
3153 DEBUG_printf(("cupsdValidateDest(uri=\"%s\", dtype=%p, printer=%p)\n", uri,
3154 dtype, printer));
3155
3156 /*
3157 * Initialize return values...
3158 */
3159
3160 if (printer)
3161 *printer = NULL;
3162
3163 if (dtype)
3164 *dtype = (cups_ptype_t)0;
3165
3166 /*
3167 * Pull the hostname and resource from the URI...
3168 */
3169
3170 httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, sizeof(scheme),
3171 username, sizeof(username), hostname, sizeof(hostname),
3172 &port, resource, sizeof(resource));
3173
3174 /*
3175 * See if the resource is a class or printer...
3176 */
3177
3178 if (!strncmp(resource, "/classes/", 9))
3179 {
3180 /*
3181 * Class...
3182 */
3183
3184 rptr = resource + 9;
3185 }
3186 else if (!strncmp(resource, "/printers/", 10))
3187 {
3188 /*
3189 * Printer...
3190 */
3191
3192 rptr = resource + 10;
3193 }
3194 else
3195 {
3196 /*
3197 * Bad resource name...
3198 */
3199
3200 return (NULL);
3201 }
3202
3203 /*
3204 * See if the printer or class name exists...
3205 */
3206
3207 p = cupsdFindDest(rptr);
3208
3209 if (p == NULL && strchr(rptr, '@') == NULL)
3210 return (NULL);
3211 else if (p != NULL)
3212 {
3213 if (printer)
3214 *printer = p;
3215
3216 if (dtype)
3217 *dtype = p->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT |
3218 CUPS_PRINTER_REMOTE | CUPS_PRINTER_DISCOVERED);
3219
3220 return (p->name);
3221 }
3222
3223 /*
3224 * Change localhost to the server name...
3225 */
3226
3227 if (!strcasecmp(hostname, "localhost"))
3228 strlcpy(hostname, ServerName, sizeof(hostname));
3229
3230 strlcpy(localname, hostname, sizeof(localname));
3231
3232 if (!strcasecmp(hostname, ServerName))
3233 {
3234 /*
3235 * Localize the hostname...
3236 */
3237
3238 lptr = strchr(localname, '.');
3239 sptr = strchr(ServerName, '.');
3240
3241 if (sptr != NULL && lptr != NULL)
3242 {
3243 /*
3244 * Strip the common domain name components...
3245 */
3246
3247 while (lptr != NULL)
3248 {
3249 if (!strcasecmp(lptr, sptr))
3250 {
3251 *lptr = '\0';
3252 break;
3253 }
3254 else
3255 lptr = strchr(lptr + 1, '.');
3256 }
3257 }
3258 }
3259
3260 DEBUG_printf(("localized hostname is \"%s\"...\n", localname));
3261
3262 /*
3263 * Find a matching printer or class...
3264 */
3265
3266 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
3267 p;
3268 p = (cupsd_printer_t *)cupsArrayNext(Printers))
3269 if (!strcasecmp(p->hostname, localname) &&
3270 !strcasecmp(p->name, rptr))
3271 {
3272 if (printer)
3273 *printer = p;
3274
3275 if (dtype)
3276 *dtype = p->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT |
3277 CUPS_PRINTER_REMOTE | CUPS_PRINTER_DISCOVERED);
3278
3279 return (p->name);
3280 }
3281
3282 return (NULL);
3283 }
3284
3285
3286 /*
3287 * 'cupsdWritePrintcap()' - Write a pseudo-printcap file for older applications
3288 * that need it...
3289 */
3290
3291 void
3292 cupsdWritePrintcap(void)
3293 {
3294 int i; /* Looping var */
3295 cups_file_t *fp; /* Printcap file */
3296 cupsd_printer_t *p; /* Current printer */
3297
3298
3299 #ifdef __sgi
3300 /*
3301 * Update the IRIX printer state for the default printer; if
3302 * no printers remain, then the default printer file will be
3303 * removed...
3304 */
3305
3306 write_irix_state(DefaultPrinter);
3307 #endif /* __sgi */
3308
3309 /*
3310 * See if we have a printcap file; if not, don't bother writing it.
3311 */
3312
3313 if (!Printcap || !*Printcap)
3314 return;
3315
3316 cupsdLogMessage(CUPSD_LOG_INFO, "Generating printcap %s...", Printcap);
3317
3318 /*
3319 * Open the printcap file...
3320 */
3321
3322 if ((fp = cupsFileOpen(Printcap, "w")) == NULL)
3323 return;
3324
3325 /*
3326 * Put a comment header at the top so that users will know where the
3327 * data has come from...
3328 */
3329
3330 if (PrintcapFormat != PRINTCAP_PLIST)
3331 cupsFilePrintf(fp, "# This file was automatically generated by cupsd(8) "
3332 "from the\n"
3333 "# %s/printers.conf file. All changes to this file\n"
3334 "# will be lost.\n", ServerRoot);
3335
3336 /*
3337 * Write a new printcap with the current list of printers.
3338 */
3339
3340 switch (PrintcapFormat)
3341 {
3342 case PRINTCAP_BSD :
3343 /*
3344 * Each printer is put in the file as:
3345 *
3346 * Printer1:
3347 * Printer2:
3348 * Printer3:
3349 * ...
3350 * PrinterN:
3351 */
3352
3353 if (DefaultPrinter)
3354 cupsFilePrintf(fp, "%s|%s:rm=%s:rp=%s:\n", DefaultPrinter->name,
3355 DefaultPrinter->info, ServerName,
3356 DefaultPrinter->name);
3357
3358 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
3359 p;
3360 p = (cupsd_printer_t *)cupsArrayNext(Printers))
3361 if (p != DefaultPrinter)
3362 cupsFilePrintf(fp, "%s|%s:rm=%s:rp=%s:\n", p->name, p->info,
3363 ServerName, p->name);
3364 break;
3365
3366 case PRINTCAP_PLIST :
3367 /*
3368 * Each printer is written as a dictionary in a plist file.
3369 * Currently the printer-name, printer-info, printer-is-accepting-jobs,
3370 * printer-location, printer-make-and-model, printer-state,
3371 * printer-state-reasons, printer-type, and (sanitized) device-uri.
3372 */
3373
3374 cupsFilePuts(fp, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
3375 "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD "
3376 "PLIST 1.0//EN\" \"http://www.apple.com/DTDs/"
3377 "PropertyList-1.0.dtd\">\n"
3378 "<plist version=\"1.0\">\n"
3379 "<array>\n");
3380
3381 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
3382 p;
3383 p = (cupsd_printer_t *)cupsArrayNext(Printers))
3384 {
3385 cupsFilePuts(fp, "\t<dict>\n"
3386 "\t\t<key>printer-name</key>\n"
3387 "\t\t<string>");
3388 write_xml_string(fp, p->name);
3389 cupsFilePuts(fp, "</string>\n"
3390 "\t\t<key>printer-info</key>\n"
3391 "\t\t<string>");
3392 write_xml_string(fp, p->info);
3393 cupsFilePrintf(fp, "</string>\n"
3394 "\t\t<key>printer-is-accepting-jobs</key>\n"
3395 "\t\t<%s/>\n"
3396 "\t\t<key>printer-location</key>\n"
3397 "\t\t<string>", p->accepting ? "true" : "false");
3398 write_xml_string(fp, p->location);
3399 cupsFilePuts(fp, "</string>\n"
3400 "\t\t<key>printer-make-and-model</key>\n"
3401 "\t\t<string>");
3402 write_xml_string(fp, p->make_model);
3403 cupsFilePrintf(fp, "</string>\n"
3404 "\t\t<key>printer-state</key>\n"
3405 "\t\t<integer>%d</integer>\n"
3406 "\t\t<key>printer-state-reasons</key>\n"
3407 "\t\t<array>\n", p->state);
3408 for (i = 0; i < p->num_reasons; i ++)
3409 {
3410 cupsFilePuts(fp, "\t\t\t<string>");
3411 write_xml_string(fp, p->reasons[i]);
3412 cupsFilePuts(fp, "</string>\n");
3413 }
3414 cupsFilePrintf(fp, "\t\t</array>\n"
3415 "\t\t<key>printer-type</key>\n"
3416 "\t\t<integer>%d</integer>\n"
3417 "\t\t<key>device-uri</key>\n"
3418 "\t\t<string>", p->type);
3419 write_xml_string(fp, p->sanitized_device_uri);
3420 cupsFilePuts(fp, "</string>\n"
3421 "\t</dict>\n");
3422 }
3423 cupsFilePuts(fp, "</array>\n"
3424 "</plist>\n");
3425 break;
3426
3427 case PRINTCAP_SOLARIS :
3428 /*
3429 * Each printer is put in the file as:
3430 *
3431 * _all:all=Printer1,Printer2,Printer3,...,PrinterN
3432 * _default:use=DefaultPrinter
3433 * Printer1:\
3434 * :bsdaddr=ServerName,Printer1:\
3435 * :description=Description:
3436 * Printer2:
3437 * :bsdaddr=ServerName,Printer2:\
3438 * :description=Description:
3439 * Printer3:
3440 * :bsdaddr=ServerName,Printer3:\
3441 * :description=Description:
3442 * ...
3443 * PrinterN:
3444 * :bsdaddr=ServerName,PrinterN:\
3445 * :description=Description:
3446 */
3447
3448 cupsFilePuts(fp, "_all:all=");
3449 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
3450 p;
3451 p = (cupsd_printer_t *)cupsArrayCurrent(Printers))
3452 cupsFilePrintf(fp, "%s%c", p->name,
3453 cupsArrayNext(Printers) ? ',' : '\n');
3454
3455 if (DefaultPrinter)
3456 cupsFilePrintf(fp, "_default:use=%s\n", DefaultPrinter->name);
3457
3458 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
3459 p;
3460 p = (cupsd_printer_t *)cupsArrayNext(Printers))
3461 cupsFilePrintf(fp, "%s:\\\n"
3462 "\t:bsdaddr=%s,%s:\\\n"
3463 "\t:description=%s:\n",
3464 p->name, ServerName, p->name,
3465 p->info ? p->info : "");
3466 break;
3467 }
3468
3469 /*
3470 * Close the file...
3471 */
3472
3473 cupsFileClose(fp);
3474 }
3475
3476
3477 /*
3478 * 'add_printer_defaults()' - Add name-default attributes to the printer attributes.
3479 */
3480
3481 static void
3482 add_printer_defaults(cupsd_printer_t *p)/* I - Printer */
3483 {
3484 int i; /* Looping var */
3485 int num_options; /* Number of default options */
3486 cups_option_t *options, /* Default options */
3487 *option; /* Current option */
3488 char name[256]; /* name-default */
3489
3490
3491 /*
3492 * Maintain a common array of default attribute names...
3493 */
3494
3495 if (!CommonDefaults)
3496 {
3497 CommonDefaults = cupsArrayNew((cups_array_func_t)strcmp, NULL);
3498
3499 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("copies-default"));
3500 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("document-format-default"));
3501 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("finishings-default"));
3502 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("job-hold-until-default"));
3503 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("job-priority-default"));
3504 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("job-sheets-default"));
3505 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("media-col-default"));
3506 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("number-up-default"));
3507 cupsArrayAdd(CommonDefaults,
3508 _cupsStrAlloc("orientation-requested-default"));
3509 }
3510
3511 /*
3512 * Add all of the default options from the .conf files...
3513 */
3514
3515 for (num_options = 0, options = NULL, i = p->num_options, option = p->options;
3516 i > 0;
3517 i --, option ++)
3518 {
3519 if (strcmp(option->name, "ipp-options") &&
3520 strcmp(option->name, "job-sheets") &&
3521 strcmp(option->name, "lease-duration"))
3522 {
3523 snprintf(name, sizeof(name), "%s-default", option->name);
3524 num_options = cupsAddOption(name, option->value, num_options, &options);
3525
3526 if (!cupsArrayFind(CommonDefaults, name))
3527 cupsArrayAdd(CommonDefaults, _cupsStrAlloc(name));
3528 }
3529 }
3530
3531 /*
3532 * Convert options to IPP attributes...
3533 */
3534
3535 cupsEncodeOptions2(p->attrs, num_options, options, IPP_TAG_PRINTER);
3536 cupsFreeOptions(num_options, options);
3537
3538 /*
3539 * Add standard -default attributes as needed...
3540 */
3541
3542 if (!cupsGetOption("copies", p->num_options, p->options))
3543 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER, "copies-default",
3544 1);
3545
3546 if (!cupsGetOption("document-format", p->num_options, p->options))
3547 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_MIMETYPE,
3548 "document-format-default", NULL, "application/octet-stream");
3549
3550 if (!cupsGetOption("job-hold-until", p->num_options, p->options))
3551 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
3552 "job-hold-until-default", NULL, "no-hold");
3553
3554 if (!cupsGetOption("job-priority", p->num_options, p->options))
3555 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
3556 "job-priority-default", 50);
3557
3558 if (!cupsGetOption("number-up", p->num_options, p->options))
3559 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
3560 "number-up-default", 1);
3561
3562 if (!cupsGetOption("notify-lease-duration", p->num_options, p->options))
3563 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
3564 "notify-lease-duration-default", DefaultLeaseDuration);
3565
3566 if (!cupsGetOption("notify-events", p->num_options, p->options))
3567 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
3568 "notify-events-default", NULL, "job-completed");
3569
3570 if (!cupsGetOption("orientation-requested", p->num_options, p->options))
3571 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NOVALUE,
3572 "orientation-requested-default", NULL, NULL);
3573
3574 if (!cupsGetOption("print-quality", p->num_options, p->options))
3575 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_ENUM,
3576 "print-quality-default", IPP_QUALITY_NORMAL);
3577 }
3578
3579
3580 /*
3581 * 'add_printer_filter()' - Add a MIME filter for a printer.
3582 */
3583
3584 static void
3585 add_printer_filter(
3586 cupsd_printer_t *p, /* I - Printer to add to */
3587 mime_type_t *filtertype, /* I - Filter or prefilter MIME type */
3588 const char *filter) /* I - Filter to add */
3589 {
3590 char super[MIME_MAX_SUPER], /* Super-type for filter */
3591 type[MIME_MAX_TYPE], /* Type for filter */
3592 program[1024]; /* Program/filter name */
3593 int cost; /* Cost of filter */
3594 mime_type_t *temptype; /* MIME type looping var */
3595 char filename[1024], /* Full filter filename */
3596 *dirsep; /* Pointer to directory separator */
3597 struct stat fileinfo; /* File information */
3598
3599
3600 /*
3601 * Parse the filter string; it should be in the following format:
3602 *
3603 * super/type cost program
3604 */
3605
3606 if (sscanf(filter, "%15[^/]/%31s%d%*[ \t]%1023[^\n]", super, type, &cost,
3607 program) != 4)
3608 {
3609 cupsdLogMessage(CUPSD_LOG_ERROR, "%s: invalid filter string \"%s\"!",
3610 p->name, filter);
3611 return;
3612 }
3613
3614 /*
3615 * See if the filter program exists; if not, stop the printer and flag
3616 * the error!
3617 */
3618
3619 if (strcmp(program, "-"))
3620 {
3621 if (program[0] == '/')
3622 strlcpy(filename, program, sizeof(filename));
3623 else
3624 snprintf(filename, sizeof(filename), "%s/filter/%s", ServerBin, program);
3625
3626 if (stat(filename, &fileinfo))
3627 {
3628 memset(&fileinfo, 0, sizeof(fileinfo));
3629
3630 snprintf(p->state_message, sizeof(p->state_message),
3631 "Filter \"%s\" for printer \"%s\" not available: %s",
3632 filename, p->name, strerror(errno));
3633 cupsdSetPrinterReasons(p, "+cups-missing-filter-warning");
3634
3635 cupsdLogMessage(CUPSD_LOG_ERROR, "%s", p->state_message);
3636 }
3637
3638 /*
3639 * When running as root, do additional security checks...
3640 */
3641
3642 if (!RunUser)
3643 {
3644 /*
3645 * Only use filters that are owned by root and do not have world write
3646 * permissions.
3647 */
3648
3649 if (fileinfo.st_uid || (fileinfo.st_mode & (S_ISUID | S_IWOTH)) != 0)
3650 {
3651 if (fileinfo.st_uid)
3652 snprintf(p->state_message, sizeof(p->state_message),
3653 "Filter \"%s\" for printer \"%s\" not owned by root",
3654 filename, p->name);
3655 else
3656 snprintf(p->state_message, sizeof(p->state_message),
3657 "Filter \"%s\" for printer \"%s\" has insecure permissions "
3658 "(0%o)", filename, p->name, fileinfo.st_mode);
3659
3660 cupsdSetPrinterReasons(p, "+cups-insecure-filter-warning");
3661
3662 cupsdLogMessage(CUPSD_LOG_ERROR, "%s", p->state_message);
3663 }
3664 else if (fileinfo.st_mode)
3665 {
3666 /*
3667 * Similarly, check that the parent directory is also owned by root and
3668 * does not have world write permissions.
3669 */
3670
3671 if ((dirsep = strrchr(filename, '/')) != NULL)
3672 *dirsep = '\0';
3673
3674 if (!stat(filename, &fileinfo) &&
3675 (fileinfo.st_uid ||
3676 (fileinfo.st_mode & (S_ISUID | S_IWOTH)) != 0))
3677 {
3678 if (fileinfo.st_uid)
3679 snprintf(p->state_message, sizeof(p->state_message),
3680 "Filter directory \"%s\" for printer \"%s\" not owned by "
3681 "root", filename, p->name);
3682 else
3683 snprintf(p->state_message, sizeof(p->state_message),
3684 "Filter directory \"%s\" for printer \"%s\" has insecure "
3685 "permissions (0%o)", filename, p->name, fileinfo.st_mode);
3686
3687 cupsdSetPrinterReasons(p, "+cups-insecure-filter-warning");
3688
3689 cupsdLogMessage(CUPSD_LOG_ERROR, "%s", p->state_message);
3690 }
3691 }
3692 }
3693 }
3694
3695 /*
3696 * Add the filter to the MIME database, supporting wildcards as needed...
3697 */
3698
3699 for (temptype = mimeFirstType(MimeDatabase);
3700 temptype;
3701 temptype = mimeNextType(MimeDatabase))
3702 if (((super[0] == '*' && strcasecmp(temptype->super, "printer")) ||
3703 !strcasecmp(temptype->super, super)) &&
3704 (type[0] == '*' || !strcasecmp(temptype->type, type)))
3705 {
3706 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3707 "add_printer_filter: %s: adding filter %s/%s %s/%s %d %s",
3708 p->name, temptype->super, temptype->type,
3709 filtertype->super, filtertype->type,
3710 cost, program);
3711 mimeAddFilter(MimeDatabase, temptype, filtertype, cost, program);
3712 }
3713 }
3714
3715
3716 /*
3717 * 'add_printer_formats()' - Add document-format-supported values for a printer.
3718 */
3719
3720 static void
3721 add_printer_formats(cupsd_printer_t *p) /* I - Printer */
3722 {
3723 int i; /* Looping var */
3724 mime_type_t *type; /* Current MIME type */
3725 cups_array_t *filters; /* Filters */
3726 ipp_attribute_t *attr; /* document-format-supported attribute */
3727 char mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2];
3728 /* MIME type name */
3729
3730
3731 /*
3732 * Raw (and remote) queues advertise all of the supported MIME
3733 * types...
3734 */
3735
3736 cupsArrayDelete(p->filetypes);
3737 p->filetypes = NULL;
3738
3739 if (p->raw)
3740 {
3741 ippAddStrings(p->attrs, IPP_TAG_PRINTER,
3742 (ipp_tag_t)(IPP_TAG_MIMETYPE | IPP_TAG_COPY),
3743 "document-format-supported", NumMimeTypes, NULL, MimeTypes);
3744 return;
3745 }
3746
3747 /*
3748 * Otherwise, loop through the supported MIME types and see if there
3749 * are filters for them...
3750 */
3751
3752 cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_printer_formats: %d types, %d filters",
3753 mimeNumTypes(MimeDatabase), mimeNumFilters(MimeDatabase));
3754
3755 p->filetypes = cupsArrayNew(NULL, NULL);
3756
3757 for (type = mimeFirstType(MimeDatabase);
3758 type;
3759 type = mimeNextType(MimeDatabase))
3760 {
3761 snprintf(mimetype, sizeof(mimetype), "%s/%s", type->super, type->type);
3762
3763 if ((filters = mimeFilter(MimeDatabase, type, p->filetype, NULL)) != NULL)
3764 {
3765 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3766 "add_printer_formats: %s: %s needs %d filters",
3767 p->name, mimetype, cupsArrayCount(filters));
3768
3769 cupsArrayDelete(filters);
3770 cupsArrayAdd(p->filetypes, type);
3771 }
3772 else
3773 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3774 "add_printer_formats: %s: %s not supported",
3775 p->name, mimetype);
3776 }
3777
3778 /*
3779 * Add the file formats that can be filtered...
3780 */
3781
3782 if ((type = mimeType(MimeDatabase, "application", "octet-stream")) == NULL ||
3783 !cupsArrayFind(p->filetypes, type))
3784 i = 1;
3785 else
3786 i = 0;
3787
3788 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3789 "add_printer_formats: %s: %d supported types",
3790 p->name, cupsArrayCount(p->filetypes) + i);
3791
3792 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_MIMETYPE,
3793 "document-format-supported",
3794 cupsArrayCount(p->filetypes) + i, NULL, NULL);
3795
3796 if (i)
3797 attr->values[0].string.text = _cupsStrAlloc("application/octet-stream");
3798
3799 for (type = (mime_type_t *)cupsArrayFirst(p->filetypes);
3800 type;
3801 i ++, type = (mime_type_t *)cupsArrayNext(p->filetypes))
3802 {
3803 snprintf(mimetype, sizeof(mimetype), "%s/%s", type->super, type->type);
3804
3805 attr->values[i].string.text = _cupsStrAlloc(mimetype);
3806 }
3807
3808 #ifdef HAVE_DNSSD
3809 {
3810 char pdl[1024]; /* Buffer to build pdl list */
3811 mime_filter_t *filter; /* MIME filter looping var */
3812
3813
3814 /*
3815 * We only support raw printing if this is not a Tioga PrintJobMgr based
3816 * queue and if application/octet-stream is a known type...
3817 */
3818
3819 for (filter = (mime_filter_t *)cupsArrayFirst(MimeDatabase->filters);
3820 filter;
3821 filter = (mime_filter_t *)cupsArrayNext(MimeDatabase->filters))
3822 {
3823 if (filter->dst == p->filetype && filter->filter &&
3824 strstr(filter->filter, "PrintJobMgr"))
3825 break;
3826 }
3827
3828 pdl[0] = '\0';
3829
3830 if (!filter && mimeType(MimeDatabase, "application", "octet-stream"))
3831 strlcat(pdl, "application/octet-stream,", sizeof(pdl));
3832
3833 /*
3834 * Then list a bunch of formats that are supported by the printer...
3835 */
3836
3837 for (type = (mime_type_t *)cupsArrayFirst(p->filetypes);
3838 type;
3839 type = (mime_type_t *)cupsArrayNext(p->filetypes))
3840 {
3841 if (!strcasecmp(type->super, "application"))
3842 {
3843 if (!strcasecmp(type->type, "pdf"))
3844 strlcat(pdl, "application/pdf,", sizeof(pdl));
3845 else if (!strcasecmp(type->type, "postscript"))
3846 strlcat(pdl, "application/postscript,", sizeof(pdl));
3847 }
3848 else if (!strcasecmp(type->super, "image"))
3849 {
3850 if (!strcasecmp(type->type, "jpeg"))
3851 strlcat(pdl, "image/jpeg,", sizeof(pdl));
3852 else if (!strcasecmp(type->type, "png"))
3853 strlcat(pdl, "image/png,", sizeof(pdl));
3854 }
3855 }
3856
3857 if (pdl[0])
3858 pdl[strlen(pdl) - 1] = '\0'; /* Remove trailing comma */
3859
3860 cupsdSetString(&p->pdl, pdl);
3861 }
3862 #endif /* HAVE_DNSSD */
3863 }
3864
3865
3866 /*
3867 * 'add_string_array()' - Add a string to an array of CUPS strings.
3868 */
3869
3870 static void
3871 add_string_array(cups_array_t **a, /* I - Array */
3872 const char *s) /* I - String */
3873 {
3874 if (!*a)
3875 *a = cupsArrayNew(NULL, NULL);
3876
3877 cupsArrayAdd(*a, _cupsStrAlloc(s));
3878 }
3879
3880
3881 /*
3882 * 'compare_printers()' - Compare two printers.
3883 */
3884
3885 static int /* O - Result of comparison */
3886 compare_printers(void *first, /* I - First printer */
3887 void *second, /* I - Second printer */
3888 void *data) /* I - App data (not used) */
3889 {
3890 return (strcasecmp(((cupsd_printer_t *)first)->name,
3891 ((cupsd_printer_t *)second)->name));
3892 }
3893
3894
3895 /*
3896 * 'delete_printer_filters()' - Delete all MIME filters for a printer.
3897 */
3898
3899 static void
3900 delete_printer_filters(
3901 cupsd_printer_t *p) /* I - Printer to remove from */
3902 {
3903 mime_filter_t *filter; /* MIME filter looping var */
3904
3905
3906 /*
3907 * Range check input...
3908 */
3909
3910 if (p == NULL)
3911 return;
3912
3913 /*
3914 * Remove all filters from the MIME database that have a destination
3915 * type == printer...
3916 */
3917
3918 for (filter = mimeFirstFilter(MimeDatabase);
3919 filter;
3920 filter = mimeNextFilter(MimeDatabase))
3921 if (filter->dst == p->filetype || filter->dst == p->prefiltertype)
3922 {
3923 /*
3924 * Delete the current filter...
3925 */
3926
3927 mimeDeleteFilter(MimeDatabase, filter);
3928 }
3929
3930 cupsdSetPrinterReasons(p, "-cups-insecure-filter-warning"
3931 ",cups-missing-filter-warning");
3932 }
3933
3934
3935 /*
3936 * 'delete_string_array()' - Delete an array of CUPS strings.
3937 */
3938
3939 static void
3940 delete_string_array(cups_array_t **a) /* I - Array */
3941 {
3942 char *ptr; /* Current string */
3943
3944
3945 for (ptr = (char *)cupsArrayFirst(*a);
3946 ptr;
3947 ptr = (char *)cupsArrayNext(*a))
3948 _cupsStrFree(ptr);
3949
3950 cupsArrayDelete(*a);
3951 *a = NULL;
3952 }
3953
3954
3955 /*
3956 * 'dirty_printer()' - Mark config and state files dirty for the specified
3957 * printer.
3958 */
3959
3960 static void
3961 dirty_printer(cupsd_printer_t *p) /* I - Printer */
3962 {
3963 if (p->type & CUPS_PRINTER_DISCOVERED)
3964 cupsdMarkDirty(CUPSD_DIRTY_REMOTE);
3965 else if (p->type & CUPS_PRINTER_CLASS)
3966 cupsdMarkDirty(CUPSD_DIRTY_CLASSES);
3967 else
3968 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
3969
3970 if (PrintcapFormat == PRINTCAP_PLIST)
3971 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP);
3972 }
3973
3974
3975 /*
3976 * 'load_ppd()' - Load a cached PPD file, updating the cache as needed.
3977 */
3978
3979 static void
3980 load_ppd(cupsd_printer_t *p) /* I - Printer */
3981 {
3982 int i, j, k; /* Looping vars */
3983 cups_file_t *cache; /* IPP cache file */
3984 char cache_name[1024]; /* IPP cache filename */
3985 struct stat cache_info; /* IPP cache file info */
3986 char pwg_name[1024]; /* PWG cache filename */
3987 struct stat pwg_info; /* PWG cache file info */
3988 ppd_file_t *ppd; /* PPD file */
3989 char ppd_name[1024]; /* PPD filename */
3990 struct stat ppd_info; /* PPD file info */
3991 int num_media; /* Number of media options */
3992 ppd_size_t *size; /* Current PPD size */
3993 ppd_option_t *duplex, /* Duplex option */
3994 *output_bin, /* OutputBin option */
3995 *output_mode, /* OutputMode option */
3996 *resolution; /* (Set|JCL|)Resolution option */
3997 ppd_choice_t *choice, /* Current PPD choice */
3998 *input_slot, /* Current input slot */
3999 *media_type; /* Current media type */
4000 ppd_attr_t *ppd_attr; /* PPD attribute */
4001 int xdpi, /* Horizontal resolution */
4002 ydpi; /* Vertical resolution */
4003 const char *resptr; /* Pointer into resolution keyword */
4004 _pwg_size_t *pwgsize; /* Current PWG size */
4005 _pwg_map_t *pwgsource, /* Current PWG source */
4006 *pwgtype; /* Current PWG type */
4007 ipp_attribute_t *attr; /* Attribute data */
4008 ipp_value_t *val; /* Attribute value */
4009 int num_finishings, /* Number of finishings */
4010 finishings[5]; /* finishings-supported values */
4011 int num_qualities, /* Number of print-quality values */
4012 qualities[3]; /* print-quality values */
4013 int num_margins, /* Number of media-*-margin-supported values */
4014 margins[16]; /* media-*-margin-supported values */
4015 static const char * const sides[3] = /* sides-supported values */
4016 {
4017 "one-sided",
4018 "two-sided-long-edge",
4019 "two-sided-short-edge"
4020 };
4021 static const char * const standard_commands[] =
4022 { /* Standard CUPS commands */
4023 "AutoConfigure",
4024 "Clean",
4025 "PrintSelfTestPage"
4026 };
4027
4028
4029 /*
4030 * Check to see if the cache is up-to-date...
4031 */
4032
4033 snprintf(cache_name, sizeof(cache_name), "%s/%s.ipp4", CacheDir, p->name);
4034 if (stat(cache_name, &cache_info))
4035 cache_info.st_mtime = 0;
4036
4037 snprintf(pwg_name, sizeof(pwg_name), "%s/%s.pwg3", CacheDir, p->name);
4038 if (stat(pwg_name, &pwg_info))
4039 pwg_info.st_mtime = 0;
4040
4041 snprintf(ppd_name, sizeof(ppd_name), "%s/ppd/%s.ppd", ServerRoot, p->name);
4042 if (stat(ppd_name, &ppd_info))
4043 ppd_info.st_mtime = 1;
4044
4045 ippDelete(p->ppd_attrs);
4046 p->ppd_attrs = ippNew();
4047
4048 _pwgDestroy(p->pwg);
4049 p->pwg = NULL;
4050
4051 if (pwg_info.st_mtime >= ppd_info.st_mtime)
4052 p->pwg = _pwgCreateWithFile(pwg_name);
4053
4054 if (cache_info.st_mtime >= ppd_info.st_mtime && p->pwg &&
4055 (cache = cupsFileOpen(cache_name, "r")) != NULL)
4056 {
4057 /*
4058 * Load cached information and return...
4059 */
4060
4061 cupsdLogMessage(CUPSD_LOG_DEBUG, "load_ppd: Loading %s...", cache_name);
4062
4063 if (ippReadIO(cache, (ipp_iocb_t)cupsFileRead, 1, NULL,
4064 p->ppd_attrs) == IPP_DATA)
4065 {
4066 cupsFileClose(cache);
4067 return;
4068 }
4069
4070 cupsFileClose(cache);
4071 }
4072
4073 /*
4074 * Reload PPD attributes from disk...
4075 */
4076
4077 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
4078
4079 _pwgDestroy(p->pwg);
4080 p->pwg = NULL;
4081
4082 cupsdLogMessage(CUPSD_LOG_DEBUG, "load_ppd: Loading %s...", ppd_name);
4083
4084 delete_string_array(&(p->filters));
4085 delete_string_array(&(p->pre_filters));
4086
4087 p->type &= ~CUPS_PRINTER_OPTIONS;
4088 p->type |= CUPS_PRINTER_BW;
4089
4090 finishings[0] = IPP_FINISHINGS_NONE;
4091 num_finishings = 1;
4092
4093 if ((ppd = ppdOpenFile(ppd_name)) != NULL)
4094 {
4095 /*
4096 * Add make/model and other various attributes...
4097 */
4098
4099 p->pwg = _pwgCreateWithPPD(ppd);
4100
4101 ppdMarkDefaults(ppd);
4102
4103 if (ppd->color_device)
4104 p->type |= CUPS_PRINTER_COLOR;
4105 if (ppd->variable_sizes)
4106 p->type |= CUPS_PRINTER_VARIABLE;
4107 if (!ppd->manual_copies)
4108 p->type |= CUPS_PRINTER_COPIES;
4109 if ((ppd_attr = ppdFindAttr(ppd, "cupsFax", NULL)) != NULL)
4110 if (ppd_attr->value && !strcasecmp(ppd_attr->value, "true"))
4111 p->type |= CUPS_PRINTER_FAX;
4112
4113 ippAddBoolean(p->ppd_attrs, IPP_TAG_PRINTER, "color-supported",
4114 ppd->color_device);
4115 if (ppd->throughput)
4116 {
4117 ippAddInteger(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
4118 "pages-per-minute", ppd->throughput);
4119 if (ppd->color_device)
4120 ippAddInteger(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
4121 "pages-per-minute-color", ppd->throughput);
4122 }
4123
4124 num_qualities = 0;
4125
4126 if ((output_mode = ppdFindOption(ppd, "OutputMode")) != NULL)
4127 {
4128 if (ppdFindChoice(output_mode, "draft") ||
4129 ppdFindChoice(output_mode, "fast"))
4130 qualities[num_qualities ++] = IPP_QUALITY_DRAFT;
4131 if (ppdFindChoice(output_mode, "normal") ||
4132 ppdFindChoice(output_mode, "good"))
4133 qualities[num_qualities ++] = IPP_QUALITY_NORMAL;
4134 if (ppdFindChoice(output_mode, "best") ||
4135 ppdFindChoice(output_mode, "high"))
4136 qualities[num_qualities ++] = IPP_QUALITY_HIGH;
4137 }
4138 else if ((ppd_attr = ppdFindAttr(ppd, "APPrinterPreset", NULL)) != NULL)
4139 {
4140 do
4141 {
4142 if (strstr(ppd_attr->spec, "draft") ||
4143 strstr(ppd_attr->spec, "Draft"))
4144 {
4145 qualities[num_qualities ++] = IPP_QUALITY_DRAFT;
4146 break;
4147 }
4148 }
4149 while ((ppd_attr = ppdFindNextAttr(ppd, "APPrinterPreset",
4150 NULL)) != NULL);
4151
4152 qualities[num_qualities ++] = IPP_QUALITY_NORMAL;
4153 qualities[num_qualities ++] = IPP_QUALITY_HIGH;
4154 }
4155
4156 if (num_qualities == 0)
4157 qualities[num_qualities ++] = IPP_QUALITY_NORMAL;
4158
4159 ippAddIntegers(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_ENUM,
4160 "print-quality-supported", num_qualities, qualities);
4161
4162 if (ppd->nickname)
4163 {
4164 /*
4165 * The NickName can be localized in the character set specified
4166 * by the LanugageEncoding attribute. However, ppdOpen2() has
4167 * already converted the ppd->nickname member to UTF-8 for us
4168 * (the original attribute value is available separately)
4169 */
4170
4171 cupsdSetString(&p->make_model, ppd->nickname);
4172 }
4173 else if (ppd->modelname)
4174 {
4175 /*
4176 * Model name can only contain specific characters...
4177 */
4178
4179 cupsdSetString(&p->make_model, ppd->modelname);
4180 }
4181 else
4182 cupsdSetString(&p->make_model, "Bad PPD File");
4183
4184 ippAddString(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
4185 "printer-make-and-model", NULL, p->make_model);
4186
4187 /*
4188 * Add media options from the PPD file...
4189 */
4190
4191 if (ppd->num_sizes == 0 || !p->pwg)
4192 {
4193 if (!ppdFindAttr(ppd, "APScannerOnly", NULL))
4194 cupsdLogMessage(CUPSD_LOG_CRIT,
4195 "The PPD file for printer %s contains no media "
4196 "options and is therefore invalid!", p->name);
4197
4198 ippAddString(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
4199 "media-default", NULL, "unknown");
4200 ippAddString(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
4201 "media-supported", NULL, "unknown");
4202 }
4203 else
4204 {
4205 /*
4206 * media-default
4207 */
4208
4209 if ((size = ppdPageSize(ppd, NULL)) != NULL)
4210 pwgsize = _pwgGetSize(p->pwg, size->name);
4211 else
4212 pwgsize = NULL;
4213
4214 ippAddString(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
4215 "media-default", NULL,
4216 pwgsize ? pwgsize->map.pwg : "unknown");
4217
4218 /*
4219 * media-col-default
4220 */
4221
4222 if (pwgsize)
4223 {
4224 ipp_t *col; /* Collection value */
4225
4226 input_slot = ppdFindMarkedChoice(ppd, "InputSlot");
4227 media_type = ppdFindMarkedChoice(ppd, "MediaType");
4228 col = new_media_col(pwgsize,
4229 input_slot ?
4230 _pwgGetSource(p->pwg,
4231 input_slot->choice) :
4232 NULL,
4233 media_type ?
4234 _pwgGetType(p->pwg,
4235 media_type->choice) :
4236 NULL);
4237
4238 ippAddCollection(p->ppd_attrs, IPP_TAG_PRINTER, "media-col-default",
4239 col);
4240 ippDelete(col);
4241 }
4242
4243 /*
4244 * media-supported
4245 */
4246
4247 num_media = p->pwg->num_sizes;
4248 if (p->pwg->custom_min_keyword)
4249 num_media += 2;
4250
4251 if ((attr = ippAddStrings(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
4252 "media-supported", num_media, NULL,
4253 NULL)) != NULL)
4254 {
4255 val = attr->values;
4256
4257 for (i = p->pwg->num_sizes, pwgsize = p->pwg->sizes;
4258 i > 0;
4259 i --, pwgsize ++, val ++)
4260 val->string.text = _cupsStrRetain(pwgsize->map.pwg);
4261
4262 if (p->pwg->custom_min_keyword)
4263 {
4264 val->string.text = _cupsStrRetain(p->pwg->custom_min_keyword);
4265 val ++;
4266 val->string.text = _cupsStrRetain(p->pwg->custom_max_keyword);
4267 }
4268 }
4269
4270 /*
4271 * media-source-supported
4272 */
4273
4274 if (p->pwg->num_sources > 0 &&
4275 (attr = ippAddStrings(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
4276 "media-source-supported", p->pwg->num_sources,
4277 NULL, NULL)) != NULL)
4278 {
4279 for (i = p->pwg->num_sources, pwgsource = p->pwg->sources,
4280 val = attr->values;
4281 i > 0;
4282 i --, pwgsource ++, val ++)
4283 val->string.text = _cupsStrRetain(pwgsource->pwg);
4284 }
4285
4286 /*
4287 * media-type-supported
4288 */
4289
4290 if (p->pwg->num_types > 0 &&
4291 (attr = ippAddStrings(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
4292 "media-type-supported", p->pwg->num_types,
4293 NULL, NULL)) != NULL)
4294 {
4295 for (i = p->pwg->num_types, pwgtype = p->pwg->types,
4296 val = attr->values;
4297 i > 0;
4298 i --, pwgtype ++, val ++)
4299 val->string.text = _cupsStrRetain(pwgtype->pwg);
4300 }
4301
4302 /*
4303 * media-*-margin-supported
4304 */
4305
4306 for (i = p->pwg->num_sizes, pwgsize = p->pwg->sizes, num_margins = 0;
4307 i > 0 && num_margins < (int)(sizeof(margins) / sizeof(margins[0]));
4308 i --, pwgsize ++)
4309 {
4310 for (j = 0; j < num_margins; j ++)
4311 if (pwgsize->bottom == margins[j])
4312 break;
4313
4314 if (j >= num_margins)
4315 {
4316 margins[num_margins] = pwgsize->bottom;
4317 num_margins ++;
4318 }
4319 }
4320
4321 if (num_margins > 0)
4322 ippAddIntegers(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
4323 "media-bottom-margin-supported", num_margins, margins);
4324 else
4325 ippAddInteger(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
4326 "media-bottom-margin-supported", 0);
4327
4328 for (i = p->pwg->num_sizes, pwgsize = p->pwg->sizes, num_margins = 0;
4329 i > 0 && num_margins < (int)(sizeof(margins) / sizeof(margins[0]));
4330 i --, pwgsize ++)
4331 {
4332 for (j = 0; j < num_margins; j ++)
4333 if (pwgsize->left == margins[j])
4334 break;
4335
4336 if (j >= num_margins)
4337 {
4338 margins[num_margins] = pwgsize->left;
4339 num_margins ++;
4340 }
4341 }
4342
4343 if (num_margins > 0)
4344 ippAddIntegers(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
4345 "media-left-margin-supported", num_margins, margins);
4346 else
4347 ippAddInteger(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
4348 "media-left-margin-supported", 0);
4349
4350 for (i = p->pwg->num_sizes, pwgsize = p->pwg->sizes, num_margins = 0;
4351 i > 0 && num_margins < (int)(sizeof(margins) / sizeof(margins[0]));
4352 i --, pwgsize ++)
4353 {
4354 for (j = 0; j < num_margins; j ++)
4355 if (pwgsize->right == margins[j])
4356 break;
4357
4358 if (j >= num_margins)
4359 {
4360 margins[num_margins] = pwgsize->right;
4361 num_margins ++;
4362 }
4363 }
4364
4365 if (num_margins > 0)
4366 ippAddIntegers(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
4367 "media-right-margin-supported", num_margins, margins);
4368 else
4369 ippAddInteger(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
4370 "media-right-margin-supported", 0);
4371
4372 for (i = p->pwg->num_sizes, pwgsize = p->pwg->sizes, num_margins = 0;
4373 i > 0 && num_margins < (int)(sizeof(margins) / sizeof(margins[0]));
4374 i --, pwgsize ++)
4375 {
4376 for (j = 0; j < num_margins; j ++)
4377 if (pwgsize->top == margins[j])
4378 break;
4379
4380 if (j >= num_margins)
4381 {
4382 margins[num_margins] = pwgsize->top;
4383 num_margins ++;
4384 }
4385 }
4386
4387 if (num_margins > 0)
4388 ippAddIntegers(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
4389 "media-top-margin-supported", num_margins, margins);
4390 else
4391 ippAddInteger(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
4392 "media-top-margin-supported", 0);
4393
4394 /*
4395 * media-col-database
4396 */
4397
4398 num_media = p->pwg->num_sizes;
4399 if (p->pwg->num_sources)
4400 {
4401 if (p->pwg->num_types > 0)
4402 num_media += p->pwg->num_sizes * p->pwg->num_sources *
4403 p->pwg->num_types;
4404 else
4405 num_media += p->pwg->num_sizes * p->pwg->num_sources;
4406 }
4407 else if (p->pwg->num_types)
4408 num_media += p->pwg->num_sizes * p->pwg->num_types;
4409
4410 if ((attr = ippAddCollections(p->ppd_attrs, IPP_TAG_PRINTER,
4411 "media-col-database", num_media,
4412 NULL)) != NULL)
4413 {
4414 for (i = p->pwg->num_sizes, pwgsize = p->pwg->sizes, val = attr->values;
4415 i > 0;
4416 i --, pwgsize ++)
4417 {
4418 /*
4419 * Start by adding the page size without source or type...
4420 */
4421
4422 ppdMarkOption(ppd, "PageSize", pwgsize->map.ppd);
4423
4424 val->collection = new_media_col(pwgsize, NULL, NULL);
4425 val ++;
4426
4427 /*
4428 * Then add the specific, supported combinations of size, source, and
4429 * type...
4430 */
4431
4432 if (p->pwg->num_sources > 0)
4433 {
4434 for (j = p->pwg->num_sources, pwgsource = p->pwg->sources;
4435 j > 0;
4436 j --, pwgsource ++)
4437 {
4438 ppdMarkOption(ppd, "InputSlot", pwgsource->ppd);
4439
4440 if (p->pwg->num_types > 0)
4441 {
4442 for (k = p->pwg->num_types, pwgtype = p->pwg->types;
4443 k > 0;
4444 k --, pwgtype ++)
4445 {
4446 if (!ppdMarkOption(ppd, "MediaType", pwgtype->ppd))
4447 {
4448 val->collection = new_media_col(pwgsize, pwgsource->pwg,
4449 pwgtype->pwg);
4450 val ++;
4451 }
4452 }
4453 }
4454 else if (!ppdConflicts(ppd))
4455 {
4456 val->collection = new_media_col(pwgsize, pwgsource->pwg, NULL);
4457 val ++;
4458 }
4459 }
4460 }
4461 else if (p->pwg->num_types > 0)
4462 {
4463 for (j = p->pwg->num_types, pwgtype = p->pwg->types;
4464 j > 0;
4465 j --, pwgtype ++)
4466 {
4467 if (!ppdMarkOption(ppd, "MediaType", pwgtype->ppd))
4468 {
4469 val->collection = new_media_col(pwgsize, NULL, pwgtype->pwg);
4470 val ++;
4471 }
4472 }
4473 }
4474 }
4475
4476 /*
4477 * Update the number of media-col-database values...
4478 */
4479
4480 attr->num_values = val - attr->values;
4481 }
4482 }
4483
4484 /*
4485 * Output bin...
4486 */
4487
4488 if (p->pwg && p->pwg->num_bins > 0)
4489 {
4490 attr = ippAddStrings(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
4491 "output-bin-supported", p->pwg->num_bins,
4492 NULL, NULL);
4493
4494 if (attr != NULL)
4495 {
4496 for (i = 0, val = attr->values;
4497 i < p->pwg->num_bins;
4498 i ++, val ++)
4499 val->string.text = _cupsStrAlloc(p->pwg->bins[i].pwg);
4500 }
4501
4502 if ((output_bin = ppdFindOption(ppd, "OutputBin")) != NULL)
4503 {
4504 for (i = 0; i < p->pwg->num_bins; i ++)
4505 if (!strcmp(p->pwg->bins[i].ppd, output_bin->defchoice))
4506 break;
4507
4508 if (i >= p->pwg->num_bins)
4509 i = 0;
4510
4511 ippAddString(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
4512 "output-bin-default", NULL, p->pwg->bins[i].pwg);
4513 }
4514 else
4515 ippAddString(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
4516 "output-bin-default", NULL, p->pwg->bins[0].pwg);
4517 }
4518 else if (((ppd_attr = ppdFindAttr(ppd, "DefaultOutputOrder",
4519 NULL)) != NULL &&
4520 !strcasecmp(ppd_attr->value, "Reverse")) ||
4521 (!ppd_attr && ppd->manufacturer && /* "Compatibility heuristic" */
4522 (!strcasecmp(ppd->manufacturer, "epson") ||
4523 !strcasecmp(ppd->manufacturer, "lexmark"))))
4524 {
4525 /*
4526 * Report that this printer has a single output bin that leaves pages face
4527 * up.
4528 */
4529
4530 ippAddString(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
4531 "output-bin-supported", NULL, "face-up");
4532 ippAddString(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
4533 "output-bin-default", NULL, "face-up");
4534 }
4535 else
4536 {
4537 ippAddString(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
4538 "output-bin-supported", NULL, "face-down");
4539 ippAddString(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
4540 "output-bin-default", NULL, "face-down");
4541 }
4542
4543 /*
4544 * output-mode...
4545 */
4546
4547 if (ppd->color_device)
4548 {
4549 static const char * const output_modes[] =
4550 {
4551 "monochrome",
4552 "color"
4553 };
4554
4555 ippAddStrings(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
4556 "output-mode-supported", 2, NULL, output_modes);
4557 ippAddString(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
4558 "output-mode-default", NULL, "color");
4559 }
4560 else
4561 {
4562 ippAddString(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
4563 "output-mode-supported", NULL, "monochrome");
4564 ippAddString(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
4565 "output-mode-default", NULL, "monochrome");
4566 }
4567
4568 /*
4569 * Printer resolutions...
4570 */
4571
4572 if ((resolution = ppdFindOption(ppd, "Resolution")) == NULL)
4573 if ((resolution = ppdFindOption(ppd, "JCLResolution")) == NULL)
4574 if ((resolution = ppdFindOption(ppd, "SetResolution")) == NULL)
4575 resolution = ppdFindOption(ppd, "CNRes_PGP");
4576
4577 if (resolution)
4578 {
4579 /*
4580 * Report all supported resolutions...
4581 */
4582
4583 attr = ippAddResolutions(p->ppd_attrs, IPP_TAG_PRINTER,
4584 "printer-resolution-supported",
4585 resolution->num_choices, IPP_RES_PER_INCH,
4586 NULL, NULL);
4587
4588 for (i = 0, choice = resolution->choices;
4589 i < resolution->num_choices;
4590 i ++, choice ++)
4591 {
4592 xdpi = ydpi = (int)strtol(choice->choice, (char **)&resptr, 10);
4593 if (resptr > choice->choice && xdpi > 0 && *resptr == 'x')
4594 ydpi = (int)strtol(resptr + 1, (char **)&resptr, 10);
4595
4596 if (xdpi <= 0 || ydpi <= 0)
4597 {
4598 cupsdLogMessage(CUPSD_LOG_WARN,
4599 "Bad resolution \"%s\" for printer %s.",
4600 choice->choice, p->name);
4601 xdpi = ydpi = 300;
4602 }
4603
4604 attr->values[i].resolution.xres = xdpi;
4605 attr->values[i].resolution.yres = ydpi;
4606 attr->values[i].resolution.units = IPP_RES_PER_INCH;
4607
4608 if (choice->marked)
4609 ippAddResolution(p->ppd_attrs, IPP_TAG_PRINTER,
4610 "printer-resolution-default", IPP_RES_PER_INCH,
4611 xdpi, ydpi);
4612 }
4613 }
4614 else if ((ppd_attr = ppdFindAttr(ppd, "DefaultResolution", NULL)) != NULL &&
4615 ppd_attr->value)
4616 {
4617 /*
4618 * Just the DefaultResolution to report...
4619 */
4620
4621 xdpi = ydpi = (int)strtol(ppd_attr->value, (char **)&resptr, 10);
4622 if (resptr > ppd_attr->value && xdpi > 0)
4623 {
4624 if (*resptr == 'x')
4625 ydpi = (int)strtol(resptr + 1, (char **)&resptr, 10);
4626 else
4627 ydpi = xdpi;
4628 }
4629
4630 if (xdpi <= 0 || ydpi <= 0)
4631 {
4632 cupsdLogMessage(CUPSD_LOG_WARN,
4633 "Bad default resolution \"%s\" for printer %s.",
4634 ppd_attr->value, p->name);
4635 xdpi = ydpi = 300;
4636 }
4637
4638 ippAddResolution(p->ppd_attrs, IPP_TAG_PRINTER,
4639 "printer-resolution-default", IPP_RES_PER_INCH,
4640 xdpi, ydpi);
4641 ippAddResolution(p->ppd_attrs, IPP_TAG_PRINTER,
4642 "printer-resolution-supported", IPP_RES_PER_INCH,
4643 xdpi, ydpi);
4644 }
4645 else
4646 {
4647 /*
4648 * No resolutions in PPD - make one up...
4649 */
4650
4651 ippAddResolution(p->ppd_attrs, IPP_TAG_PRINTER,
4652 "printer-resolution-default", IPP_RES_PER_INCH,
4653 300, 300);
4654 ippAddResolution(p->ppd_attrs, IPP_TAG_PRINTER,
4655 "printer-resolution-supported", IPP_RES_PER_INCH,
4656 300, 300);
4657 }
4658
4659 /*
4660 * Duplexing, etc...
4661 */
4662
4663 ppdMarkDefaults(ppd);
4664
4665 if ((duplex = ppdFindOption(ppd, "Duplex")) == NULL)
4666 if ((duplex = ppdFindOption(ppd, "EFDuplex")) == NULL)
4667 if ((duplex = ppdFindOption(ppd, "EFDuplexing")) == NULL)
4668 if ((duplex = ppdFindOption(ppd, "KD03Duplex")) == NULL)
4669 duplex = ppdFindOption(ppd, "JCLDuplex");
4670
4671 if (duplex && duplex->num_choices > 1 &&
4672 !ppdInstallableConflict(ppd, duplex->keyword, "DuplexTumble"))
4673 {
4674 p->type |= CUPS_PRINTER_DUPLEX;
4675
4676 ippAddStrings(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
4677 "sides-supported", 3, NULL, sides);
4678
4679 if (!strcasecmp(duplex->defchoice, "DuplexTumble"))
4680 ippAddString(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
4681 "sides-default", NULL, "two-sided-short-edge");
4682 else if (!strcasecmp(duplex->defchoice, "DuplexNoTumble"))
4683 ippAddString(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
4684 "sides-default", NULL, "two-sided-long-edge");
4685 else
4686 ippAddString(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
4687 "sides-default", NULL, "one-sided");
4688 }
4689 else
4690 {
4691 ippAddString(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
4692 "sides-supported", NULL, "one-sided");
4693 ippAddString(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
4694 "sides-default", NULL, "one-sided");
4695 }
4696
4697 if (ppdFindOption(ppd, "Collate") != NULL)
4698 p->type |= CUPS_PRINTER_COLLATE;
4699
4700 if (ppdFindOption(ppd, "StapleLocation") != NULL)
4701 {
4702 p->type |= CUPS_PRINTER_STAPLE;
4703 finishings[num_finishings++] = IPP_FINISHINGS_STAPLE;
4704 }
4705
4706 if (ppdFindOption(ppd, "BindEdge") != NULL)
4707 {
4708 p->type |= CUPS_PRINTER_BIND;
4709 finishings[num_finishings++] = IPP_FINISHINGS_BIND;
4710 }
4711
4712 for (i = 0; i < ppd->num_sizes; i ++)
4713 if (ppd->sizes[i].length > 1728)
4714 p->type |= CUPS_PRINTER_LARGE;
4715 else if (ppd->sizes[i].length > 1008)
4716 p->type |= CUPS_PRINTER_MEDIUM;
4717 else
4718 p->type |= CUPS_PRINTER_SMALL;
4719
4720 if ((ppd_attr = ppdFindAttr(ppd, "APICADriver", NULL)) != NULL &&
4721 ppd_attr->value && !strcasecmp(ppd_attr->value, "true"))
4722 {
4723 if ((ppd_attr = ppdFindAttr(ppd, "APScannerOnly", NULL)) != NULL &&
4724 ppd_attr->value && !strcasecmp(ppd_attr->value, "true"))
4725 p->type |= CUPS_PRINTER_SCANNER;
4726 else
4727 p->type |= CUPS_PRINTER_MFP;
4728 }
4729
4730 /*
4731 * Add a filter from application/vnd.cups-raw to printer/name to
4732 * handle "raw" printing by users.
4733 */
4734
4735 add_string_array(&(p->filters), "application/vnd.cups-raw 0 -");
4736
4737 /*
4738 * Add any pre-filters in the PPD file...
4739 */
4740
4741 if ((ppd_attr = ppdFindAttr(ppd, "cupsPreFilter", NULL)) != NULL)
4742 {
4743 for (; ppd_attr; ppd_attr = ppdFindNextAttr(ppd, "cupsPreFilter", NULL))
4744 if (ppd_attr->value)
4745 add_string_array(&(p->pre_filters), ppd_attr->value);
4746 }
4747
4748 /*
4749 * Add any filters in the PPD file...
4750 */
4751
4752 DEBUG_printf(("ppd->num_filters = %d\n", ppd->num_filters));
4753 for (i = 0; i < ppd->num_filters; i ++)
4754 {
4755 DEBUG_printf(("ppd->filters[%d] = \"%s\"\n", i, ppd->filters[i]));
4756 add_string_array(&(p->filters), ppd->filters[i]);
4757
4758 if (!strncasecmp(ppd->filters[i], "application/vnd.cups-command", 28) &&
4759 isspace(ppd->filters[i][28] & 255))
4760 p->type |= CUPS_PRINTER_COMMANDS;
4761 }
4762
4763 if ((ppd_attr = ppdFindAttr(ppd, "cupsCommands", NULL)) != NULL &&
4764 ppd_attr->value &&
4765 (!ppd_attr->value[0] || !strcasecmp(ppd_attr->value, "none")))
4766 {
4767 /*
4768 * Printer does not support CUPS command files (or any commands as far as
4769 * CUPS is concerned...
4770 */
4771
4772 p->type &= ~CUPS_PRINTER_COMMANDS;
4773 }
4774 else if (ppd->num_filters == 0)
4775 {
4776 /*
4777 * If there are no filters, add PostScript printing filters.
4778 */
4779
4780 add_string_array(&(p->filters),
4781 "application/vnd.cups-command 0 commandtops");
4782 add_string_array(&(p->filters),
4783 "application/vnd.cups-postscript 0 -");
4784
4785 p->type |= CUPS_PRINTER_COMMANDS;
4786 }
4787 else if (!(p->type & CUPS_PRINTER_COMMANDS))
4788 {
4789 /*
4790 * See if this is a PostScript device without a command filter...
4791 */
4792
4793 for (i = 0; i < ppd->num_filters; i ++)
4794 if (!strncasecmp(ppd->filters[i],
4795 "application/vnd.cups-postscript", 31) &&
4796 isspace(ppd->filters[i][31] & 255))
4797 break;
4798
4799 if (i < ppd->num_filters)
4800 {
4801 /*
4802 * Add the generic PostScript command filter...
4803 */
4804
4805 add_string_array(&(p->filters),
4806 "application/vnd.cups-command 0 commandtops");
4807 p->type |= CUPS_PRINTER_COMMANDS;
4808 }
4809 }
4810
4811 if (p->type & CUPS_PRINTER_COMMANDS)
4812 {
4813 char *commands, /* Copy of commands */
4814 *start, /* Start of name */
4815 *end; /* End of name */
4816 int count; /* Number of commands */
4817
4818
4819 if (ppd_attr && ppd_attr->value && ppd_attr->value[0])
4820 {
4821 for (count = 0, start = ppd_attr->value; *start; count ++)
4822 {
4823 while (isspace(*start & 255))
4824 start ++;
4825
4826 if (!*start)
4827 break;
4828
4829 while (*start && !isspace(*start & 255))
4830 start ++;
4831 }
4832 }
4833 else
4834 count = 0;
4835
4836 if (count > 0)
4837 {
4838 /*
4839 * Make a copy of the commands string and count how many commands there
4840 * are...
4841 */
4842
4843 attr = ippAddStrings(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
4844 "printer-commands", count, NULL, NULL);
4845
4846 commands = strdup(ppd_attr->value);
4847
4848 for (count = 0, start = commands; *start; count ++)
4849 {
4850 while (isspace(*start & 255))
4851 start ++;
4852
4853 if (!*start)
4854 break;
4855
4856 end = start;
4857 while (*end && !isspace(*end & 255))
4858 end ++;
4859
4860 if (*end)
4861 *end++ = '\0';
4862
4863 attr->values[count].string.text = _cupsStrAlloc(start);
4864
4865 start = end;
4866 }
4867
4868 free(commands);
4869 }
4870 else
4871 {
4872 /*
4873 * Add the standard list of commands...
4874 */
4875
4876 ippAddStrings(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
4877 "printer-commands",
4878 (int)(sizeof(standard_commands) /
4879 sizeof(standard_commands[0])), NULL,
4880 standard_commands);
4881 }
4882 }
4883 else
4884 {
4885 /*
4886 * No commands supported...
4887 */
4888
4889 ippAddString(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
4890 "printer-commands", NULL, "none");
4891 }
4892
4893 /*
4894 * Show current and available port monitors for this printer...
4895 */
4896
4897 ippAddString(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_NAME, "port-monitor",
4898 NULL, p->port_monitor ? p->port_monitor : "none");
4899
4900 for (i = 1, ppd_attr = ppdFindAttr(ppd, "cupsPortMonitor", NULL);
4901 ppd_attr;
4902 i ++, ppd_attr = ppdFindNextAttr(ppd, "cupsPortMonitor", NULL));
4903
4904 if (ppd->protocols)
4905 {
4906 if (strstr(ppd->protocols, "TBCP"))
4907 i ++;
4908 else if (strstr(ppd->protocols, "BCP"))
4909 i ++;
4910 }
4911
4912 attr = ippAddStrings(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
4913 "port-monitor-supported", i, NULL, NULL);
4914
4915 attr->values[0].string.text = _cupsStrAlloc("none");
4916
4917 for (i = 1, ppd_attr = ppdFindAttr(ppd, "cupsPortMonitor", NULL);
4918 ppd_attr;
4919 i ++, ppd_attr = ppdFindNextAttr(ppd, "cupsPortMonitor", NULL))
4920 attr->values[i].string.text = _cupsStrAlloc(ppd_attr->value);
4921
4922 if (ppd->protocols)
4923 {
4924 if (strstr(ppd->protocols, "TBCP"))
4925 attr->values[i].string.text = _cupsStrAlloc("tbcp");
4926 else if (strstr(ppd->protocols, "BCP"))
4927 attr->values[i].string.text = _cupsStrAlloc("bcp");
4928 }
4929
4930 #ifdef HAVE_DNSSD
4931 cupsdSetString(&p->product, ppd->product);
4932 #endif /* HAVE_DNSSD */
4933
4934 if (ppdFindAttr(ppd, "APRemoteQueueID", NULL))
4935 p->type |= CUPS_PRINTER_REMOTE;
4936
4937 #ifdef HAVE_APPLICATIONSERVICES_H
4938 /*
4939 * Convert the file referenced in APPrinterIconPath to a 128x128 PNG
4940 * and save it as cacheDir/printername.png
4941 */
4942
4943 if ((ppd_attr = ppdFindAttr(ppd, "APPrinterIconPath", NULL)) != NULL &&
4944 ppd_attr->value)
4945 {
4946 CGImageRef imageRef = NULL;/* Current icon image */
4947 CGImageRef biggestIconRef = NULL;
4948 /* Biggest icon image */
4949 CGImageRef closestTo128IconRef = NULL;
4950 /* Icon image closest to and >= 128 */
4951 CGImageSourceRef sourceRef; /* The file's image source */
4952 char outPath[HTTP_MAX_URI];
4953 /* The path to the PNG file */
4954 CFURLRef outUrl; /* The URL made from the outPath */
4955 CFURLRef icnsFileUrl; /* The URL of the original ICNS icon file */
4956 CGImageDestinationRef destRef; /* The image destination to write */
4957 size_t bytesPerRow; /* The bytes per row used for resizing */
4958 CGContextRef context; /* The CG context used for resizing */
4959
4960 snprintf(outPath, sizeof(outPath), "%s/%s.png", CacheDir, p->name);
4961 outUrl = CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault,
4962 (UInt8 *)outPath,
4963 strlen(outPath),
4964 FALSE);
4965 icnsFileUrl = CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault,
4966 (UInt8 *)ppd_attr->value,
4967 strlen(ppd_attr->value),
4968 FALSE);
4969 if (outUrl && icnsFileUrl)
4970 {
4971 sourceRef = CGImageSourceCreateWithURL(icnsFileUrl, NULL);
4972 if (sourceRef)
4973 {
4974 for (i = 0; i < CGImageSourceGetCount(sourceRef); i ++)
4975 {
4976 imageRef = CGImageSourceCreateImageAtIndex(sourceRef, i, NULL);
4977 if (imageRef &&
4978 CGImageGetWidth(imageRef) == CGImageGetHeight(imageRef))
4979 {
4980 /*
4981 * Loop through remembering the icon closest to 128 but >= 128
4982 * and then remember the largest icon.
4983 */
4984
4985 if (CGImageGetWidth(imageRef) >= 128 &&
4986 (!closestTo128IconRef ||
4987 CGImageGetWidth(imageRef) <
4988 CGImageGetWidth(closestTo128IconRef)))
4989 {
4990 CGImageRelease(closestTo128IconRef);
4991 CGImageRetain(imageRef);
4992 closestTo128IconRef = imageRef;
4993 }
4994
4995 if (!biggestIconRef ||
4996 CGImageGetWidth(imageRef) > CGImageGetWidth(biggestIconRef))
4997 {
4998 CGImageRelease(biggestIconRef);
4999 CGImageRetain(imageRef);
5000 biggestIconRef = imageRef;
5001 }
5002
5003 CGImageRelease(imageRef);
5004 }
5005 }
5006
5007 if (biggestIconRef)
5008 {
5009 /*
5010 * If biggestIconRef is NULL, we found no icons. Otherwise we first
5011 * want the closest to 128, but if none are larger than 128, we want
5012 * the largest icon available.
5013 */
5014
5015 imageRef = closestTo128IconRef ? closestTo128IconRef :
5016 biggestIconRef;
5017 CGImageRetain(imageRef);
5018 CGImageRelease(biggestIconRef);
5019 CGImageRelease(closestTo128IconRef);
5020 destRef = CGImageDestinationCreateWithURL(outUrl, kUTTypePNG, 1,
5021 NULL);
5022 if (destRef)
5023 {
5024 if (CGImageGetWidth(imageRef) != 128)
5025 {
5026 bytesPerRow = CGImageGetBytesPerRow(imageRef) /
5027 CGImageGetWidth(imageRef) * 128;
5028 context = CGBitmapContextCreate(NULL, 128, 128,
5029 CGImageGetBitsPerComponent(imageRef),
5030 bytesPerRow,
5031 CGImageGetColorSpace(imageRef),
5032 kCGImageAlphaPremultipliedFirst);
5033 if (context)
5034 {
5035 CGContextDrawImage(context, CGRectMake(0, 0, 128, 128),
5036 imageRef);
5037 CGImageRelease(imageRef);
5038 imageRef = CGBitmapContextCreateImage(context);
5039 CGContextRelease(context);
5040 }
5041 }
5042
5043 CGImageDestinationAddImage(destRef, imageRef, NULL);
5044 CGImageDestinationFinalize(destRef);
5045 CFRelease(destRef);
5046 }
5047
5048 CGImageRelease(imageRef);
5049 }
5050
5051 CFRelease(sourceRef);
5052 CFRelease(icnsFileUrl);
5053 }
5054
5055 CFRelease(outUrl);
5056 }
5057 }
5058 #endif /* HAVE_APPLICATIONSERVICES_H */
5059
5060 /*
5061 * Close the PPD and set the type...
5062 */
5063
5064 ppdClose(ppd);
5065 }
5066 else if (!access(ppd_name, 0))
5067 {
5068 int pline; /* PPD line number */
5069 ppd_status_t pstatus; /* PPD load status */
5070
5071
5072 pstatus = ppdLastError(&pline);
5073
5074 cupsdLogMessage(CUPSD_LOG_ERROR, "PPD file for %s cannot be loaded!",
5075 p->name);
5076
5077 if (pstatus <= PPD_ALLOC_ERROR)
5078 cupsdLogMessage(CUPSD_LOG_ERROR, "%s", strerror(errno));
5079 else
5080 cupsdLogMessage(CUPSD_LOG_ERROR, "%s on line %d.",
5081 ppdErrorString(pstatus), pline);
5082
5083 cupsdLogMessage(CUPSD_LOG_INFO,
5084 "Hint: Run \"cupstestppd %s\" and fix any errors.",
5085 ppd_name);
5086
5087 /*
5088 * Add a filter from application/vnd.cups-raw to printer/name to
5089 * handle "raw" printing by users.
5090 */
5091
5092 add_string_array(&(p->filters), "application/vnd.cups-raw 0 -");
5093
5094 /*
5095 * Add a PostScript filter, since this is still possibly PS printer.
5096 */
5097
5098 add_string_array(&(p->filters), "application/vnd.cups-postscript 0 -");
5099 }
5100 else
5101 {
5102 /*
5103 * If we have an interface script, add a filter entry for it...
5104 */
5105
5106 char interface[1024]; /* Interface script */
5107
5108
5109 snprintf(interface, sizeof(interface), "%s/interfaces/%s", ServerRoot,
5110 p->name);
5111 if (!access(interface, X_OK))
5112 {
5113 /*
5114 * Yes, we have a System V style interface script; use it!
5115 */
5116
5117 ippAddString(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
5118 "printer-make-and-model", NULL,
5119 "Local System V Printer");
5120
5121 snprintf(interface, sizeof(interface), "*/* 0 %s/interfaces/%s",
5122 ServerRoot, p->name);
5123 add_string_array(&(p->filters), interface);
5124 }
5125 else if (!strncmp(p->device_uri, "ipp://", 6) &&
5126 (strstr(p->device_uri, "/printers/") != NULL ||
5127 strstr(p->device_uri, "/classes/") != NULL ||
5128 (strstr(p->device_uri, "._ipp.") != NULL &&
5129 !strcmp(p->device_uri + strlen(p->device_uri) - 5,
5130 "/cups"))))
5131 {
5132 /*
5133 * Tell the client this is really a hard-wired remote printer.
5134 */
5135
5136 p->type |= CUPS_PRINTER_REMOTE;
5137
5138 /*
5139 * Point the printer-uri-supported attribute to the
5140 * remote printer...
5141 */
5142
5143 if (strchr(p->device_uri, '?'))
5144 {
5145 /*
5146 * Strip trailing "?options" from URI...
5147 */
5148
5149 char resource[HTTP_MAX_URI], /* New URI */
5150 *ptr; /* Pointer into URI */
5151
5152 strlcpy(resource, p->device_uri, sizeof(resource));
5153 if ((ptr = strchr(resource, '?')) != NULL)
5154 *ptr = '\0';
5155
5156 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI,
5157 "printer-uri-supported", NULL, resource);
5158 }
5159 else
5160 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI,
5161 "printer-uri-supported", NULL, p->device_uri);
5162
5163 /*
5164 * Then set the make-and-model accordingly...
5165 */
5166
5167 ippAddString(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
5168 "printer-make-and-model", NULL, "Remote Printer");
5169
5170 /*
5171 * Print all files directly...
5172 */
5173
5174 p->raw = 1;
5175 p->remote = 1;
5176 }
5177 else
5178 {
5179 /*
5180 * Otherwise we have neither - treat this as a "dumb" printer
5181 * with no PPD file...
5182 */
5183
5184 ippAddString(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
5185 "printer-make-and-model", NULL, "Local Raw Printer");
5186
5187 p->raw = 1;
5188 }
5189 }
5190
5191 ippAddIntegers(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_ENUM,
5192 "finishings-supported", num_finishings, finishings);
5193 ippAddInteger(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_ENUM,
5194 "finishings-default", IPP_FINISHINGS_NONE);
5195
5196 if (ppd && (cache = cupsFileOpen(cache_name, "w9")) != NULL)
5197 {
5198 /*
5199 * Save cached PPD attributes to disk...
5200 */
5201
5202 cupsdLogMessage(CUPSD_LOG_DEBUG, "load_ppd: Saving %s...", cache_name);
5203
5204 p->ppd_attrs->state = IPP_IDLE;
5205
5206 if (ippWriteIO(cache, (ipp_iocb_t)cupsFileWrite, 1, NULL,
5207 p->ppd_attrs) != IPP_DATA)
5208 {
5209 cupsdLogMessage(CUPSD_LOG_ERROR,
5210 "Unable to save PPD cache file \"%s\" - %s", cache_name,
5211 strerror(errno));
5212 unlink(cache_name);
5213 }
5214
5215 cupsFileClose(cache);
5216
5217 _pwgWriteFile(p->pwg, pwg_name);
5218 }
5219 else
5220 {
5221 /*
5222 * Remove cache files...
5223 */
5224
5225 if (cache_info.st_mtime)
5226 unlink(cache_name);
5227
5228 if (pwg_info.st_mtime)
5229 unlink(pwg_name);
5230 }
5231 }
5232
5233
5234 /*
5235 * 'new_media_col()' - Create a media-col collection value.
5236 */
5237
5238 static ipp_t * /* O - Collection value */
5239 new_media_col(_pwg_size_t *size, /* I - media-size/margin values */
5240 const char *source, /* I - media-source value */
5241 const char *type) /* I - media-type value */
5242 {
5243 ipp_t *media_col, /* Collection value */
5244 *media_size; /* media-size value */
5245
5246
5247 media_col = ippNew();
5248
5249 media_size = ippNew();
5250 ippAddInteger(media_size, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
5251 "x-dimension", size->width);
5252 ippAddInteger(media_size, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
5253 "y-dimension", size->length);
5254 ippAddCollection(media_col, IPP_TAG_PRINTER, "media-size", media_size);
5255 ippDelete(media_size);
5256
5257 ippAddInteger(media_col, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
5258 "media-bottom-margin", size->bottom);
5259 ippAddInteger(media_col, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
5260 "media-left-margin", size->left);
5261 ippAddInteger(media_col, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
5262 "media-right-margin", size->right);
5263 ippAddInteger(media_col, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
5264 "media-top-margin", size->top);
5265
5266 if (source)
5267 ippAddString(media_col, IPP_TAG_PRINTER, IPP_TAG_KEYWORD, "media-source",
5268 NULL, source);
5269
5270 if (type)
5271 ippAddString(media_col, IPP_TAG_PRINTER, IPP_TAG_KEYWORD, "media-type",
5272 NULL, type);
5273
5274 return (media_col);
5275 }
5276
5277
5278 #ifdef __sgi
5279 /*
5280 * 'write_irix_config()' - Update the config files used by the IRIX
5281 * desktop tools.
5282 */
5283
5284 static void
5285 write_irix_config(cupsd_printer_t *p) /* I - Printer to update */
5286 {
5287 char filename[1024]; /* Interface script filename */
5288 cups_file_t *fp; /* Interface script file */
5289 ipp_attribute_t *attr; /* Attribute data */
5290
5291
5292 /*
5293 * Add dummy interface and GUI scripts to fool SGI's "challenged" printing
5294 * tools. First the interface script that tells the tools what kind of
5295 * printer we have...
5296 */
5297
5298 snprintf(filename, sizeof(filename), "/var/spool/lp/interface/%s", p->name);
5299
5300 if (p->type & CUPS_PRINTER_CLASS)
5301 unlink(filename);
5302 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
5303 {
5304 cupsFilePuts(fp, "#!/bin/sh\n");
5305
5306 if ((attr = ippFindAttribute(p->attrs, "printer-make-and-model",
5307 IPP_TAG_TEXT)) != NULL)
5308 cupsFilePrintf(fp, "NAME=\"%s\"\n", attr->values[0].string.text);
5309 else if (p->type & CUPS_PRINTER_CLASS)
5310 cupsFilePuts(fp, "NAME=\"Printer Class\"\n");
5311 else
5312 cupsFilePuts(fp, "NAME=\"Remote Destination\"\n");
5313
5314 if (p->type & CUPS_PRINTER_COLOR)
5315 cupsFilePuts(fp, "TYPE=ColorPostScript\n");
5316 else
5317 cupsFilePuts(fp, "TYPE=MonoPostScript\n");
5318
5319 cupsFilePrintf(fp, "HOSTNAME=%s\n", ServerName);
5320 cupsFilePrintf(fp, "HOSTPRINTER=%s\n", p->name);
5321
5322 cupsFileClose(fp);
5323
5324 chmod(filename, 0755);
5325 chown(filename, User, Group);
5326 }
5327
5328 /*
5329 * Then the member file that tells which device file the queue is connected
5330 * to... Networked printers use "/dev/null" in this file, so that's what
5331 * we use (the actual device URI can confuse some apps...)
5332 */
5333
5334 snprintf(filename, sizeof(filename), "/var/spool/lp/member/%s", p->name);
5335
5336 if (p->type & CUPS_PRINTER_CLASS)
5337 unlink(filename);
5338 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
5339 {
5340 cupsFilePuts(fp, "/dev/null\n");
5341
5342 cupsFileClose(fp);
5343
5344 chmod(filename, 0644);
5345 chown(filename, User, Group);
5346 }
5347
5348 /*
5349 * The gui_interface file is a script or program that launches a GUI
5350 * option panel for the printer, using options specified on the
5351 * command-line in the third argument. The option panel must send
5352 * any printing options to stdout on a single line when the user
5353 * accepts them, or nothing if the user cancels the dialog.
5354 *
5355 * The default options panel program is /usr/bin/glpoptions, from
5356 * the ESP Print Pro software. You can select another using the
5357 * PrintcapGUI option.
5358 */
5359
5360 snprintf(filename, sizeof(filename), "/var/spool/lp/gui_interface/ELF/%s.gui", p->name);
5361
5362 if (p->type & CUPS_PRINTER_CLASS)
5363 unlink(filename);
5364 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
5365 {
5366 cupsFilePuts(fp, "#!/bin/sh\n");
5367 cupsFilePrintf(fp, "%s -d %s -o \"$3\"\n", PrintcapGUI, p->name);
5368
5369 cupsFileClose(fp);
5370
5371 chmod(filename, 0755);
5372 chown(filename, User, Group);
5373 }
5374
5375 /*
5376 * The POD config file is needed by the printstatus command to show
5377 * the printer location and device.
5378 */
5379
5380 snprintf(filename, sizeof(filename), "/var/spool/lp/pod/%s.config", p->name);
5381
5382 if (p->type & CUPS_PRINTER_CLASS)
5383 unlink(filename);
5384 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
5385 {
5386 cupsFilePrintf(fp, "Printer Class | %s\n",
5387 (p->type & CUPS_PRINTER_COLOR) ? "ColorPostScript" : "MonoPostScript");
5388 cupsFilePrintf(fp, "Printer Model | %s\n", p->make_model ? p->make_model : "");
5389 cupsFilePrintf(fp, "Location Code | %s\n", p->location ? p->location : "");
5390 cupsFilePrintf(fp, "Physical Location | %s\n", p->info ? p->info : "");
5391 cupsFilePrintf(fp, "Port Path | %s\n", p->device_uri);
5392 cupsFilePrintf(fp, "Config Path | /var/spool/lp/pod/%s.config\n", p->name);
5393 cupsFilePrintf(fp, "Active Status Path | /var/spool/lp/pod/%s.status\n", p->name);
5394 cupsFilePuts(fp, "Status Update Wait | 10 seconds\n");
5395
5396 cupsFileClose(fp);
5397
5398 chmod(filename, 0664);
5399 chown(filename, User, Group);
5400 }
5401 }
5402
5403
5404 /*
5405 * 'write_irix_state()' - Update the status files used by IRIX printing
5406 * desktop tools.
5407 */
5408
5409 static void
5410 write_irix_state(cupsd_printer_t *p) /* I - Printer to update */
5411 {
5412 char filename[1024]; /* Interface script filename */
5413 cups_file_t *fp; /* Interface script file */
5414 int tag; /* Status tag value */
5415
5416
5417 if (p)
5418 {
5419 /*
5420 * The POD status file is needed for the printstatus window to
5421 * provide the current status of the printer.
5422 */
5423
5424 snprintf(filename, sizeof(filename), "/var/spool/lp/pod/%s.status", p->name);
5425
5426 if (p->type & CUPS_PRINTER_CLASS)
5427 unlink(filename);
5428 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
5429 {
5430 cupsFilePrintf(fp, "Operational Status | %s\n",
5431 (p->state == IPP_PRINTER_IDLE) ? "Idle" :
5432 (p->state == IPP_PRINTER_PROCESSING) ? "Busy" :
5433 "Faulted");
5434 cupsFilePrintf(fp, "Information | 01 00 00 | %s\n", CUPS_SVERSION);
5435 cupsFilePrintf(fp, "Information | 02 00 00 | Device URI: %s\n",
5436 p->device_uri);
5437 cupsFilePrintf(fp, "Information | 03 00 00 | %s jobs\n",
5438 p->accepting ? "Accepting" : "Not accepting");
5439 cupsFilePrintf(fp, "Information | 04 00 00 | %s\n", p->state_message);
5440
5441 cupsFileClose(fp);
5442
5443 chmod(filename, 0664);
5444 chown(filename, User, Group);
5445 }
5446
5447 /*
5448 * The activeicons file is needed to provide desktop icons for printers:
5449 *
5450 * [ quoted from /usr/lib/print/tagit ]
5451 *
5452 * --- Type of printer tags (base values)
5453 *
5454 * Dumb=66048 # 0x10200
5455 * DumbColor=66080 # 0x10220
5456 * Raster=66112 # 0x10240
5457 * ColorRaster=66144 # 0x10260
5458 * Plotter=66176 # 0x10280
5459 * PostScript=66208 # 0x102A0
5460 * ColorPostScript=66240 # 0x102C0
5461 * MonoPostScript=66272 # 0x102E0
5462 *
5463 * --- Printer state modifiers for local printers
5464 *
5465 * Idle=0 # 0x0
5466 * Busy=1 # 0x1
5467 * Faulted=2 # 0x2
5468 * Unknown=3 # 0x3 (Faulted due to unknown reason)
5469 *
5470 * --- Printer state modifiers for network printers
5471 *
5472 * NetIdle=8 # 0x8
5473 * NetBusy=9 # 0x9
5474 * NetFaulted=10 # 0xA
5475 * NetUnknown=11 # 0xB (Faulted due to unknown reason)
5476 */
5477
5478 snprintf(filename, sizeof(filename), "/var/spool/lp/activeicons/%s", p->name);
5479
5480 if (p->type & CUPS_PRINTER_CLASS)
5481 unlink(filename);
5482 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
5483 {
5484 if (p->type & CUPS_PRINTER_COLOR)
5485 tag = 66240;
5486 else
5487 tag = 66272;
5488
5489 if (p->type & CUPS_PRINTER_REMOTE)
5490 tag |= 8;
5491
5492 if (p->state == IPP_PRINTER_PROCESSING)
5493 tag |= 1;
5494
5495 else if (p->state == IPP_PRINTER_STOPPED)
5496 tag |= 2;
5497
5498 cupsFilePuts(fp, "#!/bin/sh\n");
5499 cupsFilePrintf(fp, "#Tag %d\n", tag);
5500
5501 cupsFileClose(fp);
5502
5503 chmod(filename, 0755);
5504 chown(filename, User, Group);
5505 }
5506 }
5507
5508 /*
5509 * The default file is needed by the printers window to show
5510 * the default printer.
5511 */
5512
5513 snprintf(filename, sizeof(filename), "/var/spool/lp/default");
5514
5515 if (DefaultPrinter != NULL)
5516 {
5517 if ((fp = cupsFileOpen(filename, "w")) != NULL)
5518 {
5519 cupsFilePrintf(fp, "%s\n", DefaultPrinter->name);
5520
5521 cupsFileClose(fp);
5522
5523 chmod(filename, 0644);
5524 chown(filename, User, Group);
5525 }
5526 }
5527 else
5528 unlink(filename);
5529 }
5530 #endif /* __sgi */
5531
5532
5533 /*
5534 * 'write_xml_string()' - Write a string with XML escaping.
5535 */
5536
5537 static void
5538 write_xml_string(cups_file_t *fp, /* I - File to write to */
5539 const char *s) /* I - String to write */
5540 {
5541 const char *start; /* Start of current sequence */
5542
5543
5544 if (!s)
5545 return;
5546
5547 for (start = s; *s; s ++)
5548 {
5549 if (*s == '&')
5550 {
5551 if (s > start)
5552 cupsFileWrite(fp, start, s - start);
5553
5554 cupsFilePuts(fp, "&amp;");
5555 start = s + 1;
5556 }
5557 else if (*s == '<')
5558 {
5559 if (s > start)
5560 cupsFileWrite(fp, start, s - start);
5561
5562 cupsFilePuts(fp, "&lt;");
5563 start = s + 1;
5564 }
5565 }
5566
5567 if (s > start)
5568 cupsFilePuts(fp, start);
5569 }
5570
5571
5572 /*
5573 * End of "$Id: printers.c 7968 2008-09-19 23:03:01Z mike $".
5574 */