-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.rb
1165 lines (997 loc) · 38.9 KB
/
client.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
require 'timeout'
module TacacsPlus
# A class for creating TACACS+ clients.
class Client
attr_reader :key, :logger, :dump_file, :port, :server, :session_id, :sock_timeout, :testing
# MIXINS
include TacacsPlus::TacacsSocket
#==============================================================================#
# initialize()
#==============================================================================#
#===Synopsis
#Create a new TacacsPlus::Client object.
#
#===Usage
# TacacsPlus::Client.new(:key => 's0mek3y', :server => '127.0.0.1')
#
#===Arguments
#Hash with the following fields:
# :key => [String] -- Encryption key
# :logger => [Logger] -- Logger object for logging output
# :log_level - one of the standard Logger levels (0-4)
# :dump_file => [IO] -- record all server I/O as YAML to provided IO object.
# :port => [Integer] -- TCP port of server TACACS+ daemon
# :server => [String] -- ip/hostname of TACACS+ server.
# :session_id => [Integer] -- session id which client should use. will be random by default.
# :sock_timeout => [Integer] -- timeout for client connections to server
# :testing => [TrueClass|FalseClass] -- Enable testing mode if set True. Testing mode allows for unencrypted traffic to be accepted.
#
def initialize(options)
@logger = nil
@socket = nil
@port = 49
@key = nil
@dump_file = nil
@sock_timeout = 2
@session_id = nil
if (!options.kind_of?(Hash))
raise ArgumentError, "Expected Hash, but #{options.class} provided."
end
if (options.has_key?(:server))
@server = options[:server]
if (!@server.kind_of?(String))
raise ArgumentError, "Expected String for :server, but #{@server.class} provided."
end
else
raise ArgumentError, "Missing argument :server."
end
if (options.has_key?(:port))
@port = options[:port]
if (!@port.kind_of?(Integer))
raise ArgumentError, "Expected Integer for :port, but #{@port.class} provided."
end
if (@port >= 2**16)
raise ArgumentError, "#{@port} is not a valid TCP port."
end
end
if (options.has_key?(:key))
@key = options[:key]
if (!@key.kind_of?(String))
raise ArgumentError, "Expected String for :key, but #{@key.class} provided."
end
end
if (options.has_key?(:logger))
if (options[:logger].kind_of?(String))
begin
@logger = Logger.new(options[:logger])
rescue Exception => error
raise ArgumentError, "Error with argument :logger: #{error}"
end
else
@logger = options[:logger]
end
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] if @logger
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?(:dump_file))
if (options[:dump_file].kind_of?(String))
begin
@dump_file = File.open(options[:dump_file], 'w')
rescue Exception => error
raise ArgumentError, "Error with argument :dump_file: #{error}"
end
elsif (options[:dump_file].kind_of?(IO))
@dump_file = options[:dump_file]
else
raise ArgumentError, "Expected IO for :dump_file, but #{options[:dump_file].class} provided."
end
end
if (options.has_key?(:session_id))
@session_id = options[:session_id]
if (!@session_id.kind_of?(Integer))
raise ArgumentError, "Expected Integer for :session_id, but #{@session_id.class} provided."
end
end
if (options.has_key?(:testing))
@testing = options[:testing]
raise ArgumentError, "Expected True or False for :testing, " +
"but was #{@testing.class}" if (!@testing.kind_of?(TrueClass) && !@testing.kind_of?(FalseClass))
end
if (options.has_key?(:sock_timeout))
@sock_timeout = options[:sock_timeout]
if (!@sock_timeout.kind_of?(Integer))
raise ArgumentError, "Expected Integer for :sock_timeout, but #{@sock_timeout.class} provided."
end
end
end
#==============================================================================#
# attr_writers()
#==============================================================================#
#:key, :logger, :dump_file, :port, :server, :session_id, :sock_timeout, :testing
def key=(key)
if (!key.kind_of?(String))
raise ArgumentError, "Expected String, but #{key.class} provided."
end
@key = key
end
def logger=(logger)
@logger = logger
end
def dump_file=(dump_file)
if (!dump_file.kind_of?(IO))
raise ArgumentError, "Expected IO, but #{dump_file.class} provided."
end
@dump_file = dump_file
end
def port=(port)
if (!port.kind_of?(Integer))
raise ArgumentError, "Expected Integer, but #{port.class} provided."
end
if (port >= 2**16)
raise ArgumentError, "#{port} is not a valid TCP port."
end
@port = port
end
def server=(server)
if (!server.kind_of?(String))
raise ArgumentError, "Expected String, but #{server.class} provided."
end
@server = server
end
def session_id=(session_id)
if (!session_id.kind_of?(Integer))
raise ArgumentError, "Expected Integer, but #{session_id.class} provided."
end
@session_id = session_id
end
def sock_timeout=(sock_timeout)
if (!sock_timeout.kind_of?(Integer))
raise ArgumentError, "Expected Integer, but #{sock_timeout.class} provided."
end
@sock_timeout = sock_timeout
end
def testing=(testing)
if (!testing.kind_of?(TrueClass) && !testing.kind_of?(FalseClass))
raise ArgumentError, "Expected True or False for :testing, but was #{testing.class}"
end
@testing = testing
end
# Used for unit testing only
def socket #:nodoc:
return @socket
end
def socket=(socket) #:nodoc:
@socket = socket
end
#==============================================================================#
# account()
#==============================================================================#
#===Synopsis
# Send an accounting request to a server.
#
#===Usage
# client.account('user1', ['task_id=1', 'timezone=cst'], :start => true)
#
#===Arguments
# * username => [String] -- Mandatory
# * args => [Array] -- list of avpairs. See tac-rfc.1.78.txt for list of accounting avpairs.
# * flags => [Hash] -- set any of the following keys to true to set the appropriate flags: :start, :stop, :watchdog
#
#===Returns
#Hash with the following fields:
# :data => [String] -- message to be presented to administrator
# :pass => [true|false] -- true if request successful
# :server_msg => [String] -- message to be presented to user
#
def account(username,args,flags=nil)
if (!username.kind_of?(String))
raise ArgumentError, "Expected String for username, but #{username.class} provided."
end
if (!args.kind_of?(Array))
raise ArgumentError, "Expected Array for args, but #{args.class} provided."
end
# validate args
args.each {|arg| TacacsPlus.validate_avpair(arg)}
# open a socket to the server
socket = open_socket()
# make start packet
header = TacacsPlus::TacacsHeader.new
if (@session_id)
header.session_id = @session_id
else
header.randomize_session_id!
end
body = TacacsPlus::AccountingRequest.new
body.priv_lvl = 1
body.authen_type_ascii!
body.args = args
body.user = username
if (!flags.kind_of?(Hash))
raise ArgumentError, "Expected Hash for flags, but #{flags.class} provided."
else
body.flag_start! if (flags.has_key?(:start) && flags[:start] == true)
body.flag_stop! if (flags.has_key?(:stop) && flags[:stop] == true)
body.flag_watchdog! if (flags.has_key?(:watchdog) && flags[:watchdog] == true)
end
session = ClientSession.new()
session.request = PacketStruct.new(header,body)
session.type = :accounting
session.args = args
# process server dialog
attempt = process_response(session, socket)
return(attempt)
end
#==============================================================================#
# authorization_avpairs()
#==============================================================================#
#===Synopsis
# Request AVPairs representing shell or other settings from a TACACS+ server.
#
#===Usage
# client.authorization_avpairs('user1')
# client.authorization_avpairs('user1', 'raccess')
#
#
#===Arguments
# * username => [String] -- Mandatory
# * service => [String] -- Optional
#
#===Returns
#Hash with the following fields:
# :args => [Array] -- list of avpairs returned by the server
# :data => [String] -- message to be presented to administrator
# :pass => [true|false] -- true if request successful
# :pass_type => [:add|:repl] -- indicates whether server returned a pass_add or pass_repl message
# :server_msg => [String] -- message to be presented to user
#
def authorization_avpairs(username, service='shell')
if (!username.kind_of?(String))
raise ArgumentError, "Expected String for username, but #{username.class} provided."
end
if (!service.kind_of?(String))
raise ArgumentError, "Expected String for service, but #{service.class} provided."
end
# set args
if (service == 'shell')
args = ['service=shell', 'cmd=']
else
args = ["service=#{service}"]
end
# open a socket to the server
socket = open_socket()
# make start packet
header = TacacsPlus::TacacsHeader.new
if (@session_id)
header.session_id = @session_id
else
header.randomize_session_id!
end
body = TacacsPlus::AuthorizationRequest.new
body.priv_lvl = 1
body.authen_type_ascii!
body.args = args
body.user = username
session = ClientSession.new()
session.request = PacketStruct.new(header,body)
session.type = :authorization
session.args = args
# process server dialog
attempt = process_response(session, socket)
return(attempt)
end
#==============================================================================#
# authorize_command()
#==============================================================================#
#===Synopsis
# Perform shell command authorization on a TACACS+ server.
#
#===Usage
# client.authorize_command('user1', 'show running-configuration <cr>')
#
#
#===Arguments
# * username => [String] -- Mandatory
# * command => [String] -- Mandatory
#
#===Returns
#Hash with the following fields:
# :args => [Array] -- list of avpairs returned by the server
# :data => [String] -- message to be presented to administrator
# :pass => [true|false] -- true if request successful
# :pass_type => [:add|:repl] -- indicates whether server returned a pass_add or pass_repl message
# :server_msg => [String] -- message to be presented to user
#
def authorize_command(username,command)
if (!username.kind_of?(String))
raise ArgumentError, "Expected String for username, but #{username.class} provided."
end
if (!command.kind_of?(String))
raise ArgumentError, "Expected String for command, but #{command.class} provided."
end
# set args
args = ['service=shell']
cmd_args = command.split(' ')
cmd = "cmd=#{cmd_args.shift}"
begin
TacacsPlus.validate_avpair(cmd)
args.push(cmd)
cmd_args.each do |cmd_arg|
cmd_arg = 'cmd-arg=' + cmd_arg
TacacsPlus.validate_avpair(cmd_arg)
args.push(cmd_arg)
end
rescue => error
raise ArgumentError, "String provided as shell command was improperly formed and raised " +
"the following error: #{error}"
end
# open a socket to the server
socket = open_socket()
# make start packet
header = TacacsPlus::TacacsHeader.new
if (@session_id)
header.session_id = @session_id
else
header.randomize_session_id!
end
body = TacacsPlus::AuthorizationRequest.new
body.priv_lvl = 1
body.authen_type_ascii!
body.args = args
body.user = username
session = ClientSession.new()
session.request = PacketStruct.new(header,body)
session.type = :authorization
session.args = args
# process server dialog
attempt = process_response(session, socket)
return(attempt)
end
#==============================================================================#
# change_enable_password()
#==============================================================================#
#===Synopsis
# Perform a user enable password change on a TACACS+ server.
#
#===Usage
# client.change_enable_password('user1', 'password', 'new_password')
#
#===Arguments
# * username => [String] -- Mandatory
# * password => [String] -- Mandatory
# * new_password => [String] -- Mandatory
# * quick => [TrueClass|FalseClass] -- if true, then send username as part of Authentication Start -- Optional
#
#===Returns
#Hash with the following fields:
# :data => [String] -- message to be presented to administrator
# :pass => [true|false] -- true if request successful
# :server_msg => [String] -- message to be presented to user
#
def change_enable_password(username,password,new_password,quick=true)
if (!username.kind_of?(String))
raise ArgumentError, "Expected String for username, but #{username.class} provided."
end
if (!password.kind_of?(String))
raise ArgumentError, "Expected String for password, but #{password.class} provided."
end
if (!new_password.kind_of?(String))
raise ArgumentError, "Expected String for new_password, but #{new_password.class} provided."
end
# open a socket to the server
socket = open_socket()
# make start packet
header = TacacsPlus::TacacsHeader.new
if (@session_id)
header.session_id = @session_id
else
header.randomize_session_id!
end
body = TacacsPlus::AuthenticationStart.new
body.action_chpass!
body.authen_type_ascii!
body.service_enable!
body.priv_lvl = 1
body.user = username if (quick)
session = ClientSession.new()
session.request = PacketStruct.new(header,body)
session.type = :authentication
session.getuser = username
session.getpass = new_password
session.getdata = password
# process server dialog
attempt = process_response(session, socket)
return(attempt)
end
#==============================================================================#
# change_password()
#==============================================================================#
#===Synopsis
# Perform a user login password change on a TACACS+ server.
#
#===Usage
# client.change_password('user1', 'password', 'new_password')
#
#===Arguments
# * username => [String] -- Mandatory
# * password => [String] -- Mandatory
# * new_password => [String] -- Mandatory
# * quick => [TrueClass|FalseClass] -- if true, then send username as part of Authentication Start -- Optional
#
#===Returns
#Hash with the following fields:
# :data => [String] -- message to be presented to administrator
# :pass => [true|false] -- true if request successful
# :server_msg => [String] -- message to be presented to user
#
def change_password(username,password,new_password,quick=true)
if (!username.kind_of?(String))
raise ArgumentError, "Expected String for username, but #{username.class} provided."
end
if (!password.kind_of?(String))
raise ArgumentError, "Expected String for password, but #{password.class} provided."
end
if (!new_password.kind_of?(String))
raise ArgumentError, "Expected String for new_password, but #{new_password.class} provided."
end
# open a socket to the server
socket = open_socket()
# make start packet
header = TacacsPlus::TacacsHeader.new
if (@session_id)
header.session_id = @session_id
else
header.randomize_session_id!
end
body = TacacsPlus::AuthenticationStart.new
body.action_chpass!
body.authen_type_ascii!
body.priv_lvl = 1
body.user = username if (quick)
session = ClientSession.new()
session.request = PacketStruct.new(header,body)
session.type = :authentication
session.getuser = username
session.getpass = new_password
session.getdata = password
# process server dialog
attempt = process_response(session, socket)
return(attempt)
end
#==============================================================================#
# chap_login()
#==============================================================================#
#===Synopsis
# Perform ascii login authentication on a TACACS+ server.
#
#===Usage
# client.chap_login('user1', 'password', 'A', 'abcd')
#
#===Arguments
# * username => [String] -- Mandatory
# * password => [String] -- Mandatory
# * ppp_id => [String] -- Mandatory, 1 Byte
# * challenge => [String] -- Mandatory
#
#===Returns
#Hash with the following fields:
# :data => [String] -- message to be presented to administrator
# :pass => [true|false] -- true if request successful
# :server_msg => [String] -- message to be presented to user
#
def chap_login(username,password,ppp_id,challenge)
if (!username.kind_of?(String))
raise ArgumentError, "Expected String for username, but #{username.class} provided."
end
if (!password.kind_of?(String))
raise ArgumentError, "Expected String for password, but #{password.class} provided."
end
if (!ppp_id.kind_of?(String))
raise ArgumentError, "Expected String for ppp_id, but #{ppp_id.class} provided."
elsif (ppp_id.length != 1)
raise ArgumentError, "Expected 1-byte String for ppp_id, but was #{ppp_id.length} bytes."
end
if (!challenge.kind_of?(String))
raise ArgumentError, "Expected String for challenge, but #{challenge.class} provided."
elsif (challenge.length > 255)
raise ArgumentError, "Challenge may not be more then 255 bytes."
end
# open a socket to the server
socket = open_socket()
# make start packet
header = TacacsPlus::TacacsHeader.new
if (@session_id)
header.session_id = @session_id
else
header.randomize_session_id!
end
header.minor_version_one!
body = TacacsPlus::AuthenticationStart.new
body.action_login!
body.authen_type_chap!
body.priv_lvl = 1
body.user = username
# data should contain contat of ppp_id, challenge, and response
body.data = ppp_id + challenge + Digest::MD5.digest(ppp_id + password + challenge)
session = ClientSession.new()
session.request = PacketStruct.new(header,body)
session.type = :authentication
session.getuser = username
session.getpass = password
# process server dialog
attempt = process_response(session, socket)
return(attempt)
end
#==============================================================================#
# enable()
#==============================================================================#
#===Synopsis
# Perform enable service authentication on a TACACS+ server.
#
#===Usage
# client.enable('user1', 'password')
#
#===Arguments
# * username => [String] -- Mandatory
# * password => [String] -- Mandatory
# * quick => [TrueClass|FalseClass] -- if false, then dont send username as part of Authentication Start -- Optional
#
#===Returns
#Hash with the following fields:
# :data => [String] -- message to be presented to administrator
# :pass => [true|false] -- true if request successful
# :server_msg => [String] -- message to be presented to user
#
def enable(username,password,quick=true)
if (!username.kind_of?(String))
raise ArgumentError, "Expected String for username, but #{username.class} provided."
end
if (!password.kind_of?(String))
raise ArgumentError, "Expected String for password, but #{password.class} provided."
end
# open a socket to the server
socket = open_socket()
# make start packet
header = TacacsPlus::TacacsHeader.new
if (@session_id)
header.session_id = @session_id
else
header.randomize_session_id!
end
body = TacacsPlus::AuthenticationStart.new
body.action_login!
body.priv_lvl = 15
body.service_enable!
body.user = username if (quick)
session = ClientSession.new()
session.request = PacketStruct.new(header,body)
session.type = :authentication
session.getuser = username
session.getpass = password
# process server dialog
enable_attempt = process_response(session, socket)
return(enable_attempt)
end
#==============================================================================#
# login()
#==============================================================================#
#===Synopsis
# Perform ascii login authentication on a TACACS+ server.
#
#===Usage
# client.login('user1', 'password')
#
#===Arguments
# * username => [String] -- Mandatory
# * password => [String] -- Mandatory
# * quick => [TrueClass|FalseClass] -- if true, then send username as part of Authentication Start -- Optional
#
#===Returns
#Hash with the following fields:
# :data => [String] -- message to be presented to administrator
# :pass => [true|false] -- true if request successful
# :server_msg => [String] -- message to be presented to user
#
def login(username,password,quick=false)
if (!username.kind_of?(String))
raise ArgumentError, "Expected String for username, but #{username.class} provided."
end
if (!password.kind_of?(String))
raise ArgumentError, "Expected String for password, but #{password.class} provided."
end
# open a socket to the server
socket = open_socket()
# make start packet
header = TacacsPlus::TacacsHeader.new
if (@session_id)
header.session_id = @session_id
else
header.randomize_session_id!
end
body = TacacsPlus::AuthenticationStart.new
body.action_login!
body.authen_type_ascii!
body.priv_lvl = 1
body.user = username if (quick)
session = ClientSession.new()
session.request = PacketStruct.new(header,body)
session.type = :authentication
session.getuser = username
session.getpass = password
# process server dialog
attempt = process_response(session, socket)
return(attempt)
end
#==============================================================================#
# pap_login()
#==============================================================================#
#===Synopsis
# Perform ascii login authentication on a TACACS+ server.
#
#===Usage
# client.pap_login('user1', 'password')
#
#===Arguments
# * username => [String] -- Mandatory
# * password => [String] -- Mandatory
#
#===Returns
#Hash with the following fields:
# :data => [String] -- message to be presented to administrator
# :pass => [true|false] -- true if request successful
# :server_msg => [String] -- message to be presented to user
#
def pap_login(username,password)
if (!username.kind_of?(String))
raise ArgumentError, "Expected String for username, but #{username.class} provided."
end
if (!password.kind_of?(String))
raise ArgumentError, "Expected String for password, but #{password.class} provided."
end
# open a socket to the server
socket = open_socket()
# make start packet
header = TacacsPlus::TacacsHeader.new
if (@session_id)
header.session_id = @session_id
else
header.randomize_session_id!
end
header.minor_version_one!
body = TacacsPlus::AuthenticationStart.new
body.action_login!
body.authen_type_pap!
body.priv_lvl = 1
body.user = username
body.data = password
session = ClientSession.new()
session.request = PacketStruct.new(header,body)
session.type = :authentication
session.getuser = username
session.getpass = password
# process server dialog
attempt = process_response(session, socket)
return(attempt)
end
#==============================================================================#
# script_play()
#==============================================================================#
#===Synopsis
# Send a series of pre-defined packets to a TACACS+ server. This is useful
# for testing a server's response to malformed TACACS+ requests.
#
#===Usage
# packet1 = TacacsPlus::PacketStruct.new(TacacsPlus::TacacsHeader.new, AuthenticationStart.new)
# packet1.header.type_authentication!
# packet2 = TacacsPlus::PacketStruct.new(TacacsPlus::TacacsHeader.new, TacacsPlus::AuthenticationContinue.new)
# packet2.header.type_authorization! # see how server reacts to 'type' field set incorrectly
#
# client.script_play( [packet1,packet2] )
#
#===Arguments
# * script => Array of PacketStruct objects
#
#===Returns
# * nil
#
def script_play(script)
entry_num = 1
script.each do |entry|
raise "PacketStruct required, but entry number #{entry_num} was of type #{entry.class}." if (!entry.kind_of?(PacketStruct))
raise "TacacsHeader required, but #header portion of entry number #{entry_num} was of type #{entry.header.class}." if (!entry.header.kind_of?(TacacsPlus::TacacsHeader))
raise "TacacsBody required, but #body portion of entry number #{entry_num} was of type #{entry.body.class}." if (!entry.body.kind_of?(TacacsPlus::TacacsBody))
entry_num = entry_num + 1
end
# open a socket to the server and send first packet
socket = open_socket()
packet = script.shift
@dump_file.print("# Sent\n" + packet.to_yaml + "\n") if (@dump_file)
pkt = TacacsPlus.encode_packet(packet,@key)
socket.write(pkt)
# process response
while (!socket.closed?)
# get packet from server.
begin
recvd_pkt = get_packet(socket,@key)
if (!recvd_pkt)
@logger.debug("type=TacacsPlus , message=No response from server. Terminating connection.") if @logger
break
end
rescue Exception => error
@logger.debug("Error with connection to server: #{error}") if @logger
break
end
# send next packet if one exists
if (script.length != 0)
packet = script.shift
@dump_file.print("# Sent\n" + packet.to_yaml + "\n") if (@dump_file)
pkt = TacacsPlus.encode_packet(packet,@key)
socket.write(pkt)
else
break
end
end
socket.close if (!socket.closed?)
return(nil)
end
#==============================================================================#
# server_alive?()
#==============================================================================#
#===Synopsis
#Test a server to make sure that it is actively serving TACACS+ requests.
#The tests consists of an authentication request followed by an immediate abort.
#A valid user account is *not* needed in order to use this method.
#
#===Returns
# *true or false
#
def server_alive?()
alive = false
# open a socket to the server
begin
socket = open_socket()
rescue Exception
return(alive)
end
# make start packet
header = TacacsPlus::TacacsHeader.new
if (@session_id)
header.session_id = @session_id
else
header.randomize_session_id!
end
body = TacacsPlus::AuthenticationStart.new
body.action_login!
body.authen_type_ascii!
body.priv_lvl = 1
# send packet
if (!socket.closed?)
recvd_pkt = nil
begin
send_packet(socket,PacketStruct.new(header,body),@key)
recvd_pkt = get_packet(socket,@key)
rescue Exception => error
@logger.debug("Error with connection to server: #{error}") if @logger
return(alive)
end
if (recvd_pkt && recvd_pkt.body.kind_of?(AuthenticationReply) )
# send abort
body = TacacsPlus::AuthenticationContinue.new
body.flag_abort!
recvd_pkt.body = body
recvd_pkt.header.inc_seq_no!
send_packet(socket,recvd_pkt,@key)
alive = true
end
end
socket.close if (!socket.closed?)
return(alive)
end
# PRIVATE INSTANT METHODS
private
#==============================================================================#
# open_socket()
#==============================================================================#
# open socket to server
#
def open_socket()
socket = nil
if @socket
socket = @socket
else
Timeout::timeout(@sock_timeout) { socket = TCPSocket.new(@server, @port) }
BasicSocket.do_not_reverse_lookup = true
end
return(socket)
end
#==============================================================================#
# process_accounting()
#==============================================================================#
# main handler for accounting
#
def process_accounting(session)
if (session.reply.body.status_success?)
session.pass_fail[:pass] = true
end
session.pass_fail[:server_msg] = session.reply.body.server_msg
session.pass_fail[:data] = session.reply.body.data
session.terminate = true
return(nil)
end
#==============================================================================#
# process_authentication()
#==============================================================================#
# main handler for authentication
#
def process_authentication(session)
authen_reply = session.reply
if (authen_reply.header.seq_no >= 254)
# seq_no wrapped. start over.
msg = "Sequence Number reached 255. Sending 'abort' message to server."
@logger.error(msg) if @logger
body = TacacsPlus::AuthenticationContinue.new
body.flag_abort!
body.data = msg
session.terminate = true
elsif (authen_reply.body.status_getpass?)
body = TacacsPlus::AuthenticationContinue.new
body.user_msg = session.getpass
elsif (authen_reply.body.status_pass?)
session.pass_fail[:pass] = true
session.terminate = true
elsif(authen_reply.body.status_getuser?)
body = TacacsPlus::AuthenticationContinue.new
body.user_msg = session.getuser
elsif(authen_reply.body.status_getdata?)
body = TacacsPlus::AuthenticationContinue.new
body.user_msg = session.getdata
else
session.pass_fail[:data] = authen_reply.body.data if (authen_reply.body.data_len != 0)
session.pass_fail[:server_msg] = authen_reply.body.server_msg if (authen_reply.body.server_msg_len != 0)
session.terminate = true
end
if (body)
header = authen_reply.header.dup