瀏覽代碼

Added more smb stuffs

aroz 1 年之前
父節點
當前提交
943d920058

+ 3 - 2
mod/fileservers/servers/samba/required.go

@@ -5,10 +5,11 @@ import (
 	"fmt"
 	"log"
 	"os/exec"
-	"os/user"
+
 	"strings"
 
 	"imuslab.com/arozos/mod/fileservers"
+	"imuslab.com/arozos/mod/user"
 )
 
 /*
@@ -33,7 +34,7 @@ func (m *ShareManager) GetEndpoints(userinfo *user.User) []*fileservers.Endpoint
 	eps = append(eps, &fileservers.Endpoint{
 		ProtocolName: "//",
 		Port:         0,
-		Subpath:      "/" + userinfo.Username,
+		Subpath:      "/",
 	})
 	return eps
 }

+ 14 - 2
mod/fileservers/servers/samba/samba.go

@@ -2,12 +2,16 @@ package samba
 
 import (
 	"bufio"
+	"errors"
 	"fmt"
 	"io"
 	"os"
 	"path/filepath"
+	"runtime"
 	"strings"
 	"time"
+
+	"imuslab.com/arozos/mod/utils"
 )
 
 /*
@@ -33,11 +37,19 @@ type ShareConfig struct {
 	GuestOk    bool
 }
 
-func NewSambaShareManager() *ShareManager {
+func NewSambaShareManager() (*ShareManager, error) {
+	if runtime.GOOS == "linux" {
+		//Check if samba installed
+		if !utils.FileExists("/bin/smbcontrol") {
+			return nil, errors.New("samba not installed")
+		}
+	} else {
+		return nil, errors.New("platform not supported")
+	}
 	return &ShareManager{
 		SambaConfigPath: "/etc/samba/smb.conf",
 		BackupDir:       "./backup",
-	}
+	}, nil
 }
 
 // ReadSambaShares reads the smb.conf file and extracts all existing shares

+ 151 - 0
mod/fileservers/servers/samba/smbuser.go

@@ -0,0 +1,151 @@
+package samba
+
+import (
+	"bufio"
+	"fmt"
+	"os"
+	"strings"
+)
+
+// injectTdbsamToSmbConfig updates the smb.conf file to set the passdb backend to tdbsam if not already set
+func (m *ShareManager) injectTdbsamToSmbConfig() error {
+	// Path to smb.conf file
+	smbConfPath := m.SambaConfigPath
+
+	// Open the smb.conf file for reading
+	file, err := os.OpenFile(smbConfPath, os.O_RDWR|os.O_APPEND, 0644)
+	if err != nil {
+		return fmt.Errorf("failed to open smb.conf: %v", err)
+	}
+	defer file.Close()
+
+	// Check if passdb backend is already set to tdbsam
+	passdbBackendSet := false
+	section := "[global]"
+	tdbsamLine := "passdb backend = tdbsam"
+	scanner := bufio.NewScanner(file)
+	for scanner.Scan() {
+		line := strings.TrimSpace(scanner.Text())
+		if line == section {
+			// Inside the [global] section, check for passdb backend setting
+			for scanner.Scan() {
+				subLine := strings.TrimSpace(scanner.Text())
+				if strings.HasPrefix(subLine, "passdb backend") {
+					// If passdb backend is already set to tdbsam, no need to update
+					if strings.TrimSpace(subLine) == tdbsamLine {
+						passdbBackendSet = true
+					}
+					break
+				}
+			}
+			break
+		}
+	}
+	if err := scanner.Err(); err != nil {
+		return fmt.Errorf("error reading smb.conf: %v", err)
+	}
+
+	// If passdb backend is not set to tdbsam, append the line to the configuration file
+	if !passdbBackendSet {
+		if _, err := file.WriteString(fmt.Sprintf("%s\n", tdbsamLine)); err != nil {
+			return fmt.Errorf("failed to write to smb.conf: %v", err)
+		}
+	}
+
+	return nil
+}
+
+//TdbsamInUse checks if the current smb runtime already using Tdbsam as backend
+func (m *ShareManager) TdbsamInUse() (bool, error) {
+	// Open the smb.conf file for reading
+	file, err := os.OpenFile(s.SambaConfigPath, os.O_RDWR|os.O_APPEND, 0644)
+	if err != nil {
+		return false, fmt.Errorf("failed to open smb.conf: %v", err)
+	}
+	defer file.Close()
+
+	// Check if passdb backend is already set to tdbsam
+	passdbBackendSet := false
+	section := "[global]"
+	tdbsamLine := "passdb backend = tdbsam"
+	scanner := bufio.NewScanner(file)
+	for scanner.Scan() {
+		line := strings.TrimSpace(scanner.Text())
+		if line == section {
+			// Inside the [global] section, check for passdb backend setting
+			for scanner.Scan() {
+				subLine := strings.TrimSpace(scanner.Text())
+				if strings.HasPrefix(subLine, "passdb backend") {
+					// If passdb backend is already set to tdbsam, no need to update
+					if strings.TrimSpace(subLine) == tdbsamLine {
+						passdbBackendSet = true
+					}
+					break
+				}
+			}
+			break
+		}
+	}
+
+	return passdbBackendSet, nil
+}
+
+// removeTdbsamFromSmbConfig removes the line 'passdb backend = tdbsam' from smb.conf
+func (m *ShareManager) removeTdbsamFromSmbConfig() error {
+	// Path to smb.conf file
+	smbConfPath := "/etc/samba/smb.conf"
+
+	// Open the smb.conf file for reading
+	file, err := os.OpenFile(smbConfPath, os.O_RDWR, 0644)
+	if err != nil {
+		return fmt.Errorf("failed to open smb.conf: %v", err)
+	}
+	defer file.Close()
+
+	// Create a temporary file to store the modified content
+	tempFile, err := os.CreateTemp("", "smb.conf.temp")
+	if err != nil {
+		return fmt.Errorf("failed to create temporary file: %v", err)
+	}
+	defer os.Remove(tempFile.Name())
+	defer tempFile.Close()
+
+	// Copy lines from smb.conf to the temporary file, omitting the line 'passdb backend = tdbsam'
+	section := "[global]"
+	tdbsamLine := "passdb backend = tdbsam"
+	scanner := bufio.NewScanner(file)
+	for scanner.Scan() {
+		line := strings.TrimSpace(scanner.Text())
+		if line == section {
+			// Inside the [global] section, omit the line 'passdb backend = tdbsam'
+			for scanner.Scan() {
+				subLine := strings.TrimSpace(scanner.Text())
+				if strings.HasPrefix(subLine, "passdb backend") && subLine == tdbsamLine {
+					continue
+				}
+				tempFile.WriteString(fmt.Sprintf("%s\n", subLine))
+			}
+		}
+		tempFile.WriteString(fmt.Sprintf("%s\n", line))
+	}
+	if err := scanner.Err(); err != nil {
+		return fmt.Errorf("error reading smb.conf: %v", err)
+	}
+
+	// Close the original smb.conf file
+	if err := file.Close(); err != nil {
+		return fmt.Errorf("error closing smb.conf: %v", err)
+	}
+
+	// Remove the original smb.conf file
+	if err := os.Remove(smbConfPath); err != nil {
+		return fmt.Errorf("error removing smb.conf: %v", err)
+	}
+
+	// Rename the temporary file to smb.conf
+	if err := os.Rename(tempFile.Name(), smbConfPath); err != nil {
+		return fmt.Errorf("error renaming temporary file: %v", err)
+	}
+
+	return nil
+}

+ 25 - 1
network.go

@@ -2,6 +2,7 @@ package main
 
 import (
 	"encoding/json"
+	"log"
 	"net/http"
 	"strconv"
 	"strings"
@@ -342,7 +343,12 @@ func FileServerInit() {
 	*/
 
 	//Samba
