File: host-resolve.c

package info (click to toggle)
syslog-ng 3.8.1-10
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 47,320 kB
  • ctags: 43,937
  • sloc: ansic: 159,432; yacc: 25,059; sh: 13,574; makefile: 4,669; python: 3,468; java: 3,218; xml: 2,309; perl: 318; lex: 316; awk: 184
file content (392 lines) | stat: -rw-r--r-- 11,142 bytes parent folder | download
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
/*
 * Copyright (c) 2002-2013 Balabit
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * As an additional exemption you are allowed to compile & link against the
 * OpenSSL libraries as published by the OpenSSL project. See the file
 * COPYING for details.
 *
 */
#include "host-resolve.h"
#include "hostname.h"
#include "dnscache.h"
#include "messages.h"
#include "cfg.h"
#include "tls-support.h"
#include "compat/socket.h"

#include <arpa/inet.h>
#include <netdb.h>
#include <string.h>

#if !defined(SYSLOG_NG_HAVE_GETADDRINFO) || !defined(SYSLOG_NG_HAVE_GETNAMEINFO)
G_LOCK_DEFINE_STATIC(resolv_lock);
#endif

TLS_BLOCK_START
{
  gchar hostname_buffer[256];
}
TLS_BLOCK_END;

#define hostname_buffer  __tls_deref(hostname_buffer)

static void
normalize_hostname(gchar *result, gsize result_size, const gchar *hostname)
{
  gsize i;

  for (i = 0; hostname[i] && i < (result_size - 1); i++)
    {
      result[i] = g_ascii_tolower(hostname[i]);
    }
  result[i] = '\0'; /* the closing \0 is not copied by the previous loop */
}

static const gchar *
bounce_to_hostname_buffer(const gchar *hname)
{
  if (hname != hostname_buffer)
    g_strlcpy(hostname_buffer, hname, sizeof(hostname_buffer));
  return hostname_buffer;
}

static const gchar *
hostname_apply_options(gssize result_len_orig, gsize *result_len, const gchar *hname, const HostResolveOptions *host_resolve_options)
{
  if (host_resolve_options->normalize_hostnames)
    {
      normalize_hostname(hostname_buffer, sizeof(hostname_buffer), hname);
      hname = hostname_buffer;
    }
  if (result_len_orig >= 0)
    *result_len = result_len_orig;
  else
    *result_len = strlen(hname);
  return hname;

}

static const gchar *
hostname_apply_options_fqdn(gssize result_len_orig, gsize *result_len, const gchar *hname, gboolean positive, const HostResolveOptions *host_resolve_options)
{
  if (positive && !host_resolve_options->use_fqdn)
    {
      /* we only truncate hostnames if they were positive
       * matches (e.g. real hostnames and not IP
       * addresses) */

      hname = bounce_to_hostname_buffer(hname);
      convert_hostname_to_short_hostname(hostname_buffer, sizeof(hostname_buffer));
      result_len_orig = -1;
    }
  return hostname_apply_options(result_len_orig, result_len, hname, host_resolve_options);
}

/****************************************************************************
 * Convert a GSockAddr instance to a hostname
 ****************************************************************************/

static gboolean
is_wildcard_hostname(const gchar *name)
{
  return !name || name[0] == 0;
}

static gboolean
resolve_wildcard_hostname_to_sockaddr(GSockAddr **addr, gint family, const gchar *name)
{
  struct sockaddr_storage ss;

  /* return the wildcard address that can be used as a bind address */
  memset(&ss, 0, sizeof(ss));
  ss.ss_family = family;
  switch (family)
    {
    case AF_INET:
      *addr = g_sockaddr_inet_new2(((struct sockaddr_in *) &ss));
      break;
#if SYSLOG_NG_ENABLE_IPV6
    case AF_INET6:
      *addr = g_sockaddr_inet6_new2((struct sockaddr_in6 *) &ss);
      break;
#endif
    default:
      g_assert_not_reached();
      break;
    }
  return TRUE;
}

