Incorporate identifying string into .rpm
[pl_sshd.git] / pl_sshd
1 #!/bin/bash
2 #
3 # Init file for PlanetLab OpenSSH server daemon (copied from standard
4 # sshd init file, minor modifications made).
5 #
6 # chkconfig: 2345 55 25
7 # description: PlanetLab OpenSSH server daemon
8 #
9 # processname: sshd
10 # config: /etc/ssh/ssh_host_key
11 # config: /etc/ssh/ssh_host_key.pub
12 # config: /etc/ssh/ssh_random_seed
13 # config: /etc/ssh/sshd_config
14 # pidfile: /var/run/pl_sshd.pid
15
16 # source function library
17 . /etc/rc.d/init.d/functions
18 #
19 # pull in sysconfig settings
20 [ -f /etc/sysconfig/pl_sshd ] && . /etc/sysconfig/pl_sshd
21
22 # add the PlanetLab-specific options
23 KEYDIR=/var/pl_sshd/keys
24 OPTIONS="-p 806 -o 'AuthorizedKeysFile $KEYDIR/%u/authorized_keys'"
25 AUTOMOUNT=/usr/sbin/automount
26 AUTOMAP=/etc/auto.pl_sshd
27 AUTOPID=
28
29 RETVAL=0
30 prog="pl_sshd"
31
32 # Some functions to make the below more readable
33 SSHD=/usr/sbin/sshd
34 RSA1_KEY=/var/local/etc/ssh_host_key
35 RSA_KEY=/var/local/etc/ssh_host_rsa_key
36 DSA_KEY=/var/local/etc/ssh_host_dsa_key
37 PID_FILE=/var/run/pl_sshd.pid
38
39 do_restart_sanity_check()
40 {
41         $SSHD -t
42         RETVAL=$?
43         if [ ! "$RETVAL" = 0 ]; then
44                 failure $"Configuration file or keys are invalid"
45                 echo
46         fi
47 }
48
49 check_automount()
50 {
51     # get pid for our automount process
52     AUTOPID=`mount | \
53         sed -ne "s%^automount(pid\([0-9]*\)) on $KEYDIR type autofs.*%\1%p"`
54
55     # check if that process is still alive
56     { [ -n "$AUTOPID" ] && ps -p $AUTOPID >/dev/null 2>&1; } || return 1
57
58     # check if we can actually mount a user dir in the automount dir
59     [ -d "$KEYDIR/root" ] || return 1
60 }
61
62 start()
63 {
64         # make sure the key dir exists and automount is working on it
65         [ -d "$KEYDIR" ] || mkdir -p $KEYDIR
66         [ -x "$AUTOMAP" ] ||
67         { echo "$AUTOMAP not executable"; return 1; }
68         check_automount || $AUTOMOUNT $KEYDIR program $AUTOMAP
69
70         echo -n $"Starting $prog:"
71         initlog -c "$SSHD $OPTIONS" && success || failure
72         RETVAL=$?
73         [ "$RETVAL" = 0 ] && touch /var/lock/subsys/pl_sshd
74         echo
75 }
76
77 stop()
78 {
79         check_automount && kill -USR2 $AUTOPID
80
81         #echo -n $"Stopping $prog:"
82         #killproc $SSHD -TERM
83         echo 'you need to kill the port 806 sshd(s) manually'
84         echo 'make sure not to kill the port 22 sshd...'
85         RETVAL=$?
86         [ "$RETVAL" = 0 ] && rm -f /var/lock/subsys/pl_sshd
87         #echo
88 }
89
90 reload()
91 {
92         echo -n $"Reloading $prog:"
93         killproc $SSHD -HUP
94         RETVAL=$?
95         echo
96 }
97
98 case "$1" in
99         start)
100                 start
101                 ;;
102         stop)
103                 stop
104                 ;;
105         restart)
106                 stop
107                 start
108                 ;;
109         reload)
110                 reload
111                 ;;
112         condrestart)
113                 if [ -f /var/lock/subsys/pl_sshd ] ; then
114                         do_restart_sanity_check
115                         if [ "$RETVAL" = 0 ] ; then
116                                 stop
117                                 # avoid race
118                                 sleep 3
119                                 start
120                         fi
121                 fi
122                 ;;
123         status)
124                 check_automount && echo automount running ||
125                     echo automount not functioning
126                 status pl_sshd
127                 RETVAL=$?
128                 ;;
129         *)
130                 echo $"Usage: $0 {start|stop|restart|reload|condrestart|status}"
131                 RETVAL=1
132 esac
133 exit $RETVAL