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