Coverage Report

Created: 2025-08-03 06:32

/src/bind9/lib/dns/ttl.c
Line
Count
Source (jump to first uncovered line)
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
/*! \file */
15
16
#include <ctype.h>
17
#include <errno.h>
18
#include <inttypes.h>
19
#include <stdbool.h>
20
#include <stdio.h>
21
#include <stdlib.h>
22
23
#include <isc/ascii.h>
24
#include <isc/buffer.h>
25
#include <isc/parseint.h>
26
#include <isc/region.h>
27
#include <isc/result.h>
28
#include <isc/string.h>
29
#include <isc/util.h>
30
31
#include <dns/ttl.h>
32
33
#define RETERR(x)                        \
34
0
  do {                             \
35
0
    isc_result_t _r = (x);   \
36
0
    if (_r != ISC_R_SUCCESS) \
37
0
      return ((_r));   \
38
0
  } while (0)
39
40
static isc_result_t
41
bind_ttl(isc_textregion_t *source, uint32_t *ttl);
42
43
/*
44
 * Helper for dns_ttl_totext().
45
 */
46
static isc_result_t
47
ttlfmt(unsigned int t, const char *s, bool verbose, bool space,
48
0
       isc_buffer_t *target) {
49
0
  char tmp[60];
50
0
  unsigned int len;
51
0
  isc_region_t region;
52
53
0
  if (verbose) {
54
0
    len = snprintf(tmp, sizeof(tmp), "%s%u %s%s", space ? " " : "",
55
0
             t, s, t == 1 ? "" : "s");
56
0
  } else {
57
0
    len = snprintf(tmp, sizeof(tmp), "%u%c", t, s[0]);
58
0
  }
59
60
0
  INSIST(len + 1 <= sizeof(tmp));
61
0
  isc_buffer_availableregion(target, &region);
62
0
  if (len > region.length) {
63
0
    return ISC_R_NOSPACE;
64
0
  }
65
0
  memmove(region.base, tmp, len);
66
0
  isc_buffer_add(target, len);
67
68
0
  return ISC_R_SUCCESS;
69
0
}
70
71
/*
72
 * Derived from bind8 ns_format_ttl().
73
 */
