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