Debian Codegraph @

src/utils.c

raw
1 /* Various utility functions.
2 Copyright (C) 1996-2011, 2015, 2018-2024 Free Software Foundation,
3 Inc.
4
5 This file is part of GNU Wget.
6
7 GNU Wget is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 GNU Wget is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Wget. If not, see <http://www.gnu.org/licenses/>.
19
20 Additional permission under GNU GPL version 3 section 7
21
22 If you modify this program, or any covered work, by linking or
23 combining it with the OpenSSL project's OpenSSL library (or a
24 modified version of that library), containing parts covered by the
25 terms of the OpenSSL or SSLeay licenses, the Free Software Foundation
26 grants you additional permission to convey the resulting work.
27 Corresponding Source for a non-source form of such a combination
28 shall include the source code for the parts of OpenSSL used as well
29 as that of the covered work. */
30
31 #include "wget.h"
32
33 #include "sha256.h"
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <time.h>
38 #include <unistd.h>
39 #ifdef HAVE_PROCESS_H
40 # include <process.h> /* getpid() */
41 #endif
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <assert.h>
45 #include <stdarg.h>
46 #include <locale.h>
47 #include <errno.h>
48 #include <utime.h>
49
50 #include <sys/time.h>
51
52 #include <sys/stat.h>
53
54 /* For TIOCGWINSZ and friends: */
55 #ifndef WINDOWS
56 # include <sys/ioctl.h>
57 # include <termios.h>
58 #endif
59
60 /* Needed for Unix version of run_with_timeout. */
61 #include <signal.h>
62 #include <setjmp.h>
63
64 #include <regex.h>
65 #ifdef HAVE_LIBPCRE2
66 # define PCRE2_CODE_UNIT_WIDTH 8
67 # include <pcre2.h>
68 #elif defined HAVE_LIBPCRE
69 # include <pcre.h>
70 #endif
71
72 #ifndef HAVE_SIGSETJMP
73 /* If sigsetjmp is a macro, configure won't pick it up. */
74 # ifdef sigsetjmp
75 # define HAVE_SIGSETJMP
76 # endif
77 #endif
78
79 #if defined HAVE_SIGSETJMP || defined HAVE_SIGBLOCK
80 # define USE_SIGNAL_TIMEOUT
81 #endif
82
83 /* Some systems (Linux libc5, "NCR MP-RAS 3.0", and others) don't
84 provide MAP_FAILED, a symbolic constant for the value returned by
85 mmap() when it doesn't work. Usually, this constant should be -1.
86 This only makes sense for files that use mmap() and include
87 sys/mman.h *before* sysdep.h, but doesn't hurt others. */
88 #ifdef HAVE_MMAP
89 # include <sys/mman.h>
90 # ifndef MAP_FAILED
91 # define MAP_FAILED ((void *) -1)
92 # endif
93 #endif
94
95 #include "utils.h"
96 #include "hash.h"
97
98 #ifdef __VMS
99 #include "vms.h"
100 #endif /* def __VMS */
101
102 #ifdef TESTING
103 #include "../tests/unit-tests.h"
104 #endif
105
106 #include "exits.h"
107 #include "c-strcase.h"
108
109 _Noreturn static void
110 memfatal (const char *context, long attempted_size)
111 {
112 /* Make sure we don't try to store part of the log line, and thus
113 call malloc. */
114 log_set_save_context (false);
115
116 /* We have different log outputs in different situations:
117 1) output without bytes information
118 2) output with bytes information */
119 if (attempted_size == UNKNOWN_ATTEMPTED_SIZE)
120 {
121 logprintf (LOG_ALWAYS,
122 _("%s: %s: Failed to allocate enough memory; memory exhausted.\n"),
123 exec_name, context);
124 }
125 else
126 {
127 logprintf (LOG_ALWAYS,
128 _("%s: %s: Failed to allocate %ld bytes; memory exhausted.\n"),
129 exec_name, context, attempted_size);
130 }
131
132 exit (WGET_EXIT_GENERIC_ERROR);
133 }
134
135 /* Character property table for (re-)escaping VMS ODS5 extended file
136 names. Note that this table ignores Unicode.
137
138 ODS2 valid characters: 0-9 A-Z a-z $ - _ ~
139
140 ODS5 Invalid characters:
141 C0 control codes (0x00 to 0x1F inclusive)
142 Asterisk (*)
143 Question mark (?)
144
145 ODS5 Invalid characters only in VMS V7.2 (which no one runs, right?):
146 Double quotation marks (")
147 Backslash (\)
148 Colon (:)
149 Left angle bracket (<)
150 Right angle bracket (>)
151 Slash (/)
152 Vertical bar (|)
153
154 Characters escaped by "^":
155 SP ! " # % & ' ( ) + , . : ; =
156 @ [ \ ] ^ ` { | } ~
157
158 Either "^_" or "^ " is accepted as a space. Period (.) is a special
159 case. Note that un-escaped < and > can also confuse a directory
160 spec.
161
162 Characters put out as ^xx:
163 7F (DEL)
164 80-9F (C1 control characters)
165 A0 (nonbreaking space)
166 FF (Latin small letter y diaeresis)
167
168 Other cases:
169 Unicode: "^Uxxxx", where "xxxx" is four hex digits.
170
171 Property table values:
172 Normal escape: 1
173 Space: 2
174 Dot: 4
175 Hex-hex escape: 8
176 ODS2 normal: 16
177 ODS2 lower case: 32
178 Hex digit: 64
179 */
180
181 unsigned char char_prop[ 256] = {
182
183 /* NUL SOH STX ETX EOT ENQ ACK BEL BS HT LF VT FF CR SO SI */
184 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
185
186 /* DLE DC1 DC2 DC3 DC4 NAK SYN ETB CAN EM SUB ESC FS GS RS US */
187 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
188
189 /* SP ! " # $ % & ' ( ) * + , - . / */
190 2, 1, 1, 1, 16, 1, 1, 1, 1, 1, 0, 1, 1, 16, 4, 0,
191
192 /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
193 80, 80, 80, 80, 80, 80, 80, 80, 80, 80, 1, 1, 1, 1, 1, 1,
194
195 /* @ A B C D E F G H I J K L M N O */
196 1, 80, 80, 80, 80, 80, 80, 16, 16, 16, 16, 16, 16, 16, 16, 16,
197
198 /* P Q R S T U V W X Y Z [ \ ] ^ _ */
199 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 1, 1, 1, 1, 16,
200
201 /* ` a b c d e f g h i j k l m n o */
202 1, 96, 96, 96, 96, 96, 96, 32, 32, 32, 32, 32, 32, 32, 32, 32,
203
204 /* p q r s t u v w x y z { | } ~ DEL */
205 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 1, 1, 1, 17, 8,
206
207 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
208 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
209 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
210 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
211 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
212 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
213 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
214 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8
215 };
216
217 /* Utility function: like xstrdup(), but also lowercases S. */
218
219 char *
220 xstrdup_lower (const char *s)
221 {
222 char *copy = xstrdup (s);
223 char *p = copy;
224 for (; *p; p++)
225 *p = c_tolower (*p);
226 return copy;
227 }
228
229 /* Copy the string formed by two pointers (one on the beginning, other
230 on the char after the last char) to a new, malloc-ed location.
231 0-terminate it.
232 If both pointers are NULL, the function returns an empty string. */
233 char *
234 strdupdelim (const char *beg, const char *end)
235 {
236 if (beg && beg <= end)
237 {
238 char *res = xmalloc (end - beg + 1);
239 memcpy (res, beg, end - beg);
240 res[end - beg] = '\0';
241 return res;
242 }
243
244 return xstrdup("");
245 }
246
247 /* Parse a string containing comma-separated elements, and return a
248 vector of char pointers with the elements. Spaces following the
249 commas are ignored. */
250 char **
251 sepstring (const char *s)
252 {
253 char **res;
254 const char *p;
255 int i = 0;
256
257 if (!s || !*s)
258 return NULL;
259 res = NULL;
260 p = s;
261 while (*s)
262 {
263 if (*s == ',')
264 {
265 res = xrealloc (res, (i + 2) * sizeof (char *));
266 res[i] = strdupdelim (p, s);
267 res[++i] = NULL;
268 ++s;
269 /* Skip the blanks following the ','. */
270 while (c_isspace (*s))
271 ++s;
272 p = s;
273 }
274 else
275 ++s;
276 }
277 res = xrealloc (res, (i + 2) * sizeof (char *));
278 res[i] = strdupdelim (p, s);
279 res[i + 1] = NULL;
280 return res;
281 }
282
283 /* Like sprintf, but prints into a string of sufficient size freshly
284 allocated with malloc, which is returned. If unable to print due
285 to invalid format, returns NULL. Inability to allocate needed
286 memory results in abort, as with xmalloc. This is in spirit
287 similar to the GNU/BSD extension asprintf, but somewhat easier to
288 use.
289
290 Internally the function either calls vasprintf or loops around
291 vsnprintf until the correct size is found. Since Wget also ships a
292 fallback implementation of vsnprintf, this should be portable. */
293
294 char *
295 aprintf (const char *fmt, ...)
296 {
297 #if defined HAVE_VASPRINTF && !defined DEBUG_MALLOC
298 /* Use vasprintf. */
299 int ret;
300 va_list args;
301 char *str;
302 va_start (args, fmt);
303 ret = vasprintf (&str, fmt, args);
304 va_end (args);
305 if (ret < 0 && errno == ENOMEM)
306 memfatal ("aprintf", UNKNOWN_ATTEMPTED_SIZE); /* for consistency
307 with xmalloc/xrealloc */
308 else if (ret < 0)
309 return NULL;
310 return str;
311 #else /* not HAVE_VASPRINTF */
312
313 /* Constant is using for limits memory allocation for text buffer.
314 Applicable in situation when: vasprintf is not available in the system
315 and vsnprintf return -1 when long line is truncated (in old versions of
316 glibc and in other system where C99 doesn`t support) */
317
318 #define FMT_MAX_LENGTH 1048576
319
320 /* vasprintf is unavailable. snprintf into a small buffer and
321 resize it as necessary. */
322 int size = 32;
323 char *str = xmalloc (size);
324
325 /* #### This code will infloop and eventually abort in xrealloc if
326 passed a FMT that causes snprintf to consistently return -1. */
327
328 while (1)
329 {
330 int n;
331 va_list args;
332
333 va_start (args, fmt);
334 n = vsnprintf (str, size, fmt, args);
335 va_end (args);
336
337 /* If the printing worked, return the string. */
338 if (n > -1 && n < size)
339 return str;
340
341 /* Else try again with a larger buffer. */
342 if (n > -1) /* C99 */
343 size = n + 1; /* precisely what is needed */
344 else if (size >= FMT_MAX_LENGTH) /* We have a huge buffer, */
345 { /* maybe we have some wrong
346 format string? */
347 logprintf (LOG_ALWAYS,
348 _("%s: aprintf: text buffer is too big (%d bytes), "
349 "aborting.\n"),
350 exec_name, size); /* printout a log message */
351 abort (); /* and abort... */
352 }
353 else
354 {
355 /* else, we continue to grow our
356 * buffer: Twice the old size. */
357 size <<= 1;
358 }
359 str = xrealloc (str, size);
360 }
361 #endif /* not HAVE_VASPRINTF */
362 }
363
364 #ifndef HAVE_STRLCPY
365 /* strlcpy() is a BSD function that sometimes is really handy.
366 * It is the same as snprintf(dst,dstsize,"%s",src), but much faster. */
367
368 size_t
369 strlcpy (char *dst, const char *src, size_t size)
370 {
371 const char *old = src;
372
373 /* Copy as many bytes as will fit */
374 if (size)
375 {
376 while (--size)
377 {
378 if (!(*dst++ = *src++))
379 return src - old - 1;
380 }
381
382 *dst = 0;
383 }
384
385 while (*src++);
386 return src - old - 1;
387 }
388 #endif
389
390 /* Concatenate the NULL-terminated list of string arguments into
391 freshly allocated space. */
392
393 char *
394 concat_strings (const char *str0, ...)
395 {
396 va_list args;
397 const char *arg;
398 size_t length = 0, pos = 0;
399 char *s;
400
401 if (!str0)
402 return NULL;
403
404 /* calculate the length of the resulting string */
405 va_start (args, str0);
406 for (arg = str0; arg; arg = va_arg (args, const char *))
407 length += strlen(arg);
408 va_end (args);
409
410 s = xmalloc (length + 1);
411
412 /* concatenate strings */
413 va_start (args, str0);
414 for (arg = str0; arg; arg = va_arg (args, const char *))
415 pos += strlcpy(s + pos, arg, length - pos + 1);
416 va_end (args);
417
418 return s;
419 }
420
421 /* Format the provided time according to the specified format. The
422 format is a string with format elements supported by strftime. */
423
424 static char *
425 fmttime (time_t t, const char *fmt)
426 {
427 static char output[32];
428 struct tm *tm = localtime(&t);
429 if (!tm)
430 abort ();
431 if (!strftime(output, sizeof(output), fmt, tm))
432 abort ();
433 return output;
434 }
435
436 /* Return pointer to a static char[] buffer in which zero-terminated
437 string-representation of TM (in form hh:mm:ss) is printed.
438
439 If TM is NULL, the current time will be used. */
440
441 char *
442 time_str (time_t t)
443 {
444 return fmttime(t, "%H:%M:%S");
445 }
446
447 /* Like the above, but include the date: YYYY-MM-DD hh:mm:ss. */
448
449 char *
450 datetime_str (time_t t)
451 {
452 return fmttime(t, "%Y-%m-%d %H:%M:%S");
453 }
454
455 /* The Windows versions of the following two functions are defined in
456 mswindows.c. On MSDOS this function should never be called. */
457
458 #ifdef __VMS
459
460 bool
461 fork_to_background (void)
462 {
463 return false;
464 }
465
466 #else /* def __VMS */
467
468 #if !defined(WINDOWS) && !defined(MSDOS)
469 bool
470 fork_to_background (void)
471 {
472 pid_t pid;
473 /* Whether we arrange our own version of opt.lfilename here. */
474 bool logfile_changed = false;
475
476 if (!opt.lfilename && (!opt.quiet || opt.server_response))
477 {
478 /* We must create the file immediately to avoid either a race
479 condition (which arises from using unique_name and failing to
480 use fopen_excl) or lying to the user about the log file name
481 (which arises from using unique_name, printing the name, and
482 using fopen_excl later on.) */
483 FILE *new_log_fp = unique_create (DEFAULT_LOGFILE, false, &opt.lfilename);
484 if (new_log_fp)
485 {
486 logfile_changed = true;
487 fclose (new_log_fp);
488 }
489 }
490 pid = fork ();
491 if (pid < 0)
492 {
493 /* parent, error */
494 perror ("fork");
495 exit (WGET_EXIT_GENERIC_ERROR);
496 }
497 else if (pid != 0)
498 {
499 /* parent, no error */
500 printf (_("Continuing in background, pid %d.\n"), (int) pid);
501 if (logfile_changed)
502 printf (_("Output will be written to %s.\n"), quote (opt.lfilename));
503 exit (WGET_EXIT_SUCCESS); /* #### should we use _exit()? */
504 }
505
506 /* child: give up the privileges and keep running. */
507 setsid ();
508 if (freopen ("/dev/null", "r", stdin) == NULL)
509 DEBUGP (("Failed to redirect stdin to /dev/null.\n"));
510 if (freopen ("/dev/null", "w", stdout) == NULL)
511 DEBUGP (("Failed to redirect stdout to /dev/null.\n"));
512 if (freopen ("/dev/null", "w", stderr) == NULL)
513 DEBUGP (("Failed to redirect stderr to /dev/null.\n"));
514
515 return logfile_changed;
516 }
517 #endif /* !WINDOWS && !MSDOS */
518
519 #endif /* def __VMS [else] */
520
521
522 /* "Touch" FILE, i.e. make its mtime ("modified time") equal the time
523 specified with TM. The atime ("access time") is set to the current
524 time. */
525
526 void
527 touch (const char *file, time_t tm)
528 {
529 struct utimbuf times;
530
531 times.modtime = tm;
532 times.actime = time (NULL);
533
534 if (utime (file, &times) == -1)
535 logprintf (LOG_NOTQUIET, "utime(%s): %s\n", file, strerror (errno));
536 }
537
538 /* Checks if FILE is a symbolic link, and removes it if it is. Does
539 nothing under MS-Windows. */
540 int
541 remove_link (const char *file)
542 {
543 int err = 0;
544 struct stat st;
545
546 if (lstat (file, &st) == 0 && S_ISLNK (st.st_mode))
547 {
548 DEBUGP (("Unlinking %s (symlink).\n", file));
549 err = unlink (file);
550 if (err != 0)
551 logprintf (LOG_VERBOSE, _("Failed to unlink symlink %s: %s\n"),
552 quote (file), strerror (errno));
553 }
554 return err;
555 }
556
557 /* Does FILENAME exist? */
558 bool
559 file_exists_p (const char *filename, file_stats_t *fstats)
560 {
561 struct stat buf;
562
563 if (!filename)
564 return false;
565
566 #if defined(WINDOWS) || defined(__VMS)
567 int ret = stat (filename, &buf);
568 if (ret >= 0)
569 {
570 if (fstats != NULL)
571 fstats->access_err = errno;
572 }
573 return ret >= 0;
574 #else
575 errno = 0;
576 if (stat (filename, &buf) == 0 && S_ISREG(buf.st_mode) &&
577 (((S_IRUSR & buf.st_mode) && (getuid() == buf.st_uid)) ||
578 ((S_IRGRP & buf.st_mode) && group_member(buf.st_gid)) ||
579 (S_IROTH & buf.st_mode))) {
580 if (fstats != NULL)
581 {
582 fstats->access_err = 0;
583 fstats->st_ino = buf.st_ino;
584 fstats->st_dev = buf.st_dev;
585 }
586 return true;
587 }
588 else
589 {
590 if (fstats != NULL)
591 fstats->access_err = (errno == 0 ? EACCES : errno);
592 errno = 0;
593 return false;
594 }
595 /* NOTREACHED */
596 #endif
597 }
598
599 /* Returns 0 if PATH is a directory, 1 otherwise (any kind of file).
600 Returns 0 on error. */
601 bool
602 file_non_directory_p (const char *path)
603 {
604 struct stat buf;
605 /* Use lstat() rather than stat() so that symbolic links pointing to
606 directories can be identified correctly. */
607 if (lstat (path, &buf) != 0)
608 return false;
609 return S_ISDIR (buf.st_mode) ? false : true;
610 }
611
612 /* Return the size of file named by FILENAME, or -1 if it cannot be
613 opened or sought into. */
614 wgint
615 file_size (const char *filename)
616 {
617 #if defined(HAVE_FSEEKO) && defined(HAVE_FTELLO)
618 wgint size;
619 /* We use fseek rather than stat to determine the file size because
620 that way we can also verify that the file is readable without
621 explicitly checking for permissions. Inspired by the POST patch
622 by Arnaud Wylie. */
623 FILE *fp = fopen (filename, "rb");
624 if (!fp)
625 return -1;
626 fseeko (fp, 0, SEEK_END);
627 size = ftello (fp);
628 fclose (fp);
629 return size;
630 #else
631 struct stat st;
632 if (stat (filename, &st) < 0)
633 return -1;
634 return st.st_size;
635 #endif
636 }
637
638 /* 2005-02-19 SMS.
639 If no UNIQ_SEP is defined (as on VMS), have unique_name() return the
640 original name. With the VMS file systems' versioning, everything
641 should be fine, and appending ".NN" just causes trouble.
642 */
643
644 #ifdef UNIQ_SEP
645
646 /* stat file names named PREFIX.1, PREFIX.2, etc., until one that
647 doesn't exist is found. Return a freshly allocated copy of the
648 unused file name. */
649
650 static char *
651 unique_name_1 (const char *prefix)
652 {
653 int count = 1;
654 int plen = strlen (prefix);
655 char *template = xmalloc (plen + 1 + 24);
656 char *template_tail = template + plen;
657
658 memcpy (template, prefix, plen);
659 *template_tail++ = UNIQ_SEP;
660
661 do
662 number_to_string (template_tail, count++);
663 while (file_exists_p (template, NULL) && count < 999999);
664
665 return template;
666 }
667
668 /* Return a unique file name, based on FILE.
669
670 More precisely, if FILE doesn't exist, it is returned unmodified.
671 If not, FILE.1 is tried, then FILE.2, etc. The first FILE.<number>
672 file name that doesn't exist is returned.
673
674 2005-02-19 SMS. "." is now UNIQ_SEP, and may be different.
675
676 The resulting file is not created, only verified that it didn't
677 exist at the point in time when the function was called.
678 Therefore, where security matters, don't rely that the file created
679 by this function exists until you open it with O_EXCL or
680 equivalent.
681
682 unique_name() always returns a freshly allocated string.
683
684 unique_name_passthrough() may return FILE if the file doesn't exist
685 (and therefore doesn't need changing). */
686
687 char *
688 unique_name_passthrough (const char *file)
689 {
690 /* If the FILE itself doesn't exist, return it without
691 modification. Otherwise, find a numeric suffix that results in unused
692 file name and return it. */
693 return file_exists_p (file, NULL) ? unique_name_1 (file) : (char *) file;
694 }
695
696 char *
697 unique_name (const char *file)
698 {
699 /* If the FILE itself doesn't exist, return it without
700 modification. Otherwise, find a numeric suffix that results in unused
701 file name and return it. */
702 return file_exists_p (file, NULL) ? unique_name_1 (file) : xstrdup (file);
703 }
704
705 #else /* def UNIQ_SEP */
706
707 /* Dummy unique_name() for VMS. Return the original name as easily as
708 possible.
709 */
710 char *
711 unique_name_passthrough (const char *file, bool allow_passthrough)
712 {
713 /* Return the FILE itself, without modification, irregardful. */
714 return (char *) file;
715 }
716 char *
717
718 unique_name (const char *file)
719 {
720 /* Return the FILE itself, without modification, irregardful. */
721 return xstrdup (file);
722 }
723
724 #endif /* def UNIQ_SEP [else] */
725
726 /* Create a file based on NAME, except without overwriting an existing
727 file with that name. Providing O_EXCL is correctly implemented,
728 this function does not have the race condition associated with
729 opening the file returned by unique_name. */
730
731 FILE *
732 unique_create (const char *name, bool binary, char **opened_name)
733 {
734 /* unique file name, based on NAME */
735 char *uname = unique_name (name);
736 FILE *fp;
737 while ((fp = fopen_excl (uname, binary)) == NULL && errno == EEXIST)
738 {
739 xfree (uname);
740 uname = unique_name (name);
741 }
742 if (opened_name)
743 {
744 if (fp)
745 *opened_name = uname;
746 else
747 {
748 *opened_name = NULL;
749 xfree (uname);
750 }
751 }
752 else
753 xfree (uname);
754 return fp;
755 }
756
757 /* Open the file for writing, with the addition that the file is
758 opened "exclusively". This means that, if the file already exists,
759 this function will *fail* and errno will be set to EEXIST. If
760 BINARY is set, the file will be opened in binary mode, equivalent
761 to fopen's "wb".
762
763 If opening the file fails for any reason, including the file having
764 previously existed, this function returns NULL and sets errno
765 appropriately. */
766
767 FILE *
768 fopen_excl (const char *fname, int binary)
769 {
770 int fd;
771 #ifdef O_EXCL
772
773 /* 2005-04-14 SMS.
774 VMS lacks O_BINARY, but makes up for it in weird and wonderful ways.
775 It also has file versions which obviate all the O_EXCL effort.
776 O_TRUNC (something of a misnomer) requests a new version.
777 */
778 # ifdef __VMS
779 /* Common open() optional arguments:
780 sequential access only, access callback function.
781 */
782 # define OPEN_OPT_ARGS "fop=sqo", "acc", acc_cb, &open_id
783
784 int open_id;
785 int flags = O_WRONLY | O_CREAT | O_TRUNC;
786
787 if (binary > 1)
788 {
789 open_id = 11;
790 fd = open( fname, /* File name. */
791 flags, /* Flags. */
792 0777, /* Mode for default protection. */
793 "ctx=bin,stm", /* Binary, stream access. */
794 "rfm=stmlf", /* Stream_LF. */
795 OPEN_OPT_ARGS); /* Access callback. */
796 }
797 else if (binary)
798 {
799 open_id = 12;
800 fd = open( fname, /* File name. */
801 flags, /* Flags. */
802 0777, /* Mode for default protection. */
803 "ctx=bin,stm", /* Binary, stream access. */
804 "rfm=fix", /* Fixed-length, */
805 "mrs=512", /* 512-byte records. */
806 OPEN_OPT_ARGS); /* Access callback. */
807 }
808 else
809 {
810 open_id = 13;
811 fd = open( fname, /* File name. */
812 flags, /* Flags. */
813 0777, /* Mode for default protection. */
814 "rfm=stmlf", /* Stream_LF. */
815 OPEN_OPT_ARGS); /* Access callback. */
816 }
817 # else /* def __VMS */
818 int flags = O_WRONLY | O_CREAT | O_EXCL;
819 # ifdef O_BINARY
820 if (binary)
821 flags |= O_BINARY;
822 # endif
823 fd = open (fname, flags, 0666);
824 # endif /* def __VMS [else] */
825
826 if (fd < 0)
827 return NULL;
828 return fdopen (fd, binary ? "wb" : "w");
829 #else /* not O_EXCL */
830 /* Manually check whether the file exists. This is prone to race
831 conditions, but systems without O_EXCL haven't deserved
832 better. */
833 if (file_exists_p (fname, NULL))
834 {
835 errno = EEXIST;
836 return NULL;
837 }
838 return fopen (fname, binary ? "wb" : "w");
839 #endif /* not O_EXCL */
840 }
841
842 /* fopen_stat() assumes that file_exists_p() was called earlier.
843 file_stats_t passed to this function was returned from file_exists_p()
844 This is to prevent TOCTTOU race condition.
845 Details : FIO45-C from https://www.securecoding.cert.org/
846 Note that for creating a new file, this check is not useful
847
848 Input:
849 fname => Name of file to open
850 mode => File open mode
851 fstats => Saved file_stats_t about file that was checked for existence
852
853 Returns:
854 NULL if there was an error
855 FILE * of opened file stream
856 */
857 FILE *
858 fopen_stat(const char *fname, const char *mode, file_stats_t *fstats)
859 {
860 int fd;
861 FILE *fp;
862 struct stat fdstats;
863
864 #if defined FUZZING && defined TESTING
865 fp = fopen_wgetrc (fname, mode);
866 return fp;
867 #else
868 fp = fopen (fname, mode);
869 #endif
870 if (fp == NULL)
871 {
872 logprintf (LOG_NOTQUIET, _("Failed to Fopen file %s\n"), fname);
873 return NULL;
874 }
875 fd = fileno (fp);
876 if (fd < 0)
877 {
878 logprintf (LOG_NOTQUIET, _("Failed to get FD for file %s\n"), fname);
879 fclose (fp);
880 return NULL;
881 }
882 memset(&fdstats, 0, sizeof(fdstats));
883 if (fstat (fd, &fdstats) == -1)
884 {
885 logprintf (LOG_NOTQUIET, _("Failed to stat file %s, (check permissions)\n"), fname);
886 fclose (fp);
887 return NULL;
888 }
889 #if !(defined(WINDOWS) || defined(__VMS))
890 if (fstats != NULL &&
891 (fdstats.st_dev != fstats->st_dev ||
892 fdstats.st_ino != fstats->st_ino))
893 {
894 /* File changed since file_exists_p() : NOT SAFE */
895 logprintf (LOG_NOTQUIET, _("File %s changed since the last check. Security check failed.\n"), fname);
896 fclose (fp);
897 return NULL;
898 }
899 #endif
900
901 return fp;
902 }
903
904 /* open_stat assumes that file_exists_p() was called earlier to save file_stats
905 file_stats_t passed to this function was returned from file_exists_p()
906 This is to prevent TOCTTOU race condition.
907 Details : FIO45-C from https://www.securecoding.cert.org/
908 Note that for creating a new file, this check is not useful
909
910
911 Input:
912 fname => Name of file to open
913 flags => File open flags
914 mode => File open mode
915 fstats => Saved file_stats_t about file that was checked for existence
916
917 Returns:
918 -1 if there was an error
919 file descriptor of opened file stream
920 */
921 int
922 open_stat(const char *fname, int flags, mode_t mode, file_stats_t *fstats)
923 {
924 int fd;
925 struct stat fdstats;
926
927 fd = open (fname, flags, mode);
928 if (fd < 0)
929 {
930 logprintf (LOG_NOTQUIET, _("Failed to open file %s, reason :%s\n"), fname, strerror(errno));
931 return -1;
932 }
933 memset(&fdstats, 0, sizeof(fdstats));
934 if (fstat (fd, &fdstats) == -1)
935 {
936 logprintf (LOG_NOTQUIET, _("Failed to stat file %s, error: %s\n"), fname, strerror(errno));
937 close (fd);
938 return -1;
939 }
940 #if !(defined(WINDOWS) || defined(__VMS))
941 if (fstats != NULL &&
942 (fdstats.st_dev != fstats->st_dev ||
943 fdstats.st_ino != fstats->st_ino))
944 {
945 /* File changed since file_exists_p() : NOT SAFE */
946 logprintf (LOG_NOTQUIET, _("Trying to open file %s but it changed since last check. Security check failed.\n"), fname);
947 close (fd);
948 return -1;
949 }
950 #endif
951
952 return fd;
953 }
954
955 /* Create DIRECTORY. If some of the pathname components of DIRECTORY
956 are missing, create them first. In case any mkdir() call fails,
957 return its error status. Returns 0 on successful completion.
958
959 The behaviour of this function should be identical to the behaviour
960 of `mkdir -p' on systems where mkdir supports the `-p' option. */
961 int
962 make_directory (const char *directory)
963 {
964 int i, ret, quit = 0;
965 char buf[1024];
966 char *dir;
967 size_t len = strlen (directory);
968
969 /* Make a copy of dir, to be able to write to it. Otherwise, the
970 function is unsafe if called with a read-only char *argument. */
971 if (len < sizeof(buf))
972 {
973 memcpy(buf, directory, len + 1);
974 dir = buf;
975 }
976 else
977 dir = xstrdup(directory);
978
979 /* If the first character of dir is '/', skip it (and thus enable
980 creation of absolute-pathname directories. */
981 for (i = (*dir == '/'); 1; ++i)
982 {
983 for (; dir[i] && dir[i] != '/'; i++)
984 ;
985 if (!dir[i])
986 quit = 1;
987 dir[i] = '\0';
988 /* Check whether the directory already exists. Allow creation of
989 of intermediate directories to fail, as the initial path components
990 are not necessarily directories! */
991 if (!file_exists_p (dir, NULL))
992 ret = mkdir (dir, 0777);
993 else
994 ret = 0;
995 if (quit)
996 break;
997 else
998 dir[i] = '/';
999 }
1000
1001 if (dir != buf)
1002 xfree (dir);
1003
1004 return ret;
1005 }
1006
1007 /* Merge BASE with FILE. BASE can be a directory or a file name, FILE
1008 should be a file name.
1009
1010 file_merge("/foo/bar", "baz") => "/foo/baz"
1011 file_merge("/foo/bar/", "baz") => "/foo/bar/baz"
1012 file_merge("foo", "bar") => "bar"
1013
1014 In other words, it's a simpler and gentler version of uri_merge. */
1015
1016 char *
1017 file_merge (const char *base, const char *file)
1018 {
1019 char *result;
1020 const char *cut = (const char *)strrchr (base, '/');
1021
1022 if (!cut)
1023 return xstrdup (file);
1024
1025 result = xmalloc (cut - base + 1 + strlen (file) + 1);
1026 memcpy (result, base, cut - base);
1027 result[cut - base] = '/';
1028 strcpy (result + (cut - base) + 1, file);
1029
1030 return result;
1031 }
1032
1033 /* Like fnmatch, but performs a case-insensitive match. */
1034
1035 int
1036 fnmatch_nocase (const char *pattern, const char *string, int flags)
1037 {
1038 /* The FNM_CASEFOLD flag started as a GNU extension, but it is now
1039 also present on *BSD platforms, and possibly elsewhere.
1040 Gnulib provides this flag in case it doesn't exist. */
1041 return fnmatch (pattern, string, flags | FNM_CASEFOLD);
1042 }
1043
1044 static bool in_acclist (const char *const *, const char *, bool);
1045
1046 /* Determine whether a file is acceptable to be followed, according to
1047 lists of patterns to accept/reject. */
1048 bool
1049 acceptable (const char *s)
1050 {
1051 const char *p;
1052
1053 if (opt.output_document && strcmp (s, opt.output_document) == 0)
1054 return true;
1055
1056 if ((p = strrchr (s, '/')))
1057 s = p + 1;
1058
1059 if (opt.accepts)
1060 {
1061 if (opt.rejects)
1062 return (in_acclist ((const char *const *)opt.accepts, s, true)
1063 && !in_acclist ((const char *const *)opt.rejects, s, true));
1064 else
1065 return in_acclist ((const char *const *)opt.accepts, s, true);
1066 }
1067 else if (opt.rejects)
1068 return !in_acclist ((const char *const *)opt.rejects, s, true);
1069
1070 return true;
1071 }
1072
1073 /* Determine whether an URL is acceptable to be followed, according to
1074 regex patterns to accept/reject. */
1075 bool
1076 accept_url (const char *s)
1077 {
1078 if (opt.acceptregex && !opt.regex_match_fun (opt.acceptregex, s))
1079 return false;
1080 if (opt.rejectregex && opt.regex_match_fun (opt.rejectregex, s))
1081 return false;
1082
1083 return true;
1084 }
1085
1086 /* Check if D2 is a subdirectory of D1. E.g. if D1 is `/something', subdir_p()
1087 will return true if and only if D2 begins with `/something/' or is exactly
1088 '/something'. */
1089 bool
1090 subdir_p (const char *d1, const char *d2)
1091 {
1092 if (*d1 == '\0')
1093 return true;
1094 if (!opt.ignore_case)
1095 for (; *d1 && *d2 && (*d1 == *d2); ++d1, ++d2)
1096 ;
1097 else
1098 for (; *d1 && *d2 && (c_tolower (*d1) == c_tolower (*d2)); ++d1, ++d2)
1099 ;
1100
1101 return *d1 == '\0' && (*d2 == '\0' || *d2 == '/');
1102 }
1103
1104 /* Iterate through DIRLIST (which must be NULL-terminated), and return the
1105 first element that matches DIR, through wildcards or front comparison (as
1106 appropriate). */
1107 static bool
1108 dir_matches_p (const char **dirlist, const char *dir)
1109 {
1110 const char **x;
1111 int (*matcher) (const char *, const char *, int)
1112 = opt.ignore_case ? fnmatch_nocase : fnmatch;
1113
1114 for (x = dirlist; *x; x++)
1115 {
1116 /* Remove leading '/' */
1117 const char *p = *x + (**x == '/');
1118 if (has_wildcards_p (p))
1119 {
1120 if (matcher (p, dir, FNM_PATHNAME) == 0)
1121 break;
1122 }
1123 else
1124 {
1125 if (subdir_p (p, dir))
1126 break;
1127 }
1128 }
1129
1130 return *x ? true : false;
1131 }
1132
1133 /* Returns whether DIRECTORY is acceptable for download, wrt the
1134 include/exclude lists.
1135
1136 The leading `/' is ignored in paths; relative and absolute paths
1137 may be freely intermixed. */
1138
1139 bool
1140 accdir (const char *directory)
1141 {
1142 /* Remove starting '/'. */
1143 if (*directory == '/')
1144 ++directory;
1145 if (opt.includes)
1146 {
1147 if (!dir_matches_p (opt.includes, directory))
1148 return false;
1149 }
1150 if (opt.excludes)
1151 {
1152 if (dir_matches_p (opt.excludes, directory))
1153 return false;
1154 }
1155 return true;
1156 }
1157
1158 /* Return true if STRING ends with TAIL. For instance:
1159
1160 match_tail ("abc", "bc", false) -> 1
1161 match_tail ("abc", "ab", false) -> 0
1162 match_tail ("abc", "abc", false) -> 1
1163
1164 If FOLD_CASE is true, the comparison will be case-insensitive. */
1165
1166 bool
1167 match_tail (const char *string, const char *tail, bool fold_case)
1168 {
1169 int pos = (int) strlen (string) - (int) strlen (tail);
1170
1171 if (pos < 0)
1172 return false; /* tail is longer than string. */
1173
1174 if (!fold_case)
1175 return !strcmp (string + pos, tail);
1176 else
1177 return !strcasecmp (string + pos, tail);
1178 }
1179
1180 /* Checks whether string S matches each element of ACCEPTS. A list
1181 element are matched either with fnmatch() or match_tail(),
1182 according to whether the element contains wildcards or not.
1183
1184 If the BACKWARD is false, don't do backward comparison -- just compare
1185 them normally. */
1186 static bool
1187 in_acclist (const char *const *accepts, const char *s, bool backward)
1188 {
1189 for (; *accepts; accepts++)
1190 {
1191 if (has_wildcards_p (*accepts))
1192 {
1193 int res = opt.ignore_case
1194 ? fnmatch_nocase (*accepts, s, 0) : fnmatch (*accepts, s, 0);
1195 /* fnmatch returns 0 if the pattern *does* match the string. */
1196 if (res == 0)
1197 return true;
1198 }
1199 else
1200 {
1201 if (backward)
1202 {
1203 if (match_tail (s, *accepts, opt.ignore_case))
1204 return true;
1205 }
1206 else
1207 {
1208 int cmp = opt.ignore_case
1209 ? strcasecmp (s, *accepts) : strcmp (s, *accepts);
1210 if (cmp == 0)
1211 return true;
1212 }
1213 }
1214 }
1215 return false;
1216 }
1217
1218 /* Return the location of STR's suffix (file extension). Examples:
1219 suffix ("foo.bar") -> "bar"
1220 suffix ("foo.bar.baz") -> "baz"
1221 suffix ("/foo/bar") -> NULL
1222 suffix ("/foo.bar/baz") -> NULL */
1223 char *
1224 suffix (const char *str)
1225 {
1226 char *p;
1227
1228 if ((p = strrchr (str, '.')) && !strchr (p + 1, '/'))
1229 return p + 1;
1230
1231 return NULL;
1232 }
1233
1234 /* Return true if S contains globbing wildcards (`*', `?', `[' or
1235 `]'). */
1236
1237 bool
1238 has_wildcards_p (const char *s)
1239 {
1240 return !!strpbrk (s, "*?[]");
1241 }
1242
1243 /* Return true if FNAME ends with a typical HTML suffix. The
1244 following (case-insensitive) suffixes are presumed to be HTML
1245 files:
1246
1247 html
1248 htm
1249 ?html (`?' matches one character)
1250
1251 #### CAVEAT. This is not necessarily a good indication that FNAME
1252 refers to a file that contains HTML! */
1253 bool
1254 has_html_suffix_p (const char *fname)
1255 {
1256 char *suf;
1257
1258 if ((suf = suffix (fname)) == NULL)
1259 return false;
1260 if (!c_strcasecmp (suf, "html"))
1261 return true;
1262 if (!c_strcasecmp (suf, "htm"))
1263 return true;
1264 if (suf[0] && !c_strcasecmp (suf + 1, "html"))
1265 return true;
1266 return false;
1267 }
1268
1269 struct file_memory *
1270 wget_read_file (const char *file)
1271 {
1272 bool left_open;
1273 return wget_read_from_file(file, &left_open);
1274 }
1275
1276 /*
1277 * Set a file-descriptor to be non-blocking.
1278 * Since the needed fcntl flags are not implemented in gnulib
1279 * for Windows, we will provide an alternate stub implementation
1280 * in mswindows.c. The stub will be a no-op and will prevent
1281 * asynchronous file operations on Windows, but such is life
1282 */
1283 #if !defined(WINDOWS) && !defined(MSDOS)
1284 static void set_fd_nonblocking(const int fd)
1285 {
1286 int flags = fcntl(fd, F_GETFL, 0);
1287 fcntl(fd, F_SETFL, flags | O_NONBLOCK);
1288 }
1289 #endif
1290
1291 /* Read FILE into memory. A pointer to `struct file_memory' are
1292 returned; use struct element `content' to access file contents, and
1293 the element `length' to know the file length. `content' is *not*
1294 zero-terminated, and you should *not* read or write beyond the [0,
1295 length) range of characters.
1296
1297 After you are done with the file contents, call wget_read_file_free to
1298 release the memory.
1299
1300 Depending on the operating system and the type of file that is
1301 being read, wget_read_file() either mmap's the file into memory, or
1302 reads the file into the core using read().
1303
1304 If file is named "-", fileno(stdin) is used for reading instead.
1305 If you want to read from a real file named "-", use "./-" instead. */
1306
1307 struct file_memory *
1308 wget_read_from_file (const char *file, bool *left_open)
1309 {
1310 int fd;
1311 struct file_memory *fm;
1312 long size;
1313 bool inhibit_close = false;
1314
1315 /* Some magic in the finest tradition of Perl and its kin: if FILE
1316 is "-", just use stdin. */
1317 #ifndef FUZZING
1318 if (HYPHENP (file))
1319 {
1320 fd = fileno (stdin);
1321 inhibit_close = true;
1322 /* Note that we don't inhibit mmap() in this case. If stdin is
1323 redirected from a regular file, mmap() will still work. */
1324 }
1325 else
1326 #endif
1327 fd = open (file, O_RDONLY);
1328 if (fd < 0)
1329 return NULL;
1330 set_fd_nonblocking(fd);
1331 fm = xnew (struct file_memory);
1332
1333 #ifdef HAVE_MMAP
1334 {
1335 struct stat buf;
1336 if (fstat (fd, &buf) < 0)
1337 goto mmap_lose;
1338 fm->length = buf.st_size;
1339 /* NOTE: As far as I know, the callers of this function never
1340 modify the file text. Relying on this would enable us to
1341 specify PROT_READ and MAP_SHARED for a marginal gain in
1342 efficiency, but at some cost to generality. */
1343 fm->content = mmap (NULL, fm->length, PROT_READ | PROT_WRITE,
1344 MAP_PRIVATE, fd, 0);
1345 if (fm->content == (char *)MAP_FAILED)
1346 goto mmap_lose;
1347 if (!inhibit_close)
1348 close (fd);
1349
1350 fm->mmap_p = 1;
1351 return fm;
1352 }
1353
1354 mmap_lose:
1355 /* The most common reason why mmap() fails is that FD does not point
1356 to a plain file. However, it's also possible that mmap() doesn't
1357 work for a particular type of file. Therefore, whenever mmap()
1358 fails, we just fall back to the regular method. */
1359 #endif /* HAVE_MMAP */
1360
1361 fm->length = 0;
1362 size = 512; /* number of bytes fm->contents can
1363 hold at any given time. */
1364 fm->content = xmalloc (size);
1365 while (1)
1366 {
1367 wgint nread;
1368 if (fm->length > size / 2)
1369 {
1370 /* #### I'm not sure whether the whole exponential-growth
1371 thing makes sense with kernel read. On Linux at least,
1372 read() refuses to read more than 4K from a file at a
1373 single chunk anyway. But other Unixes might optimize it
1374 better, and it doesn't *hurt* anything, so I'm leaving
1375 it. */
1376
1377 /* Normally, we grow SIZE exponentially to make the number
1378 of calls to read() and realloc() logarithmic in relation
1379 to file size. However, read() can read an amount of data
1380 smaller than requested, and it would be unreasonable to
1381 double SIZE every time *something* was read. Therefore,
1382 we double SIZE only when the length exceeds half of the
1383 entire allocated size. */
1384 size <<= 1;
1385 fm->content = xrealloc (fm->content, size);
1386 }
1387 nread = read (fd, fm->content + fm->length, size - fm->length);
1388 if (nread > 0)
1389 /* Successful read. */
1390 fm->length += nread;
1391 else if (nread < 0)
1392 {
1393 if (errno == EAGAIN)
1394 {
1395 *left_open = true;
1396 break;
1397 }
1398 else
1399 {
1400 /* Error. */
1401 goto lose;
1402 }
1403 }
1404 else
1405 {
1406 /* EOF */
1407 *left_open = false;
1408 break;
1409 }
1410 }
1411 if (!inhibit_close)
1412 close (fd);
1413 if (size > fm->length && fm->length != 0)
1414 /* Due to exponential growth of fm->content, the allocated region
1415 might be much larger than what is actually needed. */
1416 fm->content = xrealloc (fm->content, fm->length);
1417 fm->mmap_p = 0;
1418 return fm;
1419
1420 lose:
1421 if (!inhibit_close)
1422 close (fd);
1423 xfree (fm->content);
1424 xfree (fm);
1425 return NULL;
1426 }
1427
1428 /* Release the resources held by FM. Specifically, this calls
1429 munmap() or xfree() on fm->content, depending whether mmap or
1430 malloc/read were used to read in the file. It also frees the
1431 memory needed to hold the FM structure itself. */
1432
1433 void
1434 wget_read_file_free (struct file_memory *fm)
1435 {
1436 #ifdef HAVE_MMAP
1437 if (fm->mmap_p)
1438 {
1439 munmap (fm->content, fm->length);
1440 }
1441 else
1442 #endif
1443 {
1444 xfree (fm->content);
1445 }
1446 xfree (fm);
1447 }
1448
1449 /* Free the pointers in a NULL-terminated vector of pointers, then
1450 free the pointer itself. */
1451 void
1452 free_vec (char **vec)
1453 {
1454 if (vec)
1455 {
1456 char **p = vec;
1457 while (*p)
1458 {
1459 xfree (*p);
1460 p++;
1461 }
1462 xfree (vec);
1463 }
1464 }
1465
1466 /* Append vector V2 to vector V1. The function frees V2 and
1467 reallocates V1 (thus you may not use the contents of neither
1468 pointer after the call). If V1 is NULL, V2 is returned. */
1469 char **
1470 merge_vecs (char **v1, char **v2)
1471 {
1472 int i, j;
1473
1474 if (!v1)
1475 return v2;
1476 if (!v2)
1477 return v1;
1478 if (!*v2)
1479 {
1480 /* To avoid j == 0 */
1481 xfree (v2);
1482 return v1;
1483 }
1484 /* Count v1. */
1485 for (i = 0; v1[i]; i++)
1486 ;
1487 /* Count v2. */
1488 for (j = 0; v2[j]; j++)
1489 ;
1490 /* Reallocate v1. */
1491 v1 = xrealloc (v1, (i + j + 1) * sizeof (char *));
1492 memcpy (v1 + i, v2, (j + 1) * sizeof (char *));
1493 xfree (v2);
1494 return v1;
1495 }
1496
1497 /* Append a freshly allocated copy of STR to VEC. If VEC is NULL, it
1498 is allocated as needed. Return the new value of the vector. */
1499
1500 char **
1501 vec_append (char **vec, const char *str)
1502 {
1503 int cnt; /* count of vector elements, including
1504 the one we're about to append */
1505 if (vec != NULL)
1506 {
1507 for (cnt = 0; vec[cnt]; cnt++)
1508 ;
1509 ++cnt;
1510 }
1511 else
1512 cnt = 1;
1513 /* Reallocate the array to fit the new element and the NULL. */
1514 vec = xrealloc (vec, (cnt + 1) * sizeof (char *));
1515 /* Append a copy of STR to the vector. */
1516 vec[cnt - 1] = xstrdup (str);
1517 vec[cnt] = NULL;
1518 return vec;
1519 }
1520
1521 /* Sometimes it's useful to create "sets" of strings, i.e. special
1522 hash tables where you want to store strings as keys and merely
1523 query for their existence. Here is a set of utility routines that
1524 makes that transparent. */
1525
1526 void
1527 string_set_add (struct hash_table *ht, const char *s)
1528 {
1529 /* First check whether the set element already exists. If it does,
1530 do nothing so that we don't have to free() the old element and
1531 then strdup() a new one. */
1532 if (hash_table_contains (ht, s))
1533 return;
1534
1535 /* We use "1" as value. It provides us a useful and clear arbitrary
1536 value, and it consumes no memory -- the pointers to the same
1537 string "1" will be shared by all the key-value pairs in all `set'
1538 hash tables. */
1539 hash_table_put (ht, xstrdup (s), "1");
1540 }
1541
1542 /* Synonym for hash_table_contains... */
1543
1544 int
1545 string_set_contains (struct hash_table *ht, const char *s)
1546 {
1547 return hash_table_contains (ht, s);
1548 }
1549
1550 /* Convert the specified string set to array. ARRAY should be large
1551 enough to hold hash_table_count(ht) char pointers. */
1552
1553 void string_set_to_array (struct hash_table *ht, char **array)
1554 {
1555 hash_table_iterator iter;
1556 for (hash_table_iterate (ht, &iter); hash_table_iter_next (&iter); )
1557 *array++ = iter.key;
1558 }
1559
1560 /* Free the string set. This frees both the storage allocated for
1561 keys and the actual hash table. (hash_table_destroy would only
1562 destroy the hash table.) */
1563
1564 void
1565 string_set_free (struct hash_table *ht)
1566 {
1567 hash_table_iterator iter;
1568 for (hash_table_iterate (ht, &iter); hash_table_iter_next (&iter); )
1569 xfree (iter.key);
1570 hash_table_destroy (ht);
1571 }
1572
1573 /* Utility function: simply call xfree() on all keys and values of HT. */
1574
1575 void
1576 free_keys_and_values (struct hash_table *ht)
1577 {
1578 hash_table_iterator iter;
1579 for (hash_table_iterate (ht, &iter); hash_table_iter_next (&iter); )
1580 {
1581 xfree (iter.key);
1582 xfree (iter.value);
1583 }
1584 }
1585
1586 /* Get digit grouping data for thousand separors by calling
1587 localeconv(). The data includes separator string and grouping info
1588 and is cached after the first call to the function.
1589
1590 In locales that don't set a thousand separator (such as the "C"
1591 locale), this forces it to be ",". We are now only showing
1592 thousand separators in one place, so this shouldn't be a problem in
1593 practice. */
1594
1595 static void
1596 get_grouping_data (const char **sep, const char **grouping)
1597 {
1598 static const char *cached_sep;
1599 static const char *cached_grouping;
1600 static bool initialized;
1601 if (!initialized)
1602 {
1603 /* Get the grouping info from the locale. */
1604 struct lconv *lconv = localeconv ();
1605 cached_sep = lconv->thousands_sep;
1606 cached_grouping = lconv->grouping;
1607 #if ! USE_NLS_PROGRESS_BAR
1608 /* We can't count column widths, so ensure that the separator
1609 * is single-byte only (let check below determine what byte). */
1610 if (strlen(cached_sep) > 1)
1611 cached_sep = "";
1612 #endif
1613 if (!*cached_sep)
1614 {
1615 /* Many locales (such as "C" or "hr_HR") don't specify
1616 grouping, which we still want to use it for legibility.
1617 In those locales set the sep char to ',', unless that
1618 character is used for decimal point, in which case set it
1619 to ".". */
1620 if (*lconv->decimal_point != ',')
1621 cached_sep = ",";
1622 else
1623 cached_sep = ".";
1624 cached_grouping = "\x03";
1625 }
1626 initialized = true;
1627 }
1628 *sep = cached_sep;
1629 *grouping = cached_grouping;
1630 }
1631
1632 /* Return a printed representation of N with thousand separators.
1633 This should respect locale settings, with the exception of the "C"
1634 locale which mandates no separator, but we use one anyway.
1635
1636 Unfortunately, we cannot use %'d (in fact it would be %'j) to get
1637 the separators because it's too non-portable, and it's hard to test
1638 for this feature at configure time. Besides, it wouldn't display
1639 separators in the "C" locale, still used by many Unix users. */
1640
1641 const char *
1642 with_thousand_seps (wgint n)
1643 {
1644 static char outbuf[48];
1645 char *p = outbuf + sizeof outbuf;
1646
1647 /* Info received from locale */
1648 const char *grouping, *sep;
1649 int seplen;
1650
1651 /* State information */
1652 int i = 0, groupsize;
1653 const char *atgroup;
1654
1655 bool negative = n < 0;
1656
1657 /* Initialize grouping data. */
1658 get_grouping_data (&sep, &grouping);
1659 seplen = strlen (sep);
1660 atgroup = grouping;
1661 groupsize = *atgroup++;
1662
1663 /* This would overflow on WGINT_MIN, but printing negative numbers
1664 is not an important goal of this fuinction. */
1665 if (negative)
1666 n = -n;
1667
1668 /* Write the number into the buffer, backwards, inserting the
1669 separators as necessary. */
1670 *--p = '\0';
1671 while (1)
1672 {
1673 *--p = n % 10 + '0';
1674 n /= 10;
1675 if (n == 0)
1676 break;
1677 /* Prepend SEP to every groupsize'd digit and get new groupsize. */
1678 if (++i == groupsize)
1679 {
1680 if (seplen == 1)
1681 *--p = *sep;
1682 else
1683 memcpy (p -= seplen, sep, seplen);
1684 i = 0;
1685 if (*atgroup)
1686 groupsize = *atgroup++;
1687 }
1688 }
1689 if (negative)
1690 *--p = '-';
1691
1692 return p;
1693 }
1694
1695 /* N, a byte quantity, is converted to a human-readable abberviated
1696 form a la sizes printed by `ls -lh'. The result is written to a
1697 static buffer, a pointer to which is returned.
1698
1699 Unlike `with_thousand_seps', this approximates to the nearest unit.
1700 Quoting GNU libit: "Most people visually process strings of 3-4
1701 digits effectively, but longer strings of digits are more prone to
1702 misinterpretation. Hence, converting to an abbreviated form
1703 usually improves readability."
1704
1705 This intentionally uses kilobyte (KB), megabyte (MB), etc. in their
1706 original computer-related meaning of "powers of 1024". We don't
1707 use the "*bibyte" names invented in 1998, and seldom used in
1708 practice. Wikipedia's entry on "binary prefix" discusses this in
1709 some detail. */
1710
1711 char *
1712 human_readable (wgint n, const int acc, const int decimals)
1713 {
1714 /* These suffixes are compatible with those of GNU `ls -lh'. */
1715 static char powers[] =
1716 {
1717 'K', /* kilobyte, 2^10 bytes */
1718 'M', /* megabyte, 2^20 bytes */
1719 'G', /* gigabyte, 2^30 bytes */
1720 'T', /* terabyte, 2^40 bytes */
1721 'P', /* petabyte, 2^50 bytes */
1722 'E', /* exabyte, 2^60 bytes */
1723 };
1724 static char buf[8];
1725 size_t i;
1726
1727 /* If the quantity is smaller than 1K, just print it. */
1728 if (n < 1024)
1729 {
1730 snprintf (buf, sizeof (buf), "%d", (int) n);
1731 return buf;
1732 }
1733
1734 /* Loop over powers, dividing N with 1024 in each iteration. This
1735 works unchanged for all sizes of wgint, while still avoiding
1736 non-portable `long double' arithmetic. */
1737 for (i = 0; i < countof (powers); i++)
1738 {
1739 /* At each iteration N is greater than the *subsequent* power.
1740 That way N/1024.0 produces a decimal number in the units of
1741 *this* power. */
1742 if ((n / 1024) < 1024 || i == countof (powers) - 1)
1743 {
1744 double val = n / 1024.0;
1745 /* Print values smaller than the accuracy level (acc) with (decimal)
1746 * decimal digits, and others without any decimals. */
1747 snprintf (buf, sizeof (buf), "%.*f%c",
1748 val < acc ? decimals : 0, val, powers[i]);
1749 return buf;
1750 }
1751 n /= 1024;
1752 }
1753 return NULL; /* unreached */
1754 }
1755
1756 /* Count the digits in the provided number. Used to allocate space
1757 when printing numbers. */
1758
1759 int
1760 numdigit (wgint number)
1761 {
1762 int cnt = 1;
1763 if (number < 0)
1764 ++cnt; /* accommodate '-' */
1765 while ((number /= 10) != 0)
1766 ++cnt;
1767 return cnt;
1768 }
1769
1770 #define PR(mask) *p++ = n / (mask) + '0'
1771
1772 /* DIGITS_<D> is used to print a D-digit number and should be called
1773 with mask==10^(D-1). It prints n/mask (the first digit), reducing
1774 n to n%mask (the remaining digits), and calling DIGITS_<D-1>.
1775 Recursively this continues until DIGITS_1 is invoked. */
1776
1777 #define DIGITS_1(mask) PR (mask)
1778 #define DIGITS_2(mask) PR (mask), n %= (mask), DIGITS_1 ((mask) / 10)
1779 #define DIGITS_3(mask) PR (mask), n %= (mask), DIGITS_2 ((mask) / 10)
1780 #define DIGITS_4(mask) PR (mask), n %= (mask), DIGITS_3 ((mask) / 10)
1781 #define DIGITS_5(mask) PR (mask), n %= (mask), DIGITS_4 ((mask) / 10)
1782 #define DIGITS_6(mask) PR (mask), n %= (mask), DIGITS_5 ((mask) / 10)
1783 #define DIGITS_7(mask) PR (mask), n %= (mask), DIGITS_6 ((mask) / 10)
1784 #define DIGITS_8(mask) PR (mask), n %= (mask), DIGITS_7 ((mask) / 10)
1785 #define DIGITS_9(mask) PR (mask), n %= (mask), DIGITS_8 ((mask) / 10)
1786 #define DIGITS_10(mask) PR (mask), n %= (mask), DIGITS_9 ((mask) / 10)
1787
1788 /* DIGITS_<11-20> are only used on machines with 64-bit wgints. */
1789
1790 #define DIGITS_11(mask) PR (mask), n %= (mask), DIGITS_10 ((mask) / 10)
1791 #define DIGITS_12(mask) PR (mask), n %= (mask), DIGITS_11 ((mask) / 10)
1792 #define DIGITS_13(mask) PR (mask), n %= (mask), DIGITS_12 ((mask) / 10)
1793 #define DIGITS_14(mask) PR (mask), n %= (mask), DIGITS_13 ((mask) / 10)
1794 #define DIGITS_15(mask) PR (mask), n %= (mask), DIGITS_14 ((mask) / 10)
1795 #define DIGITS_16(mask) PR (mask), n %= (mask), DIGITS_15 ((mask) / 10)
1796 #define DIGITS_17(mask) PR (mask), n %= (mask), DIGITS_16 ((mask) / 10)
1797 #define DIGITS_18(mask) PR (mask), n %= (mask), DIGITS_17 ((mask) / 10)
1798 #define DIGITS_19(mask) PR (mask), n %= (mask), DIGITS_18 ((mask) / 10)
1799
1800 /* Shorthand for casting to wgint. */
1801 #define W wgint
1802
1803 /* Print NUMBER to BUFFER in base 10. This is equivalent to
1804 `sprintf(buffer, "%lld", (long long) number)', only typically much
1805 faster and portable to machines without long long.
1806
1807 The speedup may make a difference in programs that frequently
1808 convert numbers to strings. Some implementations of sprintf,
1809 particularly the one in some versions of GNU libc, have been known
1810 to be quite slow when converting integers to strings.
1811
1812 Return the pointer to the location where the terminating zero was
1813 printed. (Equivalent to calling buffer+strlen(buffer) after the
1814 function is done.)
1815
1816 BUFFER should be large enough to accept as many bytes as you expect
1817 the number to take up. On machines with 64-bit wgints the maximum
1818 needed size is 24 bytes. That includes the digits needed for the
1819 largest 64-bit number, the `-' sign in case it's negative, and the
1820 terminating '\0'. */
1821
1822 char *
1823 number_to_string (char *buffer, wgint number)
1824 {
1825 char *p = buffer;
1826 wgint n = number;
1827
1828 int last_digit_char = 0;
1829
1830 if (n < 0)
1831 {
1832 if (n < -WGINT_MAX)
1833 {
1834 /* n = -n would overflow because -n would evaluate to a
1835 wgint value larger than WGINT_MAX. Need to make n
1836 smaller and handle the last digit separately. */
1837 int last_digit = n % 10;
1838 /* The sign of n%10 is implementation-defined. */
1839 if (last_digit < 0)
1840 last_digit_char = '0' - last_digit;
1841 else
1842 last_digit_char = '0' + last_digit;
1843 /* After n is made smaller, -n will not overflow. */
1844 n /= 10;
1845 }
1846
1847 *p++ = '-';
1848 n = -n;
1849 }
1850
1851 /* Use the DIGITS_ macro appropriate for N's number of digits. That
1852 way printing any N is fully open-coded without a loop or jump.
1853 (Also see description of DIGITS_*.) */
1854
1855 if (n < 10) DIGITS_1 (1);
1856 else if (n < 100) DIGITS_2 (10);
1857 else if (n < 1000) DIGITS_3 (100);
1858 else if (n < 10000) DIGITS_4 (1000);
1859 else if (n < 100000) DIGITS_5 (10000);
1860 else if (n < 1000000) DIGITS_6 (100000);
1861 else if (n < 10000000) DIGITS_7 (1000000);
1862 else if (n < 100000000) DIGITS_8 (10000000);
1863 else if (n < 1000000000) DIGITS_9 (100000000);
1864 else if (n < 10*(W)1000000000) DIGITS_10 (1000000000);
1865 else if (n < 100*(W)1000000000) DIGITS_11 (10*(W)1000000000);
1866 else if (n < 1000*(W)1000000000) DIGITS_12 (100*(W)1000000000);
1867 else if (n < 10000*(W)1000000000) DIGITS_13 (1000*(W)1000000000);
1868 else if (n < 100000*(W)1000000000) DIGITS_14 (10000*(W)1000000000);
1869 else if (n < 1000000*(W)1000000000) DIGITS_15 (100000*(W)1000000000);
1870 else if (n < 10000000*(W)1000000000) DIGITS_16 (1000000*(W)1000000000);
1871 else if (n < 100000000*(W)1000000000) DIGITS_17 (10000000*(W)1000000000);
1872 else if (n < 1000000000*(W)1000000000) DIGITS_18 (100000000*(W)1000000000);
1873 else DIGITS_19 (1000000000*(W)1000000000);
1874
1875 if (last_digit_char)
1876 *p++ = last_digit_char;
1877
1878 *p = '\0';
1879
1880 return p;
1881 }
1882
1883 #undef PR
1884 #undef W
1885 #undef SPRINTF_WGINT
1886 #undef DIGITS_1
1887 #undef DIGITS_2
1888 #undef DIGITS_3
1889 #undef DIGITS_4
1890 #undef DIGITS_5
1891 #undef DIGITS_6
1892 #undef DIGITS_7
1893 #undef DIGITS_8
1894 #undef DIGITS_9
1895 #undef DIGITS_10
1896 #undef DIGITS_11
1897 #undef DIGITS_12
1898 #undef DIGITS_13
1899 #undef DIGITS_14
1900 #undef DIGITS_15
1901 #undef DIGITS_16
1902 #undef DIGITS_17
1903 #undef DIGITS_18
1904 #undef DIGITS_19
1905
1906 #define RING_SIZE 3
1907
1908 /* Print NUMBER to a statically allocated string and return a pointer
1909 to the printed representation.
1910
1911 This function is intended to be used in conjunction with printf.
1912 It is hard to portably print wgint values:
1913 a) you cannot use printf("%ld", number) because wgint can be long
1914 long on 32-bit machines with LFS.
1915 b) you cannot use printf("%lld", number) because NUMBER could be
1916 long on 32-bit machines without LFS, or on 64-bit machines,
1917 which do not require LFS. Also, Windows doesn't support %lld.
1918 c) you cannot use printf("%j", (int_max_t) number) because not all
1919 versions of printf support "%j", the most notable being the one
1920 on Windows.
1921 d) you cannot #define WGINT_FMT to the appropriate format and use
1922 printf(WGINT_FMT, number) because that would break translations
1923 for user-visible messages, such as printf("Downloaded: %d
1924 bytes\n", number).
1925
1926 What you should use instead is printf("%s", number_to_static_string
1927 (number)).
1928
1929 CAVEAT: since the function returns pointers to static data, you
1930 must be careful to copy its result before calling it again.
1931 However, to make it more useful with printf, the function maintains
1932 an internal ring of static buffers to return. That way things like
1933 printf("%s %s", number_to_static_string (num1),
1934 number_to_static_string (num2)) work as expected. Three buffers
1935 are currently used, which means that "%s %s %s" will work, but "%s
1936 %s %s %s" won't. If you need to print more than three wgints,
1937 bump the RING_SIZE (or rethink your message.) */
1938
1939 char *
1940 number_to_static_string (wgint number)
1941 {
1942 static char ring[RING_SIZE][24];
1943 static int ringpos;
1944 char *buf = ring[ringpos];
1945 number_to_string (buf, number);
1946 ringpos = (ringpos + 1) % RING_SIZE;
1947 return buf;
1948 }
1949
1950 /* Converts the byte to bits format if --report-bps option is enabled
1951 */
1952 wgint
1953 convert_to_bits (wgint num)
1954 {
1955 if (opt.report_bps)
1956 return num * 8;
1957 return num;
1958 }
1959
1960
1961 /* Determine the width of the terminal we're running on. If that's
1962 not possible, return 0. */
1963
1964 int
1965 determine_screen_width (void)
1966 {
1967 /* If there's a way to get the terminal size using POSIX
1968 tcgetattr(), somebody please tell me. */
1969 #ifdef TIOCGWINSZ
1970 int fd;
1971 struct winsize wsz;
1972
1973 if (opt.lfilename != NULL && opt.show_progress != 1)
1974 return 0;
1975
1976 fd = fileno (stderr);
1977 if (ioctl (fd, TIOCGWINSZ, &wsz) < 0)
1978 return 0; /* most likely ENOTTY */
1979
1980 return wsz.ws_col;
1981 #elif defined(WINDOWS)
1982 CONSOLE_SCREEN_BUFFER_INFO csbi;
1983 if (!GetConsoleScreenBufferInfo (GetStdHandle (STD_ERROR_HANDLE), &csbi))
1984 return 0;
1985 return csbi.dwSize.X;
1986 #else /* neither TIOCGWINSZ nor WINDOWS */
1987 return 0;
1988 #endif /* neither TIOCGWINSZ nor WINDOWS */
1989 }
1990
1991 /* Whether the rnd system (either rand or [dl]rand48) has been
1992 seeded. */
1993 static int rnd_seeded;
1994
1995 /* Return a random number between 0 and MAX-1, inclusive.
1996
1997 If the system does not support lrand48 and MAX is greater than the
1998 value of RAND_MAX+1 on the system, the returned value will be in
1999 the range [0, RAND_MAX]. This may be fixed in a future release.
2000 The random number generator is seeded automatically the first time
2001 it is called.
2002
2003 This uses lrand48 where available, rand elsewhere. DO NOT use it
2004 for cryptography. It is only meant to be used in situations where
2005 quality of the random numbers returned doesn't really matter. */
2006
2007 int
2008 random_number (int max)
2009 {
2010 #ifdef HAVE_RANDOM
2011 if (!rnd_seeded)
2012 {
2013 srandom ((long) time (NULL) ^ (long) getpid ());
2014 rnd_seeded = 1;
2015 }
2016 return random () % max;
2017 #elif defined HAVE_DRAND48
2018 if (!rnd_seeded)
2019 {
2020 srand48 ((long) time (NULL) ^ (long) getpid ());
2021 rnd_seeded = 1;
2022 }
2023 return lrand48 () % max;
2024 #else /* not HAVE_DRAND48 */
2025
2026 double bounded;
2027 int rnd;
2028 if (!rnd_seeded)
2029 {
2030 srand ((unsigned) time (NULL) ^ (unsigned) getpid ());
2031 rnd_seeded = 1;
2032 }
2033 rnd = rand ();
2034
2035 /* Like rand() % max, but uses the high-order bits for better
2036 randomness on architectures where rand() is implemented using a
2037 simple congruential generator. */
2038
2039 bounded = (double) max * rnd / (RAND_MAX + 1.0);
2040 return (int) bounded;
2041
2042 #endif /* not HAVE_DRAND48 */
2043 }
2044
2045 /* Return a random uniformly distributed floating point number in the
2046 [0, 1) range. Uses drand48 where available, and a really lame
2047 kludge elsewhere. */
2048
2049 double
2050 random_float (void)
2051 {
2052 #ifdef HAVE_RANDOM
2053 return ((double) random_number (RAND_MAX)) / RAND_MAX;
2054 #elif defined HAVE_DRAND48
2055 if (!rnd_seeded)
2056 {
2057 srand48 ((long) time (NULL) ^ (long) getpid ());
2058 rnd_seeded = 1;
2059 }
2060 return drand48 ();
2061 #else /* not HAVE_DRAND48 */
2062 return ( random_number (10000) / 10000.0
2063 + random_number (10000) / (10000.0 * 10000.0)
2064 + random_number (10000) / (10000.0 * 10000.0 * 10000.0)
2065 + random_number (10000) / (10000.0 * 10000.0 * 10000.0 * 10000.0));
2066 #endif /* not HAVE_DRAND48 */
2067 }
2068
2069 /* Implementation of run_with_timeout, a generic timeout-forcing
2070 routine for systems with Unix-like signal handling. */
2071
2072 #ifdef USE_SIGNAL_TIMEOUT
2073 # ifdef HAVE_SIGSETJMP
2074 # define SETJMP(env) sigsetjmp (env, 1)
2075
2076 static sigjmp_buf run_with_timeout_env;
2077
2078 _Noreturn static void
2079 abort_run_with_timeout (int sig _GL_UNUSED)
2080 {
2081 assert (sig == SIGALRM);
2082 siglongjmp (run_with_timeout_env, -1);
2083 }
2084 # else /* not HAVE_SIGSETJMP */
2085 # define SETJMP(env) setjmp (env)
2086
2087 static jmp_buf run_with_timeout_env;
2088
2089 static void _Noreturn
2090 abort_run_with_timeout (int sig _GL_UNUSED)
2091 {
2092 assert (sig == SIGALRM);
2093 /* We don't have siglongjmp to preserve the set of blocked signals;
2094 if we longjumped out of the handler at this point, SIGALRM would
2095 remain blocked. We must unblock it manually. */
2096 sigset_t set;
2097 sigemptyset (&set);
2098 sigaddset (&set, SIGALRM);
2099 sigprocmask (SIG_BLOCK, &set, NULL);
2100
2101 /* Now it's safe to longjump. */
2102 longjmp (run_with_timeout_env, -1);
2103 }
2104 # endif /* not HAVE_SIGSETJMP */
2105
2106 /* Arrange for SIGALRM to be delivered in TIMEOUT seconds. This uses
2107 setitimer where available, alarm otherwise.
2108
2109 TIMEOUT should be non-zero. If the timeout value is so small that
2110 it would be rounded to zero, it is rounded to the least legal value
2111 instead (1us for setitimer, 1s for alarm). That ensures that
2112 SIGALRM will be delivered in all cases. */
2113
2114 static void
2115 alarm_set (double timeout)
2116 {
2117 #ifdef ITIMER_REAL
2118 /* Use the modern itimer interface. */
2119 struct itimerval itv;
2120 xzero (itv);
2121 itv.it_value.tv_sec = (long) timeout;
2122 itv.it_value.tv_usec = 1000000 * (timeout - (long)timeout);
2123 if (itv.it_value.tv_sec == 0 && itv.it_value.tv_usec == 0)
2124 /* Ensure that we wait for at least the minimum interval.
2125 Specifying zero would mean "wait forever". */
2126 itv.it_value.tv_usec = 1;
2127 setitimer (ITIMER_REAL, &itv, NULL);
2128 #else /* not ITIMER_REAL */
2129 /* Use the old alarm() interface. */
2130 int secs = (int) timeout;
2131 if (secs == 0)
2132 /* Round TIMEOUTs smaller than 1 to 1, not to zero. This is
2133 because alarm(0) means "never deliver the alarm", i.e. "wait
2134 forever", which is not what someone who specifies a 0.5s
2135 timeout would expect. */
2136 secs = 1;
2137 alarm (secs);
2138 #endif /* not ITIMER_REAL */
2139 }
2140
2141 /* Cancel the alarm set with alarm_set. */
2142
2143 static void
2144 alarm_cancel (void)
2145 {
2146 #ifdef ITIMER_REAL
2147 struct itimerval disable;
2148 xzero (disable);
2149 setitimer (ITIMER_REAL, &disable, NULL);
2150 #else /* not ITIMER_REAL */
2151 alarm (0);
2152 #endif /* not ITIMER_REAL */
2153 }
2154
2155 /* Call FUN(ARG), but don't allow it to run for more than TIMEOUT
2156 seconds. Returns true if the function was interrupted with a
2157 timeout, false otherwise.
2158
2159 This works by setting up SIGALRM to be delivered in TIMEOUT seconds
2160 using setitimer() or alarm(). The timeout is enforced by
2161 longjumping out of the SIGALRM handler. This has several
2162 advantages compared to the traditional approach of relying on
2163 signals causing system calls to exit with EINTR:
2164
2165 * The callback function is *forcibly* interrupted after the
2166 timeout expires, (almost) regardless of what it was doing and
2167 whether it was in a syscall. For example, a calculation that
2168 takes a long time is interrupted as reliably as an IO
2169 operation.
2170
2171 * It works with both SYSV and BSD signals because it doesn't
2172 depend on the default setting of SA_RESTART.
2173
2174 * It doesn't require special handler setup beyond a simple call
2175 to signal(). (It does use sigsetjmp/siglongjmp, but they're
2176 optional.)
2177
2178 The only downside is that, if FUN allocates internal resources that
2179 are normally freed prior to exit from the functions, they will be
2180 lost in case of timeout. */
2181
2182 bool
2183 run_with_timeout (double timeout, void (*fun) (void *), void *arg)
2184 {
2185 int saved_errno;
2186
2187 if (timeout == 0)
2188 {
2189 fun (arg);
2190 return false;
2191 }
2192
2193 /* Set alarm handler before doing setjmp. */
2194 signal (SIGALRM, abort_run_with_timeout);
2195
2196 if (SETJMP (run_with_timeout_env) != 0)
2197 {
2198 /* Longjumped out of FUN with a timeout. */
2199 signal (SIGALRM, SIG_DFL);
2200 return true;
2201 }
2202
2203 alarm_set (timeout);
2204 fun (arg);
2205
2206 /* Preserve errno in case alarm() or signal() modifies it. */
2207 saved_errno = errno;
2208 alarm_cancel ();
2209 signal (SIGALRM, SIG_DFL);
2210 errno = saved_errno;
2211
2212 return false;
2213 }
2214
2215 #else /* not USE_SIGNAL_TIMEOUT */
2216
2217 #ifndef WINDOWS
2218 /* A stub version of run_with_timeout that just calls FUN(ARG). Don't
2219 define it under Windows, because Windows has its own version of
2220 run_with_timeout that uses threads. */
2221
2222 bool
2223 run_with_timeout (double timeout, void (*fun) (void *), void *arg)
2224 {
2225 fun (arg);
2226 return false;
2227 }
2228 #endif /* not WINDOWS */
2229 #endif /* not USE_SIGNAL_TIMEOUT */
2230
2231 #ifndef WINDOWS
2232
2233 /* Sleep the specified amount of seconds. On machines without
2234 nanosleep(), this may sleep shorter if interrupted by signals. */
2235
2236 #if defined FUZZING && defined TESTING
2237 void
2238 xsleep (double seconds)
2239 {
2240 // Don't wait when fuzzing
2241 }
2242 #else
2243 void
2244 xsleep (double seconds)
2245 {
2246 #ifdef HAVE_NANOSLEEP
2247 /* nanosleep is the preferred interface because it offers high
2248 accuracy and, more importantly, because it allows us to reliably
2249 restart receiving a signal such as SIGWINCH. (There was an
2250 actual Debian bug report about --limit-rate malfunctioning while
2251 the terminal was being resized.) */
2252 struct timespec sleep, remaining;
2253 sleep.tv_sec = (long) seconds;
2254 sleep.tv_nsec = 1000000000 * (seconds - (long) seconds);
2255 while (nanosleep (&sleep, &remaining) < 0 && errno == EINTR)
2256 /* If nanosleep has been interrupted by a signal, adjust the
2257 sleeping period and return to sleep. */
2258 sleep = remaining;
2259 #elif defined(HAVE_USLEEP)
2260 /* If usleep is available, use it in preference to select. */
2261 if (seconds >= 1)
2262 {
2263 /* On some systems, usleep cannot handle values larger than
2264 1,000,000. If the period is larger than that, use sleep
2265 first, then add usleep for subsecond accuracy. */
2266 sleep (seconds);
2267 seconds -= (long) seconds;
2268 }
2269 usleep (seconds * 1000000);
2270 #else /* fall back select */
2271 /* Note that, although Windows supports select, it can't be used to
2272 implement sleeping because Winsock's select doesn't implement
2273 timeout when it is passed NULL pointers for all fd sets. (But it
2274 does under Cygwin, which implements Unix-compatible select.) */
2275 struct timeval sleep;
2276 sleep.tv_sec = (long) seconds;
2277 sleep.tv_usec = 1000000 * (seconds - (long) seconds);
2278 select (0, NULL, NULL, NULL, &sleep);
2279 /* If select returns -1 and errno is EINTR, it means we were
2280 interrupted by a signal. But without knowing how long we've
2281 actually slept, we can't return to sleep. Using gettimeofday to
2282 track sleeps is slow and unreliable due to clock skew. */
2283 #endif
2284 }
2285 #endif
2286
2287 #endif /* not WINDOWS */
2288
2289 /* Encode the octets in DATA of length LENGTH to base64 format,
2290 storing the result to DEST. The output will be zero-terminated,
2291 and must point to a writable buffer of at least
2292 1+BASE64_LENGTH(length) bytes. The function returns the length of
2293 the resulting base64 data, not counting the terminating zero.
2294
2295 This implementation does not emit newlines after 76 characters of
2296 base64 data. */
2297
2298 size_t
2299 wget_base64_encode (const void *data, size_t length, char *dest)
2300 {
2301 /* Conversion table. */
2302 static const char tbl[64] = {
2303 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
2304 'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
2305 'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
2306 'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'
2307 };
2308 /* Access bytes in DATA as unsigned char, otherwise the shifts below
2309 don't work for data with MSB set. */
2310 const unsigned char *s = data;
2311 /* Theoretical ANSI violation when length < 3. */
2312 const unsigned char *end = (const unsigned char *) data + length - 2;
2313 char *p = dest;
2314
2315 /* Transform the 3x8 bits to 4x6 bits, as required by base64. */
2316 for (; s < end; s += 3)
2317 {
2318 *p++ = tbl[s[0] >> 2];
2319 *p++ = tbl[((s[0] & 3) << 4) + (s[1] >> 4)];
2320 *p++ = tbl[((s[1] & 0xf) << 2) + (s[2] >> 6)];
2321 *p++ = tbl[s[2] & 0x3f];
2322 }
2323
2324 /* Pad the result if necessary... */
2325 switch (length % 3)
2326 {
2327 case 1:
2328 *p++ = tbl[s[0] >> 2];
2329 *p++ = tbl[(s[0] & 3) << 4];
2330 *p++ = '=';
2331 *p++ = '=';
2332 break;
2333 case 2:
2334 *p++ = tbl[s[0] >> 2];
2335 *p++ = tbl[((s[0] & 3) << 4) + (s[1] >> 4)];
2336 *p++ = tbl[((s[1] & 0xf) << 2)];
2337 *p++ = '=';
2338 break;
2339 }
2340 /* ...and zero-terminate it. */
2341 *p = '\0';
2342
2343 return p - dest;
2344 }
2345
2346 /* Store in C the next non-whitespace character from the string, or \0
2347 when end of string is reached. */
2348 #define NEXT_CHAR(c, p) do { \
2349 c = (unsigned char) *p++; \
2350 } while (c_isspace (c))
2351
2352 #define IS_ASCII(c) (((c) & 0x80) == 0)
2353
2354 /* Decode data from BASE64 (a null-terminated string) into memory
2355 pointed to by DEST. DEST is assumed to be large enough to
2356 accommodate the decoded data, which is guaranteed to be no more than
2357 3/4*strlen(base64).
2358
2359 Since DEST is assumed to contain binary data, it is not
2360 NUL-terminated. The function returns the length of the data
2361 written to "TO". -1 is returned in case of error caused by malformed
2362 base64 input.
2363
2364 This function originates from Free Recode. */
2365
2366 ssize_t
2367 wget_base64_decode (const char *base64, void *dest, size_t size)
2368 {
2369 /* Table of base64 values for first 128 characters. Note that this
2370 assumes ASCII (but so does Wget in other places). */
2371 static const signed char base64_char_to_value[128] =
2372 {
2373 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0- 9 */
2374 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 10- 19 */
2375 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 20- 29 */
2376 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 30- 39 */
2377 -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, /* 40- 49 */
2378 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, /* 50- 59 */
2379 -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, /* 60- 69 */
2380 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 70- 79 */
2381 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, /* 80- 89 */
2382 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, /* 90- 99 */
2383 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, /* 100-109 */
2384 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, /* 110-119 */
2385 49, 50, 51, -1, -1, -1, -1, -1 /* 120-127 */
2386 };
2387 #define BASE64_CHAR_TO_VALUE(c) ((int) base64_char_to_value[c])
2388 #define IS_BASE64(c) ((IS_ASCII (c) && BASE64_CHAR_TO_VALUE (c) >= 0) || c == '=')
2389
2390 const char *p = base64;
2391 unsigned char *q = dest;
2392 ssize_t n = 0;
2393
2394 while (1)
2395 {
2396 unsigned char c;
2397 unsigned long value;
2398
2399 /* Process first byte of a quadruplet. */
2400 NEXT_CHAR (c, p);
2401 if (!c)
2402 break;
2403 if (c == '=' || !IS_BASE64 (c))
2404 return -1; /* illegal char while decoding base64 */
2405 value = BASE64_CHAR_TO_VALUE (c) << 18;
2406
2407 /* Process second byte of a quadruplet. */
2408 NEXT_CHAR (c, p);
2409 if (!c)
2410 return -1; /* premature EOF while decoding base64 */
2411 if (c == '=' || !IS_BASE64 (c))
2412 return -1; /* illegal char while decoding base64 */
2413 value |= BASE64_CHAR_TO_VALUE (c) << 12;
2414 if (size)
2415 {
2416 *q++ = value >> 16;
2417 size--;
2418 }
2419 n++;
2420
2421 /* Process third byte of a quadruplet. */
2422 NEXT_CHAR (c, p);
2423 if (!c)
2424 return -1; /* premature EOF while decoding base64 */
2425 if (!IS_BASE64 (c))
2426 return -1; /* illegal char while decoding base64 */
2427
2428 if (c == '=')
2429 {
2430 NEXT_CHAR (c, p);
2431 if (!c)
2432 return -1; /* premature EOF while decoding base64 */
2433 if (c != '=')
2434 return -1; /* padding `=' expected but not found */
2435 continue;
2436 }
2437
2438 value |= BASE64_CHAR_TO_VALUE (c) << 6;
2439 if (size)
2440 {
2441 *q++ = 0xff & value >> 8;
2442 size--;
2443 }
2444 n++;
2445
2446 /* Process fourth byte of a quadruplet. */
2447 NEXT_CHAR (c, p);
2448 if (!c)
2449 return -1; /* premature EOF while decoding base64 */
2450 if (c == '=')
2451 continue;
2452 if (!IS_BASE64 (c))
2453 return -1; /* illegal char while decoding base64 */
2454
2455 value |= BASE64_CHAR_TO_VALUE (c);
2456 if (size)
2457 {
2458 *q++ = 0xff & value;
2459 size--;
2460 }
2461 n++;
2462 }
2463 #undef IS_BASE64
2464 #undef BASE64_CHAR_TO_VALUE
2465
2466 return n;
2467 }
2468
2469 #ifdef HAVE_LIBPCRE2
2470 /* Compiles the PCRE2 regex. */
2471 void *
2472 compile_pcre2_regex (const char *str)
2473 {
2474 int errornumber;
2475 PCRE2_SIZE erroroffset;
2476 pcre2_code *regex = pcre2_compile((PCRE2_SPTR) str, PCRE2_ZERO_TERMINATED, 0, &errornumber, &erroroffset, NULL);
2477 if (! regex)
2478 {
2479 fprintf (stderr, _("Invalid regular expression %s, PCRE2 error %d\n"),
2480 quote (str), errornumber);
2481 }
2482 return regex;
2483 }
2484 #endif
2485
2486 #ifdef HAVE_LIBPCRE
2487 /* Compiles the PCRE regex. */
2488 void *
2489 compile_pcre_regex (const char *str)
2490 {
2491 const char *errbuf;
2492 int erroffset;
2493 pcre *regex = pcre_compile (str, 0, &errbuf, &erroffset, 0);
2494 if (! regex)
2495 {
2496 fprintf (stderr, _("Invalid regular expression %s, %s\n"),
2497 quote (str), errbuf);
2498 }
2499 return regex;
2500 }
2501 #endif
2502
2503 /* Compiles the POSIX regex. */
2504 void *
2505 compile_posix_regex (const char *str)
2506 {
2507 regex_t *regex = xmalloc (sizeof (regex_t));
2508 #ifdef TESTING
2509 /* regcomp might be *very* cpu+memory intensive,
2510 * see https://sourceware.org/glibc/wiki/Security%20Exceptions */
2511 str = "a";
2512 #endif
2513 int errcode = regcomp (regex, str, REG_EXTENDED | REG_NOSUB);
2514 if (errcode != 0)
2515 {
2516 size_t errbuf_size = regerror (errcode, regex, NULL, 0);
2517 char *errbuf = xmalloc (errbuf_size);
2518 regerror (errcode, regex, errbuf, errbuf_size);
2519 fprintf (stderr, _("Invalid regular expression %s, %s\n"),
2520 quote (str), errbuf);
2521 xfree (errbuf);
2522 xfree (regex);
2523 return NULL;
2524 }
2525
2526 return regex;
2527 }
2528
2529 #ifdef HAVE_LIBPCRE2
2530 /* Matches a PCRE2 regex. */
2531 bool
2532 match_pcre2_regex (const void *regex, const char *str)
2533 {
2534 int rc;
2535 pcre2_match_data *match_data;
2536
2537 match_data = pcre2_match_data_create_from_pattern(regex, NULL);
2538
2539 if (match_data)
2540 {
2541 rc = pcre2_match(regex, (PCRE2_SPTR) str, strlen(str), 0, 0, match_data, NULL);
2542 pcre2_match_data_free(match_data);
2543 }
2544 else
2545 rc = PCRE2_ERROR_NOMEMORY;
2546
2547 if (rc < 0 && rc != PCRE2_ERROR_NOMATCH)
2548 {
2549 logprintf (LOG_VERBOSE, _("Error while matching %s: %d\n"),
2550 quote (str), rc);
2551 }
2552
2553 return rc >= 0;
2554 }
2555 #endif
2556
2557 #ifdef HAVE_LIBPCRE
2558 #define OVECCOUNT 30
2559 /* Matches a PCRE regex. */
2560 bool
2561 match_pcre_regex (const void *regex, const char *str)
2562 {
2563 size_t l = strlen (str);
2564 int ovector[OVECCOUNT];
2565
2566 int rc = pcre_exec ((pcre *) regex, 0, str, (int) l, 0, 0, ovector, OVECCOUNT);
2567 if (rc == PCRE_ERROR_NOMATCH)
2568 return false;
2569 else if (rc < 0)
2570 {
2571 logprintf (LOG_VERBOSE, _("Error while matching %s: %d\n"),
2572 quote (str), rc);
2573 return false;
2574 }
2575 else
2576 return true;
2577 }
2578 #undef OVECCOUNT
2579 #endif
2580
2581 /* Matches a POSIX regex. */
2582 bool
2583 match_posix_regex (const void *regex, const char *str)
2584 {
2585 int rc = regexec ((regex_t *) regex, str, 0, NULL, 0);
2586 if (rc == REG_NOMATCH)
2587 return false;
2588 else if (rc == 0)
2589 return true;
2590 else
2591 {
2592 size_t errbuf_size = regerror (rc, opt.acceptregex, NULL, 0);
2593 char *errbuf = xmalloc (errbuf_size);
2594 regerror (rc, opt.acceptregex, errbuf, errbuf_size);
2595 logprintf (LOG_VERBOSE, _("Error while matching %s: %d\n"),
2596 quote (str), rc);
2597 xfree (errbuf);
2598 return false;
2599 }
2600 }
2601
2602 #undef IS_ASCII
2603 #undef NEXT_CHAR
2604
2605 /* Simple merge sort for use by stable_sort. Implementation courtesy
2606 Zeljko Vrba with additional debugging by Nenad Barbutov. */
2607
2608 static void
2609 mergesort_internal (void *base, void *temp, size_t size, size_t from, size_t to,
2610 int (*cmpfun) (const void *, const void *))
2611 {
2612 #define ELT(array, pos) ((char *)(array) + (pos) * size)
2613 if (from < to)
2614 {
2615 size_t i, j, k;
2616 size_t mid = (to + from) / 2;
2617 mergesort_internal (base, temp, size, from, mid, cmpfun);
2618 mergesort_internal (base, temp, size, mid + 1, to, cmpfun);
2619 i = from;
2620 j = mid + 1;
2621 for (k = from; (i <= mid) && (j <= to); k++)
2622 if (cmpfun (ELT (base, i), ELT (base, j)) <= 0)
2623 memcpy (ELT (temp, k), ELT (base, i++), size);
2624 else
2625 memcpy (ELT (temp, k), ELT (base, j++), size);
2626 while (i <= mid)
2627 memcpy (ELT (temp, k++), ELT (base, i++), size);
2628 while (j <= to)
2629 memcpy (ELT (temp, k++), ELT (base, j++), size);
2630 for (k = from; k <= to; k++)
2631 memcpy (ELT (base, k), ELT (temp, k), size);
2632 }
2633 #undef ELT
2634 }
2635
2636 /* Stable sort with interface exactly like standard library's qsort.
2637 Uses mergesort internally. */
2638
2639 void
2640 stable_sort (void *base, size_t nmemb, size_t size,
2641 int (*cmpfun) (const void *, const void *))
2642 {
2643 if (nmemb > 1 && size > 1)
2644 {
2645 void *temp = xmalloc (nmemb * size);
2646 mergesort_internal (base, temp, size, 0, nmemb - 1, cmpfun);
2647 xfree(temp);
2648 }
2649 }
2650
2651 /* Print a decimal number. If it is equal to or larger than ten, the
2652 number is rounded. Otherwise it is printed with one significant
2653 digit without trailing zeros and with no more than three fractional
2654 digits total. For example, 0.1 is printed as "0.1", 0.035 is
2655 printed as "0.04", 0.0091 as "0.009", and 0.0003 as simply "0".
2656
2657 This is useful for displaying durations because it provides
2658 order-of-magnitude information without unnecessary clutter --
2659 long-running downloads are shown without the fractional part, and
2660 short ones still retain one significant digit. */
2661
2662 const char *
2663 print_decimal (double number)
2664 {
2665 static char buf[32];
2666 double n = number >= 0 ? number : -number;
2667
2668 if (n >= 9.95)
2669 /* Cut off at 9.95 because the below %.1f would round 9.96 to
2670 "10.0" instead of "10". OTOH 9.94 will print as "9.9". */
2671 snprintf (buf, sizeof buf, "%.0f", number);
2672 else if (n >= 0.95)
2673 snprintf (buf, sizeof buf, "%.1f", number);
2674 else if (n >= 0.001)
2675 snprintf (buf, sizeof buf, "%.1g", number);
2676 else if (n >= 0.0005)
2677 /* round [0.0005, 0.001) to 0.001 */
2678 snprintf (buf, sizeof buf, "%.3f", number);
2679 else
2680 /* print numbers close to 0 as 0, not 0.000 */
2681 strcpy (buf, "0");
2682
2683 return buf;
2684 }
2685
2686 /* Get the maximum name length for the given path. */
2687 /* Return 0 if length is unknown. */
2688 long
2689 get_max_length (const char *path, int length, int name)
2690 {
2691 long ret;
2692 char *p, *d;
2693
2694 /* Make a copy of the path that we can modify. */
2695 p = path ? strdupdelim (path, path + length) : strdup ("");
2696
2697 for (;;)
2698 {
2699 errno = 0;
2700 /* For an empty path query the current directory. */
2701 #if HAVE_PATHCONF
2702 ret = pathconf (*p ? p : ".", name);
2703 if (!(ret < 0 && errno == ENOENT))
2704 break;
2705 #else
2706 ret = PATH_MAX;
2707 #endif
2708
2709 /* The path does not exist yet, but may be created. */
2710 /* Already at current or root directory, give up. */
2711 if (!*p || strcmp (p, "/") == 0)
2712 break;
2713
2714 /* Remove one directory level and try again. */
2715 d = strrchr (p, '/');
2716 if (d == p)
2717 p[1] = '\0'; /* check root directory */
2718 else if (d)
2719 *d = '\0'; /* remove last directory part */
2720 else
2721 *p = '\0'; /* check current directory */
2722 }
2723
2724 xfree (p);
2725
2726 if (ret < 0)
2727 {
2728 /* pathconf() has a message for us. */
2729 if (errno != 0)
2730 perror ("pathconf");
2731
2732 /* If (errno == 0) then there is no max length.
2733 Even on error return 0 so the caller can continue. */
2734 return 0;
2735 }
2736
2737 return ret;
2738 }
2739
2740 void
2741 wg_hex_to_string (char *str_buffer, const char *hex_buffer, size_t hex_len)
2742 {
2743 size_t i;
2744
2745 for (i = 0; i < hex_len; i++)
2746 {
2747 /* Each byte takes 2 characters. */
2748 sprintf (str_buffer + 2 * i, "%02x", (unsigned) (hex_buffer[i] & 0xFF));
2749 }
2750
2751 /* Null-terminate result. */
2752 str_buffer[2 * i] = '\0';
2753 }
2754
2755 #ifdef HAVE_SSL
2756
2757 /*
2758 * Public key pem to der conversion
2759 */
2760
2761 static bool
2762 wg_pubkey_pem_to_der (const char *pem, unsigned char **der, size_t *der_len)
2763 {
2764 char *stripped_pem, *begin_pos, *end_pos;
2765 size_t pem_count, stripped_pem_count = 0, pem_len;
2766 ssize_t size;
2767 unsigned char *base64data;
2768
2769 *der = NULL;
2770 *der_len = 0;
2771
2772 /* if no pem, exit. */
2773 if (!pem)
2774 return false;
2775
2776 begin_pos = strstr (pem, "-----BEGIN PUBLIC KEY-----");
2777 if (!begin_pos)
2778 return false;
2779
2780 pem_count = begin_pos - pem;
2781 /* Invalid if not at beginning AND not directly following \n */
2782 if (0 != pem_count && '\n' != pem[pem_count - 1])
2783 return false;
2784
2785 /* 26 is length of "-----BEGIN PUBLIC KEY-----" */
2786 pem_count += 26;
2787
2788 /* Invalid if not directly following \n */
2789 end_pos = strstr (pem + pem_count, "\n-----END PUBLIC KEY-----");
2790 if (!end_pos)
2791 return false;
2792
2793 pem_len = end_pos - pem;
2794
2795 stripped_pem = xmalloc (pem_len - pem_count + 1);
2796
2797 /*
2798 * Here we loop through the pem array one character at a time between the
2799 * correct indices, and place each character that is not '\n' or '\r'
2800 * into the stripped_pem array, which should represent the raw base64 string
2801 */
2802 while (pem_count < pem_len) {
2803 if ('\n' != pem[pem_count] && '\r' != pem[pem_count])
2804 stripped_pem[stripped_pem_count++] = pem[pem_count];
2805 ++pem_count;
2806 }
2807 /* Place the null terminator in the correct place */
2808 stripped_pem[stripped_pem_count] = '\0';
2809
2810 base64data = xmalloc (BASE64_LENGTH(stripped_pem_count));
2811
2812 size = wget_base64_decode (stripped_pem, base64data, BASE64_LENGTH(stripped_pem_count));
2813
2814 if (size < 0) {
2815 xfree (base64data); /* malformed base64 from server */
2816 } else {
2817 *der = base64data;
2818 *der_len = (size_t) size;
2819 }
2820
2821 xfree (stripped_pem);
2822
2823 return *der_len > 0;
2824 }
2825
2826 /*
2827 * Generic pinned public key check.
2828 */
2829
2830 bool
2831 wg_pin_peer_pubkey (const char *pinnedpubkey, const char *pubkey, size_t pubkeylen)
2832 {
2833 struct file_memory *fm;
2834 unsigned char *buf = NULL, *pem_ptr = NULL;
2835 size_t size, pem_len;
2836 bool pem_read;
2837 bool result = false;
2838
2839 size_t pinkeylen;
2840 ssize_t decoded_hash_length;
2841 char *pinkeycopy, *begin_pos, *end_pos;
2842 unsigned char *sha256sumdigest = NULL, *expectedsha256sumdigest = NULL;
2843
2844 /* if a path wasn't specified, don't pin */
2845 if (!pinnedpubkey)
2846 return true;
2847 if (!pubkey || !pubkeylen)
2848 return result;
2849
2850 /* only do this if pinnedpubkey starts with "sha256//", length 8 */
2851 if (strncmp (pinnedpubkey, "sha256//", 8) == 0)
2852 {
2853 /* compute sha256sum of public key */
2854 sha256sumdigest = xmalloc (SHA256_DIGEST_SIZE);
2855 sha256_buffer (pubkey, pubkeylen, sha256sumdigest);
2856 expectedsha256sumdigest = xmalloc (SHA256_DIGEST_SIZE);
2857
2858 /* it starts with sha256//, copy so we can modify it */
2859 pinkeylen = strlen (pinnedpubkey) + 1;
2860 pinkeycopy = xmalloc (pinkeylen);
2861 memcpy (pinkeycopy, pinnedpubkey, pinkeylen);
2862
2863 /* point begin_pos to the copy, and start extracting keys */
2864 begin_pos = pinkeycopy;
2865 do
2866 {
2867 end_pos = strstr (begin_pos, ";sha256//");
2868 /*
2869 * if there is an end_pos, null terminate,
2870 * otherwise it'll go to the end of the original string
2871 */
2872 if (end_pos)
2873 end_pos[0] = '\0';
2874
2875 /* decode base64 pinnedpubkey, 8 is length of "sha256//" */
2876 decoded_hash_length = wget_base64_decode (begin_pos + 8, expectedsha256sumdigest, SHA256_DIGEST_SIZE);
2877
2878 /* if valid base64, compare sha256 digests directly */
2879 if (SHA256_DIGEST_SIZE == decoded_hash_length)
2880 {
2881 if (!memcmp (sha256sumdigest, expectedsha256sumdigest, SHA256_DIGEST_SIZE))
2882 {
2883 result = true;
2884 break;
2885 }
2886 }
2887 else
2888 logprintf (LOG_VERBOSE, _ ("Skipping key with wrong size (%d/%d): %s\n"),
2889 (int) (strlen (begin_pos + 8) * 3) / 4, SHA256_DIGEST_SIZE,
2890 quote (begin_pos + 8));
2891
2892 /*
2893 * change back the null-terminator we changed earlier,
2894 * and look for next begin
2895 */
2896 if (end_pos)
2897 {
2898 end_pos[0] = ';';
2899 begin_pos = strstr (end_pos, "sha256//");
2900 }
2901 }
2902 while (end_pos && begin_pos);
2903
2904 xfree (sha256sumdigest);
2905 xfree (expectedsha256sumdigest);
2906 xfree (pinkeycopy);
2907
2908 return result;
2909 }
2910
2911 /* fall back to assuming this is a file path */
2912 fm = wget_read_file (pinnedpubkey);
2913 if (!fm)
2914 return result;
2915
2916 /* Check the file's size */
2917 if (fm->length < 0 || fm->length > MAX_PINNED_PUBKEY_SIZE)
2918 goto cleanup;
2919
2920 /*
2921 * if the size of our certificate is bigger than the file
2922 * size then it can't match
2923 */
2924 size = (size_t) fm->length;
2925 if (pubkeylen > size)
2926 goto cleanup;
2927
2928 /* If the sizes are the same, it can't be base64 encoded, must be der */
2929 if (pubkeylen == size)
2930 {
2931 if (!memcmp (pubkey, fm->content, pubkeylen))
2932 result = true;
2933 goto cleanup;
2934 }
2935
2936 /*
2937 * Otherwise we will assume it's PEM and try to decode it
2938 * after placing null terminator
2939 */
2940 buf = xmalloc (size + 1);
2941 memcpy (buf, fm->content, size);
2942 buf[size] = '\0';
2943
2944 pem_read = wg_pubkey_pem_to_der ((const char *) buf, &pem_ptr, &pem_len);
2945 /* if it wasn't read successfully, exit */
2946 if (!pem_read)
2947 goto cleanup;
2948
2949 /*
2950 * if the size of our certificate doesn't match the size of
2951 * the decoded file, they can't be the same, otherwise compare
2952 */
2953 if (pubkeylen == pem_len && !memcmp (pubkey, pem_ptr, pubkeylen))
2954 result = true;
2955
2956 cleanup:
2957 xfree (buf);
2958 xfree (pem_ptr);
2959 wget_read_file_free (fm);
2960
2961 return result;
2962 }
2963
2964 #endif /* HAVE_SSL */
2965
2966 #ifdef TESTING
2967
2968 const char *
2969 test_subdir_p(void)
2970 {
2971 static const struct {
2972 const char *d1;
2973 const char *d2;
2974 bool result;
2975 } test_array[] = {
2976 { "/somedir", "/somedir", true },
2977 { "/somedir", "/somedir/d2", true },
2978 { "/somedir/d1", "/somedir", false },
2979 };
2980 unsigned i;
2981
2982 for (i = 0; i < countof(test_array); ++i)
2983 {
2984 bool res = subdir_p (test_array[i].d1, test_array[i].d2);
2985
2986 mu_assert ("test_subdir_p: wrong result",
2987 res == test_array[i].result);
2988 }
2989
2990 return NULL;
2991 }
2992
2993 const char *
2994 test_dir_matches_p(void)
2995 {
2996 static struct {
2997 const char *dirlist[3];
2998 const char *dir;
2999 bool result;
3000 } test_array[] = {
3001 { { "/somedir", "/someotherdir", NULL }, "somedir", true },
3002 { { "/somedir", "/someotherdir", NULL }, "anotherdir", false },
3003 { { "/somedir", "/*otherdir", NULL }, "anotherdir", true },
3004 { { "/somedir/d1", "/someotherdir", NULL }, "somedir/d1", true },
3005 { { "*/*d1", "/someotherdir", NULL }, "somedir/d1", true },
3006 { { "/somedir/d1", "/someotherdir", NULL }, "d1", false },
3007 { { "!COMPLETE", NULL, NULL }, "!COMPLETE", true },
3008 { { "*COMPLETE", NULL, NULL }, "!COMPLETE", true },
3009 { { "*/!COMPLETE", NULL, NULL }, "foo/!COMPLETE", true },
3010 { { "*COMPLETE", NULL, NULL }, "foo/!COMPLETE", false },
3011 { { "*/*COMPLETE", NULL, NULL }, "foo/!COMPLETE", true },
3012 { { "/dir with spaces", NULL, NULL }, "dir with spaces", true },
3013 { { "/dir*with*spaces", NULL, NULL }, "dir with spaces", true },
3014 { { "/Tmp/has", NULL, NULL }, "/Tmp/has space", false },
3015 { { "/Tmp/has", NULL, NULL }, "/Tmp/has,comma", false },
3016 };
3017 unsigned i;
3018
3019 for (i = 0; i < countof(test_array); ++i)
3020 {
3021 bool res = dir_matches_p (test_array[i].dirlist, test_array[i].dir);
3022
3023 mu_assert ("test_dir_matches_p: wrong result",
3024 res == test_array[i].result);
3025 }
3026
3027 return NULL;
3028 }
3029
3030 #endif /* TESTING */