Umgestellt auf OO-Perl. Wrapper fuer einfachen Aufruf gebastelt.
[ssh-keysync.git] / files / ssh-keysync
1 #!/bin/sh
2 #
3 # Pull ssh host keys
4 #
5 # Maximilian Wilhelm <max@rfc2324.org>
6 # -- Sat, 17 Apr 2004 13:36:30 +0200
7 #
8
9 # Show debug statements by default
10 debug=1;
11
12 # Some variable to be defined...
13 TMP_DIR="/tmp"
14
15
16 CONFIG_FILE="/etc/rbm/ssh-keysync-client.conf"
17
18
19 # Check some things
20 init()
21 {
22         check_config
23         check_root
24         check_dir
25 }
26
27
28
29 # Nice little die function
30 die()
31 {
32         echo $@ >&2
33         exit 1
34 }
35
36
37 # Check if the config file exist and load options if possbile
38 check_config()
39 {
40         if [ -f ${CONFIG_FILE} -a -r ${CONFIG_FILE} ]; then
41                 if ! source ${CONFIG_FILE}; then
42                         echo "Failed to load config file \"${CONFIG_FILE}\", exiting." >&2
43                         exit 1
44                 fi
45         else
46                 echo "Unable to load config file \"${CONFIG_FILE}\". File does not exist or is not accessable, exiting." >&2
47                 exit 1;
48         fi
49
50         # Check 
51         [ -z "${SERVER_HOST}" ] && die "Error, SERVER_HOST undefined!"
52         [ -z "${SERVER_PATH}" ] && die "Error, SERVER_PATH undefined!"
53         
54         if [ -z "${LOCAL_FILE}" ]; then 
55                 [ "${debug}" ] &&  "LOCAL_FILE undefined, defaulting to /etc/ssh/ssh_known_hosts!"
56                 export LOCAL_FILE="/etc/ssh/ssh_known_hosts"
57         fi
58
59 }
60
61 # Check whether we are started by root
62 check_root()
63 {
64         if [ "`whoami`" != "root" ]; then
65                 echo "Sorry, `basename ${0}` can only be run as root!"
66                 exit 1
67         fi
68 }
69
70
71 # Check if our working directory does exist
72 check_dir()
73 {
74         if [ ! -d ${TMP_DIR} ]; then
75                 echo "Tempdir \"${TMP_DIR}\" does not exist, exiting..." >&2
76                 exit 1
77         fi
78 }
79
80
81 # Check if the Server is reachable
82 check_server()
83 {
84         ping -c 0.42 ${SERVER_HOST} 2>&1 >/dev/null
85         retval=$?;
86         [ "${retval}" != "0" ] && echo "Server ${SERVER_HOST} not reachable!";
87
88         return ${retval};
89 }
90
91
92 # Push local keys to the server.
93 push()
94 {
95         echo "Command not implemented for security reasons."
96         exit 0;
97 }
98
99
100 # Get global ssh_known_hosts from the server
101 pull() {
102         [ "${debug}" ] && echo -n "Getting global key file from the server: "
103         
104         if [ check_server ]; then
105                 FILE=`basename ${SERVER_PATH}`
106                 TMPFILE=`tempfile -d "${TMP_DIR}" -s '.skeysync'`
107                 
108                 if wget -q -O ${TMPFILE} http://${SERVER_HOST}/${SERVER_PATH}; then
109                     mv ${TMPFILE} ${LOCAL_FILE}
110                     [ "${debug}" ] && echo "done."
111                 else
112                     echo "Download failed!" >&2
113                     echo "Check the config file and/or check if the partition where ${TMP_DIR} lives is full." >&2
114                     exit 1
115                 fi
116         fi
117 }
118
119
120 # Print out little help
121 help() {
122         echo "Usage: $0 { -configcheck | -pull | -help } [ -quiet ]"
123         exit 0
124 }
125
126
127 #
128 # Let the show begin
129 #
130
131 # What to do?
132     [ $# == 0 ] && help
133
134     while [ $# -gt 0 ]; do
135         case "$1" in
136             -push)              export TASK=push ;;
137             -pull)              export TASK=pull ;;
138             -quiet)             unset debug ;;
139             -configcheck)       export TASK=" ";;
140             *)                  help ;;
141         esac
142         shift
143     done
144
145 # Let's do it
146 if [ ! "${TASK}" ]; then
147         echo "Nothing to do..." >&2
148         exit 0
149 fi
150
151 init
152 $TASK
153 exit 0