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