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