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