]> git.ipfire.org Git - thirdparty/cups.git/blame - scheduler/printers.c
Use _httpDecodeURI private API to remove percent encoding of phone number and
[thirdparty/cups.git] / scheduler / printers.c
CommitLineData
ef416fc2 1/*
f2d18633 2 * "$Id$"
ef416fc2 3 *
7d644288 4 * Printer routines for the CUPS scheduler.
ef416fc2 5 *
7e86f2f6 6 * Copyright 2007-2014 by Apple Inc.
7d644288 7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
ef416fc2 8 *
7d644288
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);
7855ab56
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
ebaac3df
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 }
88f9aafc 888 else if (!_cups_strcasecmp(line, "</Printer>"))
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)
7e86f2f6 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
07623986 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
07623986 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
07623986 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
ef416fc2 1584 cupsFilePuts(fp, "</Printer>\n");
ef416fc2 1585 }
1586
321d8d57 1587 cupsdCloseCreatedConfFile(fp, filename);
ef416fc2 1588}
1589
1590
f7deaa1a 1591/*
1592 * 'cupsdSetAuthInfoRequired()' - Set the required authentication info.
1593 */
1594
1595int /* O - 1 if value OK, 0 otherwise */
1596cupsdSetAuthInfoRequired(
1597 cupsd_printer_t *p, /* I - Printer */
1598 const char *values, /* I - Plain text value (or NULL) */
1599 ipp_attribute_t *attr) /* I - IPP attribute value (or NULL) */
1600{
1601 int i; /* Looping var */
1602
1603
1604 p->num_auth_info_required = 0;
1605
1606 /*
1607 * Do we have a plain text value?
1608 */
1609
1610 if (values)
1611 {
1612 /*
1613 * Yes, grab the keywords...
1614 */
1615
1616 const char *end; /* End of current value */
1617
1618
1619 while (*values && p->num_auth_info_required < 4)
1620 {
1621 if ((end = strchr(values, ',')) == NULL)
1622 end = values + strlen(values);
1623
f899b121 1624 if ((end - values) == 4 && !strncmp(values, "none", 4))
f7deaa1a 1625 {
1626 if (p->num_auth_info_required != 0 || *end)
1627 return (0);
1628
1629 p->auth_info_required[p->num_auth_info_required] = "none";
1630 p->num_auth_info_required ++;
1631
1632 return (1);
1633 }
f899b121 1634 else if ((end - values) == 9 && !strncmp(values, "negotiate", 9))
1635 {
1636 if (p->num_auth_info_required != 0 || *end)
1637 return (0);
1638
1639 p->auth_info_required[p->num_auth_info_required] = "negotiate";
1640 p->num_auth_info_required ++;
07ed0e9a
MS
1641
1642 /*
1643 * Don't allow sharing of queues that require Kerberos authentication.
1644 */
1645
1646 if (p->shared)
1647 {
1648 cupsdDeregisterPrinter(p, 1);
1649 p->shared = 0;
1650 }
f899b121 1651 }
1652 else if ((end - values) == 6 && !strncmp(values, "domain", 6))
f7deaa1a 1653 {
1654 p->auth_info_required[p->num_auth_info_required] = "domain";
1655 p->num_auth_info_required ++;
1656 }
f899b121 1657 else if ((end - values) == 8 && !strncmp(values, "password", 8))
f7deaa1a 1658 {
1659 p->auth_info_required[p->num_auth_info_required] = "password";
1660 p->num_auth_info_required ++;
1661 }
f899b121 1662 else if ((end - values) == 8 && !strncmp(values, "username", 8))
f7deaa1a 1663 {
1664 p->auth_info_required[p->num_auth_info_required] = "username";
1665 p->num_auth_info_required ++;
1666 }
1667 else
1668 return (0);
09a101d6 1669
1670 values = (*end) ? end + 1 : end;
f7deaa1a 1671 }
1672
1673 if (p->num_auth_info_required == 0)
1674 {
1675 p->auth_info_required[0] = "none";
1676 p->num_auth_info_required = 1;
1677 }
1678
09a101d6 1679 /*
1680 * Update the printer-type value as needed...
1681 */
1682
1683 if (p->num_auth_info_required > 1 ||
1684 strcmp(p->auth_info_required[0], "none"))
1685 p->type |= CUPS_PRINTER_AUTHENTICATED;
1686 else
7e86f2f6 1687 p->type &= (cups_ptype_t)~CUPS_PRINTER_AUTHENTICATED;
09a101d6 1688
f7deaa1a 1689 return (1);
1690 }
1691
1692 /*
1693 * Grab values from an attribute instead...
1694 */
1695
1696 if (!attr || attr->num_values > 4)
1697 return (0);
1698
1699 for (i = 0; i < attr->num_values; i ++)
1700 {
1701 if (!strcmp(attr->values[i].string.text, "none"))
1702 {
1703 if (p->num_auth_info_required != 0 || attr->num_values != 1)
1704 return (0);
1705
1706 p->auth_info_required[p->num_auth_info_required] = "none";
1707 p->num_auth_info_required ++;
1708
1709 return (1);
1710 }
f899b121 1711 else if (!strcmp(attr->values[i].string.text, "negotiate"))
1712 {
1713 if (p->num_auth_info_required != 0 || attr->num_values != 1)
1714 return (0);
1715
1716 p->auth_info_required[p->num_auth_info_required] = "negotiate";
1717 p->num_auth_info_required ++;
1718
07ed0e9a
MS
1719 /*
1720 * Don't allow sharing of queues that require Kerberos authentication.
1721 */
1722
1723 if (p->shared)
1724 {
1725 cupsdDeregisterPrinter(p, 1);
1726 p->shared = 0;
1727 }
1728
f899b121 1729 return (1);
1730 }
f7deaa1a 1731 else if (!strcmp(attr->values[i].string.text, "domain"))
1732 {
1733 p->auth_info_required[p->num_auth_info_required] = "domain";
1734 p->num_auth_info_required ++;
1735 }
1736 else if (!strcmp(attr->values[i].string.text, "password"))
1737 {
1738 p->auth_info_required[p->num_auth_info_required] = "password";
1739 p->num_auth_info_required ++;
1740 }
1741 else if (!strcmp(attr->values[i].string.text, "username"))
1742 {
1743 p->auth_info_required[p->num_auth_info_required] = "username";
1744 p->num_auth_info_required ++;
1745 }
1746 else
1747 return (0);
1748 }
1749
1750 return (1);
1751}
1752
1753
0af14961
MS
1754/*
1755 * 'cupsdSetDeviceURI()' - Set the device URI for a printer.
1756 */
1757
1758void
1759cupsdSetDeviceURI(cupsd_printer_t *p, /* I - Printer */
1760 const char *uri) /* I - Device URI */
1761{
1762 char buffer[1024], /* URI buffer */
1763 *start, /* Start of data after scheme */
1764 *slash, /* First slash after scheme:// */
1765 *ptr; /* Pointer into user@host:port part */
1766
1767
1768 /*
1769 * Set the full device URI..
1770 */
1771
1772 cupsdSetString(&(p->device_uri), uri);
1773
1774 /*
1775 * Copy the device URI to a temporary buffer so we can sanitize any auth
1776 * info in it...
1777 */
1778
1779 strlcpy(buffer, uri, sizeof(buffer));
1780
1781 /*
1782 * Find the end of the scheme:// part...
1783 */
1784
1785 if ((ptr = strchr(buffer, ':')) != NULL)
1786 {
1787 for (start = ptr + 1; *start; start ++)
1788 if (*start != '/')
1789 break;
1790
1791 /*
1792 * Find the next slash (/) in the URI...
1793 */
1794
1795 if ((slash = strchr(start, '/')) == NULL)
1796 slash = start + strlen(start); /* No slash, point to the end */
1797
1798 /*
1799 * Check for an @ sign before the slash...
1800 */
1801
1802 if ((ptr = strchr(start, '@')) != NULL && ptr < slash)
1803 {
1804 /*
1805 * Found an @ sign and it is before the resource part, so we have
1806 * an authentication string. Copy the remaining URI over the
1807 * authentication string...
1808 */
1809
1810 _cups_strcpy(start, ptr + 1);
1811 }
1812 }
1813
1814 /*
1815 * Save the sanitized URI...
1816 */
1817
1818 cupsdSetString(&(p->sanitized_device_uri), buffer);
1819}
1820
1821
5a738aea
MS
1822/*
1823 * 'cupsdSetPrinterAttr()' - Set a printer attribute.
1824 */
1825
1826void
1827cupsdSetPrinterAttr(
1828 cupsd_printer_t *p, /* I - Printer */
1829 const char *name, /* I - Attribute name */
7d644288 1830 const char *value) /* I - Attribute value string */
5a738aea
MS
1831{
1832 ipp_attribute_t *attr; /* Attribute */
1833 int i, /* Looping var */
1834 count; /* Number of values */
7d644288
MS
1835 char *temp, /* Temporary copy of value string */
1836 *ptr, /* Pointer into value */
12f89d24
MS
1837 *start, /* Start of value */
1838 quote; /* Quote character */
5a738aea
MS
1839 ipp_tag_t value_tag; /* Value tag for this attribute */
1840
1841
e6013cfa
MS
1842 /*
1843 * Don't allow empty values...
1844 */
1845
1340db2d 1846 if (!*value && strcmp(name, "marker-message"))
e6013cfa
MS
1847 {
1848 cupsdLogMessage(CUPSD_LOG_ERROR, "Ignoring empty \"%s\" attribute", name);
1849 return;
1850 }
1851
7d644288
MS
1852 /*
1853 * Copy the value string so we can do what we want with it...
1854 */
1855
1856 if ((temp = strdup(value)) == NULL)
1857 {
1858 cupsdLogMessage(CUPSD_LOG_ERROR,
1859 "Unable to duplicate value for \"%s\" attribute.", name);
1860 return;
1861 }
1862
5a738aea
MS
1863 /*
1864 * Count the number of values...
1865 */
1866
7d644288 1867 for (count = 1, quote = '\0', ptr = temp;
12f89d24
MS
1868 *ptr;
1869 ptr ++)
1870 {
1871 if (*ptr == quote)
1872 quote = '\0';
1873 else if (quote)
1874 continue;
1875 else if (*ptr == '\\' && ptr[1])
1876 ptr ++;
1877 else if (*ptr == '\'' || *ptr == '\"')
1878 quote = *ptr;
3e7fe0ca 1879 else if (*ptr == ',')
12f89d24
MS
1880 count ++;
1881 }
5a738aea
MS
1882
1883 /*
1884 * Then add or update the attribute as needed...
1885 */
1886
ed6e7faf
MS
1887 if (!strcmp(name, "marker-levels") || !strcmp(name, "marker-low-levels") ||
1888 !strcmp(name, "marker-high-levels"))
5a738aea
MS
1889 {
1890 /*
1891 * Integer values...
1892 */
1893
1894 if ((attr = ippFindAttribute(p->attrs, name, IPP_TAG_INTEGER)) != NULL &&
1895 attr->num_values < count)
1896 {
1897 ippDeleteAttribute(p->attrs, attr);
1898 attr = NULL;
1899 }
1900
1901 if (attr)
1902 attr->num_values = count;
1903 else
1904 attr = ippAddIntegers(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER, name,
1905 count, NULL);
1906
1907 if (!attr)
1908 {
59ac438c 1909 free(temp);
5a738aea
MS
1910 cupsdLogMessage(CUPSD_LOG_ERROR,
1911 "Unable to allocate memory for printer attribute "
1912 "(%d values)", count);
1913 return;
1914 }
1915
7d644288 1916 for (i = 0, start = temp; i < count; i ++)
5a738aea 1917 {
7d644288 1918 if ((ptr = strchr(start, ',')) != NULL)
5a738aea
MS
1919 *ptr++ = '\0';
1920
7d644288 1921 attr->values[i].integer = strtol(start, NULL, 10);
5a738aea
MS
1922
1923 if (ptr)
7d644288 1924 start = ptr;
5a738aea
MS
1925 }
1926 }
1927 else
1928 {
1929 /*
1930 * Name or keyword values...
1931 */
1932
1933 if (!strcmp(name, "marker-types"))
1934 value_tag = IPP_TAG_KEYWORD;
75bd9771
MS
1935 else if (!strcmp(name, "marker-message"))
1936 value_tag = IPP_TAG_TEXT;
5a738aea
MS
1937 else
1938 value_tag = IPP_TAG_NAME;
1939
1940 if ((attr = ippFindAttribute(p->attrs, name, value_tag)) != NULL &&
1941 attr->num_values < count)
1942 {
1943 ippDeleteAttribute(p->attrs, attr);
1944 attr = NULL;
1945 }
1946
1947 if (attr)
75bd9771
MS
1948 {
1949 for (i = 0; i < attr->num_values; i ++)
1950 _cupsStrFree(attr->values[i].string.text);
1951
5a738aea 1952 attr->num_values = count;
75bd9771 1953 }
5a738aea
MS
1954 else
1955 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, value_tag, name,
1956 count, NULL, NULL);
1957
1958 if (!attr)
1959 {
59ac438c 1960 free(temp);
5a738aea
MS
1961 cupsdLogMessage(CUPSD_LOG_ERROR,
1962 "Unable to allocate memory for printer attribute "
1963 "(%d values)", count);
1964 return;
1965 }
1966
7d644288 1967 for (i = 0, quote = '\0', ptr = temp; i < count; i ++)
5a738aea 1968 {
12f89d24
MS
1969 for (start = ptr; *ptr; ptr ++)
1970 {
1971 if (*ptr == quote)
1972 *ptr = quote = '\0';
1973 else if (quote)
1974 continue;
1975 else if (*ptr == '\\' && ptr[1])
1976 _cups_strcpy(ptr, ptr + 1);
1977 else if (*ptr == '\'' || *ptr == '\"')
1978 {
1979 quote = *ptr;
5a738aea 1980
12f89d24
MS
1981 if (ptr == start)
1982 start ++;
1983 else
1984 _cups_strcpy(ptr, ptr + 1);
1985 }
3e7fe0ca 1986 else if (*ptr == ',')
12f89d24
MS
1987 {
1988 *ptr++ = '\0';
1989 break;
1990 }
1991 }
5a738aea 1992
12f89d24 1993 attr->values[i].string.text = _cupsStrAlloc(start);
5a738aea
MS
1994 }
1995 }
7d644288
MS
1996
1997 free(temp);
5a738aea
MS
1998}
1999
2000
ef416fc2 2001/*
2002 * 'cupsdSetPrinterAttrs()' - Set printer attributes based upon the PPD file.
2003 */
2004
2005void
2006cupsdSetPrinterAttrs(cupsd_printer_t *p)/* I - Printer to setup */
2007{
a2326b5b 2008 int i; /* Looping var */
b423cd4c 2009 char resource[HTTP_MAX_URI]; /* Resource portion of URI */
b423cd4c 2010 cupsd_location_t *auth; /* Pointer to authentication element */
2011 const char *auth_supported; /* Authentication supported */
8922323b 2012 ipp_t *oldattrs; /* Old printer attributes */
b423cd4c 2013 ipp_attribute_t *attr; /* Attribute data */
10d09e33
MS
2014 char *name, /* Current user/group name */
2015 *filter; /* Current filter */
ef416fc2 2016
2017
2018 DEBUG_printf(("cupsdSetPrinterAttrs: entering name = %s, type = %x\n", p->name,
2019 p->type));
2020
2021 /*
2022 * Make sure that we have the common attributes defined...
2023 */
2024
2025 if (!CommonData)
2026 cupsdCreateCommonData();
2027
2028 /*
2029 * Clear out old filters, if any...
2030 */
2031
e1d6a774 2032 delete_printer_filters(p);
ef416fc2 2033
2034 /*
2035 * Figure out the authentication that is required for the printer.
2036 */
2037
2038 auth_supported = "requesting-user-name";
ef416fc2 2039
178cb736
MS
2040 if (p->type & CUPS_PRINTER_CLASS)
2041 snprintf(resource, sizeof(resource), "/classes/%s", p->name);
2042 else
2043 snprintf(resource, sizeof(resource), "/printers/%s", p->name);
ef416fc2 2044
178cb736
MS
2045 if ((auth = cupsdFindBest(resource, HTTP_POST)) == NULL ||
2046 auth->type == CUPSD_AUTH_NONE)
2047 auth = cupsdFindPolicyOp(p->op_policy_ptr, IPP_PRINT_JOB);
9a4f8274 2048
178cb736
MS
2049 if (auth)
2050 {
2051 int auth_type; /* Authentication type */
9a4f8274 2052
9a4f8274 2053
178cb736 2054 if ((auth_type = auth->type) == CUPSD_AUTH_DEFAULT)
dcb445bc 2055 auth_type = cupsdDefaultAuthType();
178cb736 2056
e0660879 2057 if (auth_type == CUPSD_AUTH_BASIC)
178cb736 2058 auth_supported = "basic";
f899b121 2059#ifdef HAVE_GSSAPI
178cb736
MS
2060 else if (auth_type == CUPSD_AUTH_NEGOTIATE)
2061 auth_supported = "negotiate";
f899b121 2062#endif /* HAVE_GSSAPI */
ef416fc2 2063
a2326b5b
MS
2064 if (auth_type != CUPSD_AUTH_NONE)
2065 p->type |= CUPS_PRINTER_AUTHENTICATED;
2066 else
7e86f2f6 2067 p->type &= (cups_ptype_t)~CUPS_PRINTER_AUTHENTICATED;
bc44d920 2068 }
a2326b5b 2069 else
7e86f2f6 2070 p->type &= (cups_ptype_t)~CUPS_PRINTER_AUTHENTICATED;
ef416fc2 2071
2072 /*
2073 * Create the required IPP attributes for a printer...
2074 */
2075
8922323b 2076 oldattrs = p->attrs;
ef416fc2 2077 p->attrs = ippNew();
2078
2079 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2080 "uri-authentication-supported", NULL, auth_supported);
2081 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2082 "uri-security-supported", NULL, "none");
2083 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME, "printer-name", NULL,
2084 p->name);
2085 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-location",
2086 NULL, p->location ? p->location : "");
2087 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-info",
2088 NULL, p->info ? p->info : "");
82f97232
MS
2089 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI, "printer-uuid", NULL,
2090 p->uuid);
ef416fc2 2091
10d09e33 2092 if (cupsArrayCount(p->users) > 0)
ef416fc2 2093 {
2094 if (p->deny_users)
10d09e33
MS
2095 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
2096 "requesting-user-name-denied",
2097 cupsArrayCount(p->users), NULL, NULL);
ef416fc2 2098 else
10d09e33
MS
2099 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
2100 "requesting-user-name-allowed",
2101 cupsArrayCount(p->users), NULL, NULL);
2102
2103 for (i = 0, name = (char *)cupsArrayFirst(p->users);
2104 name;
2105 i ++, name = (char *)cupsArrayNext(p->users))
6961465f 2106 attr->values[i].string.text = _cupsStrAlloc(name);
ef416fc2 2107 }
2108
2109 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
2110 "job-quota-period", p->quota_period);
2111 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
2112 "job-k-limit", p->k_limit);
2113 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
2114 "job-page-limit", p->page_limit);
cb7f98ee
MS
2115 if (p->num_auth_info_required > 0 && strcmp(p->auth_info_required[0], "none"))
2116 ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2117 "auth-info-required", p->num_auth_info_required, NULL,
2118 p->auth_info_required);
ef416fc2 2119
a2326b5b 2120 if (cupsArrayCount(Banners) > 0)
ef416fc2 2121 {
2122 /*
2123 * Setup the job-sheets-default attribute...
2124 */
2125
2126 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
2127 "job-sheets-default", 2, NULL, NULL);
2128
2129 if (attr != NULL)
2130 {
757d2cad 2131 attr->values[0].string.text = _cupsStrAlloc(Classification ?
ef416fc2 2132 Classification : p->job_sheets[0]);
757d2cad 2133 attr->values[1].string.text = _cupsStrAlloc(Classification ?
ef416fc2 2134 Classification : p->job_sheets[1]);
2135 }
2136 }
2137
d09495fa 2138 p->raw = 0;
2139 p->remote = 0;
ef416fc2 2140
a2326b5b
MS
2141 /*
2142 * Assign additional attributes depending on whether this is a printer
2143 * or class...
2144 */
2145
2146 if (p->type & CUPS_PRINTER_CLASS)
ef416fc2 2147 {
a2326b5b 2148 p->raw = 1;
7e86f2f6 2149 p->type &= (cups_ptype_t)~CUPS_PRINTER_OPTIONS;
a2326b5b 2150
ef416fc2 2151 /*
a2326b5b 2152 * Add class-specific attributes...
ef416fc2 2153 */
2154
a2326b5b
MS
2155 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
2156 "printer-make-and-model", NULL, "Local Printer Class");
2157 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI, "device-uri", NULL,
2158 "file:///dev/null");
2159
2160 if (p->num_printers > 0)
0268488e
MS
2161 {
2162 /*
a2326b5b 2163 * Add a list of member names; URIs are added in copy_printer_attrs...
0268488e
MS
2164 */
2165
a2326b5b
MS
2166 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
2167 "member-names", p->num_printers, NULL, NULL);
2168 p->type |= CUPS_PRINTER_OPTIONS;
0268488e 2169
a2326b5b
MS
2170 for (i = 0; i < p->num_printers; i ++)
2171 {
2172 if (attr != NULL)
6961465f 2173 attr->values[i].string.text = _cupsStrAlloc(p->printers[i]->name);
0268488e 2174
7e86f2f6 2175 p->type &= (cups_ptype_t)~CUPS_PRINTER_OPTIONS | p->printers[i]->type;
a2326b5b 2176 }
0268488e 2177 }
ef416fc2 2178 }
2179 else
2180 {
2181 /*
a2326b5b 2182 * Add printer-specific attributes...
ef416fc2 2183 */
2184
a2326b5b
MS
2185 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI, "device-uri", NULL,
2186 p->sanitized_device_uri);
ef416fc2 2187
a2326b5b
MS
2188 /*
2189 * Assign additional attributes from the PPD file (if any)...
2190 */
fa73b229 2191
a2326b5b 2192 load_ppd(p);
ef416fc2 2193
a2326b5b
MS
2194 /*
2195 * Add filters for printer...
2196 */
ef416fc2 2197
a2326b5b
MS
2198 cupsdSetPrinterReasons(p, "-cups-missing-filter-warning,"
2199 "cups-insecure-filter-warning");
ef416fc2 2200
a2326b5b
MS
2201 if (p->pc && p->pc->filters)
2202 {
2203 for (filter = (char *)cupsArrayFirst(p->pc->filters);
2204 filter;
2205 filter = (char *)cupsArrayNext(p->pc->filters))
2206 add_printer_filter(p, p->filetype, filter);
ef416fc2 2207 }
a2326b5b 2208 else if (!(p->type & CUPS_PRINTER_REMOTE))
ef416fc2 2209 {
a2326b5b 2210 char interface[1024]; /* Interface script */
ef416fc2 2211
ef416fc2 2212
a2326b5b
MS
2213 snprintf(interface, sizeof(interface), "%s/interfaces/%s", ServerRoot,
2214 p->name);
2215 if (!access(interface, X_OK))
f14324a7 2216 {
a2326b5b
MS
2217 /*
2218 * Yes, we have a System V style interface script; use it!
2219 */
2220
2221 snprintf(interface, sizeof(interface), "*/* 0 %s/interfaces/%s",
2222 ServerRoot, p->name);
2223 add_printer_filter(p, p->filetype, interface);
f14324a7 2224 }
a2326b5b 2225 else
f14324a7 2226 {
a2326b5b
MS
2227 /*
2228 * Add a filter from application/vnd.cups-raw to printer/name to
2229 * handle "raw" printing by users.
2230 */
f14324a7 2231
a2326b5b 2232 add_printer_filter(p, p->filetype, "application/vnd.cups-raw 0 -");
ef416fc2 2233
a2326b5b
MS
2234 /*
2235 * Add a PostScript filter, since this is still possibly PS printer.
2236 */
f14324a7 2237
a2326b5b
MS
2238 add_printer_filter(p, p->filetype,
2239 "application/vnd.cups-postscript 0 -");
f14324a7 2240 }
a2326b5b 2241 }
f14324a7 2242
a2326b5b
MS
2243 if (p->pc && p->pc->prefilters)
2244 {
2245 if (!p->prefiltertype)
2246 p->prefiltertype = mimeAddType(MimeDatabase, "prefilter", p->name);
ef416fc2 2247
a2326b5b
MS
2248 for (filter = (char *)cupsArrayFirst(p->pc->prefilters);
2249 filter;
2250 filter = (char *)cupsArrayNext(p->pc->prefilters))
2251 add_printer_filter(p, p->prefiltertype, filter);
ef416fc2 2252 }
2253 }
2254
8922323b
MS
2255 /*
2256 * Copy marker attributes as needed...
2257 */
2258
2259 if (oldattrs)
2260 {
2261 ipp_attribute_t *oldattr; /* Old attribute */
2262
2263
2264 if ((oldattr = ippFindAttribute(oldattrs, "marker-colors",
2265 IPP_TAG_NAME)) != NULL)
2266 {
2267 if ((attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
2268 "marker-colors", oldattr->num_values, NULL,
2269 NULL)) != NULL)
2270 {
2271 for (i = 0; i < oldattr->num_values; i ++)
2272 attr->values[i].string.text =
6961465f 2273 _cupsStrAlloc(oldattr->values[i].string.text);
8922323b
MS
2274 }
2275 }
2276
2277 if ((oldattr = ippFindAttribute(oldattrs, "marker-levels",
2278 IPP_TAG_INTEGER)) != NULL)
2279 {
2280 if ((attr = ippAddIntegers(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
2281 "marker-levels", oldattr->num_values,
2282 NULL)) != NULL)
2283 {
2284 for (i = 0; i < oldattr->num_values; i ++)
2285 attr->values[i].integer = oldattr->values[i].integer;
2286 }
2287 }
2288
94da7e34
MS
2289 if ((oldattr = ippFindAttribute(oldattrs, "marker-message",
2290 IPP_TAG_TEXT)) != NULL)
2291 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT, "marker-message",
2292 NULL, oldattr->values[0].string.text);
2293
b9faaae1
MS
2294 if ((oldattr = ippFindAttribute(oldattrs, "marker-low-levels",
2295 IPP_TAG_INTEGER)) != NULL)
2296 {
2297 if ((attr = ippAddIntegers(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
2298 "marker-low-levels", oldattr->num_values,
2299 NULL)) != NULL)
2300 {
2301 for (i = 0; i < oldattr->num_values; i ++)
2302 attr->values[i].integer = oldattr->values[i].integer;
2303 }
2304 }
2305
2306 if ((oldattr = ippFindAttribute(oldattrs, "marker-high-levels",
2307 IPP_TAG_INTEGER)) != NULL)
2308 {
2309 if ((attr = ippAddIntegers(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
2310 "marker-high-levels", oldattr->num_values,
2311 NULL)) != NULL)
2312 {
2313 for (i = 0; i < oldattr->num_values; i ++)
2314 attr->values[i].integer = oldattr->values[i].integer;
2315 }
2316 }
2317
8922323b
MS
2318 if ((oldattr = ippFindAttribute(oldattrs, "marker-names",
2319 IPP_TAG_NAME)) != NULL)
2320 {
2321 if ((attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
2322 "marker-names", oldattr->num_values, NULL,
2323 NULL)) != NULL)
2324 {
2325 for (i = 0; i < oldattr->num_values; i ++)
2326 attr->values[i].string.text =
6961465f 2327 _cupsStrAlloc(oldattr->values[i].string.text);
8922323b
MS
2328 }
2329 }
2330
2331 if ((oldattr = ippFindAttribute(oldattrs, "marker-types",
2332 IPP_TAG_KEYWORD)) != NULL)
2333 {
2334 if ((attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2335 "marker-types", oldattr->num_values, NULL,
2336 NULL)) != NULL)
2337 {
2338 for (i = 0; i < oldattr->num_values; i ++)
2339 attr->values[i].string.text =
6961465f 2340 _cupsStrAlloc(oldattr->values[i].string.text);
8922323b
MS
2341 }
2342 }
2343
2344 ippDelete(oldattrs);
2345 }
2346
b423cd4c 2347 /*
bc44d920 2348 * Force sharing off for remote queues...
b423cd4c 2349 */
2350
a2326b5b 2351 if (p->type & CUPS_PRINTER_REMOTE)
bc44d920 2352 p->shared = 0;
b423cd4c 2353
bd7854cb 2354 /*
2355 * Populate the document-format-supported attribute...
2356 */
2357
2358 add_printer_formats(p);
2359
ef416fc2 2360 DEBUG_printf(("cupsdSetPrinterAttrs: leaving name = %s, type = %x\n", p->name,
2361 p->type));
2362
b423cd4c 2363 /*
2364 * Add name-default attributes...
2365 */
2366
2367 add_printer_defaults(p);
2368
f7deaa1a 2369 /*
2370 * Let the browse protocols reflect the change
2371 */
2372
2373 cupsdRegisterPrinter(p);
ef416fc2 2374}
2375
2376
2377/*
2378 * 'cupsdSetPrinterReasons()' - Set/update the reasons strings.
2379 */
2380
e07d4801 2381int /* O - 1 if something changed, 0 otherwise */
ef416fc2 2382cupsdSetPrinterReasons(
c7017ecc
MS
2383 cupsd_printer_t *p, /* I - Printer */
2384 const char *s) /* I - Reasons strings */
ef416fc2 2385{
e07d4801
MS
2386 int i, /* Looping var */
2387 changed = 0; /* Did something change? */
ef416fc2 2388 const char *sptr; /* Pointer into reasons */
2389 char reason[255], /* Reason string */
2390 *rptr; /* Pointer into reason */
2391
2392
b9faaae1
MS
2393 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2394 "cupsdSetPrinterReasons(p=%p(%s),s=\"%s\"", p, p->name, s);
d1c13e16 2395
ef416fc2 2396 if (s[0] == '-' || s[0] == '+')
2397 {
2398 /*
2399 * Add/remove reasons...
2400 */
2401
2402 sptr = s + 1;
2403 }
2404 else
2405 {
2406 /*
2407 * Replace reasons...
2408 */
2409
2410 sptr = s;
2411
2412 for (i = 0; i < p->num_reasons; i ++)
0af14961 2413 _cupsStrFree(p->reasons[i]);
ef416fc2 2414
2415 p->num_reasons = 0;
e07d4801 2416 changed = 1;
0af14961 2417
0268488e 2418 dirty_printer(p);
ef416fc2 2419 }
2420
bc44d920 2421 if (!strcmp(s, "none"))
e07d4801 2422 return (changed);
bc44d920 2423
ef416fc2 2424 /*
2425 * Loop through all of the reasons...
2426 */
2427
2428 while (*sptr)
2429 {
2430 /*
2431 * Skip leading whitespace and commas...
2432 */
2433
2434 while (isspace(*sptr & 255) || *sptr == ',')
2435 sptr ++;
2436
2437 for (rptr = reason; *sptr && !isspace(*sptr & 255) && *sptr != ','; sptr ++)
2438 if (rptr < (reason + sizeof(reason) - 1))
2439 *rptr++ = *sptr;
2440
2441 if (rptr == reason)
2442 break;
2443
2444 *rptr = '\0';
2445
2446 if (s[0] == '-')
2447 {
2448 /*
2449 * Remove reason...
2450 */
2451
2452 for (i = 0; i < p->num_reasons; i ++)
d1c13e16 2453 if (!strcmp(reason, p->reasons[i]))
ef416fc2 2454 {
2455 /*
2456 * Found a match, so remove it...
2457 */
2458
2459 p->num_reasons --;
e07d4801 2460 changed = 1;
1f6f3dbc 2461 _cupsStrFree(p->reasons[i]);
ef416fc2 2462
2463 if (i < p->num_reasons)
7e86f2f6 2464 memmove(p->reasons + i, p->reasons + i + 1, (size_t)(p->num_reasons - i) * sizeof(char *));
ef416fc2 2465
c0e1af83 2466 if (!strcmp(reason, "paused") && p->state == IPP_PRINTER_STOPPED)
2467 cupsdSetPrinterState(p, IPP_PRINTER_IDLE, 1);
0af14961 2468
1e3e80bb 2469 if (!strcmp(reason, "cups-waiting-for-job-completed") && p->job)
581dae2d
MS
2470 p->job->completed = 0;
2471
e6013cfa 2472 if (strcmp(reason, "connecting-to-device"))
0268488e 2473 dirty_printer(p);
581dae2d 2474
d1c13e16 2475 break;
ef416fc2 2476 }
2477 }
2478 else if (p->num_reasons < (int)(sizeof(p->reasons) / sizeof(p->reasons[0])))
2479 {
2480 /*
2481 * Add reason...
2482 */
2483
2484 for (i = 0; i < p->num_reasons; i ++)
d1c13e16 2485 if (!strcmp(reason, p->reasons[i]))
ef416fc2 2486 break;
2487
2488 if (i >= p->num_reasons)
2489 {
d1c13e16
MS
2490 if (i >= (int)(sizeof(p->reasons) / sizeof(p->reasons[0])))
2491 {
2492 cupsdLogMessage(CUPSD_LOG_ALERT,
2493 "Too many printer-state-reasons values for %s (%d)",
2494 p->name, i + 1);
e07d4801 2495 return (changed);
d1c13e16
MS
2496 }
2497
0af14961 2498 p->reasons[i] = _cupsStrAlloc(reason);
ef416fc2 2499 p->num_reasons ++;
e07d4801 2500 changed = 1;
c0e1af83 2501
2502 if (!strcmp(reason, "paused") && p->state != IPP_PRINTER_STOPPED)
2503 cupsdSetPrinterState(p, IPP_PRINTER_STOPPED, 1);
0af14961 2504
1e3e80bb 2505 if (!strcmp(reason, "cups-waiting-for-job-completed") && p->job)
581dae2d
MS
2506 p->job->completed = 1;
2507
e6013cfa 2508 if (strcmp(reason, "connecting-to-device"))
0268488e 2509 dirty_printer(p);
ef416fc2 2510 }
2511 }
2512 }
e07d4801
MS
2513
2514 return (changed);
ef416fc2 2515}
2516
2517
2518/*
2519 * 'cupsdSetPrinterState()' - Update the current state of a printer.
2520 */
2521
2522void
2523cupsdSetPrinterState(
2524 cupsd_printer_t *p, /* I - Printer to change */
2525 ipp_pstate_t s, /* I - New state */
2526 int update) /* I - Update printers.conf? */
2527{
12f89d24 2528 cupsd_job_t *job; /* Current job */
ef416fc2 2529 ipp_pstate_t old_state; /* Old printer state */
f8b3a85b
MS
2530 static const char * const printer_states[] =
2531 { /* State strings */
2532 "idle",
2533 "processing",
2534 "stopped"
2535 };
ef416fc2 2536
2537
ef416fc2 2538 /*
2539 * Set the new state...
2540 */
2541
2542 old_state = p->state;
2543 p->state = s;
2544
2545 if (old_state != s)
2546 {
0a682745 2547 cupsdAddEvent(s == IPP_PRINTER_STOPPED ? CUPSD_EVENT_PRINTER_STOPPED :
d9bca400 2548 CUPSD_EVENT_PRINTER_STATE, p, NULL,
f8b3a85b 2549 "%s \"%s\" state changed to %s.",
e53920b9 2550 (p->type & CUPS_PRINTER_CLASS) ? "Class" : "Printer",
0268488e 2551 p->name, printer_states[p->state - IPP_PRINTER_IDLE]);
e53920b9 2552
ef416fc2 2553 /*
2554 * Let the browse code know this needs to be updated...
2555 */
2556
a2326b5b 2557 p->state_time = time(NULL);
ef416fc2 2558 }
2559
b9faaae1
MS
2560 /*
2561 * Set/clear the paused reason as needed...
2562 */
2563
745129be
MS
2564 if (s == IPP_PRINTER_STOPPED)
2565 cupsdSetPrinterReasons(p, "+paused");
2566 else
2567 cupsdSetPrinterReasons(p, "-paused");
2568
12f89d24
MS
2569 if (old_state != s)
2570 {
2571 for (job = (cupsd_job_t *)cupsArrayFirst(ActiveJobs);
2572 job;
2573 job = (cupsd_job_t *)cupsArrayNext(ActiveJobs))
2574 if (job->reasons && job->state_value == IPP_JOB_PENDING &&
2575 !_cups_strcasecmp(job->dest, p->name))
2576 ippSetString(job->attrs, &job->reasons, 0,
2577 s == IPP_PRINTER_STOPPED ? "printer-stopped" : "none");
2578 }
2579
b9faaae1
MS
2580 /*
2581 * Clear the message for the queue when going to processing...
2582 */
2583
2584 if (s == IPP_PRINTER_PROCESSING)
2585 p->state_message[0] = '\0';
2586
f7deaa1a 2587 /*
2588 * Let the browse protocols reflect the change...
2589 */
2590
7a14d768
MS
2591 if (update)
2592 cupsdRegisterPrinter(p);
f7deaa1a 2593
ef416fc2 2594 /*
2595 * Save the printer configuration if a printer goes from idle or processing
2596 * to stopped (or visa-versa)...
2597 */
2598
b9faaae1
MS
2599 if (update &&
2600 (old_state == IPP_PRINTER_STOPPED) != (s == IPP_PRINTER_STOPPED))
0268488e 2601 dirty_printer(p);
ef416fc2 2602}
2603
2604
2605/*
2606 * 'cupsdStopPrinter()' - Stop a printer from printing any jobs...
2607 */
2608
2609void
2610cupsdStopPrinter(cupsd_printer_t *p, /* I - Printer to stop */
2611 int update)/* I - Update printers.conf? */
2612{
ef416fc2 2613 /*
2614 * Set the printer state...
2615 */
2616
2617 cupsdSetPrinterState(p, IPP_PRINTER_STOPPED, update);
2618
2619 /*
2620 * See if we have a job printing on this printer...
2621 */
2622
f11a948a 2623 if (p->job && p->job->state_value == IPP_JOB_PROCESSING)
b9faaae1
MS
2624 cupsdSetJobState(p->job, IPP_JOB_PENDING, CUPSD_JOB_DEFAULT,
2625 "Job stopped due to printer being paused.");
ef416fc2 2626}
2627
2628
c9fc04c6
MS
2629/*
2630 * 'cupsdUpdatePrinterPPD()' - Update keywords in a printer's PPD file.
2631 */
2632
2633int /* O - 1 if successful, 0 otherwise */
2634cupsdUpdatePrinterPPD(
2635 cupsd_printer_t *p, /* I - Printer */
2636 int num_keywords, /* I - Number of keywords */
2637 cups_option_t *keywords) /* I - Keywords */
2638{
2639 int i; /* Looping var */
2640 cups_file_t *src, /* Original file */
2641 *dst; /* New file */
2642 char srcfile[1024], /* Original filename */
2643 dstfile[1024], /* New filename */
2644 line[1024], /* Line from file */
2645 keystring[41]; /* Keyword from line */
2646 cups_option_t *keyword; /* Current keyword */
2647
2648
2649 cupsdLogMessage(CUPSD_LOG_INFO, "Updating keywords in PPD file for %s...",
2650 p->name);
2651
2652 /*
2653 * Get the old and new PPD filenames...
2654 */
2655
2656 snprintf(srcfile, sizeof(srcfile), "%s/ppd/%s.ppd.O", ServerRoot, p->name);
2657 snprintf(dstfile, sizeof(srcfile), "%s/ppd/%s.ppd", ServerRoot, p->name);
2658
2659 /*
2660 * Rename the old file and open the old and new...
2661 */
2662
2663 if (rename(dstfile, srcfile))
2664 {
2665 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to backup PPD file for %s: %s",
2666 p->name, strerror(errno));
2667 return (0);
2668 }
2669
2670 if ((src = cupsFileOpen(srcfile, "r")) == NULL)
2671 {
2672 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to open PPD file \"%s\": %s",
2673 srcfile, strerror(errno));
2674 rename(srcfile, dstfile);
2675 return (0);
2676 }
2677
2678 if ((dst = cupsFileOpen(dstfile, "w")) == NULL)
2679 {
2680 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create PPD file \"%s\": %s",
2681 dstfile, strerror(errno));
2682 cupsFileClose(src);
2683 rename(srcfile, dstfile);
2684 return (0);
2685 }
2686
2687 /*
2688 * Copy the first line and then write out all of the keywords...
2689 */
2690
2691 if (!cupsFileGets(src, line, sizeof(line)))
2692 {
2693 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to read PPD file \"%s\": %s",
2694 srcfile, strerror(errno));
2695 cupsFileClose(src);
2696 cupsFileClose(dst);
2697 rename(srcfile, dstfile);
2698 return (0);
2699 }
2700
2701 cupsFilePrintf(dst, "%s\n", line);
2702
2703 for (i = num_keywords, keyword = keywords; i > 0; i --, keyword ++)
2704 {
2705 cupsdLogMessage(CUPSD_LOG_DEBUG, "*%s: %s", keyword->name, keyword->value);
2706 cupsFilePrintf(dst, "*%s: %s\n", keyword->name, keyword->value);
2707 }
2708
2709 /*
2710 * Then copy the rest of the PPD file, dropping any keywords we changed.
2711 */
2712
2713 while (cupsFileGets(src, line, sizeof(line)))
2714 {
2715 /*
2716 * Skip keywords we've already set...
2717 */
2718
2719 if (sscanf(line, "*%40[^:]:", keystring) == 1 &&
2720 cupsGetOption(keystring, num_keywords, keywords))
2721 continue;
2722
2723 /*
2724 * Otherwise write the line...
2725 */
2726
2727 cupsFilePrintf(dst, "%s\n", line);
2728 }
2729
2730 /*
2731 * Close files and return...
2732 */
2733
2734 cupsFileClose(src);
2735 cupsFileClose(dst);
2736
2737 return (1);
2738}
2739
2740
ef416fc2 2741/*
2742 * 'cupsdUpdatePrinters()' - Update printers after a partial reload.
2743 */
2744
2745void
2746cupsdUpdatePrinters(void)
2747{
2748 cupsd_printer_t *p; /* Current printer */
2749
2750
2751 /*
2752 * Loop through the printers and recreate the printer attributes
2753 * for any local printers since the policy and/or access control
2754 * stuff may have changed. Also, if browsing is disabled, remove
2755 * any remote printers...
2756 */
2757
2758 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
2759 p;
2760 p = (cupsd_printer_t *)cupsArrayNext(Printers))
2761 {
ef416fc2 2762 /*
2763 * Update the operation policy pointer...
2764 */
2765
2766 if ((p->op_policy_ptr = cupsdFindPolicy(p->op_policy)) == NULL)
2767 p->op_policy_ptr = DefaultPolicyPtr;
07725fee 2768
2769 /*
a2326b5b 2770 * Update printer attributes...
07725fee 2771 */
2772
a2326b5b 2773 cupsdSetPrinterAttrs(p);
ef416fc2 2774 }
2775}
2776
2777
2778/*
2779 * 'cupsdValidateDest()' - Validate a printer/class destination.
2780 */
2781
2782const char * /* O - Printer or class name */
2783cupsdValidateDest(
f7deaa1a 2784 const char *uri, /* I - Printer URI */
ef416fc2 2785 cups_ptype_t *dtype, /* O - Type (printer or class) */
2786 cupsd_printer_t **printer) /* O - Printer pointer */
2787{
2788 cupsd_printer_t *p; /* Current printer */
2789 char localname[1024],/* Localized hostname */
2790 *lptr, /* Pointer into localized hostname */
f7deaa1a 2791 *sptr, /* Pointer into server name */
2792 *rptr, /* Pointer into resource */
2793 scheme[32], /* Scheme portion of URI */
2794 username[64], /* Username portion of URI */
2795 hostname[HTTP_MAX_HOST],
2796 /* Host portion of URI */
2797 resource[HTTP_MAX_URI];
2798 /* Resource portion of URI */
2799 int port; /* Port portion of URI */
2800
2801
2802 DEBUG_printf(("cupsdValidateDest(uri=\"%s\", dtype=%p, printer=%p)\n", uri,
ef416fc2 2803 dtype, printer));
2804
2805 /*
2806 * Initialize return values...
2807 */
2808
2809 if (printer)
2810 *printer = NULL;
2811
f7deaa1a 2812 if (dtype)
2813 *dtype = (cups_ptype_t)0;
2814
2815 /*
2816 * Pull the hostname and resource from the URI...
2817 */
2818
2819 httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, sizeof(scheme),
2820 username, sizeof(username), hostname, sizeof(hostname),
2821 &port, resource, sizeof(resource));
ef416fc2 2822
2823 /*
2824 * See if the resource is a class or printer...
2825 */
2826
2827 if (!strncmp(resource, "/classes/", 9))
2828 {
2829 /*
2830 * Class...
2831 */
2832
f7deaa1a 2833 rptr = resource + 9;
ef416fc2 2834 }
2835 else if (!strncmp(resource, "/printers/", 10))
2836 {
2837 /*
2838 * Printer...
2839 */
2840
f7deaa1a 2841 rptr = resource + 10;
ef416fc2 2842 }
2843 else
2844 {
2845 /*
2846 * Bad resource name...
2847 */
2848
2849 return (NULL);
2850 }
2851
2852 /*
2853 * See if the printer or class name exists...
2854 */
2855
f7deaa1a 2856 p = cupsdFindDest(rptr);
ef416fc2 2857
f7deaa1a 2858 if (p == NULL && strchr(rptr, '@') == NULL)
ef416fc2 2859 return (NULL);
2860 else if (p != NULL)
2861 {
2862 if (printer)
2863 *printer = p;
2864
f7deaa1a 2865 if (dtype)
a2326b5b 2866 *dtype = p->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_REMOTE);
f7deaa1a 2867
ef416fc2 2868 return (p->name);
2869 }
2870
2871 /*
2872 * Change localhost to the server name...
2873 */
2874
88f9aafc 2875 if (!_cups_strcasecmp(hostname, "localhost"))
f7deaa1a 2876 strlcpy(hostname, ServerName, sizeof(hostname));
ef416fc2 2877
2878 strlcpy(localname, hostname, sizeof(localname));
2879
88f9aafc 2880 if (!_cups_strcasecmp(hostname, ServerName))
ef416fc2 2881 {
2882 /*
2883 * Localize the hostname...
2884 */
2885
2886 lptr = strchr(localname, '.');
2887 sptr = strchr(ServerName, '.');
2888
2889 if (sptr != NULL && lptr != NULL)
2890 {
2891 /*
2892 * Strip the common domain name components...
2893 */
2894
2895 while (lptr != NULL)
2896 {
88f9aafc 2897 if (!_cups_strcasecmp(lptr, sptr))
ef416fc2 2898 {
2899 *lptr = '\0';
2900 break;
2901 }
2902 else
2903 lptr = strchr(lptr + 1, '.');
2904 }
2905 }
2906 }
2907
2908 DEBUG_printf(("localized hostname is \"%s\"...\n", localname));
2909
2910 /*
2911 * Find a matching printer or class...
2912 */
2913
2914 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
2915 p;
2916 p = (cupsd_printer_t *)cupsArrayNext(Printers))
88f9aafc
MS
2917 if (!_cups_strcasecmp(p->hostname, localname) &&
2918 !_cups_strcasecmp(p->name, rptr))
ef416fc2 2919 {
2920 if (printer)
2921 *printer = p;
2922
f7deaa1a 2923 if (dtype)
a2326b5b 2924 *dtype = p->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_REMOTE);
f7deaa1a 2925
ef416fc2 2926 return (p->name);
2927 }
2928
2929 return (NULL);
2930}
2931
2932
2933/*
2934 * 'cupsdWritePrintcap()' - Write a pseudo-printcap file for older applications
2935 * that need it...
2936 */
2937
2938void
2939cupsdWritePrintcap(void)
2940{
0af14961
MS
2941 int i; /* Looping var */
2942 cups_file_t *fp; /* Printcap file */
ef416fc2 2943 cupsd_printer_t *p; /* Current printer */
2944
2945
ef416fc2 2946 /*
2947 * See if we have a printcap file; if not, don't bother writing it.
2948 */
2949
2950 if (!Printcap || !*Printcap)
2951 return;
2952
68b10830
MS
2953 cupsdLogMessage(CUPSD_LOG_INFO, "Generating printcap %s...", Printcap);
2954
ef416fc2 2955 /*
2956 * Open the printcap file...
2957 */
2958
2959 if ((fp = cupsFileOpen(Printcap, "w")) == NULL)
2960 return;
2961
2962 /*
2963 * Put a comment header at the top so that users will know where the
2964 * data has come from...
2965 */
2966
0af14961
MS
2967 if (PrintcapFormat != PRINTCAP_PLIST)
2968 cupsFilePrintf(fp, "# This file was automatically generated by cupsd(8) "
2969 "from the\n"
2970 "# %s/printers.conf file. All changes to this file\n"
2971 "# will be lost.\n", ServerRoot);
ef416fc2 2972
d1c13e16
MS
2973 /*
2974 * Write a new printcap with the current list of printers.
2975 */
2976
2977 switch (PrintcapFormat)
ef416fc2 2978 {
d1c13e16
MS
2979 case PRINTCAP_BSD :
2980 /*
2981 * Each printer is put in the file as:
2982 *
2983 * Printer1:
2984 * Printer2:
2985 * Printer3:
2986 * ...
2987 * PrinterN:
2988 */
ef416fc2 2989
d1c13e16
MS
2990 if (DefaultPrinter)
2991 cupsFilePrintf(fp, "%s|%s:rm=%s:rp=%s:\n", DefaultPrinter->name,
2992 DefaultPrinter->info, ServerName,
2993 DefaultPrinter->name);
ef416fc2 2994
d1c13e16
MS
2995 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
2996 p;
2997 p = (cupsd_printer_t *)cupsArrayNext(Printers))
2998 if (p != DefaultPrinter)
2999 cupsFilePrintf(fp, "%s|%s:rm=%s:rp=%s:\n", p->name, p->info,
3000 ServerName, p->name);
3001 break;
0af14961 3002
d1c13e16
MS
3003 case PRINTCAP_PLIST :
3004 /*
3005 * Each printer is written as a dictionary in a plist file.
3006 * Currently the printer-name, printer-info, printer-is-accepting-jobs,
3007 * printer-location, printer-make-and-model, printer-state,
3008 * printer-state-reasons, printer-type, and (sanitized) device-uri.
3009 */
0af14961 3010
d1c13e16
MS
3011 cupsFilePuts(fp, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
3012 "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD "
3013 "PLIST 1.0//EN\" \"http://www.apple.com/DTDs/"
3014 "PropertyList-1.0.dtd\">\n"
3015 "<plist version=\"1.0\">\n"
3016 "<array>\n");
ef416fc2 3017
d1c13e16
MS
3018 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
3019 p;
3020 p = (cupsd_printer_t *)cupsArrayNext(Printers))
3021 {
3022 cupsFilePuts(fp, "\t<dict>\n"
3023 "\t\t<key>printer-name</key>\n"
3024 "\t\t<string>");
3025 write_xml_string(fp, p->name);
3026 cupsFilePuts(fp, "</string>\n"
3027 "\t\t<key>printer-info</key>\n"
3028 "\t\t<string>");
3029 write_xml_string(fp, p->info);
3030 cupsFilePrintf(fp, "</string>\n"
3031 "\t\t<key>printer-is-accepting-jobs</key>\n"
3032 "\t\t<%s/>\n"
3033 "\t\t<key>printer-location</key>\n"
3034 "\t\t<string>", p->accepting ? "true" : "false");
3035 write_xml_string(fp, p->location);
3036 cupsFilePuts(fp, "</string>\n"
3037 "\t\t<key>printer-make-and-model</key>\n"
3038 "\t\t<string>");
3039 write_xml_string(fp, p->make_model);
3040 cupsFilePrintf(fp, "</string>\n"
3041 "\t\t<key>printer-state</key>\n"
3042 "\t\t<integer>%d</integer>\n"
3043 "\t\t<key>printer-state-reasons</key>\n"
3044 "\t\t<array>\n", p->state);
3045 for (i = 0; i < p->num_reasons; i ++)
3046 {
3047 cupsFilePuts(fp, "\t\t\t<string>");
3048 write_xml_string(fp, p->reasons[i]);
3049 cupsFilePuts(fp, "</string>\n");
3050 }
3051 cupsFilePrintf(fp, "\t\t</array>\n"
3052 "\t\t<key>printer-type</key>\n"
3053 "\t\t<integer>%d</integer>\n"
3054 "\t\t<key>device-uri</key>\n"
3055 "\t\t<string>", p->type);
3056 write_xml_string(fp, p->sanitized_device_uri);
3057 cupsFilePuts(fp, "</string>\n"
3058 "\t</dict>\n");
3059 }
3060 cupsFilePuts(fp, "</array>\n"
3061 "</plist>\n");
3062 break;
3063
3064 case PRINTCAP_SOLARIS :
3065 /*
3066 * Each printer is put in the file as:
3067 *
3068 * _all:all=Printer1,Printer2,Printer3,...,PrinterN
3069 * _default:use=DefaultPrinter
3070 * Printer1:\
3071 * :bsdaddr=ServerName,Printer1:\
3072 * :description=Description:
3073 * Printer2:
3074 * :bsdaddr=ServerName,Printer2:\
3075 * :description=Description:
3076 * Printer3:
3077 * :bsdaddr=ServerName,Printer3:\
3078 * :description=Description:
3079 * ...
3080 * PrinterN:
3081 * :bsdaddr=ServerName,PrinterN:\
3082 * :description=Description:
3083 */
3084
3085 cupsFilePuts(fp, "_all:all=");
3086 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
3087 p;
3088 p = (cupsd_printer_t *)cupsArrayCurrent(Printers))
3089 cupsFilePrintf(fp, "%s%c", p->name,
3090 cupsArrayNext(Printers) ? ',' : '\n');
3091
3092 if (DefaultPrinter)
3093 cupsFilePrintf(fp, "_default:use=%s\n", DefaultPrinter->name);
3094
3095 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
3096 p;
3097 p = (cupsd_printer_t *)cupsArrayNext(Printers))
3098 cupsFilePrintf(fp, "%s:\\\n"
3099 "\t:bsdaddr=%s,%s:\\\n"
3100 "\t:description=%s:\n",
3101 p->name, ServerName, p->name,
3102 p->info ? p->info : "");
3103 break;
ef416fc2 3104 }
3105
3106 /*
3107 * Close the file...
3108 */
3109
3110 cupsFileClose(fp);
3111}
3112
3113
b423cd4c 3114/*
3115 * 'add_printer_defaults()' - Add name-default attributes to the printer attributes.
3116 */
3117
3118static void
3119add_printer_defaults(cupsd_printer_t *p)/* I - Printer */
3120{
3121 int i; /* Looping var */
3122 int num_options; /* Number of default options */
3123 cups_option_t *options, /* Default options */
3124 *option; /* Current option */
3125 char name[256]; /* name-default */
3126
3127
f7deaa1a 3128 /*
3129 * Maintain a common array of default attribute names...
3130 */
3131
3132 if (!CommonDefaults)
3133 {
3134 CommonDefaults = cupsArrayNew((cups_array_func_t)strcmp, NULL);
3135
3136 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("copies-default"));
3137 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("document-format-default"));
3138 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("finishings-default"));
5a9febac
MS
3139 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("job-account-id-default"));
3140 cupsArrayAdd(CommonDefaults,
3141 _cupsStrAlloc("job-accounting-user-id-default"));
ebaac3df 3142 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("job-cancel-after-default"));
f7deaa1a 3143 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("job-hold-until-default"));
3144 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("job-priority-default"));
3145 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("job-sheets-default"));
54afec33 3146 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("media-col-default"));
f7deaa1a 3147 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("number-up-default"));
3148 cupsArrayAdd(CommonDefaults,
3149 _cupsStrAlloc("orientation-requested-default"));
f7deaa1a 3150 }
3151
b423cd4c 3152 /*
3153 * Add all of the default options from the .conf files...
3154 */
3155
1f0275e3 3156 for (num_options = 0, options = NULL, i = p->num_options, option = p->options;
b423cd4c 3157 i > 0;
3158 i --, option ++)
3159 {
3160 if (strcmp(option->name, "ipp-options") &&
3161 strcmp(option->name, "job-sheets") &&
3162 strcmp(option->name, "lease-duration"))
3163 {
3164 snprintf(name, sizeof(name), "%s-default", option->name);
3165 num_options = cupsAddOption(name, option->value, num_options, &options);
f7deaa1a 3166
3167 if (!cupsArrayFind(CommonDefaults, name))
3168 cupsArrayAdd(CommonDefaults, _cupsStrAlloc(name));
b423cd4c 3169 }
3170 }
3171
3172 /*
3173 * Convert options to IPP attributes...
3174 */
3175
3176 cupsEncodeOptions2(p->attrs, num_options, options, IPP_TAG_PRINTER);
3177 cupsFreeOptions(num_options, options);
3178
3179 /*
3180 * Add standard -default attributes as needed...
3181 */
3182
3183 if (!cupsGetOption("copies", p->num_options, p->options))
3184 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER, "copies-default",
3185 1);
3186
f7deaa1a 3187 if (!cupsGetOption("document-format", p->num_options, p->options))
c934a06c 3188 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_MIMETYPE,
f7deaa1a 3189 "document-format-default", NULL, "application/octet-stream");
3190
ebaac3df
MS
3191 if (!cupsGetOption("job-cancel-after", p->num_options, p->options))
3192 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
3193 "job-cancel-after-default", MaxJobTime);
3194
b423cd4c 3195 if (!cupsGetOption("job-hold-until", p->num_options, p->options))
3196 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
3197 "job-hold-until-default", NULL, "no-hold");
3198
3199 if (!cupsGetOption("job-priority", p->num_options, p->options))
3200 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
3201 "job-priority-default", 50);
3202
3203 if (!cupsGetOption("number-up", p->num_options, p->options))
3204 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
3205 "number-up-default", 1);
3206
f7deaa1a 3207 if (!cupsGetOption("notify-lease-duration", p->num_options, p->options))
3208 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
3209 "notify-lease-duration-default", DefaultLeaseDuration);
3210
3211 if (!cupsGetOption("notify-events", p->num_options, p->options))
3212 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
3213 "notify-events-default", NULL, "job-completed");
ba55dc12
MS
3214
3215 if (!cupsGetOption("orientation-requested", p->num_options, p->options))
3216 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NOVALUE,
3217 "orientation-requested-default", NULL, NULL);
3218
3219 if (!cupsGetOption("print-quality", p->num_options, p->options))
3220 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_ENUM,
3221 "print-quality-default", IPP_QUALITY_NORMAL);
b423cd4c 3222}
3223
3224
bd7854cb 3225/*
3226 * 'add_printer_filter()' - Add a MIME filter for a printer.
3227 */
3228
3229static void
3230add_printer_filter(
3231 cupsd_printer_t *p, /* I - Printer to add to */
f7deaa1a 3232 mime_type_t *filtertype, /* I - Filter or prefilter MIME type */
bd7854cb 3233 const char *filter) /* I - Filter to add */
3234{
3235 char super[MIME_MAX_SUPER], /* Super-type for filter */
3236 type[MIME_MAX_TYPE], /* Type for filter */
c8fef167
MS
3237 dsuper[MIME_MAX_SUPER], /* Destination super-type for filter */
3238 dtype[MIME_MAX_TYPE], /* Destination type for filter */
3239 dest[MIME_MAX_SUPER + MIME_MAX_TYPE + 2],
3240 /* Destination super/type */
bd7854cb 3241 program[1024]; /* Program/filter name */
3242 int cost; /* Cost of filter */
07ed0e9a 3243 size_t maxsize = 0; /* Maximum supported file size */
c8fef167
MS
3244 mime_type_t *temptype, /* MIME type looping var */
3245 *desttype; /* Destination MIME type */
22c9029b
MS
3246 mime_filter_t *filterptr; /* MIME filter */
3247 char filename[1024]; /* Full filter filename */
bd7854cb 3248
3249
f14324a7
MS
3250 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3251 "add_printer_filter(p=%p(%s), filtertype=%p(%s/%s), "
3252 "filter=\"%s\")", p, p->name, filtertype, filtertype->super,
3253 filtertype->type, filter);
3254
bd7854cb 3255 /*
c8fef167 3256 * Parse the filter string; it should be in one of the following formats:
bd7854cb 3257 *
c8fef167 3258 * source/type cost program
07ed0e9a 3259 * source/type cost maxsize(nnnn) program
c8fef167 3260 * source/type dest/type cost program
07ed0e9a 3261 * source/type dest/type cost maxsize(nnnn) program
bd7854cb 3262 */
3263
c8fef167
MS
3264 if (sscanf(filter, "%15[^/]/%255s%*[ \t]%15[^/]/%255s%d%*[ \t]%1023[^\n]",
3265 super, type, dsuper, dtype, &cost, program) == 6)
bd7854cb 3266 {
f14324a7
MS
3267 snprintf(dest, sizeof(dest), "%s/%s/%s", p->name, dsuper, dtype);
3268
3269 if ((desttype = mimeType(MimeDatabase, "printer", dest)) == NULL)
3270 {
3271 desttype = mimeAddType(MimeDatabase, "printer", dest);
3272 if (!p->dest_types)
3273 p->dest_types = cupsArrayNew(NULL, NULL);
3274
3275 cupsArrayAdd(p->dest_types, desttype);
3276 }
3277
c8fef167
MS
3278 }
3279 else
3280 {
3281 if (sscanf(filter, "%15[^/]/%255s%d%*[ \t]%1023[^\n]", super, type, &cost,
3282 program) == 4)
3283 {
f14324a7 3284 desttype = filtertype;
c8fef167
MS
3285 }
3286 else
3287 {
3288 cupsdLogMessage(CUPSD_LOG_ERROR, "%s: invalid filter string \"%s\"!",
3289 p->name, filter);
3290 return;
3291 }
bd7854cb 3292 }
3293
07ed0e9a
MS
3294 if (!strncmp(program, "maxsize(", 8))
3295 {
3296 char *ptr; /* Pointer into maxsize(nnnn) program */
3297
7e86f2f6 3298 maxsize = (size_t)strtoll(program + 8, &ptr, 10);
07ed0e9a
MS
3299
3300 if (*ptr != ')')
3301 {
3302 cupsdLogMessage(CUPSD_LOG_ERROR, "%s: invalid filter string \"%s\"!",
3303 p->name, filter);
3304 return;
3305 }
3306
3307 ptr ++;
3308 while (_cups_isspace(*ptr))
3309 ptr ++;
3310
3311 _cups_strcpy(program, ptr);
3312 }
3313
bd7854cb 3314 /*
22c9029b 3315 * Check permissions on the filter and its containing directory...
bd7854cb 3316 */
3317
ecdc0628 3318 if (strcmp(program, "-"))
bd7854cb 3319 {
ecdc0628 3320 if (program[0] == '/')
3321 strlcpy(filename, program, sizeof(filename));
3322 else
3323 snprintf(filename, sizeof(filename), "%s/filter/%s", ServerBin, program);
3324
88f9aafc
MS
3325 _cupsFileCheck(filename, _CUPS_FILE_CHECK_PROGRAM, !RunUser,
3326 cupsdLogFCMessage, p);
bd7854cb 3327 }
3328
3329 /*
3330 * Add the filter to the MIME database, supporting wildcards as needed...
3331 */
3332
3333 for (temptype = mimeFirstType(MimeDatabase);
3334 temptype;
3335 temptype = mimeNextType(MimeDatabase))
88f9aafc
MS
3336 if (((super[0] == '*' && _cups_strcasecmp(temptype->super, "printer")) ||
3337 !_cups_strcasecmp(temptype->super, super)) &&
3338 (type[0] == '*' || !_cups_strcasecmp(temptype->type, type)))
bd7854cb 3339 {
c8fef167
MS
3340 if (desttype != filtertype)
3341 {
3342 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3343 "add_printer_filter: %s: adding filter %s/%s %s/%s %d "
3344 "%s", p->name, temptype->super, temptype->type,
3345 desttype->super, desttype->type,
3346 cost, program);
22c9029b
MS
3347 filterptr = mimeAddFilter(MimeDatabase, temptype, desttype, cost,
3348 program);
c8fef167
MS
3349
3350 if (!mimeFilterLookup(MimeDatabase, desttype, filtertype))
3351 {
3352 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3353 "add_printer_filter: %s: adding filter %s/%s %s/%s "
3354 "0 -", p->name, desttype->super, desttype->type,
3355 filtertype->super, filtertype->type);
771bd8cb 3356 mimeAddFilter(MimeDatabase, desttype, filtertype, 0, "-");
c8fef167
MS
3357 }
3358 }
3359 else
3360 {
3361 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3362 "add_printer_filter: %s: adding filter %s/%s %s/%s %d "
3363 "%s", p->name, temptype->super, temptype->type,
3364 filtertype->super, filtertype->type,
3365 cost, program);
22c9029b
MS
3366 filterptr = mimeAddFilter(MimeDatabase, temptype, filtertype, cost,
3367 program);
c8fef167 3368 }
22c9029b
MS
3369
3370 if (filterptr)
3371 filterptr->maxsize = maxsize;
bd7854cb 3372 }
3373}
3374
3375
3376/*
3377 * 'add_printer_formats()' - Add document-format-supported values for a printer.
3378 */
3379
3380static void
3381add_printer_formats(cupsd_printer_t *p) /* I - Printer */
3382{
3383 int i; /* Looping var */
3384 mime_type_t *type; /* Current MIME type */
3385 cups_array_t *filters; /* Filters */
80ca4592 3386 ipp_attribute_t *attr; /* document-format-supported attribute */
bd7854cb 3387 char mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2];
3388 /* MIME type name */
3389
3390
3391 /*
3392 * Raw (and remote) queues advertise all of the supported MIME
3393 * types...
3394 */
3395
80ca4592 3396 cupsArrayDelete(p->filetypes);
3397 p->filetypes = NULL;
3398
bd7854cb 3399 if (p->raw)
3400 {
3401 ippAddStrings(p->attrs, IPP_TAG_PRINTER,
3402 (ipp_tag_t)(IPP_TAG_MIMETYPE | IPP_TAG_COPY),
3403 "document-format-supported", NumMimeTypes, NULL, MimeTypes);
3404 return;
3405 }
3406
3407 /*
3408 * Otherwise, loop through the supported MIME types and see if there
3409 * are filters for them...
3410 */
3411
bd7854cb 3412 cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_printer_formats: %d types, %d filters",
3413 mimeNumTypes(MimeDatabase), mimeNumFilters(MimeDatabase));
3414
80ca4592 3415 p->filetypes = cupsArrayNew(NULL, NULL);
bd7854cb 3416
80ca4592 3417 for (type = mimeFirstType(MimeDatabase);
bd7854cb 3418 type;
3419 type = mimeNextType(MimeDatabase))
3420 {
88f9aafc 3421 if (!_cups_strcasecmp(type->super, "printer"))
c8fef167
MS
3422 continue;
3423
bd7854cb 3424 snprintf(mimetype, sizeof(mimetype), "%s/%s", type->super, type->type);
3425
3426 if ((filters = mimeFilter(MimeDatabase, type, p->filetype, NULL)) != NULL)
3427 {
3428 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3429 "add_printer_formats: %s: %s needs %d filters",
3430 p->name, mimetype, cupsArrayCount(filters));
3431
3432 cupsArrayDelete(filters);
80ca4592 3433 cupsArrayAdd(p->filetypes, type);
bd7854cb 3434 }
3435 else
3436 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3437 "add_printer_formats: %s: %s not supported",
3438 p->name, mimetype);
3439 }
3440
bd7854cb 3441 /*
3442 * Add the file formats that can be filtered...
3443 */
3444
f301802f 3445 if ((type = mimeType(MimeDatabase, "application", "octet-stream")) == NULL ||
3446 !cupsArrayFind(p->filetypes, type))
3447 i = 1;
3448 else
3449 i = 0;
bd7854cb 3450
7a0cbd5e
MS
3451 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3452 "add_printer_formats: %s: %d supported types",
3453 p->name, cupsArrayCount(p->filetypes) + i);
3454
80ca4592 3455 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_MIMETYPE,
3456 "document-format-supported",
7a0cbd5e 3457 cupsArrayCount(p->filetypes) + i, NULL, NULL);
80ca4592 3458
f301802f 3459 if (i)
3460 attr->values[0].string.text = _cupsStrAlloc("application/octet-stream");
bd7854cb 3461
f301802f 3462 for (type = (mime_type_t *)cupsArrayFirst(p->filetypes);
80ca4592 3463 type;
3464 i ++, type = (mime_type_t *)cupsArrayNext(p->filetypes))
3465 {
3466 snprintf(mimetype, sizeof(mimetype), "%s/%s", type->super, type->type);
bd7854cb 3467
80ca4592 3468 attr->values[i].string.text = _cupsStrAlloc(mimetype);
3469 }
f7deaa1a 3470
f3c17241 3471#if defined(HAVE_DNSSD) || defined(HAVE_AVAHI)
f7deaa1a 3472 {
3473 char pdl[1024]; /* Buffer to build pdl list */
3474 mime_filter_t *filter; /* MIME filter looping var */
3475
3476
f7deaa1a 3477 /*
ba55dc12
MS
3478 * We only support raw printing if this is not a Tioga PrintJobMgr based
3479 * queue and if application/octet-stream is a known type...
f7deaa1a 3480 */
3481
3482 for (filter = (mime_filter_t *)cupsArrayFirst(MimeDatabase->filters);
3483 filter;
3484 filter = (mime_filter_t *)cupsArrayNext(MimeDatabase->filters))
3485 {
5a662dc0 3486 if (filter->dst == p->filetype && filter->filter &&
f7deaa1a 3487 strstr(filter->filter, "PrintJobMgr"))
3488 break;
3489 }
3490
ba55dc12 3491 pdl[0] = '\0';
f7deaa1a 3492
3493 if (!filter && mimeType(MimeDatabase, "application", "octet-stream"))
3494 strlcat(pdl, "application/octet-stream,", sizeof(pdl));
3495
ba55dc12
MS
3496 /*
3497 * Then list a bunch of formats that are supported by the printer...
3498 */
3499
3500 for (type = (mime_type_t *)cupsArrayFirst(p->filetypes);
3501 type;
3502 type = (mime_type_t *)cupsArrayNext(p->filetypes))
3503 {
88f9aafc 3504 if (!_cups_strcasecmp(type->super, "application"))
ba55dc12 3505 {
88f9aafc 3506 if (!_cups_strcasecmp(type->type, "pdf"))
ba55dc12 3507 strlcat(pdl, "application/pdf,", sizeof(pdl));
88f9aafc 3508 else if (!_cups_strcasecmp(type->type, "postscript"))
ba55dc12
MS
3509 strlcat(pdl, "application/postscript,", sizeof(pdl));
3510 }
88f9aafc 3511 else if (!_cups_strcasecmp(type->super, "image"))
ba55dc12 3512 {
88f9aafc 3513 if (!_cups_strcasecmp(type->type, "jpeg"))
ba55dc12 3514 strlcat(pdl, "image/jpeg,", sizeof(pdl));
88f9aafc 3515 else if (!_cups_strcasecmp(type->type, "png"))
ba55dc12 3516 strlcat(pdl, "image/png,", sizeof(pdl));
88f9aafc 3517 else if (!_cups_strcasecmp(type->type, "pwg-raster"))
c8fef167 3518 strlcat(pdl, "image/pwg-raster,", sizeof(pdl));
ba55dc12
MS
3519 }
3520 }
f7deaa1a 3521
3522 if (pdl[0])
3523 pdl[strlen(pdl) - 1] = '\0'; /* Remove trailing comma */
3524
3525 cupsdSetString(&p->pdl, pdl);
3526 }
f3c17241 3527#endif /* HAVE_DNSSD || HAVE_AVAHI */
bd7854cb 3528}
3529
3530
ef416fc2 3531/*
3532 * 'compare_printers()' - Compare two printers.
3533 */
3534
3535static int /* O - Result of comparison */
3536compare_printers(void *first, /* I - First printer */
3537 void *second, /* I - Second printer */
3538 void *data) /* I - App data (not used) */
3539{
321d8d57
MS
3540 (void)data;
3541
88f9aafc 3542 return (_cups_strcasecmp(((cupsd_printer_t *)first)->name,
ef416fc2 3543 ((cupsd_printer_t *)second)->name));
3544}
3545
3546
bd7854cb 3547/*
e1d6a774 3548 * 'delete_printer_filters()' - Delete all MIME filters for a printer.
bd7854cb 3549 */
3550
3551static void
e1d6a774 3552delete_printer_filters(
3553 cupsd_printer_t *p) /* I - Printer to remove from */
bd7854cb 3554{
e1d6a774 3555 mime_filter_t *filter; /* MIME filter looping var */
c8fef167 3556 mime_type_t *type; /* Destination types for filters */
bd7854cb 3557
bd7854cb 3558
3559 /*
e1d6a774 3560 * Range check input...
bd7854cb 3561 */
3562
e1d6a774 3563 if (p == NULL)
3564 return;
bd7854cb 3565
3566 /*
e1d6a774 3567 * Remove all filters from the MIME database that have a destination
3568 * type == printer...
bd7854cb 3569 */
3570
e1d6a774 3571 for (filter = mimeFirstFilter(MimeDatabase);
3572 filter;
3573 filter = mimeNextFilter(MimeDatabase))
c8fef167
MS
3574 if (filter->dst == p->filetype || filter->dst == p->prefiltertype ||
3575 cupsArrayFind(p->dest_types, filter->dst))
e1d6a774 3576 {
3577 /*
3578 * Delete the current filter...
3579 */
bd7854cb 3580
e1d6a774 3581 mimeDeleteFilter(MimeDatabase, filter);
3582 }
b9faaae1 3583
c8fef167
MS
3584 for (type = (mime_type_t *)cupsArrayFirst(p->dest_types);
3585 type;
3586 type = (mime_type_t *)cupsArrayNext(p->dest_types))
3587 mimeDeleteType(MimeDatabase, type);
3588
3589 cupsArrayDelete(p->dest_types);
3590 p->dest_types = NULL;
3591
38e73f87
MS
3592 cupsdSetPrinterReasons(p, "-cups-insecure-filter-warning"
3593 ",cups-missing-filter-warning");
bd7854cb 3594}
3595
3596
0268488e
MS
3597/*
3598 * 'dirty_printer()' - Mark config and state files dirty for the specified
3599 * printer.
3600 */
3601
3602static void
3603dirty_printer(cupsd_printer_t *p) /* I - Printer */
3604{
a2326b5b 3605 if (p->type & CUPS_PRINTER_CLASS)
0268488e
MS
3606 cupsdMarkDirty(CUPSD_DIRTY_CLASSES);
3607 else
3608 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
3609
3610 if (PrintcapFormat == PRINTCAP_PLIST)
3611 cupsdMarkDirty(CUPSD_DIRTY_PRINTCAP);
3612}
3613
3614
61cf44e2
MS
3615/*
3616 * 'load_ppd()' - Load a cached PPD file, updating the cache as needed.
3617 */
3618
3619static void
3620load_ppd(cupsd_printer_t *p) /* I - Printer */
3621{
54afec33 3622 int i, j, k; /* Looping vars */
f14324a7
MS
3623 char cache_name[1024]; /* Cache filename */
3624 struct stat cache_info; /* Cache file info */
61cf44e2
MS
3625 ppd_file_t *ppd; /* PPD file */
3626 char ppd_name[1024]; /* PPD filename */
3627 struct stat ppd_info; /* PPD file info */
3628 int num_media; /* Number of media options */
54afec33 3629 ppd_size_t *size; /* Current PPD size */
5a6b583a
MS
3630 ppd_option_t *duplex, /* Duplex option */
3631 *output_bin, /* OutputBin option */
ba55dc12 3632 *output_mode, /* OutputMode option */
5a6b583a 3633 *resolution; /* (Set|JCL|)Resolution option */
54afec33
MS
3634 ppd_choice_t *choice, /* Current PPD choice */
3635 *input_slot, /* Current input slot */
3636 *media_type; /* Current media type */
61cf44e2 3637 ppd_attr_t *ppd_attr; /* PPD attribute */
5a6b583a
MS
3638 int xdpi, /* Horizontal resolution */
3639 ydpi; /* Vertical resolution */
3640 const char *resptr; /* Pointer into resolution keyword */
54afec33
MS
3641 _pwg_size_t *pwgsize; /* Current PWG size */
3642 _pwg_map_t *pwgsource, /* Current PWG source */
3643 *pwgtype; /* Current PWG type */
61cf44e2 3644 ipp_attribute_t *attr; /* Attribute data */
a2326b5b 3645 _ipp_value_t *val; /* Attribute value */
ba55dc12
MS
3646 int num_finishings, /* Number of finishings */
3647 finishings[5]; /* finishings-supported values */
3648 int num_qualities, /* Number of print-quality values */
3649 qualities[3]; /* print-quality values */
54afec33
MS
3650 int num_margins, /* Number of media-*-margin-supported values */
3651 margins[16]; /* media-*-margin-supported values */
5a9febac
MS
3652 const char *filter, /* Current filter */
3653 *mandatory; /* Current mandatory attribute */
61cf44e2
MS
3654 static const char * const sides[3] = /* sides-supported values */
3655 {
3656 "one-sided",
3657 "two-sided-long-edge",
3658 "two-sided-short-edge"
3659 };
3660 static const char * const standard_commands[] =
3661 { /* Standard CUPS commands */
3662 "AutoConfigure",
3663 "Clean",
c168a833 3664 "PrintSelfTestPage"
61cf44e2
MS
3665 };
3666
3667
3668 /*
3669 * Check to see if the cache is up-to-date...
3670 */
3671
f14324a7 3672 snprintf(cache_name, sizeof(cache_name), "%s/%s.data", CacheDir, p->name);
61cf44e2
MS
3673 if (stat(cache_name, &cache_info))
3674 cache_info.st_mtime = 0;
3675
3676 snprintf(ppd_name, sizeof(ppd_name), "%s/ppd/%s.ppd", ServerRoot, p->name);
3677 if (stat(ppd_name, &ppd_info))
3678 ppd_info.st_mtime = 1;
3679
3680 ippDelete(p->ppd_attrs);
22c9029b 3681 p->ppd_attrs = NULL;
61cf44e2 3682
f14324a7
MS
3683 _ppdCacheDestroy(p->pc);
3684 p->pc = NULL;
54afec33 3685
2644cfc5
MS
3686 cupsdClearString(&(p->make_model));
3687
f14324a7 3688 if (cache_info.st_mtime >= ppd_info.st_mtime)
61cf44e2 3689 {
61cf44e2
MS
3690 cupsdLogMessage(CUPSD_LOG_DEBUG, "load_ppd: Loading %s...", cache_name);
3691
f14324a7
MS
3692 if ((p->pc = _ppdCacheCreateWithFile(cache_name, &p->ppd_attrs)) != NULL &&
3693 p->ppd_attrs)
61cf44e2 3694 {
f14324a7
MS
3695 /*
3696 * Loaded successfully!
3697 */
3698
61cf44e2
MS
3699 return;
3700 }
61cf44e2
MS
3701 }
3702
3703 /*
3704 * Reload PPD attributes from disk...
3705 */
3706
8b450588
MS
3707 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
3708
61cf44e2
MS
3709 cupsdLogMessage(CUPSD_LOG_DEBUG, "load_ppd: Loading %s...", ppd_name);
3710
7e86f2f6 3711 p->type &= (cups_ptype_t)~CUPS_PRINTER_OPTIONS;
61cf44e2
MS
3712 p->type |= CUPS_PRINTER_BW;
3713
3714 finishings[0] = IPP_FINISHINGS_NONE;
3715 num_finishings = 1;
3716
22c9029b
MS
3717 p->ppd_attrs = ippNew();
3718
9c80ffa2 3719 if ((ppd = _ppdOpenFile(ppd_name, _PPD_LOCALIZATION_NONE)) != NULL)
61cf44e2
MS
3720 {
3721 /*
3722 * Add make/model and other various attributes...
3723 */
3724
f14324a7 3725 p->pc = _ppdCacheCreateWithPPD(ppd);
54afec33 3726
5a9febac
MS
3727 if (!p->pc)
3728 cupsdLogMessage(CUPSD_LOG_WARN, "Unable to create cache of \"%s\": %s",
3729 ppd_name, cupsLastErrorString());
3730
c168a833
MS
3731 ppdMarkDefaults(ppd);
3732
61cf44e2
MS
3733 if (ppd->color_device)
3734 p->type |= CUPS_PRINTER_COLOR;
3735 if (ppd->variable_sizes)
3736 p->type |= CUPS_PRINTER_VARIABLE;
3737 if (!ppd->manual_copies)
3738 p->type |= CUPS_PRINTER_COPIES;
3739 if ((ppd_attr = ppdFindAttr(ppd, "cupsFax", NULL)) != NULL)
88f9aafc 3740 if (ppd_attr->value && !_cups_strcasecmp(ppd_attr->value, "true"))
61cf44e2
MS
3741 p->type |= CUPS_PRINTER_FAX;
3742
7e86f2f6 3743 ippAddBoolean(p->ppd_attrs, IPP_TAG_PRINTER, "color-supported", (char)ppd->color_device);
5a9febac 3744
a469f8a5
MS
3745 if (p->pc && p->pc->charge_info_uri)
3746 ippAddString(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_URI,
3747 "printer-charge-info-uri", NULL, p->pc->charge_info_uri);
3748
5a9febac
MS
3749 if (p->pc && p->pc->account_id)
3750 ippAddBoolean(p->ppd_attrs, IPP_TAG_PRINTER, "job-account-id-supported",
3751 1);
3752
3753 if (p->pc && p->pc->accounting_user_id)
3754 ippAddBoolean(p->ppd_attrs, IPP_TAG_PRINTER,
3755 "job-accounting-user-id-supported", 1);
3756
3757 if (p->pc && p->pc->password)
3758 {
3759 ippAddString(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
3760 "job-password-encryption-supported", NULL, "none");
3761 ippAddInteger(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
7e86f2f6 3762 "job-password-supported", (int)strlen(p->pc->password));
5a9febac
MS
3763 }
3764
61cf44e2 3765 if (ppd->throughput)
ba55dc12 3766 {
61cf44e2
MS
3767 ippAddInteger(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
3768 "pages-per-minute", ppd->throughput);
ba55dc12
MS
3769 if (ppd->color_device)
3770 ippAddInteger(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
3771 "pages-per-minute-color", ppd->throughput);
3772 }
e60ec91f
MS
3773 else
3774 {
3775 /*
3776 * When there is no speed information, just say "1 page per minute".
3777 */
3778
3779 ippAddInteger(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
3780 "pages-per-minute", 1);
3781 if (ppd->color_device)
3782 ippAddInteger(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
3783 "pages-per-minute-color", 1);
3784 }
ba55dc12
MS
3785
3786 num_qualities = 0;
3787
3788 if ((output_mode = ppdFindOption(ppd, "OutputMode")) != NULL)
3789 {
3790 if (ppdFindChoice(output_mode, "draft") ||
3791 ppdFindChoice(output_mode, "fast"))
3792 qualities[num_qualities ++] = IPP_QUALITY_DRAFT;
f14324a7
MS
3793
3794 qualities[num_qualities ++] = IPP_QUALITY_NORMAL;
3795
ba55dc12
MS
3796 if (ppdFindChoice(output_mode, "best") ||
3797 ppdFindChoice(output_mode, "high"))
3798 qualities[num_qualities ++] = IPP_QUALITY_HIGH;
3799 }
3800 else if ((ppd_attr = ppdFindAttr(ppd, "APPrinterPreset", NULL)) != NULL)
3801 {
3802 do
3803 {
3804 if (strstr(ppd_attr->spec, "draft") ||
3805 strstr(ppd_attr->spec, "Draft"))
3806 {
3807 qualities[num_qualities ++] = IPP_QUALITY_DRAFT;
3808 break;
3809 }
3810 }
3811 while ((ppd_attr = ppdFindNextAttr(ppd, "APPrinterPreset",
3812 NULL)) != NULL);
3813
3814 qualities[num_qualities ++] = IPP_QUALITY_NORMAL;
3815 qualities[num_qualities ++] = IPP_QUALITY_HIGH;
3816 }
f14324a7 3817 else
ba55dc12
MS
3818 qualities[num_qualities ++] = IPP_QUALITY_NORMAL;
3819
3820 ippAddIntegers(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_ENUM,
3821 "print-quality-supported", num_qualities, qualities);
61cf44e2
MS
3822
3823 if (ppd->nickname)
3824 {
3825 /*
3826 * The NickName can be localized in the character set specified
3827 * by the LanugageEncoding attribute. However, ppdOpen2() has
3828 * already converted the ppd->nickname member to UTF-8 for us
3829 * (the original attribute value is available separately)
3830 */
3831
3832 cupsdSetString(&p->make_model, ppd->nickname);
3833 }
3834 else if (ppd->modelname)
3835 {
3836 /*
3837 * Model name can only contain specific characters...
3838 */
3839
3840 cupsdSetString(&p->make_model, ppd->modelname);
3841 }
3842 else
3843 cupsdSetString(&p->make_model, "Bad PPD File");
3844
3845 ippAddString(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
3846 "printer-make-and-model", NULL, p->make_model);
3847
3848 /*
3849 * Add media options from the PPD file...
3850 */
3851
f14324a7 3852 if (ppd->num_sizes == 0 || !p->pc)
61cf44e2 3853 {
b9faaae1
MS
3854 if (!ppdFindAttr(ppd, "APScannerOnly", NULL))
3855 cupsdLogMessage(CUPSD_LOG_CRIT,
3856 "The PPD file for printer %s contains no media "
3857 "options and is therefore invalid!", p->name);
3858
54afec33
MS
3859 ippAddString(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
3860 "media-default", NULL, "unknown");
b9faaae1
MS
3861 ippAddString(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
3862 "media-supported", NULL, "unknown");
61cf44e2
MS
3863 }
3864 else
3865 {
54afec33
MS
3866 /*
3867 * media-default
3868 */
c168a833 3869
54afec33 3870 if ((size = ppdPageSize(ppd, NULL)) != NULL)
f14324a7 3871 pwgsize = _ppdCacheGetSize(p->pc, size->name);
54afec33
MS
3872 else
3873 pwgsize = NULL;
3874
3875 ippAddString(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
3876 "media-default", NULL,
3877 pwgsize ? pwgsize->map.pwg : "unknown");
3878
3879 /*
3880 * media-col-default
3881 */
3882
3883 if (pwgsize)
3884 {
aaf19ab0
MS
3885 ipp_t *col; /* Collection value */
3886
54afec33
MS
3887 input_slot = ppdFindMarkedChoice(ppd, "InputSlot");
3888 media_type = ppdFindMarkedChoice(ppd, "MediaType");
aaf19ab0
MS
3889 col = new_media_col(pwgsize,
3890 input_slot ?
f14324a7
MS
3891 _ppdCacheGetSource(p->pc,
3892 input_slot->choice) :
aaf19ab0
MS
3893 NULL,
3894 media_type ?
f14324a7
MS
3895 _ppdCacheGetType(p->pc,
3896 media_type->choice) :
cc754834 3897 NULL);
54afec33
MS
3898
3899 ippAddCollection(p->ppd_attrs, IPP_TAG_PRINTER, "media-col-default",
aaf19ab0
MS
3900 col);
3901 ippDelete(col);
54afec33
MS
3902 }
3903
3904 /*
3905 * media-supported
3906 */
3907
f14324a7
MS
3908 num_media = p->pc->num_sizes;
3909 if (p->pc->custom_min_keyword)
54afec33
MS
3910 num_media += 2;
3911
3912 if ((attr = ippAddStrings(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
3913 "media-supported", num_media, NULL,
3914 NULL)) != NULL)
61cf44e2
MS
3915 {
3916 val = attr->values;
3917
f14324a7 3918 for (i = p->pc->num_sizes, pwgsize = p->pc->sizes;
54afec33
MS
3919 i > 0;
3920 i --, pwgsize ++, val ++)
6961465f 3921 val->string.text = _cupsStrAlloc(pwgsize->map.pwg);
54afec33 3922
f14324a7 3923 if (p->pc->custom_min_keyword)
54afec33 3924 {
6961465f 3925 val->string.text = _cupsStrAlloc(p->pc->custom_min_keyword);
54afec33 3926 val ++;
6961465f 3927 val->string.text = _cupsStrAlloc(p->pc->custom_max_keyword);
54afec33
MS
3928 }
3929 }
3930
a29fd7dd
MS
3931 /*
3932 * media-size-supported
3933 */
3934
3935 num_media = p->pc->num_sizes;
3936 if (p->pc->custom_min_keyword)
3937 num_media ++;
3938
3939 if ((attr = ippAddCollections(p->ppd_attrs, IPP_TAG_PRINTER,
3940 "media-size-supported", num_media,
3941 NULL)) != NULL)
3942 {
3943 val = attr->values;
3944
3945 for (i = p->pc->num_sizes, pwgsize = p->pc->sizes;
3946 i > 0;
3947 i --, pwgsize ++, val ++)
3948 {
3949 val->collection = ippNew();
3950 ippAddInteger(val->collection, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
3951 "x-dimension", pwgsize->width);
3952 ippAddInteger(val->collection, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
3953 "y-dimension", pwgsize->length);
3954 }
3955
3956 if (p->pc->custom_min_keyword)
3957 {
3958 val->collection = ippNew();
3959 ippAddRange(val->collection, IPP_TAG_PRINTER, "x-dimension",
3960 p->pc->custom_min_width, p->pc->custom_max_width);
3961 ippAddRange(val->collection, IPP_TAG_PRINTER, "y-dimension",
3962 p->pc->custom_min_length, p->pc->custom_max_length);
3963 }
3964 }
3965
54afec33
MS
3966 /*
3967 * media-source-supported
3968 */
3969
f14324a7 3970 if (p->pc->num_sources > 0 &&
54afec33 3971 (attr = ippAddStrings(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
f14324a7 3972 "media-source-supported", p->pc->num_sources,
54afec33
MS
3973 NULL, NULL)) != NULL)
3974 {
f14324a7 3975 for (i = p->pc->num_sources, pwgsource = p->pc->sources,
54afec33
MS
3976 val = attr->values;
3977 i > 0;
3978 i --, pwgsource ++, val ++)
6961465f 3979 val->string.text = _cupsStrAlloc(pwgsource->pwg);
54afec33
MS
3980 }
3981
3982 /*
3983 * media-type-supported
3984 */
3985
f14324a7 3986 if (p->pc->num_types > 0 &&
54afec33 3987 (attr = ippAddStrings(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
f14324a7 3988 "media-type-supported", p->pc->num_types,
54afec33
MS
3989 NULL, NULL)) != NULL)
3990 {
f14324a7 3991 for (i = p->pc->num_types, pwgtype = p->pc->types,
54afec33
MS
3992 val = attr->values;
3993 i > 0;
3994 i --, pwgtype ++, val ++)
6961465f 3995 val->string.text = _cupsStrAlloc(pwgtype->pwg);
54afec33
MS
3996 }
3997
3998 /*
3999 * media-*-margin-supported
4000 */
4001
f14324a7 4002 for (i = p->pc->num_sizes, pwgsize = p->pc->sizes, num_margins = 0;
54afec33
MS
4003 i > 0 && num_margins < (int)(sizeof(margins) / sizeof(margins[0]));
4004 i --, pwgsize ++)
4005 {
4006 for (j = 0; j < num_margins; j ++)
4007 if (pwgsize->bottom == margins[j])
4008 break;
4009
4010 if (j >= num_margins)
4011 {
4012 margins[num_margins] = pwgsize->bottom;
4013 num_margins ++;
4014 }
4015 }
4016
4017 if (num_margins > 0)
4018 ippAddIntegers(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
4019 "media-bottom-margin-supported", num_margins, margins);
4020 else
4021 ippAddInteger(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
4022 "media-bottom-margin-supported", 0);
4023
f14324a7 4024 for (i = p->pc->num_sizes, pwgsize = p->pc->sizes, num_margins = 0;
54afec33
MS
4025 i > 0 && num_margins < (int)(sizeof(margins) / sizeof(margins[0]));
4026 i --, pwgsize ++)
4027 {
4028 for (j = 0; j < num_margins; j ++)
4029 if (pwgsize->left == margins[j])
4030 break;
4031
4032 if (j >= num_margins)
c168a833 4033 {
54afec33
MS
4034 margins[num_margins] = pwgsize->left;
4035 num_margins ++;
4036 }
4037 }
4038
4039 if (num_margins > 0)
4040 ippAddIntegers(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
4041 "media-left-margin-supported", num_margins, margins);
4042 else
4043 ippAddInteger(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
4044 "media-left-margin-supported", 0);
4045
f14324a7 4046 for (i = p->pc->num_sizes, pwgsize = p->pc->sizes, num_margins = 0;
54afec33
MS
4047 i > 0 && num_margins < (int)(sizeof(margins) / sizeof(margins[0]));
4048 i --, pwgsize ++)
4049 {
4050 for (j = 0; j < num_margins; j ++)
4051 if (pwgsize->right == margins[j])
4052 break;
4053
4054 if (j >= num_margins)
4055 {
4056 margins[num_margins] = pwgsize->right;
4057 num_margins ++;
4058 }
4059 }
4060
4061 if (num_margins > 0)
4062 ippAddIntegers(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
4063 "media-right-margin-supported", num_margins, margins);
4064 else
4065 ippAddInteger(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
4066 "media-right-margin-supported", 0);
4067
f14324a7 4068 for (i = p->pc->num_sizes, pwgsize = p->pc->sizes, num_margins = 0;
54afec33
MS
4069 i > 0 && num_margins < (int)(sizeof(margins) / sizeof(margins[0]));
4070 i --, pwgsize ++)
4071 {
4072 for (j = 0; j < num_margins; j ++)
4073 if (pwgsize->top == margins[j])
4074 break;
4075
4076 if (j >= num_margins)
4077 {
4078 margins[num_margins] = pwgsize->top;
4079 num_margins ++;
4080 }
4081 }
4082
4083 if (num_margins > 0)
4084 ippAddIntegers(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
4085 "media-top-margin-supported", num_margins, margins);
4086 else
4087 ippAddInteger(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
4088 "media-top-margin-supported", 0);
4089
4090 /*
4091 * media-col-database
4092 */
4093
f14324a7
MS
4094 num_media = p->pc->num_sizes;
4095 if (p->pc->num_sources)
54afec33 4096 {
f14324a7
MS
4097 if (p->pc->num_types > 0)
4098 num_media += p->pc->num_sizes * p->pc->num_sources *
4099 p->pc->num_types;
54afec33 4100 else
f14324a7 4101 num_media += p->pc->num_sizes * p->pc->num_sources;
54afec33 4102 }
f14324a7
MS
4103 else if (p->pc->num_types)
4104 num_media += p->pc->num_sizes * p->pc->num_types;
54afec33
MS
4105
4106 if ((attr = ippAddCollections(p->ppd_attrs, IPP_TAG_PRINTER,
4107 "media-col-database", num_media,
4108 NULL)) != NULL)
4109 {
f14324a7 4110 for (i = p->pc->num_sizes, pwgsize = p->pc->sizes, val = attr->values;
54afec33
MS
4111 i > 0;
4112 i --, pwgsize ++)
4113 {
4114 /*
4115 * Start by adding the page size without source or type...
4116 */
4117
4118 ppdMarkOption(ppd, "PageSize", pwgsize->map.ppd);
4119
4120 val->collection = new_media_col(pwgsize, NULL, NULL);
4121 val ++;
4122
4123 /*
4124 * Then add the specific, supported combinations of size, source, and
4125 * type...
4126 */
4127
f14324a7 4128 if (p->pc->num_sources > 0)
c168a833 4129 {
f14324a7 4130 for (j = p->pc->num_sources, pwgsource = p->pc->sources;
54afec33
MS
4131 j > 0;
4132 j --, pwgsource ++)
c168a833 4133 {
54afec33 4134 ppdMarkOption(ppd, "InputSlot", pwgsource->ppd);
61cf44e2 4135
f14324a7 4136 if (p->pc->num_types > 0)
54afec33 4137 {
f14324a7 4138 for (k = p->pc->num_types, pwgtype = p->pc->types;
54afec33
MS
4139 k > 0;
4140 k --, pwgtype ++)
4141 {
4142 if (!ppdMarkOption(ppd, "MediaType", pwgtype->ppd))
4143 {
4144 val->collection = new_media_col(pwgsize, pwgsource->pwg,
4145 pwgtype->pwg);
4146 val ++;
4147 }
4148 }
4149 }
4150 else if (!ppdConflicts(ppd))
4151 {
4152 val->collection = new_media_col(pwgsize, pwgsource->pwg, NULL);
4153 val ++;
4154 }
4155 }
4156 }
f14324a7 4157 else if (p->pc->num_types > 0)
54afec33 4158 {
f14324a7 4159 for (j = p->pc->num_types, pwgtype = p->pc->types;
54afec33
MS
4160 j > 0;
4161 j --, pwgtype ++)
d2354e63 4162 {
54afec33
MS
4163 if (!ppdMarkOption(ppd, "MediaType", pwgtype->ppd))
4164 {
4165 val->collection = new_media_col(pwgsize, NULL, pwgtype->pwg);
4166 val ++;
4167 }
d2354e63 4168 }
c168a833
MS
4169 }
4170 }
61cf44e2 4171
54afec33
MS
4172 /*
4173 * Update the number of media-col-database values...
4174 */
c168a833 4175
54afec33 4176 attr->num_values = val - attr->values;
61cf44e2
MS
4177 }
4178 }
4179
4180 /*
4181 * Output bin...
4182 */
4183
f14324a7 4184 if (p->pc && p->pc->num_bins > 0)
61cf44e2
MS
4185 {
4186 attr = ippAddStrings(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
f14324a7 4187 "output-bin-supported", p->pc->num_bins,
61cf44e2
MS
4188 NULL, NULL);
4189
4190 if (attr != NULL)
4191 {
4192 for (i = 0, val = attr->values;
f14324a7 4193 i < p->pc->num_bins;
61cf44e2 4194 i ++, val ++)
f14324a7 4195 val->string.text = _cupsStrAlloc(p->pc->bins[i].pwg);
61cf44e2 4196 }
c168a833 4197
cc754834
MS
4198 if ((output_bin = ppdFindOption(ppd, "OutputBin")) != NULL)
4199 {
f14324a7
MS
4200 for (i = 0; i < p->pc->num_bins; i ++)
4201 if (!strcmp(p->pc->bins[i].ppd, output_bin->defchoice))
cc754834
MS
4202 break;
4203
f14324a7 4204 if (i >= p->pc->num_bins)
cc754834
MS
4205 i = 0;
4206
4207 ippAddString(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
f14324a7 4208 "output-bin-default", NULL, p->pc->bins[i].pwg);
cc754834
MS
4209 }
4210 else
4211 ippAddString(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
f14324a7 4212 "output-bin-default", NULL, p->pc->bins[0].pwg);
cc754834 4213 }
4220952d 4214 else if (((ppd_attr = ppdFindAttr(ppd, "DefaultOutputOrder",
cc754834 4215 NULL)) != NULL &&
88f9aafc 4216 !_cups_strcasecmp(ppd_attr->value, "Reverse")) ||
7cf5915e 4217 (!ppd_attr && ppd->manufacturer && /* "Compatibility heuristic" */
88f9aafc
MS
4218 (!_cups_strcasecmp(ppd->manufacturer, "epson") ||
4219 !_cups_strcasecmp(ppd->manufacturer, "lexmark"))))
cc754834 4220 {
4220952d
MS
4221 /*
4222 * Report that this printer has a single output bin that leaves pages face
4223 * up.
4224 */
4225
e07d4801 4226 ippAddString(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
cc754834
MS
4227 "output-bin-supported", NULL, "face-up");
4228 ippAddString(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
4229 "output-bin-default", NULL, "face-up");
4230 }
4231 else
4232 {
4233 ippAddString(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
4234 "output-bin-supported", NULL, "face-down");
4235 ippAddString(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
4236 "output-bin-default", NULL, "face-down");
61cf44e2
MS
4237 }
4238
7cf5915e 4239 /*
5a9febac 4240 * print-color-mode...
7cf5915e
MS
4241 */
4242
4243 if (ppd->color_device)
4244 {
5a9febac 4245 static const char * const color_modes[] =
7cf5915e
MS
4246 {
4247 "monochrome",
4248 "color"
4249 };
4250
4251 ippAddStrings(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
5a9febac 4252 "print-color-mode-supported", 2, NULL, color_modes);
f14324a7
MS
4253 ippAddString(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
4254 "print-color-mode-default", NULL, "color");
7cf5915e
MS
4255 }
4256 else
4257 {
f14324a7
MS
4258 ippAddString(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
4259 "print-color-mode-supported", NULL, "monochrome");
4260 ippAddString(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
4261 "print-color-mode-default", NULL, "monochrome");
7cf5915e
MS
4262 }
4263
5a9febac
MS
4264 /*
4265 * Mandatory job attributes, if any...
4266 */
4267
4268 if (p->pc && cupsArrayCount(p->pc->mandatory) > 0)
4269 {
4270 int count = cupsArrayCount(p->pc->mandatory);
4271 /* Number of mandatory attributes */
4272
4273 attr = ippAddStrings(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
4274 "printer-mandatory-job-attributes", count, NULL,
4275 NULL);
4276
4277 for (val = attr->values,
4278 mandatory = (char *)cupsArrayFirst(p->pc->mandatory);
4279 mandatory;
4280 val ++, mandatory = (char *)cupsArrayNext(p->pc->mandatory))
6961465f 4281 val->string.text = _cupsStrAlloc(mandatory);
5a9febac
MS
4282 }
4283
5a6b583a
MS
4284 /*
4285 * Printer resolutions...
4286 */
4287
4288 if ((resolution = ppdFindOption(ppd, "Resolution")) == NULL)
4289 if ((resolution = ppdFindOption(ppd, "JCLResolution")) == NULL)
4290 if ((resolution = ppdFindOption(ppd, "SetResolution")) == NULL)
4291 resolution = ppdFindOption(ppd, "CNRes_PGP");
4292
4293 if (resolution)
4294 {
4295 /*
4296 * Report all supported resolutions...
4297 */
4298
4299 attr = ippAddResolutions(p->ppd_attrs, IPP_TAG_PRINTER,
4300 "printer-resolution-supported",
4301 resolution->num_choices, IPP_RES_PER_INCH,
4302 NULL, NULL);
4303
4304 for (i = 0, choice = resolution->choices;
4305 i < resolution->num_choices;
4306 i ++, choice ++)
4307 {
aaf19ab0
MS
4308 xdpi = ydpi = (int)strtol(choice->choice, (char **)&resptr, 10);
4309 if (resptr > choice->choice && xdpi > 0 && *resptr == 'x')
4310 ydpi = (int)strtol(resptr + 1, (char **)&resptr, 10);
5a6b583a
MS
4311
4312 if (xdpi <= 0 || ydpi <= 0)
4313 {
4314 cupsdLogMessage(CUPSD_LOG_WARN,
4315 "Bad resolution \"%s\" for printer %s.",
4316 choice->choice, p->name);
7cf5915e 4317 xdpi = ydpi = 300;
5a6b583a
MS
4318 }
4319
4320 attr->values[i].resolution.xres = xdpi;
ba55dc12 4321 attr->values[i].resolution.yres = ydpi;
5a6b583a
MS
4322 attr->values[i].resolution.units = IPP_RES_PER_INCH;
4323
4324 if (choice->marked)
4325 ippAddResolution(p->ppd_attrs, IPP_TAG_PRINTER,
4326 "printer-resolution-default", IPP_RES_PER_INCH,
4327 xdpi, ydpi);
4328 }
4329 }
4330 else if ((ppd_attr = ppdFindAttr(ppd, "DefaultResolution", NULL)) != NULL &&
4331 ppd_attr->value)
4332 {
4333 /*
4334 * Just the DefaultResolution to report...
4335 */
4336
39ff2fe7 4337 xdpi = ydpi = (int)strtol(ppd_attr->value, (char **)&resptr, 10);
5a6b583a
MS
4338 if (resptr > ppd_attr->value && xdpi > 0)
4339 {
4340 if (*resptr == 'x')
4341 ydpi = (int)strtol(resptr + 1, (char **)&resptr, 10);
4342 else
4343 ydpi = xdpi;
4344 }
4345
4346 if (xdpi <= 0 || ydpi <= 0)
4347 {
4348 cupsdLogMessage(CUPSD_LOG_WARN,
4349 "Bad default resolution \"%s\" for printer %s.",
4350 ppd_attr->value, p->name);
7cf5915e 4351 xdpi = ydpi = 300;
5a6b583a
MS
4352 }
4353
4354 ippAddResolution(p->ppd_attrs, IPP_TAG_PRINTER,
4355 "printer-resolution-default", IPP_RES_PER_INCH,
4356 xdpi, ydpi);
4357 ippAddResolution(p->ppd_attrs, IPP_TAG_PRINTER,
4358 "printer-resolution-supported", IPP_RES_PER_INCH,
4359 xdpi, ydpi);
4360 }
4361 else
4362 {
4363 /*
4364 * No resolutions in PPD - make one up...
4365 */
4366
4367 ippAddResolution(p->ppd_attrs, IPP_TAG_PRINTER,
4368 "printer-resolution-default", IPP_RES_PER_INCH,
7cf5915e 4369 300, 300);
5a6b583a
MS
4370 ippAddResolution(p->ppd_attrs, IPP_TAG_PRINTER,
4371 "printer-resolution-supported", IPP_RES_PER_INCH,
7cf5915e 4372 300, 300);
5a6b583a
MS
4373 }
4374
61cf44e2
MS
4375 /*
4376 * Duplexing, etc...
4377 */
4378
7cf5915e
MS
4379 ppdMarkDefaults(ppd);
4380
61cf44e2
MS
4381 if ((duplex = ppdFindOption(ppd, "Duplex")) == NULL)
4382 if ((duplex = ppdFindOption(ppd, "EFDuplex")) == NULL)
4383 if ((duplex = ppdFindOption(ppd, "EFDuplexing")) == NULL)
4384 if ((duplex = ppdFindOption(ppd, "KD03Duplex")) == NULL)
4385 duplex = ppdFindOption(ppd, "JCLDuplex");
4386
4387 if (duplex && duplex->num_choices > 1 &&
4388 !ppdInstallableConflict(ppd, duplex->keyword, "DuplexTumble"))
4389 {
4390 p->type |= CUPS_PRINTER_DUPLEX;
4391
4392 ippAddStrings(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
4393 "sides-supported", 3, NULL, sides);
4394
88f9aafc 4395 if (!_cups_strcasecmp(duplex->defchoice, "DuplexTumble"))
61cf44e2
MS
4396 ippAddString(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
4397 "sides-default", NULL, "two-sided-short-edge");
88f9aafc 4398 else if (!_cups_strcasecmp(duplex->defchoice, "DuplexNoTumble"))
61cf44e2
MS
4399 ippAddString(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
4400 "sides-default", NULL, "two-sided-long-edge");
4401 else
4402 ippAddString(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
4403 "sides-default", NULL, "one-sided");
4404 }
cc754834
MS
4405 else
4406 {
4407 ippAddString(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
4408 "sides-supported", NULL, "one-sided");
4409 ippAddString(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
4410 "sides-default", NULL, "one-sided");
4411 }
61cf44e2
MS
4412
4413 if (ppdFindOption(ppd, "Collate") != NULL)
4414 p->type |= CUPS_PRINTER_COLLATE;
4415
4416 if (ppdFindOption(ppd, "StapleLocation") != NULL)
4417 {
4418 p->type |= CUPS_PRINTER_STAPLE;
4419 finishings[num_finishings++] = IPP_FINISHINGS_STAPLE;
4420 }
4421
4422 if (ppdFindOption(ppd, "BindEdge") != NULL)
4423 {
4424 p->type |= CUPS_PRINTER_BIND;
4425 finishings[num_finishings++] = IPP_FINISHINGS_BIND;
4426 }
4427
4428 for (i = 0; i < ppd->num_sizes; i ++)
4429 if (ppd->sizes[i].length > 1728)
4430 p->type |= CUPS_PRINTER_LARGE;
4431 else if (ppd->sizes[i].length > 1008)
4432 p->type |= CUPS_PRINTER_MEDIUM;
4433 else
4434 p->type |= CUPS_PRINTER_SMALL;
4435
b9faaae1 4436 if ((ppd_attr = ppdFindAttr(ppd, "APICADriver", NULL)) != NULL &&
88f9aafc 4437 ppd_attr->value && !_cups_strcasecmp(ppd_attr->value, "true"))
b9faaae1
MS
4438 {
4439 if ((ppd_attr = ppdFindAttr(ppd, "APScannerOnly", NULL)) != NULL &&
88f9aafc 4440 ppd_attr->value && !_cups_strcasecmp(ppd_attr->value, "true"))
b9faaae1
MS
4441 p->type |= CUPS_PRINTER_SCANNER;
4442 else
4443 p->type |= CUPS_PRINTER_MFP;
4444 }
4445
61cf44e2 4446 /*
f14324a7 4447 * Scan the filters in the PPD file...
61cf44e2
MS
4448 */
4449
f14324a7 4450 if (p->pc)
c8fef167 4451 {
f14324a7
MS
4452 for (filter = (const char *)cupsArrayFirst(p->pc->filters);
4453 filter;
4454 filter = (const char *)cupsArrayNext(p->pc->filters))
c8fef167 4455 {
88f9aafc 4456 if (!_cups_strncasecmp(filter, "application/vnd.cups-command", 28) &&
f14324a7
MS
4457 _cups_isspace(filter[28]))
4458 {
4459 p->type |= CUPS_PRINTER_COMMANDS;
61cf44e2 4460 break;
f14324a7 4461 }
61cf44e2
MS
4462 }
4463 }
4464
4465 if (p->type & CUPS_PRINTER_COMMANDS)
4466 {
4467 char *commands, /* Copy of commands */
4468 *start, /* Start of name */
4469 *end; /* End of name */
4470 int count; /* Number of commands */
4471
f14324a7 4472 if ((ppd_attr = ppdFindAttr(ppd, "cupsCommands", NULL)) != NULL)
61cf44e2
MS
4473 {
4474 for (count = 0, start = ppd_attr->value; *start; count ++)
4475 {
f14324a7 4476 while (_cups_isspace(*start))
61cf44e2
MS
4477 start ++;
4478
4479 if (!*start)
4480 break;
4481
4482 while (*start && !isspace(*start & 255))
4483 start ++;
4484 }
4485 }
4486 else
4487 count = 0;
4488
4489 if (count > 0)
4490 {
4491 /*
0268488e
MS
4492 * Make a copy of the commands string and count how many commands there
4493 * are...
61cf44e2
MS
4494 */
4495
4496 attr = ippAddStrings(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
4497 "printer-commands", count, NULL, NULL);
4498
4499 commands = strdup(ppd_attr->value);
4500
4501 for (count = 0, start = commands; *start; count ++)
4502 {
4503 while (isspace(*start & 255))
4504 start ++;
4505
4506 if (!*start)
4507 break;
4508
4509 end = start;
4510 while (*end && !isspace(*end & 255))
4511 end ++;
4512
4513 if (*end)
4514 *end++ = '\0';
4515
4516 attr->values[count].string.text = _cupsStrAlloc(start);
4517
4518 start = end;
4519 }
4520
4521 free(commands);
4522 }
4523 else
4524 {
4525 /*
4526 * Add the standard list of commands...
4527 */
4528
4529 ippAddStrings(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
4530 "printer-commands",
4531 (int)(sizeof(standard_commands) /
4532 sizeof(standard_commands[0])), NULL,
4533 standard_commands);
4534 }
4535 }
4536 else
4537 {
4538 /*
4539 * No commands supported...
4540 */
4541
4542 ippAddString(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
4543 "printer-commands", NULL, "none");
4544 }
4545
4546 /*
4547 * Show current and available port monitors for this printer...
4548 */
4549
4550 ippAddString(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_NAME, "port-monitor",
4551 NULL, p->port_monitor ? p->port_monitor : "none");
4552
4553 for (i = 1, ppd_attr = ppdFindAttr(ppd, "cupsPortMonitor", NULL);
4554 ppd_attr;
4555 i ++, ppd_attr = ppdFindNextAttr(ppd, "cupsPortMonitor", NULL));
4556
4557 if (ppd->protocols)
4558 {
4559 if (strstr(ppd->protocols, "TBCP"))
4560 i ++;
4561 else if (strstr(ppd->protocols, "BCP"))
4562 i ++;
4563 }
4564
4565 attr = ippAddStrings(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
4566 "port-monitor-supported", i, NULL, NULL);
4567
4568 attr->values[0].string.text = _cupsStrAlloc("none");
4569
4570 for (i = 1, ppd_attr = ppdFindAttr(ppd, "cupsPortMonitor", NULL);
4571 ppd_attr;
4572 i ++, ppd_attr = ppdFindNextAttr(ppd, "cupsPortMonitor", NULL))
4573 attr->values[i].string.text = _cupsStrAlloc(ppd_attr->value);
4574
4575 if (ppd->protocols)
4576 {
4577 if (strstr(ppd->protocols, "TBCP"))
4578 attr->values[i].string.text = _cupsStrAlloc("tbcp");
4579 else if (strstr(ppd->protocols, "BCP"))
4580 attr->values[i].string.text = _cupsStrAlloc("bcp");
4581 }
4582
61cf44e2
MS
4583 if (ppdFindAttr(ppd, "APRemoteQueueID", NULL))
4584 p->type |= CUPS_PRINTER_REMOTE;
4585
7cf5915e
MS
4586#ifdef HAVE_APPLICATIONSERVICES_H
4587 /*
4588 * Convert the file referenced in APPrinterIconPath to a 128x128 PNG
4589 * and save it as cacheDir/printername.png
4590 */
4591
4592 if ((ppd_attr = ppdFindAttr(ppd, "APPrinterIconPath", NULL)) != NULL &&
22c9029b
MS
4593 ppd_attr->value &&
4594 !_cupsFileCheck(ppd_attr->value, _CUPS_FILE_CHECK_FILE, !RunUser,
4595 cupsdLogFCMessage, p))
7cf5915e
MS
4596 {
4597 CGImageRef imageRef = NULL;/* Current icon image */
4598 CGImageRef biggestIconRef = NULL;
4599 /* Biggest icon image */
4600 CGImageRef closestTo128IconRef = NULL;
4601 /* Icon image closest to and >= 128 */
4602 CGImageSourceRef sourceRef; /* The file's image source */
4603 char outPath[HTTP_MAX_URI];
4604 /* The path to the PNG file */
4605 CFURLRef outUrl; /* The URL made from the outPath */
4606 CFURLRef icnsFileUrl; /* The URL of the original ICNS icon file */
4607 CGImageDestinationRef destRef; /* The image destination to write */
4608 size_t bytesPerRow; /* The bytes per row used for resizing */
4609 CGContextRef context; /* The CG context used for resizing */
4610
4611 snprintf(outPath, sizeof(outPath), "%s/%s.png", CacheDir, p->name);
7e86f2f6
MS
4612 outUrl = CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, (UInt8 *)outPath, (CFIndex)strlen(outPath), FALSE);
4613 icnsFileUrl = CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, (UInt8 *)ppd_attr->value, (CFIndex)strlen(ppd_attr->value), FALSE);
7cf5915e
MS
4614 if (outUrl && icnsFileUrl)
4615 {
4616 sourceRef = CGImageSourceCreateWithURL(icnsFileUrl, NULL);
4617 if (sourceRef)
4618 {
7e86f2f6 4619 for (i = 0; i < (int)CGImageSourceGetCount(sourceRef); i ++)
7cf5915e 4620 {
7e86f2f6 4621 imageRef = CGImageSourceCreateImageAtIndex(sourceRef, (size_t)i, NULL);
c8fef167
MS
4622 if (!imageRef)
4623 continue;
4624
4625 if (CGImageGetWidth(imageRef) == CGImageGetHeight(imageRef))
7cf5915e
MS
4626 {
4627 /*
4628 * Loop through remembering the icon closest to 128 but >= 128
4629 * and then remember the largest icon.
4630 */
4631
4632 if (CGImageGetWidth(imageRef) >= 128 &&
4633 (!closestTo128IconRef ||
4634 CGImageGetWidth(imageRef) <
4635 CGImageGetWidth(closestTo128IconRef)))
4636 {
4637 CGImageRelease(closestTo128IconRef);
4638 CGImageRetain(imageRef);
4639 closestTo128IconRef = imageRef;
4640 }
4641
4642 if (!biggestIconRef ||
4643 CGImageGetWidth(imageRef) > CGImageGetWidth(biggestIconRef))
4644 {
4645 CGImageRelease(biggestIconRef);
4646 CGImageRetain(imageRef);
4647 biggestIconRef = imageRef;
4648 }
c8fef167 4649 }
7cf5915e 4650
c8fef167 4651 CGImageRelease(imageRef);
7cf5915e
MS
4652 }
4653
4654 if (biggestIconRef)
4655 {
4656 /*
4657 * If biggestIconRef is NULL, we found no icons. Otherwise we first
4658 * want the closest to 128, but if none are larger than 128, we want
4659 * the largest icon available.
4660 */
4661
4662 imageRef = closestTo128IconRef ? closestTo128IconRef :
4663 biggestIconRef;
4664 CGImageRetain(imageRef);
4665 CGImageRelease(biggestIconRef);
c8fef167
MS
4666 if (closestTo128IconRef)
4667 CGImageRelease(closestTo128IconRef);
7cf5915e
MS
4668 destRef = CGImageDestinationCreateWithURL(outUrl, kUTTypePNG, 1,
4669 NULL);
4670 if (destRef)
4671 {
4672 if (CGImageGetWidth(imageRef) != 128)
4673 {
4674 bytesPerRow = CGImageGetBytesPerRow(imageRef) /
4675 CGImageGetWidth(imageRef) * 128;
4676 context = CGBitmapContextCreate(NULL, 128, 128,
4677 CGImageGetBitsPerComponent(imageRef),
4678 bytesPerRow,
4679 CGImageGetColorSpace(imageRef),
4680 kCGImageAlphaPremultipliedFirst);
4681 if (context)
4682 {
4683 CGContextDrawImage(context, CGRectMake(0, 0, 128, 128),
4684 imageRef);
4685 CGImageRelease(imageRef);
4686 imageRef = CGBitmapContextCreateImage(context);
4687 CGContextRelease(context);
4688 }
4689 }
4690
4691 CGImageDestinationAddImage(destRef, imageRef, NULL);
4692 CGImageDestinationFinalize(destRef);
4693 CFRelease(destRef);
4694 }
4695
4696 CGImageRelease(imageRef);
c8fef167 4697 }
7cf5915e
MS
4698
4699 CFRelease(sourceRef);
7cf5915e 4700 }
e60ec91f 4701 }
7cf5915e 4702
e60ec91f 4703 if (outUrl)
7cf5915e 4704 CFRelease(outUrl);
e60ec91f
MS
4705
4706 if (icnsFileUrl)
4707 CFRelease(icnsFileUrl);
7cf5915e
MS
4708 }
4709#endif /* HAVE_APPLICATIONSERVICES_H */
4710
61cf44e2
MS
4711 /*
4712 * Close the PPD and set the type...
4713 */
4714
4715 ppdClose(ppd);
4716 }
4717 else if (!access(ppd_name, 0))
4718 {
4719 int pline; /* PPD line number */
4720 ppd_status_t pstatus; /* PPD load status */
4721
4722
4723 pstatus = ppdLastError(&pline);
4724
4725 cupsdLogMessage(CUPSD_LOG_ERROR, "PPD file for %s cannot be loaded!",
4726 p->name);
4727
4728 if (pstatus <= PPD_ALLOC_ERROR)
4729 cupsdLogMessage(CUPSD_LOG_ERROR, "%s", strerror(errno));
4730 else
4731 cupsdLogMessage(CUPSD_LOG_ERROR, "%s on line %d.",
4732 ppdErrorString(pstatus), pline);
4733
4734 cupsdLogMessage(CUPSD_LOG_INFO,
4735 "Hint: Run \"cupstestppd %s\" and fix any errors.",
4736 ppd_name);
61cf44e2
MS
4737 }
4738 else
4739 {
4740 /*
4741 * If we have an interface script, add a filter entry for it...
4742 */
4743
4744 char interface[1024]; /* Interface script */
4745
4746
4747 snprintf(interface, sizeof(interface), "%s/interfaces/%s", ServerRoot,
4748 p->name);
4749 if (!access(interface, X_OK))
4750 {
4751 /*
4752 * Yes, we have a System V style interface script; use it!
4753 */
4754
4755 ippAddString(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
4756 "printer-make-and-model", NULL,
4757 "Local System V Printer");
61cf44e2 4758 }
37e7e6e0
MS
4759 else if (((!strncmp(p->device_uri, "ipp://", 6) ||
4760 !strncmp(p->device_uri, "ipps://", 7)) &&
4761 (strstr(p->device_uri, "/printers/") != NULL ||
4762 strstr(p->device_uri, "/classes/") != NULL)) ||
4763 ((strstr(p->device_uri, "._ipp.") != NULL ||
4764 strstr(p->device_uri, "._ipps.") != NULL) &&
4765 !strcmp(p->device_uri + strlen(p->device_uri) - 5, "/cups")))
61cf44e2
MS
4766 {
4767 /*
4768 * Tell the client this is really a hard-wired remote printer.
4769 */
4770
4771 p->type |= CUPS_PRINTER_REMOTE;
4772
4773 /*
4774 * Point the printer-uri-supported attribute to the
4775 * remote printer...
4776 */
4777
0268488e
MS
4778 if (strchr(p->device_uri, '?'))
4779 {
4780 /*
4781 * Strip trailing "?options" from URI...
4782 */
4783
4784 char resource[HTTP_MAX_URI], /* New URI */
4785 *ptr; /* Pointer into URI */
4786
4787 strlcpy(resource, p->device_uri, sizeof(resource));
4788 if ((ptr = strchr(resource, '?')) != NULL)
4789 *ptr = '\0';
4790
4791 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI,
4792 "printer-uri-supported", NULL, resource);
4793 }
4794 else
4795 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI,
4796 "printer-uri-supported", NULL, p->device_uri);
61cf44e2
MS
4797
4798 /*
4799 * Then set the make-and-model accordingly...
4800 */
4801
4802 ippAddString(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
4803 "printer-make-and-model", NULL, "Remote Printer");
4804
4805 /*
4806 * Print all files directly...
4807 */
4808
4809 p->raw = 1;
4810 p->remote = 1;
4811 }
4812 else
4813 {
4814 /*
4815 * Otherwise we have neither - treat this as a "dumb" printer
4816 * with no PPD file...
4817 */
4818
4819 ippAddString(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
4820 "printer-make-and-model", NULL, "Local Raw Printer");
4821
4822 p->raw = 1;
4823 }
4824 }
4825
4826 ippAddIntegers(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_ENUM,
4827 "finishings-supported", num_finishings, finishings);
4828 ippAddInteger(p->ppd_attrs, IPP_TAG_PRINTER, IPP_TAG_ENUM,
4829 "finishings-default", IPP_FINISHINGS_NONE);
4830
f14324a7 4831 if (ppd && p->pc)
61cf44e2
MS
4832 {
4833 /*
4834 * Save cached PPD attributes to disk...
4835 */
4836
4837 cupsdLogMessage(CUPSD_LOG_DEBUG, "load_ppd: Saving %s...", cache_name);
4838
f14324a7 4839 _ppdCacheWriteFile(p->pc, cache_name, p->ppd_attrs);
61cf44e2 4840 }
54afec33 4841 else
61cf44e2
MS
4842 {
4843 /*
54afec33 4844 * Remove cache files...
61cf44e2
MS
4845 */
4846
54afec33
MS
4847 if (cache_info.st_mtime)
4848 unlink(cache_name);
61cf44e2
MS
4849 }
4850}
4851
4852
54afec33
MS
4853/*
4854 * 'new_media_col()' - Create a media-col collection value.
4855 */
4856
4857static ipp_t * /* O - Collection value */
4858new_media_col(_pwg_size_t *size, /* I - media-size/margin values */
4859 const char *source, /* I - media-source value */
4860 const char *type) /* I - media-type value */
4861{
4862 ipp_t *media_col, /* Collection value */
4863 *media_size; /* media-size value */
4864
4865
4866 media_col = ippNew();
4867
4868 media_size = ippNew();
4869 ippAddInteger(media_size, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
4870 "x-dimension", size->width);
4871 ippAddInteger(media_size, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
4872 "y-dimension", size->length);
54afec33 4873 ippAddCollection(media_col, IPP_TAG_PRINTER, "media-size", media_size);
aaf19ab0
MS
4874 ippDelete(media_size);
4875
54afec33
MS
4876 ippAddInteger(media_col, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
4877 "media-bottom-margin", size->bottom);
4878 ippAddInteger(media_col, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
4879 "media-left-margin", size->left);
4880 ippAddInteger(media_col, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
4881 "media-right-margin", size->right);
4882 ippAddInteger(media_col, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
4883 "media-top-margin", size->top);
4884
4885 if (source)
4886 ippAddString(media_col, IPP_TAG_PRINTER, IPP_TAG_KEYWORD, "media-source",
4887 NULL, source);
4888
4889 if (type)
4890 ippAddString(media_col, IPP_TAG_PRINTER, IPP_TAG_KEYWORD, "media-type",
4891 NULL, type);
4892
4893 return (media_col);
4894}
4895
4896
ef416fc2 4897/*
0af14961
MS
4898 * 'write_xml_string()' - Write a string with XML escaping.
4899 */
4900
4901static void
4902write_xml_string(cups_file_t *fp, /* I - File to write to */
4903 const char *s) /* I - String to write */
4904{
4905 const char *start; /* Start of current sequence */
4906
4907
4908 if (!s)
4909 return;
4910
4911 for (start = s; *s; s ++)
4912 {
4913 if (*s == '&')
4914 {
4915 if (s > start)
7e86f2f6 4916 cupsFileWrite(fp, start, (size_t)(s - start));
0af14961
MS
4917
4918 cupsFilePuts(fp, "&amp;");
4919 start = s + 1;
4920 }
4921 else if (*s == '<')
4922 {
4923 if (s > start)
7e86f2f6 4924 cupsFileWrite(fp, start, (size_t)(s - start));
0af14961
MS
4925
4926 cupsFilePuts(fp, "&lt;");
4927 start = s + 1;
4928 }
4929 }
4930
4931 if (s > start)
4932 cupsFilePuts(fp, start);
4933}
4934
4935
4936/*
f2d18633 4937 * End of "$Id$".
ef416fc2 4938 */