|
@@ -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
|
|
|
+}
|