]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/ppd-util.c
cdc29d34ad988bc169f4b019804edc6cd2d9fce7
[thirdparty/cups.git] / cups / ppd-util.c
1 /*
2 * PPD utilities for CUPS.
3 *
4 * Copyright © 2007-2018 by Apple Inc.
5 * Copyright © 1997-2006 by Easy Software Products.
6 *
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more
8 * information.
9 */
10
11 /*
12 * Include necessary headers...
13 */
14
15 #include "cups-private.h"
16 #include "ppd-private.h"
17 #include <fcntl.h>
18 #include <sys/stat.h>
19 #if defined(_WIN32) || defined(__EMX__)
20 # include <io.h>
21 #else
22 # include <unistd.h>
23 #endif /* _WIN32 || __EMX__ */
24
25
26 /*
27 * Local functions...
28 */
29
30 static int cups_get_printer_uri(http_t *http, const char *name,
31 char *host, int hostsize, int *port,
32 char *resource, int resourcesize,
33 int depth);
34
35
36 /*
37 * 'cupsGetPPD()' - Get the PPD file for a printer on the default server.
38 *
39 * For classes, @code cupsGetPPD@ returns the PPD file for the first printer
40 * in the class.
41 *
42 * The returned filename is stored in a static buffer and is overwritten with
43 * each call to @code cupsGetPPD@ or @link cupsGetPPD2@. The caller "owns" the
44 * file that is created and must @code unlink@ the returned filename.
45 */
46
47 const char * /* O - Filename for PPD file */
48 cupsGetPPD(const char *name) /* I - Destination name */
49 {
50 _ppd_globals_t *pg = _ppdGlobals(); /* Pointer to library globals */
51 time_t modtime = 0; /* Modification time */
52
53
54 /*
55 * Return the PPD file...
56 */
57
58 pg->ppd_filename[0] = '\0';
59
60 if (cupsGetPPD3(CUPS_HTTP_DEFAULT, name, &modtime, pg->ppd_filename,
61 sizeof(pg->ppd_filename)) == HTTP_STATUS_OK)
62 return (pg->ppd_filename);
63 else
64 return (NULL);
65 }
66
67
68 /*
69 * 'cupsGetPPD2()' - Get the PPD file for a printer from the specified server.
70 *
71 * For classes, @code cupsGetPPD2@ returns the PPD file for the first printer
72 * in the class.
73 *
74 * The returned filename is stored in a static buffer and is overwritten with
75 * each call to @link cupsGetPPD@ or @code cupsGetPPD2@. The caller "owns" the
76 * file that is created and must @code unlink@ the returned filename.
77 *
78 * @since CUPS 1.1.21/macOS 10.4@
79 */
80
81 const char * /* O - Filename for PPD file */
82 cupsGetPPD2(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
83 const char *name) /* I - Destination name */
84 {
85 _ppd_globals_t *pg = _ppdGlobals(); /* Pointer to library globals */
86 time_t modtime = 0; /* Modification time */
87
88
89 pg->ppd_filename[0] = '\0';
90
91 if (cupsGetPPD3(http, name, &modtime, pg->ppd_filename,
92 sizeof(pg->ppd_filename)) == HTTP_STATUS_OK)
93 return (pg->ppd_filename);
94 else
95 return (NULL);
96 }
97
98
99 /*
100 * 'cupsGetPPD3()' - Get the PPD file for a printer on the specified
101 * server if it has changed.
102 *
103 * The "modtime" parameter contains the modification time of any
104 * locally-cached content and is updated with the time from the PPD file on
105 * the server.
106 *
107 * The "buffer" parameter contains the local PPD filename. If it contains
108 * the empty string, a new temporary file is created, otherwise the existing
109 * file will be overwritten as needed. The caller "owns" the file that is
110 * created and must @code unlink@ the returned filename.
111 *
112 * On success, @code HTTP_STATUS_OK@ is returned for a new PPD file and
113 * @code HTTP_STATUS_NOT_MODIFIED@ if the existing PPD file is up-to-date. Any other
114 * status is an error.
115 *
116 * For classes, @code cupsGetPPD3@ returns the PPD file for the first printer
117 * in the class.
118 *
119 * @since CUPS 1.4/macOS 10.6@
120 */
121
122 http_status_t /* O - HTTP status */
123 cupsGetPPD3(http_t *http, /* I - HTTP connection or @code CUPS_HTTP_DEFAULT@ */
124 const char *name, /* I - Destination name */
125 time_t *modtime, /* IO - Modification time */
126 char *buffer, /* I - Filename buffer */
127 size_t bufsize) /* I - Size of filename buffer */
128 {
129 int http_port; /* Port number */
130 char http_hostname[HTTP_MAX_HOST];
131 /* Hostname associated with connection */
132 http_t *http2; /* Alternate HTTP connection */
133 int fd; /* PPD file */
134 char localhost[HTTP_MAX_URI],/* Local hostname */
135 hostname[HTTP_MAX_URI], /* Hostname */
136 resource[HTTP_MAX_URI]; /* Resource name */
137 int port; /* Port number */
138 http_status_t status; /* HTTP status from server */
139 char tempfile[1024] = ""; /* Temporary filename */
140 _cups_globals_t *cg = _cupsGlobals(); /* Pointer to library globals */
141
142
143 /*
144 * Range check input...
145 */
146
147 DEBUG_printf(("cupsGetPPD3(http=%p, name=\"%s\", modtime=%p(%d), buffer=%p, "
148 "bufsize=%d)", http, name, modtime,
149 modtime ? (int)*modtime : 0, buffer, (int)bufsize));
150
151 if (!name)
152 {
153 DEBUG_puts("2cupsGetPPD3: No printer name, returning NULL.");
154 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("No printer name"), 1);
155 return (HTTP_STATUS_NOT_ACCEPTABLE);
156 }
157
158 if (!modtime)
159 {
160 DEBUG_puts("2cupsGetPPD3: No modtime, returning NULL.");
161 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("No modification time"), 1);
162 return (HTTP_STATUS_NOT_ACCEPTABLE);
163 }
164
165 if (!buffer || bufsize <= 1)
166 {
167 DEBUG_puts("2cupsGetPPD3: No filename buffer, returning NULL.");
168 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad filename buffer"), 1);
169 return (HTTP_STATUS_NOT_ACCEPTABLE);
170 }
171
172 #ifndef _WIN32
173 /*
174 * See if the PPD file is available locally...
175 */
176
177 if (http)
178 httpGetHostname(http, hostname, sizeof(hostname));
179 else
180 {
181 strlcpy(hostname, cupsServer(), sizeof(hostname));
182 if (hostname[0] == '/')
183 strlcpy(hostname, "localhost", sizeof(hostname));
184 }
185
186 if (!_cups_strcasecmp(hostname, "localhost"))
187 {
188 char ppdname[1024]; /* PPD filename */
189 struct stat ppdinfo; /* PPD file information */
190
191
192 snprintf(ppdname, sizeof(ppdname), "%s/ppd/%s.ppd", cg->cups_serverroot,
193 name);
194 if (!stat(ppdname, &ppdinfo) && !access(ppdname, R_OK))
195 {
196 /*
197 * OK, the file exists and is readable, use it!
198 */
199
200 if (buffer[0])
201 {
202 DEBUG_printf(("2cupsGetPPD3: Using filename \"%s\".", buffer));
203
204 unlink(buffer);
205
206 if (symlink(ppdname, buffer) && errno != EEXIST)
207 {
208 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, NULL, 0);
209
210 return (HTTP_STATUS_SERVER_ERROR);
211 }
212 }
213 else
214 {
215 int tries; /* Number of tries */
216 const char *tmpdir; /* TMPDIR environment variable */
217 struct timeval curtime; /* Current time */
218
219
220 #ifdef __APPLE__
221 /*
222 * On macOS and iOS, the TMPDIR environment variable is not always the
223 * best location to place temporary files due to sandboxing. Instead,
224 * the confstr function should be called to get the proper per-user,
225 * per-process TMPDIR value.
226 */
227
228 char tmppath[1024]; /* Temporary directory */
229
230 if ((tmpdir = getenv("TMPDIR")) != NULL && access(tmpdir, W_OK))
231 tmpdir = NULL;
232
233 if (!tmpdir)
234 {
235 if (confstr(_CS_DARWIN_USER_TEMP_DIR, tmppath, sizeof(tmppath)))
236 tmpdir = tmppath;
237 else
238 tmpdir = "/private/tmp"; /* This should never happen */
239 }
240 #else
241 /*
242 * Previously we put root temporary files in the default CUPS temporary
243 * directory under /var/spool/cups. However, since the scheduler cleans
244 * out temporary files there and runs independently of the user apps, we
245 * don't want to use it unless specifically told to by cupsd.
246 */
247
248 if ((tmpdir = getenv("TMPDIR")) == NULL)
249 tmpdir = "/tmp";
250 #endif /* __APPLE__ */
251
252 DEBUG_printf(("2cupsGetPPD3: tmpdir=\"%s\".", tmpdir));
253
254 /*
255 * Make the temporary name using the specified directory...
256 */
257
258 tries = 0;
259
260 do
261 {
262 /*
263 * Get the current time of day...
264 */
265
266 gettimeofday(&curtime, NULL);
267
268 /*
269 * Format a string using the hex time values...
270 */
271
272 snprintf(buffer, bufsize, "%s/%08lx%05lx", tmpdir,
273 (unsigned long)curtime.tv_sec,
274 (unsigned long)curtime.tv_usec);
275
276 /*
277 * Try to make a symlink...
278 */
279
280 if (!symlink(ppdname, buffer))
281 break;
282
283 DEBUG_printf(("2cupsGetPPD3: Symlink \"%s\" to \"%s\" failed: %s", ppdname, buffer, strerror(errno)));
284
285 tries ++;
286 }
287 while (tries < 1000);
288
289 if (tries >= 1000)
290 {
291 DEBUG_puts("2cupsGetPPD3: Unable to symlink after 1000 tries, returning error.");
292
293 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, NULL, 0);
294
295 return (HTTP_STATUS_SERVER_ERROR);
296 }
297 }
298
299 if (*modtime >= ppdinfo.st_mtime)
300 {
301 DEBUG_printf(("2cupsGetPPD3: Returning not-modified, filename=\"%s\".", buffer));
302 return (HTTP_STATUS_NOT_MODIFIED);
303 }
304 else
305 {
306 DEBUG_printf(("2cupsGetPPD3: Returning ok, filename=\"%s\", modtime=%ld.", buffer, (long)ppdinfo.st_mtime));
307 *modtime = ppdinfo.st_mtime;
308 return (HTTP_STATUS_OK);
309 }
310 }
311 }
312 #endif /* !_WIN32 */
313
314 /*
315 * Try finding a printer URI for this printer...
316 */
317
318 DEBUG_puts("2cupsGetPPD3: Unable to access local file, copying...");
319
320 if (!http)
321 {
322 if ((http = _cupsConnect()) == NULL)
323 {
324 DEBUG_puts("2cupsGetPPD3: Unable to connect to scheduler.");
325 return (HTTP_STATUS_SERVICE_UNAVAILABLE);
326 }
327 }
328
329 if (!cups_get_printer_uri(http, name, hostname, sizeof(hostname), &port, resource, sizeof(resource), 0))
330 {
331 DEBUG_puts("2cupsGetPPD3: Unable to get printer URI.");
332 return (HTTP_STATUS_NOT_FOUND);
333 }
334
335 DEBUG_printf(("2cupsGetPPD3: Printer hostname=\"%s\", port=%d", hostname, port));
336
337 if (cupsServer()[0] == '/' && !_cups_strcasecmp(hostname, "localhost") && port == ippPort())
338 {
339 /*
340 * Redirect localhost to domain socket...
341 */
342
343 strlcpy(hostname, cupsServer(), sizeof(hostname));
344 port = 0;
345
346 DEBUG_printf(("2cupsGetPPD3: Redirecting to \"%s\".", hostname));
347 }
348
349 /*
350 * Remap local hostname to localhost...
351 */
352
353 httpGetHostname(NULL, localhost, sizeof(localhost));
354
355 DEBUG_printf(("2cupsGetPPD3: Local hostname=\"%s\"", localhost));
356
357 if (!_cups_strcasecmp(localhost, hostname))
358 strlcpy(hostname, "localhost", sizeof(hostname));
359
360 /*
361 * Get the hostname and port number we are connected to...
362 */
363
364 httpGetHostname(http, http_hostname, sizeof(http_hostname));
365 http_port = httpAddrPort(http->hostaddr);
366
367 DEBUG_printf(("2cupsGetPPD3: Connection hostname=\"%s\", port=%d",
368 http_hostname, http_port));
369
370 /*
371 * Reconnect to the correct server as needed...
372 */
373
374 if (!_cups_strcasecmp(http_hostname, hostname) && port == http_port)
375 http2 = http;
376 else if ((http2 = httpConnect2(hostname, port, NULL, AF_UNSPEC,
377 cupsEncryption(), 1, 30000, NULL)) == NULL)
378 {
379 DEBUG_puts("2cupsGetPPD3: Unable to connect to server");
380
381 return (HTTP_STATUS_SERVICE_UNAVAILABLE);
382 }
383
384 /*
385 * Get a temp file...
386 */
387
388 if (buffer[0])
389 fd = open(buffer, O_CREAT | O_TRUNC | O_WRONLY, 0600);
390 else
391 fd = cupsTempFd(tempfile, sizeof(tempfile));
392
393 if (fd < 0)
394 {
395 /*
396 * Can't open file; close the server connection and return NULL...
397 */
398
399 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, NULL, 0);
400
401 if (http2 != http)
402 httpClose(http2);
403
404 return (HTTP_STATUS_SERVER_ERROR);
405 }
406
407 /*
408 * And send a request to the HTTP server...
409 */
410
411 strlcat(resource, ".ppd", sizeof(resource));
412
413 if (*modtime > 0)
414 httpSetField(http2, HTTP_FIELD_IF_MODIFIED_SINCE,
415 httpGetDateString(*modtime));
416
417 status = cupsGetFd(http2, resource, fd);
418
419 close(fd);
420
421 /*
422 * See if we actually got the file or an error...
423 */
424
425 if (status == HTTP_STATUS_OK)
426 {
427 *modtime = httpGetDateTime(httpGetField(http2, HTTP_FIELD_DATE));
428
429 if (tempfile[0])
430 strlcpy(buffer, tempfile, bufsize);
431 }
432 else if (status != HTTP_STATUS_NOT_MODIFIED)
433 {
434 _cupsSetHTTPError(status);
435
436 if (buffer[0])
437 unlink(buffer);
438 else if (tempfile[0])
439 unlink(tempfile);
440 }
441 else if (tempfile[0])
442 unlink(tempfile);
443
444 if (http2 != http)
445 httpClose(http2);
446
447 /*
448 * Return the PPD file...
449 */
450
451 DEBUG_printf(("2cupsGetPPD3: Returning status %d", status));
452
453 return (status);
454 }
455
456
457 /*
458 * 'cupsGetServerPPD()' - Get an available PPD file from the server.
459 *
460 * This function returns the named PPD file from the server. The
461 * list of available PPDs is provided by the IPP @code CUPS_GET_PPDS@
462 * operation.
463 *
464 * You must remove (unlink) the PPD file when you are finished with
465 * it. The PPD filename is stored in a static location that will be
466 * overwritten on the next call to @link cupsGetPPD@, @link cupsGetPPD2@,
467 * or @link cupsGetServerPPD@.
468 *
469 * @since CUPS 1.3/macOS 10.5@
470 */
471
472 char * /* O - Name of PPD file or @code NULL@ on error */
473 cupsGetServerPPD(http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
474 const char *name) /* I - Name of PPD file ("ppd-name") */
475 {
476 int fd; /* PPD file descriptor */
477 ipp_t *request; /* IPP request */
478 _ppd_globals_t *pg = _ppdGlobals();
479 /* Pointer to library globals */
480
481
482 /*
483 * Range check input...
484 */
485
486 if (!name)
487 {
488 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("No PPD name"), 1);
489
490 return (NULL);
491 }
492
493 if (!http)
494 if ((http = _cupsConnect()) == NULL)
495 return (NULL);
496
497 /*
498 * Get a temp file...
499 */
500
501 if ((fd = cupsTempFd(pg->ppd_filename, sizeof(pg->ppd_filename))) < 0)
502 {
503 /*
504 * Can't open file; close the server connection and return NULL...
505 */
506
507 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, NULL, 0);
508
509 return (NULL);
510 }
511
512 /*
513 * Get the PPD file...
514 */
515
516 request = ippNewRequest(IPP_OP_CUPS_GET_PPD);
517 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "ppd-name", NULL,
518 name);
519
520 ippDelete(cupsDoIORequest(http, request, "/", -1, fd));
521
522 close(fd);
523
524 if (cupsLastError() != IPP_STATUS_OK)
525 {
526 unlink(pg->ppd_filename);
527 return (NULL);
528 }
529 else
530 return (pg->ppd_filename);
531 }
532
533
534 /*
535 * 'cups_get_printer_uri()' - Get the printer-uri-supported attribute for the
536 * first printer in a class.
537 */
538
539 static int /* O - 1 on success, 0 on failure */
540 cups_get_printer_uri(
541 http_t *http, /* I - Connection to server */
542 const char *name, /* I - Name of printer or class */
543 char *host, /* I - Hostname buffer */
544 int hostsize, /* I - Size of hostname buffer */
545 int *port, /* O - Port number */
546 char *resource, /* I - Resource buffer */
547 int resourcesize, /* I - Size of resource buffer */
548 int depth) /* I - Depth of query */
549 {
550 int i; /* Looping var */
551 ipp_t *request, /* IPP request */
552 *response; /* IPP response */
553 ipp_attribute_t *attr; /* Current attribute */
554 char uri[HTTP_MAX_URI], /* printer-uri attribute */
555 scheme[HTTP_MAX_URI], /* Scheme name */
556 username[HTTP_MAX_URI]; /* Username:password */
557 static const char * const requested_attrs[] =
558 { /* Requested attributes */
559 "member-uris",
560 "printer-uri-supported"
561 };
562
563
564 DEBUG_printf(("4cups_get_printer_uri(http=%p, name=\"%s\", host=%p, hostsize=%d, resource=%p, resourcesize=%d, depth=%d)", http, name, host, hostsize, resource, resourcesize, depth));
565
566 /*
567 * Setup the printer URI...
568 */
569
570 if (httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, "localhost", 0, "/printers/%s", name) < HTTP_URI_STATUS_OK)
571 {
572 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Unable to create printer-uri"), 1);
573
574 *host = '\0';
575 *resource = '\0';
576
577 return (0);
578 }
579
580 DEBUG_printf(("5cups_get_printer_uri: printer-uri=\"%s\"", uri));
581
582 /*
583 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
584 * attributes:
585 *
586 * attributes-charset
587 * attributes-natural-language
588 * printer-uri
589 * requested-attributes
590 */
591
592 request = ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES);
593
594 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
595
596 ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "requested-attributes", sizeof(requested_attrs) / sizeof(requested_attrs[0]), NULL, requested_attrs);
597
598 /*
599 * Do the request and get back a response...
600 */
601
602 snprintf(resource, (size_t)resourcesize, "/printers/%s", name);
603
604 if ((response = cupsDoRequest(http, request, resource)) != NULL)
605 {
606 if ((attr = ippFindAttribute(response, "member-uris", IPP_TAG_URI)) != NULL)
607 {
608 /*
609 * Get the first actual printer name in the class...
610 */
611
612 DEBUG_printf(("5cups_get_printer_uri: Got member-uris with %d values.", ippGetCount(attr)));
613
614 for (i = 0; i < attr->num_values; i ++)
615 {
616 DEBUG_printf(("5cups_get_printer_uri: member-uris[%d]=\"%s\"", i, ippGetString(attr, i, NULL)));
617
618 httpSeparateURI(HTTP_URI_CODING_ALL, attr->values[i].string.text, scheme, sizeof(scheme), username, sizeof(username), host, hostsize, port, resource, resourcesize);
619 if (!strncmp(resource, "/printers/", 10))
620 {
621 /*
622 * Found a printer!
623 */
624
625 ippDelete(response);
626
627 DEBUG_printf(("5cups_get_printer_uri: Found printer member with host=\"%s\", port=%d, resource=\"%s\"", host, *port, resource));
628 return (1);
629 }
630 }
631 }
632 else if ((attr = ippFindAttribute(response, "printer-uri-supported", IPP_TAG_URI)) != NULL)
633 {
634 httpSeparateURI(HTTP_URI_CODING_ALL, _httpResolveURI(attr->values[0].string.text, uri, sizeof(uri), _HTTP_RESOLVE_DEFAULT, NULL, NULL), scheme, sizeof(scheme), username, sizeof(username), host, hostsize, port, resource, resourcesize);
635 ippDelete(response);
636
637 DEBUG_printf(("5cups_get_printer_uri: Resolved to host=\"%s\", port=%d, resource=\"%s\"", host, *port, resource));
638
639 if (!strncmp(resource, "/classes/", 9))
640 {
641 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("No printer-uri found for class"), 1);
642
643 *host = '\0';
644 *resource = '\0';
645
646 DEBUG_puts("5cups_get_printer_uri: Not returning class.");
647 return (0);
648 }
649
650 return (1);
651 }
652
653 ippDelete(response);
654 }
655
656 if (cupsLastError() != IPP_STATUS_ERROR_NOT_FOUND)
657 _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("No printer-uri found"), 1);
658
659 *host = '\0';
660 *resource = '\0';
661
662 DEBUG_puts("5cups_get_printer_uri: Printer URI not found.");
663 return (0);
664 }