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