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