74
isc_result_t
75
0
dns_ttl_totext(uint32_t src, bool verbose, bool upcase, isc_buffer_t *target) {
76
0
  unsigned int secs, mins, hours, days, weeks, x;
77
78
0
  secs = src % 60;
79
0
  src /= 60;
80
0
  mins = src % 60;
81
0
  src /= 60;
82
0
  hours = src % 24;
83
0
  src /= 24;
84
0
  days = src % 7;
85
0
  src /= 7;
86
0
  weeks = src;
87
0
  src = 0;
88
0
  POST(src);
89
90
0
  x = 0;
91
0
  if (weeks != 0) {
92
0
    RETERR(ttlfmt(weeks, "week", verbose, x > 0, target));
93
0
    x++;
94
0
  }
95
0
  if (days != 0) {
96
0
    RETERR(ttlfmt(days, "day", verbose, x > 0, target));
97
0
    x++;
98
0
  }
99
0
  if (hours != 0) {
100
0
    RETERR(ttlfmt(hours, "hour", verbose, x > 0, target));
101
0
    x++;
102
0
  }
103
0
  if (mins != 0) {
104
0
    RETERR(ttlfmt(mins, "minute", verbose, x > 0, target));
105
0
    x++;
106
0
  }
107
0
  if (secs != 0 || (weeks == 0 && days == 0 && hours == 0 && mins == 0)) {
108
0
    RETERR(ttlfmt(secs, "second", verbose, x > 0, target));
109
0
    x++;
110
0
  }
111
0
  INSIST(x > 0);
112
  /*
113
   * If only a single unit letter is printed, print it
114
   * in upper case. (Why?  Because BIND 8 does that.
115
   * Presumably it has a reason.)
116
   */
117
0
  if (x == 1 && upcase && !verbose) {
118
0
    isc_region_t region;
119
    /*
120
     * The unit letter is the last character in the
121
     * used region of the buffer.
122
     */
123
0
    isc_buffer_usedregion(target, &region);
124
0
    region.base[region.length - 1] =
125
0
      isc_ascii_toupper(region.base[region.length - 1]);
126
0
  }
127
0
  return ISC_R_SUCCESS;
128
0
}
129
130
isc_result_t
131
0
dns_counter_fromtext(isc_textregion_t *source, uint32_t *ttl) {
132
0
  return bind_ttl(source, ttl);
133
0
}
134
135
isc_result_t
136
0
dns_ttl_fromtext(isc_textregion_t *source, uint32_t *ttl) {
137
0
  isc_result_t result;
138
139
0
  result = bind_ttl(source, ttl);
140
0
  if (result != ISC_R_SUCCESS && result != ISC_R_RANGE) {
141
0
    result = DNS_R_BADTTL;
142
0
  }
143
0
  return result;
144
0
}
145
146
static isc_result_t
147
0
bind_ttl(isc_textregion_t *source, uint32_t *ttl) {
148
0
  uint64_t tmp = 0ULL;
149
0
  uint32_t n;
150
0
  char *s;
151
0
  char buf[64];
152
0
  char nbuf[64]; /* Number buffer */
153
154
  /*
155
   * Copy the buffer as it may not be NULL terminated.
156
   * No legal counter / ttl is longer that 63 characters.
157
   */
158
0
  if (source->length > sizeof(buf) - 1) {
159
0
    return DNS_R_SYNTAX;
160
0
  }
161
  /* Copy source->length bytes and NUL terminate. */
162
0
  snprintf(buf, sizeof(buf), "%.*s", (int)source->length, source->base);
163
0
  s = buf;
164
165
0
  do {
166
0
    isc_result_t result;
167
168
0
    char *np = nbuf;
169
0
    while (*s != '\0' && isdigit((unsigned char)*s)) {
170
0
      *np++ = *s++;
171
0
    }
172
0
    *np++ = '\0';
173
0
    INSIST(np - nbuf <= (int)sizeof(nbuf));
174
0
    result = isc_parse_uint32(&n, nbuf, 10);
175
0
    if (result != ISC_R_SUCCESS) {
176
0
      return DNS_R_SYNTAX;
177
0
    }
178
0
    switch (*s) {
179
0
    case 'w':
180
0
    case 'W':
181
0
      tmp += (uint64_t)n * 7 * 24 * 3600;
182
0
      s++;
183
0
      break;
184
0
    case 'd':
185
0
    case 'D':
186
0
      tmp += (uint64_t)n * 24 * 3600;
187
0
      s++;
188
0
      break;
189
0
    case 'h':
190
0
    case 'H':
191
0
      tmp += (uint64_t)n * 3600;
192
0
      s++;
193
0
      break;
194
0
    case 'm':
195
0
    case 'M':
196
0
      tmp += (uint64_t)n * 60;
197
0
      s++;
198
0
      break;
199
0
    case 's':
200
0
    case 'S':
201
0
      tmp += (uint64_t)n;
202
0
      s++;
203
0
      break;
204
0
    case '\0':
205
      /* Plain number? */
206
0
      if (tmp != 0ULL) {
207
0
        return DNS_R_SYNTAX;
208
0
      }
209
0
      tmp = n;
210
0
      break;
211
0
    default:
212
0
      return DNS_R_SYNTAX;
213
0
    }
214
0
  } while (*s != '\0');
215
216
0
  if (tmp > 0xffffffffULL) {
217
0
    return ISC_R_RANGE;
218
0
  }
219
220
0
  *ttl = (uint32_t)(tmp & 0xffffffffUL);
221
0
  return ISC_R_SUCCESS;
222
0
}