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