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