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