Clarified a few items.
[rsync/rsync.git] / clientserver.c
CommitLineData
a039749b 1/* -*- c-file-style: "linux"; -*-
4a7144ee 2 *
a254fd97
MP
3 * Copyright (C) 1998-2001 by Andrew Tridgell <tridge@samba.org>
4 * Copyright (C) 2001-2002 by Martin Pool <mbp@samba.org>
4a7144ee 5 *
a254fd97
MP
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.
4a7144ee 10 *
a254fd97
MP
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.
4a7144ee 15 *
a254fd97
MP
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 */
3591c066 20
a254fd97
MP
21/**
22 * @file
23 *
24 * The socket based protocol for setting up a connection with
25 * rsyncd.
26 **/
3591c066
AT
27
28#include "rsync.h"
29
c0814b81 30extern int verbose;
7a6653bc 31extern int quiet;
c0814b81 32extern int list_only;
6e195fe9
WD
33extern int am_sender;
34extern int am_server;
35extern int am_daemon;
36extern int am_root;
3591c066 37extern int rsync_port;
8dad7fc6 38extern int kluge_around_eof;
6e195fe9 39extern int daemon_over_rsh;
b35d0d8e 40extern int sanitize_paths;
7c2a9e76 41extern int filesfrom_fd;
daa598df
WD
42extern int remote_protocol;
43extern int protocol_version;
6e195fe9 44extern int io_timeout;
6e195fe9
WD
45extern int no_detach;
46extern int default_af_hint;
2a7d9fe9 47extern int log_initialised;
c5b7aa15 48extern mode_t orig_umask;
6e195fe9 49extern char *bind_address;
34937987 50extern char *sockopts;
6e195fe9 51extern char *config_file;
bf4679e8 52extern char *files_from;
fed1f3f4 53extern char *tmpdir;
730df9d2
WD
54extern struct chmod_mode_struct *chmod_modes;
55extern struct filter_list_struct server_filter_list;
6e195fe9
WD
56
57char *auth_user;
18638730 58int read_only = 0;
f39b2060
WD
59int daemon_log_format_has_i = 0;
60int daemon_log_format_has_o_or_i = 0;
211bc43b 61int module_id = -1;
8840ec0f 62struct chmod_mode_struct *daemon_chmod_modes;
3591c066 63
e7bf7c01
WD
64/* Length of lp_path() string when in daemon mode & not chrooted, else 0. */
65unsigned int module_dirlen = 0;
66
d749eb68
WD
67#ifdef HAVE_SIGACTION
68static struct sigaction sigact;
69#endif
70
be2961da 71/**
bc363ea9 72 * Run a client connected to an rsyncd. The alternative to this
be2961da
MP
73 * function for remote-shell connections is do_cmd().
74 *
fdf88d75
MP
75 * After negotiating which module to use and reading the server's
76 * motd, this hands over to client_run(). Telling the server the
77 * module will cause it to chroot/setuid/etc.
78 *
79 * Instead of doing a transfer, the client may at this stage instead
80 * get a listing of remote modules and exit.
be2961da
MP
81 *
82 * @return -1 for error in startup, or the result of client_run().
d0829892 83 * Either way, it eventually gets passed to exit_cleanup().
be2961da 84 **/
3591c066
AT
85int start_socket_client(char *host, char *path, int argc, char *argv[])
86{
68f40ebb 87 int fd, ret;
2e94e70e 88 char *p, *user = NULL;
4a7144ee 89
1732b6c0
WD
90 /* This is redundant with code in start_inband_exchange(), but this
91 * short-circuits a problem in the client before we open a socket,
92 * and the extra check won't hurt. */
f98df1d9 93 if (*path == '/') {
1732b6c0
WD
94 rprintf(FERROR,
95 "ERROR: The remote path must start with a module name not a /\n");
f98df1d9
AT
96 return -1;
97 }
98
b31c92ed 99 if ((p = strrchr(host, '@')) != NULL) {
bcb7e502
AT
100 user = host;
101 host = p+1;
1732b6c0 102 *p = '\0';
bcb7e502
AT
103 }
104
837cbad9
WD
105 fd = open_socket_out_wrapped(host, rsync_port, bind_address,
106 default_af_hint);
df5cd107 107 if (fd == -1)
65417579 108 exit_cleanup(RERR_SOCKETIO);
68f40ebb 109
34937987
WD
110 set_socket_options(fd, sockopts);
111
68f40ebb
WD
112 ret = start_inband_exchange(user, path, fd, fd, argc);
113
180443af 114 return ret ? ret : client_run(fd, fd, -1, argc, argv);
68f40ebb
WD
115}
116
b9f592fb
WD
117int start_inband_exchange(char *user, char *path, int f_in, int f_out,
118 int argc)
68f40ebb
WD
119{
120 int i;
121 char *sargs[MAX_ARGS];
122 int sargc = 0;
20accf4d 123 char line[BIGPATHBUFLEN];
68f40ebb 124 char *p;
68f40ebb
WD
125
126 if (argc == 0 && !am_sender)
9bcb2595 127 list_only |= 1;
68f40ebb
WD
128
129 if (*path == '/') {
1732b6c0
WD
130 rprintf(FERROR,
131 "ERROR: The remote path must start with a module name\n");
68f40ebb
WD
132 return -1;
133 }
134
2e94e70e
WD
135 if (!user)
136 user = getenv("USER");
137 if (!user)
138 user = getenv("LOGNAME");
68f40ebb 139
5a8543b8 140 io_printf(f_out, "@RSYNCD: %d\n", protocol_version);
91eee594 141
2e94e70e 142 if (!read_line(f_in, line, sizeof line - 1)) {
be2961da 143 rprintf(FERROR, "rsync: did not see server greeting\n");
3591c066
AT
144 return -1;
145 }
146
daa598df 147 if (sscanf(line,"@RSYNCD: %d", &remote_protocol) != 1) {
be2961da
MP
148 /* note that read_line strips of \n or \r */
149 rprintf(FERROR, "rsync: server sent \"%s\" rather than greeting\n",
150 line);
3591c066
AT
151 return -1;
152 }
daa598df
WD
153 if (protocol_version > remote_protocol)
154 protocol_version = remote_protocol;
3591c066 155
9bcb2595
WD
156 if (list_only && protocol_version >= 29)
157 list_only |= 2;
158
159 /* set daemon_over_rsh to false since we need to build the
160 * true set of args passed through the rsh/ssh connection;
161 * this is a no-op for direct-socket-connection mode */
162 daemon_over_rsh = 0;
163 server_options(sargs, &sargc);
164
165 sargs[sargc++] = ".";
166
167 if (path && *path)
168 sargs[sargc++] = path;
169
170 sargs[sargc] = NULL;
171
172 if (verbose > 1)
173 print_child_argv(sargs);
174
2c91d3d3
AT
175 p = strchr(path,'/');
176 if (p) *p = 0;
68f40ebb 177 io_printf(f_out, "%s\n", path);
2c91d3d3
AT
178 if (p) *p = '/';
179
063393d6
MP
180 /* Old servers may just drop the connection here,
181 rather than sending a proper EXIT command. Yuck. */
8dad7fc6 182 kluge_around_eof = list_only && protocol_version < 25 ? 1 : 0;
7a55d06e 183
063393d6 184 while (1) {
2e94e70e 185 if (!read_line(f_in, line, sizeof line - 1)) {
be2961da 186 rprintf(FERROR, "rsync: didn't get server startup line\n");
3591c066
AT
187 return -1;
188 }
31593dd6 189
31593dd6 190 if (strncmp(line,"@RSYNCD: AUTHREQD ",18) == 0) {
68f40ebb 191 auth_client(f_out, user, line+18);
31593dd6
AT
192 continue;
193 }
31593dd6 194
2e94e70e
WD
195 if (strcmp(line,"@RSYNCD: OK") == 0)
196 break;
8f04bb36 197
cae95647
MP
198 if (strcmp(line,"@RSYNCD: EXIT") == 0) {
199 /* This is sent by recent versions of the
200 * server to terminate the listing of modules.
201 * We don't want to go on and transfer
202 * anything; just exit. */
203 exit(0);
204 }
8f04bb36 205
136ac7ec 206 if (strncmp(line, "@ERROR", 6) == 0) {
1732b6c0 207 rprintf(FERROR, "%s\n", line);
136ac7ec
MP
208 /* This is always fatal; the server will now
209 * close the socket. */
180443af 210 return -1;
136ac7ec 211 }
180443af
WD
212
213 rprintf(FINFO, "%s\n", line);
3591c066 214 }
8dad7fc6 215 kluge_around_eof = 0;
3591c066 216
68f40ebb
WD
217 for (i = 0; i < sargc; i++) {
218 io_printf(f_out, "%s\n", sargs[i]);
3591c066 219 }
68f40ebb 220 io_printf(f_out, "\n");
3591c066 221
daa598df 222 if (protocol_version < 23) {
3f55bd5d 223 if (protocol_version == 22 || !am_sender)
da3478b2 224 io_start_multiplex_in();
09b7f5db 225 }
8d9dc9f9 226
68f40ebb 227 return 0;
3591c066
AT
228}
229
141c6265
WD
230static char *finish_pre_exec(pid_t pid, int fd, char *request,
231 int argc, char *argv[])
c95ca2a2 232{
141c6265 233 int j, status = -1;
3591c066 234
c95ca2a2 235 if (request) {
141c6265
WD
236 write_buf(fd, request, strlen(request)+1);
237 for (j = 0; j < argc; j++)
238 write_buf(fd, argv[j], strlen(argv[j])+1);
c95ca2a2 239 }
141c6265
WD
240
241 write_byte(fd, 0);
242
c95ca2a2
WD
243 close(fd);
244
245 if (wait_process(pid, &status, 0) < 0
246 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
247 char *e;
248 if (asprintf(&e, "pre-xfer exec returned failure (%d)\n", status) < 0)
249 out_of_memory("finish_pre_exec");
250 return e;
251 }
252 return NULL;
253}
3591c066 254
141c6265
WD
255static int read_arg_from_pipe(int fd, char *buf, int limit)
256{
257 char *bp = buf, *eob = buf + limit - 1;
258
259 while (1) {
260 if (read(fd, bp, 1) != 1)
261 return -1;
262 if (*bp == '\0')
263 break;
264 if (bp < eob)
265 bp++;
266 }
267 *bp = '\0';
268
269 return bp - buf;
270}
271
439a198d 272static int rsync_module(int f_in, int f_out, int i, char *addr, char *host)
3591c066 273{
2e94e70e 274 int argc = 0;
b7061c82
WD
275 int maxargs;
276 char **argv;
20accf4d 277 char line[BIGPATHBUFLEN];
2af27ad9 278 uid_t uid = (uid_t)-2; /* canonically "nobody" */
1a016bfd 279 gid_t gid = (gid_t)-2;
c95ca2a2 280 char *p, *err_msg = NULL;
874895d5 281 char *name = lp_name(i);
8638dd48 282 int use_chroot = lp_use_chroot(i);
2e94e70e 283 int start_glob = 0;
c95ca2a2
WD
284 int ret, pre_exec_fd = -1;
285 pid_t pre_exec_pid = 0;
2e94e70e 286 char *request = NULL;
56c473b7
AT
287
288 if (!allow_access(addr, host, lp_hosts_allow(i), lp_hosts_deny(i))) {
1732b6c0 289 rprintf(FLOG, "rsync denied on module %s from %s (%s)\n",
31ec50d7 290 name, host, addr);
c0422cea
WD
291 if (!lp_list(i))
292 io_printf(f_out, "@ERROR: Unknown module '%s'\n", name);
293 else {
294 io_printf(f_out,
295 "@ERROR: access denied to %s from %s (%s)\n",
296 name, host, addr);
297 }
56c473b7
AT
298 return -1;
299 }
3591c066 300
68f40ebb 301 if (am_daemon && am_server) {
9e5a5ddb 302 rprintf(FLOG, "rsync allowed access on module %s from %s (%s)\n",
68f40ebb
WD
303 name, host, addr);
304 }
305
5e71c444 306 if (!claim_connection(lp_lock_file(i), lp_max_connections(i))) {
8d72ef6e 307 if (errno) {
1732b6c0 308 rsyserr(FLOG, errno, "failed to open lock file %s",
45c49b52 309 lp_lock_file(i));
4875d6b6 310 io_printf(f_out, "@ERROR: failed to open lock file\n");
8d72ef6e 311 } else {
1732b6c0 312 rprintf(FLOG, "max connections (%d) reached\n",
5e71c444 313 lp_max_connections(i));
4ccfd96c 314 io_printf(f_out, "@ERROR: max connections (%d) reached -- try again later\n",
837cbad9 315 lp_max_connections(i));
8d72ef6e 316 }
0c515f17
AT
317 return -1;
318 }
319
45c5b903 320 auth_user = auth_server(f_in, f_out, i, host, addr, "@RSYNCD: AUTHREQD ");
d0d56395 321
97cb8dc2 322 if (!auth_user) {
68f40ebb 323 io_printf(f_out, "@ERROR: auth failed on module %s\n", name);
4a7144ee 324 return -1;
d0d56395
AT
325 }
326
3591c066
AT
327 module_id = i;
328
211bc43b 329 if (lp_read_only(i))
18638730
WD
330 read_only = 1;
331
f39b2060 332 if (lp_transfer_logging(i)) {
624d6be2 333 if (log_format_has(lp_log_format(i), 'i'))
f39b2060
WD
334 daemon_log_format_has_i = 1;
335 if (daemon_log_format_has_i
624d6be2 336 || log_format_has(lp_log_format(i), 'o'))
f39b2060
WD
337 daemon_log_format_has_o_or_i = 1;
338 }
c0814b81 339
6fe05820 340 am_root = (MY_UID() == 0);
8ef4ffd6 341
716baed7
DD
342 if (am_root) {
343 p = lp_uid(i);
344 if (!name_to_uid(p, &uid)) {
2e94e70e 345 if (!isdigit(*(unsigned char *)p)) {
1732b6c0 346 rprintf(FLOG, "Invalid uid %s\n", p);
68f40ebb 347 io_printf(f_out, "@ERROR: invalid uid %s\n", p);
716baed7 348 return -1;
4a7144ee 349 }
716baed7
DD
350 uid = atoi(p);
351 }
352
353 p = lp_gid(i);
354 if (!name_to_gid(p, &gid)) {
2e94e70e 355 if (!isdigit(*(unsigned char *)p)) {
1732b6c0 356 rprintf(FLOG, "Invalid gid %s\n", p);
68f40ebb 357 io_printf(f_out, "@ERROR: invalid gid %s\n", p);
716baed7 358 return -1;
4a7144ee 359 }
716baed7
DD
360 gid = atoi(p);
361 }
8ef4ffd6 362 }
3b2b5345 363
4a7144ee
WD
364 /* TODO: If we're not root, but the configuration requests
365 * that we change to some uid other than the current one, then
366 * log a warning. */
367
368 /* TODO: Perhaps take a list of gids, and make them into the
369 * supplementary groups. */
8ef4ffd6 370
46bffd98 371 if (use_chroot || (module_dirlen = strlen(lp_path(i))) == 1) {
e7bf7c01 372 module_dirlen = 0;
7842418b 373 set_filter_dir("/", 1);
46bffd98 374 } else
7842418b 375 set_filter_dir(lp_path(i), module_dirlen);
e7bf7c01
WD
376
377 p = lp_filter(i);
ebfd1a1c 378 parse_rule(&server_filter_list, p, MATCHFLG_WORD_SPLIT,
2c8c8bba 379 XFLG_ABS_IF_SLASH);
837cbad9 380
cd64343a 381 p = lp_include_from(i);
ebfd1a1c 382 parse_filter_file(&server_filter_list, p, MATCHFLG_INCLUDE,
2c8c8bba 383 XFLG_ABS_IF_SLASH | XFLG_OLD_PREFIXES | XFLG_FATAL_ERRORS);
cd64343a
DD
384
385 p = lp_include(i);
ebfd1a1c 386 parse_rule(&server_filter_list, p,
0a68f869 387 MATCHFLG_INCLUDE | MATCHFLG_WORD_SPLIT,
2c8c8bba 388 XFLG_ABS_IF_SLASH | XFLG_OLD_PREFIXES);
cd64343a 389
8f3a2d54 390 p = lp_exclude_from(i);
ebfd1a1c 391 parse_filter_file(&server_filter_list, p, 0,
2c8c8bba 392 XFLG_ABS_IF_SLASH | XFLG_OLD_PREFIXES | XFLG_FATAL_ERRORS);
8f3a2d54 393
5805327b 394 p = lp_exclude(i);
ebfd1a1c 395 parse_rule(&server_filter_list, p, MATCHFLG_WORD_SPLIT,
2c8c8bba 396 XFLG_ABS_IF_SLASH | XFLG_OLD_PREFIXES);
8f3a2d54 397
45a83540 398 log_init();
1a016bfd 399
9d0d18b5 400#ifdef HAVE_PUTENV
c95ca2a2 401 if (*lp_prexfer_exec(i) || *lp_postxfer_exec(i)) {
9d0d18b5
WD
402 char *modname, *modpath, *hostaddr, *hostname, *username;
403 int status;
404 if (asprintf(&modname, "RSYNC_MODULE_NAME=%s", name) < 0
405 || asprintf(&modpath, "RSYNC_MODULE_PATH=%s", lp_path(i)) < 0
406 || asprintf(&hostaddr, "RSYNC_HOST_ADDR=%s", addr) < 0
407 || asprintf(&hostname, "RSYNC_HOST_NAME=%s", host) < 0
408 || asprintf(&username, "RSYNC_USER_NAME=%s", auth_user) < 0)
409 out_of_memory("rsync_module");
410 putenv(modname);
411 putenv(modpath);
412 putenv(hostaddr);
413 putenv(hostname);
414 putenv(username);
415 umask(orig_umask);
c95ca2a2
WD
416 /* For post-xfer exec, fork a new process to run the rsync
417 * daemon while this process waits for the exit status and
418 * runs the indicated command at that point. */
419 if (*lp_postxfer_exec(i)) {
9d0d18b5
WD
420 pid_t pid = fork();
421 if (pid < 0) {
422 rsyserr(FLOG, errno, "fork failed");
423 io_printf(f_out, "@ERROR: fork failed\n");
424 return -1;
425 }
426 if (pid) {
427 char *ret1, *ret2;
c95ca2a2
WD
428 if (wait_process(pid, &status, 0) < 0)
429 status = -1;
9d0d18b5
WD
430 if (asprintf(&ret1, "RSYNC_RAW_STATUS=%d", status) > 0)
431 putenv(ret1);
432 if (WIFEXITED(status))
433 status = WEXITSTATUS(status);
434 else
435 status = -1;
436 if (asprintf(&ret2, "RSYNC_EXIT_STATUS=%d", status) > 0)
437 putenv(ret2);
c95ca2a2 438 system(lp_postxfer_exec(i));
9d0d18b5
WD
439 _exit(status);
440 }
441 }
c95ca2a2
WD
442 /* For pre-xfer exec, fork a child process to run the indicated
443 * command, though it first waits for the parent process to
444 * send us the user's request via a pipe. */
445 if (*lp_prexfer_exec(i)) {
446 int fds[2];
447 if (pipe(fds) < 0 || (pre_exec_pid = fork()) < 0) {
448 rsyserr(FLOG, errno, "pre-xfer exec preparation failed");
449 io_printf(f_out, "@ERROR: pre-xfer exec preparation failed\n");
450 return -1;
451 }
452 if (pre_exec_pid == 0) {
141c6265
WD
453 char buf[BIGPATHBUFLEN];
454 int j, len;
c95ca2a2
WD
455 close(fds[1]);
456 set_blocking(fds[0]);
141c6265 457 len = read_arg_from_pipe(fds[0], buf, BIGPATHBUFLEN);
c95ca2a2
WD
458 if (len <= 0)
459 _exit(1);
c95ca2a2
WD
460 if (asprintf(&p, "RSYNC_REQUEST=%s", buf) < 0)
461 out_of_memory("rsync_module");
462 putenv(p);
141c6265
WD
463 for (j = 0; ; j++) {
464 len = read_arg_from_pipe(fds[0], buf,
465 BIGPATHBUFLEN);
466 if (len <= 0) {
467 if (!len)
468 break;
469 _exit(1);
470 }
471 if (asprintf(&p, "RSYNC_ARG%d=%s", j, buf) < 0)
472 out_of_memory("rsync_module");
473 putenv(p);
474 }
475 close(fds[0]);
c95ca2a2
WD
476 close(STDIN_FILENO);
477 close(STDOUT_FILENO);
478 status = system(lp_prexfer_exec(i));
479 if (!WIFEXITED(status))
480 _exit(1);
481 _exit(WEXITSTATUS(status));
482 }
483 close(fds[0]);
484 set_blocking(fds[1]);
485 pre_exec_fd = fds[1];
486 }
9d0d18b5
WD
487 umask(0);
488 }
489#endif
490
8638dd48 491 if (use_chroot) {
b52c1d9d
MP
492 /*
493 * XXX: The 'use chroot' flag is a fairly reliable
494 * source of confusion, because it fails under two
495 * important circumstances: running as non-root,
496 * running on Win32 (or possibly others). On the
497 * other hand, if you are running as root, then it
498 * might be better to always use chroot.
499 *
500 * So, perhaps if we can't chroot we should just issue
501 * a warning, unless a "require chroot" flag is set,
502 * in which case we fail.
503 */
8638dd48 504 if (chroot(lp_path(i))) {
4875d6b6 505 rsyserr(FLOG, errno, "chroot %s failed",
45c49b52 506 lp_path(i));
68f40ebb 507 io_printf(f_out, "@ERROR: chroot failed\n");
8638dd48
DD
508 return -1;
509 }
3591c066 510
59187666 511 if (!push_dir("/")) {
4875d6b6 512 rsyserr(FLOG, errno, "chdir %s failed\n",
45c49b52 513 lp_path(i));
68f40ebb 514 io_printf(f_out, "@ERROR: chdir failed\n");
8638dd48
DD
515 return -1;
516 }
3591c066 517
716baed7 518 } else {
59187666 519 if (!push_dir(lp_path(i))) {
4875d6b6 520 rsyserr(FLOG, errno, "chdir %s failed\n",
45c49b52 521 lp_path(i));
68f40ebb 522 io_printf(f_out, "@ERROR: chdir failed\n");
716baed7
DD
523 return -1;
524 }
cb13abfe 525 sanitize_paths = 1;
716baed7
DD
526 }
527
528 if (am_root) {
715d1f45
MP
529 /* XXXX: You could argue that if the daemon is started
530 * by a non-root user and they explicitly specify a
531 * gid, then we should try to change to that gid --
532 * this could be possible if it's already in their
533 * supplementary groups. */
534
535 /* TODO: Perhaps we need to document that if rsyncd is
536 * started by somebody other than root it will inherit
537 * all their supplementary groups. */
538
716baed7 539 if (setgid(gid)) {
1732b6c0 540 rsyserr(FLOG, errno, "setgid %d failed", (int)gid);
68f40ebb 541 io_printf(f_out, "@ERROR: setgid failed\n");
8638dd48
DD
542 return -1;
543 }
4f5b0756 544#ifdef HAVE_SETGROUPS
6969ebcf
WD
545 /* Get rid of any supplementary groups this process
546 * might have inheristed. */
547 if (setgroups(1, &gid)) {
1732b6c0 548 rsyserr(FLOG, errno, "setgroups failed");
6969ebcf
WD
549 io_printf(f_out, "@ERROR: setgroups failed\n");
550 return -1;
551 }
552#endif
3591c066 553
716baed7 554 if (setuid(uid)) {
1732b6c0 555 rsyserr(FLOG, errno, "setuid %d failed", (int)uid);
68f40ebb 556 io_printf(f_out, "@ERROR: setuid failed\n");
8638dd48
DD
557 return -1;
558 }
559
6fe05820 560 am_root = (MY_UID() == 0);
3591c066
AT
561 }
562
fed1f3f4
WD
563 if (lp_temp_dir(i) && *lp_temp_dir(i)) {
564 tmpdir = lp_temp_dir(i);
565 if (strlen(tmpdir) >= MAXPATHLEN - 10) {
566 rprintf(FLOG,
567 "the 'temp dir' value for %s is WAY too long -- ignoring.\n",
568 name);
569 tmpdir = NULL;
570 }
571 }
572
68f40ebb 573 io_printf(f_out, "@RSYNCD: OK\n");
3591c066 574
b7061c82
WD
575 maxargs = MAX_ARGS;
576 if (!(argv = new_array(char *, maxargs)))
577 out_of_memory("rsync_module");
3591c066
AT
578 argv[argc++] = "rsyncd";
579
580 while (1) {
2e94e70e 581 if (!read_line(f_in, line, sizeof line - 1))
3591c066 582 return -1;
3591c066 583
2e94e70e
WD
584 if (!*line)
585 break;
3591c066 586
874895d5
AT
587 p = line;
588
b7061c82
WD
589 if (argc == maxargs) {
590 maxargs += MAX_ARGS;
591 if (!(argv = realloc_array(argv, char *, maxargs)))
592 out_of_memory("rsync_module");
593 }
2e94e70e 594 if (!(argv[argc] = strdup(p)))
b7061c82 595 out_of_memory("rsync_module");
3591c066 596
20accf4d
WD
597 switch (start_glob) {
598 case 0:
874895d5 599 argc++;
20accf4d
WD
600 if (strcmp(line, ".") == 0)
601 start_glob = 1;
602 break;
603 case 1:
c95ca2a2
WD
604 if (pre_exec_pid) {
605 err_msg = finish_pre_exec(pre_exec_pid,
141c6265
WD
606 pre_exec_fd, p,
607 argc, argv);
c95ca2a2
WD
608 pre_exec_pid = 0;
609 }
20accf4d
WD
610 request = strdup(p);
611 start_glob = 2;
612 /* FALL THROUGH */
613 default:
c95ca2a2
WD
614 if (!err_msg)
615 glob_expand(name, &argv, &argc, &maxargs);
20accf4d
WD
616 break;
617 }
3591c066
AT
618 }
619
141c6265
WD
620 if (pre_exec_pid) {
621 err_msg = finish_pre_exec(pre_exec_pid, pre_exec_fd, request,
622 argc, argv);
623 }
c95ca2a2 624
18638730 625 verbose = 0; /* future verbosity is controlled by client options */
c9dc1300 626 ret = parse_arguments(&argc, (const char ***) &argv, 0);
7a6653bc 627 quiet = 0; /* Don't let someone try to be tricky. */
3591c066 628
7c2a9e76
WD
629 if (filesfrom_fd == 0)
630 filesfrom_fd = f_in;
631
7b372642 632 if (request) {
97cb8dc2 633 if (*auth_user) {
9e5a5ddb
WD
634 rprintf(FLOG, "rsync %s %s from %s@%s (%s)\n",
635 am_sender ? "on" : "to",
97cb8dc2 636 request, auth_user, host, addr);
d0d56395 637 } else {
9e5a5ddb
WD
638 rprintf(FLOG, "rsync %s %s from %s (%s)\n",
639 am_sender ? "on" : "to",
d0d56395
AT
640 request, host, addr);
641 }
7b372642
AT
642 free(request);
643 }
644
15b7b73d 645#ifndef DEBUG
3591c066 646 /* don't allow the logs to be flooded too fast */
59b0e7a8
WD
647 if (verbose > lp_max_verbosity(i))
648 verbose = lp_max_verbosity(i);
0199b05f 649#endif
3591c066 650
1732b6c0
WD
651 if (protocol_version < 23
652 && (protocol_version == 22 || am_sender))
da3478b2 653 io_start_multiplex_out();
c95ca2a2 654 else if (!ret || err_msg) {
1732b6c0
WD
655 /* We have to get I/O multiplexing started so that we can
656 * get the error back to the client. This means getting
657 * the protocol setup finished first in later versions. */
658 setup_protocol(f_out, f_in);
b03c719f
WD
659 if (!am_sender) {
660 /* Since we failed in our option parsing, we may not
661 * have finished parsing that the client sent us a
662 * --files-from option, so look for it manually.
663 * Without this, the socket would be in the wrong
664 * state for the upcoming error message. */
665 if (!files_from) {
666 int i;
667 for (i = 0; i < argc; i++) {
668 if (strncmp(argv[i], "--files-from", 12) == 0) {
669 files_from = "";
670 break;
671 }
672 }
673 }
674 if (files_from)
675 write_byte(f_out, 0);
676 }
da3478b2 677 io_start_multiplex_out();
554e0a8d 678 }
15b7b73d 679
c95ca2a2
WD
680 if (!ret || err_msg) {
681 if (err_msg)
682 rprintf(FERROR, err_msg);
683 else
684 option_error();
7e561438 685 msleep(400);
4a7144ee 686 exit_cleanup(RERR_UNSUPPORTED);
41979ff8
AT
687 }
688
3e6ddb37
WD
689 if (lp_timeout(i) && lp_timeout(i) > io_timeout)
690 set_io_timeout(lp_timeout(i));
81791cfc 691
f041b025
WD
692 /* If we have some incoming/outgoing chmod changes, append them to
693 * any user-specified changes (making our changes have priority).
694 * We also get a pointer to just our changes so that a receiver
695 * process can use them separately if --perms wasn't specified. */
aaccaa88
WD
696 if (am_sender)
697 p = lp_outgoing_chmod(i);
698 else
699 p = lp_incoming_chmod(i);
8840ec0f 700 if (*p && !(daemon_chmod_modes = parse_chmod(p, &chmod_modes))) {
aaccaa88
WD
701 rprintf(FLOG, "Invalid \"%sing chmod\" directive: %s\n",
702 am_sender ? "outgo" : "incom", p);
703 }
730df9d2 704
c9dc1300 705 start_server(f_in, f_out, argc, argv);
3591c066
AT
706
707 return 0;
708}
709
7a6421fa
AT
710/* send a list of available modules to the client. Don't list those
711 with "list = False". */
3591c066
AT
712static void send_listing(int fd)
713{
714 int n = lp_numservices();
715 int i;
063393d6 716
c0422cea 717 for (i = 0; i < n; i++) {
3591c066 718 if (lp_list(i))
4a7144ee 719 io_printf(fd, "%-15s\t%s\n", lp_name(i), lp_comment(i));
c0422cea 720 }
8f04bb36 721
daa598df 722 if (protocol_version >= 25)
063393d6 723 io_printf(fd,"@RSYNCD: EXIT\n");
3591c066
AT
724}
725
68f40ebb 726/* this is called when a connection is established to a client
3591c066
AT
727 and we want to start talking. The setup of the system is done from
728 here */
68f40ebb 729int start_daemon(int f_in, int f_out)
3591c066 730{
20accf4d 731 char line[1024];
3591c066 732 char *motd;
439a198d
WD
733 char *addr = client_addr(f_in);
734 char *host = client_name(f_in);
c0422cea 735 int i;
4cdf25e4 736
da3478b2
WD
737 io_set_sock_fds(f_in, f_out);
738
2a7d9fe9
WD
739 if (log_initialised)
740 rprintf(FLOG, "connect from %s (%s)\n", host, addr);
741
2e94e70e 742 if (!lp_load(config_file, 0))
65417579 743 exit_cleanup(RERR_SYNTAX);
3591c066 744
2a7d9fe9
WD
745 if (!log_initialised)
746 log_init();
91d324b4 747
68f40ebb
WD
748 if (!am_server) {
749 set_socket_options(f_in, "SO_KEEPALIVE");
34937987
WD
750 if (sockopts)
751 set_socket_options(f_in, sockopts);
752 else
753 set_socket_options(f_in, lp_socket_options());
68f40ebb
WD
754 set_nonblocking(f_in);
755 }
3591c066 756
5a8543b8 757 io_printf(f_out, "@RSYNCD: %d\n", protocol_version);
3591c066
AT
758
759 motd = lp_motd_file();
27d3cdbc 760 if (motd && *motd) {
0090cbdb 761 FILE *f = fopen(motd,"r");
3591c066 762 while (f && !feof(f)) {
2e94e70e 763 int len = fread(line, 1, sizeof line - 1, f);
3591c066
AT
764 if (len > 0) {
765 line[len] = 0;
68f40ebb 766 io_printf(f_out, "%s", line);
3591c066
AT
767 }
768 }
2e94e70e
WD
769 if (f)
770 fclose(f);
68f40ebb 771 io_printf(f_out, "\n");
3591c066
AT
772 }
773
2e94e70e 774 if (!read_line(f_in, line, sizeof line - 1))
91eee594 775 return -1;
91eee594 776
daa598df 777 if (sscanf(line,"@RSYNCD: %d", &remote_protocol) != 1) {
68f40ebb 778 io_printf(f_out, "@ERROR: protocol startup error\n");
91eee594 779 return -1;
4a7144ee 780 }
daa598df
WD
781 if (protocol_version > remote_protocol)
782 protocol_version = remote_protocol;
91eee594 783
c0422cea
WD
784 line[0] = 0;
785 if (!read_line(f_in, line, sizeof line - 1))
786 return -1;
3591c066 787
c0422cea 788 if (!*line || strcmp(line, "#list") == 0) {
b3e15181
WD
789 rprintf(FLOG, "module-list request from %s (%s)\n",
790 host, addr);
c0422cea
WD
791 send_listing(f_out);
792 return -1;
793 }
3591c066 794
c0422cea
WD
795 if (*line == '#') {
796 /* it's some sort of command that I don't understand */
797 io_printf(f_out, "@ERROR: Unknown command '%s'\n", line);
798 return -1;
799 }
3591c066 800
c0422cea 801 if ((i = lp_number(line)) < 0) {
c0422cea
WD
802 rprintf(FLOG, "unknown module '%s' tried from %s (%s)\n",
803 line, host, addr);
804 io_printf(f_out, "@ERROR: Unknown module '%s'\n", line);
805 return -1;
3591c066
AT
806 }
807
d749eb68
WD
808#ifdef HAVE_SIGACTION
809 sigact.sa_flags = SA_NOCLDSTOP;
810#endif
811 SIGACTION(SIGCHLD, remember_children);
812
439a198d 813 return rsync_module(f_in, f_out, i, addr, host);
3591c066
AT
814}
815
3591c066
AT
816int daemon_main(void)
817{
8638dd48 818 char *pid_file;
1a016bfd 819
3591c066 820 if (is_a_socket(STDIN_FILENO)) {
41979ff8
AT
821 int i;
822
823 /* we are running via inetd - close off stdout and
4a7144ee
WD
824 * stderr so that library functions (and getopt) don't
825 * try to use them. Redirect them to /dev/null */
2e94e70e 826 for (i = 1; i < 3; i++) {
4a7144ee 827 close(i);
41979ff8
AT
828 open("/dev/null", O_RDWR);
829 }
3eb38818 830
68f40ebb 831 return start_daemon(STDIN_FILENO, STDIN_FILENO);
3591c066
AT
832 }
833
13e29995 834 if (!no_detach)
4a7144ee 835 become_daemon();
3591c066 836
2e94e70e 837 if (!lp_load(config_file, 1))
65417579 838 exit_cleanup(RERR_SYNTAX);
f9e940ef 839
0c56b1ad
WD
840 if (rsync_port == 0 && (rsync_port = lp_rsync_port()) == 0)
841 rsync_port = RSYNC_PORT;
3dfe6e97 842 if (bind_address == NULL && *lp_bind_address())
986aaaaa 843 bind_address = lp_bind_address();
0c56b1ad 844
45a83540 845 log_init();
f9e940ef 846
9e5a5ddb 847 rprintf(FLOG, "rsyncd version %s starting, listening on port %d\n",
4a7144ee
WD
848 RSYNC_VERSION, rsync_port);
849 /* TODO: If listening on a particular address, then show that
850 * address too. In fact, why not just do inet_ntop on the
851 * local address??? */
e42c9458 852
8638dd48 853 if (((pid_file = lp_pid_file()) != NULL) && (*pid_file != '\0')) {
ad517ce5
DD
854 char pidbuf[16];
855 int fd;
05b7bab8 856 pid_t pid = getpid();
8638dd48 857 cleanup_set_pid(pid);
ad517ce5
DD
858 if ((fd = do_open(lp_pid_file(), O_WRONLY|O_CREAT|O_TRUNC,
859 0666 & ~orig_umask)) == -1) {
4a7144ee 860 cleanup_set_pid(0);
4875d6b6 861 rsyserr(FLOG, errno, "failed to create pid file %s",
45c49b52 862 pid_file);
4a7144ee 863 exit_cleanup(RERR_FILEIO);
8638dd48 864 }
05b7bab8 865 snprintf(pidbuf, sizeof pidbuf, "%ld\n", (long)pid);
ad517ce5
DD
866 write(fd, pidbuf, strlen(pidbuf));
867 close(fd);
8638dd48
DD
868 }
869
8ef4ffd6
AT
870 start_accept_loop(rsync_port, start_daemon);
871 return -1;
3591c066 872}