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