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