Imported Upstream version 2.7
[ossec-hids.git] / src / agentlessd / scripts / main.exp
1 #!/usr/bin/env expect
2
3 # @(#) $Id: ./src/agentlessd/scripts/main.exp, 2011/09/08 dcid Exp $
4
5 # Agentless monitoring
6 #
7 # Copyright (C) 2009 Trend Micro Inc.
8 # All rights reserved.
9
10 # This program is a free software; you can redistribute it
11 # and/or modify it under the terms of the GNU General Public
12 # License (version 2) as published by the FSF - Free Software
13 # Foundation.
14
15
16 if {$argc <= 1} {
17     send_user "\nERROR: ssh_integrity_check <hostname> <arguments>\n";
18     exit 1;
19 }
20
21
22 # NOTE: this script must be called from within /var/ossec for it to work.
23 set passlist "agentless/.passlist"
24 set sshsrc "agentless/ssh.exp"
25 set susrc "agentless/su.exp"
26 set sshloginsrc "agentless/sshlogin.exp"
27 set sshnopasssrc "agentless/ssh_nopass.exp"
28 set hostname [lindex $argv 0]
29 set args [lrange $argv 1 end]
30 set pass "x"
31 set use_su " "
32 set use_sudo " "
33 set addpass "x"
34 set timeout 20 
35
36
37 # Doing script test
38 if {[string compare $hostname "test"] == 0} {
39     if {[string compare $args "test"] == 0} {
40         exit 0;
41     }
42 }
43
44 # Checking if the hostname (first argument) is an option.
45 if {[string compare $hostname "use_su"] == 0} {
46     set use_su "su;"
47     set hostname [lindex $argv 1]
48     set args [lrange $argv 2 end]
49 }
50 # Checking if the hostname (first argument) is an option.
51 if {[string compare $hostname "use_sudo"] == 0} {
52     set use_sudo "sudo sh;"
53     set hostname [lindex $argv 1]
54     set args [lrange $argv 2 end]
55 }
56
57
58 # Reading the password list.
59 if [catch {
60     set in [open "$passlist" r]
61 } loc_error] {
62     send_user "\nERROR: Password list not present (use \"register_host\" first).\n"
63     exit 1;
64 }
65
66 while {[gets $in line] != -1} {
67         set me [string first "|" $line]
68         set me2 [string last "|" $line]
69         set length [string length $line]
70         
71         if {$me == -1} {
72             continue;
73         }
74         if {$me2 == -1} {
75             continue;
76         }
77         if {$me == $me2} {
78             continue;
79         }
80         
81         set me [expr $me-1]
82         set me2 [expr $me2-1]
83         
84         set host_list [string range $line 0 $me]
85         set me [expr $me+2]
86         set pass_list [string range $line $me $me2]
87         set me2 [expr $me2+2]
88         set addpass_list [string range $line $me2 $length]
89
90         if {[string compare $host_list $hostname] == 0} {
91             set pass "$pass_list"
92             set addpass "$addpass_list"
93             break
94         }
95 }
96 close $in
97
98
99 if {[string compare $pass "x"] == 0} {
100     send_user "\nERROR: Password for '$hostname' not found.\n"
101     exit 1;
102 }