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