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