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