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