summaryrefslogtreecommitdiff
path: root/src/rexmpp_dns.c
blob: 5b3fd9ed62ad4907f24e2363ce096b1560e3d0ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/**
   @file rexmpp_dns.c
   @brief DNS helper functions
   @author defanor <defanor@uberspace.net>
   @date 2020
   @copyright MIT license.
*/

#include <memory.h>
#include <syslog.h>

#include "config.h"

#if defined(USE_UNBOUND)
#include <unbound.h>
#else
#include <netdb.h>
#endif

#include "rexmpp.h"
#include "rexmpp_dns.h"


struct rexmpp_dns_query_cb_data {
  rexmpp_t *s;
  dns_query_cb_t cb;
  void *ptr;
};



/* https://tools.ietf.org/html/rfc1035#section-3.1 */

int rexmpp_dns_parse_qname (char *in, int in_len, char *out, int out_len) {
  int i = 0;
  while (i < in_len && in[i]) {
    if (i + in[i] < in_len && i + in[i] < out_len) {
      memcpy(out + i, in + i + 1, in[i]);
      i += in[i];
      out[i] = '.';
      i++;
      out[i] = '\0';
    } else {
      return -1;
    }
  }
  return i;
}

int rexmpp_parse_srv (char *in, int in_len, struct rexmpp_dns_srv *out) {
  if (in_len < 7 || in_len > 255 + 6) {
    return -1;
  }
  out->priority = in[0] * 0x100 + in[1];
  out->weight = in[2] * 0x100 + in[3];
  out->port = in[4] * 0x100 + in[5];
  if (rexmpp_dns_parse_qname(in + 6, in_len - 6, out->target, 255) < 0) {
    return -1;
  }
  return 0;
}


void rexmpp_dns_result_free (rexmpp_dns_result_t *result) {
  if (result->data != NULL) {
    int i;
    for (i = 0; result->data[i] != NULL; i++) {
      free(result->data[i]);
    }
    free(result->data);
    result->data = NULL;
  }
  if (result->len != NULL) {
    free(result->len);
    result->len = NULL;
  }
  if (result->qname != NULL) {
    free(result->qname);
    result->qname = NULL;
  }
  free(result);
}


int rexmpp_dns_ctx_init (rexmpp_t *s) {
#if defined(USE_UNBOUND)
  int err;
  s->resolver.ctx = ub_ctx_create();
  if (s->resolver.ctx == NULL) {
    rexmpp_log(s, LOG_CRIT, "Failed to create resolver context");
    return 1;
  }
  err = ub_ctx_resolvconf(s->resolver.ctx, NULL);
  if (err != 0) {
    rexmpp_log(s, LOG_WARNING, "Failed to read resolv.conf: %s",
               ub_strerror(err));
  }
  err = ub_ctx_hosts(s->resolver.ctx, NULL);
  if (err != 0) {
    rexmpp_log(s, LOG_WARNING, "Failed to read hosts file: %s",
               ub_strerror(err));
  }
  err = ub_ctx_add_ta_file(s->resolver.ctx, DNSSEC_TRUST_ANCHOR_FILE);
  if (err != 0) {
    rexmpp_log(s, LOG_WARNING, "Failed to set root key file for DNSSEC: %s",
               ub_strerror(err));
  }
  return 0;
#else
  (void)s;
  return 0;
#endif
}

void rexmpp_dns_ctx_cleanup (rexmpp_t *s) {
#if defined(USE_UNBOUND)
  (void)s;
  return;
#else
  (void)s;
  return;
#endif
}

void rexmpp_dns_ctx_deinit (rexmpp_t *s) {
#if defined(USE_UNBOUND)
  if (s->resolver.ctx != NULL) {
    ub_ctx_delete(s->resolver.ctx);
    s->resolver.ctx = NULL;
  }
#else
  (void)s;
#endif
}

int rexmpp_dns_fds (rexmpp_t *s, fd_set *read_fds, fd_set *write_fds) {
#if defined(USE_UNBOUND)
  (void)write_fds;
  int max_fd = ub_fd(s->resolver.ctx) + 1;
  if (max_fd != 0) {
    FD_SET(max_fd - 1, read_fds);
  }
  return max_fd;
#else
  (void)s;
  (void)read_fds;
  (void)write_fds;
  return 0;
#endif
}

struct timeval * rexmpp_dns_timeout (rexmpp_t *s,
                                     struct timeval *max_tv,
                                     struct timeval *tv)
{
#if defined(USE_UNBOUND)
  (void)s;
  (void)tv;
  return max_tv;
#else
  (void)s;
  (void)max_tv;
  (void)tv;
  return max_tv;
#endif
}

