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