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