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