Coverage Report

Created: 2026-12-01 07:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/bind9/lib/isc/file.c
Line
Count
Source
1
/*
2
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3
 *
4
 * SPDX-License-Identifier: MPL-2.0
5
 *
6
 * This Source Code Form is subject to the terms of the Mozilla Public
7
 * License, v. 2.0. If a copy of the MPL was not distributed with this
8
 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
9
 *
10
 * See the COPYRIGHT file distributed with this work for additional
11
 * information regarding copyright ownership.
12
 */
13
14
/*
15
 * Portions Copyright (c) 1987, 1993
16
 *      The Regents of the University of California.  All rights reserved.
17
 *
18
 * Redistribution and use in source and binary forms, with or without
19
 * modification, are permitted provided that the following conditions
20
 * are met:
21
 * 1. Redistributions of source code must retain the above copyright
22
 *    notice, this list of conditions and the following disclaimer.
23
 * 2. Redistributions in binary form must reproduce the above copyright
24
 *    notice, this list of conditions and the following disclaimer in the
25
 *    documentation and/or other materials provided with the distribution.
26
 * 3. Neither the name of the University nor the names of its contributors
27
 *    may be used to endorse or promote products derived from this software
28
 *    without specific prior written permission.
29
 *
30
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40
 * SUCH DAMAGE.
41
 */
42
43
/*! \file */
44
45
#include <errno.h>
46
#include <fcntl.h>
47
#include <inttypes.h>
48
#include <limits.h>
49
#include <stdbool.h>
50
#include <stdlib.h>
51
#include <sys/stat.h>
52
#include <sys/time.h>
53
#include <time.h>   /* Required for utimes on some platforms. */
54
#include <unistd.h> /* Required for mkstemp on NetBSD. */
55
56
#ifdef HAVE_SYS_MMAN_H
57
#include <sys/mman.h>
58
#endif /* ifdef HAVE_SYS_MMAN_H */
59
60
#include <isc/buffer.h>
61
#include <isc/dir.h>
62
#include <isc/file.h>
63
#include <isc/hex.h>
64
#include <isc/log.h>
65
#include <isc/md.h>
66
#include <isc/mem.h>
67
#include <isc/random.h>
68
#include <isc/string.h>
69
#include <isc/time.h>
70
#include <isc/util.h>
71
72
#include "errno2result.h"
73
74
/*
75
 * XXXDCL As the API for accessing file statistics undoubtedly gets expanded,
76
 * it might be good to provide a mechanism that allows for the results
77
 * of a previous stat() to be used again without having to do another stat,
78
 * such as perl's mechanism of using "_" in place of a file name to indicate
79
 * that the results of the last stat should be used.  But then you get into
80
 * annoying MP issues.   BTW, Win32 has stat().
81
 */
