Difference between revisions of "Managing the Samba AD DC Service Using an Init Script"

From SambaWiki
(→‎Debian Systems: add note to fix debian init script looking for /etc/samba/smb.conf)
m
 
(17 intermediate revisions by 8 users not shown)
Line 1: Line 1:
  +
<noinclude>= Introduction =
This is a topic which pops every so often -- ''where are the Init scripts for Samba4?'' The problem is that init scripts are very distribution specific. The HOWTO states, "Samba4 alpha13 doesn't yet have init scripts included for each platform, but making one for your platform should not be difficult." Well, they may not be rocket science, but not everyone knows how to build a robust startup script and then integrate it with their particular startup infrastructure. This gets even more weird when distributions like Fedora radically overhaul their approach to init. (SysV to "systemd")
 
   
  +
The following describes how to use an init script to manage the Samba Active Directory (AD) domain controller (DC) service. Depending on your operating system, the location of the init script, its content, and the procedures how to manage the service can be different. For details, see your operating system's documentation.
The intent of this page is to provide a sample of at least a few init scripts, listed by their distribution family (eg., Debian based systems and Red Hat/Fedora).
 
   
  +
{{Imbox
== Red Hat/Fedora based systems ==
 
  +
| type = important
For SysV style service init scripts, Red Hat puts the init scripts in the /etc/rc.d/init.d directory, and then links to these scripts from the various run level directories (eg, link in /etc/rc3.d/S80samba4 -> ../rc.d/init.d/samba4)
 
  +
| text = If you operating system uses a different system to manage services, such as <code>systemd</code>, see [[Managing_the_Samba_AD_DC_Service|Managing the Samba AD DC Service]].
  +
}}</noinclude>
   
Fedora has gone to a systemd based startup for Init. You can still use SysV style scripts such as this one, and configure the automatic startup of the Samba4 server ad different run levels through the "chkconfig" tool.
 
   
#! /bin/bash
 
#
 
# samba4 Bring up/down samba4 service
 
#
 
# chkconfig: - 90 10
 
# description: Activates/Deactivates all samba4 interfaces configured to \
 
# start at boot time.
 
#
 
### BEGIN INIT INFO
 
# Provides:
 
# Should-Start:
 
# Short-Description: Bring up/down samba4
 
# Description: Bring up/down samba4
 
### END INIT INFO
 
# Source function library.
 
. /etc/init.d/functions
 
 
if [ -f /etc/sysconfig/samba4 ]; then
 
. /etc/sysconfig/samba4
 
fi
 
 
CWD=$(pwd)
 
prog="samba4"
 
 
start() {
 
# Attach irda device
 
echo -n $"Starting $prog: "
 
/usr/local/samba/sbin/samba
 
sleep 2
 
if ps ax | grep -v "grep" | grep -q /samba/sbin/samba ; then success $"samba4 startup"; else failure $"samba4 startup"; fi
 
echo
 
}
 
stop() {
 
# Stop service.
 
echo -n $"Shutting down $prog: "
 
killall samba
 
sleep 2
 
if ps ax | grep -v "grep" | grep -q /samba/sbin/samba ; then failure $"samba4 shutdown"; else success $"samba4 shutdown"; fi
 
echo
 
}
 
status() {
 
/usr/local/samba/sbin/samba --show-build
 
}
 
 
# See how we were called.
 
case "$1" in
 
start)
 
start
 
;;
 
stop)
 
stop
 
;;
 
status)
 
status irattach
 
;;
 
restart|reload)
 
stop
 
start
 
;;
 
*)
 
echo $"Usage: $0 {start|stop|restart|status}"
 
exit 1
 
esac
 
 
exit 0
 
   
   
== Debian Systems ==
 
   
1) Retrieve the init script
+
= Creating the Init Script =
   
  +
== Red Hat Enterprise Linux 6 ==
To retrieve the Debian init script, run either:
 
   
  +
{{Imbox
$ bzr cat http://bzr.debian.org/bzr/pkg-samba/samba4/unstable/debian/samba4.init > /etc/init.d/samba4
 
  +
| type = note
  +
| text = On Red Hat Enterprise Linux later than version 6, use <code>systemd</code> to manage the Samba service. For details, see [[Managing_the_Samba_AD_DC_Service_Using_Systemd|Managing the Samba AD DC Service Using Systemd]].
  +
}}
   
  +
