| 1 | #!/usr/bin/lua |
|---|
| 2 | -- (c) 2008 john@phrozen.org GPLv2 |
|---|
| 3 | |
|---|
| 4 | -- we use uci |
|---|
| 5 | local uci = require("luci.model.uci").cursor() |
|---|
| 6 | |
|---|
| 7 | -- add the header of smb.conf |
|---|
| 8 | function smb_header(s) |
|---|
| 9 | if not(s.name) then |
|---|
| 10 | s.name ="Fonera2.0" |
|---|
| 11 | end |
|---|
| 12 | if not(s.workgroup) then |
|---|
| 13 | s.workgroup = "Fonera" |
|---|
| 14 | end |
|---|
| 15 | if not(s.description) then |
|---|
| 16 | s.description = "La Fonera 2.0" |
|---|
| 17 | end |
|---|
| 18 | local f = io.open("/tmp/smb.conf", "w") |
|---|
| 19 | if f then |
|---|
| 20 | f:write("[global]\n") |
|---|
| 21 | f:write(" netbios name = "..s.name.."\n") |
|---|
| 22 | f:write(" workgroup = "..s.workgroup.."\n") |
|---|
| 23 | f:write(" server string = "..s.description.."\n") |
|---|
| 24 | f:write(" syslog = 10\n") |
|---|
| 25 | f:write(" encrypt passwords = true\n") |
|---|
| 26 | f:write(" passdb backend = smbpasswd\n") |
|---|
| 27 | f:write(" obey pam restrictions = yes\n") |
|---|
| 28 | f:write(" socket options = TCP_NODELAY IPTOS_LOWDELAY SO_KEEPALIVE\n") |
|---|
| 29 | f:write(" preferred master = yes\n") |
|---|
| 30 | f:write(" os level = 20\n") |
|---|
| 31 | f:write(" security = user\n") |
|---|
| 32 | f:write(" guest account = nobody\n") |
|---|
| 33 | f:write(" invalid users = root\n") |
|---|
| 34 | f:write(" domain master = Yes\n") |
|---|
| 35 | f:write(" unix extensions = no\n") |
|---|
| 36 | f:write(" deadtime = 15\n") |
|---|
| 37 | f:write(" getwd cache = yes\n") |
|---|
| 38 | f:write(" smb passwd file = /etc/samba/smbpasswd\n\n") |
|---|
| 39 | f:close() |
|---|
| 40 | end |
|---|
| 41 | end |
|---|
| 42 | |
|---|
| 43 | -- add a samba share to the config file |
|---|
| 44 | function smb_add_share(s) |
|---|
| 45 | local f = io.open("/tmp/smb.conf", "a") |
|---|
| 46 | if f then |
|---|
| 47 | f:write("\n["..s.."]\n\tpath = /tmp/mounts/"..s.."\n") |
|---|
| 48 | f:write("\tread only = no\n") |
|---|
| 49 | f:write("\tguest ok = no\n") |
|---|
| 50 | f:write("\tcreate mask = 0700\n") |
|---|
| 51 | f:write("\tdirectory mask = 0700\n") |
|---|
| 52 | f:write("\tadmin users = fonero\n") |
|---|
| 53 | f:close() |
|---|
| 54 | end |
|---|
| 55 | end |
|---|
| 56 | |
|---|
| 57 | -- add a samba share to the config file |
|---|
| 58 | function smb_add_media_share() |
|---|
| 59 | local f = io.open("/tmp/smb.conf", "a") |
|---|
| 60 | if f then |
|---|
| 61 | f:write("\n[Media]\n\tpath = /tmp/mounts/\n") |
|---|
| 62 | f:write("\tread only = no\n") |
|---|
| 63 | f:write("\tguest ok = no\n") |
|---|
| 64 | f:write("\tcreate mask = 0700\n") |
|---|
| 65 | f:write("\tdirectory mask = 0700\n") |
|---|
| 66 | f:write("\tadmin users = fonero\n") |
|---|
| 67 | f:close() |
|---|
| 68 | end |
|---|
| 69 | end |
|---|
| 70 | |
|---|
| 71 | -- samba is a registered service, so use the api to start/stop it |
|---|
| 72 | local srv = require("luci.fon.service") |
|---|
| 73 | local smbd = srv.Service("samba") |
|---|
| 74 | local nmbd = srv.Service("samba_nmbd") |
|---|
| 75 | |
|---|
| 76 | -- samba may only run if the user has set the password and enabled the daemon |
|---|
| 77 | local enable = uci:get("samba", "samba", "enable") |
|---|
| 78 | |
|---|
| 79 | -- stop it if it is running |
|---|
| 80 | smbd:stop() |
|---|
| 81 | nmbd:stop() |
|---|
| 82 | |
|---|
| 83 | -- restart if we are supposed to |
|---|
| 84 | if enable == "1" then |
|---|
| 85 | -- create smb.conf |
|---|
| 86 | uci:foreach("samba", "samba", smb_header) |
|---|
| 87 | local d = luci.util.exec("ls /tmp/mounts/ | cut -d/ -f2") |
|---|
| 88 | local a = {} |
|---|
| 89 | local p |
|---|
| 90 | while(string.find(d, string.char(10))) do |
|---|
| 91 | local p = string.find(d, string.char(10)) |
|---|
| 92 | print(string.sub(d, 1, p - 1)) |
|---|
| 93 | smb_add_share(string.sub(d, 1, p - 1)) |
|---|
| 94 | d = string.sub(d, p + 1) |
|---|
| 95 | end |
|---|
| 96 | smb_add_media_share() |
|---|
| 97 | |
|---|
| 98 | -- start smbd |
|---|
| 99 | smbd:start("-D") |
|---|
| 100 | nmbd:start() |
|---|
| 101 | end |
|---|