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