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