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