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