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