]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/printers.c
Merge changes from CUPS 1.4svn-r7961.
[thirdparty/cups.git] / scheduler / printers.c
1 /*
2 * "$Id: printers.c 7879 2008-08-28 00:08:56Z mike $"
3 *
4 * Printer routines for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 2007-2008 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
8 *
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
14 *
15 * Contents:
16 *
17 * cupsdAddPrinter() - Add a printer to the system.
18 * cupsdAddPrinterHistory() - Add the current printer state to the history.
19 * cupsdAddPrinterUser() - Add a user to the ACL.
20 * cupsdCreateCommonData() - Create the common printer data.
21 * cupsdDeleteAllPrinters() - Delete all printers from the system.
22 * cupsdDeletePrinter() - Delete a printer from the system.
23 * cupsdFindPrinter() - Find a printer in the list.
24 * cupsdFreePrinterUsers() - Free allow/deny users.
25 * cupsdLoadAllPrinters() - Load printers from the printers.conf file.
26 * cupsdRenamePrinter() - Rename a printer.
27 * cupsdSaveAllPrinters() - Save all printer definitions to the
28 * printers.conf file.
29 * cupsdSetAuthInfoRequired() - Set the required authentication info.
30 * cupsdSetPrinterAttr() - Set a printer attribute.
31 * cupsdSetPrinterAttrs() - Set printer attributes based upon the PPD
32 * file.
33 * cupsdSetPrinterReasons() - Set/update the reasons strings.
34 * cupsdSetPrinterState() - Update the current state of a printer.
35 * cupsdStopPrinter() - Stop a printer from printing any jobs...
36 * cupsdUpdatePrinters() - Update printers after a partial reload.
37 * cupsdValidateDest() - Validate a printer/class destination.
38 * cupsdWritePrintcap() - Write a pseudo-printcap file for older
39 * applications that need it...
40 * cupsdSanitizeURI() - Sanitize a device URI...
41 * add_printer_defaults() - Add name-default attributes to the printer
42 * attributes.
43 * add_printer_filter() - Add a MIME filter for a printer.
44 * add_printer_formats() - Add document-format-supported values for
45 * a printer.
46 * compare_printers() - Compare two printers.
47 * delete_printer_filters() - Delete all MIME filters for a printer.
48 * write_irix_config() - Update the config files used by the IRIX
49 * desktop tools.
50 * write_irix_state() - Update the status files used by IRIX
51 * printing desktop tools.
52 */
53
54 /*
55 * Include necessary headers...
56 */
57
58 #include "cupsd.h"
59 #include <cups/dir.h>
60
61
62 /*
63 * Local functions...
64 */
65
66 static void add_printer_defaults(cupsd_printer_t *p);
67 static void add_printer_filter(cupsd_printer_t *p, mime_type_t *type,
68 const char *filter);
69 static void add_printer_formats(cupsd_printer_t *p);
70 static int compare_printers(void *first, void *second, void *data);
71 static void delete_printer_filters(cupsd_printer_t *p);
72 #ifdef __sgi
73 static void write_irix_config(cupsd_printer_t *p);
74 static void write_irix_state(cupsd_printer_t *p);
75 #endif /* __sgi */
76
77
78 /*
79 * 'cupsdAddPrinter()' - Add a printer to the system.
80 */
81
82 cupsd_printer_t * /* O - New printer */
83 cupsdAddPrinter(const char *name) /* I - Name of printer */
84 {
85 cupsd_printer_t *p; /* New printer */
86
87
88 /*
89 * Range check input...
90 */
91
92 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdAddPrinter(\"%s\")", name);
93
94 /*
95 * Create a new printer entity...
96 */
97
98 if ((p = calloc(1, sizeof(cupsd_printer_t))) == NULL)
99 {
100 cupsdLogMessage(CUPSD_LOG_CRIT, "Unable to allocate memory for printer - %s",
101 strerror(errno));
102 return (NULL);
103 }
104
105 cupsdSetString(&p->name, name);
106 cupsdSetString(&p->info, name);
107 cupsdSetString(&p->hostname, ServerName);
108
109 cupsdSetStringf(&p->uri, "ipp://%s:%d/printers/%s", ServerName, LocalPort, name);
110 cupsdSetStringf(&p->device_uri, "file:/dev/null");
111
112 p->state = IPP_PRINTER_STOPPED;
113 p->state_time = time(NULL);
114 p->accepting = 0;
115 p->shared = DefaultShared;
116 p->filetype = mimeAddType(MimeDatabase, "printer", name);
117
118 cupsdSetString(&p->job_sheets[0], "none");
119 cupsdSetString(&p->job_sheets[1], "none");
120
121 cupsdSetString(&p->error_policy, ErrorPolicy);
122 cupsdSetString(&p->op_policy, DefaultPolicy);
123
124 p->op_policy_ptr = DefaultPolicyPtr;
125
126 if (MaxPrinterHistory)
127 p->history = calloc(MaxPrinterHistory, sizeof(ipp_t *));
128
129 /*
130 * Insert the printer in the printer list alphabetically...
131 */
132
133 if (!Printers)
134 Printers = cupsArrayNew(compare_printers, NULL);
135
136 cupsdLogMessage(CUPSD_LOG_DEBUG2,
137 "cupsdAddPrinter: Adding %s to Printers", p->name);
138 cupsArrayAdd(Printers, p);
139
140 if (!ImplicitPrinters)
141 ImplicitPrinters = cupsArrayNew(compare_printers, NULL);
142
143 /*
144 * Return the new printer...
145 */
146
147 return (p);
148 }
149
150
151 /*
152 * 'cupsdAddPrinterHistory()' - Add the current printer state to the history.
153 */
154
155 void
156 cupsdAddPrinterHistory(
157 cupsd_printer_t *p) /* I - Printer */
158 {
159 ipp_t *history; /* History collection */
160
161
162 /*
163 * Stop early if we aren't keeping history data...
164 */
165
166 if (MaxPrinterHistory <= 0)
167 return;
168
169 /*
170 * Retire old history data as needed...
171 */
172
173 p->sequence_number ++;
174
175 if (p->num_history >= MaxPrinterHistory)
176 {
177 p->num_history --;
178 ippDelete(p->history[0]);
179 memmove(p->history, p->history + 1, p->num_history * sizeof(ipp_t *));
180 }
181
182 /*
183 * Create a collection containing the current printer-state, printer-up-time,
184 * printer-state-message, and printer-state-reasons attributes.
185 */
186
187 history = ippNew();
188 ippAddInteger(history, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-state",
189 p->state);
190 ippAddBoolean(history, IPP_TAG_PRINTER, "printer-is-accepting-jobs",
191 p->accepting);
192 ippAddBoolean(history, IPP_TAG_PRINTER, "printer-is-shared", p->shared);
193 ippAddString(history, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-state-message",
194 NULL, p->state_message);
195 #ifdef __APPLE__
196 if (p->recoverable)
197 ippAddString(history, IPP_TAG_PRINTER, IPP_TAG_TEXT,
198 "com.apple.print.recoverable-message", NULL, p->recoverable);
199 #endif /* __APPLE__ */
200 if (p->num_reasons == 0)
201 ippAddString(history, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
202 "printer-state-reasons", NULL,
203 p->state == IPP_PRINTER_STOPPED ? "paused" : "none");
204 else
205 ippAddStrings(history, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
206 "printer-state-reasons", p->num_reasons, NULL,
207 (const char * const *)p->reasons);
208 ippAddInteger(history, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
209 "printer-state-change-time", p->state_time);
210 ippAddInteger(history, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
211 "printer-state-sequence-number", p->sequence_number);
212
213 p->history[p->num_history] = history;
214 p->num_history ++;
215 }
216
217
218 /*
219 * 'cupsdAddPrinterUser()' - Add a user to the ACL.
220 */
221
222 void
223 cupsdAddPrinterUser(
224 cupsd_printer_t *p, /* I - Printer */
225 const char *username) /* I - User */
226 {
227 const char **temp; /* Temporary array pointer */
228
229
230 if (!p || !username)
231 return;
232
233 if (p->num_users == 0)
234 temp = malloc(sizeof(char **));
235 else
236 temp = realloc(p->users, sizeof(char **) * (p->num_users + 1));
237
238 if (!temp)
239 return;
240
241 p->users = temp;
242 temp += p->num_users;
243
244 if ((*temp = strdup(username)) != NULL)
245 p->num_users ++;
246 }
247
248
249 /*
250 * 'cupsdCreateCommonData()' - Create the common printer data.
251 */
252
253 void
254 cupsdCreateCommonData(void)
255 {
256 int i; /* Looping var */
257 ipp_attribute_t *attr; /* Attribute data */
258 cups_dir_t *dir; /* Notifier directory */
259 cups_dentry_t *dent; /* Notifier directory entry */
260 cups_array_t *notifiers; /* Notifier array */
261 char filename[1024], /* Filename */
262 *notifier; /* Current notifier */
263 cupsd_policy_t *p; /* Current policy */
264 static const int nups[] = /* number-up-supported values */
265 { 1, 2, 4, 6, 9, 16 };
266 static const int orients[4] =/* orientation-requested-supported values */
267 {
268 IPP_PORTRAIT,
269 IPP_LANDSCAPE,
270 IPP_REVERSE_LANDSCAPE,
271 IPP_REVERSE_PORTRAIT
272 };
273 static const char * const holds[] = /* job-hold-until-supported values */
274 {
275 "no-hold",
276 "indefinite",
277 "day-time",
278 "evening",
279 "night",
280 "second-shift",
281 "third-shift",
282 "weekend"
283 };
284 static const char * const versions[] =/* ipp-versions-supported values */
285 {
286 "1.0",
287 "1.1"
288 };
289 static const int ops[] = /* operations-supported values */
290 {
291 IPP_PRINT_JOB,
292 IPP_VALIDATE_JOB,
293 IPP_CREATE_JOB,
294 IPP_SEND_DOCUMENT,
295 IPP_CANCEL_JOB,
296 IPP_GET_JOB_ATTRIBUTES,
297 IPP_GET_JOBS,
298 IPP_GET_PRINTER_ATTRIBUTES,
299 IPP_HOLD_JOB,
300 IPP_RELEASE_JOB,
301 IPP_PAUSE_PRINTER,
302 IPP_RESUME_PRINTER,
303 IPP_PURGE_JOBS,
304 IPP_SET_JOB_ATTRIBUTES,
305 IPP_CREATE_PRINTER_SUBSCRIPTION,
306 IPP_CREATE_JOB_SUBSCRIPTION,
307 IPP_GET_SUBSCRIPTION_ATTRIBUTES,
308 IPP_GET_SUBSCRIPTIONS,
309 IPP_RENEW_SUBSCRIPTION,
310 IPP_CANCEL_SUBSCRIPTION,
311 IPP_GET_NOTIFICATIONS,
312 IPP_ENABLE_PRINTER,
313 IPP_DISABLE_PRINTER,
314 CUPS_GET_DEFAULT,
315 CUPS_GET_PRINTERS,
316 CUPS_ADD_PRINTER,
317 CUPS_DELETE_PRINTER,
318 CUPS_GET_CLASSES,
319 CUPS_ADD_CLASS,
320 CUPS_DELETE_CLASS,
321 CUPS_ACCEPT_JOBS,
322 CUPS_REJECT_JOBS,
323 CUPS_SET_DEFAULT,
324 CUPS_GET_DEVICES,
325 CUPS_GET_PPDS,
326 CUPS_MOVE_JOB,
327 CUPS_AUTHENTICATE_JOB,
328 CUPS_GET_PPD,
329 CUPS_GET_DOCUMENT,
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-current-job",
353 "retry-job",
354 "stop-printer"
355 };
356 static const char * const notify_attrs[] =
357 { /* notify-attributes-supported values */
358 "printer-state-change-time",
359 "notify-lease-expiration-time",
360 "notify-subscriber-user-name"
361 };
362 static const char * const notify_events[] =
363 { /* notify-events-supported values */
364 "job-completed",
365 "job-config-changed",
366 "job-created",
367 "job-progress",
368 "job-state-changed",
369 "job-stopped",
370 "printer-added",
371 "printer-changed",
372 "printer-config-changed",
373 "printer-deleted",
374 "printer-finishings-changed",
375 "printer-media-changed",
376 "printer-modified",
377 "printer-restarted",
378 "printer-shutdown",
379 "printer-state-changed",
380 "printer-stopped",
381 "server-audit",
382 "server-restarted",
383 "server-started",
384 "server-stopped"
385 };
386
387
388 if (CommonData)
389 ippDelete(CommonData);
390
391 CommonData = ippNew();
392
393 /*
394 * This list of attributes is sorted to improve performance when the
395 * client provides a requested-attributes attribute...
396 */
397
398 /* charset-configured */
399 ippAddString(CommonData, IPP_TAG_PRINTER, IPP_TAG_CHARSET,
400 "charset-configured", NULL, DefaultCharset);
401
402 /* charset-supported */
403 ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_CHARSET,
404 "charset-supported", sizeof(charsets) / sizeof(charsets[0]),
405 NULL, charsets);
406
407 /* compression-supported */
408 ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
409 "compression-supported",
410 sizeof(compressions) / sizeof(compressions[0]),
411 NULL, compressions);
412
413 /* copies-supported */
414 ippAddRange(CommonData, IPP_TAG_PRINTER, "copies-supported", 1, MaxCopies);
415
416 /* cups-version */
417 ippAddString(CommonData, IPP_TAG_PRINTER, IPP_TAG_TEXT, "cups-version",
418 NULL, CUPS_SVERSION + 6);
419
420 /* generated-natural-language-supported */
421 ippAddString(CommonData, IPP_TAG_PRINTER, IPP_TAG_LANGUAGE,
422 "generated-natural-language-supported", NULL, DefaultLanguage);
423
424 /* ipp-versions-supported */
425 ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
426 "ipp-versions-supported", sizeof(versions) / sizeof(versions[0]),
427 NULL, versions);
428
429 /* job-hold-until-supported */
430 ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
431 "job-hold-until-supported", sizeof(holds) / sizeof(holds[0]),
432 NULL, holds);
433
434 /* job-priority-supported */
435 ippAddInteger(CommonData, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
436 "job-priority-supported", 100);
437
438 /* job-sheets-supported */
439 if (cupsArrayCount(Banners) > 0)
440 {
441 /*
442 * Setup the job-sheets-supported attribute...
443 */
444
445 if (Classification && !ClassifyOverride)
446 attr = ippAddString(CommonData, IPP_TAG_PRINTER, IPP_TAG_NAME,
447 "job-sheets-supported", NULL, Classification);
448 else
449 attr = ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_NAME,
450 "job-sheets-supported", cupsArrayCount(Banners) + 1,
451 NULL, NULL);
452
453 if (attr == NULL)
454 cupsdLogMessage(CUPSD_LOG_EMERG,
455 "Unable to allocate memory for "
456 "job-sheets-supported attribute: %s!", strerror(errno));
457 else if (!Classification || ClassifyOverride)
458 {
459 cupsd_banner_t *banner; /* Current banner */
460
461
462 attr->values[0].string.text = _cupsStrAlloc("none");
463
464 for (i = 1, banner = (cupsd_banner_t *)cupsArrayFirst(Banners);
465 banner;
466 i ++, banner = (cupsd_banner_t *)cupsArrayNext(Banners))
467 attr->values[i].string.text = _cupsStrAlloc(banner->name);
468 }
469 }
470 else
471 ippAddString(CommonData, IPP_TAG_PRINTER, IPP_TAG_NAME,
472 "job-sheets-supported", NULL, "none");
473
474 /* multiple-document-handling-supported */
475 ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
476 "multiple-document-handling-supported",
477 sizeof(multiple_document_handling) /
478 sizeof(multiple_document_handling[0]), NULL,
479 multiple_document_handling);
480
481 /* multiple-document-jobs-supported */
482 ippAddBoolean(CommonData, IPP_TAG_PRINTER,
483 "multiple-document-jobs-supported", 1);
484
485 /* multiple-operation-time-out */
486 ippAddInteger(CommonData, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
487 "multiple-operation-time-out", 60);
488
489 /* natural-language-configured */
490 ippAddString(CommonData, IPP_TAG_PRINTER, IPP_TAG_LANGUAGE,
491 "natural-language-configured", NULL, DefaultLanguage);
492
493 /* notify-attributes-supported */
494 ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
495 "notify-attributes-supported",
496 (int)(sizeof(notify_attrs) / sizeof(notify_attrs[0])),
497 NULL, notify_attrs);
498
499 /* notify-lease-duration-supported */
500 ippAddRange(CommonData, IPP_TAG_PRINTER,
501 "notify-lease-duration-supported", 0,
502 MaxLeaseDuration ? MaxLeaseDuration : 2147483647);
503
504 /* notify-max-events-supported */
505 ippAddInteger(CommonData, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
506 "notify-max-events-supported", MaxEvents);
507
508 /* notify-events-supported */
509 ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
510 "notify-events-supported",
511 (int)(sizeof(notify_events) / sizeof(notify_events[0])),
512 NULL, notify_events);
513
514 /* notify-pull-method-supported */
515 ippAddString(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
516 "notify-pull-method-supported", NULL, "ippget");
517
518 /* notify-schemes-supported */
519 snprintf(filename, sizeof(filename), "%s/notifier", ServerBin);
520 if ((dir = cupsDirOpen(filename)) != NULL)
521 {
522 notifiers = cupsArrayNew((cups_array_func_t)strcmp, NULL);
523
524 while ((dent = cupsDirRead(dir)) != NULL)
525 if (S_ISREG(dent->fileinfo.st_mode) &&
526 (dent->fileinfo.st_mode & S_IXOTH) != 0)
527 cupsArrayAdd(notifiers, _cupsStrAlloc(dent->filename));
528
529 if (cupsArrayCount(notifiers) > 0)
530 {
531 attr = ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
532 "notify-schemes-supported",
533 cupsArrayCount(notifiers), NULL, NULL);
534
535 for (i = 0, notifier = (char *)cupsArrayFirst(notifiers);
536 notifier;
537 i ++, notifier = (char *)cupsArrayNext(notifiers))
538 attr->values[i].string.text = notifier;
539 }
540
541 cupsArrayDelete(notifiers);
542 cupsDirClose(dir);
543 }
544
545 /* number-up-supported */
546 ippAddIntegers(CommonData, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
547 "number-up-supported", sizeof(nups) / sizeof(nups[0]), nups);
548
549 /* operations-supported */
550 ippAddIntegers(CommonData, IPP_TAG_PRINTER, IPP_TAG_ENUM,
551 "operations-supported",
552 sizeof(ops) / sizeof(ops[0]) + JobFiles - 1, ops);
553
554 /* orientation-requested-supported */
555 ippAddIntegers(CommonData, IPP_TAG_PRINTER, IPP_TAG_ENUM,
556 "orientation-requested-supported", 4, orients);
557
558 /* page-ranges-supported */
559 ippAddBoolean(CommonData, IPP_TAG_PRINTER, "page-ranges-supported", 1);
560
561 /* pdf-override-supported */
562 ippAddString(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
563 "pdl-override-supported", NULL, "not-attempted");
564
565 /* printer-error-policy-supported */
566 ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_NAME,
567 "printer-error-policy-supported",
568 sizeof(errors) / sizeof(errors[0]), NULL, errors);
569
570 /* printer-op-policy-supported */
571 attr = ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_NAME,
572 "printer-op-policy-supported", cupsArrayCount(Policies),
573 NULL, NULL);
574 for (i = 0, p = (cupsd_policy_t *)cupsArrayFirst(Policies);
575 p;
576 i ++, p = (cupsd_policy_t *)cupsArrayNext(Policies))
577 attr->values[i].string.text = _cupsStrAlloc(p->name);
578
579 ippAddBoolean(CommonData, IPP_TAG_PRINTER, "server-is-sharing-printers",
580 BrowseLocalProtocols != 0 && Browsing);
581 }
582
583
584 /*
585 * 'cupsdDeleteAllPrinters()' - Delete all printers from the system.
586 */
587
588 void
589 cupsdDeleteAllPrinters(void)
590 {
591 cupsd_printer_t *p; /* Pointer to current printer/class */
592
593
594 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
595 p;
596 p = (cupsd_printer_t *)cupsArrayNext(Printers))
597 if (!(p->type & CUPS_PRINTER_CLASS))
598 cupsdDeletePrinter(p, 0);
599 }
600
601
602 /*
603 * 'cupsdDeletePrinter()' - Delete a printer from the system.
604 */
605
606 void
607 cupsdDeletePrinter(
608 cupsd_printer_t *p, /* I - Printer to delete */
609 int update) /* I - Update printers.conf? */
610 {
611 int i; /* Looping var */
612 #ifdef __sgi
613 char filename[1024]; /* Interface script filename */
614 #endif /* __sgi */
615
616
617 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdDeletePrinter(p=%p(%s), update=%d)",
618 p, p->name, update);
619
620 /*
621 * Save the current position in the Printers array...
622 */
623
624 cupsArraySave(Printers);
625
626 /*
627 * Stop printing on this printer...
628 */
629
630 cupsdStopPrinter(p, update);
631
632 /*
633 * If this printer is the next for browsing, point to the next one...
634 */
635
636 if (p == BrowseNext)
637 {
638 cupsArrayFind(Printers, p);
639 BrowseNext = (cupsd_printer_t *)cupsArrayNext(Printers);
640 }
641
642 /*
643 * Remove the printer from the list...
644 */
645
646 cupsdLogMessage(CUPSD_LOG_DEBUG2,
647 "cupsdDeletePrinter: Removing %s from Printers", p->name);
648 cupsArrayRemove(Printers, p);
649
650 if (p->type & CUPS_PRINTER_IMPLICIT)
651 {
652 cupsdLogMessage(CUPSD_LOG_DEBUG2,
653 "cupsdDeletePrinter: Removing %s from ImplicitPrinters",
654 p->name);
655 cupsArrayRemove(ImplicitPrinters, p);
656 }
657
658 /*
659 * Remove the dummy interface/icon/option files under IRIX...
660 */
661
662 #ifdef __sgi
663 snprintf(filename, sizeof(filename), "/var/spool/lp/interface/%s", p->name);
664 unlink(filename);
665
666 snprintf(filename, sizeof(filename), "/var/spool/lp/gui_interface/ELF/%s.gui",
667 p->name);
668 unlink(filename);
669
670 snprintf(filename, sizeof(filename), "/var/spool/lp/activeicons/%s", p->name);
671 unlink(filename);
672
673 snprintf(filename, sizeof(filename), "/var/spool/lp/pod/%s.config", p->name);
674 unlink(filename);
675
676 snprintf(filename, sizeof(filename), "/var/spool/lp/pod/%s.status", p->name);
677 unlink(filename);
678
679 snprintf(filename, sizeof(filename), "/var/spool/lp/member/%s", p->name);
680 unlink(filename);
681 #endif /* __sgi */
682
683 /*
684 * If p is the default printer, assign a different one...
685 */
686
687 if (p == DefaultPrinter)
688 {
689 DefaultPrinter = NULL;
690
691 if (UseNetworkDefault)
692 {
693 /*
694 * Find the first network default printer and use it...
695 */
696
697 cupsd_printer_t *dp; /* New default printer */
698
699
700 for (dp = (cupsd_printer_t *)cupsArrayFirst(Printers);
701 dp;
702 dp = (cupsd_printer_t *)cupsArrayNext(Printers))
703 if (dp != p && (dp->type & CUPS_PRINTER_DEFAULT))
704 {
705 DefaultPrinter = dp;
706 break;
707 }
708 }
709 }
710
711 /*
712 * Remove this printer from any classes...
713 */
714
715 if (!(p->type & CUPS_PRINTER_IMPLICIT))
716 {
717 cupsdDeletePrinterFromClasses(p);
718
719 /*
720 * Deregister from any browse protocols...
721 */
722
723 cupsdDeregisterPrinter(p, 1);
724 }
725
726 /*
727 * Free all memory used by the printer...
728 */
729
730 if (p->printers != NULL)
731 free(p->printers);
732
733 if (MaxPrinterHistory)
734 {
735 for (i = 0; i < p->num_history; i ++)
736 ippDelete(p->history[i]);
737
738 free(p->history);
739 }
740
741 for (i = 0; i < p->num_reasons; i ++)
742 free(p->reasons[i]);
743
744 ippDelete(p->attrs);
745
746 delete_printer_filters(p);
747
748 mimeDeleteType(MimeDatabase, p->filetype);
749 mimeDeleteType(MimeDatabase, p->prefiltertype);
750
751 cupsdFreePrinterUsers(p);
752 cupsdFreeQuotas(p);
753
754 cupsdClearString(&p->uri);
755 cupsdClearString(&p->hostname);
756 cupsdClearString(&p->name);
757 cupsdClearString(&p->location);
758 cupsdClearString(&p->make_model);
759 cupsdClearString(&p->info);
760 cupsdClearString(&p->job_sheets[0]);
761 cupsdClearString(&p->job_sheets[1]);
762 cupsdClearString(&p->device_uri);
763 cupsdClearString(&p->port_monitor);
764 cupsdClearString(&p->op_policy);
765 cupsdClearString(&p->error_policy);
766
767 cupsdClearString(&p->alert);
768 cupsdClearString(&p->alert_description);
769
770 #ifdef HAVE_DNSSD
771 cupsdClearString(&p->product);
772 cupsdClearString(&p->pdl);
773 #endif /* HAVE_DNSSD */
774
775 cupsArrayDelete(p->filetypes);
776
777 if (p->browse_attrs)
778 free(p->browse_attrs);
779
780 #ifdef __APPLE__
781 cupsdClearString(&p->recoverable);
782 #endif /* __APPLE__ */
783
784 cupsFreeOptions(p->num_options, p->options);
785
786 free(p);
787
788 /*
789 * Restore the previous position in the Printers array...
790 */
791
792 cupsArrayRestore(Printers);
793 }
794
795
796 /*
797 * 'cupsdFindDest()' - Find a destination in the list.
798 */
799
800 cupsd_printer_t * /* O - Destination in list */
801 cupsdFindDest(const char *name) /* I - Name of printer or class to find */
802 {
803 cupsd_printer_t key; /* Search key */
804
805
806 key.name = (char *)name;
807 return ((cupsd_printer_t *)cupsArrayFind(Printers, &key));
808 }
809
810
811 /*
812 * 'cupsdFindPrinter()' - Find a printer in the list.
813 */
814
815 cupsd_printer_t * /* O - Printer in list */
816 cupsdFindPrinter(const char *name) /* I - Name of printer to find */
817 {
818 cupsd_printer_t *p; /* Printer in list */
819
820
821 if ((p = cupsdFindDest(name)) != NULL && (p->type & CUPS_PRINTER_CLASS))
822 return (NULL);
823 else
824 return (p);
825 }
826
827
828 /*
829 * 'cupsdFreePrinterUsers()' - Free allow/deny users.
830 */
831
832 void
833 cupsdFreePrinterUsers(
834 cupsd_printer_t *p) /* I - Printer */
835 {
836 int i; /* Looping var */
837
838
839 if (!p || !p->num_users)
840 return;
841
842 for (i = 0; i < p->num_users; i ++)
843 free((void *)p->users[i]);
844
845 free(p->users);
846
847 p->num_users = 0;
848 p->users = NULL;
849 }
850
851
852 /*
853 * 'cupsdLoadAllPrinters()' - Load printers from the printers.conf file.
854 */
855
856 void
857 cupsdLoadAllPrinters(void)
858 {
859 cups_file_t *fp; /* printers.conf file */
860 int linenum; /* Current line number */
861 char line[1024], /* Line from file */
862 *value, /* Pointer to value */
863 *valueptr; /* Pointer into value */
864 cupsd_printer_t *p; /* Current printer */
865
866
867 /*
868 * Open the printers.conf file...
869 */
870
871 snprintf(line, sizeof(line), "%s/printers.conf", ServerRoot);
872 if ((fp = cupsFileOpen(line, "r")) == NULL)
873 {
874 if (errno != ENOENT)
875 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to open %s - %s", line,
876 strerror(errno));
877 return;
878 }
879
880 /*
881 * Read printer configurations until we hit EOF...
882 */
883
884 linenum = 0;
885 p = NULL;
886
887 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
888 {
889 /*
890 * Decode the directive...
891 */
892
893 if (!strcasecmp(line, "<Printer") ||
894 !strcasecmp(line, "<DefaultPrinter"))
895 {
896 /*
897 * <Printer name> or <DefaultPrinter name>
898 */
899
900 if (p == NULL && value)
901 {
902 /*
903 * Add the printer and a base file type...
904 */
905
906 cupsdLogMessage(CUPSD_LOG_DEBUG, "Loading printer %s...", value);
907
908 p = cupsdAddPrinter(value);
909 p->accepting = 1;
910 p->state = IPP_PRINTER_IDLE;
911
912 /*
913 * Set the default printer as needed...
914 */
915
916 if (!strcasecmp(line, "<DefaultPrinter"))
917 DefaultPrinter = p;
918 }
919 else
920 {
921 cupsdLogMessage(CUPSD_LOG_ERROR,
922 "Syntax error on line %d of printers.conf.", linenum);
923 break;
924 }
925 }
926 else if (!strcasecmp(line, "</Printer>"))
927 {
928 if (p != NULL)
929 {
930 /*
931 * Close out the current printer...
932 */
933
934 cupsdSetPrinterAttrs(p);
935 cupsdAddPrinterHistory(p);
936
937 if (p->device_uri && strncmp(p->device_uri, "file:", 5) &&
938 p->state != IPP_PRINTER_STOPPED)
939 {
940 /*
941 * See if the backend exists...
942 */
943
944 snprintf(line, sizeof(line), "%s/backend/%s", ServerBin,
945 p->device_uri);
946
947 if ((valueptr = strchr(line + strlen(ServerBin), ':')) != NULL)
948 *valueptr = '\0'; /* Chop everything but URI scheme */
949
950 if (access(line, 0))
951 {
952 /*
953 * Backend does not exist, stop printer...
954 */
955
956 p->state = IPP_PRINTER_STOPPED;
957 snprintf(p->state_message, sizeof(p->state_message),
958 "Backend %s does not exist!", line);
959 }
960 }
961
962 p = NULL;
963 }
964 else
965 {
966 cupsdLogMessage(CUPSD_LOG_ERROR,
967 "Syntax error on line %d of printers.conf.", linenum);
968 break;
969 }
970 }
971 else if (!p)
972 {
973 cupsdLogMessage(CUPSD_LOG_ERROR,
974 "Syntax error on line %d of printers.conf.", linenum);
975 break;
976 }
977 else if (!strcasecmp(line, "AuthInfoRequired"))
978 {
979 if (!cupsdSetAuthInfoRequired(p, value, NULL))
980 cupsdLogMessage(CUPSD_LOG_ERROR,
981 "Bad AuthInfoRequired on line %d of printers.conf.",
982 linenum);
983 }
984 else if (!strcasecmp(line, "Info"))
985 {
986 if (value)
987 cupsdSetString(&p->info, value);
988 }
989 else if (!strcasecmp(line, "Location"))
990 {
991 if (value)
992 cupsdSetString(&p->location, value);
993 }
994 else if (!strcasecmp(line, "DeviceURI"))
995 {
996 if (value)
997 cupsdSetString(&p->device_uri, value);
998 else
999 {
1000 cupsdLogMessage(CUPSD_LOG_ERROR,
1001 "Syntax error on line %d of printers.conf.", linenum);
1002 break;
1003 }
1004 }
1005 else if (!strcasecmp(line, "Option") && value)
1006 {
1007 /*
1008 * Option name value
1009 */
1010
1011 for (valueptr = value; *valueptr && !isspace(*valueptr & 255); valueptr ++);
1012
1013 if (!*valueptr)
1014 cupsdLogMessage(CUPSD_LOG_ERROR,
1015 "Syntax error on line %d of printers.conf.", linenum);
1016 else
1017 {
1018 for (; *valueptr && isspace(*valueptr & 255); *valueptr++ = '\0');
1019
1020 p->num_options = cupsAddOption(value, valueptr, p->num_options,
1021 &(p->options));
1022 }
1023 }
1024 else if (!strcasecmp(line, "PortMonitor"))
1025 {
1026 if (value && strcmp(value, "none"))
1027 cupsdSetString(&p->port_monitor, value);
1028 else if (value)
1029 cupsdClearString(&p->port_monitor);
1030 else
1031 {
1032 cupsdLogMessage(CUPSD_LOG_ERROR,
1033 "Syntax error on line %d of printers.conf.", linenum);
1034 break;
1035 }
1036 }
1037 else if (!strcasecmp(line, "State"))
1038 {
1039 /*
1040 * Set the initial queue state...
1041 */
1042
1043 if (value && !strcasecmp(value, "idle"))
1044 p->state = IPP_PRINTER_IDLE;
1045 else if (value && !strcasecmp(value, "stopped"))
1046 p->state = IPP_PRINTER_STOPPED;
1047 else
1048 {
1049 cupsdLogMessage(CUPSD_LOG_ERROR,
1050 "Syntax error on line %d of printers.conf.", linenum);
1051 break;
1052 }
1053 }
1054 else if (!strcasecmp(line, "StateMessage"))
1055 {
1056 /*
1057 * Set the initial queue state message...
1058 */
1059
1060 if (value)
1061 strlcpy(p->state_message, value, sizeof(p->state_message));
1062 }
1063 else if (!strcasecmp(line, "StateTime"))
1064 {
1065 /*
1066 * Set the state time...
1067 */
1068
1069 if (value)
1070 p->state_time = atoi(value);
1071 }
1072 else if (!strcasecmp(line, "Accepting"))
1073 {
1074 /*
1075 * Set the initial accepting state...
1076 */
1077
1078 if (value &&
1079 (!strcasecmp(value, "yes") ||
1080 !strcasecmp(value, "on") ||
1081 !strcasecmp(value, "true")))
1082 p->accepting = 1;
1083 else if (value &&
1084 (!strcasecmp(value, "no") ||
1085 !strcasecmp(value, "off") ||
1086 !strcasecmp(value, "false")))
1087 p->accepting = 0;
1088 else
1089 {
1090 cupsdLogMessage(CUPSD_LOG_ERROR,
1091 "Syntax error on line %d of printers.conf.", linenum);
1092 break;
1093 }
1094 }
1095 else if (!strcasecmp(line, "Shared"))
1096 {
1097 /*
1098 * Set the initial shared state...
1099 */
1100
1101 if (value &&
1102 (!strcasecmp(value, "yes") ||
1103 !strcasecmp(value, "on") ||
1104 !strcasecmp(value, "true")))
1105 p->shared = 1;
1106 else if (value &&
1107 (!strcasecmp(value, "no") ||
1108 !strcasecmp(value, "off") ||
1109 !strcasecmp(value, "false")))
1110 p->shared = 0;
1111 else
1112 {
1113 cupsdLogMessage(CUPSD_LOG_ERROR,
1114 "Syntax error on line %d of printers.conf.", linenum);
1115 break;
1116 }
1117 }
1118 else if (!strcasecmp(line, "JobSheets"))
1119 {
1120 /*
1121 * Set the initial job sheets...
1122 */
1123
1124 if (value)
1125 {
1126 for (valueptr = value; *valueptr && !isspace(*valueptr & 255); valueptr ++);
1127
1128 if (*valueptr)
1129 *valueptr++ = '\0';
1130
1131 cupsdSetString(&p->job_sheets[0], value);
1132
1133 while (isspace(*valueptr & 255))
1134 valueptr ++;
1135
1136 if (*valueptr)
1137 {
1138 for (value = valueptr; *valueptr && !isspace(*valueptr & 255); valueptr ++);
1139
1140 if (*valueptr)
1141 *valueptr = '\0';
1142
1143 cupsdSetString(&p->job_sheets[1], value);
1144 }
1145 }
1146 else
1147 {
1148 cupsdLogMessage(CUPSD_LOG_ERROR,
1149 "Syntax error on line %d of printers.conf.", linenum);
1150 break;
1151 }
1152 }
1153 else if (!strcasecmp(line, "AllowUser"))
1154 {
1155 if (value)
1156 {
1157 p->deny_users = 0;
1158 cupsdAddPrinterUser(p, value);
1159 }
1160 else
1161 {
1162 cupsdLogMessage(CUPSD_LOG_ERROR,
1163 "Syntax error on line %d of printers.conf.", linenum);
1164 break;
1165 }
1166 }
1167 else if (!strcasecmp(line, "DenyUser"))
1168 {
1169 if (value)
1170 {
1171 p->deny_users = 1;
1172 cupsdAddPrinterUser(p, value);
1173 }
1174 else
1175 {
1176 cupsdLogMessage(CUPSD_LOG_ERROR,
1177 "Syntax error on line %d of printers.conf.", linenum);
1178 break;
1179 }
1180 }
1181 else if (!strcasecmp(line, "QuotaPeriod"))
1182 {
1183 if (value)
1184 p->quota_period = atoi(value);
1185 else
1186 {
1187 cupsdLogMessage(CUPSD_LOG_ERROR,
1188 "Syntax error on line %d of printers.conf.", linenum);
1189 break;
1190 }
1191 }
1192 else if (!strcasecmp(line, "PageLimit"))
1193 {
1194 if (value)
1195 p->page_limit = atoi(value);
1196 else
1197 {
1198 cupsdLogMessage(CUPSD_LOG_ERROR,
1199 "Syntax error on line %d of printers.conf.", linenum);
1200 break;
1201 }
1202 }
1203 else if (!strcasecmp(line, "KLimit"))
1204 {
1205 if (value)
1206 p->k_limit = atoi(value);
1207 else
1208 {
1209 cupsdLogMessage(CUPSD_LOG_ERROR,
1210 "Syntax error on line %d of printers.conf.", linenum);
1211 break;
1212 }
1213 }
1214 else if (!strcasecmp(line, "OpPolicy"))
1215 {
1216 if (value)
1217 {
1218 cupsd_policy_t *pol; /* Policy */
1219
1220
1221 if ((pol = cupsdFindPolicy(value)) != NULL)
1222 {
1223 cupsdSetString(&p->op_policy, value);
1224 p->op_policy_ptr = pol;
1225 }
1226 else
1227 cupsdLogMessage(CUPSD_LOG_ERROR,
1228 "Bad policy \"%s\" on line %d of printers.conf",
1229 value, linenum);
1230 }
1231 else
1232 {
1233 cupsdLogMessage(CUPSD_LOG_ERROR,
1234 "Syntax error on line %d of printers.conf.", linenum);
1235 break;
1236 }
1237 }
1238 else if (!strcasecmp(line, "ErrorPolicy"))
1239 {
1240 if (value)
1241 cupsdSetString(&p->error_policy, value);
1242 else
1243 {
1244 cupsdLogMessage(CUPSD_LOG_ERROR,
1245 "Syntax error on line %d of printers.conf.", linenum);
1246 break;
1247 }
1248 }
1249 else if (!strcasecmp(line, "Attribute") && value)
1250 {
1251 for (valueptr = value; *valueptr && !isspace(*valueptr & 255); valueptr ++);
1252
1253 if (!*valueptr)
1254 cupsdLogMessage(CUPSD_LOG_ERROR,
1255 "Syntax error on line %d of printers.conf.", linenum);
1256 else
1257 {
1258 for (; *valueptr && isspace(*valueptr & 255); *valueptr++ = '\0');
1259
1260 if (!p->attrs)
1261 cupsdSetPrinterAttrs(p);
1262
1263 cupsdSetPrinterAttr(p, value, valueptr);
1264
1265 if (!strncmp(value, "marker-", 7))
1266 p->marker_time = time(NULL);
1267 }
1268 }
1269 else
1270 {
1271 /*
1272 * Something else we don't understand...
1273 */
1274
1275 cupsdLogMessage(CUPSD_LOG_ERROR,
1276 "Unknown configuration directive %s on line %d of printers.conf.",
1277 line, linenum);
1278 }
1279 }
1280
1281 cupsFileClose(fp);
1282 }
1283
1284
1285 /*
1286 * 'cupsdRenamePrinter()' - Rename a printer.
1287 */
1288
1289 void
1290 cupsdRenamePrinter(
1291 cupsd_printer_t *p, /* I - Printer */
1292 const char *name) /* I - New name */
1293 {
1294 /*
1295 * Remove the printer from the array(s) first...
1296 */
1297
1298 cupsdLogMessage(CUPSD_LOG_DEBUG2,
1299 "cupsdRenamePrinter: Removing %s from Printers", p->name);
1300 cupsArrayRemove(Printers, p);
1301
1302 if (p->type & CUPS_PRINTER_IMPLICIT)
1303 {
1304 cupsdLogMessage(CUPSD_LOG_DEBUG2,
1305 "cupsdRenamePrinter: Removing %s from ImplicitPrinters",
1306 p->name);
1307 cupsArrayRemove(ImplicitPrinters, p);
1308 }
1309
1310 /*
1311 * Rename the printer type...
1312 */
1313
1314 mimeDeleteType(MimeDatabase, p->filetype);
1315 p->filetype = mimeAddType(MimeDatabase, "printer", name);
1316
1317 mimeDeleteType(MimeDatabase, p->prefiltertype);
1318 p->prefiltertype = mimeAddType(MimeDatabase, "prefilter", name);
1319
1320 /*
1321 * Rename the printer...
1322 */
1323
1324 cupsdSetString(&p->name, name);
1325
1326 /*
1327 * Reset printer attributes...
1328 */
1329
1330 cupsdSetPrinterAttrs(p);
1331
1332 /*
1333 * Add the printer back to the printer array(s)...
1334 */
1335
1336 cupsdLogMessage(CUPSD_LOG_DEBUG2,
1337 "cupsdRenamePrinter: Adding %s to Printers", p->name);
1338 cupsArrayAdd(Printers, p);
1339
1340 if (p->type & CUPS_PRINTER_IMPLICIT)
1341 {
1342 cupsdLogMessage(CUPSD_LOG_DEBUG2,
1343 "cupsdRenamePrinter: Adding %s to ImplicitPrinters",
1344 p->name);
1345 cupsArrayAdd(ImplicitPrinters, p);
1346 }
1347 }
1348
1349
1350 /*
1351 * 'cupsdSaveAllPrinters()' - Save all printer definitions to the printers.conf
1352 * file.
1353 */
1354
1355 void
1356 cupsdSaveAllPrinters(void)
1357 {
1358 int i; /* Looping var */
1359 cups_file_t *fp; /* printers.conf file */
1360 char temp[1024]; /* Temporary string */
1361 char backup[1024]; /* printers.conf.O file */
1362 cupsd_printer_t *printer; /* Current printer class */
1363 time_t curtime; /* Current time */
1364 struct tm *curdate; /* Current date */
1365 cups_option_t *option; /* Current option */
1366 const char *ptr; /* Pointer into info/location */
1367 ipp_attribute_t *marker; /* Current marker attribute */
1368
1369
1370 /*
1371 * Create the printers.conf file...
1372 */
1373
1374 snprintf(temp, sizeof(temp), "%s/printers.conf", ServerRoot);
1375 snprintf(backup, sizeof(backup), "%s/printers.conf.O", ServerRoot);
1376
1377 if (rename(temp, backup))
1378 {
1379 if (errno != ENOENT)
1380 cupsdLogMessage(CUPSD_LOG_ERROR,
1381 "Unable to backup printers.conf - %s", strerror(errno));
1382 }
1383
1384 if ((fp = cupsFileOpen(temp, "w")) == NULL)
1385 {
1386 cupsdLogMessage(CUPSD_LOG_ERROR,
1387 "Unable to save printers.conf - %s", strerror(errno));
1388
1389 if (rename(backup, temp))
1390 cupsdLogMessage(CUPSD_LOG_ERROR,
1391 "Unable to restore printers.conf - %s", strerror(errno));
1392 return;
1393 }
1394 else
1395 cupsdLogMessage(CUPSD_LOG_INFO, "Saving printers.conf...");
1396
1397 /*
1398 * Restrict access to the file...
1399 */
1400
1401 fchown(cupsFileNumber(fp), getuid(), Group);
1402 fchmod(cupsFileNumber(fp), 0600);
1403
1404 /*
1405 * Write a small header to the file...
1406 */
1407
1408 curtime = time(NULL);
1409 curdate = localtime(&curtime);
1410 strftime(temp, sizeof(temp) - 1, "%Y-%m-%d %H:%M", curdate);
1411
1412 cupsFilePuts(fp, "# Printer configuration file for " CUPS_SVERSION "\n");
1413 cupsFilePrintf(fp, "# Written by cupsd on %s\n", temp);
1414
1415 /*
1416 * Write each local printer known to the system...
1417 */
1418
1419 for (printer = (cupsd_printer_t *)cupsArrayFirst(Printers);
1420 printer;
1421 printer = (cupsd_printer_t *)cupsArrayNext(Printers))
1422 {
1423 /*
1424 * Skip remote destinations and printer classes...
1425 */
1426
1427 if ((printer->type & CUPS_PRINTER_DISCOVERED) ||
1428 (printer->type & CUPS_PRINTER_CLASS) ||
1429 (printer->type & CUPS_PRINTER_IMPLICIT))
1430 continue;
1431
1432 /*
1433 * Write printers as needed...
1434 */
1435
1436 if (printer == DefaultPrinter)
1437 cupsFilePrintf(fp, "<DefaultPrinter %s>\n", printer->name);
1438 else
1439 cupsFilePrintf(fp, "<Printer %s>\n", printer->name);
1440
1441 if (printer->num_auth_info_required > 0)
1442 {
1443 cupsFilePrintf(fp, "AuthInfoRequired %s", printer->auth_info_required[0]);
1444 for (i = 1; i < printer->num_auth_info_required; i ++)
1445 cupsFilePrintf(fp, ",%s", printer->auth_info_required[i]);
1446 cupsFilePutChar(fp, '\n');
1447 }
1448
1449 if (printer->info)
1450 {
1451 if ((ptr = strchr(printer->info, '#')) != NULL)
1452 {
1453 /*
1454 * Need to quote the first # in the info string...
1455 */
1456
1457 cupsFilePuts(fp, "Info ");
1458 cupsFileWrite(fp, printer->info, ptr - printer->info);
1459 cupsFilePutChar(fp, '\\');
1460 cupsFilePuts(fp, ptr);
1461 cupsFilePutChar(fp, '\n');
1462 }
1463 else
1464 cupsFilePrintf(fp, "Info %s\n", printer->info);
1465 }
1466
1467 if (printer->location)
1468 {
1469 if ((ptr = strchr(printer->info, '#')) != NULL)
1470 {
1471 /*
1472 * Need to quote the first # in the location string...
1473 */
1474
1475 cupsFilePuts(fp, "Location ");
1476 cupsFileWrite(fp, printer->location, ptr - printer->location);
1477 cupsFilePutChar(fp, '\\');
1478 cupsFilePuts(fp, ptr);
1479 cupsFilePutChar(fp, '\n');
1480 }
1481 else
1482 cupsFilePrintf(fp, "Location %s\n", printer->location);
1483 }
1484 if (printer->device_uri)
1485 cupsFilePrintf(fp, "DeviceURI %s\n", printer->device_uri);
1486
1487 if (printer->port_monitor)
1488 cupsFilePrintf(fp, "PortMonitor %s\n", printer->port_monitor);
1489
1490 if (printer->state == IPP_PRINTER_STOPPED)
1491 {
1492 cupsFilePuts(fp, "State Stopped\n");
1493 cupsFilePrintf(fp, "StateMessage %s\n", printer->state_message);
1494 }
1495 else
1496 cupsFilePuts(fp, "State Idle\n");
1497
1498 cupsFilePrintf(fp, "StateTime %d\n", (int)printer->state_time);
1499
1500 if (printer->accepting)
1501 cupsFilePuts(fp, "Accepting Yes\n");
1502 else
1503 cupsFilePuts(fp, "Accepting No\n");
1504
1505 if (printer->shared)
1506 cupsFilePuts(fp, "Shared Yes\n");
1507 else
1508 cupsFilePuts(fp, "Shared No\n");
1509
1510 cupsFilePrintf(fp, "JobSheets %s %s\n", printer->job_sheets[0],
1511 printer->job_sheets[1]);
1512
1513 cupsFilePrintf(fp, "QuotaPeriod %d\n", printer->quota_period);
1514 cupsFilePrintf(fp, "PageLimit %d\n", printer->page_limit);
1515 cupsFilePrintf(fp, "KLimit %d\n", printer->k_limit);
1516
1517 for (i = 0; i < printer->num_users; i ++)
1518 {
1519 if ((ptr = strchr(printer->users[i], '#')) != NULL)
1520 {
1521 /*
1522 * Need to quote the first # in the user string...
1523 */
1524
1525 cupsFilePrintf(fp, "%sUser ", printer->deny_users ? "Deny" : "Allow");
1526 cupsFileWrite(fp, printer->users[i], ptr - printer->users[i]);
1527 cupsFilePutChar(fp, '\\');
1528 cupsFilePuts(fp, ptr);
1529 cupsFilePutChar(fp, '\n');
1530 }
1531 else
1532 cupsFilePrintf(fp, "%sUser %s\n",
1533 printer->deny_users ? "Deny" : "Allow",
1534 printer->users[i]);
1535 }
1536
1537 if (printer->op_policy)
1538 cupsFilePrintf(fp, "OpPolicy %s\n", printer->op_policy);
1539 if (printer->error_policy)
1540 cupsFilePrintf(fp, "ErrorPolicy %s\n", printer->error_policy);
1541
1542 for (i = printer->num_options, option = printer->options;
1543 i > 0;
1544 i --, option ++)
1545 {
1546 if ((ptr = strchr(option->value, '#')) != NULL)
1547 {
1548 /*
1549 * Need to quote the first # in the option string...
1550 */
1551
1552 cupsFilePrintf(fp, "Option %s ", option->name);
1553 cupsFileWrite(fp, option->value, ptr - option->value);
1554 cupsFilePutChar(fp, '\\');
1555 cupsFilePuts(fp, ptr);
1556 cupsFilePutChar(fp, '\n');
1557 }
1558 else
1559 cupsFilePrintf(fp, "Option %s %s\n", option->name, option->value);
1560 }
1561
1562 if ((marker = ippFindAttribute(printer->attrs, "marker-colors",
1563 IPP_TAG_NAME)) != NULL)
1564 {
1565 cupsFilePrintf(fp, "Attribute %s ", marker->name);
1566
1567 for (i = 0, ptr = NULL; i < marker->num_values; i ++)
1568 {
1569 if (i)
1570 cupsFilePutChar(fp, ',');
1571
1572 if (!ptr && (ptr = strchr(marker->values[i].string.text, '#')) != NULL)
1573 {
1574 cupsFileWrite(fp, marker->values[i].string.text,
1575 ptr - marker->values[i].string.text);
1576 cupsFilePutChar(fp, '\\');
1577 cupsFilePuts(fp, ptr);
1578 }
1579 else
1580 cupsFilePuts(fp, marker->values[i].string.text);
1581 }
1582
1583 cupsFilePuts(fp, "\n");
1584 }
1585
1586 if ((marker = ippFindAttribute(printer->attrs, "marker-levels",
1587 IPP_TAG_INTEGER)) != NULL)
1588 {
1589 cupsFilePrintf(fp, "Attribute %s %d", marker->name,
1590 marker->values[0].integer);
1591 for (i = 1; i < marker->num_values; i ++)
1592 cupsFilePrintf(fp, ",%d", marker->values[i].integer);
1593 cupsFilePuts(fp, "\n");
1594 }
1595
1596 if ((marker = ippFindAttribute(printer->attrs, "marker-message",
1597 IPP_TAG_TEXT)) != NULL)
1598 {
1599 cupsFilePrintf(fp, "Attribute %s ", marker->name);
1600
1601 if ((ptr = strchr(marker->values[0].string.text, '#')) != NULL)
1602 {
1603 cupsFileWrite(fp, marker->values[0].string.text,
1604 ptr - marker->values[0].string.text);
1605 cupsFilePutChar(fp, '\\');
1606 cupsFilePuts(fp, ptr);
1607 }
1608 else
1609 cupsFilePuts(fp, marker->values[0].string.text);
1610
1611 cupsFilePuts(fp, "\n");
1612 }
1613
1614 if ((marker = ippFindAttribute(printer->attrs, "marker-names",
1615 IPP_TAG_NAME)) != NULL)
1616 {
1617 cupsFilePrintf(fp, "Attribute %s ", marker->name);
1618
1619 for (i = 0, ptr = NULL; i < marker->num_values; i ++)
1620 {
1621 if (i)
1622 cupsFilePutChar(fp, ',');
1623
1624 if (!ptr && (ptr = strchr(marker->values[i].string.text, '#')) != NULL)
1625 {
1626 cupsFileWrite(fp, marker->values[i].string.text,
1627 ptr - marker->values[i].string.text);
1628 cupsFilePutChar(fp, '\\');
1629 cupsFilePuts(fp, ptr);
1630 }
1631 else
1632 cupsFilePuts(fp, marker->values[i].string.text);
1633 }
1634
1635 cupsFilePuts(fp, "\n");
1636 }
1637
1638 if ((marker = ippFindAttribute(printer->attrs, "marker-types",
1639 IPP_TAG_KEYWORD)) != NULL)
1640 {
1641 cupsFilePrintf(fp, "Attribute %s ", marker->name);
1642
1643 for (i = 0, ptr = NULL; i < marker->num_values; i ++)
1644 {
1645 if (i)
1646 cupsFilePutChar(fp, ',');
1647
1648 if (!ptr && (ptr = strchr(marker->values[i].string.text, '#')) != NULL)
1649 {
1650 cupsFileWrite(fp, marker->values[i].string.text,
1651 ptr - marker->values[i].string.text);
1652 cupsFilePutChar(fp, '\\');
1653 cupsFilePuts(fp, ptr);
1654 }
1655 else
1656 cupsFilePuts(fp, marker->values[i].string.text);
1657 }
1658
1659 cupsFilePuts(fp, "\n");
1660 }
1661
1662 cupsFilePuts(fp, "</Printer>\n");
1663
1664 #ifdef __sgi
1665 /*
1666 * Make IRIX desktop & printer status happy
1667 */
1668
1669 write_irix_state(printer);
1670 #endif /* __sgi */
1671 }
1672
1673 cupsFileClose(fp);
1674 }
1675
1676
1677 /*
1678 * 'cupsdSetAuthInfoRequired()' - Set the required authentication info.
1679 */
1680
1681 int /* O - 1 if value OK, 0 otherwise */
1682 cupsdSetAuthInfoRequired(
1683 cupsd_printer_t *p, /* I - Printer */
1684 const char *values, /* I - Plain text value (or NULL) */
1685 ipp_attribute_t *attr) /* I - IPP attribute value (or NULL) */
1686 {
1687 int i; /* Looping var */
1688
1689
1690 p->num_auth_info_required = 0;
1691
1692 /*
1693 * Do we have a plain text value?
1694 */
1695
1696 if (values)
1697 {
1698 /*
1699 * Yes, grab the keywords...
1700 */
1701
1702 const char *end; /* End of current value */
1703
1704
1705 while (*values && p->num_auth_info_required < 4)
1706 {
1707 if ((end = strchr(values, ',')) == NULL)
1708 end = values + strlen(values);
1709
1710 if ((end - values) == 4 && !strncmp(values, "none", 4))
1711 {
1712 if (p->num_auth_info_required != 0 || *end)
1713 return (0);
1714
1715 p->auth_info_required[p->num_auth_info_required] = "none";
1716 p->num_auth_info_required ++;
1717
1718 return (1);
1719 }
1720 else if ((end - values) == 9 && !strncmp(values, "negotiate", 9))
1721 {
1722 if (p->num_auth_info_required != 0 || *end)
1723 return (0);
1724
1725 p->auth_info_required[p->num_auth_info_required] = "negotiate";
1726 p->num_auth_info_required ++;
1727 }
1728 else if ((end - values) == 6 && !strncmp(values, "domain", 6))
1729 {
1730 p->auth_info_required[p->num_auth_info_required] = "domain";
1731 p->num_auth_info_required ++;
1732 }
1733 else if ((end - values) == 8 && !strncmp(values, "password", 8))
1734 {
1735 p->auth_info_required[p->num_auth_info_required] = "password";
1736 p->num_auth_info_required ++;
1737 }
1738 else if ((end - values) == 8 && !strncmp(values, "username", 8))
1739 {
1740 p->auth_info_required[p->num_auth_info_required] = "username";
1741 p->num_auth_info_required ++;
1742 }
1743 else
1744 return (0);
1745
1746 values = (*end) ? end + 1 : end;
1747 }
1748
1749 if (p->num_auth_info_required == 0)
1750 {
1751 p->auth_info_required[0] = "none";
1752 p->num_auth_info_required = 1;
1753 }
1754
1755 /*
1756 * Update the printer-type value as needed...
1757 */
1758
1759 if (p->num_auth_info_required > 1 ||
1760 strcmp(p->auth_info_required[0], "none"))
1761 p->type |= CUPS_PRINTER_AUTHENTICATED;
1762 else
1763 p->type &= ~CUPS_PRINTER_AUTHENTICATED;
1764
1765 return (1);
1766 }
1767
1768 /*
1769 * Grab values from an attribute instead...
1770 */
1771
1772 if (!attr || attr->num_values > 4)
1773 return (0);
1774
1775 /*
1776 * Update the printer-type value as needed...
1777 */
1778
1779 if (attr->num_values > 1 ||
1780 strcmp(attr->values[0].string.text, "none"))
1781 p->type |= CUPS_PRINTER_AUTHENTICATED;
1782 else
1783 p->type &= ~CUPS_PRINTER_AUTHENTICATED;
1784
1785 for (i = 0; i < attr->num_values; i ++)
1786 {
1787 if (!strcmp(attr->values[i].string.text, "none"))
1788 {
1789 if (p->num_auth_info_required != 0 || attr->num_values != 1)
1790 return (0);
1791
1792 p->auth_info_required[p->num_auth_info_required] = "none";
1793 p->num_auth_info_required ++;
1794
1795 return (1);
1796 }
1797 else if (!strcmp(attr->values[i].string.text, "negotiate"))
1798 {
1799 if (p->num_auth_info_required != 0 || attr->num_values != 1)
1800 return (0);
1801
1802 p->auth_info_required[p->num_auth_info_required] = "negotiate";
1803 p->num_auth_info_required ++;
1804
1805 return (1);
1806 }
1807 else if (!strcmp(attr->values[i].string.text, "domain"))
1808 {
1809 p->auth_info_required[p->num_auth_info_required] = "domain";
1810 p->num_auth_info_required ++;
1811 }
1812 else if (!strcmp(attr->values[i].string.text, "password"))
1813 {
1814 p->auth_info_required[p->num_auth_info_required] = "password";
1815 p->num_auth_info_required ++;
1816 }
1817 else if (!strcmp(attr->values[i].string.text, "username"))
1818 {
1819 p->auth_info_required[p->num_auth_info_required] = "username";
1820 p->num_auth_info_required ++;
1821 }
1822 else
1823 return (0);
1824 }
1825
1826 return (1);
1827 }
1828
1829
1830 /*
1831 * 'cupsdSetPrinterAttr()' - Set a printer attribute.
1832 */
1833
1834 void
1835 cupsdSetPrinterAttr(
1836 cupsd_printer_t *p, /* I - Printer */
1837 const char *name, /* I - Attribute name */
1838 char *value) /* I - Attribute value string */
1839 {
1840 ipp_attribute_t *attr; /* Attribute */
1841 int i, /* Looping var */
1842 count; /* Number of values */
1843 char *ptr; /* Pointer into value */
1844 ipp_tag_t value_tag; /* Value tag for this attribute */
1845
1846
1847 /*
1848 * Count the number of values...
1849 */
1850
1851 for (count = 1, ptr = value;
1852 (ptr = strchr(ptr, ',')) != NULL;
1853 ptr ++, count ++);
1854
1855 /*
1856 * Then add or update the attribute as needed...
1857 */
1858
1859 if (!strcmp(name, "marker-levels"))
1860 {
1861 /*
1862 * Integer values...
1863 */
1864
1865 if ((attr = ippFindAttribute(p->attrs, name, IPP_TAG_INTEGER)) != NULL &&
1866 attr->num_values < count)
1867 {
1868 ippDeleteAttribute(p->attrs, attr);
1869 attr = NULL;
1870 }
1871
1872 if (attr)
1873 attr->num_values = count;
1874 else
1875 attr = ippAddIntegers(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER, name,
1876 count, NULL);
1877
1878 if (!attr)
1879 {
1880 cupsdLogMessage(CUPSD_LOG_ERROR,
1881 "Unable to allocate memory for printer attribute "
1882 "(%d values)", count);
1883 return;
1884 }
1885
1886 for (i = 0; i < count; i ++)
1887 {
1888 if ((ptr = strchr(value, ',')) != NULL)
1889 *ptr++ = '\0';
1890
1891 attr->values[i].integer = strtol(value, NULL, 10);
1892
1893 if (ptr)
1894 value = ptr;
1895 }
1896 }
1897 else
1898 {
1899 /*
1900 * Name or keyword values...
1901 */
1902
1903 if (!strcmp(name, "marker-types"))
1904 value_tag = IPP_TAG_KEYWORD;
1905 else if (!strcmp(name, "marker-message"))
1906 value_tag = IPP_TAG_TEXT;
1907 else
1908 value_tag = IPP_TAG_NAME;
1909
1910 if ((attr = ippFindAttribute(p->attrs, name, value_tag)) != NULL &&
1911 attr->num_values < count)
1912 {
1913 ippDeleteAttribute(p->attrs, attr);
1914 attr = NULL;
1915 }
1916
1917 if (attr)
1918 {
1919 for (i = 0; i < attr->num_values; i ++)
1920 _cupsStrFree(attr->values[i].string.text);
1921
1922 attr->num_values = count;
1923 }
1924 else
1925 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, value_tag, name,
1926 count, NULL, NULL);
1927
1928 if (!attr)
1929 {
1930 cupsdLogMessage(CUPSD_LOG_ERROR,
1931 "Unable to allocate memory for printer attribute "
1932 "(%d values)", count);
1933 return;
1934 }
1935
1936 for (i = 0; i < count; i ++)
1937 {
1938 if ((ptr = strchr(value, ',')) != NULL)
1939 *ptr++ = '\0';
1940
1941 attr->values[i].string.text = _cupsStrAlloc(value);
1942
1943 if (ptr)
1944 value = ptr;
1945 }
1946 }
1947
1948 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
1949 }
1950
1951
1952 /*
1953 * 'cupsdSetPrinterAttrs()' - Set printer attributes based upon the PPD file.
1954 */
1955
1956 void
1957 cupsdSetPrinterAttrs(cupsd_printer_t *p)/* I - Printer to setup */
1958 {
1959 int i, /* Looping var */
1960 length; /* Length of browse attributes */
1961 char uri[HTTP_MAX_URI]; /* URI for printer */
1962 char resource[HTTP_MAX_URI]; /* Resource portion of URI */
1963 char filename[1024]; /* Name of PPD file */
1964 int num_air; /* Number of auth-info-required values */
1965 const char * const *air; /* auth-info-required values */
1966 int num_media; /* Number of media options */
1967 cupsd_location_t *auth; /* Pointer to authentication element */
1968 const char *auth_supported; /* Authentication supported */
1969 ppd_file_t *ppd; /* PPD file data */
1970 ppd_option_t *input_slot, /* InputSlot options */
1971 *media_type, /* MediaType options */
1972 *page_size, /* PageSize options */
1973 *output_bin, /* OutputBin options */
1974 *media_quality, /* EFMediaQualityMode options */
1975 *duplex; /* Duplex options */
1976 ppd_attr_t *ppdattr; /* PPD attribute */
1977 ipp_t *oldattrs; /* Old printer attributes */
1978 ipp_attribute_t *attr; /* Attribute data */
1979 ipp_value_t *val; /* Attribute value */
1980 int num_finishings; /* Number of finishings */
1981 int finishings[5]; /* finishings-supported values */
1982 cups_option_t *option; /* Current printer option */
1983 static const char * const sides[3] = /* sides-supported values */
1984 {
1985 "one-sided",
1986 "two-sided-long-edge",
1987 "two-sided-short-edge"
1988 };
1989 static const char * const air_userpass[] =
1990 { /* Basic/Digest authentication */
1991 "username",
1992 "password"
1993 };
1994 #ifdef HAVE_GSSAPI
1995 static const char * const air_negotiate[] =
1996 { /* Kerberos authentication */
1997 "negotiate"
1998 };
1999 #endif /* HAVE_GSSAPI */
2000 static const char * const air_none[] =
2001 { /* No authentication */
2002 "none"
2003 };
2004 static const char * const standard_commands[] =
2005 { /* Standard CUPS commands */
2006 "AutoConfigure",
2007 "Clean",
2008 "PrintSelfTestPage",
2009 "ReportLevels"
2010 };
2011
2012
2013 DEBUG_printf(("cupsdSetPrinterAttrs: entering name = %s, type = %x\n", p->name,
2014 p->type));
2015
2016 /*
2017 * Make sure that we have the common attributes defined...
2018 */
2019
2020 if (!CommonData)
2021 cupsdCreateCommonData();
2022
2023 /*
2024 * Clear out old filters, if any...
2025 */
2026
2027 delete_printer_filters(p);
2028
2029 /*
2030 * Figure out the authentication that is required for the printer.
2031 */
2032
2033 auth_supported = "requesting-user-name";
2034 num_air = 1;
2035 air = air_none;
2036
2037 if (p->num_auth_info_required > 0 && strcmp(p->auth_info_required[0], "none"))
2038 {
2039 num_air = p->num_auth_info_required;
2040 air = p->auth_info_required;
2041
2042 if (!strcmp(air[0], "username"))
2043 auth_supported = "basic";
2044 else
2045 auth_supported = "negotiate";
2046 }
2047 else if (!(p->type & CUPS_PRINTER_DISCOVERED))
2048 {
2049 if (p->type & CUPS_PRINTER_CLASS)
2050 snprintf(resource, sizeof(resource), "/classes/%s", p->name);
2051 else
2052 snprintf(resource, sizeof(resource), "/printers/%s", p->name);
2053
2054 if ((auth = cupsdFindBest(resource, HTTP_POST)) == NULL ||
2055 auth->type == CUPSD_AUTH_NONE)
2056 auth = cupsdFindPolicyOp(p->op_policy_ptr, IPP_PRINT_JOB);
2057
2058 if (auth)
2059 {
2060 int auth_type; /* Authentication type */
2061
2062
2063 if ((auth_type = auth->type) == CUPSD_AUTH_DEFAULT)
2064 auth_type = DefaultAuthType;
2065
2066 if (auth_type == CUPSD_AUTH_BASIC || auth_type == CUPSD_AUTH_BASICDIGEST)
2067 {
2068 auth_supported = "basic";
2069 num_air = 2;
2070 air = air_userpass;
2071 }
2072 else if (auth_type == CUPSD_AUTH_DIGEST)
2073 {
2074 auth_supported = "digest";
2075 num_air = 2;
2076 air = air_userpass;
2077 }
2078 #ifdef HAVE_GSSAPI
2079 else if (auth_type == CUPSD_AUTH_NEGOTIATE)
2080 {
2081 auth_supported = "negotiate";
2082 num_air = 1;
2083 air = air_negotiate;
2084 }
2085 #endif /* HAVE_GSSAPI */
2086
2087 if (auth_type != CUPSD_AUTH_NONE)
2088 p->type |= CUPS_PRINTER_AUTHENTICATED;
2089 else
2090 p->type &= ~CUPS_PRINTER_AUTHENTICATED;
2091 }
2092 else
2093 p->type &= ~CUPS_PRINTER_AUTHENTICATED;
2094 }
2095 else if (p->type & CUPS_PRINTER_AUTHENTICATED)
2096 {
2097 num_air = 2;
2098 air = air_userpass;
2099 }
2100
2101 /*
2102 * Create the required IPP attributes for a printer...
2103 */
2104
2105 oldattrs = p->attrs;
2106 p->attrs = ippNew();
2107
2108 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2109 "uri-authentication-supported", NULL, auth_supported);
2110 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2111 "uri-security-supported", NULL, "none");
2112 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME, "printer-name", NULL,
2113 p->name);
2114 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-location",
2115 NULL, p->location ? p->location : "");
2116 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-info",
2117 NULL, p->info ? p->info : "");
2118 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI, "printer-more-info",
2119 NULL, p->uri);
2120
2121 if (p->num_users)
2122 {
2123 if (p->deny_users)
2124 ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
2125 "requesting-user-name-denied", p->num_users, NULL,
2126 p->users);
2127 else
2128 ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
2129 "requesting-user-name-allowed", p->num_users, NULL,
2130 p->users);
2131 }
2132
2133 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
2134 "job-quota-period", p->quota_period);
2135 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
2136 "job-k-limit", p->k_limit);
2137 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
2138 "job-page-limit", p->page_limit);
2139 ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2140 "auth-info-required", num_air, NULL, air);
2141
2142 if (cupsArrayCount(Banners) > 0 && !(p->type & CUPS_PRINTER_DISCOVERED))
2143 {
2144 /*
2145 * Setup the job-sheets-default attribute...
2146 */
2147
2148 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
2149 "job-sheets-default", 2, NULL, NULL);
2150
2151 if (attr != NULL)
2152 {
2153 attr->values[0].string.text = _cupsStrAlloc(Classification ?
2154 Classification : p->job_sheets[0]);
2155 attr->values[1].string.text = _cupsStrAlloc(Classification ?
2156 Classification : p->job_sheets[1]);
2157 }
2158 }
2159
2160 p->raw = 0;
2161 p->remote = 0;
2162
2163 if (p->type & CUPS_PRINTER_DISCOVERED)
2164 {
2165 /*
2166 * Tell the client this is a remote printer of some type...
2167 */
2168
2169 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI,
2170 "printer-uri-supported", NULL, p->uri);
2171
2172 if (p->make_model)
2173 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
2174 "printer-make-and-model", NULL, p->make_model);
2175
2176 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI, "device-uri", NULL,
2177 p->uri);
2178
2179 p->raw = 1;
2180 p->remote = 1;
2181 }
2182 else
2183 {
2184 /*
2185 * Assign additional attributes depending on whether this is a printer
2186 * or class...
2187 */
2188
2189 p->type &= ~CUPS_PRINTER_OPTIONS;
2190
2191 if (p->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT))
2192 {
2193 p->raw = 1;
2194
2195 /*
2196 * Add class-specific attributes...
2197 */
2198
2199 if ((p->type & CUPS_PRINTER_IMPLICIT) && p->num_printers > 0 &&
2200 p->printers[0]->make_model)
2201 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
2202 "printer-make-and-model", NULL, p->printers[0]->make_model);
2203 else
2204 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
2205 "printer-make-and-model", NULL, "Local Printer Class");
2206
2207 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI, "device-uri", NULL,
2208 "file:///dev/null");
2209
2210 if (p->num_printers > 0)
2211 {
2212 /*
2213 * Add a list of member names; URIs are added in copy_printer_attrs...
2214 */
2215
2216 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
2217 "member-names", p->num_printers, NULL, NULL);
2218 p->type |= CUPS_PRINTER_OPTIONS;
2219
2220 for (i = 0; i < p->num_printers; i ++)
2221 {
2222 if (attr != NULL)
2223 attr->values[i].string.text = _cupsStrAlloc(p->printers[i]->name);
2224
2225 p->type &= ~CUPS_PRINTER_OPTIONS | p->printers[i]->type;
2226 }
2227 }
2228 }
2229 else
2230 {
2231 /*
2232 * Add printer-specific attributes... Start by sanitizing the device
2233 * URI so it doesn't have a username or password in it...
2234 */
2235
2236 if (!p->device_uri)
2237 strcpy(uri, "file:/dev/null");
2238 else if (strstr(p->device_uri, "://") != NULL)
2239 {
2240 /*
2241 * http://..., ipp://..., etc.
2242 */
2243
2244 cupsdSanitizeURI(p->device_uri, uri, sizeof(uri));
2245 }
2246 else
2247 {
2248 /*
2249 * file:..., serial:..., etc.
2250 */
2251
2252 strlcpy(uri, p->device_uri, sizeof(uri));
2253 }
2254
2255 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI, "device-uri", NULL,
2256 uri);
2257
2258 /*
2259 * Assign additional attributes from the PPD file (if any)...
2260 */
2261
2262 p->type |= CUPS_PRINTER_BW;
2263 finishings[0] = IPP_FINISHINGS_NONE;
2264 num_finishings = 1;
2265
2266 snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot,
2267 p->name);
2268
2269 if ((ppd = ppdOpenFile(filename)) != NULL)
2270 {
2271 /*
2272 * Add make/model and other various attributes...
2273 */
2274
2275 if (ppd->color_device)
2276 p->type |= CUPS_PRINTER_COLOR;
2277 if (ppd->variable_sizes)
2278 p->type |= CUPS_PRINTER_VARIABLE;
2279 if (!ppd->manual_copies)
2280 p->type |= CUPS_PRINTER_COPIES;
2281 if ((ppdattr = ppdFindAttr(ppd, "cupsFax", NULL)) != NULL)
2282 if (ppdattr->value && !strcasecmp(ppdattr->value, "true"))
2283 p->type |= CUPS_PRINTER_FAX;
2284
2285 ippAddBoolean(p->attrs, IPP_TAG_PRINTER, "color-supported",
2286 ppd->color_device);
2287 if (ppd->throughput)
2288 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
2289 "pages-per-minute", ppd->throughput);
2290
2291 if (ppd->nickname)
2292 {
2293 /*
2294 * The NickName can be localized in the character set specified
2295 * by the LanugageEncoding attribute. However, ppdOpen2() has
2296 * already converted the ppd->nickname member to UTF-8 for us
2297 * (the original attribute value is available separately)
2298 */
2299
2300 cupsdSetString(&p->make_model, ppd->nickname);
2301 }
2302 else if (ppd->modelname)
2303 {
2304 /*
2305 * Model name can only contain specific characters...
2306 */
2307
2308 cupsdSetString(&p->make_model, ppd->modelname);
2309 }
2310 else
2311 cupsdSetString(&p->make_model, "Bad PPD File");
2312
2313 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
2314 "printer-make-and-model", NULL, p->make_model);
2315
2316 /*
2317 * Add media options from the PPD file...
2318 */
2319
2320 if ((input_slot = ppdFindOption(ppd, "InputSlot")) != NULL)
2321 num_media = input_slot->num_choices;
2322 else
2323 num_media = 0;
2324
2325 if ((media_type = ppdFindOption(ppd, "MediaType")) != NULL)
2326 num_media += media_type->num_choices;
2327
2328 if ((page_size = ppdFindOption(ppd, "PageSize")) != NULL)
2329 num_media += page_size->num_choices;
2330
2331 if ((media_quality = ppdFindOption(ppd, "EFMediaQualityMode")) != NULL)
2332 num_media += media_quality->num_choices;
2333
2334 if (num_media == 0)
2335 {
2336 cupsdLogMessage(CUPSD_LOG_CRIT,
2337 "The PPD file for printer %s contains no media "
2338 "options and is therefore invalid!", p->name);
2339 }
2340 else
2341 {
2342 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2343 "media-supported", num_media, NULL, NULL);
2344 if (attr != NULL)
2345 {
2346 val = attr->values;
2347
2348 if (input_slot != NULL)
2349 for (i = 0; i < input_slot->num_choices; i ++, val ++)
2350 val->string.text = _cupsStrAlloc(input_slot->choices[i].choice);
2351
2352 if (media_type != NULL)
2353 for (i = 0; i < media_type->num_choices; i ++, val ++)
2354 val->string.text = _cupsStrAlloc(media_type->choices[i].choice);
2355
2356 if (media_quality != NULL)
2357 for (i = 0; i < media_quality->num_choices; i ++, val ++)
2358 val->string.text = _cupsStrAlloc(media_quality->choices[i].choice);
2359
2360 if (page_size != NULL)
2361 {
2362 for (i = 0; i < page_size->num_choices; i ++, val ++)
2363 val->string.text = _cupsStrAlloc(page_size->choices[i].choice);
2364
2365 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2366 "media-default", NULL, page_size->defchoice);
2367 }
2368 else if (input_slot != NULL)
2369 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2370 "media-default", NULL, input_slot->defchoice);
2371 else if (media_type != NULL)
2372 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2373 "media-default", NULL, media_type->defchoice);
2374 else if (media_quality != NULL)
2375 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2376 "media-default", NULL, media_quality->defchoice);
2377 else
2378 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2379 "media-default", NULL, "none");
2380 }
2381 }
2382
2383 /*
2384 * Output bin...
2385 */
2386
2387 if ((output_bin = ppdFindOption(ppd, "OutputBin")) != NULL)
2388 {
2389 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2390 "output-bin-supported", output_bin->num_choices,
2391 NULL, NULL);
2392
2393 if (attr != NULL)
2394 {
2395 for (i = 0, val = attr->values;
2396 i < output_bin->num_choices;
2397 i ++, val ++)
2398 val->string.text = _cupsStrAlloc(output_bin->choices[i].choice);
2399 }
2400 }
2401
2402 /*
2403 * Duplexing, etc...
2404 */
2405
2406 if ((duplex = ppdFindOption(ppd, "Duplex")) == NULL)
2407 if ((duplex = ppdFindOption(ppd, "EFDuplex")) == NULL)
2408 if ((duplex = ppdFindOption(ppd, "EFDuplexing")) == NULL)
2409 if ((duplex = ppdFindOption(ppd, "KD03Duplex")) == NULL)
2410 duplex = ppdFindOption(ppd, "JCLDuplex");
2411
2412 if (duplex && duplex->num_choices > 1 &&
2413 !ppdInstallableConflict(ppd, duplex->keyword, "DuplexTumble"))
2414 {
2415 p->type |= CUPS_PRINTER_DUPLEX;
2416
2417 ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2418 "sides-supported", 3, NULL, sides);
2419
2420 if (!strcasecmp(duplex->defchoice, "DuplexTumble"))
2421 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2422 "sides-default", NULL, "two-sided-short-edge");
2423 else if (!strcasecmp(duplex->defchoice, "DuplexNoTumble"))
2424 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2425 "sides-default", NULL, "two-sided-long-edge");
2426 else
2427 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2428 "sides-default", NULL, "one-sided");
2429 }
2430
2431 if (ppdFindOption(ppd, "Collate") != NULL)
2432 p->type |= CUPS_PRINTER_COLLATE;
2433
2434 if (ppdFindOption(ppd, "StapleLocation") != NULL)
2435 {
2436 p->type |= CUPS_PRINTER_STAPLE;
2437 finishings[num_finishings++] = IPP_FINISHINGS_STAPLE;
2438 }
2439
2440 if (ppdFindOption(ppd, "BindEdge") != NULL)
2441 {
2442 p->type |= CUPS_PRINTER_BIND;
2443 finishings[num_finishings++] = IPP_FINISHINGS_BIND;
2444 }
2445
2446 for (i = 0; i < ppd->num_sizes; i ++)
2447 if (ppd->sizes[i].length > 1728)
2448 p->type |= CUPS_PRINTER_LARGE;
2449 else if (ppd->sizes[i].length > 1008)
2450 p->type |= CUPS_PRINTER_MEDIUM;
2451 else
2452 p->type |= CUPS_PRINTER_SMALL;
2453
2454 /*
2455 * Add a filter from application/vnd.cups-raw to printer/name to
2456 * handle "raw" printing by users.
2457 */
2458
2459 add_printer_filter(p, p->filetype, "application/vnd.cups-raw 0 -");
2460
2461 /*
2462 * Add any pre-filters in the PPD file...
2463 */
2464
2465 if ((ppdattr = ppdFindAttr(ppd, "cupsPreFilter", NULL)) != NULL)
2466 {
2467 p->prefiltertype = mimeAddType(MimeDatabase, "prefilter", p->name);
2468
2469 for (; ppdattr; ppdattr = ppdFindNextAttr(ppd, "cupsPreFilter", NULL))
2470 if (ppdattr->value)
2471 add_printer_filter(p, p->prefiltertype, ppdattr->value);
2472 }
2473
2474 /*
2475 * Add any filters in the PPD file...
2476 */
2477
2478 DEBUG_printf(("ppd->num_filters = %d\n", ppd->num_filters));
2479 for (i = 0; i < ppd->num_filters; i ++)
2480 {
2481 DEBUG_printf(("ppd->filters[%d] = \"%s\"\n", i, ppd->filters[i]));
2482 add_printer_filter(p, p->filetype, ppd->filters[i]);
2483 }
2484
2485 if (ppd->num_filters == 0)
2486 {
2487 /*
2488 * If there are no filters, add PostScript printing filters.
2489 */
2490
2491 add_printer_filter(p, p->filetype,
2492 "application/vnd.cups-command 0 commandtops");
2493 add_printer_filter(p, p->filetype,
2494 "application/vnd.cups-postscript 0 -");
2495
2496 p->type |= CUPS_PRINTER_COMMANDS;
2497 }
2498 else if (!(p->type & CUPS_PRINTER_COMMANDS))
2499 {
2500 /*
2501 * See if this is a PostScript device without a command filter...
2502 */
2503
2504 for (i = 0; i < ppd->num_filters; i ++)
2505 if (!strncasecmp(ppd->filters[i],
2506 "application/vnd.cups-postscript", 31))
2507 break;
2508
2509 if (i < ppd->num_filters)
2510 {
2511 /*
2512 * Add the generic PostScript command filter...
2513 */
2514
2515 add_printer_filter(p, p->filetype,
2516 "application/vnd.cups-command 0 commandtops");
2517 p->type |= CUPS_PRINTER_COMMANDS;
2518 }
2519 }
2520
2521 if (p->type & CUPS_PRINTER_COMMANDS)
2522 {
2523 char *commands, /* Copy of commands */
2524 *start, /* Start of name */
2525 *end; /* End of name */
2526 int count; /* Number of commands */
2527
2528
2529 if ((ppdattr = ppdFindAttr(ppd, "cupsCommands", NULL)) != NULL &&
2530 ppdattr->value && ppdattr->value[0])
2531 {
2532 for (count = 0, start = ppdattr->value; *start; count ++)
2533 {
2534 while (isspace(*start & 255))
2535 start ++;
2536
2537 if (!*start)
2538 break;
2539
2540 while (*start && !isspace(*start & 255))
2541 start ++;
2542 }
2543 }
2544 else
2545 count = 0;
2546
2547 if (count > 0)
2548 {
2549 /*
2550 * Make a copy of the commands string and count how many ...
2551 */
2552
2553 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2554 "printer-commands", count, NULL, NULL);
2555
2556 commands = strdup(ppdattr->value);
2557
2558 for (count = 0, start = commands; *start; count ++)
2559 {
2560 while (isspace(*start & 255))
2561 start ++;
2562
2563 if (!*start)
2564 break;
2565
2566 end = start;
2567 while (*end && !isspace(*end & 255))
2568 end ++;
2569
2570 if (*end)
2571 *end++ = '\0';
2572
2573 attr->values[count].string.text = _cupsStrAlloc(start);
2574
2575 start = end;
2576 }
2577
2578 free(commands);
2579 }
2580 else
2581 {
2582 /*
2583 * Add the standard list of commands...
2584 */
2585
2586 ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2587 "printer-commands",
2588 (int)(sizeof(standard_commands) /
2589 sizeof(standard_commands[0])), NULL,
2590 standard_commands);
2591 }
2592 }
2593 else
2594 {
2595 /*
2596 * No commands supported...
2597 */
2598
2599 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2600 "printer-commands", NULL, "none");
2601 }
2602
2603 /*
2604 * Show current and available port monitors for this printer...
2605 */
2606
2607 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME, "port-monitor",
2608 NULL, p->port_monitor ? p->port_monitor : "none");
2609
2610 for (i = 1, ppdattr = ppdFindAttr(ppd, "cupsPortMonitor", NULL);
2611 ppdattr;
2612 i ++, ppdattr = ppdFindNextAttr(ppd, "cupsPortMonitor", NULL));
2613
2614 if (ppd->protocols)
2615 {
2616 if (strstr(ppd->protocols, "TBCP"))
2617 i ++;
2618 else if (strstr(ppd->protocols, "BCP"))
2619 i ++;
2620 }
2621
2622 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
2623 "port-monitor-supported", i, NULL, NULL);
2624
2625 attr->values[0].string.text = _cupsStrAlloc("none");
2626
2627 for (i = 1, ppdattr = ppdFindAttr(ppd, "cupsPortMonitor", NULL);
2628 ppdattr;
2629 i ++, ppdattr = ppdFindNextAttr(ppd, "cupsPortMonitor", NULL))
2630 attr->values[i].string.text = _cupsStrAlloc(ppdattr->value);
2631
2632 if (ppd->protocols)
2633 {
2634 if (strstr(ppd->protocols, "TBCP"))
2635 attr->values[i].string.text = _cupsStrAlloc("tbcp");
2636 else if (strstr(ppd->protocols, "BCP"))
2637 attr->values[i].string.text = _cupsStrAlloc("bcp");
2638 }
2639
2640 #ifdef HAVE_DNSSD
2641 cupsdSetString(&p->product, ppd->product);
2642 #endif /* HAVE_DNSSD */
2643
2644 if (ppdFindAttr(ppd, "APRemoteQueueID", NULL))
2645 p->type |= CUPS_PRINTER_REMOTE;
2646
2647 /*
2648 * Close the PPD and set the type...
2649 */
2650
2651 ppdClose(ppd);
2652 }
2653 else if (!access(filename, 0))
2654 {
2655 int pline; /* PPD line number */
2656 ppd_status_t pstatus; /* PPD load status */
2657
2658
2659 pstatus = ppdLastError(&pline);
2660
2661 cupsdLogMessage(CUPSD_LOG_ERROR, "PPD file for %s cannot be loaded!",
2662 p->name);
2663
2664 if (pstatus <= PPD_ALLOC_ERROR)
2665 cupsdLogMessage(CUPSD_LOG_ERROR, "%s", strerror(errno));
2666 else
2667 cupsdLogMessage(CUPSD_LOG_ERROR, "%s on line %d.",
2668 ppdErrorString(pstatus), pline);
2669
2670 cupsdLogMessage(CUPSD_LOG_INFO,
2671 "Hint: Run \"cupstestppd %s\" and fix any errors.",
2672 filename);
2673
2674 /*
2675 * Add a filter from application/vnd.cups-raw to printer/name to
2676 * handle "raw" printing by users.
2677 */
2678
2679 add_printer_filter(p, p->filetype, "application/vnd.cups-raw 0 -");
2680
2681 /*
2682 * Add a PostScript filter, since this is still possibly PS printer.
2683 */
2684
2685 add_printer_filter(p, p->filetype,
2686 "application/vnd.cups-postscript 0 -");
2687 }
2688 else
2689 {
2690 /*
2691 * If we have an interface script, add a filter entry for it...
2692 */
2693
2694 snprintf(filename, sizeof(filename), "%s/interfaces/%s", ServerRoot,
2695 p->name);
2696 if (!access(filename, X_OK))
2697 {
2698 /*
2699 * Yes, we have a System V style interface script; use it!
2700 */
2701
2702 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
2703 "printer-make-and-model", NULL,
2704 "Local System V Printer");
2705
2706 snprintf(filename, sizeof(filename), "*/* 0 %s/interfaces/%s",
2707 ServerRoot, p->name);
2708 add_printer_filter(p, p->filetype, filename);
2709 }
2710 else if (p->device_uri &&
2711 !strncmp(p->device_uri, "ipp://", 6) &&
2712 (strstr(p->device_uri, "/printers/") != NULL ||
2713 strstr(p->device_uri, "/classes/") != NULL))
2714 {
2715 /*
2716 * Tell the client this is really a hard-wired remote printer.
2717 */
2718
2719 p->type |= CUPS_PRINTER_REMOTE;
2720
2721 /*
2722 * Point the printer-uri-supported attribute to the
2723 * remote printer...
2724 */
2725
2726 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI,
2727 "printer-uri-supported", NULL, p->device_uri);
2728
2729 /*
2730 * Then set the make-and-model accordingly...
2731 */
2732
2733 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
2734 "printer-make-and-model", NULL, "Remote Printer");
2735
2736 /*
2737 * Print all files directly...
2738 */
2739
2740 p->raw = 1;
2741 p->remote = 1;
2742 }
2743 else
2744 {
2745 /*
2746 * Otherwise we have neither - treat this as a "dumb" printer
2747 * with no PPD file...
2748 */
2749
2750 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
2751 "printer-make-and-model", NULL, "Local Raw Printer");
2752
2753 p->raw = 1;
2754 }
2755 }
2756
2757 ippAddIntegers(p->attrs, IPP_TAG_PRINTER, IPP_TAG_ENUM,
2758 "finishings-supported", num_finishings, finishings);
2759 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_ENUM,
2760 "finishings-default", IPP_FINISHINGS_NONE);
2761 }
2762 }
2763
2764 /*
2765 * Copy marker attributes as needed...
2766 */
2767
2768 if (oldattrs)
2769 {
2770 ipp_attribute_t *oldattr; /* Old attribute */
2771
2772
2773 if ((oldattr = ippFindAttribute(oldattrs, "marker-colors",
2774 IPP_TAG_NAME)) != NULL)
2775 {
2776 if ((attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
2777 "marker-colors", oldattr->num_values, NULL,
2778 NULL)) != NULL)
2779 {
2780 for (i = 0; i < oldattr->num_values; i ++)
2781 attr->values[i].string.text =
2782 _cupsStrAlloc(oldattr->values[i].string.text);
2783 }
2784 }
2785
2786 if ((oldattr = ippFindAttribute(oldattrs, "marker-levels",
2787 IPP_TAG_INTEGER)) != NULL)
2788 {
2789 if ((attr = ippAddIntegers(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
2790 "marker-levels", oldattr->num_values,
2791 NULL)) != NULL)
2792 {
2793 for (i = 0; i < oldattr->num_values; i ++)
2794 attr->values[i].integer = oldattr->values[i].integer;
2795 }
2796 }
2797
2798 if ((oldattr = ippFindAttribute(oldattrs, "marker-names",
2799 IPP_TAG_NAME)) != NULL)
2800 {
2801 if ((attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
2802 "marker-names", oldattr->num_values, NULL,
2803 NULL)) != NULL)
2804 {
2805 for (i = 0; i < oldattr->num_values; i ++)
2806 attr->values[i].string.text =
2807 _cupsStrAlloc(oldattr->values[i].string.text);
2808 }
2809 }
2810
2811 if ((oldattr = ippFindAttribute(oldattrs, "marker-types",
2812 IPP_TAG_KEYWORD)) != NULL)
2813 {
2814 if ((attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2815 "marker-types", oldattr->num_values, NULL,
2816 NULL)) != NULL)
2817 {
2818 for (i = 0; i < oldattr->num_values; i ++)
2819 attr->values[i].string.text =
2820 _cupsStrAlloc(oldattr->values[i].string.text);
2821 }
2822 }
2823
2824 ippDelete(oldattrs);
2825 }
2826
2827 /*
2828 * Force sharing off for remote queues...
2829 */
2830
2831 if (p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT))
2832 p->shared = 0;
2833 else
2834 {
2835 /*
2836 * Copy the printer options into a browse attributes string we can re-use.
2837 */
2838
2839 const char *valptr; /* Pointer into value */
2840 char *attrptr; /* Pointer into attribute string */
2841
2842
2843 /*
2844 * Free the old browse attributes as needed...
2845 */
2846
2847 if (p->browse_attrs)
2848 free(p->browse_attrs);
2849
2850 /*
2851 * Compute the length of all attributes + job-sheets, lease-duration,
2852 * and BrowseLocalOptions.
2853 */
2854
2855 for (length = 1, i = p->num_options, option = p->options;
2856 i > 0;
2857 i --, option ++)
2858 {
2859 length += strlen(option->name) + 2;
2860
2861 if (option->value)
2862 {
2863 for (valptr = option->value; *valptr; valptr ++)
2864 if (strchr(" \"\'\\", *valptr))
2865 length += 2;
2866 else
2867 length ++;
2868 }
2869 }
2870
2871 length += 13 + strlen(p->job_sheets[0]) + strlen(p->job_sheets[1]);
2872 length += 32;
2873 if (BrowseLocalOptions)
2874 length += 12 + strlen(BrowseLocalOptions);
2875
2876 if (p->num_auth_info_required > 0)
2877 {
2878 length += 18; /* auth-info-required */
2879
2880 for (i = 0; i < p->num_auth_info_required; i ++)
2881 length += strlen(p->auth_info_required[i]) + 1;
2882 }
2883
2884 /*
2885 * Allocate the new string...
2886 */
2887
2888 if ((p->browse_attrs = calloc(1, length)) == NULL)
2889 cupsdLogMessage(CUPSD_LOG_ERROR,
2890 "Unable to allocate %d bytes for browse data!",
2891 length);
2892 else
2893 {
2894 /*
2895 * Got the allocated string, now copy the options and attributes over...
2896 */
2897
2898 sprintf(p->browse_attrs, "job-sheets=%s,%s lease-duration=%d",
2899 p->job_sheets[0], p->job_sheets[1], BrowseTimeout);
2900 attrptr = p->browse_attrs + strlen(p->browse_attrs);
2901
2902 if (BrowseLocalOptions)
2903 {
2904 sprintf(attrptr, " ipp-options=%s", BrowseLocalOptions);
2905 attrptr += strlen(attrptr);
2906 }
2907
2908 for (i = p->num_options, option = p->options;
2909 i > 0;
2910 i --, option ++)
2911 {
2912 *attrptr++ = ' ';
2913 strcpy(attrptr, option->name);
2914 attrptr += strlen(attrptr);
2915
2916 if (option->value)
2917 {
2918 *attrptr++ = '=';
2919
2920 for (valptr = option->value; *valptr; valptr ++)
2921 {
2922 if (strchr(" \"\'\\", *valptr))
2923 *attrptr++ = '\\';
2924
2925 *attrptr++ = *valptr;
2926 }
2927 }
2928 }
2929
2930 if (p->num_auth_info_required > 0)
2931 {
2932 strcpy(attrptr, "auth-info-required");
2933 attrptr += 18;
2934
2935 for (i = 0; i < p->num_auth_info_required; i ++)
2936 {
2937 *attrptr++ = i ? ',' : '=';
2938 strcpy(attrptr, p->auth_info_required[i]);
2939 attrptr += strlen(attrptr);
2940 }
2941 }
2942 else
2943 *attrptr = '\0';
2944 }
2945 }
2946
2947 /*
2948 * Populate the document-format-supported attribute...
2949 */
2950
2951 add_printer_formats(p);
2952
2953 DEBUG_printf(("cupsdSetPrinterAttrs: leaving name = %s, type = %x\n", p->name,
2954 p->type));
2955
2956 /*
2957 * Add name-default attributes...
2958 */
2959
2960 add_printer_defaults(p);
2961
2962 #ifdef __sgi
2963 /*
2964 * Write the IRIX printer config and status files...
2965 */
2966
2967 write_irix_config(p);
2968 write_irix_state(p);
2969 #endif /* __sgi */
2970
2971 /*
2972 * Let the browse protocols reflect the change
2973 */
2974
2975 cupsdRegisterPrinter(p);
2976 }
2977
2978
2979 /*
2980 * 'cupsdSetPrinterReasons()' - Set/update the reasons strings.
2981 */
2982
2983 void
2984 cupsdSetPrinterReasons(
2985 cupsd_printer_t *p, /* I - Printer */
2986 const char *s) /* I - Reasons strings */
2987 {
2988 int i; /* Looping var */
2989 const char *sptr; /* Pointer into reasons */
2990 char reason[255], /* Reason string */
2991 *rptr; /* Pointer into reason */
2992
2993
2994 if (s[0] == '-' || s[0] == '+')
2995 {
2996 /*
2997 * Add/remove reasons...
2998 */
2999
3000 sptr = s + 1;
3001 }
3002 else
3003 {
3004 /*
3005 * Replace reasons...
3006 */
3007
3008 sptr = s;
3009
3010 for (i = 0; i < p->num_reasons; i ++)
3011 free(p->reasons[i]);
3012
3013 p->num_reasons = 0;
3014 }
3015
3016 if (!strcmp(s, "none"))
3017 return;
3018
3019 /*
3020 * Loop through all of the reasons...
3021 */
3022
3023 while (*sptr)
3024 {
3025 /*
3026 * Skip leading whitespace and commas...
3027 */
3028
3029 while (isspace(*sptr & 255) || *sptr == ',')
3030 sptr ++;
3031
3032 for (rptr = reason; *sptr && !isspace(*sptr & 255) && *sptr != ','; sptr ++)
3033 if (rptr < (reason + sizeof(reason) - 1))
3034 *rptr++ = *sptr;
3035
3036 if (rptr == reason)
3037 break;
3038
3039 *rptr = '\0';
3040
3041 if (s[0] == '-')
3042 {
3043 /*
3044 * Remove reason...
3045 */
3046
3047 for (i = 0; i < p->num_reasons; i ++)
3048 if (!strcasecmp(reason, p->reasons[i]))
3049 {
3050 /*
3051 * Found a match, so remove it...
3052 */
3053
3054 p->num_reasons --;
3055 free(p->reasons[i]);
3056
3057 if (i < p->num_reasons)
3058 memmove(p->reasons + i, p->reasons + i + 1,
3059 (p->num_reasons - i) * sizeof(char *));
3060
3061 i --;
3062
3063 if (!strcmp(reason, "paused") && p->state == IPP_PRINTER_STOPPED)
3064 cupsdSetPrinterState(p, IPP_PRINTER_IDLE, 1);
3065 }
3066 }
3067 else if (p->num_reasons < (int)(sizeof(p->reasons) / sizeof(p->reasons[0])))
3068 {
3069 /*
3070 * Add reason...
3071 */
3072
3073 for (i = 0; i < p->num_reasons; i ++)
3074 if (!strcasecmp(reason, p->reasons[i]))
3075 break;
3076
3077 if (i >= p->num_reasons)
3078 {
3079 p->reasons[i] = strdup(reason);
3080 p->num_reasons ++;
3081
3082 if (!strcmp(reason, "paused") && p->state != IPP_PRINTER_STOPPED)
3083 cupsdSetPrinterState(p, IPP_PRINTER_STOPPED, 1);
3084 }
3085 }
3086 }
3087 }
3088
3089
3090 /*
3091 * 'cupsdSetPrinterState()' - Update the current state of a printer.
3092 */
3093
3094 void
3095 cupsdSetPrinterState(
3096 cupsd_printer_t *p, /* I - Printer to change */
3097 ipp_pstate_t s, /* I - New state */
3098 int update) /* I - Update printers.conf? */
3099 {
3100 ipp_pstate_t old_state; /* Old printer state */
3101
3102
3103 /*
3104 * Can't set status of remote printers...
3105 */
3106
3107 if (p->type & CUPS_PRINTER_DISCOVERED)
3108 return;
3109
3110 /*
3111 * Set the new state...
3112 */
3113
3114 old_state = p->state;
3115 p->state = s;
3116
3117 if (old_state != s)
3118 {
3119 cupsdAddEvent(s == IPP_PRINTER_STOPPED ? CUPSD_EVENT_PRINTER_STOPPED :
3120 CUPSD_EVENT_PRINTER_STATE, p, NULL,
3121 "%s \"%s\" state changed.",
3122 (p->type & CUPS_PRINTER_CLASS) ? "Class" : "Printer",
3123 p->name);
3124
3125 /*
3126 * Let the browse code know this needs to be updated...
3127 */
3128
3129 BrowseNext = p;
3130 p->state_time = time(NULL);
3131 p->browse_time = 0;
3132
3133 #ifdef __sgi
3134 write_irix_state(p);
3135 #endif /* __sgi */
3136 }
3137
3138 cupsdAddPrinterHistory(p);
3139
3140 /*
3141 * Let the browse protocols reflect the change...
3142 */
3143
3144 if (update)
3145 cupsdRegisterPrinter(p);
3146
3147 /*
3148 * Save the printer configuration if a printer goes from idle or processing
3149 * to stopped (or visa-versa)...
3150 */
3151
3152 if ((old_state == IPP_PRINTER_STOPPED) != (s == IPP_PRINTER_STOPPED) &&
3153 update)
3154 {
3155 if (p->type & CUPS_PRINTER_CLASS)
3156 cupsdMarkDirty(CUPSD_DIRTY_CLASSES);
3157 else
3158 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
3159 }
3160 }
3161
3162
3163 /*
3164 * 'cupsdStopPrinter()' - Stop a printer from printing any jobs...
3165 */
3166
3167 void
3168 cupsdStopPrinter(cupsd_printer_t *p, /* I - Printer to stop */
3169 int update)/* I - Update printers.conf? */
3170 {
3171 cupsd_job_t *job; /* Active print job */
3172
3173
3174 /*
3175 * Set the printer state...
3176 */
3177
3178 cupsdSetPrinterState(p, IPP_PRINTER_STOPPED, update);
3179
3180 /*
3181 * See if we have a job printing on this printer...
3182 */
3183
3184 if (p->job)
3185 {
3186 /*
3187 * Get pointer to job...
3188 */
3189
3190 job = (cupsd_job_t *)p->job;
3191
3192 /*
3193 * Stop it...
3194 */
3195
3196 cupsdStopJob(job, 0);
3197
3198 /*
3199 * Reset the state to pending...
3200 */
3201
3202 job->state->values[0].integer = IPP_JOB_PENDING;
3203 job->state_value = IPP_JOB_PENDING;
3204 job->dirty = 1;
3205
3206 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
3207
3208 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED, p, job,
3209 "Job stopped due to printer being paused");
3210 }
3211 }
3212
3213
3214 /*
3215 * 'cupsdUpdatePrinterPPD()' - Update keywords in a printer's PPD file.
3216 */
3217
3218 int /* O - 1 if successful, 0 otherwise */
3219 cupsdUpdatePrinterPPD(
3220 cupsd_printer_t *p, /* I - Printer */
3221 int num_keywords, /* I - Number of keywords */
3222 cups_option_t *keywords) /* I - Keywords */
3223 {
3224 int i; /* Looping var */
3225 cups_file_t *src, /* Original file */
3226 *dst; /* New file */
3227 char srcfile[1024], /* Original filename */
3228 dstfile[1024], /* New filename */
3229 line[1024], /* Line from file */
3230 keystring[41]; /* Keyword from line */
3231 cups_option_t *keyword; /* Current keyword */
3232
3233
3234 cupsdLogMessage(CUPSD_LOG_INFO, "Updating keywords in PPD file for %s...",
3235 p->name);
3236
3237 /*
3238 * Get the old and new PPD filenames...
3239 */
3240
3241 snprintf(srcfile, sizeof(srcfile), "%s/ppd/%s.ppd.O", ServerRoot, p->name);
3242 snprintf(dstfile, sizeof(srcfile), "%s/ppd/%s.ppd", ServerRoot, p->name);
3243
3244 /*
3245 * Rename the old file and open the old and new...
3246 */
3247
3248 if (rename(dstfile, srcfile))
3249 {
3250 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to backup PPD file for %s: %s",
3251 p->name, strerror(errno));
3252 return (0);
3253 }
3254
3255 if ((src = cupsFileOpen(srcfile, "r")) == NULL)
3256 {
3257 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to open PPD file \"%s\": %s",
3258 srcfile, strerror(errno));
3259 rename(srcfile, dstfile);
3260 return (0);
3261 }
3262
3263 if ((dst = cupsFileOpen(dstfile, "w")) == NULL)
3264 {
3265 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create PPD file \"%s\": %s",
3266 dstfile, strerror(errno));
3267 cupsFileClose(src);
3268 rename(srcfile, dstfile);
3269 return (0);
3270 }
3271
3272 /*
3273 * Copy the first line and then write out all of the keywords...
3274 */
3275
3276 if (!cupsFileGets(src, line, sizeof(line)))
3277 {
3278 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to read PPD file \"%s\": %s",
3279 srcfile, strerror(errno));
3280 cupsFileClose(src);
3281 cupsFileClose(dst);
3282 rename(srcfile, dstfile);
3283 return (0);
3284 }
3285
3286 cupsFilePrintf(dst, "%s\n", line);
3287
3288 for (i = num_keywords, keyword = keywords; i > 0; i --, keyword ++)
3289 {
3290 cupsdLogMessage(CUPSD_LOG_DEBUG, "*%s: %s", keyword->name, keyword->value);
3291 cupsFilePrintf(dst, "*%s: %s\n", keyword->name, keyword->value);
3292 }
3293
3294 /*
3295 * Then copy the rest of the PPD file, dropping any keywords we changed.
3296 */
3297
3298 while (cupsFileGets(src, line, sizeof(line)))
3299 {
3300 /*
3301 * Skip keywords we've already set...
3302 */
3303
3304 if (sscanf(line, "*%40[^:]:", keystring) == 1 &&
3305 cupsGetOption(keystring, num_keywords, keywords))
3306 continue;
3307
3308 /*
3309 * Otherwise write the line...
3310 */
3311
3312 cupsFilePrintf(dst, "%s\n", line);
3313 }
3314
3315 /*
3316 * Close files and return...
3317 */
3318
3319 cupsFileClose(src);
3320 cupsFileClose(dst);
3321
3322 return (1);
3323 }
3324
3325
3326 /*
3327 * 'cupsdUpdatePrinters()' - Update printers after a partial reload.
3328 */
3329
3330 void
3331 cupsdUpdatePrinters(void)
3332 {
3333 cupsd_printer_t *p; /* Current printer */
3334
3335
3336 /*
3337 * Loop through the printers and recreate the printer attributes
3338 * for any local printers since the policy and/or access control
3339 * stuff may have changed. Also, if browsing is disabled, remove
3340 * any remote printers...
3341 */
3342
3343 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
3344 p;
3345 p = (cupsd_printer_t *)cupsArrayNext(Printers))
3346 {
3347 /*
3348 * Remove remote printers if we are no longer browsing...
3349 */
3350
3351 if (!Browsing &&
3352 (p->type & (CUPS_PRINTER_IMPLICIT | CUPS_PRINTER_DISCOVERED)))
3353 {
3354 if (p->type & CUPS_PRINTER_IMPLICIT)
3355 cupsArrayRemove(ImplicitPrinters, p);
3356
3357 cupsArraySave(Printers);
3358 cupsdDeletePrinter(p, 0);
3359 cupsArrayRestore(Printers);
3360 continue;
3361 }
3362
3363 /*
3364 * Update the operation policy pointer...
3365 */
3366
3367 if ((p->op_policy_ptr = cupsdFindPolicy(p->op_policy)) == NULL)
3368 p->op_policy_ptr = DefaultPolicyPtr;
3369
3370 /*
3371 * Update printer attributes as needed...
3372 */
3373
3374 if (!(p->type & CUPS_PRINTER_DISCOVERED))
3375 cupsdSetPrinterAttrs(p);
3376 }
3377 }
3378
3379
3380 /*
3381 * 'cupsdValidateDest()' - Validate a printer/class destination.
3382 */
3383
3384 const char * /* O - Printer or class name */
3385 cupsdValidateDest(
3386 const char *uri, /* I - Printer URI */
3387 cups_ptype_t *dtype, /* O - Type (printer or class) */
3388 cupsd_printer_t **printer) /* O - Printer pointer */
3389 {
3390 cupsd_printer_t *p; /* Current printer */
3391 char localname[1024],/* Localized hostname */
3392 *lptr, /* Pointer into localized hostname */
3393 *sptr, /* Pointer into server name */
3394 *rptr, /* Pointer into resource */
3395 scheme[32], /* Scheme portion of URI */
3396 username[64], /* Username portion of URI */
3397 hostname[HTTP_MAX_HOST],
3398 /* Host portion of URI */
3399 resource[HTTP_MAX_URI];
3400 /* Resource portion of URI */
3401 int port; /* Port portion of URI */
3402
3403
3404 DEBUG_printf(("cupsdValidateDest(uri=\"%s\", dtype=%p, printer=%p)\n", uri,
3405 dtype, printer));
3406
3407 /*
3408 * Initialize return values...
3409 */
3410
3411 if (printer)
3412 *printer = NULL;
3413
3414 if (dtype)
3415 *dtype = (cups_ptype_t)0;
3416
3417 /*
3418 * Pull the hostname and resource from the URI...
3419 */
3420
3421 httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, sizeof(scheme),
3422 username, sizeof(username), hostname, sizeof(hostname),
3423 &port, resource, sizeof(resource));
3424
3425 /*
3426 * See if the resource is a class or printer...
3427 */
3428
3429 if (!strncmp(resource, "/classes/", 9))
3430 {
3431 /*
3432 * Class...
3433 */
3434
3435 rptr = resource + 9;
3436 }
3437 else if (!strncmp(resource, "/printers/", 10))
3438 {
3439 /*
3440 * Printer...
3441 */
3442
3443 rptr = resource + 10;
3444 }
3445 else
3446 {
3447 /*
3448 * Bad resource name...
3449 */
3450
3451 return (NULL);
3452 }
3453
3454 /*
3455 * See if the printer or class name exists...
3456 */
3457
3458 p = cupsdFindDest(rptr);
3459
3460 if (p == NULL && strchr(rptr, '@') == NULL)
3461 return (NULL);
3462 else if (p != NULL)
3463 {
3464 if (printer)
3465 *printer = p;
3466
3467 if (dtype)
3468 *dtype = p->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT |
3469 CUPS_PRINTER_REMOTE | CUPS_PRINTER_DISCOVERED);
3470
3471 return (p->name);
3472 }
3473
3474 /*
3475 * Change localhost to the server name...
3476 */
3477
3478 if (!strcasecmp(hostname, "localhost"))
3479 strlcpy(hostname, ServerName, sizeof(hostname));
3480
3481 strlcpy(localname, hostname, sizeof(localname));
3482
3483 if (!strcasecmp(hostname, ServerName))
3484 {
3485 /*
3486 * Localize the hostname...
3487 */
3488
3489 lptr = strchr(localname, '.');
3490 sptr = strchr(ServerName, '.');
3491
3492 if (sptr != NULL && lptr != NULL)
3493 {
3494 /*
3495 * Strip the common domain name components...
3496 */
3497
3498 while (lptr != NULL)
3499 {
3500 if (!strcasecmp(lptr, sptr))
3501 {
3502 *lptr = '\0';
3503 break;
3504 }
3505 else
3506 lptr = strchr(lptr + 1, '.');
3507 }
3508 }
3509 }
3510
3511 DEBUG_printf(("localized hostname is \"%s\"...\n", localname));
3512
3513 /*
3514 * Find a matching printer or class...
3515 */
3516
3517 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
3518 p;
3519 p = (cupsd_printer_t *)cupsArrayNext(Printers))
3520 if (!strcasecmp(p->hostname, localname) &&
3521 !strcasecmp(p->name, rptr))
3522 {
3523 if (printer)
3524 *printer = p;
3525
3526 if (dtype)
3527 *dtype = p->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT |
3528 CUPS_PRINTER_REMOTE | CUPS_PRINTER_DISCOVERED);
3529
3530 return (p->name);
3531 }
3532
3533 return (NULL);
3534 }
3535
3536
3537 /*
3538 * 'cupsdWritePrintcap()' - Write a pseudo-printcap file for older applications
3539 * that need it...
3540 */
3541
3542 void
3543 cupsdWritePrintcap(void)
3544 {
3545 cups_file_t *fp; /* printcap file */
3546 cupsd_printer_t *p; /* Current printer */
3547
3548
3549 #ifdef __sgi
3550 /*
3551 * Update the IRIX printer state for the default printer; if
3552 * no printers remain, then the default printer file will be
3553 * removed...
3554 */
3555
3556 write_irix_state(DefaultPrinter);
3557 #endif /* __sgi */
3558
3559 /*
3560 * See if we have a printcap file; if not, don't bother writing it.
3561 */
3562
3563 if (!Printcap || !*Printcap)
3564 return;
3565
3566 /*
3567 * Open the printcap file...
3568 */
3569
3570 if ((fp = cupsFileOpen(Printcap, "w")) == NULL)
3571 return;
3572
3573 /*
3574 * Put a comment header at the top so that users will know where the
3575 * data has come from...
3576 */
3577
3578 cupsFilePuts(fp,
3579 "# This file was automatically generated by cupsd(8) from the\n");
3580 cupsFilePrintf(fp, "# %s/printers.conf file. All changes to this file\n",
3581 ServerRoot);
3582 cupsFilePuts(fp, "# will be lost.\n");
3583
3584 if (Printers)
3585 {
3586 /*
3587 * Write a new printcap with the current list of printers.
3588 */
3589
3590 switch (PrintcapFormat)
3591 {
3592 case PRINTCAP_BSD:
3593 /*
3594 * Each printer is put in the file as:
3595 *
3596 * Printer1:
3597 * Printer2:
3598 * Printer3:
3599 * ...
3600 * PrinterN:
3601 */
3602
3603 if (DefaultPrinter)
3604 cupsFilePrintf(fp, "%s|%s:rm=%s:rp=%s:\n", DefaultPrinter->name,
3605 DefaultPrinter->info, ServerName,
3606 DefaultPrinter->name);
3607
3608 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
3609 p;
3610 p = (cupsd_printer_t *)cupsArrayNext(Printers))
3611 if (p != DefaultPrinter)
3612 cupsFilePrintf(fp, "%s|%s:rm=%s:rp=%s:\n", p->name, p->info,
3613 ServerName, p->name);
3614 break;
3615
3616 case PRINTCAP_SOLARIS:
3617 /*
3618 * Each printer is put in the file as:
3619 *
3620 * _all:all=Printer1,Printer2,Printer3,...,PrinterN
3621 * _default:use=DefaultPrinter
3622 * Printer1:\
3623 * :bsdaddr=ServerName,Printer1:\
3624 * :description=Description:
3625 * Printer2:
3626 * :bsdaddr=ServerName,Printer2:\
3627 * :description=Description:
3628 * Printer3:
3629 * :bsdaddr=ServerName,Printer3:\
3630 * :description=Description:
3631 * ...
3632 * PrinterN:
3633 * :bsdaddr=ServerName,PrinterN:\
3634 * :description=Description:
3635 */
3636
3637 cupsFilePuts(fp, "_all:all=");
3638 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
3639 p;
3640 p = (cupsd_printer_t *)cupsArrayCurrent(Printers))
3641 cupsFilePrintf(fp, "%s%c", p->name,
3642 cupsArrayNext(Printers) ? ',' : '\n');
3643
3644 if (DefaultPrinter)
3645 cupsFilePrintf(fp, "_default:use=%s\n", DefaultPrinter->name);
3646
3647 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
3648 p;
3649 p = (cupsd_printer_t *)cupsArrayNext(Printers))
3650 cupsFilePrintf(fp, "%s:\\\n"
3651 "\t:bsdaddr=%s,%s:\\\n"
3652 "\t:description=%s:\n",
3653 p->name, ServerName, p->name,
3654 p->info ? p->info : "");
3655 break;
3656 }
3657 }
3658
3659 /*
3660 * Close the file...
3661 */
3662
3663 cupsFileClose(fp);
3664 }
3665
3666
3667 /*
3668 * 'cupsdSanitizeURI()' - Sanitize a device URI...
3669 */
3670
3671 char * /* O - New device URI */
3672 cupsdSanitizeURI(const char *uri, /* I - Original device URI */
3673 char *buffer, /* O - New device URI */
3674 int buflen) /* I - Size of new device URI buffer */
3675 {
3676 char *start, /* Start of data after scheme */
3677 *slash, /* First slash after scheme:// */
3678 *ptr; /* Pointer into user@host:port part */
3679
3680
3681 /*
3682 * Range check input...
3683 */
3684
3685 if (!uri || !buffer || buflen < 2)
3686 return (NULL);
3687
3688 /*
3689 * Copy the device URI to the new buffer...
3690 */
3691
3692 strlcpy(buffer, uri, buflen);
3693
3694 /*
3695 * Find the end of the scheme:// part...
3696 */
3697
3698 if ((ptr = strchr(buffer, ':')) == NULL)
3699 return (buffer); /* No scheme: part... */
3700
3701 for (start = ptr + 1; *start; start ++)
3702 if (*start != '/')
3703 break;
3704
3705 /*
3706 * Find the next slash (/) in the URI...
3707 */
3708
3709 if ((slash = strchr(start, '/')) == NULL)
3710 slash = start + strlen(start); /* No slash, point to the end */
3711
3712 /*
3713 * Check for an @ sign before the slash...
3714 */
3715
3716 if ((ptr = strchr(start, '@')) != NULL && ptr < slash)
3717 {
3718 /*
3719 * Found an @ sign and it is before the resource part, so we have
3720 * an authentication string. Copy the remaining URI over the
3721 * authentication string...
3722 */
3723
3724 _cups_strcpy(start, ptr + 1);
3725 }
3726
3727 /*
3728 * Return the new device URI...
3729 */
3730
3731 return (buffer);
3732 }
3733
3734
3735 /*
3736 * 'add_printer_defaults()' - Add name-default attributes to the printer attributes.
3737 */
3738
3739 static void
3740 add_printer_defaults(cupsd_printer_t *p)/* I - Printer */
3741 {
3742 int i; /* Looping var */
3743 int num_options; /* Number of default options */
3744 cups_option_t *options, /* Default options */
3745 *option; /* Current option */
3746 char name[256]; /* name-default */
3747
3748
3749 /*
3750 * Maintain a common array of default attribute names...
3751 */
3752
3753 if (!CommonDefaults)
3754 {
3755 CommonDefaults = cupsArrayNew((cups_array_func_t)strcmp, NULL);
3756
3757 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("copies-default"));
3758 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("document-format-default"));
3759 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("finishings-default"));
3760 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("job-hold-until-default"));
3761 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("job-priority-default"));
3762 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("job-sheets-default"));
3763 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("media-default"));
3764 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("number-up-default"));
3765 cupsArrayAdd(CommonDefaults,
3766 _cupsStrAlloc("orientation-requested-default"));
3767 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("sides-default"));
3768 }
3769
3770 /*
3771 * Add all of the default options from the .conf files...
3772 */
3773
3774 for (num_options = 0, options = NULL, i = p->num_options, option = p->options;
3775 i > 0;
3776 i --, option ++)
3777 {
3778 if (strcmp(option->name, "ipp-options") &&
3779 strcmp(option->name, "job-sheets") &&
3780 strcmp(option->name, "lease-duration"))
3781 {
3782 snprintf(name, sizeof(name), "%s-default", option->name);
3783 num_options = cupsAddOption(name, option->value, num_options, &options);
3784
3785 if (!cupsArrayFind(CommonDefaults, name))
3786 cupsArrayAdd(CommonDefaults, _cupsStrAlloc(name));
3787 }
3788 }
3789
3790 /*
3791 * Convert options to IPP attributes...
3792 */
3793
3794 cupsEncodeOptions2(p->attrs, num_options, options, IPP_TAG_PRINTER);
3795 cupsFreeOptions(num_options, options);
3796
3797 /*
3798 * Add standard -default attributes as needed...
3799 */
3800
3801 if (!cupsGetOption("copies", p->num_options, p->options))
3802 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER, "copies-default",
3803 1);
3804
3805 if (!cupsGetOption("document-format", p->num_options, p->options))
3806 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_MIMETYPE,
3807 "document-format-default", NULL, "application/octet-stream");
3808
3809 if (!cupsGetOption("job-hold-until", p->num_options, p->options))
3810 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
3811 "job-hold-until-default", NULL, "no-hold");
3812
3813 if (!cupsGetOption("job-priority", p->num_options, p->options))
3814 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
3815 "job-priority-default", 50);
3816
3817 if (!cupsGetOption("number-up", p->num_options, p->options))
3818 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
3819 "number-up-default", 1);
3820
3821 if (!cupsGetOption("orientation-requested", p->num_options, p->options))
3822 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NOVALUE,
3823 "orientation-requested-default", NULL, NULL);
3824
3825 if (!cupsGetOption("notify-lease-duration", p->num_options, p->options))
3826 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
3827 "notify-lease-duration-default", DefaultLeaseDuration);
3828
3829 if (!cupsGetOption("notify-events", p->num_options, p->options))
3830 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
3831 "notify-events-default", NULL, "job-completed");
3832 }
3833
3834
3835 /*
3836 * 'add_printer_filter()' - Add a MIME filter for a printer.
3837 */
3838
3839 static void
3840 add_printer_filter(
3841 cupsd_printer_t *p, /* I - Printer to add to */
3842 mime_type_t *filtertype, /* I - Filter or prefilter MIME type */
3843 const char *filter) /* I - Filter to add */
3844 {
3845 char super[MIME_MAX_SUPER], /* Super-type for filter */
3846 type[MIME_MAX_TYPE], /* Type for filter */
3847 program[1024]; /* Program/filter name */
3848 int cost; /* Cost of filter */
3849 mime_type_t *temptype; /* MIME type looping var */
3850 char filename[1024]; /* Full filter filename */
3851
3852
3853 /*
3854 * Parse the filter string; it should be in the following format:
3855 *
3856 * super/type cost program
3857 */
3858
3859 if (sscanf(filter, "%15[^/]/%31s%d%*[ \t]%1023[^\n]", super, type, &cost,
3860 program) != 4)
3861 {
3862 cupsdLogMessage(CUPSD_LOG_ERROR, "%s: invalid filter string \"%s\"!",
3863 p->name, filter);
3864 return;
3865 }
3866
3867 /*
3868 * See if the filter program exists; if not, stop the printer and flag
3869 * the error!
3870 */
3871
3872 if (strcmp(program, "-"))
3873 {
3874 if (program[0] == '/')
3875 strlcpy(filename, program, sizeof(filename));
3876 else
3877 snprintf(filename, sizeof(filename), "%s/filter/%s", ServerBin, program);
3878
3879 if (access(filename, X_OK))
3880 {
3881 snprintf(p->state_message, sizeof(p->state_message),
3882 "Filter \"%s\" for printer \"%s\" not available: %s",
3883 program, p->name, strerror(errno));
3884 cupsdSetPrinterReasons(p, "+cups-missing-filter-error");
3885 cupsdSetPrinterState(p, IPP_PRINTER_STOPPED, 0);
3886
3887 cupsdLogMessage(CUPSD_LOG_ERROR, "%s", p->state_message);
3888 }
3889 }
3890
3891 /*
3892 * Mark the CUPS_PRINTER_COMMANDS bit if we have a filter for
3893 * application/vnd.cups-command...
3894 */
3895
3896 if (!strcasecmp(super, "application") &&
3897 !strcasecmp(type, "vnd.cups-command"))
3898 p->type |= CUPS_PRINTER_COMMANDS;
3899
3900 /*
3901 * Add the filter to the MIME database, supporting wildcards as needed...
3902 */
3903
3904 for (temptype = mimeFirstType(MimeDatabase);
3905 temptype;
3906 temptype = mimeNextType(MimeDatabase))
3907 if (((super[0] == '*' && strcasecmp(temptype->super, "printer")) ||
3908 !strcasecmp(temptype->super, super)) &&
3909 (type[0] == '*' || !strcasecmp(temptype->type, type)))
3910 {
3911 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3912 "add_printer_filter: %s: adding filter %s/%s %s/%s %d %s",
3913 p->name, temptype->super, temptype->type,
3914 filtertype->super, filtertype->type,
3915 cost, program);
3916 mimeAddFilter(MimeDatabase, temptype, filtertype, cost, program);
3917 }
3918 }
3919
3920
3921 /*
3922 * 'add_printer_formats()' - Add document-format-supported values for a printer.
3923 */
3924
3925 static void
3926 add_printer_formats(cupsd_printer_t *p) /* I - Printer */
3927 {
3928 int i; /* Looping var */
3929 mime_type_t *type; /* Current MIME type */
3930 cups_array_t *filters; /* Filters */
3931 ipp_attribute_t *attr; /* document-format-supported attribute */
3932 char mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2];
3933 /* MIME type name */
3934
3935
3936 /*
3937 * Raw (and remote) queues advertise all of the supported MIME
3938 * types...
3939 */
3940
3941 cupsArrayDelete(p->filetypes);
3942 p->filetypes = NULL;
3943
3944 if (p->raw)
3945 {
3946 ippAddStrings(p->attrs, IPP_TAG_PRINTER,
3947 (ipp_tag_t)(IPP_TAG_MIMETYPE | IPP_TAG_COPY),
3948 "document-format-supported", NumMimeTypes, NULL, MimeTypes);
3949 return;
3950 }
3951
3952 /*
3953 * Otherwise, loop through the supported MIME types and see if there
3954 * are filters for them...
3955 */
3956
3957 cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_printer_formats: %d types, %d filters",
3958 mimeNumTypes(MimeDatabase), mimeNumFilters(MimeDatabase));
3959
3960 p->filetypes = cupsArrayNew(NULL, NULL);
3961
3962 for (type = mimeFirstType(MimeDatabase);
3963 type;
3964 type = mimeNextType(MimeDatabase))
3965 {
3966 snprintf(mimetype, sizeof(mimetype), "%s/%s", type->super, type->type);
3967
3968 if ((filters = mimeFilter(MimeDatabase, type, p->filetype, NULL)) != NULL)
3969 {
3970 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3971 "add_printer_formats: %s: %s needs %d filters",
3972 p->name, mimetype, cupsArrayCount(filters));
3973
3974 cupsArrayDelete(filters);
3975 cupsArrayAdd(p->filetypes, type);
3976 }
3977 else
3978 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3979 "add_printer_formats: %s: %s not supported",
3980 p->name, mimetype);
3981 }
3982
3983 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3984 "add_printer_formats: %s: %d supported types",
3985 p->name, cupsArrayCount(p->filetypes) + 1);
3986
3987 /*
3988 * Add the file formats that can be filtered...
3989 */
3990
3991 if ((type = mimeType(MimeDatabase, "application", "octet-stream")) == NULL ||
3992 !cupsArrayFind(p->filetypes, type))
3993 i = 1;
3994 else
3995 i = 0;
3996
3997 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_MIMETYPE,
3998 "document-format-supported",
3999 cupsArrayCount(p->filetypes) + 1, NULL, NULL);
4000
4001 if (i)
4002 attr->values[0].string.text = _cupsStrAlloc("application/octet-stream");
4003
4004 for (type = (mime_type_t *)cupsArrayFirst(p->filetypes);
4005 type;
4006 i ++, type = (mime_type_t *)cupsArrayNext(p->filetypes))
4007 {
4008 snprintf(mimetype, sizeof(mimetype), "%s/%s", type->super, type->type);
4009
4010 attr->values[i].string.text = _cupsStrAlloc(mimetype);
4011 }
4012
4013 #ifdef HAVE_DNSSD
4014 {
4015 char pdl[1024]; /* Buffer to build pdl list */
4016 mime_filter_t *filter; /* MIME filter looping var */
4017
4018
4019 pdl[0] = '\0';
4020
4021 if (mimeType(MimeDatabase, "application", "pdf"))
4022 strlcat(pdl, "application/pdf,", sizeof(pdl));
4023
4024 if (mimeType(MimeDatabase, "application", "postscript"))
4025 strlcat(pdl, "application/postscript,", sizeof(pdl));
4026
4027 if (mimeType(MimeDatabase, "application", "vnd.cups-raster"))
4028 strlcat(pdl, "application/vnd.cups-raster,", sizeof(pdl));
4029
4030 /*
4031 * Determine if this is a Tioga PrintJobMgr based queue...
4032 */
4033
4034 for (filter = (mime_filter_t *)cupsArrayFirst(MimeDatabase->filters);
4035 filter;
4036 filter = (mime_filter_t *)cupsArrayNext(MimeDatabase->filters))
4037 {
4038 if (filter->dst == p->filetype && filter->filter &&
4039 strstr(filter->filter, "PrintJobMgr"))
4040 break;
4041 }
4042
4043 /*
4044 * We only support raw printing if this is not a Tioga PrintJobMgr based
4045 * queue and if application/octet-stream is a known conversion...
4046 */
4047
4048 if (!filter && mimeType(MimeDatabase, "application", "octet-stream"))
4049 strlcat(pdl, "application/octet-stream,", sizeof(pdl));
4050
4051 if (mimeType(MimeDatabase, "image", "png"))
4052 strlcat(pdl, "image/png,", sizeof(pdl));
4053
4054 if (pdl[0])
4055 pdl[strlen(pdl) - 1] = '\0'; /* Remove trailing comma */
4056
4057 cupsdSetString(&p->pdl, pdl);
4058 }
4059 #endif /* HAVE_DNSSD */
4060 }
4061
4062
4063 /*
4064 * 'compare_printers()' - Compare two printers.
4065 */
4066
4067 static int /* O - Result of comparison */
4068 compare_printers(void *first, /* I - First printer */
4069 void *second, /* I - Second printer */
4070 void *data) /* I - App data (not used) */
4071 {
4072 return (strcasecmp(((cupsd_printer_t *)first)->name,
4073 ((cupsd_printer_t *)second)->name));
4074 }
4075
4076
4077 /*
4078 * 'delete_printer_filters()' - Delete all MIME filters for a printer.
4079 */
4080
4081 static void
4082 delete_printer_filters(
4083 cupsd_printer_t *p) /* I - Printer to remove from */
4084 {
4085 mime_filter_t *filter; /* MIME filter looping var */
4086
4087
4088 /*
4089 * Range check input...
4090 */
4091
4092 if (p == NULL)
4093 return;
4094
4095 /*
4096 * Remove all filters from the MIME database that have a destination
4097 * type == printer...
4098 */
4099
4100 for (filter = mimeFirstFilter(MimeDatabase);
4101 filter;
4102 filter = mimeNextFilter(MimeDatabase))
4103 if (filter->dst == p->filetype)
4104 {
4105 /*
4106 * Delete the current filter...
4107 */
4108
4109 mimeDeleteFilter(MimeDatabase, filter);
4110 }
4111 }
4112
4113
4114 #ifdef __sgi
4115 /*
4116 * 'write_irix_config()' - Update the config files used by the IRIX
4117 * desktop tools.
4118 */
4119
4120 static void
4121 write_irix_config(cupsd_printer_t *p) /* I - Printer to update */
4122 {
4123 char filename[1024]; /* Interface script filename */
4124 cups_file_t *fp; /* Interface script file */
4125 ipp_attribute_t *attr; /* Attribute data */
4126
4127
4128 /*
4129 * Add dummy interface and GUI scripts to fool SGI's "challenged" printing
4130 * tools. First the interface script that tells the tools what kind of
4131 * printer we have...
4132 */
4133
4134 snprintf(filename, sizeof(filename), "/var/spool/lp/interface/%s", p->name);
4135
4136 if (p->type & CUPS_PRINTER_CLASS)
4137 unlink(filename);
4138 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
4139 {
4140 cupsFilePuts(fp, "#!/bin/sh\n");
4141
4142 if ((attr = ippFindAttribute(p->attrs, "printer-make-and-model",
4143 IPP_TAG_TEXT)) != NULL)
4144 cupsFilePrintf(fp, "NAME=\"%s\"\n", attr->values[0].string.text);
4145 else if (p->type & CUPS_PRINTER_CLASS)
4146 cupsFilePuts(fp, "NAME=\"Printer Class\"\n");
4147 else
4148 cupsFilePuts(fp, "NAME=\"Remote Destination\"\n");
4149
4150 if (p->type & CUPS_PRINTER_COLOR)
4151 cupsFilePuts(fp, "TYPE=ColorPostScript\n");
4152 else
4153 cupsFilePuts(fp, "TYPE=MonoPostScript\n");
4154
4155 cupsFilePrintf(fp, "HOSTNAME=%s\n", ServerName);
4156 cupsFilePrintf(fp, "HOSTPRINTER=%s\n", p->name);
4157
4158 cupsFileClose(fp);
4159
4160 chmod(filename, 0755);
4161 chown(filename, User, Group);
4162 }
4163
4164 /*
4165 * Then the member file that tells which device file the queue is connected
4166 * to... Networked printers use "/dev/null" in this file, so that's what
4167 * we use (the actual device URI can confuse some apps...)
4168 */
4169
4170 snprintf(filename, sizeof(filename), "/var/spool/lp/member/%s", p->name);
4171
4172 if (p->type & CUPS_PRINTER_CLASS)
4173 unlink(filename);
4174 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
4175 {
4176 cupsFilePuts(fp, "/dev/null\n");
4177
4178 cupsFileClose(fp);
4179
4180 chmod(filename, 0644);
4181 chown(filename, User, Group);
4182 }
4183
4184 /*
4185 * The gui_interface file is a script or program that launches a GUI
4186 * option panel for the printer, using options specified on the
4187 * command-line in the third argument. The option panel must send
4188 * any printing options to stdout on a single line when the user
4189 * accepts them, or nothing if the user cancels the dialog.
4190 *
4191 * The default options panel program is /usr/bin/glpoptions, from
4192 * the ESP Print Pro software. You can select another using the
4193 * PrintcapGUI option.
4194 */
4195
4196 snprintf(filename, sizeof(filename), "/var/spool/lp/gui_interface/ELF/%s.gui", p->name);
4197
4198 if (p->type & CUPS_PRINTER_CLASS)
4199 unlink(filename);
4200 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
4201 {
4202 cupsFilePuts(fp, "#!/bin/sh\n");
4203 cupsFilePrintf(fp, "%s -d %s -o \"$3\"\n", PrintcapGUI, p->name);
4204
4205 cupsFileClose(fp);
4206
4207 chmod(filename, 0755);
4208 chown(filename, User, Group);
4209 }
4210
4211 /*
4212 * The POD config file is needed by the printstatus command to show
4213 * the printer location and device.
4214 */
4215
4216 snprintf(filename, sizeof(filename), "/var/spool/lp/pod/%s.config", p->name);
4217
4218 if (p->type & CUPS_PRINTER_CLASS)
4219 unlink(filename);
4220 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
4221 {
4222 cupsFilePrintf(fp, "Printer Class | %s\n",
4223 (p->type & CUPS_PRINTER_COLOR) ? "ColorPostScript" : "MonoPostScript");
4224 cupsFilePrintf(fp, "Printer Model | %s\n", p->make_model ? p->make_model : "");
4225 cupsFilePrintf(fp, "Location Code | %s\n", p->location ? p->location : "");
4226 cupsFilePrintf(fp, "Physical Location | %s\n", p->info ? p->info : "");
4227 cupsFilePrintf(fp, "Port Path | %s\n", p->device_uri ? p->device_uri : "");
4228 cupsFilePrintf(fp, "Config Path | /var/spool/lp/pod/%s.config\n", p->name);
4229 cupsFilePrintf(fp, "Active Status Path | /var/spool/lp/pod/%s.status\n", p->name);
4230 cupsFilePuts(fp, "Status Update Wait | 10 seconds\n");
4231
4232 cupsFileClose(fp);
4233
4234 chmod(filename, 0664);
4235 chown(filename, User, Group);
4236 }
4237 }
4238
4239
4240 /*
4241 * 'write_irix_state()' - Update the status files used by IRIX printing
4242 * desktop tools.
4243 */
4244
4245 static void
4246 write_irix_state(cupsd_printer_t *p) /* I - Printer to update */
4247 {
4248 char filename[1024]; /* Interface script filename */
4249 cups_file_t *fp; /* Interface script file */
4250 int tag; /* Status tag value */
4251
4252
4253 if (p)
4254 {
4255 /*
4256 * The POD status file is needed for the printstatus window to
4257 * provide the current status of the printer.
4258 */
4259
4260 snprintf(filename, sizeof(filename), "/var/spool/lp/pod/%s.status", p->name);
4261
4262 if (p->type & CUPS_PRINTER_CLASS)
4263 unlink(filename);
4264 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
4265 {
4266 cupsFilePrintf(fp, "Operational Status | %s\n",
4267 (p->state == IPP_PRINTER_IDLE) ? "Idle" :
4268 (p->state == IPP_PRINTER_PROCESSING) ? "Busy" :
4269 "Faulted");
4270 cupsFilePrintf(fp, "Information | 01 00 00 | %s\n", CUPS_SVERSION);
4271 cupsFilePrintf(fp, "Information | 02 00 00 | Device URI: %s\n",
4272 p->device_uri ? p->device_uri : "");
4273 cupsFilePrintf(fp, "Information | 03 00 00 | %s jobs\n",
4274 p->accepting ? "Accepting" : "Not accepting");
4275 cupsFilePrintf(fp, "Information | 04 00 00 | %s\n", p->state_message);
4276
4277 cupsFileClose(fp);
4278
4279 chmod(filename, 0664);
4280 chown(filename, User, Group);
4281 }
4282
4283 /*
4284 * The activeicons file is needed to provide desktop icons for printers:
4285 *
4286 * [ quoted from /usr/lib/print/tagit ]
4287 *
4288 * --- Type of printer tags (base values)
4289 *
4290 * Dumb=66048 # 0x10200
4291 * DumbColor=66080 # 0x10220
4292 * Raster=66112 # 0x10240
4293 * ColorRaster=66144 # 0x10260
4294 * Plotter=66176 # 0x10280
4295 * PostScript=66208 # 0x102A0
4296 * ColorPostScript=66240 # 0x102C0
4297 * MonoPostScript=66272 # 0x102E0
4298 *
4299 * --- Printer state modifiers for local printers
4300 *
4301 * Idle=0 # 0x0
4302 * Busy=1 # 0x1
4303 * Faulted=2 # 0x2
4304 * Unknown=3 # 0x3 (Faulted due to unknown reason)
4305 *
4306 * --- Printer state modifiers for network printers
4307 *
4308 * NetIdle=8 # 0x8
4309 * NetBusy=9 # 0x9
4310 * NetFaulted=10 # 0xA
4311 * NetUnknown=11 # 0xB (Faulted due to unknown reason)
4312 */
4313
4314 snprintf(filename, sizeof(filename), "/var/spool/lp/activeicons/%s", p->name);
4315
4316 if (p->type & CUPS_PRINTER_CLASS)
4317 unlink(filename);
4318 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
4319 {
4320 if (p->type & CUPS_PRINTER_COLOR)
4321 tag = 66240;
4322 else
4323 tag = 66272;
4324
4325 if (p->type & CUPS_PRINTER_REMOTE)
4326 tag |= 8;
4327
4328 if (p->state == IPP_PRINTER_PROCESSING)
4329 tag |= 1;
4330
4331 else if (p->state == IPP_PRINTER_STOPPED)
4332 tag |= 2;
4333
4334 cupsFilePuts(fp, "#!/bin/sh\n");
4335 cupsFilePrintf(fp, "#Tag %d\n", tag);
4336
4337 cupsFileClose(fp);
4338
4339 chmod(filename, 0755);
4340 chown(filename, User, Group);
4341 }
4342 }
4343
4344 /*
4345 * The default file is needed by the printers window to show
4346 * the default printer.
4347 */
4348
4349 snprintf(filename, sizeof(filename), "/var/spool/lp/default");
4350
4351 if (DefaultPrinter != NULL)
4352 {
4353 if ((fp = cupsFileOpen(filename, "w")) != NULL)
4354 {
4355 cupsFilePrintf(fp, "%s\n", DefaultPrinter->name);
4356
4357 cupsFileClose(fp);
4358
4359 chmod(filename, 0644);
4360 chown(filename, User, Group);
4361 }
4362 }
4363 else
4364 unlink(filename);
4365 }
4366 #endif /* __sgi */
4367
4368
4369 /*
4370 * End of "$Id: printers.c 7879 2008-08-28 00:08:56Z mike $".
4371 */