]> git.ipfire.org Git - thirdparty/cups.git/blob - scheduler/cups-driverd.cxx
Stop advertising the HTTP methods that are supported (Issue #5540)
[thirdparty/cups.git] / scheduler / cups-driverd.cxx
1 /*
2 * PPD/driver support for CUPS.
3 *
4 * This program handles listing and installing static PPD files, PPD files
5 * created from driver information files, and dynamically generated PPD files
6 * using driver helper programs.
7 *
8 * Copyright © 2007-2019 by Apple Inc.
9 * Copyright © 1997-2007 by Easy Software Products.
10 *
11 * Licensed under Apache License v2.0. See the file "LICENSE" for more
12 * information.
13 */
14
15 /*
16 * Include necessary headers...
17 */
18
19 #include "util.h"
20 #include <cups/dir.h>
21 #include <cups/transcode.h>
22 #include <cups/ppd-private.h>
23 #include <ppdc/ppdc.h>
24 #include <regex.h>
25
26
27 /*
28 * Constants...
29 */
30
31 #define PPD_SYNC 0x50504441 /* Sync word for ppds.dat (PPDA) */
32 #define PPD_MAX_LANG 32 /* Maximum languages */
33 #define PPD_MAX_PROD 32 /* Maximum products */
34 #define PPD_MAX_VERS 32 /* Maximum versions */
35
36 #define PPD_TYPE_POSTSCRIPT 0 /* PostScript PPD */
37 #define PPD_TYPE_PDF 1 /* PDF PPD */
38 #define PPD_TYPE_RASTER 2 /* CUPS raster PPD */
39 #define PPD_TYPE_FAX 3 /* Facsimile/MFD PPD */
40 #define PPD_TYPE_UNKNOWN 4 /* Other/hybrid PPD */
41 #define PPD_TYPE_DRV 5 /* Driver info file */
42 #define PPD_TYPE_ARCHIVE 6 /* Archive file */
43
44 #define TAR_BLOCK 512 /* Number of bytes in a block */
45 #define TAR_BLOCKS 10 /* Blocking factor */
46
47 #define TAR_MAGIC "ustar" /* 5 chars and a null */
48 #define TAR_VERSION "00" /* POSIX tar version */
49
50 #define TAR_OLDNORMAL '\0' /* Normal disk file, Unix compat */
51 #define TAR_NORMAL '0' /* Normal disk file */
52 #define TAR_LINK '1' /* Link to previously dumped file */
53 #define TAR_SYMLINK '2' /* Symbolic link */
54 #define TAR_CHR '3' /* Character special file */
55 #define TAR_BLK '4' /* Block special file */
56 #define TAR_DIR '5' /* Directory */
57 #define TAR_FIFO '6' /* FIFO special file */
58 #define TAR_CONTIG '7' /* Contiguous file */
59
60
61 /*
62 * PPD information structures...
63 */
64
65 typedef struct /**** PPD record ****/
66 {
67 time_t mtime; /* Modification time */
68 off_t size; /* Size in bytes */
69 int model_number; /* cupsModelNumber */
70 int type; /* ppd-type */
71 char filename[512], /* Filename */
72 name[256], /* PPD name */
73 languages[PPD_MAX_LANG][6],
74 /* LanguageVersion/cupsLanguages */
75 products[PPD_MAX_PROD][128],
76 /* Product strings */
77 psversions[PPD_MAX_VERS][32],
78 /* PSVersion strings */
79 make[128], /* Manufacturer */
80 make_and_model[128], /* NickName/ModelName */
81 device_id[256], /* IEEE 1284 Device ID */
82 scheme[128]; /* PPD scheme */
83 } ppd_rec_t;
84
85 typedef struct /**** In-memory record ****/
86 {
87 int found; /* 1 if PPD is found */
88 int matches; /* Match count */
89 ppd_rec_t record; /* PPDs.dat record */
90 } ppd_info_t;
91
92 typedef union /**** TAR record format ****/
93 {
94 unsigned char all[TAR_BLOCK]; /* Raw data block */
95 struct
96 {
97 char pathname[100], /* Destination path */
98 mode[8], /* Octal file permissions */
99 uid[8], /* Octal user ID */
100 gid[8], /* Octal group ID */
101 size[12], /* Octal size in bytes */
102 mtime[12], /* Octal modification time */
103 chksum[8], /* Octal checksum value */
104 linkflag, /* File type */
105 linkname[100], /* Source path for link */
106 magic[6], /* Magic string */
107 version[2], /* Format version */
108 uname[32], /* User name */
109 gname[32], /* Group name */
110 devmajor[8], /* Octal device major number */
111 devminor[8], /* Octal device minor number */
112 prefix[155]; /* Prefix for long filenames */
113 } header;
114 } tar_rec_t;
115
116
117 /*
118 * Globals...
119 */
120
121 static cups_array_t *Inodes = NULL, /* Inodes of directories we've visited */
122 *PPDsByName = NULL,
123 /* PPD files sorted by filename and name */
124 *PPDsByMakeModel = NULL;
125 /* PPD files sorted by make and model */
126 static int ChangedPPD; /* Did we change the PPD database? */
127 static const char * const PPDTypes[] = /* ppd-type values */
128 {
129 "postscript",
130 "pdf",
131 "raster",
132 "fax",
133 "object",
134 "object-direct",
135 "object-storage",
136 "unknown",
137 "drv",
138 "archive"
139 };
140
141
142 /*
143 * Local functions...
144 */
145
146 static ppd_info_t *add_ppd(const char *filename, const char *name,
147 const char *language, const char *make,
148 const char *make_and_model,
149 const char *device_id, const char *product,
150 const char *psversion, time_t mtime,
151 size_t size, int model_number, int type,
152 const char *scheme);
153 static int cat_drv(const char *name, int request_id);
154 static void cat_ppd(const char *name, int request_id);
155 static int cat_static(const char *name, int request_id);
156 static int cat_tar(const char *name, int request_id);
157 static int compare_inodes(struct stat *a, struct stat *b);
158 static int compare_matches(const ppd_info_t *p0,
159 const ppd_info_t *p1);
160 static int compare_names(const ppd_info_t *p0,
161 const ppd_info_t *p1);
162 static int compare_ppds(const ppd_info_t *p0,
163 const ppd_info_t *p1);
164 static void dump_ppds_dat(const char *filename);
165 static void free_array(cups_array_t *a);
166 static cups_file_t *get_file(const char *name, int request_id,
167 const char *subdir, char *buffer,
168 size_t bufsize, char **subfile);
169 static void list_ppds(int request_id, int limit, const char *opt);
170 static int load_drivers(cups_array_t *include,
171 cups_array_t *exclude);
172 static int load_drv(const char *filename, const char *name,
173 cups_file_t *fp, time_t mtime, off_t size);
174 static void load_ppd(const char *filename, const char *name,
175 const char *scheme, struct stat *fileinfo,
176 ppd_info_t *ppd, cups_file_t *fp, off_t end);
177 static int load_ppds(const char *d, const char *p, int descend);
178 static void load_ppds_dat(char *filename, size_t filesize,
179 int verbose);
180 static int load_tar(const char *filename, const char *name,
181 cups_file_t *fp, time_t mtime, off_t size);
182 static int read_tar(cups_file_t *fp, char *name, size_t namesize,
183 struct stat *info);
184 static regex_t *regex_device_id(const char *device_id);
185 static regex_t *regex_string(const char *s);
186
187
188 /*
189 * 'main()' - Scan for drivers and return an IPP response.
190 *
191 * Usage:
192 *
193 * cups-driverd request_id limit options
194 */
195
196 int /* O - Exit code */
197 main(int argc, /* I - Number of command-line args */
198 char *argv[]) /* I - Command-line arguments */
199 {
200 /*
201 * Install or list PPDs...
202 */
203
204 if (argc == 3 && !strcmp(argv[1], "cat"))
205 cat_ppd(argv[2], 0);
206 else if ((argc == 2 || argc == 3) && !strcmp(argv[1], "dump"))
207 dump_ppds_dat(argv[2]);
208 else if (argc == 4 && !strcmp(argv[1], "get"))
209 cat_ppd(argv[3], atoi(argv[2]));
210 else if (argc == 5 && !strcmp(argv[1], "list"))
211 list_ppds(atoi(argv[2]), atoi(argv[3]), argv[4]);
212 else
213 {
214 fputs("Usage: cups-driverd cat ppd-name\n", stderr);
215 fputs("Usage: cups-driverd dump\n", stderr);
216 fputs("Usage: cups-driverd get request_id ppd-name\n", stderr);
217 fputs("Usage: cups-driverd list request_id limit options\n", stderr);
218 return (1);
219 }
220 }
221
222
223 /*
224 * 'add_ppd()' - Add a PPD file.
225 */
226
227 static ppd_info_t * /* O - PPD */
228 add_ppd(const char *filename, /* I - PPD filename */
229 const char *name, /* I - PPD name */
230 const char *language, /* I - LanguageVersion */
231 const char *make, /* I - Manufacturer */
232 const char *make_and_model, /* I - NickName/ModelName */
233 const char *device_id, /* I - 1284DeviceID */
234 const char *product, /* I - Product */
235 const char *psversion, /* I - PSVersion */
236 time_t mtime, /* I - Modification time */
237 size_t size, /* I - File size */
238 int model_number, /* I - Model number */
239 int type, /* I - Driver type */
240 const char *scheme) /* I - PPD scheme */
241 {
242 ppd_info_t *ppd; /* PPD */
243 char *recommended; /* Foomatic driver string */
244
245
246 /*
247 * Add a new PPD file...
248 */
249
250 if ((ppd = (ppd_info_t *)calloc(1, sizeof(ppd_info_t))) == NULL)
251 {
252 fprintf(stderr,
253 "ERROR: [cups-driverd] Ran out of memory for %d PPD files!\n",
254 cupsArrayCount(PPDsByName));
255 return (NULL);
256 }
257
258 /*
259 * Zero-out the PPD data and copy the values over...
260 */
261
262 ppd->found = 1;
263 ppd->record.mtime = mtime;
264 ppd->record.size = (off_t)size;
265 ppd->record.model_number = model_number;
266 ppd->record.type = type;
267
268 strlcpy(ppd->record.filename, filename, sizeof(ppd->record.filename));
269 strlcpy(ppd->record.name, name, sizeof(ppd->record.name));
270 strlcpy(ppd->record.languages[0], language,
271 sizeof(ppd->record.languages[0]));
272 strlcpy(ppd->record.products[0], product, sizeof(ppd->record.products[0]));
273 strlcpy(ppd->record.psversions[0], psversion,
274 sizeof(ppd->record.psversions[0]));
275 strlcpy(ppd->record.make, make, sizeof(ppd->record.make));
276 strlcpy(ppd->record.make_and_model, make_and_model,
277 sizeof(ppd->record.make_and_model));
278 strlcpy(ppd->record.device_id, device_id, sizeof(ppd->record.device_id));
279 strlcpy(ppd->record.scheme, scheme, sizeof(ppd->record.scheme));
280
281 /*
282 * Strip confusing (and often wrong) "recommended" suffix added by
283 * Foomatic drivers...
284 */
285
286 if ((recommended = strstr(ppd->record.make_and_model,
287 " (recommended)")) != NULL)
288 *recommended = '\0';
289
290 /*
291 * Add the PPD to the PPD arrays...
292 */
293
294 cupsArrayAdd(PPDsByName, ppd);
295 cupsArrayAdd(PPDsByMakeModel, ppd);
296
297 /*
298 * Return the new PPD pointer...
299 */
300
301 return (ppd);
302 }
303
304
305 /*
306 * 'cat_drv()' - Generate a PPD from a driver info file.
307 */
308
309 static int /* O - Exit code */
310 cat_drv(const char *name, /* I - PPD name */
311 int request_id) /* I - Request ID for response? */
312 {
313 cups_file_t *fp; // File pointer
314 ppdcSource *src; // PPD source file data
315 ppdcDriver *d; // Current driver
316 cups_file_t *out; // Stdout via CUPS file API
317 char message[2048], // status-message
318 filename[1024], // Full path to .drv file(s)
319 scheme[32], // URI scheme ("drv")
320 userpass[256], // User/password info (unused)
321 host[2], // Hostname (unused)
322 resource[1024], // Resource path (/dir/to/filename.drv)
323 *pc_file_name; // Filename portion of URI
324 int port; // Port number (unused)
325
326
327 // Pull out the path to the .drv file...
328 if (httpSeparateURI(HTTP_URI_CODING_ALL, name, scheme, sizeof(scheme),
329 userpass, sizeof(userpass), host, sizeof(host), &port,
330 resource, sizeof(resource)) < HTTP_URI_OK)
331 {
332 fprintf(stderr, "ERROR: Bad PPD name \"%s\".\n", name);
333
334 if (request_id)
335 {
336 snprintf(message, sizeof(message), "Bad PPD name \"%s\".", name);
337
338 cupsdSendIPPHeader(IPP_NOT_FOUND, request_id);
339 cupsdSendIPPGroup(IPP_TAG_OPERATION);
340 cupsdSendIPPString(IPP_TAG_CHARSET, "attributes-charset", "utf-8");
341 cupsdSendIPPString(IPP_TAG_LANGUAGE, "attributes-natural-language",
342 "en-US");
343 cupsdSendIPPString(IPP_TAG_TEXT, "status-message", message);
344 cupsdSendIPPTrailer();
345 }
346
347 return (1);
348 }
349
350 if ((fp = get_file(resource, request_id, "drv", filename, sizeof(filename),
351 &pc_file_name)) == NULL)
352 return (1);
353
354 src = new ppdcSource(filename, fp);
355
356 for (d = (ppdcDriver *)src->drivers->first();
357 d;
358 d = (ppdcDriver *)src->drivers->next())
359 if (!strcmp(pc_file_name, d->pc_file_name->value) ||
360 (d->file_name && !strcmp(pc_file_name, d->file_name->value)))
361 break;
362
363 if (d)
364 {
365 ppdcArray *locales; // Locale names
366 ppdcCatalog *catalog; // Message catalog in .drv file
367
368
369 fprintf(stderr, "DEBUG2: [cups-driverd] %u locales defined in \"%s\"...\n", (unsigned)src->po_files->count, filename);
370
371 locales = new ppdcArray();
372 for (catalog = (ppdcCatalog *)src->po_files->first();
373 catalog;
374 catalog = (ppdcCatalog *)src->po_files->next())
375 {
376 fprintf(stderr, "DEBUG2: [cups-driverd] Adding locale \"%s\"...\n",
377 catalog->locale->value);
378 catalog->locale->retain();
379 locales->add(catalog->locale);
380 }
381
382 if (request_id)
383 {
384 cupsdSendIPPHeader(IPP_OK, request_id);
385 cupsdSendIPPGroup(IPP_TAG_OPERATION);
386 cupsdSendIPPString(IPP_TAG_CHARSET, "attributes-charset", "utf-8");
387 cupsdSendIPPString(IPP_TAG_LANGUAGE, "attributes-natural-language",
388 "en-US");
389 cupsdSendIPPTrailer();
390 fflush(stdout);
391 }
392
393 out = cupsFileStdout();
394 d->write_ppd_file(out, NULL, locales, src, PPDC_LFONLY);
395 cupsFileClose(out);
396
397 locales->release();
398 }
399 else
400 {
401 fprintf(stderr, "ERROR: PPD \"%s\" not found.\n", name);
402
403 if (request_id)
404 {
405 snprintf(message, sizeof(message), "PPD \"%s\" not found.", name);
406
407 cupsdSendIPPHeader(IPP_NOT_FOUND, request_id);
408 cupsdSendIPPGroup(IPP_TAG_OPERATION);
409 cupsdSendIPPString(IPP_TAG_CHARSET, "attributes-charset", "utf-8");
410 cupsdSendIPPString(IPP_TAG_LANGUAGE, "attributes-natural-language",
411 "en-US");
412 cupsdSendIPPString(IPP_TAG_TEXT, "status-message", message);
413 cupsdSendIPPTrailer();
414 }
415 }
416
417 src->release();
418 cupsFileClose(fp);
419
420 return (!d);
421 }
422
423
424 /*
425 * 'cat_ppd()' - Copy a PPD file to stdout.
426 */
427
428 static void
429 cat_ppd(const char *name, /* I - PPD name */
430 int request_id) /* I - Request ID for response? */
431 {
432 char scheme[256], /* Scheme from PPD name */
433 *sptr, /* Pointer into scheme */
434 line[1024], /* Line/filename */
435 message[2048]; /* status-message */
436
437
438 /*
439 * Figure out if this is a static or dynamic PPD file...
440 */
441
442 if (strstr(name, "../"))
443 {
444 fputs("ERROR: Invalid PPD name.\n", stderr);
445 exit(1);
446 }
447
448 strlcpy(scheme, name, sizeof(scheme));
449 if ((sptr = strchr(scheme, ':')) != NULL)
450 {
451 *sptr = '\0';
452
453 if (!strcmp(scheme, "file"))
454 {
455 /*
456 * "file:name" == "name"...
457 */
458
459 name += 5;
460
461 while (*name == '/')
462 name ++;
463
464 if (!strstr(name, ".tar/") && !strstr(name, ".tar.gz/"))
465 scheme[0] = '\0';
466 }
467 }
468 else
469 scheme[0] = '\0';
470
471 if (request_id > 0)
472 puts("Content-Type: application/ipp\n");
473
474 if (!scheme[0])
475 exit(cat_static(name, request_id));
476 else if (!strcmp(scheme, "drv"))
477 exit(cat_drv(name, request_id));
478 else if (!strcmp(scheme, "file"))
479 exit(cat_tar(name, request_id));
480 else
481 {
482 /*
483 * Dynamic PPD, see if we have a driver program to support it...
484 */
485
486 const char *serverbin; /* CUPS_SERVERBIN env var */
487 char *argv[4]; /* Arguments for program */
488
489
490 if ((serverbin = getenv("CUPS_SERVERBIN")) == NULL)
491 serverbin = CUPS_SERVERBIN;
492
493 snprintf(line, sizeof(line), "%s/driver/%s", serverbin, scheme);
494 if (access(line, X_OK))
495 {
496 /*
497 * File does not exist or is not executable...
498 */
499
500 fprintf(stderr, "ERROR: [cups-driverd] Unable to access \"%s\" - %s\n",
501 line, strerror(errno));
502
503 if (request_id > 0)
504 {
505 snprintf(message, sizeof(message), "Unable to access \"%s\" - %s",
506 line, strerror(errno));
507
508 cupsdSendIPPHeader(IPP_NOT_FOUND, request_id);
509 cupsdSendIPPGroup(IPP_TAG_OPERATION);
510 cupsdSendIPPString(IPP_TAG_CHARSET, "attributes-charset", "utf-8");
511 cupsdSendIPPString(IPP_TAG_LANGUAGE, "attributes-natural-language",
512 "en-US");
513 cupsdSendIPPString(IPP_TAG_TEXT, "status-message", message);
514 cupsdSendIPPTrailer();
515 }
516
517 exit(1);
518 }
519
520 /*
521 * Yes, let it cat the PPD file...
522 */
523
524 if (request_id)
525 {
526 cupsdSendIPPHeader(IPP_OK, request_id);
527 cupsdSendIPPGroup(IPP_TAG_OPERATION);
528 cupsdSendIPPString(IPP_TAG_CHARSET, "attributes-charset", "utf-8");
529 cupsdSendIPPString(IPP_TAG_LANGUAGE, "attributes-natural-language",
530 "en-US");
531 cupsdSendIPPTrailer();
532 }
533
534 argv[0] = scheme;
535 argv[1] = (char *)"cat";
536 argv[2] = (char *)name;
537 argv[3] = NULL;
538
539 if (cupsdExec(line, argv))
540 {
541 /*
542 * Unable to execute driver...
543 */
544
545 fprintf(stderr, "ERROR: [cups-driverd] Unable to execute \"%s\" - %s\n",
546 line, strerror(errno));
547 exit(1);
548 }
549 }
550
551 /*
552 * Exit with no errors...
553 */
554
555 exit(0);
556 }
557
558
559 /*
560 * 'copy_static()' - Copy a static PPD file to stdout.
561 */
562
563 static int /* O - Exit code */
564 cat_static(const char *name, /* I - PPD name */
565 int request_id) /* I - Request ID for response? */
566 {
567 cups_file_t *fp; /* PPD file */
568 char filename[1024], /* PPD filename */
569 line[1024]; /* Line buffer */
570
571
572 if ((fp = get_file(name, request_id, "model", filename, sizeof(filename),
573 NULL)) == NULL)
574 return (1);
575
576 if (request_id)
577 {
578 cupsdSendIPPHeader(IPP_OK, request_id);
579 cupsdSendIPPGroup(IPP_TAG_OPERATION);
580 cupsdSendIPPString(IPP_TAG_CHARSET, "attributes-charset", "utf-8");
581 cupsdSendIPPString(IPP_TAG_LANGUAGE, "attributes-natural-language",
582 "en-US");
583 cupsdSendIPPTrailer();
584 }
585
586 /*
587 * Now copy the file to stdout...
588 */
589
590 while (cupsFileGets(fp, line, sizeof(line)))
591 puts(line);
592
593 cupsFileClose(fp);
594
595 return (0);
596 }
597
598
599 /*
600 * 'cat_tar()' - Copy an archived PPD file to stdout.
601 */
602
603 static int /* O - Exit code */
604 cat_tar(const char *name, /* I - PPD name */
605 int request_id) /* I - Request ID */
606 {
607 cups_file_t *fp; /* Archive file pointer */
608 char filename[1024], /* Archive filename */
609 *ppdname, /* PPD filename in archive */
610 curname[256], /* Current name in archive */
611 buffer[8192]; /* Copy buffer */
612 struct stat curinfo; /* Current file info in archive */
613 off_t total, /* Total bytes copied */
614 next; /* Offset for next record in archive */
615 ssize_t bytes; /* Bytes read */
616
617
618 /*
619 * Open the archive file...
620 */
621
622 if ((fp = get_file(name, request_id, "model", filename, sizeof(filename),
623 &ppdname)) == NULL || !ppdname)
624 return (1);
625
626 /*
627 * Scan the archive for the PPD...
628 */
629
630 while (read_tar(fp, curname, sizeof(curname), &curinfo))
631 {
632 next = cupsFileTell(fp) + ((curinfo.st_size + TAR_BLOCK - 1) &
633 ~(TAR_BLOCK - 1));
634
635 if (!strcmp(ppdname, curname))
636 {
637 if (request_id)
638 {
639 cupsdSendIPPHeader(IPP_OK, request_id);
640 cupsdSendIPPGroup(IPP_TAG_OPERATION);
641 cupsdSendIPPString(IPP_TAG_CHARSET, "attributes-charset", "utf-8");
642 cupsdSendIPPString(IPP_TAG_LANGUAGE, "attributes-natural-language",
643 "en-US");
644 cupsdSendIPPTrailer();
645 }
646
647 for (total = 0; total < curinfo.st_size; total += bytes)
648 {
649 if ((size_t)(bytes = (curinfo.st_size - total)) > sizeof(buffer))
650 bytes = sizeof(buffer);
651
652 if ((bytes = cupsFileRead(fp, buffer, (size_t)bytes)) < 0)
653 {
654 if (errno == EINTR || errno == EAGAIN)
655 {
656 bytes = 0;
657 }
658 else
659 {
660 perror("ERROR: [cups-driverd] Read error");
661 break;
662 }
663 }
664 else if (bytes > 0 && fwrite(buffer, (size_t)bytes, 1, stdout) != 1)
665 break;
666 }
667
668 cupsFileClose(fp);
669 return (0);
670 }
671
672 if (cupsFileTell(fp) != next)
673 cupsFileSeek(fp, next);
674 }
675
676 cupsFileClose(fp);
677
678 fprintf(stderr, "ERROR: PPD \"%s\" not found.\n", name);
679
680 if (request_id)
681 {
682 snprintf(buffer, sizeof(buffer), "PPD \"%s\" not found.", name);
683
684 cupsdSendIPPHeader(IPP_NOT_FOUND, request_id);
685 cupsdSendIPPGroup(IPP_TAG_OPERATION);
686 cupsdSendIPPString(IPP_TAG_CHARSET, "attributes-charset", "utf-8");
687 cupsdSendIPPString(IPP_TAG_LANGUAGE, "attributes-natural-language",
688 "en-US");
689 cupsdSendIPPString(IPP_TAG_TEXT, "status-message", buffer);
690 cupsdSendIPPTrailer();
691 }
692
693 return (1);
694 }
695
696
697 /*
698 * 'compare_inodes()' - Compare two inodes.
699 */
700
701 static int /* O - Result of comparison */
702 compare_inodes(struct stat *a, /* I - First inode */
703 struct stat *b) /* I - Second inode */
704 {
705 if (a->st_dev != b->st_dev)
706 return (a->st_dev - b->st_dev);
707 else
708 return (a->st_ino - b->st_ino);
709 }
710
711
712 /*
713 * 'compare_matches()' - Compare PPD match scores for sorting.
714 */
715
716 static int
717 compare_matches(const ppd_info_t *p0, /* I - First PPD */
718 const ppd_info_t *p1) /* I - Second PPD */
719 {
720 if (p1->matches != p0->matches)
721 return (p1->matches - p0->matches);
722 else
723 return (cupsdCompareNames(p0->record.make_and_model,
724 p1->record.make_and_model));
725 }
726
727
728 /*
729 * 'compare_names()' - Compare PPD filenames for sorting.
730 */
731
732 static int /* O - Result of comparison */
733 compare_names(const ppd_info_t *p0, /* I - First PPD file */
734 const ppd_info_t *p1) /* I - Second PPD file */
735 {
736 int diff; /* Difference between strings */
737
738
739 if ((diff = strcmp(p0->record.filename, p1->record.filename)) != 0)
740 return (diff);
741 else
742 return (strcmp(p0->record.name, p1->record.name));
743 }
744
745
746 /*
747 * 'compare_ppds()' - Compare PPD file make and model names for sorting.
748 */
749
750 static int /* O - Result of comparison */
751 compare_ppds(const ppd_info_t *p0, /* I - First PPD file */
752 const ppd_info_t *p1) /* I - Second PPD file */
753 {
754 int diff; /* Difference between strings */
755
756
757 /*
758 * First compare manufacturers...
759 */
760
761 if ((diff = _cups_strcasecmp(p0->record.make, p1->record.make)) != 0)
762 return (diff);
763 else if ((diff = cupsdCompareNames(p0->record.make_and_model,
764 p1->record.make_and_model)) != 0)
765 return (diff);
766 else if ((diff = strcmp(p0->record.languages[0],
767 p1->record.languages[0])) != 0)
768 return (diff);
769 else
770 return (compare_names(p0, p1));
771 }
772
773
774 /*
775 * 'dump_ppds_dat()' - Dump the contents of the ppds.dat file.
776 */
777
778 static void
779 dump_ppds_dat(const char *filename) /* I - Filename */
780 {
781 char temp[1024]; /* ppds.dat filename */
782 ppd_info_t *ppd; /* Current PPD */
783
784
785 /*
786 * See if we a PPD database file...
787 */
788
789 if (filename)
790 strlcpy(temp, filename, sizeof(temp));
791 else
792 temp[0] = '\0';
793
794 load_ppds_dat(temp, sizeof(temp), 0);
795
796 puts("mtime,size,model_number,type,filename,name,languages0,products0,"
797 "psversions0,make,make_and_model,device_id,scheme");
798 for (ppd = (ppd_info_t *)cupsArrayFirst(PPDsByName);
799 ppd;
800 ppd = (ppd_info_t *)cupsArrayNext(PPDsByName))
801 printf("%d,%ld,%d,%d,\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\","
802 "\"%s\",\"%s\"\n",
803 (int)ppd->record.mtime, (long)ppd->record.size,
804 ppd->record.model_number, ppd->record.type, ppd->record.filename,
805 ppd->record.name, ppd->record.languages[0], ppd->record.products[0],
806 ppd->record.psversions[0], ppd->record.make,
807 ppd->record.make_and_model, ppd->record.device_id,
808 ppd->record.scheme);
809
810 exit(0);
811 }
812
813
814 /*
815 * 'free_array()' - Free an array of strings.
816 */
817
818 static void
819 free_array(cups_array_t *a) /* I - Array to free */
820 {
821 char *ptr; /* Pointer to string */
822
823
824 for (ptr = (char *)cupsArrayFirst(a);
825 ptr;
826 ptr = (char *)cupsArrayNext(a))
827 free(ptr);
828
829 cupsArrayDelete(a);
830 }
831
832
833 /*
834 * 'get_file()' - Get the filename associated with a request.
835 */
836
837 static cups_file_t * /* O - File pointer or NULL */
838 get_file(const char *name, /* I - Name */
839 int request_id, /* I - Request ID */
840 const char *subdir, /* I - Subdirectory for file */
841 char *buffer, /* I - Filename buffer */
842 size_t bufsize, /* I - Size of filename buffer */
843 char **subfile) /* O - Sub-filename */
844 {
845 cups_file_t *fp; /* File pointer */
846 const char *datadir; /* CUPS_DATADIR env var */
847 char *bufptr, /* Pointer into filename buffer */
848 message[2048]; /* status-message */
849 #ifdef __APPLE__
850 const char *printerDriver, /* Pointer to .printerDriver extension */
851 *slash; /* Pointer to next slash */
852 #endif /* __APPLE__ */
853
854
855 if (subfile)
856 *subfile = NULL;
857
858 while (*name == '/')
859 name ++;
860
861 if (strstr(name, "../") || strstr(name, "/.."))
862 {
863 /*
864 * Bad name...
865 */
866
867 fprintf(stderr, "ERROR: [cups-driverd] Bad PPD name \"%s\".\n", name);
868
869 if (request_id)
870 {
871 snprintf(message, sizeof(message), "Bad PPD name \"%s\".", name);
872
873 cupsdSendIPPHeader(IPP_NOT_FOUND, request_id);
874 cupsdSendIPPGroup(IPP_TAG_OPERATION);
875 cupsdSendIPPString(IPP_TAG_CHARSET, "attributes-charset", "utf-8");
876 cupsdSendIPPString(IPP_TAG_LANGUAGE, "attributes-natural-language",
877 "en-US");
878 cupsdSendIPPString(IPP_TAG_TEXT, "status-message", message);
879 cupsdSendIPPTrailer();
880 }
881
882 return (NULL);
883 }
884
885 /*
886 * Try opening the file...
887 */
888
889 #ifdef __APPLE__
890 if (!strncmp(name, "System/Library/Printers/PPDs/Contents/Resources/", 48) ||
891 !strncmp(name, "Library/Printers/PPDs/Contents/Resources/", 41) ||
892 (!strncmp(name, "System/Library/Printers/", 24) &&
893 (printerDriver =
894 strstr(name + 24,
895 ".printerDriver/Contents/Resources/PPDs")) != NULL &&
896 (slash = strchr(name + 24, '/')) != NULL &&
897 slash > printerDriver) ||
898 (!strncmp(name, "Library/Printers/", 17) &&
899 (printerDriver =
900 strstr(name + 17,
901 ".printerDriver/Contents/Resources/PPDs")) != NULL &&
902 (slash = strchr(name + 17, '/')) != NULL &&
903 slash > printerDriver))
904 {
905 /*
906 * Map ppd-name to macOS standard locations...
907 */
908
909 snprintf(buffer, bufsize, "/%s", name);
910 }
911 else
912
913 #elif defined(__linux)
914 if (!strncmp(name, "lsb/usr/", 8))
915 {
916 /*
917 * Map ppd-name to LSB standard /usr/share/ppd location...
918 */
919
920 snprintf(buffer, bufsize, "/usr/share/ppd/%s", name + 8);
921 }
922 else if (!strncmp(name, "lsb/opt/", 8))
923 {
924 /*
925 * Map ppd-name to LSB standard /opt/share/ppd location...
926 */
927
928 snprintf(buffer, bufsize, "/opt/share/ppd/%s", name + 8);
929 }
930 else if (!strncmp(name, "lsb/local/", 10))
931 {
932 /*
933 * Map ppd-name to LSB standard /usr/local/share/ppd location...
934 */
935
936 snprintf(buffer, bufsize, "/usr/local/share/ppd/%s", name + 10);
937 }
938 else
939
940 #endif /* __APPLE__ */
941 {
942 if ((datadir = getenv("CUPS_DATADIR")) == NULL)
943 datadir = CUPS_DATADIR;
944
945 snprintf(buffer, bufsize, "%s/%s/%s", datadir, subdir, name);
946 }
947
948 /*
949 * Strip anything after ".drv/", ".drv.gz/", ".tar/", or ".tar.gz/"...
950 */
951
952 if (subfile)
953 {
954 if ((bufptr = strstr(buffer, ".drv/")) != NULL)
955 bufptr += 4;
956 else if ((bufptr = strstr(buffer, ".drv.gz/")) != NULL)
957 bufptr += 7;
958 else if ((bufptr = strstr(buffer, ".tar/")) != NULL)
959 bufptr += 4;
960 else if ((bufptr = strstr(buffer, ".tar.gz/")) != NULL)
961 bufptr += 7;
962
963 if (bufptr)
964 {
965 *bufptr++ = '\0';
966 *subfile = bufptr;
967 }
968 }
969
970 /*
971 * Try opening the file...
972 */
973
974 if ((fp = cupsFileOpen(buffer, "r")) == NULL)
975 {
976 fprintf(stderr, "ERROR: [cups-driverd] Unable to open \"%s\" - %s\n",
977 buffer, strerror(errno));
978
979 if (request_id)
980 {
981 snprintf(message, sizeof(message), "Unable to open \"%s\" - %s",
982 buffer, strerror(errno));
983
984 cupsdSendIPPHeader(IPP_NOT_FOUND, request_id);
985 cupsdSendIPPGroup(IPP_TAG_OPERATION);
986 cupsdSendIPPString(IPP_TAG_CHARSET, "attributes-charset", "utf-8");
987 cupsdSendIPPString(IPP_TAG_LANGUAGE, "attributes-natural-language",
988 "en-US");
989 cupsdSendIPPString(IPP_TAG_TEXT, "status-message", message);
990 cupsdSendIPPTrailer();
991 }
992
993 return (NULL);
994 }
995
996 return (fp);
997 }
998
999
1000 /*
1001 * 'list_ppds()' - List PPD files.
1002 */
1003
1004 static void
1005 list_ppds(int request_id, /* I - Request ID */
1006 int limit, /* I - Limit */
1007 const char *opt) /* I - Option argument */
1008 {
1009 int i; /* Looping vars */
1010 int count; /* Number of PPDs to send */
1011 ppd_info_t *ppd; /* Current PPD file */
1012 cups_file_t *fp; /* ppds.dat file */
1013 char filename[1024], /* ppds.dat filename */
1014 model[1024]; /* Model directory */
1015 const char *cups_datadir; /* CUPS_DATADIR environment variable */
1016 int num_options; /* Number of options */
1017 cups_option_t *options; /* Options */
1018 cups_array_t *requested, /* requested-attributes values */
1019 *include, /* PPD schemes to include */
1020 *exclude; /* PPD schemes to exclude */
1021 const char *device_id, /* ppd-device-id option */
1022 *language, /* ppd-natural-language option */
1023 *make, /* ppd-make option */
1024 *make_and_model, /* ppd-make-and-model option */
1025 *model_number_str, /* ppd-model-number option */
1026 *product, /* ppd-product option */
1027 *psversion, /* ppd-psversion option */
1028 *type_str; /* ppd-type option */
1029 int model_number, /* ppd-model-number value */
1030 type, /* ppd-type value */
1031 send_device_id, /* Send ppd-device-id? */
1032 send_make, /* Send ppd-make? */
1033 send_make_and_model, /* Send ppd-make-and-model? */
1034 send_model_number, /* Send ppd-model-number? */
1035 send_name, /* Send ppd-name? */
1036 send_natural_language, /* Send ppd-natural-language? */
1037 send_product, /* Send ppd-product? */
1038 send_psversion, /* Send ppd-psversion? */
1039 send_type, /* Send ppd-type? */
1040 sent_header; /* Sent the IPP header? */
1041 size_t make_and_model_len, /* Length of ppd-make-and-model */
1042 product_len; /* Length of ppd-product */
1043 regex_t *device_id_re, /* Regular expression for matching device ID */
1044 *make_and_model_re; /* Regular expression for matching make and model */
1045 regmatch_t re_matches[6]; /* Regular expression matches */
1046 cups_array_t *matches; /* Matching PPDs */
1047
1048
1049 fprintf(stderr,
1050 "DEBUG2: [cups-driverd] list_ppds(request_id=%d, limit=%d, "
1051 "opt=\"%s\"\n", request_id, limit, opt);
1052
1053 /*
1054 * See if we a PPD database file...
1055 */
1056
1057 filename[0] = '\0';
1058 load_ppds_dat(filename, sizeof(filename), 1);
1059
1060 /*
1061 * Load all PPDs in the specified directory and below...
1062 */
1063
1064 if ((cups_datadir = getenv("CUPS_DATADIR")) == NULL)
1065 cups_datadir = CUPS_DATADIR;
1066
1067 Inodes = cupsArrayNew((cups_array_func_t)compare_inodes, NULL);
1068
1069 snprintf(model, sizeof(model), "%s/model", cups_datadir);
1070 load_ppds(model, "", 1);
1071
1072 snprintf(model, sizeof(model), "%s/drv", cups_datadir);
1073 load_ppds(model, "", 1);
1074
1075 #ifdef __APPLE__
1076 /*
1077 * Load PPDs from standard macOS locations...
1078 */
1079
1080 load_ppds("/Library/Printers",
1081 "Library/Printers", 0);
1082 load_ppds("/Library/Printers/PPDs/Contents/Resources",
1083 "Library/Printers/PPDs/Contents/Resources", 0);
1084 load_ppds("/Library/Printers/PPDs/Contents/Resources/en.lproj",
1085 "Library/Printers/PPDs/Contents/Resources/en.lproj", 0);
1086 load_ppds("/System/Library/Printers",
1087 "System/Library/Printers", 0);
1088 load_ppds("/System/Library/Printers/PPDs/Contents/Resources",
1089 "System/Library/Printers/PPDs/Contents/Resources", 0);
1090 load_ppds("/System/Library/Printers/PPDs/Contents/Resources/en.lproj",
1091 "System/Library/Printers/PPDs/Contents/Resources/en.lproj", 0);
1092
1093 #elif defined(__linux)
1094 /*
1095 * Load PPDs from LSB-defined locations...
1096 */
1097
1098 if (!access("/usr/local/share/ppd", 0))
1099 load_ppds("/usr/local/share/ppd", "lsb/local", 1);
1100 if (!access("/usr/share/ppd", 0))
1101 load_ppds("/usr/share/ppd", "lsb/usr", 1);
1102 if (!access("/opt/share/ppd", 0))
1103 load_ppds("/opt/share/ppd", "lsb/opt", 1);
1104 #endif /* __APPLE__ */
1105
1106 /*
1107 * Cull PPD files that are no longer present...
1108 */
1109
1110 for (ppd = (ppd_info_t *)cupsArrayFirst(PPDsByName);
1111 ppd;
1112 ppd = (ppd_info_t *)cupsArrayNext(PPDsByName))
1113 if (!ppd->found)
1114 {
1115 /*
1116 * Remove this PPD file from the list...
1117 */
1118
1119 cupsArrayRemove(PPDsByName, ppd);
1120 cupsArrayRemove(PPDsByMakeModel, ppd);
1121 free(ppd);
1122
1123 ChangedPPD = 1;
1124 }
1125
1126 /*
1127 * Write the new ppds.dat file...
1128 */
1129
1130 fprintf(stderr, "DEBUG: [cups-driverd] ChangedPPD=%d\n", ChangedPPD);
1131
1132 if (ChangedPPD)
1133 {
1134 char newname[1024]; /* New filename */
1135
1136 snprintf(newname, sizeof(newname), "%s.%d", filename, (int)getpid());
1137
1138 if ((fp = cupsFileOpen(newname, "w")) != NULL)
1139 {
1140 unsigned ppdsync = PPD_SYNC; /* Sync word */
1141
1142 cupsFileWrite(fp, (char *)&ppdsync, sizeof(ppdsync));
1143
1144 for (ppd = (ppd_info_t *)cupsArrayFirst(PPDsByName);
1145 ppd;
1146 ppd = (ppd_info_t *)cupsArrayNext(PPDsByName))
1147 cupsFileWrite(fp, (char *)&(ppd->record), sizeof(ppd_rec_t));
1148
1149 cupsFileClose(fp);
1150
1151 if (rename(newname, filename))
1152 fprintf(stderr, "ERROR: [cups-driverd] Unable to rename \"%s\" - %s\n",
1153 newname, strerror(errno));
1154 else
1155 fprintf(stderr, "INFO: [cups-driverd] Wrote \"%s\", %d PPDs...\n",
1156 filename, cupsArrayCount(PPDsByName));
1157 }
1158 else
1159 fprintf(stderr, "ERROR: [cups-driverd] Unable to write \"%s\" - %s\n",
1160 filename, strerror(errno));
1161 }
1162 else
1163 fputs("INFO: [cups-driverd] No new or changed PPDs...\n", stderr);
1164
1165 /*
1166 * Scan for dynamic PPD files...
1167 */
1168
1169 num_options = cupsParseOptions(opt, 0, &options);
1170 exclude = cupsdCreateStringsArray(cupsGetOption("exclude-schemes",
1171 num_options, options));
1172 include = cupsdCreateStringsArray(cupsGetOption("include-schemes",
1173 num_options, options));
1174
1175 load_drivers(include, exclude);
1176
1177 /*
1178 * Add the raw driver...
1179 */
1180
1181 add_ppd("", "raw", "en", "Raw", "Raw Queue", "", "", "", 0, 0, 0,
1182 PPD_TYPE_UNKNOWN, "raw");
1183
1184 /*
1185 * Send IPP attributes...
1186 */
1187
1188 requested = cupsdCreateStringsArray(
1189 cupsGetOption("requested-attributes", num_options,
1190 options));
1191 device_id = cupsGetOption("ppd-device-id", num_options, options);
1192 language = cupsGetOption("ppd-natural-language", num_options, options);
1193 make = cupsGetOption("ppd-make", num_options, options);
1194 make_and_model = cupsGetOption("ppd-make-and-model", num_options, options);
1195 model_number_str = cupsGetOption("ppd-model-number", num_options, options);
1196 product = cupsGetOption("ppd-product", num_options, options);
1197 psversion = cupsGetOption("ppd-psversion", num_options, options);
1198 type_str = cupsGetOption("ppd-type", num_options, options);
1199
1200 if (make_and_model)
1201 make_and_model_len = strlen(make_and_model);
1202 else
1203 make_and_model_len = 0;
1204
1205 if (product)
1206 product_len = strlen(product);
1207 else
1208 product_len = 0;
1209
1210 if (model_number_str)
1211 model_number = atoi(model_number_str);
1212 else
1213 model_number = 0;
1214
1215 if (type_str)
1216 {
1217 for (type = 0;
1218 type < (int)(sizeof(PPDTypes) / sizeof(PPDTypes[0]));
1219 type ++)
1220 if (!strcmp(type_str, PPDTypes[type]))
1221 break;
1222
1223 if (type >= (int)(sizeof(PPDTypes) / sizeof(PPDTypes[0])))
1224 {
1225 fprintf(stderr, "ERROR: [cups-driverd] Bad ppd-type=\"%s\" ignored!\n",
1226 type_str);
1227 type_str = NULL;
1228 }
1229 }
1230 else
1231 type = 0;
1232
1233 for (i = 0; i < num_options; i ++)
1234 fprintf(stderr, "DEBUG2: [cups-driverd] %s=\"%s\"\n", options[i].name,
1235 options[i].value);
1236
1237 if (!requested || cupsArrayFind(requested, (void *)"all") != NULL)
1238 {
1239 send_name = 1;
1240 send_make = 1;
1241 send_make_and_model = 1;
1242 send_model_number = 1;
1243 send_natural_language = 1;
1244 send_device_id = 1;
1245 send_product = 1;
1246 send_psversion = 1;
1247 send_type = 1;
1248 }
1249 else
1250 {
1251 send_name = cupsArrayFind(requested,
1252 (void *)"ppd-name") != NULL;
1253 send_make = cupsArrayFind(requested,
1254 (void *)"ppd-make") != NULL;
1255 send_make_and_model = cupsArrayFind(requested,
1256 (void *)"ppd-make-and-model") != NULL;
1257 send_model_number = cupsArrayFind(requested,
1258 (void *)"ppd-model-number") != NULL;
1259 send_natural_language = cupsArrayFind(requested,
1260 (void *)"ppd-natural-language") != NULL;
1261 send_device_id = cupsArrayFind(requested,
1262 (void *)"ppd-device-id") != NULL;
1263 send_product = cupsArrayFind(requested,
1264 (void *)"ppd-product") != NULL;
1265 send_psversion = cupsArrayFind(requested,
1266 (void *)"ppd-psversion") != NULL;
1267 send_type = cupsArrayFind(requested,
1268 (void *)"ppd-type") != NULL;
1269 }
1270
1271 /*
1272 * Send the content type header to the scheduler; request_id can only be
1273 * 0 when run manually since the scheduler enforces the IPP requirement for
1274 * a request ID from 1 to 2^31-1...
1275 */
1276
1277 if (request_id > 0)
1278 puts("Content-Type: application/ipp\n");
1279
1280 sent_header = 0;
1281
1282 if (limit <= 0 || limit > cupsArrayCount(PPDsByMakeModel))
1283 count = cupsArrayCount(PPDsByMakeModel);
1284 else
1285 count = limit;
1286
1287 if (device_id || language || make || make_and_model || model_number_str ||
1288 product)
1289 {
1290 matches = cupsArrayNew((cups_array_func_t)compare_matches, NULL);
1291
1292 if (device_id)
1293 device_id_re = regex_device_id(device_id);
1294 else
1295 device_id_re = NULL;
1296
1297 if (make_and_model)
1298 make_and_model_re = regex_string(make_and_model);
1299 else
1300 make_and_model_re = NULL;
1301
1302 for (ppd = (ppd_info_t *)cupsArrayFirst(PPDsByMakeModel);
1303 ppd;
1304 ppd = (ppd_info_t *)cupsArrayNext(PPDsByMakeModel))
1305 {
1306 /*
1307 * Filter PPDs based on make, model, product, language, model number,
1308 * and/or device ID using the "matches" score value. An exact match
1309 * for product, make-and-model, or device-id adds 3 to the score.
1310 * Partial matches for make-and-model yield 1 or 2 points, and matches
1311 * for the make and language add a single point. Results are then sorted
1312 * by score, highest score first.
1313 */
1314
1315 if (ppd->record.type < PPD_TYPE_POSTSCRIPT ||
1316 ppd->record.type >= PPD_TYPE_DRV)
1317 continue;
1318
1319 if (cupsArrayFind(exclude, ppd->record.scheme) ||
1320 (include && !cupsArrayFind(include, ppd->record.scheme)))
1321 continue;
1322
1323 ppd->matches = 0;
1324
1325 if (device_id_re &&
1326 !regexec(device_id_re, ppd->record.device_id,
1327 (size_t)(sizeof(re_matches) / sizeof(re_matches[0])),
1328 re_matches, 0))
1329 {
1330 /*
1331 * Add the number of matching values from the device ID - it will be
1332 * at least 2 (manufacturer and model), and as much as 3 (command set).
1333 */
1334
1335 for (i = 1; i < (int)(sizeof(re_matches) / sizeof(re_matches[0])); i ++)
1336 if (re_matches[i].rm_so >= 0)
1337 ppd->matches ++;
1338 }
1339
1340 if (language)
1341 {
1342 for (i = 0; i < PPD_MAX_LANG; i ++)
1343 if (!ppd->record.languages[i][0])
1344 break;
1345 else if (!strcmp(ppd->record.languages[i], language))
1346 {
1347 ppd->matches ++;
1348 break;
1349 }
1350 }
1351
1352 if (make && !_cups_strcasecmp(ppd->record.make, make))
1353 ppd->matches ++;
1354
1355 if (make_and_model_re &&
1356 !regexec(make_and_model_re, ppd->record.make_and_model,
1357 (size_t)(sizeof(re_matches) / sizeof(re_matches[0])),
1358 re_matches, 0))
1359 {
1360 // See how much of the make-and-model string we matched...
1361 if (re_matches[0].rm_so == 0)
1362 {
1363 if ((size_t)re_matches[0].rm_eo == make_and_model_len)
1364 ppd->matches += 3; // Exact match
1365 else
1366 ppd->matches += 2; // Prefix match
1367 }
1368 else
1369 ppd->matches ++; // Infix match
1370 }
1371
1372 if (model_number_str && ppd->record.model_number == model_number)
1373 ppd->matches ++;
1374
1375 if (product)
1376 {
1377 for (i = 0; i < PPD_MAX_PROD; i ++)
1378 if (!ppd->record.products[i][0])
1379 break;
1380 else if (!_cups_strcasecmp(ppd->record.products[i], product))
1381 {
1382 ppd->matches += 3;
1383 break;
1384 }
1385 else if (!_cups_strncasecmp(ppd->record.products[i], product,
1386 product_len))
1387 {
1388 ppd->matches += 2;
1389 break;
1390 }
1391 }
1392
1393 if (psversion)
1394 {
1395 for (i = 0; i < PPD_MAX_VERS; i ++)
1396 if (!ppd->record.psversions[i][0])
1397 break;
1398 else if (!_cups_strcasecmp(ppd->record.psversions[i], psversion))
1399 {
1400 ppd->matches ++;
1401 break;
1402 }
1403 }
1404
1405 if (type_str && ppd->record.type == type)
1406 ppd->matches ++;
1407
1408 if (ppd->matches)
1409 {
1410 fprintf(stderr, "DEBUG2: [cups-driverd] %s matches with score %d!\n",
1411 ppd->record.name, ppd->matches);
1412 cupsArrayAdd(matches, ppd);
1413 }
1414 }
1415 }
1416 else if (include || exclude)
1417 {
1418 matches = cupsArrayNew((cups_array_func_t)compare_ppds, NULL);
1419
1420 for (ppd = (ppd_info_t *)cupsArrayFirst(PPDsByMakeModel);
1421 ppd;
1422 ppd = (ppd_info_t *)cupsArrayNext(PPDsByMakeModel))
1423 {
1424 /*
1425 * Filter PPDs based on the include/exclude lists.
1426 */
1427
1428 if (ppd->record.type < PPD_TYPE_POSTSCRIPT ||
1429 ppd->record.type >= PPD_TYPE_DRV)
1430 continue;
1431
1432 if (cupsArrayFind(exclude, ppd->record.scheme) ||
1433 (include && !cupsArrayFind(include, ppd->record.scheme)))
1434 continue;
1435
1436 cupsArrayAdd(matches, ppd);
1437 }
1438 }
1439 else
1440 matches = PPDsByMakeModel;
1441
1442 for (ppd = (ppd_info_t *)cupsArrayFirst(matches);
1443 count > 0 && ppd;
1444 ppd = (ppd_info_t *)cupsArrayNext(matches))
1445 {
1446 /*
1447 * Skip invalid PPDs...
1448 */
1449
1450 if (ppd->record.type < PPD_TYPE_POSTSCRIPT ||
1451 ppd->record.type >= PPD_TYPE_DRV)
1452 continue;
1453
1454 /*
1455 * Send this PPD...
1456 */
1457
1458 if (!sent_header)
1459 {
1460 sent_header = 1;
1461
1462 if (request_id)
1463 {
1464 cupsdSendIPPHeader(IPP_OK, request_id);
1465 cupsdSendIPPGroup(IPP_TAG_OPERATION);
1466 cupsdSendIPPString(IPP_TAG_CHARSET, "attributes-charset", "utf-8");
1467 cupsdSendIPPString(IPP_TAG_LANGUAGE, "attributes-natural-language",
1468 "en-US");
1469 }
1470 }
1471
1472 fprintf(stderr, "DEBUG2: [cups-driverd] Sending %s (%s)...\n",
1473 ppd->record.name, ppd->record.make_and_model);
1474
1475 count --;
1476
1477 if (request_id)
1478 {
1479 cupsdSendIPPGroup(IPP_TAG_PRINTER);
1480
1481 if (send_name)
1482 cupsdSendIPPString(IPP_TAG_NAME, "ppd-name", ppd->record.name);
1483
1484 if (send_natural_language)
1485 {
1486 cupsdSendIPPString(IPP_TAG_LANGUAGE, "ppd-natural-language",
1487 ppd->record.languages[0]);
1488
1489 for (i = 1; i < PPD_MAX_LANG && ppd->record.languages[i][0]; i ++)
1490 cupsdSendIPPString(IPP_TAG_LANGUAGE, "", ppd->record.languages[i]);
1491 }
1492
1493 if (send_make)
1494 cupsdSendIPPString(IPP_TAG_TEXT, "ppd-make", ppd->record.make);
1495
1496 if (send_make_and_model)
1497 cupsdSendIPPString(IPP_TAG_TEXT, "ppd-make-and-model",
1498 ppd->record.make_and_model);
1499
1500 if (send_device_id)
1501 cupsdSendIPPString(IPP_TAG_TEXT, "ppd-device-id",
1502 ppd->record.device_id);
1503
1504 if (send_product)
1505 {
1506 cupsdSendIPPString(IPP_TAG_TEXT, "ppd-product",
1507 ppd->record.products[0]);
1508
1509 for (i = 1; i < PPD_MAX_PROD && ppd->record.products[i][0]; i ++)
1510 cupsdSendIPPString(IPP_TAG_TEXT, "", ppd->record.products[i]);
1511 }
1512
1513 if (send_psversion)
1514 {
1515 cupsdSendIPPString(IPP_TAG_TEXT, "ppd-psversion",
1516 ppd->record.psversions[0]);
1517
1518 for (i = 1; i < PPD_MAX_VERS && ppd->record.psversions[i][0]; i ++)
1519 cupsdSendIPPString(IPP_TAG_TEXT, "", ppd->record.psversions[i]);
1520 }
1521
1522 if (send_type)
1523 {
1524 if (ppd->record.type < PPD_TYPE_POSTSCRIPT || ppd->record.type > PPD_TYPE_ARCHIVE)
1525 {
1526 /*
1527 * This cache file is corrupted, remove it!
1528 */
1529
1530 unlink(filename);
1531
1532 cupsdSendIPPString(IPP_TAG_KEYWORD, "ppd-type", PPDTypes[PPD_TYPE_UNKNOWN]);
1533 }
1534 else
1535 cupsdSendIPPString(IPP_TAG_KEYWORD, "ppd-type", PPDTypes[ppd->record.type]);
1536 }
1537
1538 if (send_model_number)
1539 cupsdSendIPPInteger(IPP_TAG_INTEGER, "ppd-model-number",
1540 ppd->record.model_number);
1541 }
1542 else
1543 printf("%s (%s)\n", ppd->record.name, ppd->record.make_and_model);
1544
1545 /*
1546 * If we have only requested the ppd-make attribute, then skip
1547 * the remaining PPDs with this make...
1548 */
1549
1550 if (cupsArrayFind(requested, (void *)"ppd-make") &&
1551 cupsArrayCount(requested) == 1)
1552 {
1553 const char *this_make; /* This ppd-make */
1554
1555
1556 for (this_make = ppd->record.make,
1557 ppd = (ppd_info_t *)cupsArrayNext(matches);
1558 ppd;
1559 ppd = (ppd_info_t *)cupsArrayNext(matches))
1560 if (_cups_strcasecmp(this_make, ppd->record.make))
1561 break;
1562
1563 cupsArrayPrev(matches);
1564 }
1565 }
1566
1567 if (!sent_header && request_id)
1568 {
1569 cupsdSendIPPHeader(IPP_NOT_FOUND, request_id);
1570 cupsdSendIPPGroup(IPP_TAG_OPERATION);
1571 cupsdSendIPPString(IPP_TAG_CHARSET, "attributes-charset", "utf-8");
1572 cupsdSendIPPString(IPP_TAG_LANGUAGE, "attributes-natural-language", "en-US");
1573 }
1574
1575 if (request_id)
1576 cupsdSendIPPTrailer();
1577
1578 exit(0);
1579 }
1580
1581
1582 /*
1583 * 'load_drv()' - Load the PPDs from a driver information file.
1584 */
1585
1586 static int /* O - 1 on success, 0 on failure */
1587 load_drv(const char *filename, /* I - Actual filename */
1588 const char *name, /* I - Name to the rest of the world */
1589 cups_file_t *fp, /* I - File to read from */
1590 time_t mtime, /* I - Mod time of driver info file */
1591 off_t size) /* I - Size of driver info file */
1592 {
1593 ppdcSource *src; // Driver information file
1594 ppdcDriver *d; // Current driver
1595 ppdcAttr *device_id, // 1284DeviceID attribute
1596 *product, // Current product value
1597 *ps_version, // PSVersion attribute
1598 *cups_fax, // cupsFax attribute
1599 *nick_name; // NickName attribute
1600 ppdcFilter *filter; // Current filter
1601 ppd_info_t *ppd; // Current PPD
1602 int products_found; // Number of products found
1603 char uri[1024], // Driver URI
1604 make_model[1024]; // Make and model
1605 int type; // Driver type
1606
1607
1608 /*
1609 * Load the driver info file...
1610 */
1611
1612 src = new ppdcSource(filename, fp);
1613
1614 if (src->drivers->count == 0)
1615 {
1616 fprintf(stderr,
1617 "ERROR: [cups-driverd] Bad driver information file \"%s\"!\n",
1618 filename);
1619 src->release();
1620 return (0);
1621 }
1622
1623 /*
1624 * Add a dummy entry for the file...
1625 */
1626
1627 add_ppd(name, name, "", "", "", "", "", "", mtime, (size_t)size, 0, PPD_TYPE_DRV, "drv");
1628 ChangedPPD = 1;
1629
1630 /*
1631 * Then the drivers in the file...
1632 */
1633
1634 for (d = (ppdcDriver *)src->drivers->first();
1635 d;
1636 d = (ppdcDriver *)src->drivers->next())
1637 {
1638 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "drv", "", "", 0,
1639 "/%s/%s", name,
1640 d->file_name ? d->file_name->value :
1641 d->pc_file_name->value);
1642
1643 device_id = d->find_attr("1284DeviceID", NULL);
1644 ps_version = d->find_attr("PSVersion", NULL);
1645 nick_name = d->find_attr("NickName", NULL);
1646
1647 if (nick_name)
1648 strlcpy(make_model, nick_name->value->value, sizeof(make_model));
1649 else if (_cups_strncasecmp(d->model_name->value, d->manufacturer->value,
1650 strlen(d->manufacturer->value)))
1651 snprintf(make_model, sizeof(make_model), "%s %s, %s",
1652 d->manufacturer->value, d->model_name->value,
1653 d->version->value);
1654 else
1655 snprintf(make_model, sizeof(make_model), "%s, %s", d->model_name->value,
1656 d->version->value);
1657
1658 if ((cups_fax = d->find_attr("cupsFax", NULL)) != NULL &&
1659 !_cups_strcasecmp(cups_fax->value->value, "true"))
1660 type = PPD_TYPE_FAX;
1661 else if (d->type == PPDC_DRIVER_PS)
1662 type = PPD_TYPE_POSTSCRIPT;
1663 else if (d->type != PPDC_DRIVER_CUSTOM)
1664 type = PPD_TYPE_RASTER;
1665 else
1666 {
1667 for (filter = (ppdcFilter *)d->filters->first(),
1668 type = PPD_TYPE_POSTSCRIPT;
1669 filter;
1670 filter = (ppdcFilter *)d->filters->next())
1671 if (_cups_strcasecmp(filter->mime_type->value, "application/vnd.cups-raster"))
1672 type = PPD_TYPE_RASTER;
1673 else if (_cups_strcasecmp(filter->mime_type->value,
1674 "application/vnd.cups-pdf"))
1675 type = PPD_TYPE_PDF;
1676 }
1677
1678 for (product = (ppdcAttr *)d->attrs->first(), products_found = 0,
1679 ppd = NULL;
1680 product;
1681 product = (ppdcAttr *)d->attrs->next())
1682 if (!strcmp(product->name->value, "Product"))
1683 {
1684 if (!products_found)
1685 ppd = add_ppd(name, uri, "en", d->manufacturer->value, make_model, device_id ? device_id->value->value : "", product->value->value,
1686 ps_version ? ps_version->value->value : "(3010) 0", mtime, (size_t)size, d->model_number, type, "drv");
1687 else if (products_found < PPD_MAX_PROD)
1688 strlcpy(ppd->record.products[products_found], product->value->value, sizeof(ppd->record.products[0]));
1689 else
1690 break;
1691
1692 products_found ++;
1693 }
1694
1695 if (!products_found)
1696 add_ppd(name, uri, "en", d->manufacturer->value, make_model, device_id ? device_id->value->value : "", d->model_name->value, ps_version ? ps_version->value->value : "(3010) 0", mtime, (size_t)size, d->model_number, type, "drv");
1697 }
1698
1699 src->release();
1700
1701 return (1);
1702 }
1703
1704
1705 /*
1706 * 'load_drivers()' - Load driver-generated PPD files.
1707 */
1708
1709 static int /* O - 1 on success, 0 on failure */
1710 load_drivers(cups_array_t *include, /* I - Drivers to include */
1711 cups_array_t *exclude) /* I - Drivers to exclude */
1712 {
1713 int i; /* Looping var */
1714 char *start, /* Start of value */
1715 *ptr; /* Pointer into string */
1716 const char *server_bin, /* CUPS_SERVERBIN env variable */
1717 *scheme, /* Scheme for this driver */
1718 *scheme_end; /* Pointer to end of scheme */
1719 char drivers[1024]; /* Location of driver programs */
1720 int pid; /* Process ID for driver program */
1721 cups_file_t *fp; /* Pipe to driver program */
1722 cups_dir_t *dir; /* Directory pointer */
1723 cups_dentry_t *dent; /* Directory entry */
1724 char *argv[3], /* Arguments for command */
1725 filename[1024], /* Name of driver */
1726 line[2048], /* Line from driver */
1727 name[256], /* ppd-name */
1728 make[128], /* ppd-make */
1729 make_and_model[128], /* ppd-make-and-model */
1730 device_id[256], /* ppd-device-id */
1731 languages[128], /* ppd-natural-language */
1732 product[128], /* ppd-product */
1733 psversion[128], /* ppd-psversion */
1734 type_str[128]; /* ppd-type */
1735 int type; /* PPD type */
1736 ppd_info_t *ppd; /* Newly added PPD */
1737
1738
1739 /*
1740 * Try opening the driver directory...
1741 */
1742
1743 if ((server_bin = getenv("CUPS_SERVERBIN")) == NULL)
1744 server_bin = CUPS_SERVERBIN;
1745
1746 snprintf(drivers, sizeof(drivers), "%s/driver", server_bin);
1747
1748 if ((dir = cupsDirOpen(drivers)) == NULL)
1749 {
1750 fprintf(stderr, "ERROR: [cups-driverd] Unable to open driver directory "
1751 "\"%s\": %s\n",
1752 drivers, strerror(errno));
1753 return (0);
1754 }
1755
1756 /*
1757 * Loop through all of the device drivers...
1758 */
1759
1760 argv[1] = (char *)"list";
1761 argv[2] = NULL;
1762
1763 while ((dent = cupsDirRead(dir)) != NULL)
1764 {
1765 /*
1766 * Only look at executable files...
1767 */
1768
1769 if (!(dent->fileinfo.st_mode & 0111) || !S_ISREG(dent->fileinfo.st_mode))
1770 continue;
1771
1772 /*
1773 * Include/exclude specific drivers...
1774 */
1775
1776 if (exclude)
1777 {
1778 /*
1779 * Look for "scheme" or "scheme*" (prefix match), and skip any matches.
1780 */
1781
1782 for (scheme = (char *)cupsArrayFirst(exclude);
1783 scheme;
1784 scheme = (char *)cupsArrayNext(exclude))
1785 {
1786 fprintf(stderr, "DEBUG: [cups-driverd] Exclude \"%s\" with \"%s\"?\n",
1787 dent->filename, scheme);
1788 scheme_end = scheme + strlen(scheme) - 1;
1789
1790 if ((scheme_end > scheme && *scheme_end == '*' &&
1791 !strncmp(scheme, dent->filename, (size_t)(scheme_end - scheme))) ||
1792 !strcmp(scheme, dent->filename))
1793 {
1794 fputs("DEBUG: [cups-driverd] Yes, exclude!\n", stderr);
1795 break;
1796 }
1797 }
1798
1799 if (scheme)
1800 continue;
1801 }
1802
1803 if (include)
1804 {
1805 /*
1806 * Look for "scheme" or "scheme*" (prefix match), and skip any non-matches.
1807 */
1808
1809 for (scheme = (char *)cupsArrayFirst(include);
1810 scheme;
1811 scheme = (char *)cupsArrayNext(include))
1812 {
1813 fprintf(stderr, "DEBUG: [cups-driverd] Include \"%s\" with \"%s\"?\n",
1814 dent->filename, scheme);
1815 scheme_end = scheme + strlen(scheme) - 1;
1816
1817 if ((scheme_end > scheme && *scheme_end == '*' &&
1818 !strncmp(scheme, dent->filename, (size_t)(scheme_end - scheme))) ||
1819 !strcmp(scheme, dent->filename))
1820 {
1821 fputs("DEBUG: [cups-driverd] Yes, include!\n", stderr);
1822 break;
1823 }
1824 }
1825
1826 if (!scheme)
1827 continue;
1828 }
1829 else
1830 scheme = dent->filename;
1831
1832 /*
1833 * Run the driver with no arguments and collect the output...
1834 */
1835
1836 snprintf(filename, sizeof(filename), "%s/%s", drivers, dent->filename);
1837
1838 if (_cupsFileCheck(filename, _CUPS_FILE_CHECK_PROGRAM, !geteuid(),
1839 _cupsFileCheckFilter, NULL))
1840 continue;
1841
1842 argv[0] = dent->filename;
1843
1844 if ((fp = cupsdPipeCommand(&pid, filename, argv, 0)) != NULL)
1845 {
1846 while (cupsFileGets(fp, line, sizeof(line)))
1847 {
1848 /*
1849 * Each line is of the form:
1850 *
1851 * "ppd-name" ppd-natural-language "ppd-make" "ppd-make-and-model" \
1852 * "ppd-device-id" "ppd-product" "ppd-psversion"
1853 */
1854
1855 device_id[0] = '\0';
1856 product[0] = '\0';
1857 psversion[0] = '\0';
1858 strlcpy(type_str, "postscript", sizeof(type_str));
1859
1860 if (sscanf(line, "\"%255[^\"]\"%127s%*[ \t]\"%127[^\"]\""
1861 "%*[ \t]\"%127[^\"]\"%*[ \t]\"%255[^\"]\""
1862 "%*[ \t]\"%127[^\"]\"%*[ \t]\"%127[^\"]\""
1863 "%*[ \t]\"%127[^\"]\"",
1864 name, languages, make, make_and_model,
1865 device_id, product, psversion, type_str) < 4)
1866 {
1867 /*
1868 * Bad format; strip trailing newline and write an error message.
1869 */
1870
1871 if (line[strlen(line) - 1] == '\n')
1872 line[strlen(line) - 1] = '\0';
1873
1874 fprintf(stderr, "ERROR: [cups-driverd] Bad line from \"%s\": %s\n",
1875 dent->filename, line);
1876 break;
1877 }
1878 else
1879 {
1880 /*
1881 * Add the device to the array of available devices...
1882 */
1883
1884 if ((start = strchr(languages, ',')) != NULL)
1885 *start++ = '\0';
1886
1887 for (type = 0;
1888 type < (int)(sizeof(PPDTypes) / sizeof(PPDTypes[0]));
1889 type ++)
1890 if (!strcmp(type_str, PPDTypes[type]))
1891 break;
1892
1893 if (type >= (int)(sizeof(PPDTypes) / sizeof(PPDTypes[0])))
1894 {
1895 fprintf(stderr,
1896 "ERROR: [cups-driverd] Bad ppd-type \"%s\" ignored!\n",
1897 type_str);
1898 type = PPD_TYPE_UNKNOWN;
1899 }
1900
1901 ppd = add_ppd(filename, name, languages, make, make_and_model,
1902 device_id, product, psversion, 0, 0, 0, type, scheme);
1903
1904 if (!ppd)
1905 {
1906 cupsDirClose(dir);
1907 cupsFileClose(fp);
1908 return (0);
1909 }
1910
1911 if (start && *start)
1912 {
1913 for (i = 1; i < PPD_MAX_LANG && *start; i ++)
1914 {
1915 if ((ptr = strchr(start, ',')) != NULL)
1916 *ptr++ = '\0';
1917 else
1918 ptr = start + strlen(start);
1919
1920 strlcpy(ppd->record.languages[i], start,
1921 sizeof(ppd->record.languages[0]));
1922
1923 start = ptr;
1924 }
1925 }
1926
1927 fprintf(stderr, "DEBUG2: [cups-driverd] Added dynamic PPD \"%s\"...\n",
1928 name);
1929 }
1930 }
1931
1932 cupsFileClose(fp);
1933 }
1934 else
1935 fprintf(stderr, "WARNING: [cups-driverd] Unable to execute \"%s\": %s\n",
1936 filename, strerror(errno));
1937 }
1938
1939 cupsDirClose(dir);
1940
1941 return (1);
1942 }
1943
1944
1945 /*
1946 * 'load_ppd()' - Load a PPD file.
1947 */
1948
1949 static void
1950 load_ppd(const char *filename, /* I - Real filename */
1951 const char *name, /* I - Virtual filename */
1952 const char *scheme, /* I - PPD scheme */
1953 struct stat *fileinfo, /* I - File information */
1954 ppd_info_t *ppd, /* I - Existing PPD file or NULL */
1955 cups_file_t *fp, /* I - File to read from */
1956 off_t end) /* I - End of file position or 0 */
1957 {
1958 int i; /* Looping var */
1959 char line[256], /* Line from file */
1960 *ptr, /* Pointer into line */
1961 lang_version[64], /* PPD LanguageVersion */
1962 lang_encoding[64], /* PPD LanguageEncoding */
1963 country[64], /* Country code */
1964 manufacturer[256], /* Manufacturer */
1965 make_model[256], /* Make and Model */
1966 model_name[256], /* ModelName */
1967 nick_name[256], /* NickName */
1968 device_id[256], /* 1284DeviceID */
1969 product[256], /* Product */
1970 psversion[256], /* PSVersion */
1971 temp[512]; /* Temporary make and model */
1972 int install_group, /* In the installable options group? */
1973 model_number, /* cupsModelNumber */
1974 type; /* ppd-type */
1975 cups_array_t *products, /* Product array */
1976 *psversions, /* PSVersion array */
1977 *cups_languages; /* cupsLanguages array */
1978 int new_ppd; /* Is this a new PPD? */
1979 struct /* LanguageVersion translation table */
1980 {
1981 const char *version, /* LanguageVersion string */
1982 *language; /* Language code */
1983 } languages[] =
1984 {
1985 { "chinese", "zh" },
1986 { "czech", "cs" },
1987 { "danish", "da" },
1988 { "dutch", "nl" },
1989 { "english", "en" },
1990 { "finnish", "fi" },
1991 { "french", "fr" },
1992 { "german", "de" },
1993 { "greek", "el" },
1994 { "hungarian", "hu" },
1995 { "italian", "it" },
1996 { "japanese", "ja" },
1997 { "korean", "ko" },
1998 { "norwegian", "no" },
1999 { "polish", "pl" },
2000 { "portuguese", "pt" },
2001 { "russian", "ru" },
2002 { "simplified chinese", "zh_CN" },
2003 { "slovak", "sk" },
2004 { "spanish", "es" },
2005 { "swedish", "sv" },
2006 { "traditional chinese", "zh_TW" },
2007 { "turkish", "tr" }
2008 };
2009
2010
2011 /*
2012 * Now read until we get the required fields...
2013 */
2014
2015 cups_languages = cupsArrayNew(NULL, NULL);
2016 products = cupsArrayNew(NULL, NULL);
2017 psversions = cupsArrayNew(NULL, NULL);
2018
2019 model_name[0] = '\0';
2020 nick_name[0] = '\0';
2021 manufacturer[0] = '\0';
2022 device_id[0] = '\0';
2023 lang_encoding[0] = '\0';
2024 strlcpy(lang_version, "en", sizeof(lang_version));
2025 model_number = 0;
2026 install_group = 0;
2027 type = PPD_TYPE_POSTSCRIPT;
2028
2029 while ((end == 0 || cupsFileTell(fp) < end) &&
2030 cupsFileGets(fp, line, sizeof(line)))
2031 {
2032 if (!strncmp(line, "*Manufacturer:", 14))
2033 sscanf(line, "%*[^\"]\"%255[^\"]", manufacturer);
2034 else if (!strncmp(line, "*ModelName:", 11))
2035 sscanf(line, "%*[^\"]\"%127[^\"]", model_name);
2036 else if (!strncmp(line, "*LanguageEncoding:", 18))
2037 sscanf(line, "%*[^:]:%63s", lang_encoding);
2038 else if (!strncmp(line, "*LanguageVersion:", 17))
2039 sscanf(line, "%*[^:]:%63s", lang_version);
2040 else if (!strncmp(line, "*NickName:", 10))
2041 sscanf(line, "%*[^\"]\"%255[^\"]", nick_name);
2042 else if (!_cups_strncasecmp(line, "*1284DeviceID:", 14))
2043 {
2044 sscanf(line, "%*[^\"]\"%255[^\"]", device_id);
2045
2046 // Make sure device ID ends with a semicolon...
2047 if (device_id[0] && device_id[strlen(device_id) - 1] != ';')
2048 strlcat(device_id, ";", sizeof(device_id));
2049 }
2050 else if (!strncmp(line, "*Product:", 9))
2051 {
2052 if (sscanf(line, "%*[^\"]\"(%255[^\"]", product) == 1)
2053 {
2054 /*
2055 * Make sure the value ends with a right parenthesis - can't stop at
2056 * the first right paren since the product name may contain escaped
2057 * parenthesis...
2058 */
2059
2060 ptr = product + strlen(product) - 1;
2061 if (ptr > product && *ptr == ')')
2062 {
2063 /*
2064 * Yes, ends with a parenthesis, so remove it from the end and
2065 * add the product to the list...
2066 */
2067
2068 *ptr = '\0';
2069 cupsArrayAdd(products, strdup(product));
2070 }
2071 }
2072 }
2073 else if (!strncmp(line, "*PSVersion:", 11))
2074 {
2075 sscanf(line, "%*[^\"]\"%255[^\"]", psversion);
2076 cupsArrayAdd(psversions, strdup(psversion));
2077 }
2078 else if (!strncmp(line, "*cupsLanguages:", 15))
2079 {
2080 char *start; /* Start of language */
2081
2082
2083 for (start = line + 15; *start && isspace(*start & 255); start ++);
2084
2085 if (*start++ == '\"')
2086 {
2087 while (*start)
2088 {
2089 for (ptr = start + 1;
2090 *ptr && *ptr != '\"' && !isspace(*ptr & 255);
2091 ptr ++);
2092
2093 if (*ptr)
2094 {
2095 *ptr++ = '\0';
2096
2097 while (isspace(*ptr & 255))
2098 *ptr++ = '\0';
2099 }
2100
2101 cupsArrayAdd(cups_languages, strdup(start));
2102 start = ptr;
2103 }
2104 }
2105 }
2106 else if (!strncmp(line, "*cupsFax:", 9))
2107 {
2108 for (ptr = line + 9; isspace(*ptr & 255); ptr ++);
2109
2110 if (!_cups_strncasecmp(ptr, "true", 4))
2111 type = PPD_TYPE_FAX;
2112 }
2113 else if ((!strncmp(line, "*cupsFilter:", 12) || !strncmp(line, "*cupsFilter2:", 13)) && type == PPD_TYPE_POSTSCRIPT)
2114 {
2115 if (strstr(line + 12, "application/vnd.cups-raster"))
2116 type = PPD_TYPE_RASTER;
2117 else if (strstr(line + 12, "application/vnd.cups-pdf"))
2118 type = PPD_TYPE_PDF;
2119 }
2120 else if (!strncmp(line, "*cupsModelNumber:", 17))
2121 sscanf(line, "*cupsModelNumber:%d", &model_number);
2122 else if (!strncmp(line, "*OpenGroup: Installable", 23))
2123 install_group = 1;
2124 else if (!strncmp(line, "*CloseGroup:", 12))
2125 install_group = 0;
2126 else if (!strncmp(line, "*OpenUI", 7))
2127 {
2128 /*
2129 * Stop early if we have a NickName or ModelName attributes
2130 * before the first non-installable OpenUI...
2131 */
2132
2133 if (!install_group && (model_name[0] || nick_name[0]) &&
2134 cupsArrayCount(products) > 0 && cupsArrayCount(psversions) > 0)
2135 break;
2136 }
2137 }
2138
2139 /*
2140 * See if we got all of the required info...
2141 */
2142
2143 if (nick_name[0])
2144 cupsCharsetToUTF8((cups_utf8_t *)make_model, nick_name,
2145 sizeof(make_model), _ppdGetEncoding(lang_encoding));
2146 else
2147 strlcpy(make_model, model_name, sizeof(make_model));
2148
2149 while (isspace(make_model[0] & 255))
2150 _cups_strcpy(make_model, make_model + 1);
2151
2152 if (!make_model[0] || cupsArrayCount(products) == 0 ||
2153 cupsArrayCount(psversions) == 0)
2154 {
2155 /*
2156 * We don't have all the info needed, so skip this file...
2157 */
2158
2159 if (!make_model[0])
2160 fprintf(stderr, "WARNING: Missing NickName and ModelName in %s!\n",
2161 filename);
2162
2163 if (cupsArrayCount(products) == 0)
2164 fprintf(stderr, "WARNING: Missing Product in %s!\n", filename);
2165
2166 if (cupsArrayCount(psversions) == 0)
2167 fprintf(stderr, "WARNING: Missing PSVersion in %s!\n", filename);
2168
2169 free_array(products);
2170 free_array(psversions);
2171 free_array(cups_languages);
2172
2173 return;
2174 }
2175
2176 if (model_name[0])
2177 cupsArrayAdd(products, strdup(model_name));
2178
2179 /*
2180 * Normalize the make and model string...
2181 */
2182
2183 while (isspace(manufacturer[0] & 255))
2184 _cups_strcpy(manufacturer, manufacturer + 1);
2185
2186 if (!_cups_strncasecmp(make_model, manufacturer, strlen(manufacturer)))
2187 strlcpy(temp, make_model, sizeof(temp));
2188 else
2189 snprintf(temp, sizeof(temp), "%s %s", manufacturer, make_model);
2190
2191 _ppdNormalizeMakeAndModel(temp, make_model, sizeof(make_model));
2192
2193 /*
2194 * See if we got a manufacturer...
2195 */
2196
2197 if (!manufacturer[0] || !strcmp(manufacturer, "ESP"))
2198 {
2199 /*
2200 * Nope, copy the first part of the make and model then...
2201 */
2202
2203 strlcpy(manufacturer, make_model, sizeof(manufacturer));
2204
2205 /*
2206 * Truncate at the first space, dash, or slash, or make the
2207 * manufacturer "Other"...
2208 */
2209
2210 for (ptr = manufacturer; *ptr; ptr ++)
2211 if (*ptr == ' ' || *ptr == '-' || *ptr == '/')
2212 break;
2213
2214 if (*ptr && ptr > manufacturer)
2215 *ptr = '\0';
2216 else
2217 strlcpy(manufacturer, "Other", sizeof(manufacturer));
2218 }
2219 else if (!_cups_strncasecmp(manufacturer, "LHAG", 4) ||
2220 !_cups_strncasecmp(manufacturer, "linotype", 8))
2221 strlcpy(manufacturer, "LHAG", sizeof(manufacturer));
2222 else if (!_cups_strncasecmp(manufacturer, "Hewlett", 7))
2223 strlcpy(manufacturer, "HP", sizeof(manufacturer));
2224
2225 /*
2226 * Fix the lang_version as needed...
2227 */
2228
2229 if ((ptr = strchr(lang_version, '-')) != NULL)
2230 *ptr++ = '\0';
2231 else if ((ptr = strchr(lang_version, '_')) != NULL)
2232 *ptr++ = '\0';
2233
2234 if (ptr)
2235 {
2236 /*
2237 * Setup the country suffix...
2238 */
2239
2240 country[0] = '_';
2241 _cups_strcpy(country + 1, ptr);
2242 }
2243 else
2244 {
2245 /*
2246 * No country suffix...
2247 */
2248
2249 country[0] = '\0';
2250 }
2251
2252 for (i = 0; i < (int)(sizeof(languages) / sizeof(languages[0])); i ++)
2253 if (!_cups_strcasecmp(languages[i].version, lang_version))
2254 break;
2255
2256 if (i < (int)(sizeof(languages) / sizeof(languages[0])))
2257 {
2258 /*
2259 * Found a known language...
2260 */
2261
2262 snprintf(lang_version, sizeof(lang_version), "%s%s",
2263 languages[i].language, country);
2264 }
2265 else
2266 {
2267 /*
2268 * Unknown language; use "xx"...
2269 */
2270
2271 strlcpy(lang_version, "xx", sizeof(lang_version));
2272 }
2273
2274 /*
2275 * Record the PPD file...
2276 */
2277
2278 new_ppd = !ppd;
2279
2280 if (new_ppd)
2281 {
2282 /*
2283 * Add new PPD file...
2284 */
2285
2286 fprintf(stderr, "DEBUG2: [cups-driverd] Adding ppd \"%s\"...\n", name);
2287
2288 ppd = add_ppd(name, name, lang_version, manufacturer, make_model, device_id, (char *)cupsArrayFirst(products), (char *)cupsArrayFirst(psversions), fileinfo->st_mtime, (size_t)fileinfo->st_size, model_number, type, scheme);
2289
2290 if (!ppd)
2291 return;
2292 }
2293 else
2294 {
2295 /*
2296 * Update existing record...
2297 */
2298
2299 fprintf(stderr, "DEBUG2: [cups-driverd] Updating ppd \"%s\"...\n", name);
2300
2301 memset(ppd, 0, sizeof(ppd_info_t));
2302
2303 ppd->found = 1;
2304 ppd->record.mtime = fileinfo->st_mtime;
2305 ppd->record.size = fileinfo->st_size;
2306 ppd->record.model_number = model_number;
2307 ppd->record.type = type;
2308
2309 strlcpy(ppd->record.filename, name, sizeof(ppd->record.filename));
2310 strlcpy(ppd->record.name, name, sizeof(ppd->record.name));
2311 strlcpy(ppd->record.languages[0], lang_version,
2312 sizeof(ppd->record.languages[0]));
2313 strlcpy(ppd->record.products[0], (char *)cupsArrayFirst(products),
2314 sizeof(ppd->record.products[0]));
2315 strlcpy(ppd->record.psversions[0], (char *)cupsArrayFirst(psversions),
2316 sizeof(ppd->record.psversions[0]));
2317 strlcpy(ppd->record.make, manufacturer, sizeof(ppd->record.make));
2318 strlcpy(ppd->record.make_and_model, make_model,
2319 sizeof(ppd->record.make_and_model));
2320 strlcpy(ppd->record.device_id, device_id, sizeof(ppd->record.device_id));
2321 strlcpy(ppd->record.scheme, scheme, sizeof(ppd->record.scheme));
2322 }
2323
2324 /*
2325 * Add remaining products, versions, and languages...
2326 */
2327
2328 for (i = 1;
2329 i < PPD_MAX_PROD && (ptr = (char *)cupsArrayNext(products)) != NULL;
2330 i ++)
2331 strlcpy(ppd->record.products[i], ptr,
2332 sizeof(ppd->record.products[0]));
2333
2334 for (i = 1;
2335 i < PPD_MAX_VERS && (ptr = (char *)cupsArrayNext(psversions)) != NULL;
2336 i ++)
2337 strlcpy(ppd->record.psversions[i], ptr,
2338 sizeof(ppd->record.psversions[0]));
2339
2340 for (i = 1, ptr = (char *)cupsArrayFirst(cups_languages);
2341 i < PPD_MAX_LANG && ptr;
2342 i ++, ptr = (char *)cupsArrayNext(cups_languages))
2343 strlcpy(ppd->record.languages[i], ptr,
2344 sizeof(ppd->record.languages[0]));
2345
2346 /*
2347 * Free products, versions, and languages...
2348 */
2349
2350 free_array(cups_languages);
2351 free_array(products);
2352 free_array(psversions);
2353
2354 ChangedPPD = 1;
2355 }
2356
2357
2358 /*
2359 * 'load_ppds()' - Load PPD files recursively.
2360 */
2361
2362 static int /* O - 1 on success, 0 on failure */
2363 load_ppds(const char *d, /* I - Actual directory */
2364 const char *p, /* I - Virtual path in name */
2365 int descend) /* I - Descend into directories? */
2366 {
2367 struct stat dinfo, /* Directory information */
2368 *dinfoptr; /* Pointer to match */
2369 cups_file_t *fp; /* Pointer to file */
2370 cups_dir_t *dir; /* Directory pointer */
2371 cups_dentry_t *dent; /* Directory entry */
2372 char filename[1024], /* Name of PPD or directory */
2373 line[256], /* Line from file */
2374 *ptr, /* Pointer into name */
2375 name[256]; /* Name of PPD file */
2376 ppd_info_t *ppd, /* New PPD file */
2377 key; /* Search key */
2378
2379
2380 /*
2381 * See if we've loaded this directory before...
2382 */
2383
2384 if (stat(d, &dinfo))
2385 {
2386 if (errno != ENOENT)
2387 fprintf(stderr, "ERROR: [cups-driverd] Unable to stat \"%s\": %s\n", d,
2388 strerror(errno));
2389
2390 return (0);
2391 }
2392 else if (cupsArrayFind(Inodes, &dinfo))
2393 {
2394 fprintf(stderr, "ERROR: [cups-driverd] Skipping \"%s\": loop detected!\n",
2395 d);
2396 return (1);
2397 }
2398
2399 /*
2400 * Nope, add it to the Inodes array and continue...
2401 */
2402
2403 dinfoptr = (struct stat *)malloc(sizeof(struct stat));
2404 memcpy(dinfoptr, &dinfo, sizeof(struct stat));
2405 cupsArrayAdd(Inodes, dinfoptr);
2406
2407 /*
2408 * Check permissions...
2409 */
2410
2411 if (_cupsFileCheck(d, _CUPS_FILE_CHECK_DIRECTORY, !geteuid(),
2412 _cupsFileCheckFilter, NULL))
2413 return (0);
2414
2415 if ((dir = cupsDirOpen(d)) == NULL)
2416 {
2417 if (errno != ENOENT)
2418 fprintf(stderr,
2419 "ERROR: [cups-driverd] Unable to open PPD directory \"%s\": %s\n",
2420 d, strerror(errno));
2421
2422 return (0);
2423 }
2424
2425 fprintf(stderr, "DEBUG: [cups-driverd] Loading \"%s\"...\n", d);
2426
2427 while ((dent = cupsDirRead(dir)) != NULL)
2428 {
2429 /*
2430 * Skip files/directories starting with "."...
2431 */
2432
2433 if (dent->filename[0] == '.')
2434 continue;
2435
2436 /*
2437 * See if this is a file...
2438 */
2439
2440 snprintf(filename, sizeof(filename), "%s/%s", d, dent->filename);
2441
2442 if (p[0])
2443 snprintf(name, sizeof(name), "%s/%s", p, dent->filename);
2444 else
2445 strlcpy(name, dent->filename, sizeof(name));
2446
2447 if (S_ISDIR(dent->fileinfo.st_mode))
2448 {
2449 /*
2450 * Do subdirectory...
2451 */
2452
2453 if (descend)
2454 {
2455 if (!load_ppds(filename, name, 1))
2456 {
2457 cupsDirClose(dir);
2458 return (1);
2459 }
2460 }
2461 else if ((ptr = filename + strlen(filename) - 14) > filename &&
2462 !strcmp(ptr, ".printerDriver"))
2463 {
2464 /*
2465 * Load PPDs in a printer driver bundle.
2466 */
2467
2468 if (_cupsFileCheck(filename, _CUPS_FILE_CHECK_DIRECTORY, !geteuid(),
2469 _cupsFileCheckFilter, NULL))
2470 continue;
2471
2472 strlcat(filename, "/Contents/Resources/PPDs", sizeof(filename));
2473 strlcat(name, "/Contents/Resources/PPDs", sizeof(name));
2474
2475 load_ppds(filename, name, 0);
2476 }
2477
2478 continue;
2479 }
2480 else if (strstr(filename, ".plist"))
2481 {
2482 /*
2483 * Skip plist files in the PPDs directory...
2484 */
2485
2486 continue;
2487 }
2488 else if (_cupsFileCheck(filename, _CUPS_FILE_CHECK_FILE_ONLY, !geteuid(),
2489 _cupsFileCheckFilter, NULL))
2490 continue;
2491
2492 /*
2493 * See if this file has been scanned before...
2494 */
2495
2496 strlcpy(key.record.filename, name, sizeof(key.record.filename));
2497 strlcpy(key.record.name, name, sizeof(key.record.name));
2498
2499 ppd = (ppd_info_t *)cupsArrayFind(PPDsByName, &key);
2500
2501 if (ppd &&
2502 ppd->record.size == dent->fileinfo.st_size &&
2503 ppd->record.mtime == dent->fileinfo.st_mtime)
2504 {
2505 /*
2506 * Rewind to the first entry for this file...
2507 */
2508
2509 while ((ppd = (ppd_info_t *)cupsArrayPrev(PPDsByName)) != NULL &&
2510 !strcmp(ppd->record.filename, name));
2511
2512 /*
2513 * Then mark all of the matches for this file as found...
2514 */
2515
2516 while ((ppd = (ppd_info_t *)cupsArrayNext(PPDsByName)) != NULL &&
2517 !strcmp(ppd->record.filename, name))
2518 ppd->found = 1;
2519
2520 continue;
2521 }
2522
2523 /*
2524 * No, file is new/changed, so re-scan it...
2525 */
2526
2527 if ((fp = cupsFileOpen(filename, "r")) == NULL)
2528 continue;
2529
2530 /*
2531 * Now see if this is a PPD file...
2532 */
2533
2534 line[0] = '\0';
2535 cupsFileGets(fp, line, sizeof(line));
2536
2537 if (!strncmp(line, "*PPD-Adobe:", 11))
2538 {
2539 /*
2540 * Yes, load it...
2541 */
2542
2543 load_ppd(filename, name, "file", &dent->fileinfo, ppd, fp, 0);
2544 }
2545 else
2546 {
2547 /*
2548 * Nope, treat it as a driver information file or archive...
2549 */
2550
2551 cupsFileRewind(fp);
2552
2553 if ((ptr = strstr(filename, ".tar")) != NULL &&
2554 (!strcmp(ptr, ".tar") || !strcmp(ptr, ".tar.gz")))
2555 load_tar(filename, name, fp, dent->fileinfo.st_mtime,
2556 dent->fileinfo.st_size);
2557 else
2558 load_drv(filename, name, fp, dent->fileinfo.st_mtime,
2559 dent->fileinfo.st_size);
2560 }
2561
2562 /*
2563 * Close the file...
2564 */
2565
2566 cupsFileClose(fp);
2567 }
2568
2569 cupsDirClose(dir);
2570
2571 return (1);
2572 }
2573
2574
2575 /*
2576 * 'load_ppds_dat()' - Load the ppds.dat file.
2577 */
2578
2579 static void
2580 load_ppds_dat(char *filename, /* I - Filename buffer */
2581 size_t filesize, /* I - Size of filename buffer */
2582 int verbose) /* I - Be verbose? */
2583 {
2584 ppd_info_t *ppd; /* Current PPD file */
2585 cups_file_t *fp; /* ppds.dat file */
2586 struct stat fileinfo; /* ppds.dat information */
2587 const char *cups_cachedir; /* CUPS_CACHEDIR environment variable */
2588
2589
2590 PPDsByName = cupsArrayNew((cups_array_func_t)compare_names, NULL);
2591 PPDsByMakeModel = cupsArrayNew((cups_array_func_t)compare_ppds, NULL);
2592 ChangedPPD = 0;
2593
2594 if (!filename[0])
2595 {
2596 if ((cups_cachedir = getenv("CUPS_CACHEDIR")) == NULL)
2597 cups_cachedir = CUPS_CACHEDIR;
2598
2599 snprintf(filename, filesize, "%s/ppds.dat", cups_cachedir);
2600 }
2601
2602 if ((fp = cupsFileOpen(filename, "r")) != NULL)
2603 {
2604 /*
2605 * See if we have the right sync word...
2606 */
2607
2608 unsigned ppdsync; /* Sync word */
2609 int num_ppds; /* Number of PPDs */
2610
2611 if ((size_t)cupsFileRead(fp, (char *)&ppdsync, sizeof(ppdsync)) == sizeof(ppdsync) &&
2612 ppdsync == PPD_SYNC &&
2613 !stat(filename, &fileinfo) &&
2614 (((size_t)fileinfo.st_size - sizeof(ppdsync)) % sizeof(ppd_rec_t)) == 0 &&
2615 (num_ppds = ((size_t)fileinfo.st_size - sizeof(ppdsync)) / sizeof(ppd_rec_t)) > 0)
2616 {
2617 /*
2618 * We have a ppds.dat file, so read it!
2619 */
2620
2621 for (; num_ppds > 0; num_ppds --)
2622 {
2623 if ((ppd = (ppd_info_t *)calloc(1, sizeof(ppd_info_t))) == NULL)
2624 {
2625 if (verbose)
2626 fputs("ERROR: [cups-driverd] Unable to allocate memory for PPD!\n",
2627 stderr);
2628 exit(1);
2629 }
2630
2631 if (cupsFileRead(fp, (char *)&(ppd->record), sizeof(ppd_rec_t)) > 0)
2632 {
2633 cupsArrayAdd(PPDsByName, ppd);
2634 cupsArrayAdd(PPDsByMakeModel, ppd);
2635 }
2636 else
2637 {
2638 free(ppd);
2639 break;
2640 }
2641 }
2642
2643 if (verbose)
2644 fprintf(stderr, "INFO: [cups-driverd] Read \"%s\", %d PPDs...\n",
2645 filename, cupsArrayCount(PPDsByName));
2646 }
2647
2648 cupsFileClose(fp);
2649 }
2650 }
2651
2652
2653 /*
2654 * 'load_tar()' - Load archived PPD files.
2655 */
2656
2657 static int /* O - 1 on success, 0 on failure */
2658 load_tar(const char *filename, /* I - Actual filename */
2659 const char *name, /* I - Name to the rest of the world */
2660 cups_file_t *fp, /* I - File to read from */
2661 time_t mtime, /* I - Mod time of driver info file */
2662 off_t size) /* I - Size of driver info file */
2663 {
2664 char curname[256], /* Current archive file name */
2665 uri[1024]; /* Virtual file URI */
2666 const char *curext; /* Extension on file */
2667 struct stat curinfo; /* Current archive file information */
2668 off_t next; /* Position for next header */
2669
2670
2671 /*
2672 * Add a dummy entry for the file...
2673 */
2674
2675 (void)filename;
2676
2677 add_ppd(name, name, "", "", "", "", "", "", mtime, (size_t)size, 0, PPD_TYPE_ARCHIVE, "file");
2678 ChangedPPD = 1;
2679
2680 /*
2681 * Scan for PPDs in the archive...
2682 */
2683
2684 while (read_tar(fp, curname, sizeof(curname), &curinfo))
2685 {
2686 next = cupsFileTell(fp) + ((curinfo.st_size + TAR_BLOCK - 1) &
2687 ~(TAR_BLOCK - 1));
2688
2689 if ((curext = strrchr(curname, '.')) != NULL &&
2690 !_cups_strcasecmp(curext, ".ppd"))
2691 {
2692 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "file", "", "",
2693 0, "/%s/%s", name, curname);
2694 load_ppd(name, uri, "file", &curinfo, NULL, fp, next);
2695 }
2696
2697 if (cupsFileTell(fp) != next)
2698 cupsFileSeek(fp, next);
2699 }
2700
2701 return (1);
2702 }
2703
2704
2705 /*
2706 * 'read_tar()' - Read a file header from an archive.
2707 *
2708 * This function skips all directories and special files.
2709 */
2710
2711 static int /* O - 1 if found, 0 on EOF */
2712 read_tar(cups_file_t *fp, /* I - Archive to read */
2713 char *name, /* I - Filename buffer */
2714 size_t namesize, /* I - Size of filename buffer */
2715 struct stat *info) /* O - File information */
2716 {
2717 tar_rec_t record; /* Record from file */
2718
2719
2720 while ((size_t)cupsFileRead(fp, (char *)&record, sizeof(record)) == sizeof(record))
2721 {
2722 /*
2723 * Check for a valid tar header...
2724 */
2725
2726 if (memcmp(record.header.magic, TAR_MAGIC, 6) ||
2727 memcmp(record.header.version, TAR_VERSION, 2))
2728 {
2729 if (record.header.magic[0] ||
2730 memcmp(record.header.magic, record.header.magic + 1, 5))
2731 fputs("ERROR: [cups-driverd] Bad tar magic/version.\n", stderr);
2732 break;
2733 }
2734
2735 /*
2736 * Ignore non-files...
2737 */
2738
2739 if (record.header.linkflag != TAR_OLDNORMAL &&
2740 record.header.linkflag != TAR_NORMAL)
2741 continue;
2742
2743 /*
2744 * Grab size and name from tar header and return...
2745 */
2746
2747 if (record.header.prefix[0])
2748 snprintf(name, namesize, "%s/%s", record.header.prefix,
2749 record.header.pathname);
2750 else
2751 strlcpy(name, record.header.pathname, namesize);
2752
2753 info->st_mtime = strtol(record.header.mtime, NULL, 8);
2754 info->st_size = strtoll(record.header.size, NULL, 8);
2755
2756 return (1);
2757 }
2758
2759 return (0);
2760 }
2761
2762
2763 /*
2764 * 'regex_device_id()' - Compile a regular expression based on the 1284 device
2765 * ID.
2766 */
2767
2768 static regex_t * /* O - Regular expression */
2769 regex_device_id(const char *device_id) /* I - IEEE-1284 device ID */
2770 {
2771 char res[2048], /* Regular expression string */
2772 *ptr; /* Pointer into string */
2773 regex_t *re; /* Regular expression */
2774 int cmd; /* Command set string? */
2775
2776
2777 fprintf(stderr, "DEBUG: [cups-driverd] regex_device_id(\"%s\")\n", device_id);
2778
2779 /*
2780 * Scan the device ID string and insert class, command set, manufacturer, and
2781 * model attributes to match. We assume that the device ID in the PPD and the
2782 * device ID reported by the device itself use the same attribute names and
2783 * order of attributes.
2784 */
2785
2786 ptr = res;
2787
2788 while (*device_id && ptr < (res + sizeof(res) - 6))
2789 {
2790 cmd = !_cups_strncasecmp(device_id, "COMMAND SET:", 12) ||
2791 !_cups_strncasecmp(device_id, "CMD:", 4);
2792
2793 if (cmd || !_cups_strncasecmp(device_id, "MANUFACTURER:", 13) ||
2794 !_cups_strncasecmp(device_id, "MFG:", 4) ||
2795 !_cups_strncasecmp(device_id, "MFR:", 4) ||
2796 !_cups_strncasecmp(device_id, "MODEL:", 6) ||
2797 !_cups_strncasecmp(device_id, "MDL:", 4))
2798 {
2799 if (ptr > res)
2800 {
2801 *ptr++ = '.';
2802 *ptr++ = '*';
2803 }
2804
2805 *ptr++ = '(';
2806
2807 while (*device_id && *device_id != ';' && ptr < (res + sizeof(res) - 8))
2808 {
2809 if (strchr("[]{}().*\\|", *device_id))
2810 *ptr++ = '\\';
2811 if (*device_id == ':')
2812 {
2813 /*
2814 * KEY:.*value
2815 */
2816
2817 *ptr++ = *device_id++;
2818 *ptr++ = '.';
2819 *ptr++ = '*';
2820 }
2821 else
2822 *ptr++ = *device_id++;
2823 }
2824
2825 if (*device_id == ';' || !*device_id)
2826 {
2827 /*
2828 * KEY:.*value.*;
2829 */
2830
2831 *ptr++ = '.';
2832 *ptr++ = '*';
2833 *ptr++ = ';';
2834 }
2835 *ptr++ = ')';
2836 if (cmd)
2837 *ptr++ = '?';
2838 }
2839 else if ((device_id = strchr(device_id, ';')) == NULL)
2840 break;
2841 else
2842 device_id ++;
2843 }
2844
2845 *ptr = '\0';
2846
2847 fprintf(stderr, "DEBUG: [cups-driverd] regex_device_id: \"%s\"\n", res);
2848
2849 /*
2850 * Compile the regular expression and return...
2851 */
2852
2853 if (res[0] && (re = (regex_t *)calloc(1, sizeof(regex_t))) != NULL)
2854 {
2855 if (!regcomp(re, res, REG_EXTENDED | REG_ICASE))
2856 {
2857 fputs("DEBUG: [cups-driverd] regex_device_id: OK\n", stderr);
2858 return (re);
2859 }
2860
2861 free(re);
2862 }
2863
2864 return (NULL);
2865 }
2866
2867
2868 /*
2869 * 'regex_string()' - Construct a regular expression to compare a simple string.
2870 */
2871
2872 static regex_t * /* O - Regular expression */
2873 regex_string(const char *s) /* I - String to compare */
2874 {
2875 char res[2048], /* Regular expression string */
2876 *ptr; /* Pointer into string */
2877 regex_t *re; /* Regular expression */
2878
2879
2880 fprintf(stderr, "DEBUG: [cups-driverd] regex_string(\"%s\")\n", s);
2881
2882 /*
2883 * Convert the string to a regular expression, escaping special characters
2884 * as needed.
2885 */
2886
2887 ptr = res;
2888
2889 while (*s && ptr < (res + sizeof(res) - 2))
2890 {
2891 if (strchr("[]{}().*\\", *s))
2892 *ptr++ = '\\';
2893
2894 *ptr++ = *s++;
2895 }
2896
2897 *ptr = '\0';
2898
2899 fprintf(stderr, "DEBUG: [cups-driverd] regex_string: \"%s\"\n", res);
2900
2901 /*
2902 * Create a case-insensitive regular expression...
2903 */
2904
2905 if (res[0] && (re = (regex_t *)calloc(1, sizeof(regex_t))) != NULL)
2906 {
2907 if (!regcomp(re, res, REG_ICASE))
2908 {
2909 fputs("DEBUG: [cups-driverd] regex_string: OK\n", stderr);
2910 return (re);
2911 }
2912
2913 free(re);
2914 }
2915
2916 return (NULL);
2917 }