* Create the <code>/etc/init.d/samba-ad-dc</code> file with the following content:
Or if you don't have bzr:
 
   
  +
#!/bin/bash
$ wget http://anonscm.debian.org/loggerhead/pkg-samba/samba4/unstable/download/head:/1833%40fc4039ab-9d04-0410-8cac-899223bdd6b0:trunk%252Fsamba4:debian%252Fsamba4.init/samba4.init -O /etc/init.d/samba4
 
  +
#
  +
# samba-ad-dc This shell script takes care of starting and stopping
  +
# samba AD daemons.
  +
#
  +
# chkconfig: - 58 74
  +
# description: Samba Active Directory Domain Controller
  +
  +
### BEGIN INIT INFO
  +
# Provides: samba-ad-dc
  +
# Required-Start: $network $local_fs $remote_fs
  +
# Required-Stop: $network $local_fs $remote_fs
  +
# Should-Start: $syslog $named
  +
# Should-Stop: $syslog $named
  +
# Short-Description: start and stop samba-ad-dc
  +
# Description: Samba Active Directory Domain Controller
  +
### END INIT INFO
  +
  +
# Source function library.
  +
. /etc/init.d/functions
  +
  +
# Source networking configuration.
  +
. /etc/sysconfig/network
  +
  +
prog=samba
  +
prog_dir=/usr/local/samba/sbin/
  +
lockfile=/var/lock/subsys/$prog
  +
  +
start() {
  +
[ "$NETWORKING" = "no" ] && exit 1
  +
echo -n $"Starting Samba AD DC: "
  +
daemon $prog_dir/$prog -D
  +
RETVAL=$?
  +
echo
  +
[ $RETVAL -eq 0 ] && touch $lockfile
  +
return $RETVAL
  +
}
  +
  +
stop() {
  +
[ "$EUID" != "0" ] && exit 4
  +
echo -n $"Shutting down Samba AD DC: "
  +
killproc $prog_dir/$prog
  +
RETVAL=$?
  +
echo
  +
[ $RETVAL -eq 0 ] && rm -f $lockfile
  +
return $RETVAL
  +
}
  +
  +
case "$1" in
  +
start)
  +
start
  +
;;
  +
stop)
  +
stop
  +
;;
  +
status)
  +
status $prog
  +
;;
  +
restart)
  +
stop
  +
start
  +
;;
  +
*)
  +
echo $"Usage: $0 {start|stop|status|restart}"
  +
exit 2
  +
esac
   
  +
* Make the script executeable:
2) (Optional) Update the paths where Samba is installed
 
   
  +
# chmod 755 /etc/init.d/samba-ad-dc
The Debian package assumes that Samba is installed in /usr. If you've installed it in the default location (/usr/local/samba) instead, run:
 
   
$ sed -i 's|/usr/sbin|/usr/local/samba/sbin|g' /etc/init.d/samba4
 
   
Likewise the Debian package assumes you'll use /etc/samba/smb.conf for the configuration file. If you're using the default location and build run:
 
   
  +
== Debian ==
$ sed -i 's|/etc/samba|/usr/local/samba/etc|g' /etc/init.d/samba4
 
   
  +
* Create the <code>/etc/init.d/samba-ad-dc</code> file with the following content:
 
3) Make the init script executable
 
 
Make the init script executable by running:
 
 
$ chmod 755 /etc/init.d/samba4
 
 
4) Enable the script at startup
 
 
$ update-rc.d samba4 defaults
 
 
== Upstart Systems (such as Ubuntu) ==
 
 
Ubuntu uses the upstart system. To retrieve the upstart config file, run:
 
 
$ bzr cat http://bzr.debian.org/bzr/pkg-samba/samba4/unstable/debian/samba4.upstart > /etc/init/samba4.conf
 
 
The upstart file doesn't appear to be there any more...so here's one that appears to work.<br>
 
It normally goes here: /etc/init/samba4.conf <br>
 
Change the exec line to point to where ever the samba executable is. By default it's here: /usr/local/samba/sbin/
 
 
description "SMB/CIFS File and Active Directory Server"
 
author "Jelmer Vernooij <jelmer@ubuntu.com>"
 
