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