]> git.ipfire.org Git - thirdparty/cups.git/blame - scheduler/printers.c
Merge changes from CUPS 1.4svn-r7791.
[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
005dd1eb
MS
2405 if (duplex && duplex->num_choices > 1 &&
2406 !ppdInstallableConflict(ppd, duplex->keyword, "DuplexTumble"))
ef416fc2 2407 {
2408 p->type |= CUPS_PRINTER_DUPLEX;
2409
b423cd4c 2410 ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2411 "sides-supported", 3, NULL, sides);
2412
2413 if (!strcasecmp(duplex->defchoice, "DuplexTumble"))
2414 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2415 "sides-default", NULL, "two-sided-short-edge");
2416 else if (!strcasecmp(duplex->defchoice, "DuplexNoTumble"))
2417 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2418 "sides-default", NULL, "two-sided-long-edge");
2419 else
2420 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2421 "sides-default", NULL, "one-sided");
ef416fc2 2422 }
2423
2424 if (ppdFindOption(ppd, "Collate") != NULL)
2425 p->type |= CUPS_PRINTER_COLLATE;
2426
2427 if (ppdFindOption(ppd, "StapleLocation") != NULL)
2428 {
2429 p->type |= CUPS_PRINTER_STAPLE;
2430 finishings[num_finishings++] = IPP_FINISHINGS_STAPLE;
2431 }
2432
2433 if (ppdFindOption(ppd, "BindEdge") != NULL)
2434 {
2435 p->type |= CUPS_PRINTER_BIND;
2436 finishings[num_finishings++] = IPP_FINISHINGS_BIND;
2437 }
2438
2439 for (i = 0; i < ppd->num_sizes; i ++)
2440 if (ppd->sizes[i].length > 1728)
2441 p->type |= CUPS_PRINTER_LARGE;
2442 else if (ppd->sizes[i].length > 1008)
2443 p->type |= CUPS_PRINTER_MEDIUM;
2444 else
2445 p->type |= CUPS_PRINTER_SMALL;
2446
2447 /*
2448 * Add a filter from application/vnd.cups-raw to printer/name to
2449 * handle "raw" printing by users.
2450 */
2451
f7deaa1a 2452 add_printer_filter(p, p->filetype, "application/vnd.cups-raw 0 -");
2453
2454 /*
2455 * Add any pre-filters in the PPD file...
2456 */
2457
2458 if ((ppdattr = ppdFindAttr(ppd, "cupsPreFilter", NULL)) != NULL)
2459 {
2460 p->prefiltertype = mimeAddType(MimeDatabase, "prefilter", p->name);
2461
2462 for (; ppdattr; ppdattr = ppdFindNextAttr(ppd, "cupsPreFilter", NULL))
2463 if (ppdattr->value)
2464 add_printer_filter(p, p->prefiltertype, ppdattr->value);
2465 }
ef416fc2 2466
2467 /*
2468 * Add any filters in the PPD file...
2469 */
2470
2471 DEBUG_printf(("ppd->num_filters = %d\n", ppd->num_filters));
2472 for (i = 0; i < ppd->num_filters; i ++)
2473 {
2474 DEBUG_printf(("ppd->filters[%d] = \"%s\"\n", i, ppd->filters[i]));
f7deaa1a 2475 add_printer_filter(p, p->filetype, ppd->filters[i]);
ef416fc2 2476 }
2477
2478 if (ppd->num_filters == 0)
2479 {
2480 /*
7a14d768 2481 * If there are no filters, add PostScript printing filters.
ef416fc2 2482 */
2483
7a14d768
MS
2484 add_printer_filter(p, p->filetype,
2485 "application/vnd.cups-command 0 commandtops");
f7deaa1a 2486 add_printer_filter(p, p->filetype,
2487 "application/vnd.cups-postscript 0 -");
7a14d768
MS
2488
2489 p->type |= CUPS_PRINTER_COMMANDS;
ef416fc2 2490 }
20fbc903
MS
2491 else if (!(p->type & CUPS_PRINTER_COMMANDS))
2492 {
2493 /*
2494 * See if this is a PostScript device without a command filter...
2495 */
2496
2497 for (i = 0; i < ppd->num_filters; i ++)
2498 if (!strncasecmp(ppd->filters[i],
2499 "application/vnd.cups-postscript", 31))
2500 break;
2501
2502 if (i < ppd->num_filters)
2503 {
2504 /*
2505 * Add the generic PostScript command filter...
2506 */
2507
2508 add_printer_filter(p, p->filetype,
2509 "application/vnd.cups-command 0 commandtops");
2510 p->type |= CUPS_PRINTER_COMMANDS;
2511 }
2512 }
ef416fc2 2513
01ce6322
MS
2514 if (p->type & CUPS_PRINTER_COMMANDS)
2515 {
2516 char *commands, /* Copy of commands */
2517 *start, /* Start of name */
2518 *end; /* End of name */
2519 int count; /* Number of commands */
2520
2521
2522 if ((ppdattr = ppdFindAttr(ppd, "cupsCommands", NULL)) != NULL &&
2523 ppdattr->value && ppdattr->value[0])
2524 {
2525 for (count = 0, start = ppdattr->value; *start; count ++)
2526 {
2527 while (isspace(*start & 255))
2528 start ++;
2529
2530 if (!*start)
2531 break;
2532
2533 while (*start && !isspace(*start & 255))
2534 start ++;
2535 }
2536 }
2537 else
2538 count = 0;
2539
2540 if (count > 0)
2541 {
2542 /*
2543 * Make a copy of the commands string and count how many ...
2544 */
2545
2546 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2547 "printer-commands", count, NULL, NULL);
2548
2549 commands = strdup(ppdattr->value);
2550
2551 for (count = 0, start = commands; *start; count ++)
2552 {
2553 while (isspace(*start & 255))
2554 start ++;
2555
2556 if (!*start)
2557 break;
2558
2559 end = start;
2560 while (*end && !isspace(*end & 255))
2561 end ++;
2562
2563 if (*end)
2564 *end++ = '\0';
2565
2566 attr->values[count].string.text = _cupsStrAlloc(start);
2567
2568 start = end;
2569 }
2570
2571 free(commands);
2572 }
2573 else
2574 {
2575 /*
2576 * Add the standard list of commands...
2577 */
2578
2579 ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2580 "printer-commands",
2581 (int)(sizeof(standard_commands) /
2582 sizeof(standard_commands[0])), NULL,
2583 standard_commands);
2584 }
2585 }
2586 else
2587 {
2588 /*
2589 * No commands supported...
2590 */
2591
2592 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2593 "printer-commands", NULL, "none");
2594 }
2595
ef416fc2 2596 /*
2597 * Show current and available port monitors for this printer...
2598 */
2599
09a101d6 2600 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME, "port-monitor",
ef416fc2 2601 NULL, p->port_monitor ? p->port_monitor : "none");
2602
ef416fc2 2603 for (i = 1, ppdattr = ppdFindAttr(ppd, "cupsPortMonitor", NULL);
2604 ppdattr;
2605 i ++, ppdattr = ppdFindNextAttr(ppd, "cupsPortMonitor", NULL));
2606
2607 if (ppd->protocols)
2608 {
2609 if (strstr(ppd->protocols, "TBCP"))
2610 i ++;
2611 else if (strstr(ppd->protocols, "BCP"))
2612 i ++;
2613 }
2614
09a101d6 2615 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
ef416fc2 2616 "port-monitor-supported", i, NULL, NULL);
2617
757d2cad 2618 attr->values[0].string.text = _cupsStrAlloc("none");
ef416fc2 2619
2620 for (i = 1, ppdattr = ppdFindAttr(ppd, "cupsPortMonitor", NULL);
2621 ppdattr;
2622 i ++, ppdattr = ppdFindNextAttr(ppd, "cupsPortMonitor", NULL))
757d2cad 2623 attr->values[i].string.text = _cupsStrAlloc(ppdattr->value);
ef416fc2 2624
2625 if (ppd->protocols)
2626 {
2627 if (strstr(ppd->protocols, "TBCP"))
757d2cad 2628 attr->values[i].string.text = _cupsStrAlloc("tbcp");
ef416fc2 2629 else if (strstr(ppd->protocols, "BCP"))
757d2cad 2630 attr->values[i].string.text = _cupsStrAlloc("bcp");
ef416fc2 2631 }
2632
f7deaa1a 2633#ifdef HAVE_DNSSD
2634 cupsdSetString(&p->product, ppd->product);
2635#endif /* HAVE_DNSSD */
2636
09a101d6 2637 if (ppdFindAttr(ppd, "APRemoteQueueID", NULL))
2638 p->type |= CUPS_PRINTER_REMOTE;
3d8365b8 2639
ef416fc2 2640 /*
2641 * Close the PPD and set the type...
2642 */
2643
2644 ppdClose(ppd);
ef416fc2 2645 }
2646 else if (!access(filename, 0))
2647 {
2648 int pline; /* PPD line number */
2649 ppd_status_t pstatus; /* PPD load status */
2650
2651
2652 pstatus = ppdLastError(&pline);
2653
b423cd4c 2654 cupsdLogMessage(CUPSD_LOG_ERROR, "PPD file for %s cannot be loaded!",
2655 p->name);
ef416fc2 2656
2657 if (pstatus <= PPD_ALLOC_ERROR)
2658 cupsdLogMessage(CUPSD_LOG_ERROR, "%s", strerror(errno));
2659 else
b423cd4c 2660 cupsdLogMessage(CUPSD_LOG_ERROR, "%s on line %d.",
2661 ppdErrorString(pstatus), pline);
ef416fc2 2662
b423cd4c 2663 cupsdLogMessage(CUPSD_LOG_INFO,
2664 "Hint: Run \"cupstestppd %s\" and fix any errors.",
2665 filename);
ef416fc2 2666
2667 /*
2668 * Add a filter from application/vnd.cups-raw to printer/name to
2669 * handle "raw" printing by users.
2670 */
2671
f7deaa1a 2672 add_printer_filter(p, p->filetype, "application/vnd.cups-raw 0 -");
ef416fc2 2673
2674 /*
2675 * Add a PostScript filter, since this is still possibly PS printer.
2676 */
2677
f7deaa1a 2678 add_printer_filter(p, p->filetype,
2679 "application/vnd.cups-postscript 0 -");
ef416fc2 2680 }
2681 else
2682 {
2683 /*
2684 * If we have an interface script, add a filter entry for it...
2685 */
2686
2687 snprintf(filename, sizeof(filename), "%s/interfaces/%s", ServerRoot,
2688 p->name);
b423cd4c 2689 if (!access(filename, X_OK))
ef416fc2 2690 {
2691 /*
2692 * Yes, we have a System V style interface script; use it!
2693 */
2694
2695 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
f7deaa1a 2696 "printer-make-and-model", NULL,
2697 "Local System V Printer");
ef416fc2 2698
2699 snprintf(filename, sizeof(filename), "*/* 0 %s/interfaces/%s",
2700 ServerRoot, p->name);
f7deaa1a 2701 add_printer_filter(p, p->filetype, filename);
ef416fc2 2702 }
2703 else if (p->device_uri &&
2704 !strncmp(p->device_uri, "ipp://", 6) &&
2705 (strstr(p->device_uri, "/printers/") != NULL ||
2706 strstr(p->device_uri, "/classes/") != NULL))
2707 {
2708 /*
2709 * Tell the client this is really a hard-wired remote printer.
2710 */
2711
09a101d6 2712 p->type |= CUPS_PRINTER_REMOTE;
ef416fc2 2713
2714 /*
2715 * Point the printer-uri-supported attribute to the
2716 * remote printer...
2717 */
2718
2719 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI,
2720 "printer-uri-supported", NULL, p->device_uri);
2721
2722 /*
2723 * Then set the make-and-model accordingly...
2724 */
2725
2726 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
2727 "printer-make-and-model", NULL, "Remote Printer");
2728
2729 /*
2730 * Print all files directly...
2731 */
2732
d09495fa 2733 p->raw = 1;
2734 p->remote = 1;
ef416fc2 2735 }
2736 else
2737 {
2738 /*
2739 * Otherwise we have neither - treat this as a "dumb" printer
2740 * with no PPD file...
2741 */
2742
2743 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
2744 "printer-make-and-model", NULL, "Local Raw Printer");
2745
2746 p->raw = 1;
2747 }
2748 }
2749
2750 ippAddIntegers(p->attrs, IPP_TAG_PRINTER, IPP_TAG_ENUM,
b94498cf 2751 "finishings-supported", num_finishings, finishings);
ef416fc2 2752 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_ENUM,
2753 "finishings-default", IPP_FINISHINGS_NONE);
2754 }
2755 }
2756
8922323b
MS
2757 /*
2758 * Copy marker attributes as needed...
2759 */
2760
2761 if (oldattrs)
2762 {
2763 ipp_attribute_t *oldattr; /* Old attribute */
2764
2765
2766 if ((oldattr = ippFindAttribute(oldattrs, "marker-colors",
2767 IPP_TAG_NAME)) != NULL)
2768 {
2769 if ((attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
2770 "marker-colors", oldattr->num_values, NULL,
2771 NULL)) != NULL)
2772 {
2773 for (i = 0; i < oldattr->num_values; i ++)
2774 attr->values[i].string.text =
2775 _cupsStrAlloc(oldattr->values[i].string.text);
2776 }
2777 }
2778
2779 if ((oldattr = ippFindAttribute(oldattrs, "marker-levels",
2780 IPP_TAG_INTEGER)) != NULL)
2781 {
2782 if ((attr = ippAddIntegers(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
2783 "marker-levels", oldattr->num_values,
2784 NULL)) != NULL)
2785 {
2786 for (i = 0; i < oldattr->num_values; i ++)
2787 attr->values[i].integer = oldattr->values[i].integer;
2788 }
2789 }
2790
2791 if ((oldattr = ippFindAttribute(oldattrs, "marker-names",
2792 IPP_TAG_NAME)) != NULL)
2793 {
2794 if ((attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
2795 "marker-names", oldattr->num_values, NULL,
2796 NULL)) != NULL)
2797 {
2798 for (i = 0; i < oldattr->num_values; i ++)
2799 attr->values[i].string.text =
2800 _cupsStrAlloc(oldattr->values[i].string.text);
2801 }
2802 }
2803
2804 if ((oldattr = ippFindAttribute(oldattrs, "marker-types",
2805 IPP_TAG_KEYWORD)) != NULL)
2806 {
2807 if ((attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
2808 "marker-types", oldattr->num_values, NULL,
2809 NULL)) != NULL)
2810 {
2811 for (i = 0; i < oldattr->num_values; i ++)
2812 attr->values[i].string.text =
2813 _cupsStrAlloc(oldattr->values[i].string.text);
2814 }
2815 }
2816
2817 ippDelete(oldattrs);
2818 }
2819
b423cd4c 2820 /*
bc44d920 2821 * Force sharing off for remote queues...
b423cd4c 2822 */
2823
bc44d920 2824 if (p->type & (CUPS_PRINTER_REMOTE | CUPS_PRINTER_IMPLICIT))
2825 p->shared = 0;
2826 else
b423cd4c 2827 {
bc44d920 2828 /*
2829 * Copy the printer options into a browse attributes string we can re-use.
2830 */
2831
b423cd4c 2832 const char *valptr; /* Pointer into value */
2833 char *attrptr; /* Pointer into attribute string */
2834
2835
2836 /*
2837 * Free the old browse attributes as needed...
2838 */
2839
2840 if (p->browse_attrs)
2841 free(p->browse_attrs);
2842
2843 /*
2844 * Compute the length of all attributes + job-sheets, lease-duration,
2845 * and BrowseLocalOptions.
2846 */
2847
2848 for (length = 1, i = p->num_options, option = p->options;
2849 i > 0;
2850 i --, option ++)
2851 {
2852 length += strlen(option->name) + 2;
2853
2854 if (option->value)
2855 {
2856 for (valptr = option->value; *valptr; valptr ++)
2857 if (strchr(" \"\'\\", *valptr))
2858 length += 2;
2859 else
2860 length ++;
2861 }
2862 }
2863
2864 length += 13 + strlen(p->job_sheets[0]) + strlen(p->job_sheets[1]);
2865 length += 32;
2866 if (BrowseLocalOptions)
f7deaa1a 2867 length += 12 + strlen(BrowseLocalOptions);
b423cd4c 2868
7594b224 2869 if (p->num_auth_info_required > 0)
2870 {
2871 length += 18; /* auth-info-required */
2872
2873 for (i = 0; i < p->num_auth_info_required; i ++)
2874 length += strlen(p->auth_info_required[i]) + 1;
2875 }
2876
b423cd4c 2877 /*
2878 * Allocate the new string...
2879 */
f7deaa1a 2880
b423cd4c 2881 if ((p->browse_attrs = calloc(1, length)) == NULL)
2882 cupsdLogMessage(CUPSD_LOG_ERROR,
2883 "Unable to allocate %d bytes for browse data!",
2884 length);
2885 else
2886 {
2887 /*
2888 * Got the allocated string, now copy the options and attributes over...
2889 */
2890
2891 sprintf(p->browse_attrs, "job-sheets=%s,%s lease-duration=%d",
2892 p->job_sheets[0], p->job_sheets[1], BrowseTimeout);
2893 attrptr = p->browse_attrs + strlen(p->browse_attrs);
2894
2895 if (BrowseLocalOptions)
2896 {
2897 sprintf(attrptr, " ipp-options=%s", BrowseLocalOptions);
2898 attrptr += strlen(attrptr);
2899 }
2900
2901 for (i = p->num_options, option = p->options;
2902 i > 0;
2903 i --, option ++)
2904 {
2905 *attrptr++ = ' ';
2906 strcpy(attrptr, option->name);
2907 attrptr += strlen(attrptr);
2908
2909 if (option->value)
2910 {
2911 *attrptr++ = '=';
2912
2913 for (valptr = option->value; *valptr; valptr ++)
2914 {
2915 if (strchr(" \"\'\\", *valptr))
2916 *attrptr++ = '\\';
2917
2918 *attrptr++ = *valptr;
2919 }
2920 }
2921 }
2922
7594b224 2923 if (p->num_auth_info_required > 0)
2924 {
2925 strcpy(attrptr, "auth-info-required");
2926 attrptr += 18;
2927
2928 for (i = 0; i < p->num_auth_info_required; i ++)
2929 {
2930 *attrptr++ = i ? ',' : '=';
2931 strcpy(attrptr, p->auth_info_required[i]);
2932 attrptr += strlen(attrptr);
2933 }
2934 }
2935 else
2936 *attrptr = '\0';
b423cd4c 2937 }
2938 }
2939
bd7854cb 2940 /*
2941 * Populate the document-format-supported attribute...
2942 */
2943
2944 add_printer_formats(p);
2945
ef416fc2 2946 DEBUG_printf(("cupsdSetPrinterAttrs: leaving name = %s, type = %x\n", p->name,
2947 p->type));
2948
b423cd4c 2949 /*
2950 * Add name-default attributes...
2951 */
2952
2953 add_printer_defaults(p);
2954
ef416fc2 2955#ifdef __sgi
2956 /*
2957 * Write the IRIX printer config and status files...
2958 */
2959
2960 write_irix_config(p);
2961 write_irix_state(p);
2962#endif /* __sgi */
f7deaa1a 2963
2964 /*
2965 * Let the browse protocols reflect the change
2966 */
2967
2968 cupsdRegisterPrinter(p);
ef416fc2 2969}
2970
2971
2972/*
2973 * 'cupsdSetPrinterReasons()' - Set/update the reasons strings.
2974 */
2975
2976void
2977cupsdSetPrinterReasons(
2978 cupsd_printer_t *p, /* I - Printer */
2979 const char *s) /* I - Reasons strings */
2980{
2981 int i; /* Looping var */
2982 const char *sptr; /* Pointer into reasons */
2983 char reason[255], /* Reason string */
2984 *rptr; /* Pointer into reason */
2985
2986
2987 if (s[0] == '-' || s[0] == '+')
2988 {
2989 /*
2990 * Add/remove reasons...
2991 */
2992
2993 sptr = s + 1;
2994 }
2995 else
2996 {
2997 /*
2998 * Replace reasons...
2999 */
3000
3001 sptr = s;
3002
3003 for (i = 0; i < p->num_reasons; i ++)
3004 free(p->reasons[i]);
3005
3006 p->num_reasons = 0;
3007 }
3008
bc44d920 3009 if (!strcmp(s, "none"))
3010 return;
3011
ef416fc2 3012 /*
3013 * Loop through all of the reasons...
3014 */
3015
3016 while (*sptr)
3017 {
3018 /*
3019 * Skip leading whitespace and commas...
3020 */
3021
3022 while (isspace(*sptr & 255) || *sptr == ',')
3023 sptr ++;
3024
3025 for (rptr = reason; *sptr && !isspace(*sptr & 255) && *sptr != ','; sptr ++)
3026 if (rptr < (reason + sizeof(reason) - 1))
3027 *rptr++ = *sptr;
3028
3029 if (rptr == reason)
3030 break;
3031
3032 *rptr = '\0';
3033
3034 if (s[0] == '-')
3035 {
3036 /*
3037 * Remove reason...
3038 */
3039
3040 for (i = 0; i < p->num_reasons; i ++)
3041 if (!strcasecmp(reason, p->reasons[i]))
3042 {
3043 /*
3044 * Found a match, so remove it...
3045 */
3046
3047 p->num_reasons --;
3048 free(p->reasons[i]);
3049
3050 if (i < p->num_reasons)
3051 memmove(p->reasons + i, p->reasons + i + 1,
3052 (p->num_reasons - i) * sizeof(char *));
3053
3054 i --;
c0e1af83 3055
3056 if (!strcmp(reason, "paused") && p->state == IPP_PRINTER_STOPPED)
3057 cupsdSetPrinterState(p, IPP_PRINTER_IDLE, 1);
ef416fc2 3058 }
3059 }
3060 else if (p->num_reasons < (int)(sizeof(p->reasons) / sizeof(p->reasons[0])))
3061 {
3062 /*
3063 * Add reason...
3064 */
3065
3066 for (i = 0; i < p->num_reasons; i ++)
3067 if (!strcasecmp(reason, p->reasons[i]))
3068 break;
3069
3070 if (i >= p->num_reasons)
3071 {
3072 p->reasons[i] = strdup(reason);
3073 p->num_reasons ++;
c0e1af83 3074
3075 if (!strcmp(reason, "paused") && p->state != IPP_PRINTER_STOPPED)
3076 cupsdSetPrinterState(p, IPP_PRINTER_STOPPED, 1);
ef416fc2 3077 }
3078 }
3079 }
3080}
3081
3082
3083/*
3084 * 'cupsdSetPrinterState()' - Update the current state of a printer.
3085 */
3086
3087void
3088cupsdSetPrinterState(
3089 cupsd_printer_t *p, /* I - Printer to change */
3090 ipp_pstate_t s, /* I - New state */
3091 int update) /* I - Update printers.conf? */
3092{
3093 ipp_pstate_t old_state; /* Old printer state */
3094
3095
3096 /*
3097 * Can't set status of remote printers...
3098 */
3099
09a101d6 3100 if (p->type & CUPS_PRINTER_DISCOVERED)
ef416fc2 3101 return;
3102
3103 /*
3104 * Set the new state...
3105 */
3106
3107 old_state = p->state;
3108 p->state = s;
3109
3110 if (old_state != s)
3111 {
0a682745 3112 cupsdAddEvent(s == IPP_PRINTER_STOPPED ? CUPSD_EVENT_PRINTER_STOPPED :
d9bca400 3113 CUPSD_EVENT_PRINTER_STATE, p, NULL,
e53920b9 3114 "%s \"%s\" state changed.",
3115 (p->type & CUPS_PRINTER_CLASS) ? "Class" : "Printer",
3116 p->name);
3117
ef416fc2 3118 /*
3119 * Let the browse code know this needs to be updated...
3120 */
3121
3122 BrowseNext = p;
3123 p->state_time = time(NULL);
3124 p->browse_time = 0;
3125
3126#ifdef __sgi
3127 write_irix_state(p);
3128#endif /* __sgi */
3129 }
3130
3131 cupsdAddPrinterHistory(p);
3132
f7deaa1a 3133 /*
3134 * Let the browse protocols reflect the change...
3135 */
3136
7a14d768
MS
3137 if (update)
3138 cupsdRegisterPrinter(p);
f7deaa1a 3139
ef416fc2 3140 /*
3141 * Save the printer configuration if a printer goes from idle or processing
3142 * to stopped (or visa-versa)...
3143 */
3144
3145 if ((old_state == IPP_PRINTER_STOPPED) != (s == IPP_PRINTER_STOPPED) &&
3146 update)
3147 {
3148 if (p->type & CUPS_PRINTER_CLASS)
3dfe78b3 3149 cupsdMarkDirty(CUPSD_DIRTY_CLASSES);
ef416fc2 3150 else
3dfe78b3 3151 cupsdMarkDirty(CUPSD_DIRTY_PRINTERS);
ef416fc2 3152 }
3153}
3154
3155
3156/*
3157 * 'cupsdStopPrinter()' - Stop a printer from printing any jobs...
3158 */
3159
3160void
3161cupsdStopPrinter(cupsd_printer_t *p, /* I - Printer to stop */
3162 int update)/* I - Update printers.conf? */
3163{
3164 cupsd_job_t *job; /* Active print job */
3165
3166
3167 /*
3168 * Set the printer state...
3169 */
3170
3171 cupsdSetPrinterState(p, IPP_PRINTER_STOPPED, update);
3172
3173 /*
3174 * See if we have a job printing on this printer...
3175 */
3176
3177 if (p->job)
3178 {
3179 /*
3180 * Get pointer to job...
3181 */
3182
3183 job = (cupsd_job_t *)p->job;
3184
3185 /*
3186 * Stop it...
3187 */
3188
3189 cupsdStopJob(job, 0);
3190
3191 /*
3192 * Reset the state to pending...
3193 */
3194
3195 job->state->values[0].integer = IPP_JOB_PENDING;
bd7854cb 3196 job->state_value = IPP_JOB_PENDING;
3dfe78b3 3197 job->dirty = 1;
ef416fc2 3198
3dfe78b3 3199 cupsdMarkDirty(CUPSD_DIRTY_JOBS);
07725fee 3200
3201 cupsdAddEvent(CUPSD_EVENT_JOB_STOPPED, p, job,
3202 "Job stopped due to printer being paused");
ef416fc2 3203 }
3204}
3205
3206
c9fc04c6
MS
3207/*
3208 * 'cupsdUpdatePrinterPPD()' - Update keywords in a printer's PPD file.
3209 */
3210
3211int /* O - 1 if successful, 0 otherwise */
3212cupsdUpdatePrinterPPD(
3213 cupsd_printer_t *p, /* I - Printer */
3214 int num_keywords, /* I - Number of keywords */
3215 cups_option_t *keywords) /* I - Keywords */
3216{
3217 int i; /* Looping var */
3218 cups_file_t *src, /* Original file */
3219 *dst; /* New file */
3220 char srcfile[1024], /* Original filename */
3221 dstfile[1024], /* New filename */
3222 line[1024], /* Line from file */
3223 keystring[41]; /* Keyword from line */
3224 cups_option_t *keyword; /* Current keyword */
3225
3226
3227 cupsdLogMessage(CUPSD_LOG_INFO, "Updating keywords in PPD file for %s...",
3228 p->name);
3229
3230 /*
3231 * Get the old and new PPD filenames...
3232 */
3233
3234 snprintf(srcfile, sizeof(srcfile), "%s/ppd/%s.ppd.O", ServerRoot, p->name);
3235 snprintf(dstfile, sizeof(srcfile), "%s/ppd/%s.ppd", ServerRoot, p->name);
3236
3237 /*
3238 * Rename the old file and open the old and new...
3239 */
3240
3241 if (rename(dstfile, srcfile))
3242 {
3243 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to backup PPD file for %s: %s",
3244 p->name, strerror(errno));
3245 return (0);
3246 }
3247
3248 if ((src = cupsFileOpen(srcfile, "r")) == NULL)
3249 {
3250 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to open PPD file \"%s\": %s",
3251 srcfile, strerror(errno));
3252 rename(srcfile, dstfile);
3253 return (0);
3254 }
3255
3256 if ((dst = cupsFileOpen(dstfile, "w")) == NULL)
3257 {
3258 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to create PPD file \"%s\": %s",
3259 dstfile, strerror(errno));
3260 cupsFileClose(src);
3261 rename(srcfile, dstfile);
3262 return (0);
3263 }
3264
3265 /*
3266 * Copy the first line and then write out all of the keywords...
3267 */
3268
3269 if (!cupsFileGets(src, line, sizeof(line)))
3270 {
3271 cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to read PPD file \"%s\": %s",
3272 srcfile, strerror(errno));
3273 cupsFileClose(src);
3274 cupsFileClose(dst);
3275 rename(srcfile, dstfile);
3276 return (0);
3277 }
3278
3279 cupsFilePrintf(dst, "%s\n", line);
3280
3281 for (i = num_keywords, keyword = keywords; i > 0; i --, keyword ++)
3282 {
3283 cupsdLogMessage(CUPSD_LOG_DEBUG, "*%s: %s", keyword->name, keyword->value);
3284 cupsFilePrintf(dst, "*%s: %s\n", keyword->name, keyword->value);
3285 }
3286
3287 /*
3288 * Then copy the rest of the PPD file, dropping any keywords we changed.
3289 */
3290
3291 while (cupsFileGets(src, line, sizeof(line)))
3292 {
3293 /*
3294 * Skip keywords we've already set...
3295 */
3296
3297 if (sscanf(line, "*%40[^:]:", keystring) == 1 &&
3298 cupsGetOption(keystring, num_keywords, keywords))
3299 continue;
3300
3301 /*
3302 * Otherwise write the line...
3303 */
3304
3305 cupsFilePrintf(dst, "%s\n", line);
3306 }
3307
3308 /*
3309 * Close files and return...
3310 */
3311
3312 cupsFileClose(src);
3313 cupsFileClose(dst);
3314
3315 return (1);
3316}
3317
3318
ef416fc2 3319/*
3320 * 'cupsdUpdatePrinters()' - Update printers after a partial reload.
3321 */
3322
3323void
3324cupsdUpdatePrinters(void)
3325{
3326 cupsd_printer_t *p; /* Current printer */
3327
3328
3329 /*
3330 * Loop through the printers and recreate the printer attributes
3331 * for any local printers since the policy and/or access control
3332 * stuff may have changed. Also, if browsing is disabled, remove
3333 * any remote printers...
3334 */
3335
3336 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
3337 p;
3338 p = (cupsd_printer_t *)cupsArrayNext(Printers))
3339 {
07725fee 3340 /*
3341 * Remove remote printers if we are no longer browsing...
3342 */
3343
09a101d6 3344 if (!Browsing &&
3345 (p->type & (CUPS_PRINTER_IMPLICIT | CUPS_PRINTER_DISCOVERED)))
ef416fc2 3346 {
3347 if (p->type & CUPS_PRINTER_IMPLICIT)
3348 cupsArrayRemove(ImplicitPrinters, p);
3349
3350 cupsArraySave(Printers);
3351 cupsdDeletePrinter(p, 0);
3352 cupsArrayRestore(Printers);
3353 continue;
3354 }
ef416fc2 3355
3356 /*
3357 * Update the operation policy pointer...
3358 */
3359
3360 if ((p->op_policy_ptr = cupsdFindPolicy(p->op_policy)) == NULL)
3361 p->op_policy_ptr = DefaultPolicyPtr;
07725fee 3362
3363 /*
3364 * Update printer attributes as needed...
3365 */
3366
09a101d6 3367 if (!(p->type & CUPS_PRINTER_DISCOVERED))
07725fee 3368 cupsdSetPrinterAttrs(p);
ef416fc2 3369 }
3370}
3371
3372
3373/*
3374 * 'cupsdValidateDest()' - Validate a printer/class destination.
3375 */
3376
3377const char * /* O - Printer or class name */
3378cupsdValidateDest(
f7deaa1a 3379 const char *uri, /* I - Printer URI */
ef416fc2 3380 cups_ptype_t *dtype, /* O - Type (printer or class) */
3381 cupsd_printer_t **printer) /* O - Printer pointer */
3382{
3383 cupsd_printer_t *p; /* Current printer */
3384 char localname[1024],/* Localized hostname */
3385 *lptr, /* Pointer into localized hostname */
f7deaa1a 3386 *sptr, /* Pointer into server name */
3387 *rptr, /* Pointer into resource */
3388 scheme[32], /* Scheme portion of URI */
3389 username[64], /* Username portion of URI */
3390 hostname[HTTP_MAX_HOST],
3391 /* Host portion of URI */
3392 resource[HTTP_MAX_URI];
3393 /* Resource portion of URI */
3394 int port; /* Port portion of URI */
3395
3396
3397 DEBUG_printf(("cupsdValidateDest(uri=\"%s\", dtype=%p, printer=%p)\n", uri,
ef416fc2 3398 dtype, printer));
3399
3400 /*
3401 * Initialize return values...
3402 */
3403
3404 if (printer)
3405 *printer = NULL;
3406
f7deaa1a 3407 if (dtype)
3408 *dtype = (cups_ptype_t)0;
3409
3410 /*
3411 * Pull the hostname and resource from the URI...
3412 */
3413
3414 httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, sizeof(scheme),
3415 username, sizeof(username), hostname, sizeof(hostname),
3416 &port, resource, sizeof(resource));
ef416fc2 3417
3418 /*
3419 * See if the resource is a class or printer...
3420 */
3421
3422 if (!strncmp(resource, "/classes/", 9))
3423 {
3424 /*
3425 * Class...
3426 */
3427
f7deaa1a 3428 rptr = resource + 9;
ef416fc2 3429 }
3430 else if (!strncmp(resource, "/printers/", 10))
3431 {
3432 /*
3433 * Printer...
3434 */
3435
f7deaa1a 3436 rptr = resource + 10;
ef416fc2 3437 }
3438 else
3439 {
3440 /*
3441 * Bad resource name...
3442 */
3443
3444 return (NULL);
3445 }
3446
3447 /*
3448 * See if the printer or class name exists...
3449 */
3450
f7deaa1a 3451 p = cupsdFindDest(rptr);
ef416fc2 3452
f7deaa1a 3453 if (p == NULL && strchr(rptr, '@') == NULL)
ef416fc2 3454 return (NULL);
3455 else if (p != NULL)
3456 {
3457 if (printer)
3458 *printer = p;
3459
f7deaa1a 3460 if (dtype)
3461 *dtype = p->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT |
09a101d6 3462 CUPS_PRINTER_REMOTE | CUPS_PRINTER_DISCOVERED);
f7deaa1a 3463
ef416fc2 3464 return (p->name);
3465 }
3466
3467 /*
3468 * Change localhost to the server name...
3469 */
3470
3471 if (!strcasecmp(hostname, "localhost"))
f7deaa1a 3472 strlcpy(hostname, ServerName, sizeof(hostname));
ef416fc2 3473
3474 strlcpy(localname, hostname, sizeof(localname));
3475
3476 if (!strcasecmp(hostname, ServerName))
3477 {
3478 /*
3479 * Localize the hostname...
3480 */
3481
3482 lptr = strchr(localname, '.');
3483 sptr = strchr(ServerName, '.');
3484
3485 if (sptr != NULL && lptr != NULL)
3486 {
3487 /*
3488 * Strip the common domain name components...
3489 */
3490
3491 while (lptr != NULL)
3492 {
3493 if (!strcasecmp(lptr, sptr))
3494 {
3495 *lptr = '\0';
3496 break;
3497 }
3498 else
3499 lptr = strchr(lptr + 1, '.');
3500 }
3501 }
3502 }
3503
3504 DEBUG_printf(("localized hostname is \"%s\"...\n", localname));
3505
3506 /*
3507 * Find a matching printer or class...
3508 */
3509
3510 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
3511 p;
3512 p = (cupsd_printer_t *)cupsArrayNext(Printers))
3513 if (!strcasecmp(p->hostname, localname) &&
f7deaa1a 3514 !strcasecmp(p->name, rptr))
ef416fc2 3515 {
3516 if (printer)
3517 *printer = p;
3518
f7deaa1a 3519 if (dtype)
3520 *dtype = p->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT |
09a101d6 3521 CUPS_PRINTER_REMOTE | CUPS_PRINTER_DISCOVERED);
f7deaa1a 3522
ef416fc2 3523 return (p->name);
3524 }
3525
3526 return (NULL);
3527}
3528
3529
3530/*
3531 * 'cupsdWritePrintcap()' - Write a pseudo-printcap file for older applications
3532 * that need it...
3533 */
3534
3535void
3536cupsdWritePrintcap(void)
3537{
3538 cups_file_t *fp; /* printcap file */
3539 cupsd_printer_t *p; /* Current printer */
3540
3541
3542#ifdef __sgi
3543 /*
3544 * Update the IRIX printer state for the default printer; if
3545 * no printers remain, then the default printer file will be
3546 * removed...
3547 */
3548
3549 write_irix_state(DefaultPrinter);
3550#endif /* __sgi */
3551
3552 /*
3553 * See if we have a printcap file; if not, don't bother writing it.
3554 */
3555
3556 if (!Printcap || !*Printcap)
3557 return;
3558
3559 /*
3560 * Open the printcap file...
3561 */
3562
3563 if ((fp = cupsFileOpen(Printcap, "w")) == NULL)
3564 return;
3565
3566 /*
3567 * Put a comment header at the top so that users will know where the
3568 * data has come from...
3569 */
3570
c277e2f8
MS
3571 cupsFilePuts(fp,
3572 "# This file was automatically generated by cupsd(8) from the\n");
ef416fc2 3573 cupsFilePrintf(fp, "# %s/printers.conf file. All changes to this file\n",
3574 ServerRoot);
3575 cupsFilePuts(fp, "# will be lost.\n");
3576
3577 if (Printers)
3578 {
3579 /*
3580 * Write a new printcap with the current list of printers.
3581 */
3582
3583 switch (PrintcapFormat)
3584 {
3585 case PRINTCAP_BSD:
3586 /*
3587 * Each printer is put in the file as:
3588 *
3589 * Printer1:
3590 * Printer2:
3591 * Printer3:
3592 * ...
3593 * PrinterN:
3594 */
3595
3596 if (DefaultPrinter)
3597 cupsFilePrintf(fp, "%s|%s:rm=%s:rp=%s:\n", DefaultPrinter->name,
c277e2f8
MS
3598 DefaultPrinter->info, ServerName,
3599 DefaultPrinter->name);
ef416fc2 3600
3601 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
3602 p;
3603 p = (cupsd_printer_t *)cupsArrayNext(Printers))
3604 if (p != DefaultPrinter)
3605 cupsFilePrintf(fp, "%s|%s:rm=%s:rp=%s:\n", p->name, p->info,
c277e2f8 3606 ServerName, p->name);
ef416fc2 3607 break;
3608
3609 case PRINTCAP_SOLARIS:
3610 /*
3611 * Each printer is put in the file as:
3612 *
3613 * _all:all=Printer1,Printer2,Printer3,...,PrinterN
3614 * _default:use=DefaultPrinter
3615 * Printer1:\
3616 * :bsdaddr=ServerName,Printer1:\
3617 * :description=Description:
3618 * Printer2:
3619 * :bsdaddr=ServerName,Printer2:\
3620 * :description=Description:
3621 * Printer3:
3622 * :bsdaddr=ServerName,Printer3:\
3623 * :description=Description:
3624 * ...
3625 * PrinterN:
3626 * :bsdaddr=ServerName,PrinterN:\
3627 * :description=Description:
3628 */
3629
3630 cupsFilePuts(fp, "_all:all=");
3631 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
3632 p;
3633 p = (cupsd_printer_t *)cupsArrayCurrent(Printers))
3634 cupsFilePrintf(fp, "%s%c", p->name,
3635 cupsArrayNext(Printers) ? ',' : '\n');
3636
3637 if (DefaultPrinter)
3638 cupsFilePrintf(fp, "_default:use=%s\n", DefaultPrinter->name);
3639
3640 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
3641 p;
3642 p = (cupsd_printer_t *)cupsArrayNext(Printers))
3643 cupsFilePrintf(fp, "%s:\\\n"
c277e2f8
MS
3644 "\t:bsdaddr=%s,%s:\\\n"
3645 "\t:description=%s:\n",
3646 p->name, ServerName, p->name,
3647 p->info ? p->info : "");
ef416fc2 3648 break;
3649 }
3650 }
3651
3652 /*
3653 * Close the file...
3654 */
3655
3656 cupsFileClose(fp);
3657}
3658
3659
3660/*
3661 * 'cupsdSanitizeURI()' - Sanitize a device URI...
3662 */
3663
3664char * /* O - New device URI */
3665cupsdSanitizeURI(const char *uri, /* I - Original device URI */
3666 char *buffer, /* O - New device URI */
3667 int buflen) /* I - Size of new device URI buffer */
3668{
3669 char *start, /* Start of data after scheme */
3670 *slash, /* First slash after scheme:// */
3671 *ptr; /* Pointer into user@host:port part */
3672
3673
3674 /*
3675 * Range check input...
3676 */
3677
3678 if (!uri || !buffer || buflen < 2)
3679 return (NULL);
3680
3681 /*
3682 * Copy the device URI to the new buffer...
3683 */
3684
3685 strlcpy(buffer, uri, buflen);
3686
3687 /*
3688 * Find the end of the scheme:// part...
3689 */
3690
3691 if ((ptr = strchr(buffer, ':')) == NULL)
3692 return (buffer); /* No scheme: part... */
3693
3694 for (start = ptr + 1; *start; start ++)
3695 if (*start != '/')
3696 break;
3697
3698 /*
3699 * Find the next slash (/) in the URI...
3700 */
3701
3702 if ((slash = strchr(start, '/')) == NULL)
3703 slash = start + strlen(start); /* No slash, point to the end */
3704
3705 /*
3706 * Check for an @ sign before the slash...
3707 */
3708
3709 if ((ptr = strchr(start, '@')) != NULL && ptr < slash)
3710 {
3711 /*
3712 * Found an @ sign and it is before the resource part, so we have
3713 * an authentication string. Copy the remaining URI over the
3714 * authentication string...
3715 */
3716
3717 _cups_strcpy(start, ptr + 1);
3718 }
3719
3720 /*
3721 * Return the new device URI...
3722 */
3723
3724 return (buffer);
3725}
3726
3727
b423cd4c 3728/*
3729 * 'add_printer_defaults()' - Add name-default attributes to the printer attributes.
3730 */
3731
3732static void
3733add_printer_defaults(cupsd_printer_t *p)/* I - Printer */
3734{
3735 int i; /* Looping var */
3736 int num_options; /* Number of default options */
3737 cups_option_t *options, /* Default options */
3738 *option; /* Current option */
3739 char name[256]; /* name-default */
3740
3741
f7deaa1a 3742 /*
3743 * Maintain a common array of default attribute names...
3744 */
3745
3746 if (!CommonDefaults)
3747 {
3748 CommonDefaults = cupsArrayNew((cups_array_func_t)strcmp, NULL);
3749
3750 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("copies-default"));
3751 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("document-format-default"));
3752 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("finishings-default"));
3753 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("job-hold-until-default"));
3754 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("job-priority-default"));
3755 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("job-sheets-default"));
3756 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("media-default"));
3757 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("number-up-default"));
3758 cupsArrayAdd(CommonDefaults,
3759 _cupsStrAlloc("orientation-requested-default"));
3760 cupsArrayAdd(CommonDefaults, _cupsStrAlloc("sides-default"));
3761 }
3762
b423cd4c 3763 /*
3764 * Add all of the default options from the .conf files...
3765 */
3766
1f0275e3 3767 for (num_options = 0, options = NULL, i = p->num_options, option = p->options;
b423cd4c 3768 i > 0;
3769 i --, option ++)
3770 {
3771 if (strcmp(option->name, "ipp-options") &&
3772 strcmp(option->name, "job-sheets") &&
3773 strcmp(option->name, "lease-duration"))
3774 {
3775 snprintf(name, sizeof(name), "%s-default", option->name);
3776 num_options = cupsAddOption(name, option->value, num_options, &options);
f7deaa1a 3777
3778 if (!cupsArrayFind(CommonDefaults, name))
3779 cupsArrayAdd(CommonDefaults, _cupsStrAlloc(name));
b423cd4c 3780 }
3781 }
3782
3783 /*
3784 * Convert options to IPP attributes...
3785 */
3786
3787 cupsEncodeOptions2(p->attrs, num_options, options, IPP_TAG_PRINTER);
3788 cupsFreeOptions(num_options, options);
3789
3790 /*
3791 * Add standard -default attributes as needed...
3792 */
3793
3794 if (!cupsGetOption("copies", p->num_options, p->options))
3795 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER, "copies-default",
3796 1);
3797
f7deaa1a 3798 if (!cupsGetOption("document-format", p->num_options, p->options))
c934a06c 3799 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_MIMETYPE,
f7deaa1a 3800 "document-format-default", NULL, "application/octet-stream");
3801
b423cd4c 3802 if (!cupsGetOption("job-hold-until", p->num_options, p->options))
3803 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
3804 "job-hold-until-default", NULL, "no-hold");
3805
3806 if (!cupsGetOption("job-priority", p->num_options, p->options))
3807 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
3808 "job-priority-default", 50);
3809
3810 if (!cupsGetOption("number-up", p->num_options, p->options))
3811 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
3812 "number-up-default", 1);
3813
3814 if (!cupsGetOption("orientation-requested", p->num_options, p->options))
c277e2f8
MS
3815 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NOVALUE,
3816 "orientation-requested-default", NULL, NULL);
f7deaa1a 3817
3818 if (!cupsGetOption("notify-lease-duration", p->num_options, p->options))
3819 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
3820 "notify-lease-duration-default", DefaultLeaseDuration);
3821
3822 if (!cupsGetOption("notify-events", p->num_options, p->options))
3823 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
3824 "notify-events-default", NULL, "job-completed");
b423cd4c 3825}
3826
3827
bd7854cb 3828/*
3829 * 'add_printer_filter()' - Add a MIME filter for a printer.
3830 */
3831
3832static void
3833add_printer_filter(
3834 cupsd_printer_t *p, /* I - Printer to add to */
f7deaa1a 3835 mime_type_t *filtertype, /* I - Filter or prefilter MIME type */
bd7854cb 3836 const char *filter) /* I - Filter to add */
3837{
3838 char super[MIME_MAX_SUPER], /* Super-type for filter */
3839 type[MIME_MAX_TYPE], /* Type for filter */
3840 program[1024]; /* Program/filter name */
3841 int cost; /* Cost of filter */
3842 mime_type_t *temptype; /* MIME type looping var */
3843 char filename[1024]; /* Full filter filename */
3844
3845
3846 /*
3847 * Parse the filter string; it should be in the following format:
3848 *
3849 * super/type cost program
3850 */
3851
1f0275e3
MS
3852 if (sscanf(filter, "%15[^/]/%31s%d%*[ \t]%1023[^\n]", super, type, &cost,
3853 program) != 4)
bd7854cb 3854 {
3855 cupsdLogMessage(CUPSD_LOG_ERROR, "%s: invalid filter string \"%s\"!",
3856 p->name, filter);
3857 return;
3858 }
3859
3860 /*
3861 * See if the filter program exists; if not, stop the printer and flag
3862 * the error!
3863 */
3864
ecdc0628 3865 if (strcmp(program, "-"))
bd7854cb 3866 {
ecdc0628 3867 if (program[0] == '/')
3868 strlcpy(filename, program, sizeof(filename));
3869 else
3870 snprintf(filename, sizeof(filename), "%s/filter/%s", ServerBin, program);
3871
3872 if (access(filename, X_OK))
3873 {
3874 snprintf(p->state_message, sizeof(p->state_message),
3875 "Filter \"%s\" for printer \"%s\" not available: %s",
3876 program, p->name, strerror(errno));
ecdc0628 3877 cupsdSetPrinterReasons(p, "+cups-missing-filter-error");
07725fee 3878 cupsdSetPrinterState(p, IPP_PRINTER_STOPPED, 0);
ecdc0628 3879
3880 cupsdLogMessage(CUPSD_LOG_ERROR, "%s", p->state_message);
3881 }
bd7854cb 3882 }
3883
b423cd4c 3884 /*
3885 * Mark the CUPS_PRINTER_COMMANDS bit if we have a filter for
3886 * application/vnd.cups-command...
3887 */
3888
3889 if (!strcasecmp(super, "application") &&
3890 !strcasecmp(type, "vnd.cups-command"))
3891 p->type |= CUPS_PRINTER_COMMANDS;
3892
bd7854cb 3893 /*
3894 * Add the filter to the MIME database, supporting wildcards as needed...
3895 */
3896
3897 for (temptype = mimeFirstType(MimeDatabase);
3898 temptype;
3899 temptype = mimeNextType(MimeDatabase))
3900 if (((super[0] == '*' && strcasecmp(temptype->super, "printer")) ||
3901 !strcasecmp(temptype->super, super)) &&
3902 (type[0] == '*' || !strcasecmp(temptype->type, type)))
3903 {
3904 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3905 "add_printer_filter: %s: adding filter %s/%s %s/%s %d %s",
3906 p->name, temptype->super, temptype->type,
f7deaa1a 3907 filtertype->super, filtertype->type,
bd7854cb 3908 cost, program);
f7deaa1a 3909 mimeAddFilter(MimeDatabase, temptype, filtertype, cost, program);
bd7854cb 3910 }
3911}
3912
3913
3914/*
3915 * 'add_printer_formats()' - Add document-format-supported values for a printer.
3916 */
3917
3918static void
3919add_printer_formats(cupsd_printer_t *p) /* I - Printer */
3920{
3921 int i; /* Looping var */
3922 mime_type_t *type; /* Current MIME type */
3923 cups_array_t *filters; /* Filters */
80ca4592 3924 ipp_attribute_t *attr; /* document-format-supported attribute */
bd7854cb 3925 char mimetype[MIME_MAX_SUPER + MIME_MAX_TYPE + 2];
3926 /* MIME type name */
3927
3928
3929 /*
3930 * Raw (and remote) queues advertise all of the supported MIME
3931 * types...
3932 */
3933
80ca4592 3934 cupsArrayDelete(p->filetypes);
3935 p->filetypes = NULL;
3936
bd7854cb 3937 if (p->raw)
3938 {
3939 ippAddStrings(p->attrs, IPP_TAG_PRINTER,
3940 (ipp_tag_t)(IPP_TAG_MIMETYPE | IPP_TAG_COPY),
3941 "document-format-supported", NumMimeTypes, NULL, MimeTypes);
3942 return;
3943 }
3944
3945 /*
3946 * Otherwise, loop through the supported MIME types and see if there
3947 * are filters for them...
3948 */
3949
bd7854cb 3950 cupsdLogMessage(CUPSD_LOG_DEBUG2, "add_printer_formats: %d types, %d filters",
3951 mimeNumTypes(MimeDatabase), mimeNumFilters(MimeDatabase));
3952
80ca4592 3953 p->filetypes = cupsArrayNew(NULL, NULL);
bd7854cb 3954
80ca4592 3955 for (type = mimeFirstType(MimeDatabase);
bd7854cb 3956 type;
3957 type = mimeNextType(MimeDatabase))
3958 {
bd7854cb 3959 snprintf(mimetype, sizeof(mimetype), "%s/%s", type->super, type->type);
3960
3961 if ((filters = mimeFilter(MimeDatabase, type, p->filetype, NULL)) != NULL)
3962 {
3963 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3964 "add_printer_formats: %s: %s needs %d filters",
3965 p->name, mimetype, cupsArrayCount(filters));
3966
3967 cupsArrayDelete(filters);
80ca4592 3968 cupsArrayAdd(p->filetypes, type);
bd7854cb 3969 }
3970 else
3971 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3972 "add_printer_formats: %s: %s not supported",
3973 p->name, mimetype);
3974 }
3975
3976 cupsdLogMessage(CUPSD_LOG_DEBUG2,
3977 "add_printer_formats: %s: %d supported types",
80ca4592 3978 p->name, cupsArrayCount(p->filetypes) + 1);
bd7854cb 3979
3980 /*
3981 * Add the file formats that can be filtered...
3982 */
3983
f301802f 3984 if ((type = mimeType(MimeDatabase, "application", "octet-stream")) == NULL ||
3985 !cupsArrayFind(p->filetypes, type))
3986 i = 1;
3987 else
3988 i = 0;
bd7854cb 3989
80ca4592 3990 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_MIMETYPE,
3991 "document-format-supported",
3992 cupsArrayCount(p->filetypes) + 1, NULL, NULL);
3993
f301802f 3994 if (i)
3995 attr->values[0].string.text = _cupsStrAlloc("application/octet-stream");
bd7854cb 3996
f301802f 3997 for (type = (mime_type_t *)cupsArrayFirst(p->filetypes);
80ca4592 3998 type;
3999 i ++, type = (mime_type_t *)cupsArrayNext(p->filetypes))
4000 {
4001 snprintf(mimetype, sizeof(mimetype), "%s/%s", type->super, type->type);
bd7854cb 4002
80ca4592 4003 attr->values[i].string.text = _cupsStrAlloc(mimetype);
4004 }
f7deaa1a 4005
4006#ifdef HAVE_DNSSD
4007 {
4008 char pdl[1024]; /* Buffer to build pdl list */
4009 mime_filter_t *filter; /* MIME filter looping var */
4010
4011
4012 pdl[0] = '\0';
4013
4014 if (mimeType(MimeDatabase, "application", "pdf"))
4015 strlcat(pdl, "application/pdf,", sizeof(pdl));
4016
4017 if (mimeType(MimeDatabase, "application", "postscript"))
4018 strlcat(pdl, "application/postscript,", sizeof(pdl));
4019
4020 if (mimeType(MimeDatabase, "application", "vnd.cups-raster"))
4021 strlcat(pdl, "application/vnd.cups-raster,", sizeof(pdl));
4022
4023 /*
4024 * Determine if this is a Tioga PrintJobMgr based queue...
4025 */
4026
4027 for (filter = (mime_filter_t *)cupsArrayFirst(MimeDatabase->filters);
4028 filter;
4029 filter = (mime_filter_t *)cupsArrayNext(MimeDatabase->filters))
4030 {
4031 if (filter->dst == p->filetype && filter->filter &&
4032 strstr(filter->filter, "PrintJobMgr"))
4033 break;
4034 }
4035
4036 /*
4037 * We only support raw printing if this is not a Tioga PrintJobMgr based
4038 * queue and if application/octet-stream is a known conversion...
4039 */
4040
4041 if (!filter && mimeType(MimeDatabase, "application", "octet-stream"))
4042 strlcat(pdl, "application/octet-stream,", sizeof(pdl));
4043
4044 if (mimeType(MimeDatabase, "image", "png"))
4045 strlcat(pdl, "image/png,", sizeof(pdl));
4046
4047 if (pdl[0])
4048 pdl[strlen(pdl) - 1] = '\0'; /* Remove trailing comma */
4049
4050 cupsdSetString(&p->pdl, pdl);
4051 }
4052#endif /* HAVE_DNSSD */
bd7854cb 4053}
4054
4055
ef416fc2 4056/*
4057 * 'compare_printers()' - Compare two printers.
4058 */
4059
4060static int /* O - Result of comparison */
4061compare_printers(void *first, /* I - First printer */
4062 void *second, /* I - Second printer */
4063 void *data) /* I - App data (not used) */
4064{
4065 return (strcasecmp(((cupsd_printer_t *)first)->name,
4066 ((cupsd_printer_t *)second)->name));
4067}
4068
4069
bd7854cb 4070/*
e1d6a774 4071 * 'delete_printer_filters()' - Delete all MIME filters for a printer.
bd7854cb 4072 */
4073
4074static void
e1d6a774 4075delete_printer_filters(
4076 cupsd_printer_t *p) /* I - Printer to remove from */
bd7854cb 4077{
e1d6a774 4078 mime_filter_t *filter; /* MIME filter looping var */
bd7854cb 4079
bd7854cb 4080
4081 /*
e1d6a774 4082 * Range check input...
bd7854cb 4083 */
4084
e1d6a774 4085 if (p == NULL)
4086 return;
bd7854cb 4087
4088 /*
e1d6a774 4089 * Remove all filters from the MIME database that have a destination
4090 * type == printer...
bd7854cb 4091 */
4092
e1d6a774 4093 for (filter = mimeFirstFilter(MimeDatabase);
4094 filter;
4095 filter = mimeNextFilter(MimeDatabase))
4096 if (filter->dst == p->filetype)
4097 {
4098 /*
4099 * Delete the current filter...
4100 */
bd7854cb 4101
e1d6a774 4102 mimeDeleteFilter(MimeDatabase, filter);
4103 }
bd7854cb 4104}
4105
4106
ef416fc2 4107#ifdef __sgi
4108/*
4109 * 'write_irix_config()' - Update the config files used by the IRIX
4110 * desktop tools.
4111 */
4112
4113static void
4114write_irix_config(cupsd_printer_t *p) /* I - Printer to update */
4115{
4116 char filename[1024]; /* Interface script filename */
4117 cups_file_t *fp; /* Interface script file */
f301802f 4118 ipp_attribute_t *attr; /* Attribute data */
ef416fc2 4119
4120
4121 /*
4122 * Add dummy interface and GUI scripts to fool SGI's "challenged" printing
4123 * tools. First the interface script that tells the tools what kind of
4124 * printer we have...
4125 */
4126
4127 snprintf(filename, sizeof(filename), "/var/spool/lp/interface/%s", p->name);
4128
4129 if (p->type & CUPS_PRINTER_CLASS)
4130 unlink(filename);
4131 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
4132 {
4133 cupsFilePuts(fp, "#!/bin/sh\n");
4134
4135 if ((attr = ippFindAttribute(p->attrs, "printer-make-and-model",
4136 IPP_TAG_TEXT)) != NULL)
4137 cupsFilePrintf(fp, "NAME=\"%s\"\n", attr->values[0].string.text);
4138 else if (p->type & CUPS_PRINTER_CLASS)
4139 cupsFilePuts(fp, "NAME=\"Printer Class\"\n");
4140 else
4141 cupsFilePuts(fp, "NAME=\"Remote Destination\"\n");
4142
4143 if (p->type & CUPS_PRINTER_COLOR)
4144 cupsFilePuts(fp, "TYPE=ColorPostScript\n");
4145 else
4146 cupsFilePuts(fp, "TYPE=MonoPostScript\n");
4147
4148 cupsFilePrintf(fp, "HOSTNAME=%s\n", ServerName);
4149 cupsFilePrintf(fp, "HOSTPRINTER=%s\n", p->name);
4150
4151 cupsFileClose(fp);
4152
4153 chmod(filename, 0755);
4154 chown(filename, User, Group);
4155 }
4156
4157 /*
4158 * Then the member file that tells which device file the queue is connected
4159 * to... Networked printers use "/dev/null" in this file, so that's what
4160 * we use (the actual device URI can confuse some apps...)
4161 */
4162
4163 snprintf(filename, sizeof(filename), "/var/spool/lp/member/%s", p->name);
4164
4165 if (p->type & CUPS_PRINTER_CLASS)
4166 unlink(filename);
4167 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
4168 {
4169 cupsFilePuts(fp, "/dev/null\n");
4170
4171 cupsFileClose(fp);
4172
4173 chmod(filename, 0644);
4174 chown(filename, User, Group);
4175 }
4176
4177 /*
4178 * The gui_interface file is a script or program that launches a GUI
4179 * option panel for the printer, using options specified on the
4180 * command-line in the third argument. The option panel must send
4181 * any printing options to stdout on a single line when the user
4182 * accepts them, or nothing if the user cancels the dialog.
4183 *
4184 * The default options panel program is /usr/bin/glpoptions, from
4185 * the ESP Print Pro software. You can select another using the
4186 * PrintcapGUI option.
4187 */
4188
4189 snprintf(filename, sizeof(filename), "/var/spool/lp/gui_interface/ELF/%s.gui", p->name);
4190
4191 if (p->type & CUPS_PRINTER_CLASS)
4192 unlink(filename);
4193 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
4194 {
4195 cupsFilePuts(fp, "#!/bin/sh\n");
4196 cupsFilePrintf(fp, "%s -d %s -o \"$3\"\n", PrintcapGUI, p->name);
4197
4198 cupsFileClose(fp);
4199
4200 chmod(filename, 0755);
4201 chown(filename, User, Group);
4202 }
4203
4204 /*
4205 * The POD config file is needed by the printstatus command to show
4206 * the printer location and device.
4207 */
4208
4209 snprintf(filename, sizeof(filename), "/var/spool/lp/pod/%s.config", p->name);
4210
4211 if (p->type & CUPS_PRINTER_CLASS)
4212 unlink(filename);
4213 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
4214 {
4215 cupsFilePrintf(fp, "Printer Class | %s\n",
4216 (p->type & CUPS_PRINTER_COLOR) ? "ColorPostScript" : "MonoPostScript");
4217 cupsFilePrintf(fp, "Printer Model | %s\n", p->make_model ? p->make_model : "");
4218 cupsFilePrintf(fp, "Location Code | %s\n", p->location ? p->location : "");
4219 cupsFilePrintf(fp, "Physical Location | %s\n", p->info ? p->info : "");
4220 cupsFilePrintf(fp, "Port Path | %s\n", p->device_uri ? p->device_uri : "");
4221 cupsFilePrintf(fp, "Config Path | /var/spool/lp/pod/%s.config\n", p->name);
4222 cupsFilePrintf(fp, "Active Status Path | /var/spool/lp/pod/%s.status\n", p->name);
4223 cupsFilePuts(fp, "Status Update Wait | 10 seconds\n");
4224
4225 cupsFileClose(fp);
4226
4227 chmod(filename, 0664);
4228 chown(filename, User, Group);
4229 }
4230}
4231
4232
4233/*
4234 * 'write_irix_state()' - Update the status files used by IRIX printing
4235 * desktop tools.
4236 */
4237
4238static void
4239write_irix_state(cupsd_printer_t *p) /* I - Printer to update */
4240{
4241 char filename[1024]; /* Interface script filename */
4242 cups_file_t *fp; /* Interface script file */
4243 int tag; /* Status tag value */
4244
4245
4246 if (p)
4247 {
4248 /*
4249 * The POD status file is needed for the printstatus window to
4250 * provide the current status of the printer.
4251 */
4252
4253 snprintf(filename, sizeof(filename), "/var/spool/lp/pod/%s.status", p->name);
4254
4255 if (p->type & CUPS_PRINTER_CLASS)
4256 unlink(filename);
4257 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
4258 {
4259 cupsFilePrintf(fp, "Operational Status | %s\n",
4260 (p->state == IPP_PRINTER_IDLE) ? "Idle" :
4261 (p->state == IPP_PRINTER_PROCESSING) ? "Busy" :
4262 "Faulted");
4263 cupsFilePrintf(fp, "Information | 01 00 00 | %s\n", CUPS_SVERSION);
4264 cupsFilePrintf(fp, "Information | 02 00 00 | Device URI: %s\n",
4265 p->device_uri ? p->device_uri : "");
4266 cupsFilePrintf(fp, "Information | 03 00 00 | %s jobs\n",
4267 p->accepting ? "Accepting" : "Not accepting");
4268 cupsFilePrintf(fp, "Information | 04 00 00 | %s\n", p->state_message);
4269
4270 cupsFileClose(fp);
4271
4272 chmod(filename, 0664);
4273 chown(filename, User, Group);
4274 }
4275
4276 /*
4277 * The activeicons file is needed to provide desktop icons for printers:
4278 *
4279 * [ quoted from /usr/lib/print/tagit ]
4280 *
4281 * --- Type of printer tags (base values)
4282 *
4283 * Dumb=66048 # 0x10200
4284 * DumbColor=66080 # 0x10220
4285 * Raster=66112 # 0x10240
4286 * ColorRaster=66144 # 0x10260
4287 * Plotter=66176 # 0x10280
4288 * PostScript=66208 # 0x102A0
4289 * ColorPostScript=66240 # 0x102C0
4290 * MonoPostScript=66272 # 0x102E0
4291 *
4292 * --- Printer state modifiers for local printers
4293 *
4294 * Idle=0 # 0x0
4295 * Busy=1 # 0x1
4296 * Faulted=2 # 0x2
4297 * Unknown=3 # 0x3 (Faulted due to unknown reason)
4298 *
4299 * --- Printer state modifiers for network printers
4300 *
4301 * NetIdle=8 # 0x8
4302 * NetBusy=9 # 0x9
4303 * NetFaulted=10 # 0xA
4304 * NetUnknown=11 # 0xB (Faulted due to unknown reason)
4305 */
4306
4307 snprintf(filename, sizeof(filename), "/var/spool/lp/activeicons/%s", p->name);
4308
4309 if (p->type & CUPS_PRINTER_CLASS)
4310 unlink(filename);
4311 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
4312 {
4313 if (p->type & CUPS_PRINTER_COLOR)
4314 tag = 66240;
4315 else
4316 tag = 66272;
4317
4318 if (p->type & CUPS_PRINTER_REMOTE)
4319 tag |= 8;
4320
4321 if (p->state == IPP_PRINTER_PROCESSING)
4322 tag |= 1;
4323
4324 else if (p->state == IPP_PRINTER_STOPPED)
4325 tag |= 2;
4326
4327 cupsFilePuts(fp, "#!/bin/sh\n");
4328 cupsFilePrintf(fp, "#Tag %d\n", tag);
4329
4330 cupsFileClose(fp);
4331
4332 chmod(filename, 0755);
4333 chown(filename, User, Group);
4334 }
4335 }
4336
4337 /*
4338 * The default file is needed by the printers window to show
4339 * the default printer.
4340 */
4341
4342 snprintf(filename, sizeof(filename), "/var/spool/lp/default");
4343
4344 if (DefaultPrinter != NULL)
4345 {
4346 if ((fp = cupsFileOpen(filename, "w")) != NULL)
4347 {
4348 cupsFilePrintf(fp, "%s\n", DefaultPrinter->name);
4349
4350 cupsFileClose(fp);
4351
4352 chmod(filename, 0644);
4353 chown(filename, User, Group);
4354 }
4355 }
4356 else
4357 unlink(filename);
4358}
4359#endif /* __sgi */
4360
4361
4362/*
75bd9771 4363 * End of "$Id: printers.c 7677 2008-06-19 23:22:19Z mike $".
ef416fc2 4364 */