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