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