path.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package localapi
  2. // DirName is the directory under /var/run that holds nebula's sockets by default.
  3. const DirName = "nebula"
  4. // CtlEnabledKey is the optional config key that turns the control socket off. It is on
  5. // unless the config says false, and reloadable: a reload that turns it off removes the
  6. // socket, and one that turns it back on brings it up again.
  7. const CtlEnabledKey = "local_api.control.enabled"
  8. // ControlEnabled reports whether a config leaves the control socket on. nebula ctl reads
  9. // it too, so a client pointed at a config that turned the socket off says so rather than
  10. // failing to dial.
  11. func ControlEnabled(c interface{ GetBool(string, bool) bool }) bool {
  12. return c.GetBool(CtlEnabledKey, true)
  13. }
  14. // CtlSocketKey is the optional config key naming the control socket. Unset, the socket
  15. // is DefaultSocketPath, one fixed path per platform that both the daemon and nebula ctl
  16. // know, which is what lets the endpoint work with no configuration on either side. It
  17. // suits a daemon running as root, which is what the tun device needs anyway. A daemon run as another user, or a second daemon on
  18. // one host, names its own. The CLI reads the same key, so when both sides see the same
  19. // config file they agree on the socket with nothing more said.
  20. const CtlSocketKey = "local_api.control.socket"
  21. // CtlGroupKey is the optional config key naming a group that shares the control socket.
  22. // Unset, the socket admits the account nebula runs as and nobody else. Set, the socket
  23. // is handed to the group, 0660 on unix and an entry in the pipe's security descriptor on
  24. // windows; see Access and Server.trustSocket.
  25. const CtlGroupKey = "local_api.control.group"
  26. // controlAccessFrom returns the control socket's access a config implies: the group
  27. // CtlGroupKey names, if any, and the mode that goes with having one or not.
  28. func controlAccessFrom(c interface{ GetString(string, string) string }) (Access, error) {
  29. group, err := groupFromConfig(c, CtlGroupKey)
  30. if err != nil {
  31. return Access{}, err
  32. }
  33. if group.ID == "" {
  34. return ownerOnly(), nil
  35. }
  36. return Access{Mode: groupSocketMode, Group: group}, nil
  37. }
  38. // SocketPathFromConfig returns the control socket path a config implies: CtlSocketKey
  39. // when it is set, else DefaultSocketPath. A configured path must be absolute, so that
  40. // the daemon and nebula ctl, started from wherever the operator happens to be, name the
  41. // same socket from one config, and one this platform can bind; see checkSocketPath.
  42. func SocketPathFromConfig(c interface{ GetString(string, string) string }) (string, error) {
  43. override := c.GetString(CtlSocketKey, "")
  44. if override == "" {
  45. return DefaultSocketPath, nil
  46. }
  47. if err := checkSocketPath(CtlSocketKey, override); err != nil {
  48. return "", err
  49. }
  50. return override, nil
  51. }