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