]> 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 5970 2006-09-19 20:11:08Z 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 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED, p, job,
2391 "Job stopped due to printer being paused");
2392 }
2393 }
2394
2395
2396 /*
2397 * 'cupsdUpdatePrinters()' - Update printers after a partial reload.
2398 */
2399
2400 void
2401 cupsdUpdatePrinters(void)
2402 {
2403 cupsd_printer_t *p; /* Current printer */
2404
2405
2406 /*
2407 * Loop through the printers and recreate the printer attributes
2408 * for any local printers since the policy and/or access control
2409 * stuff may have changed. Also, if browsing is disabled, remove
2410 * any remote printers...
2411 */
2412
2413 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
2414 p;
2415 p = (cupsd_printer_t *)cupsArrayNext(Printers))
2416 {
2417 /*
2418 * Remove remote printers if we are no longer browsing...
2419 */
2420
2421 if (!Browsing && (p->type & (CUPS_PRINTER_IMPLICIT | CUPS_PRINTER_REMOTE)))
2422 {
2423 if (p->type & CUPS_PRINTER_IMPLICIT)
2424 cupsArrayRemove(ImplicitPrinters, p);
2425
2426 cupsArraySave(Printers);
2427 cupsdDeletePrinter(p, 0);
2428 cupsArrayRestore(Printers);
2429 continue;
2430 }
2431
2432 /*
2433 * Update the operation policy pointer...
2434 */
2435
2436 if ((p->op_policy_ptr = cupsdFindPolicy(p->op_policy)) == NULL)
2437 p->op_policy_ptr = DefaultPolicyPtr;
2438
2439 /*
2440 * Update printer attributes as needed...
2441 */
2442
2443 if (!(p->type & CUPS_PRINTER_REMOTE))
2444 cupsdSetPrinterAttrs(p);
2445 }
2446 }
2447
2448
2449 /*
2450 * 'cupsdValidateDest()' - Validate a printer/class destination.
2451 */
2452
2453 const char * /* O - Printer or class name */
2454 cupsdValidateDest(
2455 const char *hostname, /* I - Host name */
2456 const char *resource, /* I - Resource name */
2457 cups_ptype_t *dtype, /* O - Type (printer or class) */
2458 cupsd_printer_t **printer) /* O - Printer pointer */
2459 {
2460 cupsd_printer_t *p; /* Current printer */
2461 char localname[1024],/* Localized hostname */
2462 *lptr, /* Pointer into localized hostname */
2463 *sptr; /* Pointer into server name */
2464
2465
2466 DEBUG_printf(("cupsdValidateDest(\"%s\", \"%s\", %p, %p)\n", hostname, resource,
2467 dtype, printer));
2468
2469 /*
2470 * Initialize return values...
2471 */
2472
2473 if (printer)
2474 *printer = NULL;
2475
2476 *dtype = (cups_ptype_t)0;
2477
2478 /*
2479 * See if the resource is a class or printer...
2480 */
2481
2482 if (!strncmp(resource, "/classes/", 9))
2483 {
2484 /*
2485 * Class...
2486 */
2487
2488 resource += 9;
2489 }
2490 else if (!strncmp(resource, "/printers/", 10))
2491 {
2492 /*
2493 * Printer...
2494 */
2495
2496 resource += 10;
2497 }
2498 else
2499 {
2500 /*
2501 * Bad resource name...
2502 */
2503
2504 return (NULL);
2505 }
2506
2507 /*
2508 * See if the printer or class name exists...
2509 */
2510
2511 p = cupsdFindDest(resource);
2512
2513 if (p == NULL && strchr(resource, '@') == NULL)
2514 return (NULL);
2515 else if (p != NULL)
2516 {
2517 if (printer)
2518 *printer = p;
2519
2520 *dtype = p->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT |
2521 CUPS_PRINTER_REMOTE);
2522 return (p->name);
2523 }
2524
2525 /*
2526 * Change localhost to the server name...
2527 */
2528
2529 if (!strcasecmp(hostname, "localhost"))
2530 hostname = ServerName;
2531
2532 strlcpy(localname, hostname, sizeof(localname));
2533
2534 if (!strcasecmp(hostname, ServerName))
2535 {
2536 /*
2537 * Localize the hostname...
2538 */
2539
2540 lptr = strchr(localname, '.');
2541 sptr = strchr(ServerName, '.');
2542
2543 if (sptr != NULL && lptr != NULL)
2544 {
2545 /*
2546 * Strip the common domain name components...
2547 */
2548
2549 while (lptr != NULL)
2550 {
2551 if (!strcasecmp(lptr, sptr))
2552 {
2553 *lptr = '\0';
2554 break;
2555 }
2556 else
2557 lptr = strchr(lptr + 1, '.');
2558 }
2559 }
2560 }
2561
2562 DEBUG_printf(("localized hostname is \"%s\"...\n", localname));
2563
2564 /*
2565 * Find a matching printer or class...
2566 */
2567
2568 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
2569 p;
2570 p = (cupsd_printer_t *)cupsArrayNext(Printers))
2571 if (!strcasecmp(p->hostname, localname) &&
2572 !strcasecmp(p->name, resource))
2573 {
2574 if (printer)
2575 *printer = p;
2576
2577 *dtype = p->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT |
2578 CUPS_PRINTER_REMOTE);
2579 return (p->name);
2580 }
2581
2582 return (NULL);
2583 }
2584
2585
2586 /*
2587 * 'cupsdWritePrintcap()' - Write a pseudo-printcap file for older applications
2588 * that need it...
2589 */
2590
2591 void
2592 cupsdWritePrintcap(void)
2593 {
2594 cups_file_t *fp; /* printcap file */
2595 cupsd_printer_t *p; /* Current printer */
2596
2597
2598 #ifdef __sgi
2599 /*
2600 * Update the IRIX printer state for the default printer; if
2601 * no printers remain, then the default printer file will be
2602 * removed...
2603 */
2604
2605 write_irix_state(DefaultPrinter);
2606 #endif /* __sgi */
2607
2608 /*
2609 * See if we have a printcap file; if not, don't bother writing it.
2610 */
2611
2612 if (!Printcap || !*Printcap)
2613 return;
2614
2615 /*
2616 * Open the printcap file...
2617 */
2618
2619 if ((fp = cupsFileOpen(Printcap, "w")) == NULL)
2620 return;
2621
2622 /*
2623 * Put a comment header at the top so that users will know where the
2624 * data has come from...
2625 */
2626
2627 cupsFilePuts(fp, "# This file was automatically generated by cupsd(8) from the\n");
2628 cupsFilePrintf(fp, "# %s/printers.conf file. All changes to this file\n",
2629 ServerRoot);
2630 cupsFilePuts(fp, "# will be lost.\n");
2631
2632 if (Printers)
2633 {
2634 /*
2635 * Write a new printcap with the current list of printers.
2636 */
2637
2638 switch (PrintcapFormat)
2639 {
2640 case PRINTCAP_BSD:
2641 /*
2642 * Each printer is put in the file as:
2643 *
2644 * Printer1:
2645 * Printer2:
2646 * Printer3:
2647 * ...
2648 * PrinterN:
2649 */
2650
2651 if (DefaultPrinter)
2652 cupsFilePrintf(fp, "%s|%s:rm=%s:rp=%s:\n", DefaultPrinter->name,
2653 DefaultPrinter->info, ServerName, DefaultPrinter->name);
2654
2655 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
2656 p;
2657 p = (cupsd_printer_t *)cupsArrayNext(Printers))
2658 if (p != DefaultPrinter)
2659 cupsFilePrintf(fp, "%s|%s:rm=%s:rp=%s:\n", p->name, p->info,
2660 ServerName, p->name);
2661 break;
2662
2663 case PRINTCAP_SOLARIS:
2664 /*
2665 * Each printer is put in the file as:
2666 *
2667 * _all:all=Printer1,Printer2,Printer3,...,PrinterN
2668 * _default:use=DefaultPrinter
2669 * Printer1:\
2670 * :bsdaddr=ServerName,Printer1:\
2671 * :description=Description:
2672 * Printer2:
2673 * :bsdaddr=ServerName,Printer2:\
2674 * :description=Description:
2675 * Printer3:
2676 * :bsdaddr=ServerName,Printer3:\
2677 * :description=Description:
2678 * ...
2679 * PrinterN:
2680 * :bsdaddr=ServerName,PrinterN:\
2681 * :description=Description:
2682 */
2683
2684 cupsFilePuts(fp, "_all:all=");
2685 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
2686 p;
2687 p = (cupsd_printer_t *)cupsArrayCurrent(Printers))
2688 cupsFilePrintf(fp, "%s%c", p->name,
2689 cupsArrayNext(Printers) ? ',' : '\n');
2690
2691 if (DefaultPrinter)
2692 cupsFilePrintf(fp, "_default:use=%s\n", DefaultPrinter->name);
2693
2694 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
2695 p;
2696 p = (cupsd_printer_t *)cupsArrayNext(Printers))
2697 cupsFilePrintf(fp, "%s:\\\n"
2698 "\t:bsdaddr=%s,%s:\\\n"
2699 "\t:description=%s:\n",
2700 p->name, ServerName, p->name, p->info ? p->info : "");
2701 break;
2702 }
2703 }
2704
2705 /*
2706 * Close the file...
2707 */
2708
2709 cupsFileClose(fp);
2710 }
2711
2712
2713 /*
2714 * 'cupsdSanitizeURI()' - Sanitize a device URI...
2715 */
2716
2717 char * /* O - New device URI */
2718 cupsdSanitizeURI(const char *uri, /* I - Original device URI */
2719 char *buffer, /* O - New device URI */
2720 int buflen) /* I - Size of new device URI buffer */
2721 {
2722 char *start, /* Start of data after scheme */
2723 *slash, /* First slash after scheme:// */
2724 *ptr; /* Pointer into user@host:port part */
2725
2726
2727 /*
2728 * Range check input...
2729 */
2730
2731 if (!uri || !buffer || buflen < 2)
2732 return (NULL);
2733
2734 /*
2735 * Copy the device URI to the new buffer...
2736 */
2737
2738 strlcpy(buffer, uri, buflen);
2739
2740 /*
2741 * Find the end of the scheme:// part...
2742 */
2743
2744 if ((ptr = strchr(buffer, ':')) == NULL)
2745 return (buffer); /* No scheme: part... */
2746
2747 for (start = ptr + 1; *start; start ++)
2748 if (*start != '/')
2749 break;
2750
2751 /*
2752 * Find the next slash (/) in the URI...
2753 */
2754
2755 if ((slash = strchr(start, '/')) == NULL)
2756 slash = start + strlen(start); /* No slash, point to the end */
2757
2758 /*
2759 * Check for an @ sign before the slash...
2760 */
2761
2762 if ((ptr = strchr(start, '@')) != NULL && ptr < slash)
2763 {
2764 /*
2765 * Found an @ sign and it is before the resource part, so we have
2766 * an authentication string. Copy the remaining URI over the
2767 * authentication string...
2768 */
2769
2770 _cups_strcpy(start, ptr + 1);
2771 }
2772
2773 /*
2774 * Return the new device URI...
2775 */
2776
2777 return (buffer);
2778 }
2779
2780
2781 /*
2782 * 'add_printer_defaults()' - Add name-default attributes to the printer attributes.
2783 */
2784
2785 static void
2786 add_printer_defaults(cupsd_printer_t *p)/* I - Printer */
2787 {
2788 int i; /* Looping var */
2789 int num_options; /* Number of default options */
2790 cups_option_t *options, /* Default options */
2791 *option; /* Current option */
2792 char name[256]; /* name-default */
2793
2794
2795 /*
2796 * Add all of the default options from the .conf files...
2797 */
2798
2799 for (num_options = 0, i = p->num_options, option = p->options;
2800 i > 0;
2801 i --, option ++)
2802 {
2803 if (strcmp(option->name, "ipp-options") &&
2804 strcmp(option->name, "job-sheets") &&
2805 strcmp(option->name, "lease-duration"))
2806 {
2807 snprintf(name, sizeof(name), "%s-default", option->name);
2808 num_options = cupsAddOption(name, option->value, num_options, &options);
2809 }
2810 }
2811
2812 /*
2813 * Convert options to IPP attributes...
2814 */
2815
2816 cupsEncodeOptions2(p->attrs, num_options, options, IPP_TAG_PRINTER);
2817 cupsFreeOptions(num_options, options);
2818
2819 /*
2820 * Add standard -default attributes as needed...
2821 */
2822
2823 if (!cupsGetOption("copies", p->num_options, p->options))
2824 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER, "copies-default",
2825 1);
2826
2827 if (!cupsGetOption("job-hold-until", p->num_options, p->options))
2828 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2829 "job-hold-until-default", NULL, "no-hold");
2830
2831 if (!cupsGetOption("job-priority", p->num_options, p->options))
2832 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
2833 "job-priority-default", 50);
2834
2835 if (!cupsGetOption("number-up", p->num_options, p->options))
2836 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
2837 "number-up-default", 1);
2838
2839 if (!cupsGetOption("orientation-requested", p->num_options, p->options))
2840 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_ENUM,
2841 "orientation-requested-default", IPP_PORTRAIT);
2842 }
2843
2844
2845 /*
2846 * 'add_printer_filter()' - Add a MIME filter for a printer.
2847 */
2848
2849 static void
2850 add_printer_filter(
2851 cupsd_printer_t *p, /* I - Printer to add to */
2852 const char *filter) /* I - Filter to add */
2853 {
2854 char super[MIME_MAX_SUPER], /* Super-type for filter */
2855 type[MIME_MAX_TYPE], /* Type for filter */
2856 program[1024]; /* Program/filter name */
2857 int cost; /* Cost of filter */
2858 mime_type_t *temptype; /* MIME type looping var */
2859 char filename[1024]; /* Full filter filename */
2860
2861
2862 /*
2863 * Parse the filter string; it should be in the following format:
2864 *
2865 * super/type cost program
2866 */
2867
2868 if (sscanf(filter, "%15[^/]/%31s%d%1023s", super, type, &cost, program) != 4)
2869 {
2870 cupsdLogMessage(CUPSD_LOG_ERROR, "%s: invalid filter string \"%s\"!",
2871 p->name, filter);
2872 return;
2873 }
2874
2875 /*
2876 * See if the filter program exists; if not, stop the printer and flag
2877 * the error!
2878 */
2879
2880 if (strcmp(program, "-"))
2881 {
2882 if (program[0] == '/')
2883 strlcpy(filename, program, sizeof(filename));
2884 else
2885 snprintf(filename, sizeof(filename), "%s/filter/%s", ServerBin, program);
2886
2887 if (access(filename, X_OK))
2888 {
2889 snprintf(p->state_message, sizeof(p->state_message),
2890 "Filter \"%s\" for printer \"%s\" not available: %s",
2891 program, p->name, strerror(errno));
2892 cupsdSetPrinterReasons(p, "+cups-missing-filter-error");
2893 cupsdSetPrinterState(p, IPP_PRINTER_STOPPED, 0);
2894
2895 cupsdLogMessage(CUPSD_LOG_ERROR, "%s", p->state_message);
2896 }
2897 }
2898
2899 /*
2900 * Mark the CUPS_PRINTER_COMMANDS bit if we have a filter for
2901 * application/vnd.cups-command...
2902 */
2903
2904 if (!strcasecmp(super, "application") &&
2905 !strcasecmp(type, "vnd.cups-command"))
2906 p->type |= CUPS_PRINTER_COMMANDS;
2907
2908 /*
2909 * Add the filter to the MIME database, supporting wildcards as needed...
2910 */
2911
2912 for (temptype = mimeFirstType(MimeDatabase);
2913 temptype;
2914 temptype = mimeNextType(MimeDatabase))
2915 if (((super[0] == '*' && strcasecmp(temptype->super, "printer")) ||
2916 !strcasecmp(temptype->super, super)) &&
2917 (type[0] == '*' || !strcasecmp(temptype->type, type)))
2918 {
2919 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2920 "add_printer_filter: %s: adding filter %s/%s %s/%s %d %s",
2921 p->name, temptype->super, temptype->type,
2922 p->filetype->super, p->filetype->type,
2923 cost, program);
2924 mimeAddFilter(MimeDatabase, temptype, p->filetype, cost, program);
2925 }
2926 }
2927
2928
2929 /*
2930 * 'add_printer_formats()' - Add document-format-supported values for a printer.
2931 */
2932
2933 static void
2934 add_printer_formats(cupsd_printer_t *p) /* I - Printer */
2935 {
2936 int i; /* Looping var */
2937 mime_type_t *type; /* Current MIME type */
2938 cups_array_t *filters; /* Filters */
2939 ipp_attribute_t *attr; /* document-format-supported attribute */
2940 char mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2];
2941 /* MIME type name */
2942
2943
2944 /*
2945 * Raw (and remote) queues advertise all of the supported MIME
2946 * types...
2947 */
2948
2949 cupsArrayDelete(p->filetypes);
2950 p->filetypes = NULL;
2951
2952 if (p->raw)
2953 {
2954 ippAddStrings(p->attrs, IPP_TAG_PRINTER,
2955 (ipp_tag_t)(IPP_TAG_MIMETYPE | IPP_TAG_COPY),
2956 "document-format-supported", NumMimeTypes, NULL, MimeTypes);
2957 return;
2958 }
2959
2960 /*
2961 * Otherwise, loop through the supported MIME types and see if there
2962 * are filters for them...
2963 */
2964
2965 cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_printer_formats: %d types, %d filters",
2966 mimeNumTypes(MimeDatabase), mimeNumFilters(MimeDatabase));
2967
2968 p->filetypes = cupsArrayNew(NULL, NULL);
2969
2970 for (type = mimeFirstType(MimeDatabase);
2971 type;
2972 type = mimeNextType(MimeDatabase))
2973 {
2974 snprintf(mimetype, sizeof(mimetype), "%s/%s", type->super, type->type);
2975
2976 if ((filters = mimeFilter(MimeDatabase, type, p->filetype, NULL)) != NULL)
2977 {
2978 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2979 "add_printer_formats: %s: %s needs %d filters",
2980 p->name, mimetype, cupsArrayCount(filters));
2981
2982 cupsArrayDelete(filters);
2983 cupsArrayAdd(p->filetypes, type);
2984 }
2985 else
2986 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2987 "add_printer_formats: %s: %s not supported",
2988 p->name, mimetype);
2989 }
2990
2991 cupsdLogMessage(CUPSD_LOG_DEBUG2,
2992 "add_printer_formats: %s: %d supported types",
2993 p->name, cupsArrayCount(p->filetypes) + 1);
2994
2995 /*
2996 * Add the file formats that can be filtered...
2997 */
2998
2999 if ((type = mimeType(MimeDatabase, "application", "octet-stream")) == NULL ||
3000 !cupsArrayFind(p->filetypes, type))
3001 i = 1;
3002 else
3003 i = 0;
3004
3005 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_MIMETYPE,
3006 "document-format-supported",
3007 cupsArrayCount(p->filetypes) + 1, NULL, NULL);
3008
3009 if (i)
3010 attr->values[0].string.text = _cupsStrAlloc("application/octet-stream");
3011
3012 for (type = (mime_type_t *)cupsArrayFirst(p->filetypes);
3013 type;
3014 i ++, type = (mime_type_t *)cupsArrayNext(p->filetypes))
3015 {
3016 snprintf(mimetype, sizeof(mimetype), "%s/%s", type->super, type->type);
3017
3018 attr->values[i].string.text = _cupsStrAlloc(mimetype);
3019 }
3020 }
3021
3022
3023 /*
3024 * 'compare_printers()' - Compare two printers.
3025 */
3026
3027 static int /* O - Result of comparison */
3028 compare_printers(void *first, /* I - First printer */
3029 void *second, /* I - Second printer */
3030 void *data) /* I - App data (not used) */
3031 {
3032 return (strcasecmp(((cupsd_printer_t *)first)->name,
3033 ((cupsd_printer_t *)second)->name));
3034 }
3035
3036
3037 /*
3038 * 'delete_printer_filters()' - Delete all MIME filters for a printer.
3039 */
3040
3041 static void
3042 delete_printer_filters(
3043 cupsd_printer_t *p) /* I - Printer to remove from */
3044 {
3045 mime_filter_t *filter; /* MIME filter looping var */
3046
3047
3048 /*
3049 * Range check input...
3050 */
3051
3052 if (p == NULL)
3053 return;
3054
3055 /*
3056 * Remove all filters from the MIME database that have a destination
3057 * type == printer...
3058 */
3059
3060 for (filter = mimeFirstFilter(MimeDatabase);
3061 filter;
3062 filter = mimeNextFilter(MimeDatabase))
3063 if (filter->dst == p->filetype)
3064 {
3065 /*
3066 * Delete the current filter...
3067 */
3068
3069 mimeDeleteFilter(MimeDatabase, filter);
3070 }
3071 }
3072
3073
3074 #ifdef __sgi
3075 /*
3076 * 'write_irix_config()' - Update the config files used by the IRIX
3077 * desktop tools.
3078 */
3079
3080 static void
3081 write_irix_config(cupsd_printer_t *p) /* I - Printer to update */
3082 {
3083 char filename[1024]; /* Interface script filename */
3084 cups_file_t *fp; /* Interface script file */
3085 ipp_attribute_t *attr; /* Attribute data */
3086
3087
3088 /*
3089 * Add dummy interface and GUI scripts to fool SGI's "challenged" printing
3090 * tools. First the interface script that tells the tools what kind of
3091 * printer we have...
3092 */
3093
3094 snprintf(filename, sizeof(filename), "/var/spool/lp/interface/%s", p->name);
3095
3096 if (p->type & CUPS_PRINTER_CLASS)
3097 unlink(filename);
3098 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
3099 {
3100 cupsFilePuts(fp, "#!/bin/sh\n");
3101
3102 if ((attr = ippFindAttribute(p->attrs, "printer-make-and-model",
3103 IPP_TAG_TEXT)) != NULL)
3104 cupsFilePrintf(fp, "NAME=\"%s\"\n", attr->values[0].string.text);
3105 else if (p->type & CUPS_PRINTER_CLASS)
3106 cupsFilePuts(fp, "NAME=\"Printer Class\"\n");
3107 else
3108 cupsFilePuts(fp, "NAME=\"Remote Destination\"\n");
3109
3110 if (p->type & CUPS_PRINTER_COLOR)
3111 cupsFilePuts(fp, "TYPE=ColorPostScript\n");
3112 else
3113 cupsFilePuts(fp, "TYPE=MonoPostScript\n");
3114
3115 cupsFilePrintf(fp, "HOSTNAME=%s\n", ServerName);
3116 cupsFilePrintf(fp, "HOSTPRINTER=%s\n", p->name);
3117
3118 cupsFileClose(fp);
3119
3120 chmod(filename, 0755);
3121 chown(filename, User, Group);
3122 }
3123
3124 /*
3125 * Then the member file that tells which device file the queue is connected
3126 * to... Networked printers use "/dev/null" in this file, so that's what
3127 * we use (the actual device URI can confuse some apps...)
3128 */
3129
3130 snprintf(filename, sizeof(filename), "/var/spool/lp/member/%s", p->name);
3131
3132 if (p->type & CUPS_PRINTER_CLASS)
3133 unlink(filename);
3134 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
3135 {
3136 cupsFilePuts(fp, "/dev/null\n");
3137
3138 cupsFileClose(fp);
3139
3140 chmod(filename, 0644);
3141 chown(filename, User, Group);
3142 }
3143
3144 /*
3145 * The gui_interface file is a script or program that launches a GUI
3146 * option panel for the printer, using options specified on the
3147 * command-line in the third argument. The option panel must send
3148 * any printing options to stdout on a single line when the user
3149 * accepts them, or nothing if the user cancels the dialog.
3150 *
3151 * The default options panel program is /usr/bin/glpoptions, from
3152 * the ESP Print Pro software. You can select another using the
3153 * PrintcapGUI option.
3154 */
3155
3156 snprintf(filename, sizeof(filename), "/var/spool/lp/gui_interface/ELF/%s.gui", p->name);
3157
3158 if (p->type & CUPS_PRINTER_CLASS)
3159 unlink(filename);
3160 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
3161 {
3162 cupsFilePuts(fp, "#!/bin/sh\n");
3163 cupsFilePrintf(fp, "%s -d %s -o \"$3\"\n", PrintcapGUI, p->name);
3164
3165 cupsFileClose(fp);
3166
3167 chmod(filename, 0755);
3168 chown(filename, User, Group);
3169 }
3170
3171 /*
3172 * The POD config file is needed by the printstatus command to show
3173 * the printer location and device.
3174 */
3175
3176 snprintf(filename, sizeof(filename), "/var/spool/lp/pod/%s.config", p->name);
3177
3178 if (p->type & CUPS_PRINTER_CLASS)
3179 unlink(filename);
3180 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
3181 {
3182 cupsFilePrintf(fp, "Printer Class | %s\n",
3183 (p->type & CUPS_PRINTER_COLOR) ? "ColorPostScript" : "MonoPostScript");
3184 cupsFilePrintf(fp, "Printer Model | %s\n", p->make_model ? p->make_model : "");
3185 cupsFilePrintf(fp, "Location Code | %s\n", p->location ? p->location : "");
3186 cupsFilePrintf(fp, "Physical Location | %s\n", p->info ? p->info : "");
3187 cupsFilePrintf(fp, "Port Path | %s\n", p->device_uri ? p->device_uri : "");
3188 cupsFilePrintf(fp, "Config Path | /var/spool/lp/pod/%s.config\n", p->name);
3189 cupsFilePrintf(fp, "Active Status Path | /var/spool/lp/pod/%s.status\n", p->name);
3190 cupsFilePuts(fp, "Status Update Wait | 10 seconds\n");
3191
3192 cupsFileClose(fp);
3193
3194 chmod(filename, 0664);
3195 chown(filename, User, Group);
3196 }
3197 }
3198
3199
3200 /*
3201 * 'write_irix_state()' - Update the status files used by IRIX printing
3202 * desktop tools.
3203 */
3204
3205 static void
3206 write_irix_state(cupsd_printer_t *p) /* I - Printer to update */
3207 {
3208 char filename[1024]; /* Interface script filename */
3209 cups_file_t *fp; /* Interface script file */
3210 int tag; /* Status tag value */
3211
3212
3213 if (p)
3214 {
3215 /*
3216 * The POD status file is needed for the printstatus window to
3217 * provide the current status of the printer.
3218 */
3219
3220 snprintf(filename, sizeof(filename), "/var/spool/lp/pod/%s.status", p->name);
3221
3222 if (p->type & CUPS_PRINTER_CLASS)
3223 unlink(filename);
3224 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
3225 {
3226 cupsFilePrintf(fp, "Operational Status | %s\n",
3227 (p->state == IPP_PRINTER_IDLE) ? "Idle" :
3228 (p->state == IPP_PRINTER_PROCESSING) ? "Busy" :
3229 "Faulted");
3230 cupsFilePrintf(fp, "Information | 01 00 00 | %s\n", CUPS_SVERSION);
3231 cupsFilePrintf(fp, "Information | 02 00 00 | Device URI: %s\n",
3232 p->device_uri ? p->device_uri : "");
3233 cupsFilePrintf(fp, "Information | 03 00 00 | %s jobs\n",
3234 p->accepting ? "Accepting" : "Not accepting");
3235 cupsFilePrintf(fp, "Information | 04 00 00 | %s\n", p->state_message);
3236
3237 cupsFileClose(fp);
3238
3239 chmod(filename, 0664);
3240 chown(filename, User, Group);
3241 }
3242
3243 /*
3244 * The activeicons file is needed to provide desktop icons for printers:
3245 *
3246 * [ quoted from /usr/lib/print/tagit ]
3247 *
3248 * --- Type of printer tags (base values)
3249 *
3250 * Dumb=66048 # 0x10200
3251 * DumbColor=66080 # 0x10220
3252 * Raster=66112 # 0x10240
3253 * ColorRaster=66144 # 0x10260
3254 * Plotter=66176 # 0x10280
3255 * PostScript=66208 # 0x102A0
3256 * ColorPostScript=66240 # 0x102C0
3257 * MonoPostScript=66272 # 0x102E0
3258 *
3259 * --- Printer state modifiers for local printers
3260 *
3261 * Idle=0 # 0x0
3262 * Busy=1 # 0x1
3263 * Faulted=2 # 0x2
3264 * Unknown=3 # 0x3 (Faulted due to unknown reason)
3265 *
3266 * --- Printer state modifiers for network printers
3267 *
3268 * NetIdle=8 # 0x8
3269 * NetBusy=9 # 0x9
3270 * NetFaulted=10 # 0xA
3271 * NetUnknown=11 # 0xB (Faulted due to unknown reason)
3272 */
3273
3274 snprintf(filename, sizeof(filename), "/var/spool/lp/activeicons/%s", p->name);
3275
3276 if (p->type & CUPS_PRINTER_CLASS)
3277 unlink(filename);
3278 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
3279 {
3280 if (p->type & CUPS_PRINTER_COLOR)
3281 tag = 66240;
3282 else
3283 tag = 66272;
3284
3285 if (p->type & CUPS_PRINTER_REMOTE)
3286 tag |= 8;
3287
3288 if (p->state == IPP_PRINTER_PROCESSING)
3289 tag |= 1;
3290
3291 else if (p->state == IPP_PRINTER_STOPPED)
3292 tag |= 2;
3293
3294 cupsFilePuts(fp, "#!/bin/sh\n");
3295 cupsFilePrintf(fp, "#Tag %d\n", tag);
3296
3297 cupsFileClose(fp);
3298
3299 chmod(filename, 0755);
3300 chown(filename, User, Group);
3301 }
3302 }
3303
3304 /*
3305 * The default file is needed by the printers window to show
3306 * the default printer.
3307 */
3308
3309 snprintf(filename, sizeof(filename), "/var/spool/lp/default");
3310
3311 if (DefaultPrinter != NULL)
3312 {
3313 if ((fp = cupsFileOpen(filename, "w")) != NULL)
3314 {
3315 cupsFilePrintf(fp, "%s\n", DefaultPrinter->name);
3316
3317 cupsFileClose(fp);
3318
3319 chmod(filename, 0644);
3320 chown(filename, User, Group);
3321 }
3322 }
3323 else
3324 unlink(filename);
3325 }
3326 #endif /* __sgi */
3327
3328
3329 /*
3330 * End of "$Id: printers.c 5970 2006-09-19 20:11:08Z mike $".
3331 */