package samba import ( "fmt" "os/exec" "path/filepath" "strings" "unicode" ) // convertShareConfigToString converts a ShareConfig to its string representation for smb.conf func convertShareConfigToString(share *ShareConfig) string { var builder strings.Builder builder.WriteString(fmt.Sprintf("\n[%s]\n", share.Name)) builder.WriteString(fmt.Sprintf("\tpath = %s\n", share.Path)) if len(share.ValidUsers) > 0 { builder.WriteString(fmt.Sprintf("\tvalid users = %s\n", strings.Join(share.ValidUsers, " "))) } builder.WriteString(fmt.Sprintf("\tread only = %s\n", boolToYesNo(share.ReadOnly))) builder.WriteString(fmt.Sprintf("\tbrowseable = %s\n", boolToYesNo(share.Browseable))) builder.WriteString(fmt.Sprintf("\tguest ok = %s\n", boolToYesNo(share.GuestOk))) return builder.String() } // boolToYesNo converts a boolean to "yes" or "no" func boolToYesNo(value bool) string { if value { return "yes" } return "no" } // RestartSmbd restarts the smbd service using systemctl func restartSmbd() error { cmd := exec.Command("sudo", "systemctl", "restart", "smbd") output, err := cmd.CombinedOutput() if err != nil { return fmt.Errorf("failed to restart smbd: %v - %s", err, output) } return nil } // Check if a samba username exists (unix username only) func (s *ShareManager) SambaUserExists(username string) (bool, error) { userInfos, err := s.ListSambaUsersInfo() if err != nil { return false, err } for _, userInfo := range userInfos { if userInfo.UnixUsername == username { return true, nil } } return false, nil } // List of important folders not to be shared via SMB var importantFolders = []string{ "/bin", "/boot", "/dev", "/etc", "/lib", "/lib64", "/proc", "/root", "/sbin", "/sys", "/tmp", "/usr", "/var", } // IsPathInsideImportantFolders checks if the given path is inside one of the important folders func isPathInsideImportantFolders(path string) bool { // Clean the given path cleanedPath := filepath.Clean(path) // Iterate over the important folders for _, folder := range importantFolders { // Clean the important folder path cleanedFolder := filepath.Clean(folder) // Check if the cleaned path is inside the cleaned folder if strings.HasPrefix(cleanedPath, cleanedFolder) { return true } } return false } // Clean and make sure the share name is valid func sanitizeShareName(input string) string { var result strings.Builder for _, char := range input { if unicode.IsLetter(char) || unicode.IsDigit(char) { result.WriteRune(char) } else if unicode.IsSpace(char) { result.WriteRune(' ') } } return result.String() }