#if defined(USE_UNBOUND)
void rexmpp_dns_cb (void *ptr,
                    int err,
                    struct ub_result *result)
{
  struct rexmpp_dns_query_cb_data *d = ptr;
  rexmpp_t *s = d->s;

  if (err != 0) {
    rexmpp_log(s, LOG_WARNING, "%s DNS query failure: %s",
               result->qname, ub_strerror(err));
    ub_resolve_free(result);
    d->cb(s, d->ptr, NULL);
    return;
  }

  if (result->bogus) {
    rexmpp_log(s, LOG_WARNING,
               "Received a bogus DNS resolution result for %s",
               result->qname);
    ub_resolve_free(result);
    d->cb(s, d->ptr, NULL);
    return;
  }

  if (! result->havedata) {
    rexmpp_log(s, LOG_DEBUG, "No data in the %s query result", result->qname);
    ub_resolve_free(result);
    d->cb(s, d->ptr, NULL);
    return;
  }

  int i, size = 0;
  while (result->data[size] != NULL) {
    size++;
  }
  rexmpp_dns_result_t *res = malloc(sizeof(rexmpp_dns_result_t));
  res->data = malloc(sizeof(char *) * (size + 1));
  res->len = malloc(sizeof(int) * size);
  for (i = 0; i < size; i++) {
    res->len[i] = result->len[i];
    res->data[i] = malloc(res->len[i]);
    memcpy(res->data[i], result->data[i], res->len[i]);
  }
  res->data[size] = NULL;
  res->secure = result->secure;
  res->qname = strdup(result->qname);
  ub_resolve_free(result);
  d->cb(s, d->ptr, res);
  free(d);
}
#endif


int rexmpp_dns_resolve (rexmpp_t *s,
                        const char *query,
                        int rrtype,
                        int rrclass,
                        void* ptr,
                        dns_query_cb_t callback)
{
#if defined(USE_UNBOUND)
  struct rexmpp_dns_query_cb_data *d =
    malloc(sizeof(struct rexmpp_dns_query_cb_data));
  d->s = s;
  d->cb = callback;
  d->ptr = ptr;
  int err = ub_resolve_async(s->resolver.ctx, query, rrtype, rrclass,
                             d, rexmpp_dns_cb, NULL);
  if (err) {
    rexmpp_log(s, LOG_ERR, "Failed to query %s: %s",
               query, ub_strerror(err));
    return 1;
  }
#else
  rexmpp_dns_result_t *r = malloc(sizeof(rexmpp_dns_result_t));;
  if (rrclass == 1) {
    if (rrtype == 1 || rrtype == 28) {
      struct hostent *hostinfo = gethostbyname(query);
      if (hostinfo == NULL) {
        rexmpp_log(s, LOG_ERR, "Failed to lookup %s", query);
        callback(s, ptr, NULL);
      } else {
        r->qname = strdup(query);
        r->secure = 0;
        int i, size = 0;
        while (hostinfo->h_addr_list[size] != NULL) {
          size++;
        }
        r->data = malloc(sizeof(char *) * (size + 1));
        r->len = malloc(sizeof(int) * size);
        for (i = 0; i < size; i++) {
          r->len[i] = hostinfo->h_length;
          r->data[i] = malloc(r->len[i]);
          memcpy(r->data[i], hostinfo->h_addr_list[i], hostinfo->h_length);
        }
        r->data[size] = NULL;
        callback(s, ptr, r);
      }
    } else if (rrtype == 33) {
      rexmpp_log(s, LOG_WARNING, "rexmpp is built without SRV lookup support");
      callback(s, ptr, NULL);
    } else {
      rexmpp_log(s, LOG_ERR, "A DNS lookup of unrecognized type is requested");
      callback(s, ptr, NULL);
      return -1;
    }
  } else {
    rexmpp_log(s, LOG_ERR, "A DNS lookup of unrecognized class is requested");
    callback(s, ptr, NULL);
    return -1;
  }
#endif
  return 0;
}

int rexmpp_dns_process (rexmpp_t *s, fd_set *read_fds, fd_set *write_fds) {
#if defined(USE_UNBOUND)
  (void)read_fds;
  (void)write_fds;
  if (ub_poll(s->resolver.ctx)) {
    int err = ub_process(s->resolver.ctx);
    if (err != 0) {
      rexmpp_log(s, LOG_ERR, "DNS query processing error: %s",
                 ub_strerror(err));
      return 1;
    }
  }
  return 0;
#else
  (void)s;
  (void)read_fds;
  (void)write_fds;
  return 0;
#endif
}