-	SambaShareManager = samba.NewSambaShareManager()
+	var err error
+	SambaShareManager, err = samba.NewSambaShareManager()
+	if err != nil {
+		//Disable samba if not installed or platform not supported
+		log.Println("[INFO] Samba Share Manager Disabled: " + err.Error())
+	}
 
 	//Register Endpoints
 	//WebDAV
@@ -430,6 +436,24 @@ func FileServerInit() {
 		GetEndpoints:      DirListManager.ListEndpoints,
 	})
 
+	if SambaShareManager != nil {
+		//Samba is external and might not exists on this host
+		networkFileServerDaemon = append(networkFileServerDaemon, &fileservers.Server{
+			ID:                "smbd",
+			Name:              "Samba Shares",
+			Desc:              "Share files via SMB using Samba",
+			IconPath:          "img/system/network-samba.svg",
+			DefaultPorts:      []int{},
+			Ports:             []int{},
+			ForwardPortIfUpnp: false,
+			ConnInstrPage:     "SystemAO/disk/instr/samba.html",
+			ConfigPage:        "SystemAO/disk/samba.html",
+			EnableCheck:       SambaShareManager.IsEnabled,
+			ToggleFunc:        SambaShareManager.ServerToggle,
+			GetEndpoints:      SambaShareManager.GetEndpoints,
+		})
+	}
+
 	router.HandleFunc("/system/network/server/list", NetworkHandleGetFileServerServiceList)
 	router.HandleFunc("/system/network/server/endpoints", NetworkHandleGetFileServerEndpoints)
 	router.HandleFunc("/system/network/server/status", NetworkHandleGetFileServerStatus)

