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