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