]> git.ipfire.org Git - thirdparty/squid.git/blob - src/acl/ServerName.cc
Fix printf format for size_t and mb_size_t
[thirdparty/squid.git] / src / acl / ServerName.cc
1 /*
2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 /* DEBUG: section 28 Access Control */
10
11 #include "squid.h"
12 #include "acl/Checklist.h"
13 #include "acl/DomainData.h"
14 #include "acl/RegexData.h"
15 #include "acl/ServerName.h"
16 #include "client_side.h"
17 #include "fde.h"
18 #include "HttpRequest.h"
19 #include "ipcache.h"
20 #include "SquidString.h"
21 #include "ssl/bio.h"
22 #include "ssl/ServerBump.h"
23 #include "ssl/support.h"
24 #include "URL.h"
25
26 // Compare function for tree search algorithms
27 static int
28 aclHostDomainCompare( char *const &a, char * const &b)
29 {
30 const char *h = static_cast<const char *>(a);
31 const char *d = static_cast<const char *>(b);
32 debugs(28, 7, "Match:" << h << " <> " << d);
33 return matchDomainName(h, d, true);
34 }
35
36 bool
37 ACLServerNameData::match(const char *host)
38 {
39 if (host == NULL)
40 return 0;
41
42 debugs(28, 3, "checking '" << host << "'");
43
44 char *h = const_cast<char *>(host);
45 char const * const * result = domains->find(h, aclHostDomainCompare);
46
47 debugs(28, 3, "'" << host << "' " << (result ? "found" : "NOT found"));
48
49 return (result != NULL);
50
51 }
52
53 ACLData<char const *> *
54 ACLServerNameData::clone() const
55 {
56 /* Splay trees don't clone yet. */
57 assert (!domains);
58 return new ACLServerNameData;
59 }
60
61 /// A helper function to be used with Ssl::matchX509CommonNames().
62 /// \retval 0 when the name (cn or an alternate name) matches acl data
63 /// \retval 1 when the name does not match
64 template<class MatchType>
65 int
66 check_cert_domain( void *check_data, ASN1_STRING *cn_data)
67 {
68 char cn[1024];
69 ACLData<MatchType> * data = (ACLData<MatchType> *)check_data;
70
71 if (cn_data->length > (int)sizeof(cn) - 1)
72 return 1; // ignore data that does not fit our buffer
73
74 memcpy(cn, cn_data->data, cn_data->length);
75 cn[cn_data->length] = '\0';
76 debugs(28, 4, "Verifying certificate name/subjectAltName " << cn);
77 if (data->match(cn))
78 return 0;
79 return 1;
80 }
81
82 int
83 ACLServerNameStrategy::match (ACLData<MatchType> * &data, ACLFilledChecklist *checklist, ACLFlags &flags)
84 {
85 assert(checklist != NULL && checklist->request != NULL);
86
87 if (checklist->conn() && checklist->conn()->serverBump()) {
88 if (X509 *peer_cert = checklist->conn()->serverBump()->serverCert.get()) {
89 if (Ssl::matchX509CommonNames(peer_cert, (void *)data, check_cert_domain<MatchType>))
90 return 1;
91 }
92 }
93
94 const char *serverName = NULL;
95 if (checklist->conn() && !checklist->conn()->sslCommonName().isEmpty()) {
96 SBuf scn = checklist->conn()->sslCommonName();
97 serverName = scn.c_str();
98 }
99
100 if (serverName == NULL)
101 serverName = checklist->request->GetHost();
102
103 if (serverName && data->match(serverName)) {
104 return 1;
105 }
106
107 return data->match("none");
108 }
109
110 ACLServerNameStrategy *
111 ACLServerNameStrategy::Instance()
112 {
113 return &Instance_;
114 }
115
116 ACLServerNameStrategy ACLServerNameStrategy::Instance_;
117