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