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