Commit | Line | Data |
---|---|---|
a039749b MP |
1 | /* -*- c-file-style: "linux"; -*- |
2 | ||
15b7b73d MP |
3 | Copyright (C) 1998-2001 by Andrew Tridgell <tridge@samba.org> |
4 | Copyright (C) 2001 by Martin Pool <mbp@samba.org> | |
3591c066 AT |
5 | |
6 | This program is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | This program is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with this program; if not, write to the Free Software | |
18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
19 | */ | |
20 | ||
bc363ea9 | 21 | /* the socket based protocol for setting up a connection with rsyncd */ |
3591c066 AT |
22 | |
23 | #include "rsync.h" | |
24 | ||
25 | extern int module_id; | |
26 | extern int read_only; | |
27 | extern int verbose; | |
28 | extern int rsync_port; | |
97cb8dc2 | 29 | char *auth_user; |
cb13abfe | 30 | int sanitize_paths = 0; |
3591c066 | 31 | |
bc363ea9 MP |
32 | /* |
33 | * Run a client connected to an rsyncd. The alternative to this | |
34 | * function for remote-shell connections is do_cmd. | |
35 | */ | |
3591c066 AT |
36 | int start_socket_client(char *host, char *path, int argc, char *argv[]) |
37 | { | |
38 | int fd, i; | |
39 | char *sargs[MAX_ARGS]; | |
40 | int sargc=0; | |
e42c9458 | 41 | char line[MAXPATHLEN]; |
bcb7e502 | 42 | char *p, *user=NULL; |
13c5fc0e | 43 | extern int remote_version; |
8d9dc9f9 | 44 | extern int am_sender; |
e30f0657 | 45 | extern struct in_addr socket_address; |
bc363ea9 | 46 | extern char *shell_cmd; |
7a55d06e MP |
47 | extern int kludge_around_eof; |
48 | ||
27e3e9c9 AT |
49 | if (argc == 0 && !am_sender) { |
50 | extern int list_only; | |
51 | list_only = 1; | |
52 | } | |
53 | ||
bc363ea9 MP |
54 | /* This is just a friendliness enhancement: if the connection |
55 | * is to an rsyncd then there is no point specifying the -e option. | |
56 | * Note that this is only set if the -e was explicitly specified, | |
57 | * not if the environment variable just happens to be set. | |
58 | * See http://lists.samba.org/pipermail/rsync/2000-September/002744.html | |
59 | */ | |
60 | if (shell_cmd) { | |
61 | rprintf(FERROR, "WARNING: --rsh or -e option ignored when " | |
62 | "connecting to rsync daemon\n"); | |
63 | /* continue */ | |
64 | } | |
65 | ||
f98df1d9 | 66 | if (*path == '/') { |
ff81e809 | 67 | rprintf(FERROR,"ERROR: The remote path must start with a module name not a /\n"); |
f98df1d9 AT |
68 | return -1; |
69 | } | |
70 | ||
bcb7e502 AT |
71 | p = strchr(host, '@'); |
72 | if (p) { | |
73 | user = host; | |
74 | host = p+1; | |
75 | *p = 0; | |
76 | } | |
77 | ||
78 | if (!user) user = getenv("USER"); | |
79 | if (!user) user = getenv("LOGNAME"); | |
80 | ||
eecd22ff | 81 | fd = open_socket_out_wrapped (host, rsync_port, &socket_address); |
3591c066 | 82 | if (fd == -1) { |
65417579 | 83 | exit_cleanup(RERR_SOCKETIO); |
3591c066 AT |
84 | } |
85 | ||
86 | server_options(sargs,&sargc); | |
87 | ||
88 | sargs[sargc++] = "."; | |
89 | ||
90 | if (path && *path) | |
91 | sargs[sargc++] = path; | |
92 | ||
93 | sargs[sargc] = NULL; | |
94 | ||
91eee594 AT |
95 | io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION); |
96 | ||
3591c066 AT |
97 | if (!read_line(fd, line, sizeof(line)-1)) { |
98 | return -1; | |
99 | } | |
100 | ||
13c5fc0e | 101 | if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) { |
3591c066 AT |
102 | return -1; |
103 | } | |
104 | ||
2c91d3d3 AT |
105 | p = strchr(path,'/'); |
106 | if (p) *p = 0; | |
107 | io_printf(fd,"%s\n",path); | |
108 | if (p) *p = '/'; | |
109 | ||
063393d6 MP |
110 | /* Old servers may just drop the connection here, |
111 | rather than sending a proper EXIT command. Yuck. */ | |
112 | kludge_around_eof = remote_version < 25; | |
7a55d06e | 113 | |
063393d6 | 114 | while (1) { |
3591c066 AT |
115 | if (!read_line(fd, line, sizeof(line)-1)) { |
116 | return -1; | |
117 | } | |
31593dd6 | 118 | |
31593dd6 | 119 | if (strncmp(line,"@RSYNCD: AUTHREQD ",18) == 0) { |
bcb7e502 | 120 | auth_client(fd, user, line+18); |
31593dd6 AT |
121 | continue; |
122 | } | |
31593dd6 | 123 | |
3591c066 | 124 | if (strcmp(line,"@RSYNCD: OK") == 0) break; |
8f04bb36 AT |
125 | |
126 | if (strcmp(line,"@RSYNCD: EXIT") == 0) exit(0); | |
127 | ||
3591c066 AT |
128 | rprintf(FINFO,"%s\n", line); |
129 | } | |
7a55d06e | 130 | kludge_around_eof = False; |
3591c066 AT |
131 | |
132 | for (i=0;i<sargc;i++) { | |
133 | io_printf(fd,"%s\n", sargs[i]); | |
134 | } | |
135 | io_printf(fd,"\n"); | |
136 | ||
09b7f5db AT |
137 | if (remote_version < 23) { |
138 | if (remote_version == 22 || (remote_version > 17 && !am_sender)) | |
139 | io_start_multiplex_in(fd); | |
140 | } | |
8d9dc9f9 | 141 | |
3591c066 AT |
142 | return client_run(fd, fd, -1, argc, argv); |
143 | } | |
144 | ||
145 | ||
146 | ||
147 | static int rsync_module(int fd, int i) | |
148 | { | |
149 | int argc=0; | |
150 | char *argv[MAX_ARGS]; | |
151 | char **argp; | |
e42c9458 | 152 | char line[MAXPATHLEN]; |
2af27ad9 | 153 | uid_t uid = (uid_t)-2; /* canonically "nobody" */ |
1a016bfd | 154 | gid_t gid = (gid_t)-2; |
8ef4ffd6 | 155 | char *p; |
56c473b7 AT |
156 | char *addr = client_addr(fd); |
157 | char *host = client_name(fd); | |
874895d5 | 158 | char *name = lp_name(i); |
8638dd48 | 159 | int use_chroot = lp_use_chroot(i); |
874895d5 | 160 | int start_glob=0; |
41979ff8 | 161 | int ret; |
7b372642 AT |
162 | char *request=NULL; |
163 | extern int am_sender; | |
8d9dc9f9 | 164 | extern int remote_version; |
943882a2 | 165 | extern int am_root; |
56c473b7 AT |
166 | |
167 | if (!allow_access(addr, host, lp_hosts_allow(i), lp_hosts_deny(i))) { | |
168 | rprintf(FERROR,"rsync denied on module %s from %s (%s)\n", | |
874895d5 | 169 | name, client_name(fd), client_addr(fd)); |
c8e78d87 | 170 | io_printf(fd,"@ERROR: access denied to %s from %s (%s)\n", |
874895d5 | 171 | name, client_name(fd), client_addr(fd)); |
56c473b7 AT |
172 | return -1; |
173 | } | |
3591c066 | 174 | |
5e71c444 | 175 | if (!claim_connection(lp_lock_file(i), lp_max_connections(i))) { |
8d72ef6e AT |
176 | if (errno) { |
177 | rprintf(FERROR,"failed to open lock file %s : %s\n", | |
5e71c444 | 178 | lp_lock_file(i), strerror(errno)); |
8d72ef6e | 179 | io_printf(fd,"@ERROR: failed to open lock file %s : %s\n", |
5e71c444 | 180 | lp_lock_file(i), strerror(errno)); |
8d72ef6e AT |
181 | } else { |
182 | rprintf(FERROR,"max connections (%d) reached\n", | |
5e71c444 AT |
183 | lp_max_connections(i)); |
184 | io_printf(fd,"@ERROR: max connections (%d) reached - try again later\n", lp_max_connections(i)); | |
8d72ef6e | 185 | } |
0c515f17 AT |
186 | return -1; |
187 | } | |
188 | ||
31593dd6 | 189 | |
97cb8dc2 | 190 | auth_user = auth_server(fd, i, addr, "@RSYNCD: AUTHREQD "); |
d0d56395 | 191 | |
97cb8dc2 | 192 | if (!auth_user) { |
d0d56395 AT |
193 | rprintf(FERROR,"auth failed on module %s from %s (%s)\n", |
194 | name, client_name(fd), client_addr(fd)); | |
195 | io_printf(fd,"@ERROR: auth failed on module %s\n",name); | |
196 | return -1; | |
197 | } | |
198 | ||
3591c066 AT |
199 | module_id = i; |
200 | ||
716baed7 | 201 | am_root = (getuid() == 0); |
8ef4ffd6 | 202 | |
716baed7 DD |
203 | if (am_root) { |
204 | p = lp_uid(i); | |
205 | if (!name_to_uid(p, &uid)) { | |
206 | if (!isdigit(*p)) { | |
207 | rprintf(FERROR,"Invalid uid %s\n", p); | |
fd2dd2aa | 208 | io_printf(fd,"@ERROR: invalid uid %s\n", p); |
716baed7 DD |
209 | return -1; |
210 | } | |
211 | uid = atoi(p); | |
212 | } | |
213 | ||
214 | p = lp_gid(i); | |
215 | if (!name_to_gid(p, &gid)) { | |
216 | if (!isdigit(*p)) { | |
217 | rprintf(FERROR,"Invalid gid %s\n", p); | |
fd2dd2aa | 218 | io_printf(fd,"@ERROR: invalid gid %s\n", p); |
716baed7 DD |
219 | return -1; |
220 | } | |
221 | gid = atoi(p); | |
222 | } | |
8ef4ffd6 | 223 | } |
3b2b5345 MP |
224 | |
225 | /* TODO: If we're not root, but the configuration requests | |
226 | * that we change to some uid other than the current one, then | |
227 | * log a warning. */ | |
228 | ||
229 | /* TODO: Perhaps take a list of gids, and make them into the | |
230 | * supplementary groups. */ | |
8ef4ffd6 | 231 | |
cd64343a DD |
232 | p = lp_include_from(i); |
233 | add_exclude_file(p, 1, 1); | |
234 | ||
235 | p = lp_include(i); | |
236 | add_include_line(p); | |
237 | ||
8f3a2d54 | 238 | p = lp_exclude_from(i); |
2b6b4d53 | 239 | add_exclude_file(p, 1, 0); |
8f3a2d54 | 240 | |
5805327b | 241 | p = lp_exclude(i); |
8f3a2d54 AT |
242 | add_exclude_line(p); |
243 | ||
45a83540 | 244 | log_init(); |
1a016bfd | 245 | |
8638dd48 | 246 | if (use_chroot) { |
3ef526f5 MP |
247 | /* TODO: If we're not running as root, then we'll get |
248 | * an error here. I wonder if we should perhaps just | |
249 | * log a warning here, and continue anyhow using the | |
250 | * push_dir feature? -- mbp 2001-08-31 */ | |
8638dd48 | 251 | if (chroot(lp_path(i))) { |
a039749b | 252 | rsyserr(FERROR, errno, "chroot %s failed", lp_path(i)); |
8638dd48 DD |
253 | io_printf(fd,"@ERROR: chroot failed\n"); |
254 | return -1; | |
255 | } | |
3591c066 | 256 | |
c226b7c2 | 257 | if (!push_dir("/", 0)) { |
a039749b | 258 | rsyserr(FERROR, errno, "chdir %s failed\n", lp_path(i)); |
8638dd48 DD |
259 | io_printf(fd,"@ERROR: chdir failed\n"); |
260 | return -1; | |
261 | } | |
3591c066 | 262 | |
716baed7 DD |
263 | } else { |
264 | if (!push_dir(lp_path(i), 0)) { | |
a039749b | 265 | rsyserr(FERROR, errno, "chdir %s failed\n", lp_path(i)); |
716baed7 DD |
266 | io_printf(fd,"@ERROR: chdir failed\n"); |
267 | return -1; | |
268 | } | |
cb13abfe | 269 | sanitize_paths = 1; |
716baed7 DD |
270 | } |
271 | ||
272 | if (am_root) { | |
273 | if (setgid(gid)) { | |
08a740ff | 274 | rsyserr(FERROR, errno, "setgid %d failed", (int) gid); |
8638dd48 DD |
275 | io_printf(fd,"@ERROR: setgid failed\n"); |
276 | return -1; | |
277 | } | |
3591c066 | 278 | |
716baed7 | 279 | if (setuid(uid)) { |
08a740ff | 280 | rsyserr(FERROR, errno, "setuid %d failed", (int) uid); |
8638dd48 DD |
281 | io_printf(fd,"@ERROR: setuid failed\n"); |
282 | return -1; | |
283 | } | |
284 | ||
716baed7 | 285 | am_root = (getuid() == 0); |
3591c066 AT |
286 | } |
287 | ||
288 | io_printf(fd,"@RSYNCD: OK\n"); | |
289 | ||
290 | argv[argc++] = "rsyncd"; | |
291 | ||
292 | while (1) { | |
293 | if (!read_line(fd, line, sizeof(line)-1)) { | |
294 | return -1; | |
295 | } | |
296 | ||
297 | if (!*line) break; | |
298 | ||
874895d5 AT |
299 | p = line; |
300 | ||
874895d5 | 301 | argv[argc] = strdup(p); |
3591c066 AT |
302 | if (!argv[argc]) { |
303 | return -1; | |
304 | } | |
305 | ||
874895d5 | 306 | if (start_glob) { |
1a016bfd | 307 | if (start_glob == 1) { |
7b372642 | 308 | request = strdup(p); |
1a016bfd AT |
309 | start_glob++; |
310 | } | |
cb13abfe | 311 | glob_expand(name, argv, &argc, MAX_ARGS); |
874895d5 AT |
312 | } else { |
313 | argc++; | |
314 | } | |
315 | ||
316 | if (strcmp(line,".") == 0) { | |
317 | start_glob = 1; | |
318 | } | |
319 | ||
3591c066 AT |
320 | if (argc == MAX_ARGS) { |
321 | return -1; | |
322 | } | |
323 | } | |
324 | ||
cb13abfe | 325 | if (sanitize_paths) { |
8638dd48 DD |
326 | /* |
327 | * Note that this is applied to all parameters, whether or not | |
328 | * they are filenames, but no other legal parameters contain | |
329 | * the forms that need to be sanitized so it doesn't hurt; | |
330 | * it is not known at this point which parameters are files | |
331 | * and which aren't. | |
332 | */ | |
333 | for (i = 1; i < argc; i++) { | |
cb13abfe | 334 | sanitize_path(argv[i], NULL); |
8638dd48 DD |
335 | } |
336 | } | |
337 | ||
15b7b73d MP |
338 | argp = argv; |
339 | ret = parse_arguments(&argc, (const char ***) &argp, 0); | |
3591c066 | 340 | |
7b372642 | 341 | if (request) { |
97cb8dc2 | 342 | if (*auth_user) { |
d0d56395 AT |
343 | rprintf(FINFO,"rsync %s %s from %s@%s (%s)\n", |
344 | am_sender?"on":"to", | |
97cb8dc2 | 345 | request, auth_user, host, addr); |
d0d56395 AT |
346 | } else { |
347 | rprintf(FINFO,"rsync %s %s from %s (%s)\n", | |
348 | am_sender?"on":"to", | |
349 | request, host, addr); | |
350 | } | |
7b372642 AT |
351 | free(request); |
352 | } | |
353 | ||
15b7b73d | 354 | #ifndef DEBUG |
3591c066 AT |
355 | /* don't allow the logs to be flooded too fast */ |
356 | if (verbose > 1) verbose = 1; | |
0199b05f | 357 | #endif |
3591c066 | 358 | |
09b7f5db AT |
359 | if (remote_version < 23) { |
360 | if (remote_version == 22 || (remote_version > 17 && am_sender)) | |
361 | io_start_multiplex_out(fd); | |
554e0a8d | 362 | } |
15b7b73d MP |
363 | |
364 | /* For later protocol versions, we don't start multiplexing | |
365 | * until we've configured nonblocking in start_server. That | |
366 | * means we're in a sticky situation now: there's no way to | |
367 | * convey errors to the client. */ | |
368 | ||
369 | /* FIXME: Hold off on reporting option processing errors until | |
370 | * we've set up nonblocking and multiplexed IO and can get the | |
371 | * message back to them. */ | |
41979ff8 | 372 | if (!ret) { |
15b7b73d MP |
373 | option_error(); |
374 | exit_cleanup(RERR_UNSUPPORTED); | |
41979ff8 AT |
375 | } |
376 | ||
81791cfc AT |
377 | if (lp_timeout(i)) { |
378 | extern int io_timeout; | |
379 | io_timeout = lp_timeout(i); | |
380 | } | |
381 | ||
3591c066 AT |
382 | start_server(fd, fd, argc, argp); |
383 | ||
384 | return 0; | |
385 | } | |
386 | ||
7a6421fa AT |
387 | /* send a list of available modules to the client. Don't list those |
388 | with "list = False". */ | |
3591c066 AT |
389 | static void send_listing(int fd) |
390 | { | |
391 | int n = lp_numservices(); | |
392 | int i; | |
063393d6 MP |
393 | extern int remote_version; |
394 | ||
3591c066 AT |
395 | for (i=0;i<n;i++) |
396 | if (lp_list(i)) | |
397 | io_printf(fd, "%-15s\t%s\n", lp_name(i), lp_comment(i)); | |
8f04bb36 | 398 | |
063393d6 MP |
399 | if (remote_version >= 25) |
400 | io_printf(fd,"@RSYNCD: EXIT\n"); | |
3591c066 AT |
401 | } |
402 | ||
403 | /* this is called when a socket connection is established to a client | |
404 | and we want to start talking. The setup of the system is done from | |
405 | here */ | |
406 | static int start_daemon(int fd) | |
407 | { | |
7a6421fa | 408 | char line[200]; |
3591c066 | 409 | char *motd; |
8ef4ffd6 | 410 | int i = -1; |
4cdf25e4 | 411 | extern char *config_file; |
13c5fc0e | 412 | extern int remote_version; |
4cdf25e4 | 413 | |
f9e940ef | 414 | if (!lp_load(config_file, 0)) { |
65417579 | 415 | exit_cleanup(RERR_SYNTAX); |
4cdf25e4 | 416 | } |
3591c066 AT |
417 | |
418 | set_socket_options(fd,"SO_KEEPALIVE"); | |
a6801c39 | 419 | set_socket_options(fd,lp_socket_options()); |
f0359dd0 | 420 | set_nonblocking(fd); |
3591c066 AT |
421 | |
422 | io_printf(fd,"@RSYNCD: %d\n", PROTOCOL_VERSION); | |
423 | ||
424 | motd = lp_motd_file(); | |
27d3cdbc | 425 | if (motd && *motd) { |
3591c066 AT |
426 | FILE *f = fopen(motd,"r"); |
427 | while (f && !feof(f)) { | |
428 | int len = fread(line, 1, sizeof(line)-1, f); | |
429 | if (len > 0) { | |
430 | line[len] = 0; | |
431 | io_printf(fd,"%s", line); | |
432 | } | |
433 | } | |
434 | if (f) fclose(f); | |
435 | io_printf(fd,"\n"); | |
436 | } | |
437 | ||
91eee594 AT |
438 | if (!read_line(fd, line, sizeof(line)-1)) { |
439 | return -1; | |
440 | } | |
441 | ||
442 | if (sscanf(line,"@RSYNCD: %d", &remote_version) != 1) { | |
c8e78d87 | 443 | io_printf(fd,"@ERROR: protocol startup error\n"); |
91eee594 AT |
444 | return -1; |
445 | } | |
446 | ||
8ef4ffd6 | 447 | while (i == -1) { |
3591c066 AT |
448 | line[0] = 0; |
449 | if (!read_line(fd, line, sizeof(line)-1)) { | |
450 | return -1; | |
451 | } | |
452 | ||
453 | if (!*line || strcmp(line,"#list")==0) { | |
454 | send_listing(fd); | |
455 | return -1; | |
456 | } | |
457 | ||
458 | if (*line == '#') { | |
459 | /* it's some sort of command that I don't understand */ | |
c8e78d87 | 460 | io_printf(fd,"@ERROR: Unknown command '%s'\n", line); |
3591c066 AT |
461 | return -1; |
462 | } | |
463 | ||
464 | i = lp_number(line); | |
465 | if (i == -1) { | |
c8e78d87 | 466 | io_printf(fd,"@ERROR: Unknown module '%s'\n", line); |
3591c066 AT |
467 | return -1; |
468 | } | |
3591c066 AT |
469 | } |
470 | ||
8ef4ffd6 | 471 | return rsync_module(fd, i); |
3591c066 AT |
472 | } |
473 | ||
474 | ||
475 | int daemon_main(void) | |
476 | { | |
f9e940ef | 477 | extern char *config_file; |
ad517ce5 | 478 | extern int orig_umask; |
8638dd48 | 479 | char *pid_file; |
1a016bfd | 480 | |
3591c066 | 481 | if (is_a_socket(STDIN_FILENO)) { |
41979ff8 AT |
482 | int i; |
483 | ||
484 | /* we are running via inetd - close off stdout and | |
485 | stderr so that library functions (and getopt) don't | |
486 | try to use them. Redirect them to /dev/null */ | |
487 | for (i=1;i<3;i++) { | |
488 | close(i); | |
489 | open("/dev/null", O_RDWR); | |
490 | } | |
3eb38818 | 491 | |
3591c066 AT |
492 | return start_daemon(STDIN_FILENO); |
493 | } | |
494 | ||
495 | become_daemon(); | |
496 | ||
f9e940ef | 497 | if (!lp_load(config_file, 1)) { |
65417579 | 498 | exit_cleanup(RERR_SYNTAX); |
f9e940ef AT |
499 | } |
500 | ||
45a83540 | 501 | log_init(); |
f9e940ef | 502 | |
15b7b73d MP |
503 | rprintf(FINFO, "rsyncd version %s starting, listening on port %d\n", VERSION, |
504 | rsync_port); | |
505 | /* TODO: If listening on a particular address, then show that | |
506 | * address too. */ | |
e42c9458 | 507 | |
8638dd48 | 508 | if (((pid_file = lp_pid_file()) != NULL) && (*pid_file != '\0')) { |
ad517ce5 DD |
509 | char pidbuf[16]; |
510 | int fd; | |
8638dd48 DD |
511 | int pid = (int) getpid(); |
512 | cleanup_set_pid(pid); | |
ad517ce5 DD |
513 | if ((fd = do_open(lp_pid_file(), O_WRONLY|O_CREAT|O_TRUNC, |
514 | 0666 & ~orig_umask)) == -1) { | |
8638dd48 | 515 | cleanup_set_pid(0); |
a039749b | 516 | rsyserr(FLOG, errno, "failed to create pid file %s", pid_file); |
65417579 | 517 | exit_cleanup(RERR_FILEIO); |
8638dd48 | 518 | } |
8950ac03 | 519 | snprintf(pidbuf, sizeof(pidbuf), "%d\n", pid); |
ad517ce5 DD |
520 | write(fd, pidbuf, strlen(pidbuf)); |
521 | close(fd); | |
8638dd48 DD |
522 | } |
523 | ||
8ef4ffd6 AT |
524 | start_accept_loop(rsync_port, start_daemon); |
525 | return -1; | |
3591c066 AT |
526 | } |
527 |