Minor bugfixes to main queue.
[carnet-upgrade.git] / src / files / etc / init.d / slapd
1 #! /bin/sh
2
3 # Kill me on all errors
4 set -e
5
6 # Stop processing if slapd is not there
7 [ -x /usr/sbin/slapd ] || exit 0
8
9 # Set default values
10 DB_RECOVER_CMD=db4.2_recover
11
12 # Source the init script configuration
13 if [ -f "/etc/default/slapd" ]; then
14         . /etc/default/slapd
15 fi
16
17 # Load the default location of the slapd config file
18 if [ -z "$SLAPD_CONF" ]; then
19         SLAPD_CONF="/etc/ldap/slapd.conf"
20 else
21         SLAPD_OPTIONS="-f $SLAPD_CONF $SLAPD_OPTIONS"
22         SLURPD_OPTIONS="-f $SLAPD_CONF $SLURPD_OPTIONS"
23 fi
24
25 # Stop processing if the config file is not there
26 if [ ! -r "$SLAPD_CONF" ]; then
27   cat <<EOF >&2
28 No configuration file was found for slapd at $SLAPD_CONF.
29 If you have moved the slapd configuration file please modify
30 /etc/default/slapd to reflect this.  If you chose to not
31 configure slapd during installation then you need to do so
32 prior to attempting to start slapd.
33 An example slapd.conf is in /usr/share/slapd
34 EOF
35   exit 0 # Should this be 1?
36 fi
37
38 # Figure out some default settings
39 # Check wether slurpd should get started
40 if [ "$SLURPD_START" != "yes" ] && [ "$SLURPD_START" != "no" ]; then
41         if grep -q '^replica' "$SLAPD_CONF" > /dev/null 2>&1 ; then
42                 SLURPD_START=yes
43         else
44                 SLURPD_START=no
45         fi
46 fi
47         
48 # Find out the name of slapd's pid file
49 if [ -z "$SLAPD_PIDFILE" ]; then
50         SLAPD_PIDFILE=`sed -ne 's/^pidfile[[:space:]]\+\(.\+\)/\1/p' \
51                 "$SLAPD_CONF"`
52 fi
53
54 # XXX: Breaks upgrading if there is no pidfile (invoke-rc.d stop will fail)
55 # -- Torsten
56 if [ -z "$SLAPD_PIDFILE" ]; then
57         cat <<EOF >&2
58 The pidfile for slapd is neither specified in "$SLAPD_CONF" nor
59 in /etc/default/slapd. Consequently, slapd will not be started.
60 EOF
61         exit 1
62 fi
63
64 # Pass the user and group to run under to slapd
65 if [  "$SLAPD_USER" ]; then
66         SLAPD_OPTIONS="-u $SLAPD_USER $SLAPD_OPTIONS"
67 fi
68
69 if [ "$SLAPD_GROUP" ]; then
70         SLAPD_OPTIONS="-g $SLAPD_GROUP $SLAPD_OPTIONS"
71 fi
72
73 # Tell the user that something went wrong and give some hints for
74 # resolving the problem.
75 report_failure() {
76         if [ -n "$reason" ]; then
77                 echo " - failed: "
78                 echo "$reason"
79         else
80                 echo " - failed."
81                 cat <<EOF
82 The operation failed but no output was produced. For hints on what went
83 wrong please refer to the system's logfiles (e.g. /var/log/syslog) or
84 try running the daemon in Debug mode like via "slapd -d 16383" (warning:
85 this will create copious output).
86 EOF
87
88                 if [ -n "$SLURPD_OPTIONS" -o \
89                      -n "$SLAPD_OPTIONS" -o \
90                      -n "$SLAPD_SERVICES" ]; then
91                         cat << EOF
92
93 Below, you can find the command line options used by this script to 
94 run slapd and slurpd. Do not forget to specify those options if you
95 want to look to debugging output:
96 EOF
97                         if [ -z "$SLAPD_SERVICES" ]; then
98                                 if [ -n "$SLAPD_OPTIONS" ]; then
99                                         echo "  slapd $SLAPD_OPTIONS"
100                                 fi
101                         else
102                                 echo "  slapd -h '$SLAPD_SERVICES' $SLAPD_OPTIONS"
103                         fi
104
105                         if [ "$SLURPD" = "yes" -a -n "$SLURPD_OPTIONS" ]; then
106                                 echo "  slurpd $SLURPD_OPTIONS"
107                         fi
108                 fi
109         fi
110 }
111
112 # Try to recover slapd database
113 try_fix_db() {
114         local dbdir failed bdb_envs
115
116         # db4.2-util is just recommended by slapd, so make sure it is
117         # available before trying to use it
118         if ! command -v $DB_RECOVER_CMD >/dev/null 2>&1; then
119                 echo -n " ($DB_RECOVER_CMD not found), "
120                 return 0
121         fi
122
123         bdb_envs=`find_bdb_envs`
124
125         # We care only about BDB environments
126         if [ -z "$bdb_envs" ]; then
127                 return 0
128         fi
129
130         # Make sure there is no slapcat and no slapd running as we might
131         # break the DB in that case
132         if pidof /usr/lib/slapd >/dev/null; then
133                 echo -n " (slapd running, no recovery), "
134                 return 0
135         fi
136
137         echo -n " running BDB recovery"
138         for dbdir in $bdb_envs; do
139                 reason="`$DB_RECOVER_CMD -eh $dbdir 2>&1`" || \
140                         db_recover_failed $dbdir
141         done
142         echo -n ","
143 }
144
145 # Find bdb environment dirs
146 find_bdb_envs() {
147         local d
148         for d in `sed -ne 's/^directory[[:space:]]\+"*\([^"]\+\).*/\1/p' \
149                         < "$SLAPD_CONF"`; do
150                 if [ -d "$d" -a -f "$d/objectClass.bdb" ]; then
151                         echo $d
152                 fi
153         done
154 }
155
156 # Inform the user that BDB recovery failed
157 db_recover_failed() {
158         local dbdir
159         dbdir="$1"
160
161         reason="`cat <<EOF
162 Automatic recovery of the OpenLDAP directory database in
163
164         $dbdir
165
166 failed. You will need to perform a manual recovery, possibly from backup.
167 The failed command was $DB_RECOVER_CMD -eh $dbdir. Output:
168
169 $reason
170 EOF`"
171         exit 1
172 }
173
174 # Start the slapd daemon and capture the error message if any to 
175 # $reason.
176 start_slapd() {
177         echo -n " slapd"
178         if [ -z "$SLAPD_SERVICES" ]; then
179                 reason="`start-stop-daemon --start --quiet --oknodo \
180                         --pidfile "$SLAPD_PIDFILE" \
181                         --exec /usr/sbin/slapd -- $SLAPD_OPTIONS 2>&1`"
182         else
183                 reason="`start-stop-daemon --start --quiet --oknodo \
184                         --pidfile "$SLAPD_PIDFILE" \
185                         --exec /usr/sbin/slapd -- -h "$SLAPD_SERVICES" $SLAPD_OPTIONS 2>&1`"
186         fi
187 }
188
189 # Start the slurpd daemon and capture the error message if any to
190 # $reason.
191 start_slurpd() {
192         if [ "$SLURPD_START" != yes ]; then
193                 return 0
194         fi
195         echo -n " slurpd"
196         reason="`start-stop-daemon --start --quiet --oknodo \
197                 --exec /usr/sbin/slurpd -- $SLURPD_OPTIONS 2>&1`"
198 }
199
200 # Stop the slapd daemon and capture the error message (if any) to
201 # $reason.
202 stop_slapd() {
203         echo -n " slapd"
204         reason="`start-stop-daemon --stop --quiet --oknodo --retry 10 \
205                 --pidfile "$SLAPD_PIDFILE" \
206                 --exec /usr/sbin/slapd 2>&1`"
207 }
208
209 # Stop the slurpd daemon and capture the error message (if any) to
210 # $reason.
211 stop_slurpd() {
212         if [ "$SLURPD_START" != yes ]; then
213                 return 0
214         fi
215         echo -n " slurpd"
216         reason="`start-stop-daemon --stop --quiet --oknodo --retry 10 \
217                 --exec /usr/sbin/slurpd 2>&1`"
218 }
219
220 # Start the OpenLDAP daemons
221 start() {
222         echo -n "Starting OpenLDAP:"
223         trap 'report_failure' 0
224         if [ "$TRY_BDB_RECOVERY" = "yes" ]; then
225                 try_fix_db
226         fi
227         start_slapd
228         start_slurpd
229         trap "-" 0
230         echo .
231 }
232
233 # Stop the OpenLDAP daemons
234 stop() {
235         echo -n "Stopping OpenLDAP:"
236         trap 'report_failure' 0
237         stop_slurpd
238         stop_slapd
239         trap "-" 0
240         echo .
241 }
242
243 case "$1" in
244   start)
245         start ;;
246   stop)
247         stop ;;
248   restart|force-reload)
249         stop
250         start
251         ;;
252   *)
253         echo "Usage: $0 {start|stop|restart|force-reload}"
254         exit 1
255         ;;
256 esac