required.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package samba
  2. import (
  3. "fmt"
  4. "log"
  5. "os/exec"
  6. "strings"
  7. "imuslab.com/arozos/mod/fileservers"
  8. "imuslab.com/arozos/mod/user"
  9. )
  10. /*
  11. Functions requested by the file server service router
  12. */
  13. func (m *ShareManager) ServerToggle(enabled bool) error {
  14. return SetSmbdEnableState(enabled)
  15. }
  16. func (m *ShareManager) IsEnabled() bool {
  17. smbdRunning, err := checkSmbdRunning()
  18. if err != nil {
  19. log.Println("Unable to get smbd state: " + err.Error())
  20. return false
  21. }
  22. return smbdRunning
  23. }
  24. func (m *ShareManager) GetEndpoints(userInfo *user.User) []*fileservers.Endpoint {
  25. //Get a list of connection endpoint for this user
  26. eps := []*fileservers.Endpoint{}
  27. for _, fsh := range userInfo.GetAllAccessibleFileSystemHandler() {
  28. if fsh.IsNetworkDrive() {
  29. continue
  30. }
  31. fshID := fsh.UUID
  32. if fsh.RequierUserIsolation() {
  33. //User seperated storage. Only mount the user one
  34. fshID = userInfo.Username + "_" + fsh.UUID
  35. }
  36. targetShare, err := m.GetShareByName(fshID)
  37. if err != nil {
  38. continue
  39. }
  40. userCanAccess := m.UserCanAccessShare(targetShare, userInfo.Username)
  41. if err != nil || !userCanAccess {
  42. continue
  43. }
  44. eps = append(eps, &fileservers.Endpoint{
  45. ProtocolName: "//",
  46. Port: 0,
  47. Subpath: "/" + fshID,
  48. })
  49. }
  50. return eps
  51. }
  52. func checkSmbdRunning() (bool, error) {
  53. // Run the system command to check the smbd service status
  54. cmd := exec.Command("systemctl", "is-active", "--quiet", "smbd")
  55. err := cmd.Run()
  56. // If the command exits with status 0, smbd is running
  57. if err == nil {
  58. return true, nil
  59. }
  60. // If the command exits with a non-zero status, smbd is not running
  61. // We can check the error message to be sure it's not another issue
  62. if exitError, ok := err.(*exec.ExitError); ok {
  63. if strings.TrimSpace(string(exitError.Stderr)) == "" {
  64. return false, nil
  65. }
  66. return false, fmt.Errorf("error checking smbd status: %s", exitError.Stderr)
  67. }
  68. return false, fmt.Errorf("unexpected error checking smbd status: %v", err)
  69. }