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