+ 2 - 0
web/SystemAO/disk/instr/samba.html

@@ -0,0 +1,2 @@
+<h1>Work In Progress</h1>
+<p>Personal Settings</p>

+ 1 - 0
web/SystemAO/disk/samba.html

@@ -0,0 +1 @@
+<p>Admin Settings</p>

+ 37 - 0
web/img/system/network-samba.svg

@@ -0,0 +1,37 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Generator: Adobe Illustrator 16.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
+<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
+<svg version="1.1" id="圖層_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
+	 width="128px" height="128px" viewBox="0 0 128 128" enable-background="new 0 0 128 128" xml:space="preserve">
+<g id="圖層_2">
+	<polygon fill="#DBAC50" points="110.998,23.424 110.998,84.064 17.001,84.064 17.001,13.652 48.449,13.469 55.315,23.669 	"/>
+</g>
+<g id="圖層_3">
+	<polygon fill="#E5BD64" points="110.998,84.064 17.001,84.064 17.087,31.401 110.57,31.401 	"/>
+</g>
+<g id="圖層_4">
+	<rect x="17.001" y="103.51" fill="#443F5D" width="93.997" height="4.691"/>
+	<rect x="60.985" y="84.064" fill="#443F5D" width="6.029" height="19.445"/>
+	<path fill="#55516E" d="M72.936,110.512c0,2.221-1.801,4.02-4.021,4.02h-9.827c-2.221,0-4.021-1.799-4.021-4.02v-9.828
+		c0-2.221,1.8-4.021,4.021-4.021h9.827c2.221,0,4.021,1.801,4.021,4.021V110.512z"/>
+</g>
+<g>
+	<path fill="#FFFFFF" d="M51.275,75.102c1.564,1.273,3.406,1.911,5.524,1.911c2.707,0,4.061-0.927,4.061-2.782
+		c0-0.535-0.149-1.02-0.448-1.449c-0.299-0.432-0.701-0.807-1.207-1.128c-0.505-0.32-1.505-0.837-2.999-1.549
+		c-1.837-0.879-3.12-1.771-3.85-2.677c-0.729-0.904-1.094-1.969-1.094-3.19c0-1.758,0.694-3.142,2.083-4.153
+		s3.164-1.516,5.326-1.516c2.048,0,3.595,0.25,4.641,0.751v3.243c-1.257-0.87-2.826-1.305-4.707-1.305
+		c-1.204,0-2.175,0.242-2.914,0.725s-1.107,1.147-1.107,1.991c0,0.764,0.27,1.393,0.811,1.885c0.54,0.492,1.681,1.151,3.421,1.978
+		c2.03,0.949,3.432,1.901,4.206,2.854s1.16,2.05,1.16,3.289c0,1.846-0.668,3.256-2.004,4.232c-1.336,0.975-3.186,1.463-5.55,1.463
+		c-0.87,0-1.837-0.105-2.9-0.316s-1.881-0.492-2.452-0.844V75.102z"/>
+	<path fill="#FFFFFF" d="M86.541,79.333V66.149c0-1.151,0.062-2.518,0.185-4.1h-0.079c-0.264,1.134-0.471,1.876-0.619,2.228
+		l-6.21,15.056h-2.069l-6.236-14.937c-0.158-0.378-0.365-1.16-0.619-2.347h-0.08c0.088,1.23,0.133,2.716,0.133,4.457v12.827h-2.94
+		V58.912h4.417l5.523,13.487c0.431,1.055,0.716,1.93,0.857,2.623h0.092c0.167-0.553,0.475-1.441,0.923-2.662l5.616-13.448h4.245
+		v20.421H86.541z"/>
+	<path fill="#FFFFFF" d="M94.794,79.333V58.912h6.381c1.925,0,3.443,0.426,4.555,1.279c1.112,0.853,1.668,2,1.668,3.441
+		c0,1.125-0.339,2.114-1.015,2.966c-0.678,0.853-1.569,1.45-2.677,1.793v0.053c1.389,0.158,2.507,0.682,3.355,1.568
+		c0.848,0.889,1.272,2.014,1.272,3.375c0,1.775-0.636,3.211-1.905,4.305c-1.271,1.095-2.946,1.642-5.03,1.642H94.794z
+		 M97.958,61.509v5.972h2.294c1.204,0,2.14-0.289,2.809-0.869c0.667-0.58,1.002-1.376,1.002-2.387c0-1.811-1.24-2.716-3.719-2.716
+		H97.958z M97.958,70.092v6.645h2.979c1.292,0,2.294-0.299,3.006-0.896s1.068-1.429,1.068-2.492c0-2.171-1.464-3.256-4.391-3.256
+		H97.958z"/>
+</g>
+</svg>