summaryrefslogtreecommitdiffstats
path: root/packaging/Debian/debian/samba.postinst
blob: 772513f4c78b518f25c7d020321f8a41dbe3eca9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/bin/sh -e
#
# Post-installation script for the Samba package for Debian GNU/Linux
#
#

case "$1" in
	configure)
		# continue below
	;;

	abort-upgrade|abort-remove|abort-deconfigure)
		exit 0
	;;

	*)
		echo "postinst called with unknown argument \`$1'" >&2
		exit 0
	;;
esac

# Handle debconf
. /usr/share/debconf/confmodule

INITCONFFILE=/etc/default/samba

# We generate several files during the postinst, and we don't want
#	them to be readable only by root.
umask 022

# Generate configuration file if it does not exist, using default values.
[ -r "${INITCONFFILE}" ] || {
	echo Generating ${INITCONFFILE}... >&2
	cat >${INITCONFFILE} <<'EOFMAGICNUMBER1234'
# Defaults for samba initscript
# sourced by /etc/init.d/samba
# installed at /etc/default/samba by the maintainer scripts
#

#
# This is a POSIX shell fragment
#

# How should Samba (nmbd and smbd) run? Possible values are "daemons"
#	or "inetd".
RUN_MODE=""
EOFMAGICNUMBER1234
}

# --- Begin of FHS migration code ---

# Starting with Samba 2.2.3-4 the WINS database, the browse
#	database and other important run-time files are stored in
#	FHS-compliant directories. The following code takes care of
#	moving the files in the old directories (/var/samba/ and
#	/var/state/samba) to the new FHS-compliant directories.

if [ -d /var/samba/ ]; then
	mv /var/samba/* /var/lib/samba/ 2>/dev/null || true
	rmdir /var/samba/
fi

# Default for anything we don't know about (see next two 'for' loops)
#	is /var/lib/samba -- guaranteed not to accidentally tromp on any 
#	files the admin thought were safe.
if [ -d /var/state/samba ]; then
	mv /var/state/samba/* /var/lib/samba/ 2>/dev/null || true
	rmdir /var/state/samba/

	# It's not FHS, and it's probably our fault this is here,
	# so delete it if we can.
	rmdir /var/state/ 2> /dev/null || true
fi

# All these files are now placed in their respective FHS-compliant
#	directories.  Separate out the individual files accordingly.
for F in browse.dat printing.tdb winbindd_cache.tdb
do
	if [ -e /var/lib/samba/"$F" ]; then
		mv /var/lib/samba/"$F" /var/cache/samba/
	fi
done

for F in brlock.tdb connections.tdb locking.tdb messages.tdb nmbd.pid \
         sessionid.tdb smbd.pid unexpected.tdb 
do
	if [ -e /var/lib/samba/"$F" ]; then
		mv /var/lib/samba/"$F" /var/run/samba/
	fi
done

# Beginning with Samba 2.2.5-1, we also move the domain secrets file
# to a more suitable location, since no one really edits this by hand.
if [ -e /etc/samba/secrets.tdb -a ! -e /var/lib/samba/secrets.tdb ]
then
	mv /etc/samba/secrets.tdb /var/lib/samba/
fi

# --- End of FHS migration code ---

# ------------------------- Debconf questions start ---------------------

# Run Samba as daemons or from inetd?
db_get samba/run_mode || true
RUN_MODE="${RET}"

TMPFILE=`mktemp -q /tmp/samba.config.XXXXXX`
sed -e "s/^[[:space:]]*RUN_MODE[[:space:]]*=.*/RUN_MODE=\"${RUN_MODE}\"/" \
        < ${INITCONFFILE} >${TMPFILE}
mv -f ${TMPFILE} ${INITCONFFILE}

# Generate a smbpasswd file?
db_get samba/generate_smbpasswd || true
GENERATE_SMBPASSWD="${RET}"

# Done with debconf now.
db_stop

if [ "${GENERATE_SMBPASSWD}" = "true" -a ! -e /etc/samba/smbpasswd ]; then
	getent passwd | /usr/sbin/mksmbpasswd > /etc/samba/smbpasswd
	chmod 600 /etc/samba/smbpasswd 
fi

# ------------------------- Debconf questions end ---------------------

# We always run /etc/init.d/samba, even if we run Samba from inetd.
#	The init.d script takes care of handling the conflict of running
#	from inetd or as daemons.
update-rc.d samba defaults 20 19 > /dev/null

# We want to add these entries to inetd.conf commented out. Otherwise
#	UDP traffic could make inetd to start nmbd or smbd right during
#	the configuration stage.
if [ "$1" = "configure" -a -z "$2" ]; then
	update-inetd --add "#<off># netbios-ns	dgram	udp	wait	root 	/usr/sbin/tcpd	/usr/sbin/nmbd -a"
	update-inetd --add "#<off># netbios-ssn	stream	tcp	nowait	root	/usr/sbin/tcpd	/usr/sbin/smbd"
fi

if [ "$RUN_MODE" = "daemons" ]; then
	update-inetd --disable netbios-ns
	update-inetd --disable netbios-ssn
else
	update-inetd --enable netbios-ns
	update-inetd --enable netbios-ssn
fi

# Start Samba: we don't want to call /etc/init.d/samba if we are
# running from inetd because a nasty help message would be printed out.

# Run the init script if this is a first-time install, or if it's an
# upgrade and Samba was running before, _and_ we're not running from inetd.
if [ \( -z "$2" -o ! -f /tmp/samba-was-not-running \) -a \
		"$RUN_MODE" = "daemons" ]; then
	# Check the script is executable before running it.
	[ -x /etc/init.d/samba ] && /etc/init.d/samba start
fi

# This check is a safety net: the /etc/samba/smbpasswd file must have
#	permissions 600.
if [ -f /etc/samba/smbpasswd ]; then
	chmod 600 /etc/samba/smbpasswd
fi

# Do the same check for /var/backup/smbpasswd.bak, just in case.
if [ -f /var/backups/smbpasswd.bak ]; then
	chmod 600 /var/backups/smbpasswd.bak
fi

# Delete old /etc/samba/debian_config file, which is not used anymore
#	now that we are using debconf.
rm -f /etc/samba/debian_config

# Move old log files to the new location of Samba's log files
mv -f /var/log/nmb* /var/log/samba/ 2> /dev/null || true
mv -f /var/log/smb* /var/log/samba/ 2> /dev/null || true

# Do this last, so we don't accidentally start the daemons if something 
# else in the script fails above.
rm -f /tmp/samba-was-not-running

#DEBHELPER#

exit 0