| 1 | package ipacl_test |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "net" |
| 6 | "os" |
| 7 | "os/exec" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | |
| 12 | "github.com/gokrazy/rsync/internal/rsyncdconfig" |
| 13 | "github.com/gokrazy/rsync/internal/rsynctest" |
| 14 | "github.com/gokrazy/rsync/internal/testlogger" |
| 15 | ) |
| 16 | |
| 17 | type connWithRemoteAddrListener struct { |
| 18 | net.Listener |
| 19 | |
| 20 | remoteAddr net.Addr |
| 21 | } |
| 22 | |
| 23 | func (l *connWithRemoteAddrListener) Accept() (net.Conn, error) { |
| 24 | conn, err := l.Listener.Accept() |
| 25 | if err != nil { |
| 26 | return nil, err |
| 27 | } |
| 28 | return &connWithRemoteAddr{ |
| 29 | Conn: conn, |
| 30 | remoteAddr: l.remoteAddr, |
| 31 | }, nil |
| 32 | } |
| 33 | |
| 34 | type connWithRemoteAddr struct { |
| 35 | net.Conn |
| 36 | |
| 37 | remoteAddr net.Addr |
| 38 | } |
| 39 | |
| 40 | func (c *connWithRemoteAddr) RemoteAddr() net.Addr { |
| 41 | return c.remoteAddr |
| 42 | } |
| 43 | |
| 44 | func TestIPACL(t *testing.T) { |
| 45 | rsyncBin := rsynctest.TridgeOrGTFO(t, "TODO: reason") |
| 46 | |
| 47 | tmp := t.TempDir() |
| 48 | source := filepath.Join(tmp, "source") |
| 49 | dest := filepath.Join(tmp, "dest") |
| 50 | // create files in source to be copied |
| 51 | if err := os.MkdirAll(source, 0755); err != nil { |
| 52 | t.Fatal(err) |
| 53 | } |
| 54 | |
| 55 | cfg, err := rsyncdconfig.FromString(` |
| 56 | [[module]] |
| 57 | name = "interop" |
| 58 | path = '` + source + `' |
| 59 | acl = [ |
| 60 | "allow 192.168.1.0/24", |
| 61 | "allow 2001:db8::1/32", |
| 62 | "deny all" |
| 63 | ] |
| 64 | |
| 65 | `) |
| 66 | if err != nil { |
| 67 | t.Fatal(err) |
| 68 | } |
| 69 | |
| 70 | for _, tt := range []struct { |
| 71 | remoteAddr string |
| 72 | wantError bool |
| 73 | }{ |
| 74 | { |
| 75 | remoteAddr: "192.168.1.1", |
| 76 | wantError: false, |
| 77 | }, |
| 78 | { |
| 79 | remoteAddr: "2001:db8::1234", |
| 80 | wantError: false, |
| 81 | }, |
| 82 | { |
| 83 | remoteAddr: "10.0.0.1", |
| 84 | wantError: true, |
| 85 | }, |
| 86 | } { |
| 87 | t.Run(tt.remoteAddr, func(t *testing.T) { |
| 88 | t.Parallel() |
| 89 | |
| 90 | ln, err := net.Listen("tcp", "localhost:0") |
| 91 | if err != nil { |
| 92 | t.Fatal(err) |
| 93 | } |
| 94 | ln = &connWithRemoteAddrListener{ |
| 95 | Listener: ln, |
| 96 | remoteAddr: &net.TCPAddr{ |
| 97 | IP: net.ParseIP(tt.remoteAddr), |
| 98 | Port: 1234, |
| 99 | }, |
| 100 | } |
| 101 | srv := rsynctest.New(t, cfg.Modules, rsynctest.Listener(ln)) |
| 102 | |
| 103 | // request module list: this should work regardless of the source IP |
| 104 | var buf bytes.Buffer |
| 105 | rsync := exec.Command(rsyncBin, |
| 106 | // "--debug=all4", |
| 107 | "--archive", |
| 108 | "-v", "-v", "-v", "-v", |
| 109 | "--port="+srv.Port, |
| 110 | "rsync://localhost") |
| 111 | rsync.Stdout = &buf |
| 112 | rsync.Stderr = testlogger.New(t) |
| 113 | if err := rsync.Run(); err != nil { |
| 114 | t.Fatalf("%v: %v", rsync.Args, err) |
| 115 | } |
| 116 | |
| 117 | output := buf.String() |
| 118 | if want := "interop\tinterop"; !strings.Contains(output, want) { |
| 119 | t.Fatalf("rsync output unexpectedly did not contain %q:\n%s", want, output) |
| 120 | } |
| 121 | |
| 122 | buf.Reset() |
| 123 | // actually transfer the interop module (in dry-run mode) to trigger |
| 124 | // the ACL check dry run (slight differences in protocol) |
| 125 | rsync = exec.Command(rsynctest.AnyRsync(t), |
| 126 | // "--debug=all4", |
| 127 | "--archive", |
| 128 | "-v", "-v", "-v", "-v", |
| 129 | "--port="+srv.Port, |
| 130 | "--dry-run", |
| 131 | "rsync://localhost/interop/", // copy contents of interop |
| 132 | //source+"/", // sync from local directory |
| 133 | filepath.Base(dest)) // directly into dest |
| 134 | rsync.Dir = filepath.Dir(dest) |
| 135 | rsync.Stdout = testlogger.New(t) |
| 136 | rsync.Stderr = &buf |
| 137 | if err := rsync.Run(); err != nil { |
| 138 | if !tt.wantError { |
| 139 | t.Fatalf("%v: %v", rsync.Args, err) |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | if tt.wantError { |
| 144 | if !strings.Contains(buf.String(), "@ERROR: access denied") { |
| 145 | t.Fatalf("expected access denied error, got: %q", buf.String()) |
| 146 | } |
| 147 | } |
| 148 | }) |
| 149 | } |
| 150 | } |