package samba import ( "bufio" "errors" "fmt" "io" "os" "path/filepath" "runtime" "strings" "time" "imuslab.com/arozos/mod/user" "imuslab.com/arozos/mod/utils" ) /* 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 //Config file for samba, aka smb.conf BackupDir string //Backup directory for restoring previous config UserHandler *user.UserHandler } type ShareConfig struct { Name string Path string ValidUsers []string ReadOnly bool Browseable bool GuestOk bool } func NewSambaShareManager() (*ShareManager, error) { if runtime.GOOS == "linux" { //Check if samba installed if !utils.FileExists("/bin/smbcontrol") { return nil, errors.New("samba not installed") } } else { return nil, errors.New("platform not supported") } return &ShareManager{ SambaConfigPath: "/etc/samba/smb.conf", BackupDir: "./backup", }, nil } // 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 } // Check if a share name is already used func (s *ShareManager) ShareNameExists(sharename string) (bool, error) { allShares, err := s.ReadSambaShares() if err != nil { return false, err } for _, share := range allShares { if strings.EqualFold(share.Name, sharename) { return true, nil } } return false, nil } // A basic filter to remove system created smb shares entry in the list func (s *ShareManager) FilterSystemCreatedShares(shares []ShareConfig) []ShareConfig { namesToRemove := []string{"global", "homes", "printers", "print$"} filteredShares := []ShareConfig{} for _, share := range shares { if !utils.StringInArray(namesToRemove, share.Name) { filteredShares = append(filteredShares, share) } } return filteredShares } // CreateNewSambaShare converts the shareConfig to string and appends it to smb.conf if the share name does not already exist func (s *ShareManager) CreateNewSambaShare(shareToCreate *ShareConfig) error { // Path to smb.conf smbConfPath := s.SambaConfigPath // Open the smb.conf file for reading file, err := os.Open(smbConfPath) if err != nil { return fmt.Errorf("failed to open smb.conf: %v", err) } defer file.Close() // Check if the share already exists scanner := bufio.NewScanner(file) shareExists := false shareNameSection := fmt.Sprintf("[%s]", shareToCreate.Name) for scanner.Scan() { if strings.TrimSpace(scanner.Text()) == shareNameSection { shareExists = true break } } if err := scanner.Err(); err != nil { return fmt.Errorf("failed to read smb.conf: %v", err) } if shareExists { return fmt.Errorf("share %s already exists", shareToCreate.Name) } // Convert ShareConfig to string shareConfigString := convertShareConfigToString(shareToCreate) // Open the smb.conf file for appending file, err = os.OpenFile(smbConfPath, os.O_APPEND|os.O_WRONLY, 0644) if err != nil { return fmt.Errorf("failed to open smb.conf for writing: %v", err) } defer file.Close() // Append the new share configuration if _, err := file.WriteString(shareConfigString); err != nil { return fmt.Errorf("failed to write to smb.conf: %v", 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 } // ShareExists checks if a given share name exists in smb.conf func (s *ShareManager) ShareExists(shareName string) (bool, error) { // Path to smb.conf smbConfPath := s.SambaConfigPath // Open the smb.conf file for reading file, err := os.Open(smbConfPath) if err != nil { return false, fmt.Errorf("failed to open smb.conf: %v", err) } defer file.Close() // Check if the share already exists scanner := bufio.NewScanner(file) shareNameSection := fmt.Sprintf("[%s]", shareName) for scanner.Scan() { if strings.TrimSpace(scanner.Text()) == shareNameSection { return true, nil } } if err := scanner.Err(); err != nil { return false, fmt.Errorf("failed to read smb.conf: %v", err) } return false, nil } // Backup the current smb.conf to the backup folder func (s *ShareManager) BackupSmbConf() error { // Define source and backup directory sourceFile := s.SambaConfigPath backupDir := s.BackupDir // Ensure the backup directory exists err := os.MkdirAll(backupDir, 0755) if err != nil { return fmt.Errorf("failed to create backup directory: %v", err) } // Create a timestamped backup filename timestamp := time.Now().Format("20060102_150405") backupFile := filepath.Join(backupDir, fmt.Sprintf("%s.smb.conf", timestamp)) // Open the source file src, err := os.Open(sourceFile) if err != nil { return fmt.Errorf("failed to open source file: %v", err) } defer src.Close() // Create the destination file dst, err := os.Create(backupFile) if err != nil { return fmt.Errorf("failed to create backup file: %v", err) } defer dst.Close() // Copy the contents of the source file to the backup file _, err = io.Copy(dst, src) if err != nil { return fmt.Errorf("failed to copy file contents: %v", err) } return nil } // Add a new user to smb.conf share by name func (s *ShareManager) AddUserToSambaShare(shareName, username string) error { // Open the smb.conf file for reading file, err := os.Open(s.SambaConfigPath) if err != nil { return fmt.Errorf("failed to open smb.conf: %v", err) } defer file.Close() var lines []string var insideShare bool var shareExists bool var userAdded bool scanner := bufio.NewScanner(file) for scanner.Scan() { line := scanner.Text() lines = append(lines, line) // Check if we are inside the specified share section if strings.HasPrefix(line, "[") && strings.HasSuffix(line, "]") { if insideShare && shareExists && !userAdded { // Add the username to the valid users list lines = append(lines, " valid users = @"+username) userAdded = true } insideShare = false } if strings.TrimSpace(line) == fmt.Sprintf("[%s]", shareName) { insideShare = true shareExists = true } if insideShare && strings.HasPrefix(strings.TrimSpace(line), "valid users =") { // Check if the username already exists in the valid users list validUsersLine := strings.TrimSpace(line) if !strings.Contains(validUsersLine, username) { lines[len(lines)-1] = validUsersLine + ", @" + username userAdded = true } } } if err := scanner.Err(); err != nil { return fmt.Errorf("error reading smb.conf: %v", err) } if !shareExists { return fmt.Errorf("share [%s] not found in smb.conf", shareName) } if !userAdded { // If no valid users line was found, add the username to the share lines = append(lines, fmt.Sprintf("[%s]", shareName)) lines = append(lines, " valid users = @"+username) } // Write the updated configuration back to the smb.conf file outputFile, err := os.Create(s.SambaConfigPath) if err != nil { return fmt.Errorf("failed to open smb.conf for writing: %v", err) } defer outputFile.Close() writer := bufio.NewWriter(outputFile) for _, line := range lines { _, err := writer.WriteString(line + "\n") if err != nil { return fmt.Errorf("error writing to smb.conf: %v", err) } } writer.Flush() return nil }