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