start on (local-filesystems and net-device-up)
 
stop on runlevel [!2345]
 
expect fork
 
normal exit 0
 
pre-start script
 
[ -r /etc/default/samba4 ] && . /etc/default/samba4
 
install -o root -g root -m 755 -d /var/run/samba
 
install -o root -g root -m 755 -d /var/log/samba
 
end script
 
exec /usr/local/samba/sbin/samba -D
 
 
 
The following init script will start the samba a source installed samba NON AD server:
 
   
 
#!/bin/sh
 
#!/bin/sh
 
 
 
### BEGIN INIT INFO
 
### BEGIN INIT INFO
# Provides: samba
+
# Provides: samba-ad-dc
 
# Required-Start: $network $local_fs $remote_fs
 
# Required-Start: $network $local_fs $remote_fs
 
# Required-Stop: $network $local_fs $remote_fs
 
# Required-Stop: $network $local_fs $remote_fs
 
# Default-Start: 2 3 4 5
 
# Default-Start: 2 3 4 5
 
# Default-Stop: 0 1 6
 
# Default-Stop: 0 1 6
  +
# Short-Description: start Samba daemons for the AD DC
# Should-Start: slapd
 
# Should-Stop: slapd
 
# Short-Description: start Samba daemons (nmbd and smbd)
 
 
### END INIT INFO
 
### END INIT INFO
 
 
# Description of this script:
 
#
 
# This script comes initially from a Debian Squeeze machine on
 
# which samba 3.x was installed with "apt-get install samba". The script
 
# was modified/adjusted so it points to the correct paths of a default
 
# samba4 installation (/usr/local/samba).
 
 
#
 
#
  +
# Start/stops the Samba daemon (samba).
# Installation instructions:
 
  +
# Adapted from the Samba 3 packages.
# (1) copy the content of this script into your clipboard or download it
 
  +
#
# (2) save the content into /etc/init.d/samba of your samba4 host.
 
# (3) execute "chmod +x /etc/init.d/samba" to have the script executable
 
# (4) execute "update-rc.d samba defaults" to install auto-start function.
 
# smbd+nmbd will automatically being started after earch system start/reboot
 
#
 
# Modified by local@#samba~irc.freenode.net at 06th March 2013
 
# The script was successfully tested on Debian GNU/Linux Squeeze+Wheezy
 
 
# Defaults
 
RUN_MODE="daemons"
 
 
 
  +
PATH=/usr/local/samba/sbin:/usr/local/samba/bin:$PATH
# Reads config file (will override defaults above)
 
[ -r /etc/default/samba ] && . /etc/default/samba
 
 
 
 
PIDDIR=/usr/local/samba/var/run
 
PIDDIR=/usr/local/samba/var/run
NMBDPID=$PIDDIR/nmbd.pid
+
SAMBAPID=$PIDDIR/samba.pid
SMBDPID=$PIDDIR/smbd.pid
 
 
 
 
# clear conflicting settings from the environment
 
# clear conflicting settings from the environment
 
unset TMPDIR
 
unset TMPDIR
 
 
# See if the daemons are there
+
# See if the daemon and the config file are there
test -x /usr/local/samba/sbin/nmbd -a -x /usr/local/samba/sbin/smbd || exit 0
+
test -x /usr/local/samba/sbin/samba -a -r /usr/local/samba/etc/smb.conf || exit 0
 
 
 
. /lib/lsb/init-functions
 
. /lib/lsb/init-functions
Line 184: Line 131:
 
case "$1" in
 
case "$1" in
 
start)
 
start)
  +
SERVER_ROLE=`samba-tool testparm --parameter-name="server role" 2>/dev/null | tail -1`
log_daemon_msg "Starting Samba daemons"
 
# Make sure we have our PIDDIR, even if it's on a tmpfs
+
if [ "$SERVER_ROLE" != "active directory domain controller" ]; then
install -o root -g root -m 755 -d $PIDDIR
+
exit 0
 
NMBD_DISABLED=`testparm -s --parameter-name='disable netbios' 2>/dev/null`
 
if [ "$NMBD_DISABLED" != 'Yes' ]; then
 
log_progress_msg "nmbd"
 
if ! start-stop-daemon --start --quiet --oknodo --exec /usr/local/samba/sbin/nmbd -- -D
 
