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