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