2 * PPD utilities for CUPS.
4 * Copyright © 2007-2018 by Apple Inc.
5 * Copyright © 1997-2006 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more
12 * Include necessary headers...
15 #include "cups-private.h"
16 #include "ppd-private.h"
19 #if defined(WIN32) || defined(__EMX__)
23 #endif /* WIN32 || __EMX__ */
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
,
37 * 'cupsGetPPD()' - Get the PPD file for a printer on the default server.
39 * For classes, @code cupsGetPPD@ returns the PPD file for the first printer
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.
47 const char * /* O - Filename for PPD file */
48 cupsGetPPD(const char *name
) /* I - Destination name */
50 _ppd_globals_t
*pg
= _ppdGlobals(); /* Pointer to library globals */
51 time_t modtime
= 0; /* Modification time */
55 * Return the PPD file...
58 pg
->ppd_filename
[0] = '\0';
60 if (cupsGetPPD3(CUPS_HTTP_DEFAULT
, name
, &modtime
, pg
->ppd_filename
,
61 sizeof(pg
->ppd_filename
)) == HTTP_STATUS_OK
)
62 return (pg
->ppd_filename
);
69 * 'cupsGetPPD2()' - Get the PPD file for a printer from the specified server.
71 * For classes, @code cupsGetPPD2@ returns the PPD file for the first printer
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.
78 * @since CUPS 1.1.21/macOS 10.4@
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 */
85 _ppd_globals_t
*pg
= _ppdGlobals(); /* Pointer to library globals */
86 time_t modtime
= 0; /* Modification time */
89 pg
->ppd_filename
[0] = '\0';
91 if (cupsGetPPD3(http
, name
, &modtime
, pg
->ppd_filename
,
92 sizeof(pg
->ppd_filename
)) == HTTP_STATUS_OK
)
93 return (pg
->ppd_filename
);
100 * 'cupsGetPPD3()' - Get the PPD file for a printer on the specified
101 * server if it has changed.
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
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.
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.
116 * For classes, @code cupsGetPPD3@ returns the PPD file for the first printer
119 * @since CUPS 1.4/macOS 10.6@
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 */
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 */
144 * Range check input...
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
));
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
);
160 DEBUG_puts("2cupsGetPPD3: No modtime, returning NULL.");
161 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No modification time"), 1);
162 return (HTTP_STATUS_NOT_ACCEPTABLE
);
165 if (!buffer
|| bufsize
<= 1)
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
);
174 * See if the PPD file is available locally...
178 httpGetHostname(http
, hostname
, sizeof(hostname
));
181 strlcpy(hostname
, cupsServer(), sizeof(hostname
));
182 if (hostname
[0] == '/')
183 strlcpy(hostname
, "localhost", sizeof(hostname
));
186 if (!_cups_strcasecmp(hostname
, "localhost"))
188 char ppdname
[1024]; /* PPD filename */
189 struct stat ppdinfo
; /* PPD file information */
192 snprintf(ppdname
, sizeof(ppdname
), "%s/ppd/%s.ppd", cg
->cups_serverroot
,
194 if (!stat(ppdname
, &ppdinfo
) && !access(ppdname
, R_OK
))
197 * OK, the file exists and is readable, use it!
202 DEBUG_printf(("2cupsGetPPD3: Using filename \"%s\".", buffer
));
206 if (symlink(ppdname
, buffer
) && errno
!= EEXIST
)
208 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, NULL
, 0);
210 return (HTTP_STATUS_SERVER_ERROR
);
215 int tries
; /* Number of tries */
216 const char *tmpdir
; /* TMPDIR environment variable */
217 struct timeval curtime
; /* Current time */
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.
228 char tmppath
[1024]; /* Temporary directory */
230 if ((tmpdir
= getenv("TMPDIR")) != NULL
&& access(tmpdir
, W_OK
))
235 if (confstr(_CS_DARWIN_USER_TEMP_DIR
, tmppath
, sizeof(tmppath
)))
238 tmpdir
= "/private/tmp"; /* This should never happen */
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.
248 if ((tmpdir
= getenv("TMPDIR")) == NULL
)
250 #endif /* __APPLE__ */
252 DEBUG_printf(("2cupsGetPPD3: tmpdir=\"%s\".", tmpdir
));
255 * Make the temporary name using the specified directory...
263 * Get the current time of day...
266 gettimeofday(&curtime
, NULL
);
269 * Format a string using the hex time values...
272 snprintf(buffer
, bufsize
, "%s/%08lx%05lx", tmpdir
,
273 (unsigned long)curtime
.tv_sec
,
274 (unsigned long)curtime
.tv_usec
);
277 * Try to make a symlink...
280 if (!symlink(ppdname
, buffer
))
283 DEBUG_printf(("2cupsGetPPD3: Symlink \"%s\" to \"%s\" failed: %s", ppdname
, buffer
, strerror(errno
)));
287 while (tries
< 1000);
291 DEBUG_puts("2cupsGetPPD3: Unable to symlink after 1000 tries, returning error.");
293 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, NULL
, 0);
295 return (HTTP_STATUS_SERVER_ERROR
);
299 if (*modtime
>= ppdinfo
.st_mtime
)
301 DEBUG_printf(("2cupsGetPPD3: Returning not-modified, filename=\"%s\".", buffer
));
302 return (HTTP_STATUS_NOT_MODIFIED
);
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
);
315 * Try finding a printer URI for this printer...
318 DEBUG_puts("2cupsGetPPD3: Unable to access local file, copying...");
322 if ((http
= _cupsConnect()) == NULL
)
324 DEBUG_puts("2cupsGetPPD3: Unable to connect to scheduler.");
325 return (HTTP_STATUS_SERVICE_UNAVAILABLE
);
329 if (!cups_get_printer_uri(http
, name
, hostname
, sizeof(hostname
), &port
, resource
, sizeof(resource
), 0))
331 DEBUG_puts("2cupsGetPPD3: Unable to get printer URI.");
332 return (HTTP_STATUS_NOT_FOUND
);
335 DEBUG_printf(("2cupsGetPPD3: Printer hostname=\"%s\", port=%d", hostname
, port
));
337 if (cupsServer()[0] == '/' && !_cups_strcasecmp(hostname
, "localhost") && port
== ippPort())
340 * Redirect localhost to domain socket...
343 strlcpy(hostname
, cupsServer(), sizeof(hostname
));
346 DEBUG_printf(("2cupsGetPPD3: Redirecting to \"%s\".", hostname
));
350 * Remap local hostname to localhost...
353 httpGetHostname(NULL
, localhost
, sizeof(localhost
));
355 DEBUG_printf(("2cupsGetPPD3: Local hostname=\"%s\"", localhost
));
357 if (!_cups_strcasecmp(localhost
, hostname
))
358 strlcpy(hostname
, "localhost", sizeof(hostname
));
361 * Get the hostname and port number we are connected to...
364 httpGetHostname(http
, http_hostname
, sizeof(http_hostname
));
365 http_port
= httpAddrPort(http
->hostaddr
);
367 DEBUG_printf(("2cupsGetPPD3: Connection hostname=\"%s\", port=%d",
368 http_hostname
, http_port
));
371 * Reconnect to the correct server as needed...
374 if (!_cups_strcasecmp(http_hostname
, hostname
) && port
== http_port
)
376 else if ((http2
= httpConnect2(hostname
, port
, NULL
, AF_UNSPEC
,
377 cupsEncryption(), 1, 30000, NULL
)) == NULL
)
379 DEBUG_puts("2cupsGetPPD3: Unable to connect to server");
381 return (HTTP_STATUS_SERVICE_UNAVAILABLE
);
389 fd
= open(buffer
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
391 fd
= cupsTempFd(tempfile
, sizeof(tempfile
));
396 * Can't open file; close the server connection and return NULL...
399 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, NULL
, 0);
404 return (HTTP_STATUS_SERVER_ERROR
);
408 * And send a request to the HTTP server...
411 strlcat(resource
, ".ppd", sizeof(resource
));
414 httpSetField(http2
, HTTP_FIELD_IF_MODIFIED_SINCE
,
415 httpGetDateString(*modtime
));
417 status
= cupsGetFd(http2
, resource
, fd
);
422 * See if we actually got the file or an error...
425 if (status
== HTTP_STATUS_OK
)
427 *modtime
= httpGetDateTime(httpGetField(http2
, HTTP_FIELD_DATE
));
430 strlcpy(buffer
, tempfile
, bufsize
);
432 else if (status
!= HTTP_STATUS_NOT_MODIFIED
)
434 _cupsSetHTTPError(status
);
438 else if (tempfile
[0])
441 else if (tempfile
[0])
448 * Return the PPD file...
451 DEBUG_printf(("2cupsGetPPD3: Returning status %d", status
));
458 * 'cupsGetServerPPD()' - Get an available PPD file from the server.
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@
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@.
469 * @since CUPS 1.3/macOS 10.5@
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") */
476 int fd
; /* PPD file descriptor */
477 ipp_t
*request
; /* IPP request */
478 _ppd_globals_t
*pg
= _ppdGlobals();
479 /* Pointer to library globals */
483 * Range check input...
488 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No PPD name"), 1);
494 if ((http
= _cupsConnect()) == NULL
)
501 if ((fd
= cupsTempFd(pg
->ppd_filename
, sizeof(pg
->ppd_filename
))) < 0)
504 * Can't open file; close the server connection and return NULL...
507 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, NULL
, 0);
513 * Get the PPD file...
516 request
= ippNewRequest(IPP_OP_CUPS_GET_PPD
);
517 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "ppd-name", NULL
,
520 ippDelete(cupsDoIORequest(http
, request
, "/", -1, fd
));
524 if (cupsLastError() != IPP_STATUS_OK
)
526 unlink(pg
->ppd_filename
);
530 return (pg
->ppd_filename
);
535 * 'cups_get_printer_uri()' - Get the printer-uri-supported attribute for the
536 * first printer in a class.
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 */
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 */
560 "printer-uri-supported"
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
));
567 * Setup the printer URI...
570 if (httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
, "localhost", 0, "/printers/%s", name
) < HTTP_URI_STATUS_OK
)
572 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Unable to create printer-uri"), 1);
580 DEBUG_printf(("5cups_get_printer_uri: printer-uri=\"%s\"", uri
));
583 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
587 * attributes-natural-language
589 * requested-attributes
592 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
594 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
596 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
, "requested-attributes", sizeof(requested_attrs
) / sizeof(requested_attrs
[0]), NULL
, requested_attrs
);
599 * Do the request and get back a response...
602 snprintf(resource
, (size_t)resourcesize
, "/printers/%s", name
);
604 if ((response
= cupsDoRequest(http
, request
, resource
)) != NULL
)
606 if ((attr
= ippFindAttribute(response
, "member-uris", IPP_TAG_URI
)) != NULL
)
609 * Get the first actual printer name in the class...
612 DEBUG_printf(("5cups_get_printer_uri: Got member-uris with %d values.", ippGetCount(attr
)));
614 for (i
= 0; i
< attr
->num_values
; i
++)
616 DEBUG_printf(("5cups_get_printer_uri: member-uris[%d]=\"%s\"", i
, ippGetString(attr
, i
, NULL
)));
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))
627 DEBUG_printf(("5cups_get_printer_uri: Found printer member with host=\"%s\", port=%d, resource=\"%s\"", host
, *port
, resource
));
632 else if ((attr
= ippFindAttribute(response
, "printer-uri-supported", IPP_TAG_URI
)) != NULL
)
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
);
637 DEBUG_printf(("5cups_get_printer_uri: Resolved to host=\"%s\", port=%d, resource=\"%s\"", host
, *port
, resource
));
639 if (!strncmp(resource
, "/classes/", 9))
641 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No printer-uri found for class"), 1);
646 DEBUG_puts("5cups_get_printer_uri: Not returning class.");
656 if (cupsLastError() != IPP_STATUS_ERROR_NOT_FOUND
)
657 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No printer-uri found"), 1);
662 DEBUG_puts("5cups_get_printer_uri: Printer URI not found.");