| 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 | |
| 68 | config_get name $1 name |
| 69 | config_get path $1 path |
| 70 | config_get users $1 users |
| 71 | config_get read_only $1 read_only |
| 72 | config_get guest_ok $1 guest_ok |
| 73 | config_get create_mask $1 create_mask |
| 74 | config_get dir_mask $1 dir_mask |
| 75 | |
| 76 | [ -z "$name" -o -z "$path" ] && return |
| 77 | |
| 78 | echo -e "\n[$name]\n\tpath = $path" >> /var/etc/smb.conf |
| 79 | [ -n "$users" ] && echo -e "\tvalid users = $users" >> /var/etc/smb.conf |
| 80 | [ -n "$read_only" ] && echo -e "\tread only = $read_only" >> /var/etc/smb.conf |
| 81 | [ -n "$guest_ok" ] && echo -e "\tguest ok = $guest_ok" >> /var/etc/smb.conf |
| 82 | [ -n "$create_mask" ] && echo -e "\tcreate mask = $create_mask" >> /var/etc/smb.conf |
| 83 | [ -n "$dir_mask" ] && echo -e "\tdirectory mask = $dir_mask" >> /var/etc/smb.conf |
| 84 | } |
| 85 | |
| 86 | start() { |
| 87 | config_load samba |
| 88 | config_foreach smb_header samba |
| 89 | config_foreach smb_add_share sambashare |
| 90 | service_start /usr/sbin/smbd -D |
| 91 | service_start /usr/sbin/nmbd -D |
| 92 | } |
| 93 | |
| 94 | stop() { |
| 95 | service_stop /usr/sbin/smbd |
| 96 | service_stop /usr/sbin/nmbd |
| 97 | } |
| 98 | |