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