1 /* Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved.
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License, version 2.0, for more details.
18 
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
22 
23 #ifndef HOSTNAME_INCLUDED
24 #define HOSTNAME_INCLUDED
25 
26 #include "my_global.h"                          /* uint */
27 #include "my_net.h"
28 #include "hash_filo.h"
29 
30 struct Host_errors
31 {
32 public:
33   Host_errors();
34   ~Host_errors();
35 
36   void reset();
37   void aggregate(const Host_errors *errors);
38 
39   /** Number of connect errors. */
40   ulong m_connect;
41 
42   /** Number of host blocked errors. */
43   ulong m_host_blocked;
44   /** Number of transient errors from getnameinfo(). */
45   ulong m_nameinfo_transient;
46   /** Number of permanent errors from getnameinfo(). */
47   ulong m_nameinfo_permanent;
48   /** Number of errors from is_hostname_valid(). */
49   ulong m_format;
50   /** Number of transient errors from getaddrinfo(). */
51   ulong m_addrinfo_transient;
52   /** Number of permanent errors from getaddrinfo(). */
53   ulong m_addrinfo_permanent;
54   /** Number of errors from Forward-Confirmed reverse DNS checks. */
55   ulong m_FCrDNS;
56   /** Number of errors from host grants. */
57   ulong m_host_acl;
58   /** Number of errors from missing auth plugin. */
59   ulong m_no_auth_plugin;
60   /** Number of errors from auth plugin. */
61   ulong m_auth_plugin;
62   /** Number of errors from authentication plugins. */
63   ulong m_handshake;
64   /** Number of errors from proxy user. */
65   ulong m_proxy_user;
66   /** Number of errors from proxy user acl. */
67   ulong m_proxy_user_acl;
68   /** Number of errors from authentication. */
69   ulong m_authentication;
70   /** Number of errors from ssl. */
71   ulong m_ssl;
72   /** Number of errors from max user connection. */
73   ulong m_max_user_connection;
74   /** Number of errors from max user connection per hour. */
75   ulong m_max_user_connection_per_hour;
76   /** Number of errors from the default database. */
77   ulong m_default_database;
78   /** Number of errors from init_connect. */
79   ulong m_init_connect;
80   /** Number of errors from the server itself. */
81   ulong m_local;
82 
has_errorHost_errors83   bool has_error() const
84   {
85     return ((m_host_blocked != 0)
86       || (m_nameinfo_transient != 0)
87       || (m_nameinfo_permanent != 0)
88       || (m_format != 0)
89       || (m_addrinfo_transient != 0)
90       || (m_addrinfo_permanent != 0)
91       || (m_FCrDNS != 0)
92       || (m_host_acl != 0)
93       || (m_no_auth_plugin != 0)
94       || (m_auth_plugin != 0)
95       || (m_handshake != 0)
96       || (m_proxy_user != 0)
97       || (m_proxy_user_acl != 0)
98       || (m_authentication != 0)
99       || (m_ssl != 0)
100       || (m_max_user_connection != 0)
101       || (m_max_user_connection_per_hour != 0)
102       || (m_default_database != 0)
103       || (m_init_connect != 0)
104       || (m_local != 0));
105   }
106 
sum_connect_errorsHost_errors107   void sum_connect_errors()
108   {
109     /* Current (historical) behavior: */
110     m_connect= m_handshake;
111   }
112 
clear_connect_errorsHost_errors113   void clear_connect_errors()
114   {
115     m_connect= 0;
116   }
117 };
118 
119 /** Size of IP address string in the hash cache. */
120 #define HOST_ENTRY_KEY_SIZE INET6_ADDRSTRLEN
121 
122 /**
123   An entry in the hostname hash table cache.
124 
125   Host name cache does two things:
126     - caches host names to save DNS look ups;
127     - counts errors from IP.
128 
129   Host name can be empty (that means DNS look up failed),
130   but errors still are counted.
131 */
132 class Host_entry : public hash_filo_element
133 {
134 public:
next()135   Host_entry *next()
136   { return (Host_entry*) hash_filo_element::next(); }
137 
138   /**
139     Client IP address. This is the key used with the hash table.
140 
141     The client IP address is always expressed in IPv6, even when the
142     network IPv6 stack is not present.
143 
144     This IP address is never used to connect to a socket.
145   */
146   char ip_key[HOST_ENTRY_KEY_SIZE];
147 
148   /**
149     One of the host names for the IP address. May be a zero length string.
150   */
151   char m_hostname[HOSTNAME_LENGTH + 1];
152   /** Length in bytes of @c m_hostname. */
153   uint m_hostname_length;
154   /** The hostname is validated and used for authorization. */
155   bool m_host_validated;
156   ulonglong m_first_seen;
157   ulonglong m_last_seen;
158   ulonglong m_first_error_seen;
159   ulonglong m_last_error_seen;
160   /** Error statistics. */
161   Host_errors m_errors;
162 
set_error_timestamps(ulonglong now)163   void set_error_timestamps(ulonglong now)
164   {
165     if (m_first_error_seen == 0)
166       m_first_error_seen= now;
167     m_last_error_seen= now;
168   }
169 };
170 
171 #define RC_OK 0
172 #define RC_BLOCKED_HOST 1
173 int ip_to_hostname(struct sockaddr_storage *ip_storage,
174                    const char *ip_string,
175                    char **hostname, uint *connect_errors);
176 
177 void inc_host_errors(const char *ip_string, Host_errors *errors);
178 void reset_host_connect_errors(const char *ip_string);
179 bool hostname_cache_init(uint size);
180 void hostname_cache_free();
181 void hostname_cache_refresh(void);
182 uint hostname_cache_size();
183 void hostname_cache_resize(uint size);
184 void hostname_cache_lock();
185 void hostname_cache_unlock();
186 Host_entry *hostname_cache_first();
187 
188 #endif /* HOSTNAME_INCLUDED */
189