feat: add protected SMB disk-root sharing

This commit is contained in:
Meghdad committed 2026-10-16 15:37:29 +03:30
1 parent b9cb187f0d
commit 1ca321e312
18 files changed
+480 -21

No files matched your search

+12 -3
View File
@@ -28,11 +28,13 @@ type config struct {
Allow []string `json:"allow"`
ProtectedPaths []string `json:"protected_paths"`
PublicRoots []string `json:"public_roots"`
AllowStorageRoot bool `json:"allow_storage_root"`
AllowPublicOverlap bool `json:"allow_public_overlap"`
UID int `json:"uid"`
GID int `json:"gid"`
Diagnostics bool `json:"diagnostics"`
CaseInsensitive bool `json:"case_insensitive"`
protectedRelative []string
prefixes []netip.Prefix
ntHash [16]byte
ready func()
@@ -154,7 +156,7 @@ func (c *config) validate() error {
return errors.New("selected root is unavailable")
}
fi, err := os.Stat(c.Root)
if err != nil || !fi.IsDir() || c.Root == c.Mount || !within(c.Mount, c.Root) {
if err != nil || !fi.IsDir() || (c.Root == c.Mount && !c.AllowStorageRoot) || !within(c.Mount, c.Root) {
return errors.New("root must be an existing directory strictly below storage_mount; it is never created automatically")
}
if within(c.Root, c.path) {
@@ -174,8 +176,15 @@ func (c *config) validate() error {
} else if !os.IsNotExist(err) {
return errors.New("cannot resolve a protected path")
}
if within(c.Root, p) || within(p, c.Root) {
return errors.New("SMB root overlaps a private service or protected path")
if within(p, c.Root) {
return errors.New("SMB root is inside a private service or protected path")
}
if within(c.Root, p) {
if c.Root != c.Mount || !c.AllowStorageRoot {
return errors.New("SMB root overlaps a private service or protected path")
}
rel, _ := filepath.Rel(c.Root, p)
c.protectedRelative = append(c.protectedRelative, filepath.ToSlash(rel))
}
}
public := append([]string{filepath.Join(c.Mount, "public-storage")}, c.PublicRoots...)
+31
View File
@@ -129,3 +129,34 @@ func TestSecretFileAndOverlap(t *testing.T) {
}
}
}
func TestExplicitDiskRootView(t *testing.T) {
c, p := fixture(t)
c.Listen = "127.0.0.1:1445"
c.Share = "Storage"
c.UID = 65533
c.GID = 65533
c.Root = c.Mount
c.AllowStorageRoot = true
saveConfig(t, c, p)
if _, _, err := loadConfig([]string{"-config", p}); err == nil {
t.Fatal("root view must explicitly allow public overlap too")
}
c.AllowPublicOverlap = true
saveConfig(t, c, p)
got, _, err := loadConfig([]string{"-config", p})
if err != nil {
t.Fatal(err)
}
if len(got.protectedRelative) < 2 {
t.Fatal("disk root lacks mandatory service exclusions")
}
c.Root = filepath.Join(c.Mount, ".smb-service")
if err := os.Mkdir(c.Root, 0700); err != nil {
t.Fatal(err)
}
saveConfig(t, c, p)
if _, _, err := loadConfig([]string{"-config", p}); err == nil {
t.Fatal("protected service directory became a share")
}
}
+1 -1
View File
@@ -108,7 +108,7 @@ func serve(ctx context.Context, c config) error {
if c.Diagnostics {
diagnostic = log.Printf
}
err = smb.Serve(ctx, ln, smb.Config{ExpectedRoot: initial, Ready: func() {
err = smb.Serve(ctx, ln, smb.Config{ProtectedPaths: c.protectedRelative, ExpectedRoot: initial, Ready: func() {
log.Print("SMB ready; authenticated signing required; read_only=", c.ReadOnly)
if c.ready != nil {
c.ready()