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