Merge Samba3 and Samba4 together
[sfrench/samba-autobuild/.git] / packaging / Debian / debian-sarge / samba.postinst
1 #!/bin/sh -e
2 #
3 # Post-installation script for the Samba package for Debian GNU/Linux
4 #
5 #
6
7 case "$1" in
8         configure)
9                 # continue below
10         ;;
11
12         abort-upgrade|abort-remove|abort-deconfigure)
13                 exit 0
14         ;;
15
16         *)
17                 echo "postinst called with unknown argument \`$1'" >&2
18                 exit 0
19         ;;
20 esac
21
22 # Handle debconf
23 . /usr/share/debconf/confmodule
24
25 INITCONFFILE=/etc/default/samba
26
27 # We generate several files during the postinst, and we don't want
28 #       them to be readable only by root.
29 umask 022
30
31
32 # Generate configuration file if it does not exist, using default values.
33 [ -r "${INITCONFFILE}" ] || {
34         echo Generating ${INITCONFFILE}... >&2
35         cat >${INITCONFFILE} <<'EOFMAGICNUMBER1234'
36 # Defaults for samba initscript
37 # sourced by /etc/init.d/samba
38 # installed at /etc/default/samba by the maintainer scripts
39 #
40
41 #
42 # This is a POSIX shell fragment
43 #
44
45 # How should Samba (smbd) run? Possible values are "daemons"
46 #       or "inetd".
47 RUN_MODE=""
48 EOFMAGICNUMBER1234
49 }
50
51 # --- Begin of FHS migration code ---
52
53 # Starting with Samba 2.2.3-4 the WINS database, the browse
54 #       database and other important run-time files are stored in
55 #       FHS-compliant directories. The following code takes care of
56 #       moving the files in the old directories (/var/samba/ and
57 #       /var/state/samba) to the new FHS-compliant directories.
58
59 if [ -d /var/samba/ ] && [ -n "$2" ] && dpkg --compare-versions "$2" lt 2.0.7-4
60 then
61         mv /var/samba/* /var/lib/samba/ 2>/dev/null || true
62         rmdir /var/samba/
63 fi
64
65 # Default for anything we don't know about (see next two 'for' loops)
66 #       is /var/lib/samba -- guaranteed not to accidentally tromp on any 
67 #       files the admin thought were safe.
68 if [ -d /var/state/samba ] && [ -n "$2" ] && dpkg --compare-versions "$2" lt 2.2.3-4
69 then
70         mv /var/state/samba/* /var/lib/samba/ 2>/dev/null || true
71         rmdir /var/state/samba/
72
73         # It's not FHS, and it's probably our fault this is here,
74         # so delete it if we can.
75         rmdir /var/state/ 2> /dev/null || true
76 fi
77
78 # All these files are now placed in their respective FHS-compliant
79 #       directories.  Separate out the individual files accordingly.
80 for F in browse.dat printing.tdb winbindd_cache.tdb
81 do
82         if [ -e /var/lib/samba/"$F" ]; then
83                 mv /var/lib/samba/"$F" /var/cache/samba/
84         fi
85 done
86
87 for F in brlock.tdb connections.tdb locking.tdb messages.tdb nmbd.pid \
88          sessionid.tdb smbd.pid unexpected.tdb 
89 do
90         if [ -e /var/lib/samba/"$F" ]; then
91                 mv /var/lib/samba/"$F" /var/run/samba/
92         fi
93 done
94
95 # Beginning with Samba 2.2.5-1, we also move the domain secrets file
96 # to a more suitable location, since no one really edits this by hand.
97 if [ -e /etc/samba/secrets.tdb -a ! -e /var/lib/samba/secrets.tdb ]
98 then
99         mv /etc/samba/secrets.tdb /var/lib/samba/
100 fi
101
102 # If upgrading from a previous 2.999 snapshot, move the passdb.tdb
103 # database into /var/lib.
104
105 if [ -n "$2" ] && dpkg --compare-versions "$2" lt 2.999+3.0.alpha23-5 \
106    && [ -e /etc/samba/passdb.tdb -a ! -e /var/lib/samba/passdb.tdb ]
107 then
108         mv /etc/samba/passdb.tdb /var/lib/samba/
109 fi
110
111 # --- End of FHS migration code ---
112
113 # If upgrading from a previous 2.999 snapshot, clear the broken
114 # registry.tdb file.
115 if [ -n "$2" ] && dpkg --compare-versions "$2" gt 2.99.cvs.20020713-1 \
116    && dpkg --compare-versions "$2" lt 2.999+3.0cvs20020805-1
117 then
118         rm -f /var/lib/samba/registry.tdb
119 fi
120
121 # ------------------------- Debconf questions start ---------------------
122
123 # Run Samba as daemons or from inetd?
124 db_get samba/run_mode || true
125 RUN_MODE="${RET}"
126
127 TMPFILE=/etc/default/samba.dpkg-tmp
128 sed -e "s/^[[:space:]]*RUN_MODE[[:space:]]*=.*/RUN_MODE=\"${RUN_MODE}\"/" \
129         < ${INITCONFFILE} >${TMPFILE}
130 chmod a+r ${TMPFILE}
131 mv -f ${TMPFILE} ${INITCONFFILE}
132
133 # Generate a smbpasswd file?
134 db_get samba/generate_smbpasswd || true
135 GENERATE_SMBPASSWD="${RET}"
136
137 db_get samba/tdbsam || true
138 PDB_MIGRATE="${RET}"
139
140 # Done with debconf now.
141 db_stop
142
143 umask 066
144
145 # FIXME: disable if ldapsam support is enabled?
146 # FIXME: we don't want to pass these through the smbpasswd backend,
147 # some of the faking can cause us problems!
148 if [ "${GENERATE_SMBPASSWD}" = "true" -a ! -e /var/lib/samba/passdb.tdb -a ! -e /etc/samba/smbpasswd ]; then
149         getent passwd | /usr/sbin/mksmbpasswd > /etc/samba/smbpasswd
150         pdbedit -i smbpasswd -e tdbsam
151         rm /etc/samba/smbpasswd
152 fi
153
154 umask 022
155
156 if [ -n "$2" -a -e /etc/samba/smbpasswd \
157      -a ! -e /var/lib/samba/passdb.tdb -a "$PDB_MIGRATE" = "true" ] \
158    && dpkg --compare-versions "$2" lt 2.99.cvs.20020713-2
159 then
160         umask 066
161         pdbedit -i smbpasswd -e tdbsam
162         rm /etc/samba/smbpasswd
163         umask 022
164
165         # The database has been moved, now make sure we can still find it.
166         PASSDB=`sed -n -e"s/^[[:space:]]*\[global\]/\[global\]/i
167                         /^\[global\]/,/^[[:space:]]*\[/ \
168                                 s/^[[:space:]]*passdb backend[[:space:]]*=[[:space:]]*//pi" \
169                         < /etc/samba/smb.conf \
170          | tail -n 1`
171         if echo "$PASSDB" | egrep -q "(^|[[:space:]])smbpasswd"; then
172                 if ! echo "$PASSDB" | egrep -q "(^|[[:space:]])tdbsam"; then
173                         PASSDB=`echo $PASSDB | sed -e's/\(^\|[[:space:]]\)smbpasswd/\1tdbsam/'`
174                 fi
175         fi
176         if ! echo "$PASSDB" | egrep -q "(^|[[:space:]])tdbsam"; then
177                 PASSDB="tdbsam $PASSDB"
178         fi
179         TMPFILE=/etc/samba/smb.conf.dpkg-tmp
180         sed -e "s/^\([[:space:]]*\)\[global\]/\1\[global\]/i
181                 /^[[:space:]]*\[global\]/,/^[[:space:]]*\[/ \
182                         s/^\([[:space:]]*\)passdb backend[[:space:]]*=.*/\1passdb backend = ${PASSDB}/i" \
183                 < /etc/samba/smb.conf >${TMPFILE}
184         chmod a+r ${TMPFILE}
185         mv -f ${TMPFILE} /etc/samba/smb.conf
186 fi
187
188 # ------------------------- Debconf questions end ---------------------
189
190 # Handle removal of nmbd from inetd.conf, which is no longer a supported
191 #       configuration.
192 if dpkg --compare-versions "$2" lt 2.999+3.0.alpha20-4; then
193         update-inetd --remove netbios-ns
194 fi
195
196 # We want to add these entries to inetd.conf commented out. Otherwise
197 #       UDP traffic could make inetd to start nmbd or smbd right during
198 #       the configuration stage.
199 if [ -z "$2" ]; then
200         update-inetd --add "#<off># netbios-ssn stream  tcp     nowait  root    /usr/sbin/tcpd  /usr/sbin/smbd"
201 fi
202
203 if [ "$RUN_MODE" = "daemons" ]; then
204         update-inetd --disable netbios-ssn
205 else
206         update-inetd --enable netbios-ssn
207 fi
208
209 # This check is a safety net: the /etc/samba/smbpasswd file must have
210 #       permissions 600.
211 if [ -f /etc/samba/smbpasswd ]; then
212         chmod 600 /etc/samba/smbpasswd
213 fi
214
215 # Do the same check for /var/backup/smbpasswd.bak, just in case.
216 if [ -f /var/backups/smbpasswd.bak ]; then
217         chmod 600 /var/backups/smbpasswd.bak
218 fi
219
220 # Delete old /etc/samba/debian_config file, which is not used anymore
221 #       now that we are using debconf.
222 rm -f /etc/samba/debian_config
223
224 # Move old log files to the new location of Samba's log files
225 mv -f /var/log/nmb* /var/log/samba/ 2> /dev/null || true
226 mv -f /var/log/smb* /var/log/samba/ 2> /dev/null || true
227
228 #DEBHELPER#
229
230 exit 0