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