then
 
log_end_msg 1
 
exit 1
 
fi
 
 
fi
 
fi
 
 
if [ "$RUN_MODE" != "inetd" ]; then
+
# CVE-2013-4475
log_progress_msg "smbd"
+
KEYFILE=/usr/local/samba/private/tls/key.pem
if ! start-stop-daemon --start --quiet --oknodo --exec /usr/local/samba/sbin/smbd -- -D; then
+
if [ -e $KEYFILE ]; then
log_end_msg 1
+
KEYPERMS=`stat -c %a $KEYFILE`
exit 1
+
if [ "$KEYPERMS" != "600" ]; then
fi
+
echo "wrong permission on $KEYFILE, must be 600"
fi
+
echo "samba will not start (CVE-2013-4475)"
  +
echo "Removing all tls .pem files will cause an auto-regeneration with the correct permissions."
  +
exit 1
  +
fi
  +
fi
 
 
log_end_msg 0
+
log_daemon_msg "Starting Samba AD DC daemon" "samba"
;;
+
# Make sure we have our PIDDIR, even if it's on a tmpfs
  +
install -o root -g root -m 755 -d $PIDDIR
stop)
 
  +
log_daemon_msg "Stopping Samba daemons"
 
  +
if ! start-stop-daemon --start --quiet --oknodo --exec /usr/local/samba/sbin/samba -- -D; then
log_progress_msg "nmbd"
 
  +
log_end_msg 1
  +
exit 1
  +
fi
 
 
start-stop-daemon --stop --quiet --pidfile $NMBDPID
+
log_end_msg 0
# Wait a little and remove stale PID file
+
;;
sleep 1
+
stop)
if [ -f $NMBDPID ] && ! ps h `cat $NMBDPID` > /dev/null
+
log_daemon_msg "Stopping Samba AD DC daemon" "samba"
then
 
# Stale PID file (nmbd was succesfully stopped),
 
# remove it (should be removed by nmbd itself IMHO.)
 
rm -f $NMBDPID
 
fi
 
 
 
if [ "$RUN_MODE" != "inetd" ]; then
+
start-stop-daemon --stop --quiet --pidfile $SAMBAPID
log_progress_msg "smbd"
+
# Wait a little and remove stale PID file
start-stop-daemon --stop --quiet --pidfile $SMBDPID
+
sleep 1
# Wait a little and remove stale PID file
+
if [ -f $SAMBAPID ] && ! ps h `cat $SAMBAPID` > /dev/null
sleep 1
+
then
if [ -f $SMBDPID ] && ! ps h `cat $SMBDPID` > /dev/null
+
# Stale PID file (samba was succesfully stopped),
then
+
# remove it (should be removed by samba itself IMHO.)
# Stale PID file (nmbd was succesfully stopped),
+
rm -f $SAMBAPID
  +
fi
# remove it (should be removed by smbd itself IMHO.)
 
rm -f $SMBDPID
 
fi
 
fi
 
 
 
log_end_msg 0
+
log_end_msg 0
 
 
;;
+
;;
  +
restart|force-reload)
 
reload)
+
$0 stop
  +
sleep 1
log_daemon_msg "Reloading /usr/local/samba/etc/smb.conf" "smbd only"
 
  +
$0 start
 
start-stop-daemon --stop --signal HUP --pidfile $SMBDPID
+
;;
  +
status)
 
log_end_msg 0
+
status_of_proc -p $SAMBAPID /usr/local/samba/sbin/samba samba
;;
+
exit $?
restart|force-reload)
+
;;
$0 stop
+
*)
  +
echo "Usage: /etc/init.d/samba-ad-dc {start|stop|restart|force-reload|status}"
sleep 1
 
$0 start
+
exit 1
;;
+
;;
status)
 
status="0"
 
NMBD_DISABLED=`testparm -s --parameter-name='disable netbios' 2>/dev/null`
 
if [ "$NMBD_DISABLED" != "Yes" ]; then
 
status_of_proc -p $NMBDPID /usr/local/samba/sbin/nmbd nmbd || status=$?
 
fi
 
if [ "$RUN_MODE" != "inetd" ]; then
 
status_of_proc -p $SMBDPID /usr/local/samba/sbin/smbd smbd || status=$?
 
