samba: convert init script to procd, add reload support
[openwrt.git] / package / network / services / samba36 / files / samba.init
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2008-2012 OpenWrt.org
3
4 START=60
5 USE_PROCD=1
6
7 smb_header() {
8         local interface
9         config_get interface $1 interface "loopback lan"
10
11         # resolve interfaces
12         local interfaces=$(
13                 . /lib/functions/network.sh
14
15                 local net
16                 for net in $interface; do
17                         local device
18                         network_get_device device "$net" && {
19                                 local subnet
20                                 network_get_subnet  subnet "$net" && echo -n "$subnet "
21                                 network_get_subnet6 subnet "$net" && echo -n "$subnet "
22                         }
23
24                         echo -n "${device:-$net} "
25                 done
26         )
27
28         local name workgroup description charset
29         local hostname="$(uci_get system.@system[0].hostname)"
30
31         config_get name        $1 name        "${hostname:-OpenWrt}"
32         config_get workgroup   $1 workgroup   "${hostname:-OpenWrt}"
33         config_get description $1 description "Samba on ${hostname:-OpenWrt}"
34         config_get charset     $1 charset     "UTF-8"
35
36         mkdir -p /var/etc
37         sed -e "s#|NAME|#$name#g" \
38             -e "s#|WORKGROUP|#$workgroup#g" \
39             -e "s#|DESCRIPTION|#$description#g" \
40             -e "s#|INTERFACES|#$interfaces#g" \
41             -e "s#|CHARSET|#$charset#g" \
42             /etc/samba/smb.conf.template > /var/etc/smb.conf
43
44         local homes
45         config_get_bool homes $1 homes 0
46         [ $homes -gt 0 ] && {
47                 cat <<EOT >> /var/etc/smb.conf
48
49 [homes]
50         comment     = Home Directories
51         browsable   = no
52         read only   = no
53         create mode = 0750
54 EOT
55         }
56
57         [ -L /etc/samba/smb.conf ] || ln -nsf /var/etc/smb.conf /etc/samba/smb.conf
58 }
59
60 smb_add_share() {
61         local name
62         local path
63         local users
64         local read_only
65         local guest_ok
66         local create_mask
67         local dir_mask
68         local browseable
69
70         config_get name $1 name
71         config_get path $1 path
72         config_get users $1 users
73         config_get read_only $1 read_only
74         config_get guest_ok $1 guest_ok
75         config_get create_mask $1 create_mask
76         config_get dir_mask $1 dir_mask
77         config_get browseable $1 browseable
78
79         [ -z "$name" -o -z "$path" ] && return
80
81         echo -e "\n[$name]\n\tpath = $path" >> /var/etc/smb.conf
82         [ -n "$users" ] && echo -e "\tvalid users = $users" >> /var/etc/smb.conf
83         [ -n "$read_only" ] && echo -e "\tread only = $read_only" >> /var/etc/smb.conf
84         [ -n "$guest_ok" ] && echo -e "\tguest ok = $guest_ok" >> /var/etc/smb.conf
85         [ -n "$create_mask" ] && echo -e "\tcreate mask = $create_mask" >> /var/etc/smb.conf
86         [ -n "$dir_mask" ] && echo -e "\tdirectory mask = $dir_mask" >> /var/etc/smb.conf
87         [ -n "$browseable" ] && echo -e "\tbrowseable = $browseable" >> /var/etc/smb.conf
88 }
89
90 init_config() {
91         config_load samba
92         config_foreach smb_header samba
93         config_foreach smb_add_share sambashare
94 }
95
96 reload_service() {
97         init_config
98
99         killall -HUP smbd
100 }
101
102 service_triggers() {
103         procd_add_reload_trigger samba
104 }
105
106 start_service() {
107         init_config
108
109         procd_open_instance
110         procd_set_param command /usr/sbin/smbd -F
111         procd_set_param respawn
112         procd_close_instance
113
114         procd_open_instance
115         procd_set_param command /usr/sbin/nmbd -F
116         procd_set_param respawn
117         procd_close_instance
118 }