]> 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 6429 2007-04-02 13:44:52Z 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 {
1198 cupsd_policy_t *pol; /* Policy */
1199
1200
1201 if ((pol = cupsdFindPolicy(value)) != NULL)
1202 {
1203 cupsdSetString(&p->op_policy, value);
1204 p->op_policy_ptr = pol;
1205 }
1206 else
1207 cupsdLogMessage(CUPSD_LOG_ERROR,
1208 "Bad policy \"%s\" on line %d of printers.conf",
1209 value, linenum);
1210 }
1211 else
1212 {
1213 cupsdLogMessage(CUPSD_LOG_ERROR,
1214 "Syntax error on line %d of printers.conf.", linenum);
1215 return;
1216 }
1217 }
1218 else if (!strcasecmp(line, "ErrorPolicy"))
1219 {
1220 if (value)
1221 cupsdSetString(&p->error_policy, value);
1222 else
1223 {
1224 cupsdLogMessage(CUPSD_LOG_ERROR,
1225 "Syntax error on line %d of printers.conf.", linenum);
1226 return;
1227 }
1228 }
1229 else
1230 {
1231 /*
1232 * Something else we don't understand...
1233 */
1234
1235 cupsdLogMessage(CUPSD_LOG_ERROR,
1236 "Unknown configuration directive %s on line %d of printers.conf.",
1237 line, linenum);
1238 }
1239 }
1240
1241 cupsFileClose(fp);
1242 }
1243
1244
1245 /*
1246 * 'cupsdRenamePrinter()' - Rename a printer.
1247 */
1248
1249 void
1250 cupsdRenamePrinter(
1251 cupsd_printer_t *p, /* I - Printer */
1252 const char *name) /* I - New name */
1253 {
1254 /*
1255 * Remove the printer from the array(s) first...
1256 */
1257
1258 cupsArrayRemove(Printers, p);
1259
1260 if (p->type & CUPS_PRINTER_IMPLICIT)
1261 cupsArrayRemove(ImplicitPrinters, p);
1262
1263 /*
1264 * Rename the printer type...
1265 */
1266
1267 mimeDeleteType(MimeDatabase, p->filetype);
1268 p->filetype = mimeAddType(MimeDatabase, "printer", name);
1269
1270 mimeDeleteType(MimeDatabase, p->prefiltertype);
1271 p->prefiltertype = mimeAddType(MimeDatabase, "prefilter", name);
1272
1273 /*
1274 * Rename the printer...
1275 */
1276
1277 cupsdSetString(&p->name, name);
1278
1279 /*
1280 * Reset printer attributes...
1281 */
1282
1283 cupsdSetPrinterAttrs(p);
1284
1285 /*
1286 * Add the printer back to the printer array(s)...
1287 */
1288
1289 cupsArrayAdd(Printers, p);
1290
1291 if (p->type & CUPS_PRINTER_IMPLICIT)
1292 cupsArrayAdd(ImplicitPrinters, p);
1293 }
1294
1295
1296 /*
1297 * 'cupsdSaveAllPrinters()' - Save all printer definitions to the printers.conf
1298 * file.
1299 */
1300
1301 void
1302 cupsdSaveAllPrinters(void)
1303 {
1304 int i; /* Looping var */
1305 cups_file_t *fp; /* printers.conf file */
1306 char temp[1024]; /* Temporary string */
1307 char backup[1024]; /* printers.conf.O file */
1308 cupsd_printer_t *printer; /* Current printer class */
1309 time_t curtime; /* Current time */
1310 struct tm *curdate; /* Current date */
1311 cups_option_t *option; /* Current option */
1312 const char *ptr; /* Pointer into info/location */
1313
1314
1315 /*
1316 * Create the printers.conf file...
1317 */
1318
1319 snprintf(temp, sizeof(temp), "%s/printers.conf", ServerRoot);
1320 snprintf(backup, sizeof(backup), "%s/printers.conf.O", ServerRoot);
1321
1322 if (rename(temp, backup))
1323 {
1324 if (errno != ENOENT)
1325 cupsdLogMessage(CUPSD_LOG_ERROR,
1326 "Unable to backup printers.conf - %s", strerror(errno));
1327 }
1328
1329 if ((fp = cupsFileOpen(temp, "w")) == NULL)
1330 {
1331 cupsdLogMessage(CUPSD_LOG_ERROR,
1332 "Unable to save printers.conf - %s", strerror(errno));
1333
1334 if (rename(backup, temp))
1335 cupsdLogMessage(CUPSD_LOG_ERROR,
1336 "Unable to restore printers.conf - %s", strerror(errno));
1337 return;
1338 }
1339 else
1340 cupsdLogMessage(CUPSD_LOG_INFO, "Saving printers.conf...");
1341
1342 /*
1343 * Restrict access to the file...
1344 */
1345
1346 fchown(cupsFileNumber(fp), getuid(), Group);
1347 fchmod(cupsFileNumber(fp), 0600);
1348
1349 /*
1350 * Write a small header to the file...
1351 */
1352
1353 curtime = time(NULL);
1354 curdate = localtime(&curtime);
1355 strftime(temp, sizeof(temp) - 1, "%Y-%m-%d %H:%M", curdate);
1356
1357 cupsFilePuts(fp, "# Printer configuration file for " CUPS_SVERSION "\n");
1358 cupsFilePrintf(fp, "# Written by cupsd on %s\n", temp);
1359
1360 /*
1361 * Write each local printer known to the system...
1362 */
1363
1364 for (printer = (cupsd_printer_t *)cupsArrayFirst(Printers);
1365 printer;
1366 printer = (cupsd_printer_t *)cupsArrayNext(Printers))
1367 {
1368 /*
1369 * Skip remote destinations and printer classes...
1370 */
1371
1372 if ((printer->type & CUPS_PRINTER_REMOTE) ||
1373 (printer->type & CUPS_PRINTER_CLASS) ||
1374 (printer->type & CUPS_PRINTER_IMPLICIT))
1375 continue;
1376
1377 /*
1378 * Write printers as needed...
1379 */
1380
1381 if (printer == DefaultPrinter)
1382 cupsFilePrintf(fp, "<DefaultPrinter %s>\n", printer->name);
1383 else
1384 cupsFilePrintf(fp, "<Printer %s>\n", printer->name);
1385
1386 if (printer->num_auth_info_required > 0)
1387 {
1388 cupsFilePrintf(fp, "AuthInfoRequired %s", printer->auth_info_required[0]);
1389 for (i = 1; i < printer->num_auth_info_required; i ++)
1390 cupsFilePrintf(fp, ",%s", printer->auth_info_required[i]);
1391 cupsFilePutChar(fp, '\n');
1392 }
1393
1394 if (printer->info)
1395 {
1396 if ((ptr = strchr(printer->info, '#')) != NULL)
1397 {
1398 /*
1399 * Need to quote the first # in the info string...
1400 */
1401
1402 cupsFilePuts(fp, "Info ");
1403 cupsFileWrite(fp, printer->info, ptr - printer->info);
1404 cupsFilePutChar(fp, '\\');
1405 cupsFilePuts(fp, ptr);
1406 cupsFilePutChar(fp, '\n');
1407 }
1408 else
1409 cupsFilePrintf(fp, "Info %s\n", printer->info);
1410 }
1411
1412 if (printer->location)
1413 {
1414 if ((ptr = strchr(printer->info, '#')) != NULL)
1415 {
1416 /*
1417 * Need to quote the first # in the location string...
1418 */
1419
1420 cupsFilePuts(fp, "Location ");
1421 cupsFileWrite(fp, printer->location, ptr - printer->location);
1422 cupsFilePutChar(fp, '\\');
1423 cupsFilePuts(fp, ptr);
1424 cupsFilePutChar(fp, '\n');
1425 }
1426 else
1427 cupsFilePrintf(fp, "Location %s\n", printer->location);
1428 }
1429 if (printer->device_uri)
1430 cupsFilePrintf(fp, "DeviceURI %s\n", printer->device_uri);
1431
1432 if (printer->port_monitor)
1433 cupsFilePrintf(fp, "PortMonitor %s\n", printer->port_monitor);
1434
1435 if (printer->state == IPP_PRINTER_STOPPED)
1436 {
1437 cupsFilePuts(fp, "State Stopped\n");
1438 cupsFilePrintf(fp, "StateMessage %s\n", printer->state_message);
1439 }
1440 else
1441 cupsFilePuts(fp, "State Idle\n");
1442
1443 cupsFilePrintf(fp, "StateTime %d\n", (int)printer->state_time);
1444
1445 if (printer->accepting)
1446 cupsFilePuts(fp, "Accepting Yes\n");
1447 else
1448 cupsFilePuts(fp, "Accepting No\n");
1449
1450 if (printer->shared)
1451 cupsFilePuts(fp, "Shared Yes\n");
1452 else
1453 cupsFilePuts(fp, "Shared No\n");
1454
1455 cupsFilePrintf(fp, "JobSheets %s %s\n", printer->job_sheets[0],
1456 printer->job_sheets[1]);
1457
1458 cupsFilePrintf(fp, "QuotaPeriod %d\n", printer->quota_period);
1459 cupsFilePrintf(fp, "PageLimit %d\n", printer->page_limit);
1460 cupsFilePrintf(fp, "KLimit %d\n", printer->k_limit);
1461
1462 for (i = 0; i < printer->num_users; i ++)
1463 cupsFilePrintf(fp, "%sUser %s\n", printer->deny_users ? "Deny" : "Allow",
1464 printer->users[i]);
1465
1466 if (printer->op_policy)
1467 cupsFilePrintf(fp, "OpPolicy %s\n", printer->op_policy);
1468 if (printer->error_policy)
1469 cupsFilePrintf(fp, "ErrorPolicy %s\n", printer->error_policy);
1470
1471 for (i = printer->num_options, option = printer->options;
1472 i > 0;
1473 i --, option ++)
1474 cupsFilePrintf(fp, "Option %s %s\n", option->name, option->value);
1475
1476 cupsFilePuts(fp, "</Printer>\n");
1477
1478 #ifdef __sgi
1479 /*
1480 * Make IRIX desktop & printer status happy
1481 */
1482
1483 write_irix_state(printer);
1484 #endif /* __sgi */
1485 }
1486
1487 cupsFileClose(fp);
1488 }
1489
1490
1491 /*
1492 * 'cupsdSetAuthInfoRequired()' - Set the required authentication info.
1493 */
1494
1495 int /* O - 1 if value OK, 0 otherwise */
1496 cupsdSetAuthInfoRequired(
1497 cupsd_printer_t *p, /* I - Printer */
1498 const char *values, /* I - Plain text value (or NULL) */
1499 ipp_attribute_t *attr) /* I - IPP attribute value (or NULL) */
1500 {
1501 int i; /* Looping var */
1502
1503
1504 p->num_auth_info_required = 0;
1505
1506 /*
1507 * Do we have a plain text value?
1508 */
1509
1510 if (values)
1511 {
1512 /*
1513 * Yes, grab the keywords...
1514 */
1515
1516 const char *end; /* End of current value */
1517
1518
1519 while (*values && p->num_auth_info_required < 4)
1520 {
1521 if ((end = strchr(values, ',')) == NULL)
1522 end = values + strlen(values);
1523
1524 if ((end - values) == 4 && !strncmp(values, "none", 4))
1525 {
1526 if (p->num_auth_info_required != 0 || *end)
1527 return (0);
1528
1529 p->auth_info_required[p->num_auth_info_required] = "none";
1530 p->num_auth_info_required ++;
1531
1532 return (1);
1533 }
1534 else if ((end - values) == 9 && !strncmp(values, "negotiate", 9))
1535 {
1536 if (p->num_auth_info_required != 0 || *end)
1537 return (0);
1538
1539 p->auth_info_required[p->num_auth_info_required] = "negotiate";
1540 p->num_auth_info_required ++;
1541
1542 return (1);
1543 }
1544 else if ((end - values) == 6 && !strncmp(values, "domain", 6))
1545 {
1546 p->auth_info_required[p->num_auth_info_required] = "domain";
1547 p->num_auth_info_required ++;
1548 }
1549 else if ((end - values) == 8 && !strncmp(values, "password", 8))
1550 {
1551 p->auth_info_required[p->num_auth_info_required] = "password";
1552 p->num_auth_info_required ++;
1553 }
1554 else if ((end - values) == 8 && !strncmp(values, "username", 8))
1555 {
1556 p->auth_info_required[p->num_auth_info_required] = "username";
1557 p->num_auth_info_required ++;
1558 }
1559 else
1560 return (0);
1561 }
1562
1563 if (p->num_auth_info_required == 0)
1564 {
1565 p->auth_info_required[0] = "none";
1566 p->num_auth_info_required = 1;
1567 }
1568
1569 return (1);
1570 }
1571
1572 /*
1573 * Grab values from an attribute instead...
1574 */
1575
1576 if (!attr || attr->num_values > 4)
1577 return (0);
1578
1579 for (i = 0; i < attr->num_values; i ++)
1580 {
1581 if (!strcmp(attr->values[i].string.text, "none"))
1582 {
1583 if (p->num_auth_info_required != 0 || attr->num_values != 1)
1584 return (0);
1585
1586 p->auth_info_required[p->num_auth_info_required] = "none";
1587 p->num_auth_info_required ++;
1588
1589 return (1);
1590 }
1591 else if (!strcmp(attr->values[i].string.text, "negotiate"))
1592 {
1593 if (p->num_auth_info_required != 0 || attr->num_values != 1)
1594 return (0);
1595
1596 p->auth_info_required[p->num_auth_info_required] = "negotiate";
1597 p->num_auth_info_required ++;
1598
1599 return (1);
1600 }
1601 else if (!strcmp(attr->values[i].string.text, "domain"))
1602 {
1603 p->auth_info_required[p->num_auth_info_required] = "domain";
1604 p->num_auth_info_required ++;
1605 }
1606 else if (!strcmp(attr->values[i].string.text, "password"))
1607 {
1608 p->auth_info_required[p->num_auth_info_required] = "password";
1609 p->num_auth_info_required ++;
1610 }
1611 else if (!strcmp(attr->values[i].string.text, "username"))
1612 {
1613 p->auth_info_required[p->num_auth_info_required] = "username";
1614 p->num_auth_info_required ++;
1615 }
1616 else
1617 return (0);
1618 }
1619
1620 return (1);
1621 }
1622
1623
1624 /*
1625 * 'cupsdSetPrinterAttrs()' - Set printer attributes based upon the PPD file.
1626 */
1627
1628 void
1629 cupsdSetPrinterAttrs(cupsd_printer_t *p)/* I - Printer to setup */
1630 {
1631 int i, /* Looping var */
1632 length; /* Length of browse attributes */
1633 char uri[HTTP_MAX_URI]; /* URI for printer */
1634 char resource[HTTP_MAX_URI]; /* Resource portion of URI */
1635 char filename[1024]; /* Name of PPD file */
1636 int num_media; /* Number of media options */
1637 cupsd_location_t *auth; /* Pointer to authentication element */
1638 const char *auth_supported; /* Authentication supported */
1639 cups_ptype_t printer_type; /* Printer type data */
1640 ppd_file_t *ppd; /* PPD file data */
1641 ppd_option_t *input_slot, /* InputSlot options */
1642 *media_type, /* MediaType options */
1643 *page_size, /* PageSize options */
1644 *output_bin, /* OutputBin options */
1645 *media_quality, /* EFMediaQualityMode options */
1646 *duplex; /* Duplex options */
1647 ppd_attr_t *ppdattr; /* PPD attribute */
1648 ipp_attribute_t *attr; /* Attribute data */
1649 ipp_value_t *val; /* Attribute value */
1650 int num_finishings; /* Number of finishings */
1651 ipp_finish_t finishings[5]; /* finishings-supported values */
1652 cups_option_t *option; /* Current printer option */
1653 static const char * const sides[3] = /* sides-supported values */
1654 {
1655 "one-sided",
1656 "two-sided-long-edge",
1657 "two-sided-short-edge"
1658 };
1659
1660
1661 DEBUG_printf(("cupsdSetPrinterAttrs: entering name = %s, type = %x\n", p->name,
1662 p->type));
1663
1664 /*
1665 * Make sure that we have the common attributes defined...
1666 */
1667
1668 if (!CommonData)
1669 cupsdCreateCommonData();
1670
1671 /*
1672 * Clear out old filters, if any...
1673 */
1674
1675 delete_printer_filters(p);
1676
1677 /*
1678 * Figure out the authentication that is required for the printer.
1679 */
1680
1681 auth_supported = "requesting-user-name";
1682 if (!(p->type & CUPS_PRINTER_REMOTE))
1683 {
1684 if (p->type & CUPS_PRINTER_CLASS)
1685 snprintf(resource, sizeof(resource), "/classes/%s", p->name);
1686 else
1687 snprintf(resource, sizeof(resource), "/printers/%s", p->name);
1688
1689 if ((auth = cupsdFindBest(resource, HTTP_POST)) == NULL ||
1690 auth->type == AUTH_NONE)
1691 auth = cupsdFindPolicyOp(p->op_policy_ptr, IPP_PRINT_JOB);
1692
1693 if (auth)
1694 {
1695 if (auth->type == AUTH_BASIC || auth->type == AUTH_BASICDIGEST)
1696 {
1697 auth_supported = "basic";
1698 cupsdSetAuthInfoRequired(p, "username,password", NULL);
1699 }
1700 else if (auth->type == AUTH_DIGEST)
1701 {
1702 auth_supported = "digest";
1703 cupsdSetAuthInfoRequired(p, "username,password", NULL);
1704 }
1705 #ifdef HAVE_GSSAPI
1706 else if (auth->type == AUTH_NEGOTIATE)
1707 {
1708 auth_supported = "negotiate";
1709 cupsdSetAuthInfoRequired(p, "negotiate", NULL);
1710 }
1711 #endif /* HAVE_GSSAPI */
1712
1713 if (auth->type != AUTH_NONE)
1714 p->type |= CUPS_PRINTER_AUTHENTICATED;
1715 else
1716 p->type &= ~CUPS_PRINTER_AUTHENTICATED;
1717 }
1718 else
1719 p->type &= ~CUPS_PRINTER_AUTHENTICATED;
1720 }
1721
1722 /*
1723 * Create the required IPP attributes for a printer...
1724 */
1725
1726 if (p->attrs)
1727 ippDelete(p->attrs);
1728
1729 p->attrs = ippNew();
1730
1731 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
1732 "uri-authentication-supported", NULL, auth_supported);
1733 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
1734 "uri-security-supported", NULL, "none");
1735 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME, "printer-name", NULL,
1736 p->name);
1737 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-location",
1738 NULL, p->location ? p->location : "");
1739 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-info",
1740 NULL, p->info ? p->info : "");
1741 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI, "printer-more-info",
1742 NULL, p->uri);
1743
1744 if (p->num_users)
1745 {
1746 if (p->deny_users)
1747 ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
1748 "requesting-user-name-denied", p->num_users, NULL,
1749 p->users);
1750 else
1751 ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
1752 "requesting-user-name-allowed", p->num_users, NULL,
1753 p->users);
1754 }
1755
1756 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
1757 "job-quota-period", p->quota_period);
1758 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
1759 "job-k-limit", p->k_limit);
1760 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
1761 "job-page-limit", p->page_limit);
1762 if (p->num_auth_info_required)
1763 ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
1764 "auth-info-required", p->num_auth_info_required,
1765 NULL, p->auth_info_required);
1766 else
1767 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
1768 "auth-info-required", NULL, "none");
1769
1770 if (cupsArrayCount(Banners) > 0 && !(p->type & CUPS_PRINTER_REMOTE))
1771 {
1772 /*
1773 * Setup the job-sheets-default attribute...
1774 */
1775
1776 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
1777 "job-sheets-default", 2, NULL, NULL);
1778
1779 if (attr != NULL)
1780 {
1781 attr->values[0].string.text = _cupsStrAlloc(Classification ?
1782 Classification : p->job_sheets[0]);
1783 attr->values[1].string.text = _cupsStrAlloc(Classification ?
1784 Classification : p->job_sheets[1]);
1785 }
1786 }
1787
1788 printer_type = p->type;
1789
1790 p->raw = 0;
1791 p->remote = 0;
1792
1793 if (p->type & CUPS_PRINTER_REMOTE)
1794 {
1795 /*
1796 * Tell the client this is a remote printer of some type...
1797 */
1798
1799 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI,
1800 "printer-uri-supported", NULL, p->uri);
1801
1802 if (p->make_model)
1803 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
1804 "printer-make-and-model", NULL, p->make_model);
1805
1806 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI, "device-uri", NULL,
1807 p->uri);
1808
1809 p->raw = 1;
1810 p->remote = 1;
1811 }
1812 else
1813 {
1814 /*
1815 * Assign additional attributes depending on whether this is a printer
1816 * or class...
1817 */
1818
1819 p->type &= ~CUPS_PRINTER_OPTIONS;
1820
1821 if (p->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT))
1822 {
1823 p->raw = 1;
1824
1825 /*
1826 * Add class-specific attributes...
1827 */
1828
1829 if ((p->type & CUPS_PRINTER_IMPLICIT) && p->num_printers > 0 &&
1830 p->printers[0]->make_model)
1831 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
1832 "printer-make-and-model", NULL, p->printers[0]->make_model);
1833 else
1834 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
1835 "printer-make-and-model", NULL, "Local Printer Class");
1836
1837 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI, "device-uri", NULL,
1838 "file:///dev/null");
1839
1840 if (p->num_printers > 0)
1841 {
1842 /*
1843 * Add a list of member URIs and names...
1844 */
1845
1846 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI,
1847 "member-uris", p->num_printers, NULL, NULL);
1848 p->type |= CUPS_PRINTER_OPTIONS;
1849
1850 for (i = 0; i < p->num_printers; i ++)
1851 {
1852 if (attr != NULL)
1853 attr->values[i].string.text = _cupsStrAlloc(p->printers[i]->uri);
1854
1855 p->type &= ~CUPS_PRINTER_OPTIONS | p->printers[i]->type;
1856 }
1857
1858 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
1859 "member-names", p->num_printers, NULL, NULL);
1860
1861 if (attr != NULL)
1862 {
1863 for (i = 0; i < p->num_printers; i ++)
1864 attr->values[i].string.text = _cupsStrAlloc(p->printers[i]->name);
1865 }
1866 }
1867 }
1868 else
1869 {
1870 /*
1871 * Add printer-specific attributes... Start by sanitizing the device
1872 * URI so it doesn't have a username or password in it...
1873 */
1874
1875 if (!p->device_uri)
1876 strcpy(uri, "file:/dev/null");
1877 else if (strstr(p->device_uri, "://") != NULL)
1878 {
1879 /*
1880 * http://..., ipp://..., etc.
1881 */
1882
1883 cupsdSanitizeURI(p->device_uri, uri, sizeof(uri));
1884 }
1885 else
1886 {
1887 /*
1888 * file:..., serial:..., etc.
1889 */
1890
1891 strlcpy(uri, p->device_uri, sizeof(uri));
1892 }
1893
1894 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI, "device-uri", NULL,
1895 uri);
1896
1897 /*
1898 * Assign additional attributes from the PPD file (if any)...
1899 */
1900
1901 p->type |= CUPS_PRINTER_BW;
1902 finishings[0] = IPP_FINISHINGS_NONE;
1903 num_finishings = 1;
1904
1905 snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot,
1906 p->name);
1907
1908 if ((ppd = ppdOpenFile(filename)) != NULL)
1909 {
1910 /*
1911 * Add make/model and other various attributes...
1912 */
1913
1914 if (ppd->color_device)
1915 p->type |= CUPS_PRINTER_COLOR;
1916 if (ppd->variable_sizes)
1917 p->type |= CUPS_PRINTER_VARIABLE;
1918 if (!ppd->manual_copies)
1919 p->type |= CUPS_PRINTER_COPIES;
1920 if ((ppdattr = ppdFindAttr(ppd, "cupsFax", NULL)) != NULL)
1921 if (ppdattr->value && !strcasecmp(ppdattr->value, "true"))
1922 p->type |= CUPS_PRINTER_FAX;
1923
1924 ippAddBoolean(p->attrs, IPP_TAG_PRINTER, "color-supported",
1925 ppd->color_device);
1926 if (ppd->throughput)
1927 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
1928 "pages-per-minute", ppd->throughput);
1929
1930 if (ppd->nickname)
1931 {
1932 /*
1933 * The NickName can be localized in the character set specified
1934 * by the LanugageEncoding attribute. However, ppdOpen2() has
1935 * already converted the ppd->nickname member to UTF-8 for us
1936 * (the original attribute value is available separately)
1937 */
1938
1939 cupsdSetString(&p->make_model, ppd->nickname);
1940 }
1941 else if (ppd->modelname)
1942 {
1943 /*
1944 * Model name can only contain specific characters...
1945 */
1946
1947 cupsdSetString(&p->make_model, ppd->modelname);
1948 }
1949 else
1950 cupsdSetString(&p->make_model, "Bad PPD File");
1951
1952 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
1953 "printer-make-and-model", NULL, p->make_model);
1954
1955 /*
1956 * Add media options from the PPD file...
1957 */
1958
1959 if ((input_slot = ppdFindOption(ppd, "InputSlot")) != NULL)
1960 num_media = input_slot->num_choices;
1961 else
1962 num_media = 0;
1963
1964 if ((media_type = ppdFindOption(ppd, "MediaType")) != NULL)
1965 num_media += media_type->num_choices;
1966
1967 if ((page_size = ppdFindOption(ppd, "PageSize")) != NULL)
1968 num_media += page_size->num_choices;
1969
1970 if ((media_quality = ppdFindOption(ppd, "EFMediaQualityMode")) != NULL)
1971 num_media += media_quality->num_choices;
1972
1973 if (num_media == 0)
1974 {
1975 cupsdLogMessage(CUPSD_LOG_CRIT,
1976 "The PPD file for printer %s contains no media "
1977 "options and is therefore invalid!", p->name);
1978 }
1979 else
1980 {
1981 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
1982 "media-supported", num_media, NULL, NULL);
1983 if (attr != NULL)
1984 {
1985 val = attr->values;
1986
1987 if (input_slot != NULL)
1988 for (i = 0; i < input_slot->num_choices; i ++, val ++)
1989 val->string.text = _cupsStrAlloc(input_slot->choices[i].choice);
1990
1991 if (media_type != NULL)
1992 for (i = 0; i < media_type->num_choices; i ++, val ++)
1993 val->string.text = _cupsStrAlloc(media_type->choices[i].choice);
1994
1995 if (media_quality != NULL)
1996 for (i = 0; i < media_quality->num_choices; i ++, val ++)
1997 val->string.text = _cupsStrAlloc(media_quality->choices[i].choice);
1998
1999 if (page_size != NULL)
2000 {
2001 for (i = 0; i < page_size->num_choices; i ++, val ++)
2002 val->string.text = _cupsStrAlloc(page_size->choices[i].choice);
2003
2004 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2005 "media-default", NULL, page_size->defchoice);
2006 }
2007 else if (input_slot != NULL)
2008 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2009 "media-default", NULL, input_slot->defchoice);
2010 else if (media_type != NULL)
2011 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2012 "media-default", NULL, media_type->defchoice);
2013 else if (media_quality != NULL)
2014 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2015 "media-default", NULL, media_quality->defchoice);
2016 else
2017 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2018 "media-default", NULL, "none");
2019 }
2020 }
2021
2022 /*
2023 * Output bin...
2024 */
2025
2026 if ((output_bin = ppdFindOption(ppd, "OutputBin")) != NULL)
2027 {
2028 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2029 "output-bin-supported", output_bin->num_choices,
2030 NULL, NULL);
2031
2032 if (attr != NULL)
2033 {
2034 for (i = 0, val = attr->values;
2035 i < output_bin->num_choices;
2036 i ++, val ++)
2037 val->string.text = _cupsStrAlloc(output_bin->choices[i].choice);
2038 }
2039 }
2040
2041 /*
2042 * Duplexing, etc...
2043 */
2044
2045 if ((duplex = ppdFindOption(ppd, "Duplex")) == NULL)
2046 if ((duplex = ppdFindOption(ppd, "EFDuplex")) == NULL)
2047 if ((duplex = ppdFindOption(ppd, "EFDuplexing")) == NULL)
2048 if ((duplex = ppdFindOption(ppd, "KD03Duplex")) == NULL)
2049 duplex = ppdFindOption(ppd, "JCLDuplex");
2050
2051 if (duplex && duplex->num_choices > 1)
2052 {
2053 p->type |= CUPS_PRINTER_DUPLEX;
2054
2055 ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2056 "sides-supported", 3, NULL, sides);
2057
2058 if (!strcasecmp(duplex->defchoice, "DuplexTumble"))
2059 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2060 "sides-default", NULL, "two-sided-short-edge");
2061 else if (!strcasecmp(duplex->defchoice, "DuplexNoTumble"))
2062 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2063 "sides-default", NULL, "two-sided-long-edge");
2064 else
2065 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2066 "sides-default", NULL, "one-sided");
2067 }
2068
2069 if (ppdFindOption(ppd, "Collate") != NULL)
2070 p->type |= CUPS_PRINTER_COLLATE;
2071
2072 if (ppdFindOption(ppd, "StapleLocation") != NULL)
2073 {
2074 p->type |= CUPS_PRINTER_STAPLE;
2075 finishings[num_finishings++] = IPP_FINISHINGS_STAPLE;
2076 }
2077
2078 if (ppdFindOption(ppd, "BindEdge") != NULL)
2079 {
2080 p->type |= CUPS_PRINTER_BIND;
2081 finishings[num_finishings++] = IPP_FINISHINGS_BIND;
2082 }
2083
2084 for (i = 0; i < ppd->num_sizes; i ++)
2085 if (ppd->sizes[i].length > 1728)
2086 p->type |= CUPS_PRINTER_LARGE;
2087 else if (ppd->sizes[i].length > 1008)
2088 p->type |= CUPS_PRINTER_MEDIUM;
2089 else
2090 p->type |= CUPS_PRINTER_SMALL;
2091
2092 /*
2093 * Add a filter from application/vnd.cups-raw to printer/name to
2094 * handle "raw" printing by users.
2095 */
2096
2097 add_printer_filter(p, p->filetype, "application/vnd.cups-raw 0 -");
2098
2099 /*
2100 * Add any pre-filters in the PPD file...
2101 */
2102
2103 if ((ppdattr = ppdFindAttr(ppd, "cupsPreFilter", NULL)) != NULL)
2104 {
2105 p->prefiltertype = mimeAddType(MimeDatabase, "prefilter", p->name);
2106
2107 for (; ppdattr; ppdattr = ppdFindNextAttr(ppd, "cupsPreFilter", NULL))
2108 if (ppdattr->value)
2109 add_printer_filter(p, p->prefiltertype, ppdattr->value);
2110 }
2111
2112 /*
2113 * Add any filters in the PPD file...
2114 */
2115
2116 DEBUG_printf(("ppd->num_filters = %d\n", ppd->num_filters));
2117 for (i = 0; i < ppd->num_filters; i ++)
2118 {
2119 DEBUG_printf(("ppd->filters[%d] = \"%s\"\n", i, ppd->filters[i]));
2120 add_printer_filter(p, p->filetype, ppd->filters[i]);
2121 }
2122
2123 if (ppd->num_filters == 0)
2124 {
2125 /*
2126 * If there are no filters, add a PostScript printing filter.
2127 */
2128
2129 add_printer_filter(p, p->filetype,
2130 "application/vnd.cups-postscript 0 -");
2131 }
2132
2133 /*
2134 * Show current and available port monitors for this printer...
2135 */
2136
2137 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD, "port-monitor",
2138 NULL, p->port_monitor ? p->port_monitor : "none");
2139
2140
2141 for (i = 1, ppdattr = ppdFindAttr(ppd, "cupsPortMonitor", NULL);
2142 ppdattr;
2143 i ++, ppdattr = ppdFindNextAttr(ppd, "cupsPortMonitor", NULL));
2144
2145 if (ppd->protocols)
2146 {
2147 if (strstr(ppd->protocols, "TBCP"))
2148 i ++;
2149 else if (strstr(ppd->protocols, "BCP"))
2150 i ++;
2151 }
2152
2153 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2154 "port-monitor-supported", i, NULL, NULL);
2155
2156 attr->values[0].string.text = _cupsStrAlloc("none");
2157
2158 for (i = 1, ppdattr = ppdFindAttr(ppd, "cupsPortMonitor", NULL);
2159 ppdattr;
2160 i ++, ppdattr = ppdFindNextAttr(ppd, "cupsPortMonitor", NULL))
2161 attr->values[i].string.text = _cupsStrAlloc(ppdattr->value);
2162
2163 if (ppd->protocols)
2164 {
2165 if (strstr(ppd->protocols, "TBCP"))
2166 attr->values[i].string.text = _cupsStrAlloc("tbcp");
2167 else if (strstr(ppd->protocols, "BCP"))
2168 attr->values[i].string.text = _cupsStrAlloc("bcp");
2169 }
2170
2171 #ifdef HAVE_DNSSD
2172 cupsdSetString(&p->product, ppd->product);
2173 #endif /* HAVE_DNSSD */
2174
2175 /*
2176 * Close the PPD and set the type...
2177 */
2178
2179 ppdClose(ppd);
2180
2181 printer_type = p->type;
2182 }
2183 else if (!access(filename, 0))
2184 {
2185 int pline; /* PPD line number */
2186 ppd_status_t pstatus; /* PPD load status */
2187
2188
2189 pstatus = ppdLastError(&pline);
2190
2191 cupsdLogMessage(CUPSD_LOG_ERROR, "PPD file for %s cannot be loaded!",
2192 p->name);
2193
2194 if (pstatus <= PPD_ALLOC_ERROR)
2195 cupsdLogMessage(CUPSD_LOG_ERROR, "%s", strerror(errno));
2196 else
2197 cupsdLogMessage(CUPSD_LOG_ERROR, "%s on line %d.",
2198 ppdErrorString(pstatus), pline);
2199
2200 cupsdLogMessage(CUPSD_LOG_INFO,
2201 "Hint: Run \"cupstestppd %s\" and fix any errors.",
2202 filename);
2203
2204 /*
2205 * Add a filter from application/vnd.cups-raw to printer/name to
2206 * handle "raw" printing by users.
2207 */
2208
2209 add_printer_filter(p, p->filetype, "application/vnd.cups-raw 0 -");
2210
2211 /*
2212 * Add a PostScript filter, since this is still possibly PS printer.
2213 */
2214
2215 add_printer_filter(p, p->filetype,
2216 "application/vnd.cups-postscript 0 -");
2217 }
2218 else
2219 {
2220 /*
2221 * If we have an interface script, add a filter entry for it...
2222 */
2223
2224 snprintf(filename, sizeof(filename), "%s/interfaces/%s", ServerRoot,
2225 p->name);
2226 if (!access(filename, X_OK))
2227 {
2228 /*
2229 * Yes, we have a System V style interface script; use it!
2230 */
2231
2232 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
2233 "printer-make-and-model", NULL,
2234 "Local System V Printer");
2235
2236 snprintf(filename, sizeof(filename), "*/* 0 %s/interfaces/%s",
2237 ServerRoot, p->name);
2238 add_printer_filter(p, p->filetype, filename);
2239 }
2240 else if (p->device_uri &&
2241 !strncmp(p->device_uri, "ipp://", 6) &&
2242 (strstr(p->device_uri, "/printers/") != NULL ||
2243 strstr(p->device_uri, "/classes/") != NULL))
2244 {
2245 /*
2246 * Tell the client this is really a hard-wired remote printer.
2247 */
2248
2249 printer_type |= CUPS_PRINTER_REMOTE;
2250
2251 /*
2252 * Point the printer-uri-supported attribute to the
2253 * remote printer...
2254 */
2255
2256 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI,
2257 "printer-uri-supported", NULL, p->device_uri);
2258
2259 /*
2260 * Then set the make-and-model accordingly...
2261 */
2262
2263 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
2264 "printer-make-and-model", NULL, "Remote Printer");
2265
2266 /*
2267 * Print all files directly...
2268 */
2269
2270 p->raw = 1;
2271 p->remote = 1;
2272 }
2273 else
2274 {
2275 /*
2276 * Otherwise we have neither - treat this as a "dumb" printer
2277 * with no PPD file...
2278 */
2279
2280 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
2281 "printer-make-and-model", NULL, "Local Raw Printer");
2282
2283 p->raw = 1;
2284 }
2285 }
2286
2287 ippAddIntegers(p->attrs, IPP_TAG_PRINTER, IPP_TAG_ENUM,
2288 "finishings-supported", num_finishings, (int *)finishings);
2289 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_ENUM,
2290 "finishings-default", IPP_FINISHINGS_NONE);
2291 }
2292 }
2293
2294 /*
2295 * Copy the printer options into a browse attributes string we can re-use.
2296 */
2297
2298 if (!(p->type & CUPS_PRINTER_REMOTE))
2299 {
2300 const char *valptr; /* Pointer into value */
2301 char *attrptr; /* Pointer into attribute string */
2302
2303
2304 /*
2305 * Free the old browse attributes as needed...
2306 */
2307
2308 if (p->browse_attrs)
2309 free(p->browse_attrs);
2310
2311 /*
2312 * Compute the length of all attributes + job-sheets, lease-duration,
2313 * and BrowseLocalOptions.
2314 */
2315
2316 for (length = 1, i = p->num_options, option = p->options;
2317 i > 0;
2318 i --, option ++)
2319 {
2320 length += strlen(option->name) + 2;
2321
2322 if (option->value)
2323 {
2324 for (valptr = option->value; *valptr; valptr ++)
2325 if (strchr(" \"\'\\", *valptr))
2326 length += 2;
2327 else
2328 length ++;
2329 }
2330 }
2331
2332 length += 13 + strlen(p->job_sheets[0]) + strlen(p->job_sheets[1]);
2333 length += 32;
2334 if (BrowseLocalOptions)
2335 length += 12 + strlen(BrowseLocalOptions);
2336
2337 if (p->num_auth_info_required > 0)
2338 {
2339 length += 18; /* auth-info-required */
2340
2341 for (i = 0; i < p->num_auth_info_required; i ++)
2342 length += strlen(p->auth_info_required[i]) + 1;
2343 }
2344
2345 /*
2346 * Allocate the new string...
2347 */
2348
2349 if ((p->browse_attrs = calloc(1, length)) == NULL)
2350 cupsdLogMessage(CUPSD_LOG_ERROR,
2351 "Unable to allocate %d bytes for browse data!",
2352 length);
2353 else
2354 {
2355 /*
2356 * Got the allocated string, now copy the options and attributes over...
2357 */
2358
2359 sprintf(p->browse_attrs, "job-sheets=%s,%s lease-duration=%d",
2360 p->job_sheets[0], p->job_sheets[1], BrowseTimeout);
2361 attrptr = p->browse_attrs + strlen(p->browse_attrs);
2362
2363 if (BrowseLocalOptions)
2364 {
2365 sprintf(attrptr, " ipp-options=%s", BrowseLocalOptions);
2366 attrptr += strlen(attrptr);
2367 }
2368
2369 for (i = p->num_options, option = p->options;
2370 i > 0;
2371 i --, option ++)
2372 {
2373 *attrptr++ = ' ';
2374 strcpy(attrptr, option->name);
2375 attrptr += strlen(attrptr);
2376
2377 if (option->value)
2378 {
2379 *attrptr++ = '=';
2380
2381 for (valptr = option->value; *valptr; valptr ++)
2382 {
2383 if (strchr(" \"\'\\", *valptr))
2384 *attrptr++ = '\\';
2385
2386 *attrptr++ = *valptr;
2387 }
2388 }
2389 }
2390
2391 if (p->num_auth_info_required > 0)
2392 {
2393 strcpy(attrptr, "auth-info-required");
2394 attrptr += 18;
2395
2396 for (i = 0; i < p->num_auth_info_required; i ++)
2397 {
2398 *attrptr++ = i ? ',' : '=';
2399 strcpy(attrptr, p->auth_info_required[i]);
2400 attrptr += strlen(attrptr);
2401 }
2402 }
2403 else
2404 *attrptr = '\0';
2405 }
2406 }
2407
2408 /*
2409 * Populate the document-format-supported attribute...
2410 */
2411
2412 add_printer_formats(p);
2413
2414 DEBUG_printf(("cupsdSetPrinterAttrs: leaving name = %s, type = %x\n", p->name,
2415 p->type));
2416
2417 /*
2418 * Add name-default attributes...
2419 */
2420
2421 add_printer_defaults(p);
2422
2423 #ifdef __sgi
2424 /*
2425 * Write the IRIX printer config and status files...
2426 */
2427
2428 write_irix_config(p);
2429 write_irix_state(p);
2430 #endif /* __sgi */
2431
2432 /*
2433 * Let the browse protocols reflect the change
2434 */
2435
2436 cupsdRegisterPrinter(p);
2437 }
2438
2439
2440 /*
2441 * 'cupsdSetPrinterReasons()' - Set/update the reasons strings.
2442 */
2443
2444 void
2445 cupsdSetPrinterReasons(
2446 cupsd_printer_t *p, /* I - Printer */
2447 const char *s) /* I - Reasons strings */
2448 {
2449 int i; /* Looping var */
2450 const char *sptr; /* Pointer into reasons */
2451 char reason[255], /* Reason string */
2452 *rptr; /* Pointer into reason */
2453
2454
2455 if (s[0] == '-' || s[0] == '+')
2456 {
2457 /*
2458 * Add/remove reasons...
2459 */
2460
2461 sptr = s + 1;
2462 }
2463 else
2464 {
2465 /*
2466 * Replace reasons...
2467 */
2468
2469 sptr = s;
2470
2471 for (i = 0; i < p->num_reasons; i ++)
2472 free(p->reasons[i]);
2473
2474 p->num_reasons = 0;
2475 }
2476
2477 /*
2478 * Loop through all of the reasons...
2479 */
2480
2481 while (*sptr)
2482 {
2483 /*
2484 * Skip leading whitespace and commas...
2485 */
2486
2487 while (isspace(*sptr & 255) || *sptr == ',')
2488 sptr ++;
2489
2490 for (rptr = reason; *sptr && !isspace(*sptr & 255) && *sptr != ','; sptr ++)
2491 if (rptr < (reason + sizeof(reason) - 1))
2492 *rptr++ = *sptr;
2493
2494 if (rptr == reason)
2495 break;
2496
2497 *rptr = '\0';
2498
2499 if (s[0] == '-')
2500 {
2501 /*
2502 * Remove reason...
2503 */
2504
2505 for (i = 0; i < p->num_reasons; i ++)
2506 if (!strcasecmp(reason, p->reasons[i]))
2507 {
2508 /*
2509 * Found a match, so remove it...
2510 */
2511
2512 p->num_reasons --;
2513 free(p->reasons[i]);
2514
2515 if (i < p->num_reasons)
2516 memmove(p->reasons + i, p->reasons + i + 1,
2517 (p->num_reasons - i) * sizeof(char *));
2518
2519 i --;
2520
2521 if (!strcmp(reason, "paused") && p->state == IPP_PRINTER_STOPPED)
2522 cupsdSetPrinterState(p, IPP_PRINTER_IDLE, 1);
2523 }
2524 }
2525 else if (p->num_reasons < (int)(sizeof(p->reasons) / sizeof(p->reasons[0])))
2526 {
2527 /*
2528 * Add reason...
2529 */
2530
2531 for (i = 0; i < p->num_reasons; i ++)
2532 if (!strcasecmp(reason, p->reasons[i]))
2533 break;
2534
2535 if (i >= p->num_reasons)
2536 {
2537 p->reasons[i] = strdup(reason);
2538 p->num_reasons ++;
2539
2540 if (!strcmp(reason, "paused") && p->state != IPP_PRINTER_STOPPED)
2541 cupsdSetPrinterState(p, IPP_PRINTER_STOPPED, 1);
2542 }
2543 }
2544 }
2545 }
2546
2547
2548 /*
2549 * 'cupsdSetPrinterState()' - Update the current state of a printer.
2550 */
2551
2552 void
2553 cupsdSetPrinterState(
2554 cupsd_printer_t *p, /* I - Printer to change */
2555 ipp_pstate_t s, /* I - New state */
2556 int update) /* I - Update printers.conf? */
2557 {
2558 ipp_pstate_t old_state; /* Old printer state */
2559
2560
2561 /*
2562 * Can't set status of remote printers...
2563 */
2564
2565 if (p->type & CUPS_PRINTER_REMOTE)
2566 return;
2567
2568 /*
2569 * Set the new state...
2570 */
2571
2572 old_state = p->state;
2573 p->state = s;
2574
2575 if (old_state != s)
2576 {
2577 cupsdAddEvent(CUPSD_EVENT_PRINTER_STATE_CHANGED, p, NULL,
2578 "%s \"%s\" state changed.",
2579 (p->type & CUPS_PRINTER_CLASS) ? "Class" : "Printer",
2580 p->name);
2581
2582 /*
2583 * Let the browse code know this needs to be updated...
2584 */
2585
2586 BrowseNext = p;
2587 p->state_time = time(NULL);
2588 p->browse_time = 0;
2589
2590 #ifdef __sgi
2591 write_irix_state(p);
2592 #endif /* __sgi */
2593 }
2594
2595 cupsdAddPrinterHistory(p);
2596
2597 /*
2598 * Let the browse protocols reflect the change...
2599 */
2600
2601 cupsdRegisterPrinter(p);
2602
2603 /*
2604 * Save the printer configuration if a printer goes from idle or processing
2605 * to stopped (or visa-versa)...
2606 */
2607
2608 if ((old_state == IPP_PRINTER_STOPPED) != (s == IPP_PRINTER_STOPPED) &&
2609 update)
2610 {
2611 if (p->type & CUPS_PRINTER_CLASS)
2612 cupsdSaveAllClasses();
2613 else
2614 cupsdSaveAllPrinters();
2615 }
2616 }
2617
2618
2619 /*
2620 * 'cupsdStopPrinter()' - Stop a printer from printing any jobs...
2621 */
2622
2623 void
2624 cupsdStopPrinter(cupsd_printer_t *p, /* I - Printer to stop */
2625 int update)/* I - Update printers.conf? */
2626 {
2627 cupsd_job_t *job; /* Active print job */
2628
2629
2630 /*
2631 * Set the printer state...
2632 */
2633
2634 cupsdSetPrinterState(p, IPP_PRINTER_STOPPED, update);
2635
2636 /*
2637 * See if we have a job printing on this printer...
2638 */
2639
2640 if (p->job)
2641 {
2642 /*
2643 * Get pointer to job...
2644 */
2645
2646 job = (cupsd_job_t *)p->job;
2647
2648 /*
2649 * Stop it...
2650 */
2651
2652 cupsdStopJob(job, 0);
2653
2654 /*
2655 * Reset the state to pending...
2656 */
2657
2658 job->state->values[0].integer = IPP_JOB_PENDING;
2659 job->state_value = IPP_JOB_PENDING;
2660
2661 cupsdSaveJob(job);
2662
2663 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED, p, job,
2664 "Job stopped due to printer being paused");
2665 }
2666 }
2667
2668
2669 /*
2670 * 'cupsdUpdatePrinters()' - Update printers after a partial reload.
2671 */
2672
2673 void
2674 cupsdUpdatePrinters(void)
2675 {
2676 cupsd_printer_t *p; /* Current printer */
2677
2678
2679 /*
2680 * Loop through the printers and recreate the printer attributes
2681 * for any local printers since the policy and/or access control
2682 * stuff may have changed. Also, if browsing is disabled, remove
2683 * any remote printers...
2684 */
2685
2686 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
2687 p;
2688 p = (cupsd_printer_t *)cupsArrayNext(Printers))
2689 {
2690 /*
2691 * Remove remote printers if we are no longer browsing...
2692 */
2693
2694 if (!Browsing && (p->type & (CUPS_PRINTER_IMPLICIT | CUPS_PRINTER_REMOTE)))
2695 {
2696 if (p->type & CUPS_PRINTER_IMPLICIT)
2697 cupsArrayRemove(ImplicitPrinters, p);
2698
2699 cupsArraySave(Printers);
2700 cupsdDeletePrinter(p, 0);
2701 cupsArrayRestore(Printers);
2702 continue;
2703 }
2704
2705 /*
2706 * Update the operation policy pointer...
2707 */
2708
2709 if ((p->op_policy_ptr = cupsdFindPolicy(p->op_policy)) == NULL)
2710 p->op_policy_ptr = DefaultPolicyPtr;
2711
2712 /*
2713 * Update printer attributes as needed...
2714 */
2715
2716 if (!(p->type & CUPS_PRINTER_REMOTE))
2717 cupsdSetPrinterAttrs(p);
2718 }
2719 }
2720
2721
2722 /*
2723 * 'cupsdValidateDest()' - Validate a printer/class destination.
2724 */
2725
2726 const char * /* O - Printer or class name */
2727 cupsdValidateDest(
2728 const char *uri, /* I - Printer URI */
2729 cups_ptype_t *dtype, /* O - Type (printer or class) */
2730 cupsd_printer_t **printer) /* O - Printer pointer */
2731 {
2732 cupsd_printer_t *p; /* Current printer */
2733 char localname[1024],/* Localized hostname */
2734 *lptr, /* Pointer into localized hostname */
2735 *sptr, /* Pointer into server name */
2736 *rptr, /* Pointer into resource */
2737 scheme[32], /* Scheme portion of URI */
2738 username[64], /* Username portion of URI */
2739 hostname[HTTP_MAX_HOST],
2740 /* Host portion of URI */
2741 resource[HTTP_MAX_URI];
2742 /* Resource portion of URI */
2743 int port; /* Port portion of URI */
2744
2745
2746 DEBUG_printf(("cupsdValidateDest(uri=\"%s\", dtype=%p, printer=%p)\n", uri,
2747 dtype, printer));
2748
2749 /*
2750 * Initialize return values...
2751 */
2752
2753 if (printer)
2754 *printer = NULL;
2755
2756 if (dtype)
2757 *dtype = (cups_ptype_t)0;
2758
2759 /*
2760 * Pull the hostname and resource from the URI...
2761 */
2762
2763 httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, sizeof(scheme),
2764 username, sizeof(username), hostname, sizeof(hostname),
2765 &port, resource, sizeof(resource));
2766
2767 /*
2768 * See if the resource is a class or printer...
2769 */
2770
2771 if (!strncmp(resource, "/classes/", 9))
2772 {
2773 /*
2774 * Class...
2775 */
2776
2777 rptr = resource + 9;
2778 }
2779 else if (!strncmp(resource, "/printers/", 10))
2780 {
2781 /*
2782 * Printer...
2783 */
2784
2785 rptr = resource + 10;
2786 }
2787 else
2788 {
2789 /*
2790 * Bad resource name...
2791 */
2792
2793 return (NULL);
2794 }
2795
2796 /*
2797 * See if the printer or class name exists...
2798 */
2799
2800 p = cupsdFindDest(rptr);
2801
2802 if (p == NULL && strchr(rptr, '@') == NULL)
2803 return (NULL);
2804 else if (p != NULL)
2805 {
2806 if (printer)
2807 *printer = p;
2808
2809 if (dtype)
2810 *dtype = p->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT |
2811 CUPS_PRINTER_REMOTE);
2812
2813 return (p->name);
2814 }
2815
2816 /*
2817 * Change localhost to the server name...
2818 */
2819
2820 if (!strcasecmp(hostname, "localhost"))
2821 strlcpy(hostname, ServerName, sizeof(hostname));
2822
2823 strlcpy(localname, hostname, sizeof(localname));
2824
2825 if (!strcasecmp(hostname, ServerName))
2826 {
2827 /*
2828 * Localize the hostname...
2829 */
2830
2831 lptr = strchr(localname, '.');
2832 sptr = strchr(ServerName, '.');
2833
2834 if (sptr != NULL && lptr != NULL)
2835 {
2836 /*
2837 * Strip the common domain name components...
2838 */
2839
2840 while (lptr != NULL)
2841 {
2842 if (!strcasecmp(lptr, sptr))
2843 {
2844 *lptr = '\0';
2845 break;
2846 }
2847 else
2848 lptr = strchr(lptr + 1, '.');
2849 }
2850 }
2851 }
2852
2853 DEBUG_printf(("localized hostname is \"%s\"...\n", localname));
2854
2855 /*
2856 * Find a matching printer or class...
2857 */
2858
2859 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
2860 p;
2861 p = (cupsd_printer_t *)cupsArrayNext(Printers))
2862 if (!strcasecmp(p->hostname, localname) &&
2863 !strcasecmp(p->name, rptr))
2864 {
2865 if (printer)
2866 *printer = p;
2867
2868 if (dtype)
2869 *dtype = p->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT |
2870 CUPS_PRINTER_REMOTE);
2871
2872 return (p->name);
2873 }
2874
2875 return (NULL);
2876 }
2877
2878
2879 /*
2880 * 'cupsdWritePrintcap()' - Write a pseudo-printcap file for older applications
2881 * that need it...
2882 */
2883
2884 void
2885 cupsdWritePrintcap(void)
2886 {
2887 cups_file_t *fp; /* printcap file */
2888 cupsd_printer_t *p; /* Current printer */
2889
2890
2891 #ifdef __sgi
2892 /*
2893 * Update the IRIX printer state for the default printer; if
2894 * no printers remain, then the default printer file will be
2895 * removed...
2896 */
2897
2898 write_irix_state(DefaultPrinter);
2899 #endif /* __sgi */
2900
2901 /*
2902 * See if we have a printcap file; if not, don't bother writing it.
2903 */
2904
2905 if (!Printcap || !*Printcap)
2906 return;
2907
2908 /*
2909 * Open the printcap file...
2910 */
2911
2912 if ((fp = cupsFileOpen(Printcap, "w")) == NULL)
2913 return;
2914
2915 /*
2916 * Put a comment header at the top so that users will know where the
2917 * data has come from...
2918 */
2919
2920 cupsFilePuts(fp, "# This file was automatically generated by cupsd(8) from the\n");
2921 cupsFilePrintf(fp, "# %s/printers.conf file. All changes to this file\n",
2922 ServerRoot);
2923 cupsFilePuts(fp, "# will be lost.\n");
2924
2925 if (Printers)
2926 {
2927 /*
2928 * Write a new printcap with the current list of printers.
2929 */
2930
2931 switch (PrintcapFormat)
2932 {
2933 case PRINTCAP_BSD:
2934 /*
2935 * Each printer is put in the file as:
2936 *
2937 * Printer1:
2938 * Printer2:
2939 * Printer3:
2940 * ...
2941 * PrinterN:
2942 */
2943
2944 if (DefaultPrinter)
2945 cupsFilePrintf(fp, "%s|%s:rm=%s:rp=%s:\n", DefaultPrinter->name,
2946 DefaultPrinter->info, ServerName, DefaultPrinter->name);
2947
2948 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
2949 p;
2950 p = (cupsd_printer_t *)cupsArrayNext(Printers))
2951 if (p != DefaultPrinter)
2952 cupsFilePrintf(fp, "%s|%s:rm=%s:rp=%s:\n", p->name, p->info,
2953 ServerName, p->name);
2954 break;
2955
2956 case PRINTCAP_SOLARIS:
2957 /*
2958 * Each printer is put in the file as:
2959 *
2960 * _all:all=Printer1,Printer2,Printer3,...,PrinterN
2961 * _default:use=DefaultPrinter
2962 * Printer1:\
2963 * :bsdaddr=ServerName,Printer1:\
2964 * :description=Description:
2965 * Printer2:
2966 * :bsdaddr=ServerName,Printer2:\
2967 * :description=Description:
2968 * Printer3:
2969 * :bsdaddr=ServerName,Printer3:\
2970 * :description=Description:
2971 * ...
2972 * PrinterN:
2973 * :bsdaddr=ServerName,PrinterN:\
2974 * :description=Description:
2975 */
2976
2977 cupsFilePuts(fp, "_all:all=");
2978 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
2979 p;
2980 p = (cupsd_printer_t *)cupsArrayCurrent(Printers))
2981 cupsFilePrintf(fp, "%s%c", p->name,
2982 cupsArrayNext(Printers) ? ',' : '\n');
2983
2984 if (DefaultPrinter)
2985 cupsFilePrintf(fp, "_default:use=%s\n", DefaultPrinter->name);
2986
2987 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
2988 p;
2989 p = (cupsd_printer_t *)cupsArrayNext(Printers))
2990 cupsFilePrintf(fp, "%s:\\\n"
2991 "\t:bsdaddr=%s,%s:\\\n"
2992 "\t:description=%s:\n",
2993 p->name, ServerName, p->name, p->info ? p->info : "");
2994 break;
2995 }
2996 }
2997
2998 /*
2999 * Close the file...
3000 */
3001
3002 cupsFileClose(fp);
3003 }
3004
3005
3006 /*
3007 * 'cupsdSanitizeURI()' - Sanitize a device URI...
3008 */
3009
3010 char * /* O - New device URI */
3011 cupsdSanitizeURI(const char *uri, /* I - Original device URI */
3012 char *buffer, /* O - New device URI */
3013 int buflen) /* I - Size of new device URI buffer */
3014 {
3015 char *start, /* Start of data after scheme */
3016 *slash, /* First slash after scheme:// */
3017 *ptr; /* Pointer into user@host:port part */
3018
3019
3020 /*
3021 * Range check input...
3022 */
3023
3024 if (!uri || !buffer || buflen < 2)
3025 return (NULL);
3026
3027 /*
3028 * Copy the device URI to the new buffer...
3029 */
3030
3031 strlcpy(buffer, uri, buflen);
3032
3033 /*
3034 * Find the end of the scheme:// part...
3035 */
3036
3037 if ((ptr = strchr(buffer, ':')) == NULL)
3038 return (buffer); /* No scheme: part... */
3039
3040 for (start = ptr + 1; *start; start ++)
3041 if (*start != '/')
3042 break;
3043
3044 /*
3045 * Find the next slash (/) in the URI...
3046 */
3047
3048 if ((slash = strchr(start, '/')) == NULL)
3049 slash = start + strlen(start); /* No slash, point to the end */
3050
3051 /*
3052 * Check for an @ sign before the slash...
3053 */
3054
3055 if ((ptr = strchr(start, '@')) != NULL && ptr < slash)
3056 {
3057 /*
3058 * Found an @ sign and it is before the resource part, so we have
3059 * an authentication string. Copy the remaining URI over the
3060 * authentication string...
3061 */
3062
3063 _cups_strcpy(start, ptr + 1);
3064 }
3065
3066 /*
3067 * Return the new device URI...
3068 */
3069
3070 return (buffer);
3071 }
3072
3073
3074 /*
3075 * 'add_printer_defaults()' - Add name-default attributes to the printer attributes.
3076 */
3077
3078 static void
3079 add_printer_defaults(cupsd_printer_t *p)/* I - Printer */
3080 {
3081 int i; /* Looping var */
3082 int num_options; /* Number of default options */
3083 cups_option_t *options, /* Default options */
3084 *option; /* Current option */
3085 char name[256]; /* name-default */
3086
3087
3088 /*
3089 * Maintain a common array of default attribute names...
3090 */
3091
3092 if (!CommonDefaults)
3093 {
3094 CommonDefaults = cupsArrayNew((cups_array_func_t)strcmp, NULL);
3095
3096 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("copies-default"));
3097 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("document-format-default"));
3098 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("finishings-default"));
3099 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("job-hold-until-default"));
3100 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("job-priority-default"));
3101 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("job-sheets-default"));
3102 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("media-default"));
3103 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("number-up-default"));
3104 cupsArrayAdd(CommonDefaults,
3105 _cupsStrAlloc("orientation-requested-default"));
3106 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("sides-default"));
3107 }
3108
3109 /*
3110 * Add all of the default options from the .conf files...
3111 */
3112
3113 for (num_options = 0, i = p->num_options, option = p->options;
3114 i > 0;
3115 i --, option ++)
3116 {
3117 if (strcmp(option->name, "ipp-options") &&
3118 strcmp(option->name, "job-sheets") &&
3119 strcmp(option->name, "lease-duration"))
3120 {
3121 snprintf(name, sizeof(name), "%s-default", option->name);
3122 num_options = cupsAddOption(name, option->value, num_options, &options);
3123
3124 if (!cupsArrayFind(CommonDefaults, name))
3125 cupsArrayAdd(CommonDefaults, _cupsStrAlloc(name));
3126 }
3127 }
3128
3129 /*
3130 * Convert options to IPP attributes...
3131 */
3132
3133 cupsEncodeOptions2(p->attrs, num_options, options, IPP_TAG_PRINTER);
3134 cupsFreeOptions(num_options, options);
3135
3136 /*
3137 * Add standard -default attributes as needed...
3138 */
3139
3140 if (!cupsGetOption("copies", p->num_options, p->options))
3141 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER, "copies-default",
3142 1);
3143
3144 if (!cupsGetOption("document-format", p->num_options, p->options))
3145 ippAddString(CommonData, IPP_TAG_PRINTER, IPP_TAG_MIMETYPE,
3146 "document-format-default", NULL, "application/octet-stream");
3147
3148 if (!cupsGetOption("job-hold-until", p->num_options, p->options))
3149 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
3150 "job-hold-until-default", NULL, "no-hold");
3151
3152 if (!cupsGetOption("job-priority", p->num_options, p->options))
3153 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
3154 "job-priority-default", 50);
3155
3156 if (!cupsGetOption("number-up", p->num_options, p->options))
3157 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
3158 "number-up-default", 1);
3159
3160 if (!cupsGetOption("orientation-requested", p->num_options, p->options))
3161 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_ENUM,
3162 "orientation-requested-default", IPP_PORTRAIT);
3163
3164 if (!cupsGetOption("notify-lease-duration", p->num_options, p->options))
3165 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
3166 "notify-lease-duration-default", DefaultLeaseDuration);
3167
3168 if (!cupsGetOption("notify-events", p->num_options, p->options))
3169 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
3170 "notify-events-default", NULL, "job-completed");
3171 }
3172
3173
3174 /*
3175 * 'add_printer_filter()' - Add a MIME filter for a printer.
3176 */
3177
3178 static void
3179 add_printer_filter(
3180 cupsd_printer_t *p, /* I - Printer to add to */
3181 mime_type_t *filtertype, /* I - Filter or prefilter MIME type */
3182 const char *filter) /* I - Filter to add */
3183 {
3184 char super[MIME_MAX_SUPER], /* Super-type for filter */
3185 type[MIME_MAX_TYPE], /* Type for filter */
3186 program[1024]; /* Program/filter name */
3187 int cost; /* Cost of filter */
3188 mime_type_t *temptype; /* MIME type looping var */
3189 char filename[1024]; /* Full filter filename */
3190
3191
3192 /*
3193 * Parse the filter string; it should be in the following format:
3194 *
3195 * super/type cost program
3196 */
3197
3198 if (sscanf(filter, "%15[^/]/%31s%d%1023s", super, type, &cost, program) != 4)
3199 {
3200 cupsdLogMessage(CUPSD_LOG_ERROR, "%s: invalid filter string \"%s\"!",
3201 p->name, filter);
3202 return;
3203 }
3204
3205 /*
3206 * See if the filter program exists; if not, stop the printer and flag
3207 * the error!
3208 */
3209
3210 if (strcmp(program, "-"))
3211 {
3212 if (program[0] == '/')
3213 strlcpy(filename, program, sizeof(filename));
3214 else
3215 snprintf(filename, sizeof(filename), "%s/filter/%s", ServerBin, program);
3216
3217 if (access(filename, X_OK))
3218 {
3219 snprintf(p->state_message, sizeof(p->state_message),
3220 "Filter \"%s\" for printer \"%s\" not available: %s",
3221 program, p->name, strerror(errno));
3222 cupsdSetPrinterReasons(p, "+cups-missing-filter-error");
3223 cupsdSetPrinterState(p, IPP_PRINTER_STOPPED, 0);
3224
3225 cupsdLogMessage(CUPSD_LOG_ERROR, "%s", p->state_message);
3226 }
3227 }
3228
3229 /*
3230 * Mark the CUPS_PRINTER_COMMANDS bit if we have a filter for
3231 * application/vnd.cups-command...
3232 */
3233
3234 if (!strcasecmp(super, "application") &&
3235 !strcasecmp(type, "vnd.cups-command"))
3236 p->type |= CUPS_PRINTER_COMMANDS;
3237
3238 /*
3239 * Add the filter to the MIME database, supporting wildcards as needed...
3240 */
3241
3242 for (temptype = mimeFirstType(MimeDatabase);
3243 temptype;
3244 temptype = mimeNextType(MimeDatabase))
3245 if (((super[0] == '*' && strcasecmp(temptype->super, "printer")) ||
3246 !strcasecmp(temptype->super, super)) &&
3247 (type[0] == '*' || !strcasecmp(temptype->type, type)))
3248 {
3249 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3250 "add_printer_filter: %s: adding filter %s/%s %s/%s %d %s",
3251 p->name, temptype->super, temptype->type,
3252 filtertype->super, filtertype->type,
3253 cost, program);
3254 mimeAddFilter(MimeDatabase, temptype, filtertype, cost, program);
3255 }
3256 }
3257
3258
3259 /*
3260 * 'add_printer_formats()' - Add document-format-supported values for a printer.
3261 */
3262
3263 static void
3264 add_printer_formats(cupsd_printer_t *p) /* I - Printer */
3265 {
3266 int i; /* Looping var */
3267 mime_type_t *type; /* Current MIME type */
3268 cups_array_t *filters; /* Filters */
3269 ipp_attribute_t *attr; /* document-format-supported attribute */
3270 char mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2];
3271 /* MIME type name */
3272
3273
3274 /*
3275 * Raw (and remote) queues advertise all of the supported MIME
3276 * types...
3277 */
3278
3279 cupsArrayDelete(p->filetypes);
3280 p->filetypes = NULL;
3281
3282 if (p->raw)
3283 {
3284 ippAddStrings(p->attrs, IPP_TAG_PRINTER,
3285 (ipp_tag_t)(IPP_TAG_MIMETYPE | IPP_TAG_COPY),
3286 "document-format-supported", NumMimeTypes, NULL, MimeTypes);
3287 return;
3288 }
3289
3290 /*
3291 * Otherwise, loop through the supported MIME types and see if there
3292 * are filters for them...
3293 */
3294
3295 cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_printer_formats: %d types, %d filters",
3296 mimeNumTypes(MimeDatabase), mimeNumFilters(MimeDatabase));
3297
3298 p->filetypes = cupsArrayNew(NULL, NULL);
3299
3300 for (type = mimeFirstType(MimeDatabase);
3301 type;
3302 type = mimeNextType(MimeDatabase))
3303 {
3304 snprintf(mimetype, sizeof(mimetype), "%s/%s", type->super, type->type);
3305
3306 if ((filters = mimeFilter(MimeDatabase, type, p->filetype, NULL)) != NULL)
3307 {
3308 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3309 "add_printer_formats: %s: %s needs %d filters",
3310 p->name, mimetype, cupsArrayCount(filters));
3311
3312 cupsArrayDelete(filters);
3313 cupsArrayAdd(p->filetypes, type);
3314 }
3315 else
3316 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3317 "add_printer_formats: %s: %s not supported",
3318 p->name, mimetype);
3319 }
3320
3321 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3322 "add_printer_formats: %s: %d supported types",
3323 p->name, cupsArrayCount(p->filetypes) + 1);
3324
3325 /*
3326 * Add the file formats that can be filtered...
3327 */
3328
3329 if ((type = mimeType(MimeDatabase, "application", "octet-stream")) == NULL ||
3330 !cupsArrayFind(p->filetypes, type))
3331 i = 1;
3332 else
3333 i = 0;
3334
3335 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_MIMETYPE,
3336 "document-format-supported",
3337 cupsArrayCount(p->filetypes) + 1, NULL, NULL);
3338
3339 if (i)
3340 attr->values[0].string.text = _cupsStrAlloc("application/octet-stream");
3341
3342 for (type = (mime_type_t *)cupsArrayFirst(p->filetypes);
3343 type;
3344 i ++, type = (mime_type_t *)cupsArrayNext(p->filetypes))
3345 {
3346 snprintf(mimetype, sizeof(mimetype), "%s/%s", type->super, type->type);
3347
3348 attr->values[i].string.text = _cupsStrAlloc(mimetype);
3349 }
3350
3351 #ifdef HAVE_DNSSD
3352 {
3353 char pdl[1024]; /* Buffer to build pdl list */
3354 mime_filter_t *filter; /* MIME filter looping var */
3355
3356
3357 pdl[0] = '\0';
3358
3359 if (mimeType(MimeDatabase, "application", "pdf"))
3360 strlcat(pdl, "application/pdf,", sizeof(pdl));
3361
3362 if (mimeType(MimeDatabase, "application", "postscript"))
3363 strlcat(pdl, "application/postscript,", sizeof(pdl));
3364
3365 if (mimeType(MimeDatabase, "application", "vnd.cups-raster"))
3366 strlcat(pdl, "application/vnd.cups-raster,", sizeof(pdl));
3367
3368 /*
3369 * Determine if this is a Tioga PrintJobMgr based queue...
3370 */
3371
3372 for (filter = (mime_filter_t *)cupsArrayFirst(MimeDatabase->filters);
3373 filter;
3374 filter = (mime_filter_t *)cupsArrayNext(MimeDatabase->filters))
3375 {
3376 if (filter->dst == p->filetype && filter->filter &&
3377 strstr(filter->filter, "PrintJobMgr"))
3378 break;
3379 }
3380
3381 /*
3382 * We only support raw printing if this is not a Tioga PrintJobMgr based
3383 * queue and if application/octet-stream is a known conversion...
3384 */
3385
3386 if (!filter && mimeType(MimeDatabase, "application", "octet-stream"))
3387 strlcat(pdl, "application/octet-stream,", sizeof(pdl));
3388
3389 if (mimeType(MimeDatabase, "image", "png"))
3390 strlcat(pdl, "image/png,", sizeof(pdl));
3391
3392 if (pdl[0])
3393 pdl[strlen(pdl) - 1] = '\0'; /* Remove trailing comma */
3394
3395 cupsdSetString(&p->pdl, pdl);
3396 }
3397 #endif /* HAVE_DNSSD */
3398 }
3399
3400
3401 /*
3402 * 'compare_printers()' - Compare two printers.
3403 */
3404
3405 static int /* O - Result of comparison */
3406 compare_printers(void *first, /* I - First printer */
3407 void *second, /* I - Second printer */
3408 void *data) /* I - App data (not used) */
3409 {
3410 return (strcasecmp(((cupsd_printer_t *)first)->name,
3411 ((cupsd_printer_t *)second)->name));
3412 }
3413
3414
3415 /*
3416 * 'delete_printer_filters()' - Delete all MIME filters for a printer.
3417 */
3418
3419 static void
3420 delete_printer_filters(
3421 cupsd_printer_t *p) /* I - Printer to remove from */
3422 {
3423 mime_filter_t *filter; /* MIME filter looping var */
3424
3425
3426 /*
3427 * Range check input...
3428 */
3429
3430 if (p == NULL)
3431 return;
3432
3433 /*
3434 * Remove all filters from the MIME database that have a destination
3435 * type == printer...
3436 */
3437
3438 for (filter = mimeFirstFilter(MimeDatabase);
3439 filter;
3440 filter = mimeNextFilter(MimeDatabase))
3441 if (filter->dst == p->filetype)
3442 {
3443 /*
3444 * Delete the current filter...
3445 */
3446
3447 mimeDeleteFilter(MimeDatabase, filter);
3448 }
3449 }
3450
3451
3452 #ifdef __sgi
3453 /*
3454 * 'write_irix_config()' - Update the config files used by the IRIX
3455 * desktop tools.
3456 */
3457
3458 static void
3459 write_irix_config(cupsd_printer_t *p) /* I - Printer to update */
3460 {
3461 char filename[1024]; /* Interface script filename */
3462 cups_file_t *fp; /* Interface script file */
3463 ipp_attribute_t *attr; /* Attribute data */
3464
3465
3466 /*
3467 * Add dummy interface and GUI scripts to fool SGI's "challenged" printing
3468 * tools. First the interface script that tells the tools what kind of
3469 * printer we have...
3470 */
3471
3472 snprintf(filename, sizeof(filename), "/var/spool/lp/interface/%s", p->name);
3473
3474 if (p->type & CUPS_PRINTER_CLASS)
3475 unlink(filename);
3476 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
3477 {
3478 cupsFilePuts(fp, "#!/bin/sh\n");
3479
3480 if ((attr = ippFindAttribute(p->attrs, "printer-make-and-model",
3481 IPP_TAG_TEXT)) != NULL)
3482 cupsFilePrintf(fp, "NAME=\"%s\"\n", attr->values[0].string.text);
3483 else if (p->type & CUPS_PRINTER_CLASS)
3484 cupsFilePuts(fp, "NAME=\"Printer Class\"\n");
3485 else
3486 cupsFilePuts(fp, "NAME=\"Remote Destination\"\n");
3487
3488 if (p->type & CUPS_PRINTER_COLOR)
3489 cupsFilePuts(fp, "TYPE=ColorPostScript\n");
3490 else
3491 cupsFilePuts(fp, "TYPE=MonoPostScript\n");
3492
3493 cupsFilePrintf(fp, "HOSTNAME=%s\n", ServerName);
3494 cupsFilePrintf(fp, "HOSTPRINTER=%s\n", p->name);
3495
3496 cupsFileClose(fp);
3497
3498 chmod(filename, 0755);
3499 chown(filename, User, Group);
3500 }
3501
3502 /*
3503 * Then the member file that tells which device file the queue is connected
3504 * to... Networked printers use "/dev/null" in this file, so that's what
3505 * we use (the actual device URI can confuse some apps...)
3506 */
3507
3508 snprintf(filename, sizeof(filename), "/var/spool/lp/member/%s", p->name);
3509
3510 if (p->type & CUPS_PRINTER_CLASS)
3511 unlink(filename);
3512 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
3513 {
3514 cupsFilePuts(fp, "/dev/null\n");
3515
3516 cupsFileClose(fp);
3517
3518 chmod(filename, 0644);
3519 chown(filename, User, Group);
3520 }
3521
3522 /*
3523 * The gui_interface file is a script or program that launches a GUI
3524 * option panel for the printer, using options specified on the
3525 * command-line in the third argument. The option panel must send
3526 * any printing options to stdout on a single line when the user
3527 * accepts them, or nothing if the user cancels the dialog.
3528 *
3529 * The default options panel program is /usr/bin/glpoptions, from
3530 * the ESP Print Pro software. You can select another using the
3531 * PrintcapGUI option.
3532 */
3533
3534 snprintf(filename, sizeof(filename), "/var/spool/lp/gui_interface/ELF/%s.gui", p->name);
3535
3536 if (p->type & CUPS_PRINTER_CLASS)
3537 unlink(filename);
3538 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
3539 {
3540 cupsFilePuts(fp, "#!/bin/sh\n");
3541 cupsFilePrintf(fp, "%s -d %s -o \"$3\"\n", PrintcapGUI, p->name);
3542
3543 cupsFileClose(fp);
3544
3545 chmod(filename, 0755);
3546 chown(filename, User, Group);
3547 }
3548
3549 /*
3550 * The POD config file is needed by the printstatus command to show
3551 * the printer location and device.
3552 */
3553
3554 snprintf(filename, sizeof(filename), "/var/spool/lp/pod/%s.config", p->name);
3555
3556 if (p->type & CUPS_PRINTER_CLASS)
3557 unlink(filename);
3558 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
3559 {
3560 cupsFilePrintf(fp, "Printer Class | %s\n",
3561 (p->type & CUPS_PRINTER_COLOR) ? "ColorPostScript" : "MonoPostScript");
3562 cupsFilePrintf(fp, "Printer Model | %s\n", p->make_model ? p->make_model : "");
3563 cupsFilePrintf(fp, "Location Code | %s\n", p->location ? p->location : "");
3564 cupsFilePrintf(fp, "Physical Location | %s\n", p->info ? p->info : "");
3565 cupsFilePrintf(fp, "Port Path | %s\n", p->device_uri ? p->device_uri : "");
3566 cupsFilePrintf(fp, "Config Path | /var/spool/lp/pod/%s.config\n", p->name);
3567 cupsFilePrintf(fp, "Active Status Path | /var/spool/lp/pod/%s.status\n", p->name);
3568 cupsFilePuts(fp, "Status Update Wait | 10 seconds\n");
3569
3570 cupsFileClose(fp);
3571
3572 chmod(filename, 0664);
3573 chown(filename, User, Group);
3574 }
3575 }
3576
3577
3578 /*
3579 * 'write_irix_state()' - Update the status files used by IRIX printing
3580 * desktop tools.
3581 */
3582
3583 static void
3584 write_irix_state(cupsd_printer_t *p) /* I - Printer to update */
3585 {
3586 char filename[1024]; /* Interface script filename */
3587 cups_file_t *fp; /* Interface script file */
3588 int tag; /* Status tag value */
3589
3590
3591 if (p)
3592 {
3593 /*
3594 * The POD status file is needed for the printstatus window to
3595 * provide the current status of the printer.
3596 */
3597
3598 snprintf(filename, sizeof(filename), "/var/spool/lp/pod/%s.status", p->name);
3599
3600 if (p->type & CUPS_PRINTER_CLASS)
3601 unlink(filename);
3602 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
3603 {
3604 cupsFilePrintf(fp, "Operational Status | %s\n",
3605 (p->state == IPP_PRINTER_IDLE) ? "Idle" :
3606 (p->state == IPP_PRINTER_PROCESSING) ? "Busy" :
3607 "Faulted");
3608 cupsFilePrintf(fp, "Information | 01 00 00 | %s\n", CUPS_SVERSION);
3609 cupsFilePrintf(fp, "Information | 02 00 00 | Device URI: %s\n",
3610 p->device_uri ? p->device_uri : "");
3611 cupsFilePrintf(fp, "Information | 03 00 00 | %s jobs\n",
3612 p->accepting ? "Accepting" : "Not accepting");
3613 cupsFilePrintf(fp, "Information | 04 00 00 | %s\n", p->state_message);
3614
3615 cupsFileClose(fp);
3616
3617 chmod(filename, 0664);
3618 chown(filename, User, Group);
3619 }
3620
3621 /*
3622 * The activeicons file is needed to provide desktop icons for printers:
3623 *
3624 * [ quoted from /usr/lib/print/tagit ]
3625 *
3626 * --- Type of printer tags (base values)
3627 *
3628 * Dumb=66048 # 0x10200
3629 * DumbColor=66080 # 0x10220
3630 * Raster=66112 # 0x10240
3631 * ColorRaster=66144 # 0x10260
3632 * Plotter=66176 # 0x10280
3633 * PostScript=66208 # 0x102A0
3634 * ColorPostScript=66240 # 0x102C0
3635 * MonoPostScript=66272 # 0x102E0
3636 *
3637 * --- Printer state modifiers for local printers
3638 *
3639 * Idle=0 # 0x0
3640 * Busy=1 # 0x1
3641 * Faulted=2 # 0x2
3642 * Unknown=3 # 0x3 (Faulted due to unknown reason)
3643 *
3644 * --- Printer state modifiers for network printers
3645 *
3646 * NetIdle=8 # 0x8
3647 * NetBusy=9 # 0x9
3648 * NetFaulted=10 # 0xA
3649 * NetUnknown=11 # 0xB (Faulted due to unknown reason)
3650 */
3651
3652 snprintf(filename, sizeof(filename), "/var/spool/lp/activeicons/%s", p->name);
3653
3654 if (p->type & CUPS_PRINTER_CLASS)
3655 unlink(filename);
3656 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
3657 {
3658 if (p->type & CUPS_PRINTER_COLOR)
3659 tag = 66240;
3660 else
3661 tag = 66272;
3662
3663 if (p->type & CUPS_PRINTER_REMOTE)
3664 tag |= 8;
3665
3666 if (p->state == IPP_PRINTER_PROCESSING)
3667 tag |= 1;
3668
3669 else if (p->state == IPP_PRINTER_STOPPED)
3670 tag |= 2;
3671
3672 cupsFilePuts(fp, "#!/bin/sh\n");
3673 cupsFilePrintf(fp, "#Tag %d\n", tag);
3674
3675 cupsFileClose(fp);
3676
3677 chmod(filename, 0755);
3678 chown(filename, User, Group);
3679 }
3680 }
3681
3682 /*
3683 * The default file is needed by the printers window to show
3684 * the default printer.
3685 */
3686
3687 snprintf(filename, sizeof(filename), "/var/spool/lp/default");
3688
3689 if (DefaultPrinter != NULL)
3690 {
3691 if ((fp = cupsFileOpen(filename, "w")) != NULL)
3692 {
3693 cupsFilePrintf(fp, "%s\n", DefaultPrinter->name);
3694
3695 cupsFileClose(fp);
3696
3697 chmod(filename, 0644);
3698 chown(filename, User, Group);
3699 }
3700 }
3701 else
3702 unlink(filename);
3703 }
3704 #endif /* __sgi */
3705
3706
3707 /*
3708 * End of "$Id: printers.c 6429 2007-04-02 13:44:52Z mike $".
3709 */