2 * PPD utilities for CUPS.
4 * Copyright © 2007-2018 by Apple Inc.
5 * Copyright © 1997-2006 by Easy Software Products.
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * missing or damaged, see the license at "http://www.cups.org/".
13 * This file is subject to the Apple OS-Developed Software exception.
17 * Include necessary headers...
20 #include "cups-private.h"
21 #include "ppd-private.h"
24 #if defined(WIN32) || defined(__EMX__)
28 #endif /* WIN32 || __EMX__ */
35 static int cups_get_printer_uri(http_t
*http
, const char *name
,
36 char *host
, int hostsize
, int *port
,
37 char *resource
, int resourcesize
,
42 * 'cupsGetPPD()' - Get the PPD file for a printer on the default server.
44 * For classes, @code cupsGetPPD@ returns the PPD file for the first printer
47 * The returned filename is stored in a static buffer and is overwritten with
48 * each call to @code cupsGetPPD@ or @link cupsGetPPD2@. The caller "owns" the
49 * file that is created and must @code unlink@ the returned filename.
52 const char * /* O - Filename for PPD file */
53 cupsGetPPD(const char *name
) /* I - Destination name */
55 _ppd_globals_t
*pg
= _ppdGlobals(); /* Pointer to library globals */
56 time_t modtime
= 0; /* Modification time */
60 * Return the PPD file...
63 pg
->ppd_filename
[0] = '\0';
65 if (cupsGetPPD3(CUPS_HTTP_DEFAULT
, name
, &modtime
, pg
->ppd_filename
,
66 sizeof(pg
->ppd_filename
)) == HTTP_STATUS_OK
)
67 return (pg
->ppd_filename
);
74 * 'cupsGetPPD2()' - Get the PPD file for a printer from the specified server.
76 * For classes, @code cupsGetPPD2@ returns the PPD file for the first printer
79 * The returned filename is stored in a static buffer and is overwritten with
80 * each call to @link cupsGetPPD@ or @code cupsGetPPD2@. The caller "owns" the
81 * file that is created and must @code unlink@ the returned filename.
83 * @since CUPS 1.1.21/macOS 10.4@
86 const char * /* O - Filename for PPD file */
87 cupsGetPPD2(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
88 const char *name
) /* I - Destination name */
90 _ppd_globals_t
*pg
= _ppdGlobals(); /* Pointer to library globals */
91 time_t modtime
= 0; /* Modification time */
94 pg
->ppd_filename
[0] = '\0';
96 if (cupsGetPPD3(http
, name
, &modtime
, pg
->ppd_filename
,
97 sizeof(pg
->ppd_filename
)) == HTTP_STATUS_OK
)
98 return (pg
->ppd_filename
);
105 * 'cupsGetPPD3()' - Get the PPD file for a printer on the specified
106 * server if it has changed.
108 * The "modtime" parameter contains the modification time of any
109 * locally-cached content and is updated with the time from the PPD file on
112 * The "buffer" parameter contains the local PPD filename. If it contains
113 * the empty string, a new temporary file is created, otherwise the existing
114 * file will be overwritten as needed. The caller "owns" the file that is
115 * created and must @code unlink@ the returned filename.
117 * On success, @code HTTP_STATUS_OK@ is returned for a new PPD file and
118 * @code HTTP_STATUS_NOT_MODIFIED@ if the existing PPD file is up-to-date. Any other
119 * status is an error.
121 * For classes, @code cupsGetPPD3@ returns the PPD file for the first printer
124 * @since CUPS 1.4/macOS 10.6@
127 http_status_t
/* O - HTTP status */
128 cupsGetPPD3(http_t
*http
, /* I - HTTP connection or @code CUPS_HTTP_DEFAULT@ */
129 const char *name
, /* I - Destination name */
130 time_t *modtime
, /* IO - Modification time */
131 char *buffer
, /* I - Filename buffer */
132 size_t bufsize
) /* I - Size of filename buffer */
134 int http_port
; /* Port number */
135 char http_hostname
[HTTP_MAX_HOST
];
136 /* Hostname associated with connection */
137 http_t
*http2
; /* Alternate HTTP connection */
138 int fd
; /* PPD file */
139 char localhost
[HTTP_MAX_URI
],/* Local hostname */
140 hostname
[HTTP_MAX_URI
], /* Hostname */
141 resource
[HTTP_MAX_URI
]; /* Resource name */
142 int port
; /* Port number */
143 http_status_t status
; /* HTTP status from server */
144 char tempfile
[1024] = ""; /* Temporary filename */
145 _cups_globals_t
*cg
= _cupsGlobals(); /* Pointer to library globals */
149 * Range check input...
152 DEBUG_printf(("cupsGetPPD3(http=%p, name=\"%s\", modtime=%p(%d), buffer=%p, "
153 "bufsize=%d)", http
, name
, modtime
,
154 modtime
? (int)*modtime
: 0, buffer
, (int)bufsize
));
158 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No printer name"), 1);
159 return (HTTP_STATUS_NOT_ACCEPTABLE
);
164 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No modification time"), 1);
165 return (HTTP_STATUS_NOT_ACCEPTABLE
);
168 if (!buffer
|| bufsize
<= 1)
170 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Bad filename buffer"), 1);
171 return (HTTP_STATUS_NOT_ACCEPTABLE
);
176 * See if the PPD file is available locally...
180 httpGetHostname(http
, hostname
, sizeof(hostname
));
183 strlcpy(hostname
, cupsServer(), sizeof(hostname
));
184 if (hostname
[0] == '/')
185 strlcpy(hostname
, "localhost", sizeof(hostname
));
188 if (!_cups_strcasecmp(hostname
, "localhost"))
190 char ppdname
[1024]; /* PPD filename */
191 struct stat ppdinfo
; /* PPD file information */
194 snprintf(ppdname
, sizeof(ppdname
), "%s/ppd/%s.ppd", cg
->cups_serverroot
,
196 if (!stat(ppdname
, &ppdinfo
) && !access(ppdname
, R_OK
))
199 * OK, the file exists and is readable, use it!
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 */
221 * On macOS and iOS, the TMPDIR environment variable is not always the
222 * best location to place temporary files due to sandboxing. Instead,
223 * the confstr function should be called to get the proper per-user,
224 * per-process TMPDIR value.
227 char tmppath
[1024]; /* Temporary directory */
229 if ((tmpdir
= getenv("TMPDIR")) != NULL
&& access(tmpdir
, W_OK
))
234 if (confstr(_CS_DARWIN_USER_TEMP_DIR
, tmppath
, sizeof(tmppath
)))
237 tmpdir
= "/private/tmp"; /* This should never happen */
241 * Previously we put root temporary files in the default CUPS temporary
242 * directory under /var/spool/cups. However, since the scheduler cleans
243 * out temporary files there and runs independently of the user apps, we
244 * don't want to use it unless specifically told to by cupsd.
247 if ((tmpdir
= getenv("TMPDIR")) == NULL
)
249 #endif /* __APPLE__ */
252 * Make the temporary name using the specified directory...
260 * Get the current time of day...
263 gettimeofday(&curtime
, NULL
);
266 * Format a string using the hex time values...
269 snprintf(buffer
, bufsize
, "%s/%08lx%05lx", tmpdir
,
270 (unsigned long)curtime
.tv_sec
,
271 (unsigned long)curtime
.tv_usec
);
274 * Try to make a symlink...
277 if (!symlink(ppdname
, buffer
))
282 while (tries
< 1000);
286 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, NULL
, 0);
288 return (HTTP_STATUS_SERVER_ERROR
);
292 if (*modtime
>= ppdinfo
.st_mtime
)
293 return (HTTP_STATUS_NOT_MODIFIED
);
296 *modtime
= ppdinfo
.st_mtime
;
297 return (HTTP_STATUS_OK
);
304 * Try finding a printer URI for this printer...
308 if ((http
= _cupsConnect()) == NULL
)
309 return (HTTP_STATUS_SERVICE_UNAVAILABLE
);
311 if (!cups_get_printer_uri(http
, name
, hostname
, sizeof(hostname
), &port
,
312 resource
, sizeof(resource
), 0))
313 return (HTTP_STATUS_NOT_FOUND
);
315 DEBUG_printf(("2cupsGetPPD3: Printer hostname=\"%s\", port=%d", hostname
,
318 if (cupsServer()[0] == '/' && !_cups_strcasecmp(hostname
, "localhost") && port
== ippPort())
321 * Redirect localhost to domain socket...
324 strlcpy(hostname
, cupsServer(), sizeof(hostname
));
327 DEBUG_printf(("2cupsGetPPD3: Redirecting to \"%s\".", hostname
));
331 * Remap local hostname to localhost...
334 httpGetHostname(NULL
, localhost
, sizeof(localhost
));
336 DEBUG_printf(("2cupsGetPPD3: Local hostname=\"%s\"", localhost
));
338 if (!_cups_strcasecmp(localhost
, hostname
))
339 strlcpy(hostname
, "localhost", sizeof(hostname
));
342 * Get the hostname and port number we are connected to...
345 httpGetHostname(http
, http_hostname
, sizeof(http_hostname
));
346 http_port
= httpAddrPort(http
->hostaddr
);
348 DEBUG_printf(("2cupsGetPPD3: Connection hostname=\"%s\", port=%d",
349 http_hostname
, http_port
));
352 * Reconnect to the correct server as needed...
355 if (!_cups_strcasecmp(http_hostname
, hostname
) && port
== http_port
)
357 else if ((http2
= httpConnect2(hostname
, port
, NULL
, AF_UNSPEC
,
358 cupsEncryption(), 1, 30000, NULL
)) == NULL
)
360 DEBUG_puts("1cupsGetPPD3: Unable to connect to server");
362 return (HTTP_STATUS_SERVICE_UNAVAILABLE
);
370 fd
= open(buffer
, O_CREAT
| O_TRUNC
| O_WRONLY
, 0600);
372 fd
= cupsTempFd(tempfile
, sizeof(tempfile
));
377 * Can't open file; close the server connection and return NULL...
380 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, NULL
, 0);
385 return (HTTP_STATUS_SERVER_ERROR
);
389 * And send a request to the HTTP server...
392 strlcat(resource
, ".ppd", sizeof(resource
));
395 httpSetField(http2
, HTTP_FIELD_IF_MODIFIED_SINCE
,
396 httpGetDateString(*modtime
));
398 status
= cupsGetFd(http2
, resource
, fd
);
403 * See if we actually got the file or an error...
406 if (status
== HTTP_STATUS_OK
)
408 *modtime
= httpGetDateTime(httpGetField(http2
, HTTP_FIELD_DATE
));
411 strlcpy(buffer
, tempfile
, bufsize
);
413 else if (status
!= HTTP_STATUS_NOT_MODIFIED
)
415 _cupsSetHTTPError(status
);
419 else if (tempfile
[0])
422 else if (tempfile
[0])
429 * Return the PPD file...
432 DEBUG_printf(("1cupsGetPPD3: Returning status %d", status
));
439 * 'cupsGetServerPPD()' - Get an available PPD file from the server.
441 * This function returns the named PPD file from the server. The
442 * list of available PPDs is provided by the IPP @code CUPS_GET_PPDS@
445 * You must remove (unlink) the PPD file when you are finished with
446 * it. The PPD filename is stored in a static location that will be
447 * overwritten on the next call to @link cupsGetPPD@, @link cupsGetPPD2@,
448 * or @link cupsGetServerPPD@.
450 * @since CUPS 1.3/macOS 10.5@
453 char * /* O - Name of PPD file or @code NULL@ on error */
454 cupsGetServerPPD(http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
455 const char *name
) /* I - Name of PPD file ("ppd-name") */
457 int fd
; /* PPD file descriptor */
458 ipp_t
*request
; /* IPP request */
459 _ppd_globals_t
*pg
= _ppdGlobals();
460 /* Pointer to library globals */
464 * Range check input...
469 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No PPD name"), 1);
475 if ((http
= _cupsConnect()) == NULL
)
482 if ((fd
= cupsTempFd(pg
->ppd_filename
, sizeof(pg
->ppd_filename
))) < 0)
485 * Can't open file; close the server connection and return NULL...
488 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, NULL
, 0);
494 * Get the PPD file...
497 request
= ippNewRequest(IPP_OP_CUPS_GET_PPD
);
498 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_NAME
, "ppd-name", NULL
,
501 ippDelete(cupsDoIORequest(http
, request
, "/", -1, fd
));
505 if (cupsLastError() != IPP_STATUS_OK
)
507 unlink(pg
->ppd_filename
);
511 return (pg
->ppd_filename
);
516 * 'cups_get_printer_uri()' - Get the printer-uri-supported attribute for the
517 * first printer in a class.
520 static int /* O - 1 on success, 0 on failure */
521 cups_get_printer_uri(
522 http_t
*http
, /* I - Connection to server */
523 const char *name
, /* I - Name of printer or class */
524 char *host
, /* I - Hostname buffer */
525 int hostsize
, /* I - Size of hostname buffer */
526 int *port
, /* O - Port number */
527 char *resource
, /* I - Resource buffer */
528 int resourcesize
, /* I - Size of resource buffer */
529 int depth
) /* I - Depth of query */
531 int i
; /* Looping var */
532 ipp_t
*request
, /* IPP request */
533 *response
; /* IPP response */
534 ipp_attribute_t
*attr
; /* Current attribute */
535 char uri
[HTTP_MAX_URI
], /* printer-uri attribute */
536 scheme
[HTTP_MAX_URI
], /* Scheme name */
537 username
[HTTP_MAX_URI
]; /* Username:password */
538 static const char * const requested_attrs
[] =
539 { /* Requested attributes */
541 "printer-uri-supported"
545 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
));
548 * Setup the printer URI...
551 if (httpAssembleURIf(HTTP_URI_CODING_ALL
, uri
, sizeof(uri
), "ipp", NULL
, "localhost", 0, "/printers/%s", name
) < HTTP_URI_STATUS_OK
)
553 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("Unable to create printer-uri"), 1);
561 DEBUG_printf(("5cups_get_printer_uri: printer-uri=\"%s\"", uri
));
564 * Build an IPP_GET_PRINTER_ATTRIBUTES request, which requires the following
568 * attributes-natural-language
570 * requested-attributes
573 request
= ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES
);
575 ippAddString(request
, IPP_TAG_OPERATION
, IPP_TAG_URI
, "printer-uri", NULL
, uri
);
577 ippAddStrings(request
, IPP_TAG_OPERATION
, IPP_TAG_KEYWORD
, "requested-attributes", sizeof(requested_attrs
) / sizeof(requested_attrs
[0]), NULL
, requested_attrs
);
580 * Do the request and get back a response...
583 snprintf(resource
, (size_t)resourcesize
, "/printers/%s", name
);
585 if ((response
= cupsDoRequest(http
, request
, resource
)) != NULL
)
587 if ((attr
= ippFindAttribute(response
, "member-uris", IPP_TAG_URI
)) != NULL
)
590 * Get the first actual printer name in the class...
593 DEBUG_printf(("5cups_get_printer_uri: Got member-uris with %d values.", ippGetCount(attr
)));
595 for (i
= 0; i
< attr
->num_values
; i
++)
597 DEBUG_printf(("5cups_get_printer_uri: member-uris[%d]=\"%s\"", i
, ippGetString(attr
, i
, NULL
)));
599 httpSeparateURI(HTTP_URI_CODING_ALL
, attr
->values
[i
].string
.text
, scheme
, sizeof(scheme
), username
, sizeof(username
), host
, hostsize
, port
, resource
, resourcesize
);
600 if (!strncmp(resource
, "/printers/", 10))
608 DEBUG_printf(("5cups_get_printer_uri: Found printer member with host=\"%s\", port=%d, resource=\"%s\"", host
, *port
, resource
));
613 else if ((attr
= ippFindAttribute(response
, "printer-uri-supported", IPP_TAG_URI
)) != NULL
)
615 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
);
618 DEBUG_printf(("5cups_get_printer_uri: Resolved to host=\"%s\", port=%d, resource=\"%s\"", host
, *port
, resource
));
620 if (!strncmp(resource
, "/classes/", 9))
622 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No printer-uri found for class"), 1);
627 DEBUG_puts("5cups_get_printer_uri: Not returning class.");
637 if (cupsLastError() != IPP_STATUS_ERROR_NOT_FOUND
)
638 _cupsSetError(IPP_STATUS_ERROR_INTERNAL
, _("No printer-uri found"), 1);
643 DEBUG_puts("5cups_get_printer_uri: Printer URI not found.");