]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/printers.c
Write printcap file as needed from callers to add/delete printer/class and
[thirdparty/cups.git] / scheduler / printers.c
1 /*
2 * "$Id$"
3 *
4 * Printer routines for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1997-2005 by Easy Software Products, all rights reserved.
7 *
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
13 * at:
14 *
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
19 *
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * Contents:
25 *
26 * cupsdAddPrinter() - Add a printer to the system.
27 * cupsdAddPrinterFilter() - Add a MIME filter for a printer.
28 * cupsdAddPrinterHistory() - Add the current printer state to the history.
29 * cupsdAddPrinterUser() - Add a user to the ACL.
30 * cupsdDeleteAllPrinters() - Delete all printers from the system.
31 * cupsdDeletePrinter() - Delete a printer from the system.
32 * cupsdDeletePrinterFilters() - Delete all MIME filters for a printer.
33 * cupsdFindPrinter() - Find a printer in the list.
34 * cupsdFreePrinterUsers() - Free allow/deny users.
35 * cupsdLoadAllPrinters() - Load printers from the printers.conf file.
36 * cupsdSaveAllPrinters() - Save all printer definitions to the printers.conf
37 * cupsdSetPrinterAttrs() - Set printer attributes based upon the PPD file.
38 * cupsdSetPrinterReasons() - Set/update the reasons strings.
39 * cupsdSetPrinterState() - Update the current state of a printer.
40 * cupsdStopPrinter() - Stop a printer from printing any jobs...
41 * cupsdUpdatePrinters() - Update printers after a partial reload.
42 * cupsdValidateDest() - Validate a printer/class destination.
43 * cupsdWritePrintcap() - Write a pseudo-printcap file for older
44 * applications that need it...
45 * cupsdSanitizeURI() - Sanitize a device URI...
46 * compare_printers() - Compare two printers.
47 * write_irix_config() - Update the config files used by the IRIX
48 * desktop tools.
49 * write_irix_state() - Update the status files used by IRIX printing
50 * desktop tools.
51 */
52
53 /*
54 * Include necessary headers...
55 */
56
57 #include "cupsd.h"
58
59
60 /*
61 * Local functions...
62 */
63
64 static int compare_printers(void *first, void *second, void *data);
65 #ifdef __sgi
66 static void write_irix_config(cupsd_printer_t *p);
67 static void write_irix_state(cupsd_printer_t *p);
68 #endif /* __sgi */
69
70
71 /*
72 * 'cupsdAddPrinter()' - Add a printer to the system.
73 */
74
75 cupsd_printer_t * /* O - New printer */
76 cupsdAddPrinter(const char *name) /* I - Name of printer */
77 {
78 cupsd_printer_t *p; /* New printer */
79
80
81 /*
82 * Range check input...
83 */
84
85 cupsdLogMessage(CUPSD_LOG_DEBUG2, "cupsdAddPrinter(\"%s\")", name);
86
87 /*
88 * Create a new printer entity...
89 */
90
91 if ((p = calloc(1, sizeof(cupsd_printer_t))) == NULL)
92 {
93 cupsdLogMessage(CUPSD_LOG_CRIT, "Unable to allocate memory for printer - %s",
94 strerror(errno));
95 return (NULL);
96 }
97
98 cupsdSetString(&p->name, name);
99 cupsdSetString(&p->info, name);
100 cupsdSetString(&p->hostname, ServerName);
101
102 cupsdSetStringf(&p->uri, "ipp://%s:%d/printers/%s", ServerName, LocalPort, name);
103 cupsdSetStringf(&p->device_uri, "file:/dev/null");
104
105 p->state = IPP_PRINTER_STOPPED;
106 p->state_time = time(NULL);
107 p->accepting = 0;
108 p->shared = 1;
109 p->filetype = mimeAddType(MimeDatabase, "printer", name);
110
111 cupsdSetString(&p->job_sheets[0], "none");
112 cupsdSetString(&p->job_sheets[1], "none");
113
114 cupsdSetString(&p->error_policy, "stop-printer");
115 cupsdSetString(&p->op_policy, DefaultPolicy);
116
117 p->op_policy_ptr = DefaultPolicyPtr;
118
119 if (MaxPrinterHistory)
120 p->history = calloc(MaxPrinterHistory, sizeof(ipp_t *));
121
122 /*
123 * Insert the printer in the printer list alphabetically...
124 */
125
126 if (!Printers)
127 Printers = cupsArrayNew(compare_printers, NULL);
128
129 cupsArrayAdd(Printers, p);
130
131 if (!ImplicitPrinters)
132 ImplicitPrinters = cupsArrayNew(compare_printers, NULL);
133
134 /*
135 * Return the new printer...
136 */
137
138 return (p);
139 }
140
141
142 /*
143 * 'cupsdAddPrinterFilter()' - Add a MIME filter for a printer.
144 */
145
146 void
147 cupsdAddPrinterFilter(
148 cupsd_printer_t *p, /* I - Printer to add to */
149 const char *filter) /* I - Filter to add */
150 {
151 int i; /* Looping var */
152 char super[MIME_MAX_SUPER], /* Super-type for filter */
153 type[MIME_MAX_TYPE], /* Type for filter */
154 program[1024]; /* Program/filter name */
155 int cost; /* Cost of filter */
156 mime_type_t **temptype; /* MIME type looping var */
157
158
159 /*
160 * Range check input...
161 */
162
163 if (p == NULL || p->filetype == NULL || filter == NULL)
164 return;
165
166 /*
167 * Parse the filter string; it should be in the following format:
168 *
169 * super/type cost program
170 */
171
172 if (sscanf(filter, "%15[^/]/%31s%d%1023s", super, type, &cost, program) != 4)
173 {
174 cupsdLogMessage(CUPSD_LOG_ERROR,
175 "cupsdAddPrinterFilter: Invalid filter string \"%s\"!",
176 filter);
177 return;
178 }
179
180 /*
181 * Add the filter to the MIME database, supporting wildcards as needed...
182 */
183
184 for (temptype = MimeDatabase->types, i = MimeDatabase->num_types;
185 i > 0;
186 i --, temptype ++)
187 if (((super[0] == '*' && strcasecmp((*temptype)->super, "printer") != 0) ||
188 !strcasecmp((*temptype)->super, super)) &&
189 (type[0] == '*' || !strcasecmp((*temptype)->type, type)))
190 {
191 cupsdLogMessage(CUPSD_LOG_DEBUG2, "Adding filter %s/%s %s/%s %d %s",
192 (*temptype)->super, (*temptype)->type,
193 p->filetype->super, p->filetype->type,
194 cost, program);
195 mimeAddFilter(MimeDatabase, *temptype, p->filetype, cost, program);
196 }
197 }
198
199
200 /*
201 * 'cupsdAddPrinterHistory()' - Add the current printer state to the history.
202 */
203
204 void
205 cupsdAddPrinterHistory(
206 cupsd_printer_t *p) /* I - Printer */
207 {
208 ipp_t *history; /* History collection */
209
210
211 /*
212 * Stop early if we aren't keeping history data...
213 */
214
215 if (MaxPrinterHistory <= 0)
216 return;
217
218 /*
219 * Retire old history data as needed...
220 */
221
222 p->sequence_number ++;
223
224 if (p->num_history >= MaxPrinterHistory)
225 {
226 p->num_history --;
227 ippDelete(p->history[0]);
228 memmove(p->history, p->history + 1, p->num_history * sizeof(ipp_t *));
229 }
230
231 /*
232 * Create a collection containing the current printer-state, printer-up-time,
233 * printer-state-message, and printer-state-reasons attributes.
234 */
235
236 history = ippNew();
237 ippAddInteger(history, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-state",
238 p->state);
239 ippAddBoolean(history, IPP_TAG_PRINTER, "printer-is-accepting-jobs",
240 p->accepting);
241 ippAddString(history, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-state-message",
242 NULL, p->state_message);
243 if (p->num_reasons == 0)
244 ippAddString(history, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
245 "printer-state-reasons", NULL,
246 p->state == IPP_PRINTER_STOPPED ? "paused" : "none");
247 else
248 ippAddStrings(history, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
249 "printer-state-reasons", p->num_reasons, NULL,
250 (const char * const *)p->reasons);
251 ippAddInteger(history, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
252 "printer-state-change-time", p->state_time);
253 ippAddInteger(history, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
254 "printer-state-sequence-number", p->sequence_number);
255
256 p->history[p->num_history] = history;
257 p->num_history ++;
258 }
259
260
261 /*
262 * 'cupsdAddPrinterUser()' - Add a user to the ACL.
263 */
264
265 void
266 cupsdAddPrinterUser(
267 cupsd_printer_t *p, /* I - Printer */
268 const char *username) /* I - User */
269 {
270 const char **temp; /* Temporary array pointer */
271
272
273 if (!p || !username)
274 return;
275
276 if (p->num_users == 0)
277 temp = malloc(sizeof(char **));
278 else
279 temp = realloc(p->users, sizeof(char **) * (p->num_users + 1));
280
281 if (!temp)
282 return;
283
284 p->users = temp;
285 temp += p->num_users;
286
287 if ((*temp = strdup(username)) != NULL)
288 p->num_users ++;
289 }
290
291
292 /*
293 * 'cupsdCreateCommonData()' - Create the common printer data.
294 */
295
296 void
297 cupsdCreateCommonData(void)
298 {
299 int i; /* Looping var */
300 ipp_attribute_t *attr; /* Attribute data */
301 static const int nups[] = /* number-up-supported values */
302 { 1, 2, 4, 6, 9, 16 };
303 static const ipp_orient_t orients[4] =/* orientation-requested-supported values */
304 {
305 IPP_PORTRAIT,
306 IPP_LANDSCAPE,
307 IPP_REVERSE_LANDSCAPE,
308 IPP_REVERSE_PORTRAIT
309 };
310 static const char * const holds[] = /* job-hold-until-supported values */
311 {
312 "no-hold",
313 "indefinite",
314 "day-time",
315 "evening",
316 "night",
317 "second-shift",
318 "third-shift",
319 "weekend"
320 };
321 static const char * const versions[] =/* ipp-versions-supported values */
322 {
323 "1.0",
324 "1.1"
325 };
326 static const ipp_op_t ops[] = /* operations-supported values */
327 {
328 IPP_PRINT_JOB,
329 IPP_VALIDATE_JOB,
330 IPP_CREATE_JOB,
331 IPP_SEND_DOCUMENT,
332 IPP_CANCEL_JOB,
333 IPP_GET_JOB_ATTRIBUTES,
334 IPP_GET_JOBS,
335 IPP_GET_PRINTER_ATTRIBUTES,
336 IPP_HOLD_JOB,
337 IPP_RELEASE_JOB,
338 IPP_PAUSE_PRINTER,
339 IPP_RESUME_PRINTER,
340 IPP_PURGE_JOBS,
341 IPP_SET_JOB_ATTRIBUTES,
342 IPP_CREATE_PRINTER_SUBSCRIPTION,
343 IPP_CREATE_JOB_SUBSCRIPTION,
344 IPP_GET_SUBSCRIPTION_ATTRIBUTES,
345 IPP_GET_SUBSCRIPTIONS,
346 IPP_RENEW_SUBSCRIPTION,
347 IPP_CANCEL_SUBSCRIPTION,
348 IPP_GET_NOTIFICATIONS,
349 IPP_ENABLE_PRINTER,
350 IPP_DISABLE_PRINTER,
351 CUPS_GET_DEFAULT,
352 CUPS_GET_PRINTERS,
353 CUPS_ADD_PRINTER,
354 CUPS_DELETE_PRINTER,
355 CUPS_GET_CLASSES,
356 CUPS_ADD_CLASS,
357 CUPS_DELETE_CLASS,
358 CUPS_ACCEPT_JOBS,
359 CUPS_REJECT_JOBS,
360 CUPS_SET_DEFAULT,
361 CUPS_GET_DEVICES,
362 CUPS_GET_PPDS,
363 CUPS_MOVE_JOB,
364 CUPS_AUTHENTICATE_JOB,
365 IPP_RESTART_JOB
366 };
367 static const char * const charsets[] =/* charset-supported values */
368 {
369 "us-ascii",
370 "utf-8"
371 };
372 static const char * const compressions[] =
373 { /* document-compression-supported values */
374 "none"
375 #ifdef HAVE_LIBZ
376 ,"gzip"
377 #endif /* HAVE_LIBZ */
378 };
379 static const char * const multiple_document_handling[] =
380 { /* multiple-document-handling-supported values */
381 "separate-documents-uncollated-copies",
382 "separate-documents-collated-copies"
383 };
384 static const char * const errors[] = /* printer-error-policy-supported values */
385 {
386 "abort-job",
387 "retry-job",
388 "stop-printer"
389 };
390 static const char * const notify_attrs[] =
391 { /* notify-attributes-supported values */
392 "printer-state-change-time",
393 "notify-lease-expiration-time",
394 "notify-subscriber-user-name"
395 };
396 static const char * const notify_events[] =
397 { /* notify-events-supported values */
398 "job-completed",
399 "job-config-changed",
400 "job-created",
401 "job-progress",
402 "job-state-changed",
403 "job-stopped",
404 "printer-added",
405 "printer-changed",
406 "printer-config-changed",
407 "printer-deleted",
408 "printer-finishings-changed",
409 "printer-media-changed",
410 "printer-modified",
411 "printer-restarted",
412 "printer-shutdown",
413 "printer-state-changed",
414 "printer-stopped",
415 "server-audit",
416 "server-restarted",
417 "server-started",
418 "server-stopped"
419 };
420
421
422 if (CommonData)
423 ippDelete(CommonData);
424
425 CommonData = ippNew();
426
427 /*
428 * This list of attributes is sorted to improve performance when the
429 * client provides a requested-attributes attribute...
430 */
431
432 /* charset-configured */
433 ippAddString(CommonData, IPP_TAG_PRINTER, IPP_TAG_CHARSET,
434 "charset-configured", NULL, DefaultCharset);
435
436 /* charset-supported */
437 ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_CHARSET,
438 "charset-supported", sizeof(charsets) / sizeof(charsets[0]),
439 NULL, charsets);
440
441 /* compression-supported */
442 ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
443 "compression-supported",
444 sizeof(compressions) / sizeof(compressions[0]),
445 NULL, compressions);
446
447 /* TODO: move to printer-specific section! */
448 /* copies-default */
449 ippAddInteger(CommonData, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
450 "copies-default", 1);
451
452 /* copies-supported */
453 ippAddRange(CommonData, IPP_TAG_PRINTER, "copies-supported", 1, MaxCopies);
454
455 /* TODO: move to printer-specific section! */
456 /* document-format-default */
457 ippAddString(CommonData, IPP_TAG_PRINTER, IPP_TAG_MIMETYPE,
458 "document-format-default", NULL, "application/octet-stream");
459
460 /* document-format-supported */
461 ippAddStrings(CommonData, IPP_TAG_PRINTER,
462 (ipp_tag_t)(IPP_TAG_MIMETYPE | IPP_TAG_COPY),
463 "document-format-supported", NumMimeTypes, NULL, MimeTypes);
464
465 /* generated-natural-language-supported */
466 ippAddString(CommonData, IPP_TAG_PRINTER, IPP_TAG_LANGUAGE,
467 "generated-natural-language-supported", NULL, DefaultLanguage);
468
469 /* ipp-versions-supported */
470 ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
471 "ipp-versions-supported", sizeof(versions) / sizeof(versions[0]),
472 NULL, versions);
473
474 /* TODO: move to printer-specific section! */
475 /* job-hold-until-default */
476 ippAddString(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
477 "job-hold-until-default", NULL, "no-hold");
478
479 /* job-hold-until-supported */
480 ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
481 "job-hold-until-supported", sizeof(holds) / sizeof(holds[0]),
482 NULL, holds);
483
484 /* TODO: move to printer-specific section! */
485 /* job-priority-default */
486 ippAddInteger(CommonData, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
487 "job-priority-default", 50);
488
489 /* job-priority-supported */
490 ippAddInteger(CommonData, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
491 "job-priority-supported", 100);
492
493 /* job-sheets-supported */
494 if (NumBanners > 0)
495 {
496 /*
497 * Setup the job-sheets-supported attribute...
498 */
499
500 if (Classification && !ClassifyOverride)
501 attr = ippAddString(CommonData, IPP_TAG_PRINTER, IPP_TAG_NAME,
502 "job-sheets-supported", NULL, Classification);
503 else
504 attr = ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_NAME,
505 "job-sheets-supported", NumBanners + 1, NULL, NULL);
506
507 if (attr == NULL)
508 cupsdLogMessage(CUPSD_LOG_EMERG,
509 "cupsdSetPrinterAttrs: Unable to allocate memory for "
510 "job-sheets-supported attribute: %s!", strerror(errno));
511 else if (!Classification || ClassifyOverride)
512 {
513 attr->values[0].string.text = strdup("none");
514
515 for (i = 0; i < NumBanners; i ++)
516 attr->values[i + 1].string.text = strdup(Banners[i].name);
517 }
518 }
519 else
520 ippAddString(CommonData, IPP_TAG_PRINTER, IPP_TAG_NAME,
521 "job-sheets-supported", NULL, "none");
522
523 /* multiple-document-handling-supported */
524 ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
525 "multiple-document-handling-supported",
526 sizeof(multiple_document_handling) /
527 sizeof(multiple_document_handling[0]), NULL,
528 multiple_document_handling);
529
530 /* multiple-document-jobs-supported */
531 ippAddBoolean(CommonData, IPP_TAG_PRINTER,
532 "multiple-document-jobs-supported", 1);
533
534 /* multiple-operation-time-out */
535 ippAddInteger(CommonData, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
536 "multiple-operation-time-out", 60);
537
538 /* natural-language-configured */
539 ippAddString(CommonData, IPP_TAG_PRINTER, IPP_TAG_LANGUAGE,
540 "natural-language-configured", NULL, DefaultLanguage);
541
542 /* notify-attributes-supported */
543 ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
544 "notify-attributes-supported",
545 (int)(sizeof(notify_attrs) / sizeof(notify_attrs[0])),
546 NULL, notify_attrs);
547
548 /* TODO: move to printer-specific section! */
549 /* notify-lease-duration-default */
550 ippAddInteger(CommonData, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
551 "notify-lease-duration-default", DefaultLeaseDuration);
552
553 /* notify-lease-duration-supported */
554 ippAddRange(CommonData, IPP_TAG_PRINTER,
555 "notify-lease-duration-supported", 0,
556 MaxLeaseDuration ? MaxLeaseDuration : 2147483647);
557
558 /* notify-max-events-supported */
559 ippAddInteger(CommonData, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
560 "notify-max-events-supported", MaxEvents);
561
562 /* notify-notify-events-default */
563 ippAddString(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
564 "notify-events-default", NULL, "job-completed");
565
566 /* notify-notify-events-supported */
567 ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
568 "notify-events-supported",
569 (int)(sizeof(notify_events) / sizeof(notify_events[0])),
570 NULL, notify_events);
571
572 /* notify-pull-method-supported */
573 ippAddString(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
574 "notify-pull-method-supported", NULL, "ippget");
575
576 /* TODO: scan notifier directory */
577 /* notify-schemes-supported */
578 ippAddString(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
579 "notify-schemes-supported", NULL, "mailto");
580
581 /* TODO: move to printer-specific section! */
582 /* number-up-default */
583 ippAddInteger(CommonData, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
584 "number-up-default", 1);
585
586 /* number-up-supported */
587 ippAddIntegers(CommonData, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
588 "number-up-supported", sizeof(nups) / sizeof(nups[0]), nups);
589
590 /* operations-supported */
591 ippAddIntegers(CommonData, IPP_TAG_PRINTER, IPP_TAG_ENUM,
592 "operations-supported",
593 sizeof(ops) / sizeof(ops[0]) + JobFiles - 1, (int *)ops);
594
595 /* TODO: move to printer-specific section! */
596 /* orientation-requested-default */
597 ippAddInteger(CommonData, IPP_TAG_PRINTER, IPP_TAG_ENUM,
598 "orientation-requested-default", IPP_PORTRAIT);
599
600 /* orientation-requested-supported */
601 ippAddIntegers(CommonData, IPP_TAG_PRINTER, IPP_TAG_ENUM,
602 "orientation-requested-supported", 4, (int *)orients);
603
604 /* page-ranges-supported */
605 ippAddBoolean(CommonData, IPP_TAG_PRINTER, "page-ranges-supported", 1);
606
607 /* pdf-override-supported */
608 ippAddString(CommonData, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
609 "pdl-override-supported", NULL, "not-attempted");
610
611 /* printer-error-policy-supported */
612 ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_NAME,
613 "printer-error-policy-supported",
614 sizeof(errors) / sizeof(errors[0]), NULL, errors);
615
616 /* printer-op-policy-supported */
617 attr = ippAddStrings(CommonData, IPP_TAG_PRINTER, IPP_TAG_NAME,
618 "printer-op-policy-supported", NumPolicies, NULL, NULL);
619 for (i = 0; i < NumPolicies; i ++)
620 attr->values[i].string.text = strdup(Policies[i]->name);
621 }
622
623
624 /*
625 * 'cupsdDeleteAllPrinters()' - Delete all printers from the system.
626 */
627
628 void
629 cupsdDeleteAllPrinters(void)
630 {
631 cupsd_printer_t *p; /* Pointer to current printer/class */
632
633
634 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
635 p;
636 p = (cupsd_printer_t *)cupsArrayNext(Printers))
637 if (!(p->type & CUPS_PRINTER_CLASS))
638 cupsdDeletePrinter(p, 0);
639 }
640
641
642 /*
643 * 'cupsdDeletePrinter()' - Delete a printer from the system.
644 */
645
646 void
647 cupsdDeletePrinter(
648 cupsd_printer_t *p, /* I - Printer to delete */
649 int update) /* I - Update printers.conf? */
650 {
651 int i; /* Looping var */
652 #ifdef __sgi
653 char filename[1024]; /* Interface script filename */
654 #endif /* __sgi */
655
656
657 DEBUG_printf(("cupsdDeletePrinter(%08x): p->name = \"%s\"...\n", p, p->name));
658
659 /*
660 * Stop printing on this printer...
661 */
662
663 cupsdStopPrinter(p, update);
664
665 /*
666 * If this printer is the next for browsing, point to the next one...
667 */
668
669 if (p == BrowseNext)
670 {
671 cupsArrayFind(Printers, p);
672 BrowseNext = (cupsd_printer_t *)cupsArrayNext(Printers);
673 }
674
675 /*
676 * Remove the printer from the list...
677 */
678
679 cupsArrayRemove(Printers, p);
680
681 /*
682 * Remove the dummy interface/icon/option files under IRIX...
683 */
684
685 #ifdef __sgi
686 snprintf(filename, sizeof(filename), "/var/spool/lp/interface/%s", p->name);
687 unlink(filename);
688
689 snprintf(filename, sizeof(filename), "/var/spool/lp/gui_interface/ELF/%s.gui",
690 p->name);
691 unlink(filename);
692
693 snprintf(filename, sizeof(filename), "/var/spool/lp/activeicons/%s", p->name);
694 unlink(filename);
695
696 snprintf(filename, sizeof(filename), "/var/spool/lp/pod/%s.config", p->name);
697 unlink(filename);
698
699 snprintf(filename, sizeof(filename), "/var/spool/lp/pod/%s.status", p->name);
700 unlink(filename);
701
702 snprintf(filename, sizeof(filename), "/var/spool/lp/member/%s", p->name);
703 unlink(filename);
704 #endif /* __sgi */
705
706 /*
707 * If p is the default printer, assign the next one...
708 */
709
710 if (p == DefaultPrinter)
711 DefaultPrinter = (cupsd_printer_t *)cupsArrayFirst(Printers);
712
713 /*
714 * Remove this printer from any classes and send a browse delete message...
715 */
716
717 if (!(p->type & CUPS_PRINTER_IMPLICIT))
718 {
719 cupsdDeletePrinterFromClasses(p);
720 cupsdSendBrowseDelete(p);
721 }
722
723 /*
724 * Free all memory used by the printer...
725 */
726
727 if (p->printers != NULL)
728 free(p->printers);
729
730 if (MaxPrinterHistory)
731 {
732 for (i = 0; i < p->num_history; i ++)
733 ippDelete(p->history[i]);
734
735 free(p->history);
736 }
737
738 for (i = 0; i < p->num_reasons; i ++)
739 free(p->reasons[i]);
740
741 ippDelete(p->attrs);
742
743 cupsdDeletePrinterFilters(p);
744
745 cupsdFreePrinterUsers(p);
746 cupsdFreeQuotas(p);
747
748 cupsdClearString(&p->uri);
749 cupsdClearString(&p->hostname);
750 cupsdClearString(&p->name);
751 cupsdClearString(&p->location);
752 cupsdClearString(&p->make_model);
753 cupsdClearString(&p->info);
754 cupsdClearString(&p->job_sheets[0]);
755 cupsdClearString(&p->job_sheets[1]);
756 cupsdClearString(&p->device_uri);
757 cupsdClearString(&p->port_monitor);
758 cupsdClearString(&p->op_policy);
759 cupsdClearString(&p->error_policy);
760
761 free(p);
762 }
763
764
765 /*
766 * 'cupsdDeletePrinterFilters()' - Delete all MIME filters for a printer.
767 */
768
769 void
770 cupsdDeletePrinterFilters(
771 cupsd_printer_t *p) /* I - Printer to remove from */
772 {
773 int i; /* Looping var */
774 mime_filter_t *filter; /* MIME filter looping var */
775
776
777 /*
778 * Range check input...
779 */
780
781 if (p == NULL)
782 return;
783
784 /*
785 * Remove all filters from the MIME database that have a destination
786 * type == printer...
787 */
788
789 for (filter = MimeDatabase->filters, i = MimeDatabase->num_filters;
790 i > 0;
791 i --, filter ++)
792 if (filter->dst == p->filetype)
793 {
794 /*
795 * Delete the current filter...
796 */
797
798 MimeDatabase->num_filters --;
799
800 if (i > 1)
801 memmove(filter, filter + 1, sizeof(mime_filter_t) * (i - 1));
802
803 filter --;
804 }
805 }
806
807
808 /*
809 * 'cupsdFindDest()' - Find a destination in the list.
810 */
811
812 cupsd_printer_t * /* O - Destination in list */
813 cupsdFindDest(const char *name) /* I - Name of printer or class to find */
814 {
815 cupsd_printer_t key; /* Search key */
816
817
818 key.name = (char *)name;
819 return ((cupsd_printer_t *)cupsArrayFind(Printers, &key));
820 }
821
822
823 /*
824 * 'cupsdFindPrinter()' - Find a printer in the list.
825 */
826
827 cupsd_printer_t * /* O - Printer in list */
828 cupsdFindPrinter(const char *name) /* I - Name of printer to find */
829 {
830 cupsd_printer_t *p; /* Printer in list */
831
832
833 if ((p = cupsdFindDest(name)) != NULL && (p->type & CUPS_PRINTER_CLASS))
834 return (NULL);
835 else
836 return (p);
837 }
838
839
840 /*
841 * 'cupsdFreePrinterUsers()' - Free allow/deny users.
842 */
843
844 void
845 cupsdFreePrinterUsers(
846 cupsd_printer_t *p) /* I - Printer */
847 {
848 int i; /* Looping var */
849
850
851 if (!p || !p->num_users)
852 return;
853
854 for (i = 0; i < p->num_users; i ++)
855 free((void *)p->users[i]);
856
857 free(p->users);
858
859 p->num_users = 0;
860 p->users = NULL;
861 }
862
863
864 /*
865 * 'cupsdLoadAllPrinters()' - Load printers from the printers.conf file.
866 */
867
868 void
869 cupsdLoadAllPrinters(void)
870 {
871 cups_file_t *fp; /* printers.conf file */
872 int linenum; /* Current line number */
873 char line[1024], /* Line from file */
874 *value, /* Pointer to value */
875 *valueptr; /* Pointer into value */
876 cupsd_printer_t *p; /* Current printer */
877
878
879 /*
880 * Open the printers.conf file...
881 */
882
883 snprintf(line, sizeof(line), "%s/printers.conf", ServerRoot);
884 if ((fp = cupsFileOpen(line, "r")) == NULL)
885 {
886 cupsdLogMessage(CUPSD_LOG_ERROR,
887 "cupsdLoadAllPrinters: Unable to open %s - %s", line,
888 strerror(errno));
889 return;
890 }
891
892 /*
893 * Read printer configurations until we hit EOF...
894 */
895
896 linenum = 0;
897 p = NULL;
898
899 while (cupsFileGetConf(fp, line, sizeof(line), &value, &linenum))
900 {
901 /*
902 * Decode the directive...
903 */
904
905 if (!strcasecmp(line, "<Printer") ||
906 !strcasecmp(line, "<DefaultPrinter"))
907 {
908 /*
909 * <Printer name> or <DefaultPrinter name>
910 */
911
912 if (p == NULL && value)
913 {
914 /*
915 * Add the printer and a base file type...
916 */
917
918 cupsdLogMessage(CUPSD_LOG_DEBUG,
919 "cupsdLoadAllPrinters: Loading printer %s...", value);
920
921 p = cupsdAddPrinter(value);
922 p->accepting = 1;
923 p->state = IPP_PRINTER_IDLE;
924
925 /*
926 * Set the default printer as needed...
927 */
928
929 if (!strcasecmp(line, "<DefaultPrinter"))
930 DefaultPrinter = p;
931 }
932 else
933 {
934 cupsdLogMessage(CUPSD_LOG_ERROR,
935 "Syntax error on line %d of printers.conf.", linenum);
936 return;
937 }
938 }
939 else if (!strcasecmp(line, "</Printer>"))
940 {
941 if (p != NULL)
942 {
943 /*
944 * Close out the current printer...
945 */
946
947 cupsdSetPrinterAttrs(p);
948 cupsdAddPrinterHistory(p);
949
950 if (p->device_uri && strncmp(p->device_uri, "file:", 5) &&
951 p->state != IPP_PRINTER_STOPPED)
952 {
953 /*
954 * See if the backend exists...
955 */
956
957 snprintf(line, sizeof(line), "%s/backend/%s", ServerBin,
958 p->device_uri);
959
960 if ((valueptr = strchr(line + strlen(ServerBin), ':')) != NULL)
961 *valueptr = '\0'; /* Chop everything but URI scheme */
962
963 if (access(line, 0))
964 {
965 /*
966 * Backend does not exist, stop printer...
967 */
968
969 p->state = IPP_PRINTER_STOPPED;
970 snprintf(p->state_message, sizeof(p->state_message),
971 "Backend %s does not exist!", line);
972 }
973 }
974
975 p = NULL;
976 }
977 else
978 {
979 cupsdLogMessage(CUPSD_LOG_ERROR,
980 "Syntax error on line %d of printers.conf.", linenum);
981 return;
982 }
983 }
984 else if (!p)
985 {
986 cupsdLogMessage(CUPSD_LOG_ERROR,
987 "Syntax error on line %d of printers.conf.", linenum);
988 return;
989 }
990 else if (!strcasecmp(line, "Info"))
991 {
992 if (value)
993 cupsdSetString(&p->info, value);
994 }
995 else if (!strcasecmp(line, "Location"))
996 {
997 if (value)
998 cupsdSetString(&p->location, value);
999 }
1000 else if (!strcasecmp(line, "DeviceURI"))
1001 {
1002 if (value)
1003 cupsdSetString(&p->device_uri, value);
1004 else
1005 {
1006 cupsdLogMessage(CUPSD_LOG_ERROR,
1007 "Syntax error on line %d of printers.conf.", linenum);
1008 return;
1009 }
1010 }
1011 else if (!strcasecmp(line, "PortMonitor"))
1012 {
1013 if (value && strcmp(value, "none"))
1014 cupsdSetString(&p->port_monitor, value);
1015 else if (value)
1016 cupsdClearString(&p->port_monitor);
1017 else
1018 {
1019 cupsdLogMessage(CUPSD_LOG_ERROR,
1020 "Syntax error on line %d of printers.conf.", linenum);
1021 return;
1022 }
1023 }
1024 else if (!strcasecmp(line, "State"))
1025 {
1026 /*
1027 * Set the initial queue state...
1028 */
1029
1030 if (value && !strcasecmp(value, "idle"))
1031 p->state = IPP_PRINTER_IDLE;
1032 else if (value && !strcasecmp(value, "stopped"))
1033 p->state = IPP_PRINTER_STOPPED;
1034 else
1035 {
1036 cupsdLogMessage(CUPSD_LOG_ERROR,
1037 "Syntax error on line %d of printers.conf.", linenum);
1038 return;
1039 }
1040 }
1041 else if (!strcasecmp(line, "StateMessage"))
1042 {
1043 /*
1044 * Set the initial queue state message...
1045 */
1046
1047 if (value)
1048 strlcpy(p->state_message, value, sizeof(p->state_message));
1049 }
1050 else if (!strcasecmp(line, "StateTime"))
1051 {
1052 /*
1053 * Set the state time...
1054 */
1055
1056 if (value)
1057 p->state_time = atoi(value);
1058 }
1059 else if (!strcasecmp(line, "Accepting"))
1060 {
1061 /*
1062 * Set the initial accepting state...
1063 */
1064
1065 if (value &&
1066 (!strcasecmp(value, "yes") ||
1067 !strcasecmp(value, "on") ||
1068 !strcasecmp(value, "true")))
1069 p->accepting = 1;
1070 else if (value &&
1071 (!strcasecmp(value, "no") ||
1072 !strcasecmp(value, "off") ||
1073 !strcasecmp(value, "false")))
1074 p->accepting = 0;
1075 else
1076 {
1077 cupsdLogMessage(CUPSD_LOG_ERROR,
1078 "Syntax error on line %d of printers.conf.", linenum);
1079 return;
1080 }
1081 }
1082 else if (!strcasecmp(line, "Shared"))
1083 {
1084 /*
1085 * Set the initial shared state...
1086 */
1087
1088 if (value &&
1089 (!strcasecmp(value, "yes") ||
1090 !strcasecmp(value, "on") ||
1091 !strcasecmp(value, "true")))
1092 p->shared = 1;
1093 else if (value &&
1094 (!strcasecmp(value, "no") ||
1095 !strcasecmp(value, "off") ||
1096 !strcasecmp(value, "false")))
1097 p->shared = 0;
1098 else
1099 {
1100 cupsdLogMessage(CUPSD_LOG_ERROR,
1101 "Syntax error on line %d of printers.conf.", linenum);
1102 return;
1103 }
1104 }
1105 else if (!strcasecmp(line, "JobSheets"))
1106 {
1107 /*
1108 * Set the initial job sheets...
1109 */
1110
1111 if (value)
1112 {
1113 for (valueptr = value; *valueptr && !isspace(*valueptr & 255); valueptr ++);
1114
1115 if (*valueptr)
1116 *valueptr++ = '\0';
1117
1118 cupsdSetString(&p->job_sheets[0], value);
1119
1120 while (isspace(*valueptr & 255))
1121 valueptr ++;
1122
1123 if (*valueptr)
1124 {
1125 for (value = valueptr; *valueptr && !isspace(*valueptr & 255); valueptr ++);
1126
1127 if (*valueptr)
1128 *valueptr++ = '\0';
1129
1130 cupsdSetString(&p->job_sheets[1], value);
1131 }
1132 }
1133 else
1134 {
1135 cupsdLogMessage(CUPSD_LOG_ERROR,
1136 "Syntax error on line %d of printers.conf.", linenum);
1137 return;
1138 }
1139 }
1140 else if (!strcasecmp(line, "AllowUser"))
1141 {
1142 if (value)
1143 {
1144 p->deny_users = 0;
1145 cupsdAddPrinterUser(p, value);
1146 }
1147 else
1148 {
1149 cupsdLogMessage(CUPSD_LOG_ERROR,
1150 "Syntax error on line %d of printers.conf.", linenum);
1151 return;
1152 }
1153 }
1154 else if (!strcasecmp(line, "DenyUser"))
1155 {
1156 if (value)
1157 {
1158 p->deny_users = 1;
1159 cupsdAddPrinterUser(p, value);
1160 }
1161 else
1162 {
1163 cupsdLogMessage(CUPSD_LOG_ERROR,
1164 "Syntax error on line %d of printers.conf.", linenum);
1165 return;
1166 }
1167 }
1168 else if (!strcasecmp(line, "QuotaPeriod"))
1169 {
1170 if (value)
1171 p->quota_period = atoi(value);
1172 else
1173 {
1174 cupsdLogMessage(CUPSD_LOG_ERROR,
1175 "Syntax error on line %d of printers.conf.", linenum);
1176 return;
1177 }
1178 }
1179 else if (!strcasecmp(line, "PageLimit"))
1180 {
1181 if (value)
1182 p->page_limit = atoi(value);
1183 else
1184 {
1185 cupsdLogMessage(CUPSD_LOG_ERROR,
1186 "Syntax error on line %d of printers.conf.", linenum);
1187 return;
1188 }
1189 }
1190 else if (!strcasecmp(line, "KLimit"))
1191 {
1192 if (value)
1193 p->k_limit = atoi(value);
1194 else
1195 {
1196 cupsdLogMessage(CUPSD_LOG_ERROR,
1197 "Syntax error on line %d of printers.conf.", linenum);
1198 return;
1199 }
1200 }
1201 else if (!strcasecmp(line, "OpPolicy"))
1202 {
1203 if (value)
1204 cupsdSetString(&p->op_policy, value);
1205 else
1206 {
1207 cupsdLogMessage(CUPSD_LOG_ERROR,
1208 "Syntax error on line %d of printers.conf.", linenum);
1209 return;
1210 }
1211 }
1212 else if (!strcasecmp(line, "ErrorPolicy"))
1213 {
1214 if (value)
1215 cupsdSetString(&p->error_policy, value);
1216 else
1217 {
1218 cupsdLogMessage(CUPSD_LOG_ERROR,
1219 "Syntax error on line %d of printers.conf.", linenum);
1220 return;
1221 }
1222 }
1223 else
1224 {
1225 /*
1226 * Something else we don't understand...
1227 */
1228
1229 cupsdLogMessage(CUPSD_LOG_ERROR,
1230 "Unknown configuration directive %s on line %d of printers.conf.",
1231 line, linenum);
1232 }
1233 }
1234
1235 cupsFileClose(fp);
1236 }
1237
1238
1239 /*
1240 * 'cupsdSaveAllPrinters()' - Save all printer definitions to the printers.conf
1241 * file.
1242 */
1243
1244 void
1245 cupsdSaveAllPrinters(void)
1246 {
1247 int i; /* Looping var */
1248 cups_file_t *fp; /* printers.conf file */
1249 char temp[1024]; /* Temporary string */
1250 char backup[1024]; /* printers.conf.O file */
1251 cupsd_printer_t *printer; /* Current printer class */
1252 time_t curtime; /* Current time */
1253 struct tm *curdate; /* Current date */
1254
1255
1256 /*
1257 * Create the printers.conf file...
1258 */
1259
1260 snprintf(temp, sizeof(temp), "%s/printers.conf", ServerRoot);
1261 snprintf(backup, sizeof(backup), "%s/printers.conf.O", ServerRoot);
1262
1263 if (rename(temp, backup))
1264 {
1265 if (errno != ENOENT)
1266 cupsdLogMessage(CUPSD_LOG_ERROR,
1267 "Unable to backup printers.conf - %s", strerror(errno));
1268 }
1269
1270 if ((fp = cupsFileOpen(temp, "w")) == NULL)
1271 {
1272 cupsdLogMessage(CUPSD_LOG_ERROR,
1273 "Unable to save printers.conf - %s", strerror(errno));
1274
1275 if (rename(backup, temp))
1276 cupsdLogMessage(CUPSD_LOG_ERROR,
1277 "Unable to restore printers.conf - %s", strerror(errno));
1278 return;
1279 }
1280 else
1281 cupsdLogMessage(CUPSD_LOG_INFO, "Saving printers.conf...");
1282
1283 /*
1284 * Restrict access to the file...
1285 */
1286
1287 fchown(cupsFileNumber(fp), getuid(), Group);
1288 fchmod(cupsFileNumber(fp), ConfigFilePerm);
1289
1290 /*
1291 * Write a small header to the file...
1292 */
1293
1294 curtime = time(NULL);
1295 curdate = localtime(&curtime);
1296 strftime(temp, sizeof(temp) - 1, "%Y-%m-%d %H:%M", curdate);
1297
1298 cupsFilePuts(fp, "# Printer configuration file for " CUPS_SVERSION "\n");
1299 cupsFilePrintf(fp, "# Written by cupsd on %s\n", temp);
1300
1301 /*
1302 * Write each local printer known to the system...
1303 */
1304
1305 for (printer = (cupsd_printer_t *)cupsArrayFirst(Printers);
1306 printer;
1307 printer = (cupsd_printer_t *)cupsArrayNext(Printers))
1308 {
1309 /*
1310 * Skip remote destinations and printer classes...
1311 */
1312
1313 if ((printer->type & CUPS_PRINTER_REMOTE) ||
1314 (printer->type & CUPS_PRINTER_CLASS) ||
1315 (printer->type & CUPS_PRINTER_IMPLICIT))
1316 continue;
1317
1318 /*
1319 * Write printers as needed...
1320 */
1321
1322 if (printer == DefaultPrinter)
1323 cupsFilePrintf(fp, "<DefaultPrinter %s>\n", printer->name);
1324 else
1325 cupsFilePrintf(fp, "<Printer %s>\n", printer->name);
1326
1327 if (printer->info)
1328 cupsFilePrintf(fp, "Info %s\n", printer->info);
1329
1330 if (printer->location)
1331 cupsFilePrintf(fp, "Location %s\n", printer->location);
1332
1333 if (printer->device_uri)
1334 cupsFilePrintf(fp, "DeviceURI %s\n", printer->device_uri);
1335
1336 if (printer->port_monitor)
1337 cupsFilePrintf(fp, "PortMonitor %s\n", printer->port_monitor);
1338
1339 if (printer->state == IPP_PRINTER_STOPPED)
1340 {
1341 cupsFilePuts(fp, "State Stopped\n");
1342 cupsFilePrintf(fp, "StateMessage %s\n", printer->state_message);
1343 }
1344 else
1345 cupsFilePuts(fp, "State Idle\n");
1346
1347 cupsFilePrintf(fp, "StateTime %d\n", (int)printer->state_time);
1348
1349 if (printer->accepting)
1350 cupsFilePuts(fp, "Accepting Yes\n");
1351 else
1352 cupsFilePuts(fp, "Accepting No\n");
1353
1354 if (printer->shared)
1355 cupsFilePuts(fp, "Shared Yes\n");
1356 else
1357 cupsFilePuts(fp, "Shared No\n");
1358
1359 cupsFilePrintf(fp, "JobSheets %s %s\n", printer->job_sheets[0],
1360 printer->job_sheets[1]);
1361
1362 cupsFilePrintf(fp, "QuotaPeriod %d\n", printer->quota_period);
1363 cupsFilePrintf(fp, "PageLimit %d\n", printer->page_limit);
1364 cupsFilePrintf(fp, "KLimit %d\n", printer->k_limit);
1365
1366 for (i = 0; i < printer->num_users; i ++)
1367 cupsFilePrintf(fp, "%sUser %s\n", printer->deny_users ? "Deny" : "Allow",
1368 printer->users[i]);
1369
1370 if (printer->op_policy)
1371 cupsFilePrintf(fp, "OpPolicy %s\n", printer->op_policy);
1372 if (printer->error_policy)
1373 cupsFilePrintf(fp, "ErrorPolicy %s\n", printer->error_policy);
1374
1375 cupsFilePuts(fp, "</Printer>\n");
1376
1377 #ifdef __sgi
1378 /*
1379 * Make IRIX desktop & printer status happy
1380 */
1381
1382 write_irix_state(printer);
1383 #endif /* __sgi */
1384 }
1385
1386 cupsFileClose(fp);
1387 }
1388
1389
1390 /*
1391 * 'cupsdSetPrinterAttrs()' - Set printer attributes based upon the PPD file.
1392 */
1393
1394 void
1395 cupsdSetPrinterAttrs(cupsd_printer_t *p)/* I - Printer to setup */
1396 {
1397 char uri[HTTP_MAX_URI];
1398 /* URI for printer */
1399 char resource[HTTP_MAX_URI];
1400 /* Resource portion of URI */
1401 int i; /* Looping var */
1402 char filename[1024]; /* Name of PPD file */
1403 int num_media; /* Number of media options */
1404 cupsd_location_t *auth; /* Pointer to authentication element */
1405 const char *auth_supported;/* Authentication supported */
1406 cups_ptype_t cupsd_printer_type;
1407 /* Printer type data */
1408 ppd_file_t *ppd; /* PPD file data */
1409 ppd_option_t *input_slot, /* InputSlot options */
1410 *media_type, /* MediaType options */
1411 *page_size, /* PageSize options */
1412 *output_bin, /* OutputBin options */
1413 *media_quality; /* EFMediaQualityMode options */
1414 ppd_attr_t *ppdattr; /* PPD attribute */
1415 ipp_attribute_t *attr; /* Attribute data */
1416 ipp_value_t *val; /* Attribute value */
1417 int num_finishings; /* Number of finishings */
1418 ipp_finish_t finishings[5]; /* finishings-supported values */
1419 static const char * const sides[3] = /* sides-supported values */
1420 {
1421 "one",
1422 "two-long-edge",
1423 "two-short-edge"
1424 };
1425
1426
1427 DEBUG_printf(("cupsdSetPrinterAttrs: entering name = %s, type = %x\n", p->name,
1428 p->type));
1429
1430 /*
1431 * Make sure that we have the common attributes defined...
1432 */
1433
1434 if (!CommonData)
1435 cupsdCreateCommonData();
1436
1437 /*
1438 * Clear out old filters, if any...
1439 */
1440
1441 cupsdDeletePrinterFilters(p);
1442
1443 /*
1444 * Figure out the authentication that is required for the printer.
1445 */
1446
1447 auth_supported = "requesting-user-name";
1448 if (!(p->type & CUPS_PRINTER_REMOTE))
1449 {
1450 if (p->type & CUPS_PRINTER_CLASS)
1451 snprintf(resource, sizeof(resource), "/classes/%s", p->name);
1452 else
1453 snprintf(resource, sizeof(resource), "/printers/%s", p->name);
1454
1455 if ((auth = cupsdFindBest(resource, HTTP_POST)) == NULL)
1456 auth = cupsdFindPolicyOp(p->op_policy_ptr, IPP_PRINT_JOB);
1457
1458 if (auth)
1459 {
1460 if (auth->type == AUTH_BASIC || auth->type == AUTH_BASICDIGEST)
1461 auth_supported = "basic";
1462 else if (auth->type == AUTH_DIGEST)
1463 auth_supported = "digest";
1464
1465 if (auth->type != AUTH_NONE)
1466 p->type |= CUPS_PRINTER_AUTHENTICATED;
1467 else
1468 p->type &= ~CUPS_PRINTER_AUTHENTICATED;
1469 }
1470 else
1471 p->type &= ~CUPS_PRINTER_AUTHENTICATED;
1472 }
1473
1474 /*
1475 * Create the required IPP attributes for a printer...
1476 */
1477
1478 if (p->attrs)
1479 ippDelete(p->attrs);
1480
1481 p->attrs = ippNew();
1482
1483 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
1484 "uri-authentication-supported", NULL, auth_supported);
1485 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
1486 "uri-security-supported", NULL, "none");
1487 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME, "printer-name", NULL,
1488 p->name);
1489 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-location",
1490 NULL, p->location ? p->location : "");
1491 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-info",
1492 NULL, p->info ? p->info : "");
1493 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI, "printer-more-info",
1494 NULL, p->uri);
1495
1496 if (p->num_users)
1497 {
1498 if (p->deny_users)
1499 ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
1500 "requesting-user-name-denied", p->num_users, NULL,
1501 p->users);
1502 else
1503 ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
1504 "requesting-user-name-allowed", p->num_users, NULL,
1505 p->users);
1506 }
1507
1508 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
1509 "job-quota-period", p->quota_period);
1510 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
1511 "job-k-limit", p->k_limit);
1512 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
1513 "job-page-limit", p->page_limit);
1514
1515 if (NumBanners > 0 && !(p->type & CUPS_PRINTER_REMOTE))
1516 {
1517 /*
1518 * Setup the job-sheets-default attribute...
1519 */
1520
1521 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
1522 "job-sheets-default", 2, NULL, NULL);
1523
1524 if (attr != NULL)
1525 {
1526 attr->values[0].string.text = strdup(Classification ?
1527 Classification : p->job_sheets[0]);
1528 attr->values[1].string.text = strdup(Classification ?
1529 Classification : p->job_sheets[1]);
1530 }
1531 }
1532
1533 cupsd_printer_type = p->type;
1534
1535 p->raw = 0;
1536
1537 if (p->type & CUPS_PRINTER_REMOTE)
1538 {
1539 /*
1540 * Tell the client this is a remote printer of some type...
1541 */
1542
1543 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI,
1544 "printer-uri-supported", NULL, p->uri);
1545
1546 if (p->make_model)
1547 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
1548 "printer-make-and-model", NULL, p->make_model);
1549
1550 p->raw = 1;
1551 }
1552 else
1553 {
1554 /*
1555 * Assign additional attributes depending on whether this is a printer
1556 * or class...
1557 */
1558
1559 p->type &= ~CUPS_PRINTER_OPTIONS;
1560
1561 if (p->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT))
1562 {
1563 p->raw = 1;
1564
1565 /*
1566 * Add class-specific attributes...
1567 */
1568
1569 if ((p->type & CUPS_PRINTER_IMPLICIT) && p->num_printers > 0 &&
1570 p->printers[0]->make_model)
1571 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
1572 "printer-make-and-model", NULL, p->printers[0]->make_model);
1573 else
1574 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
1575 "printer-make-and-model", NULL, "Local Printer Class");
1576
1577 if (p->num_printers > 0)
1578 {
1579 /*
1580 * Add a list of member URIs and names...
1581 */
1582
1583 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI,
1584 "member-uris", p->num_printers, NULL, NULL);
1585 p->type |= CUPS_PRINTER_OPTIONS;
1586
1587 for (i = 0; i < p->num_printers; i ++)
1588 {
1589 if (attr != NULL)
1590 attr->values[i].string.text = strdup(p->printers[i]->uri);
1591
1592 p->type &= ~CUPS_PRINTER_OPTIONS | p->printers[i]->type;
1593 }
1594
1595 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_NAME,
1596 "member-names", p->num_printers, NULL, NULL);
1597
1598 if (attr != NULL)
1599 {
1600 for (i = 0; i < p->num_printers; i ++)
1601 attr->values[i].string.text = strdup(p->printers[i]->name);
1602 }
1603 }
1604 }
1605 else
1606 {
1607 /*
1608 * Add printer-specific attributes... Start by sanitizing the device
1609 * URI so it doesn't have a username or password in it...
1610 */
1611
1612 if (!p->device_uri)
1613 strcpy(uri, "file:/dev/null");
1614 else if (strstr(p->device_uri, "://") != NULL)
1615 {
1616 /*
1617 * http://..., ipp://..., etc.
1618 */
1619
1620 cupsdSanitizeURI(p->device_uri, uri, sizeof(uri));
1621 }
1622 else
1623 {
1624 /*
1625 * file:..., serial:..., etc.
1626 */
1627
1628 strlcpy(uri, p->device_uri, sizeof(uri));
1629 }
1630
1631 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI, "device-uri", NULL,
1632 uri);
1633
1634 /*
1635 * Assign additional attributes from the PPD file (if any)...
1636 */
1637
1638 p->type |= CUPS_PRINTER_BW;
1639 finishings[0] = IPP_FINISHINGS_NONE;
1640 num_finishings = 1;
1641
1642 snprintf(filename, sizeof(filename), "%s/ppd/%s.ppd", ServerRoot,
1643 p->name);
1644
1645 if ((ppd = ppdOpenFile(filename)) != NULL)
1646 {
1647 /*
1648 * Add make/model and other various attributes...
1649 */
1650
1651 if (ppd->color_device)
1652 p->type |= CUPS_PRINTER_COLOR;
1653 if (ppd->variable_sizes)
1654 p->type |= CUPS_PRINTER_VARIABLE;
1655 if (!ppd->manual_copies)
1656 p->type |= CUPS_PRINTER_COPIES;
1657 if ((ppdattr = ppdFindAttr(ppd, "cupsFax", NULL)) != NULL)
1658 if (ppdattr->value && !strcasecmp(ppdattr->value, "true"))
1659 p->type |= CUPS_PRINTER_FAX;
1660
1661 ippAddBoolean(p->attrs, IPP_TAG_PRINTER, "color-supported",
1662 ppd->color_device);
1663 if (ppd->throughput)
1664 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_INTEGER,
1665 "pages-per-minute", ppd->throughput);
1666
1667 if (ppd->nickname)
1668 cupsdSetString(&p->make_model, ppd->nickname);
1669 else if (ppd->modelname)
1670 cupsdSetString(&p->make_model, ppd->modelname);
1671 else
1672 cupsdSetString(&p->make_model, "Bad PPD File");
1673
1674 if (p->make_model)
1675 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
1676 "printer-make-and-model", NULL, p->make_model);
1677
1678 /*
1679 * Add media options from the PPD file...
1680 */
1681
1682 if ((input_slot = ppdFindOption(ppd, "InputSlot")) != NULL)
1683 num_media = input_slot->num_choices;
1684 else
1685 num_media = 0;
1686
1687 if ((media_type = ppdFindOption(ppd, "MediaType")) != NULL)
1688 num_media += media_type->num_choices;
1689
1690 if ((page_size = ppdFindOption(ppd, "PageSize")) != NULL)
1691 num_media += page_size->num_choices;
1692
1693 if ((media_quality = ppdFindOption(ppd, "EFMediaQualityMode")) != NULL)
1694 num_media += media_quality->num_choices;
1695
1696 if (num_media == 0)
1697 {
1698 cupsdLogMessage(CUPSD_LOG_CRIT, "cupsdSetPrinterAttrs: The PPD file for printer %s "
1699 "contains no media options and is therefore "
1700 "invalid!", p->name);
1701 }
1702 else
1703 {
1704 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
1705 "media-supported", num_media, NULL, NULL);
1706 if (attr != NULL)
1707 {
1708 val = attr->values;
1709
1710 if (input_slot != NULL)
1711 for (i = 0; i < input_slot->num_choices; i ++, val ++)
1712 val->string.text = strdup(input_slot->choices[i].choice);
1713
1714 if (media_type != NULL)
1715 for (i = 0; i < media_type->num_choices; i ++, val ++)
1716 val->string.text = strdup(media_type->choices[i].choice);
1717
1718 if (media_quality != NULL)
1719 for (i = 0; i < media_quality->num_choices; i ++, val ++)
1720 val->string.text = strdup(media_quality->choices[i].choice);
1721
1722 if (page_size != NULL)
1723 {
1724 for (i = 0; i < page_size->num_choices; i ++, val ++)
1725 val->string.text = strdup(page_size->choices[i].choice);
1726
1727 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD, "media-default",
1728 NULL, page_size->defchoice);
1729 }
1730 else if (input_slot != NULL)
1731 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD, "media-default",
1732 NULL, input_slot->defchoice);
1733 else if (media_type != NULL)
1734 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD, "media-default",
1735 NULL, media_type->defchoice);
1736 else if (media_quality != NULL)
1737 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD, "media-default",
1738 NULL, media_quality->defchoice);
1739 else
1740 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD, "media-default",
1741 NULL, "none");
1742 }
1743 }
1744
1745 /*
1746 * Output bin...
1747 */
1748
1749 if ((output_bin = ppdFindOption(ppd, "OutputBin")) != NULL)
1750 {
1751 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
1752 "output-bin-supported", output_bin->num_choices,
1753 NULL, NULL);
1754
1755 if (attr != NULL)
1756 {
1757 for (i = 0, val = attr->values;
1758 i < output_bin->num_choices;
1759 i ++, val ++)
1760 val->string.text = strdup(output_bin->choices[i].choice);
1761 }
1762 }
1763
1764 /*
1765 * Duplexing, etc...
1766 */
1767
1768 if (ppdFindOption(ppd, "Duplex") != NULL)
1769 {
1770 p->type |= CUPS_PRINTER_DUPLEX;
1771
1772 ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD, "sides-supported",
1773 3, NULL, sides);
1774 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD, "sides-default",
1775 NULL, "one");
1776 }
1777
1778 if (ppdFindOption(ppd, "Collate") != NULL)
1779 p->type |= CUPS_PRINTER_COLLATE;
1780
1781 if (ppdFindOption(ppd, "StapleLocation") != NULL)
1782 {
1783 p->type |= CUPS_PRINTER_STAPLE;
1784 finishings[num_finishings++] = IPP_FINISHINGS_STAPLE;
1785 }
1786
1787 if (ppdFindOption(ppd, "BindEdge") != NULL)
1788 {
1789 p->type |= CUPS_PRINTER_BIND;
1790 finishings[num_finishings++] = IPP_FINISHINGS_BIND;
1791 }
1792
1793 for (i = 0; i < ppd->num_sizes; i ++)
1794 if (ppd->sizes[i].length > 1728)
1795 p->type |= CUPS_PRINTER_LARGE;
1796 else if (ppd->sizes[i].length > 1008)
1797 p->type |= CUPS_PRINTER_MEDIUM;
1798 else
1799 p->type |= CUPS_PRINTER_SMALL;
1800
1801 /*
1802 * Add a filter from application/vnd.cups-raw to printer/name to
1803 * handle "raw" printing by users.
1804 */
1805
1806 cupsdAddPrinterFilter(p, "application/vnd.cups-raw 0 -");
1807
1808 /*
1809 * Add any filters in the PPD file...
1810 */
1811
1812 DEBUG_printf(("ppd->num_filters = %d\n", ppd->num_filters));
1813 for (i = 0; i < ppd->num_filters; i ++)
1814 {
1815 DEBUG_printf(("ppd->filters[%d] = \"%s\"\n", i, ppd->filters[i]));
1816 cupsdAddPrinterFilter(p, ppd->filters[i]);
1817 }
1818
1819 if (ppd->num_filters == 0)
1820 {
1821 /*
1822 * If there are no filters, add a PostScript printing filter.
1823 */
1824
1825 cupsdAddPrinterFilter(p, "application/vnd.cups-postscript 0 -");
1826 }
1827
1828 /*
1829 * Show current and available port monitors for this printer...
1830 */
1831
1832 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD, "port-monitor",
1833 NULL, p->port_monitor ? p->port_monitor : "none");
1834
1835
1836 for (i = 1, ppdattr = ppdFindAttr(ppd, "cupsPortMonitor", NULL);
1837 ppdattr;
1838 i ++, ppdattr = ppdFindNextAttr(ppd, "cupsPortMonitor", NULL));
1839
1840 if (ppd->protocols)
1841 {
1842 if (strstr(ppd->protocols, "TBCP"))
1843 i ++;
1844 else if (strstr(ppd->protocols, "BCP"))
1845 i ++;
1846 }
1847
1848 attr = ippAddStrings(p->attrs, IPP_TAG_PRINTER, IPP_TAG_KEYWORD,
1849 "port-monitor-supported", i, NULL, NULL);
1850
1851 attr->values[0].string.text = strdup("none");
1852
1853 for (i = 1, ppdattr = ppdFindAttr(ppd, "cupsPortMonitor", NULL);
1854 ppdattr;
1855 i ++, ppdattr = ppdFindNextAttr(ppd, "cupsPortMonitor", NULL))
1856 attr->values[i].string.text = strdup(ppdattr->value);
1857
1858 if (ppd->protocols)
1859 {
1860 if (strstr(ppd->protocols, "TBCP"))
1861 attr->values[i].string.text = strdup("tbcp");
1862 else if (strstr(ppd->protocols, "BCP"))
1863 attr->values[i].string.text = strdup("bcp");
1864 }
1865
1866 /*
1867 * Close the PPD and set the type...
1868 */
1869
1870 ppdClose(ppd);
1871
1872 cupsd_printer_type = p->type;
1873 }
1874 else if (!access(filename, 0))
1875 {
1876 int pline; /* PPD line number */
1877 ppd_status_t pstatus; /* PPD load status */
1878
1879
1880 pstatus = ppdLastError(&pline);
1881
1882 cupsdLogMessage(CUPSD_LOG_ERROR, "PPD file for %s cannot be loaded!", p->name);
1883
1884 if (pstatus <= PPD_ALLOC_ERROR)
1885 cupsdLogMessage(CUPSD_LOG_ERROR, "%s", strerror(errno));
1886 else
1887 cupsdLogMessage(CUPSD_LOG_ERROR, "%s on line %d.", ppdErrorString(pstatus),
1888 pline);
1889
1890 cupsdLogMessage(CUPSD_LOG_INFO, "Hint: Run \"cupstestppd %s\" and fix any errors.",
1891 filename);
1892
1893 /*
1894 * Add a filter from application/vnd.cups-raw to printer/name to
1895 * handle "raw" printing by users.
1896 */
1897
1898 cupsdAddPrinterFilter(p, "application/vnd.cups-raw 0 -");
1899
1900 /*
1901 * Add a PostScript filter, since this is still possibly PS printer.
1902 */
1903
1904 cupsdAddPrinterFilter(p, "application/vnd.cups-postscript 0 -");
1905 }
1906 else
1907 {
1908 /*
1909 * If we have an interface script, add a filter entry for it...
1910 */
1911
1912 snprintf(filename, sizeof(filename), "%s/interfaces/%s", ServerRoot,
1913 p->name);
1914 if (access(filename, X_OK) == 0)
1915 {
1916 /*
1917 * Yes, we have a System V style interface script; use it!
1918 */
1919
1920 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
1921 "printer-make-and-model", NULL, "Local System V Printer");
1922
1923 snprintf(filename, sizeof(filename), "*/* 0 %s/interfaces/%s",
1924 ServerRoot, p->name);
1925 cupsdAddPrinterFilter(p, filename);
1926 }
1927 else if (p->device_uri &&
1928 !strncmp(p->device_uri, "ipp://", 6) &&
1929 (strstr(p->device_uri, "/printers/") != NULL ||
1930 strstr(p->device_uri, "/classes/") != NULL))
1931 {
1932 /*
1933 * Tell the client this is really a hard-wired remote printer.
1934 */
1935
1936 cupsd_printer_type |= CUPS_PRINTER_REMOTE;
1937
1938 /*
1939 * Point the printer-uri-supported attribute to the
1940 * remote printer...
1941 */
1942
1943 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_URI,
1944 "printer-uri-supported", NULL, p->device_uri);
1945
1946 /*
1947 * Then set the make-and-model accordingly...
1948 */
1949
1950 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
1951 "printer-make-and-model", NULL, "Remote Printer");
1952
1953 /*
1954 * Print all files directly...
1955 */
1956
1957 p->raw = 1;
1958 }
1959 else
1960 {
1961 /*
1962 * Otherwise we have neither - treat this as a "dumb" printer
1963 * with no PPD file...
1964 */
1965
1966 ippAddString(p->attrs, IPP_TAG_PRINTER, IPP_TAG_TEXT,
1967 "printer-make-and-model", NULL, "Local Raw Printer");
1968
1969 p->raw = 1;
1970 }
1971 }
1972
1973 ippAddIntegers(p->attrs, IPP_TAG_PRINTER, IPP_TAG_ENUM,
1974 "finishings-supported", num_finishings, (int *)finishings);
1975 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_ENUM,
1976 "finishings-default", IPP_FINISHINGS_NONE);
1977 }
1978 }
1979
1980 /*
1981 * Add the CUPS-specific printer-type attribute...
1982 */
1983
1984 if (!p->shared)
1985 p->type |= CUPS_PRINTER_NOT_SHARED;
1986 else
1987 p->type &= ~CUPS_PRINTER_NOT_SHARED;
1988
1989 ippAddInteger(p->attrs, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-type",
1990 cupsd_printer_type);
1991
1992 DEBUG_printf(("cupsdSetPrinterAttrs: leaving name = %s, type = %x\n", p->name,
1993 p->type));
1994
1995 #ifdef __sgi
1996 /*
1997 * Write the IRIX printer config and status files...
1998 */
1999
2000 write_irix_config(p);
2001 write_irix_state(p);
2002 #endif /* __sgi */
2003 }
2004
2005
2006 /*
2007 * 'cupsdSetPrinterReasons()' - Set/update the reasons strings.
2008 */
2009
2010 void
2011 cupsdSetPrinterReasons(
2012 cupsd_printer_t *p, /* I - Printer */
2013 const char *s) /* I - Reasons strings */
2014 {
2015 int i; /* Looping var */
2016 const char *sptr; /* Pointer into reasons */
2017 char reason[255], /* Reason string */
2018 *rptr; /* Pointer into reason */
2019
2020
2021 if (s[0] == '-' || s[0] == '+')
2022 {
2023 /*
2024 * Add/remove reasons...
2025 */
2026
2027 sptr = s + 1;
2028 }
2029 else
2030 {
2031 /*
2032 * Replace reasons...
2033 */
2034
2035 sptr = s;
2036
2037 for (i = 0; i < p->num_reasons; i ++)
2038 free(p->reasons[i]);
2039
2040 p->num_reasons = 0;
2041 }
2042
2043 /*
2044 * Loop through all of the reasons...
2045 */
2046
2047 while (*sptr)
2048 {
2049 /*
2050 * Skip leading whitespace and commas...
2051 */
2052
2053 while (isspace(*sptr & 255) || *sptr == ',')
2054 sptr ++;
2055
2056 for (rptr = reason; *sptr && !isspace(*sptr & 255) && *sptr != ','; sptr ++)
2057 if (rptr < (reason + sizeof(reason) - 1))
2058 *rptr++ = *sptr;
2059
2060 if (rptr == reason)
2061 break;
2062
2063 *rptr = '\0';
2064
2065 if (s[0] == '-')
2066 {
2067 /*
2068 * Remove reason...
2069 */
2070
2071 for (i = 0; i < p->num_reasons; i ++)
2072 if (!strcasecmp(reason, p->reasons[i]))
2073 {
2074 /*
2075 * Found a match, so remove it...
2076 */
2077
2078 p->num_reasons --;
2079 free(p->reasons[i]);
2080
2081 if (i < p->num_reasons)
2082 memmove(p->reasons + i, p->reasons + i + 1,
2083 (p->num_reasons - i) * sizeof(char *));
2084
2085 i --;
2086 }
2087 }
2088 else if (p->num_reasons < (int)(sizeof(p->reasons) / sizeof(p->reasons[0])))
2089 {
2090 /*
2091 * Add reason...
2092 */
2093
2094 for (i = 0; i < p->num_reasons; i ++)
2095 if (!strcasecmp(reason, p->reasons[i]))
2096 break;
2097
2098 if (i >= p->num_reasons)
2099 {
2100 p->reasons[i] = strdup(reason);
2101 p->num_reasons ++;
2102 }
2103 }
2104 }
2105 }
2106
2107
2108 /*
2109 * 'cupsdSetPrinterState()' - Update the current state of a printer.
2110 */
2111
2112 void
2113 cupsdSetPrinterState(
2114 cupsd_printer_t *p, /* I - Printer to change */
2115 ipp_pstate_t s, /* I - New state */
2116 int update) /* I - Update printers.conf? */
2117 {
2118 ipp_pstate_t old_state; /* Old printer state */
2119
2120
2121 /*
2122 * Can't set status of remote printers...
2123 */
2124
2125 if (p->type & CUPS_PRINTER_REMOTE)
2126 return;
2127
2128 /*
2129 * Set the new state...
2130 */
2131
2132 old_state = p->state;
2133 p->state = s;
2134
2135 if (old_state != s)
2136 {
2137 /*
2138 * Let the browse code know this needs to be updated...
2139 */
2140
2141 BrowseNext = p;
2142 p->state_time = time(NULL);
2143 p->browse_time = 0;
2144
2145 #ifdef __sgi
2146 write_irix_state(p);
2147 #endif /* __sgi */
2148 }
2149
2150 cupsdAddPrinterHistory(p);
2151
2152 /*
2153 * Save the printer configuration if a printer goes from idle or processing
2154 * to stopped (or visa-versa)...
2155 */
2156
2157 if ((old_state == IPP_PRINTER_STOPPED) != (s == IPP_PRINTER_STOPPED) &&
2158 update)
2159 {
2160 if (p->type & CUPS_PRINTER_CLASS)
2161 cupsdSaveAllClasses();
2162 else
2163 cupsdSaveAllPrinters();
2164 }
2165 }
2166
2167
2168 /*
2169 * 'cupsdStopPrinter()' - Stop a printer from printing any jobs...
2170 */
2171
2172 void
2173 cupsdStopPrinter(cupsd_printer_t *p, /* I - Printer to stop */
2174 int update)/* I - Update printers.conf? */
2175 {
2176 cupsd_job_t *job; /* Active print job */
2177
2178
2179 /*
2180 * Set the printer state...
2181 */
2182
2183 cupsdSetPrinterState(p, IPP_PRINTER_STOPPED, update);
2184
2185 /*
2186 * See if we have a job printing on this printer...
2187 */
2188
2189 if (p->job)
2190 {
2191 /*
2192 * Get pointer to job...
2193 */
2194
2195 job = (cupsd_job_t *)p->job;
2196
2197 /*
2198 * Stop it...
2199 */
2200
2201 cupsdStopJob(job, 0);
2202
2203 /*
2204 * Reset the state to pending...
2205 */
2206
2207 job->state->values[0].integer = IPP_JOB_PENDING;
2208
2209 cupsdSaveJob(job);
2210 }
2211 }
2212
2213
2214 /*
2215 * 'cupsdUpdatePrinters()' - Update printers after a partial reload.
2216 */
2217
2218 void
2219 cupsdUpdatePrinters(void)
2220 {
2221 cupsd_printer_t *p; /* Current printer */
2222
2223
2224 /*
2225 * Loop through the printers and recreate the printer attributes
2226 * for any local printers since the policy and/or access control
2227 * stuff may have changed. Also, if browsing is disabled, remove
2228 * any remote printers...
2229 */
2230
2231 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
2232 p;
2233 p = (cupsd_printer_t *)cupsArrayNext(Printers))
2234 {
2235 if (!Browsing && (p->type & (CUPS_PRINTER_IMPLICIT | CUPS_PRINTER_REMOTE)))
2236 {
2237 if (p->type & CUPS_PRINTER_IMPLICIT)
2238 cupsArrayRemove(ImplicitPrinters, p);
2239
2240 cupsArraySave(Printers);
2241 cupsdDeletePrinter(p, 0);
2242 cupsArrayRestore(Printers);
2243 continue;
2244 }
2245 else if (!(p->type & CUPS_PRINTER_REMOTE))
2246 cupsdSetPrinterAttrs(p);
2247
2248 /*
2249 * Update the operation policy pointer...
2250 */
2251
2252 if ((p->op_policy_ptr = cupsdFindPolicy(p->op_policy)) == NULL)
2253 p->op_policy_ptr = DefaultPolicyPtr;
2254 }
2255 }
2256
2257
2258 /*
2259 * 'cupsdValidateDest()' - Validate a printer/class destination.
2260 */
2261
2262 const char * /* O - Printer or class name */
2263 cupsdValidateDest(
2264 const char *hostname, /* I - Host name */
2265 const char *resource, /* I - Resource name */
2266 cups_ptype_t *dtype, /* O - Type (printer or class) */
2267 cupsd_printer_t **printer) /* O - Printer pointer */
2268 {
2269 cupsd_printer_t *p; /* Current printer */
2270 char localname[1024],/* Localized hostname */
2271 *lptr, /* Pointer into localized hostname */
2272 *sptr; /* Pointer into server name */
2273
2274
2275 DEBUG_printf(("cupsdValidateDest(\"%s\", \"%s\", %p, %p)\n", hostname, resource,
2276 dtype, printer));
2277
2278 /*
2279 * Initialize return values...
2280 */
2281
2282 if (printer)
2283 *printer = NULL;
2284
2285 *dtype = (cups_ptype_t)0;
2286
2287 /*
2288 * See if the resource is a class or printer...
2289 */
2290
2291 if (!strncmp(resource, "/classes/", 9))
2292 {
2293 /*
2294 * Class...
2295 */
2296
2297 resource += 9;
2298 }
2299 else if (!strncmp(resource, "/printers/", 10))
2300 {
2301 /*
2302 * Printer...
2303 */
2304
2305 resource += 10;
2306 }
2307 else
2308 {
2309 /*
2310 * Bad resource name...
2311 */
2312
2313 return (NULL);
2314 }
2315
2316 /*
2317 * See if the printer or class name exists...
2318 */
2319
2320 p = cupsdFindDest(resource);
2321
2322 if (p == NULL && strchr(resource, '@') == NULL)
2323 return (NULL);
2324 else if (p != NULL)
2325 {
2326 if (printer)
2327 *printer = p;
2328
2329 *dtype = p->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT |
2330 CUPS_PRINTER_REMOTE);
2331 return (p->name);
2332 }
2333
2334 /*
2335 * Change localhost to the server name...
2336 */
2337
2338 if (!strcasecmp(hostname, "localhost"))
2339 hostname = ServerName;
2340
2341 strlcpy(localname, hostname, sizeof(localname));
2342
2343 if (!strcasecmp(hostname, ServerName))
2344 {
2345 /*
2346 * Localize the hostname...
2347 */
2348
2349 lptr = strchr(localname, '.');
2350 sptr = strchr(ServerName, '.');
2351
2352 if (sptr != NULL && lptr != NULL)
2353 {
2354 /*
2355 * Strip the common domain name components...
2356 */
2357
2358 while (lptr != NULL)
2359 {
2360 if (!strcasecmp(lptr, sptr))
2361 {
2362 *lptr = '\0';
2363 break;
2364 }
2365 else
2366 lptr = strchr(lptr + 1, '.');
2367 }
2368 }
2369 }
2370
2371 DEBUG_printf(("localized hostname is \"%s\"...\n", localname));
2372
2373 /*
2374 * Find a matching printer or class...
2375 */
2376
2377 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
2378 p;
2379 p = (cupsd_printer_t *)cupsArrayNext(Printers))
2380 if (!strcasecmp(p->hostname, localname) &&
2381 !strcasecmp(p->name, resource))
2382 {
2383 if (printer)
2384 *printer = p;
2385
2386 *dtype = p->type & (CUPS_PRINTER_CLASS | CUPS_PRINTER_IMPLICIT |
2387 CUPS_PRINTER_REMOTE);
2388 return (p->name);
2389 }
2390
2391 return (NULL);
2392 }
2393
2394
2395 /*
2396 * 'cupsdWritePrintcap()' - Write a pseudo-printcap file for older applications
2397 * that need it...
2398 */
2399
2400 void
2401 cupsdWritePrintcap(void)
2402 {
2403 cups_file_t *fp; /* printcap file */
2404 cupsd_printer_t *p; /* Current printer */
2405
2406
2407 #ifdef __sgi
2408 /*
2409 * Update the IRIX printer state for the default printer; if
2410 * no printers remain, then the default printer file will be
2411 * removed...
2412 */
2413
2414 write_irix_state(DefaultPrinter);
2415 #endif /* __sgi */
2416
2417 /*
2418 * See if we have a printcap file; if not, don't bother writing it.
2419 */
2420
2421 if (!Printcap || !*Printcap)
2422 return;
2423
2424 /*
2425 * Open the printcap file...
2426 */
2427
2428 if ((fp = cupsFileOpen(Printcap, "w")) == NULL)
2429 return;
2430
2431 /*
2432 * Put a comment header at the top so that users will know where the
2433 * data has come from...
2434 */
2435
2436 cupsFilePuts(fp, "# This file was automatically generated by cupsd(8) from the\n");
2437 cupsFilePrintf(fp, "# %s/printers.conf file. All changes to this file\n",
2438 ServerRoot);
2439 cupsFilePuts(fp, "# will be lost.\n");
2440
2441 if (Printers)
2442 {
2443 /*
2444 * Write a new printcap with the current list of printers.
2445 */
2446
2447 switch (PrintcapFormat)
2448 {
2449 case PRINTCAP_BSD:
2450 /*
2451 * Each printer is put in the file as:
2452 *
2453 * Printer1:
2454 * Printer2:
2455 * Printer3:
2456 * ...
2457 * PrinterN:
2458 */
2459
2460 if (DefaultPrinter)
2461 cupsFilePrintf(fp, "%s|%s:rm=%s:rp=%s:\n", DefaultPrinter->name,
2462 DefaultPrinter->info, ServerName, DefaultPrinter->name);
2463
2464 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
2465 p;
2466 p = (cupsd_printer_t *)cupsArrayNext(Printers))
2467 if (p != DefaultPrinter)
2468 cupsFilePrintf(fp, "%s|%s:rm=%s:rp=%s:\n", p->name, p->info,
2469 ServerName, p->name);
2470 break;
2471
2472 case PRINTCAP_SOLARIS:
2473 /*
2474 * Each printer is put in the file as:
2475 *
2476 * _all:all=Printer1,Printer2,Printer3,...,PrinterN
2477 * _default:use=DefaultPrinter
2478 * Printer1:\
2479 * :bsdaddr=ServerName,Printer1:\
2480 * :description=Description:
2481 * Printer2:
2482 * :bsdaddr=ServerName,Printer2:\
2483 * :description=Description:
2484 * Printer3:
2485 * :bsdaddr=ServerName,Printer3:\
2486 * :description=Description:
2487 * ...
2488 * PrinterN:
2489 * :bsdaddr=ServerName,PrinterN:\
2490 * :description=Description:
2491 */
2492
2493 cupsFilePuts(fp, "_all:all=");
2494 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
2495 p;
2496 p = (cupsd_printer_t *)cupsArrayCurrent(Printers))
2497 cupsFilePrintf(fp, "%s%c", p->name,
2498 cupsArrayNext(Printers) ? ',' : '\n');
2499
2500 if (DefaultPrinter)
2501 cupsFilePrintf(fp, "_default:use=%s\n", DefaultPrinter->name);
2502
2503 for (p = (cupsd_printer_t *)cupsArrayFirst(Printers);
2504 p;
2505 p = (cupsd_printer_t *)cupsArrayNext(Printers))
2506 cupsFilePrintf(fp, "%s:\\\n"
2507 "\t:bsdaddr=%s,%s:\\\n"
2508 "\t:description=%s:\n",
2509 p->name, ServerName, p->name, p->info ? p->info : "");
2510 break;
2511 }
2512 }
2513
2514 /*
2515 * Close the file...
2516 */
2517
2518 cupsFileClose(fp);
2519 }
2520
2521
2522 /*
2523 * 'cupsdSanitizeURI()' - Sanitize a device URI...
2524 */
2525
2526 char * /* O - New device URI */
2527 cupsdSanitizeURI(const char *uri, /* I - Original device URI */
2528 char *buffer, /* O - New device URI */
2529 int buflen) /* I - Size of new device URI buffer */
2530 {
2531 char *start, /* Start of data after scheme */
2532 *slash, /* First slash after scheme:// */
2533 *ptr; /* Pointer into user@host:port part */
2534
2535
2536 /*
2537 * Range check input...
2538 */
2539
2540 if (!uri || !buffer || buflen < 2)
2541 return (NULL);
2542
2543 /*
2544 * Copy the device URI to the new buffer...
2545 */
2546
2547 strlcpy(buffer, uri, buflen);
2548
2549 /*
2550 * Find the end of the scheme:// part...
2551 */
2552
2553 if ((ptr = strchr(buffer, ':')) == NULL)
2554 return (buffer); /* No scheme: part... */
2555
2556 for (start = ptr + 1; *start; start ++)
2557 if (*start != '/')
2558 break;
2559
2560 /*
2561 * Find the next slash (/) in the URI...
2562 */
2563
2564 if ((slash = strchr(start, '/')) == NULL)
2565 slash = start + strlen(start); /* No slash, point to the end */
2566
2567 /*
2568 * Check for an @ sign before the slash...
2569 */
2570
2571 if ((ptr = strchr(start, '@')) != NULL && ptr < slash)
2572 {
2573 /*
2574 * Found an @ sign and it is before the resource part, so we have
2575 * an authentication string. Copy the remaining URI over the
2576 * authentication string...
2577 */
2578
2579 _cups_strcpy(start, ptr + 1);
2580 }
2581
2582 /*
2583 * Return the new device URI...
2584 */
2585
2586 return (buffer);
2587 }
2588
2589
2590 /*
2591 * 'compare_printers()' - Compare two printers.
2592 */
2593
2594 static int /* O - Result of comparison */
2595 compare_printers(void *first, /* I - First printer */
2596 void *second, /* I - Second printer */
2597 void *data) /* I - App data (not used) */
2598 {
2599 return (strcasecmp(((cupsd_printer_t *)first)->name,
2600 ((cupsd_printer_t *)second)->name));
2601 }
2602
2603
2604 #ifdef __sgi
2605 /*
2606 * 'write_irix_config()' - Update the config files used by the IRIX
2607 * desktop tools.
2608 */
2609
2610 static void
2611 write_irix_config(cupsd_printer_t *p) /* I - Printer to update */
2612 {
2613 char filename[1024]; /* Interface script filename */
2614 cups_file_t *fp; /* Interface script file */
2615 int tag; /* Status tag value */
2616
2617
2618
2619 /*
2620 * Add dummy interface and GUI scripts to fool SGI's "challenged" printing
2621 * tools. First the interface script that tells the tools what kind of
2622 * printer we have...
2623 */
2624
2625 snprintf(filename, sizeof(filename), "/var/spool/lp/interface/%s", p->name);
2626
2627 if (p->type & CUPS_PRINTER_CLASS)
2628 unlink(filename);
2629 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
2630 {
2631 cupsFilePuts(fp, "#!/bin/sh\n");
2632
2633 if ((attr = ippFindAttribute(p->attrs, "printer-make-and-model",
2634 IPP_TAG_TEXT)) != NULL)
2635 cupsFilePrintf(fp, "NAME=\"%s\"\n", attr->values[0].string.text);
2636 else if (p->type & CUPS_PRINTER_CLASS)
2637 cupsFilePuts(fp, "NAME=\"Printer Class\"\n");
2638 else
2639 cupsFilePuts(fp, "NAME=\"Remote Destination\"\n");
2640
2641 if (p->type & CUPS_PRINTER_COLOR)
2642 cupsFilePuts(fp, "TYPE=ColorPostScript\n");
2643 else
2644 cupsFilePuts(fp, "TYPE=MonoPostScript\n");
2645
2646 cupsFilePrintf(fp, "HOSTNAME=%s\n", ServerName);
2647 cupsFilePrintf(fp, "HOSTPRINTER=%s\n", p->name);
2648
2649 cupsFileClose(fp);
2650
2651 chmod(filename, 0755);
2652 chown(filename, User, Group);
2653 }
2654
2655 /*
2656 * Then the member file that tells which device file the queue is connected
2657 * to... Networked printers use "/dev/null" in this file, so that's what
2658 * we use (the actual device URI can confuse some apps...)
2659 */
2660
2661 snprintf(filename, sizeof(filename), "/var/spool/lp/member/%s", p->name);
2662
2663 if (p->type & CUPS_PRINTER_CLASS)
2664 unlink(filename);
2665 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
2666 {
2667 cupsFilePuts(fp, "/dev/null\n");
2668
2669 cupsFileClose(fp);
2670
2671 chmod(filename, 0644);
2672 chown(filename, User, Group);
2673 }
2674
2675 /*
2676 * The gui_interface file is a script or program that launches a GUI
2677 * option panel for the printer, using options specified on the
2678 * command-line in the third argument. The option panel must send
2679 * any printing options to stdout on a single line when the user
2680 * accepts them, or nothing if the user cancels the dialog.
2681 *
2682 * The default options panel program is /usr/bin/glpoptions, from
2683 * the ESP Print Pro software. You can select another using the
2684 * PrintcapGUI option.
2685 */
2686
2687 snprintf(filename, sizeof(filename), "/var/spool/lp/gui_interface/ELF/%s.gui", p->name);
2688
2689 if (p->type & CUPS_PRINTER_CLASS)
2690 unlink(filename);
2691 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
2692 {
2693 cupsFilePuts(fp, "#!/bin/sh\n");
2694 cupsFilePrintf(fp, "%s -d %s -o \"$3\"\n", PrintcapGUI, p->name);
2695
2696 cupsFileClose(fp);
2697
2698 chmod(filename, 0755);
2699 chown(filename, User, Group);
2700 }
2701
2702 /*
2703 * The POD config file is needed by the printstatus command to show
2704 * the printer location and device.
2705 */
2706
2707 snprintf(filename, sizeof(filename), "/var/spool/lp/pod/%s.config", p->name);
2708
2709 if (p->type & CUPS_PRINTER_CLASS)
2710 unlink(filename);
2711 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
2712 {
2713 cupsFilePrintf(fp, "Printer Class | %s\n",
2714 (p->type & CUPS_PRINTER_COLOR) ? "ColorPostScript" : "MonoPostScript");
2715 cupsFilePrintf(fp, "Printer Model | %s\n", p->make_model ? p->make_model : "");
2716 cupsFilePrintf(fp, "Location Code | %s\n", p->location ? p->location : "");
2717 cupsFilePrintf(fp, "Physical Location | %s\n", p->info ? p->info : "");
2718 cupsFilePrintf(fp, "Port Path | %s\n", p->device_uri ? p->device_uri : "");
2719 cupsFilePrintf(fp, "Config Path | /var/spool/lp/pod/%s.config\n", p->name);
2720 cupsFilePrintf(fp, "Active Status Path | /var/spool/lp/pod/%s.status\n", p->name);
2721 cupsFilePuts(fp, "Status Update Wait | 10 seconds\n");
2722
2723 cupsFileClose(fp);
2724
2725 chmod(filename, 0664);
2726 chown(filename, User, Group);
2727 }
2728 }
2729
2730
2731 /*
2732 * 'write_irix_state()' - Update the status files used by IRIX printing
2733 * desktop tools.
2734 */
2735
2736 static void
2737 write_irix_state(cupsd_printer_t *p) /* I - Printer to update */
2738 {
2739 char filename[1024]; /* Interface script filename */
2740 cups_file_t *fp; /* Interface script file */
2741 int tag; /* Status tag value */
2742
2743
2744 if (p)
2745 {
2746 /*
2747 * The POD status file is needed for the printstatus window to
2748 * provide the current status of the printer.
2749 */
2750
2751 snprintf(filename, sizeof(filename), "/var/spool/lp/pod/%s.status", p->name);
2752
2753 if (p->type & CUPS_PRINTER_CLASS)
2754 unlink(filename);
2755 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
2756 {
2757 cupsFilePrintf(fp, "Operational Status | %s\n",
2758 (p->state == IPP_PRINTER_IDLE) ? "Idle" :
2759 (p->state == IPP_PRINTER_PROCESSING) ? "Busy" :
2760 "Faulted");
2761 cupsFilePrintf(fp, "Information | 01 00 00 | %s\n", CUPS_SVERSION);
2762 cupsFilePrintf(fp, "Information | 02 00 00 | Device URI: %s\n",
2763 p->device_uri ? p->device_uri : "");
2764 cupsFilePrintf(fp, "Information | 03 00 00 | %s jobs\n",
2765 p->accepting ? "Accepting" : "Not accepting");
2766 cupsFilePrintf(fp, "Information | 04 00 00 | %s\n", p->state_message);
2767
2768 cupsFileClose(fp);
2769
2770 chmod(filename, 0664);
2771 chown(filename, User, Group);
2772 }
2773
2774 /*
2775 * The activeicons file is needed to provide desktop icons for printers:
2776 *
2777 * [ quoted from /usr/lib/print/tagit ]
2778 *
2779 * --- Type of printer tags (base values)
2780 *
2781 * Dumb=66048 # 0x10200
2782 * DumbColor=66080 # 0x10220
2783 * Raster=66112 # 0x10240
2784 * ColorRaster=66144 # 0x10260
2785 * Plotter=66176 # 0x10280
2786 * PostScript=66208 # 0x102A0
2787 * ColorPostScript=66240 # 0x102C0
2788 * MonoPostScript=66272 # 0x102E0
2789 *
2790 * --- Printer state modifiers for local printers
2791 *
2792 * Idle=0 # 0x0
2793 * Busy=1 # 0x1
2794 * Faulted=2 # 0x2
2795 * Unknown=3 # 0x3 (Faulted due to unknown reason)
2796 *
2797 * --- Printer state modifiers for network printers
2798 *
2799 * NetIdle=8 # 0x8
2800 * NetBusy=9 # 0x9
2801 * NetFaulted=10 # 0xA
2802 * NetUnknown=11 # 0xB (Faulted due to unknown reason)
2803 */
2804
2805 snprintf(filename, sizeof(filename), "/var/spool/lp/activeicons/%s", p->name);
2806
2807 if (p->type & CUPS_PRINTER_CLASS)
2808 unlink(filename);
2809 else if ((fp = cupsFileOpen(filename, "w")) != NULL)
2810 {
2811 if (p->type & CUPS_PRINTER_COLOR)
2812 tag = 66240;
2813 else
2814 tag = 66272;
2815
2816 if (p->type & CUPS_PRINTER_REMOTE)
2817 tag |= 8;
2818
2819 if (p->state == IPP_PRINTER_PROCESSING)
2820 tag |= 1;
2821
2822 else if (p->state == IPP_PRINTER_STOPPED)
2823 tag |= 2;
2824
2825 cupsFilePuts(fp, "#!/bin/sh\n");
2826 cupsFilePrintf(fp, "#Tag %d\n", tag);
2827
2828 cupsFileClose(fp);
2829
2830 chmod(filename, 0755);
2831 chown(filename, User, Group);
2832 }
2833 }
2834
2835 /*
2836 * The default file is needed by the printers window to show
2837 * the default printer.
2838 */
2839
2840 snprintf(filename, sizeof(filename), "/var/spool/lp/default");
2841
2842 if (DefaultPrinter != NULL)
2843 {
2844 if ((fp = cupsFileOpen(filename, "w")) != NULL)
2845 {
2846 cupsFilePrintf(fp, "%s\n", DefaultPrinter->name);
2847
2848 cupsFileClose(fp);
2849
2850 chmod(filename, 0644);
2851 chown(filename, User, Group);
2852 }
2853 }
2854 else
2855 unlink(filename);
2856 }
2857 #endif /* __sgi */
2858
2859
2860 /*
2861 * End of "$Id$".
2862 */