fi
 
if [ "$NMBD_DISABLED" = "Yes" -a "$RUN_MODE" = "inetd" ]; then
 
status="4"
 
fi
 
exit $status
 
;;
 
*)
 
echo "Usage: /etc/init.d/samba {start|stop|reload|restart|force-reload|status}"
 
exit 1
 
;;
 
 
esac
 
esac
 
 
 
exit 0
 
exit 0
  +
  +
  +
* If necessary, update the locations to the <code>samba</code> service, the <code>samba-tool</code> utility, and the <code>smb.conf</code> file in the <code>/etc/init.d/samba-ad-dc</code> file.
  +
  +
* Make the script executeable:
  +
  +
# chmod 755 /etc/init.d/samba-ad-dc
  +
  +
  +
  +
  +
  +
= Managing the Samba AD DC Service =
  +
  +
The following assumes that the Samba Active Directory (AD) domain controller (DC) service is managed by the <code>/etc/init.d/samba-ad-dc</code> init script. If you have not created the script manually, see your operating system's documentation for the name of the Samba AD DC service.
  +
  +
{{Imbox
  +
| type = note
  +
| text = Depending on your operating system, there can be different ways to enable or disable a service. See your operating system's documentation for details.
  +
}}
  +
  +
  +
  +
== Enabling and Disabling the Samba AD DC Service ==
  +
  +
To enable the Samba Active Directory (AD) domain controller (DC) service to start automatically when the system boots, enter:
  +
  +
== Red Hat Enterprise Linux 6 ==
  +
  +
# chkconfig samba-ad-dc enable
  +
  +
To disable the automatic start of the Samba AD DC service, enter:
  +
  +
# chkconfig samba-ad-dc disable
  +
  +
  +
== Debian ==
  +
  +
# update-rc.d samba-ad-dc defaults
  +
  +
To disable the automatic start of the Samba AD DC service, enter:
  +
  +
# update-rc.d -f samba-ad-dc remove
  +
  +
  +
  +
== Manually Starting and Stopping the Samba AD DC Service ==
  +
  +
To manually start the Samba Active Directory (AD) domain controller (DC) service, enter:
  +
  +
# service start samba-ad-dc
  +
  +
To manually stop the Samba AD DC service, enter:
  +
  +
# service stop samba-ad-dc

Latest revision as of 13:36, 22 September 2020

Introduction

The following describes how to use an init script to manage the Samba Active Directory (AD) domain controller (DC) service. Depending on your operating system, the location of the init script, its content, and the procedures how to manage the service can be different. For details, see your operating system's documentation.



Creating the Init Script

Red Hat Enterprise Linux 6

  • Create the /etc/init.d/samba-ad-dc file with the following content:
#!/bin/bash
#
# samba-ad-dc	This shell script takes care of starting and stopping
# 		samba AD daemons.
#
# chkconfig: - 58 74
# description: Samba Active Directory Domain Controller

### BEGIN INIT INFO
# Provides: samba-ad-dc
# Required-Start: $network $local_fs $remote_fs
# Required-Stop: $network $local_fs $remote_fs
# Should-Start: $syslog $named
# Should-Stop: $syslog $named
# Short-Description: start and stop samba-ad-dc
# Description: Samba Active Directory Domain Controller
### END INIT INFO

# Source function library.
. /etc/init.d/functions
 
# Source networking configuration.
. /etc/sysconfig/network
 
prog=samba
prog_dir=/usr/local/samba/sbin/
lockfile=/var/lock/subsys/$prog
 
start() {
	[ "$NETWORKING" = "no" ] && exit 1
	echo -n $"Starting Samba AD DC: "
	daemon $prog_dir/$prog -D
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && touch $lockfile
	return $RETVAL
}

 stop() {
	[ "$EUID" != "0" ] && exit 4
	echo -n $"Shutting down Samba AD DC: "
	killproc $prog_dir/$prog
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && rm -f $lockfile
	return $RETVAL
}
 
case "$1" in
start)
	start
	;;
stop)
	stop
	;;
status)
	status $prog
	;;
restart)
	stop
	start
	;;
*)
	echo $"Usage: $0 {start|stop|status|restart}"
	exit 2
esac
  • Make the script executeable:
