-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver_config_elements.rb
1700 lines (1386 loc) · 64.3 KB
/
server_config_elements.rb
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
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module TacacsPlus
class Acl #:nodoc:
#==============================================================================#
# initialize
#==============================================================================#
attr_reader :name
def initialize(tacacs_daemon,name,entries)
@name = name
@entries = []
if (!name.kind_of?(String))
raise ArgumentError, "Expected String for name of ACL, but #{name.class} provided."
end
if (!entries.kind_of?(Array))
raise ArgumentError, "Expected Array for entries of ACL '#{name}', but #{entries.class} provided."
end
entries.each do |entry|
begin
e = AclEntry.new(tacacs_daemon,entry)
@entries.push(e)
rescue Exception => error
raise ArgumentError, "Entry #{@entries.length + 1} of ACL '#{name}' raised the following errors: #{error}"
end
end
end
#==============================================================================#
# match
#==============================================================================#
# check a NetAddr::CIDR against this acl
#
# return Hash
# :permit => True/False
# :by => String
#
def match(cidr)
line = 1
@entries.each do |entry|
if (entry.match?(cidr))
if (entry.permission == :permit)
permit = true
else
permit = false
end
return({:permit => permit, :by => "(line #{line})"})
end
line += 1
end
return({:permit => false, :by => '(implicit deny)'})
end
#==============================================================================#
# configuration
#==============================================================================#
# config hash for this object
def configuration
cfg = []
@entries.each {|e| cfg.push(e.configuration)}
return(cfg)
end
end
class AclEntry #:nodoc:
#==============================================================================#
# initialize
#==============================================================================#
attr_reader :permission
def initialize(tacacs_daemon,options)
@any = false
@cidr = nil
@permission = nil
@ip = nil
@wildcard_mask = nil
@network_object_group = nil
known_args = [:permission, :ip, :wildcard_mask, :network_object_group]
raise ArgumentError, "Expected Hash, but #{options.class} provided."if (!options.kind_of?(Hash))
TacacsPlus.validate_args(options.keys,known_args)
if (options.has_key?(:permission))
raise ArgumentError, "Expected String for :permission, but '#{options[:permission].class}' received." if (!options[:permission].kind_of?(String))
raise ArgumentError, "Expected 'permit' or 'deny' for :permission, but was '#{options[:permission]}'." if(options[:permission] != 'permit' && options[:permission] != 'deny')
@permission = options[:permission].to_sym
else
raise ArgumentError, "Argument :permission is missing."
end
if (options.has_key?(:ip))
if (options[:ip] != 'any')
begin
NetAddr.validate_ip_addr(options[:ip])
rescue Exception => error
raise ArgumentError, "Argument :ip raised the following errors: #{error}"
end
@ip = options[:ip]
if (options.has_key?(:wildcard_mask))
begin
NetAddr.validate_ip_addr(options[:wildcard_mask])
rescue Exception => error
raise ArgumentError, "Argument :wildcard_mask raised the following errors: #{error}"
end
@wildcard_mask = options[:wildcard_mask]
@cidr = NetAddr::CIDR.create(@ip, :WildcardMask => [@wildcard_mask,true])
else
@cidr = NetAddr::CIDR.create(@ip)
end
else
@any = true
end
elsif (options.has_key?(:network_object_group))
@network_object_group = tacacs_daemon.network_object_groups(options[:network_object_group])
if (!@network_object_group)
raise ArgumentError, "Unknown Network Object Group '#{options[:network_object_group]}' referenced."
end
else
raise ArgumentError, "ACL entry must have one of the following arguements: :ip or :network_object_group."
end
end
#==============================================================================#
# configuration
#==============================================================================#
# config hash for this object
def configuration
cfg = {:permission => @permission.to_s}
if (@any)
cfg[:ip] = 'any'
elsif (@ip)
cfg[:ip] = @ip
cfg[:wildcard_mask] = @wildcard_mask if (@wildcard_mask)
else
cfg[:network_object_group] = @network_object_group.name
end
return(cfg)
end
#==============================================================================#
# match?
#==============================================================================#
# match NetAddr::CIDR against this entry
# return True/False
#
def match?(cidr)
if (@any)
return(true)
elsif (@cidr)
if ( @cidr.matches?(cidr) )
return(true)
end
else
if (@network_object_group.match?(cidr))
return(true)
end
end
return(false)
end
end
class AuthorAVPair #:nodoc:
#==============================================================================#
# initialize
#==============================================================================#
attr_reader :name
def initialize(tacacs_daemon,name,entries)
@name = name
@entries = []
if (!name.kind_of?(String))
raise ArgumentError, "Expected String for name of AuthorAVPair, but #{name.class} provided."
end
if (!entries.kind_of?(Array))
raise ArgumentError, "Expected Array for entries of AuthorAVPair '#{name}', but #{entries.class} provided."
end
entries.each do |entry|
begin
e = AuthorAVPairEntry.new(tacacs_daemon,entry)
@entries.push(e)
rescue Exception => error
raise ArgumentError, "Entry #{@entries.length + 1} of AuthorAVPair '#{name}' raised the following errors: #{error}"
end
end
end
#==============================================================================#
# configuration
#==============================================================================#
# config hash for this object
def configuration
cfg = []
@entries.each {|e| cfg.push(e.configuration)}
return(cfg)
end
#==============================================================================#
# matching_entry
#==============================================================================#
# find matching entry on service and NetAddr::CIDR
# return AVPairs of matching entry or nil
#
def matching_entry(service,cidr)
match = nil
@entries.each do |entry|
if ( entry.match?(service,cidr) )
match = entry.avpairs
break
end
end
return(match)
end
end
class AuthorAVPairEntry #:nodoc:
#==============================================================================#
# initialize
#==============================================================================#
def initialize(tacacs_daemon,options)
@acl = nil
@avpairs = nil
@dynamic_avpairs = []
@network_av = nil
@service = nil
@shell_command_av = nil
known_args = [:acl, :avpairs, :network_av, :service, :shell_command_av]
raise ArgumentError, "Expected Hash, but #{options.class} provided."if (!options.kind_of?(Hash))
TacacsPlus.validate_args(options.keys,known_args)
if (options.has_key?(:acl))
@acl = tacacs_daemon.acls(options[:acl])
if (!@acl)
raise ArgumentError, "Unknown ACL '#{options[:acl]}' referenced."
end
end
if (options.has_key?(:service))
raise ArgumentError, "Expected String for :service, but #{options[:service].class} provided." if (!options[:service].kind_of?(String))
@service = options[:service]
else
raise ArgumentError, "No Service specified."
end
if (options.has_key?(:network_av))
raise ArgumentError, "Expected Hash for :network_av, but #{options[:network_av].class} provided." if (!options[:network_av].kind_of?(Hash))
@network_av = NetworkAV.new(tacacs_daemon, options[:network_av])
@dynamic_avpairs.push(@network_av.avpair)
end
if (options.has_key?(:shell_command_av))
raise ArgumentError, "Expected Hash for :shell_command_av, but #{options[:shell_command_av].class} provided." if (!options[:shell_command_av].kind_of?(Hash))
@shell_command_av = ShellCommandAV.new(tacacs_daemon, options[:shell_command_av])
@dynamic_avpairs.push(@shell_command_av.avpair)
end
if (options.has_key?(:avpairs))
raise ArgumentError, "Expected Array for :avpairs, but #{options[:avpairs].class} provided." if (!options[:avpairs].kind_of?(Array))
@avpairs = []
options[:avpairs].each do |avpair|
begin
TacacsPlus.validate_avpair(avpair)
rescue Exception => msg
raise ArgumentError, "Error on avpair '#{avpair}' : #{msg}"
end
@avpairs.push(avpair)
end
else
raise ArgumentError, "No AVPairs specified." if (!@network_av && !@shell_command_av)
end
end
#==============================================================================#
# avpairs
#==============================================================================#
def avpairs
avp = []
avp.concat(@avpairs) if (@avpairs)
avp.concat(@dynamic_avpairs) if (@dynamic_avpairs)
return(avp)
end
#==============================================================================#
# configuration
#==============================================================================#
# config hash for this object
def configuration
cfg = {:service => @service}
cfg[:acl] = @acl.name if (@acl)
cfg[:avpairs] = @avpairs if (@avpairs)
cfg[:network_av] = @network_av.configuration if (@network_av)
cfg[:shell_command_av] = @shell_command_av.configuration if (@shell_command_av)
return(cfg)
end
#==============================================================================#
# match?
#==============================================================================#
# match service and NetAddr::CIDR against this entry
# return True/False
#
def match?(service,cidr)
if (@acl)
return(true) if ( @service == service && @acl.match(cidr)[:permit] )
else
return(true) if ( @service == service )
end
return(false)
end
end
class CommandAuthorizationProfile #:nodoc:
#==============================================================================#
# initialize
#==============================================================================#
attr_reader :name
def initialize(tacacs_daemon,name,entries)
@name = name
@entries = []
if (!name.kind_of?(String))
raise ArgumentError, "Expected String for name of Command Authorization Profile, but #{name.class} provided."
end
if (!entries.kind_of?(Array))
raise ArgumentError, "Expected Array for entries of Command Authorization Profile '#{name}', but #{entries.class} provided."
end
entries.each do |entry|
begin
e = CommandAuthorizationProfileEntry.new(tacacs_daemon,entry)
@entries.push(e)
rescue Exception => error
raise ArgumentError, "Entry #{@entries.length + 1} of Command Authorization Profile '#{name}' raised the following errors: #{error}"
end
end
end
#==============================================================================#
# configuration
#==============================================================================#
# config hash for this object
def configuration
cfg = []
@entries.each {|e| cfg.push(e.configuration)}
return(cfg)
end
#==============================================================================#
# matching_entry
#==============================================================================#
# find matching entry on command
# return nil on no match or Hash
# :rule => String
# :permit => True/False
# :by => String
#
def matching_entry(command,cidr)
match = nil
@entries.each do |entry|
rule = entry.match?(command)
if (rule)
ret = {:rule => rule, :permit => true}
if (entry.acl)
acl_match = entry.acl.match(cidr)
ret[:permit] = acl_match[:permit]
ret[:by] = "ACL '#{entry.acl.name}' #{acl_match[:by]}"
else
ret[:permit] = true
end
return(ret)
end
end
return(nil)
end
end
class CommandAuthorizationProfileEntry #:nodoc:
#==============================================================================#
# initialize
#==============================================================================#
attr_reader :acl
def initialize(tacacs_daemon,options)
@acl = nil
@command = nil
@shell_command_object_group = nil
known_args = [:acl, :command, :shell_command_object_group]
raise ArgumentError, "Expected Hash, but #{options.class} provided."if (!options.kind_of?(Hash))
TacacsPlus.validate_args(options.keys,known_args)
if (options.has_key?(:acl))
@acl = tacacs_daemon.acls(options[:acl])
if (!@acl)
raise ArgumentError, "Unknown ACL '#{options[:acl]}' referenced."
end
end
if (options.has_key?(:command))
raise ArgumentError, "Expected String for :command, but #{options[:command].class} received." if(!options[:command].kind_of?(String))
@command = Regexp.new(options[:command])
elsif(options.has_key?(:shell_command_object_group))
@shell_command_object_group = tacacs_daemon.shell_command_object_groups(options[:shell_command_object_group])
if (!@shell_command_object_group)
raise ArgumentError, "Unknown Shell Command Object Group '#{options[:shell_command_object_group]}' referenced."
end
else
raise ArgumentError, "Command Authorization Profile entry must have one of the following arguements: :command or :shell_command_object_group."
end
end
#==============================================================================#
# configuration
#==============================================================================#
# config hash for this object
def configuration
cfg = {}
if (@command)
cfg[:command] = @command.source
else
cfg[:shell_command_object_group] = @shell_command_object_group.name
end
cfg[:acl] = @acl.name if (@acl)
return(cfg)
end
#==============================================================================#
# match?
#==============================================================================#
# match command against this entry
# return matching rule (as String) or nil
#
def match?(command)
if (@command && @command.match(command) )
return(@command.inspect)
elsif (@shell_command_object_group)
return( @shell_command_object_group.match?(command) )
end
return(nil)
end
end
class CommandAuthorizationWhitelistEntry #:nodoc:
#==============================================================================#
# initialize
#==============================================================================#
def initialize(tacacs_daemon,options)
@acl = nil
@command = nil
@shell_command_object_group = nil
known_args = [:acl, :command, :shell_command_object_group]
raise ArgumentError, "Expected Hash, but #{options.class} provided."if (!options.kind_of?(Hash))
TacacsPlus.validate_args(options.keys,known_args)
if (options.has_key?(:acl))
@acl = tacacs_daemon.acls(options[:acl])
if (!@acl)
raise ArgumentError, "Unknown ACL '#{options[:acl]}' referenced."
end
end
if (options.has_key?(:command))
raise ArgumentError, "Expected String for :command, but #{options[:command].class} received." if(!options[:command].kind_of?(String))
@command = Regexp.new(options[:command])
elsif(options.has_key?(:shell_command_object_group))
@shell_command_object_group = tacacs_daemon.shell_command_object_groups(options[:shell_command_object_group])
if (!@shell_command_object_group)
raise ArgumentError, "Unknown Shell Command Object Group '#{options[:shell_command_object_group]}' referenced."
end
else
raise ArgumentError, "Command Authorization Whitelist entry must have one of the following arguements: :command or :shell_command_object_group."
end
end
#==============================================================================#
# configuration
#==============================================================================#
# config hash for this object
def configuration
cfg = {}
cfg[:acl] = @acl.name if (@acl)
if (@command)
cfg[:command] = @command.source
else
cfg[:shell_command_object_group] = @shell_command_object_group.name
end
return(cfg)
end
#==============================================================================#
# match?
#==============================================================================#
# match command against this entry
# return matching rule (as String) or nil
#
def match?(cidr,command)
permit = true
if (@acl)
permit = @acl.match(cidr)[:permit]
end
if (permit)
if (@command && @command.match(command))
return(@command.inspect)
elsif (@shell_command_object_group)
return( @shell_command_object_group.match?(command) )
end
end
return(nil)
end
end
class NetworkAV #:nodoc:
#==============================================================================#
# initialize
#==============================================================================#
def initialize(tacacs_daemon,options)
@attribute = nil
@delimiter = nil
@value = nil
@tacacs_daemon = tacacs_daemon
known_args = [:attribute, :delimiter, :value]
raise ArgumentError, "Expected Hash for :network_av, but #{options.class} provided."if (!options.kind_of?(Hash))
TacacsPlus.validate_args(options.keys,known_args)
if (options.has_key?(:attribute))
raise ArgumentError, "Expected String for :attribute of :network_av, but '#{options[:attribute].class}' received." if (!options[:attribute].kind_of?(String))
raise ArgumentError, "Argument :attribute of :network_av (#{options[:attribute]}) should end with a '=' or '*'." if (options[:attribute] !~ /=$/ && options[:attribute] !~ /\*$/)
@attribute = options[:attribute]
else
raise ArgumentError, "Argument :attribute of :network_av is missing."
end
if (options.has_key?(:delimiter))
raise ArgumentError, "Expected String for :delimiter of :network_av, but '#{options[:delimiter].class}' received." if (!options[:delimiter].kind_of?(String))
@delimiter = options[:delimiter]
else
raise ArgumentError, "Argument :delimiter of :network_av is missing."
end
if (options.has_key?(:value))
raise ArgumentError, "Expected Array for :value of :network_av, but '#{options[:value].class}' received." if (!options[:value].kind_of?(Array))
options[:value].each do |x|
if (!@tacacs_daemon.network_object_groups(x))
raise ArgumentError, "Unknown Network Object Group '#{x}' referenced by :network_av."
end
end
@value = options[:value]
else
raise ArgumentError, "Argument :value of :network_av is missing."
end
end
#==============================================================================#
# avpair
#==============================================================================#
def avpair
avp = @attribute.dup
cidrs = []
@value.each do |x|
@tacacs_daemon.network_object_groups(x).entries.each do |e|
cidrs.push(e.to_s)
end
end
avp << cidrs.join(@delimiter)
return(avp)
end
#==============================================================================#
# configuration
#==============================================================================#
# config hash for this object
def configuration
cfg = {:attribute => @attribute, :delimiter => @delimiter, :value => @value}
return(cfg)
end
end
class NetworkObjectGroup #:nodoc:
#==============================================================================#
# initialize
#==============================================================================#
attr_reader :name, :entries
def initialize(name,entries)
@name = name
@entries = []
if (!name.kind_of?(String))
raise ArgumentError, "Expected String for name of Network Object Group, but #{name.class} provided."
end
if (!entries.kind_of?(Array))
raise ArgumentError, "Expected Array for entries of Network Object Group '#{name}', but #{entries.class} provided."
end
entries.each do |entry|
begin
raise ArgumentError, "Expected String, but #{entry.class} received." if(!entry.kind_of?(String))
@entries.push( NetAddr::CIDR.create(entry) )
rescue Exception => error
raise ArgumentError, "Entry #{@entries.length + 1} of Network Object Group '#{name}' raised the following errors: #{error}"
end
end
end
#==============================================================================#
# configuration
#==============================================================================#
# config hash for this object
def configuration
cfg = []
@entries.each {|e| cfg.push(e.desc)}
return(cfg)
end
#==============================================================================#
# match?
#==============================================================================#
# match NetAddr::CIDR against this group
# return True/False
#
def match?(cidr)
@entries.each do |entry|
if ( entry.matches?(cidr) )
return(true)
end
end
return(false)
end
end
class ShellCommandAV #:nodoc:
#==============================================================================#
# initialize
#==============================================================================#
def initialize(tacacs_daemon,options)
@attribute = nil
@delimiter = nil
@value = nil
@tacacs_daemon = tacacs_daemon
known_args = [:attribute, :delimiter, :value]
raise ArgumentError, "Expected Hash for :shell_command_av, but #{options.class} provided."if (!options.kind_of?(Hash))
TacacsPlus.validate_args(options.keys,known_args)
if (options.has_key?(:attribute))
raise ArgumentError, "Expected String for :attribute of :shell_command_av, but '#{options[:attribute].class}' received." if (!options[:attribute].kind_of?(String))
raise ArgumentError, "Argument :attribute of :shell_command_av (#{options[:attribute]})should end with a '=' or '*'." if (options[:attribute] !~ /=$/ && options[:attribute] !~ /\*$/)
@attribute = options[:attribute]
else
raise ArgumentError, "Argument :attribute of :shell_command_av is missing."
end
if (options.has_key?(:delimiter))
raise ArgumentError, "Expected String for :delimiter of :shell_command_av, but '#{options[:delimiter].class}' received." if (!options[:delimiter].kind_of?(String))
@delimiter = options[:delimiter]
else
raise ArgumentError, "Argument :delimiter of :shell_command_av is missing."
end
if (options.has_key?(:value))
raise ArgumentError, "Expected Array for :value of :shell_command_av, but '#{options[:value].class}' received." if (!options[:value].kind_of?(Array))
options[:value].each do |x|
if (!@tacacs_daemon.shell_command_object_groups(x))
raise ArgumentError, "Unknown Shell Command Object Group '#{x}' referenced by :shell_command_av."
end
end
@value = options[:value]
else
raise ArgumentError, "Argument :value of :shell_command_av is missing."
end
end
#==============================================================================#
# avpair
#==============================================================================#
def avpair
avp = @attribute.dup
regexps = []
@value.each do |x|
@tacacs_daemon.shell_command_object_groups(x).entries.each do |e|
regexps.push(e.source)
end
end
avp << regexps.join(@delimiter)
return(avp)
end
#==============================================================================#
# configuration
#==============================================================================#
# config hash for this object
def configuration
cfg = {:attribute => @attribute, :delimiter => @delimiter, :value => @value}
return(cfg)
end
end
class ShellCommandObjectGroup #:nodoc:
#==============================================================================#
# initialize
#==============================================================================#
attr_reader :name, :entries
def initialize(name,entries)
@name = name
@entries = []
if (!name.kind_of?(String))
raise ArgumentError, "Expected String for name of Shell Command Object Group, but #{name.class} provided."
end
if (!entries.kind_of?(Array))
raise ArgumentError, "Expected Array for entries of Shell Command Object Group '#{name}', but #{entries.class} provided."
end
entries.each do |entry|
begin
raise ArgumentError, "Expected String, but #{entry.class} received." if(!entry.kind_of?(String))
@entries.push( Regexp.new(entry) )
rescue Exception => error
raise ArgumentError, "Entry #{@entries.length + 1} of Shell Command Object Group '#{name}' raised the following errors: #{error}"
end
end
end
#==============================================================================#
# configuration
#==============================================================================#
# config hash for this object
def configuration
cfg = []
@entries.each {|e| cfg.push(e.source)}
return(cfg)
end
#==============================================================================#
# match?
#==============================================================================#
# match command against this entry
# return matching rule (as String) or nil
#
def match?(command)
@entries.each do |entry|
if (entry.match(command))
return(entry.inspect)
end
end
return(nil)
end
end
class TacacsDaemon #:nodoc:
#==============================================================================#
# initialize
#==============================================================================#
attr_reader :default_policy, :delimiter, :disabled_prompt, :ip, :key,
:log_file, :log_accounting, :log_authentication, :log_authorization,
:login_prompt, :name, :max_clients, :password_expired_prompt, :password_prompt,
:port, :testing, :sock_timeout
attr_accessor :command_authorization_whitelist, :dump_file, :logger, :logger_level
def initialize(options)
@default_policy = :deny
@delimiter = "\t"
@disabled_prompt = "Account has been disabled."
@dump_file = nil
@ip = '0.0.0.0'
@key = nil
@log_levels = {:debug => 0, :info => 1, :warn => 2, :error => 3, :fail => 4}
@log_accounting = true
@log_authentication = true
@log_authorization = true
@log_file = nil
@logger = nil
@logger_level = 2
@login_prompt = "User Access Verification\n\nUsername: "
@max_clients = 100
@name = nil
@password_expired_prompt = "Password has expired."
@password_prompt = "Password: "
@port = 49
@testing = false
@sock_timeout = 30
# elements init
@acls = {}
@author_avpairs = {}
@command_authorization_profiles = {}
@network_object_groups = {}
@shell_command_object_groups = {}
@user_groups = {}
@users = {}
if (!options.kind_of?(Hash))
raise ArgumentError, "Expected Hash, but #{options.class} provided."
end
known_args = [:default_policy, :disable_inactive, :disabled_prompt, :dump_file, :ip, :key, :log_accounting, :log_authentication,
:log_authorization, :logger, :log_level, :login_prompt, :name, :max_clients, :password_expired_prompt, :password_prompt,
:port, :sock_timeout, :testing, :delimiter]
TacacsPlus.validate_args(options.keys,known_args)
if (options.has_key?(:default_policy))
raise ArgumentError, "Expected String for :default_policy, but '#{options[:default_policy].class}' " +
"received." if (!options[:default_policy].kind_of?(String))
raise ArgumentError, "Expected 'permit' or 'deny' for :default_policy, but " +
"was '#{options[:default_policy]}'." if(options[:default_policy] != 'permit' && options[:default_policy] != 'deny')
@default_policy = options[:default_policy].to_sym
end
if (options.has_key?(:disabled_prompt))
raise ArgumentError, "Expected String for :disabled_prompt, but #{options[:disabled_prompt].class} received." if(!options[:disabled_prompt].kind_of?(String))
raise ArgumentError, "Custom disabled prompt must be less than 255 characters." if (options[:disabled_prompt].length > 255)
@disabled_prompt = options[:disabled_prompt]
end
if (options.has_key?(:dump_file))
if (options[:dump_file].kind_of?(IO) || options[:dump_file].kind_of?(String))
@dump_file = options[:dump_file]
else
raise ArgumentError, "Expected IO or String for argument :dump_file, but #{options[:dump_file].class} provided."
end
end
if (options.has_key?(:ip))
begin
NetAddr.validate_ip_addr(options[:ip])
rescue Exception => error
raise ArgumentError, "Argument :ip raised the following errors: #{error}"
end
@ip = options[:ip]
end
if (options.has_key?(:log_accounting))
if (!options[:log_accounting].kind_of?(FalseClass) && !options[:log_accounting].kind_of?(TrueClass) )
raise ArgumentError, "Expected True or False for argument :log_accounting, but #{options[:log_accounting].class} provided."
end
@log_accounting = options[:log_accounting]
end
if (options.has_key?(:log_authentication))
if (!options[:log_authentication].kind_of?(FalseClass) && !options[:log_authentication].kind_of?(TrueClass) )
raise ArgumentError, "Expected True or False for argument :log_authentication, but #{options[:log_authentication].class} provided."
end
@log_authentication = options[:log_authentication]
end
if (options.has_key?(:log_authorization))
if (!options[:log_authorization].kind_of?(FalseClass) && !options[:log_authorization].kind_of?(TrueClass) )
raise ArgumentError, "Expected True or False for argument :log_authorization, but #{options[:log_authorization].class} provided."
end
@log_authorization = options[:log_authorization]
end
if (options.has_key?(:logger))
if ( options[:logger].kind_of?(String) )
@log_file = options[:logger]
else
@logger = options[:logger]
end
else
@logger = Logger.new(STDOUT)
end
if (options.has_key?(:log_level))
if ( options[:log_level].kind_of?(Integer) )
if ( (0..4).member?(options[:log_level]) )
@logger_level = options[:log_level]
else
raise ArgumentError, "Argument :log_level should be between 0 and 4, but was #{options[:log_level]}."
end
else
raise ArgumentError, "Expected Integer for argument :log_level, but #{options[:log_level].class} provided."
end
end
if (options.has_key?(:login_prompt))
raise ArgumentError, "Expected String for :login_prompt, but #{options[:login_prompt].class} received." if(!options[:login_prompt].kind_of?(String))
raise ArgumentError, "Custom login prompt must be less than 255 characters." if (options[:login_prompt].length > 255)
@login_prompt = options[:login_prompt]
end