]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/printers.c
Merge changes from CUPS trunk, r7566.
[thirdparty/cups.git] / scheduler / printers.c
1 /*
2 * "$Id: printers.c 6970 2007-09-17 23:58:28Z mike $"
3 *
4 * Printer routines for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 2007-2008 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 * cupsdFindPrinter() - Find a printer in the list.
24 * cupsdFreePrinterUsers() - Free allow/deny users.
25 * cupsdLoadAllPrinters() - Load printers from the printers.conf file.
26 * cupsdRenamePrinter() - Rename a printer.
27 * cupsdSaveAllPrinters() - Save all printer definitions to the
28 * printers.conf file.
29 * cupsdSetAuthInfoRequired() - Set the required authentication info.
30 * cupsdSetPrinterAttr() - Set a printer attribute.
31 * cupsdSetPrinterAttrs() - Set printer attributes based upon the PPD
32 * file.
33 * cupsdSetPrinterReasons() - Set/update the reasons strings.
34 * cupsdSetPrinterState() - Update the current state of a printer.
35 * cupsdStopPrinter() - Stop a printer from printing any jobs...
36 * cupsdUpdatePrinters() - Update printers after a partial reload.
37 * cupsdValidateDest() - Validate a printer/class destination.
38 * cupsdWritePrintcap() - Write a pseudo-printcap file for older
39 * applications that need it...
40 * cupsdSanitizeURI() - Sanitize a device URI...
41 * add_printer_defaults() - Add name-default attributes to the printer
42 * attributes.
43 * add_printer_filter() - Add a MIME filter for a printer.
44 * add_printer_formats() - Add document-format-supported values for
45 * a printer.
46 * compare_printers() - Compare two printers.
47 * delete_printer_filters() - Delete all MIME filters for a printer.
48 * write_irix_config() - Update the config files used by the IRIX
49 * desktop tools.
50 * write_irix_state() - Update the status files used by IRIX
51 * printing desktop tools.
52 */
53
54 /*
55 * Include necessary headers...
56 */
57
58 #include "cupsd.h"
59 #include <cups/dir.h>
60
61
62 /*
63 * Local functions...
64 */
65
66 static void add_printer_defaults(cupsd_printer_t *p);
67 static void add_printer_filter(cupsd_printer_t *p, mime_type_t *type,
68 const char *filter);
69 static void add_printer_formats(cupsd_printer_t *p);
70 static int compare_printers(void *first, void *second, void *data);
71 static void delete_printer_filters(cupsd_printer_t *p);
72 #ifdef __sgi
73 static void write_irix_config(cupsd_printer_t *p);
74 static void write_irix_state(cupsd_printer_t *p);
75 #endif /* __sgi */
76
77
78 /*
79 * 'cupsdAddPrinter()' - Add a printer to the system.
80 */
81
82 cupsd_printer_t * /* O - New printer */
83 cupsdAddPrinter(const char *name) /* I - Name of printer */
84 {
85 cupsd_printer_t *p; /* New printer */
86
87
88 /*
89 * Range check input...
90 */
91
92 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdAddPrinter(\"%s\")", name);
93
94 /*
95 * Create a new printer entity...
96 */
97
98 if ((p = calloc(1, sizeof(cupsd_printer_t))) == NULL)
99 {
100 cupsdLogMessage(CUPSD_LOG_CRIT, "Unable to allocate memory for printer - %s",
101 strerror(errno));
102 return (NULL);
103 }
104
105 cupsdSetString(&p->name, name);
106 cupsdSetString(&p->info, name);
107 cupsdSetString(&p->hostname, ServerName);
108
109 cupsdSetStringf(&p->uri, "ipp://%s:%d/printers/%s", ServerName, LocalPort, name);
110 cupsdSetStringf(&p->device_uri, "file:/dev/null");
111
112 p->state = IPP_PRINTER_STOPPED;
113 p->state_time = time(NULL);
114 p->accepting = 0;
115 p->shared = DefaultShared;
116 p->filetype = mimeAddType(MimeDatabase, "printer", name);
117
118 cupsdSetString(&p->job_sheets[0], "none");
119 cupsdSetString(&p->job_sheets[1], "none");
120
121 cupsdSetString(&p->error_policy, ErrorPolicy);
122 cupsdSetString(&p->op_policy, DefaultPolicy);
123
124 p->op_policy_ptr = DefaultPolicyPtr;
125
126 if (MaxPrinterHistory)
127 p->history = calloc(MaxPrinterHistory, sizeof(ipp_t *));
128
129 /*
130 * Insert the printer in the printer list alphabetically...
131 */
132
133 if (!Printers)
134 Printers = cupsArrayNew(compare_printers, NULL);
135
136 cupsArrayAdd(Printers, p);
137
138 if (!ImplicitPrinters)
139 ImplicitPrinters = cupsArrayNew(compare_printers, NULL);
140
141 /*
142 * Return the new printer...
143 */
144
145 return (p);
146 }
147
148
149 /*
150 * 'cupsdAddPrinterHistory()' - Add the current printer state to the history.
151 */
152
153 void
154 cupsdAddPrinterHistory(
155 cupsd_printer_t *p) /* I - Printer */
156 {
157 ipp_t *history; /* History collection */
158
159
160 /*
161 * Stop early if we aren't keeping history data...
162 */
163
164 if (MaxPrinterHistory <= 0)
165 return;
166
167 /*
168 * Retire old history data as needed...
169 */
170
171 p->sequence_number ++;
172
173 if (p->num_history >= MaxPrinterHistory)
174 {
175 p->num_history --;
176 ippDelete(p->history[0]);
177 memmove(p->history, p->history + 1, p->num_history * sizeof(ipp_t *));
178 }
179
180 /*
181 * Create a collection containing the current printer-state, printer-up-time,
182 * printer-state-message, and printer-state-reasons attributes.
183 */
184
185 history = ippNew();
186 ippAddInteger(history, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-state",
187 p->state);
188 ippAddBoolean(history, IPP_TAG_PRINTER, "printer-is-accepting-jobs",
189 p->accepting);
190 ippAddBoolean(history, IPP_TAG_PRINTER, "printer-is-shared", p->shared);
191 ippAddString(history, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-state-message",
192 NULL, p->state_message);
193 #ifdef __APPLE__
194 if (p->recoverable)
195 ippAddString(history, IPP_TAG_PRINTER, IPP_TAG_TEXT,
196 "com.apple.print.recoverable-message", NULL, p->recoverable);
197 #endif /* __APPLE__ */
198 if (p->num_reasons == 0)
199 ippAddString(history, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
200 "printer-state-reasons", NULL,
201 p->state == IPP_PRINTER_STOPPED ? "paused" : "none");
202 else
203 ippAddStrings(history, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
204 "printer-state-reasons", p->num_reasons, NULL,
205 (const char * const *)p->reasons);
206 ippAddInteger(history, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
207 "printer-state-change-time", p->state_time);
208 ippAddInteger(history, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
209 "printer-state-sequence-number", p->sequence_number);
210
211 p->history[p->num_history] = history;
212 p->num_history ++;
213 }
214
215
216 /*
217 * 'cupsdAddPrinterUser()' - Add a user to the ACL.
218 */
219
220 void
221 cupsdAddPrinterUser(
222 cupsd_printer_t *p, /* I - Printer */
223 const char *username) /* I - User */
224 {
225 const char **temp; /* Temporary array pointer */
226
227
228 if (!p || !username)
229 return;
230
231 if (p->num_users == 0)
232 temp = malloc(sizeof(char **));
233 else
234 temp = realloc(p->users, sizeof(char **) * (p->num_users + 1));
235
236 if (!temp)
237 return;
238
239 p->users = temp;
240 temp += p->num_users;
241
242 if ((*temp = strdup(username)) != NULL)
243 p->num_users ++;
244 }
245
246
247 /*
248 * 'cupsdCreateCommonData()' - Create the common printer data.
249 */
250
251 void
252 cupsdCreateCommonData(void)
253 {
254 int i; /* Looping var */
255 ipp_attribute_t *attr; /* Attribute data */
256 cups_dir_t *dir; /* Notifier directory */
257 cups_dentry_t *dent; /* Notifier directory entry */
258 cups_array_t *notifiers; /* Notifier array */
259 char filename[1024], /* Filename */
260 *notifier; /* Current notifier */
261 cupsd_policy_t *p; /* Current policy */
262 static const int nups[] = /* number-up-supported values */
263 { 1, 2, 4, 6, 9, 16 };
264 static const int orients[4] =/* orientation-requested-supported values */
265 {
266 IPP_PORTRAIT,
267 IPP_LANDSCAPE,
268 IPP_REVERSE_LANDSCAPE,
269 IPP_REVERSE_PORTRAIT
270 };
271 static const char * const holds[] = /* job-hold-until-supported values */
272 {
273 "no-hold",
274 "indefinite",
275 "day-time",
276 "evening",
277 "night",
278 "second-shift",
279 "third-shift",
280 "weekend"
281 };
282 static const char * const versions[] =/* ipp-versions-supported values */
283 {
284 "1.0",
285 "1.1"
286 };
287 static const int ops[] = /* operations-supported values */
288 {
289 IPP_PRINT_JOB,
290 IPP_VALIDATE_JOB,
291 IPP_CREATE_JOB,
292 IPP_SEND_DOCUMENT,
293 IPP_CANCEL_JOB,
294 IPP_GET_JOB_ATTRIBUTES,
295 IPP_GET_JOBS,
296 IPP_GET_PRINTER_ATTRIBUTES,
297 IPP_HOLD_JOB,
298 IPP_RELEASE_JOB,
299 IPP_PAUSE_PRINTER,
300 IPP_RESUME_PRINTER,
301 IPP_PURGE_JOBS,
302 IPP_SET_JOB_ATTRIBUTES,
303 IPP_CREATE_PRINTER_SUBSCRIPTION,
304 IPP_CREATE_JOB_SUBSCRIPTION,
305 IPP_GET_SUBSCRIPTION_ATTRIBUTES,
306 IPP_GET_SUBSCRIPTIONS,
307 IPP_RENEW_SUBSCRIPTION,
308 IPP_CANCEL_SUBSCRIPTION,
309 IPP_GET_NOTIFICATIONS,
310 IPP_ENABLE_PRINTER,
311 IPP_DISABLE_PRINTER,
312 CUPS_GET_DEFAULT,
313 CUPS_GET_PRINTERS,
314 CUPS_ADD_PRINTER,
315 CUPS_DELETE_PRINTER,
316 CUPS_GET_CLASSES,
317 CUPS_ADD_CLASS,
318 CUPS_DELETE_CLASS,
319 CUPS_ACCEPT_JOBS,
320 CUPS_REJECT_JOBS,
321 CUPS_SET_DEFAULT,
322 CUPS_GET_DEVICES,
323 CUPS_GET_PPDS,
324 CUPS_MOVE_JOB,
325 CUPS_AUTHENTICATE_JOB,
326 CUPS_GET_PPD,
327 CUPS_GET_DOCUMENT,
328 IPP_RESTART_JOB
329 };
330 static const char * const charsets[] =/* charset-supported values */
331 {
332 "us-ascii",
333 "utf-8"
334 };
335 static const char * const compressions[] =
336 { /* document-compression-supported values */
337 "none"
338 #ifdef HAVE_LIBZ
339 ,"gzip"
340 #endif /* HAVE_LIBZ */
341 };
342 static const char * const multiple_document_handling[] =
343 { /* multiple-document-handling-supported values */
344 "separate-documents-uncollated-copies",
345 "separate-documents-collated-copies"
346 };
347 static const char * const errors[] = /* printer-error-policy-supported values */
348 {
349 "abort-job",
350 "retry-job",
351 "stop-printer"
352 };
353 static const char * const notify_attrs[] =
354 { /* notify-attributes-supported values */
355 "printer-state-change-time",
356 "notify-lease-expiration-time",
357 "notify-subscriber-user-name"
358 };
359 static const char * const notify_events[] =
360 { /* notify-events-supported values */
361 "job-completed",
362 "job-config-changed",
363 "job-created",
364 "job-progress",
365 "job-state-changed",
366 "job-stopped",
367 "printer-added",
368 "printer-changed",
369 "printer-config-changed",
370 "printer-deleted",
371 "printer-finishings-changed",
372 "printer-media-changed",
373 "printer-modified",
374 "printer-restarted",
375 "printer-shutdown",
376 "printer-state-changed",
377 "printer-stopped",
378 "server-audit",
379 "server-restarted",
380 "server-started",
381 "server-stopped"
382 };
383
384
385 if (CommonData)
386 ippDelete(CommonData);
387
388 CommonData = ippNew();
389
390 /*
391 * This list of attributes is sorted to improve performance when the
392 * client provides a requested-attributes attribute...
393 */
394
395 /* charset-configured */
396 ippAddString(CommonData, IPP_TAG_PRINTER, IPP_TAG_CHARSET,
397 "charset-configured", NULL, DefaultCharset);
398
399 /* charset-supported */
400 ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_CHARSET,
401 "charset-supported", sizeof(charsets) / sizeof(charsets[0]),
402 NULL, charsets);
403
404 /* compression-supported */
405 ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
406 "compression-supported",
407 sizeof(compressions) / sizeof(compressions[0]),
408 NULL, compressions);
409
410 /* copies-supported */
411 ippAddRange(CommonData, IPP_TAG_PRINTER, "copies-supported", 1, MaxCopies);
412
413 /* cups-version */
414 ippAddString(CommonData, IPP_TAG_PRINTER, IPP_TAG_TEXT, "cups-version",
415 NULL, CUPS_SVERSION + 6);
416
417 /* generated-natural-language-supported */
418 ippAddString(CommonData, IPP_TAG_PRINTER, IPP_TAG_LANGUAGE,
419 "generated-natural-language-supported", NULL, DefaultLanguage);
420
421 /* ipp-versions-supported */
422 ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
423 "ipp-versions-supported", sizeof(versions) / sizeof(versions[0]),
424 NULL, versions);
425
426 /* job-hold-until-supported */
427 ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
428 "job-hold-until-supported", sizeof(holds) / sizeof(holds[0]),
429 NULL, holds);
430
431 /* job-priority-supported */
432 ippAddInteger(CommonData, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
433 "job-priority-supported", 100);
434
435 /* job-sheets-supported */
436 if (cupsArrayCount(Banners) > 0)
437 {
438 /*
439 * Setup the job-sheets-supported attribute...
440 */
441
442 if (Classification && !ClassifyOverride)
443 attr = ippAddString(CommonData, IPP_TAG_PRINTER, IPP_TAG_NAME,
444 "job-sheets-supported", NULL, Classification);
445 else
446 attr = ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_NAME,
447 "job-sheets-supported", cupsArrayCount(Banners) + 1,
448 NULL, NULL);
449
450 if (attr == NULL)
451 cupsdLogMessage(CUPSD_LOG_EMERG,
452 "Unable to allocate memory for "
453 "job-sheets-supported attribute: %s!", strerror(errno));
454 else if (!Classification || ClassifyOverride)
455 {
456 cupsd_banner_t *banner; /* Current banner */
457
458
459 attr->values[0].string.text = _cupsStrAlloc("none");
460
461 for (i = 1, banner = (cupsd_banner_t *)cupsArrayFirst(Banners);
462 banner;
463 i ++, banner = (cupsd_banner_t *)cupsArrayNext(Banners))
464 attr->values[i].string.text = _cupsStrAlloc(banner->name);
465 }
466 }
467 else
468 ippAddString(CommonData, IPP_TAG_PRINTER, IPP_TAG_NAME,
469 "job-sheets-supported", NULL, "none");
470
471 /* multiple-document-handling-supported */
472 ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
473 "multiple-document-handling-supported",
474 sizeof(multiple_document_handling) /
475 sizeof(multiple_document_handling[0]), NULL,
476 multiple_document_handling);
477
478 /* multiple-document-jobs-supported */
479 ippAddBoolean(CommonData, IPP_TAG_PRINTER,
480 "multiple-document-jobs-supported", 1);
481
482 /* multiple-operation-time-out */
483 ippAddInteger(CommonData, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
484 "multiple-operation-time-out", 60);
485
486 /* natural-language-configured */
487 ippAddString(CommonData, IPP_TAG_PRINTER, IPP_TAG_LANGUAGE,
488 "natural-language-configured", NULL, DefaultLanguage);
489
490 /* notify-attributes-supported */
491 ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
492 "notify-attributes-supported",
493 (int)(sizeof(notify_attrs) / sizeof(notify_attrs[0])),
494 NULL, notify_attrs);
495
496 /* notify-lease-duration-supported */
497 ippAddRange(CommonData, IPP_TAG_PRINTER,
498 "notify-lease-duration-supported", 0,
499 MaxLeaseDuration ? MaxLeaseDuration : 2147483647);
500
501 /* notify-max-events-supported */
502 ippAddInteger(CommonData, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
503 "notify-max-events-supported", MaxEvents);
504
505 /* notify-events-supported */
506 ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
507 "notify-events-supported",
508 (int)(sizeof(notify_events) / sizeof(notify_events[0])),
509 NULL, notify_events);
510
511 /* notify-pull-method-supported */
512 ippAddString(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
513 "notify-pull-method-supported", NULL, "ippget");
514
515 /* notify-schemes-supported */
516 snprintf(filename, sizeof(filename), "%s/notifier", ServerBin);
517 if ((dir = cupsDirOpen(filename)) != NULL)
518 {
519 notifiers = cupsArrayNew((cups_array_func_t)strcmp, NULL);
520
521 while ((dent = cupsDirRead(dir)) != NULL)
522 if (S_ISREG(dent->fileinfo.st_mode) &&
523 (dent->fileinfo.st_mode & S_IXOTH) != 0)
524 cupsArrayAdd(notifiers, _cupsStrAlloc(dent->filename));
525
526 if (cupsArrayCount(notifiers) > 0)
527 {
528 attr = ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
529 "notify-schemes-supported",
530 cupsArrayCount(notifiers), NULL, NULL);
531
532 for (i = 0, notifier = (char *)cupsArrayFirst(notifiers);
533 notifier;
534 i ++, notifier = (char *)cupsArrayNext(notifiers))
535 attr->values[i].string.text = notifier;
536 }
537
538 cupsArrayDelete(notifiers);
539 cupsDirClose(dir);
540 }
541
542 /* number-up-supported */
543 ippAddIntegers(CommonData, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
544 "number-up-supported", sizeof(nups) / sizeof(nups[0]), nups);
545
546 /* operations-supported */
547 ippAddIntegers(CommonData, IPP_TAG_PRINTER, IPP_TAG_ENUM,
548 "operations-supported",
549 sizeof(ops) / sizeof(ops[0]) + JobFiles - 1, ops);
550
551 /* orientation-requested-supported */
552 ippAddIntegers(CommonData, IPP_TAG_PRINTER, IPP_TAG_ENUM,
553 "orientation-requested-supported", 4, orients);
554
555 /* page-ranges-supported */
556 ippAddBoolean(CommonData, IPP_TAG_PRINTER, "page-ranges-supported", 1);
557
558 /* pdf-override-supported */
559 ippAddString(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
560 "pdl-override-supported", NULL, "not-attempted");
561
562 /* printer-error-policy-supported */
563 ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_NAME,
564 "printer-error-policy-supported",
565 sizeof(errors) / sizeof(errors[0]), NULL, errors);
566
567 /* printer-op-policy-supported */
568 attr = ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_NAME,
569 "printer-op-policy-supported", cupsArrayCount(Policies),
570 NULL, NULL);
571 for (i = 0, p = (cupsd_policy_t *)cupsArrayFirst(Policies);
572 p;
573 i ++, p = (cupsd_policy_t *)cupsArrayNext(Policies))
574 attr->values[i].string.text = _cupsStrAlloc(p->name);
575
576 ippAddBoolean(CommonData, IPP_TAG_PRINTER, "server-is-sharing-printers",
577 BrowseLocalProtocols != 0 && Browsing);
578 }
579
580
581 /*
582 * 'cupsdDeleteAllPrinters()' - Delete all printers from the system.
583 */
584
585 void
586 cupsdDeleteAllPrinters(void)
587 {
588 cupsd_printer_t *p; /* Pointer to current printer/class */
589
590
591 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
592 p;
593 p = (cupsd_printer_t *)cupsArrayNext(Printers))
594 if (!(p->type & CUPS_PRINTER_CLASS))
595 cupsdDeletePrinter(p, 0);
596 }
597
598
599 /*
600 * 'cupsdDeletePrinter()' - Delete a printer from the system.
601 */
602
603 void
604 cupsdDeletePrinter(
605 cupsd_printer_t *p, /* I - Printer to delete */
606 int update) /* I - Update printers.conf? */
607 {
608 int i; /* Looping var */
609 #ifdef __sgi
610 char filename[1024]; /* Interface script filename */
611 #endif /* __sgi */
612
613
614 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdDeletePrinter(p=%p(%s), update=%d)",
615 p, p->name, update);
616
617 /*
618 * Save the current position in the Printers array...
619 */
620
621 cupsArraySave(Printers);
622
623 /*
624 * Stop printing on this printer...
625 */
626
627 cupsdStopPrinter(p, update);
628
629 /*
630 * If this printer is the next for browsing, point to the next one...
631 */
632
633 if (p == BrowseNext)
634 {
635 cupsArrayFind(Printers, p);
636 BrowseNext = (cupsd_printer_t *)cupsArrayNext(Printers);
637 }
638
639 /*
640 * Remove the printer from the list...
641 */
642
643 cupsArrayRemove(Printers, p);
644
645 if (p->type & CUPS_PRINTER_IMPLICIT)
646 cupsArrayRemove(ImplicitPrinters, p);
647
648 /*
649 * Remove the dummy interface/icon/option files under IRIX...
650 */
651
652 #ifdef __sgi
653 snprintf(filename, sizeof(filename), "/var/spool/lp/interface/%s", p->name);
654 unlink(filename);
655
656 snprintf(filename, sizeof(filename), "/var/spool/lp/gui_interface/ELF/%s.gui",
657 p->name);
658 unlink(filename);
659
660 snprintf(filename, sizeof(filename), "/var/spool/lp/activeicons/%s", p->name);
661 unlink(filename);
662
663 snprintf(filename, sizeof(filename), "/var/spool/lp/pod/%s.config", p->name);
664 unlink(filename);
665
666 snprintf(filename, sizeof(filename), "/var/spool/lp/pod/%s.status", p->name);
667 unlink(filename);
668
669 snprintf(filename, sizeof(filename), "/var/spool/lp/member/%s", p->name);
670 unlink(filename);
671 #endif /* __sgi */
672
673 /*
674 * If p is the default printer, assign a different one...
675 */
676
677 if (p == DefaultPrinter)
678 {
679 DefaultPrinter = NULL;
680
681 if (UseNetworkDefault)
682 {
683 /*
684 * Find the first network default printer and use it...
685 */
686
687 cupsd_printer_t *dp; /* New default printer */
688
689
690 for (dp = (cupsd_printer_t *)cupsArrayFirst(Printers);
691 dp;
692 dp = (cupsd_printer_t *)cupsArrayNext(Printers))
693 if (dp != p && (dp->type & CUPS_PRINTER_DEFAULT))
694 {
695 DefaultPrinter = dp;
696 break;
697 }
698 }
699 }
700
701 /*
702 * Remove this printer from any classes...
703 */
704
705 if (!(p->type & CUPS_PRINTER_IMPLICIT))
706 {
707 cupsdDeletePrinterFromClasses(p);
708
709 /*
710 * Deregister from any browse protocols...
711 */
712
713 cupsdDeregisterPrinter(p, 1);
714 }
715
716 /*
717 * Free all memory used by the printer...
718 */
719
720 if (p->printers != NULL)
721 free(p->printers);
722
723 if (MaxPrinterHistory)
724 {
725 for (i = 0; i < p->num_history; i ++)
726 ippDelete(p->history[i]);
727
728 free(p->history);
729 }
730
731 for (i = 0; i < p->num_reasons; i ++)
732 free(p->reasons[i]);
733
734 ippDelete(p->attrs);
735
736 delete_printer_filters(p);
737
738 mimeDeleteType(MimeDatabase, p->filetype);
739 mimeDeleteType(MimeDatabase, p->prefiltertype);
740
741 cupsdFreePrinterUsers(p);
742 cupsdFreeQuotas(p);
743
744 cupsdClearString(&p->uri);
745 cupsdClearString(&p->hostname);
746 cupsdClearString(&p->name);
747 cupsdClearString(&p->location);
748 cupsdClearString(&p->make_model);
749 cupsdClearString(&p->info);
750 cupsdClearString(&p->job_sheets[0]);
751 cupsdClearString(&p->job_sheets[1]);
752 cupsdClearString(&p->device_uri);
753 cupsdClearString(&p->port_monitor);
754 cupsdClearString(&p->op_policy);
755 cupsdClearString(&p->error_policy);
756
757 cupsdClearString(&p->alert);
758 cupsdClearString(&p->alert_description);
759
760 #ifdef HAVE_DNSSD
761 cupsdClearString(&p->product);
762 cupsdClearString(&p->pdl);
763 #endif /* HAVE_DNSSD */
764
765 cupsArrayDelete(p->filetypes);
766
767 if (p->browse_attrs)
768 free(p->browse_attrs);
769
770 #ifdef __APPLE__
771 cupsdClearString(&p->recoverable);
772 #endif /* __APPLE__ */
773
774 cupsFreeOptions(p->num_options, p->options);
775
776 free(p);
777
778 /*
779 * Restore the previous position in the Printers array...
780 */
781
782 cupsArrayRestore(Printers);
783 }
784
785
786 /*
787 * 'cupsdFindDest()' - Find a destination in the list.
788 */
789
790 cupsd_printer_t * /* O - Destination in list */
791 cupsdFindDest(const char *name) /* I - Name of printer or class to find */
792 {
793 cupsd_printer_t key; /* Search key */
794
795
796 key.name = (char *)name;
797 return ((cupsd_printer_t *)cupsArrayFind(Printers, &key));
798 }
799
800
801 /*
802 * 'cupsdFindPrinter()' - Find a printer in the list.
803 */
804
805 cupsd_printer_t * /* O - Printer in list */
806 cupsdFindPrinter(const char *name) /* I - Name of printer to find */
807 {
808 cupsd_printer_t *p; /* Printer in list */
809
810
811 if ((p = cupsdFindDest(name)) != NULL && (p->type & CUPS_PRINTER_CLASS))
812 return (NULL);
813 else
814 return (p);
815 }
816
817
818 /*
819 * 'cupsdFreePrinterUsers()' - Free allow/deny users.
820 */
821
822 void
823 cupsdFreePrinterUsers(
824 cupsd_printer_t *p) /* I - Printer */
825 {
826 int i; /* Looping var */
827
828
829 if (!p || !p->num_users)
830 return;
831
832 for (i = 0; i < p->num_users; i ++)
833 free((void *)p->users[i]);
834
835 free(p->users);
836
837 p->num_users = 0;
838 p->users = NULL;
839 }
840
841
842 /*
843 * 'cupsdLoadAllPrinters()' - Load printers from the printers.conf file.
844 */
845
846 void
847 cupsdLoadAllPrinters(void)
848 {
849 cups_file_t *fp; /* printers.conf file */
850 int linenum; /* Current line number */
851 char line[1024], /* Line from file */
852 *value, /* Pointer to value */
853 *valueptr; /* Pointer into value */
854 cupsd_printer_t *p; /* Current printer */
855
856
857 /*
858 * Open the printers.conf file...
859 */
860
861 snprintf(line, sizeof(line), "%s/printers.conf", ServerRoot);
862 if ((fp = cupsFileOpen(line, "r")) == NULL)
863 {
864 if (errno != ENOENT)
865 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to open %s - %s", line,
866 strerror(errno));
867 return;
868 }
869
870 /*
871 * Read printer configurations until we hit EOF...
872 */
873
874 linenum = 0;
875 p = NULL;
876
877 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
878 {
879 /*
880 * Decode the directive...
881 */
882
883 if (!strcasecmp(line, "<Printer") ||
884 !strcasecmp(line, "<DefaultPrinter"))
885 {
886 /*
887 * <Printer name> or <DefaultPrinter name>
888 */
889
890 if (p == NULL && value)
891 {
892 /*
893 * Add the printer and a base file type...
894 */
895
896 cupsdLogMessage(CUPSD_LOG_DEBUG, "Loading printer %s...", value);
897
898 p = cupsdAddPrinter(value);
899 p->accepting = 1;
900 p->state = IPP_PRINTER_IDLE;
901
902 /*
903 * Set the default printer as needed...
904 */
905
906 if (!strcasecmp(line, "<DefaultPrinter"))
907 DefaultPrinter = p;
908 }
909 else
910 {
911 cupsdLogMessage(CUPSD_LOG_ERROR,
912 "Syntax error on line %d of printers.conf.", linenum);
913 break;
914 }
915 }
916 else if (!strcasecmp(line, "</Printer>"))
917 {
918 if (p != NULL)
919 {
920 /*
921 * Close out the current printer...
922 */
923
924 cupsdSetPrinterAttrs(p);
925 cupsdAddPrinterHistory(p);
926
927 if (p->device_uri && strncmp(p->device_uri, "file:", 5) &&
928 p->state != IPP_PRINTER_STOPPED)
929 {
930 /*
931 * See if the backend exists...
932 */
933
934 snprintf(line, sizeof(line), "%s/backend/%s", ServerBin,
935 p->device_uri);
936
937 if ((valueptr = strchr(line + strlen(ServerBin), ':')) != NULL)
938 *valueptr = '\0'; /* Chop everything but URI scheme */
939
940 if (access(line, 0))
941 {
942 /*
943 * Backend does not exist, stop printer...
944 */
945
946 p->state = IPP_PRINTER_STOPPED;
947 snprintf(p->state_message, sizeof(p->state_message),
948 "Backend %s does not exist!", line);
949 }
950 }
951
952 p = NULL;
953 }
954 else
955 {
956 cupsdLogMessage(CUPSD_LOG_ERROR,
957 "Syntax error on line %d of printers.conf.", linenum);
958 break;
959 }
960 }
961 else if (!p)
962 {
963 cupsdLogMessage(CUPSD_LOG_ERROR,
964 "Syntax error on line %d of printers.conf.", linenum);
965 break;
966 }
967 else if (!strcasecmp(line, "AuthInfoRequired"))
968 {
969 if (!cupsdSetAuthInfoRequired(p, value, NULL))
970 cupsdLogMessage(CUPSD_LOG_ERROR,
971 "Bad AuthInfoRequired on line %d of printers.conf.",
972 linenum);
973 }
974 else if (!strcasecmp(line, "Info"))
975 {
976 if (value)
977 cupsdSetString(&p->info, value);
978 }
979 else if (!strcasecmp(line, "Location"))
980 {
981 if (value)
982 cupsdSetString(&p->location, value);
983 }
984 else if (!strcasecmp(line, "DeviceURI"))
985 {
986 if (value)
987 cupsdSetString(&p->device_uri, value);
988 else
989 {
990 cupsdLogMessage(CUPSD_LOG_ERROR,
991 "Syntax error on line %d of printers.conf.", linenum);
992 break;
993 }
994 }
995 else if (!strcasecmp(line, "Option") && value)
996 {
997 /*
998 * Option name value
999 */
1000
1001 for (valueptr = value; *valueptr && !isspace(*valueptr & 255); valueptr ++);
1002
1003 if (!*valueptr)
1004 cupsdLogMessage(CUPSD_LOG_ERROR,
1005 "Syntax error on line %d of printers.conf.", linenum);
1006 else
1007 {
1008 for (; *valueptr && isspace(*valueptr & 255); *valueptr++ = '\0');
1009
1010 p->num_options = cupsAddOption(value, valueptr, p->num_options,
1011 &(p->options));
1012 }
1013 }
1014 else if (!strcasecmp(line, "PortMonitor"))
1015 {
1016 if (value && strcmp(value, "none"))
1017 cupsdSetString(&p->port_monitor, value);
1018 else if (value)
1019 cupsdClearString(&p->port_monitor);
1020 else
1021 {
1022 cupsdLogMessage(CUPSD_LOG_ERROR,
1023 "Syntax error on line %d of printers.conf.", linenum);
1024 break;
1025 }
1026 }
1027 else if (!strcasecmp(line, "State"))
1028 {
1029 /*
1030 * Set the initial queue state...
1031 */
1032
1033 if (value && !strcasecmp(value, "idle"))
1034 p->state = IPP_PRINTER_IDLE;
1035 else if (value && !strcasecmp(value, "stopped"))
1036 p->state = IPP_PRINTER_STOPPED;
1037 else
1038 {
1039 cupsdLogMessage(CUPSD_LOG_ERROR,
1040 "Syntax error on line %d of printers.conf.", linenum);
1041 break;
1042 }
1043 }
1044 else if (!strcasecmp(line, "StateMessage"))
1045 {
1046 /*
1047 * Set the initial queue state message...
1048 */
1049
1050 if (value)
1051 strlcpy(p->state_message, value, sizeof(p->state_message));
1052 }
1053 else if (!strcasecmp(line, "StateTime"))
1054 {
1055 /*
1056 * Set the state time...
1057 */
1058
1059 if (value)
1060 p->state_time = atoi(value);
1061 }
1062 else if (!strcasecmp(line, "Accepting"))
1063 {
1064 /*
1065 * Set the initial accepting state...
1066 */
1067
1068 if (value &&
1069 (!strcasecmp(value, "yes") ||
1070 !strcasecmp(value, "on") ||
1071 !strcasecmp(value, "true")))
1072 p->accepting = 1;
1073 else if (value &&
1074 (!strcasecmp(value, "no") ||
1075 !strcasecmp(value, "off") ||
1076 !strcasecmp(value, "false")))
1077 p->accepting = 0;
1078 else
1079 {
1080 cupsdLogMessage(CUPSD_LOG_ERROR,
1081 "Syntax error on line %d of printers.conf.", linenum);
1082 break;
1083 }
1084 }
1085 else if (!strcasecmp(line, "Shared"))
1086 {
1087 /*
1088 * Set the initial shared state...
1089 */
1090
1091 if (value &&
1092 (!strcasecmp(value, "yes") ||
1093 !strcasecmp(value, "on") ||
1094 !strcasecmp(value, "true")))
1095 p->shared = 1;
1096 else if (value &&
1097 (!strcasecmp(value, "no") ||
1098 !strcasecmp(value, "off") ||
1099 !strcasecmp(value, "false")))
1100 p->shared = 0;
1101 else
1102 {
1103 cupsdLogMessage(CUPSD_LOG_ERROR,
1104 "Syntax error on line %d of printers.conf.", linenum);
1105 break;
1106 }
1107 }
1108 else if (!strcasecmp(line, "JobSheets"))
1109 {
1110 /*
1111 * Set the initial job sheets...
1112 */
1113
1114 if (value)
1115 {
1116 for (valueptr = value; *valueptr && !isspace(*valueptr & 255); valueptr ++);
1117
1118 if (*valueptr)
1119 *valueptr++ = '\0';
1120
1121 cupsdSetString(&p->job_sheets[0], value);
1122
1123 while (isspace(*valueptr & 255))
1124 valueptr ++;
1125
1126 if (*valueptr)
1127 {
1128 for (value = valueptr; *valueptr && !isspace(*valueptr & 255); valueptr ++);
1129
1130 if (*valueptr)
1131 *valueptr++ = '\0';
1132
1133 cupsdSetString(&p->job_sheets[1], value);
1134 }
1135 }
1136 else
1137 {
1138 cupsdLogMessage(CUPSD_LOG_ERROR,
1139 "Syntax error on line %d of printers.conf.", linenum);
1140 break;
1141 }
1142 }
1143 else if (!strcasecmp(line, "AllowUser"))
1144 {
1145 if (value)
1146 {
1147 p->deny_users = 0;
1148 cupsdAddPrinterUser(p, value);
1149 }
1150 else
1151 {
1152 cupsdLogMessage(CUPSD_LOG_ERROR,
1153 "Syntax error on line %d of printers.conf.", linenum);
1154 break;
1155 }
1156 }
1157 else if (!strcasecmp(line, "DenyUser"))
1158 {
1159 if (value)
1160 {
1161 p->deny_users = 1;
1162 cupsdAddPrinterUser(p, value);
1163 }
1164 else
1165 {
1166 cupsdLogMessage(CUPSD_LOG_ERROR,
1167 "Syntax error on line %d of printers.conf.", linenum);
1168 break;
1169 }
1170 }
1171 else if (!strcasecmp(line, "QuotaPeriod"))
1172 {
1173 if (value)
1174 p->quota_period = atoi(value);
1175 else
1176 {
1177 cupsdLogMessage(CUPSD_LOG_ERROR,
1178 "Syntax error on line %d of printers.conf.", linenum);
1179 break;
1180 }
1181 }
1182 else if (!strcasecmp(line, "PageLimit"))
1183 {
1184 if (value)
1185 p->page_limit = atoi(value);
1186 else
1187 {
1188 cupsdLogMessage(CUPSD_LOG_ERROR,
1189 "Syntax error on line %d of printers.conf.", linenum);
1190 break;
1191 }
1192 }
1193 else if (!strcasecmp(line, "KLimit"))
1194 {
1195 if (value)
1196 p->k_limit = atoi(value);
1197 else
1198 {
1199 cupsdLogMessage(CUPSD_LOG_ERROR,
1200 "Syntax error on line %d of printers.conf.", linenum);
1201 break;
1202 }
1203 }
1204 else if (!strcasecmp(line, "OpPolicy"))
1205 {
1206 if (value)
1207 {
1208 cupsd_policy_t *pol; /* Policy */
1209
1210
1211 if ((pol = cupsdFindPolicy(value)) != NULL)
1212 {
1213 cupsdSetString(&p->op_policy, value);
1214 p->op_policy_ptr = pol;
1215 }
1216 else
1217 cupsdLogMessage(CUPSD_LOG_ERROR,
1218 "Bad policy \"%s\" on line %d of printers.conf",
1219 value, linenum);
1220 }
1221 else
1222 {
1223 cupsdLogMessage(CUPSD_LOG_ERROR,
1224 "Syntax error on line %d of printers.conf.", linenum);
1225 break;
1226 }
1227 }
1228 else if (!strcasecmp(line, "ErrorPolicy"))
1229 {
1230 if (value)
1231 cupsdSetString(&p->error_policy, value);
1232 else
1233 {
1234 cupsdLogMessage(CUPSD_LOG_ERROR,
1235 "Syntax error on line %d of printers.conf.", linenum);
1236 break;
1237 }
1238 }
1239 else
1240 {
1241 /*
1242 * Something else we don't understand...
1243 */
1244
1245 cupsdLogMessage(CUPSD_LOG_ERROR,
1246 "Unknown configuration directive %s on line %d of printers.conf.",
1247 line, linenum);
1248 }
1249 }
1250
1251 cupsFileClose(fp);
1252 }
1253
1254
1255 /*
1256 * 'cupsdRenamePrinter()' - Rename a printer.
1257 */
1258
1259 void
1260 cupsdRenamePrinter(
1261 cupsd_printer_t *p, /* I - Printer */
1262 const char *name) /* I - New name */
1263 {
1264 /*
1265 * Remove the printer from the array(s) first...
1266 */
1267
1268 cupsArrayRemove(Printers, p);
1269
1270 if (p->type & CUPS_PRINTER_IMPLICIT)
1271 cupsArrayRemove(ImplicitPrinters, p);
1272
1273 /*
1274 * Rename the printer type...
1275 */
1276
1277 mimeDeleteType(MimeDatabase, p->filetype);
1278 p->filetype = mimeAddType(MimeDatabase, "printer", name);
1279
1280 mimeDeleteType(MimeDatabase, p->prefiltertype);
1281 p->prefiltertype = mimeAddType(MimeDatabase, "prefilter", name);
1282
1283 /*
1284 * Rename the printer...
1285 */
1286
1287 cupsdSetString(&p->name, name);
1288
1289 /*
1290 * Reset printer attributes...
1291 */
1292
1293 cupsdSetPrinterAttrs(p);
1294
1295 /*
1296 * Add the printer back to the printer array(s)...
1297 */
1298
1299 cupsArrayAdd(Printers, p);
1300
1301 if (p->type & CUPS_PRINTER_IMPLICIT)
1302 cupsArrayAdd(ImplicitPrinters, p);
1303 }
1304
1305
1306 /*
1307 * 'cupsdSaveAllPrinters()' - Save all printer definitions to the printers.conf
1308 * file.
1309 */
1310
1311 void
1312 cupsdSaveAllPrinters(void)
1313 {
1314 int i; /* Looping var */
1315 cups_file_t *fp; /* printers.conf file */
1316 char temp[1024]; /* Temporary string */
1317 char backup[1024]; /* printers.conf.O file */
1318 cupsd_printer_t *printer; /* Current printer class */
1319 time_t curtime; /* Current time */
1320 struct tm *curdate; /* Current date */
1321 cups_option_t *option; /* Current option */
1322 const char *ptr; /* Pointer into info/location */
1323
1324
1325 /*
1326 * Create the printers.conf file...
1327 */
1328
1329 snprintf(temp, sizeof(temp), "%s/printers.conf", ServerRoot);
1330 snprintf(backup, sizeof(backup), "%s/printers.conf.O", ServerRoot);
1331
1332 if (rename(temp, backup))
1333 {
1334 if (errno != ENOENT)
1335 cupsdLogMessage(CUPSD_LOG_ERROR,
1336 "Unable to backup printers.conf - %s", strerror(errno));
1337 }
1338
1339 if ((fp = cupsFileOpen(temp, "w")) == NULL)
1340 {
1341 cupsdLogMessage(CUPSD_LOG_ERROR,
1342 "Unable to save printers.conf - %s", strerror(errno));
1343
1344 if (rename(backup, temp))
1345 cupsdLogMessage(CUPSD_LOG_ERROR,
1346 "Unable to restore printers.conf - %s", strerror(errno));
1347 return;
1348 }
1349 else
1350 cupsdLogMessage(CUPSD_LOG_INFO, "Saving printers.conf...");
1351
1352 /*
1353 * Restrict access to the file...
1354 */
1355
1356 fchown(cupsFileNumber(fp), getuid(), Group);
1357 fchmod(cupsFileNumber(fp), 0600);
1358
1359 /*
1360 * Write a small header to the file...
1361 */
1362
1363 curtime = time(NULL);
1364 curdate = localtime(&curtime);
1365 strftime(temp, sizeof(temp) - 1, "%Y-%m-%d %H:%M", curdate);
1366
1367 cupsFilePuts(fp, "# Printer configuration file for " CUPS_SVERSION "\n");
1368 cupsFilePrintf(fp, "# Written by cupsd on %s\n", temp);
1369
1370 /*
1371 * Write each local printer known to the system...
1372 */
1373
1374 for (printer = (cupsd_printer_t *)cupsArrayFirst(Printers);
1375 printer;
1376 printer = (cupsd_printer_t *)cupsArrayNext(Printers))
1377 {
1378 /*
1379 * Skip remote destinations and printer classes...
1380 */
1381
1382 if ((printer->type & CUPS_PRINTER_DISCOVERED) ||
1383 (printer->type & CUPS_PRINTER_CLASS) ||
1384 (printer->type & CUPS_PRINTER_IMPLICIT))
1385 continue;
1386
1387 /*
1388 * Write printers as needed...
1389 */
1390
1391 if (printer == DefaultPrinter)
1392 cupsFilePrintf(fp, "<DefaultPrinter %s>\n", printer->name);
1393 else
1394 cupsFilePrintf(fp, "<Printer %s>\n", printer->name);
1395
1396 if (printer->num_auth_info_required > 0)
1397 {
1398 cupsFilePrintf(fp, "AuthInfoRequired %s", printer->auth_info_required[0]);
1399 for (i = 1; i < printer->num_auth_info_required; i ++)
1400 cupsFilePrintf(fp, ",%s", printer->auth_info_required[i]);
1401 cupsFilePutChar(fp, '\n');
1402 }
1403
1404 if (printer->info)
1405 {
1406 if ((ptr = strchr(printer->info, '#')) != NULL)
1407 {
1408 /*
1409 * Need to quote the first # in the info string...
1410 */
1411
1412 cupsFilePuts(fp, "Info ");
1413 cupsFileWrite(fp, printer->info, ptr - printer->info);
1414 cupsFilePutChar(fp, '\\');
1415 cupsFilePuts(fp, ptr);
1416 cupsFilePutChar(fp, '\n');
1417 }
1418 else
1419 cupsFilePrintf(fp, "Info %s\n", printer->info);
1420 }
1421
1422 if (printer->location)
1423 {
1424 if ((ptr = strchr(printer->info, '#')) != NULL)
1425 {
1426 /*
1427 * Need to quote the first # in the location string...
1428 */
1429
1430 cupsFilePuts(fp, "Location ");
1431 cupsFileWrite(fp, printer->location, ptr - printer->location);
1432 cupsFilePutChar(fp, '\\');
1433 cupsFilePuts(fp, ptr);
1434 cupsFilePutChar(fp, '\n');
1435 }
1436 else
1437 cupsFilePrintf(fp, "Location %s\n", printer->location);
1438 }
1439 if (printer->device_uri)
1440 cupsFilePrintf(fp, "DeviceURI %s\n", printer->device_uri);
1441
1442 if (printer->port_monitor)
1443 cupsFilePrintf(fp, "PortMonitor %s\n", printer->port_monitor);
1444
1445 if (printer->state == IPP_PRINTER_STOPPED)
1446 {
1447 cupsFilePuts(fp, "State Stopped\n");
1448 cupsFilePrintf(fp, "StateMessage %s\n", printer->state_message);
1449 }
1450 else
1451 cupsFilePuts(fp, "State Idle\n");
1452
1453 cupsFilePrintf(fp, "StateTime %d\n", (int)printer->state_time);
1454
1455 if (printer->accepting)
1456 cupsFilePuts(fp, "Accepting Yes\n");
1457 else
1458 cupsFilePuts(fp, "Accepting No\n");
1459
1460 if (printer->shared)
1461 cupsFilePuts(fp, "Shared Yes\n");
1462 else
1463 cupsFilePuts(fp, "Shared No\n");
1464
1465 cupsFilePrintf(fp, "JobSheets %s %s\n", printer->job_sheets[0],
1466 printer->job_sheets[1]);
1467
1468 cupsFilePrintf(fp, "QuotaPeriod %d\n", printer->quota_period);
1469 cupsFilePrintf(fp, "PageLimit %d\n", printer->page_limit);
1470 cupsFilePrintf(fp, "KLimit %d\n", printer->k_limit);
1471
1472 for (i = 0; i < printer->num_users; i ++)
1473 cupsFilePrintf(fp, "%sUser %s\n", printer->deny_users ? "Deny" : "Allow",
1474 printer->users[i]);
1475
1476 if (printer->op_policy)
1477 cupsFilePrintf(fp, "OpPolicy %s\n", printer->op_policy);
1478 if (printer->error_policy)
1479 cupsFilePrintf(fp, "ErrorPolicy %s\n", printer->error_policy);
1480
1481 for (i = printer->num_options, option = printer->options;
1482 i > 0;
1483 i --, option ++)
1484 cupsFilePrintf(fp, "Option %s %s\n", option->name, option->value);
1485
1486 cupsFilePuts(fp, "</Printer>\n");
1487
1488 #ifdef __sgi
1489 /*
1490 * Make IRIX desktop & printer status happy
1491 */
1492
1493 write_irix_state(printer);
1494 #endif /* __sgi */
1495 }
1496
1497 cupsFileClose(fp);
1498 }
1499
1500
1501 /*
1502 * 'cupsdSetAuthInfoRequired()' - Set the required authentication info.
1503 */
1504
1505 int /* O - 1 if value OK, 0 otherwise */
1506 cupsdSetAuthInfoRequired(
1507 cupsd_printer_t *p, /* I - Printer */
1508 const char *values, /* I - Plain text value (or NULL) */
1509 ipp_attribute_t *attr) /* I - IPP attribute value (or NULL) */
1510 {
1511 int i; /* Looping var */
1512
1513
1514 p->num_auth_info_required = 0;
1515
1516 /*
1517 * Do we have a plain text value?
1518 */
1519
1520 if (values)
1521 {
1522 /*
1523 * Yes, grab the keywords...
1524 */
1525
1526 const char *end; /* End of current value */
1527
1528
1529 while (*values && p->num_auth_info_required < 4)
1530 {
1531 if ((end = strchr(values, ',')) == NULL)
1532 end = values + strlen(values);
1533
1534 if ((end - values) == 4 && !strncmp(values, "none", 4))
1535 {
1536 if (p->num_auth_info_required != 0 || *end)
1537 return (0);
1538
1539 p->auth_info_required[p->num_auth_info_required] = "none";
1540 p->num_auth_info_required ++;
1541
1542 return (1);
1543 }
1544 else if ((end - values) == 9 && !strncmp(values, "negotiate", 9))
1545 {
1546 if (p->num_auth_info_required != 0 || *end)
1547 return (0);
1548
1549 p->auth_info_required[p->num_auth_info_required] = "negotiate";
1550 p->num_auth_info_required ++;
1551 }
1552 else if ((end - values) == 6 && !strncmp(values, "domain", 6))
1553 {
1554 p->auth_info_required[p->num_auth_info_required] = "domain";
1555 p->num_auth_info_required ++;
1556 }
1557 else if ((end - values) == 8 && !strncmp(values, "password", 8))
1558 {
1559 p->auth_info_required[p->num_auth_info_required] = "password";
1560 p->num_auth_info_required ++;
1561 }
1562 else if ((end - values) == 8 && !strncmp(values, "username", 8))
1563 {
1564 p->auth_info_required[p->num_auth_info_required] = "username";
1565 p->num_auth_info_required ++;
1566 }
1567 else
1568 return (0);
1569
1570 values = (*end) ? end + 1 : end;
1571 }
1572
1573 if (p->num_auth_info_required == 0)
1574 {
1575 p->auth_info_required[0] = "none";
1576 p->num_auth_info_required = 1;
1577 }
1578
1579 /*
1580 * Update the printer-type value as needed...
1581 */
1582
1583 if (p->num_auth_info_required > 1 ||
1584 strcmp(p->auth_info_required[0], "none"))
1585 p->type |= CUPS_PRINTER_AUTHENTICATED;
1586 else
1587 p->type &= ~CUPS_PRINTER_AUTHENTICATED;
1588
1589 return (1);
1590 }
1591
1592 /*
1593 * Grab values from an attribute instead...
1594 */
1595
1596 if (!attr || attr->num_values > 4)
1597 return (0);
1598
1599 /*
1600 * Update the printer-type value as needed...
1601 */
1602
1603 if (attr->num_values > 1 ||
1604 strcmp(attr->values[0].string.text, "none"))
1605 p->type |= CUPS_PRINTER_AUTHENTICATED;
1606 else
1607 p->type &= ~CUPS_PRINTER_AUTHENTICATED;
1608
1609 for (i = 0; i < attr->num_values; i ++)
1610 {
1611 if (!strcmp(attr->values[i].string.text, "none"))
1612 {
1613 if (p->num_auth_info_required != 0 || attr->num_values != 1)
1614 return (0);
1615
1616 p->auth_info_required[p->num_auth_info_required] = "none";
1617 p->num_auth_info_required ++;
1618
1619 return (1);
1620 }
1621 else if (!strcmp(attr->values[i].string.text, "negotiate"))
1622 {
1623 if (p->num_auth_info_required != 0 || attr->num_values != 1)
1624 return (0);
1625
1626 p->auth_info_required[p->num_auth_info_required] = "negotiate";
1627 p->num_auth_info_required ++;
1628
1629 return (1);
1630 }
1631 else if (!strcmp(attr->values[i].string.text, "domain"))
1632 {
1633 p->auth_info_required[p->num_auth_info_required] = "domain";
1634 p->num_auth_info_required ++;
1635 }
1636 else if (!strcmp(attr->values[i].string.text, "password"))
1637 {
1638 p->auth_info_required[p->num_auth_info_required] = "password";
1639 p->num_auth_info_required ++;
1640 }
1641 else if (!strcmp(attr->values[i].string.text, "username"))
1642 {
1643 p->auth_info_required[p->num_auth_info_required] = "username";
1644 p->num_auth_info_required ++;
1645 }
1646 else
1647 return (0);
1648 }
1649
1650 return (1);
1651 }
1652
1653
1654 /*
1655 * 'cupsdSetPrinterAttr()' - Set a printer attribute.
1656 */
1657
1658 void
1659 cupsdSetPrinterAttr(
1660 cupsd_printer_t *p, /* I - Printer */
1661 const char *name, /* I - Attribute name */
1662 char *value) /* I - Attribute value string */
1663 {
1664 ipp_attribute_t *attr; /* Attribute */
1665 int i, /* Looping var */
1666 count; /* Number of values */
1667 char *ptr; /* Pointer into value */
1668 ipp_tag_t value_tag; /* Value tag for this attribute */
1669
1670
1671 /*
1672 * Count the number of values...
1673 */
1674
1675 for (count = 1, ptr = value;
1676 (ptr = strchr(ptr, ',')) != NULL;
1677 ptr ++, count ++);
1678
1679 /*
1680 * Then add or update the attribute as needed...
1681 */
1682
1683 if (!strcmp(name, "marker-levels"))
1684 {
1685 /*
1686 * Integer values...
1687 */
1688
1689 if ((attr = ippFindAttribute(p->attrs, name, IPP_TAG_INTEGER)) != NULL &&
1690 attr->num_values < count)
1691 {
1692 ippDeleteAttribute(p->attrs, attr);
1693 attr = NULL;
1694 }
1695
1696 if (attr)
1697 attr->num_values = count;
1698 else
1699 attr = ippAddIntegers(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER, name,
1700 count, NULL);
1701
1702 if (!attr)
1703 {
1704 cupsdLogMessage(CUPSD_LOG_ERROR,
1705 "Unable to allocate memory for printer attribute "
1706 "(%d values)", count);
1707 return;
1708 }
1709
1710 for (i = 0; i < count; i ++)
1711 {
1712 if ((ptr = strchr(value, ',')) != NULL)
1713 *ptr++ = '\0';
1714
1715 attr->values[i].integer = strtol(value, NULL, 10);
1716
1717 if (ptr)
1718 value = ptr;
1719 }
1720 }
1721 else
1722 {
1723 /*
1724 * Name or keyword values...
1725 */
1726
1727 if (!strcmp(name, "marker-types"))
1728 value_tag = IPP_TAG_KEYWORD;
1729 else
1730 value_tag = IPP_TAG_NAME;
1731
1732 if ((attr = ippFindAttribute(p->attrs, name, value_tag)) != NULL &&
1733 attr->num_values < count)
1734 {
1735 ippDeleteAttribute(p->attrs, attr);
1736 attr = NULL;
1737 }
1738
1739 if (attr)
1740 attr->num_values = count;
1741 else
1742 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, value_tag, name,
1743 count, NULL, NULL);
1744
1745 if (!attr)
1746 {
1747 cupsdLogMessage(CUPSD_LOG_ERROR,
1748 "Unable to allocate memory for printer attribute "
1749 "(%d values)", count);
1750 return;
1751 }
1752
1753 for (i = 0; i < count; i ++)
1754 {
1755 if ((ptr = strchr(value, ',')) != NULL)
1756 *ptr++ = '\0';
1757
1758 _cupsStrFree(attr->values[i].string.text);
1759 attr->values[i].string.text = _cupsStrAlloc(value);
1760
1761 if (ptr)
1762 value = ptr;
1763 }
1764 }
1765 }
1766
1767
1768 /*
1769 * 'cupsdSetPrinterAttrs()' - Set printer attributes based upon the PPD file.
1770 */
1771
1772 void
1773 cupsdSetPrinterAttrs(cupsd_printer_t *p)/* I - Printer to setup */
1774 {
1775 int i, /* Looping var */
1776 length; /* Length of browse attributes */
1777 char uri[HTTP_MAX_URI]; /* URI for printer */
1778 char resource[HTTP_MAX_URI]; /* Resource portion of URI */
1779 char filename[1024]; /* Name of PPD file */
1780 int num_air; /* Number of auth-info-required values */
1781 const char * const *air; /* auth-info-required values */
1782 int num_media; /* Number of media options */
1783 cupsd_location_t *auth; /* Pointer to authentication element */
1784 const char *auth_supported; /* Authentication supported */
1785 ppd_file_t *ppd; /* PPD file data */
1786 ppd_option_t *input_slot, /* InputSlot options */
1787 *media_type, /* MediaType options */
1788 *page_size, /* PageSize options */
1789 *output_bin, /* OutputBin options */
1790 *media_quality, /* EFMediaQualityMode options */
1791 *duplex; /* Duplex options */
1792 ppd_attr_t *ppdattr; /* PPD attribute */
1793 ipp_attribute_t *attr; /* Attribute data */
1794 ipp_value_t *val; /* Attribute value */
1795 int num_finishings; /* Number of finishings */
1796 int finishings[5]; /* finishings-supported values */
1797 cups_option_t *option; /* Current printer option */
1798 static const char * const sides[3] = /* sides-supported values */
1799 {
1800 "one-sided",
1801 "two-sided-long-edge",
1802 "two-sided-short-edge"
1803 };
1804 static const char * const air_userpass[] =
1805 { /* Basic/Digest authentication */
1806 "username",
1807 "password"
1808 };
1809 #ifdef HAVE_GSSAPI
1810 static const char * const air_negotiate[] =
1811 { /* Kerberos authentication */
1812 "negotiate"
1813 };
1814 #endif /* HAVE_GSSAPI */
1815 static const char * const air_none[] =
1816 { /* No authentication */
1817 "none"
1818 };
1819
1820
1821 DEBUG_printf(("cupsdSetPrinterAttrs: entering name = %s, type = %x\n", p->name,
1822 p->type));
1823
1824 /*
1825 * Make sure that we have the common attributes defined...
1826 */
1827
1828 if (!CommonData)
1829 cupsdCreateCommonData();
1830
1831 /*
1832 * Clear out old filters, if any...
1833 */
1834
1835 delete_printer_filters(p);
1836
1837 /*
1838 * Figure out the authentication that is required for the printer.
1839 */
1840
1841 auth_supported = "requesting-user-name";
1842 num_air = 1;
1843 air = air_none;
1844
1845 if (p->num_auth_info_required > 0 && strcmp(p->auth_info_required[0], "none"))
1846 {
1847 num_air = p->num_auth_info_required;
1848 air = p->auth_info_required;
1849
1850 if (!strcmp(air[0], "username"))
1851 auth_supported = "basic";
1852 else
1853 auth_supported = "negotiate";
1854 }
1855 else if (!(p->type & CUPS_PRINTER_DISCOVERED))
1856 {
1857 if (p->type & CUPS_PRINTER_CLASS)
1858 snprintf(resource, sizeof(resource), "/classes/%s", p->name);
1859 else
1860 snprintf(resource, sizeof(resource), "/printers/%s", p->name);
1861
1862 if ((auth = cupsdFindBest(resource, HTTP_POST)) == NULL ||
1863 auth->type == CUPSD_AUTH_NONE)
1864 auth = cupsdFindPolicyOp(p->op_policy_ptr, IPP_PRINT_JOB);
1865
1866 if (auth)
1867 {
1868 if (auth->type == CUPSD_AUTH_BASIC || auth->type == CUPSD_AUTH_BASICDIGEST)
1869 {
1870 auth_supported = "basic";
1871 num_air = 2;
1872 air = air_userpass;
1873 }
1874 else if (auth->type == CUPSD_AUTH_DIGEST)
1875 {
1876 auth_supported = "digest";
1877 num_air = 2;
1878 air = air_userpass;
1879 }
1880 #ifdef HAVE_GSSAPI
1881 else if (auth->type == CUPSD_AUTH_NEGOTIATE)
1882 {
1883 auth_supported = "negotiate";
1884 num_air = 1;
1885 air = air_negotiate;
1886 }
1887 #endif /* HAVE_GSSAPI */
1888
1889 if (auth->type != CUPSD_AUTH_NONE)
1890 p->type |= CUPS_PRINTER_AUTHENTICATED;
1891 else
1892 p->type &= ~CUPS_PRINTER_AUTHENTICATED;
1893 }
1894 else
1895 p->type &= ~CUPS_PRINTER_AUTHENTICATED;
1896 }
1897 else if (p->type & CUPS_PRINTER_AUTHENTICATED)
1898 {
1899 num_air = 2;
1900 air = air_userpass;
1901 }
1902
1903 /*
1904 * Create the required IPP attributes for a printer...
1905 */
1906
1907 if (p->attrs)
1908 ippDelete(p->attrs);
1909
1910 p->attrs = ippNew();
1911
1912 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
1913 "uri-authentication-supported", NULL, auth_supported);
1914 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
1915 "uri-security-supported", NULL, "none");
1916 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME, "printer-name", NULL,
1917 p->name);
1918 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-location",
1919 NULL, p->location ? p->location : "");
1920 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-info",
1921 NULL, p->info ? p->info : "");
1922 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI, "printer-more-info",
1923 NULL, p->uri);
1924
1925 if (p->num_users)
1926 {
1927 if (p->deny_users)
1928 ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
1929 "requesting-user-name-denied", p->num_users, NULL,
1930 p->users);
1931 else
1932 ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
1933 "requesting-user-name-allowed", p->num_users, NULL,
1934 p->users);
1935 }
1936
1937 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
1938 "job-quota-period", p->quota_period);
1939 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
1940 "job-k-limit", p->k_limit);
1941 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
1942 "job-page-limit", p->page_limit);
1943 ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
1944 "auth-info-required", num_air, NULL, air);
1945
1946 if (cupsArrayCount(Banners) > 0 && !(p->type & CUPS_PRINTER_DISCOVERED))
1947 {
1948 /*
1949 * Setup the job-sheets-default attribute...
1950 */
1951
1952 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
1953 "job-sheets-default", 2, NULL, NULL);
1954
1955 if (attr != NULL)
1956 {
1957 attr->values[0].string.text = _cupsStrAlloc(Classification ?
1958 Classification : p->job_sheets[0]);
1959 attr->values[1].string.text = _cupsStrAlloc(Classification ?
1960 Classification : p->job_sheets[1]);
1961 }
1962 }
1963
1964 p->raw = 0;
1965 p->remote = 0;
1966
1967 if (p->type & CUPS_PRINTER_DISCOVERED)
1968 {
1969 /*
1970 * Tell the client this is a remote printer of some type...
1971 */
1972
1973 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI,
1974 "printer-uri-supported", NULL, p->uri);
1975
1976 if (p->make_model)
1977 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
1978 "printer-make-and-model", NULL, p->make_model);
1979
1980 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI, "device-uri", NULL,
1981 p->uri);
1982
1983 p->raw = 1;
1984 p->remote = 1;
1985 }
1986 else
1987 {
1988 /*
1989 * Assign additional attributes depending on whether this is a printer
1990 * or class...
1991 */
1992
1993 p->type &= ~CUPS_PRINTER_OPTIONS;
1994
1995 if (p->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT))
1996 {
1997 p->raw = 1;
1998
1999 /*
2000 * Add class-specific attributes...
2001 */
2002
2003 if ((p->type & CUPS_PRINTER_IMPLICIT) && p->num_printers > 0 &&
2004 p->printers[0]->make_model)
2005 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
2006 "printer-make-and-model", NULL, p->printers[0]->make_model);
2007 else
2008 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
2009 "printer-make-and-model", NULL, "Local Printer Class");
2010
2011 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI, "device-uri", NULL,
2012 "file:///dev/null");
2013
2014 if (p->num_printers > 0)
2015 {
2016 /*
2017 * Add a list of member URIs and names...
2018 */
2019
2020 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI,
2021 "member-uris", p->num_printers, NULL, NULL);
2022 p->type |= CUPS_PRINTER_OPTIONS;
2023
2024 for (i = 0; i < p->num_printers; i ++)
2025 {
2026 if (attr != NULL)
2027 attr->values[i].string.text = _cupsStrAlloc(p->printers[i]->uri);
2028
2029 p->type &= ~CUPS_PRINTER_OPTIONS | p->printers[i]->type;
2030 }
2031
2032 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
2033 "member-names", p->num_printers, NULL, NULL);
2034
2035 if (attr != NULL)
2036 {
2037 for (i = 0; i < p->num_printers; i ++)
2038 attr->values[i].string.text = _cupsStrAlloc(p->printers[i]->name);
2039 }
2040 }
2041 }
2042 else
2043 {
2044 /*
2045 * Add printer-specific attributes... Start by sanitizing the device
2046 * URI so it doesn't have a username or password in it...
2047 */
2048
2049 if (!p->device_uri)
2050 strcpy(uri, "file:/dev/null");
2051 else if (strstr(p->device_uri, "://") != NULL)
2052 {
2053 /*
2054 * http://..., ipp://..., etc.
2055 */
2056
2057 cupsdSanitizeURI(p->device_uri, uri, sizeof(uri));
2058 }
2059 else
2060 {
2061 /*
2062 * file:..., serial:..., etc.
2063 */
2064
2065 strlcpy(uri, p->device_uri, sizeof(uri));
2066 }
2067
2068 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI, "device-uri", NULL,
2069 uri);
2070
2071 /*
2072 * Assign additional attributes from the PPD file (if any)...
2073 */
2074
2075 p->type |= CUPS_PRINTER_BW;
2076 finishings[0] = IPP_FINISHINGS_NONE;
2077 num_finishings = 1;
2078
2079 snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot,
2080 p->name);
2081
2082 if ((ppd = ppdOpenFile(filename)) != NULL)
2083 {
2084 /*
2085 * Add make/model and other various attributes...
2086 */
2087
2088 if (ppd->color_device)
2089 p->type |= CUPS_PRINTER_COLOR;
2090 if (ppd->variable_sizes)
2091 p->type |= CUPS_PRINTER_VARIABLE;
2092 if (!ppd->manual_copies)
2093 p->type |= CUPS_PRINTER_COPIES;
2094 if ((ppdattr = ppdFindAttr(ppd, "cupsFax", NULL)) != NULL)
2095 if (ppdattr->value && !strcasecmp(ppdattr->value, "true"))
2096 p->type |= CUPS_PRINTER_FAX;
2097
2098 ippAddBoolean(p->attrs, IPP_TAG_PRINTER, "color-supported",
2099 ppd->color_device);
2100 if (ppd->throughput)
2101 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
2102 "pages-per-minute", ppd->throughput);
2103
2104 if (ppd->nickname)
2105 {
2106 /*
2107 * The NickName can be localized in the character set specified
2108 * by the LanugageEncoding attribute. However, ppdOpen2() has
2109 * already converted the ppd->nickname member to UTF-8 for us
2110 * (the original attribute value is available separately)
2111 */
2112
2113 cupsdSetString(&p->make_model, ppd->nickname);
2114 }
2115 else if (ppd->modelname)
2116 {
2117 /*
2118 * Model name can only contain specific characters...
2119 */
2120
2121 cupsdSetString(&p->make_model, ppd->modelname);
2122 }
2123 else
2124 cupsdSetString(&p->make_model, "Bad PPD File");
2125
2126 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
2127 "printer-make-and-model", NULL, p->make_model);
2128
2129 /*
2130 * Add media options from the PPD file...
2131 */
2132
2133 if ((input_slot = ppdFindOption(ppd, "InputSlot")) != NULL)
2134 num_media = input_slot->num_choices;
2135 else
2136 num_media = 0;
2137
2138 if ((media_type = ppdFindOption(ppd, "MediaType")) != NULL)
2139 num_media += media_type->num_choices;
2140
2141 if ((page_size = ppdFindOption(ppd, "PageSize")) != NULL)
2142 num_media += page_size->num_choices;
2143
2144 if ((media_quality = ppdFindOption(ppd, "EFMediaQualityMode")) != NULL)
2145 num_media += media_quality->num_choices;
2146
2147 if (num_media == 0)
2148 {
2149 cupsdLogMessage(CUPSD_LOG_CRIT,
2150 "The PPD file for printer %s contains no media "
2151 "options and is therefore invalid!", p->name);
2152 }
2153 else
2154 {
2155 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2156 "media-supported", num_media, NULL, NULL);
2157 if (attr != NULL)
2158 {
2159 val = attr->values;
2160
2161 if (input_slot != NULL)
2162 for (i = 0; i < input_slot->num_choices; i ++, val ++)
2163 val->string.text = _cupsStrAlloc(input_slot->choices[i].choice);
2164
2165 if (media_type != NULL)
2166 for (i = 0; i < media_type->num_choices; i ++, val ++)
2167 val->string.text = _cupsStrAlloc(media_type->choices[i].choice);
2168
2169 if (media_quality != NULL)
2170 for (i = 0; i < media_quality->num_choices; i ++, val ++)
2171 val->string.text = _cupsStrAlloc(media_quality->choices[i].choice);
2172
2173 if (page_size != NULL)
2174 {
2175 for (i = 0; i < page_size->num_choices; i ++, val ++)
2176 val->string.text = _cupsStrAlloc(page_size->choices[i].choice);
2177
2178 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2179 "media-default", NULL, page_size->defchoice);
2180 }
2181 else if (input_slot != NULL)
2182 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2183 "media-default", NULL, input_slot->defchoice);
2184 else if (media_type != NULL)
2185 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2186 "media-default", NULL, media_type->defchoice);
2187 else if (media_quality != NULL)
2188 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2189 "media-default", NULL, media_quality->defchoice);
2190 else
2191 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2192 "media-default", NULL, "none");
2193 }
2194 }
2195
2196 /*
2197 * Output bin...
2198 */
2199
2200 if ((output_bin = ppdFindOption(ppd, "OutputBin")) != NULL)
2201 {
2202 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2203 "output-bin-supported", output_bin->num_choices,
2204 NULL, NULL);
2205
2206 if (attr != NULL)
2207 {
2208 for (i = 0, val = attr->values;
2209 i < output_bin->num_choices;
2210 i ++, val ++)
2211 val->string.text = _cupsStrAlloc(output_bin->choices[i].choice);
2212 }
2213 }
2214
2215 /*
2216 * Duplexing, etc...
2217 */
2218
2219 if ((duplex = ppdFindOption(ppd, "Duplex")) == NULL)
2220 if ((duplex = ppdFindOption(ppd, "EFDuplex")) == NULL)
2221 if ((duplex = ppdFindOption(ppd, "EFDuplexing")) == NULL)
2222 if ((duplex = ppdFindOption(ppd, "KD03Duplex")) == NULL)
2223 duplex = ppdFindOption(ppd, "JCLDuplex");
2224
2225 if (duplex && duplex->num_choices > 1)
2226 {
2227 p->type |= CUPS_PRINTER_DUPLEX;
2228
2229 ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2230 "sides-supported", 3, NULL, sides);
2231
2232 if (!strcasecmp(duplex->defchoice, "DuplexTumble"))
2233 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2234 "sides-default", NULL, "two-sided-short-edge");
2235 else if (!strcasecmp(duplex->defchoice, "DuplexNoTumble"))
2236 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2237 "sides-default", NULL, "two-sided-long-edge");
2238 else
2239 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2240 "sides-default", NULL, "one-sided");
2241 }
2242
2243 if (ppdFindOption(ppd, "Collate") != NULL)
2244 p->type |= CUPS_PRINTER_COLLATE;
2245
2246 if (ppdFindOption(ppd, "StapleLocation") != NULL)
2247 {
2248 p->type |= CUPS_PRINTER_STAPLE;
2249 finishings[num_finishings++] = IPP_FINISHINGS_STAPLE;
2250 }
2251
2252 if (ppdFindOption(ppd, "BindEdge") != NULL)
2253 {
2254 p->type |= CUPS_PRINTER_BIND;
2255 finishings[num_finishings++] = IPP_FINISHINGS_BIND;
2256 }
2257
2258 for (i = 0; i < ppd->num_sizes; i ++)
2259 if (ppd->sizes[i].length > 1728)
2260 p->type |= CUPS_PRINTER_LARGE;
2261 else if (ppd->sizes[i].length > 1008)
2262 p->type |= CUPS_PRINTER_MEDIUM;
2263 else
2264 p->type |= CUPS_PRINTER_SMALL;
2265
2266 /*
2267 * Add a filter from application/vnd.cups-raw to printer/name to
2268 * handle "raw" printing by users.
2269 */
2270
2271 add_printer_filter(p, p->filetype, "application/vnd.cups-raw 0 -");
2272
2273 /*
2274 * Add any pre-filters in the PPD file...
2275 */
2276
2277 if ((ppdattr = ppdFindAttr(ppd, "cupsPreFilter", NULL)) != NULL)
2278 {
2279 p->prefiltertype = mimeAddType(MimeDatabase, "prefilter", p->name);
2280
2281 for (; ppdattr; ppdattr = ppdFindNextAttr(ppd, "cupsPreFilter", NULL))
2282 if (ppdattr->value)
2283 add_printer_filter(p, p->prefiltertype, ppdattr->value);
2284 }
2285
2286 /*
2287 * Add any filters in the PPD file...
2288 */
2289
2290 DEBUG_printf(("ppd->num_filters = %d\n", ppd->num_filters));
2291 for (i = 0; i < ppd->num_filters; i ++)
2292 {
2293 DEBUG_printf(("ppd->filters[%d] = \"%s\"\n", i, ppd->filters[i]));
2294 add_printer_filter(p, p->filetype, ppd->filters[i]);
2295 }
2296
2297 if (ppd->num_filters == 0)
2298 {
2299 /*
2300 * If there are no filters, add PostScript printing filters.
2301 */
2302
2303 add_printer_filter(p, p->filetype,
2304 "application/vnd.cups-command 0 commandtops");
2305 add_printer_filter(p, p->filetype,
2306 "application/vnd.cups-postscript 0 -");
2307
2308 p->type |= CUPS_PRINTER_COMMANDS;
2309 }
2310
2311 /*
2312 * Show current and available port monitors for this printer...
2313 */
2314
2315 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME, "port-monitor",
2316 NULL, p->port_monitor ? p->port_monitor : "none");
2317
2318 for (i = 1, ppdattr = ppdFindAttr(ppd, "cupsPortMonitor", NULL);
2319 ppdattr;
2320 i ++, ppdattr = ppdFindNextAttr(ppd, "cupsPortMonitor", NULL));
2321
2322 if (ppd->protocols)
2323 {
2324 if (strstr(ppd->protocols, "TBCP"))
2325 i ++;
2326 else if (strstr(ppd->protocols, "BCP"))
2327 i ++;
2328 }
2329
2330 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
2331 "port-monitor-supported", i, NULL, NULL);
2332
2333 attr->values[0].string.text = _cupsStrAlloc("none");
2334
2335 for (i = 1, ppdattr = ppdFindAttr(ppd, "cupsPortMonitor", NULL);
2336 ppdattr;
2337 i ++, ppdattr = ppdFindNextAttr(ppd, "cupsPortMonitor", NULL))
2338 attr->values[i].string.text = _cupsStrAlloc(ppdattr->value);
2339
2340 if (ppd->protocols)
2341 {
2342 if (strstr(ppd->protocols, "TBCP"))
2343 attr->values[i].string.text = _cupsStrAlloc("tbcp");
2344 else if (strstr(ppd->protocols, "BCP"))
2345 attr->values[i].string.text = _cupsStrAlloc("bcp");
2346 }
2347
2348 #ifdef HAVE_DNSSD
2349 cupsdSetString(&p->product, ppd->product);
2350 #endif /* HAVE_DNSSD */
2351
2352 if (ppdFindAttr(ppd, "APRemoteQueueID", NULL))
2353 p->type |= CUPS_PRINTER_REMOTE;
2354
2355 /*
2356 * Close the PPD and set the type...
2357 */
2358
2359 ppdClose(ppd);
2360 }
2361 else if (!access(filename, 0))
2362 {
2363 int pline; /* PPD line number */
2364 ppd_status_t pstatus; /* PPD load status */
2365
2366
2367 pstatus = ppdLastError(&pline);
2368
2369 cupsdLogMessage(CUPSD_LOG_ERROR, "PPD file for %s cannot be loaded!",
2370 p->name);
2371
2372 if (pstatus <= PPD_ALLOC_ERROR)
2373 cupsdLogMessage(CUPSD_LOG_ERROR, "%s", strerror(errno));
2374 else
2375 cupsdLogMessage(CUPSD_LOG_ERROR, "%s on line %d.",
2376 ppdErrorString(pstatus), pline);
2377
2378 cupsdLogMessage(CUPSD_LOG_INFO,
2379 "Hint: Run \"cupstestppd %s\" and fix any errors.",
2380 filename);
2381
2382 /*
2383 * Add a filter from application/vnd.cups-raw to printer/name to
2384 * handle "raw" printing by users.
2385 */
2386
2387 add_printer_filter(p, p->filetype, "application/vnd.cups-raw 0 -");
2388
2389 /*
2390 * Add a PostScript filter, since this is still possibly PS printer.
2391 */
2392
2393 add_printer_filter(p, p->filetype,
2394 "application/vnd.cups-postscript 0 -");
2395 }
2396 else
2397 {
2398 /*
2399 * If we have an interface script, add a filter entry for it...
2400 */
2401
2402 snprintf(filename, sizeof(filename), "%s/interfaces/%s", ServerRoot,
2403 p->name);
2404 if (!access(filename, X_OK))
2405 {
2406 /*
2407 * Yes, we have a System V style interface script; use it!
2408 */
2409
2410 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
2411 "printer-make-and-model", NULL,
2412 "Local System V Printer");
2413
2414 snprintf(filename, sizeof(filename), "*/* 0 %s/interfaces/%s",
2415 ServerRoot, p->name);
2416 add_printer_filter(p, p->filetype, filename);
2417 }
2418 else if (p->device_uri &&
2419 !strncmp(p->device_uri, "ipp://", 6) &&
2420 (strstr(p->device_uri, "/printers/") != NULL ||
2421 strstr(p->device_uri, "/classes/") != NULL))
2422 {
2423 /*
2424 * Tell the client this is really a hard-wired remote printer.
2425 */
2426
2427 p->type |= CUPS_PRINTER_REMOTE;
2428
2429 /*
2430 * Point the printer-uri-supported attribute to the
2431 * remote printer...
2432 */
2433
2434 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI,
2435 "printer-uri-supported", NULL, p->device_uri);
2436
2437 /*
2438 * Then set the make-and-model accordingly...
2439 */
2440
2441 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
2442 "printer-make-and-model", NULL, "Remote Printer");
2443
2444 /*
2445 * Print all files directly...
2446 */
2447
2448 p->raw = 1;
2449 p->remote = 1;
2450 }
2451 else
2452 {
2453 /*
2454 * Otherwise we have neither - treat this as a "dumb" printer
2455 * with no PPD file...
2456 */
2457
2458 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
2459 "printer-make-and-model", NULL, "Local Raw Printer");
2460
2461 p->raw = 1;
2462 }
2463 }
2464
2465 ippAddIntegers(p->attrs, IPP_TAG_PRINTER, IPP_TAG_ENUM,
2466 "finishings-supported", num_finishings, finishings);
2467 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_ENUM,
2468 "finishings-default", IPP_FINISHINGS_NONE);
2469 }
2470 }
2471
2472 /*
2473 * Force sharing off for remote queues...
2474 */
2475
2476 if (p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT))
2477 p->shared = 0;
2478 else
2479 {
2480 /*
2481 * Copy the printer options into a browse attributes string we can re-use.
2482 */
2483
2484 const char *valptr; /* Pointer into value */
2485 char *attrptr; /* Pointer into attribute string */
2486
2487
2488 /*
2489 * Free the old browse attributes as needed...
2490 */
2491
2492 if (p->browse_attrs)
2493 free(p->browse_attrs);
2494
2495 /*
2496 * Compute the length of all attributes + job-sheets, lease-duration,
2497 * and BrowseLocalOptions.
2498 */
2499
2500 for (length = 1, i = p->num_options, option = p->options;
2501 i > 0;
2502 i --, option ++)
2503 {
2504 length += strlen(option->name) + 2;
2505
2506 if (option->value)
2507 {
2508 for (valptr = option->value; *valptr; valptr ++)
2509 if (strchr(" \"\'\\", *valptr))
2510 length += 2;
2511 else
2512 length ++;
2513 }
2514 }
2515
2516 length += 13 + strlen(p->job_sheets[0]) + strlen(p->job_sheets[1]);
2517 length += 32;
2518 if (BrowseLocalOptions)
2519 length += 12 + strlen(BrowseLocalOptions);
2520
2521 if (p->num_auth_info_required > 0)
2522 {
2523 length += 18; /* auth-info-required */
2524
2525 for (i = 0; i < p->num_auth_info_required; i ++)
2526 length += strlen(p->auth_info_required[i]) + 1;
2527 }
2528
2529 /*
2530 * Allocate the new string...
2531 */
2532
2533 if ((p->browse_attrs = calloc(1, length)) == NULL)
2534 cupsdLogMessage(CUPSD_LOG_ERROR,
2535 "Unable to allocate %d bytes for browse data!",
2536 length);
2537 else
2538 {
2539 /*
2540 * Got the allocated string, now copy the options and attributes over...
2541 */
2542
2543 sprintf(p->browse_attrs, "job-sheets=%s,%s lease-duration=%d",
2544 p->job_sheets[0], p->job_sheets[1], BrowseTimeout);
2545 attrptr = p->browse_attrs + strlen(p->browse_attrs);
2546
2547 if (BrowseLocalOptions)
2548 {
2549 sprintf(attrptr, " ipp-options=%s", BrowseLocalOptions);
2550 attrptr += strlen(attrptr);
2551 }
2552
2553 for (i = p->num_options, option = p->options;
2554 i > 0;
2555 i --, option ++)
2556 {
2557 *attrptr++ = ' ';
2558 strcpy(attrptr, option->name);
2559 attrptr += strlen(attrptr);
2560
2561 if (option->value)
2562 {
2563 *attrptr++ = '=';
2564
2565 for (valptr = option->value; *valptr; valptr ++)
2566 {
2567 if (strchr(" \"\'\\", *valptr))
2568 *attrptr++ = '\\';
2569
2570 *attrptr++ = *valptr;
2571 }
2572 }
2573 }
2574
2575 if (p->num_auth_info_required > 0)
2576 {
2577 strcpy(attrptr, "auth-info-required");
2578 attrptr += 18;
2579
2580 for (i = 0; i < p->num_auth_info_required; i ++)
2581 {
2582 *attrptr++ = i ? ',' : '=';
2583 strcpy(attrptr, p->auth_info_required[i]);
2584 attrptr += strlen(attrptr);
2585 }
2586 }
2587 else
2588 *attrptr = '\0';
2589 }
2590 }
2591
2592 /*
2593 * Populate the document-format-supported attribute...
2594 */
2595
2596 add_printer_formats(p);
2597
2598 DEBUG_printf(("cupsdSetPrinterAttrs: leaving name = %s, type = %x\n", p->name,
2599 p->type));
2600
2601 /*
2602 * Add name-default attributes...
2603 */
2604
2605 add_printer_defaults(p);
2606
2607 #ifdef __sgi
2608 /*
2609 * Write the IRIX printer config and status files...
2610 */
2611
2612 write_irix_config(p);
2613 write_irix_state(p);
2614 #endif /* __sgi */
2615
2616 /*
2617 * Let the browse protocols reflect the change
2618 */
2619
2620 cupsdRegisterPrinter(p);
2621 }
2622
2623
2624 /*
2625 * 'cupsdSetPrinterReasons()' - Set/update the reasons strings.
2626 */
2627
2628 void
2629 cupsdSetPrinterReasons(
2630 cupsd_printer_t *p, /* I - Printer */
2631 const char *s) /* I - Reasons strings */
2632 {
2633 int i; /* Looping var */
2634 const char *sptr; /* Pointer into reasons */
2635 char reason[255], /* Reason string */
2636 *rptr; /* Pointer into reason */
2637
2638
2639 if (s[0] == '-' || s[0] == '+')
2640 {
2641 /*
2642 * Add/remove reasons...
2643 */
2644
2645 sptr = s + 1;
2646 }
2647 else
2648 {
2649 /*
2650 * Replace reasons...
2651 */
2652
2653 sptr = s;
2654
2655 for (i = 0; i < p->num_reasons; i ++)
2656 free(p->reasons[i]);
2657
2658 p->num_reasons = 0;
2659 }
2660
2661 if (!strcmp(s, "none"))
2662 return;
2663
2664 /*
2665 * Loop through all of the reasons...
2666 */
2667
2668 while (*sptr)
2669 {
2670 /*
2671 * Skip leading whitespace and commas...
2672 */
2673
2674 while (isspace(*sptr & 255) || *sptr == ',')
2675 sptr ++;
2676
2677 for (rptr = reason; *sptr && !isspace(*sptr & 255) && *sptr != ','; sptr ++)
2678 if (rptr < (reason + sizeof(reason) - 1))
2679 *rptr++ = *sptr;
2680
2681 if (rptr == reason)
2682 break;
2683
2684 *rptr = '\0';
2685
2686 if (s[0] == '-')
2687 {
2688 /*
2689 * Remove reason...
2690 */
2691
2692 for (i = 0; i < p->num_reasons; i ++)
2693 if (!strcasecmp(reason, p->reasons[i]))
2694 {
2695 /*
2696 * Found a match, so remove it...
2697 */
2698
2699 p->num_reasons --;
2700 free(p->reasons[i]);
2701
2702 if (i < p->num_reasons)
2703 memmove(p->reasons + i, p->reasons + i + 1,
2704 (p->num_reasons - i) * sizeof(char *));
2705
2706 i --;
2707
2708 if (!strcmp(reason, "paused") && p->state == IPP_PRINTER_STOPPED)
2709 cupsdSetPrinterState(p, IPP_PRINTER_IDLE, 1);
2710 }
2711 }
2712 else if (p->num_reasons < (int)(sizeof(p->reasons) / sizeof(p->reasons[0])))
2713 {
2714 /*
2715 * Add reason...
2716 */
2717
2718 for (i = 0; i < p->num_reasons; i ++)
2719 if (!strcasecmp(reason, p->reasons[i]))
2720 break;
2721
2722 if (i >= p->num_reasons)
2723 {
2724 p->reasons[i] = strdup(reason);
2725 p->num_reasons ++;
2726
2727 if (!strcmp(reason, "paused") && p->state != IPP_PRINTER_STOPPED)
2728 cupsdSetPrinterState(p, IPP_PRINTER_STOPPED, 1);
2729 }
2730 }
2731 }
2732 }
2733
2734
2735 /*
2736 * 'cupsdSetPrinterState()' - Update the current state of a printer.
2737 */
2738
2739 void
2740 cupsdSetPrinterState(
2741 cupsd_printer_t *p, /* I - Printer to change */
2742 ipp_pstate_t s, /* I - New state */
2743 int update) /* I - Update printers.conf? */
2744 {
2745 ipp_pstate_t old_state; /* Old printer state */
2746
2747
2748 /*
2749 * Can't set status of remote printers...
2750 */
2751
2752 if (p->type & CUPS_PRINTER_DISCOVERED)
2753 return;
2754
2755 /*
2756 * Set the new state...
2757 */
2758
2759 old_state = p->state;
2760 p->state = s;
2761
2762 if (old_state != s)
2763 {
2764 cupsdAddEvent(s == IPP_PRINTER_STOPPED ? CUPSD_EVENT_PRINTER_STOPPED :
2765 CUPSD_EVENT_PRINTER_STATE, p, NULL,
2766 "%s \"%s\" state changed.",
2767 (p->type & CUPS_PRINTER_CLASS) ? "Class" : "Printer",
2768 p->name);
2769
2770 /*
2771 * Let the browse code know this needs to be updated...
2772 */
2773
2774 BrowseNext = p;
2775 p->state_time = time(NULL);
2776 p->browse_time = 0;
2777
2778 #ifdef __sgi
2779 write_irix_state(p);
2780 #endif /* __sgi */
2781 }
2782
2783 cupsdAddPrinterHistory(p);
2784
2785 /*
2786 * Let the browse protocols reflect the change...
2787 */
2788
2789 if (update)
2790 cupsdRegisterPrinter(p);
2791
2792 /*
2793 * Save the printer configuration if a printer goes from idle or processing
2794 * to stopped (or visa-versa)...
2795 */
2796
2797 if ((old_state == IPP_PRINTER_STOPPED) != (s == IPP_PRINTER_STOPPED) &&
2798 update)
2799 {
2800 if (p->type & CUPS_PRINTER_CLASS)
2801 cupsdMarkDirty(CUPSD_DIRTY_CLASSES);
2802 else
2803 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
2804 }
2805 }
2806
2807
2808 /*
2809 * 'cupsdStopPrinter()' - Stop a printer from printing any jobs...
2810 */
2811
2812 void
2813 cupsdStopPrinter(cupsd_printer_t *p, /* I - Printer to stop */
2814 int update)/* I - Update printers.conf? */
2815 {
2816 cupsd_job_t *job; /* Active print job */
2817
2818
2819 /*
2820 * Set the printer state...
2821 */
2822
2823 cupsdSetPrinterState(p, IPP_PRINTER_STOPPED, update);
2824
2825 /*
2826 * See if we have a job printing on this printer...
2827 */
2828
2829 if (p->job)
2830 {
2831 /*
2832 * Get pointer to job...
2833 */
2834
2835 job = (cupsd_job_t *)p->job;
2836
2837 /*
2838 * Stop it...
2839 */
2840
2841 cupsdStopJob(job, 0);
2842
2843 /*
2844 * Reset the state to pending...
2845 */
2846
2847 job->state->values[0].integer = IPP_JOB_PENDING;
2848 job->state_value = IPP_JOB_PENDING;
2849 job->dirty = 1;
2850
2851 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
2852
2853 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED, p, job,
2854 "Job stopped due to printer being paused");
2855 }
2856 }
2857
2858
2859 /*
2860 * 'cupsdUpdatePrinterPPD()' - Update keywords in a printer's PPD file.
2861 */
2862
2863 int /* O - 1 if successful, 0 otherwise */
2864 cupsdUpdatePrinterPPD(
2865 cupsd_printer_t *p, /* I - Printer */
2866 int num_keywords, /* I - Number of keywords */
2867 cups_option_t *keywords) /* I - Keywords */
2868 {
2869 int i; /* Looping var */
2870 cups_file_t *src, /* Original file */
2871 *dst; /* New file */
2872 char srcfile[1024], /* Original filename */
2873 dstfile[1024], /* New filename */
2874 line[1024], /* Line from file */
2875 keystring[41]; /* Keyword from line */
2876 cups_option_t *keyword; /* Current keyword */
2877
2878
2879 cupsdLogMessage(CUPSD_LOG_INFO, "Updating keywords in PPD file for %s...",
2880 p->name);
2881
2882 /*
2883 * Get the old and new PPD filenames...
2884 */
2885
2886 snprintf(srcfile, sizeof(srcfile), "%s/ppd/%s.ppd.O", ServerRoot, p->name);
2887 snprintf(dstfile, sizeof(srcfile), "%s/ppd/%s.ppd", ServerRoot, p->name);
2888
2889 /*
2890 * Rename the old file and open the old and new...
2891 */
2892
2893 if (rename(dstfile, srcfile))
2894 {
2895 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to backup PPD file for %s: %s",
2896 p->name, strerror(errno));
2897 return (0);
2898 }
2899
2900 if ((src = cupsFileOpen(srcfile, "r")) == NULL)
2901 {
2902 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to open PPD file \"%s\": %s",
2903 srcfile, strerror(errno));
2904 rename(srcfile, dstfile);
2905 return (0);
2906 }
2907
2908 if ((dst = cupsFileOpen(dstfile, "w")) == NULL)
2909 {
2910 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create PPD file \"%s\": %s",
2911 dstfile, strerror(errno));
2912 cupsFileClose(src);
2913 rename(srcfile, dstfile);
2914 return (0);
2915 }
2916
2917 /*
2918 * Copy the first line and then write out all of the keywords...
2919 */
2920
2921 if (!cupsFileGets(src, line, sizeof(line)))
2922 {
2923 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to read PPD file \"%s\": %s",
2924 srcfile, strerror(errno));
2925 cupsFileClose(src);
2926 cupsFileClose(dst);
2927 rename(srcfile, dstfile);
2928 return (0);
2929 }
2930
2931 cupsFilePrintf(dst, "%s\n", line);
2932
2933 for (i = num_keywords, keyword = keywords; i > 0; i --, keyword ++)
2934 {
2935 cupsdLogMessage(CUPSD_LOG_DEBUG, "*%s: %s", keyword->name, keyword->value);
2936 cupsFilePrintf(dst, "*%s: %s\n", keyword->name, keyword->value);
2937 }
2938
2939 /*
2940 * Then copy the rest of the PPD file, dropping any keywords we changed.
2941 */
2942
2943 while (cupsFileGets(src, line, sizeof(line)))
2944 {
2945 /*
2946 * Skip keywords we've already set...
2947 */
2948
2949 if (sscanf(line, "*%40[^:]:", keystring) == 1 &&
2950 cupsGetOption(keystring, num_keywords, keywords))
2951 continue;
2952
2953 /*
2954 * Otherwise write the line...
2955 */
2956
2957 cupsFilePrintf(dst, "%s\n", line);
2958 }
2959
2960 /*
2961 * Close files and return...
2962 */
2963
2964 cupsFileClose(src);
2965 cupsFileClose(dst);
2966
2967 return (1);
2968 }
2969
2970
2971 /*
2972 * 'cupsdUpdatePrinters()' - Update printers after a partial reload.
2973 */
2974
2975 void
2976 cupsdUpdatePrinters(void)
2977 {
2978 cupsd_printer_t *p; /* Current printer */
2979
2980
2981 /*
2982 * Loop through the printers and recreate the printer attributes
2983 * for any local printers since the policy and/or access control
2984 * stuff may have changed. Also, if browsing is disabled, remove
2985 * any remote printers...
2986 */
2987
2988 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
2989 p;
2990 p = (cupsd_printer_t *)cupsArrayNext(Printers))
2991 {
2992 /*
2993 * Remove remote printers if we are no longer browsing...
2994 */
2995
2996 if (!Browsing &&
2997 (p->type & (CUPS_PRINTER_IMPLICIT | CUPS_PRINTER_DISCOVERED)))
2998 {
2999 if (p->type & CUPS_PRINTER_IMPLICIT)
3000 cupsArrayRemove(ImplicitPrinters, p);
3001
3002 cupsArraySave(Printers);
3003 cupsdDeletePrinter(p, 0);
3004 cupsArrayRestore(Printers);
3005 continue;
3006 }
3007
3008 /*
3009 * Update the operation policy pointer...
3010 */
3011
3012 if ((p->op_policy_ptr = cupsdFindPolicy(p->op_policy)) == NULL)
3013 p->op_policy_ptr = DefaultPolicyPtr;
3014
3015 /*
3016 * Update printer attributes as needed...
3017 */
3018
3019 if (!(p->type & CUPS_PRINTER_DISCOVERED))
3020 cupsdSetPrinterAttrs(p);
3021 }
3022 }
3023
3024
3025 /*
3026 * 'cupsdValidateDest()' - Validate a printer/class destination.
3027 */
3028
3029 const char * /* O - Printer or class name */
3030 cupsdValidateDest(
3031 const char *uri, /* I - Printer URI */
3032 cups_ptype_t *dtype, /* O - Type (printer or class) */
3033 cupsd_printer_t **printer) /* O - Printer pointer */
3034 {
3035 cupsd_printer_t *p; /* Current printer */
3036 char localname[1024],/* Localized hostname */
3037 *lptr, /* Pointer into localized hostname */
3038 *sptr, /* Pointer into server name */
3039 *rptr, /* Pointer into resource */
3040 scheme[32], /* Scheme portion of URI */
3041 username[64], /* Username portion of URI */
3042 hostname[HTTP_MAX_HOST],
3043 /* Host portion of URI */
3044 resource[HTTP_MAX_URI];
3045 /* Resource portion of URI */
3046 int port; /* Port portion of URI */
3047
3048
3049 DEBUG_printf(("cupsdValidateDest(uri=\"%s\", dtype=%p, printer=%p)\n", uri,
3050 dtype, printer));
3051
3052 /*
3053 * Initialize return values...
3054 */
3055
3056 if (printer)
3057 *printer = NULL;
3058
3059 if (dtype)
3060 *dtype = (cups_ptype_t)0;
3061
3062 /*
3063 * Pull the hostname and resource from the URI...
3064 */
3065
3066 httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, sizeof(scheme),
3067 username, sizeof(username), hostname, sizeof(hostname),
3068 &port, resource, sizeof(resource));
3069
3070 /*
3071 * See if the resource is a class or printer...
3072 */
3073
3074 if (!strncmp(resource, "/classes/", 9))
3075 {
3076 /*
3077 * Class...
3078 */
3079
3080 rptr = resource + 9;
3081 }
3082 else if (!strncmp(resource, "/printers/", 10))
3083 {
3084 /*
3085 * Printer...
3086 */
3087
3088 rptr = resource + 10;
3089 }
3090 else
3091 {
3092 /*
3093 * Bad resource name...
3094 */
3095
3096 return (NULL);
3097 }
3098
3099 /*
3100 * See if the printer or class name exists...
3101 */
3102
3103 p = cupsdFindDest(rptr);
3104
3105 if (p == NULL && strchr(rptr, '@') == NULL)
3106 return (NULL);
3107 else if (p != NULL)
3108 {
3109 if (printer)
3110 *printer = p;
3111
3112 if (dtype)
3113 *dtype = p->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT |
3114 CUPS_PRINTER_REMOTE | CUPS_PRINTER_DISCOVERED);
3115
3116 return (p->name);
3117 }
3118
3119 /*
3120 * Change localhost to the server name...
3121 */
3122
3123 if (!strcasecmp(hostname, "localhost"))
3124 strlcpy(hostname, ServerName, sizeof(hostname));
3125
3126 strlcpy(localname, hostname, sizeof(localname));
3127
3128 if (!strcasecmp(hostname, ServerName))
3129 {
3130 /*
3131 * Localize the hostname...
3132 */
3133
3134 lptr = strchr(localname, '.');
3135 sptr = strchr(ServerName, '.');
3136
3137 if (sptr != NULL && lptr != NULL)
3138 {
3139 /*
3140 * Strip the common domain name components...
3141 */
3142
3143 while (lptr != NULL)
3144 {
3145 if (!strcasecmp(lptr, sptr))
3146 {
3147 *lptr = '\0';
3148 break;
3149 }
3150 else
3151 lptr = strchr(lptr + 1, '.');
3152 }
3153 }
3154 }
3155
3156 DEBUG_printf(("localized hostname is \"%s\"...\n", localname));
3157
3158 /*
3159 * Find a matching printer or class...
3160 */
3161
3162 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
3163 p;
3164 p = (cupsd_printer_t *)cupsArrayNext(Printers))
3165 if (!strcasecmp(p->hostname, localname) &&
3166 !strcasecmp(p->name, rptr))
3167 {
3168 if (printer)
3169 *printer = p;
3170
3171 if (dtype)
3172 *dtype = p->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT |
3173 CUPS_PRINTER_REMOTE | CUPS_PRINTER_DISCOVERED);
3174
3175 return (p->name);
3176 }
3177
3178 return (NULL);
3179 }
3180
3181
3182 /*
3183 * 'cupsdWritePrintcap()' - Write a pseudo-printcap file for older applications
3184 * that need it...
3185 */
3186
3187 void
3188 cupsdWritePrintcap(void)
3189 {
3190 cups_file_t *fp; /* printcap file */
3191 cupsd_printer_t *p; /* Current printer */
3192
3193
3194 #ifdef __sgi
3195 /*
3196 * Update the IRIX printer state for the default printer; if
3197 * no printers remain, then the default printer file will be
3198 * removed...
3199 */
3200
3201 write_irix_state(DefaultPrinter);
3202 #endif /* __sgi */
3203
3204 /*
3205 * See if we have a printcap file; if not, don't bother writing it.
3206 */
3207
3208 if (!Printcap || !*Printcap)
3209 return;
3210
3211 /*
3212 * Open the printcap file...
3213 */
3214
3215 if ((fp = cupsFileOpen(Printcap, "w")) == NULL)
3216 return;
3217
3218 /*
3219 * Put a comment header at the top so that users will know where the
3220 * data has come from...
3221 */
3222
3223 cupsFilePuts(fp,
3224 "# This file was automatically generated by cupsd(8) from the\n");
3225 cupsFilePrintf(fp, "# %s/printers.conf file. All changes to this file\n",
3226 ServerRoot);
3227 cupsFilePuts(fp, "# will be lost.\n");
3228
3229 if (Printers)
3230 {
3231 /*
3232 * Write a new printcap with the current list of printers.
3233 */
3234
3235 switch (PrintcapFormat)
3236 {
3237 case PRINTCAP_BSD:
3238 /*
3239 * Each printer is put in the file as:
3240 *
3241 * Printer1:
3242 * Printer2:
3243 * Printer3:
3244 * ...
3245 * PrinterN:
3246 */
3247
3248 if (DefaultPrinter)
3249 cupsFilePrintf(fp, "%s|%s:rm=%s:rp=%s:\n", DefaultPrinter->name,
3250 DefaultPrinter->info, ServerName,
3251 DefaultPrinter->name);
3252
3253 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
3254 p;
3255 p = (cupsd_printer_t *)cupsArrayNext(Printers))
3256 if (p != DefaultPrinter)
3257 cupsFilePrintf(fp, "%s|%s:rm=%s:rp=%s:\n", p->name, p->info,
3258 ServerName, p->name);
3259 break;
3260
3261 case PRINTCAP_SOLARIS:
3262 /*
3263 * Each printer is put in the file as:
3264 *
3265 * _all:all=Printer1,Printer2,Printer3,...,PrinterN
3266 * _default:use=DefaultPrinter
3267 * Printer1:\
3268 * :bsdaddr=ServerName,Printer1:\
3269 * :description=Description:
3270 * Printer2:
3271 * :bsdaddr=ServerName,Printer2:\
3272 * :description=Description:
3273 * Printer3:
3274 * :bsdaddr=ServerName,Printer3:\
3275 * :description=Description:
3276 * ...
3277 * PrinterN:
3278 * :bsdaddr=ServerName,PrinterN:\
3279 * :description=Description:
3280 */
3281
3282 cupsFilePuts(fp, "_all:all=");
3283 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
3284 p;
3285 p = (cupsd_printer_t *)cupsArrayCurrent(Printers))
3286 cupsFilePrintf(fp, "%s%c", p->name,
3287 cupsArrayNext(Printers) ? ',' : '\n');
3288
3289 if (DefaultPrinter)
3290 cupsFilePrintf(fp, "_default:use=%s\n", DefaultPrinter->name);
3291
3292 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
3293 p;
3294 p = (cupsd_printer_t *)cupsArrayNext(Printers))
3295 cupsFilePrintf(fp, "%s:\\\n"
3296 "\t:bsdaddr=%s,%s:\\\n"
3297 "\t:description=%s:\n",
3298 p->name, ServerName, p->name,
3299 p->info ? p->info : "");
3300 break;
3301 }
3302 }
3303
3304 /*
3305 * Close the file...
3306 */
3307
3308 cupsFileClose(fp);
3309 }
3310
3311
3312 /*
3313 * 'cupsdSanitizeURI()' - Sanitize a device URI...
3314 */
3315
3316 char * /* O - New device URI */
3317 cupsdSanitizeURI(const char *uri, /* I - Original device URI */
3318 char *buffer, /* O - New device URI */
3319 int buflen) /* I - Size of new device URI buffer */
3320 {
3321 char *start, /* Start of data after scheme */
3322 *slash, /* First slash after scheme:// */
3323 *ptr; /* Pointer into user@host:port part */
3324
3325
3326 /*
3327 * Range check input...
3328 */
3329
3330 if (!uri || !buffer || buflen < 2)
3331 return (NULL);
3332
3333 /*
3334 * Copy the device URI to the new buffer...
3335 */
3336
3337 strlcpy(buffer, uri, buflen);
3338
3339 /*
3340 * Find the end of the scheme:// part...
3341 */
3342
3343 if ((ptr = strchr(buffer, ':')) == NULL)
3344 return (buffer); /* No scheme: part... */
3345
3346 for (start = ptr + 1; *start; start ++)
3347 if (*start != '/')
3348 break;
3349
3350 /*
3351 * Find the next slash (/) in the URI...
3352 */
3353
3354 if ((slash = strchr(start, '/')) == NULL)
3355 slash = start + strlen(start); /* No slash, point to the end */
3356
3357 /*
3358 * Check for an @ sign before the slash...
3359 */
3360
3361 if ((ptr = strchr(start, '@')) != NULL && ptr < slash)
3362 {
3363 /*
3364 * Found an @ sign and it is before the resource part, so we have
3365 * an authentication string. Copy the remaining URI over the
3366 * authentication string...
3367 */
3368
3369 _cups_strcpy(start, ptr + 1);
3370 }
3371
3372 /*
3373 * Return the new device URI...
3374 */
3375
3376 return (buffer);
3377 }
3378
3379
3380 /*
3381 * 'add_printer_defaults()' - Add name-default attributes to the printer attributes.
3382 */
3383
3384 static void
3385 add_printer_defaults(cupsd_printer_t *p)/* I - Printer */
3386 {
3387 int i; /* Looping var */
3388 int num_options; /* Number of default options */
3389 cups_option_t *options, /* Default options */
3390 *option; /* Current option */
3391 char name[256]; /* name-default */
3392
3393
3394 /*
3395 * Maintain a common array of default attribute names...
3396 */
3397
3398 if (!CommonDefaults)
3399 {
3400 CommonDefaults = cupsArrayNew((cups_array_func_t)strcmp, NULL);
3401
3402 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("copies-default"));
3403 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("document-format-default"));
3404 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("finishings-default"));
3405 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("job-hold-until-default"));
3406 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("job-priority-default"));
3407 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("job-sheets-default"));
3408 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("media-default"));
3409 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("number-up-default"));
3410 cupsArrayAdd(CommonDefaults,
3411 _cupsStrAlloc("orientation-requested-default"));
3412 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("sides-default"));
3413 }
3414
3415 /*
3416 * Add all of the default options from the .conf files...
3417 */
3418
3419 for (num_options = 0, i = p->num_options, option = p->options;
3420 i > 0;
3421 i --, option ++)
3422 {
3423 if (strcmp(option->name, "ipp-options") &&
3424 strcmp(option->name, "job-sheets") &&
3425 strcmp(option->name, "lease-duration"))
3426 {
3427 snprintf(name, sizeof(name), "%s-default", option->name);
3428 num_options = cupsAddOption(name, option->value, num_options, &options);
3429
3430 if (!cupsArrayFind(CommonDefaults, name))
3431 cupsArrayAdd(CommonDefaults, _cupsStrAlloc(name));
3432 }
3433 }
3434
3435 /*
3436 * Convert options to IPP attributes...
3437 */
3438
3439 cupsEncodeOptions2(p->attrs, num_options, options, IPP_TAG_PRINTER);
3440 cupsFreeOptions(num_options, options);
3441
3442 /*
3443 * Add standard -default attributes as needed...
3444 */
3445
3446 if (!cupsGetOption("copies", p->num_options, p->options))
3447 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER, "copies-default",
3448 1);
3449
3450 if (!cupsGetOption("document-format", p->num_options, p->options))
3451 ippAddString(CommonData, IPP_TAG_PRINTER, IPP_TAG_MIMETYPE,
3452 "document-format-default", NULL, "application/octet-stream");
3453
3454 if (!cupsGetOption("job-hold-until", p->num_options, p->options))
3455 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
3456 "job-hold-until-default", NULL, "no-hold");
3457
3458 if (!cupsGetOption("job-priority", p->num_options, p->options))
3459 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
3460 "job-priority-default", 50);
3461
3462 if (!cupsGetOption("number-up", p->num_options, p->options))
3463 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
3464 "number-up-default", 1);
3465
3466 if (!cupsGetOption("orientation-requested", p->num_options, p->options))
3467 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NOVALUE,
3468 "orientation-requested-default", NULL, NULL);
3469
3470 if (!cupsGetOption("notify-lease-duration", p->num_options, p->options))
3471 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
3472 "notify-lease-duration-default", DefaultLeaseDuration);
3473
3474 if (!cupsGetOption("notify-events", p->num_options, p->options))
3475 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
3476 "notify-events-default", NULL, "job-completed");
3477 }
3478
3479
3480 /*
3481 * 'add_printer_filter()' - Add a MIME filter for a printer.
3482 */
3483
3484 static void
3485 add_printer_filter(
3486 cupsd_printer_t *p, /* I - Printer to add to */
3487 mime_type_t *filtertype, /* I - Filter or prefilter MIME type */
3488 const char *filter) /* I - Filter to add */
3489 {
3490 char super[MIME_MAX_SUPER], /* Super-type for filter */
3491 type[MIME_MAX_TYPE], /* Type for filter */
3492 program[1024]; /* Program/filter name */
3493 int cost; /* Cost of filter */
3494 mime_type_t *temptype; /* MIME type looping var */
3495 char filename[1024]; /* Full filter filename */
3496
3497
3498 /*
3499 * Parse the filter string; it should be in the following format:
3500 *
3501 * super/type cost program
3502 */
3503
3504 if (sscanf(filter, "%15[^/]/%31s%d%1023s", super, type, &cost, program) != 4)
3505 {
3506 cupsdLogMessage(CUPSD_LOG_ERROR, "%s: invalid filter string \"%s\"!",
3507 p->name, filter);
3508 return;
3509 }
3510
3511 /*
3512 * See if the filter program exists; if not, stop the printer and flag
3513 * the error!
3514 */
3515
3516 if (strcmp(program, "-"))
3517 {
3518 if (program[0] == '/')
3519 strlcpy(filename, program, sizeof(filename));
3520 else
3521 snprintf(filename, sizeof(filename), "%s/filter/%s", ServerBin, program);
3522
3523 if (access(filename, X_OK))
3524 {
3525 snprintf(p->state_message, sizeof(p->state_message),
3526 "Filter \"%s\" for printer \"%s\" not available: %s",
3527 program, p->name, strerror(errno));
3528 cupsdSetPrinterReasons(p, "+cups-missing-filter-error");
3529 cupsdSetPrinterState(p, IPP_PRINTER_STOPPED, 0);
3530
3531 cupsdLogMessage(CUPSD_LOG_ERROR, "%s", p->state_message);
3532 }
3533 }
3534
3535 /*
3536 * Mark the CUPS_PRINTER_COMMANDS bit if we have a filter for
3537 * application/vnd.cups-command...
3538 */
3539
3540 if (!strcasecmp(super, "application") &&
3541 !strcasecmp(type, "vnd.cups-command"))
3542 p->type |= CUPS_PRINTER_COMMANDS;
3543
3544 /*
3545 * Add the filter to the MIME database, supporting wildcards as needed...
3546 */
3547
3548 for (temptype = mimeFirstType(MimeDatabase);
3549 temptype;
3550 temptype = mimeNextType(MimeDatabase))
3551 if (((super[0] == '*' && strcasecmp(temptype->super, "printer")) ||
3552 !strcasecmp(temptype->super, super)) &&
3553 (type[0] == '*' || !strcasecmp(temptype->type, type)))
3554 {
3555 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3556 "add_printer_filter: %s: adding filter %s/%s %s/%s %d %s",
3557 p->name, temptype->super, temptype->type,
3558 filtertype->super, filtertype->type,
3559 cost, program);
3560 mimeAddFilter(MimeDatabase, temptype, filtertype, cost, program);
3561 }
3562 }
3563
3564
3565 /*
3566 * 'add_printer_formats()' - Add document-format-supported values for a printer.
3567 */
3568
3569 static void
3570 add_printer_formats(cupsd_printer_t *p) /* I - Printer */
3571 {
3572 int i; /* Looping var */
3573 mime_type_t *type; /* Current MIME type */
3574 cups_array_t *filters; /* Filters */
3575 ipp_attribute_t *attr; /* document-format-supported attribute */
3576 char mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2];
3577 /* MIME type name */
3578
3579
3580 /*
3581 * Raw (and remote) queues advertise all of the supported MIME
3582 * types...
3583 */
3584
3585 cupsArrayDelete(p->filetypes);
3586 p->filetypes = NULL;
3587
3588 if (p->raw)
3589 {
3590 ippAddStrings(p->attrs, IPP_TAG_PRINTER,
3591 (ipp_tag_t)(IPP_TAG_MIMETYPE | IPP_TAG_COPY),
3592 "document-format-supported", NumMimeTypes, NULL, MimeTypes);
3593 return;
3594 }
3595
3596 /*
3597 * Otherwise, loop through the supported MIME types and see if there
3598 * are filters for them...
3599 */
3600
3601 cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_printer_formats: %d types, %d filters",
3602 mimeNumTypes(MimeDatabase), mimeNumFilters(MimeDatabase));
3603
3604 p->filetypes = cupsArrayNew(NULL, NULL);
3605
3606 for (type = mimeFirstType(MimeDatabase);
3607 type;
3608 type = mimeNextType(MimeDatabase))
3609 {
3610 snprintf(mimetype, sizeof(mimetype), "%s/%s", type->super, type->type);
3611
3612 if ((filters = mimeFilter(MimeDatabase, type, p->filetype, NULL)) != NULL)
3613 {
3614 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3615 "add_printer_formats: %s: %s needs %d filters",
3616 p->name, mimetype, cupsArrayCount(filters));
3617
3618 cupsArrayDelete(filters);
3619 cupsArrayAdd(p->filetypes, type);
3620 }
3621 else
3622 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3623 "add_printer_formats: %s: %s not supported",
3624 p->name, mimetype);
3625 }
3626
3627 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3628 "add_printer_formats: %s: %d supported types",
3629 p->name, cupsArrayCount(p->filetypes) + 1);
3630
3631 /*
3632 * Add the file formats that can be filtered...
3633 */
3634
3635 if ((type = mimeType(MimeDatabase, "application", "octet-stream")) == NULL ||
3636 !cupsArrayFind(p->filetypes, type))
3637 i = 1;
3638 else
3639 i = 0;
3640
3641 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_MIMETYPE,
3642 "document-format-supported",
3643 cupsArrayCount(p->filetypes) + 1, NULL, NULL);
3644
3645 if (i)
3646 attr->values[0].string.text = _cupsStrAlloc("application/octet-stream");
3647
3648 for (type = (mime_type_t *)cupsArrayFirst(p->filetypes);
3649 type;
3650 i ++, type = (mime_type_t *)cupsArrayNext(p->filetypes))
3651 {
3652 snprintf(mimetype, sizeof(mimetype), "%s/%s", type->super, type->type);
3653
3654 attr->values[i].string.text = _cupsStrAlloc(mimetype);
3655 }
3656
3657 #ifdef HAVE_DNSSD
3658 {
3659 char pdl[1024]; /* Buffer to build pdl list */
3660 mime_filter_t *filter; /* MIME filter looping var */
3661
3662
3663 pdl[0] = '\0';
3664
3665 if (mimeType(MimeDatabase, "application", "pdf"))
3666 strlcat(pdl, "application/pdf,", sizeof(pdl));
3667
3668 if (mimeType(MimeDatabase, "application", "postscript"))
3669 strlcat(pdl, "application/postscript,", sizeof(pdl));
3670
3671 if (mimeType(MimeDatabase, "application", "vnd.cups-raster"))
3672 strlcat(pdl, "application/vnd.cups-raster,", sizeof(pdl));
3673
3674 /*
3675 * Determine if this is a Tioga PrintJobMgr based queue...
3676 */
3677
3678 for (filter = (mime_filter_t *)cupsArrayFirst(MimeDatabase->filters);
3679 filter;
3680 filter = (mime_filter_t *)cupsArrayNext(MimeDatabase->filters))
3681 {
3682 if (filter->dst == p->filetype && filter->filter &&
3683 strstr(filter->filter, "PrintJobMgr"))
3684 break;
3685 }
3686
3687 /*
3688 * We only support raw printing if this is not a Tioga PrintJobMgr based
3689 * queue and if application/octet-stream is a known conversion...
3690 */
3691
3692 if (!filter && mimeType(MimeDatabase, "application", "octet-stream"))
3693 strlcat(pdl, "application/octet-stream,", sizeof(pdl));
3694
3695 if (mimeType(MimeDatabase, "image", "png"))
3696 strlcat(pdl, "image/png,", sizeof(pdl));
3697
3698 if (pdl[0])
3699 pdl[strlen(pdl) - 1] = '\0'; /* Remove trailing comma */
3700
3701 cupsdSetString(&p->pdl, pdl);
3702 }
3703 #endif /* HAVE_DNSSD */
3704 }
3705
3706
3707 /*
3708 * 'compare_printers()' - Compare two printers.
3709 */
3710
3711 static int /* O - Result of comparison */
3712 compare_printers(void *first, /* I - First printer */
3713 void *second, /* I - Second printer */
3714 void *data) /* I - App data (not used) */
3715 {
3716 return (strcasecmp(((cupsd_printer_t *)first)->name,
3717 ((cupsd_printer_t *)second)->name));
3718 }
3719
3720
3721 /*
3722 * 'delete_printer_filters()' - Delete all MIME filters for a printer.
3723 */
3724
3725 static void
3726 delete_printer_filters(
3727 cupsd_printer_t *p) /* I - Printer to remove from */
3728 {
3729 mime_filter_t *filter; /* MIME filter looping var */
3730
3731
3732 /*
3733 * Range check input...
3734 */
3735
3736 if (p == NULL)
3737 return;
3738
3739 /*
3740 * Remove all filters from the MIME database that have a destination
3741 * type == printer...
3742 */
3743
3744 for (filter = mimeFirstFilter(MimeDatabase);
3745 filter;
3746 filter = mimeNextFilter(MimeDatabase))
3747 if (filter->dst == p->filetype)
3748 {
3749 /*
3750 * Delete the current filter...
3751 */
3752
3753 mimeDeleteFilter(MimeDatabase, filter);
3754 }
3755 }
3756
3757
3758 #ifdef __sgi
3759 /*
3760 * 'write_irix_config()' - Update the config files used by the IRIX
3761 * desktop tools.
3762 */
3763
3764 static void
3765 write_irix_config(cupsd_printer_t *p) /* I - Printer to update */
3766 {
3767 char filename[1024]; /* Interface script filename */
3768 cups_file_t *fp; /* Interface script file */
3769 ipp_attribute_t *attr; /* Attribute data */
3770
3771
3772 /*
3773 * Add dummy interface and GUI scripts to fool SGI's "challenged" printing
3774 * tools. First the interface script that tells the tools what kind of
3775 * printer we have...
3776 */
3777
3778 snprintf(filename, sizeof(filename), "/var/spool/lp/interface/%s", p->name);
3779
3780 if (p->type & CUPS_PRINTER_CLASS)
3781 unlink(filename);
3782 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
3783 {
3784 cupsFilePuts(fp, "#!/bin/sh\n");
3785
3786 if ((attr = ippFindAttribute(p->attrs, "printer-make-and-model",
3787 IPP_TAG_TEXT)) != NULL)
3788 cupsFilePrintf(fp, "NAME=\"%s\"\n", attr->values[0].string.text);
3789 else if (p->type & CUPS_PRINTER_CLASS)
3790 cupsFilePuts(fp, "NAME=\"Printer Class\"\n");
3791 else
3792 cupsFilePuts(fp, "NAME=\"Remote Destination\"\n");
3793
3794 if (p->type & CUPS_PRINTER_COLOR)
3795 cupsFilePuts(fp, "TYPE=ColorPostScript\n");
3796 else
3797 cupsFilePuts(fp, "TYPE=MonoPostScript\n");
3798
3799 cupsFilePrintf(fp, "HOSTNAME=%s\n", ServerName);
3800 cupsFilePrintf(fp, "HOSTPRINTER=%s\n", p->name);
3801
3802 cupsFileClose(fp);
3803
3804 chmod(filename, 0755);
3805 chown(filename, User, Group);
3806 }
3807
3808 /*
3809 * Then the member file that tells which device file the queue is connected
3810 * to... Networked printers use "/dev/null" in this file, so that's what
3811 * we use (the actual device URI can confuse some apps...)
3812 */
3813
3814 snprintf(filename, sizeof(filename), "/var/spool/lp/member/%s", p->name);
3815
3816 if (p->type & CUPS_PRINTER_CLASS)
3817 unlink(filename);
3818 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
3819 {
3820 cupsFilePuts(fp, "/dev/null\n");
3821
3822 cupsFileClose(fp);
3823
3824 chmod(filename, 0644);
3825 chown(filename, User, Group);
3826 }
3827
3828 /*
3829 * The gui_interface file is a script or program that launches a GUI
3830 * option panel for the printer, using options specified on the
3831 * command-line in the third argument. The option panel must send
3832 * any printing options to stdout on a single line when the user
3833 * accepts them, or nothing if the user cancels the dialog.
3834 *
3835 * The default options panel program is /usr/bin/glpoptions, from
3836 * the ESP Print Pro software. You can select another using the
3837 * PrintcapGUI option.
3838 */
3839
3840 snprintf(filename, sizeof(filename), "/var/spool/lp/gui_interface/ELF/%s.gui", p->name);
3841
3842 if (p->type & CUPS_PRINTER_CLASS)
3843 unlink(filename);
3844 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
3845 {
3846 cupsFilePuts(fp, "#!/bin/sh\n");
3847 cupsFilePrintf(fp, "%s -d %s -o \"$3\"\n", PrintcapGUI, p->name);
3848
3849 cupsFileClose(fp);
3850
3851 chmod(filename, 0755);
3852 chown(filename, User, Group);
3853 }
3854
3855 /*
3856 * The POD config file is needed by the printstatus command to show
3857 * the printer location and device.
3858 */
3859
3860 snprintf(filename, sizeof(filename), "/var/spool/lp/pod/%s.config", p->name);
3861
3862 if (p->type & CUPS_PRINTER_CLASS)
3863 unlink(filename);
3864 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
3865 {
3866 cupsFilePrintf(fp, "Printer Class | %s\n",
3867 (p->type & CUPS_PRINTER_COLOR) ? "ColorPostScript" : "MonoPostScript");
3868 cupsFilePrintf(fp, "Printer Model | %s\n", p->make_model ? p->make_model : "");
3869 cupsFilePrintf(fp, "Location Code | %s\n", p->location ? p->location : "");
3870 cupsFilePrintf(fp, "Physical Location | %s\n", p->info ? p->info : "");
3871 cupsFilePrintf(fp, "Port Path | %s\n", p->device_uri ? p->device_uri : "");
3872 cupsFilePrintf(fp, "Config Path | /var/spool/lp/pod/%s.config\n", p->name);
3873 cupsFilePrintf(fp, "Active Status Path | /var/spool/lp/pod/%s.status\n", p->name);
3874 cupsFilePuts(fp, "Status Update Wait | 10 seconds\n");
3875
3876 cupsFileClose(fp);
3877
3878 chmod(filename, 0664);
3879 chown(filename, User, Group);
3880 }
3881 }
3882
3883
3884 /*
3885 * 'write_irix_state()' - Update the status files used by IRIX printing
3886 * desktop tools.
3887 */
3888
3889 static void
3890 write_irix_state(cupsd_printer_t *p) /* I - Printer to update */
3891 {
3892 char filename[1024]; /* Interface script filename */
3893 cups_file_t *fp; /* Interface script file */
3894 int tag; /* Status tag value */
3895
3896
3897 if (p)
3898 {
3899 /*
3900 * The POD status file is needed for the printstatus window to
3901 * provide the current status of the printer.
3902 */
3903
3904 snprintf(filename, sizeof(filename), "/var/spool/lp/pod/%s.status", p->name);
3905
3906 if (p->type & CUPS_PRINTER_CLASS)
3907 unlink(filename);
3908 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
3909 {
3910 cupsFilePrintf(fp, "Operational Status | %s\n",
3911 (p->state == IPP_PRINTER_IDLE) ? "Idle" :
3912 (p->state == IPP_PRINTER_PROCESSING) ? "Busy" :
3913 "Faulted");
3914 cupsFilePrintf(fp, "Information | 01 00 00 | %s\n", CUPS_SVERSION);
3915 cupsFilePrintf(fp, "Information | 02 00 00 | Device URI: %s\n",
3916 p->device_uri ? p->device_uri : "");
3917 cupsFilePrintf(fp, "Information | 03 00 00 | %s jobs\n",
3918 p->accepting ? "Accepting" : "Not accepting");
3919 cupsFilePrintf(fp, "Information | 04 00 00 | %s\n", p->state_message);
3920
3921 cupsFileClose(fp);
3922
3923 chmod(filename, 0664);
3924 chown(filename, User, Group);
3925 }
3926
3927 /*
3928 * The activeicons file is needed to provide desktop icons for printers:
3929 *
3930 * [ quoted from /usr/lib/print/tagit ]
3931 *
3932 * --- Type of printer tags (base values)
3933 *
3934 * Dumb=66048 # 0x10200
3935 * DumbColor=66080 # 0x10220
3936 * Raster=66112 # 0x10240
3937 * ColorRaster=66144 # 0x10260
3938 * Plotter=66176 # 0x10280
3939 * PostScript=66208 # 0x102A0
3940 * ColorPostScript=66240 # 0x102C0
3941 * MonoPostScript=66272 # 0x102E0
3942 *
3943 * --- Printer state modifiers for local printers
3944 *
3945 * Idle=0 # 0x0
3946 * Busy=1 # 0x1
3947 * Faulted=2 # 0x2
3948 * Unknown=3 # 0x3 (Faulted due to unknown reason)
3949 *
3950 * --- Printer state modifiers for network printers
3951 *
3952 * NetIdle=8 # 0x8
3953 * NetBusy=9 # 0x9
3954 * NetFaulted=10 # 0xA
3955 * NetUnknown=11 # 0xB (Faulted due to unknown reason)
3956 */
3957
3958 snprintf(filename, sizeof(filename), "/var/spool/lp/activeicons/%s", p->name);
3959
3960 if (p->type & CUPS_PRINTER_CLASS)
3961 unlink(filename);
3962 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
3963 {
3964 if (p->type & CUPS_PRINTER_COLOR)
3965 tag = 66240;
3966 else
3967 tag = 66272;
3968
3969 if (p->type & CUPS_PRINTER_REMOTE)
3970 tag |= 8;
3971
3972 if (p->state == IPP_PRINTER_PROCESSING)
3973 tag |= 1;
3974
3975 else if (p->state == IPP_PRINTER_STOPPED)
3976 tag |= 2;
3977
3978 cupsFilePuts(fp, "#!/bin/sh\n");
3979 cupsFilePrintf(fp, "#Tag %d\n", tag);
3980
3981 cupsFileClose(fp);
3982
3983 chmod(filename, 0755);
3984 chown(filename, User, Group);
3985 }
3986 }
3987
3988 /*
3989 * The default file is needed by the printers window to show
3990 * the default printer.
3991 */
3992
3993 snprintf(filename, sizeof(filename), "/var/spool/lp/default");
3994
3995 if (DefaultPrinter != NULL)
3996 {
3997 if ((fp = cupsFileOpen(filename, "w")) != NULL)
3998 {
3999 cupsFilePrintf(fp, "%s\n", DefaultPrinter->name);
4000
4001 cupsFileClose(fp);
4002
4003 chmod(filename, 0644);
4004 chown(filename, User, Group);
4005 }
4006 }
4007 else
4008 unlink(filename);
4009 }
4010 #endif /* __sgi */
4011
4012
4013 /*
4014 * End of "$Id: printers.c 6970 2007-09-17 23:58:28Z mike $".
4015 */