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