required.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package samba
  2. import (
  3. "errors"
  4. "fmt"
  5. "log"
  6. "os/exec"
  7. "os/user"
  8. "strings"
  9. "imuslab.com/arozos/mod/fileservers"
  10. )
  11. /*
  12. Functions requested by the file server service router
  13. */
  14. func (m *ShareManager) ServerToggle(enabled bool) error {
  15. return errors.New("not supported")
  16. }
  17. func (m *ShareManager) IsEnabled() bool {
  18. smbdRunning, err := checkSmbdRunning()
  19. if err != nil {
  20. log.Println("Unable to get smbd state: " + err.Error())
  21. return false
  22. }
  23. return smbdRunning
  24. }
  25. func (m *ShareManager) GetEndpoints(userinfo *user.User) []*fileservers.Endpoint {
  26. eps := []*fileservers.Endpoint{}
  27. eps = append(eps, &fileservers.Endpoint{
  28. ProtocolName: "//",
  29. Port: 0,
  30. Subpath: "/" + userinfo.Username,
  31. })
  32. return eps
  33. }
  34. func checkSmbdRunning() (bool, error) {
  35. // Run the system command to check the smbd service status
  36. cmd := exec.Command("systemctl", "is-active", "--quiet", "smbd")
  37. err := cmd.Run()
  38. // If the command exits with status 0, smbd is running
  39. if err == nil {
  40. return true, nil
  41. }
  42. // If the command exits with a non-zero status, smbd is not running
  43. // We can check the error message to be sure it's not another issue
  44. if exitError, ok := err.(*exec.ExitError); ok {
  45. if strings.TrimSpace(string(exitError.Stderr)) == "" {
  46. return false, nil
  47. }
  48. return false, fmt.Errorf("error checking smbd status: %s", exitError.Stderr)
  49. }
  50. return false, fmt.Errorf("unexpected error checking smbd status: %v", err)
  51. }