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