samba.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. package samba
  2. import (
  3. "bufio"
  4. "fmt"
  5. "os"
  6. "strings"
  7. )
  8. /*
  9. Samba Share Warpper
  10. Note that this module only provide exposing of local disk / storage.
  11. This module do not handle providing the virtualized interface for samba
  12. */
  13. type ShareManager struct {
  14. SambaConfigPath string
  15. }
  16. type ShareConfig struct {
  17. Name string
  18. Path string
  19. ValidUsers []string
  20. ReadOnly bool
  21. Browseable bool
  22. GuestOk bool
  23. }
  24. func NewSambaShareManager() *ShareManager {
  25. return &ShareManager{
  26. SambaConfigPath: "/etc/samba/smb.conf",
  27. }
  28. }
  29. // ReadSambaShares reads the smb.conf file and extracts all existing shares
  30. func (s *ShareManager) ReadSambaShares() ([]ShareConfig, error) {
  31. file, err := os.Open(s.SambaConfigPath)
  32. if err != nil {
  33. return nil, err
  34. }
  35. defer file.Close()
  36. var shares []ShareConfig
  37. var currentShare *ShareConfig
  38. scanner := bufio.NewScanner(file)
  39. for scanner.Scan() {
  40. line := strings.TrimSpace(scanner.Text())
  41. // Check for section headers
  42. if strings.HasPrefix(line, "[") && strings.HasSuffix(line, "]") {
  43. if currentShare != nil {
  44. shares = append(shares, *currentShare)
  45. }
  46. currentShare = &ShareConfig{
  47. Name: strings.Trim(line, "[]"),
  48. }
  49. continue
  50. }
  51. // Check if we are currently processing a share section
  52. if currentShare != nil {
  53. tokens := strings.SplitN(line, "=", 2)
  54. if len(tokens) != 2 {
  55. continue
  56. }
  57. key := strings.TrimSpace(tokens[0])
  58. value := strings.TrimSpace(tokens[1])
  59. switch key {
  60. case "path":
  61. currentShare.Path = value
  62. case "valid users":
  63. currentShare.ValidUsers = strings.Fields(value)
  64. case "read only":
  65. currentShare.ReadOnly = (value == "yes")
  66. case "browseable":
  67. currentShare.Browseable = (value == "yes")
  68. case "guest ok":
  69. currentShare.GuestOk = (value == "yes")
  70. }
  71. }
  72. }
  73. // Add the last share if there is one
  74. if currentShare != nil {
  75. shares = append(shares, *currentShare)
  76. }
  77. // Check for scanner errors
  78. if err := scanner.Err(); err != nil {
  79. return nil, err
  80. }
  81. return shares, nil
  82. }
  83. // AppendSambaShareConfig appends the Samba share configuration to smb.conf
  84. func (s *ShareManager) AppendSambaShareConfig(config string) error {
  85. file, err := os.OpenFile(s.SambaConfigPath, os.O_APPEND|os.O_WRONLY, 0644)
  86. if err != nil {
  87. return err
  88. }
  89. defer file.Close()
  90. if _, err := file.WriteString(config); err != nil {
  91. return err
  92. }
  93. return nil
  94. }
  95. // RemoveSambaShareConfig removes the Samba share configuration from smb.conf
  96. func (s *ShareManager) RemoveSambaShareConfig(shareName string) error {
  97. // Open the smb.conf file for reading
  98. file, err := os.Open(s.SambaConfigPath)
  99. if err != nil {
  100. return err
  101. }
  102. defer file.Close()
  103. // Create a temporary file to store modified smb.conf
  104. tmpFile, err := os.CreateTemp("", "smb.conf.*.tmp")
  105. if err != nil {
  106. return err
  107. }
  108. defer tmpFile.Close()
  109. // Create a scanner to read the smb.conf file line by line
  110. scanner := bufio.NewScanner(file)
  111. for scanner.Scan() {
  112. line := scanner.Text()
  113. // Check if the line contains the share name
  114. if strings.HasPrefix(line, "["+shareName+"]") {
  115. // Skip the lines until the next section
  116. for scanner.Scan() {
  117. if strings.HasPrefix(scanner.Text(), "[") {
  118. break
  119. }
  120. }
  121. continue // Skip writing the share configuration to the temporary file
  122. }
  123. // Write the line to the temporary file
  124. _, err := fmt.Fprintln(tmpFile, line)
  125. if err != nil {
  126. return err
  127. }
  128. }
  129. // Check for scanner errors
  130. if err := scanner.Err(); err != nil {
  131. return err
  132. }
  133. // Close the original smb.conf file
  134. if err := file.Close(); err != nil {
  135. return err
  136. }
  137. // Close the temporary file
  138. if err := tmpFile.Close(); err != nil {
  139. return err
  140. }
  141. // Replace the original smb.conf file with the temporary file
  142. if err := os.Rename(tmpFile.Name(), "/etc/samba/smb.conf"); err != nil {
  143. return err
  144. }
  145. return nil
  146. }