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