Debian Codegraph @

internal/maincmd/clientserver.go

raw symbols SCIP
1 package maincmd
2
3 import (
4 "bufio"
5 "context"
6 "fmt"
7 "io"
8 "net"
9 "strconv"
10 "strings"
11 "time"
12
13 "github.com/gokrazy/rsync"
14 "github.com/gokrazy/rsync/internal/restrict"
15 "github.com/gokrazy/rsync/internal/rsyncopts"
16 "github.com/gokrazy/rsync/internal/rsyncos"
17 "github.com/gokrazy/rsync/internal/rsyncstats"
18 )
19
20 // rsync/clientserver.c:start_socket_client
21 func socketClient(ctx context.Context, osenv *rsyncos.Env, opts *rsyncopts.Options, host string, remotePath string, port int, paths []string, roDirs, rwDirs []string) (*rsyncstats.TransferStats, error) {
22 if port < 0 {
23 if port := opts.RsyncPort(); port > 0 {
24 host += ":" + strconv.Itoa(port)
25 } else {
26 host += ":873" // rsync daemon port
27 }
28 } else {
29 host += ":" + strconv.Itoa(port)
30 }
31 dialer := net.Dialer{
32 // Prefer the Go resolver: We know which files it uses (which makes life
33 // easier for the restrict package), whereas the C resolver can be
34 // extended by host-specific plugins.
35 Resolver: &net.Resolver{
36 PreferGo: true,
37 },
38 }
39 timeoutStr := ""
40 if timeout := opts.ConnectTimeoutSeconds(); timeout > 0 {
41 dialer.Timeout = time.Duration(timeout) * time.Second
42 timeoutStr = fmt.Sprintf(" (timeout: %d seconds)", timeout)
43 }
44 dialFn := dialer.DialContext
45 if osenv.DialContext != nil {
46 dialFn = osenv.DialContext
47 osenv.Logf("Opening TCP connection to %s%s (via custom DialContext)", host, timeoutStr)
48 } else {
49 osenv.Logf("Opening TCP connection to %s%s", host, timeoutStr)
50 }
51 conn, err := dialFn(ctx, "tcp", host)
52 if err != nil {
53 return nil, err
54 }
55 defer conn.Close()
56
57 if osenv.Restrict() {
58 if err := restrict.MaybeFileSystem(roDirs, rwDirs); err != nil {
59 return nil, err
60 }
61 }
62 done, err := StartInbandExchange(osenv, opts, conn, remotePath)
63 if err != nil {
64 return nil, err
65 }
66 if done {
67 return nil, nil
68 }
69 stats, err := ClientRun(osenv, opts, conn, paths, false)
70 if err != nil {
71 return nil, err
72 }
73 return stats, nil
74 }
75
76 // rsync/clientserver.c:start_inband_exchange
77 func StartInbandExchange(osenv *rsyncos.Env, opts *rsyncopts.Options, conn io.ReadWriter, remotePath string) (done bool, _ error) {
78 module := remotePath
79 if idx := strings.IndexByte(module, '/'); idx > -1 {
80 module = module[:idx]
81 }
82 osenv.Logf("rsync module %q, path %q", module, remotePath)
83
84 rd := bufio.NewReader(conn)
85
86 // send client greeting
87 fmt.Fprintf(conn, "@RSYNCD: %d\n", rsync.ProtocolVersion)
88
89 // read server greeting
90 serverGreeting, err := rd.ReadString('\n')
91 if err != nil {
92 return false, fmt.Errorf("ReadString: %v", err)
93 }
94 serverGreeting = strings.TrimSpace(serverGreeting)
95 const serverGreetingPrefix = "@RSYNCD: "
96 if !strings.HasPrefix(serverGreeting, serverGreetingPrefix) {
97 return false, fmt.Errorf("invalid server greeting: got %q", serverGreeting)
98 }
99 // protocol negotiation: require at least version 27
100 serverGreeting = strings.TrimPrefix(serverGreeting, serverGreetingPrefix)
101 var remoteProtocol, remoteSub int32
102 if _, err := fmt.Sscanf(serverGreeting, "%d.%d", &remoteProtocol, &remoteSub); err != nil {
103 if _, err := fmt.Sscanf(serverGreeting, "%d", &remoteProtocol); err != nil {
104 return false, fmt.Errorf("reading server greeting: %v", err)
105 }
106 }
107 if remoteProtocol < 27 {
108 return false, fmt.Errorf("server version %d too old", remoteProtocol)
109 }
110
111 if opts.Verbose() {
112 osenv.Logf("(Client) Protocol versions: remote=%d, negotiated=%d", remoteProtocol, rsync.ProtocolVersion)
113 osenv.Logf("Client checksum: md4")
114 }
115
116 // send module name
117 fmt.Fprintf(conn, "%s\n", module)
118 for {
119 line, err := rd.ReadString('\n')
120 if err != nil {
121 return false, fmt.Errorf("did not get server startup line: %v", err)
122 }
123 line = strings.TrimSpace(line)
124 if opts.DebugGTE(rsyncopts.DEBUG_PROTO, 1) {
125 osenv.Logf("read line: %q", line)
126 }
127
128 if strings.HasPrefix(line, "@RSYNCD: AUTHREQD ") {
129 // TODO: implement support for authentication
130 return false, fmt.Errorf("authentication not yet implemented")
131 }
132
133 if line == "@RSYNCD: OK" {
134 break
135 }
136
137 if line == "@RSYNCD: EXIT" {
138 return true, nil
139 }
140
141 if strings.HasPrefix(line, "@ERROR") {
142 fmt.Fprintf(osenv.Stderr, "%s\n", line)
143 return false, fmt.Errorf("abort (rsync fatal error)")
144 }
145
146 if opts.OutputMOTD() {
147 // print rsync server message of the day (MOTD)
148 fmt.Fprintf(osenv.Stdout, "%s\n", line)
149 }
150 }
151
152 sargv := opts.ServerOptions()
153 sargv = append(sargv, ".")
154 sargv = append(sargv, remotePath)
155 if opts.Verbose() {
156 osenv.Logf("sending daemon args: %s", sargv)
157 }
158 for _, argv := range sargv {
159 fmt.Fprintf(conn, "%s\n", argv)
160 }
161 fmt.Fprintf(conn, "\n")
162
163 return false, nil
164 }