new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / debian / ossec-hids / var / ossec / agentless / ssh_asa-fwsmconfig_diff
1 #!/usr/bin/env expect
2
3 # Agentless monitoring
4 #
5 # Copyright (C) 2009 Trend Micro Inc.
6 # All rights reserved.
7 #
8 # This program is a free software; you can redistribute it
9 # and/or modify it under the terms of the GNU General Public
10 # License (version 2) as published by the FSF - Free Software
11 # Foundation.
12
13 if {$argc < 1} {
14     send_user "ERROR: ssh_asa-fwsmconfig_diff <hostname> <commands>\n";
15     send_user "ERROR: Must be run from /var/ossec\n";
16     exit 1;
17 }
18
19 # NOTE: this script must be called from within /var/ossec for it to work
20 set passlist "agentless/.passlist"
21 set hostname [lindex $argv 0]
22 set commands [lrange $argv 1 end]
23 set pass "x"
24 set addpass "x"
25 set timeout 20
26
27 if {[string compare $hostname "test"] == 0} {
28     if {[string compare $commands "test"] == 0} {
29         exit 0;
30     }
31 }
32
33 # Read the password list
34 if [catch {
35     set in [open "$passlist" r]
36 } loc_error] {
37     send_user "ERROR: Password list not present (use \"register_host\" first).\n"
38     exit 1;
39 }
40
41 while {[gets $in line] != -1} {
42         set me [string first "|" $line]
43         set me2 [string last "|" $line]
44         set length [string length $line]
45
46         if {$me == -1} {
47             continue;
48         }
49         if {$me2 == -1} {
50             continue;
51         }
52         if {$me == $me2} {
53             continue;
54         }
55
56         set me [expr $me-1]
57         set me2 [expr $me2-1]
58
59         set host_list [string range $line 0 $me]
60         set me [expr $me+2]
61         set pass_list [string range $line $me $me2]
62         set me2 [expr $me2+2]
63         set addpass_list [string range $line $me2 $length]
64
65         if {[string compare $host_list $hostname] == 0} {
66             set pass "$pass_list"
67             set addpass "$addpass_list"
68             break
69         }
70 }
71 close $in
72
73 if {[string compare $pass "x"] == 0} {
74     send_user "ERROR: Password for '$hostname' not found.\n"
75     exit 1;
76 }
77
78 # SSH to the box and pass the directories to check
79 if [catch {
80     spawn ssh -c des $hostname
81 } loc_error] {
82     send_user "ERROR: Opening connection: $loc_error.\n"
83     exit 1;
84 }
85
86 expect {
87     "WARNING: REMOTE HOST" {
88         send_user "ERROR: RSA host key for '$hostname' has changed. Unable to access.\n"
89         exit 1;
90     }
91     "*sure you want to continue connecting*" {
92         send "yes\r"
93         expect "* password:*" {
94             send "$pass\r"
95
96             expect {
97                 "Permission denied" {
98                     send_user "ERROR: Incorrect password to remote host: $hostname .\n"
99                     exit 1;
100                 }
101                 timeout {
102                     send_user "ERROR: Timeout while running on host (too long to finish): $hostname .\n"
103                     exit 1;
104                 }
105                 "*>" {
106                     send_user "\nINFO: Starting.\n"
107                 }
108             }
109         }
110     }
111     "ssh: connect to host*" {
112         send_user "ERROR: Unable to connect to remote host: $hostname .\n"
113         exit 1;
114     }
115     "no address associated with name" {
116         send_user "ERROR: Unable to connect to remote host: $hostname .\n"
117         exit 1;
118     }
119     "*Connection refused*" {
120         send_user "ERROR: Unable to connect to remote host: $hostname .\n"
121         exit 1;
122     }
123     "*Connection closed by remote host*" {
124         send_user "ERROR: Unable to connect to remote host: $hostname .\n"
125         exit 1;
126     }
127     "* password:*" {
128         send "$pass\r"
129
130         expect {
131             "Permission denied" {
132                 send_user "ERROR: Incorrect password to remote host: $hostname .\n"
133                 exit 1;
134             }
135             timeout {
136                 send_user "ERROR: Timeout while running on host (too long to finish): $hostname .\n"
137                 exit 1;
138             }
139             "*>" {
140                 send_user "INFO: Starting.\n"
141             }
142         }
143     }
144     timeout {
145         send_user "ERROR: Timeout while connecting to host: $hostname . \n"
146         exit 1;
147     }
148 }
149
150 # Go into enable mode
151 send "enable\r"
152 expect {
153     "Password:" {
154         send "$addpass\r"
155
156         expect {
157             "*asswor*" {
158                 send_user "ERROR: Incorrect enable password to remote host: $hostname .\n"
159                 exit 1;
160             }
161             "*rror in authenticatio*" {
162                 send_user "ERROR: Incorrect enable password to remote host: $hostname .\n"
163                 exit 1;
164             }
165             timeout {
166                 send_user "ERROR: Timeout while going to enable mode on host: $hostname .\n"
167                 exit 1;
168             }
169             "*#" {
170                 send_user "ok on enable pass\n"
171             }
172         }
173     }
174     timeout {
175         send_user "ERROR: Timeout while running enable on host: $hostname .\n"
176         exit 1;
177     }
178 }
179
180 # Send commands
181 set timeout 60
182 send_user "\nSTORE: now\n"
183
184 send "term pager 0\r"
185
186 # Exclude uptime from the output
187 send "show version | grep -v Configuration last| up\r"
188 send "show running-config\r"
189 send "$commands\r"
190 send "exit\r"
191
192 expect {
193     timeout {
194         send_user "ERROR: Timeout while running commands on host: $hostname .\n"
195         exit 1;
196     }
197     eof {
198         send_user "\nINFO: Finished.\n"
199         exit 0;
200     }
201 }
202
203 send_user "ERROR: Unable to finish properly.\n"
204 exit 1;