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