#ifdef SYSLOG_NG_HAVE_GETADDRINFO
static gboolean
resolve_hostname_to_sockaddr_using_getaddrinfo(GSockAddr **addr, gint family, const gchar *name)
{
  struct addrinfo hints;
  struct addrinfo *res;

  memset(&hints, 0, sizeof(hints));
  hints.ai_family = family;
  hints.ai_socktype = 0;
  hints.ai_protocol = 0;

  if (getaddrinfo(name, NULL, &hints, &res) == 0)
    {
      /* we only use the first entry in the returned list */
      switch (family)
        {
        case AF_INET:
          *addr = g_sockaddr_inet_new2(((struct sockaddr_in *) res->ai_addr));
          break;
#if SYSLOG_NG_ENABLE_IPV6
        case AF_INET6:
          *addr = g_sockaddr_inet6_new2((struct sockaddr_in6 *) res->ai_addr);
          break;
#endif
        default:
          g_assert_not_reached();
          break;
        }
      freeaddrinfo(res);
      return TRUE;
    }
  return FALSE;
}

#else

static gboolean
resolve_hostname_to_sockaddr_using_gethostbyname(GSockAddr **addr, gint family, const gchar *name)
{
  struct hostent *he;

  G_LOCK(resolv_lock);
  he = gethostbyname(name);
  if (he)
    {
      switch (family)
        {
        case AF_INET:
          {
            struct sockaddr_in sin;

            sin.sin_family = AF_INET;
            sin.sin_addr = *(struct in_addr *) he->h_addr;
            sin.sin_port = htons(0);
            *addr = g_sockaddr_inet_new2(&sin);
            break;
          }
        default:
          g_assert_not_reached();
          break;
        }
    }
  G_UNLOCK(resolv_lock);
  return he != NULL;
}
#endif

gboolean
resolve_hostname_to_sockaddr(GSockAddr **addr, gint family, const gchar *name)
{
  gboolean result;

  if (is_wildcard_hostname(name))
    return resolve_wildcard_hostname_to_sockaddr(addr, family, name);

#ifdef SYSLOG_NG_HAVE_GETADDRINFO
  result = resolve_hostname_to_sockaddr_using_getaddrinfo(addr, family, name);
#else
  result = resolve_hostname_to_sockaddr_using_gethostbyname(addr, family, name);
#endif
  if (!result)
    {
      msg_error("Error resolving hostname",
                evt_tag_str("host", name));
    }
  return result;
}

/****************************************************************************
 * Convert a hostname to a GSockAddr instance
 ****************************************************************************/

static gboolean
is_sockaddr_local(GSockAddr *saddr)
{
  return !saddr || (saddr->sa.sa_family != AF_INET && saddr->sa.sa_family != AF_INET6);
}

static const gchar *
resolve_sockaddr_to_local_hostname(gsize *result_len, GSockAddr *saddr, const HostResolveOptions *host_resolve_options)
{
  const gchar *hname;

  if (host_resolve_options->use_fqdn)
    hname = get_local_hostname_fqdn();
  else
    hname = get_local_hostname_short();

  return hostname_apply_options(-1, result_len, hname, host_resolve_options);
}

#ifdef SYSLOG_NG_HAVE_GETNAMEINFO

static const gchar *
resolve_address_using_getnameinfo(GSockAddr *saddr, gchar *buf, gsize buf_len)
{
  if (getnameinfo(&saddr->sa, saddr->salen, buf, buf_len, NULL, 0, NI_NAMEREQD) == 0)
    return buf;
  return NULL;
}

#else

static const gchar *
resolve_address_using_gethostbyaddr(GSockAddr *saddr, gchar *buf, gsize buf_len)
{
  const gchar *result = NULL;
  struct hostent *hp;
  void *addr;
  socklen_t addr_len G_GNUC_UNUSED;

  g_assert(saddr->sa.sa_family == AF_INET);

  addr = &((struct sockaddr_in *) &saddr->sa)->sin_addr;
  addr_len = sizeof(struct in_addr);

  G_LOCK(resolv_lock);
  hp = gethostbyaddr(addr, addr_len, saddr->sa.sa_family);
  if (hp && hp->h_name)
    {
      strncpy(buf, hp->h_name, buf_len);
      buf[buf_len - 1] = 0;
      result = buf;
    }

  G_UNLOCK(resolv_lock);
  return result;
}