# chmod 755 /etc/init.d/samba-ad-dc


Debian

  • Create the /etc/init.d/samba-ad-dc file with the following content:
#!/bin/sh

### BEGIN INIT INFO
# Provides:          samba-ad-dc
# Required-Start:    $network $local_fs $remote_fs
# Required-Stop:     $network $local_fs $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: start Samba daemons for the AD DC
### END INIT INFO

#
# Start/stops the Samba daemon (samba).
# Adapted from the Samba 3 packages.
#

PATH=/usr/local/samba/sbin:/usr/local/samba/bin:$PATH

PIDDIR=/usr/local/samba/var/run
SAMBAPID=$PIDDIR/samba.pid

# clear conflicting settings from the environment
unset TMPDIR

# See if the daemon and the config file are there
test -x /usr/local/samba/sbin/samba -a -r /usr/local/samba/etc/smb.conf || exit 0

. /lib/lsb/init-functions

case "$1" in
        start)
                SERVER_ROLE=`samba-tool testparm --parameter-name="server role"  2>/dev/null | tail -1`
                if [ "$SERVER_ROLE" != "active directory domain controller" ]; then
                    exit 0
                fi

                # CVE-2013-4475
                KEYFILE=/usr/local/samba/private/tls/key.pem
                if [ -e $KEYFILE ]; then
                    KEYPERMS=`stat -c %a $KEYFILE`
                    if [ "$KEYPERMS" != "600" ]; then
                        echo "wrong permission on $KEYFILE, must be 600"
                        echo "samba will not start (CVE-2013-4475)"
                        echo "Removing all tls .pem files will cause an auto-regeneration with the correct permissions."
                        exit 1
                    fi
               fi

               log_daemon_msg "Starting Samba AD DC daemon" "samba"
               # Make sure we have our PIDDIR, even if it's on a tmpfs
               install -o root -g root -m 755 -d $PIDDIR
 
               if ! start-stop-daemon --start --quiet --oknodo --exec /usr/local/samba/sbin/samba -- -D; then
                   log_end_msg 1
                   exit 1
               fi

               log_end_msg 0
               ;;
       stop)
               log_daemon_msg "Stopping Samba AD DC daemon" "samba"

              start-stop-daemon --stop --quiet --pidfile $SAMBAPID
               # Wait a little and remove stale PID file
               sleep 1
               if [ -f $SAMBAPID ] && ! ps h `cat $SAMBAPID` > /dev/null
               then
                   # Stale PID file (samba was succesfully stopped),
                   # remove it (should be removed by samba itself IMHO.)
                   rm -f $SAMBAPID
               fi

              log_end_msg 0

               ;;
       restart|force-reload)
               $0 stop
               sleep 1
               $0 start
               ;;
       status)
               status_of_proc -p $SAMBAPID /usr/local/samba/sbin/samba samba
               exit $?
               ;;
       *)
               echo "Usage: /etc/init.d/samba-ad-dc {start|stop|restart|force-reload|status}"
               exit 1
               ;;
esac

exit 0


  • If necessary, update the locations to the samba service, the samba-tool utility, and the smb.conf file in the /etc/init.d/samba-ad-dc file.
  • Make the script executeable:
# chmod 755 /etc/init.d/samba-ad-dc



Managing the Samba AD DC Service

The following assumes that the Samba Active Directory (AD) domain controller (DC) service is managed by the /etc/init.d/samba-ad-dc init script. If you have not created the script manually, see your operating system's documentation for the name of the Samba AD DC service.


Enabling and Disabling the Samba AD DC Service

To enable the Samba Active Directory (AD) domain controller (DC) service to start automatically when the system boots, enter:

Red Hat Enterprise Linux 6

# chkconfig samba-ad-dc enable

To disable the automatic start of the Samba AD DC service, enter:

# chkconfig samba-ad-dc disable


Debian

# update-rc.d samba-ad-dc defaults

To disable the automatic start of the Samba AD DC service, enter:

# update-rc.d -f samba-ad-dc remove


Manually Starting and Stopping the Samba AD DC Service

To manually start the Samba Active Directory (AD) domain controller (DC) service, enter:

# service start samba-ad-dc

To manually stop the Samba AD DC service, enter:

# service stop samba-ad-dc