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