123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- 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
- }
|