/src/libcups/cups/http-support.c
Line | Count | Source (jump to first uncovered line) |
1 | | // |
2 | | // HTTP support routines for CUPS. |
3 | | // |
4 | | // Copyright © 2020-2023 by OpenPrinting |
5 | | // Copyright © 2007-2019 by Apple Inc. |
6 | | // Copyright © 1997-2007 by Easy Software Products, all rights reserved. |
7 | | // |
8 | | // Licensed under Apache License v2.0. See the file "LICENSE" for more |
9 | | // information. |
10 | | // |
11 | | |
12 | | #include "cups-private.h" |
13 | | #include "dnssd.h" |
14 | | |
15 | | |
16 | | // |
17 | | // Local types... |
18 | | // |
19 | | |
20 | | typedef struct _http_uribuf_s // URI buffer |
21 | | { |
22 | | cups_dnssd_t *dnssd; // DNS-SD context |
23 | | char *buffer; // Pointer to buffer |
24 | | size_t bufsize; // Size of buffer |
25 | | http_resolve_t options; // Options passed to httpResolveURI |
26 | | const char *resource; // Resource from URI |
27 | | const char *uuid; // UUID from URI |
28 | | } _http_uribuf_t; |
29 | | |
30 | | |
31 | | // |
32 | | // Local globals... |
33 | | // |
34 | | |
35 | | static const char * const http_days[7] =// Days of the week |
36 | | { |
37 | | "Sun", |
38 | | "Mon", |
39 | | "Tue", |
40 | | "Wed", |
41 | | "Thu", |
42 | | "Fri", |
43 | | "Sat" |
44 | | }; |
45 | | static const char * const http_months[12] = |
46 | | { // Months of the year |
47 | | "Jan", |
48 | | "Feb", |
49 | | "Mar", |
50 | | "Apr", |
51 | | "May", |
52 | | "Jun", |
53 | | "Jul", |
54 | | "Aug", |
55 | | "Sep", |
56 | | "Oct", |
57 | | "Nov", |
58 | | "Dec" |
59 | | }; |
60 | | static const char * const http_states[] = |
61 | | { // HTTP state strings |
62 | | "ERROR", |
63 | | "WAITING", |
64 | | "CONNECT", |
65 | | "COPY", |
66 | | "COPY-send", |
67 | | "DELETE", |
68 | | "DELETE-send", |
69 | | "GET", |
70 | | "GET-send", |
71 | | "HEAD", |
72 | | "LOCK", |
73 | | "LOCK-recv", |
74 | | "LOCK-send", |
75 | | "MKCOL", |
76 | | "MOVE", |
77 | | "MOVE-send", |
78 | | "OPTIONS", |
79 | | "POST", |
80 | | "POST-recv", |
81 | | "POST-send", |
82 | | "PROPFIND", |
83 | | "PROPFIND-recv", |
84 | | "PROPFIND-send", |
85 | | "PROPPATCH", |
86 | | "PROPPATCH-recv", |
87 | | "PROPPATCH-send", |
88 | | "PUT", |
89 | | "PUT-recv", |
90 | | "TRACE", |
91 | | "UNLOCK", |
92 | | "STATUS", |
93 | | "UNKNOWN_METHOD", |
94 | | "UNKNOWN_VERSION" |
95 | | }; |
96 | | |
97 | | |
98 | | // |
99 | | // Local functions... |
100 | | // |
101 | | |
102 | | static const char *http_copy_decode(char *dst, const char *src, size_t dstsize, const char *term, bool decode); |
103 | | static char *http_copy_encode(char *dst, const char *src, char *dstend, const char *reserved, const char *term, bool encode); |
104 | | static void http_resolve_cb(cups_dnssd_resolve_t *res, void *cb_data, cups_dnssd_flags_t flags, uint32_t if_index, const char *fullname, const char *host, uint16_t port, size_t num_txt, cups_option_t *txt); |
105 | | |
106 | | |
107 | | // |
108 | | // 'httpAssembleURI()' - Assemble a uniform resource identifier from its |
109 | | // components. |
110 | | // |
111 | | // This function escapes reserved characters in the URI depending on the |
112 | | // value of the "encoding" argument. You should use this function in |
113 | | // place of traditional string functions whenever you need to create a |
114 | | // URI string. |
115 | | // |
116 | | |
117 | | http_uri_status_t // O - URI status |
118 | | httpAssembleURI( |
119 | | http_uri_coding_t encoding, // I - Encoding flags |
120 | | char *uri, // I - URI buffer |
121 | | size_t urilen, // I - Size of URI buffer |
122 | | const char *scheme, // I - Scheme name |
123 | | const char *username, // I - Username |
124 | | const char *host, // I - Hostname or address |
125 | | int port, // I - Port number |
126 | | const char *resource) // I - Resource |
127 | 0 | { |
128 | 0 | char *ptr, // Pointer into URI buffer |
129 | 0 | *end; // End of URI buffer |
130 | | |
131 | | |
132 | | // Range check input... |
133 | 0 | if (!uri || urilen < 1 || !scheme || port < 0) |
134 | 0 | { |
135 | 0 | if (uri) |
136 | 0 | *uri = '\0'; |
137 | |
|
138 | 0 | return (HTTP_URI_STATUS_BAD_ARGUMENTS); |
139 | 0 | } |
140 | | |
141 | | // Assemble the URI starting with the scheme... |
142 | 0 | end = uri + urilen - 1; |
143 | 0 | ptr = http_copy_encode(uri, scheme, end, NULL, NULL, false); |
144 | |
|
145 | 0 | if (!ptr) |
146 | 0 | goto assemble_overflow; |
147 | | |
148 | 0 | if (!strcmp(scheme, "geo") || !strcmp(scheme, "mailto") || !strcmp(scheme, "tel")) |
149 | 0 | { |
150 | | // geo:, mailto:, and tel: only have :, no //... |
151 | 0 | if (ptr < end) |
152 | 0 | *ptr++ = ':'; |
153 | 0 | else |
154 | 0 | goto assemble_overflow; |
155 | 0 | } |
156 | 0 | else |
157 | 0 | { |
158 | | // Schemes other than geo:, mailto:, and tel: typically have //... |
159 | 0 | if ((ptr + 2) < end) |
160 | 0 | { |
161 | 0 | *ptr++ = ':'; |
162 | 0 | *ptr++ = '/'; |
163 | 0 | *ptr++ = '/'; |
164 | 0 | } |
165 | 0 | else |
166 | 0 | goto assemble_overflow; |
167 | 0 | } |
168 | | |
169 | | // Next the username and hostname, if any... |
170 | 0 | if (host) |
171 | 0 | { |
172 | 0 | const char *hostptr; // Pointer into hostname |
173 | 0 | int have_ipv6; // Do we have an IPv6 address? |
174 | |
|
175 | 0 | if (username && *username) |
176 | 0 | { |
177 | | // Add username@ first... |
178 | 0 | ptr = http_copy_encode(ptr, username, end, "/?#[]@", NULL, encoding & HTTP_URI_CODING_USERNAME); |
179 | |
|
180 | 0 | if (!ptr) |
181 | 0 | goto assemble_overflow; |
182 | | |
183 | 0 | if (ptr < end) |
184 | 0 | *ptr++ = '@'; |
185 | 0 | else |
186 | 0 | goto assemble_overflow; |
187 | 0 | } |
188 | | |
189 | | // Then add the hostname. Since IPv6 is a particular pain to deal |
190 | | // with, we have several special cases to deal with. If we get |
191 | | // an IPv6 address with brackets around it, assume it is already in |
192 | | // URI format. Since DNS-SD service names can sometimes look like |
193 | | // raw IPv6 addresses, we specifically look for "._tcp" in the name, |
194 | | // too... |
195 | 0 | for (hostptr = host, have_ipv6 = strchr(host, ':') && !strstr(host, "._tcp"); *hostptr && have_ipv6; hostptr ++) |
196 | 0 | { |
197 | 0 | if (*hostptr != ':' && !isxdigit(*hostptr & 255)) |
198 | 0 | { |
199 | 0 | have_ipv6 = *hostptr == '%'; |
200 | 0 | break; |
201 | 0 | } |
202 | 0 | } |
203 | |
|
204 | 0 | if (have_ipv6) |
205 | 0 | { |
206 | | // We have a raw IPv6 address... |
207 | 0 | if (strchr(host, '%') && !(encoding & HTTP_URI_CODING_RFC6874)) |
208 | 0 | { |
209 | | // We have a link-local address, add "[v1." prefix... |
210 | 0 | if ((ptr + 4) < end) |
211 | 0 | { |
212 | 0 | *ptr++ = '['; |
213 | 0 | *ptr++ = 'v'; |
214 | 0 | *ptr++ = '1'; |
215 | 0 | *ptr++ = '.'; |
216 | 0 | } |
217 | 0 | else |
218 | 0 | goto assemble_overflow; |
219 | 0 | } |
220 | 0 | else |
221 | 0 | { |
222 | | // We have a normal (or RFC 6874 link-local) address, add "[" prefix... |
223 | 0 | if (ptr < end) |
224 | 0 | *ptr++ = '['; |
225 | 0 | else |
226 | 0 | goto assemble_overflow; |
227 | 0 | } |
228 | | |
229 | | // Copy the rest of the IPv6 address, and terminate with "]". |
230 | 0 | while (ptr < end && *host) |
231 | 0 | { |
232 | 0 | if (*host == '%') |
233 | 0 | { |
234 | | // Convert/encode zone separator |
235 | 0 | if (encoding & HTTP_URI_CODING_RFC6874) |
236 | 0 | { |
237 | 0 | if (ptr >= (end - 2)) |
238 | 0 | goto assemble_overflow; |
239 | | |
240 | 0 | *ptr++ = '%'; |
241 | 0 | *ptr++ = '2'; |
242 | 0 | *ptr++ = '5'; |
243 | 0 | } |
244 | 0 | else |
245 | 0 | *ptr++ = '+'; |
246 | | |
247 | 0 | host ++; |
248 | 0 | } |
249 | 0 | else |
250 | 0 | *ptr++ = *host++; |
251 | 0 | } |
252 | | |
253 | 0 | if (*host) |
254 | 0 | goto assemble_overflow; |
255 | | |
256 | 0 | if (ptr < end) |
257 | 0 | *ptr++ = ']'; |
258 | 0 | else |
259 | 0 | goto assemble_overflow; |
260 | 0 | } |
261 | 0 | else |
262 | 0 | { |
263 | | // Otherwise, just copy the host string (the extra chars are not in the |
264 | | // "reg-name" ABNF rule; anything <= SP or >= DEL plus % gets automatically |
265 | | // percent-encoded. |
266 | 0 | ptr = http_copy_encode(ptr, host, end, "\"#/:<>?@[\\]^`{|}", NULL, encoding & HTTP_URI_CODING_HOSTNAME); |
267 | |
|
268 | 0 | if (!ptr) |
269 | 0 | goto assemble_overflow; |
270 | 0 | } |
271 | | |
272 | | // Finish things off with the port number... |
273 | 0 | if (!strcmp(scheme, "http") && port == 80) |
274 | 0 | port = 0; |
275 | 0 | else if (!strcmp(scheme, "https") && port == 443) |
276 | 0 | port = 0; |
277 | 0 | else if ((!strcmp(scheme, "ipp") || !strcmp(scheme, "ipps")) && port == 631) |
278 | 0 | port = 0; |
279 | |
|
280 | 0 | if (port > 0) |
281 | 0 | { |
282 | 0 | snprintf(ptr, (size_t)(end - ptr + 1), ":%d", port); |
283 | 0 | ptr += strlen(ptr); |
284 | |
|
285 | 0 | if (ptr >= end) |
286 | 0 | goto assemble_overflow; |
287 | 0 | } |
288 | 0 | } |
289 | | |
290 | | // Last but not least, add the resource string... |
291 | 0 | if (resource) |
292 | 0 | { |
293 | 0 | char *query; // Pointer to query string |
294 | | |
295 | | // Copy the resource string up to the query string if present... |
296 | 0 | query = strchr(resource, '?'); |
297 | 0 | ptr = http_copy_encode(ptr, resource, end, NULL, "?", encoding & HTTP_URI_CODING_RESOURCE); |
298 | 0 | if (!ptr) |
299 | 0 | goto assemble_overflow; |
300 | | |
301 | 0 | if (query) |
302 | 0 | { |
303 | | // Copy query string without encoding... |
304 | 0 | ptr = http_copy_encode(ptr, query, end, NULL, NULL, encoding & HTTP_URI_CODING_QUERY); |
305 | 0 | if (!ptr) |
306 | 0 | goto assemble_overflow; |
307 | 0 | } |
308 | 0 | } |
309 | 0 | else if (ptr < end) |
310 | 0 | { |
311 | 0 | *ptr++ = '/'; |
312 | 0 | } |
313 | 0 | else |
314 | 0 | { |
315 | 0 | goto assemble_overflow; |
316 | 0 | } |
317 | | |
318 | | // Nul-terminate the URI buffer and return with no errors... |
319 | 0 | *ptr = '\0'; |
320 | |
|
321 | 0 | return (HTTP_URI_STATUS_OK); |
322 | | |
323 | | // Clear the URI string and return an overflow error; I don't usually |
324 | | // like goto's, but in this case it makes sense... |
325 | 0 | assemble_overflow: |
326 | |
|
327 | 0 | *uri = '\0'; |
328 | 0 | return (HTTP_URI_STATUS_OVERFLOW); |
329 | 0 | } |
330 | | |
331 | | |
332 | | // |
333 | | // 'httpAssembleURIf()' - Assemble a uniform resource identifier from its |
334 | | // components with a formatted resource. |
335 | | // |
336 | | // This function creates a formatted version of the resource string |
337 | | // argument "resourcef" and escapes reserved characters in the URI |
338 | | // depending on the value of the "encoding" argument. You should use |
339 | | // this function in place of traditional string functions whenever |
340 | | // you need to create a URI string. |
341 | | // |
342 | | |
343 | | http_uri_status_t // O - URI status |
344 | | httpAssembleURIf( |
345 | | http_uri_coding_t encoding, // I - Encoding flags |
346 | | char *uri, // I - URI buffer |
347 | | size_t urilen, // I - Size of URI buffer |
348 | | const char *scheme, // I - Scheme name |
349 | | const char *username, // I - Username |
350 | | const char *host, // I - Hostname or address |
351 | | int port, // I - Port number |
352 | | const char *resourcef, // I - Printf-style resource |
353 | | ...) // I - Additional arguments as needed |
354 | 0 | { |
355 | 0 | va_list ap; // Pointer to additional arguments |
356 | 0 | char resource[1024]; // Formatted resource string |
357 | 0 | int bytes; // Bytes in formatted string |
358 | | |
359 | | |
360 | | // Range check input... |
361 | 0 | if (!uri || urilen < 1 || !scheme || port < 0 || !resourcef) |
362 | 0 | { |
363 | 0 | if (uri) |
364 | 0 | *uri = '\0'; |
365 | |
|
366 | 0 | return (HTTP_URI_STATUS_BAD_ARGUMENTS); |
367 | 0 | } |
368 | | |
369 | | // Format the resource string and assemble the URI... |
370 | 0 | va_start(ap, resourcef); |
371 | 0 | bytes = vsnprintf(resource, sizeof(resource), resourcef, ap); |
372 | 0 | va_end(ap); |
373 | |
|
374 | 0 | if ((size_t)bytes >= sizeof(resource)) |
375 | 0 | { |
376 | 0 | *uri = '\0'; |
377 | 0 | return (HTTP_URI_STATUS_OVERFLOW); |
378 | 0 | } |
379 | 0 | else |
380 | 0 | { |
381 | 0 | return (httpAssembleURI(encoding, uri, urilen, scheme, username, host, port, resource)); |
382 | 0 | } |
383 | 0 | } |
384 | | |
385 | | |
386 | | // |
387 | | // 'httpAssembleUUID()' - Assemble a name-based UUID URN conforming to RFC 4122. |
388 | | // |
389 | | // This function creates a unique 128-bit identifying number using the server |
390 | | // name, port number, random data, and optionally an object name and/or object |
391 | | // number. The result is formatted as a UUID URN as defined in RFC 4122. |
392 | | // |
393 | | // The buffer needs to be at least 46 bytes in size. |
394 | | // |
395 | | |
396 | | char * // I - UUID string |
397 | | httpAssembleUUID(const char *server, // I - Server name |
398 | | int port, // I - Port number |
399 | | const char *name, // I - Object name or NULL |
400 | | int number, // I - Object number or 0 |
401 | | char *buffer, // I - String buffer |
402 | | size_t bufsize) // I - Size of buffer |
403 | 0 | { |
404 | 0 | char data[1024]; // Source string for MD5 |
405 | 0 | unsigned char md5sum[16]; // MD5 digest/sum |
406 | | |
407 | | |
408 | | // Build a version 3 UUID conforming to RFC 4122. |
409 | | // |
410 | | // Start with the MD5 sum of the server, port, object name and |
411 | | // number, and some random data on the end. |
412 | 0 | snprintf(data, sizeof(data), "%s:%d:%s:%d:%04x:%04x", server, port, name ? name : server, number, (unsigned)cupsGetRand() & 0xffff, (unsigned)cupsGetRand() & 0xffff); |
413 | |
|
414 | 0 | cupsHashData("md5", (unsigned char *)data, strlen(data), md5sum, sizeof(md5sum)); |
415 | | |
416 | | // Generate the UUID from the MD5... |
417 | 0 | snprintf(buffer, bufsize, "urn:uuid:%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", md5sum[0], md5sum[1], md5sum[2], md5sum[3], md5sum[4], md5sum[5], (md5sum[6] & 15) | 0x30, md5sum[7], (md5sum[8] & 0x3f) | 0x40, md5sum[9], md5sum[10], md5sum[11], md5sum[12], md5sum[13], md5sum[14], md5sum[15]); |
418 | |
|
419 | 0 | return (buffer); |
420 | 0 | } |
421 | | |
422 | | |
423 | | // |
424 | | // 'httpDecode64()' - Base64-decode a string. |
425 | | // |
426 | | // This function decodes a Base64 string as defined by RFC 4648. The caller |
427 | | // must initialize "outlen" to the maximum size of the decoded string. On |
428 | | // return "outlen" contains the decoded length of the string and "end" (if not |
429 | | // `NULL`) points to the end of the Base64 data that has been decoded. |
430 | | // |
431 | | // This function always reserves one byte in the output buffer for a nul |
432 | | // terminating character, even if the result is not a regular string. Callers |
433 | | // should ensure that the output buffer is at least one byte larger than the |
434 | | // expected size, for example 33 bytes for a SHA-256 hash which is 32 bytes in |
435 | | // length. |
436 | | // |
437 | | // This function supports both Base64 and Base64url strings. |
438 | | // |
439 | | |
440 | | char * // O - Decoded string or `NULL` on error |
441 | | httpDecode64(char *out, // I - String to write to |
442 | | size_t *outlen, // IO - Size of output string |
443 | | const char *in, // I - String to read from |
444 | | const char **end) // O - Pointer to end of Base64 data (`NULL` if don't care) |
445 | 0 | { |
446 | 0 | int pos; // Bit position |
447 | 0 | unsigned base64; // Value of this character |
448 | 0 | char *outptr, // Output pointer |
449 | 0 | *outend; // End of output buffer |
450 | | |
451 | | |
452 | | // Range check input... |
453 | 0 | if (!out || !outlen || *outlen < 1 || !in) |
454 | 0 | return (NULL); |
455 | | |
456 | 0 | if (!*in) |
457 | 0 | { |
458 | 0 | *out = '\0'; |
459 | 0 | *outlen = 0; |
460 | |
|
461 | 0 | if (end) |
462 | 0 | *end = in; |
463 | |
|
464 | 0 | return (out); |
465 | 0 | } |
466 | | |
467 | | // Convert from base-64 to bytes... |
468 | 0 | for (outptr = out, outend = out + *outlen - 1, pos = 0; *in != '\0'; in ++) |
469 | 0 | { |
470 | | // Decode this character into a number from 0 to 63... |
471 | 0 | if (*in >= 'A' && *in <= 'Z') |
472 | 0 | base64 = (unsigned)(*in - 'A'); |
473 | 0 | else if (*in >= 'a' && *in <= 'z') |
474 | 0 | base64 = (unsigned)(*in - 'a' + 26); |
475 | 0 | else if (*in >= '0' && *in <= '9') |
476 | 0 | base64 = (unsigned)(*in - '0' + 52); |
477 | 0 | else if (*in == '+' || *in == '-') |
478 | 0 | base64 = 62; |
479 | 0 | else if (*in == '/' || *in == '_') |
480 | 0 | base64 = 63; |
481 | 0 | else if (*in == '=') |
482 | 0 | break; |
483 | 0 | else if (isspace(*in & 255)) |
484 | 0 | continue; |
485 | 0 | else |
486 | 0 | break; |
487 | | |
488 | | // Store the result in the appropriate chars... |
489 | 0 | switch (pos) |
490 | 0 | { |
491 | 0 | case 0 : |
492 | 0 | if (outptr < outend) |
493 | 0 | *outptr = (char)(base64 << 2); |
494 | 0 | pos ++; |
495 | 0 | break; |
496 | 0 | case 1 : |
497 | 0 | if (outptr < outend) |
498 | 0 | *outptr++ |= (char)((base64 >> 4) & 3); |
499 | 0 | if (outptr < outend) |
500 | 0 | *outptr = (char)((base64 << 4) & 255); |
501 | 0 | pos ++; |
502 | 0 | break; |
503 | 0 | case 2 : |
504 | 0 | if (outptr < outend) |
505 | 0 | *outptr++ |= (char)((base64 >> 2) & 15); |
506 | 0 | if (outptr < outend) |
507 | 0 | *outptr = (char)((base64 << 6) & 255); |
508 | 0 | pos ++; |
509 | 0 | break; |
510 | 0 | case 3 : |
511 | 0 | if (outptr < outend) |
512 | 0 | *outptr++ |= (char)base64; |
513 | 0 | pos = 0; |
514 | 0 | break; |
515 | 0 | } |
516 | 0 | } |
517 | | |
518 | | // Add a trailing nul... |
519 | 0 | *outptr = '\0'; |
520 | | |
521 | | // Skip trailing '='... |
522 | 0 | while (*in == '=') |
523 | 0 | in ++; |
524 | | |
525 | | // Return the decoded string, next input pointer, and size... |
526 | 0 | *outlen = (size_t)(outptr - out); |
527 | |
|
528 | 0 | if (end) |
529 | 0 | *end = in; |
530 | |
|
531 | 0 | return (out); |
532 | 0 | } |
533 | | |
534 | | |
535 | | // |
536 | | // 'httpEncode64()' - Base64-encode a string. |
537 | | // |
538 | | // This function encodes a Base64 string as defined by RFC 4648. The "url" |
539 | | // parameter controls whether the original Base64 ("url" = `false`) or the |
540 | | // Base64url ("url" = `true`) alphabet is used. |
541 | | // |
542 | | |
543 | | char * // O - Encoded string |
544 | | httpEncode64(char *out, // I - String to write to |
545 | | size_t outlen, // I - Maximum size of output string |
546 | | const char *in, // I - String to read from |
547 | | size_t inlen, // I - Size of input string |
548 | | bool url) // I - `true` for Base64url, `false` for Base64 |
549 | 0 | { |
550 | 0 | char *outptr, // Output pointer |
551 | 0 | *outend; // End of output buffer |
552 | 0 | const char *alpha; // Alphabet |
553 | 0 | static const char *base64 = // Base64 alphabet |
554 | 0 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
555 | 0 | static const char *base64url = // Base64url alphabet |
556 | 0 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; |
557 | | |
558 | | |
559 | | // Range check input... |
560 | 0 | if (!out || outlen < 1 || !in) |
561 | 0 | return (NULL); |
562 | | |
563 | | // Encode bytes... |
564 | 0 | alpha = url ? base64url : base64; |
565 | |
|
566 | 0 | for (outptr = out, outend = out + outlen - 1; inlen > 0; in ++, inlen --) |
567 | 0 | { |
568 | | // Encode the up to 3 characters as 4 Base64 numbers... |
569 | 0 | if (outptr < outend) |
570 | 0 | *outptr ++ = alpha[(in[0] & 255) >> 2]; |
571 | |
|
572 | 0 | if (outptr < outend) |
573 | 0 | { |
574 | 0 | if (inlen > 1) |
575 | 0 | *outptr ++ = alpha[(((in[0] & 255) << 4) | ((in[1] & 255) >> 4)) & 63]; |
576 | 0 | else |
577 | 0 | *outptr ++ = alpha[((in[0] & 255) << 4) & 63]; |
578 | 0 | } |
579 | |
|
580 | 0 | in ++; |
581 | 0 | inlen --; |
582 | 0 | if (inlen <= 0) |
583 | 0 | { |
584 | 0 | if (!url && outptr < outend) |
585 | 0 | *outptr ++ = '='; |
586 | 0 | if (!url && outptr < outend) |
587 | 0 | *outptr ++ = '='; |
588 | 0 | break; |
589 | 0 | } |
590 | | |
591 | 0 | if (outptr < outend) |
592 | 0 | { |
593 | 0 | if (inlen > 1) |
594 | 0 | *outptr ++ = alpha[(((in[0] & 255) << 2) | ((in[1] & 255) >> 6)) & 63]; |
595 | 0 | else |
596 | 0 | *outptr ++ = alpha[((in[0] & 255) << 2) & 63]; |
597 | 0 | } |
598 | |
|
599 | 0 | in ++; |
600 | 0 | inlen --; |
601 | 0 | if (inlen <= 0) |
602 | 0 | { |
603 | 0 | if (!url && outptr < outend) |
604 | 0 | *outptr ++ = '='; |
605 | 0 | break; |
606 | 0 | } |
607 | | |
608 | 0 | if (outptr < outend) |
609 | 0 | *outptr ++ = alpha[in[0] & 63]; |
610 | 0 | } |
611 | |
|
612 | 0 | *outptr = '\0'; |
613 | | |
614 | | // Return the encoded string... |
615 | 0 | return (out); |
616 | 0 | } |
617 | | |
618 | | |
619 | | // |
620 | | // 'httpGetDateString()' - Get a formatted date/time string from a time value. |
621 | | // |
622 | | |
623 | | const char * // O - Date/time string |
624 | | httpGetDateString(time_t t, // I - Time in seconds |
625 | | char *s, // I - String buffer |
626 | | size_t slen) // I - Size of string buffer |
627 | 0 | { |
628 | 0 | struct tm tdate; // UNIX date/time data |
629 | | |
630 | |
|
631 | 0 | gmtime_r(&t, &tdate); |
632 | |
|
633 | 0 | snprintf(s, slen, "%s, %02d %s %d %02d:%02d:%02d GMT", http_days[tdate.tm_wday], tdate.tm_mday, http_months[tdate.tm_mon], tdate.tm_year + 1900, tdate.tm_hour, tdate.tm_min, tdate.tm_sec); |
634 | |
|
635 | 0 | return (s); |
636 | 0 | } |
637 | | |
638 | | |
639 | | // |
640 | | // 'httpGetDateTime()' - Get a time value from a formatted date/time string. |
641 | | // |
642 | | |
643 | | time_t // O - Time in seconds |
644 | | httpGetDateTime(const char *s) // I - Date/time string |
645 | 0 | { |
646 | 0 | int i; // Looping var |
647 | 0 | char mon[16]; // Abbreviated month name |
648 | 0 | int day, year; // Day of month and year |
649 | 0 | int hour, min, sec; // Time |
650 | 0 | int days; // Number of days since 1970 |
651 | 0 | static const int normal_days[] = // Days to a month, normal years |
652 | 0 | { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 }; |
653 | 0 | static const int leap_days[] = // Days to a month, leap years |
654 | 0 | { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }; |
655 | | |
656 | |
|
657 | 0 | DEBUG_printf("2httpGetDateTime(s=\"%s\")", s); |
658 | | |
659 | | // Extract the date and time from the formatted string... |
660 | 0 | if (sscanf(s, "%*s%d%15s%d%d:%d:%d", &day, mon, &year, &hour, &min, &sec) < 6) |
661 | 0 | return (0); |
662 | | |
663 | 0 | DEBUG_printf("4httpGetDateTime: day=%d, mon=\"%s\", year=%d, hour=%d, min=%d, sec=%d", day, mon, year, hour, min, sec); |
664 | | |
665 | | // Check for invalid year (RFC 7231 says it's 4DIGIT) |
666 | 0 | if (year > 9999) |
667 | 0 | return (0); |
668 | | |
669 | | // Convert the month name to a number from 0 to 11. |
670 | 0 | for (i = 0; i < 12; i ++) |
671 | 0 | if (!_cups_strcasecmp(mon, http_months[i])) |
672 | 0 | break; |
673 | |
|
674 | 0 | if (i >= 12) |
675 | 0 | return (0); |
676 | | |
677 | 0 | DEBUG_printf("4httpGetDateTime: i=%d", i); |
678 | | |
679 | | // Now convert the date and time to a UNIX time value in seconds since |
680 | | // 1970. We can't use mktime() since the timezone may not be UTC but |
681 | | // the date/time string *is* UTC. |
682 | 0 | if ((year & 3) == 0 && ((year % 100) != 0 || (year % 400) == 0)) |
683 | 0 | days = leap_days[i] + day - 1; |
684 | 0 | else |
685 | 0 | days = normal_days[i] + day - 1; |
686 | |
|
687 | 0 | DEBUG_printf("4httpGetDateTime: days=%d", days); |
688 | |
|
689 | 0 | days += (year - 1970) * 365 + // 365 days per year (normally) |
690 | 0 | ((year - 1) / 4 - 492) - // + leap days |
691 | 0 | ((year - 1) / 100 - 19) + // - 100 year days |
692 | 0 | ((year - 1) / 400 - 4); // + 400 year days |
693 | |
|
694 | 0 | DEBUG_printf("4httpGetDateTime: days=%d\n", days); |
695 | |
|
696 | 0 | return (days * 86400 + hour * 3600 + min * 60 + sec); |
697 | 0 | } |
698 | | |
699 | | |
700 | | // |
701 | | // 'httpSeparateURI()' - Separate a Universal Resource Identifier into its |
702 | | // components. |
703 | | // |
704 | | |
705 | | http_uri_status_t // O - Result of separation |
706 | | httpSeparateURI( |
707 | | http_uri_coding_t decoding, // I - Decoding flags |
708 | | const char *uri, // I - Universal Resource Identifier |
709 | | char *scheme, // O - Scheme (http, https, etc.) |
710 | | size_t schemelen, // I - Size of scheme buffer |
711 | | char *username, // O - Username |
712 | | size_t usernamelen, // I - Size of username buffer |
713 | | char *host, // O - Hostname |
714 | | size_t hostlen, // I - Size of hostname buffer |
715 | | int *port, // O - Port number to use |
716 | | char *resource, // O - Resource/filename |
717 | | size_t resourcelen) // I - Size of resource buffer |
718 | 0 | { |
719 | 0 | char *ptr, // Pointer into string... |
720 | 0 | *end; // End of string |
721 | 0 | const char *sep; // Separator character |
722 | 0 | http_uri_status_t status; // Result of separation |
723 | | |
724 | | |
725 | | // Initialize everything to blank... |
726 | 0 | if (scheme && schemelen > 0) |
727 | 0 | *scheme = '\0'; |
728 | |
|
729 | 0 | if (username && usernamelen > 0) |
730 | 0 | *username = '\0'; |
731 | |
|
732 | 0 | if (host && hostlen > 0) |
733 | 0 | *host = '\0'; |
734 | |
|
735 | 0 | if (port) |
736 | 0 | *port = 0; |
737 | |
|
738 | 0 | if (resource && resourcelen > 0) |
739 | 0 | *resource = '\0'; |
740 | | |
741 | | // Range check input... |
742 | 0 | if (!uri || !port || !scheme || schemelen == 0 || !username || usernamelen == 0 || !host || hostlen == 0 || !resource || resourcelen == 0) |
743 | 0 | return (HTTP_URI_STATUS_BAD_ARGUMENTS); |
744 | | |
745 | 0 | if (!*uri) |
746 | 0 | return (HTTP_URI_STATUS_BAD_URI); |
747 | | |
748 | | // Grab the scheme portion of the URI... |
749 | 0 | status = HTTP_URI_STATUS_OK; |
750 | |
|
751 | 0 | if (!strncmp(uri, "//", 2)) |
752 | 0 | { |
753 | | // Workaround for HP IPP client bug... |
754 | 0 | cupsCopyString(scheme, "ipp", (size_t)schemelen); |
755 | 0 | status = HTTP_URI_STATUS_MISSING_SCHEME; |
756 | 0 | } |
757 | 0 | else if (*uri == '/') |
758 | 0 | { |
759 | | // Filename... |
760 | 0 | cupsCopyString(scheme, "file", (size_t)schemelen); |
761 | 0 | status = HTTP_URI_STATUS_MISSING_SCHEME; |
762 | 0 | } |
763 | 0 | else |
764 | 0 | { |
765 | | // Standard URI with scheme... |
766 | 0 | for (ptr = scheme, end = scheme + schemelen - 1; *uri && *uri != ':' && ptr < end;) |
767 | 0 | { |
768 | 0 | if (strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-+.", *uri) != NULL) |
769 | 0 | *ptr++ = *uri++; |
770 | 0 | else |
771 | 0 | break; |
772 | 0 | } |
773 | |
|
774 | 0 | *ptr = '\0'; |
775 | |
|
776 | 0 | if (*uri != ':' || *scheme == '.' || !*scheme) |
777 | 0 | { |
778 | 0 | *scheme = '\0'; |
779 | 0 | return (HTTP_URI_STATUS_BAD_SCHEME); |
780 | 0 | } |
781 | | |
782 | 0 | uri ++; |
783 | 0 | } |
784 | | |
785 | | // Set the default port number... |
786 | 0 | if (!strcmp(scheme, "http")) |
787 | 0 | *port = 80; |
788 | 0 | else if (!strcmp(scheme, "https")) |
789 | 0 | *port = 443; |
790 | 0 | else if (!strcmp(scheme, "ipp") || !strcmp(scheme, "ipps")) |
791 | 0 | *port = 631; |
792 | 0 | else if (!_cups_strcasecmp(scheme, "lpd")) |
793 | 0 | *port = 515; |
794 | 0 | else if (!strcmp(scheme, "socket")) // Not yet registered with IANA... |
795 | 0 | *port = 9100; |
796 | 0 | else if (strcmp(scheme, "file") && strcmp(scheme, "mailto") && strcmp(scheme, "tel")) |
797 | 0 | status = HTTP_URI_STATUS_UNKNOWN_SCHEME; |
798 | | |
799 | | // Now see if we have a hostname... |
800 | 0 | if (!strncmp(uri, "//", 2)) |
801 | 0 | { |
802 | | // Yes, extract it... |
803 | 0 | uri += 2; |
804 | | |
805 | | // Grab the username, if any... |
806 | 0 | if ((sep = strpbrk(uri, "@/")) != NULL && *sep == '@') |
807 | 0 | { |
808 | | // Get a username:password combo... |
809 | 0 | uri = http_copy_decode(username, uri, usernamelen, "@", decoding & HTTP_URI_CODING_USERNAME); |
810 | |
|
811 | 0 | if (!uri) |
812 | 0 | { |
813 | 0 | *username = '\0'; |
814 | 0 | return (HTTP_URI_STATUS_BAD_USERNAME); |
815 | 0 | } |
816 | | |
817 | 0 | uri ++; |
818 | 0 | } |
819 | | |
820 | | // Then the hostname/IP address... |
821 | 0 | if (*uri == '[') |
822 | 0 | { |
823 | | // Grab IPv6 address... |
824 | 0 | uri ++; |
825 | 0 | if (*uri == 'v') |
826 | 0 | { |
827 | | // Skip IPvFuture ("vXXXX.") prefix... |
828 | 0 | uri ++; |
829 | |
|
830 | 0 | while (isxdigit(*uri & 255)) |
831 | 0 | uri ++; |
832 | |
|
833 | 0 | if (*uri != '.') |
834 | 0 | { |
835 | 0 | *host = '\0'; |
836 | 0 | return (HTTP_URI_STATUS_BAD_HOSTNAME); |
837 | 0 | } |
838 | | |
839 | 0 | uri ++; |
840 | 0 | } |
841 | | |
842 | 0 | uri = http_copy_decode(host, uri, hostlen, "]", decoding & HTTP_URI_CODING_HOSTNAME); |
843 | |
|
844 | 0 | if (!uri) |
845 | 0 | { |
846 | 0 | *host = '\0'; |
847 | 0 | return (HTTP_URI_STATUS_BAD_HOSTNAME); |
848 | 0 | } |
849 | | |
850 | | // Validate value... |
851 | 0 | if (*uri != ']') |
852 | 0 | { |
853 | 0 | *host = '\0'; |
854 | 0 | return (HTTP_URI_STATUS_BAD_HOSTNAME); |
855 | 0 | } |
856 | | |
857 | 0 | uri ++; |
858 | |
|
859 | 0 | for (ptr = host; *ptr; ptr ++) |
860 | 0 | { |
861 | 0 | if (*ptr == '+') |
862 | 0 | { |
863 | | // Convert zone separator to % and stop here... |
864 | 0 | *ptr = '%'; |
865 | 0 | break; |
866 | 0 | } |
867 | 0 | else if (*ptr == '%') |
868 | 0 | { |
869 | | // Stop at zone separator (RFC 6874) |
870 | 0 | break; |
871 | 0 | } |
872 | 0 | else if (*ptr != ':' && *ptr != '.' && !isxdigit(*ptr & 255)) |
873 | 0 | { |
874 | 0 | *host = '\0'; |
875 | 0 | return (HTTP_URI_STATUS_BAD_HOSTNAME); |
876 | 0 | } |
877 | 0 | } |
878 | 0 | } |
879 | 0 | else |
880 | 0 | { |
881 | | // Validate the hostname or IPv4 address first... |
882 | 0 | for (ptr = (char *)uri; *ptr; ptr ++) |
883 | 0 | { |
884 | 0 | if (strchr(":?/", *ptr)) |
885 | 0 | break; |
886 | 0 | else if (!strchr("abcdefghijklmnopqrstuvwxyz" // unreserved |
887 | 0 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" // unreserved |
888 | 0 | "0123456789" // unreserved |
889 | 0 | "-._~" // unreserved |
890 | 0 | "%" // pct-encoded |
891 | 0 | "!$&'()*+,;=" // sub-delims |
892 | 0 | "\\", *ptr)) // SMB domain |
893 | 0 | { |
894 | 0 | *host = '\0'; |
895 | 0 | return (HTTP_URI_STATUS_BAD_HOSTNAME); |
896 | 0 | } |
897 | 0 | } |
898 | | |
899 | | // Then copy the hostname or IPv4 address to the buffer... |
900 | 0 | uri = http_copy_decode(host, uri, hostlen, ":?/", decoding & HTTP_URI_CODING_HOSTNAME); |
901 | |
|
902 | 0 | if (!uri) |
903 | 0 | { |
904 | 0 | *host = '\0'; |
905 | 0 | return (HTTP_URI_STATUS_BAD_HOSTNAME); |
906 | 0 | } |
907 | 0 | } |
908 | | |
909 | | // Validate hostname for file scheme - only empty and localhost are |
910 | | // acceptable. |
911 | 0 | if (!strcmp(scheme, "file") && strcmp(host, "localhost") && host[0]) |
912 | 0 | { |
913 | 0 | *host = '\0'; |
914 | 0 | return (HTTP_URI_STATUS_BAD_HOSTNAME); |
915 | 0 | } |
916 | | |
917 | | // See if we have a port number... |
918 | 0 | if (*uri == ':') |
919 | 0 | { |
920 | | // Yes, collect the port number... |
921 | 0 | if (!isdigit(uri[1] & 255)) |
922 | 0 | { |
923 | 0 | *port = 0; |
924 | 0 | return (HTTP_URI_STATUS_BAD_PORT); |
925 | 0 | } |
926 | | |
927 | 0 | *port = (int)strtol(uri + 1, (char **)&uri, 10); |
928 | |
|
929 | 0 | if (*port <= 0 || *port > 65535) |
930 | 0 | { |
931 | 0 | *port = 0; |
932 | 0 | return (HTTP_URI_STATUS_BAD_PORT); |
933 | 0 | } |
934 | | |
935 | 0 | if (*uri != '/' && *uri) |
936 | 0 | { |
937 | 0 | *port = 0; |
938 | 0 | return (HTTP_URI_STATUS_BAD_PORT); |
939 | 0 | } |
940 | 0 | } |
941 | 0 | } |
942 | | |
943 | | // The remaining portion is the resource string... |
944 | 0 | if (*uri == '?' || !*uri) |
945 | 0 | { |
946 | | // Hostname but no path... |
947 | 0 | status = HTTP_URI_STATUS_MISSING_RESOURCE; |
948 | 0 | *resource = '/'; |
949 | | |
950 | | // Copy any query string... |
951 | 0 | if (*uri == '?') |
952 | 0 | uri = http_copy_decode(resource + 1, uri, resourcelen - 1, NULL, decoding & HTTP_URI_CODING_QUERY); |
953 | 0 | else |
954 | 0 | resource[1] = '\0'; |
955 | 0 | } |
956 | 0 | else |
957 | 0 | { |
958 | 0 | uri = http_copy_decode(resource, uri, resourcelen, "?", decoding & HTTP_URI_CODING_RESOURCE); |
959 | |
|
960 | 0 | if (uri && *uri == '?') |
961 | 0 | { |
962 | | // Concatenate any query string... |
963 | 0 | char *resptr = resource + strlen(resource); |
964 | |
|
965 | 0 | uri = http_copy_decode(resptr, uri, resourcelen - (size_t)(resptr - resource), NULL, decoding & HTTP_URI_CODING_QUERY); |
966 | 0 | } |
967 | 0 | } |
968 | |
|
969 | 0 | if (!uri) |
970 | 0 | { |
971 | 0 | *resource = '\0'; |
972 | 0 | return (HTTP_URI_STATUS_BAD_RESOURCE); |
973 | 0 | } |
974 | | |
975 | | // Return the URI separation status... |
976 | 0 | return (status); |
977 | 0 | } |
978 | | |
979 | | |
980 | | // |
981 | | // '_httpSetDigestAuthString()' - Calculate a Digest authentication response |
982 | | // using the appropriate RFC 2068/2617/7616 |
983 | | // algorithm. |
984 | | // |
985 | | |
986 | | bool // O - `true` on success, `false` on failure |
987 | | _httpSetDigestAuthString( |
988 | | http_t *http, // I - HTTP connection |
989 | | const char *nonce, // I - Nonce value |
990 | | const char *method, // I - HTTP method |
991 | | const char *resource) // I - HTTP resource path |
992 | 0 | { |
993 | 0 | char kd[65], // Final MD5/SHA-256 digest |
994 | 0 | ha1[65], // Hash of username:realm:password |
995 | 0 | ha2[65], // Hash of method:request-uri |
996 | 0 | username[HTTP_MAX_VALUE], |
997 | | // username:password |
998 | 0 | *password, // Pointer to password |
999 | 0 | temp[1024], // Temporary string |
1000 | 0 | digest[1024]; // Digest auth data |
1001 | 0 | unsigned char hash[32]; // Hash buffer |
1002 | 0 | size_t hashsize; // Size of hash |
1003 | 0 | _cups_globals_t *cg = _cupsGlobals(); // Per-thread globals |
1004 | | |
1005 | |
|
1006 | 0 | DEBUG_printf("2_httpSetDigestAuthString(http=%p, nonce=\"%s\", method=\"%s\", resource=\"%s\")", (void *)http, nonce, method, resource); |
1007 | |
|
1008 | 0 | if (nonce && *nonce && strcmp(nonce, http->nonce)) |
1009 | 0 | { |
1010 | 0 | cupsCopyString(http->nonce, nonce, sizeof(http->nonce)); |
1011 | |
|
1012 | 0 | if (nonce == http->nextnonce) |
1013 | 0 | http->nextnonce[0] = '\0'; |
1014 | |
|
1015 | 0 | http->nonce_count = 1; |
1016 | 0 | } |
1017 | 0 | else |
1018 | 0 | http->nonce_count ++; |
1019 | |
|
1020 | 0 | cupsCopyString(username, http->userpass, sizeof(username)); |
1021 | 0 | if ((password = strchr(username, ':')) != NULL) |
1022 | 0 | *password++ = '\0'; |
1023 | 0 | else |
1024 | 0 | return (false); |
1025 | | |
1026 | 0 | if (http->qop[0]) |
1027 | 0 | { |
1028 | | // Follow RFC 2617/7616... |
1029 | 0 | int i; // Looping var |
1030 | 0 | char cnonce[65]; // cnonce value |
1031 | 0 | const char *hashalg; // Hashing algorithm |
1032 | 0 | const char *qop; // Quality of Protection |
1033 | |
|
1034 | 0 | for (i = 0; i < 64; i ++) |
1035 | 0 | cnonce[i] = "0123456789ABCDEF"[cupsGetRand() & 15]; |
1036 | 0 | cnonce[64] = '\0'; |
1037 | |
|
1038 | 0 | if (!_cups_strcasecmp(http->qop, "auth")) |
1039 | 0 | { |
1040 | | // RFC 2617: "auth" | "auth-int" | token |
1041 | 0 | qop = "auth"; |
1042 | 0 | } |
1043 | 0 | else |
1044 | 0 | { |
1045 | | // Some other qop we don't support, skip this one... |
1046 | 0 | return (false); |
1047 | 0 | } |
1048 | | |
1049 | 0 | if (!http->algorithm[0] || !_cups_strcasecmp(http->algorithm, "MD5")) |
1050 | 0 | { |
1051 | | // RFC 2617 Digest with MD5 |
1052 | 0 | if (cg->digestoptions == _CUPS_DIGESTOPTIONS_DENYMD5) |
1053 | 0 | { |
1054 | 0 | DEBUG_puts("3_httpSetDigestAuthString: MD5 Digest is disabled."); |
1055 | 0 | return (false); |
1056 | 0 | } |
1057 | | |
1058 | 0 | hashalg = "md5"; |
1059 | 0 | } |
1060 | 0 | else if (!_cups_strcasecmp(http->algorithm, "SHA-256")) |
1061 | 0 | { |
1062 | | // RFC 7616 Digest with SHA-256 |
1063 | 0 | hashalg = "sha2-256"; |
1064 | 0 | } |
1065 | 0 | else |
1066 | 0 | { |
1067 | | // Some other algorithm we don't support, skip this one... |
1068 | 0 | return (false); |
1069 | 0 | } |
1070 | | |
1071 | | // Calculate digest value... |
1072 | | |
1073 | | // H(A1) = H(username:realm:password) |
1074 | 0 | snprintf(temp, sizeof(temp), "%s:%s:%s", username, http->realm, password); |
1075 | 0 | hashsize = (size_t)cupsHashData(hashalg, (unsigned char *)temp, strlen(temp), hash, sizeof(hash)); |
1076 | 0 | cupsHashString(hash, hashsize, ha1, sizeof(ha1)); |
1077 | | |
1078 | | // H(A2) = H(method:uri) |
1079 | 0 | snprintf(temp, sizeof(temp), "%s:%s", method, resource); |
1080 | 0 | hashsize = (size_t)cupsHashData(hashalg, (unsigned char *)temp, strlen(temp), hash, sizeof(hash)); |
1081 | 0 | cupsHashString(hash, hashsize, ha2, sizeof(ha2)); |
1082 | | |
1083 | | // KD = H(H(A1):nonce:nc:cnonce:qop:H(A2)) |
1084 | 0 | snprintf(temp, sizeof(temp), "%s:%s:%08x:%s:%s:%s", ha1, http->nonce, http->nonce_count, cnonce, qop, ha2); |
1085 | 0 | hashsize = (size_t)cupsHashData(hashalg, (unsigned char *)temp, strlen(temp), hash, sizeof(hash)); |
1086 | 0 | cupsHashString(hash, hashsize, kd, sizeof(kd)); |
1087 | | |
1088 | | // Pass the RFC 2617/7616 WWW-Authenticate header... |
1089 | 0 | if (http->opaque[0]) |
1090 | 0 | snprintf(digest, sizeof(digest), "username=\"%s\", realm=\"%s\", nonce=\"%s\", algorithm=%s, qop=%s, opaque=\"%s\", cnonce=\"%s\", nc=%08x, uri=\"%s\", response=\"%s\"", cupsGetUser(), http->realm, http->nonce, http->algorithm, qop, http->opaque, cnonce, http->nonce_count, resource, kd); |
1091 | 0 | else |
1092 | 0 | snprintf(digest, sizeof(digest), "username=\"%s\", realm=\"%s\", nonce=\"%s\", algorithm=%s, qop=%s, cnonce=\"%s\", nc=%08x, uri=\"%s\", response=\"%s\"", username, http->realm, http->nonce, http->algorithm, qop, cnonce, http->nonce_count, resource, kd); |
1093 | 0 | } |
1094 | 0 | else |
1095 | 0 | { |
1096 | | // Use old RFC 2069 Digest method... |
1097 | | |
1098 | | // H(A1) = H(username:realm:password) |
1099 | 0 | snprintf(temp, sizeof(temp), "%s:%s:%s", username, http->realm, password); |
1100 | 0 | hashsize = (size_t)cupsHashData("md5", (unsigned char *)temp, strlen(temp), hash, sizeof(hash)); |
1101 | 0 | cupsHashString(hash, hashsize, ha1, sizeof(ha1)); |
1102 | | |
1103 | | // H(A2) = H(method:uri) |
1104 | 0 | snprintf(temp, sizeof(temp), "%s:%s", method, resource); |
1105 | 0 | hashsize = (size_t)cupsHashData("md5", (unsigned char *)temp, strlen(temp), hash, sizeof(hash)); |
1106 | 0 | cupsHashString(hash, hashsize, ha2, sizeof(ha2)); |
1107 | | |
1108 | | // KD = H(H(A1):nonce:H(A2)) |
1109 | 0 | snprintf(temp, sizeof(temp), "%s:%s:%s", ha1, http->nonce, ha2); |
1110 | 0 | hashsize = (size_t)cupsHashData("md5", (unsigned char *)temp, strlen(temp), hash, sizeof(hash)); |
1111 | 0 | cupsHashString(hash, hashsize, kd, sizeof(kd)); |
1112 | | |
1113 | | // Pass the old RFC 2069 WWW-Authenticate header... |
1114 | 0 | snprintf(digest, sizeof(digest), "username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", response=\"%s\"", username, http->realm, http->nonce, resource, kd); |
1115 | 0 | } |
1116 | | |
1117 | 0 | httpSetAuthString(http, "Digest", digest); |
1118 | |
|
1119 | 0 | return (true); |
1120 | 0 | } |
1121 | | |
1122 | | |
1123 | | // |
1124 | | // 'httpStateString()' - Return the string describing a HTTP state value. |
1125 | | // |
1126 | | |
1127 | | const char * // O - State string |
1128 | | httpStateString(http_state_t state) // I - HTTP state value |
1129 | 0 | { |
1130 | 0 | if (state < HTTP_STATE_ERROR || state >= HTTP_STATE_MAX) |
1131 | 0 | return ("HTTP_STATE_???"); |
1132 | 0 | else |
1133 | 0 | return (http_states[state - HTTP_STATE_ERROR]); |
1134 | 0 | } |
1135 | | |
1136 | | |
1137 | | // |
1138 | | // '_httpStatusString()' - Return the localized string describing a HTTP |
1139 | | // status code. |
1140 | | // |
1141 | | // The returned string is localized using the passed message catalog. |
1142 | | // |
1143 | | |
1144 | | const char * // O - Localized status string |
1145 | | _httpStatusString( |
1146 | | cups_lang_t *lang, // I - Language |
1147 | | http_status_t status) // I - HTTP status code |
1148 | 0 | { |
1149 | 0 | const char *s; // Status string |
1150 | 0 | _cups_globals_t *cg = _cupsGlobals(); // Global data |
1151 | | |
1152 | |
|
1153 | 0 | switch (status) |
1154 | 0 | { |
1155 | 0 | case HTTP_STATUS_ERROR : |
1156 | 0 | s = strerror(errno); |
1157 | 0 | break; |
1158 | 0 | case HTTP_STATUS_CONTINUE : |
1159 | 0 | s = _("Continue"); |
1160 | 0 | break; |
1161 | 0 | case HTTP_STATUS_SWITCHING_PROTOCOLS : |
1162 | 0 | s = _("Switching Protocols"); |
1163 | 0 | break; |
1164 | 0 | case HTTP_STATUS_OK : |
1165 | 0 | s = _("OK"); |
1166 | 0 | break; |
1167 | 0 | case HTTP_STATUS_CREATED : |
1168 | 0 | s = _("Created"); |
1169 | 0 | break; |
1170 | 0 | case HTTP_STATUS_ACCEPTED : |
1171 | 0 | s = _("Accepted"); |
1172 | 0 | break; |
1173 | 0 | case HTTP_STATUS_NOT_AUTHORITATIVE : |
1174 | 0 | s = _("Not Authoritative"); |
1175 | 0 | break; |
1176 | 0 | case HTTP_STATUS_NO_CONTENT : |
1177 | 0 | s = _("No Content"); |
1178 | 0 | break; |
1179 | 0 | case HTTP_STATUS_RESET_CONTENT : |
1180 | 0 | s = _("Reset Content"); |
1181 | 0 | break; |
1182 | 0 | case HTTP_STATUS_PARTIAL_CONTENT : |
1183 | 0 | s = _("Partial Content"); |
1184 | 0 | break; |
1185 | 0 | case HTTP_STATUS_MULTI_STATUS : |
1186 | 0 | s = _("Multi-Status"); |
1187 | 0 | break; |
1188 | 0 | case HTTP_STATUS_ALREADY_REPORTED : |
1189 | 0 | s = _("Already Reported"); |
1190 | 0 | break; |
1191 | | |
1192 | 0 | case HTTP_STATUS_MULTIPLE_CHOICES : |
1193 | 0 | s = _("Multiple Choices"); |
1194 | 0 | break; |
1195 | 0 | case HTTP_STATUS_MOVED_PERMANENTLY : |
1196 | 0 | s = _("Moved Permanently"); |
1197 | 0 | break; |
1198 | 0 | case HTTP_STATUS_FOUND : |
1199 | 0 | s = _("Found"); |
1200 | 0 | break; |
1201 | 0 | case HTTP_STATUS_SEE_OTHER : |
1202 | 0 | s = _("See Other"); |
1203 | 0 | break; |
1204 | 0 | case HTTP_STATUS_NOT_MODIFIED : |
1205 | 0 | s = _("Not Modified"); |
1206 | 0 | break; |
1207 | 0 | case HTTP_STATUS_USE_PROXY : |
1208 | 0 | s = _("Use Proxy"); |
1209 | 0 | break; |
1210 | 0 | case HTTP_STATUS_TEMPORARY_REDIRECT : |
1211 | 0 | s = _("Temporary Redirect"); |
1212 | 0 | break; |
1213 | 0 | case HTTP_STATUS_PERMANENT_REDIRECT : |
1214 | 0 | s = _("Permanent Redirect"); |
1215 | 0 | break; |
1216 | | |
1217 | 0 | case HTTP_STATUS_BAD_REQUEST : |
1218 | 0 | s = _("Bad Request"); |
1219 | 0 | break; |
1220 | 0 | case HTTP_STATUS_UNAUTHORIZED : |
1221 | 0 | case HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED : |
1222 | 0 | s = _("Unauthorized"); |
1223 | 0 | break; |
1224 | 0 | case HTTP_STATUS_PAYMENT_REQUIRED : |
1225 | 0 | s = _("Payment Required"); |
1226 | 0 | break; |
1227 | 0 | case HTTP_STATUS_FORBIDDEN : |
1228 | 0 | s = _("Forbidden"); |
1229 | 0 | break; |
1230 | 0 | case HTTP_STATUS_NOT_FOUND : |
1231 | 0 | s = _("Not Found"); |
1232 | 0 | break; |
1233 | 0 | case HTTP_STATUS_METHOD_NOT_ALLOWED : |
1234 | 0 | s = _("Method Now Allowed"); |
1235 | 0 | break; |
1236 | 0 | case HTTP_STATUS_NOT_ACCEPTABLE : |
1237 | 0 | s = _("Not Acceptable"); |
1238 | 0 | break; |
1239 | 0 | case HTTP_STATUS_PROXY_AUTHENTICATION_REQUIRED : |
1240 | 0 | s = _("Proxy Authentication Required"); |
1241 | 0 | break; |
1242 | 0 | case HTTP_STATUS_REQUEST_TIMEOUT : |
1243 | 0 | s = _("Request Timeout"); |
1244 | 0 | break; |
1245 | 0 | case HTTP_STATUS_CONFLICT : |
1246 | 0 | s = _("Conflict"); |
1247 | 0 | break; |
1248 | 0 | case HTTP_STATUS_GONE : |
1249 | 0 | s = _("Gone"); |
1250 | 0 | break; |
1251 | 0 | case HTTP_STATUS_LENGTH_REQUIRED : |
1252 | 0 | s = _("Length Required"); |
1253 | 0 | break; |
1254 | 0 | case HTTP_STATUS_PRECONDITION_FAILED : |
1255 | 0 | s = _("Precondition Failed"); |
1256 | 0 | break; |
1257 | 0 | case HTTP_STATUS_CONTENT_TOO_LARGE : |
1258 | 0 | s = _("Content Too Large"); |
1259 | 0 | break; |
1260 | 0 | case HTTP_STATUS_URI_TOO_LONG : |
1261 | 0 | s = _("URI Too Long"); |
1262 | 0 | break; |
1263 | 0 | case HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE : |
1264 | 0 | s = _("Unsupported Media Type"); |
1265 | 0 | break; |
1266 | 0 | case HTTP_STATUS_RANGE_NOT_SATISFIABLE : |
1267 | 0 | s = _("Range Not Satisfiable"); |
1268 | 0 | break; |
1269 | 0 | case HTTP_STATUS_EXPECTATION_FAILED : |
1270 | 0 | s = _("Expectation Failed"); |
1271 | 0 | break; |
1272 | 0 | case HTTP_STATUS_MISDIRECTED_REQUEST : |
1273 | 0 | s = _("Misdirected Request"); |
1274 | 0 | break; |
1275 | 0 | case HTTP_STATUS_UNPROCESSABLE_CONTENT : |
1276 | 0 | s = _("Unprocessable Content"); |
1277 | 0 | break; |
1278 | 0 | case HTTP_STATUS_LOCKED : |
1279 | 0 | s = _("Locked"); |
1280 | 0 | break; |
1281 | 0 | case HTTP_STATUS_FAILED_DEPENDENCY : |
1282 | 0 | s = _("Failed Dependency"); |
1283 | 0 | break; |
1284 | 0 | case HTTP_STATUS_TOO_EARLY : |
1285 | 0 | s = _("Too Early"); |
1286 | 0 | break; |
1287 | 0 | case HTTP_STATUS_UPGRADE_REQUIRED : |
1288 | 0 | s = _("Upgrade Required"); |
1289 | 0 | break; |
1290 | 0 | case HTTP_STATUS_PRECONDITION_REQUIRED : |
1291 | 0 | s = _("Precondition Required"); |
1292 | 0 | break; |
1293 | 0 | case HTTP_STATUS_TOO_MANY_REQUESTS : |
1294 | 0 | s = _("Too Many Requests"); |
1295 | 0 | break; |
1296 | 0 | case HTTP_STATUS_REQUEST_HEADER_FIELDS_TOO_LARGE : |
1297 | 0 | s = _("Request Header Fields Too Large"); |
1298 | 0 | break; |
1299 | 0 | case HTTP_STATUS_UNAVAILABLE_FOR_LEGAL_REASONS : |
1300 | 0 | s = _("Unavailable for Legal Reasons"); |
1301 | 0 | break; |
1302 | 0 | case HTTP_STATUS_SERVER_ERROR : |
1303 | 0 | s = _("Server Error"); |
1304 | 0 | break; |
1305 | 0 | case HTTP_STATUS_NOT_IMPLEMENTED : |
1306 | 0 | s = _("Not Implemented"); |
1307 | 0 | break; |
1308 | 0 | case HTTP_STATUS_BAD_GATEWAY : |
1309 | 0 | s = _("Bad Gateway"); |
1310 | 0 | break; |
1311 | 0 | case HTTP_STATUS_SERVICE_UNAVAILABLE : |
1312 | 0 | s = _("Service Unavailable"); |
1313 | 0 | break; |
1314 | 0 | case HTTP_STATUS_GATEWAY_TIMEOUT : |
1315 | 0 | s = _("Gateway Timeout"); |
1316 | 0 | break; |
1317 | 0 | case HTTP_STATUS_HTTP_VERSION_NOT_SUPPORTED : |
1318 | 0 | s = _("HTTP Version Not Supported"); |
1319 | 0 | break; |
1320 | 0 | case HTTP_STATUS_INSUFFICIENT_STORAGE : |
1321 | 0 | s = _("Insufficient Storage"); |
1322 | 0 | break; |
1323 | 0 | case HTTP_STATUS_LOOP_DETECTED : |
1324 | 0 | s = _("Loop Detected"); |
1325 | 0 | break; |
1326 | 0 | case HTTP_STATUS_NETWORK_AUTHENTICATION_REQUIRED : |
1327 | 0 | s = _("Network Authentication Required"); |
1328 | 0 | break; |
1329 | 0 | case HTTP_STATUS_CUPS_PKI_ERROR : |
1330 | 0 | s = _("TLS Negotiation Error"); |
1331 | 0 | break; |
1332 | 0 | default : |
1333 | 0 | snprintf(cg->http_status, sizeof(cg->http_status), "%d", (int)status); |
1334 | 0 | return (cg->http_status); |
1335 | 0 | } |
1336 | | |
1337 | 0 | return (cupsLangGetString(lang, s)); |
1338 | 0 | } |
1339 | | |
1340 | | |
1341 | | // |
1342 | | // 'httpStatusString()' - Return a short string describing a HTTP status code. |
1343 | | // |
1344 | | // This function returns a short (localized) string describing a HTTP status |
1345 | | // code. The strings are taken from the IANA HTTP Status Code registry. |
1346 | | // |
1347 | | |
1348 | | const char * // O - Localized status string |
1349 | | httpStatusString(http_status_t status) // I - HTTP status code |
1350 | 0 | { |
1351 | 0 | _cups_globals_t *cg = _cupsGlobals(); // Global data |
1352 | | |
1353 | |
|
1354 | 0 | if (!cg->lang_default) |
1355 | 0 | cg->lang_default = cupsLangDefault(); |
1356 | |
|
1357 | 0 | return (_httpStatusString(cg->lang_default, status)); |
1358 | 0 | } |
1359 | | |
1360 | | |
1361 | | // |
1362 | | // 'httpURIStatusString()' - Return a string describing a URI status value. |
1363 | | // |
1364 | | // This function returns a short (localized) string describing a URI status |
1365 | | // value. |
1366 | | // |
1367 | | |
1368 | | const char * // O - Localized status string |
1369 | | httpURIStatusString( |
1370 | | http_uri_status_t status) // I - URI status value |
1371 | 0 | { |
1372 | 0 | const char *s; // Status string |
1373 | 0 | _cups_globals_t *cg = _cupsGlobals(); // Global data |
1374 | | |
1375 | |
|
1376 | 0 | if (!cg->lang_default) |
1377 | 0 | cg->lang_default = cupsLangDefault(); |
1378 | |
|
1379 | 0 | switch (status) |
1380 | 0 | { |
1381 | 0 | case HTTP_URI_STATUS_OVERFLOW : |
1382 | 0 | s = _("URI too large"); |
1383 | 0 | break; |
1384 | 0 | case HTTP_URI_STATUS_BAD_ARGUMENTS : |
1385 | 0 | s = _("Bad arguments to function"); |
1386 | 0 | break; |
1387 | 0 | case HTTP_URI_STATUS_BAD_RESOURCE : |
1388 | 0 | s = _("Bad resource in URI"); |
1389 | 0 | break; |
1390 | 0 | case HTTP_URI_STATUS_BAD_PORT : |
1391 | 0 | s = _("Bad port number in URI"); |
1392 | 0 | break; |
1393 | 0 | case HTTP_URI_STATUS_BAD_HOSTNAME : |
1394 | 0 | s = _("Bad hostname/address in URI"); |
1395 | 0 | break; |
1396 | 0 | case HTTP_URI_STATUS_BAD_USERNAME : |
1397 | 0 | s = _("Bad username in URI"); |
1398 | 0 | break; |
1399 | 0 | case HTTP_URI_STATUS_BAD_SCHEME : |
1400 | 0 | s = _("Bad scheme in URI"); |
1401 | 0 | break; |
1402 | 0 | case HTTP_URI_STATUS_BAD_URI : |
1403 | 0 | s = _("Bad/empty URI"); |
1404 | 0 | break; |
1405 | 0 | case HTTP_URI_STATUS_OK : |
1406 | 0 | s = _("OK"); |
1407 | 0 | break; |
1408 | 0 | case HTTP_URI_STATUS_MISSING_SCHEME : |
1409 | 0 | s = _("Missing scheme in URI"); |
1410 | 0 | break; |
1411 | 0 | case HTTP_URI_STATUS_UNKNOWN_SCHEME : |
1412 | 0 | s = _("Unknown scheme in URI"); |
1413 | 0 | break; |
1414 | 0 | case HTTP_URI_STATUS_MISSING_RESOURCE : |
1415 | 0 | s = _("Missing resource in URI"); |
1416 | 0 | break; |
1417 | | |
1418 | 0 | default: |
1419 | 0 | s = _("Unknown"); |
1420 | 0 | break; |
1421 | 0 | } |
1422 | | |
1423 | 0 | return (cupsLangGetString(cg->lang_default, s)); |
1424 | 0 | } |
1425 | | |
1426 | | |
1427 | | #ifndef HAVE_HSTRERROR |
1428 | | // |
1429 | | // '_cups_hstrerror()' - hstrerror() emulation function for Solaris and others. |
1430 | | // |
1431 | | |
1432 | | const char * // O - Error string |
1433 | | _cups_hstrerror(int error) // I - Error number |
1434 | | { |
1435 | | static const char * const errors[] = // Error strings |
1436 | | { |
1437 | | "OK", |
1438 | | "Host not found.", |
1439 | | "Try again.", |
1440 | | "Unrecoverable lookup error.", |
1441 | | "No data associated with name." |
1442 | | }; |
1443 | | |
1444 | | |
1445 | | if (error < 0 || error > 4) |
1446 | | return ("Unknown hostname lookup error."); |
1447 | | else |
1448 | | return (errors[error]); |
1449 | | } |
1450 | | #endif // !HAVE_HSTRERROR |
1451 | | |
1452 | | |
1453 | | // |
1454 | | // '_httpDecodeURI()' - Percent-decode a HTTP request URI. |
1455 | | // |
1456 | | |
1457 | | char * // O - Decoded URI or NULL on error |
1458 | | _httpDecodeURI(char *dst, // I - Destination buffer |
1459 | | const char *src, // I - Source URI |
1460 | | size_t dstsize) // I - Size of destination buffer |
1461 | 0 | { |
1462 | 0 | if (http_copy_decode(dst, src, dstsize, NULL, true)) |
1463 | 0 | return (dst); |
1464 | 0 | else |
1465 | 0 | return (NULL); |
1466 | 0 | } |
1467 | | |
1468 | | |
1469 | | // |
1470 | | // '_httpEncodeURI()' - Percent-encode a HTTP request URI. |
1471 | | // |
1472 | | |
1473 | | char * // O - Encoded URI |
1474 | | _httpEncodeURI(char *dst, // I - Destination buffer |
1475 | | const char *src, // I - Source URI |
1476 | | size_t dstsize) // I - Size of destination buffer |
1477 | 0 | { |
1478 | 0 | http_copy_encode(dst, src, dst + dstsize - 1, NULL, NULL, true); |
1479 | 0 | return (dst); |
1480 | 0 | } |
1481 | | |
1482 | | |
1483 | | // |
1484 | | // 'httpResolveURI()' - Resolve a DNS-SD URI. |
1485 | | // |
1486 | | // This function resolves a DNS-SD URI of the form |
1487 | | // "scheme://service-instance-name._protocol._tcp.domain/...". The "options" |
1488 | | // parameter specifies a bitfield of resolution options including: |
1489 | | // |
1490 | | // - `HTTP_RESOLVE_DEFAULT`: Use default options |
1491 | | // - `HTTP_RESOLVE_FQDN`: Resolve the fully-qualified domain name instead of an IP address |
1492 | | // - `HTTP_RESOLVE_FAXOUT`: Resolve the FaxOut service instead of Print (IPP/IPPS) |
1493 | | // |
1494 | | // The "cb" parameter specifies a callback that allows resolution to be |
1495 | | // terminated. The callback is provided the "cb_data" value and returns a |
1496 | | // `bool` value that is `true` to continue and `false` to stop. If no callback |
1497 | | // is specified ("cb" is `NULL`), then this function will block up to 90 seconds |
1498 | | // to resolve the specified URI. |
1499 | | // |
1500 | | |
1501 | | const char * // O - Resolved URI |
1502 | | httpResolveURI( |
1503 | | const char *uri, // I - DNS-SD URI |
1504 | | char *resolved_uri, // I - Buffer for resolved URI |
1505 | | size_t resolved_size, // I - Size of URI buffer |
1506 | | http_resolve_t options, // I - Resolve options |
1507 | | http_resolve_cb_t cb, // I - Continue callback function |
1508 | | void *cb_data) // I - Context pointer for callback |
1509 | 0 | { |
1510 | 0 | char scheme[32], // URI components... |
1511 | 0 | userpass[256], |
1512 | 0 | hostname[1024], |
1513 | 0 | resource[1024]; |
1514 | 0 | int port; |
1515 | | #ifdef DEBUG |
1516 | | http_uri_status_t status; // URI decode status |
1517 | | #endif // DEBUG |
1518 | | |
1519 | |
|
1520 | 0 | DEBUG_printf("httpResolveURI(uri=\"%s\", resolved_uri=%p, resolved_size=" CUPS_LLFMT ", options=0x%x, cb=%p, cb_data=%p)", uri, (void *)resolved_uri, CUPS_LLCAST resolved_size, options, (void *)cb, cb_data); |
1521 | | |
1522 | | // Get the device URI... |
1523 | | #ifdef DEBUG |
1524 | | if ((status = httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, sizeof(scheme), userpass, sizeof(userpass), hostname, sizeof(hostname), &port, resource, sizeof(resource))) < HTTP_URI_STATUS_OK) |
1525 | | #else |
1526 | 0 | if (httpSeparateURI(HTTP_URI_CODING_ALL, uri, scheme, sizeof(scheme), userpass, sizeof(userpass), hostname, sizeof(hostname), &port, resource, sizeof(resource)) < HTTP_URI_STATUS_OK) |
1527 | 0 | #endif // DEBUG |
1528 | 0 | { |
1529 | 0 | DEBUG_printf("2httpResolveURI: httpSeparateURI returned %d.", status); |
1530 | 0 | DEBUG_puts("1httpResolveURI: Returning NULL"); |
1531 | 0 | return (NULL); |
1532 | 0 | } |
1533 | | |
1534 | | // Resolve it as needed... |
1535 | 0 | if (strstr(hostname, "._tcp")) |
1536 | 0 | { |
1537 | 0 | time_t domain_time, // Domain lookup time, if any |
1538 | 0 | end_time; // End time for resolve |
1539 | 0 | cups_dnssd_t *dnssd; // DNS-SD context |
1540 | 0 | uint32_t if_index; // Interface index |
1541 | 0 | char name[256], // Service instance name |
1542 | 0 | regtype[256], // Registration type |
1543 | 0 | domain[256], // Domain name |
1544 | 0 | *uuid, // Pointer to UUID in URI |
1545 | 0 | *uuidend; // Pointer to end of UUID in URI |
1546 | 0 | _http_uribuf_t uribuf; // URI buffer |
1547 | | |
1548 | | // Separate the hostname into service name, registration type, and domain... |
1549 | 0 | if (!cupsDNSSDSeparateFullName(hostname, name, sizeof(name), regtype, sizeof(regtype), domain, sizeof(domain))) |
1550 | 0 | { |
1551 | 0 | DEBUG_puts("2httpResolveURI: Bad hostname, returning NULL"); |
1552 | 0 | return (NULL); |
1553 | 0 | } |
1554 | | |
1555 | 0 | if ((uuid = strstr(resource, "?uuid=")) != NULL) |
1556 | 0 | { |
1557 | 0 | *uuid = '\0'; |
1558 | 0 | uuid += 6; |
1559 | 0 | if ((uuidend = strchr(uuid, '&')) != NULL) |
1560 | 0 | *uuidend = '\0'; |
1561 | 0 | } |
1562 | |
|
1563 | 0 | resolved_uri[0] = '\0'; |
1564 | |
|
1565 | 0 | uribuf.buffer = resolved_uri; |
1566 | 0 | uribuf.bufsize = resolved_size; |
1567 | 0 | uribuf.options = options; |
1568 | 0 | uribuf.resource = resource; |
1569 | 0 | uribuf.uuid = uuid; |
1570 | |
|
1571 | 0 | DEBUG_printf("2httpResolveURI: Resolving name=\"%s\", regtype=\"%s\", domain=\"%s\"\n", name, regtype, domain); |
1572 | |
|
1573 | 0 | uri = NULL; |
1574 | |
|
1575 | 0 | if (!strcmp(scheme, "ippusb")) |
1576 | 0 | if_index = CUPS_DNSSD_IF_INDEX_LOCAL; |
1577 | 0 | else |
1578 | 0 | if_index = CUPS_DNSSD_IF_INDEX_ANY; |
1579 | |
|
1580 | 0 | dnssd = cupsDNSSDNew(NULL, NULL); |
1581 | |
|
1582 | 0 | if (!cupsDNSSDResolveNew(dnssd, if_index, name, regtype, "local.", http_resolve_cb, &uribuf)) |
1583 | 0 | { |
1584 | 0 | cupsDNSSDDelete(dnssd); |
1585 | 0 | return (NULL); |
1586 | 0 | } |
1587 | | |
1588 | 0 | domain_time = time(NULL) + 2; |
1589 | 0 | end_time = time(NULL) + 90; |
1590 | |
|
1591 | 0 | while (!resolved_uri[0] && time(NULL) < end_time) |
1592 | 0 | { |
1593 | | // Start the domain resolve as needed... |
1594 | 0 | if (time(NULL) >= domain_time && _cups_strcasecmp(domain, "local.")) |
1595 | 0 | { |
1596 | 0 | cupsDNSSDResolveNew(dnssd, if_index, name, regtype, domain, http_resolve_cb, &uribuf); |
1597 | 0 | domain_time = end_time; |
1598 | 0 | } |
1599 | | |
1600 | | // Sleep 1/4 second to allow time for resolve... |
1601 | 0 | usleep(250000); |
1602 | |
|
1603 | 0 | if (resolved_uri[0]) |
1604 | 0 | break; |
1605 | 0 | } |
1606 | |
|
1607 | 0 | cupsDNSSDDelete(dnssd); |
1608 | | |
1609 | | // Save the results of the resolve... |
1610 | 0 | uri = *resolved_uri ? resolved_uri : NULL; |
1611 | 0 | } |
1612 | 0 | else |
1613 | 0 | { |
1614 | | // Nothing more to do... |
1615 | 0 | cupsCopyString(resolved_uri, uri, resolved_size); |
1616 | 0 | uri = resolved_uri; |
1617 | 0 | } |
1618 | | |
1619 | 0 | DEBUG_printf("2httpResolveURI: Returning \"%s\"", uri); |
1620 | |
|
1621 | 0 | return (uri); |
1622 | 0 | } |
1623 | | |
1624 | | |
1625 | | // |
1626 | | // 'http_copy_decode()' - Copy and decode a URI. |
1627 | | // |
1628 | | |
1629 | | static const char * // O - New source pointer or NULL on error |
1630 | | http_copy_decode(char *dst, // O - Destination buffer |
1631 | | const char *src, // I - Source pointer |
1632 | | size_t dstsize, // I - Destination size |
1633 | | const char *term, // I - Terminating characters |
1634 | | bool decode) // I - Decode %-encoded values? |
1635 | 0 | { |
1636 | 0 | char *ptr, // Pointer into buffer |
1637 | 0 | *end; // End of buffer |
1638 | 0 | int quoted; // Quoted character |
1639 | | |
1640 | | |
1641 | | // Copy the src to the destination until we hit a terminating character |
1642 | | // or the end of the string. |
1643 | 0 | for (ptr = dst, end = dst + dstsize - 1; *src && (!term || !strchr(term, *src)); src ++) |
1644 | 0 | { |
1645 | 0 | if (ptr < end) |
1646 | 0 | { |
1647 | 0 | if (*src == '%' && decode) |
1648 | 0 | { |
1649 | 0 | if (isxdigit(src[1] & 255) && isxdigit(src[2] & 255)) |
1650 | 0 | { |
1651 | | // Grab a hex-encoded character... |
1652 | 0 | src ++; |
1653 | 0 | if (isalpha(*src)) |
1654 | 0 | quoted = (tolower(*src) - 'a' + 10) << 4; |
1655 | 0 | else |
1656 | 0 | quoted = (*src - '0') << 4; |
1657 | |
|
1658 | 0 | src ++; |
1659 | 0 | if (isalpha(*src)) |
1660 | 0 | quoted |= tolower(*src) - 'a' + 10; |
1661 | 0 | else |
1662 | 0 | quoted |= *src - '0'; |
1663 | |
|
1664 | 0 | *ptr++ = (char)quoted; |
1665 | 0 | } |
1666 | 0 | else |
1667 | 0 | { |
1668 | | // Bad hex-encoded character... |
1669 | 0 | *ptr = '\0'; |
1670 | 0 | return (NULL); |
1671 | 0 | } |
1672 | 0 | } |
1673 | 0 | else if ((*src & 255) <= 0x20 || (*src & 255) >= 0x7f) |
1674 | 0 | { |
1675 | | // Bad control character... |
1676 | 0 | *ptr = '\0'; |
1677 | 0 | return (NULL); |
1678 | 0 | } |
1679 | 0 | else |
1680 | 0 | { |
1681 | | // Good literal... |
1682 | 0 | *ptr++ = *src; |
1683 | 0 | } |
1684 | 0 | } |
1685 | 0 | } |
1686 | | |
1687 | 0 | *ptr = '\0'; |
1688 | |
|
1689 | 0 | return (src); |
1690 | 0 | } |
1691 | | |
1692 | | |
1693 | | // |
1694 | | // 'http_copy_encode()' - Copy and encode a URI. |
1695 | | // |
1696 | | |
1697 | | static char * // O - End of current URI |
1698 | | http_copy_encode(char *dst, // O - Destination buffer |
1699 | | const char *src, // I - Source pointer |
1700 | | char *dstend, // I - End of destination buffer |
1701 | | const char *reserved, // I - Extra reserved characters |
1702 | | const char *term, // I - Terminating characters |
1703 | | bool encode) // I - %-encode reserved chars? |
1704 | 0 | { |
1705 | 0 | static const char hex[] = "0123456789ABCDEF"; |
1706 | | |
1707 | |
|
1708 | 0 | while (*src && dst < dstend) |
1709 | 0 | { |
1710 | 0 | if (term && *src == *term) |
1711 | 0 | return (dst); |
1712 | | |
1713 | 0 | if (encode && (*src == '%' || *src <= ' ' || *src & 128 || (reserved && strchr(reserved, *src)))) |
1714 | 0 | { |
1715 | | // Hex encode reserved characters... |
1716 | 0 | if ((dst + 2) >= dstend) |
1717 | 0 | break; |
1718 | | |
1719 | 0 | *dst++ = '%'; |
1720 | 0 | *dst++ = hex[(*src >> 4) & 15]; |
1721 | 0 | *dst++ = hex[*src & 15]; |
1722 | |
|
1723 | 0 | src ++; |
1724 | 0 | } |
1725 | 0 | else |
1726 | 0 | { |
1727 | | // Copy literal... |
1728 | 0 | *dst++ = *src++; |
1729 | 0 | } |
1730 | 0 | } |
1731 | | |
1732 | 0 | *dst = '\0'; |
1733 | |
|
1734 | 0 | if (*src) |
1735 | 0 | return (NULL); |
1736 | 0 | else |
1737 | 0 | return (dst); |
1738 | 0 | } |
1739 | | |
1740 | | |
1741 | | // |
1742 | | // 'http_resolve_cb()' - Build a device URI for the given service name. |
1743 | | // |
1744 | | |
1745 | | static void |
1746 | | http_resolve_cb( |
1747 | | cups_dnssd_resolve_t *res, // I - Resolver |
1748 | | void *cb_data, // I - Pointer to URI buffer |
1749 | | cups_dnssd_flags_t flags, // I - Results flags |
1750 | | uint32_t if_index, // I - Interface index |
1751 | | const char *fullname, // I - Full service name |
1752 | | const char *host, // I - Hostname |
1753 | | uint16_t port, // I - Port number |
1754 | | size_t num_txt, // I - Number of TXT key/value pairs |
1755 | | cups_option_t *txt) // I - TXT key/value pairs |
1756 | 0 | { |
1757 | 0 | _http_uribuf_t *uribuf = (_http_uribuf_t *)cb_data; |
1758 | | // URI buffer |
1759 | 0 | const char *scheme, // URI scheme |
1760 | 0 | *hostptr, // Pointer into hostTarget |
1761 | 0 | *reskey, // "rp" or "rfo" |
1762 | 0 | *resdefault; // Default path |
1763 | 0 | char fqdn[256]; // FQDN of the .local name |
1764 | 0 | const char *value, // Value from TXT record |
1765 | 0 | *resource; // Resource path |
1766 | | |
1767 | |
|
1768 | 0 | DEBUG_printf("4http_resolve_cb(res=%p, cb_data=%p, flags=%x, if_index=%u, fullname=\"%s\", host=\"%s\", port=%u, num_txt=%u, txt=%p)", (void *)res, cb_data, flags, if_index, fullname, host, port, (unsigned)num_txt, (void *)txt); |
1769 | | |
1770 | | // If we have a UUID, compare it... |
1771 | 0 | if (uribuf->uuid && (value = cupsGetOption("UUID", num_txt, txt)) != NULL) |
1772 | 0 | { |
1773 | 0 | if (_cups_strcasecmp(value, uribuf->uuid)) |
1774 | 0 | { |
1775 | 0 | DEBUG_printf("5http_resolve_cb: Found UUID %s, looking for %s.", value, uribuf->uuid); |
1776 | 0 | return; |
1777 | 0 | } |
1778 | 0 | } |
1779 | | |
1780 | | // Figure out the scheme from the full name... |
1781 | 0 | if (strstr(fullname, "._ipps") || strstr(fullname, "._ipp-tls")) |
1782 | 0 | scheme = "ipps"; |
1783 | 0 | else if (strstr(fullname, "._ipp") || strstr(fullname, "._fax-ipp")) |
1784 | 0 | scheme = "ipp"; |
1785 | 0 | else if (strstr(fullname, "._http.")) |
1786 | 0 | scheme = "http"; |
1787 | 0 | else if (strstr(fullname, "._https.")) |
1788 | 0 | scheme = "https"; |
1789 | 0 | else if (strstr(fullname, "._printer.")) |
1790 | 0 | scheme = "lpd"; |
1791 | 0 | else if (strstr(fullname, "._pdl-datastream.")) |
1792 | 0 | scheme = "socket"; |
1793 | 0 | else |
1794 | 0 | scheme = "riousbprint"; |
1795 | | |
1796 | | // Extract the "remote printer" key from the TXT record... |
1797 | 0 | if ((uribuf->options & HTTP_RESOLVE_FAXOUT) && (!strcmp(scheme, "ipp") || !strcmp(scheme, "ipps")) && !cupsGetOption("printer-type", num_txt, txt)) |
1798 | 0 | { |
1799 | 0 | reskey = "rfo"; |
1800 | 0 | resdefault = "ipp/faxout"; |
1801 | 0 | } |
1802 | 0 | else |
1803 | 0 | { |
1804 | 0 | reskey = "rp"; |
1805 | 0 | resdefault = ""; |
1806 | 0 | } |
1807 | |
|
1808 | 0 | if ((resource = cupsGetOption(reskey, num_txt, txt)) != NULL) |
1809 | 0 | { |
1810 | | // Use the resource path from the TXT record... |
1811 | 0 | if (*resource == '/') |
1812 | 0 | { |
1813 | | // Value (incorrectly) has a leading slash already... |
1814 | 0 | resource ++; |
1815 | 0 | } |
1816 | 0 | } |
1817 | 0 | else |
1818 | 0 | { |
1819 | | // Use the default resource path... |
1820 | 0 | resource = resdefault; |
1821 | 0 | } |
1822 | | |
1823 | | // Lookup the FQDN if needed... |
1824 | 0 | if ((uribuf->options & HTTP_RESOLVE_FQDN) && (hostptr = host + strlen(host) - 7) > host && !_cups_strcasecmp(hostptr, ".local.")) |
1825 | 0 | { |
1826 | | // OK, we got a .local name but the caller needs a real domain. Start by |
1827 | | // getting the IP address of the .local name and then do reverse-lookups... |
1828 | 0 | http_addrlist_t *addrlist, // List of addresses |
1829 | 0 | *addr; // Current address |
1830 | |
|
1831 | 0 | DEBUG_printf("5http_resolve_cb: Looking up \"%s\".", host); |
1832 | |
|
1833 | 0 | snprintf(fqdn, sizeof(fqdn), "%d", ntohs(port)); |
1834 | 0 | if ((addrlist = httpAddrGetList(host, AF_UNSPEC, fqdn)) != NULL) |
1835 | 0 | { |
1836 | 0 | for (addr = addrlist; addr; addr = addr->next) |
1837 | 0 | { |
1838 | 0 | int error = getnameinfo(&(addr->addr.addr), (socklen_t)httpAddrGetLength(&(addr->addr)), fqdn, sizeof(fqdn), NULL, 0, NI_NAMEREQD); |
1839 | |
|
1840 | 0 | if (!error) |
1841 | 0 | { |
1842 | 0 | DEBUG_printf("5http_resolve_cb: Found \"%s\".", fqdn); |
1843 | |
|
1844 | 0 | if ((hostptr = fqdn + strlen(fqdn) - 6) <= fqdn || _cups_strcasecmp(hostptr, ".local")) |
1845 | 0 | { |
1846 | 0 | host = fqdn; |
1847 | 0 | break; |
1848 | 0 | } |
1849 | 0 | } |
1850 | | #ifdef DEBUG |
1851 | | else |
1852 | | DEBUG_printf("5http_resolve_cb: \"%s\" did not resolve: %d", httpAddrGetString(&(addr->addr), fqdn, sizeof(fqdn)), error); |
1853 | | #endif // DEBUG |
1854 | 0 | } |
1855 | |
|
1856 | 0 | httpAddrFreeList(addrlist); |
1857 | 0 | } |
1858 | 0 | } |
1859 | | |
1860 | | // Assemble the final URI... |
1861 | 0 | if ((!strcmp(scheme, "ipp") || !strcmp(scheme, "ipps")) && !strcmp(uribuf->resource, "/cups")) |
1862 | 0 | httpAssembleURIf(HTTP_URI_CODING_ALL, uribuf->buffer, uribuf->bufsize, scheme, NULL, host, port, "/%s?snmp=false", resource); |
1863 | 0 | else |
1864 | 0 | httpAssembleURIf(HTTP_URI_CODING_ALL, uribuf->buffer, uribuf->bufsize, scheme, NULL, host, port, "/%s", resource); |
1865 | |
|
1866 | 0 | DEBUG_printf("5http_resolve_cb: Resolved URI is \"%s\"...", uribuf->buffer); |
1867 | 0 | } |