summaryrefslogtreecommitdiff
path: root/src/rexmpp_dns.c
blob: 5c3f4f6e750882d1ca1478d8c2161846c7dff27e (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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/**
   @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>
#elif defined(USE_CARES)
#include <ares.h>
#else
#endif
#include <netdb.h>


#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;
  }
  free(result);
}

rexmpp_dns_result_t *result_from_hostent (struct hostent *hostinfo) {
  rexmpp_dns_result_t *r = malloc(sizeof(rexmpp_dns_result_t));
  r->secure = 0;
  int i, size = 0;
  while (hostinfo->h_addr_list[size] != NULL) {
    size++;
  }
  r->data = malloc(sizeof(void *) * (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;
  return r;
}


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;
#elif defined(USE_CARES)
  int err = ares_library_init(ARES_LIB_INIT_ALL);
  if (err != 0) {
    rexmpp_log(s, LOG_CRIT, "ares library initialisation error: %s",
               ares_strerror(err));
    return 1;
  }
  err = ares_init(&(s->resolver.channel));
  if (err) {
    rexmpp_log(s, LOG_CRIT, "ares channel initialisation error: %s",
               ares_strerror(err));
    ares_library_cleanup();
    return 1;
  }
  return 0;
#else
  (void)s;
  return 0;
#endif
}

void rexmpp_dns_ctx_cleanup (rexmpp_t *s) {
  (void)s;
  return;
}

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;
  }
#elif defined(USE_CARES)
  ares_destroy(s->resolver.channel);
  ares_library_cleanup();
#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;
#elif defined(USE_CARES)
  return ares_fds(s->resolver.channel, read_fds, write_fds);
#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;
#elif defined(USE_CARES)
  return ares_timeout(s->resolver.channel, max_tv, 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, "DNS query failure: %s",
               ub_strerror(err));
    ub_resolve_free(result);
    d->cb(s, d->ptr, NULL);
    free(d);
    return;
  }

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

  if (! result->havedata) {
    rexmpp_log(s, LOG_DEBUG, "No data in the query result");
    ub_resolve_free(result);
    d->cb(s, d->ptr, NULL);
    free(d);
    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(void *) * (size + 1));
  res->len = malloc(sizeof(int) * size);
  for (i = 0; i < size; i++) {
    if (result->qtype == 33) {
      /* SRV */
      res->len[i] = sizeof(rexmpp_dns_srv_t);
      res->data[i] = malloc(res->len[i]);
      int err = rexmpp_parse_srv(result->data[i], result->len[i],
                                 (rexmpp_dns_srv_t*)res->data[i]);
      if (err) {
        rexmpp_log(s, LOG_WARNING, "Failed to parse an SRV record");
        res->data[i + 1] = NULL;
        rexmpp_dns_result_free(res);
        d->cb(s, d->ptr, NULL);
        free(d);
        return;
      }
    } else {
      /* Non-SRV, for now that's just A or AAAA */
      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;
  ub_resolve_free(result);
  d->cb(s, d->ptr, res);
  free(d);
}
#elif defined(USE_CARES)
void rexmpp_dns_cb (void *ptr,
                    int err,
                    int timeouts,
                    unsigned char *abuf,
                    int alen)
{
  (void)timeouts;
  struct rexmpp_dns_query_cb_data *d = ptr;
  rexmpp_t *s = d->s;
  if (err != ARES_SUCCESS) {
    rexmpp_log(s, LOG_WARNING, "A DNS query failure: %s",
               ares_strerror(err));
    d->cb(s, d->ptr, NULL);
    free(d);
    return;
  }
  /* c-ares won't just tell us the type, but it does check for it in
     the parsing functions, so we just try them out. */
  struct hostent *hostinfo;
  struct ares_srv_reply *srv, *cur_srv;
  if (ares_parse_a_reply(abuf, alen, &hostinfo, NULL, NULL) == ARES_SUCCESS ||
      ares_parse_aaaa_reply(abuf, alen, &hostinfo, NULL, NULL) == ARES_SUCCESS) {
    rexmpp_dns_result_t *r = result_from_hostent(hostinfo);
    ares_free_hostent(hostinfo);
    d->cb(s, d->ptr, r);
  } else if (ares_parse_srv_reply(abuf, alen, &srv) == ARES_SUCCESS) {
    int i, size;
    for (size = 0, cur_srv = srv; cur_srv != NULL; size++, cur_srv = cur_srv->next);
    rexmpp_dns_result_t *r = malloc(sizeof(rexmpp_dns_result_t));
    r->secure = 0;
    r->data = malloc(sizeof(void*) * (size + 1));
    r->len = malloc(sizeof(int) * size);
    for (cur_srv = srv, i = 0; i < size; i++, cur_srv = cur_srv->next) {
      r->len[i] = sizeof(rexmpp_dns_srv_t);
      rexmpp_dns_srv_t *r_srv = malloc(sizeof(rexmpp_dns_srv_t));
      r_srv->priority = cur_srv->priority;
      r_srv->weight = cur_srv->weight;
      r_srv->port = cur_srv->port;
      strncpy(r_srv->target, cur_srv->host, 255);
      r_srv->target[255] = '\0';
      r->data[i] = r_srv;
    }
    r->data[size] = NULL;
    ares_free_data(srv);
    d->cb(s, d->ptr, r);
  } else {
    rexmpp_log(s, LOG_ERR, "Failed to parse a query");
    d->cb(s, d->ptr, NULL);
  }
  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;
  }
#elif defined(USE_CARES)
  struct rexmpp_dns_query_cb_data *d =
    malloc(sizeof(struct rexmpp_dns_query_cb_data));
  d->s = s;
  d->cb = callback;
  d->ptr = ptr;
  ares_query(s->resolver.channel, query, rrclass, rrtype, rexmpp_dns_cb, d);
#else
  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 {
        rexmpp_dns_result_t *r = result_from_hostent(hostinfo);
        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;
#elif defined(USE_CARES)
  ares_process(s->resolver.channel, read_fds, write_fds);
  return 0;
#else
  (void)s;
  (void)read_fds;
  (void)write_fds;
  return 0;
#endif
}