amqp_hostcheck.c

filter: Statementstmt 0.0% 0 / 102 Branchbran 0.0% 0 / 40 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 142 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« amqp_consumer.c ↑ Summary amqp_table.c »
1 /* vim:set ft=c ts=2 sw=2 sts=2 et cindent: */
2 /*
3 * Copyright 1996-2014 Daniel Stenberg <daniel@haxx.se>.
4 * Copyright 2014 Michael Steinert
5 *
6 * All rights reserved.
7 *
8 * Permission to use, copy, modify, and distribute this software for any
9 * purpose with or without fee is hereby granted, provided that the above
10 * copyright notice and this permission notice appear in all copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
15 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
17 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
18 * USE OR OTHER DEALINGS IN THE SOFTWARE.
19 *
20 * Except as contained in this notice, the name of a copyright holder shall
21 * not be used in advertising or otherwise to promote the sale, use or other
22 * dealings in this Software without prior written authorization of the
23 * copyright holder.
24 */
25  
26 #include "amqp_private.h"
27  
28 #include <string.h>
29  
30 /* Portable, consistent toupper (remember EBCDIC). Do not use toupper()
31 * because its behavior is altered by the current locale.
32 */
33  
34 static char
35 0 amqp_raw_toupper(char in)
36 {
37 0 switch (in) {
38 0 case 'a':
39 0 return 'A';
40 0 case 'b':
41 0 return 'B';
42 0 case 'c':
43 0 return 'C';
44 0 case 'd':
45 0 return 'D';
46 0 case 'e':
47 0 return 'E';
48 0 case 'f':
49 0 return 'F';
50 0 case 'g':
51 0 return 'G';
52 0 case 'h':
53 0 return 'H';
54 0 case 'i':
55 0 return 'I';
56 0 case 'j':
57 0 return 'J';
58 0 case 'k':
59 0 return 'K';
60 0 case 'l':
61 0 return 'L';
62 0 case 'm':
63 0 return 'M';
64 0 case 'n':
65 0 return 'N';
66 0 case 'o':
67 0 return 'O';
68 0 case 'p':
69 0 return 'P';
70 0 case 'q':
71 0 return 'Q';
72 0 case 'r':
73 0 return 'R';
74 0 case 's':
75 0 return 'S';
76 0 case 't':
77 0 return 'T';
78 0 case 'u':
79 0 return 'U';
80 0 case 'v':
81 0 return 'V';
82 0 case 'w':
83 0 return 'W';
84 0 case 'x':
85 0 return 'X';
86 0 case 'y':
87 0 return 'Y';
88 0 case 'z':
89 0 return 'Z';
90 }
91 0 return in;
92 }
93  
94 /*
95 * amqp_raw_equal() is for doing "raw" case insensitive strings. This is meant
96 * to be locale independent and only compare strings we know are safe for
97 * this. See http://daniel.haxx.se/blog/2008/10/15/strcasecmp-in-turkish/ for
98 * some further explanation to why this function is necessary.
99 *
100 * The function is capable of comparing a-z case insensitively even for
101 * non-ascii.
102 */
103  
104 static int
105 0 amqp_raw_equal(const char *first, const char *second)
106 {
107 0 ▶ while (*first && *second) {
Branch: 2 branches
BranchTrueFalseTotal
while (*first && *second) { 0 0 0
while (*first && *second) { 0 0 0
108 0 ▶ if (amqp_raw_toupper(*first) != amqp_raw_toupper(*second)) {
Branch: 1 branch
BranchTrueFalseTotal
if (amqp_raw_toupper(*first) != amqp_raw_toupper(*second)) { 0 0 0
109 /* get out of the loop as soon as they don't match */
110 0 break;
111 }
112 0 first++;
113 0 second++;
114 }
115 /* we do the comparison here (possibly again), just to make sure that if
116 * the loop above is skipped because one of the strings reached zero, we
117 * must not return this as a successful match
118 */
119 0 return (amqp_raw_toupper(*first) == amqp_raw_toupper(*second));
120 }
121  
122 static int
123 0 amqp_raw_nequal(const char *first, const char *second, size_t max)
124 {
125 0 ▶ while (*first && *second && max) {
Branch: 3 branches
BranchTrueFalseTotal
while (*first && *second && max) { 0 0 0
while (*first && *second && max) { 0 0 0
while (*first && *second && max) { 0 0 0
126 0 ▶ if (amqp_raw_toupper(*first) != amqp_raw_toupper(*second)) {
Branch: 1 branch
BranchTrueFalseTotal
if (amqp_raw_toupper(*first) != amqp_raw_toupper(*second)) { 0 0 0
127 0 break;
128 }
129 0 max--;
130 0 first++;
131 0 second++;
132 }
133 0 ▶ if (0 == max) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == max) { 0 0 0
134 0 return 1; /* they are equal this far */
135 }
136 0 return amqp_raw_toupper(*first) == amqp_raw_toupper(*second);
137 }
138  
139 /*
140 * Match a hostname against a wildcard pattern.
141 * E.g.
142 * "foo.host.com" matches "*.host.com".
143 *
144 * We use the matching rule described in RFC6125, section 6.4.3.
145 * http://tools.ietf.org/html/rfc6125#section-6.4.3
146 */
147  
148 static int
149 0 amqp_hostmatch(const char *hostname, const char *pattern)
150 {
151 const char *pattern_label_end, *pattern_wildcard, *hostname_label_end;
152 int wildcard_enabled;
153 size_t prefixlen, suffixlen;
154 0 pattern_wildcard = strchr(pattern, '*');
155 0 ▶ if (pattern_wildcard == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (pattern_wildcard == NULL) { 0 0 0
156 0 return amqp_raw_equal(pattern, hostname) ? 1 : 0;
157 }
158 /* We require at least 2 dots in pattern to avoid too wide wildcard match. */
159 0 wildcard_enabled = 1;
160 0 pattern_label_end = strchr(pattern, '.');
161 0 ▶ if (pattern_label_end == NULL ||
Branch: 1 branch
BranchTrueFalseTotal
if (pattern_label_end == NULL || 0 0 0
162 0 ▶ strchr(pattern_label_end + 1, '.') == NULL ||
Branch: 2 branches
BranchTrueFalseTotal
strchr(pattern_label_end + 1, '.') == NULL || 0 0 0
strchr(pattern_label_end + 1, '.') == NULL || 0 0 0
163 0 ▶ pattern_wildcard > pattern_label_end ||
Branch: 1 branch
BranchTrueFalseTotal
pattern_wildcard > pattern_label_end || 0 0 0
164 0 amqp_raw_nequal(pattern, "xn--", 4)) {
165 0 wildcard_enabled = 0;
166 }
167 0 ▶ if (!wildcard_enabled) {
Branch: 1 branch
BranchTrueFalseTotal
if (!wildcard_enabled) { 0 0 0
168 0 return amqp_raw_equal(pattern, hostname) ? 1 : 0;
169 }
170 0 hostname_label_end = strchr(hostname, '.');
171 0 if (hostname_label_end == NULL ||
172 0 !amqp_raw_equal(pattern_label_end, hostname_label_end)) {
173 0 return 0;
174 }
175 /* The wildcard must match at least one character, so the left-most
176 * label of the hostname is at least as large as the left-most label
177 * of the pattern.
178 */
179 0 ▶ if (hostname_label_end - hostname < pattern_label_end - pattern) {
Branch: 1 branch
BranchTrueFalseTotal
if (hostname_label_end - hostname < pattern_label_end - pattern) { 0 0 0
180 0 return 0;
181 }
182 0 prefixlen = pattern_wildcard - pattern;
183 0 suffixlen = pattern_label_end - (pattern_wildcard + 1);
184 0 return amqp_raw_nequal(pattern, hostname, prefixlen) &&
185 0 amqp_raw_nequal(pattern_wildcard + 1, hostname_label_end - suffixlen,
186 0 suffixlen) ? 1 : 0;
187 }
188  
189 int
190 0 amqp_hostcheck(const char *match_pattern, const char *hostname)
191 {
192 /* sanity check */
193 0 ▶ if (!match_pattern || !*match_pattern || !hostname || !*hostname) {
Branch: 4 branches
BranchTrueFalseTotal
if (!match_pattern || !*match_pattern || !hostname || !*hostname) { 0 0 0
if (!match_pattern || !*match_pattern || !hostname || !*hostname) { 0 0 0
if (!match_pattern || !*match_pattern || !hostname || !*hostname) { 0 0 0
if (!match_pattern || !*match_pattern || !hostname || !*hostname) { 0 0 0
194 0 return 0;
195 }
196 /* trivial case */
197 0 ▶ if (amqp_raw_equal(hostname, match_pattern)) {
Branch: 1 branch
BranchTrueFalseTotal
if (amqp_raw_equal(hostname, match_pattern)) { 0 0 0
198 0 return 1;
199 }
200 0 return amqp_hostmatch(hostname, match_pattern);
201 }
« amqp_consumer.c ↑ Summary amqp_table.c »