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