File Coverage

amqp_hostcheck.c
Criterion Covered Total %
statement 52 78 66.6
branch 24 40 60.0
condition n/a
subroutine n/a
pod n/a
total 76 118 64.4


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