add cifs-utils, replacing the old cifsmount package
[packages.git] / net / samba3 / files / samba.init
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2008-2011 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                 include /lib/network
13                 scan_interfaces
14
15                 local net
16                 for net in $interface; do
17                         local ifname
18                         config_get ifname "$net" ifname
19                         [ -n "$ifname" ] && {
20                                 local ipaddr netmask
21                                 config_get ipaddr  "$net" ipaddr
22                                 config_get netmask "$net" netmask
23                                 [ -n "$ipaddr" ] && echo -n "$ipaddr/${netmask:-255.255.255.255} "
24
25                                 local ip6addr
26                                 config_get ip6addr "$net" ip6addr
27                                 [ -n "$ip6addr" ] && echo -n "$ip6addr "
28                         }
29
30                         echo -n "${ifname:-$net} "
31                 done
32         )
33
34         local name workgroup description charset
35         local hostname="$(uci_get system.@system[0].hostname)"
36
37         config_get name        $1 name        "${hostname:-OpenWrt}"
38         config_get workgroup   $1 workgroup   "${hostname:-OpenWrt}"
39         config_get description $1 description "Samba on ${hostname:-OpenWrt}"
40         config_get charset     $1 charset     "UTF-8"
41
42         mkdir -p /var/etc
43         sed -e "s#|NAME|#$name#g" \
44             -e "s#|WORKGROUP|#$workgroup#g" \
45             -e "s#|DESCRIPTION|#$description#g" \
46             -e "s#|INTERFACES|#$interfaces#g" \
47             -e "s#|CHARSET|#$charset#g" \
48             /etc/samba/smb.conf.template > /var/etc/smb.conf
49
50         local homes
51         config_get_bool homes $1 homes 0
52         [ $homes -gt 0 ] && {
53                 cat <<EOT >> /var/etc/smb.conf
54
55 [homes]
56         comment     = Home Directories
57         browsable   = no
58         read only   = no
59         create mode = 0750
60 EOT
61         }
62
63         [ -L /etc/samba/smb.conf ] || ln -nsf /var/etc/smb.conf /etc/samba/smb.conf
64 }
65
66 smb_add_share() {
67         local name
68         local path
69         local users
70         local read_only
71         local guest_ok
72         local create_mask
73         local dir_mask
74
75         config_get name $1 name
76         config_get path $1 path
77         config_get users $1 users
78         config_get read_only $1 read_only
79         config_get guest_ok $1 guest_ok
80         config_get create_mask $1 create_mask
81         config_get dir_mask $1 dir_mask
82
83         [ -z "$name" -o -z "$path" ] && return
84
85         echo -e "\n[$name]\n\tpath = $path" >> /var/etc/smb.conf
86         [ -n "$users" ] && echo -e "\tvalid users = $users" >> /var/etc/smb.conf
87         [ -n "$read_only" ] && echo -e "\tread only = $read_only" >> /var/etc/smb.conf
88         [ -n "$guest_ok" ] && echo -e "\tguest ok = $guest_ok" >> /var/etc/smb.conf
89         [ -n "$create_mask" ] && echo -e "\tcreate mask = $create_mask" >> /var/etc/smb.conf
90         [ -n "$dir_mask" ] && echo -e "\tdirectory mask = $dir_mask" >> /var/etc/smb.conf
91 }
92
93 start() {
94         config_load samba
95         config_foreach smb_header samba
96         config_foreach smb_add_share sambashare
97         service_start /usr/sbin/smbd -D
98 }
99
100 stop() {
101         service_stop /usr/sbin/smbd
102 }