samba.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. package samba
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. "time"
  10. )
  11. /*
  12. Samba Share Warpper
  13. Note that this module only provide exposing of local disk / storage.
  14. This module do not handle providing the virtualized interface for samba
  15. */
  16. type ShareManager struct {
  17. SambaConfigPath string //Config file for samba, aka smb.conf
  18. BackupDir string //Backup directory for restoring previous config
  19. }
  20. type ShareConfig struct {
  21. Name string
  22. Path string
  23. ValidUsers []string
  24. ReadOnly bool
  25. Browseable bool
  26. GuestOk bool
  27. }
  28. func NewSambaShareManager() *ShareManager {
  29. return &ShareManager{
  30. SambaConfigPath: "/etc/samba/smb.conf",
  31. BackupDir: "./backup",
  32. }
  33. }
  34. // ReadSambaShares reads the smb.conf file and extracts all existing shares
  35. func (s *ShareManager) ReadSambaShares() ([]ShareConfig, error) {
  36. file, err := os.Open(s.SambaConfigPath)
  37. if err != nil {
  38. return nil, err
  39. }
  40. defer file.Close()
  41. var shares []ShareConfig
  42. var currentShare *ShareConfig
  43. scanner := bufio.NewScanner(file)
  44. for scanner.Scan() {
  45. line := strings.TrimSpace(scanner.Text())
  46. // Check for section headers
  47. if strings.HasPrefix(line, "[") && strings.HasSuffix(line, "]") {
  48. if currentShare != nil {
  49. shares = append(shares, *currentShare)
  50. }
  51. currentShare = &ShareConfig{
  52. Name: strings.Trim(line, "[]"),
  53. }
  54. continue
  55. }
  56. // Check if we are currently processing a share section
  57. if currentShare != nil {
  58. tokens := strings.SplitN(line, "=", 2)
  59. if len(tokens) != 2 {
  60. continue
  61. }
  62. key := strings.TrimSpace(tokens[0])
  63. value := strings.TrimSpace(tokens[1])
  64. switch key {
  65. case "path":
  66. currentShare.Path = value
  67. case "valid users":
  68. currentShare.ValidUsers = strings.Fields(value)
  69. case "read only":
  70. currentShare.ReadOnly = (value == "yes")
  71. case "browseable":
  72. currentShare.Browseable = (value == "yes")
  73. case "guest ok":
  74. currentShare.GuestOk = (value == "yes")
  75. }
  76. }
  77. }
  78. // Add the last share if there is one
  79. if currentShare != nil {
  80. shares = append(shares, *currentShare)
  81. }
  82. // Check for scanner errors
  83. if err := scanner.Err(); err != nil {
  84. return nil, err
  85. }
  86. return shares, nil
  87. }
  88. // CreateNewSambaShare converts the shareConfig to string and appends it to smb.conf if the share name does not already exist
  89. func (s *ShareManager) CreateNewSambaShare(shareToCreate *ShareConfig) error {
  90. // Path to smb.conf
  91. smbConfPath := s.SambaConfigPath
  92. // Open the smb.conf file for reading
  93. file, err := os.Open(smbConfPath)
  94. if err != nil {
  95. return fmt.Errorf("failed to open smb.conf: %v", err)
  96. }
  97. defer file.Close()
  98. // Check if the share already exists
  99. scanner := bufio.NewScanner(file)
  100. shareExists := false
  101. shareNameSection := fmt.Sprintf("[%s]", shareToCreate.Name)
  102. for scanner.Scan() {
  103. if strings.TrimSpace(scanner.Text()) == shareNameSection {
  104. shareExists = true
  105. break
  106. }
  107. }
  108. if err := scanner.Err(); err != nil {
  109. return fmt.Errorf("failed to read smb.conf: %v", err)
  110. }
  111. if shareExists {
  112. return fmt.Errorf("share %s already exists", shareToCreate.Name)
  113. }
  114. // Convert ShareConfig to string
  115. shareConfigString := convertShareConfigToString(shareToCreate)
  116. // Open the smb.conf file for appending
  117. file, err = os.OpenFile(smbConfPath, os.O_APPEND|os.O_WRONLY, 0644)
  118. if err != nil {
  119. return fmt.Errorf("failed to open smb.conf for writing: %v", err)
  120. }
  121. defer file.Close()
  122. // Append the new share configuration
  123. if _, err := file.WriteString(shareConfigString); err != nil {
  124. return fmt.Errorf("failed to write to smb.conf: %v", err)
  125. }
  126. return nil
  127. }
  128. // RemoveSambaShareConfig removes the Samba share configuration from smb.conf
  129. func (s *ShareManager) RemoveSambaShareConfig(shareName string) error {
  130. // Open the smb.conf file for reading
  131. file, err := os.Open(s.SambaConfigPath)
  132. if err != nil {
  133. return err
  134. }
  135. defer file.Close()
  136. // Create a temporary file to store modified smb.conf
  137. tmpFile, err := os.CreateTemp("", "smb.conf.*.tmp")
  138. if err != nil {
  139. return err
  140. }
  141. defer tmpFile.Close()
  142. // Create a scanner to read the smb.conf file line by line
  143. scanner := bufio.NewScanner(file)
  144. for scanner.Scan() {
  145. line := scanner.Text()
  146. // Check if the line contains the share name
  147. if strings.HasPrefix(line, "["+shareName+"]") {
  148. // Skip the lines until the next section
  149. for scanner.Scan() {
  150. if strings.HasPrefix(scanner.Text(), "[") {
  151. break
  152. }
  153. }
  154. continue // Skip writing the share configuration to the temporary file
  155. }
  156. // Write the line to the temporary file
  157. _, err := fmt.Fprintln(tmpFile, line)
  158. if err != nil {
  159. return err
  160. }
  161. }
  162. // Check for scanner errors
  163. if err := scanner.Err(); err != nil {
  164. return err
  165. }
  166. // Close the original smb.conf file
  167. if err := file.Close(); err != nil {
  168. return err
  169. }
  170. // Close the temporary file
  171. if err := tmpFile.Close(); err != nil {
  172. return err
  173. }
  174. // Replace the original smb.conf file with the temporary file
  175. if err := os.Rename(tmpFile.Name(), "/etc/samba/smb.conf"); err != nil {
  176. return err
  177. }
  178. return nil
  179. }
  180. // ShareExists checks if a given share name exists in smb.conf
  181. func (s *ShareManager) ShareExists(shareName string) (bool, error) {
  182. // Path to smb.conf
  183. smbConfPath := s.SambaConfigPath
  184. // Open the smb.conf file for reading
  185. file, err := os.Open(smbConfPath)
  186. if err != nil {
  187. return false, fmt.Errorf("failed to open smb.conf: %v", err)
  188. }
  189. defer file.Close()
  190. // Check if the share already exists
  191. scanner := bufio.NewScanner(file)
  192. shareNameSection := fmt.Sprintf("[%s]", shareName)
  193. for scanner.Scan() {
  194. if strings.TrimSpace(scanner.Text()) == shareNameSection {
  195. return true, nil
  196. }
  197. }
  198. if err := scanner.Err(); err != nil {
  199. return false, fmt.Errorf("failed to read smb.conf: %v", err)
  200. }
  201. return false, nil
  202. }
  203. // Backup the current smb.conf to the backup folder
  204. func (s *ShareManager) BackupSmbConf() error {
  205. // Define source and backup directory
  206. sourceFile := s.SambaConfigPath
  207. backupDir := s.BackupDir
  208. // Ensure the backup directory exists
  209. err := os.MkdirAll(backupDir, 0755)
  210. if err != nil {
  211. return fmt.Errorf("failed to create backup directory: %v", err)
  212. }
  213. // Create a timestamped backup filename
  214. timestamp := time.Now().Format("20060102_150405")
  215. backupFile := filepath.Join(backupDir, fmt.Sprintf("%s.smb.conf", timestamp))
  216. // Open the source file
  217. src, err := os.Open(sourceFile)
  218. if err != nil {
  219. return fmt.Errorf("failed to open source file: %v", err)
  220. }
  221. defer src.Close()
  222. // Create the destination file
  223. dst, err := os.Create(backupFile)
  224. if err != nil {
  225. return fmt.Errorf("failed to create backup file: %v", err)
  226. }
  227. defer dst.Close()
  228. // Copy the contents of the source file to the backup file
  229. _, err = io.Copy(dst, src)
  230. if err != nil {
  231. return fmt.Errorf("failed to copy file contents: %v", err)
  232. }
  233. return nil
  234. }