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