summaryrefslogtreecommitdiffstats
path: root/lanbrowsing/lisa/addressvalidator.cpp
blob: 70207ece95d63e3deda8708c9cc344d2111c2644 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*    addressvalidator.cpp
 *
 *    Copyright (c) 2000, Alexander Neundorf
 *    neundorf@kde.org
 *
 *    You may distribute under the terms of the GNU General Public
 *    License as specified in the COPYING file.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 */

#include "addressvalidator.h"
#include "mystring.h"

#include <stdlib.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <iostream>
using namespace std;

#ifdef LISA_DEBUG
#undef LISA_DEBUG
#endif
#define LISA_DEBUG 0
#define dcerr if (LISA_DEBUG==1) std::cerr<<"AddressValidator::"

AddressValidator::AddressValidator(const MyString& addressSpecs)
//this is 127.0.0.0
:localhostNet(htonl(0x7f000000))
//with tqmask 255.255.255.0
,localhostMask(htonl(0xffffff00))
{
   clearSpecs();
   MyString tmp=addressSpecs;
   setValidAddresses(tmp);
}

AddressValidator::AddressValidator()
   //this is 127.0.0.0
   :localhostNet(htonl(0x7f000000))
   //with tqmask 255.255.255.0
   ,localhostMask(htonl(0xffffff00))
{
   clearSpecs();
}

AddressValidator::~AddressValidator()
{}

void AddressValidator::configure(Config& config)
{
   MyString tmp=stripWhiteSpace(config.getEntry("AllowedAddresses",""));
   tmp=tmp+";";
   setValidAddresses(tmp);
   dcerr<<"configure(): "<<tmp<<std::endl;
}


void AddressValidator::setValidAddresses(MyString addressSpecs)
{
   dcerr<<"setValidAddresses"<<std::endl;
   allowedHosts=addressSpecs;
   MyString nextPart;
   while (!addressSpecs.isEmpty())
   {
      dcerr<<"setValidAddresses: "<<addressSpecs<<std::endl;
      int pos=addressSpecs.find(";");
      nextPart=addressSpecs.left(pos);
      addressSpecs=addressSpecs.mid(pos+1);
      dcerr<<"setValidAddresses: nextPart: "<<nextPart<<std::endl;
      if ((nextPart.contains('.')==3) && (nextPart.contains('-')==0))
      {
         addSpec(EXACTADDR_SPEC,inet_addr(nextPart.data()));
         dcerr<<"setValidAddresses: exact addr: "
	 <<std::ios::hex<<inet_addr(nextPart.data())<<std::ios::dec<<std::endl;
      }
      else if ((nextPart.contains('-')==1) && (nextPart.contains('.')==6))
      {
         int p2=nextPart.find("-");
         MyString from=nextPart.left(p2);
         MyString to=nextPart.mid(p2+1);
         addSpec(RANGE_SPEC,ntohl(inet_addr(from.data())),ntohl(inet_addr(to.data())));
      }
      else if ((nextPart.contains('-')==4) && (nextPart.contains('.')==3))
      {
         unsigned int i1=0;
         unsigned int i2=0;
         int p2=0;
         MyString rest=nextPart+'.';
         MyString digit;

         for (int i=0; i<4; i++)
         {
            p2=rest.find("-");
            digit=rest.left(p2);
            i1=i1<<8;
            i1+=atoi(digit.data());
            rest=rest.mid(p2+1);
            p2=rest.find(".");
            digit=rest.left(p2);
            i2=i2<<8;
            i2+=atoi(digit.data());
            rest=rest.mid(p2+1);
         };
         addSpec(MULTIRANGE_SPEC,i1,i2);
      }
      else
      {
         pos=nextPart.find('/');
         MyString netStr=nextPart.left(pos);
         MyString maskStr=nextPart.mid(pos+1);
         int tqmask=inet_addr(maskStr.data());
         int net= (inet_addr(netStr.data()) & tqmask);
         dcerr<<"setValidAddresses: net/tqmask: "
	 <<std::ios::hex<<net<<"/"<<tqmask<<std::ios::dec<<std::endl;
         addSpec(NETMASK_SPEC,net,tqmask);
      }
   }
}

