Debian Codegraph @

internal/maincmd/maincmd.go

raw symbols SCIP
1 // Package maincmd implements a subset of the '$ rsync' CLI surface, namely that it can:
2 // - serve as a server daemon over TCP or SSH (via SSH session stdin/stdout)
3 // - act as "client" CLI for connecting to the server
4 // - Not yet implemented: both "client" and "server" can act as the sender and the receiver
5 package maincmdscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/maincmd`/
6
7 import (
8 "contextscip-go gomod github.com/golang/go/src go1.25.0 context/"
9 "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/"
10 "ioscip-go gomod github.com/golang/go/src go1.25.0 io/"
11 "netscip-go gomod github.com/golang/go/src go1.25.0 net/"
12 "net/httpscip-go gomod github.com/golang/go/src go1.25.0 `net/http`/"
13 "osscip-go gomod github.com/golang/go/src go1.25.0 os/"
14 "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/"
15
16 "github.com/gokrazy/rsync/internal/anonsshscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/anonssh`/"
17 "github.com/gokrazy/rsync/internal/restrictscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/restrict`/"
18 "github.com/gokrazy/rsync/internal/rsyncdconfigscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/"
19 "github.com/gokrazy/rsync/internal/rsyncoptsscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/"
20 "github.com/gokrazy/rsync/internal/rsyncosscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/"
21 "github.com/gokrazy/rsync/internal/rsyncstatsscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncstats`/"
22 "github.com/gokrazy/rsync/rsyncdscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncd`/"
23
24 // For profiling and debugging
25 _ "net/http/pprofscip-go gomod github.com/golang/go/src go1.25.0 `net/http/pprof`/"
26 )
27
28 func versionscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/maincmd`/version().(osenv *rsyncosscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/.Envscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/Env#) {
29 osenv.Logfscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/Env#Logf().("gokrazy rsync, pid %d", osscip-go gomod github.com/golang/go/src go1.25.0 os/.Getpidscip-go gomod github.com/golang/go/src go1.25.0 os/Getpid().())
30 }
31
32 type readWriteCloserscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/maincmd`/readWriteCloser# struct {
33 rscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/maincmd`/readWriteCloser#r. ioscip-go gomod github.com/golang/go/src go1.25.0 io/.Readerscip-go gomod github.com/golang/go/src go1.25.0 io/Reader#
34 wscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/maincmd`/readWriteCloser#w. ioscip-go gomod github.com/golang/go/src go1.25.0 io/.Writerscip-go gomod github.com/golang/go/src go1.25.0 io/Writer#
35 cscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/maincmd`/readWriteCloser#c. ioscip-go gomod github.com/golang/go/src go1.25.0 io/.Closerscip-go gomod github.com/golang/go/src go1.25.0 io/Closer#
36 }
37
38 func (r *readWriteCloserscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/maincmd`/readWriteCloser#) Readscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/maincmd`/readWriteCloser#Read().(p []byte) (n int, err error) { return r.rscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/maincmd`/readWriteCloser#r..Readscip-go gomod github.com/golang/go/src go1.25.0 io/Reader#Read().(p) }
39 func (r *readWriteCloserscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/maincmd`/readWriteCloser#) Writescip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/maincmd`/readWriteCloser#Write().(p []byte) (n int, err error) { return r.wscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/maincmd`/readWriteCloser#w..Writescip-go gomod github.com/golang/go/src go1.25.0 io/Writer#Write().(p) }
40 func (r *readWriteCloserscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/maincmd`/readWriteCloser#) Closescip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/maincmd`/readWriteCloser#Close().() error { return r.cscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/maincmd`/readWriteCloser#c..Closescip-go gomod github.com/golang/go/src go1.25.0 io/Closer#Close().() }
41
42 func Mainscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/maincmd`/Main().(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, osenv *rsyncosscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/.Envscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/Env#, args []string, cfg *rsyncdconfigscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/.Configscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/Config#) (*rsyncstatsscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncstats`/.TransferStatsscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncstats`/TransferStats#, error) {
43 osenv.Logfscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/Env#Logf().("Main(osenv=%v, args=%q)", osenv, args)
44 pc := rsyncoptsscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/.NewContextscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/NewContext().(rsyncoptsscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/.NewOptionsWithGokrazyDefaultsscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/NewOptionsWithGokrazyDefaults().(osenv))
45 if err := pc.ParseArgumentsscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/Context#ParseArguments().(osenv, args[1:]); err != nil {
46 if pe, ok := err.(*rsyncoptsscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/.PoptErrorscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/PoptError#); ok &&
47 pe.Errnoscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/PoptError#Errno. == rsyncoptsscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/.POPT_ERROR_BADOPTscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/POPT_ERROR_BADOPT. &&
48 stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasPrefix().(pe.Errorscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/PoptError#Error().(), "--gokr.") {
49 return nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("%v (you need to specify --daemon before flags starting with --gokr are available)", pe)
50 }
51 return nil, err
52 }
53 opts := pc.Optionsscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/Context#Options.
54 remaining := pc.RemainingArgsscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/Context#RemainingArgs.
55 // osenv.Logf("remaining: %v", remaining)
56
57 // calling convention: daemon mode over remote shell (also builtin SSH)
58 // Example: --server --daemon .
59 if opts.Daemonscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/Options#Daemon().() && opts.Serverscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/Options#Server().() {
60 // start_daemon()
61 if cfg == nil {
62 var err error
63 cfg, _, err = rsyncdconfigscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/.FromDefaultFilesscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/FromDefaultFiles().()
64 if err != nil {
65 return nil, err
66 }
67 }
68 rsyncdOpts := []rsyncdscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncd`/.Optionscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncd`/Option#{
69 rsyncdscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncd`/.WithStderrscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncd`/WithStderr().(osenv.Stderrscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/Env#Stderr.),
70 }
71 if osenv.DontRestrictscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/Env#DontRestrict. {
72 rsyncdOpts = append(rsyncdOpts, rsyncdscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncd`/.DontRestrictscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncd`/DontRestrict().())
73 }
74 srv, err := rsyncdscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncd`/.NewServerscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncd`/NewServer().(cfg.Modulesscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/Config#Modules., rsyncdOpts...)
75 if err != nil {
76 return nil, err
77 }
78 conn := rsyncdscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncd`/.NewConnectionscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncd`/NewConnection().(osenv.Stdinscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/Env#Stdin., osenv.Stdoutscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/Env#Stdout., "<remote-shell-daemon>")
79 return nil, srv.HandleDaemonConnscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncd`/Server#HandleDaemonConn().(ctx, conn)
80 }
81
82 // calling convention: command mode (over remote shell or locally)
83 // Example: --server --sender -vvvvlogDtpre.iLsfxCIvu . .
84 if opts.Serverscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/Options#Server().() {
85 // start_server()
86 srv, err := rsyncdscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncd`/.NewServerscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncd`/NewServer().(nil, rsyncdscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncd`/.WithStderrscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncd`/WithStderr().(osenv.Stderrscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/Env#Stderr.))
87 if err != nil {
88 return nil, err
89 }
90
91 // TODO: remove duplication with handleDaemonConn
92 if len(remaining) < 2 {
93 return nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("invalid args: at least one directory required")
94 }
95 if got, want := remaining[0], "."; got != want {
96 return nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("protocol error: got %q, expected %q", got, want)
97 }
98 paths := remaining[1:]
99 if opts.Verbosescip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/Options#Verbose().() {
100 osenv.Logfscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/Env#Logf().("paths: %q", paths)
101 }
102 var roDirs, rwDirs []string
103 if opts.Senderscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/Options#Sender().() {
104 roDirs = append(roDirs, paths...)
105 } else {
106 for _, path := range paths {
107 if err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.MkdirAllscip-go gomod github.com/golang/go/src go1.25.0 os/MkdirAll().(path, 0755); err != nil {
108 return nil, err
109 }
110 }
111 rwDirs = append(rwDirs, paths...)
112 }
113 if osenv.Restrictscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/Env#Restrict().() {
114 if err := restrictscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/restrict`/.MaybeFileSystemscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/restrict`/MaybeFileSystem().(roDirs, rwDirs); err != nil {
115 return nil, err
116 }
117 }
118 conn := rsyncdscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncd`/.NewConnectionscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncd`/NewConnection().(osenv.Stdinscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/Env#Stdin., osenv.Stdoutscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/Env#Stdout., "<remote-shell>")
119 return nil, srv.InternalHandleConnscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncd`/Server#InternalHandleConn().(ctx, conn, nil, pc)
120 }
121
122 if !opts.Daemonscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/Options#Daemon().() {
123 if !osenv.DontRestrictscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/Env#DontRestrict. {
124 osenv.DontRestrictscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/Env#DontRestrict. = opts.GokrazyClientscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/Options#GokrazyClient..DontRestrictscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/GokrazyClientOptions#DontRestrict. == 1
125 }
126 return clientMainscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/maincmd`/clientMain().(ctx, osenv, opts, remaining)
127 }
128
129 // daemon_main()
130
131 // calling convention: start a daemon in TCP listening mode (or with systemd
132 // socket activation)
133
134 var cfgfn string
135 var cfgErr error
136 if cfg == nil {
137 if opts.GokrazyDaemonscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/Options#GokrazyDaemon..Configscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/GokrazyDaemonOptions#Config. != "" {
138 cfgfn = opts.GokrazyDaemonscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/Options#GokrazyDaemon..Configscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/GokrazyDaemonOptions#Config.
139 cfg, cfgErr = rsyncdconfigscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/.FromFilescip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/FromFile().(cfgfn)
140 } else {
141 cfg, cfgfn, cfgErr = rsyncdconfigscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/.FromDefaultFilesscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/FromDefaultFiles().()
142 }
143 if cfgErr != nil {
144 if osscip-go gomod github.com/golang/go/src go1.25.0 os/.IsNotExistscip-go gomod github.com/golang/go/src go1.25.0 os/IsNotExist().(cfgErr) {
145 osenv.Logfscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/Env#Logf().("config file not found, relying on flags")
146 // a non-existant config file is not an error: users can start
147 // gokr-rsyncd with e.g. the -gokr.listen and -gokr.modulemap flags.
148 cfg = &rsyncdconfigscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/.Configscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/Config#{
149 Listenersscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/Config#Listeners.: []rsyncdconfigscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/.Listenerscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/Listener#{
150 {
151 Rsyncdscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/Listener#Rsyncd.: opts.GokrazyDaemonscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/Options#GokrazyDaemon..Listenscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/GokrazyDaemonOptions#Listen.,
152 AnonSSHscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/Listener#AnonSSH.: opts.GokrazyDaemonscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/Options#GokrazyDaemon..AnonSSHListenscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/GokrazyDaemonOptions#AnonSSHListen.,
153 },
154 },
155 Modulesscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/Config#Modules.: []rsyncdscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncd`/.Modulescip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncd`/Module#{},
156 }
157 } else {
158 return nil, cfgErr
159 }
160 } else {
161 osenv.Logfscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/Env#Logf().("config file %s loaded", cfgfn)
162 }
163 }
164
165 if osscip-go gomod github.com/golang/go/src go1.25.0 os/.IsNotExistscip-go gomod github.com/golang/go/src go1.25.0 os/IsNotExist().(cfgErr) {
166 if opts.GokrazyDaemonscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/Options#GokrazyDaemon..Listenscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/GokrazyDaemonOptions#Listen. == "" &&
167 opts.GokrazyDaemonscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/Options#GokrazyDaemon..AnonSSHListenscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/GokrazyDaemonOptions#AnonSSHListen. == "" {
168 return nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("neither -gokr.listen nor -gokr.anonssh_listen specified, and config file not found: %v", cfgErr)
169 }
170 // If no config file was found, and the user did not specify a
171 // -gokr.modulemap flag, use a default value to force the user to
172 // configure a module map.
173 if opts.GokrazyDaemonscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/Options#GokrazyDaemon..ModuleMapscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/GokrazyDaemonOptions#ModuleMap. == "" {
174 opts.GokrazyDaemonscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/Options#GokrazyDaemon..ModuleMapscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/GokrazyDaemonOptions#ModuleMap. = "nonex=/nonexistant/path"
175 }
176 } else {
177 if len(cfg.Listenersscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/Config#Listeners.) == 0 ||
178 (cfg.Listenersscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/Config#Listeners.[0].Rsyncdscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/Listener#Rsyncd. == "" &&
179 cfg.Listenersscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/Config#Listeners.[0].AnonSSHscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/Listener#AnonSSH. == "" &&
180 cfg.Listenersscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/Config#Listeners.[0].AuthorizedSSHscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/Listener#AuthorizedSSH..Addressscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/SSHListener#Address. == "") {
181 return nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("no rsyncd listeners configured, add a [[listener]] to %s", cfgfn)
182 }
183 }
184 // TODO: loosen this restriction, create multiple listeners
185
186 if len(cfg.Listenersscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/Config#Listeners.) != 1 ||
187 (cfg.Listenersscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/Config#Listeners.[0].Rsyncdscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/Listener#Rsyncd. == "" &&
188 cfg.Listenersscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/Config#Listeners.[0].AnonSSHscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/Listener#AnonSSH. == "" &&
189 cfg.Listenersscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/Config#Listeners.[0].AuthorizedSSHscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/Listener#AuthorizedSSH..Addressscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/SSHListener#Address. == "") {
190 return nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("not precisely 1 rsyncd listener specified")
191 }
192
193 var sshListener *anonsshscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/anonssh`/.Listenerscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/anonssh`/Listener#
194 listenAddr := cfg.Listenersscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/Config#Listeners.[0].Rsyncdscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/Listener#Rsyncd.
195 if listenAddr == "" {
196 listenAddr = cfg.Listenersscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/Config#Listeners.[0].AnonSSHscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/Listener#AnonSSH.
197 if listenAddr == "" {
198 listenAddr = cfg.Listenersscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/Config#Listeners.[0].AuthorizedSSHscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/Listener#AuthorizedSSH..Addressscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/SSHListener#Address.
199 var err error
200 sshListener, err = anonsshscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/anonssh`/.ListenerFromConfigscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/anonssh`/ListenerFromConfig().(osenv, cfg.Listenersscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/Config#Listeners.[0])
201 if err != nil {
202 return nil, err
203 }
204 } else {
205 var err error
206 sshListener, err = anonsshscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/anonssh`/.ListenerFromConfigscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/anonssh`/ListenerFromConfig().(osenv, cfg.Listenersscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/Config#Listeners.[0])
207 if err != nil {
208 return nil, err
209 }
210 }
211 }
212
213 if moduleMap := opts.GokrazyDaemonscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/Options#GokrazyDaemon..ModuleMapscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/GokrazyDaemonOptions#ModuleMap.; moduleMap != "" {
214 parts := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Splitscip-go gomod github.com/golang/go/src go1.25.0 strings/Split().(moduleMap, "=")
215 if len(parts) != 2 {
216 return nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("malformed -gokr.modulemap parameter %q, expected <modulename>=<path>", moduleMap)
217 }
218 module := rsyncdscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncd`/.Modulescip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncd`/Module#{
219 Namescip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncd`/Module#Name.: parts[0],
220 Pathscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncd`/Module#Path.: parts[1],
221 }
222 cfg.Modulesscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/Config#Modules. = append(cfg.Modulesscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/Config#Modules., module)
223 }
224 if cfg.DontNamespacescip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/Config#DontNamespace. {
225 if cfg.Listenersscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/Config#Listeners.[0].Rsyncdscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/Listener#Rsyncd. != "" ||
226 cfg.Listenersscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/Config#Listeners.[0].AnonSSHscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/Listener#AnonSSH. != "" {
227 return nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("dont_namespace must be used with authorized_ssh listeners only")
228 }
229 versionscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/maincmd`/version().(osenv)
230 osenv.Logfscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/Env#Logf().("environment: not namespace due to dont_namespace option")
231 } else {
232 if err := namespacescip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/maincmd`/namespace().(osenv, cfg.Modulesscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/Config#Modules., listenAddr); err == errIsParentscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/maincmd`/errIsParent. {
233 return nil, nil
234 } else if err != nil {
235 return nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("namespace: %v", err)
236 }
237 }
238 osenv.Logfscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/Env#Logf().("%d rsync modules configured in total", len(cfg.Modulesscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/Config#Modules.))
239 for _, mod := range cfg.Modulesscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/Config#Modules. {
240 if !cfg.DontNamespacescip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/Config#DontNamespace. && !mod.Writablescip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncd`/Module#Writable. {
241 if err := canUnexpectedlyWriteToscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/maincmd`/canUnexpectedlyWriteTo().(mod.Pathscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncd`/Module#Path.); err != nil {
242 return nil, err
243 }
244 }
245
246 osenv.Logfscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/Env#Logf().("rsync module %q with path %s configured", mod.Namescip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncd`/Module#Name., mod.Pathscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncd`/Module#Path.)
247 }
248
249 if monitoringListen := opts.GokrazyDaemonscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/Options#GokrazyDaemon..MonitoringListenscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncopts`/GokrazyDaemonOptions#MonitoringListen.; monitoringListen != "" {
250 go func() {
251 osenv.Logfscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/Env#Logf().("HTTP server for monitoring listening on http://%s/debug/pprof", monitoringListen)
252 if err := httpscip-go gomod github.com/golang/go/src go1.25.0 `net/http`/.ListenAndServescip-go gomod github.com/golang/go/src go1.25.0 `net/http`/ListenAndServe().(monitoringListen, nil); err != nil {
253 osenv.Logfscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/Env#Logf().("-monitoring_listen: %v", err)
254 }
255 }()
256 }
257
258 srv, err := rsyncdscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncd`/.NewServerscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncd`/NewServer().(cfg.Modulesscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/Config#Modules., rsyncdscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncd`/.WithStderrscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncd`/WithStderr().(osenv.Stderrscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/Env#Stderr.))
259 if err != nil {
260 return nil, err
261 }
262 var ln netscip-go gomod github.com/golang/go/src go1.25.0 net/.Listenerscip-go gomod github.com/golang/go/src go1.25.0 net/Listener#
263 listeners, err := systemdListenersscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/maincmd`/systemdListeners().()
264 if err != nil {
265 return nil, err
266 }
267 if len(listeners) > 0 {
268 ln = listeners[0]
269 } else {
270 osenv.Logfscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/Env#Logf().("not using systemd socket activation, creating listener")
271 ln, err = netscip-go gomod github.com/golang/go/src go1.25.0 net/.Listenscip-go gomod github.com/golang/go/src go1.25.0 net/Listen().("tcp", listenAddr)
272 if err != nil {
273 return nil, err
274 }
275 }
276
277 if cfg.Listenersscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/Config#Listeners.[0].AuthorizedSSHscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/Listener#AuthorizedSSH..Addressscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/SSHListener#Address. != "" {
278 if cfg.Listenersscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/Config#Listeners.[0].AuthorizedSSHscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/Listener#AuthorizedSSH..AuthorizedKeysscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/SSHListener#AuthorizedKeys. == "" {
279 return nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("misconfiguration: authorized_keys must not be empty when using an authorized_ssh listener")
280 }
281 osenv.Logfscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/Env#Logf().("rsync daemon listening (authorized SSH) on %s", ln.Addrscip-go gomod github.com/golang/go/src go1.25.0 net/Listener#Addr().())
282 return nil, anonsshscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/anonssh`/.Servescip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/anonssh`/Serve().(ctx, osenv, ln, sshListener, cfg, func(args []string, stdin ioscip-go gomod github.com/golang/go/src go1.25.0 io/.ReadCloserscip-go gomod github.com/golang/go/src go1.25.0 io/ReadCloser#, stdout ioscip-go gomod github.com/golang/go/src go1.25.0 io/.WriteCloserscip-go gomod github.com/golang/go/src go1.25.0 io/WriteCloser#, stderr ioscip-go gomod github.com/golang/go/src go1.25.0 io/.WriteCloserscip-go gomod github.com/golang/go/src go1.25.0 io/WriteCloser#) error {
283 osenv := &rsyncosscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/.Envscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/Env#{
284 Stdinscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/Env#Stdin.: stdin,
285 Stdoutscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/Env#Stdout.: stdout,
286 Stderrscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/Env#Stderr.: stderr,
287 // This process is already restricted since to the
288 // rsyncd.NewServer call above. Do not add more rulesets to stay
289 // under the limit of policy layers per process.
290 DontRestrictscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/Env#DontRestrict.: true,
291 }
292 _, err := Mainscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/maincmd`/Main().(ctx, osenv, args, cfg)
293 return err
294 })
295 }
296
297 if cfg.Listenersscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/Config#Listeners.[0].AnonSSHscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncdconfig`/Listener#AnonSSH. != "" {
298 osenv.Logfscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/Env#Logf().("rsync daemon listening (anon SSH) on %s", ln.Addrscip-go gomod github.com/golang/go/src go1.25.0 net/Listener#Addr().())
299 return nil, anonsshscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/anonssh`/.Servescip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/anonssh`/Serve().(ctx, osenv, ln, sshListener, cfg, func(args []string, stdin ioscip-go gomod github.com/golang/go/src go1.25.0 io/.ReadCloserscip-go gomod github.com/golang/go/src go1.25.0 io/ReadCloser#, stdout ioscip-go gomod github.com/golang/go/src go1.25.0 io/.WriteCloserscip-go gomod github.com/golang/go/src go1.25.0 io/WriteCloser#, stderr ioscip-go gomod github.com/golang/go/src go1.25.0 io/.WriteCloserscip-go gomod github.com/golang/go/src go1.25.0 io/WriteCloser#) error {
300 osenv := &rsyncosscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/.Envscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/Env#{
301 Stdinscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/Env#Stdin.: stdin,
302 Stdoutscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/Env#Stdout.: stdout,
303 Stderrscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/Env#Stderr.: stderr,
304 // This process is already restricted since to the
305 // rsyncd.NewServer call above. Do not add more rulesets to stay
306 // under the limit of policy layers per process.
307 DontRestrictscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/Env#DontRestrict.: true,
308 }
309 _, err := Mainscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/maincmd`/Main().(ctx, osenv, args, cfg)
310 return err
311 })
312 }
313
314 osenv.Logfscip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/internal/rsyncos`/Env#Logf().("rsync daemon listening on rsync://%s", ln.Addrscip-go gomod github.com/golang/go/src go1.25.0 net/Listener#Addr().())
315 return nil, srv.Servescip-go gomod github.com/gokrazy/rsync . `github.com/gokrazy/rsync/rsyncd`/Server#Serve().(ctx, ln)
316 }