new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / debian / ossec-hids / var / ossec / agentless / main.exp
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 "\nERROR: ssh_integrity_check <hostname> <arguments>\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 sshsrc "agentless/ssh.exp"
21 set susrc "agentless/su.exp"
22 set sshloginsrc "agentless/sshlogin.exp"
23 set sshnopasssrc "agentless/ssh_nopass.exp"
24 set hostname [lindex $argv 0]
25 set args [lrange $argv 1 end]
26 set pass "x"
27 set use_su " "
28 set use_sudo " "
29 set addpass "x"
30 set timeout 20
31
32 # Do script test
33 if {[string compare $hostname "test"] == 0} {
34     if {[string compare $args "test"] == 0} {
35         exit 0;
36     }
37 }
38
39 # Check if the hostname (first argument) is an option
40 if {[string compare $hostname "use_su"] == 0} {
41     set use_su "su;"
42     set hostname [lindex $argv 1]
43     set args [lrange $argv 2 end]
44 }
45 # Check if the hostname (first argument) is an option
46 if {[string compare $hostname "use_sudo"] == 0} {
47     set use_sudo "sudo sh;"
48     set hostname [lindex $argv 1]
49     set args [lrange $argv 2 end]
50 }
51
52 # Read the password list
53 if [catch {
54     set in [open "$passlist" r]
55 } loc_error] {
56     send_user "\nERROR: Password list not present (use \"register_host\" first).\n"
57     exit 1;
58 }
59
60 while {[gets $in line] != -1} {
61         set me [string first "|" $line]
62         set me2 [string last "|" $line]
63         set length [string length $line]
64
65         if {$me == -1} {
66             continue;
67         }
68         if {$me2 == -1} {
69             continue;
70         }
71         if {$me == $me2} {
72             continue;
73         }
74
75         set me [expr $me-1]
76         set me2 [expr $me2-1]
77
78         set host_list [string range $line 0 $me]
79         set me [expr $me+2]
80         set pass_list [string range $line $me $me2]
81         set me2 [expr $me2+2]
82         set addpass_list [string range $line $me2 $length]
83
84         if {[string compare $host_list $hostname] == 0} {
85             set pass "$pass_list"
86             set addpass "$addpass_list"
87             break
88         }
89 }
90 close $in
91
92
93 if {[string compare $pass "x"] == 0} {
94     send_user "\nERROR: Password for '$hostname' not found.\n"
95     exit 1;
96 }