3146dd9f9b41d9ae374465ba1f4d7f636141b197
[openwrt/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
6 smb_header() {
7 local interface
8 config_get interface $1 interface "loopback lan"
9
10 # resolve interfaces
11 local interfaces=$(
12 . /lib/functions/network.sh
13
14 local net
15 for net in $interface; do
16 local device
17 network_get_device device "$net" && {
18 local subnet
19 network_get_subnet subnet "$net" && echo -n "$subnet "
20 network_get_subnet6 subnet "$net" && echo -n "$subnet "
21 }
22
23 echo -n "${device:-$net} "
24 done
25 )
26
27 local name workgroup description charset
28 local hostname="$(uci_get system.@system[0].hostname)"
29
30 config_get name $1 name "${hostname:-OpenWrt}"
31 config_get workgroup $1 workgroup "${hostname:-OpenWrt}"
32 config_get description $1 description "Samba on ${hostname:-OpenWrt}"
33 config_get charset $1 charset "UTF-8"
34
35 mkdir -p /var/etc
36 sed -e "s#|NAME|#$name#g" \
37 -e "s#|WORKGROUP|#$workgroup#g" \
38 -e "s#|DESCRIPTION|#$description#g" \
39 -e "s#|INTERFACES|#$interfaces#g" \
40 -e "s#|CHARSET|#$charset#g" \
41 /etc/samba/smb.conf.template > /var/etc/smb.conf
42
43 local homes
44 config_get_bool homes $1 homes 0
45 [ $homes -gt 0 ] && {
46 cat <<EOT >> /var/etc/smb.conf
47
48 [homes]
49 comment = Home Directories
50 browsable = no
51 read only = no
52 create mode = 0750
53 EOT
54 }
55
56 [ -L /etc/samba/smb.conf ] || ln -nsf /var/etc/smb.conf /etc/samba/smb.conf
57 }
58
59 smb_add_share() {
60 local name
61 local path
62 local users
63 local read_only
64 local guest_ok
65 local create_mask
66 local dir_mask
67 local browseable
68
69 config_get name $1 name
70 config_get path $1 path
71 config_get users $1 users
72 config_get read_only $1 read_only
73 config_get guest_ok $1 guest_ok
74 config_get create_mask $1 create_mask
75 config_get dir_mask $1 dir_mask
76 config_get browseable $1 browseable
77
78 [ -z "$name" -o -z "$path" ] && return
79
80 echo -e "\n[$name]\n\tpath = $path" >> /var/etc/smb.conf
81 [ -n "$users" ] && echo -e "\tvalid users = $users" >> /var/etc/smb.conf
82 [ -n "$read_only" ] && echo -e "\tread only = $read_only" >> /var/etc/smb.conf
83 [ -n "$guest_ok" ] && echo -e "\tguest ok = $guest_ok" >> /var/etc/smb.conf
84 [ -n "$create_mask" ] && echo -e "\tcreate mask = $create_mask" >> /var/etc/smb.conf
85 [ -n "$dir_mask" ] && echo -e "\tdirectory mask = $dir_mask" >> /var/etc/smb.conf
86 [ -n "$browseable" ] && echo -e "\tbrowseable = $browseable" >> /var/etc/smb.conf
87 }
88
89 start() {
90 config_load samba
91 config_foreach smb_header samba
92 config_foreach smb_add_share sambashare
93 service_start /usr/sbin/smbd -D
94 service_start /usr/sbin/nmbd -D
95 }
96
97 stop() {
98 service_stop /usr/sbin/smbd
99 service_stop /usr/sbin/nmbd
100 }