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