#endif

static void *
sockaddr_to_dnscache_key(GSockAddr *saddr)
{
  if (saddr->sa.sa_family == AF_INET)
    return &((struct sockaddr_in *) &saddr->sa)->sin_addr;
#if SYSLOG_NG_ENABLE_IPV6
  else
    return &((struct sockaddr_in6 *) &saddr->sa)->sin6_addr;
#endif
}

static const gchar *
resolve_sockaddr_to_inet_or_inet6_hostname(gsize *result_len, GSockAddr *saddr, const HostResolveOptions *host_resolve_options)
{
  const gchar *hname;
  gsize hname_len;
  gboolean positive;
  void *dnscache_key;

  dnscache_key = sockaddr_to_dnscache_key(saddr);

  hname = NULL;
  positive = FALSE;

  if (host_resolve_options->use_dns_cache)
    {
      if (dns_caching_lookup(saddr->sa.sa_family, dnscache_key, (const gchar **) &hname, &hname_len, &positive))
        return hostname_apply_options_fqdn(hname_len, result_len, hname, positive, host_resolve_options);
    }

  if (!hname && host_resolve_options->use_dns && host_resolve_options->use_dns != 2)
    {
#ifdef SYSLOG_NG_HAVE_GETNAMEINFO
      hname = resolve_address_using_getnameinfo(saddr, hostname_buffer, sizeof(hostname_buffer));
#else
      hname = resolve_address_using_gethostbyaddr(saddr, hostname_buffer, sizeof(hostname_buffer));
#endif
      positive = (hname != NULL);
    }

  if (!hname)
    {
      hname = g_sockaddr_format(saddr, hostname_buffer, sizeof(hostname_buffer), GSA_ADDRESS_ONLY);
      positive = FALSE;
    }
  if (host_resolve_options->use_dns_cache)
    dns_caching_store(saddr->sa.sa_family, dnscache_key, hname, positive);

  return hostname_apply_options_fqdn(-1, result_len, hname, positive, host_resolve_options);
}

const gchar *
resolve_sockaddr_to_hostname(gsize *result_len, GSockAddr *saddr, const HostResolveOptions *host_resolve_options)
{
  if (is_sockaddr_local(saddr))
    return resolve_sockaddr_to_local_hostname(result_len, saddr, host_resolve_options);
  else
    return resolve_sockaddr_to_inet_or_inet6_hostname(result_len, saddr, host_resolve_options);
}

/****************************************************************************
 * Convert a hostname to a hostname with options applied.
 ****************************************************************************/

const gchar *
resolve_hostname_to_hostname(gsize *result_len, const gchar *hname, HostResolveOptions *host_resolve_options)
{
  hname = bounce_to_hostname_buffer(hname);

  if (host_resolve_options->use_fqdn)
    convert_hostname_to_fqdn(hostname_buffer, sizeof(hostname_buffer));
  else
    convert_hostname_to_short_hostname(hostname_buffer, sizeof(hostname_buffer));

  return hostname_apply_options(-1, result_len, hname, host_resolve_options);
}

/****************************************************************************
 * HostResolveOptions
 ****************************************************************************/

void
host_resolve_options_defaults(HostResolveOptions *options)
{
  options->use_dns = -1;
  options->use_fqdn = -1;
  options->use_dns_cache = -1;
  options->normalize_hostnames = -1;
}

void
host_resolve_options_init(HostResolveOptions *options, GlobalConfig *cfg)
{
  if (options->use_dns == -1)
    options->use_dns = cfg->host_resolve_options.use_dns;
  if (options->use_fqdn == -1)
    options->use_fqdn = cfg->host_resolve_options.use_fqdn;
  if (options->use_dns_cache == -1)
    options->use_dns_cache = cfg->host_resolve_options.use_dns_cache;
  if (options->normalize_hostnames == -1)
    options->normalize_hostnames = cfg->host_resolve_options.normalize_hostnames;
}

void
host_resolve_options_destroy(HostResolveOptions *options)
{
}