123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322 |
- package samba
- import (
- "bufio"
- "errors"
- "fmt"
- "io"
- "os"
- "path/filepath"
- "runtime"
- "strings"
- "time"
- "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
- }
- 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
- }
|