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