summaryrefslogtreecommitdiff
path: root/librabbitmq/amqp_hostcheck.c
blob: 8ad2cf7872a7914450d6e579af37dd3bc1c0a9b3 (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
/* vim:set ft=c ts=2 sw=2 sts=2 et cindent: */
/*
 * Copyright 1996-2014 Daniel Stenberg <daniel@haxx.se>.
 * Copyright 2014 Michael Steinert
 *
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * Except as contained in this notice, the name of a copyright holder shall
 * not be used in advertising or otherwise to promote the sale, use or other
 * dealings in this Software without prior written authorization of the
 * copyright holder.
 */

#include "amqp_private.h"

#include <string.h>

/* Portable, consistent toupper (remember EBCDIC). Do not use toupper()
 * because its behavior is altered by the current locale.
 */

static char
amqp_raw_toupper(char in)
{
  switch (in) {
  case 'a':
    return 'A';
  case 'b':
    return 'B';
  case 'c':
    return 'C';
  case 'd':
    return 'D';
  case 'e':
    return 'E';
  case 'f':
    return 'F';
  case 'g':
    return 'G';
  case 'h':
    return 'H';
  case 'i':
    return 'I';
  case 'j':
    return 'J';
  case 'k':
    return 'K';
  case 'l':
    return 'L';
  case 'm':
    return 'M';
  case 'n':
    return 'N';
  case 'o':
    return 'O';
  case 'p':
    return 'P';
  case 'q':
    return 'Q';
  case 'r':
    return 'R';
  case 's':
    return 'S';
  case 't':
    return 'T';
  case 'u':
    return 'U';
  case 'v':
    return 'V';
  case 'w':
    return 'W';
  case 'x':
    return 'X';
  case 'y':
    return 'Y';
  case 'z':
    return 'Z';
  }
  return in;
}

/*
 * amqp_raw_equal() is for doing "raw" case insensitive strings. This is meant
 * to be locale independent and only compare strings we know are safe for
 * this. See http://daniel.haxx.se/blog/2008/10/15/strcasecmp-in-turkish/ for
 * some further explanation to why this function is necessary.
 *
 * The function is capable of comparing a-z case insensitively even for
 * non-ascii.
 */

static int
amqp_raw_equal(const char *first, const char *second)
{
  while (*first && *second) {
    if (amqp_raw_toupper(*first) != amqp_raw_toupper(*second)) {
      /* get out of the loop as soon as they don't match */
      break;
    }
    first++;
    second++;
  }
  /* we do the comparison here (possibly again), just to make sure that if
   * the loop above is skipped because one of the strings reached zero, we
   * must not return this as a successful match
   */
  return (amqp_raw_toupper(*first) == amqp_raw_toupper(*second));
}

static int
amqp_raw_nequal(const char *first, const char *second, size_t max)
{
  while (*first && *second && max) {
    if (amqp_raw_toupper(*first) != amqp_raw_toupper(*second)) {
      break;
    }
    max--;
    first++;
    second++;
  }
  if (0 == max) {
    return 1; /* they are equal this far */
  }
  return amqp_raw_toupper(*first) == amqp_raw_toupper(*second);
}

/*
 * Match a hostname against a wildcard pattern.
 * E.g.
 *  "foo.host.com" matches "*.host.com".
 *
 * We use the matching rule described in RFC6125, section 6.4.3.
 * http://tools.ietf.org/html/rfc6125#section-6.4.3
 */

static int
amqp_hostmatch(const char *hostname, const char *pattern)
{
  const char *pattern_label_end, *pattern_wildcard, *hostname_label_end;
  int wildcard_enabled;
  size_t prefixlen, suffixlen;
  pattern_wildcard = strchr(pattern, '*');
  if (pattern_wildcard == NULL) {
    return amqp_raw_equal(pattern, hostname) ? 1 : 0;
  }
  /* We require at least 2 dots in pattern to avoid too wide wildcard match. */
  wildcard_enabled = 1;
  pattern_label_end = strchr(pattern, '.');
  if (pattern_label_end == NULL ||
      strchr(pattern_label_end + 1, '.') == NULL ||
      pattern_wildcard > pattern_label_end ||
      amqp_raw_nequal(pattern, "xn--", 4)) {
    wildcard_enabled = 0;
  }
  if (!wildcard_enabled) {
    return amqp_raw_equal(pattern, hostname) ? 1 : 0;
  }
  hostname_label_end = strchr(hostname, '.');
  if (hostname_label_end == NULL ||
      !amqp_raw_equal(pattern_label_end, hostname_label_end)) {
    return 0;
  }
  /* The wildcard must match at least one character, so the left-most
   * label of the hostname is at least as large as the left-most label
   * of the pattern.
   */
  if (hostname_label_end - hostname < pattern_label_end - pattern) {
    return 0;
  }
  prefixlen = pattern_wildcard - pattern;
  suffixlen = pattern_label_end - (pattern_wildcard + 1);
  return amqp_raw_nequal(pattern, hostname, prefixlen) &&
    amqp_raw_nequal(pattern_wildcard + 1, hostname_label_end - suffixlen,
                    suffixlen) ? 1 : 0;
}

int
amqp_hostcheck(const char *match_pattern, const char *hostname)
{
  /* sanity check */
  if (!match_pattern || !*match_pattern || !hostname || !*hostname) {
    return 0;
  }
  /* trivial case */
  if (amqp_raw_equal(hostname, match_pattern)) {
    return 1;
  }
  return amqp_hostmatch(hostname, match_pattern);
}