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