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