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