new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / debian / ossec-hids / var / ossec / agentless / ssh_foundry_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_pixconfig_diff <hostname> <commands>\n";
15     exit 1;
16 }
17
18 # NOTE: this script must be called from within /var/ossec for it to work
19 set passlist "agentless/.passlist"
20 set hostname [lindex $argv 0]
21 set commands [lrange $argv 1 end]
22 set pass "x"
23 set addpass "x"
24 set timeout 20
25
26 if {[string compare $hostname "test"] == 0} {
27     if {[string compare $commands "test"] == 0} {
28         exit 0;
29     }
30 }
31
32 # Read the password list
33 if [catch {
34     set in [open "$passlist" r]
35 } loc_error] {
36     send_user "ERROR: Password list not present (use \"register_host\" first).\n"
37     exit 1;
38 }
39
40 while {[gets $in line] != -1} {
41         set me [string first "|" $line]
42         set me2 [string last "|" $line]
43         set length [string length $line]
44
45         if {$me == -1} {
46             continue;
47         }
48         if {$me2 == -1} {
49             continue;
50         }
51         if {$me == $me2} {
52             continue;
53         }
54
55         set me [expr $me-1]
56         set me2 [expr $me2-1]
57
58         set host_list [string range $line 0 $me]
59         set me [expr $me+2]
60         set pass_list [string range $line $me $me2]
61         set me2 [expr $me2+2]
62         set addpass_list [string range $line $me2 $length]
63
64         if {[string compare $host_list $hostname] == 0} {
65             set pass "$pass_list"
66             set addpass "$addpass_list"
67             break
68         }
69 }
70 close $in
71
72 if {[string compare $pass "x"] == 0} {
73     send_user "ERROR: Password for '$hostname' not found.\n"
74     exit 1;
75 }
76
77 # SSH to the box and pass the directories to check
78 if [catch {
79     spawn ssh $hostname
80 } loc_error] {
81     send_user "ERROR: Opening connection: $loc_error.\n"
82     exit 1;
83 }
84
85 expect {
86     "WARNING: REMOTE HOST" {
87         send_user "ERROR: RSA host key for '$hostname' has changed. Unable to access.\n"
88         exit 1;
89     }
90     "*sure you want to continue connecting*" {
91         send "yes\r"
92         expect "* password:*" {
93             send "$pass\r"
94
95             expect {
96                 "Permission denied" {
97                     send_user "ERROR: Incorrect password to remote host: $hostname .\n"
98                     exit 1;
99                 }
100                 timeout {
101                     send_user "ERROR: Timeout while running on host (too long to finish): $hostname .\n"
102                     exit 1;
103                 }
104                 "*>" {
105                     send_user "\nINFO: Starting.\n"
106                 }
107             }
108         }
109     }
110     "ssh: connect to host*" {
111         send_user "ERROR: Unable to connect to remote host: $hostname .\n"
112         exit 1;
113     }
114     "no address associated with name" {
115         send_user "ERROR: Unable to connect to remote host: $hostname .\n"
116         exit 1;
117     }
118     "*Connection refused*" {
119         send_user "ERROR: Unable to connect to remote host: $hostname .\n"
120         exit 1;
121     }
122     "*Connection closed by remote host*" {
123         send_user "ERROR: Unable to connect to remote host: $hostname .\n"
124         exit 1;
125     }
126     "* password:*" {
127         send "$pass\r"
128
129         expect {
130             "Permission denied" {
131                 send_user "ERROR: Incorrect password to remote host: $hostname .\n"
132                 exit 1;
133             }
134             timeout {
135                 send_user "ERROR: Timeout while running on host (too long to finish): $hostname .\n"
136                 exit 1;
137             }
138             "*>" {
139                 send_user "INFO: Starting.\n"
140             }
141         }
142     }
143     timeout {
144         send_user "ERROR: Timeout while connecting to host: $hostname . \n"
145         exit 1;
146     }
147 }
148
149 if {[string compare $addpass ""] != 0} {
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 - incorrect password*" {
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
181 # Send commands
182 set timeout 60
183 send_user "\nSTORE: now\n"
184
185 send "skip-page-display\r"
186
187 # Exclude uptime from the output
188 send "sh run\r"
189 send "$commands\r"
190 send "exit\rexit\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;