/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to init.d-mandos

  • Committer: Teddy Hogeborn
  • Date: 2019-08-02 22:16:53 UTC
  • Revision ID: teddy@recompile.se-20190802221653-ic1iko9hbefzwsk7
Fix bug in server Debian package: Fails to start on first install

There has been a very long-standing bug where installation of the
server (the "mandos" Debian package) would fail to start the server
properly right after installation.  It would work on manual (re)start
after installation, or after reboot, and even after package purge and
reinstall, it would then work the first time.  The problem, it turns
out, is when the new "_mandos" user (and corresponding group) is
created, the D-Bus server is not reloaded, and is therefore not aware
of that user, and does not recognize the user and group name in the
/etc/dbus-1/system.d/mandos.conf file.  The Mandos server, when it
tries to start and access the D-Bus, is then not permitted to connect
to its D-Bus bus name, and disables D-Bus use as a fallback measure;
i.e. the server works, but it is not controllable via D-Bus commands
(via mandos-ctl or mandos-monitor).  The next time the D-Bus daemon is
reloaded for any reason, the new user & group would become visible to
the D-Bus daemon and after that, any restart of the Mandos server
would succeed and it would bind to its D-Bus name properly, and
thereby be visible and controllable by mandos-ctl & mandos-monitor.
This was mostly invisible when using sysvinit, but systemd makes the
problem visible since the systemd service file for the Mandos server
is configured to not consider the Mandos server "started" until the
D-Bus name has been bound; this makes the starting of the service wait
for 90 seconds and then fail with a timeout error.

Fixing this should also make the Debian CI autopkgtest tests work.

* debian/mandos.postinst (configure): After creating (or renaming)
                                      user & group, reload D-Bus
                                      daemon (if present).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /bin/sh
 
2
### BEGIN INIT INFO
 
3
# Provides:          mandos
 
4
# Required-Start:    $remote_fs $syslog avahi-daemon
 
5
# Required-Stop:     $remote_fs $syslog avahi-daemon
 
6
# Default-Start:     2 3 4 5
 
7
# Default-Stop:      0 1 6
 
8
# Short-Description: Mandos server
 
9
# Description:       Server of encrypted passwords to Mandos clients
 
10
### END INIT INFO
 
11
 
 
12
# Author: Teddy Hogeborn <teddy@recompile.se>
 
13
# Author: Björn Påhlsson <belorn@recompile.se>
 
14
 
 
15
# Do NOT "set -e"
 
16
 
 
17
# PATH should only include /usr/* if it runs after the mountnfs.sh script
 
18
PATH=/sbin:/usr/sbin:/bin:/usr/bin
 
19
DESC="Mandos root file system password server"
 
20
NAME=mandos
 
21
DAEMON=/usr/sbin/$NAME
 
22
DAEMON_ARGS=""
 
23
if [ -d /run/. ]; then
 
24
    PIDFILE=/run/$NAME.pid
 
25
else
 
26
    PIDFILE=/var/run/$NAME.pid
 
27
fi
 
28
SCRIPTNAME=/etc/init.d/$NAME
 
29
 
 
30
# Exit if the package is not installed
 
31
[ -x "$DAEMON" ] || exit 0
 
32
 
 
33
# Read configuration variable file if it is present
 
34
[ -r /etc/default/$NAME ] && . /etc/default/$NAME
 
35
 
 
36
if [ -n "$CONFIGDIR" ]; then
 
37
   DAEMON_ARGS="$DAEMON_ARGS --configdir $CONFIGDIR"
 
38
fi
 
39
 
 
40
# Load the VERBOSE setting and other rcS variables
 
41
. /lib/init/vars.sh
 
42
 
 
43
# Define LSB log_* functions.
 
44
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
 
45
# and status_of_proc is working.
 
46
. /lib/lsb/init-functions
 
47
 
 
48
#
 
49
# Function that starts the daemon/service
 
50
#
 
51
do_start()
 
52
{
 
53
        # Return
 
54
        #   0 if daemon has been started
 
55
        #   1 if daemon was already running
 
56
        #   2 if daemon could not be started
 
57
        start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
 
58
                || return 1
 
59
        start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- \
 
60
                $DAEMON_ARGS \
 
61
                || return 2
 
62
        # Add code here, if necessary, that waits for the process to be ready
 
63
        # to handle requests from services started subsequently which depend
 
64
        # on this one.  As a last resort, sleep for some time.
 
65
}
 
66
 
 
67
#
 
68
# Function that stops the daemon/service
 
69
#
 
70
do_stop()
 
71
{
 
72
        # Return
 
73
        #   0 if daemon has been stopped
 
74
        #   1 if daemon was already stopped
 
75
        #   2 if daemon could not be stopped
 
76
        #   other if a failure occurred
 
77
        start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
 
78
        RETVAL="$?"
 
79
        [ "$RETVAL" = 2 ] && return 2
 
80
        # Wait for children to finish too if this is a daemon that forks
 
81
        # and if the daemon is only ever run from this initscript.
 
82
        # If the above conditions are not satisfied then add some other code
 
83
        # that waits for the process to drop all resources that could be
 
84
        # needed by services started subsequently.  A last resort is to
 
85
        # sleep for some time.
 
86
        start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
 
87
        [ "$?" = 2 ] && return 2
 
88
        # Many daemons don't delete their pidfiles when they exit.
 
89
        rm -f $PIDFILE
 
90
        return "$RETVAL"
 
91
}
 
92
 
 
93
#
 
94
# Function that sends a SIGHUP to the daemon/service
 
95
#
 
96
do_reload() {
 
97
        #
 
98
        # If the daemon can reload its configuration without
 
99
        # restarting (for example, when it is sent a SIGHUP),
 
100
        # then implement that here.
 
101
        #
 
102
        start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
 
103
        return 0
 
104
}
 
105
 
 
106
case "$1" in
 
107
  start)
 
108
        [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
 
109
        do_start
 
110
        case "$?" in
 
111
                0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
 
112
                2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
 
113
        esac
 
114
        ;;
 
115
  stop)
 
116
        [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
 
117
        do_stop
 
118
        case "$?" in
 
119
                0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
 
120
                2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
 
121
        esac
 
122
        ;;
 
123
  status)
 
124
        status_of_proc "$DAEMON" "$NAME" -p "$PIDFILE" && exit 0 || exit $?
 
125
        ;;
 
126
  #reload|force-reload)
 
127
        #
 
128
        # If do_reload() is not implemented then leave this commented out
 
129
        # and leave 'force-reload' as an alias for 'restart'.
 
130
        #
 
131
        #log_daemon_msg "Reloading $DESC" "$NAME"
 
132
        #do_reload
 
133
        #log_end_msg $?
 
134
        #;;
 
135
  restart|force-reload)
 
136
        #
 
137
        # If the "reload" option is implemented then remove the
 
138
        # 'force-reload' alias
 
139
        #
 
140
        log_daemon_msg "Restarting $DESC" "$NAME"
 
141
        do_stop
 
142
        case "$?" in
 
143
          0|1)
 
144
                do_start
 
145
                case "$?" in
 
146
                        0) log_end_msg 0 ;;
 
147
                        1) log_end_msg 1 ;; # Old process is still running
 
148
                        *) log_end_msg 1 ;; # Failed to start
 
149
                esac
 
150
                ;;
 
151
          *)
 
152
                # Failed to stop
 
153
                log_end_msg 1
 
154
                ;;
 
155
        esac
 
156
        ;;
 
157
  *)
 
158
        #echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
 
159
        echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
 
160
        exit 3
 
161
        ;;
 
162
esac
 
163
 
 
164
: