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