annotate src/lib-dns/dns-util.c @ 22656:1789bf2a1e01

director: Make sure HOST-RESET-USERS isn't used with max_moving_users=0 The reset command would just hang in that case. doveadm would never have sent this, so this is just an extra sanity check.
author Timo Sirainen <timo.sirainen@dovecot.fi>
date Sun, 05 Nov 2017 23:51:56 +0200
parents 2e2563132d5f
children cb108f786fb4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
21390
2e2563132d5f Updated copyright notices to include the year 2017.
Stephan Bosch <stephan.bosch@dovecot.fi>
parents: 21125
diff changeset
1 /* Copyright (c) 2010-2017 Dovecot authors, see the included COPYING file */
21125
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
2 #include "lib.h"
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
3 #include "dns-util.h"
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
4
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
5 /**
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
6 return first position from b->a of c or a if not found
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
7 */
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
8 static inline
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
9 const char *strchr_ba(const char *a, const char *b, char c)
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
10 {
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
11 for(;b>a && *b != c; b--);
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
12 return b;
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
13 }
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
14
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
15 int dns_ncompare(const char *a, const char *b, size_t n)
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
16 {
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
17 if (a == NULL && b == NULL) return 0;
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
18 if (a == NULL && b != NULL) return 1;
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
19 if (a != NULL && b == NULL) return -1;
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
20
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
21 for(size_t i = 0; i < n &&
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
22 *a != '\0' &&
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
23 *b != '\0' &&
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
24 dns_tolower(*a) == dns_tolower(*b);
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
25 i++, a++, b++);
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
26
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
27 return dns_tolower(*a) - dns_tolower(*b);
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
28 }
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
29
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
30 int dns_compare(const char *a, const char *b)
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
31 {
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
32 return dns_ncompare(a, b, (size_t)-1);
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
33 }
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
34
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
35 int dns_compare_labels(const char *a, const char *b)
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
36 {
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
37 if (a == NULL && b == NULL) return 0;
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
38 if (a == NULL && b != NULL) return 1;
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
39 if (a != NULL && b == NULL) return -1;
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
40
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
41 const char *ptr_a = a + strlen(a);
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
42 const char *ptr_b = b + strlen(b);
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
43 const char *label_a = ptr_a, *label_b = ptr_b;
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
44 int comp = 0;
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
45
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
46 while(comp == 0 && ptr_a > a && ptr_b > b) {
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
47 /* look for start of label, including dot */
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
48 label_a = strchr_ba(a, ptr_a, '.');
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
49 label_b = strchr_ba(b, ptr_b, '.');
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
50 if (ptr_a - label_a != ptr_b - label_b)
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
51 /* compare labels up to minimum length
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
52 but include \0 to make sure that we
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
53 don't consider alpha and alphabet
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
54 equal */
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
55 return dns_ncompare(label_a, label_b,
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
56 I_MIN(ptr_a - label_a,
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
57 ptr_b - label_b)+1);
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
58 comp = dns_ncompare(label_a, label_b, ptr_a -label_a);
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
59 ptr_a = label_a - 1;
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
60 ptr_b = label_b - 1;
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
61 }
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
62
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
63 return dns_tolower(*label_a) - dns_tolower(*label_b);
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
64 }
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
65
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
66 int dns_match_wildcard(const char *name, const char *mask)
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
67 {
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
68 i_assert(name != NULL && mask != NULL);
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
69
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
70 for(;*name != '\0' && *mask != '\0'; name++, mask++) {
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
71 switch(*mask) {
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
72 case '*':
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
73 name = strchr(name, '.');
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
74 if (name == NULL || mask[1] != '.') return -1;
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
75 mask++;
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
76 break;
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
77 case '?':
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
78 break;
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
79 default:
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
80 if (dns_tolower(*name) != dns_tolower(*mask)) return -1;
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
81 }
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
82 }
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
83 if (*mask == '*') mask++;
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
84 return dns_tolower(*name) == dns_tolower(*mask) ? 0 : -1;
001a41d133dc lib-dns: Add DNS specific matching algorithms
Aki Tuomi <aki.tuomi@dovecot.fi>
parents:
diff changeset
85 }