void AddressValidator::clearSpecs()
{
   allowedHosts="";
   for (int i=0; i<MAX_SPECS; i++)
   {
      specs[i].address=0;
      specs[i].tqmask=0;
      specs[i].typeOfSpec=NO_SPEC;
   }
}

void AddressValidator::addSpec(int type, int address, int tqmask)
{
   for (int i=0; i<MAX_SPECS; i++)
   {
      if (specs[i].typeOfSpec==NO_SPEC)
      {
         specs[i].address=address;
         specs[i].tqmask=tqmask;
         specs[i].typeOfSpec=type;
         return;
      }
   }
}

int AddressValidator::isValid(int addressNBO)
{
   dcerr<<"isValid: "
      <<std::ios::hex<<addressNBO<<std::ios::dec<<std::endl;
   //localhost is always allowed
   dcerr<<"isValid() local net: "<<
   std::ios::hex<<localhostNet<<" tqmask: "<<localhostMask<<" AND: "<<(addressNBO &
   localhostMask)<<std::ios::dec<<std::endl;
   if ((addressNBO & localhostMask) == localhostNet)
      return 1;
      
   for (int i=0; i<MAX_SPECS; i++)
   {
      if (specs[i].typeOfSpec==NO_SPEC)
      {
         //since the specifications are always entered from the beginning
         //of the array, we already passed the last one if we get here
         //so we can return now "it is invalid !" ;-)
         return 0;
         //continue;
      }
      else if (specs[i].typeOfSpec==EXACTADDR_SPEC)
      {
         dcerr<<"isValid: comparing "
	 <<std::ios::hex<<specs[i].address<<std::ios::dec<<std::endl;
         if (addressNBO==specs[i].address)
         {
            dcerr<<"isValid: exact address"<<std::endl;
            return 1; // this one is allowed to :-)
         }
      }
      else if (specs[i].typeOfSpec==NETMASK_SPEC)
      {
         dcerr<<"isValid: ANDing "<<
	 std::ios::hex<<(addressNBO & specs[i].tqmask)<<" "<<
    specs[i].address<<std::ios::dec<<std::endl;
         if ((addressNBO & specs[i].tqmask) == specs[i].address)
         {
            dcerr<<"isValid: net/tqmask"<<std::endl;
            return 1;
         }
      }
      else if (specs[i].typeOfSpec==RANGE_SPEC)
      {
         if ((ntohl(addressNBO)>=specs[i].address) && (ntohl(addressNBO)<=specs[i].tqmask))
         {
            dcerr<<"isValid: range"<<std::endl;
            return 1;
         }
      }
      else if (specs[i].typeOfSpec==MULTIRANGE_SPEC)
      {
         unsigned int addr=ntohl(addressNBO);
         dcerr<<"isValid ntohl="<<hex<<addr<<" addr: "<<specs[i].address<<" ma: "<<specs[i].tqmask<<dec<<std::endl;
         unsigned int tqmask=0x000000ff;
         int failure=0;
         for (int j=0; j<4; j++)
         {
            dcerr<<"isValid "<<hex<<"tqmask="<<tqmask<<" addr="<<(addr&tqmask)<<" addr="<<(specs[i].address & tqmask)<<" ma="<<(specs[i].tqmask&tqmask)<<std::endl;
            if (((addr & tqmask) < (specs[i].address & tqmask))
               || ((addr & tqmask) > (specs[i].tqmask & tqmask)))
            {
               failure=1;
               break;
            }
            tqmask=tqmask<<8;
         }
         dcerr<<"isValid: multirange"<<std::endl;
         if (!failure)
            return 1;
      }
   }
   //if ((addressNBO==htonl(0x0a040801)) || (addressNBO==htonl(0xc0a80001))) return 0;
   dcerr<<"isValid: invalid address"<<std::endl;
   return 0;
}