samba.go 7.7 KB

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