82
static isc_result_t
83
8
file_stats(const char *file, struct stat *stats) {
84
8
  isc_result_t result = ISC_R_SUCCESS;
85
86
8
  REQUIRE(file != NULL);
87
8
  REQUIRE(stats != NULL);
88
89
8
  if (stat(file, stats) != 0) {
90
4
    result = isc__errno2result(errno);
91
4
  }
92
93
8
  return result;
94
8
}
95
96
static isc_result_t
97
0
file_lstats(const char *file, struct stat *stats) {
98
0
  isc_result_t result = ISC_R_SUCCESS;
99
100
0
  REQUIRE(file != NULL);
101
0
  REQUIRE(stats != NULL);
102
103
0
  if (lstat(file, stats) != 0) {
104
0
    result = isc__errno2result(errno);
105
0
  }
106
107
0
  return result;
108
0
}
109
110
static isc_result_t
111
0
fd_stats(int fd, struct stat *stats) {
112
0
  isc_result_t result = ISC_R_SUCCESS;
113
114
0
  REQUIRE(stats != NULL);
115
116
0
  if (fstat(fd, stats) != 0) {
117
0
    result = isc__errno2result(errno);
118
0
  }
119
120
0
  return result;
121
0
}
122
123
isc_result_t
124
0
isc_file_getsizefd(int fd, off_t *size) {
125
0
  isc_result_t result;
126
0
  struct stat stats;
127
128
0
  REQUIRE(size != NULL);
129
130
0
  result = fd_stats(fd, &stats);
131
132
0
  if (result == ISC_R_SUCCESS) {
133
0
    *size = stats.st_size;
134
0
  }
135
136
0
  return result;
137
0
}
138
139
isc_result_t
140
0
isc_file_mode(const char *file, mode_t *modep) {
141
0
  isc_result_t result;
142
0
  struct stat stats;
143
144
0
  REQUIRE(modep != NULL);
145
146
0
  result = file_stats(file, &stats);
147
0
  if (result == ISC_R_SUCCESS) {
148
0
    *modep = (stats.st_mode & 07777);
149
0
  }
150
151
0
  return result;
152
0
}
153
154
isc_result_t
155
2
isc_file_getmodtime(const char *file, isc_time_t *modtime) {
156
2
  isc_result_t result;
157
2
  struct stat stats;
158
159
2
  REQUIRE(file != NULL);
160
2
  REQUIRE(modtime != NULL);
161
162
2
  result = file_stats(file, &stats);
163
164
2
  if (result == ISC_R_SUCCESS) {
165
2
#if defined(HAVE_STAT_NSEC)
166
2
    isc_time_set(modtime, stats.st_mtime, stats.st_mtim.tv_nsec);
167
#else  /* if defined(HAVE_STAT_NSEC) */
168
    isc_time_set(modtime, stats.st_mtime, 0);
169
#endif /* if defined(HAVE_STAT_NSEC) */
170
2
  }
171
172
2
  return result;
173
2
}
174
175
isc_result_t
176
0
isc_file_getsize(const char *file, off_t *size) {
177
0
  isc_result_t result;
178
0
  struct stat stats;
179
180
0
  REQUIRE(file != NULL);
181
0
  REQUIRE(size != NULL);
182
183
0
  result = file_stats(file, &stats);
184
185
0
  if (result == ISC_R_SUCCESS) {
186
0
    *size = stats.st_size;
187
0
  }
188
189
0
  return result;
190
0
}
191
192
isc_result_t
193
0
isc_file_settime(const char *file, isc_time_t *when) {
194
0
  struct timeval times[2];
195
196
0
  REQUIRE(file != NULL && when != NULL);
197
198
  /*
199
   * tv_sec is at least a 32 bit quantity on all platforms we're
200
   * dealing with, but it is signed on most (all?) of them,
201
   * so we need to make sure the high bit isn't set.  This unfortunately
202
   * loses when either:
203
   *   * tv_sec becomes a signed 64 bit integer but long is 32 bits
204
   *  and isc_time_seconds > LONG_MAX, or
205
   *   * isc_time_seconds is changed to be > 32 bits but long is 32 bits
206
   *      and isc_time_seconds has at least 33 significant bits.
207
   */
208
0
  times[0].tv_sec = times[1].tv_sec = (long)isc_time_seconds(when);
209
210
  /*
211
   * Here is the real check for the high bit being set.
212
   */
213
0
  if ((times[0].tv_sec &
214
0
       (1ULL << (sizeof(times[0].tv_sec) * CHAR_BIT - 1))) != 0)
215
0
  {
216
0
    return ISC_R_RANGE;
217
0
  }
218
219
  /*
220
   * isc_time_nanoseconds guarantees a value that divided by 1000 will
221
   * fit into the minimum possible size tv_usec field.
222
   */
223
0
  times[0].tv_usec = times[1].tv_usec =
224
0
    (int32_t)(isc_time_nanoseconds(when) / 1000);
225
226
0
  if (utimes(file, times) < 0) {
227
0
    return isc__errno2result(errno);
228
0
  }
229
230
0
  return ISC_R_SUCCESS;
231
0
}
232
233
#undef TEMPLATE
234
0
#define TEMPLATE "tmp-XXXXXXXXXX" /*%< 14 characters. */
235
236
isc_result_t
237
0
isc_file_mktemplate(const char *path, char *buf, size_t buflen) {
238
0
  return isc_file_template(path, TEMPLATE, buf, buflen);
239
0
}
240
241
isc_result_t
242
isc_file_template(const char *path, const char *templet, char *buf,
243
0
      size_t buflen) {
244
0
  const char *s;
245
246
0
  REQUIRE(templet != NULL);
247
0
  REQUIRE(buf != NULL);
248
249
0
  if (path == NULL) {
250
0
    path = "";
251
0
  }
252
253
0
  s = strrchr(templet, '/');
254
0
  if (s != NULL) {
255
0
    templet = s + 1;
256
0
  }
257
258
0
  s = strrchr(path, '/');
259
260
0
  if (s != NULL) {
261
0
    size_t prefixlen = s - path + 1;
262
0
    if ((prefixlen + strlen(templet) + 1) > buflen) {
263
0
      return ISC_R_NOSPACE;
264
0
    }
265
266
    /* Copy 'prefixlen' bytes and NUL terminate. */
267
0
    strlcpy(buf, path, ISC_MIN(prefixlen + 1, buflen));
268
0
    strlcat(buf, templet, buflen);
269
0
  } else {
270
0
    if ((strlen(templet) + 1) > buflen) {
271
0
      return ISC_R_NOSPACE;
272
0
    }
273
274
0
    strlcpy(buf, templet, buflen);
275
0
  }
276
277
0
  return ISC_R_SUCCESS;
278
0
}
279
280
static const char alphnum[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv"
281
            "wxyz0123456789";
282
283
isc_result_t
284
0
isc_file_renameunique(const char *file, char *templet) {
285
0
  char *x;
286
0
  char *cp;
287
288
0
  REQUIRE(file != NULL);
289
0
  REQUIRE(templet != NULL);
290
291
0
  cp = templet;
292
0
  while (*cp != '\0') {
293
0
    cp++;
294
0
  }
295
0
  if (cp == templet) {
296
0
    return ISC_R_FAILURE;
297
0
  }
298
299
0
  x = cp--;
300
0
  while (cp >= templet && *cp == 'X') {
301
0
    *cp = alphnum[isc_random_uniform(sizeof(alphnum) - 1)];
302
0
    x = cp--;
303
0
  }
304
0
  while (link(file, templet) == -1) {
305
0
    if (errno != EEXIST) {
306
0
      return isc__errno2result(errno);
307
0
    }
308
0
    for (cp = x;;) {
309
0
      const char *t;
310
0
      if (*cp == '\0') {
311
0
        return ISC_R_FAILURE;
312
0
      }
313
0
      t = strchr(alphnum, *cp);
314
0
      if (t == NULL || *++t == '\0') {
315
0
        *cp++ = alphnum[0];
316
0
      } else {
317
0
        *cp = *t;
318
0
        break;
319
0
      }
320
0
    }
321
0
  }
322
0
  if (unlink(file) < 0) {
323
0
    if (errno != ENOENT) {
324
0
      return isc__errno2result(errno);
325
0
    }
326
0
  }
327
0
  return ISC_R_SUCCESS;
328
0
}
329
330
isc_result_t
331
0
isc_file_openunique(char *templet, FILE **fp) {
332
0
  int mode = S_IWUSR | S_IRUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
333
0
  return isc_file_openuniquemode(templet, mode, fp);
334
0
}
335
336
isc_result_t
337
0
isc_file_openuniqueprivate(char *templet, FILE **fp) {
338
0
  int mode = S_IWUSR | S_IRUSR;
339
0
  return isc_file_openuniquemode(templet, mode, fp);
340
0
}
341
342
isc_result_t
343
0
isc_file_openuniquemode(char *templet, int mode, FILE **fp) {
344
0
  int fd;
345
0
  FILE *f;
346
0
  isc_result_t result = ISC_R_SUCCESS;
347
0
  char *x;
348
0
  char *cp;
349
350
0
  REQUIRE(templet != NULL);
351
0
  REQUIRE(fp != NULL && *fp == NULL);
352
353
0
  cp = templet;
354
0
  while (*cp != '\0') {
355
0
    cp++;
356
0
  }
357
0
  if (cp == templet) {
358
0
    return ISC_R_FAILURE;
359
0
  }
360
361
0
  x = cp--;
362
0
  while (cp >= templet && *cp == 'X') {
363
0
    *cp = alphnum[isc_random_uniform(sizeof(alphnum) - 1)];
364
0
    x = cp--;
365
0
  }
366
367
0
  while ((fd = open(templet, O_RDWR | O_CREAT | O_EXCL, mode)) == -1) {
368
0
    if (errno != EEXIST) {
369
0
      return isc__errno2result(errno);
370
0
    }
371
0
    for (cp = x;;) {
372
0
      const char *t;
373
0
      if (*cp == '\0') {
374
0
        return ISC_R_FAILURE;
375
0
      }
376
0
      t = strchr(alphnum, *cp);
377
0
      if (t == NULL || *++t == '\0') {
378
0
        *cp++ = alphnum[0];
379
0
      } else {
380
0
        *cp = *t;
381
0
        break;
382
0
      }
383
0
    }
384
0
  }
385
0
  f = fdopen(fd, "w+");
386
0
  if (f == NULL) {
387
0
    result = isc__errno2result(errno);
388
0
    if (remove(templet) < 0) {
389
0
      isc_log_write(ISC_LOGCATEGORY_GENERAL,
390
0
              ISC_LOGMODULE_FILE, ISC_LOG_ERROR,
391
0
              "remove '%s': failed", templet);
392
0
    }
393
0
    (void)close(fd);
394
0
  } else {
395
0
    *fp = f;
396
0
  }
397
398
0
  return result;
399
0
}
400
401
isc_result_t
402
0
isc_file_remove(const char *filename) {
403
0
  int r;
404
405
0
  REQUIRE(filename != NULL);
406
407
0
  r = unlink(filename);
408
0
  if (r == 0) {
409
0
    return ISC_R_SUCCESS;
410
0
  } else {
411
0
    return isc__errno2result(errno);
412
0
  }
413
0
}
414
415
isc_result_t
416
0
isc_file_rename(const char *oldname, const char *newname) {
417
0
  int r;
418
419
0
  REQUIRE(oldname != NULL);
420
0
  REQUIRE(newname != NULL);
421
422
0
  r = rename(oldname, newname);
423
0
  if (r == 0) {
424
0
    return ISC_R_SUCCESS;
425
0
  } else {
426
0
    return isc__errno2result(errno);
427
0
  }
428
0
}
429
430
bool
431
6
isc_file_exists(const char *pathname) {
432
6
  struct stat stats;
433
434
6
  REQUIRE(pathname != NULL);
435
436
6
  return file_stats(pathname, &stats) == ISC_R_SUCCESS;
437
6
}
438
439
isc_result_t
440
0
isc_file_isplainfile(const char *filename) {
441
  /*
442
   * This function returns success if filename is a plain file.
443
   */
444
0
  struct stat filestat;
445
0
  memset(&filestat, 0, sizeof(struct stat));
446
447
0
  if ((stat(filename, &filestat)) == -1) {
448
0
    return isc__errno2result(errno);
449
0
  }
450
451
0
  if (!S_ISREG(filestat.st_mode)) {
452
0
    return ISC_R_INVALIDFILE;
453
0
  }
454
455
0
  return ISC_R_SUCCESS;
456
0
}
457
458
isc_result_t
459
0
isc_file_isplainfilefd(int fd) {
460
  /*
461
   * This function returns success if filename is a plain file.
462
   */
463
0
  struct stat filestat;
464
0
  memset(&filestat, 0, sizeof(struct stat));
465
466
0
  if ((fstat(fd, &filestat)) == -1) {
467
0
    return isc__errno2result(errno);
468
0
  }
469
470
0
  if (!S_ISREG(filestat.st_mode)) {
471
0
    return ISC_R_INVALIDFILE;
472
0
  }
473
474
0
  return ISC_R_SUCCESS;
475
0
}
476
477
isc_result_t
478
0
isc_file_isdirectory(const char *filename) {
479
  /*
480
   * This function returns success if filename exists and is a
481
   * directory.
482
   */
483
0
  struct stat filestat;
484
0
  memset(&filestat, 0, sizeof(struct stat));
485
486
0
  if ((stat(filename, &filestat)) == -1) {
487
0
    return isc__errno2result(errno);
488
0
  }
489
490
0
  if (!S_ISDIR(filestat.st_mode)) {
491
0
    return ISC_R_INVALIDFILE;
492
0
  }
493
494
0
  return ISC_R_SUCCESS;
495
0
}
496
497
bool
498
0
isc_file_isabsolute(const char *filename) {
499
0
  REQUIRE(filename != NULL);
500
0
  return filename[0] == '/';
501
0
}
502
503
bool
504
0
isc_file_iscurrentdir(const char *filename) {
505
0
  REQUIRE(filename != NULL);
506
0
  return filename[0] == '.' && filename[1] == '\0';
507
0
}
508
509
bool
510
0
isc_file_ischdiridempotent(const char *filename) {
511
0
  REQUIRE(filename != NULL);
512
0
  if (isc_file_isabsolute(filename)) {
513
0
    return true;
514
0
  }
515
0
  if (isc_file_iscurrentdir(filename)) {
516
0
    return true;
517
0
  }
518
0
  return false;
519
0
}
520
521
const char *
522
0
isc_file_basename(const char *filename) {
523
0
  const char *s;
524
525
0
  REQUIRE(filename != NULL);
526
527
0
  s = strrchr(filename, '/');
528
0
  if (s == NULL) {
529
0
    return filename;
530
0
  }
531
532
0
  return s + 1;
533
0
}
534
535
void
536
0
isc_file_progname(const char *filename, char *buf, size_t buflen) {
537
0
  const char *base;
538
0
  const char *tail;
539
0
  size_t len;
540
541
0
  REQUIRE(filename != NULL);
542
0
  REQUIRE(buf != NULL);
543
544
0
  base = isc_file_basename(filename);
545
546
  /*
547
   * Libtool doesn't preserve the program name prior to final
548
   * installation.  Remove the libtool prefix ("lt-").
549
   */
550
0
  tail = isc_string_stripprefix(base, "lt-");
551
0
  if (tail != NULL) {
552
0
    base = tail;
553
0
  }
554
555
0
  len = strlen(base) + 1;
556
557
0
  RUNTIME_CHECK(len <= buflen);
558
559
0
  memmove(buf, base, len);
560
0
}
561
562
/*
563
 * Put the absolute name of the current directory into 'dirname', which is
564
 * a buffer of at least 'length' characters.  End the string with the
565
 * appropriate path separator, such that the final product could be
566
 * concatenated with a relative pathname to make a valid pathname string.
567
 */
568
static isc_result_t
569
0
dir_current(char *dirname, size_t length) {
570
0
  char *cwd;
571
0
  isc_result_t result = ISC_R_SUCCESS;
572
573
0
  REQUIRE(dirname != NULL);
574
0
  REQUIRE(length > 0U);
575
576
0
  cwd = getcwd(dirname, length);
577
578
0
  if (cwd == NULL) {
579
0
    if (errno == ERANGE) {
580
0
      result = ISC_R_NOSPACE;
581
0
    } else {
582
0
      result = isc__errno2result(errno);
583
0
    }
584
0
  } else {
585
0
    if (strlen(dirname) + 1 == length) {
586
0
      result = ISC_R_NOSPACE;
587
0
    } else if (dirname[1] != '\0') {
588
0
      strlcat(dirname, "/", length);
589
0
    }
590
0
  }
591
592
0
  return result;
593
0
}
594
595
isc_result_t
596
0
isc_file_absolutepath(const char *filename, char *path, size_t pathlen) {
597
0
  RETERR(dir_current(path, pathlen));
598
599
0
  if (strlen(path) + strlen(filename) + 1 > pathlen) {
600
0
    return ISC_R_NOSPACE;
601
0
  }
602
0
  strlcat(path, filename, pathlen);
603
604
0
  return ISC_R_SUCCESS;
605
0
}
606
607
isc_result_t
608
0
isc_file_truncate(const char *filename, off_t size) {
609
0
  isc_result_t result = ISC_R_SUCCESS;
610
611
0
  if (truncate(filename, size) < 0) {
612
0
    result = isc__errno2result(errno);
613
0
  }
614
0
  return result;
615
0
}
616
617
isc_result_t
618
0
isc_file_safecreate(const char *filename, FILE **fp) {
619
0
  isc_result_t result;
620
0
  int flags;
621
0
  struct stat sb;
622
0
  FILE *f;
623
0
  int fd;
624
625
0
  REQUIRE(filename != NULL);
626
0
  REQUIRE(fp != NULL && *fp == NULL);
627
628
0
  result = file_lstats(filename, &sb);
629
0
  if (result == ISC_R_SUCCESS) {
630
0
    if (!S_ISREG(sb.st_mode)) {
631
0
      return ISC_R_INVALIDFILE;
632
0
    }
633
0
    flags = O_WRONLY | O_TRUNC | O_NOFOLLOW;
634
0
  } else if (result == ISC_R_FILENOTFOUND) {
635
0
    flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
636
0
  } else {
637
0
    return result;
638
0
  }
639
640
0
  fd = open(filename, flags, S_IRUSR | S_IWUSR);
641
0
  if (fd == -1) {
642
0
    return isc__errno2result(errno);
643
0
  }
644
645
0
  f = fdopen(fd, "w");
646
0
  if (f == NULL) {
647
0
    result = isc__errno2result(errno);
648
0
    close(fd);
649
0
    return result;
650
0
  }
651
652
0
  *fp = f;
653
0
  return ISC_R_SUCCESS;
654
0
}
655
656
isc_result_t
657
isc_file_splitpath(isc_mem_t *mctx, const char *path, char **dirname,
658
0
       char const **bname) {
659
0
  char *dir = NULL;
660
0
  const char *file = NULL, *slash = NULL;
661
662
0
  if (path == NULL) {
663
0
    return ISC_R_INVALIDFILE;
664
0
  }
665
666
0
  slash = strrchr(path, '/');
667
668
0
  if (slash == path) {
669
0
    file = ++slash;
670
0
    dir = isc_mem_strdup(mctx, "/");
671
0
  } else if (slash != NULL) {
672
0
    file = ++slash;
673
0
    dir = isc_mem_allocate(mctx, slash - path);
674
0
    strlcpy(dir, path, slash - path);
675
0
  } else {
676
0
    file = path;
677
0
    dir = isc_mem_strdup(mctx, ".");
678
0
  }
679
680
0
  if (*file == '\0') {
681
0
    isc_mem_free(mctx, dir);
682
0
    return ISC_R_INVALIDFILE;
683
0
  }
684
685
0
  *dirname = dir;
686
0
  *bname = file;
687
688
0
  return ISC_R_SUCCESS;
689
0
}
690
691
2
#define DISALLOW "\\/ABCDEFGHIJKLMNOPQRSTUVWXYZ"
692
693
static isc_result_t
694
path_join(isc_buffer_t *b, const char *dir, const char *base, const char *ext,
695
6
    size_t length) {
696
6
  isc_buffer_clear(b);
697
698
6
  if (dir != NULL) {
699
0
    isc_buffer_putstr(b, dir);
700
0
    isc_buffer_putuint8(b, '/');
701
0
  }
702
703
6
  isc_buffer_putstr(b, base);
704
705
6
  if (ext != NULL) {
706
6
    isc_buffer_putuint8(b, '.');
707
6
    isc_buffer_putstr(b, ext);
708
6
  }
709
710
6
  isc_buffer_putuint8(b, '\0');
711
712
6
  if (isc_buffer_usedlength(b) > length) {
713
0
    return ISC_R_NOSPACE;
714
0
  }
715
716
6
  return ISC_R_SUCCESS;
717
6
}
718
719
isc_result_t
720
isc_file_sanitize(const char *dir, const char *base, const char *ext,
721
2
      char *path, size_t length) {
722
2
  REQUIRE(base != NULL);
723
2
  REQUIRE(path != NULL);
724
725
2
  isc_result_t result = ISC_R_SUCCESS;
726
2
  char *buf = NULL;
727
2
  unsigned char digest[ISC_MAX_MD_SIZE];
728
2
  char hash[ISC_MAX_MD_SIZE * 2 + 1];
729
2
  isc_buffer_t b, h;
730
2
  isc_region_t r = { .base = digest };
731
732
  /* Calculate the full-length SHA256 hash first */
733
2
  isc_buffer_init(&h, hash, sizeof(hash));
734
2
  RETERR(isc_md(ISC_MD_SHA256, (const unsigned char *)base, strlen(base),
735
2
          r.base, &r.length));
736
2
  RETERR(isc_hex_totextlower(&r, 0, "", &h));
737
2
  INSIST(isc_buffer_availablelength(&h) > 0);
738
2
  isc_buffer_putuint8(&h, '\0');
739
740
  /* Check whether the full-length SHA256 hash filename exists */
741
2
  isc_buffer_initnull(&b);
742
2
  isc_buffer_setmctx(&b, isc_g_mctx);
743
744
2
  CHECK(path_join(&b, dir, hash, ext, length));
745
746
2
  buf = isc_buffer_base(&b);
747
2
  if (isc_file_exists(buf)) {
748
0
    strlcpy(path, buf, length);
749
0
    goto cleanup;
750
0
  }
751
752
  /* Check for a truncated SHA256 hash filename */
753
2
  hash[16] = '\0';
754
2
  CHECK(path_join(&b, dir, hash, ext, length));
755
756
2
  buf = isc_buffer_base(&b);
757
2
  if (isc_file_exists(buf)) {
758
0
    strlcpy(path, buf, length);
759
0
    goto cleanup;
760
0
  }
761
762
  /*
763
   * If neither hash filename already exists, then we'll use
764
   * the original base name if it has no disallowed characters,
765
   * or the truncated hash name if it does.
766
   */
767
2
  if (strpbrk(base, DISALLOW) != NULL) {
768
0
    strlcpy(path, buf, length);
769
0
    goto cleanup;
770
0
  }
771
772
2
  CHECK(path_join(&b, dir, base, ext, length));
773
774
2
  buf = isc_buffer_base(&b);
775
2
  strlcpy(path, buf, length);
776
777
2
cleanup:
778
2
  isc_buffer_clearmctx(&b);
779
2
  return result;
780
2
}
781
782
bool
783
0
isc_file_isdirwritable(const char *path) {
784
0
  return access(path, W_OK | X_OK) == 0;
785
0
}