2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * Author : Boris Mikhailenko <stg34@ua.fm>
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
35 #include "hostallow.h"
36 //-----------------------------------------------------------------------------
37 HOSTALLOW::HOSTALLOW()
41 //-----------------------------------------------------------------------------
42 int HOSTALLOW::ParseHosts(const char * str, int hostsType)
45 ðÒÏÉÚ×ÏÄÉÍ ÒÁÚÂÏÒ ÓÔÒÏËÉ ×ÉÄÁ host host host ...
46 ÇÄÅ host ÍÏÖÅÔ ÉÍÅÔØ ×ÉÄ a.b.c.d ÉÌÉ a.b.c.d/e
48 ÐÒÉÞÅÍ × ÓÌÕÞÁÅ ÓÅÔÉ ÍÁÓËÁ É ÁÄÒÅÓ ÄÏÌÖÎÙ ÂÙÔØ
49 ÓÏÏÔ×ÅÔÓÔ×ÕÀÝÉÍÉ ÄÒÕÇ ÄÒÕÇÕ.
51 òÅÚÕÌØÔÁÔÙ ÚÁÎÏÓÉÍ × ÓÏÏÔ×ÅÔÓÔ×ÕÀÝÉÊ ÓÐÉÓÏË
61 if (strcasecmp(str, "all") == 0)
63 if (hostsType == hostsAllow)
64 hostAllowList.push_back(INETADDR());
66 hostDenyList.push_back(INETADDR());
73 s = new char[len + 1];
81 if (ParseIPMask(tok, &ip, &mask) != 0)
86 //printfd(__FILE__, "ParseHosts tok %s\n", tok);
87 tok = strtok(NULL, " ");
88 if (hostsType == hostsAllow)
90 //printfd(__FILE__, "ParseHosts APPEND allow %X %X\n", ip, mask);
91 hostAllowList.push_back(INETADDR(ip, mask));
95 //printfd(__FILE__, "ParseHosts APPEND deny %X %X\n", ip, mask);
96 hostDenyList.push_back(INETADDR(ip, mask));
104 //-----------------------------------------------------------------------------
105 int HOSTALLOW::ParseIPMask(const char * s, uint32_t * ip, uint32_t * mask)
108 òÁÚÂÏÒ ÓÔÒÏËÉ ×ÉÄÁ a.b.c.d/e ÉÌÉ a.b.c.d
118 host = new char[len + 1];
120 while (s[i] != 0 && s[i] != '/')
128 if (inet_addr(host) == INADDR_NONE)
131 sprintf(errMsg, "Icorrect IP address %s", host);
135 *ip = inet_addr(host);
141 msk = strtol(&s[i+1], &res, 10);
144 sprintf(errMsg, "Icorrect mask %s", &s[i+1]);
149 if (msk < 0 || msk > 32)
151 sprintf(errMsg, "Icorrect mask %s", &s[i+1]);
158 m = htonl(0xFFffFFff<<(32 - msk));
167 if ((*ip & *mask) != *ip)
169 sprintf(errMsg, "Address does'n match mask.\n");
177 //-----------------------------------------------------------------------------
178 int HOSTALLOW::ParseOrder(const char * str)
181 ÐÒÏÉÚ×ÏÄÉÍ ÒÁÚÂÏÒ ÓÔÒÏËÉ ×ÉÄÁ allow deny ÉÌÉ deny allow
184 if (strcasecmp(str, "allow,deny") == 0)
190 if (strcasecmp(str, "deny,allow") == 0)
196 sprintf(errMsg, "Parameter \'order\' must be \'allow,deny\' or \'deny,allow\'");
199 //-----------------------------------------------------------------------------
200 int HOSTALLOW::GetError()
203 ÷ÏÚ×ÒÁÝÁÅÍ ËÏÄ ÏÛÉÂËÉ É ÓÂÒÁÓÙ×ÁÅÍ ÅÅ.
207 //-----------------------------------------------------------------------------
208 bool HOSTALLOW::HostAllowed(uint32_t ip)
211 ðÒÏ×ÅÒÑÅÍ Ñ×ÌÑÅÔÓÑ ÌÉ éð ÒÁÚÒÅÛÅÎÎÙÍ ÉÌÉ ÎÅÔ
214 if (order == orderDeny)
216 if (IsHostInDeniedList(ip))
221 if (IsHostInAllowedList(ip))
228 if (IsHostInAllowedList(ip))
233 if (IsHostInDeniedList(ip))
241 //-----------------------------------------------------------------------------
242 int HOSTALLOW::IsIPInSubnet(uint32_t checkedIP, INETADDR &ia)
244 //uint32_t checkedIP;
245 if ((ia.mask & checkedIP) == (ia.ip))
249 //-----------------------------------------------------------------------------
250 bool HOSTALLOW::IsHostInAllowedList(uint32_t ip)
253 îÁÈÏÄÉÔÓÑ ÌÉ éð × ÓÐÉÓËÅ ÒÁÚÒÅÛÅÎÎÙÈ
255 list<INETADDR>::iterator li;
257 li = hostAllowList.begin();
259 while(li != hostAllowList.end())
261 if (IsIPInSubnet(ip, *li))
267 //-----------------------------------------------------------------------------
268 bool HOSTALLOW::IsHostInDeniedList(uint32_t ip)
271 îÁÈÏÄÉÔÓÑ ÌÉ éð × ÓÐÉÓËÅ ÚÁÐÒÅÝÅÎÎÙÈ
273 list<INETADDR>::iterator li;
275 li = hostDenyList.begin();
277 while(li != hostDenyList.end())
279 if (IsIPInSubnet(ip, *li))
285 //-----------------------------------------------------------------------------
286 const char * HOSTALLOW::GetStrError()
289 ÷ÏÚ×ÒÁÝÁÅÍ ÔÅËÓÔÏ×ÏÅ ÏÐÉÓÁÎÉÅ ÏÛÉÂËÉ.
293 //-----------------------------------------------------------------------------