| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- package samba
- import (
- "bufio"
- "fmt"
- "os"
- "strings"
- )
- /*
- Samba Share Warpper
- Note that this module only provide exposing of local disk / storage.
- This module do not handle providing the virtualized interface for samba
- */
- type ShareManager struct {
- SambaConfigPath string
- }
- type ShareConfig struct {
- Name string
- Path string
- ValidUsers []string
- ReadOnly bool
- Browseable bool
- GuestOk bool
- }
- func NewSambaShareManager() *ShareManager {
- return &ShareManager{
- SambaConfigPath: "/etc/samba/smb.conf",
- }
- }
- // ReadSambaShares reads the smb.conf file and extracts all existing shares
- func (s *ShareManager) ReadSambaShares() ([]ShareConfig, error) {
- file, err := os.Open(s.SambaConfigPath)
- if err != nil {
- return nil, err
- }
- defer file.Close()
- var shares []ShareConfig
- var currentShare *ShareConfig
- scanner := bufio.NewScanner(file)
- for scanner.Scan() {
- line := strings.TrimSpace(scanner.Text())
- // Check for section headers
- if strings.HasPrefix(line, "[") && strings.HasSuffix(line, "]") {
- if currentShare != nil {
- shares = append(shares, *currentShare)
- }
- currentShare = &ShareConfig{
- Name: strings.Trim(line, "[]"),
- }
- continue
- }
- // Check if we are currently processing a share section
- if currentShare != nil {
- tokens := strings.SplitN(line, "=", 2)
- if len(tokens) != 2 {
- continue
- }
- key := strings.TrimSpace(tokens[0])
- value := strings.TrimSpace(tokens[1])
- switch key {
- case "path":
- currentShare.Path = value
- case "valid users":
- currentShare.ValidUsers = strings.Fields(value)
- case "read only":
- currentShare.ReadOnly = (value == "yes")
- case "browseable":
- currentShare.Browseable = (value == "yes")
- case "guest ok":
- currentShare.GuestOk = (value == "yes")
- }
- }
- }
- // Add the last share if there is one
- if currentShare != nil {
- shares = append(shares, *currentShare)
- }
- // Check for scanner errors
- if err := scanner.Err(); err != nil {
- return nil, err
- }
- return shares, nil
- }
- // AppendSambaShareConfig appends the Samba share configuration to smb.conf
- func (s *ShareManager) AppendSambaShareConfig(config string) error {
- file, err := os.OpenFile(s.SambaConfigPath, os.O_APPEND|os.O_WRONLY, 0644)
- if err != nil {
- return err
- }
- defer file.Close()
- if _, err := file.WriteString(config); err != nil {
- return err
- }
- return nil
- }
- // RemoveSambaShareConfig removes the Samba share configuration from smb.conf
- func (s *ShareManager) RemoveSambaShareConfig(shareName string) error {
- // Open the smb.conf file for reading
- file, err := os.Open(s.SambaConfigPath)
- if err != nil {
- return err
- }
- defer file.Close()
- // Create a temporary file to store modified smb.conf
- tmpFile, err := os.CreateTemp("", "smb.conf.*.tmp")
- if err != nil {
- return err
- }
- defer tmpFile.Close()
- // Create a scanner to read the smb.conf file line by line
- scanner := bufio.NewScanner(file)
- for scanner.Scan() {
- line := scanner.Text()
- // Check if the line contains the share name
- if strings.HasPrefix(line, "["+shareName+"]") {
- // Skip the lines until the next section
- for scanner.Scan() {
- if strings.HasPrefix(scanner.Text(), "[") {
- break
- }
- }
- continue // Skip writing the share configuration to the temporary file
- }
- // Write the line to the temporary file
- _, err := fmt.Fprintln(tmpFile, line)
- if err != nil {
- return err
- }
- }
- // Check for scanner errors
- if err := scanner.Err(); err != nil {
- return err
- }
- // Close the original smb.conf file
- if err := file.Close(); err != nil {
- return err
- }
- // Close the temporary file
- if err := tmpFile.Close(); err != nil {
- return err
- }
- // Replace the original smb.conf file with the temporary file
- if err := os.Rename(tmpFile.Name(), "/etc/samba/smb.conf"); err != nil {
- return err
- }
- return nil
- }
|