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