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