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