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