smbuser.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package samba
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. "strings"
  7. )
  8. // injectTdbsamToSmbConfig updates the smb.conf file to set the passdb backend to tdbsam if not already set
  9. func (m *ShareManager) injectTdbsamToSmbConfig() error {
  10. // Path to smb.conf file
  11. smbConfPath := m.SambaConfigPath
  12. // Open the smb.conf file for reading
  13. file, err := os.OpenFile(smbConfPath, os.O_RDWR|os.O_APPEND, 0644)
  14. if err != nil {
  15. return fmt.Errorf("failed to open smb.conf: %v", err)
  16. }
  17. defer file.Close()
  18. // Check if passdb backend is already set to tdbsam
  19. passdbBackendSet := false
  20. section := "[global]"
  21. tdbsamLine := "passdb backend = tdbsam"
  22. scanner := bufio.NewScanner(file)
  23. for scanner.Scan() {
  24. line := strings.TrimSpace(scanner.Text())
  25. if line == section {
  26. // Inside the [global] section, check for passdb backend setting
  27. for scanner.Scan() {
  28. subLine := strings.TrimSpace(scanner.Text())
  29. if strings.HasPrefix(subLine, "passdb backend") {
  30. // If passdb backend is already set to tdbsam, no need to update
  31. if strings.TrimSpace(subLine) == tdbsamLine {
  32. passdbBackendSet = true
  33. }
  34. break
  35. }
  36. }
  37. break
  38. }
  39. }
  40. if err := scanner.Err(); err != nil {
  41. return fmt.Errorf("error reading smb.conf: %v", err)
  42. }
  43. // If passdb backend is not set to tdbsam, append the line to the configuration file
  44. if !passdbBackendSet {
  45. if _, err := file.WriteString(fmt.Sprintf("%s\n", tdbsamLine)); err != nil {
  46. return fmt.Errorf("failed to write to smb.conf: %v", err)
  47. }
  48. }
  49. return nil
  50. }
  51. //TdbsamInUse checks if the current smb runtime already using Tdbsam as backend
  52. func (m *ShareManager) TdbsamInUse() (bool, error) {
  53. // Open the smb.conf file for reading
  54. file, err := os.OpenFile(s.SambaConfigPath, os.O_RDWR|os.O_APPEND, 0644)
  55. if err != nil {
  56. return false, fmt.Errorf("failed to open smb.conf: %v", err)
  57. }
  58. defer file.Close()
  59. // Check if passdb backend is already set to tdbsam
  60. passdbBackendSet := false
  61. section := "[global]"
  62. tdbsamLine := "passdb backend = tdbsam"
  63. scanner := bufio.NewScanner(file)
  64. for scanner.Scan() {
  65. line := strings.TrimSpace(scanner.Text())
  66. if line == section {
  67. // Inside the [global] section, check for passdb backend setting
  68. for scanner.Scan() {
  69. subLine := strings.TrimSpace(scanner.Text())
  70. if strings.HasPrefix(subLine, "passdb backend") {
  71. // If passdb backend is already set to tdbsam, no need to update
  72. if strings.TrimSpace(subLine) == tdbsamLine {
  73. passdbBackendSet = true
  74. }
  75. break
  76. }
  77. }
  78. break
  79. }
  80. }
  81. return passdbBackendSet, nil
  82. }
  83. // removeTdbsamFromSmbConfig removes the line 'passdb backend = tdbsam' from smb.conf
  84. func (m *ShareManager) removeTdbsamFromSmbConfig() error {
  85. // Path to smb.conf file
  86. smbConfPath := "/etc/samba/smb.conf"
  87. // Open the smb.conf file for reading
  88. file, err := os.OpenFile(smbConfPath, os.O_RDWR, 0644)
  89. if err != nil {
  90. return fmt.Errorf("failed to open smb.conf: %v", err)
  91. }
  92. defer file.Close()
  93. // Create a temporary file to store the modified content
  94. tempFile, err := os.CreateTemp("", "smb.conf.temp")
  95. if err != nil {
  96. return fmt.Errorf("failed to create temporary file: %v", err)
  97. }
  98. defer os.Remove(tempFile.Name())
  99. defer tempFile.Close()
  100. // Copy lines from smb.conf to the temporary file, omitting the line 'passdb backend = tdbsam'
  101. section := "[global]"
  102. tdbsamLine := "passdb backend = tdbsam"
  103. scanner := bufio.NewScanner(file)
  104. for scanner.Scan() {
  105. line := strings.TrimSpace(scanner.Text())
  106. if line == section {
  107. // Inside the [global] section, omit the line 'passdb backend = tdbsam'
  108. for scanner.Scan() {
  109. subLine := strings.TrimSpace(scanner.Text())
  110. if strings.HasPrefix(subLine, "passdb backend") && subLine == tdbsamLine {
  111. continue
  112. }
  113. tempFile.WriteString(fmt.Sprintf("%s\n", subLine))
  114. }
  115. }
  116. tempFile.WriteString(fmt.Sprintf("%s\n", line))
  117. }
  118. if err := scanner.Err(); err != nil {
  119. return fmt.Errorf("error reading smb.conf: %v", err)
  120. }
  121. // Close the original smb.conf file
  122. if err := file.Close(); err != nil {
  123. return fmt.Errorf("error closing smb.conf: %v", err)
  124. }
  125. // Remove the original smb.conf file
  126. if err := os.Remove(smbConfPath); err != nil {
  127. return fmt.Errorf("error removing smb.conf: %v", err)
  128. }
  129. // Rename the temporary file to smb.conf
  130. if err := os.Rename(tempFile.Name(), smbConfPath); err != nil {
  131. return fmt.Errorf("error renaming temporary file: %v", err)
  132. }
  133. return nil
  134. }