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