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