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