aboutsummaryrefslogtreecommitdiff
path: root/net/samba36/files/samba.in
blob: 0e6c25bc34d15a6efc059eb3f42191bb24964365 (plain) (blame)
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
184
185
186
187
188
189
190
#!/bin/sh
#
# $FreeBSD$
#

# PROVIDE: nmbd smbd
%%WINBIND%%# PROVIDE: winbindd
# REQUIRE: NETWORKING SERVERS DAEMON ldconfig resolv
%%CUPS%%# REQUIRE: cupsd
# BEFORE: LOGIN
# KEYWORD: shutdown
#
# Add the following lines to /etc/rc.conf.local or /etc/rc.conf
# to enable this service:
#
#samba_enable="YES"
# or, for fine grain control:
#nmbd_enable="YES"
#smbd_enable="YES"
%%WINBIND%%# You need to enable winbindd separately, by adding:
%%WINBIND%%#winbindd_enable="YES"
#
# Configuration file can be set with:
#samba_config="/usr/local/etc/smb.conf"
#

. /etc/rc.subr

name="samba"
rcvar=samba_enable
# Custom commands
extra_commands="reload status"
start_precmd="samba_start_precmd"
start_cmd="samba_cmd"
stop_cmd="samba_cmd"
status_cmd="samba_cmd"
restart_precmd="samba_checkconfig"
reload_precmd="samba_checkconfig"
reload_cmd="samba_reload_cmd"
rcvar_cmd="samba_rcvar_cmd"
# Defaults
samba_config_default="/usr/local/etc/smb.conf"
testparm_command="/usr/local/bin/testparm"
smbcontrol_command="/usr/local/bin/smbcontrol"
samba_parm="${testparm_command} -s -v --parameter-name"
pid_extra=
#
samba_daemons="nmbd smbd"
%%WINBIND%%samba_daemons="${samba_daemons} winbindd"

samba_checkconfig() {
    echo -n "Performing sanity check on Samba configuration: "
    if ${testparm_command} -s ${samba_config:+"${samba_config}"} >/dev/null 2>&1; then #"
	echo "OK"
    else
	echo "FAILED"
	return 1
    fi
    return 0
}

samba_start_precmd() {
    # Make sure rundir exists
    if [ ! -d "%%SAMBA_RUNDIR%%" -a ! -e "%%SAMBA_RUNDIR%%" ]; then
	install -m 0755 -d "%%SAMBA_RUNDIR%%"
    fi
    # XXX: Never delete winbindd_idmap, winbindd_cache and group_mapping
    if [ -n "${samba_lockdir}" -a -d "${samba_lockdir}" ]; then
	echo -n "Removing stale Samba tdb files: "
	for file in brlock.tdb browse.dat connections.tdb gencache.tdb \
		    locking.tdb messages.tdb namelist.debug sessionid.tdb \
		    unexpected.tdb
	do
	    rm "${samba_lockdir}/${file}" </dev/null 2>/dev/null && echo -n '.'
	done
	echo " done"
    fi
}

samba_rcvar_cmd() {
    local name rcvar
    rcvar=${name}_enable
    # Prevent recursive calling
    unset "${rc_arg}_cmd" "${rc_arg}_precmd" "${rc_arg}_postcmd"
    # Check master variable
    run_rc_command "${_rc_prefix}${rc_arg}" ${rc_extra_args}
    # Check dependent variables
    for name in ${samba_daemons}; do
	# XXX
	rcvars=''; v=''
	rcvar=${name}_enable
	run_rc_command "${_rc_prefix}${rc_arg}" ${rc_extra_args}
    done
}

samba_reload_cmd() {
    local name rcvar command pidfile
    # Prevent recursive calling
    unset "${rc_arg}_cmd" "${rc_arg}_precmd" "${rc_arg}_postcmd"
    # Apply to all daemons
    for name in ${samba_daemons}; do
	rcvar=${name}_enable
	command="%%PREFIX%%/sbin/${name}"
	pidfile="%%SAMBA_RUNDIR%%/${name}${pid_extra}.pid"
	# Daemon should be enabled and running
	if [ -n "${rcvar}" ] && checkyesno "${rcvar}"; then
	    if [ -n "$(check_pidfile "${pidfile}" "${command}")" ]; then
		debug "reloading ${name} configuration"
		echo "Reloading ${name}."
		# XXX: Hack with pid_extra
		${smbcontrol_command} "${name}${pid_extra}" 'reload-config' ${command_args} >/dev/null 2>&1
	    fi
	fi
    done
}

samba_cmd() {
    local name rcvar rcvars v command pidfile samba_daemons result
    # Prevent recursive calling
    unset "${rc_arg}_cmd" "${rc_arg}_precmd" "${rc_arg}_postcmd"
    # Stop processes in the reverse order
    if [ "${rc_arg}" = "stop" ] ; then
	samba_daemons=$(reverse_list ${samba_daemons})
    fi
    # Assume success
    result=0
    # Apply to all daemons
    for name in ${samba_daemons}; do
	# XXX
	rcvars=''; v=''
	rcvar=${name}_enable
	command="%%PREFIX%%/sbin/${name}"
	pidfile="%%SAMBA_RUNDIR%%/${name}${pid_extra}.pid"
	# Daemon should be enabled and running
	if [ -n "${rcvar}" ] && checkyesno "${rcvar}"; then
	    run_rc_command "${_rc_prefix}${rc_arg}" ${rc_extra_args}
	    # If any of the of the commands failed, take it as a total result
	    result=$((${result} || $?))
	fi
    done
    return ${result}
}

samba_config_init() {
    local name
    # Load configuration
    load_rc_config "${name}"
    for name in ${samba_daemons}; do
	load_rc_config "${name}"
    done
    # Location of the config
    samba_config=${samba_config="${samba_config_default}"} #"
    # XXX: Hack to work around name change of pid file with non-default config
    if [ -n "${samba_config}" -a "${samba_config}" != "${samba_config_default}" ]; then
	pid_extra="-$(basename "${samba_config}")"
    fi
    # Setup dependent variables
    if [ -n "${rcvar}" ] && checkyesno "${rcvar}"; then
	nmbd_enable=${nmbd_enable=YES}
	smbd_enable=${smbd_enable=YES}
    fi
    # Defaults
    samba_enable=${samba_enable:=NO}
    nmbd_enable=${nmbd_enable:=NO}
    smbd_enable=${smbd_enable:=NO}
%%WINBIND%%    # Winbindd
%%WINBIND%%    if [ -z "${winbind_enable}" ]; then
%%WINBIND%%	samba_idmap=$(${samba_parm} 'idmap uid' "${samba_config}" 2>/dev/null)
%%WINBIND%%	# Check that winbindd is actually configured
%%WINBIND%%	if [ -n "${samba_idmap}" ]; then
%%WINBIND%%		winbindd_enable="YES"
%%WINBIND%%	fi
%%WINBIND%%	winbindd_enable=${winbindd_enable:=NO}
%%WINBIND%%    fi
}

# Load configuration variables
samba_config_init
# Where is lockdir
samba_lockdir=$(${samba_parm} 'lock directory' "${samba_config}" 2>/dev/null)
# Common flags
command_args=${samba_config:+-s "${samba_config}"} #"
nmbd_flags=${nmbd_flags="-D"}
smbd_flags=${smbd_flags="-D"}
%%WINBIND%%winbindd_flags=${winbindd_flags=''}
# Requirements
required_files="${samba_config}"
required_dirs="${samba_lockdir}"

run_rc_command "$1"