helpers.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package samba
  2. import (
  3. "fmt"
  4. "os/exec"
  5. "path/filepath"
  6. "strings"
  7. "unicode"
  8. )
  9. // convertShareConfigToString converts a ShareConfig to its string representation for smb.conf
  10. func convertShareConfigToString(share *ShareConfig) string {
  11. var builder strings.Builder
  12. builder.WriteString(fmt.Sprintf("\n[%s]\n", share.Name))
  13. builder.WriteString(fmt.Sprintf("\tpath = %s\n", share.Path))
  14. if len(share.ValidUsers) > 0 {
  15. builder.WriteString(fmt.Sprintf("\tvalid users = %s\n", strings.Join(share.ValidUsers, " ")))
  16. }
  17. builder.WriteString(fmt.Sprintf("\tread only = %s\n", boolToYesNo(share.ReadOnly)))
  18. builder.WriteString(fmt.Sprintf("\tbrowseable = %s\n", boolToYesNo(share.Browseable)))
  19. builder.WriteString(fmt.Sprintf("\tguest ok = %s\n", boolToYesNo(share.GuestOk)))
  20. return builder.String()
  21. }
  22. // boolToYesNo converts a boolean to "yes" or "no"
  23. func boolToYesNo(value bool) string {
  24. if value {
  25. return "yes"
  26. }
  27. return "no"
  28. }
  29. // RestartSmbd restarts the smbd service using systemctl
  30. func restartSmbd() error {
  31. cmd := exec.Command("sudo", "systemctl", "restart", "smbd")
  32. output, err := cmd.CombinedOutput()
  33. if err != nil {
  34. return fmt.Errorf("failed to restart smbd: %v - %s", err, output)
  35. }
  36. return nil
  37. }
  38. // Check if a samba username exists (unix username only)
  39. func (s *ShareManager) SambaUserExists(username string) (bool, error) {
  40. userInfos, err := s.ListSambaUsersInfo()
  41. if err != nil {
  42. return false, err
  43. }
  44. for _, userInfo := range userInfos {
  45. if userInfo.UnixUsername == username {
  46. return true, nil
  47. }
  48. }
  49. return false, nil
  50. }
  51. // List of important folders not to be shared via SMB
  52. var importantFolders = []string{
  53. "/bin",
  54. "/boot",
  55. "/dev",
  56. "/etc",
  57. "/lib",
  58. "/lib64",
  59. "/proc",
  60. "/root",
  61. "/sbin",
  62. "/sys",
  63. "/tmp",
  64. "/usr",
  65. "/var",
  66. }
  67. // IsPathInsideImportantFolders checks if the given path is inside one of the important folders
  68. func isPathInsideImportantFolders(path string) bool {
  69. // Clean the given path
  70. cleanedPath := filepath.Clean(path)
  71. // Iterate over the important folders
  72. for _, folder := range importantFolders {
  73. // Clean the important folder path
  74. cleanedFolder := filepath.Clean(folder)
  75. // Check if the cleaned path is inside the cleaned folder
  76. if strings.HasPrefix(cleanedPath, cleanedFolder) {
  77. return true
  78. }
  79. }
  80. return false
  81. }
  82. // Clean and make sure the share name is valid
  83. func sanitizeShareName(input string) string {
  84. var result strings.Builder
  85. for _, char := range input {
  86. if unicode.IsLetter(char) || unicode.IsDigit(char) {
  87. result.WriteRune(char)
  88. } else if unicode.IsSpace(char) {
  89. result.WriteRune(' ')
  90. }
  91. }
  92. return result.String()
  93. }