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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
|
// **********************************************************************
//
// Copyright (c) 2003-2014 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
#include <Glacier2/ProxyVerifier.h>
#include <vector>
#include <string>
using namespace std;
using namespace Ice;
//
// TODO: Some of the address matching helper classes can probably be
// refactored out. It's a question of whether to keep really simple
// differences refactored into separate classes or go to fewer but
// slightly smarter classes.
//
//
// Proxy rule implementations.
//
namespace Glacier2
{
//
// A numeric range that can be encoded in a filter.
//
struct Range
{
long start;
long end;
};
static void
parseGroup(const string& parameter, vector<int>& validPorts, vector<Range>& ranges)
{
istringstream istr(parameter);
while(!istr.eof())
{
ws(istr);
int value;
if(!(istr >> value))
{
throw string("expected number");
}
ws(istr);
if(!istr.eof())
{
char c;
if(istr >> c)
{
if(c == ',')
{
validPorts.push_back(value);
}
else if(c == '-')
{
Range r;
r.start = value;
ws(istr);
if(istr.eof())
{
throw string("Unterminated range");
}
if(!(istr >> value))
{
throw string("expected number");
}
r.end = value;
ws(istr);
if(!istr.eof())
{
istr >> c;
if(c != ',')
{
throw string("expected comma separator");
}
}
ranges.push_back(r);
}
else if(!istr.eof())
{
throw string("unexpected trailing character");
}
}
}
else
{
validPorts.push_back(value);
}
}
}
//
// Base class for address matching operations.
//
class AddressMatcher
{
public:
virtual ~AddressMatcher() {}
virtual bool match(const string&, string::size_type& pos) = 0;
virtual const char* toString() const = 0;
protected:
};
class MatchesAny : public AddressMatcher
{
public:
MatchesAny()
{
}
bool
match(const string&, string::size_type&)
{
return true;
}
const char*
toString() const
{
return "(ANY)";
}
};
//
// Match the start of a string (i.e. position == 0). Occurs when filter
// string starts with a set of characters followed by a wildcard or
// numeric range.
//
class StartsWithString : public AddressMatcher
{
public:
StartsWithString(const string& criteria):
_criteria(criteria),
_description("starts with " + criteria)
{
}
bool
match(const string& space, string::size_type& pos)
{
assert(pos == 0);
bool result = strncmp(space.c_str(), _criteria.c_str(), _criteria.size()) == 0;
if(result)
{
pos += _criteria.size();
}
return result;
}
const char*
toString() const
{
return _description.c_str();
}
private:
string _criteria;
string _description;
};
//
// Match the end portion of a string. Occurs when a filter string starts
// with a wildcard or numeric range, but ends with a string.
//
class EndsWithString : public AddressMatcher
{
public:
EndsWithString(const string& criteria):
_criteria(criteria),
_description("ends with " + criteria)
{
}
bool
match(const string& space, string::size_type& pos)
{
if(space.size() - pos < _criteria.size())
{
return false;
}
string::size_type spaceEnd = space.size();
for(string::size_type i = _criteria.size(); i > 0; --i)
{
if(space[spaceEnd - 1] != _criteria[i-1])
{
return false;
}
--spaceEnd;
}
return true;
}
virtual const char*
toString() const
{
return _description.c_str();
}
private:
string _criteria;
string _description;
};
class MatchesString : public AddressMatcher
{
public:
MatchesString(const string& criteria):
_criteria(criteria),
_description("matches " + criteria)
{
}
bool
match(const string& space, string::size_type& pos)
{
if(strncmp(space.c_str(), _criteria.c_str(), _criteria.size()) == 0)
{
pos += _criteria.size();
return true;
}
return false;
}
virtual const char*
toString() const
{
return _description.c_str();
}
private:
string _criteria;
string _description;
};
//
// Match against somewhere within a string. Occurs when a filter
// contains a string bounded by wildcards, or numeric ranges. e.g. *bar*.com.
//
class ContainsString : public AddressMatcher
{
public:
ContainsString(const string& criteria):
_criteria(criteria),
_description("contains " + criteria)
{
}
bool
match(const string& space, string::size_type& pos)
{
string::size_type offset = space.find(_criteria, pos);
if(offset == string::npos)
{
return false;
}
pos = offset + _criteria.size() +1;
return true;
}
virtual const char*
toString() const
{
return _description.c_str();
}
private:
string _criteria;
string _description;
};
//
// Match a number against a range of values. This occurs when the filter
// contains a numeric range or group of numeric values. e.g. foo[1-3,
// 10].bar.com. Also used to match port numbers and ranges.
//
class MatchesNumber : public AddressMatcher
{
public:
MatchesNumber(const vector<int>& values, const vector<Range>& ranges,
const char* descriptionPrefix = "matches "):
_values(values),
_ranges(ranges)
{
ostringstream ostr;
ostr << descriptionPrefix;
{
bool start = true;
for(vector<int>::const_iterator i = values.begin(); i != values.end(); ++i)
{
if(start)
{
if(values.size() > 1)
{
ostr << "one of ";
}
start = false;
}
else
{
ostr << ", ";
}
ostr << *i;
}
}
if(values.size() > 0 && ranges.size() > 0)
{
ostr << " or ";
}
{
bool start = true;
for(vector<Range>::const_iterator i = ranges.begin(); i != ranges.end(); ++i)
{
if(start)
{
start = false;
}
else
{
ostr << ", or";
}
ostr << i->start << " up to " << i->end;
}
}
ostr << ends;
_description = ostr.str();
}
bool
match(const string & space, string::size_type& pos)
{
istringstream istr(space.substr(pos));
int val;
if(!(istr >> val))
{
return false;
}
pos += static_cast<string::size_type>(istr.tellg());
{
for(vector<int>::const_iterator i = _values.begin(); i != _values.end(); ++i)
{
if(val == *i)
{
return true;
}
}
}
{
for(vector<Range>::const_iterator i = _ranges.begin(); i != _ranges.end(); ++i)
{
if((val >= i->start) && (val <= i->end))
{
return true;
}
}
}
return false;
}
virtual const char*
toString() const
{
return _description.c_str();
}
private:
const vector<int> _values;
const vector<Range> _ranges;
string _description;
};
//
// Occurs when a numeric range is preceded by a wildcard.
//
class ContainsNumberMatch : public MatchesNumber
{
public:
ContainsNumberMatch(const vector<int>& values, const vector<Range>& ranges):
MatchesNumber(values, ranges, "contains ")
{
}
bool
match(const string& space, string::size_type& pos)
{
while(true)
{
pos = space.find_first_of("0123456789", pos);
if(pos == string::npos)
{
return false;
}
if(MatchesNumber::match(space, pos))
{
return true;
}
}
return false;
}
};
class EndsWithNumber : public MatchesNumber
{
public:
EndsWithNumber(const vector<int>& values, const vector<Range>& ranges):
MatchesNumber(values, ranges, "ends with ")
{
}
bool
match(const string& space, string::size_type& pos)
{
pos = space.find_last_not_of("0123456789", pos);
if(pos == space.size()-1)
{
return false;
}
return MatchesNumber::match(space, pos);
}
};
//
// AddressMatcher factories abstract away the logic of which matching
// objects need to be created depending on the state of the filter
// string parsing. Similar to changing a tool that produces the right
// result depending on how far along you are in the job, the factories
// are selected according to what transition has occurred while parsing
// the filter string.
//
class AddressMatcherFactory
{
public:
virtual ~AddressMatcherFactory() {}
virtual AddressMatcher*
create(const string& criteria) = 0;
virtual AddressMatcher*
create(const vector<int>& ports, const vector<Range>& ranges) = 0;
};
class StartFactory : public AddressMatcherFactory
{
public:
AddressMatcher*
create(const string& criteria)
{
return new StartsWithString(criteria);
}
AddressMatcher*
create(const vector<int>& ports, const vector<Range>& ranges)
{
return new MatchesNumber(ports, ranges);
}
};
class WildCardFactory : public AddressMatcherFactory
{
public:
AddressMatcher*
create(const string& criteria)
{
return new ContainsString(criteria);
}
AddressMatcher*
create(const vector<int>& ports, const vector<Range>& ranges)
{
return new ContainsNumberMatch(ports, ranges);
}
};
class FollowingFactory : public AddressMatcherFactory
{
public:
AddressMatcher*
create(const string& criteria)
{
return new MatchesString(criteria);
}
AddressMatcher*
create(const vector<int>& ports, const vector<Range>& ranges)
{
return new MatchesNumber(ports, ranges);
}
};
class EndsWithFactory : public AddressMatcherFactory
{
public:
AddressMatcher*
create(const string& criteria)
{
return new EndsWithString(criteria);
}
AddressMatcher*
create(const vector<int>& ports, const vector<Range>& ranges)
{
return new EndsWithNumber(ports, ranges);
}
};
//
// A proxy validation rule encapsulating an address filter.
//
class AddressRule : public Glacier2::ProxyRule
{
public:
AddressRule(const CommunicatorPtr& communicator, const vector<AddressMatcher*>& address, MatchesNumber* port,
const int traceLevel) :
_communicator(communicator),
_addressRules(address),
_portMatcher(port),
_traceLevel(traceLevel)
{
}
~AddressRule()
{
for(vector<AddressMatcher*>::const_iterator i = _addressRules.begin(); i != _addressRules.end(); ++i)
{
delete *i;
}
delete _portMatcher;
}
virtual bool
check(const ObjectPrx& prx) const
{
EndpointSeq endpoints = prx->ice_getEndpoints();
if(endpoints.size() == 0)
{
return false;
}
for(EndpointSeq::const_iterator i = endpoints.begin(); i != endpoints.end(); ++i)
{
string info = (*i)->toString();
string host;
if(!extractPart("-h ", info, host))
{
return false;
}
string port;
if(!extractPart("-p ", info, port))
{
return false;
}
string::size_type pos = 0;
if(_portMatcher && !_portMatcher->match(port, pos))
{
if(_traceLevel >= 3)
{
Trace out(_communicator->getLogger(), "Glacier2");
out << _portMatcher->toString() << " failed to match " << port << " at pos=" << pos << "\n";
}
return false;
}
pos = 0;
for(vector<AddressMatcher*>::const_iterator i = _addressRules.begin(); i != _addressRules.end(); ++i)
{
if(!(*i)->match(host, pos))
{
if(_traceLevel >= 3)
{
Trace out(_communicator->getLogger(), "Glacier2");
out << (*i)->toString() << " failed to match " << host << " at pos=" << pos << "\n";
}
return false;
}
if(_traceLevel >= 3)
{
Trace out(_communicator->getLogger(), "Glacier2");
out << (*i)->toString() << " matched " << host << " at pos=" << pos << "\n";
}
}
}
return true;
}
void
dump() const
{
cerr << "address(";
for(vector<AddressMatcher*>::const_iterator i = _addressRules.begin(); i != _addressRules.end(); ++i)
{
cerr << (*i)->toString() << " ";
}
if(_portMatcher != 0)
{
cerr << "):port(" << _portMatcher->toString() << " ";
}
cerr << ")" << endl;
}
private:
bool
extractPart(const char* opt, const string& source, string& result) const
{
string::size_type start = source.find(opt);
if(start == string::npos)
{
return false;
}
start += strlen(opt);
string::size_type end = source.find(' ', start);
if(end != string::npos)
{
result = source.substr(start, end - start);
}
else
{
result = source.substr(start);
}
return true;
}
CommunicatorPtr _communicator;
vector<AddressMatcher*> _addressRules;
MatchesNumber* _portMatcher;
const int _traceLevel;
};
static void
parseProperty(const Ice::CommunicatorPtr& communicator, const string& property, vector<ProxyRule*>& rules,
const int traceLevel)
{
StartFactory startsWithFactory;
WildCardFactory wildCardFactory;
EndsWithFactory endsWithFactory;
FollowingFactory followingFactory;
vector<ProxyRule*> allRules;
try
{
istringstream propertyInput(property);
while(!propertyInput.eof() && propertyInput.good())
{
MatchesNumber* portMatch = 0;
vector<AddressMatcher*> currentRuleSet;
string parameter;
ws(propertyInput);
propertyInput >> parameter;
string portInfo;
string::size_type portPortion = parameter.find(':');
string addr;
if(portPortion != string::npos)
{
addr = parameter.substr(0, portPortion);
string port = parameter.substr(portPortion + 1);
string::size_type openBracket = port.find('[');
if(openBracket != string::npos)
{
++openBracket;
string::size_type closeBracket = port.find(']', openBracket);
if(closeBracket == string::npos)
{
throw string("unclosed group");
}
port = port.substr(openBracket, closeBracket-openBracket);
}
vector<int> ports;
vector<Range> ranges;
parseGroup(port, ports, ranges);
portMatch = new MatchesNumber(ports, ranges);
}
else
{
addr = parameter;
}
//
// The addr portion can contain alphanumerics, * and
// ranges.
//
string::size_type current = 0;
if(current == addr.size())
{
throw string("expected address information before ':'");
}
//
// TODO: assuming that there is no leading or trailing whitespace. This
// should probably be confirmed.
//
assert(!isspace(static_cast<unsigned char>(parameter[current])));
assert(!isspace(static_cast<unsigned char>(addr[addr.size() -1])));
if(current != 0)
{
addr = addr.substr(current);
}
string::size_type mark = 0;
bool inGroup = false;
AddressMatcherFactory* currentFactory = &startsWithFactory;
if(addr == "*")
{
//
// Special case. Match everything.
//
currentRuleSet.push_back(new MatchesAny);
}
else
{
for(current = 0; current < addr.size(); ++current)
{
if(addr[current] == '*')
{
if(inGroup)
{
throw string("wildcards not permitted in groups");
}
//
// current == mark when the wildcard is at the head of a
// string or directly after a group.
//
if(current != mark)
{
currentRuleSet.push_back(currentFactory->create(addr.substr(mark, current-mark)));
}
currentFactory = &wildCardFactory;
mark = current + 1;
}
else if(addr[current] == '[')
{
// ??? what does it mean if current == mark?
if(current != mark)
{
currentRuleSet.push_back(currentFactory->create(addr.substr(mark, current-mark)));
currentFactory = &followingFactory;
}
inGroup = true;
mark = current + 1;
}
else if(addr[current] == ']')
{
if(!inGroup)
{
throw string("group close without group start");
}
inGroup = false;
if(mark == current)
{
throw string("empty group");
}
string group = addr.substr(mark, current - mark);
vector<int> numbers;
vector<Range> ranges;
parseGroup(group, numbers, ranges);
currentRuleSet.push_back(currentFactory->create(numbers, ranges));
currentFactory = &followingFactory;
mark = current + 1;
}
}
currentFactory = &endsWithFactory;
if(inGroup)
{
throw string("unclosed group");
}
if(mark != current)
{
currentRuleSet.push_back(currentFactory->create(addr.substr(mark, current - mark)));
}
}
allRules.push_back(new AddressRule(communicator, currentRuleSet, portMatch, traceLevel));
}
}
catch(...)
{
for(vector<ProxyRule*>::const_iterator i = allRules.begin(); i != allRules.end(); ++i)
{
delete *i;
}
throw;
}
rules = allRules;
}
//
// Helper function for checking a rule set.
//
static bool
match(const vector<ProxyRule*>& rules, const ObjectPrx& proxy)
{
for(vector<ProxyRule*>::const_iterator i = rules.begin(); i != rules.end(); ++i)
{
if((*i)->check(proxy))
{
return true;
}
}
return false;
}
//
// ProxyLengthRule returns 'true' if the string form of the proxy exceeds the configured
// length.
//
class ProxyLengthRule : public ProxyRule
{
public:
ProxyLengthRule(const CommunicatorPtr communicator, const string& count, int traceLevel) :
_communicator(communicator),
_traceLevel(traceLevel)
{
istringstream s(count);
if(!(s >> _count) || !s.eof())
{
throw string("Error parsing ProxySizeMax property");
}
if(_count <= 0)
{
throw string("ProxySizeMax must be greater than 1");
}
}
bool
check(const ObjectPrx& p) const
{
string s = p->ice_toString();
bool result = (s.size() > _count);
if(_traceLevel >= 1)
{
Trace out(_communicator->getLogger(), "Glacier2");
out << _communicator->proxyToString(p) << (result ? " exceeds " : " meets ")
<< "proxy size restriction\n";
}
return result;
}
private:
const CommunicatorPtr _communicator;
const int _traceLevel;
unsigned long _count;
};
} // End proxy rule implementations.
Glacier2::ProxyVerifier::ProxyVerifier(const CommunicatorPtr& communicator):
_communicator(communicator),
_traceLevel(communicator->getProperties()->getPropertyAsInt("Glacier2.Client.Trace.Reject"))
{
//
// Evaluation order is dependant on how the rules are stored to the
// rules vectors.
//
string s = communicator->getProperties()->getProperty("Glacier2.Filter.Address.Accept");
if(s != "")
{
try
{
Glacier2::parseProperty(communicator, s, _acceptRules, _traceLevel);
}
catch(const string& msg)
{
InitializationException ex(__FILE__, __LINE__);
ex.reason = "invalid `Glacier2.Filter.Address.Accept' property:\n" + msg;
throw ex;
}
}
s = communicator->getProperties()->getProperty("Glacier2.Filter.Address.Reject");
if(s != "")
{
try
{
Glacier2::parseProperty(communicator, s, _rejectRules, _traceLevel);
}
catch(const string& msg)
{
InitializationException ex(__FILE__, __LINE__);
ex.reason = "invalid `Glacier2.Filter.Address.Reject' property:\n" + msg;
throw ex;
}
}
s = communicator->getProperties()->getProperty("Glacier2.Filter.ProxySizeMax");
if(s != "")
{
try
{
_rejectRules.push_back(new ProxyLengthRule(communicator, s, _traceLevel));
}
catch(const string& msg)
{
InitializationException ex(__FILE__, __LINE__);
ex.reason = "invalid `Glacier2.Filter.ProxySizeMax' property:\n" + msg;
throw ex;
}
}
}
Glacier2::ProxyVerifier::~ProxyVerifier()
{
for(vector<ProxyRule*>::const_iterator i = _acceptRules.begin(); i != _acceptRules.end(); ++i)
{
delete (*i);
}
for(vector<ProxyRule*>::const_iterator j = _rejectRules.begin(); j != _rejectRules.end(); ++j)
{
delete (*j);
}
}
bool
Glacier2::ProxyVerifier::verify(const ObjectPrx& proxy)
{
//
// No rules have been defined so we accept all.
//
if(_acceptRules.size() == 0 && _rejectRules.size() == 0)
{
return true;
}
bool result = false;
if(_rejectRules.size() == 0)
{
//
// If there are no reject rules, we assume "reject all".
//
result = match(_acceptRules, proxy);
}
else if(_acceptRules.size() == 0)
{
//
// If no accept rules are defined we assume accept all.
//
result = !match(_rejectRules, proxy);
}
else
{
if(match(_acceptRules, proxy))
{
result = !match(_rejectRules, proxy);
}
}
//
// The proxy rules take care of the tracing for higher trace levels.
//
if(_traceLevel > 0)
{
Trace out(_communicator->getLogger(), "Glacier2");
if(result)
{
out << "accepted proxy " << _communicator->proxyToString(proxy) << '\n';
}
else
{
out << "rejected proxy " << _communicator->proxyToString(proxy) << '\n';
}
}
return result;
}
|