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