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