Imported Upstream version 2.5.1
[ossec-hids.git] / src / agentlessd / scripts / register_host.sh
1 #!/bin/sh
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 MYNAME="register_host.sh"
16 MYPASS=".passlist"
17
18
19 # Checking the location.
20 ls -la $MYNAME > /dev/null 2>&1
21 if [ ! $? = 0 ]; then
22     LOCALDIR=`dirname $0`;
23     cd ${LOCALDIR}
24
25     ls -la $MYNAME > /dev/null 2>&1
26     if [ ! $? = 0 ]; then
27         echo "ERROR: You must run this script from the same directory."
28         exit 1;
29     fi    
30 fi    
31
32
33
34 # Arguments
35 if [ "x$1" = "x" -o "x$1" = "xhelp" -o "x$1" = "x-h" ]; then
36     echo "$0 options:"
37     echo "        add <user@host> [<passwd>] (<additional_pass>)"
38     echo "        list (passwords)"
39     exit 0;
40 fi
41
42
43 if [ "x$1" = "xlist" ]; then
44     echo "*Available hosts: "
45     if [ "x$2" = "xpasswords" ]; then
46         cat $MYPASS | sort | uniq;
47     else    
48         cat $MYPASS | cut -d "|" -f 1 | sort | uniq;
49     fi    
50     exit 0;
51
52
53
54 elif [ "x$1" = "xadd" ]; then
55     if [ "x$2" = "x" ]; then
56         echo "ERROR: Missing hostname name.";
57         echo "ex: $0 add <user@host> [<passwd>] (<additional_pass>)";
58         exit 1;
59     fi
60     
61     grep "$2|" $MYPASS > /dev/null 2>&1
62     if [ $? = 0 ]; then
63         echo "ERROR: Host '$2' already added.";
64         exit 1;
65     fi
66     
67     
68     # Checking if the password was supplied.
69     if [ "x$3" = "x" ]; then
70         echo "Please provide password for host $2."
71         echo -n "Password: ";
72         stty -echo
73         read INPASS
74         stty echo
75         
76         echo "Please provide additional password for host $2 (<enter> for empty)."
77         echo -n "Password: ";
78         stty -echo
79         read ADDPASS
80         stty echo
81     else
82         INPASS=$3    
83         ADDPASS=$4
84     fi
85     
86     echo "$2|$INPASS|$ADDPASS" >> $MYPASS;
87     if [ ! $? = 0 ]; then
88         echo "ERROR: Unable to creating entry (echo failed)."
89         exit 1;
90     fi    
91     echo "*Host $2 added."
92
93 else
94     
95     echo "ERROR: Invalid argument.";
96     exit 1;
97     
98 fi        
99
100
101 # EOF
102