]> git.ipfire.org Git - thirdparty/cups.git/blame - scheduler/printers.c
Import CUPS 1.4svn-r7226.
[thirdparty/cups.git] / scheduler / printers.c
CommitLineData
ef416fc2 1/*
2e4ff8af 2 * "$Id: printers.c 6970 2007-09-17 23:58:28Z mike $"
ef416fc2 3 *
4 * Printer routines for the Common UNIX Printing System (CUPS).
5 *
91c84a35 6 * Copyright 2007-2008 by Apple Inc.
f7deaa1a 7 * Copyright 1997-2007 by Easy Software Products, all rights reserved.
ef416fc2 8 *
9 * These coded instructions, statements, and computer programs are the
bc44d920 10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
ef416fc2 14 *
15 * Contents:
16 *
17 * cupsdAddPrinter() - Add a printer to the system.
ef416fc2 18 * cupsdAddPrinterHistory() - Add the current printer state to the history.
19 * cupsdAddPrinterUser() - Add a user to the ACL.
b94498cf 20 * cupsdCreateCommonData() - Create the common printer data.
ef416fc2 21 * cupsdDeleteAllPrinters() - Delete all printers from the system.
22 * cupsdDeletePrinter() - Delete a printer from the system.
ef416fc2 23 * cupsdFindPrinter() - Find a printer in the list.
24 * cupsdFreePrinterUsers() - Free allow/deny users.
25 * cupsdLoadAllPrinters() - Load printers from the printers.conf file.
b423cd4c 26 * cupsdRenamePrinter() - Rename a printer.
fa73b229 27 * cupsdSaveAllPrinters() - Save all printer definitions to the
28 * printers.conf file.
09a101d6 29 * cupsdSetAuthInfoRequired() - Set the required authentication info.
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);
91c84a35 912 break;
ef416fc2 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);
91c84a35 957 break;
ef416fc2 958 }
959 }
960 else if (!p)
961 {
962 cupsdLogMessage(CUPSD_LOG_ERROR,
963 "Syntax error on line %d of printers.conf.", linenum);
91c84a35 964 break;
ef416fc2 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);
91c84a35 991 break;
ef416fc2 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);
91c84a35 1023 break;
ef416fc2 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);
91c84a35 1040 break;
ef416fc2 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);
91c84a35 1081 break;
ef416fc2 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);
91c84a35 1104 break;
ef416fc2 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);
91c84a35 1139 break;
ef416fc2 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);
91c84a35 1153 break;
ef416fc2 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);
91c84a35 1167 break;
ef416fc2 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);
91c84a35 1178 break;
ef416fc2 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);
91c84a35 1189 break;
ef416fc2 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);
91c84a35 1200 break;
ef416fc2 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);
91c84a35 1224 break;
ef416fc2 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);
91c84a35 1235 break;
ef416fc2 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 };
a4924f6c 1694#ifdef HAVE_GSSAPI
bc44d920 1695 static const char * const air_negotiate[] =
1696 { /* Kerberos authentication */
1697 "negotiate"
1698 };
a4924f6c 1699#endif /* HAVE_GSSAPI */
bc44d920 1700 static const char * const air_none[] =
1701 { /* No authentication */
1702 "none"
1703 };
ef416fc2 1704
1705
1706 DEBUG_printf(("cupsdSetPrinterAttrs: entering name = %s, type = %x\n", p->name,
1707 p->type));
1708
1709 /*
1710 * Make sure that we have the common attributes defined...
1711 */
1712
1713 if (!CommonData)
1714 cupsdCreateCommonData();
1715
1716 /*
1717 * Clear out old filters, if any...
1718 */
1719
e1d6a774 1720 delete_printer_filters(p);
ef416fc2 1721
1722 /*
1723 * Figure out the authentication that is required for the printer.
1724 */
1725
1726 auth_supported = "requesting-user-name";
bc44d920 1727 num_air = 1;
1728 air = air_none;
1729
1730 if (p->num_auth_info_required > 0 && strcmp(p->auth_info_required[0], "none"))
1731 {
1732 num_air = p->num_auth_info_required;
1733 air = p->auth_info_required;
1734
1735 if (!strcmp(air[0], "username"))
1736 auth_supported = "basic";
1737 else
1738 auth_supported = "negotiate";
1739 }
1740 else if (!(p->type & CUPS_PRINTER_DISCOVERED))
ef416fc2 1741 {
1742 if (p->type & CUPS_PRINTER_CLASS)
1743 snprintf(resource, sizeof(resource), "/classes/%s", p->name);
1744 else
1745 snprintf(resource, sizeof(resource), "/printers/%s", p->name);
1746
d09495fa 1747 if ((auth = cupsdFindBest(resource, HTTP_POST)) == NULL ||
1748 auth->type == AUTH_NONE)
ef416fc2 1749 auth = cupsdFindPolicyOp(p->op_policy_ptr, IPP_PRINT_JOB);
1750
1751 if (auth)
1752 {
1753 if (auth->type == AUTH_BASIC || auth->type == AUTH_BASICDIGEST)
f899b121 1754 {
ef416fc2 1755 auth_supported = "basic";
bc44d920 1756 num_air = 2;
1757 air = air_userpass;
f899b121 1758 }
ef416fc2 1759 else if (auth->type == AUTH_DIGEST)
f899b121 1760 {
ef416fc2 1761 auth_supported = "digest";
bc44d920 1762 num_air = 2;
1763 air = air_userpass;
f899b121 1764 }
1765#ifdef HAVE_GSSAPI
1766 else if (auth->type == AUTH_NEGOTIATE)
1767 {
1768 auth_supported = "negotiate";
bc44d920 1769 num_air = 1;
1770 air = air_negotiate;
f899b121 1771 }
1772#endif /* HAVE_GSSAPI */
ef416fc2 1773
1774 if (auth->type != AUTH_NONE)
1775 p->type |= CUPS_PRINTER_AUTHENTICATED;
1776 else
1777 p->type &= ~CUPS_PRINTER_AUTHENTICATED;
1778 }
1779 else
1780 p->type &= ~CUPS_PRINTER_AUTHENTICATED;
1781 }
bc44d920 1782 else if (p->type & CUPS_PRINTER_AUTHENTICATED)
1783 {
1784 num_air = 2;
1785 air = air_userpass;
1786 }
ef416fc2 1787
1788 /*
1789 * Create the required IPP attributes for a printer...
1790 */
1791
1792 if (p->attrs)
1793 ippDelete(p->attrs);
1794
1795 p->attrs = ippNew();
1796
1797 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
1798 "uri-authentication-supported", NULL, auth_supported);
1799 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
1800 "uri-security-supported", NULL, "none");
1801 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME, "printer-name", NULL,
1802 p->name);
1803 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-location",
1804 NULL, p->location ? p->location : "");
1805 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-info",
1806 NULL, p->info ? p->info : "");
1807 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI, "printer-more-info",
1808 NULL, p->uri);
1809
1810 if (p->num_users)
1811 {
1812 if (p->deny_users)
1813 ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
1814 "requesting-user-name-denied", p->num_users, NULL,
1815 p->users);
1816 else
1817 ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
1818 "requesting-user-name-allowed", p->num_users, NULL,
1819 p->users);
1820 }
1821
1822 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
1823 "job-quota-period", p->quota_period);
1824 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
1825 "job-k-limit", p->k_limit);
1826 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
1827 "job-page-limit", p->page_limit);
bc44d920 1828 ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
1829 "auth-info-required", num_air, NULL, air);
ef416fc2 1830
09a101d6 1831 if (cupsArrayCount(Banners) > 0 && !(p->type & CUPS_PRINTER_DISCOVERED))
ef416fc2 1832 {
1833 /*
1834 * Setup the job-sheets-default attribute...
1835 */
1836
1837 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
1838 "job-sheets-default", 2, NULL, NULL);
1839
1840 if (attr != NULL)
1841 {
757d2cad 1842 attr->values[0].string.text = _cupsStrAlloc(Classification ?
ef416fc2 1843 Classification : p->job_sheets[0]);
757d2cad 1844 attr->values[1].string.text = _cupsStrAlloc(Classification ?
ef416fc2 1845 Classification : p->job_sheets[1]);
1846 }
1847 }
1848
d09495fa 1849 p->raw = 0;
1850 p->remote = 0;
ef416fc2 1851
09a101d6 1852 if (p->type & CUPS_PRINTER_DISCOVERED)
ef416fc2 1853 {
1854 /*
1855 * Tell the client this is a remote printer of some type...
1856 */
1857
1858 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI,
1859 "printer-uri-supported", NULL, p->uri);
1860
1861 if (p->make_model)
1862 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
1863 "printer-make-and-model", NULL, p->make_model);
1864
fa73b229 1865 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI, "device-uri", NULL,
1866 p->uri);
1867
d09495fa 1868 p->raw = 1;
1869 p->remote = 1;
ef416fc2 1870 }
1871 else
1872 {
1873 /*
1874 * Assign additional attributes depending on whether this is a printer
1875 * or class...
1876 */
1877
1878 p->type &= ~CUPS_PRINTER_OPTIONS;
1879
1880 if (p->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT))
1881 {
1882 p->raw = 1;
1883
1884 /*
1885 * Add class-specific attributes...
1886 */
1887
1888 if ((p->type & CUPS_PRINTER_IMPLICIT) && p->num_printers > 0 &&
1889 p->printers[0]->make_model)
1890 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
1891 "printer-make-and-model", NULL, p->printers[0]->make_model);
1892 else
1893 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
1894 "printer-make-and-model", NULL, "Local Printer Class");
1895
fa73b229 1896 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI, "device-uri", NULL,
1897 "file:///dev/null");
1898
ef416fc2 1899 if (p->num_printers > 0)
1900 {
1901 /*
1902 * Add a list of member URIs and names...
1903 */
1904
1905 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI,
1906 "member-uris", p->num_printers, NULL, NULL);
1907 p->type |= CUPS_PRINTER_OPTIONS;
1908
1909 for (i = 0; i < p->num_printers; i ++)
1910 {
1911 if (attr != NULL)
757d2cad 1912 attr->values[i].string.text = _cupsStrAlloc(p->printers[i]->uri);
ef416fc2 1913
1914 p->type &= ~CUPS_PRINTER_OPTIONS | p->printers[i]->type;
1915 }
1916
1917 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
1918 "member-names", p->num_printers, NULL, NULL);
1919
1920 if (attr != NULL)
1921 {
1922 for (i = 0; i < p->num_printers; i ++)
757d2cad 1923 attr->values[i].string.text = _cupsStrAlloc(p->printers[i]->name);
ef416fc2 1924 }
1925 }
1926 }
1927 else
1928 {
1929 /*
1930 * Add printer-specific attributes... Start by sanitizing the device
1931 * URI so it doesn't have a username or password in it...
1932 */
1933
1934 if (!p->device_uri)
1935 strcpy(uri, "file:/dev/null");
1936 else if (strstr(p->device_uri, "://") != NULL)
1937 {
1938 /*
1939 * http://..., ipp://..., etc.
1940 */
1941
1942 cupsdSanitizeURI(p->device_uri, uri, sizeof(uri));
1943 }
1944 else
1945 {
1946 /*
1947 * file:..., serial:..., etc.
1948 */
1949
1950 strlcpy(uri, p->device_uri, sizeof(uri));
1951 }
1952
1953 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI, "device-uri", NULL,
1954 uri);
1955
1956 /*
1957 * Assign additional attributes from the PPD file (if any)...
1958 */
1959
1960 p->type |= CUPS_PRINTER_BW;
1961 finishings[0] = IPP_FINISHINGS_NONE;
1962 num_finishings = 1;
1963
1964 snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot,
1965 p->name);
1966
1967 if ((ppd = ppdOpenFile(filename)) != NULL)
1968 {
1969 /*
1970 * Add make/model and other various attributes...
1971 */
1972
1973 if (ppd->color_device)
1974 p->type |= CUPS_PRINTER_COLOR;
1975 if (ppd->variable_sizes)
1976 p->type |= CUPS_PRINTER_VARIABLE;
1977 if (!ppd->manual_copies)
1978 p->type |= CUPS_PRINTER_COPIES;
1979 if ((ppdattr = ppdFindAttr(ppd, "cupsFax", NULL)) != NULL)
1980 if (ppdattr->value && !strcasecmp(ppdattr->value, "true"))
1981 p->type |= CUPS_PRINTER_FAX;
1982
1983 ippAddBoolean(p->attrs, IPP_TAG_PRINTER, "color-supported",
1984 ppd->color_device);
1985 if (ppd->throughput)
1986 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
1987 "pages-per-minute", ppd->throughput);
1988
1989 if (ppd->nickname)
bd7854cb 1990 {
1991 /*
1992 * The NickName can be localized in the character set specified
e1d6a774 1993 * by the LanugageEncoding attribute. However, ppdOpen2() has
1994 * already converted the ppd->nickname member to UTF-8 for us
1995 * (the original attribute value is available separately)
bd7854cb 1996 */
1997
e1d6a774 1998 cupsdSetString(&p->make_model, ppd->nickname);
bd7854cb 1999 }
ef416fc2 2000 else if (ppd->modelname)
e1d6a774 2001 {
2002 /*
2003 * Model name can only contain specific characters...
2004 */
2005
ef416fc2 2006 cupsdSetString(&p->make_model, ppd->modelname);
e1d6a774 2007 }
ef416fc2 2008 else
2009 cupsdSetString(&p->make_model, "Bad PPD File");
2010
bd7854cb 2011 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
2012 "printer-make-and-model", NULL, p->make_model);
ef416fc2 2013
2014 /*
2015 * Add media options from the PPD file...
2016 */
2017
2018 if ((input_slot = ppdFindOption(ppd, "InputSlot")) != NULL)
2019 num_media = input_slot->num_choices;
2020 else
2021 num_media = 0;
2022
2023 if ((media_type = ppdFindOption(ppd, "MediaType")) != NULL)
2024 num_media += media_type->num_choices;
2025
2026 if ((page_size = ppdFindOption(ppd, "PageSize")) != NULL)
2027 num_media += page_size->num_choices;
2028
2029 if ((media_quality = ppdFindOption(ppd, "EFMediaQualityMode")) != NULL)
2030 num_media += media_quality->num_choices;
2031
2032 if (num_media == 0)
2033 {
bd7854cb 2034 cupsdLogMessage(CUPSD_LOG_CRIT,
b423cd4c 2035 "The PPD file for printer %s contains no media "
2036 "options and is therefore invalid!", p->name);
ef416fc2 2037 }
2038 else
2039 {
2040 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2041 "media-supported", num_media, NULL, NULL);
2042 if (attr != NULL)
2043 {
2044 val = attr->values;
2045
2046 if (input_slot != NULL)
2047 for (i = 0; i < input_slot->num_choices; i ++, val ++)
757d2cad 2048 val->string.text = _cupsStrAlloc(input_slot->choices[i].choice);
ef416fc2 2049
2050 if (media_type != NULL)
2051 for (i = 0; i < media_type->num_choices; i ++, val ++)
757d2cad 2052 val->string.text = _cupsStrAlloc(media_type->choices[i].choice);
ef416fc2 2053
2054 if (media_quality != NULL)
2055 for (i = 0; i < media_quality->num_choices; i ++, val ++)
757d2cad 2056 val->string.text = _cupsStrAlloc(media_quality->choices[i].choice);
ef416fc2 2057
2058 if (page_size != NULL)
2059 {
2060 for (i = 0; i < page_size->num_choices; i ++, val ++)
757d2cad 2061 val->string.text = _cupsStrAlloc(page_size->choices[i].choice);
ef416fc2 2062
b423cd4c 2063 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2064 "media-default", NULL, page_size->defchoice);
ef416fc2 2065 }
2066 else if (input_slot != NULL)
b423cd4c 2067 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2068 "media-default", NULL, input_slot->defchoice);
ef416fc2 2069 else if (media_type != NULL)
b423cd4c 2070 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2071 "media-default", NULL, media_type->defchoice);
ef416fc2 2072 else if (media_quality != NULL)
b423cd4c 2073 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2074 "media-default", NULL, media_quality->defchoice);
ef416fc2 2075 else
b423cd4c 2076 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2077 "media-default", NULL, "none");
ef416fc2 2078 }
2079 }
2080
2081 /*
2082 * Output bin...
2083 */
2084
2085 if ((output_bin = ppdFindOption(ppd, "OutputBin")) != NULL)
2086 {
2087 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2088 "output-bin-supported", output_bin->num_choices,
2089 NULL, NULL);
2090
2091 if (attr != NULL)
2092 {
2093 for (i = 0, val = attr->values;
2094 i < output_bin->num_choices;
2095 i ++, val ++)
757d2cad 2096 val->string.text = _cupsStrAlloc(output_bin->choices[i].choice);
ef416fc2 2097 }
2098 }
2099
2100 /*
2101 * Duplexing, etc...
2102 */
2103
b423cd4c 2104 if ((duplex = ppdFindOption(ppd, "Duplex")) == NULL)
2105 if ((duplex = ppdFindOption(ppd, "EFDuplex")) == NULL)
2106 if ((duplex = ppdFindOption(ppd, "EFDuplexing")) == NULL)
2107 if ((duplex = ppdFindOption(ppd, "KD03Duplex")) == NULL)
2108 duplex = ppdFindOption(ppd, "JCLDuplex");
2109
2110 if (duplex && duplex->num_choices > 1)
ef416fc2 2111 {
2112 p->type |= CUPS_PRINTER_DUPLEX;
2113
b423cd4c 2114 ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2115 "sides-supported", 3, NULL, sides);
2116
2117 if (!strcasecmp(duplex->defchoice, "DuplexTumble"))
2118 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2119 "sides-default", NULL, "two-sided-short-edge");
2120 else if (!strcasecmp(duplex->defchoice, "DuplexNoTumble"))
2121 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2122 "sides-default", NULL, "two-sided-long-edge");
2123 else
2124 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2125 "sides-default", NULL, "one-sided");
ef416fc2 2126 }
2127
2128 if (ppdFindOption(ppd, "Collate") != NULL)
2129 p->type |= CUPS_PRINTER_COLLATE;
2130
2131 if (ppdFindOption(ppd, "StapleLocation") != NULL)
2132 {
2133 p->type |= CUPS_PRINTER_STAPLE;
2134 finishings[num_finishings++] = IPP_FINISHINGS_STAPLE;
2135 }
2136
2137 if (ppdFindOption(ppd, "BindEdge") != NULL)
2138 {
2139 p->type |= CUPS_PRINTER_BIND;
2140 finishings[num_finishings++] = IPP_FINISHINGS_BIND;
2141 }
2142
2143 for (i = 0; i < ppd->num_sizes; i ++)
2144 if (ppd->sizes[i].length > 1728)
2145 p->type |= CUPS_PRINTER_LARGE;
2146 else if (ppd->sizes[i].length > 1008)
2147 p->type |= CUPS_PRINTER_MEDIUM;
2148 else
2149 p->type |= CUPS_PRINTER_SMALL;
2150
2151 /*
2152 * Add a filter from application/vnd.cups-raw to printer/name to
2153 * handle "raw" printing by users.
2154 */
2155
f7deaa1a 2156 add_printer_filter(p, p->filetype, "application/vnd.cups-raw 0 -");
2157
2158 /*
2159 * Add any pre-filters in the PPD file...
2160 */
2161
2162 if ((ppdattr = ppdFindAttr(ppd, "cupsPreFilter", NULL)) != NULL)
2163 {
2164 p->prefiltertype = mimeAddType(MimeDatabase, "prefilter", p->name);
2165
2166 for (; ppdattr; ppdattr = ppdFindNextAttr(ppd, "cupsPreFilter", NULL))
2167 if (ppdattr->value)
2168 add_printer_filter(p, p->prefiltertype, ppdattr->value);
2169 }
ef416fc2 2170
2171 /*
2172 * Add any filters in the PPD file...
2173 */
2174
2175 DEBUG_printf(("ppd->num_filters = %d\n", ppd->num_filters));
2176 for (i = 0; i < ppd->num_filters; i ++)
2177 {
2178 DEBUG_printf(("ppd->filters[%d] = \"%s\"\n", i, ppd->filters[i]));
f7deaa1a 2179 add_printer_filter(p, p->filetype, ppd->filters[i]);
ef416fc2 2180 }
2181
2182 if (ppd->num_filters == 0)
2183 {
2184 /*
2185 * If there are no filters, add a PostScript printing filter.
2186 */
2187
f7deaa1a 2188 add_printer_filter(p, p->filetype,
2189 "application/vnd.cups-postscript 0 -");
ef416fc2 2190 }
2191
2192 /*
2193 * Show current and available port monitors for this printer...
2194 */
2195
09a101d6 2196 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME, "port-monitor",
ef416fc2 2197 NULL, p->port_monitor ? p->port_monitor : "none");
2198
ef416fc2 2199 for (i = 1, ppdattr = ppdFindAttr(ppd, "cupsPortMonitor", NULL);
2200 ppdattr;
2201 i ++, ppdattr = ppdFindNextAttr(ppd, "cupsPortMonitor", NULL));
2202
2203 if (ppd->protocols)
2204 {
2205 if (strstr(ppd->protocols, "TBCP"))
2206 i ++;
2207 else if (strstr(ppd->protocols, "BCP"))
2208 i ++;
2209 }
2210
09a101d6 2211 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
ef416fc2 2212 "port-monitor-supported", i, NULL, NULL);
2213
757d2cad 2214 attr->values[0].string.text = _cupsStrAlloc("none");
ef416fc2 2215
2216 for (i = 1, ppdattr = ppdFindAttr(ppd, "cupsPortMonitor", NULL);
2217 ppdattr;
2218 i ++, ppdattr = ppdFindNextAttr(ppd, "cupsPortMonitor", NULL))
757d2cad 2219 attr->values[i].string.text = _cupsStrAlloc(ppdattr->value);
ef416fc2 2220
2221 if (ppd->protocols)
2222 {
2223 if (strstr(ppd->protocols, "TBCP"))
757d2cad 2224 attr->values[i].string.text = _cupsStrAlloc("tbcp");
ef416fc2 2225 else if (strstr(ppd->protocols, "BCP"))
757d2cad 2226 attr->values[i].string.text = _cupsStrAlloc("bcp");
ef416fc2 2227 }
2228
f7deaa1a 2229#ifdef HAVE_DNSSD
2230 cupsdSetString(&p->product, ppd->product);
2231#endif /* HAVE_DNSSD */
2232
09a101d6 2233 if (ppdFindAttr(ppd, "APRemoteQueueID", NULL))
2234 p->type |= CUPS_PRINTER_REMOTE;
3d8365b8 2235
ef416fc2 2236 /*
2237 * Close the PPD and set the type...
2238 */
2239
2240 ppdClose(ppd);
ef416fc2 2241 }
2242 else if (!access(filename, 0))
2243 {
2244 int pline; /* PPD line number */
2245 ppd_status_t pstatus; /* PPD load status */
2246
2247
2248 pstatus = ppdLastError(&pline);
2249
b423cd4c 2250 cupsdLogMessage(CUPSD_LOG_ERROR, "PPD file for %s cannot be loaded!",
2251 p->name);
ef416fc2 2252
2253 if (pstatus <= PPD_ALLOC_ERROR)
2254 cupsdLogMessage(CUPSD_LOG_ERROR, "%s", strerror(errno));
2255 else
b423cd4c 2256 cupsdLogMessage(CUPSD_LOG_ERROR, "%s on line %d.",
2257 ppdErrorString(pstatus), pline);
ef416fc2 2258
b423cd4c 2259 cupsdLogMessage(CUPSD_LOG_INFO,
2260 "Hint: Run \"cupstestppd %s\" and fix any errors.",
2261 filename);
ef416fc2 2262
2263 /*
2264 * Add a filter from application/vnd.cups-raw to printer/name to
2265 * handle "raw" printing by users.
2266 */
2267
f7deaa1a 2268 add_printer_filter(p, p->filetype, "application/vnd.cups-raw 0 -");
ef416fc2 2269
2270 /*
2271 * Add a PostScript filter, since this is still possibly PS printer.
2272 */
2273
f7deaa1a 2274 add_printer_filter(p, p->filetype,
2275 "application/vnd.cups-postscript 0 -");
ef416fc2 2276 }
2277 else
2278 {
2279 /*
2280 * If we have an interface script, add a filter entry for it...
2281 */
2282
2283 snprintf(filename, sizeof(filename), "%s/interfaces/%s", ServerRoot,
2284 p->name);
b423cd4c 2285 if (!access(filename, X_OK))
ef416fc2 2286 {
2287 /*
2288 * Yes, we have a System V style interface script; use it!
2289 */
2290
2291 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
f7deaa1a 2292 "printer-make-and-model", NULL,
2293 "Local System V Printer");
ef416fc2 2294
2295 snprintf(filename, sizeof(filename), "*/* 0 %s/interfaces/%s",
2296 ServerRoot, p->name);
f7deaa1a 2297 add_printer_filter(p, p->filetype, filename);
ef416fc2 2298 }
2299 else if (p->device_uri &&
2300 !strncmp(p->device_uri, "ipp://", 6) &&
2301 (strstr(p->device_uri, "/printers/") != NULL ||
2302 strstr(p->device_uri, "/classes/") != NULL))
2303 {
2304 /*
2305 * Tell the client this is really a hard-wired remote printer.
2306 */
2307
09a101d6 2308 p->type |= CUPS_PRINTER_REMOTE;
ef416fc2 2309
2310 /*
2311 * Point the printer-uri-supported attribute to the
2312 * remote printer...
2313 */
2314
2315 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI,
2316 "printer-uri-supported", NULL, p->device_uri);
2317
2318 /*
2319 * Then set the make-and-model accordingly...
2320 */
2321
2322 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
2323 "printer-make-and-model", NULL, "Remote Printer");
2324
2325 /*
2326 * Print all files directly...
2327 */
2328
d09495fa 2329 p->raw = 1;
2330 p->remote = 1;
ef416fc2 2331 }
2332 else
2333 {
2334 /*
2335 * Otherwise we have neither - treat this as a "dumb" printer
2336 * with no PPD file...
2337 */
2338
2339 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
2340 "printer-make-and-model", NULL, "Local Raw Printer");
2341
2342 p->raw = 1;
2343 }
2344 }
2345
2346 ippAddIntegers(p->attrs, IPP_TAG_PRINTER, IPP_TAG_ENUM,
b94498cf 2347 "finishings-supported", num_finishings, finishings);
ef416fc2 2348 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_ENUM,
2349 "finishings-default", IPP_FINISHINGS_NONE);
2350 }
2351 }
2352
b423cd4c 2353 /*
bc44d920 2354 * Force sharing off for remote queues...
b423cd4c 2355 */
2356
bc44d920 2357 if (p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT))
2358 p->shared = 0;
2359 else
b423cd4c 2360 {
bc44d920 2361 /*
2362 * Copy the printer options into a browse attributes string we can re-use.
2363 */
2364
b423cd4c 2365 const char *valptr; /* Pointer into value */
2366 char *attrptr; /* Pointer into attribute string */
2367
2368
2369 /*
2370 * Free the old browse attributes as needed...
2371 */
2372
2373 if (p->browse_attrs)
2374 free(p->browse_attrs);
2375
2376 /*
2377 * Compute the length of all attributes + job-sheets, lease-duration,
2378 * and BrowseLocalOptions.
2379 */
2380
2381 for (length = 1, i = p->num_options, option = p->options;
2382 i > 0;
2383 i --, option ++)
2384 {
2385 length += strlen(option->name) + 2;
2386
2387 if (option->value)
2388 {
2389 for (valptr = option->value; *valptr; valptr ++)
2390 if (strchr(" \"\'\\", *valptr))
2391 length += 2;
2392 else
2393 length ++;
2394 }
2395 }
2396
2397 length += 13 + strlen(p->job_sheets[0]) + strlen(p->job_sheets[1]);
2398 length += 32;
2399 if (BrowseLocalOptions)
f7deaa1a 2400 length += 12 + strlen(BrowseLocalOptions);
b423cd4c 2401
7594b224 2402 if (p->num_auth_info_required > 0)
2403 {
2404 length += 18; /* auth-info-required */
2405
2406 for (i = 0; i < p->num_auth_info_required; i ++)
2407 length += strlen(p->auth_info_required[i]) + 1;
2408 }
2409
b423cd4c 2410 /*
2411 * Allocate the new string...
2412 */
f7deaa1a 2413
b423cd4c 2414 if ((p->browse_attrs = calloc(1, length)) == NULL)
2415 cupsdLogMessage(CUPSD_LOG_ERROR,
2416 "Unable to allocate %d bytes for browse data!",
2417 length);
2418 else
2419 {
2420 /*
2421 * Got the allocated string, now copy the options and attributes over...
2422 */
2423
2424 sprintf(p->browse_attrs, "job-sheets=%s,%s lease-duration=%d",
2425 p->job_sheets[0], p->job_sheets[1], BrowseTimeout);
2426 attrptr = p->browse_attrs + strlen(p->browse_attrs);
2427
2428 if (BrowseLocalOptions)
2429 {
2430 sprintf(attrptr, " ipp-options=%s", BrowseLocalOptions);
2431 attrptr += strlen(attrptr);
2432 }
2433
2434 for (i = p->num_options, option = p->options;
2435 i > 0;
2436 i --, option ++)
2437 {
2438 *attrptr++ = ' ';
2439 strcpy(attrptr, option->name);
2440 attrptr += strlen(attrptr);
2441
2442 if (option->value)
2443 {
2444 *attrptr++ = '=';
2445
2446 for (valptr = option->value; *valptr; valptr ++)
2447 {
2448 if (strchr(" \"\'\\", *valptr))
2449 *attrptr++ = '\\';
2450
2451 *attrptr++ = *valptr;
2452 }
2453 }
2454 }
2455
7594b224 2456 if (p->num_auth_info_required > 0)
2457 {
2458 strcpy(attrptr, "auth-info-required");
2459 attrptr += 18;
2460
2461 for (i = 0; i < p->num_auth_info_required; i ++)
2462 {
2463 *attrptr++ = i ? ',' : '=';
2464 strcpy(attrptr, p->auth_info_required[i]);
2465 attrptr += strlen(attrptr);
2466 }
2467 }
2468 else
2469 *attrptr = '\0';
b423cd4c 2470 }
2471 }
2472
bd7854cb 2473 /*
2474 * Populate the document-format-supported attribute...
2475 */
2476
2477 add_printer_formats(p);
2478
ef416fc2 2479 DEBUG_printf(("cupsdSetPrinterAttrs: leaving name = %s, type = %x\n", p->name,
2480 p->type));
2481
b423cd4c 2482 /*
2483 * Add name-default attributes...
2484 */
2485
2486 add_printer_defaults(p);
2487
ef416fc2 2488#ifdef __sgi
2489 /*
2490 * Write the IRIX printer config and status files...
2491 */
2492
2493 write_irix_config(p);
2494 write_irix_state(p);
2495#endif /* __sgi */
f7deaa1a 2496
2497 /*
2498 * Let the browse protocols reflect the change
2499 */
2500
2501 cupsdRegisterPrinter(p);
ef416fc2 2502}
2503
2504
2505/*
2506 * 'cupsdSetPrinterReasons()' - Set/update the reasons strings.
2507 */
2508
2509void
2510cupsdSetPrinterReasons(
2511 cupsd_printer_t *p, /* I - Printer */
2512 const char *s) /* I - Reasons strings */
2513{
2514 int i; /* Looping var */
2515 const char *sptr; /* Pointer into reasons */
2516 char reason[255], /* Reason string */
2517 *rptr; /* Pointer into reason */
2518
2519
2520 if (s[0] == '-' || s[0] == '+')
2521 {
2522 /*
2523 * Add/remove reasons...
2524 */
2525
2526 sptr = s + 1;
2527 }
2528 else
2529 {
2530 /*
2531 * Replace reasons...
2532 */
2533
2534 sptr = s;
2535
2536 for (i = 0; i < p->num_reasons; i ++)
2537 free(p->reasons[i]);
2538
2539 p->num_reasons = 0;
2540 }
2541
bc44d920 2542 if (!strcmp(s, "none"))
2543 return;
2544
ef416fc2 2545 /*
2546 * Loop through all of the reasons...
2547 */
2548
2549 while (*sptr)
2550 {
2551 /*
2552 * Skip leading whitespace and commas...
2553 */
2554
2555 while (isspace(*sptr & 255) || *sptr == ',')
2556 sptr ++;
2557
2558 for (rptr = reason; *sptr && !isspace(*sptr & 255) && *sptr != ','; sptr ++)
2559 if (rptr < (reason + sizeof(reason) - 1))
2560 *rptr++ = *sptr;
2561
2562 if (rptr == reason)
2563 break;
2564
2565 *rptr = '\0';
2566
2567 if (s[0] == '-')
2568 {
2569 /*
2570 * Remove reason...
2571 */
2572
2573 for (i = 0; i < p->num_reasons; i ++)
2574 if (!strcasecmp(reason, p->reasons[i]))
2575 {
2576 /*
2577 * Found a match, so remove it...
2578 */
2579
2580 p->num_reasons --;
2581 free(p->reasons[i]);
2582
2583 if (i < p->num_reasons)
2584 memmove(p->reasons + i, p->reasons + i + 1,
2585 (p->num_reasons - i) * sizeof(char *));
2586
2587 i --;
c0e1af83 2588
2589 if (!strcmp(reason, "paused") && p->state == IPP_PRINTER_STOPPED)
2590 cupsdSetPrinterState(p, IPP_PRINTER_IDLE, 1);
ef416fc2 2591 }
2592 }
2593 else if (p->num_reasons < (int)(sizeof(p->reasons) / sizeof(p->reasons[0])))
2594 {
2595 /*
2596 * Add reason...
2597 */
2598
2599 for (i = 0; i < p->num_reasons; i ++)
2600 if (!strcasecmp(reason, p->reasons[i]))
2601 break;
2602
2603 if (i >= p->num_reasons)
2604 {
2605 p->reasons[i] = strdup(reason);
2606 p->num_reasons ++;
c0e1af83 2607
2608 if (!strcmp(reason, "paused") && p->state != IPP_PRINTER_STOPPED)
2609 cupsdSetPrinterState(p, IPP_PRINTER_STOPPED, 1);
ef416fc2 2610 }
2611 }
2612 }
2613}
2614
2615
2616/*
2617 * 'cupsdSetPrinterState()' - Update the current state of a printer.
2618 */
2619
2620void
2621cupsdSetPrinterState(
2622 cupsd_printer_t *p, /* I - Printer to change */
2623 ipp_pstate_t s, /* I - New state */
2624 int update) /* I - Update printers.conf? */
2625{
2626 ipp_pstate_t old_state; /* Old printer state */
2627
2628
2629 /*
2630 * Can't set status of remote printers...
2631 */
2632
09a101d6 2633 if (p->type & CUPS_PRINTER_DISCOVERED)
ef416fc2 2634 return;
2635
2636 /*
2637 * Set the new state...
2638 */
2639
2640 old_state = p->state;
2641 p->state = s;
2642
2643 if (old_state != s)
2644 {
0a682745
MS
2645 cupsdAddEvent(s == IPP_PRINTER_STOPPED ? CUPSD_EVENT_PRINTER_STOPPED :
2646 CUPSD_EVENT_PRINTER_STATE_CHANGED, p, NULL,
e53920b9 2647 "%s \"%s\" state changed.",
2648 (p->type & CUPS_PRINTER_CLASS) ? "Class" : "Printer",
2649 p->name);
2650
ef416fc2 2651 /*
2652 * Let the browse code know this needs to be updated...
2653 */
2654
2655 BrowseNext = p;
2656 p->state_time = time(NULL);
2657 p->browse_time = 0;
2658
2659#ifdef __sgi
2660 write_irix_state(p);
2661#endif /* __sgi */
2662 }
2663
2664 cupsdAddPrinterHistory(p);
2665
f7deaa1a 2666 /*
2667 * Let the browse protocols reflect the change...
2668 */
2669
2670 cupsdRegisterPrinter(p);
2671
ef416fc2 2672 /*
2673 * Save the printer configuration if a printer goes from idle or processing
2674 * to stopped (or visa-versa)...
2675 */
2676
2677 if ((old_state == IPP_PRINTER_STOPPED) != (s == IPP_PRINTER_STOPPED) &&
2678 update)
2679 {
2680 if (p->type & CUPS_PRINTER_CLASS)
2681 cupsdSaveAllClasses();
2682 else
2683 cupsdSaveAllPrinters();
2684 }
2685}
2686
2687
2688/*
2689 * 'cupsdStopPrinter()' - Stop a printer from printing any jobs...
2690 */
2691
2692void
2693cupsdStopPrinter(cupsd_printer_t *p, /* I - Printer to stop */
2694 int update)/* I - Update printers.conf? */
2695{
2696 cupsd_job_t *job; /* Active print job */
2697
2698
2699 /*
2700 * Set the printer state...
2701 */
2702
2703 cupsdSetPrinterState(p, IPP_PRINTER_STOPPED, update);
2704
2705 /*
2706 * See if we have a job printing on this printer...
2707 */
2708
2709 if (p->job)
2710 {
2711 /*
2712 * Get pointer to job...
2713 */
2714
2715 job = (cupsd_job_t *)p->job;
2716
2717 /*
2718 * Stop it...
2719 */
2720
2721 cupsdStopJob(job, 0);
2722
2723 /*
2724 * Reset the state to pending...
2725 */
2726
2727 job->state->values[0].integer = IPP_JOB_PENDING;
bd7854cb 2728 job->state_value = IPP_JOB_PENDING;
ef416fc2 2729
2730 cupsdSaveJob(job);
07725fee 2731
2732 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED, p, job,
2733 "Job stopped due to printer being paused");
ef416fc2 2734 }
2735}
2736
2737
2738/*
2739 * 'cupsdUpdatePrinters()' - Update printers after a partial reload.
2740 */
2741
2742void
2743cupsdUpdatePrinters(void)
2744{
2745 cupsd_printer_t *p; /* Current printer */
2746
2747
2748 /*
2749 * Loop through the printers and recreate the printer attributes
2750 * for any local printers since the policy and/or access control
2751 * stuff may have changed. Also, if browsing is disabled, remove
2752 * any remote printers...
2753 */
2754
2755 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
2756 p;
2757 p = (cupsd_printer_t *)cupsArrayNext(Printers))
2758 {
07725fee 2759 /*
2760 * Remove remote printers if we are no longer browsing...
2761 */
2762
09a101d6 2763 if (!Browsing &&
2764 (p->type & (CUPS_PRINTER_IMPLICIT | CUPS_PRINTER_DISCOVERED)))
ef416fc2 2765 {
2766 if (p->type & CUPS_PRINTER_IMPLICIT)
2767 cupsArrayRemove(ImplicitPrinters, p);
2768
2769 cupsArraySave(Printers);
2770 cupsdDeletePrinter(p, 0);
2771 cupsArrayRestore(Printers);
2772 continue;
2773 }
ef416fc2 2774
2775 /*
2776 * Update the operation policy pointer...
2777 */
2778
2779 if ((p->op_policy_ptr = cupsdFindPolicy(p->op_policy)) == NULL)
2780 p->op_policy_ptr = DefaultPolicyPtr;
07725fee 2781
2782 /*
2783 * Update printer attributes as needed...
2784 */
2785
09a101d6 2786 if (!(p->type & CUPS_PRINTER_DISCOVERED))
07725fee 2787 cupsdSetPrinterAttrs(p);
ef416fc2 2788 }
2789}
2790
2791
2792/*
2793 * 'cupsdValidateDest()' - Validate a printer/class destination.
2794 */
2795
2796const char * /* O - Printer or class name */
2797cupsdValidateDest(
f7deaa1a 2798 const char *uri, /* I - Printer URI */
ef416fc2 2799 cups_ptype_t *dtype, /* O - Type (printer or class) */
2800 cupsd_printer_t **printer) /* O - Printer pointer */
2801{
2802 cupsd_printer_t *p; /* Current printer */
2803 char localname[1024],/* Localized hostname */
2804 *lptr, /* Pointer into localized hostname */
f7deaa1a 2805 *sptr, /* Pointer into server name */
2806 *rptr, /* Pointer into resource */
2807 scheme[32], /* Scheme portion of URI */
2808 username[64], /* Username portion of URI */
2809 hostname[HTTP_MAX_HOST],
2810 /* Host portion of URI */
2811 resource[HTTP_MAX_URI];
2812 /* Resource portion of URI */
2813 int port; /* Port portion of URI */
2814
2815
2816 DEBUG_printf(("cupsdValidateDest(uri=\"%s\", dtype=%p, printer=%p)\n", uri,
ef416fc2 2817 dtype, printer));
2818
2819 /*
2820 * Initialize return values...
2821 */
2822
2823 if (printer)
2824 *printer = NULL;
2825
f7deaa1a 2826 if (dtype)
2827 *dtype = (cups_ptype_t)0;
2828
2829 /*
2830 * Pull the hostname and resource from the URI...
2831 */
2832
2833 httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, sizeof(scheme),
2834 username, sizeof(username), hostname, sizeof(hostname),
2835 &port, resource, sizeof(resource));
ef416fc2 2836
2837 /*
2838 * See if the resource is a class or printer...
2839 */
2840
2841 if (!strncmp(resource, "/classes/", 9))
2842 {
2843 /*
2844 * Class...
2845 */
2846
f7deaa1a 2847 rptr = resource + 9;
ef416fc2 2848 }
2849 else if (!strncmp(resource, "/printers/", 10))
2850 {
2851 /*
2852 * Printer...
2853 */
2854
f7deaa1a 2855 rptr = resource + 10;
ef416fc2 2856 }
2857 else
2858 {
2859 /*
2860 * Bad resource name...
2861 */
2862
2863 return (NULL);
2864 }
2865
2866 /*
2867 * See if the printer or class name exists...
2868 */
2869
f7deaa1a 2870 p = cupsdFindDest(rptr);
ef416fc2 2871
f7deaa1a 2872 if (p == NULL && strchr(rptr, '@') == NULL)
ef416fc2 2873 return (NULL);
2874 else if (p != NULL)
2875 {
2876 if (printer)
2877 *printer = p;
2878
f7deaa1a 2879 if (dtype)
2880 *dtype = p->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT |
09a101d6 2881 CUPS_PRINTER_REMOTE | CUPS_PRINTER_DISCOVERED);
f7deaa1a 2882
ef416fc2 2883 return (p->name);
2884 }
2885
2886 /*
2887 * Change localhost to the server name...
2888 */
2889
2890 if (!strcasecmp(hostname, "localhost"))
f7deaa1a 2891 strlcpy(hostname, ServerName, sizeof(hostname));
ef416fc2 2892
2893 strlcpy(localname, hostname, sizeof(localname));
2894
2895 if (!strcasecmp(hostname, ServerName))
2896 {
2897 /*
2898 * Localize the hostname...
2899 */
2900
2901 lptr = strchr(localname, '.');
2902 sptr = strchr(ServerName, '.');
2903
2904 if (sptr != NULL && lptr != NULL)
2905 {
2906 /*
2907 * Strip the common domain name components...
2908 */
2909
2910 while (lptr != NULL)
2911 {
2912 if (!strcasecmp(lptr, sptr))
2913 {
2914 *lptr = '\0';
2915 break;
2916 }
2917 else
2918 lptr = strchr(lptr + 1, '.');
2919 }
2920 }
2921 }
2922
2923 DEBUG_printf(("localized hostname is \"%s\"...\n", localname));
2924
2925 /*
2926 * Find a matching printer or class...
2927 */
2928
2929 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
2930 p;
2931 p = (cupsd_printer_t *)cupsArrayNext(Printers))
2932 if (!strcasecmp(p->hostname, localname) &&
f7deaa1a 2933 !strcasecmp(p->name, rptr))
ef416fc2 2934 {
2935 if (printer)
2936 *printer = p;
2937
f7deaa1a 2938 if (dtype)
2939 *dtype = p->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT |
09a101d6 2940 CUPS_PRINTER_REMOTE | CUPS_PRINTER_DISCOVERED);
f7deaa1a 2941
ef416fc2 2942 return (p->name);
2943 }
2944
2945 return (NULL);
2946}
2947
2948
2949/*
2950 * 'cupsdWritePrintcap()' - Write a pseudo-printcap file for older applications
2951 * that need it...
2952 */
2953
2954void
2955cupsdWritePrintcap(void)
2956{
2957 cups_file_t *fp; /* printcap file */
2958 cupsd_printer_t *p; /* Current printer */
2959
2960
2961#ifdef __sgi
2962 /*
2963 * Update the IRIX printer state for the default printer; if
2964 * no printers remain, then the default printer file will be
2965 * removed...
2966 */
2967
2968 write_irix_state(DefaultPrinter);
2969#endif /* __sgi */
2970
2971 /*
2972 * See if we have a printcap file; if not, don't bother writing it.
2973 */
2974
2975 if (!Printcap || !*Printcap)
2976 return;
2977
2978 /*
2979 * Open the printcap file...
2980 */
2981
2982 if ((fp = cupsFileOpen(Printcap, "w")) == NULL)
2983 return;
2984
2985 /*
2986 * Put a comment header at the top so that users will know where the
2987 * data has come from...
2988 */
2989
c277e2f8
MS
2990 cupsFilePuts(fp,
2991 "# This file was automatically generated by cupsd(8) from the\n");
ef416fc2 2992 cupsFilePrintf(fp, "# %s/printers.conf file. All changes to this file\n",
2993 ServerRoot);
2994 cupsFilePuts(fp, "# will be lost.\n");
2995
2996 if (Printers)
2997 {
2998 /*
2999 * Write a new printcap with the current list of printers.
3000 */
3001
3002 switch (PrintcapFormat)
3003 {
3004 case PRINTCAP_BSD:
3005 /*
3006 * Each printer is put in the file as:
3007 *
3008 * Printer1:
3009 * Printer2:
3010 * Printer3:
3011 * ...
3012 * PrinterN:
3013 */
3014
3015 if (DefaultPrinter)
3016 cupsFilePrintf(fp, "%s|%s:rm=%s:rp=%s:\n", DefaultPrinter->name,
c277e2f8
MS
3017 DefaultPrinter->info, ServerName,
3018 DefaultPrinter->name);
ef416fc2 3019
3020 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
3021 p;
3022 p = (cupsd_printer_t *)cupsArrayNext(Printers))
3023 if (p != DefaultPrinter)
3024 cupsFilePrintf(fp, "%s|%s:rm=%s:rp=%s:\n", p->name, p->info,
c277e2f8 3025 ServerName, p->name);
ef416fc2 3026 break;
3027
3028 case PRINTCAP_SOLARIS:
3029 /*
3030 * Each printer is put in the file as:
3031 *
3032 * _all:all=Printer1,Printer2,Printer3,...,PrinterN
3033 * _default:use=DefaultPrinter
3034 * Printer1:\
3035 * :bsdaddr=ServerName,Printer1:\
3036 * :description=Description:
3037 * Printer2:
3038 * :bsdaddr=ServerName,Printer2:\
3039 * :description=Description:
3040 * Printer3:
3041 * :bsdaddr=ServerName,Printer3:\
3042 * :description=Description:
3043 * ...
3044 * PrinterN:
3045 * :bsdaddr=ServerName,PrinterN:\
3046 * :description=Description:
3047 */
3048
3049 cupsFilePuts(fp, "_all:all=");
3050 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
3051 p;
3052 p = (cupsd_printer_t *)cupsArrayCurrent(Printers))
3053 cupsFilePrintf(fp, "%s%c", p->name,
3054 cupsArrayNext(Printers) ? ',' : '\n');
3055
3056 if (DefaultPrinter)
3057 cupsFilePrintf(fp, "_default:use=%s\n", DefaultPrinter->name);
3058
3059 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
3060 p;
3061 p = (cupsd_printer_t *)cupsArrayNext(Printers))
3062 cupsFilePrintf(fp, "%s:\\\n"
c277e2f8
MS
3063 "\t:bsdaddr=%s,%s:\\\n"
3064 "\t:description=%s:\n",
3065 p->name, ServerName, p->name,
3066 p->info ? p->info : "");
ef416fc2 3067 break;
3068 }
3069 }
3070
3071 /*
3072 * Close the file...
3073 */
3074
3075 cupsFileClose(fp);
3076}
3077
3078
3079/*
3080 * 'cupsdSanitizeURI()' - Sanitize a device URI...
3081 */
3082
3083char * /* O - New device URI */
3084cupsdSanitizeURI(const char *uri, /* I - Original device URI */
3085 char *buffer, /* O - New device URI */
3086 int buflen) /* I - Size of new device URI buffer */
3087{
3088 char *start, /* Start of data after scheme */
3089 *slash, /* First slash after scheme:// */
3090 *ptr; /* Pointer into user@host:port part */
3091
3092
3093 /*
3094 * Range check input...
3095 */
3096
3097 if (!uri || !buffer || buflen < 2)
3098 return (NULL);
3099
3100 /*
3101 * Copy the device URI to the new buffer...
3102 */
3103
3104 strlcpy(buffer, uri, buflen);
3105
3106 /*
3107 * Find the end of the scheme:// part...
3108 */
3109
3110 if ((ptr = strchr(buffer, ':')) == NULL)
3111 return (buffer); /* No scheme: part... */
3112
3113 for (start = ptr + 1; *start; start ++)
3114 if (*start != '/')
3115 break;
3116
3117 /*
3118 * Find the next slash (/) in the URI...
3119 */
3120
3121 if ((slash = strchr(start, '/')) == NULL)
3122 slash = start + strlen(start); /* No slash, point to the end */
3123
3124 /*
3125 * Check for an @ sign before the slash...
3126 */
3127
3128 if ((ptr = strchr(start, '@')) != NULL && ptr < slash)
3129 {
3130 /*
3131 * Found an @ sign and it is before the resource part, so we have
3132 * an authentication string. Copy the remaining URI over the
3133 * authentication string...
3134 */
3135
3136 _cups_strcpy(start, ptr + 1);
3137 }
3138
3139 /*
3140 * Return the new device URI...
3141 */
3142
3143 return (buffer);
3144}
3145
3146
b423cd4c 3147/*
3148 * 'add_printer_defaults()' - Add name-default attributes to the printer attributes.
3149 */
3150
3151static void
3152add_printer_defaults(cupsd_printer_t *p)/* I - Printer */
3153{
3154 int i; /* Looping var */
3155 int num_options; /* Number of default options */
3156 cups_option_t *options, /* Default options */
3157 *option; /* Current option */
3158 char name[256]; /* name-default */
3159
3160
f7deaa1a 3161 /*
3162 * Maintain a common array of default attribute names...
3163 */
3164
3165 if (!CommonDefaults)
3166 {
3167 CommonDefaults = cupsArrayNew((cups_array_func_t)strcmp, NULL);
3168
3169 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("copies-default"));
3170 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("document-format-default"));
3171 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("finishings-default"));
3172 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("job-hold-until-default"));
3173 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("job-priority-default"));
3174 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("job-sheets-default"));
3175 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("media-default"));
3176 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("number-up-default"));
3177 cupsArrayAdd(CommonDefaults,
3178 _cupsStrAlloc("orientation-requested-default"));
3179 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("sides-default"));
3180 }
3181
b423cd4c 3182 /*
3183 * Add all of the default options from the .conf files...
3184 */
3185
3186 for (num_options = 0, i = p->num_options, option = p->options;
3187 i > 0;
3188 i --, option ++)
3189 {
3190 if (strcmp(option->name, "ipp-options") &&
3191 strcmp(option->name, "job-sheets") &&
3192 strcmp(option->name, "lease-duration"))
3193 {
3194 snprintf(name, sizeof(name), "%s-default", option->name);
3195 num_options = cupsAddOption(name, option->value, num_options, &options);
f7deaa1a 3196
3197 if (!cupsArrayFind(CommonDefaults, name))
3198 cupsArrayAdd(CommonDefaults, _cupsStrAlloc(name));
b423cd4c 3199 }
3200 }
3201
3202 /*
3203 * Convert options to IPP attributes...
3204 */
3205
3206 cupsEncodeOptions2(p->attrs, num_options, options, IPP_TAG_PRINTER);
3207 cupsFreeOptions(num_options, options);
3208
3209 /*
3210 * Add standard -default attributes as needed...
3211 */
3212
3213 if (!cupsGetOption("copies", p->num_options, p->options))
3214 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER, "copies-default",
3215 1);
3216
f7deaa1a 3217 if (!cupsGetOption("document-format", p->num_options, p->options))
3218 ippAddString(CommonData, IPP_TAG_PRINTER, IPP_TAG_MIMETYPE,
3219 "document-format-default", NULL, "application/octet-stream");
3220
b423cd4c 3221 if (!cupsGetOption("job-hold-until", p->num_options, p->options))
3222 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
3223 "job-hold-until-default", NULL, "no-hold");
3224
3225 if (!cupsGetOption("job-priority", p->num_options, p->options))
3226 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
3227 "job-priority-default", 50);
3228
3229 if (!cupsGetOption("number-up", p->num_options, p->options))
3230 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
3231 "number-up-default", 1);
3232
3233 if (!cupsGetOption("orientation-requested", p->num_options, p->options))
c277e2f8
MS
3234 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NOVALUE,
3235 "orientation-requested-default", NULL, NULL);
f7deaa1a 3236
3237 if (!cupsGetOption("notify-lease-duration", p->num_options, p->options))
3238 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
3239 "notify-lease-duration-default", DefaultLeaseDuration);
3240
3241 if (!cupsGetOption("notify-events", p->num_options, p->options))
3242 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
3243 "notify-events-default", NULL, "job-completed");
b423cd4c 3244}
3245
3246
bd7854cb 3247/*
3248 * 'add_printer_filter()' - Add a MIME filter for a printer.
3249 */
3250
3251static void
3252add_printer_filter(
3253 cupsd_printer_t *p, /* I - Printer to add to */
f7deaa1a 3254 mime_type_t *filtertype, /* I - Filter or prefilter MIME type */
bd7854cb 3255 const char *filter) /* I - Filter to add */
3256{
3257 char super[MIME_MAX_SUPER], /* Super-type for filter */
3258 type[MIME_MAX_TYPE], /* Type for filter */
3259 program[1024]; /* Program/filter name */
3260 int cost; /* Cost of filter */
3261 mime_type_t *temptype; /* MIME type looping var */
3262 char filename[1024]; /* Full filter filename */
3263
3264
3265 /*
3266 * Parse the filter string; it should be in the following format:
3267 *
3268 * super/type cost program
3269 */
3270
3271 if (sscanf(filter, "%15[^/]/%31s%d%1023s", super, type, &cost, program) != 4)
3272 {
3273 cupsdLogMessage(CUPSD_LOG_ERROR, "%s: invalid filter string \"%s\"!",
3274 p->name, filter);
3275 return;
3276 }
3277
3278 /*
3279 * See if the filter program exists; if not, stop the printer and flag
3280 * the error!
3281 */
3282
ecdc0628 3283 if (strcmp(program, "-"))
bd7854cb 3284 {
ecdc0628 3285 if (program[0] == '/')
3286 strlcpy(filename, program, sizeof(filename));
3287 else
3288 snprintf(filename, sizeof(filename), "%s/filter/%s", ServerBin, program);
3289
3290 if (access(filename, X_OK))
3291 {
3292 snprintf(p->state_message, sizeof(p->state_message),
3293 "Filter \"%s\" for printer \"%s\" not available: %s",
3294 program, p->name, strerror(errno));
ecdc0628 3295 cupsdSetPrinterReasons(p, "+cups-missing-filter-error");
07725fee 3296 cupsdSetPrinterState(p, IPP_PRINTER_STOPPED, 0);
ecdc0628 3297
3298 cupsdLogMessage(CUPSD_LOG_ERROR, "%s", p->state_message);
3299 }
bd7854cb 3300 }
3301
b423cd4c 3302 /*
3303 * Mark the CUPS_PRINTER_COMMANDS bit if we have a filter for
3304 * application/vnd.cups-command...
3305 */
3306
3307 if (!strcasecmp(super, "application") &&
3308 !strcasecmp(type, "vnd.cups-command"))
3309 p->type |= CUPS_PRINTER_COMMANDS;
3310
bd7854cb 3311 /*
3312 * Add the filter to the MIME database, supporting wildcards as needed...
3313 */
3314
3315 for (temptype = mimeFirstType(MimeDatabase);
3316 temptype;
3317 temptype = mimeNextType(MimeDatabase))
3318 if (((super[0] == '*' && strcasecmp(temptype->super, "printer")) ||
3319 !strcasecmp(temptype->super, super)) &&
3320 (type[0] == '*' || !strcasecmp(temptype->type, type)))
3321 {
3322 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3323 "add_printer_filter: %s: adding filter %s/%s %s/%s %d %s",
3324 p->name, temptype->super, temptype->type,
f7deaa1a 3325 filtertype->super, filtertype->type,
bd7854cb 3326 cost, program);
f7deaa1a 3327 mimeAddFilter(MimeDatabase, temptype, filtertype, cost, program);
bd7854cb 3328 }
3329}
3330
3331
3332/*
3333 * 'add_printer_formats()' - Add document-format-supported values for a printer.
3334 */
3335
3336static void
3337add_printer_formats(cupsd_printer_t *p) /* I - Printer */
3338{
3339 int i; /* Looping var */
3340 mime_type_t *type; /* Current MIME type */
3341 cups_array_t *filters; /* Filters */
80ca4592 3342 ipp_attribute_t *attr; /* document-format-supported attribute */
bd7854cb 3343 char mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2];
3344 /* MIME type name */
3345
3346
3347 /*
3348 * Raw (and remote) queues advertise all of the supported MIME
3349 * types...
3350 */
3351
80ca4592 3352 cupsArrayDelete(p->filetypes);
3353 p->filetypes = NULL;
3354
bd7854cb 3355 if (p->raw)
3356 {
3357 ippAddStrings(p->attrs, IPP_TAG_PRINTER,
3358 (ipp_tag_t)(IPP_TAG_MIMETYPE | IPP_TAG_COPY),
3359 "document-format-supported", NumMimeTypes, NULL, MimeTypes);
3360 return;
3361 }
3362
3363 /*
3364 * Otherwise, loop through the supported MIME types and see if there
3365 * are filters for them...
3366 */
3367
bd7854cb 3368 cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_printer_formats: %d types, %d filters",
3369 mimeNumTypes(MimeDatabase), mimeNumFilters(MimeDatabase));
3370
80ca4592 3371 p->filetypes = cupsArrayNew(NULL, NULL);
bd7854cb 3372
80ca4592 3373 for (type = mimeFirstType(MimeDatabase);
bd7854cb 3374 type;
3375 type = mimeNextType(MimeDatabase))
3376 {
bd7854cb 3377 snprintf(mimetype, sizeof(mimetype), "%s/%s", type->super, type->type);
3378
3379 if ((filters = mimeFilter(MimeDatabase, type, p->filetype, NULL)) != NULL)
3380 {
3381 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3382 "add_printer_formats: %s: %s needs %d filters",
3383 p->name, mimetype, cupsArrayCount(filters));
3384
3385 cupsArrayDelete(filters);
80ca4592 3386 cupsArrayAdd(p->filetypes, type);
bd7854cb 3387 }
3388 else
3389 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3390 "add_printer_formats: %s: %s not supported",
3391 p->name, mimetype);
3392 }
3393
3394 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3395 "add_printer_formats: %s: %d supported types",
80ca4592 3396 p->name, cupsArrayCount(p->filetypes) + 1);
bd7854cb 3397
3398 /*
3399 * Add the file formats that can be filtered...
3400 */
3401
f301802f 3402 if ((type = mimeType(MimeDatabase, "application", "octet-stream")) == NULL ||
3403 !cupsArrayFind(p->filetypes, type))
3404 i = 1;
3405 else
3406 i = 0;
bd7854cb 3407
80ca4592 3408 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_MIMETYPE,
3409 "document-format-supported",
3410 cupsArrayCount(p->filetypes) + 1, NULL, NULL);
3411
f301802f 3412 if (i)
3413 attr->values[0].string.text = _cupsStrAlloc("application/octet-stream");
bd7854cb 3414
f301802f 3415 for (type = (mime_type_t *)cupsArrayFirst(p->filetypes);
80ca4592 3416 type;
3417 i ++, type = (mime_type_t *)cupsArrayNext(p->filetypes))
3418 {
3419 snprintf(mimetype, sizeof(mimetype), "%s/%s", type->super, type->type);
bd7854cb 3420
80ca4592 3421 attr->values[i].string.text = _cupsStrAlloc(mimetype);
3422 }
f7deaa1a 3423
3424#ifdef HAVE_DNSSD
3425 {
3426 char pdl[1024]; /* Buffer to build pdl list */
3427 mime_filter_t *filter; /* MIME filter looping var */
3428
3429
3430 pdl[0] = '\0';
3431
3432 if (mimeType(MimeDatabase, "application", "pdf"))
3433 strlcat(pdl, "application/pdf,", sizeof(pdl));
3434
3435 if (mimeType(MimeDatabase, "application", "postscript"))
3436 strlcat(pdl, "application/postscript,", sizeof(pdl));
3437
3438 if (mimeType(MimeDatabase, "application", "vnd.cups-raster"))
3439 strlcat(pdl, "application/vnd.cups-raster,", sizeof(pdl));
3440
3441 /*
3442 * Determine if this is a Tioga PrintJobMgr based queue...
3443 */
3444
3445 for (filter = (mime_filter_t *)cupsArrayFirst(MimeDatabase->filters);
3446 filter;
3447 filter = (mime_filter_t *)cupsArrayNext(MimeDatabase->filters))
3448 {
3449 if (filter->dst == p->filetype && filter->filter &&
3450 strstr(filter->filter, "PrintJobMgr"))
3451 break;
3452 }
3453
3454 /*
3455 * We only support raw printing if this is not a Tioga PrintJobMgr based
3456 * queue and if application/octet-stream is a known conversion...
3457 */
3458
3459 if (!filter && mimeType(MimeDatabase, "application", "octet-stream"))
3460 strlcat(pdl, "application/octet-stream,", sizeof(pdl));
3461
3462 if (mimeType(MimeDatabase, "image", "png"))
3463 strlcat(pdl, "image/png,", sizeof(pdl));
3464
3465 if (pdl[0])
3466 pdl[strlen(pdl) - 1] = '\0'; /* Remove trailing comma */
3467
3468 cupsdSetString(&p->pdl, pdl);
3469 }
3470#endif /* HAVE_DNSSD */
bd7854cb 3471}
3472
3473
ef416fc2 3474/*
3475 * 'compare_printers()' - Compare two printers.
3476 */
3477
3478static int /* O - Result of comparison */
3479compare_printers(void *first, /* I - First printer */
3480 void *second, /* I - Second printer */
3481 void *data) /* I - App data (not used) */
3482{
3483 return (strcasecmp(((cupsd_printer_t *)first)->name,
3484 ((cupsd_printer_t *)second)->name));
3485}
3486
3487
bd7854cb 3488/*
e1d6a774 3489 * 'delete_printer_filters()' - Delete all MIME filters for a printer.
bd7854cb 3490 */
3491
3492static void
e1d6a774 3493delete_printer_filters(
3494 cupsd_printer_t *p) /* I - Printer to remove from */
bd7854cb 3495{
e1d6a774 3496 mime_filter_t *filter; /* MIME filter looping var */
bd7854cb 3497
bd7854cb 3498
3499 /*
e1d6a774 3500 * Range check input...
bd7854cb 3501 */
3502
e1d6a774 3503 if (p == NULL)
3504 return;
bd7854cb 3505
3506 /*
e1d6a774 3507 * Remove all filters from the MIME database that have a destination
3508 * type == printer...
bd7854cb 3509 */
3510
e1d6a774 3511 for (filter = mimeFirstFilter(MimeDatabase);
3512 filter;
3513 filter = mimeNextFilter(MimeDatabase))
3514 if (filter->dst == p->filetype)
3515 {
3516 /*
3517 * Delete the current filter...
3518 */
bd7854cb 3519
e1d6a774 3520 mimeDeleteFilter(MimeDatabase, filter);
3521 }
bd7854cb 3522}
3523
3524
ef416fc2 3525#ifdef __sgi
3526/*
3527 * 'write_irix_config()' - Update the config files used by the IRIX
3528 * desktop tools.
3529 */
3530
3531static void
3532write_irix_config(cupsd_printer_t *p) /* I - Printer to update */
3533{
3534 char filename[1024]; /* Interface script filename */
3535 cups_file_t *fp; /* Interface script file */
f301802f 3536 ipp_attribute_t *attr; /* Attribute data */
ef416fc2 3537
3538
3539 /*
3540 * Add dummy interface and GUI scripts to fool SGI's "challenged" printing
3541 * tools. First the interface script that tells the tools what kind of
3542 * printer we have...
3543 */
3544
3545 snprintf(filename, sizeof(filename), "/var/spool/lp/interface/%s", p->name);
3546
3547 if (p->type & CUPS_PRINTER_CLASS)
3548 unlink(filename);
3549 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
3550 {
3551 cupsFilePuts(fp, "#!/bin/sh\n");
3552
3553 if ((attr = ippFindAttribute(p->attrs, "printer-make-and-model",
3554 IPP_TAG_TEXT)) != NULL)
3555 cupsFilePrintf(fp, "NAME=\"%s\"\n", attr->values[0].string.text);
3556 else if (p->type & CUPS_PRINTER_CLASS)
3557 cupsFilePuts(fp, "NAME=\"Printer Class\"\n");
3558 else
3559 cupsFilePuts(fp, "NAME=\"Remote Destination\"\n");
3560
3561 if (p->type & CUPS_PRINTER_COLOR)
3562 cupsFilePuts(fp, "TYPE=ColorPostScript\n");
3563 else
3564 cupsFilePuts(fp, "TYPE=MonoPostScript\n");
3565
3566 cupsFilePrintf(fp, "HOSTNAME=%s\n", ServerName);
3567 cupsFilePrintf(fp, "HOSTPRINTER=%s\n", p->name);
3568
3569 cupsFileClose(fp);
3570
3571 chmod(filename, 0755);
3572 chown(filename, User, Group);
3573 }
3574
3575 /*
3576 * Then the member file that tells which device file the queue is connected
3577 * to... Networked printers use "/dev/null" in this file, so that's what
3578 * we use (the actual device URI can confuse some apps...)
3579 */
3580
3581 snprintf(filename, sizeof(filename), "/var/spool/lp/member/%s", p->name);
3582
3583 if (p->type & CUPS_PRINTER_CLASS)
3584 unlink(filename);
3585 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
3586 {
3587 cupsFilePuts(fp, "/dev/null\n");
3588
3589 cupsFileClose(fp);
3590
3591 chmod(filename, 0644);
3592 chown(filename, User, Group);
3593 }
3594
3595 /*
3596 * The gui_interface file is a script or program that launches a GUI
3597 * option panel for the printer, using options specified on the
3598 * command-line in the third argument. The option panel must send
3599 * any printing options to stdout on a single line when the user
3600 * accepts them, or nothing if the user cancels the dialog.
3601 *
3602 * The default options panel program is /usr/bin/glpoptions, from
3603 * the ESP Print Pro software. You can select another using the
3604 * PrintcapGUI option.
3605 */
3606
3607 snprintf(filename, sizeof(filename), "/var/spool/lp/gui_interface/ELF/%s.gui", p->name);
3608
3609 if (p->type & CUPS_PRINTER_CLASS)
3610 unlink(filename);
3611 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
3612 {
3613 cupsFilePuts(fp, "#!/bin/sh\n");
3614 cupsFilePrintf(fp, "%s -d %s -o \"$3\"\n", PrintcapGUI, p->name);
3615
3616 cupsFileClose(fp);
3617
3618 chmod(filename, 0755);
3619 chown(filename, User, Group);
3620 }
3621
3622 /*
3623 * The POD config file is needed by the printstatus command to show
3624 * the printer location and device.
3625 */
3626
3627 snprintf(filename, sizeof(filename), "/var/spool/lp/pod/%s.config", p->name);
3628
3629 if (p->type & CUPS_PRINTER_CLASS)
3630 unlink(filename);
3631 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
3632 {
3633 cupsFilePrintf(fp, "Printer Class | %s\n",
3634 (p->type & CUPS_PRINTER_COLOR) ? "ColorPostScript" : "MonoPostScript");
3635 cupsFilePrintf(fp, "Printer Model | %s\n", p->make_model ? p->make_model : "");
3636 cupsFilePrintf(fp, "Location Code | %s\n", p->location ? p->location : "");
3637 cupsFilePrintf(fp, "Physical Location | %s\n", p->info ? p->info : "");
3638 cupsFilePrintf(fp, "Port Path | %s\n", p->device_uri ? p->device_uri : "");
3639 cupsFilePrintf(fp, "Config Path | /var/spool/lp/pod/%s.config\n", p->name);
3640 cupsFilePrintf(fp, "Active Status Path | /var/spool/lp/pod/%s.status\n", p->name);
3641 cupsFilePuts(fp, "Status Update Wait | 10 seconds\n");
3642
3643 cupsFileClose(fp);
3644
3645 chmod(filename, 0664);
3646 chown(filename, User, Group);
3647 }
3648}
3649
3650
3651/*
3652 * 'write_irix_state()' - Update the status files used by IRIX printing
3653 * desktop tools.
3654 */
3655
3656static void
3657write_irix_state(cupsd_printer_t *p) /* I - Printer to update */
3658{
3659 char filename[1024]; /* Interface script filename */
3660 cups_file_t *fp; /* Interface script file */
3661 int tag; /* Status tag value */
3662
3663
3664 if (p)
3665 {
3666 /*
3667 * The POD status file is needed for the printstatus window to
3668 * provide the current status of the printer.
3669 */
3670
3671 snprintf(filename, sizeof(filename), "/var/spool/lp/pod/%s.status", p->name);
3672
3673 if (p->type & CUPS_PRINTER_CLASS)
3674 unlink(filename);
3675 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
3676 {
3677 cupsFilePrintf(fp, "Operational Status | %s\n",
3678 (p->state == IPP_PRINTER_IDLE) ? "Idle" :
3679 (p->state == IPP_PRINTER_PROCESSING) ? "Busy" :
3680 "Faulted");
3681 cupsFilePrintf(fp, "Information | 01 00 00 | %s\n", CUPS_SVERSION);
3682 cupsFilePrintf(fp, "Information | 02 00 00 | Device URI: %s\n",
3683 p->device_uri ? p->device_uri : "");
3684 cupsFilePrintf(fp, "Information | 03 00 00 | %s jobs\n",
3685 p->accepting ? "Accepting" : "Not accepting");
3686 cupsFilePrintf(fp, "Information | 04 00 00 | %s\n", p->state_message);
3687
3688 cupsFileClose(fp);
3689
3690 chmod(filename, 0664);
3691 chown(filename, User, Group);
3692 }
3693
3694 /*
3695 * The activeicons file is needed to provide desktop icons for printers:
3696 *
3697 * [ quoted from /usr/lib/print/tagit ]
3698 *
3699 * --- Type of printer tags (base values)
3700 *
3701 * Dumb=66048 # 0x10200
3702 * DumbColor=66080 # 0x10220
3703 * Raster=66112 # 0x10240
3704 * ColorRaster=66144 # 0x10260
3705 * Plotter=66176 # 0x10280
3706 * PostScript=66208 # 0x102A0
3707 * ColorPostScript=66240 # 0x102C0
3708 * MonoPostScript=66272 # 0x102E0
3709 *
3710 * --- Printer state modifiers for local printers
3711 *
3712 * Idle=0 # 0x0
3713 * Busy=1 # 0x1
3714 * Faulted=2 # 0x2
3715 * Unknown=3 # 0x3 (Faulted due to unknown reason)
3716 *
3717 * --- Printer state modifiers for network printers
3718 *
3719 * NetIdle=8 # 0x8
3720 * NetBusy=9 # 0x9
3721 * NetFaulted=10 # 0xA
3722 * NetUnknown=11 # 0xB (Faulted due to unknown reason)
3723 */
3724
3725 snprintf(filename, sizeof(filename), "/var/spool/lp/activeicons/%s", p->name);
3726
3727 if (p->type & CUPS_PRINTER_CLASS)
3728 unlink(filename);
3729 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
3730 {
3731 if (p->type & CUPS_PRINTER_COLOR)
3732 tag = 66240;
3733 else
3734 tag = 66272;
3735
3736 if (p->type & CUPS_PRINTER_REMOTE)
3737 tag |= 8;
3738
3739 if (p->state == IPP_PRINTER_PROCESSING)
3740 tag |= 1;
3741
3742 else if (p->state == IPP_PRINTER_STOPPED)
3743 tag |= 2;
3744
3745 cupsFilePuts(fp, "#!/bin/sh\n");
3746 cupsFilePrintf(fp, "#Tag %d\n", tag);
3747
3748 cupsFileClose(fp);
3749
3750 chmod(filename, 0755);
3751 chown(filename, User, Group);
3752 }
3753 }
3754
3755 /*
3756 * The default file is needed by the printers window to show
3757 * the default printer.
3758 */
3759
3760 snprintf(filename, sizeof(filename), "/var/spool/lp/default");
3761
3762 if (DefaultPrinter != NULL)
3763 {
3764 if ((fp = cupsFileOpen(filename, "w")) != NULL)
3765 {
3766 cupsFilePrintf(fp, "%s\n", DefaultPrinter->name);
3767
3768 cupsFileClose(fp);
3769
3770 chmod(filename, 0644);
3771 chown(filename, User, Group);
3772 }
3773 }
3774 else
3775 unlink(filename);
3776}
3777#endif /* __sgi */
3778
3779
3780/*
2e4ff8af 3781 * End of "$Id: printers.c 6970 2007-09-17 23:58:28Z mike $".
ef416fc2 3782 */