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