]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/printers.c
Merge changes from CUPS 1.4svn-r7874.
[thirdparty/cups.git] / scheduler / printers.c
1 /*
2 * "$Id: printers.c 7677 2008-06-19 23:22:19Z 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 if (auth->type == CUPSD_AUTH_BASIC || auth->type == CUPSD_AUTH_BASICDIGEST)
2061 {
2062 auth_supported = "basic";
2063 num_air = 2;
2064 air = air_userpass;
2065 }
2066 else if (auth->type == CUPSD_AUTH_DIGEST)
2067 {
2068 auth_supported = "digest";
2069 num_air = 2;
2070 air = air_userpass;
2071 }
2072 #ifdef HAVE_GSSAPI
2073 else if (auth->type == CUPSD_AUTH_NEGOTIATE)
2074 {
2075 auth_supported = "negotiate";
2076 num_air = 1;
2077 air = air_negotiate;
2078 }
2079 #endif /* HAVE_GSSAPI */
2080
2081 if (auth->type != CUPSD_AUTH_NONE)
2082 p->type |= CUPS_PRINTER_AUTHENTICATED;
2083 else
2084 p->type &= ~CUPS_PRINTER_AUTHENTICATED;
2085 }
2086 else
2087 p->type &= ~CUPS_PRINTER_AUTHENTICATED;
2088 }
2089 else if (p->type & CUPS_PRINTER_AUTHENTICATED)
2090 {
2091 num_air = 2;
2092 air = air_userpass;
2093 }
2094
2095 /*
2096 * Create the required IPP attributes for a printer...
2097 */
2098
2099 oldattrs = p->attrs;
2100 p->attrs = ippNew();
2101
2102 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2103 "uri-authentication-supported", NULL, auth_supported);
2104 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2105 "uri-security-supported", NULL, "none");
2106 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME, "printer-name", NULL,
2107 p->name);
2108 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-location",
2109 NULL, p->location ? p->location : "");
2110 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-info",
2111 NULL, p->info ? p->info : "");
2112 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI, "printer-more-info",
2113 NULL, p->uri);
2114
2115 if (p->num_users)
2116 {
2117 if (p->deny_users)
2118 ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
2119 "requesting-user-name-denied", p->num_users, NULL,
2120 p->users);
2121 else
2122 ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
2123 "requesting-user-name-allowed", p->num_users, NULL,
2124 p->users);
2125 }
2126
2127 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
2128 "job-quota-period", p->quota_period);
2129 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
2130 "job-k-limit", p->k_limit);
2131 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
2132 "job-page-limit", p->page_limit);
2133 ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2134 "auth-info-required", num_air, NULL, air);
2135
2136 if (cupsArrayCount(Banners) > 0 && !(p->type & CUPS_PRINTER_DISCOVERED))
2137 {
2138 /*
2139 * Setup the job-sheets-default attribute...
2140 */
2141
2142 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
2143 "job-sheets-default", 2, NULL, NULL);
2144
2145 if (attr != NULL)
2146 {
2147 attr->values[0].string.text = _cupsStrAlloc(Classification ?
2148 Classification : p->job_sheets[0]);
2149 attr->values[1].string.text = _cupsStrAlloc(Classification ?
2150 Classification : p->job_sheets[1]);
2151 }
2152 }
2153
2154 p->raw = 0;
2155 p->remote = 0;
2156
2157 if (p->type & CUPS_PRINTER_DISCOVERED)
2158 {
2159 /*
2160 * Tell the client this is a remote printer of some type...
2161 */
2162
2163 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI,
2164 "printer-uri-supported", NULL, p->uri);
2165
2166 if (p->make_model)
2167 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
2168 "printer-make-and-model", NULL, p->make_model);
2169
2170 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI, "device-uri", NULL,
2171 p->uri);
2172
2173 p->raw = 1;
2174 p->remote = 1;
2175 }
2176 else
2177 {
2178 /*
2179 * Assign additional attributes depending on whether this is a printer
2180 * or class...
2181 */
2182
2183 p->type &= ~CUPS_PRINTER_OPTIONS;
2184
2185 if (p->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT))
2186 {
2187 p->raw = 1;
2188
2189 /*
2190 * Add class-specific attributes...
2191 */
2192
2193 if ((p->type & CUPS_PRINTER_IMPLICIT) && p->num_printers > 0 &&
2194 p->printers[0]->make_model)
2195 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
2196 "printer-make-and-model", NULL, p->printers[0]->make_model);
2197 else
2198 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
2199 "printer-make-and-model", NULL, "Local Printer Class");
2200
2201 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI, "device-uri", NULL,
2202 "file:///dev/null");
2203
2204 if (p->num_printers > 0)
2205 {
2206 /*
2207 * Add a list of member names; URIs are added in copy_printer_attrs...
2208 */
2209
2210 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
2211 "member-names", p->num_printers, NULL, NULL);
2212 p->type |= CUPS_PRINTER_OPTIONS;
2213
2214 for (i = 0; i < p->num_printers; i ++)
2215 {
2216 if (attr != NULL)
2217 attr->values[i].string.text = _cupsStrAlloc(p->printers[i]->name);
2218
2219 p->type &= ~CUPS_PRINTER_OPTIONS | p->printers[i]->type;
2220 }
2221 }
2222 }
2223 else
2224 {
2225 /*
2226 * Add printer-specific attributes... Start by sanitizing the device
2227 * URI so it doesn't have a username or password in it...
2228 */
2229
2230 if (!p->device_uri)
2231 strcpy(uri, "file:/dev/null");
2232 else if (strstr(p->device_uri, "://") != NULL)
2233 {
2234 /*
2235 * http://..., ipp://..., etc.
2236 */
2237
2238 cupsdSanitizeURI(p->device_uri, uri, sizeof(uri));
2239 }
2240 else
2241 {
2242 /*
2243 * file:..., serial:..., etc.
2244 */
2245
2246 strlcpy(uri, p->device_uri, sizeof(uri));
2247 }
2248
2249 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI, "device-uri", NULL,
2250 uri);
2251
2252 /*
2253 * Assign additional attributes from the PPD file (if any)...
2254 */
2255
2256 p->type |= CUPS_PRINTER_BW;
2257 finishings[0] = IPP_FINISHINGS_NONE;
2258 num_finishings = 1;
2259
2260 snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot,
2261 p->name);
2262
2263 if ((ppd = ppdOpenFile(filename)) != NULL)
2264 {
2265 /*
2266 * Add make/model and other various attributes...
2267 */
2268
2269 if (ppd->color_device)
2270 p->type |= CUPS_PRINTER_COLOR;
2271 if (ppd->variable_sizes)
2272 p->type |= CUPS_PRINTER_VARIABLE;
2273 if (!ppd->manual_copies)
2274 p->type |= CUPS_PRINTER_COPIES;
2275 if ((ppdattr = ppdFindAttr(ppd, "cupsFax", NULL)) != NULL)
2276 if (ppdattr->value && !strcasecmp(ppdattr->value, "true"))
2277 p->type |= CUPS_PRINTER_FAX;
2278
2279 ippAddBoolean(p->attrs, IPP_TAG_PRINTER, "color-supported",
2280 ppd->color_device);
2281 if (ppd->throughput)
2282 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
2283 "pages-per-minute", ppd->throughput);
2284
2285 if (ppd->nickname)
2286 {
2287 /*
2288 * The NickName can be localized in the character set specified
2289 * by the LanugageEncoding attribute. However, ppdOpen2() has
2290 * already converted the ppd->nickname member to UTF-8 for us
2291 * (the original attribute value is available separately)
2292 */
2293
2294 cupsdSetString(&p->make_model, ppd->nickname);
2295 }
2296 else if (ppd->modelname)
2297 {
2298 /*
2299 * Model name can only contain specific characters...
2300 */
2301
2302 cupsdSetString(&p->make_model, ppd->modelname);
2303 }
2304 else
2305 cupsdSetString(&p->make_model, "Bad PPD File");
2306
2307 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
2308 "printer-make-and-model", NULL, p->make_model);
2309
2310 /*
2311 * Add media options from the PPD file...
2312 */
2313
2314 if ((input_slot = ppdFindOption(ppd, "InputSlot")) != NULL)
2315 num_media = input_slot->num_choices;
2316 else
2317 num_media = 0;
2318
2319 if ((media_type = ppdFindOption(ppd, "MediaType")) != NULL)
2320 num_media += media_type->num_choices;
2321
2322 if ((page_size = ppdFindOption(ppd, "PageSize")) != NULL)
2323 num_media += page_size->num_choices;
2324
2325 if ((media_quality = ppdFindOption(ppd, "EFMediaQualityMode")) != NULL)
2326 num_media += media_quality->num_choices;
2327
2328 if (num_media == 0)
2329 {
2330 cupsdLogMessage(CUPSD_LOG_CRIT,
2331 "The PPD file for printer %s contains no media "
2332 "options and is therefore invalid!", p->name);
2333 }
2334 else
2335 {
2336 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2337 "media-supported", num_media, NULL, NULL);
2338 if (attr != NULL)
2339 {
2340 val = attr->values;
2341
2342 if (input_slot != NULL)
2343 for (i = 0; i < input_slot->num_choices; i ++, val ++)
2344 val->string.text = _cupsStrAlloc(input_slot->choices[i].choice);
2345
2346 if (media_type != NULL)
2347 for (i = 0; i < media_type->num_choices; i ++, val ++)
2348 val->string.text = _cupsStrAlloc(media_type->choices[i].choice);
2349
2350 if (media_quality != NULL)
2351 for (i = 0; i < media_quality->num_choices; i ++, val ++)
2352 val->string.text = _cupsStrAlloc(media_quality->choices[i].choice);
2353
2354 if (page_size != NULL)
2355 {
2356 for (i = 0; i < page_size->num_choices; i ++, val ++)
2357 val->string.text = _cupsStrAlloc(page_size->choices[i].choice);
2358
2359 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2360 "media-default", NULL, page_size->defchoice);
2361 }
2362 else if (input_slot != NULL)
2363 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2364 "media-default", NULL, input_slot->defchoice);
2365 else if (media_type != NULL)
2366 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2367 "media-default", NULL, media_type->defchoice);
2368 else if (media_quality != NULL)
2369 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2370 "media-default", NULL, media_quality->defchoice);
2371 else
2372 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2373 "media-default", NULL, "none");
2374 }
2375 }
2376
2377 /*
2378 * Output bin...
2379 */
2380
2381 if ((output_bin = ppdFindOption(ppd, "OutputBin")) != NULL)
2382 {
2383 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2384 "output-bin-supported", output_bin->num_choices,
2385 NULL, NULL);
2386
2387 if (attr != NULL)
2388 {
2389 for (i = 0, val = attr->values;
2390 i < output_bin->num_choices;
2391 i ++, val ++)
2392 val->string.text = _cupsStrAlloc(output_bin->choices[i].choice);
2393 }
2394 }
2395
2396 /*
2397 * Duplexing, etc...
2398 */
2399
2400 if ((duplex = ppdFindOption(ppd, "Duplex")) == NULL)
2401 if ((duplex = ppdFindOption(ppd, "EFDuplex")) == NULL)
2402 if ((duplex = ppdFindOption(ppd, "EFDuplexing")) == NULL)
2403 if ((duplex = ppdFindOption(ppd, "KD03Duplex")) == NULL)
2404 duplex = ppdFindOption(ppd, "JCLDuplex");
2405
2406 if (duplex && duplex->num_choices > 1 &&
2407 !ppdInstallableConflict(ppd, duplex->keyword, "DuplexTumble"))
2408 {
2409 p->type |= CUPS_PRINTER_DUPLEX;
2410
2411 ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2412 "sides-supported", 3, NULL, sides);
2413
2414 if (!strcasecmp(duplex->defchoice, "DuplexTumble"))
2415 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2416 "sides-default", NULL, "two-sided-short-edge");
2417 else if (!strcasecmp(duplex->defchoice, "DuplexNoTumble"))
2418 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2419 "sides-default", NULL, "two-sided-long-edge");
2420 else
2421 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2422 "sides-default", NULL, "one-sided");
2423 }
2424
2425 if (ppdFindOption(ppd, "Collate") != NULL)
2426 p->type |= CUPS_PRINTER_COLLATE;
2427
2428 if (ppdFindOption(ppd, "StapleLocation") != NULL)
2429 {
2430 p->type |= CUPS_PRINTER_STAPLE;
2431 finishings[num_finishings++] = IPP_FINISHINGS_STAPLE;
2432 }
2433
2434 if (ppdFindOption(ppd, "BindEdge") != NULL)
2435 {
2436 p->type |= CUPS_PRINTER_BIND;
2437 finishings[num_finishings++] = IPP_FINISHINGS_BIND;
2438 }
2439
2440 for (i = 0; i < ppd->num_sizes; i ++)
2441 if (ppd->sizes[i].length > 1728)
2442 p->type |= CUPS_PRINTER_LARGE;
2443 else if (ppd->sizes[i].length > 1008)
2444 p->type |= CUPS_PRINTER_MEDIUM;
2445 else
2446 p->type |= CUPS_PRINTER_SMALL;
2447
2448 /*
2449 * Add a filter from application/vnd.cups-raw to printer/name to
2450 * handle "raw" printing by users.
2451 */
2452
2453 add_printer_filter(p, p->filetype, "application/vnd.cups-raw 0 -");
2454
2455 /*
2456 * Add any pre-filters in the PPD file...
2457 */
2458
2459 if ((ppdattr = ppdFindAttr(ppd, "cupsPreFilter", NULL)) != NULL)
2460 {
2461 p->prefiltertype = mimeAddType(MimeDatabase, "prefilter", p->name);
2462
2463 for (; ppdattr; ppdattr = ppdFindNextAttr(ppd, "cupsPreFilter", NULL))
2464 if (ppdattr->value)
2465 add_printer_filter(p, p->prefiltertype, ppdattr->value);
2466 }
2467
2468 /*
2469 * Add any filters in the PPD file...
2470 */
2471
2472 DEBUG_printf(("ppd->num_filters = %d\n", ppd->num_filters));
2473 for (i = 0; i < ppd->num_filters; i ++)
2474 {
2475 DEBUG_printf(("ppd->filters[%d] = \"%s\"\n", i, ppd->filters[i]));
2476 add_printer_filter(p, p->filetype, ppd->filters[i]);
2477 }
2478
2479 if (ppd->num_filters == 0)
2480 {
2481 /*
2482 * If there are no filters, add PostScript printing filters.
2483 */
2484
2485 add_printer_filter(p, p->filetype,
2486 "application/vnd.cups-command 0 commandtops");
2487 add_printer_filter(p, p->filetype,
2488 "application/vnd.cups-postscript 0 -");
2489
2490 p->type |= CUPS_PRINTER_COMMANDS;
2491 }
2492 else if (!(p->type & CUPS_PRINTER_COMMANDS))
2493 {
2494 /*
2495 * See if this is a PostScript device without a command filter...
2496 */
2497
2498 for (i = 0; i < ppd->num_filters; i ++)
2499 if (!strncasecmp(ppd->filters[i],
2500 "application/vnd.cups-postscript", 31))
2501 break;
2502
2503 if (i < ppd->num_filters)
2504 {
2505 /*
2506 * Add the generic PostScript command filter...
2507 */
2508
2509 add_printer_filter(p, p->filetype,
2510 "application/vnd.cups-command 0 commandtops");
2511 p->type |= CUPS_PRINTER_COMMANDS;
2512 }
2513 }
2514
2515 if (p->type & CUPS_PRINTER_COMMANDS)
2516 {
2517 char *commands, /* Copy of commands */
2518 *start, /* Start of name */
2519 *end; /* End of name */
2520 int count; /* Number of commands */
2521
2522
2523 if ((ppdattr = ppdFindAttr(ppd, "cupsCommands", NULL)) != NULL &&
2524 ppdattr->value && ppdattr->value[0])
2525 {
2526 for (count = 0, start = ppdattr->value; *start; count ++)
2527 {
2528 while (isspace(*start & 255))
2529 start ++;
2530
2531 if (!*start)
2532 break;
2533
2534 while (*start && !isspace(*start & 255))
2535 start ++;
2536 }
2537 }
2538 else
2539 count = 0;
2540
2541 if (count > 0)
2542 {
2543 /*
2544 * Make a copy of the commands string and count how many ...
2545 */
2546
2547 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2548 "printer-commands", count, NULL, NULL);
2549
2550 commands = strdup(ppdattr->value);
2551
2552 for (count = 0, start = commands; *start; count ++)
2553 {
2554 while (isspace(*start & 255))
2555 start ++;
2556
2557 if (!*start)
2558 break;
2559
2560 end = start;
2561 while (*end && !isspace(*end & 255))
2562 end ++;
2563
2564 if (*end)
2565 *end++ = '\0';
2566
2567 attr->values[count].string.text = _cupsStrAlloc(start);
2568
2569 start = end;
2570 }
2571
2572 free(commands);
2573 }
2574 else
2575 {
2576 /*
2577 * Add the standard list of commands...
2578 */
2579
2580 ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2581 "printer-commands",
2582 (int)(sizeof(standard_commands) /
2583 sizeof(standard_commands[0])), NULL,
2584 standard_commands);
2585 }
2586 }
2587 else
2588 {
2589 /*
2590 * No commands supported...
2591 */
2592
2593 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2594 "printer-commands", NULL, "none");
2595 }
2596
2597 /*
2598 * Show current and available port monitors for this printer...
2599 */
2600
2601 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME, "port-monitor",
2602 NULL, p->port_monitor ? p->port_monitor : "none");
2603
2604 for (i = 1, ppdattr = ppdFindAttr(ppd, "cupsPortMonitor", NULL);
2605 ppdattr;
2606 i ++, ppdattr = ppdFindNextAttr(ppd, "cupsPortMonitor", NULL));
2607
2608 if (ppd->protocols)
2609 {
2610 if (strstr(ppd->protocols, "TBCP"))
2611 i ++;
2612 else if (strstr(ppd->protocols, "BCP"))
2613 i ++;
2614 }
2615
2616 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
2617 "port-monitor-supported", i, NULL, NULL);
2618
2619 attr->values[0].string.text = _cupsStrAlloc("none");
2620
2621 for (i = 1, ppdattr = ppdFindAttr(ppd, "cupsPortMonitor", NULL);
2622 ppdattr;
2623 i ++, ppdattr = ppdFindNextAttr(ppd, "cupsPortMonitor", NULL))
2624 attr->values[i].string.text = _cupsStrAlloc(ppdattr->value);
2625
2626 if (ppd->protocols)
2627 {
2628 if (strstr(ppd->protocols, "TBCP"))
2629 attr->values[i].string.text = _cupsStrAlloc("tbcp");
2630 else if (strstr(ppd->protocols, "BCP"))
2631 attr->values[i].string.text = _cupsStrAlloc("bcp");
2632 }
2633
2634 #ifdef HAVE_DNSSD
2635 cupsdSetString(&p->product, ppd->product);
2636 #endif /* HAVE_DNSSD */
2637
2638 if (ppdFindAttr(ppd, "APRemoteQueueID", NULL))
2639 p->type |= CUPS_PRINTER_REMOTE;
2640
2641 /*
2642 * Close the PPD and set the type...
2643 */
2644
2645 ppdClose(ppd);
2646 }
2647 else if (!access(filename, 0))
2648 {
2649 int pline; /* PPD line number */
2650 ppd_status_t pstatus; /* PPD load status */
2651
2652
2653 pstatus = ppdLastError(&pline);
2654
2655 cupsdLogMessage(CUPSD_LOG_ERROR, "PPD file for %s cannot be loaded!",
2656 p->name);
2657
2658 if (pstatus <= PPD_ALLOC_ERROR)
2659 cupsdLogMessage(CUPSD_LOG_ERROR, "%s", strerror(errno));
2660 else
2661 cupsdLogMessage(CUPSD_LOG_ERROR, "%s on line %d.",
2662 ppdErrorString(pstatus), pline);
2663
2664 cupsdLogMessage(CUPSD_LOG_INFO,
2665 "Hint: Run \"cupstestppd %s\" and fix any errors.",
2666 filename);
2667
2668 /*
2669 * Add a filter from application/vnd.cups-raw to printer/name to
2670 * handle "raw" printing by users.
2671 */
2672
2673 add_printer_filter(p, p->filetype, "application/vnd.cups-raw 0 -");
2674
2675 /*
2676 * Add a PostScript filter, since this is still possibly PS printer.
2677 */
2678
2679 add_printer_filter(p, p->filetype,
2680 "application/vnd.cups-postscript 0 -");
2681 }
2682 else
2683 {
2684 /*
2685 * If we have an interface script, add a filter entry for it...
2686 */
2687
2688 snprintf(filename, sizeof(filename), "%s/interfaces/%s", ServerRoot,
2689 p->name);
2690 if (!access(filename, X_OK))
2691 {
2692 /*
2693 * Yes, we have a System V style interface script; use it!
2694 */
2695
2696 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
2697 "printer-make-and-model", NULL,
2698 "Local System V Printer");
2699
2700 snprintf(filename, sizeof(filename), "*/* 0 %s/interfaces/%s",
2701 ServerRoot, p->name);
2702 add_printer_filter(p, p->filetype, filename);
2703 }
2704 else if (p->device_uri &&
2705 !strncmp(p->device_uri, "ipp://", 6) &&
2706 (strstr(p->device_uri, "/printers/") != NULL ||
2707 strstr(p->device_uri, "/classes/") != NULL))
2708 {
2709 /*
2710 * Tell the client this is really a hard-wired remote printer.
2711 */
2712
2713 p->type |= CUPS_PRINTER_REMOTE;
2714
2715 /*
2716 * Point the printer-uri-supported attribute to the
2717 * remote printer...
2718 */
2719
2720 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI,
2721 "printer-uri-supported", NULL, p->device_uri);
2722
2723 /*
2724 * Then set the make-and-model accordingly...
2725 */
2726
2727 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
2728 "printer-make-and-model", NULL, "Remote Printer");
2729
2730 /*
2731 * Print all files directly...
2732 */
2733
2734 p->raw = 1;
2735 p->remote = 1;
2736 }
2737 else
2738 {
2739 /*
2740 * Otherwise we have neither - treat this as a "dumb" printer
2741 * with no PPD file...
2742 */
2743
2744 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
2745 "printer-make-and-model", NULL, "Local Raw Printer");
2746
2747 p->raw = 1;
2748 }
2749 }
2750
2751 ippAddIntegers(p->attrs, IPP_TAG_PRINTER, IPP_TAG_ENUM,
2752 "finishings-supported", num_finishings, finishings);
2753 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_ENUM,
2754 "finishings-default", IPP_FINISHINGS_NONE);
2755 }
2756 }
2757
2758 /*
2759 * Copy marker attributes as needed...
2760 */
2761
2762 if (oldattrs)
2763 {
2764 ipp_attribute_t *oldattr; /* Old attribute */
2765
2766
2767 if ((oldattr = ippFindAttribute(oldattrs, "marker-colors",
2768 IPP_TAG_NAME)) != NULL)
2769 {
2770 if ((attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
2771 "marker-colors", oldattr->num_values, NULL,
2772 NULL)) != NULL)
2773 {
2774 for (i = 0; i < oldattr->num_values; i ++)
2775 attr->values[i].string.text =
2776 _cupsStrAlloc(oldattr->values[i].string.text);
2777 }
2778 }
2779
2780 if ((oldattr = ippFindAttribute(oldattrs, "marker-levels",
2781 IPP_TAG_INTEGER)) != NULL)
2782 {
2783 if ((attr = ippAddIntegers(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
2784 "marker-levels", oldattr->num_values,
2785 NULL)) != NULL)
2786 {
2787 for (i = 0; i < oldattr->num_values; i ++)
2788 attr->values[i].integer = oldattr->values[i].integer;
2789 }
2790 }
2791
2792 if ((oldattr = ippFindAttribute(oldattrs, "marker-names",
2793 IPP_TAG_NAME)) != NULL)
2794 {
2795 if ((attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
2796 "marker-names", oldattr->num_values, NULL,
2797 NULL)) != NULL)
2798 {
2799 for (i = 0; i < oldattr->num_values; i ++)
2800 attr->values[i].string.text =
2801 _cupsStrAlloc(oldattr->values[i].string.text);
2802 }
2803 }
2804
2805 if ((oldattr = ippFindAttribute(oldattrs, "marker-types",
2806 IPP_TAG_KEYWORD)) != NULL)
2807 {
2808 if ((attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2809 "marker-types", oldattr->num_values, NULL,
2810 NULL)) != NULL)
2811 {
2812 for (i = 0; i < oldattr->num_values; i ++)
2813 attr->values[i].string.text =
2814 _cupsStrAlloc(oldattr->values[i].string.text);
2815 }
2816 }
2817
2818 ippDelete(oldattrs);
2819 }
2820
2821 /*
2822 * Force sharing off for remote queues...
2823 */
2824
2825 if (p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT))
2826 p->shared = 0;
2827 else
2828 {
2829 /*
2830 * Copy the printer options into a browse attributes string we can re-use.
2831 */
2832
2833 const char *valptr; /* Pointer into value */
2834 char *attrptr; /* Pointer into attribute string */
2835
2836
2837 /*
2838 * Free the old browse attributes as needed...
2839 */
2840
2841 if (p->browse_attrs)
2842 free(p->browse_attrs);
2843
2844 /*
2845 * Compute the length of all attributes + job-sheets, lease-duration,
2846 * and BrowseLocalOptions.
2847 */
2848
2849 for (length = 1, i = p->num_options, option = p->options;
2850 i > 0;
2851 i --, option ++)
2852 {
2853 length += strlen(option->name) + 2;
2854
2855 if (option->value)
2856 {
2857 for (valptr = option->value; *valptr; valptr ++)
2858 if (strchr(" \"\'\\", *valptr))
2859 length += 2;
2860 else
2861 length ++;
2862 }
2863 }
2864
2865 length += 13 + strlen(p->job_sheets[0]) + strlen(p->job_sheets[1]);
2866 length += 32;
2867 if (BrowseLocalOptions)
2868 length += 12 + strlen(BrowseLocalOptions);
2869
2870 if (p->num_auth_info_required > 0)
2871 {
2872 length += 18; /* auth-info-required */
2873
2874 for (i = 0; i < p->num_auth_info_required; i ++)
2875 length += strlen(p->auth_info_required[i]) + 1;
2876 }
2877
2878 /*
2879 * Allocate the new string...
2880 */
2881
2882 if ((p->browse_attrs = calloc(1, length)) == NULL)
2883 cupsdLogMessage(CUPSD_LOG_ERROR,
2884 "Unable to allocate %d bytes for browse data!",
2885 length);
2886 else
2887 {
2888 /*
2889 * Got the allocated string, now copy the options and attributes over...
2890 */
2891
2892 sprintf(p->browse_attrs, "job-sheets=%s,%s lease-duration=%d",
2893 p->job_sheets[0], p->job_sheets[1], BrowseTimeout);
2894 attrptr = p->browse_attrs + strlen(p->browse_attrs);
2895
2896 if (BrowseLocalOptions)
2897 {
2898 sprintf(attrptr, " ipp-options=%s", BrowseLocalOptions);
2899 attrptr += strlen(attrptr);
2900 }
2901
2902 for (i = p->num_options, option = p->options;
2903 i > 0;
2904 i --, option ++)
2905 {
2906 *attrptr++ = ' ';
2907 strcpy(attrptr, option->name);
2908 attrptr += strlen(attrptr);
2909
2910 if (option->value)
2911 {
2912 *attrptr++ = '=';
2913
2914 for (valptr = option->value; *valptr; valptr ++)
2915 {
2916 if (strchr(" \"\'\\", *valptr))
2917 *attrptr++ = '\\';
2918
2919 *attrptr++ = *valptr;
2920 }
2921 }
2922 }
2923
2924 if (p->num_auth_info_required > 0)
2925 {
2926 strcpy(attrptr, "auth-info-required");
2927 attrptr += 18;
2928
2929 for (i = 0; i < p->num_auth_info_required; i ++)
2930 {
2931 *attrptr++ = i ? ',' : '=';
2932 strcpy(attrptr, p->auth_info_required[i]);
2933 attrptr += strlen(attrptr);
2934 }
2935 }
2936 else
2937 *attrptr = '\0';
2938 }
2939 }
2940
2941 /*
2942 * Populate the document-format-supported attribute...
2943 */
2944
2945 add_printer_formats(p);
2946
2947 DEBUG_printf(("cupsdSetPrinterAttrs: leaving name = %s, type = %x\n", p->name,
2948 p->type));
2949
2950 /*
2951 * Add name-default attributes...
2952 */
2953
2954 add_printer_defaults(p);
2955
2956 #ifdef __sgi
2957 /*
2958 * Write the IRIX printer config and status files...
2959 */
2960
2961 write_irix_config(p);
2962 write_irix_state(p);
2963 #endif /* __sgi */
2964
2965 /*
2966 * Let the browse protocols reflect the change
2967 */
2968
2969 cupsdRegisterPrinter(p);
2970 }
2971
2972
2973 /*
2974 * 'cupsdSetPrinterReasons()' - Set/update the reasons strings.
2975 */
2976
2977 void
2978 cupsdSetPrinterReasons(
2979 cupsd_printer_t *p, /* I - Printer */
2980 const char *s) /* I - Reasons strings */
2981 {
2982 int i; /* Looping var */
2983 const char *sptr; /* Pointer into reasons */
2984 char reason[255], /* Reason string */
2985 *rptr; /* Pointer into reason */
2986
2987
2988 if (s[0] == '-' || s[0] == '+')
2989 {
2990 /*
2991 * Add/remove reasons...
2992 */
2993
2994 sptr = s + 1;
2995 }
2996 else
2997 {
2998 /*
2999 * Replace reasons...
3000 */
3001
3002 sptr = s;
3003
3004 for (i = 0; i < p->num_reasons; i ++)
3005 free(p->reasons[i]);
3006
3007 p->num_reasons = 0;
3008 }
3009
3010 if (!strcmp(s, "none"))
3011 return;
3012
3013 /*
3014 * Loop through all of the reasons...
3015 */
3016
3017 while (*sptr)
3018 {
3019 /*
3020 * Skip leading whitespace and commas...
3021 */
3022
3023 while (isspace(*sptr & 255) || *sptr == ',')
3024 sptr ++;
3025
3026 for (rptr = reason; *sptr && !isspace(*sptr & 255) && *sptr != ','; sptr ++)
3027 if (rptr < (reason + sizeof(reason) - 1))
3028 *rptr++ = *sptr;
3029
3030 if (rptr == reason)
3031 break;
3032
3033 *rptr = '\0';
3034
3035 if (s[0] == '-')
3036 {
3037 /*
3038 * Remove reason...
3039 */
3040
3041 for (i = 0; i < p->num_reasons; i ++)
3042 if (!strcasecmp(reason, p->reasons[i]))
3043 {
3044 /*
3045 * Found a match, so remove it...
3046 */
3047
3048 p->num_reasons --;
3049 free(p->reasons[i]);
3050
3051 if (i < p->num_reasons)
3052 memmove(p->reasons + i, p->reasons + i + 1,
3053 (p->num_reasons - i) * sizeof(char *));
3054
3055 i --;
3056
3057 if (!strcmp(reason, "paused") && p->state == IPP_PRINTER_STOPPED)
3058 cupsdSetPrinterState(p, IPP_PRINTER_IDLE, 1);
3059 }
3060 }
3061 else if (p->num_reasons < (int)(sizeof(p->reasons) / sizeof(p->reasons[0])))
3062 {
3063 /*
3064 * Add reason...
3065 */
3066
3067 for (i = 0; i < p->num_reasons; i ++)
3068 if (!strcasecmp(reason, p->reasons[i]))
3069 break;
3070
3071 if (i >= p->num_reasons)
3072 {
3073 p->reasons[i] = strdup(reason);
3074 p->num_reasons ++;
3075
3076 if (!strcmp(reason, "paused") && p->state != IPP_PRINTER_STOPPED)
3077 cupsdSetPrinterState(p, IPP_PRINTER_STOPPED, 1);
3078 }
3079 }
3080 }
3081 }
3082
3083
3084 /*
3085 * 'cupsdSetPrinterState()' - Update the current state of a printer.
3086 */
3087
3088 void
3089 cupsdSetPrinterState(
3090 cupsd_printer_t *p, /* I - Printer to change */
3091 ipp_pstate_t s, /* I - New state */
3092 int update) /* I - Update printers.conf? */
3093 {
3094 ipp_pstate_t old_state; /* Old printer state */
3095
3096
3097 /*
3098 * Can't set status of remote printers...
3099 */
3100
3101 if (p->type & CUPS_PRINTER_DISCOVERED)
3102 return;
3103
3104 /*
3105 * Set the new state...
3106 */
3107
3108 old_state = p->state;
3109 p->state = s;
3110
3111 if (old_state != s)
3112 {
3113 cupsdAddEvent(s == IPP_PRINTER_STOPPED ? CUPSD_EVENT_PRINTER_STOPPED :
3114 CUPSD_EVENT_PRINTER_STATE, p, NULL,
3115 "%s \"%s\" state changed.",
3116 (p->type & CUPS_PRINTER_CLASS) ? "Class" : "Printer",
3117 p->name);
3118
3119 /*
3120 * Let the browse code know this needs to be updated...
3121 */
3122
3123 BrowseNext = p;
3124 p->state_time = time(NULL);
3125 p->browse_time = 0;
3126
3127 #ifdef __sgi
3128 write_irix_state(p);
3129 #endif /* __sgi */
3130 }
3131
3132 cupsdAddPrinterHistory(p);
3133
3134 /*
3135 * Let the browse protocols reflect the change...
3136 */
3137
3138 if (update)
3139 cupsdRegisterPrinter(p);
3140
3141 /*
3142 * Save the printer configuration if a printer goes from idle or processing
3143 * to stopped (or visa-versa)...
3144 */
3145
3146 if ((old_state == IPP_PRINTER_STOPPED) != (s == IPP_PRINTER_STOPPED) &&
3147 update)
3148 {
3149 if (p->type & CUPS_PRINTER_CLASS)
3150 cupsdMarkDirty(CUPSD_DIRTY_CLASSES);
3151 else
3152 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
3153 }
3154 }
3155
3156
3157 /*
3158 * 'cupsdStopPrinter()' - Stop a printer from printing any jobs...
3159 */
3160
3161 void
3162 cupsdStopPrinter(cupsd_printer_t *p, /* I - Printer to stop */
3163 int update)/* I - Update printers.conf? */
3164 {
3165 cupsd_job_t *job; /* Active print job */
3166
3167
3168 /*
3169 * Set the printer state...
3170 */
3171
3172 cupsdSetPrinterState(p, IPP_PRINTER_STOPPED, update);
3173
3174 /*
3175 * See if we have a job printing on this printer...
3176 */
3177
3178 if (p->job)
3179 {
3180 /*
3181 * Get pointer to job...
3182 */
3183
3184 job = (cupsd_job_t *)p->job;
3185
3186 /*
3187 * Stop it...
3188 */
3189
3190 cupsdStopJob(job, 0);
3191
3192 /*
3193 * Reset the state to pending...
3194 */
3195
3196 job->state->values[0].integer = IPP_JOB_PENDING;
3197 job->state_value = IPP_JOB_PENDING;
3198 job->dirty = 1;
3199
3200 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
3201
3202 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED, p, job,
3203 "Job stopped due to printer being paused");
3204 }
3205 }
3206
3207
3208 /*
3209 * 'cupsdUpdatePrinterPPD()' - Update keywords in a printer's PPD file.
3210 */
3211
3212 int /* O - 1 if successful, 0 otherwise */
3213 cupsdUpdatePrinterPPD(
3214 cupsd_printer_t *p, /* I - Printer */
3215 int num_keywords, /* I - Number of keywords */
3216 cups_option_t *keywords) /* I - Keywords */
3217 {
3218 int i; /* Looping var */
3219 cups_file_t *src, /* Original file */
3220 *dst; /* New file */
3221 char srcfile[1024], /* Original filename */
3222 dstfile[1024], /* New filename */
3223 line[1024], /* Line from file */
3224 keystring[41]; /* Keyword from line */
3225 cups_option_t *keyword; /* Current keyword */
3226
3227
3228 cupsdLogMessage(CUPSD_LOG_INFO, "Updating keywords in PPD file for %s...",
3229 p->name);
3230
3231 /*
3232 * Get the old and new PPD filenames...
3233 */
3234
3235 snprintf(srcfile, sizeof(srcfile), "%s/ppd/%s.ppd.O", ServerRoot, p->name);
3236 snprintf(dstfile, sizeof(srcfile), "%s/ppd/%s.ppd", ServerRoot, p->name);
3237
3238 /*
3239 * Rename the old file and open the old and new...
3240 */
3241
3242 if (rename(dstfile, srcfile))
3243 {
3244 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to backup PPD file for %s: %s",
3245 p->name, strerror(errno));
3246 return (0);
3247 }
3248
3249 if ((src = cupsFileOpen(srcfile, "r")) == NULL)
3250 {
3251 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to open PPD file \"%s\": %s",
3252 srcfile, strerror(errno));
3253 rename(srcfile, dstfile);
3254 return (0);
3255 }
3256
3257 if ((dst = cupsFileOpen(dstfile, "w")) == NULL)
3258 {
3259 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create PPD file \"%s\": %s",
3260 dstfile, strerror(errno));
3261 cupsFileClose(src);
3262 rename(srcfile, dstfile);
3263 return (0);
3264 }
3265
3266 /*
3267 * Copy the first line and then write out all of the keywords...
3268 */
3269
3270 if (!cupsFileGets(src, line, sizeof(line)))
3271 {
3272 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to read PPD file \"%s\": %s",
3273 srcfile, strerror(errno));
3274 cupsFileClose(src);
3275 cupsFileClose(dst);
3276 rename(srcfile, dstfile);
3277 return (0);
3278 }
3279
3280 cupsFilePrintf(dst, "%s\n", line);
3281
3282 for (i = num_keywords, keyword = keywords; i > 0; i --, keyword ++)
3283 {
3284 cupsdLogMessage(CUPSD_LOG_DEBUG, "*%s: %s", keyword->name, keyword->value);
3285 cupsFilePrintf(dst, "*%s: %s\n", keyword->name, keyword->value);
3286 }
3287
3288 /*
3289 * Then copy the rest of the PPD file, dropping any keywords we changed.
3290 */
3291
3292 while (cupsFileGets(src, line, sizeof(line)))
3293 {
3294 /*
3295 * Skip keywords we've already set...
3296 */
3297
3298 if (sscanf(line, "*%40[^:]:", keystring) == 1 &&
3299 cupsGetOption(keystring, num_keywords, keywords))
3300 continue;
3301
3302 /*
3303 * Otherwise write the line...
3304 */
3305
3306 cupsFilePrintf(dst, "%s\n", line);
3307 }
3308
3309 /*
3310 * Close files and return...
3311 */
3312
3313 cupsFileClose(src);
3314 cupsFileClose(dst);
3315
3316 return (1);
3317 }
3318
3319
3320 /*
3321 * 'cupsdUpdatePrinters()' - Update printers after a partial reload.
3322 */
3323
3324 void
3325 cupsdUpdatePrinters(void)
3326 {
3327 cupsd_printer_t *p; /* Current printer */
3328
3329
3330 /*
3331 * Loop through the printers and recreate the printer attributes
3332 * for any local printers since the policy and/or access control
3333 * stuff may have changed. Also, if browsing is disabled, remove
3334 * any remote printers...
3335 */
3336
3337 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
3338 p;
3339 p = (cupsd_printer_t *)cupsArrayNext(Printers))
3340 {
3341 /*
3342 * Remove remote printers if we are no longer browsing...
3343 */
3344
3345 if (!Browsing &&
3346 (p->type & (CUPS_PRINTER_IMPLICIT | CUPS_PRINTER_DISCOVERED)))
3347 {
3348 if (p->type & CUPS_PRINTER_IMPLICIT)
3349 cupsArrayRemove(ImplicitPrinters, p);
3350
3351 cupsArraySave(Printers);
3352 cupsdDeletePrinter(p, 0);
3353 cupsArrayRestore(Printers);
3354 continue;
3355 }
3356
3357 /*
3358 * Update the operation policy pointer...
3359 */
3360
3361 if ((p->op_policy_ptr = cupsdFindPolicy(p->op_policy)) == NULL)
3362 p->op_policy_ptr = DefaultPolicyPtr;
3363
3364 /*
3365 * Update printer attributes as needed...
3366 */
3367
3368 if (!(p->type & CUPS_PRINTER_DISCOVERED))
3369 cupsdSetPrinterAttrs(p);
3370 }
3371 }
3372
3373
3374 /*
3375 * 'cupsdValidateDest()' - Validate a printer/class destination.
3376 */
3377
3378 const char * /* O - Printer or class name */
3379 cupsdValidateDest(
3380 const char *uri, /* I - Printer URI */
3381 cups_ptype_t *dtype, /* O - Type (printer or class) */
3382 cupsd_printer_t **printer) /* O - Printer pointer */
3383 {
3384 cupsd_printer_t *p; /* Current printer */
3385 char localname[1024],/* Localized hostname */
3386 *lptr, /* Pointer into localized hostname */
3387 *sptr, /* Pointer into server name */
3388 *rptr, /* Pointer into resource */
3389 scheme[32], /* Scheme portion of URI */
3390 username[64], /* Username portion of URI */
3391 hostname[HTTP_MAX_HOST],
3392 /* Host portion of URI */
3393 resource[HTTP_MAX_URI];
3394 /* Resource portion of URI */
3395 int port; /* Port portion of URI */
3396
3397
3398 DEBUG_printf(("cupsdValidateDest(uri=\"%s\", dtype=%p, printer=%p)\n", uri,
3399 dtype, printer));
3400
3401 /*
3402 * Initialize return values...
3403 */
3404
3405 if (printer)
3406 *printer = NULL;
3407
3408 if (dtype)
3409 *dtype = (cups_ptype_t)0;
3410
3411 /*
3412 * Pull the hostname and resource from the URI...
3413 */
3414
3415 httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, sizeof(scheme),
3416 username, sizeof(username), hostname, sizeof(hostname),
3417 &port, resource, sizeof(resource));
3418
3419 /*
3420 * See if the resource is a class or printer...
3421 */
3422
3423 if (!strncmp(resource, "/classes/", 9))
3424 {
3425 /*
3426 * Class...
3427 */
3428
3429 rptr = resource + 9;
3430 }
3431 else if (!strncmp(resource, "/printers/", 10))
3432 {
3433 /*
3434 * Printer...
3435 */
3436
3437 rptr = resource + 10;
3438 }
3439 else
3440 {
3441 /*
3442 * Bad resource name...
3443 */
3444
3445 return (NULL);
3446 }
3447
3448 /*
3449 * See if the printer or class name exists...
3450 */
3451
3452 p = cupsdFindDest(rptr);
3453
3454 if (p == NULL && strchr(rptr, '@') == NULL)
3455 return (NULL);
3456 else if (p != NULL)
3457 {
3458 if (printer)
3459 *printer = p;
3460
3461 if (dtype)
3462 *dtype = p->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT |
3463 CUPS_PRINTER_REMOTE | CUPS_PRINTER_DISCOVERED);
3464
3465 return (p->name);
3466 }
3467
3468 /*
3469 * Change localhost to the server name...
3470 */
3471
3472 if (!strcasecmp(hostname, "localhost"))
3473 strlcpy(hostname, ServerName, sizeof(hostname));
3474
3475 strlcpy(localname, hostname, sizeof(localname));
3476
3477 if (!strcasecmp(hostname, ServerName))
3478 {
3479 /*
3480 * Localize the hostname...
3481 */
3482
3483 lptr = strchr(localname, '.');
3484 sptr = strchr(ServerName, '.');
3485
3486 if (sptr != NULL && lptr != NULL)
3487 {
3488 /*
3489 * Strip the common domain name components...
3490 */
3491
3492 while (lptr != NULL)
3493 {
3494 if (!strcasecmp(lptr, sptr))
3495 {
3496 *lptr = '\0';
3497 break;
3498 }
3499 else
3500 lptr = strchr(lptr + 1, '.');
3501 }
3502 }
3503 }
3504
3505 DEBUG_printf(("localized hostname is \"%s\"...\n", localname));
3506
3507 /*
3508 * Find a matching printer or class...
3509 */
3510
3511 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
3512 p;
3513 p = (cupsd_printer_t *)cupsArrayNext(Printers))
3514 if (!strcasecmp(p->hostname, localname) &&
3515 !strcasecmp(p->name, rptr))
3516 {
3517 if (printer)
3518 *printer = p;
3519
3520 if (dtype)
3521 *dtype = p->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT |
3522 CUPS_PRINTER_REMOTE | CUPS_PRINTER_DISCOVERED);
3523
3524 return (p->name);
3525 }
3526
3527 return (NULL);
3528 }
3529
3530
3531 /*
3532 * 'cupsdWritePrintcap()' - Write a pseudo-printcap file for older applications
3533 * that need it...
3534 */
3535
3536 void
3537 cupsdWritePrintcap(void)
3538 {
3539 cups_file_t *fp; /* printcap file */
3540 cupsd_printer_t *p; /* Current printer */
3541
3542
3543 #ifdef __sgi
3544 /*
3545 * Update the IRIX printer state for the default printer; if
3546 * no printers remain, then the default printer file will be
3547 * removed...
3548 */
3549
3550 write_irix_state(DefaultPrinter);
3551 #endif /* __sgi */
3552
3553 /*
3554 * See if we have a printcap file; if not, don't bother writing it.
3555 */
3556
3557 if (!Printcap || !*Printcap)
3558 return;
3559
3560 /*
3561 * Open the printcap file...
3562 */
3563
3564 if ((fp = cupsFileOpen(Printcap, "w")) == NULL)
3565 return;
3566
3567 /*
3568 * Put a comment header at the top so that users will know where the
3569 * data has come from...
3570 */
3571
3572 cupsFilePuts(fp,
3573 "# This file was automatically generated by cupsd(8) from the\n");
3574 cupsFilePrintf(fp, "# %s/printers.conf file. All changes to this file\n",
3575 ServerRoot);
3576 cupsFilePuts(fp, "# will be lost.\n");
3577
3578 if (Printers)
3579 {
3580 /*
3581 * Write a new printcap with the current list of printers.
3582 */
3583
3584 switch (PrintcapFormat)
3585 {
3586 case PRINTCAP_BSD:
3587 /*
3588 * Each printer is put in the file as:
3589 *
3590 * Printer1:
3591 * Printer2:
3592 * Printer3:
3593 * ...
3594 * PrinterN:
3595 */
3596
3597 if (DefaultPrinter)
3598 cupsFilePrintf(fp, "%s|%s:rm=%s:rp=%s:\n", DefaultPrinter->name,
3599 DefaultPrinter->info, ServerName,
3600 DefaultPrinter->name);
3601
3602 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
3603 p;
3604 p = (cupsd_printer_t *)cupsArrayNext(Printers))
3605 if (p != DefaultPrinter)
3606 cupsFilePrintf(fp, "%s|%s:rm=%s:rp=%s:\n", p->name, p->info,
3607 ServerName, p->name);
3608 break;
3609
3610 case PRINTCAP_SOLARIS:
3611 /*
3612 * Each printer is put in the file as:
3613 *
3614 * _all:all=Printer1,Printer2,Printer3,...,PrinterN
3615 * _default:use=DefaultPrinter
3616 * Printer1:\
3617 * :bsdaddr=ServerName,Printer1:\
3618 * :description=Description:
3619 * Printer2:
3620 * :bsdaddr=ServerName,Printer2:\
3621 * :description=Description:
3622 * Printer3:
3623 * :bsdaddr=ServerName,Printer3:\
3624 * :description=Description:
3625 * ...
3626 * PrinterN:
3627 * :bsdaddr=ServerName,PrinterN:\
3628 * :description=Description:
3629 */
3630
3631 cupsFilePuts(fp, "_all:all=");
3632 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
3633 p;
3634 p = (cupsd_printer_t *)cupsArrayCurrent(Printers))
3635 cupsFilePrintf(fp, "%s%c", p->name,
3636 cupsArrayNext(Printers) ? ',' : '\n');
3637
3638 if (DefaultPrinter)
3639 cupsFilePrintf(fp, "_default:use=%s\n", DefaultPrinter->name);
3640
3641 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
3642 p;
3643 p = (cupsd_printer_t *)cupsArrayNext(Printers))
3644 cupsFilePrintf(fp, "%s:\\\n"
3645 "\t:bsdaddr=%s,%s:\\\n"
3646 "\t:description=%s:\n",
3647 p->name, ServerName, p->name,
3648 p->info ? p->info : "");
3649 break;
3650 }
3651 }
3652
3653 /*
3654 * Close the file...
3655 */
3656
3657 cupsFileClose(fp);
3658 }
3659
3660
3661 /*
3662 * 'cupsdSanitizeURI()' - Sanitize a device URI...
3663 */
3664
3665 char * /* O - New device URI */
3666 cupsdSanitizeURI(const char *uri, /* I - Original device URI */
3667 char *buffer, /* O - New device URI */
3668 int buflen) /* I - Size of new device URI buffer */
3669 {
3670 char *start, /* Start of data after scheme */
3671 *slash, /* First slash after scheme:// */
3672 *ptr; /* Pointer into user@host:port part */
3673
3674
3675 /*
3676 * Range check input...
3677 */
3678
3679 if (!uri || !buffer || buflen < 2)
3680 return (NULL);
3681
3682 /*
3683 * Copy the device URI to the new buffer...
3684 */
3685
3686 strlcpy(buffer, uri, buflen);
3687
3688 /*
3689 * Find the end of the scheme:// part...
3690 */
3691
3692 if ((ptr = strchr(buffer, ':')) == NULL)
3693 return (buffer); /* No scheme: part... */
3694
3695 for (start = ptr + 1; *start; start ++)
3696 if (*start != '/')
3697 break;
3698
3699 /*
3700 * Find the next slash (/) in the URI...
3701 */
3702
3703 if ((slash = strchr(start, '/')) == NULL)
3704 slash = start + strlen(start); /* No slash, point to the end */
3705
3706 /*
3707 * Check for an @ sign before the slash...
3708 */
3709
3710 if ((ptr = strchr(start, '@')) != NULL && ptr < slash)
3711 {
3712 /*
3713 * Found an @ sign and it is before the resource part, so we have
3714 * an authentication string. Copy the remaining URI over the
3715 * authentication string...
3716 */
3717
3718 _cups_strcpy(start, ptr + 1);
3719 }
3720
3721 /*
3722 * Return the new device URI...
3723 */
3724
3725 return (buffer);
3726 }
3727
3728
3729 /*
3730 * 'add_printer_defaults()' - Add name-default attributes to the printer attributes.
3731 */
3732
3733 static void
3734 add_printer_defaults(cupsd_printer_t *p)/* I - Printer */
3735 {
3736 int i; /* Looping var */
3737 int num_options; /* Number of default options */
3738 cups_option_t *options, /* Default options */
3739 *option; /* Current option */
3740 char name[256]; /* name-default */
3741
3742
3743 /*
3744 * Maintain a common array of default attribute names...
3745 */
3746
3747 if (!CommonDefaults)
3748 {
3749 CommonDefaults = cupsArrayNew((cups_array_func_t)strcmp, NULL);
3750
3751 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("copies-default"));
3752 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("document-format-default"));
3753 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("finishings-default"));
3754 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("job-hold-until-default"));
3755 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("job-priority-default"));
3756 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("job-sheets-default"));
3757 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("media-default"));
3758 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("number-up-default"));
3759 cupsArrayAdd(CommonDefaults,
3760 _cupsStrAlloc("orientation-requested-default"));
3761 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("sides-default"));
3762 }
3763
3764 /*
3765 * Add all of the default options from the .conf files...
3766 */
3767
3768 for (num_options = 0, options = NULL, i = p->num_options, option = p->options;
3769 i > 0;
3770 i --, option ++)
3771 {
3772 if (strcmp(option->name, "ipp-options") &&
3773 strcmp(option->name, "job-sheets") &&
3774 strcmp(option->name, "lease-duration"))
3775 {
3776 snprintf(name, sizeof(name), "%s-default", option->name);
3777 num_options = cupsAddOption(name, option->value, num_options, &options);
3778
3779 if (!cupsArrayFind(CommonDefaults, name))
3780 cupsArrayAdd(CommonDefaults, _cupsStrAlloc(name));
3781 }
3782 }
3783
3784 /*
3785 * Convert options to IPP attributes...
3786 */
3787
3788 cupsEncodeOptions2(p->attrs, num_options, options, IPP_TAG_PRINTER);
3789 cupsFreeOptions(num_options, options);
3790
3791 /*
3792 * Add standard -default attributes as needed...
3793 */
3794
3795 if (!cupsGetOption("copies", p->num_options, p->options))
3796 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER, "copies-default",
3797 1);
3798
3799 if (!cupsGetOption("document-format", p->num_options, p->options))
3800 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_MIMETYPE,
3801 "document-format-default", NULL, "application/octet-stream");
3802
3803 if (!cupsGetOption("job-hold-until", p->num_options, p->options))
3804 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
3805 "job-hold-until-default", NULL, "no-hold");
3806
3807 if (!cupsGetOption("job-priority", p->num_options, p->options))
3808 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
3809 "job-priority-default", 50);
3810
3811 if (!cupsGetOption("number-up", p->num_options, p->options))
3812 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
3813 "number-up-default", 1);
3814
3815 if (!cupsGetOption("orientation-requested", p->num_options, p->options))
3816 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NOVALUE,
3817 "orientation-requested-default", NULL, NULL);
3818
3819 if (!cupsGetOption("notify-lease-duration", p->num_options, p->options))
3820 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
3821 "notify-lease-duration-default", DefaultLeaseDuration);
3822
3823 if (!cupsGetOption("notify-events", p->num_options, p->options))
3824 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
3825 "notify-events-default", NULL, "job-completed");
3826 }
3827
3828
3829 /*
3830 * 'add_printer_filter()' - Add a MIME filter for a printer.
3831 */
3832
3833 static void
3834 add_printer_filter(
3835 cupsd_printer_t *p, /* I - Printer to add to */
3836 mime_type_t *filtertype, /* I - Filter or prefilter MIME type */
3837 const char *filter) /* I - Filter to add */
3838 {
3839 char super[MIME_MAX_SUPER], /* Super-type for filter */
3840 type[MIME_MAX_TYPE], /* Type for filter */
3841 program[1024]; /* Program/filter name */
3842 int cost; /* Cost of filter */
3843 mime_type_t *temptype; /* MIME type looping var */
3844 char filename[1024]; /* Full filter filename */
3845
3846
3847 /*
3848 * Parse the filter string; it should be in the following format:
3849 *
3850 * super/type cost program
3851 */
3852
3853 if (sscanf(filter, "%15[^/]/%31s%d%*[ \t]%1023[^\n]", super, type, &cost,
3854 program) != 4)
3855 {
3856 cupsdLogMessage(CUPSD_LOG_ERROR, "%s: invalid filter string \"%s\"!",
3857 p->name, filter);
3858 return;
3859 }
3860
3861 /*
3862 * See if the filter program exists; if not, stop the printer and flag
3863 * the error!
3864 */
3865
3866 if (strcmp(program, "-"))
3867 {
3868 if (program[0] == '/')
3869 strlcpy(filename, program, sizeof(filename));
3870 else
3871 snprintf(filename, sizeof(filename), "%s/filter/%s", ServerBin, program);
3872
3873 if (access(filename, X_OK))
3874 {
3875 snprintf(p->state_message, sizeof(p->state_message),
3876 "Filter \"%s\" for printer \"%s\" not available: %s",
3877 program, p->name, strerror(errno));
3878 cupsdSetPrinterReasons(p, "+cups-missing-filter-error");
3879 cupsdSetPrinterState(p, IPP_PRINTER_STOPPED, 0);
3880
3881 cupsdLogMessage(CUPSD_LOG_ERROR, "%s", p->state_message);
3882 }
3883 }
3884
3885 /*
3886 * Mark the CUPS_PRINTER_COMMANDS bit if we have a filter for
3887 * application/vnd.cups-command...
3888 */
3889
3890 if (!strcasecmp(super, "application") &&
3891 !strcasecmp(type, "vnd.cups-command"))
3892 p->type |= CUPS_PRINTER_COMMANDS;
3893
3894 /*
3895 * Add the filter to the MIME database, supporting wildcards as needed...
3896 */
3897
3898 for (temptype = mimeFirstType(MimeDatabase);
3899 temptype;
3900 temptype = mimeNextType(MimeDatabase))
3901 if (((super[0] == '*' && strcasecmp(temptype->super, "printer")) ||
3902 !strcasecmp(temptype->super, super)) &&
3903 (type[0] == '*' || !strcasecmp(temptype->type, type)))
3904 {
3905 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3906 "add_printer_filter: %s: adding filter %s/%s %s/%s %d %s",
3907 p->name, temptype->super, temptype->type,
3908 filtertype->super, filtertype->type,
3909 cost, program);
3910 mimeAddFilter(MimeDatabase, temptype, filtertype, cost, program);
3911 }
3912 }
3913
3914
3915 /*
3916 * 'add_printer_formats()' - Add document-format-supported values for a printer.
3917 */
3918
3919 static void
3920 add_printer_formats(cupsd_printer_t *p) /* I - Printer */
3921 {
3922 int i; /* Looping var */
3923 mime_type_t *type; /* Current MIME type */
3924 cups_array_t *filters; /* Filters */
3925 ipp_attribute_t *attr; /* document-format-supported attribute */
3926 char mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2];
3927 /* MIME type name */
3928
3929
3930 /*
3931 * Raw (and remote) queues advertise all of the supported MIME
3932 * types...
3933 */
3934
3935 cupsArrayDelete(p->filetypes);
3936 p->filetypes = NULL;
3937
3938 if (p->raw)
3939 {
3940 ippAddStrings(p->attrs, IPP_TAG_PRINTER,
3941 (ipp_tag_t)(IPP_TAG_MIMETYPE | IPP_TAG_COPY),
3942 "document-format-supported", NumMimeTypes, NULL, MimeTypes);
3943 return;
3944 }
3945
3946 /*
3947 * Otherwise, loop through the supported MIME types and see if there
3948 * are filters for them...
3949 */
3950
3951 cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_printer_formats: %d types, %d filters",
3952 mimeNumTypes(MimeDatabase), mimeNumFilters(MimeDatabase));
3953
3954 p->filetypes = cupsArrayNew(NULL, NULL);
3955
3956 for (type = mimeFirstType(MimeDatabase);
3957 type;
3958 type = mimeNextType(MimeDatabase))
3959 {
3960 snprintf(mimetype, sizeof(mimetype), "%s/%s", type->super, type->type);
3961
3962 if ((filters = mimeFilter(MimeDatabase, type, p->filetype, NULL)) != NULL)
3963 {
3964 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3965 "add_printer_formats: %s: %s needs %d filters",
3966 p->name, mimetype, cupsArrayCount(filters));
3967
3968 cupsArrayDelete(filters);
3969 cupsArrayAdd(p->filetypes, type);
3970 }
3971 else
3972 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3973 "add_printer_formats: %s: %s not supported",
3974 p->name, mimetype);
3975 }
3976
3977 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3978 "add_printer_formats: %s: %d supported types",
3979 p->name, cupsArrayCount(p->filetypes) + 1);
3980
3981 /*
3982 * Add the file formats that can be filtered...
3983 */
3984
3985 if ((type = mimeType(MimeDatabase, "application", "octet-stream")) == NULL ||
3986 !cupsArrayFind(p->filetypes, type))
3987 i = 1;
3988 else
3989 i = 0;
3990
3991 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_MIMETYPE,
3992 "document-format-supported",
3993 cupsArrayCount(p->filetypes) + 1, NULL, NULL);
3994
3995 if (i)
3996 attr->values[0].string.text = _cupsStrAlloc("application/octet-stream");
3997
3998 for (type = (mime_type_t *)cupsArrayFirst(p->filetypes);
3999 type;
4000 i ++, type = (mime_type_t *)cupsArrayNext(p->filetypes))
4001 {
4002 snprintf(mimetype, sizeof(mimetype), "%s/%s", type->super, type->type);
4003
4004 attr->values[i].string.text = _cupsStrAlloc(mimetype);
4005 }
4006
4007 #ifdef HAVE_DNSSD
4008 {
4009 char pdl[1024]; /* Buffer to build pdl list */
4010 mime_filter_t *filter; /* MIME filter looping var */
4011
4012
4013 pdl[0] = '\0';
4014
4015 if (mimeType(MimeDatabase, "application", "pdf"))
4016 strlcat(pdl, "application/pdf,", sizeof(pdl));
4017
4018 if (mimeType(MimeDatabase, "application", "postscript"))
4019 strlcat(pdl, "application/postscript,", sizeof(pdl));
4020
4021 if (mimeType(MimeDatabase, "application", "vnd.cups-raster"))
4022 strlcat(pdl, "application/vnd.cups-raster,", sizeof(pdl));
4023
4024 /*
4025 * Determine if this is a Tioga PrintJobMgr based queue...
4026 */
4027
4028 for (filter = (mime_filter_t *)cupsArrayFirst(MimeDatabase->filters);
4029 filter;
4030 filter = (mime_filter_t *)cupsArrayNext(MimeDatabase->filters))
4031 {
4032 if (filter->dst == p->filetype && filter->filter &&
4033 strstr(filter->filter, "PrintJobMgr"))
4034 break;
4035 }
4036
4037 /*
4038 * We only support raw printing if this is not a Tioga PrintJobMgr based
4039 * queue and if application/octet-stream is a known conversion...
4040 */
4041
4042 if (!filter && mimeType(MimeDatabase, "application", "octet-stream"))
4043 strlcat(pdl, "application/octet-stream,", sizeof(pdl));
4044
4045 if (mimeType(MimeDatabase, "image", "png"))
4046 strlcat(pdl, "image/png,", sizeof(pdl));
4047
4048 if (pdl[0])
4049 pdl[strlen(pdl) - 1] = '\0'; /* Remove trailing comma */
4050
4051 cupsdSetString(&p->pdl, pdl);
4052 }
4053 #endif /* HAVE_DNSSD */
4054 }
4055
4056
4057 /*
4058 * 'compare_printers()' - Compare two printers.
4059 */
4060
4061 static int /* O - Result of comparison */
4062 compare_printers(void *first, /* I - First printer */
4063 void *second, /* I - Second printer */
4064 void *data) /* I - App data (not used) */
4065 {
4066 return (strcasecmp(((cupsd_printer_t *)first)->name,
4067 ((cupsd_printer_t *)second)->name));
4068 }
4069
4070
4071 /*
4072 * 'delete_printer_filters()' - Delete all MIME filters for a printer.
4073 */
4074
4075 static void
4076 delete_printer_filters(
4077 cupsd_printer_t *p) /* I - Printer to remove from */
4078 {
4079 mime_filter_t *filter; /* MIME filter looping var */
4080
4081
4082 /*
4083 * Range check input...
4084 */
4085
4086 if (p == NULL)
4087 return;
4088
4089 /*
4090 * Remove all filters from the MIME database that have a destination
4091 * type == printer...
4092 */
4093
4094 for (filter = mimeFirstFilter(MimeDatabase);
4095 filter;
4096 filter = mimeNextFilter(MimeDatabase))
4097 if (filter->dst == p->filetype)
4098 {
4099 /*
4100 * Delete the current filter...
4101 */
4102
4103 mimeDeleteFilter(MimeDatabase, filter);
4104 }
4105 }
4106
4107
4108 #ifdef __sgi
4109 /*
4110 * 'write_irix_config()' - Update the config files used by the IRIX
4111 * desktop tools.
4112 */
4113
4114 static void
4115 write_irix_config(cupsd_printer_t *p) /* I - Printer to update */
4116 {
4117 char filename[1024]; /* Interface script filename */
4118 cups_file_t *fp; /* Interface script file */
4119 ipp_attribute_t *attr; /* Attribute data */
4120
4121
4122 /*
4123 * Add dummy interface and GUI scripts to fool SGI's "challenged" printing
4124 * tools. First the interface script that tells the tools what kind of
4125 * printer we have...
4126 */
4127
4128 snprintf(filename, sizeof(filename), "/var/spool/lp/interface/%s", p->name);
4129
4130 if (p->type & CUPS_PRINTER_CLASS)
4131 unlink(filename);
4132 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
4133 {
4134 cupsFilePuts(fp, "#!/bin/sh\n");
4135
4136 if ((attr = ippFindAttribute(p->attrs, "printer-make-and-model",
4137 IPP_TAG_TEXT)) != NULL)
4138 cupsFilePrintf(fp, "NAME=\"%s\"\n", attr->values[0].string.text);
4139 else if (p->type & CUPS_PRINTER_CLASS)
4140 cupsFilePuts(fp, "NAME=\"Printer Class\"\n");
4141 else
4142 cupsFilePuts(fp, "NAME=\"Remote Destination\"\n");
4143
4144 if (p->type & CUPS_PRINTER_COLOR)
4145 cupsFilePuts(fp, "TYPE=ColorPostScript\n");
4146 else
4147 cupsFilePuts(fp, "TYPE=MonoPostScript\n");
4148
4149 cupsFilePrintf(fp, "HOSTNAME=%s\n", ServerName);
4150 cupsFilePrintf(fp, "HOSTPRINTER=%s\n", p->name);
4151
4152 cupsFileClose(fp);
4153
4154 chmod(filename, 0755);
4155 chown(filename, User, Group);
4156 }
4157
4158 /*
4159 * Then the member file that tells which device file the queue is connected
4160 * to... Networked printers use "/dev/null" in this file, so that's what
4161 * we use (the actual device URI can confuse some apps...)
4162 */
4163
4164 snprintf(filename, sizeof(filename), "/var/spool/lp/member/%s", p->name);
4165
4166 if (p->type & CUPS_PRINTER_CLASS)
4167 unlink(filename);
4168 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
4169 {
4170 cupsFilePuts(fp, "/dev/null\n");
4171
4172 cupsFileClose(fp);
4173
4174 chmod(filename, 0644);
4175 chown(filename, User, Group);
4176 }
4177
4178 /*
4179 * The gui_interface file is a script or program that launches a GUI
4180 * option panel for the printer, using options specified on the
4181 * command-line in the third argument. The option panel must send
4182 * any printing options to stdout on a single line when the user
4183 * accepts them, or nothing if the user cancels the dialog.
4184 *
4185 * The default options panel program is /usr/bin/glpoptions, from
4186 * the ESP Print Pro software. You can select another using the
4187 * PrintcapGUI option.
4188 */
4189
4190 snprintf(filename, sizeof(filename), "/var/spool/lp/gui_interface/ELF/%s.gui", p->name);
4191
4192 if (p->type & CUPS_PRINTER_CLASS)
4193 unlink(filename);
4194 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
4195 {
4196 cupsFilePuts(fp, "#!/bin/sh\n");
4197 cupsFilePrintf(fp, "%s -d %s -o \"$3\"\n", PrintcapGUI, p->name);
4198
4199 cupsFileClose(fp);
4200
4201 chmod(filename, 0755);
4202 chown(filename, User, Group);
4203 }
4204
4205 /*
4206 * The POD config file is needed by the printstatus command to show
4207 * the printer location and device.
4208 */
4209
4210 snprintf(filename, sizeof(filename), "/var/spool/lp/pod/%s.config", p->name);
4211
4212 if (p->type & CUPS_PRINTER_CLASS)
4213 unlink(filename);
4214 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
4215 {
4216 cupsFilePrintf(fp, "Printer Class | %s\n",
4217 (p->type & CUPS_PRINTER_COLOR) ? "ColorPostScript" : "MonoPostScript");
4218 cupsFilePrintf(fp, "Printer Model | %s\n", p->make_model ? p->make_model : "");
4219 cupsFilePrintf(fp, "Location Code | %s\n", p->location ? p->location : "");
4220 cupsFilePrintf(fp, "Physical Location | %s\n", p->info ? p->info : "");
4221 cupsFilePrintf(fp, "Port Path | %s\n", p->device_uri ? p->device_uri : "");
4222 cupsFilePrintf(fp, "Config Path | /var/spool/lp/pod/%s.config\n", p->name);
4223 cupsFilePrintf(fp, "Active Status Path | /var/spool/lp/pod/%s.status\n", p->name);
4224 cupsFilePuts(fp, "Status Update Wait | 10 seconds\n");
4225
4226 cupsFileClose(fp);
4227
4228 chmod(filename, 0664);
4229 chown(filename, User, Group);
4230 }
4231 }
4232
4233
4234 /*
4235 * 'write_irix_state()' - Update the status files used by IRIX printing
4236 * desktop tools.
4237 */
4238
4239 static void
4240 write_irix_state(cupsd_printer_t *p) /* I - Printer to update */
4241 {
4242 char filename[1024]; /* Interface script filename */
4243 cups_file_t *fp; /* Interface script file */
4244 int tag; /* Status tag value */
4245
4246
4247 if (p)
4248 {
4249 /*
4250 * The POD status file is needed for the printstatus window to
4251 * provide the current status of the printer.
4252 */
4253
4254 snprintf(filename, sizeof(filename), "/var/spool/lp/pod/%s.status", p->name);
4255
4256 if (p->type & CUPS_PRINTER_CLASS)
4257 unlink(filename);
4258 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
4259 {
4260 cupsFilePrintf(fp, "Operational Status | %s\n",
4261 (p->state == IPP_PRINTER_IDLE) ? "Idle" :
4262 (p->state == IPP_PRINTER_PROCESSING) ? "Busy" :
4263 "Faulted");
4264 cupsFilePrintf(fp, "Information | 01 00 00 | %s\n", CUPS_SVERSION);
4265 cupsFilePrintf(fp, "Information | 02 00 00 | Device URI: %s\n",
4266 p->device_uri ? p->device_uri : "");
4267 cupsFilePrintf(fp, "Information | 03 00 00 | %s jobs\n",
4268 p->accepting ? "Accepting" : "Not accepting");
4269 cupsFilePrintf(fp, "Information | 04 00 00 | %s\n", p->state_message);
4270
4271 cupsFileClose(fp);
4272
4273 chmod(filename, 0664);
4274 chown(filename, User, Group);
4275 }
4276
4277 /*
4278 * The activeicons file is needed to provide desktop icons for printers:
4279 *
4280 * [ quoted from /usr/lib/print/tagit ]
4281 *
4282 * --- Type of printer tags (base values)
4283 *
4284 * Dumb=66048 # 0x10200
4285 * DumbColor=66080 # 0x10220
4286 * Raster=66112 # 0x10240
4287 * ColorRaster=66144 # 0x10260
4288 * Plotter=66176 # 0x10280
4289 * PostScript=66208 # 0x102A0
4290 * ColorPostScript=66240 # 0x102C0
4291 * MonoPostScript=66272 # 0x102E0
4292 *
4293 * --- Printer state modifiers for local printers
4294 *
4295 * Idle=0 # 0x0
4296 * Busy=1 # 0x1
4297 * Faulted=2 # 0x2
4298 * Unknown=3 # 0x3 (Faulted due to unknown reason)
4299 *
4300 * --- Printer state modifiers for network printers
4301 *
4302 * NetIdle=8 # 0x8
4303 * NetBusy=9 # 0x9
4304 * NetFaulted=10 # 0xA
4305 * NetUnknown=11 # 0xB (Faulted due to unknown reason)
4306 */
4307
4308 snprintf(filename, sizeof(filename), "/var/spool/lp/activeicons/%s", p->name);
4309
4310 if (p->type & CUPS_PRINTER_CLASS)
4311 unlink(filename);
4312 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
4313 {
4314 if (p->type & CUPS_PRINTER_COLOR)
4315 tag = 66240;
4316 else
4317 tag = 66272;
4318
4319 if (p->type & CUPS_PRINTER_REMOTE)
4320 tag |= 8;
4321
4322 if (p->state == IPP_PRINTER_PROCESSING)
4323 tag |= 1;
4324
4325 else if (p->state == IPP_PRINTER_STOPPED)
4326 tag |= 2;
4327
4328 cupsFilePuts(fp, "#!/bin/sh\n");
4329 cupsFilePrintf(fp, "#Tag %d\n", tag);
4330
4331 cupsFileClose(fp);
4332
4333 chmod(filename, 0755);
4334 chown(filename, User, Group);
4335 }
4336 }
4337
4338 /*
4339 * The default file is needed by the printers window to show
4340 * the default printer.
4341 */
4342
4343 snprintf(filename, sizeof(filename), "/var/spool/lp/default");
4344
4345 if (DefaultPrinter != NULL)
4346 {
4347 if ((fp = cupsFileOpen(filename, "w")) != NULL)
4348 {
4349 cupsFilePrintf(fp, "%s\n", DefaultPrinter->name);
4350
4351 cupsFileClose(fp);
4352
4353 chmod(filename, 0644);
4354 chown(filename, User, Group);
4355 }
4356 }
4357 else
4358 unlink(filename);
4359 }
4360 #endif /* __sgi */
4361
4362
4363 /*
4364 * End of "$Id: printers.c 7677 2008-06-19 23:22:19Z mike $".
4365 */