Advertisement
pepeknamornik

SMBserver

May 26th, 2021 (edited)
1,188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.79 KB | None | 0 0
  1. verze = "23.0402.R1"
  2. logs = false
  3. term.clear()
  4. term.setCursorPos(1,1)
  5. local idPC = os.getComputerID()
  6. local ADmode = false
  7. local IsAdmin = false
  8. dofile("/C/system/api/APIS.lua")
  9.  
  10. if not fs.exists ("/C/system/.core/SMBsecure.cfg") then
  11.     local data = fs.open("/C/system/.core/SMBsecure.cfg", "w")
  12.     data.writeLine("C")
  13.     data.writeLine("rom")
  14.     data.writeLine("startup")
  15.     data.close()
  16. end
  17.  
  18. if fs.exists("/C/system/server/rules/AD/") and fs.exists("/C/service/AD.lua") then
  19. ADmode = true
  20. end
  21.  
  22. if fs.exists("/C/system/.core/defmodem") then
  23.         local data = fs.open ("/C/system/.core/defmodem","r")
  24.         modemPos = data.readLine()
  25.         data.close()
  26.         if not modemPos then
  27.         modemPos = "offline"
  28.         end
  29.     else
  30.         modemPos = "offline"
  31.     end
  32.    
  33. if not (modemPos == "offline") then
  34. modem = peripheral.wrap(modemPos)
  35. modem.open(idPC)
  36. print("Samba server, version "..verze..", ID serveru: "..idPC)
  37. if ADmode then print"sluzba Active directory je aktivni" end
  38. else
  39. term.setTextColor(colors.red)
  40. print("Error, you must configure the modem first")
  41. modem.open("Error")
  42. end
  43.  
  44. function logy(Zlog)
  45.     cas = (textutils.formatTime(os.time(), true))
  46.     local l = fs.open("/C/system/server/rules/AD/log","a")
  47.     l.writeLine(cas..": "..Zlog)
  48.     l.close()
  49. end
  50.  
  51. function bar(styl, text)
  52.     if (styl == "OK") then                  -- Uspech
  53.         term.setTextColor(colors.green)
  54.     elseif (styl == "FAIL") then            -- Neuspesne
  55.         term.setTextColor(colors.red)
  56.     elseif (styl == "INFO") then            -- Informativni
  57.         term.setTextColor(colors.lightBlue)
  58.     elseif (styl == "def") then             -- defaultni
  59.         term.setTextColor(colors.white)
  60.     elseif (styl == "PRO") then             -- Progress
  61.         term.setTextColor(colors.yellow)
  62.     else
  63.         styl = "def"
  64.         term.setTextColor(colors.white)
  65.     end
  66.    
  67.     if not (styl == "def") then
  68.         zprava = ("["..textutils.formatTime(os.time(), true).."] ["..styl.."] "..text)
  69.         else
  70.         zprava = text
  71.     end
  72.    
  73.     if logs then
  74.     logy(zprava)
  75.     end
  76.     print (zprava)
  77. end
  78.  
  79. local function defaultV()
  80.  idComp = nil
  81.  filePath = ""
  82.  exfile = false
  83.  user = "guest"
  84.  pass = ""
  85. end
  86.  
  87. function findFile(filePath, inverze)
  88.     if fs.exists (filePath) then
  89.     if inverze then
  90.         bar("FAIL", "Soubor "..filePath.." nalezen!")
  91.     else
  92.         bar("OK", "Soubor "..filePath.." nalezen!")
  93.     end
  94.         exfile = true
  95.     else
  96.         if inverze then
  97.             bar("OK", "Soubor "..filePath.." nenalezen!")
  98.         else
  99.             bar("FAIL", "Soubor "..filePath.." nenalezen!")
  100.         end
  101.         exfile = false
  102.     end
  103.     if inverze then
  104.     modem.transmit(tonumber(idComp),992,not exfile)
  105.     else
  106.     modem.transmit(tonumber(idComp),992,exfile)
  107.     end
  108. end
  109.  
  110. function fail() modem.transmit(tonumber(idComp),992,false) defaultV() end
  111.  
  112. function copyFile(filePath)
  113.     if not (fs.isDir(filePath)) then
  114.         if exfile then
  115.             local data = fs.open(filePath, "r")
  116.             obsah = data.readAll()
  117.             data.close()
  118.             modem.transmit(tonumber(idComp),994,obsah)
  119.             bar("OK", "Soubor byl odeslan na adresu: "..idComp)
  120.         end
  121.     else
  122.         bar("FAIL", "Slozku nelze kopirovat jako soubor!")
  123.     end
  124. end
  125.  
  126. function deleteFile(filePath)
  127.     if exfile then
  128.         fs.delete(filePath)
  129.         bar("OK", "Soubor byl smazan, PC: "..idComp)
  130.         modem.transmit(tonumber(idComp),997,"Soubor byl smazan!")
  131.     end
  132. end
  133.  
  134. function struktura(filePath)
  135.    
  136.     if (filePath == nil) then
  137.         fail()
  138.     else
  139.        
  140.     function detekce(slv)
  141.     if (slv == "") then return false end
  142.     local dct = string.find(filePath.."/"..name, slv)
  143.         if dct ~= nil then
  144.             if dct > 0 then
  145.             return true
  146.             else return false
  147.             end
  148.         end
  149.     end
  150.    
  151.     local function scann()
  152.         for i=1,#BL do
  153.             if (detekce(BL[i])) then
  154.             return true
  155.             end
  156.         end
  157.     end
  158.    
  159.    
  160.     BL = {}
  161.     local e = 0
  162.     if fs.exists("/C/system/.core/SMBsecure.cfg") then
  163.     local blackList = fs.open("/C/system/.core/SMBsecure.cfg", "r")
  164.                 repeat
  165.                 e = e+1
  166.                 BL[e] = blackList.readLine()
  167.                 until BL[e] == nil
  168.             blackList.close()
  169.     end
  170.    
  171.     logy(filePath)
  172.    
  173.     if fs.exists(filePath) then
  174.     if fs.isDir(filePath) then
  175.             if (fs.list(filePath)) then
  176.                 cDir = fs.list(filePath)
  177.                 local data = fs.open("/C/system/RAM/signature.tmp", "w")
  178.                 for a = 1,#cDir do
  179.                     name = tostring(cDir[a])
  180.                         if (string.sub(cDir[a],1,1) == ".") or scann() then
  181.                         bar("FAIL", "Pristup odepren!")
  182.                         else
  183.                             if fs.isDir(filePath.."/"..name) then
  184.                             data.writeLine(name.."*")
  185.                             else
  186.                             data.writeLine(name)
  187.                             end
  188.                         end
  189.                 end
  190.                 data.close()
  191.                 exfile=true copyFile("/C/system/RAM/signature.tmp")
  192.                 bar("OK", "Struktura odeslana na PC "..idComp)
  193.                 defaultV()
  194.                 end
  195.         else
  196.         bar("FAIL", "Neni signatura, ale soubor!")
  197.         fail()
  198.         end
  199.         else
  200.         bar("FAIL", "Slozka nenalezena!")
  201.         fail()
  202.         end
  203.     end
  204. end
  205.  
  206. function overit()
  207. if fs.exists ("/C/system/server/rules/AD/Users/"..user.."/.data") then
  208.     bar("OK", "Uzivatel existuje!")
  209.         local f = fs.open("/C/system/server/rules/AD/Users/"..user.."/.data", "r")
  210.             Su=f.readLine()
  211.             Sp=f.readLine()
  212.             Fname=f.readLine()
  213.             Lname=f.readLine()
  214.             perm=f.readLine()
  215.         if (perm == "t") then IsAdmin = true end
  216.         f.close()
  217.        
  218.         if user == Su then
  219.             if pass == Sp then
  220.                 bar("OK", "Uzivatel overen!")
  221.                 return true
  222.             else
  223.                 bar("FAIL", "Spatne heslo!")
  224.                 modem.transmit(tonumber(idComp),999,"pass")
  225.                 fail()
  226.                 return false
  227.             end
  228.         else
  229.             bar("FAIL", "Spatne uzivatelske jmeno!")
  230.             modem.transmit(tonumber(idComp),999,"user")
  231.             fail()
  232.             return false
  233.         end
  234.     else
  235.     bar("FAIL", "Pristup uzivateli odepren!")
  236.     modem.transmit(tonumber(idComp),999,"user")
  237.     fail()
  238. end
  239.  
  240. end
  241.  
  242. function createFile(filePath)
  243.     if not exfile then
  244.         create_file = fs.open(filePath,"w")
  245.         create_file.close()
  246.         bar("OK", "Soubor uspesne vytvoren!")
  247.         modem.transmit(tonumber(idComp),997,"Soubor uspesne vytvoren!")
  248.     else
  249.         bar("FAIL", "Soubor jiz existuje!")
  250.         modem.transmit(tonumber(idComp),997,"Soubor jiz existuje, nevytvoreno!")
  251.     end
  252. end
  253.  
  254. function createFolder(filePath)
  255.     if not exfile then
  256.         fs.makeDir(filePath)
  257.         bar("OK", "Slozka uspesne vytvorena!")
  258.         modem.transmit(tonumber(idComp),997,"Slozka uspesne vytvorena!")
  259.     else
  260.         bar("FAIL", "Slozka jiz existuje!")
  261.         modem.transmit(tonumber(idComp),997,"Slozka jiz existuje, nevytvoreno!")
  262.     end
  263. end
  264.  
  265.  
  266. function ulozit(filePath)
  267. local file = fs.open(filePath,"w")
  268.         file.write(obsah)
  269.         file.close()
  270.         bar("OK", "Soubor byl ulozen, z PC: "..idComp)
  271. end
  272.  
  273. function prijmout ()
  274.     local _, side, freq, rfreq, message = os.pullEvent('modem_message')
  275.  
  276.     if (rfreq == 990) then
  277.         bar("INFO", "Zadost o zpracovani udaju.")
  278.         idComp = message
  279.         bar("PRO", "Adresa zarizeni: "..idComp)
  280.     end
  281.    
  282.     if (rfreq == 998) then
  283.         user = message
  284.         bar("INFO", "Jmeno uzivatele: "..user)
  285.     end
  286.    
  287.     if (rfreq == 999) then
  288.         pass = message
  289.         bar("INFO", "Heslo prijato!")
  290.     end
  291.    
  292.     if (rfreq == 991) then
  293.         filePath = message
  294.         bar("INFO", "Zadana cesta!")
  295.     end
  296.    
  297.     if (rfreq == 993) then
  298.         if overit() then
  299.             if (message == "copy") then
  300.             findFile(filePath)
  301.             copyFile(filePath)
  302.             elseif(message == "cut") then
  303.             findFile(filePath)
  304.             copyFile(filePath)
  305.             deleteFile(filePath)
  306.             elseif(message == "signature") then
  307.             struktura(filePath)
  308.             elseif(message == "delete") then
  309.             findFile(filePath)
  310.             deleteFile(filePath)
  311.             elseif(message == "newFile") then
  312.             findFile(filePath, true)
  313.             createFile(filePath)
  314.             elseif(message == "newFolder") then
  315.             findFile(filePath, true)
  316.             createFolder(filePath)
  317.             elseif(message == "paste") then
  318.             ulozit(filePath)
  319.             end
  320.         end
  321.     end
  322.    
  323.     if (rfreq == 994) then
  324.         obsah = message
  325.         bar("INFO", "Obsah souboru prijat!")
  326.     end
  327.    
  328.     prijmout()
  329. end
  330.  
  331. defaultV()
  332. prijmout()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement