Made the formerly static sigchld_handler() available as a
[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
WD
44extern int io_timeout;
45extern int orig_umask;
46extern int no_detach;
47extern int default_af_hint;
48extern char *bind_address;
34937987 49extern char *sockopts;
6e195fe9 50extern char *config_file;
bf4679e8 51extern char *files_from;
fed1f3f4 52extern char *tmpdir;
730df9d2
WD
53extern struct chmod_mode_struct *chmod_modes;
54extern struct filter_list_struct server_filter_list;
6e195fe9
WD
55
56char *auth_user;
18638730 57int read_only = 0;
f39b2060
WD
58int daemon_log_format_has_i = 0;
59int daemon_log_format_has_o_or_i = 0;
211bc43b 60int module_id = -1;
8840ec0f 61struct chmod_mode_struct *daemon_chmod_modes;
3591c066 62
e7bf7c01
WD
63/* Length of lp_path() string when in daemon mode & not chrooted, else 0. */
64unsigned int module_dirlen = 0;
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
b9f592fb
WD
112int start_inband_exchange(char *user, char *path, int f_in, int f_out,
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
68f40ebb 267static int rsync_module(int f_in, int f_out, int i)
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;
09021eab
DD
276 char *addr = client_addr(f_in);
277 char *host = client_name(f_in);
874895d5 278 char *name = lp_name(i);
8638dd48 279 int use_chroot = lp_use_chroot(i);
2e94e70e 280 int start_glob = 0;
c95ca2a2
WD
281 int ret, pre_exec_fd = -1;
282 pid_t pre_exec_pid = 0;
2e94e70e 283 char *request = NULL;
56c473b7
AT
284
285 if (!allow_access(addr, host, lp_hosts_allow(i), lp_hosts_deny(i))) {
1732b6c0 286 rprintf(FLOG, "rsync denied on module %s from %s (%s)\n",
31ec50d7 287 name, host, addr);
c0422cea
WD
288 if (!lp_list(i))
289 io_printf(f_out, "@ERROR: Unknown module '%s'\n", name);
290 else {
291 io_printf(f_out,
292 "@ERROR: access denied to %s from %s (%s)\n",
293 name, host, addr);
294 }
56c473b7
AT
295 return -1;
296 }
3591c066 297
68f40ebb 298 if (am_daemon && am_server) {
9e5a5ddb 299 rprintf(FLOG, "rsync allowed access on module %s from %s (%s)\n",
68f40ebb
WD
300 name, host, addr);
301 }
302
5e71c444 303 if (!claim_connection(lp_lock_file(i), lp_max_connections(i))) {
8d72ef6e 304 if (errno) {
1732b6c0 305 rsyserr(FLOG, errno, "failed to open lock file %s",
45c49b52 306 lp_lock_file(i));
4875d6b6 307 io_printf(f_out, "@ERROR: failed to open lock file\n");
8d72ef6e 308 } else {
1732b6c0 309 rprintf(FLOG, "max connections (%d) reached\n",
5e71c444 310 lp_max_connections(i));
4ccfd96c 311 io_printf(f_out, "@ERROR: max connections (%d) reached -- try again later\n",
837cbad9 312 lp_max_connections(i));
8d72ef6e 313 }
0c515f17
AT
314 return -1;
315 }
316
45c5b903 317 auth_user = auth_server(f_in, f_out, i, host, addr, "@RSYNCD: AUTHREQD ");
d0d56395 318
97cb8dc2 319 if (!auth_user) {
68f40ebb 320 io_printf(f_out, "@ERROR: auth failed on module %s\n", name);
4a7144ee 321 return -1;
d0d56395
AT
322 }
323
3591c066
AT
324 module_id = i;
325
211bc43b 326 if (lp_read_only(i))
18638730
WD
327 read_only = 1;
328
f39b2060 329 if (lp_transfer_logging(i)) {
624d6be2 330 if (log_format_has(lp_log_format(i), 'i'))
f39b2060
WD
331 daemon_log_format_has_i = 1;
332 if (daemon_log_format_has_i
624d6be2 333 || log_format_has(lp_log_format(i), 'o'))
f39b2060
WD
334 daemon_log_format_has_o_or_i = 1;
335 }
c0814b81 336
6fe05820 337 am_root = (MY_UID() == 0);
8ef4ffd6 338
716baed7
DD
339 if (am_root) {
340 p = lp_uid(i);
341 if (!name_to_uid(p, &uid)) {
2e94e70e 342 if (!isdigit(*(unsigned char *)p)) {
1732b6c0 343 rprintf(FLOG, "Invalid uid %s\n", p);
68f40ebb 344 io_printf(f_out, "@ERROR: invalid uid %s\n", p);
716baed7 345 return -1;
4a7144ee 346 }
716baed7
DD
347 uid = atoi(p);
348 }
349
350 p = lp_gid(i);
351 if (!name_to_gid(p, &gid)) {
2e94e70e 352 if (!isdigit(*(unsigned char *)p)) {
1732b6c0 353 rprintf(FLOG, "Invalid gid %s\n", p);
68f40ebb 354 io_printf(f_out, "@ERROR: invalid gid %s\n", p);
716baed7 355 return -1;
4a7144ee 356 }
716baed7
DD
357 gid = atoi(p);
358 }
8ef4ffd6 359 }
3b2b5345 360
4a7144ee
WD
361 /* TODO: If we're not root, but the configuration requests
362 * that we change to some uid other than the current one, then
363 * log a warning. */
364
365 /* TODO: Perhaps take a list of gids, and make them into the
366 * supplementary groups. */
8ef4ffd6 367
46bffd98 368 if (use_chroot || (module_dirlen = strlen(lp_path(i))) == 1) {
e7bf7c01 369 module_dirlen = 0;
7842418b 370 set_filter_dir("/", 1);
46bffd98 371 } else
7842418b 372 set_filter_dir(lp_path(i), module_dirlen);
e7bf7c01
WD
373
374 p = lp_filter(i);
ebfd1a1c 375 parse_rule(&server_filter_list, p, MATCHFLG_WORD_SPLIT,
2c8c8bba 376 XFLG_ABS_IF_SLASH);
837cbad9 377
cd64343a 378 p = lp_include_from(i);
ebfd1a1c 379 parse_filter_file(&server_filter_list, p, MATCHFLG_INCLUDE,
2c8c8bba 380 XFLG_ABS_IF_SLASH | XFLG_OLD_PREFIXES | XFLG_FATAL_ERRORS);
cd64343a
DD
381
382 p = lp_include(i);
ebfd1a1c 383 parse_rule(&server_filter_list, p,
0a68f869 384 MATCHFLG_INCLUDE | MATCHFLG_WORD_SPLIT,
2c8c8bba 385 XFLG_ABS_IF_SLASH | XFLG_OLD_PREFIXES);
cd64343a 386
8f3a2d54 387 p = lp_exclude_from(i);
ebfd1a1c 388 parse_filter_file(&server_filter_list, p, 0,
2c8c8bba 389 XFLG_ABS_IF_SLASH | XFLG_OLD_PREFIXES | XFLG_FATAL_ERRORS);
8f3a2d54 390
5805327b 391 p = lp_exclude(i);
ebfd1a1c 392 parse_rule(&server_filter_list, p, MATCHFLG_WORD_SPLIT,
2c8c8bba 393 XFLG_ABS_IF_SLASH | XFLG_OLD_PREFIXES);
8f3a2d54 394
45a83540 395 log_init();
1a016bfd 396
9d0d18b5 397#ifdef HAVE_PUTENV
c95ca2a2 398 if (*lp_prexfer_exec(i) || *lp_postxfer_exec(i)) {
9d0d18b5
WD
399 char *modname, *modpath, *hostaddr, *hostname, *username;
400 int status;
401 if (asprintf(&modname, "RSYNC_MODULE_NAME=%s", name) < 0
402 || asprintf(&modpath, "RSYNC_MODULE_PATH=%s", lp_path(i)) < 0
403 || asprintf(&hostaddr, "RSYNC_HOST_ADDR=%s", addr) < 0
404 || asprintf(&hostname, "RSYNC_HOST_NAME=%s", host) < 0
405 || asprintf(&username, "RSYNC_USER_NAME=%s", auth_user) < 0)
406 out_of_memory("rsync_module");
407 putenv(modname);
408 putenv(modpath);
409 putenv(hostaddr);
410 putenv(hostname);
411 putenv(username);
412 umask(orig_umask);
c95ca2a2
WD
413 /* For post-xfer exec, fork a new process to run the rsync
414 * daemon while this process waits for the exit status and
415 * runs the indicated command at that point. */
416 if (*lp_postxfer_exec(i)) {
9d0d18b5
WD
417 pid_t pid = fork();
418 if (pid < 0) {
419 rsyserr(FLOG, errno, "fork failed");
420 io_printf(f_out, "@ERROR: fork failed\n");
421 return -1;
422 }
423 if (pid) {
424 char *ret1, *ret2;
c95ca2a2
WD
425 if (wait_process(pid, &status, 0) < 0)
426 status = -1;
9d0d18b5
WD
427 if (asprintf(&ret1, "RSYNC_RAW_STATUS=%d", status) > 0)
428 putenv(ret1);
429 if (WIFEXITED(status))
430 status = WEXITSTATUS(status);
431 else
432 status = -1;
433 if (asprintf(&ret2, "RSYNC_EXIT_STATUS=%d", status) > 0)
434 putenv(ret2);
c95ca2a2 435 system(lp_postxfer_exec(i));
9d0d18b5
WD
436 _exit(status);
437 }
438 }
c95ca2a2
WD
439 /* For pre-xfer exec, fork a child process to run the indicated
440 * command, though it first waits for the parent process to
441 * send us the user's request via a pipe. */
442 if (*lp_prexfer_exec(i)) {
443 int fds[2];
444 if (pipe(fds) < 0 || (pre_exec_pid = fork()) < 0) {
445 rsyserr(FLOG, errno, "pre-xfer exec preparation failed");
446 io_printf(f_out, "@ERROR: pre-xfer exec preparation failed\n");
447 return -1;
448 }
449 if (pre_exec_pid == 0) {
141c6265
WD
450 char buf[BIGPATHBUFLEN];
451 int j, len;
c95ca2a2
WD
452 close(fds[1]);
453 set_blocking(fds[0]);
141c6265 454 len = read_arg_from_pipe(fds[0], buf, BIGPATHBUFLEN);
c95ca2a2
WD
455 if (len <= 0)
456 _exit(1);
c95ca2a2
WD
457 if (asprintf(&p, "RSYNC_REQUEST=%s", buf) < 0)
458 out_of_memory("rsync_module");
459 putenv(p);
141c6265
WD
460 for (j = 0; ; j++) {
461 len = read_arg_from_pipe(fds[0], buf,
462 BIGPATHBUFLEN);
463 if (len <= 0) {
464 if (!len)
465 break;
466 _exit(1);
467 }
468 if (asprintf(&p, "RSYNC_ARG%d=%s", j, buf) < 0)
469 out_of_memory("rsync_module");
470 putenv(p);
471 }
472 close(fds[0]);
c95ca2a2
WD
473 close(STDIN_FILENO);
474 close(STDOUT_FILENO);
475 status = system(lp_prexfer_exec(i));
476 if (!WIFEXITED(status))
477 _exit(1);
478 _exit(WEXITSTATUS(status));
479 }
480 close(fds[0]);
481 set_blocking(fds[1]);
482 pre_exec_fd = fds[1];
483 }
9d0d18b5
WD
484 umask(0);
485 }
486#endif
487
8638dd48 488 if (use_chroot) {
b52c1d9d
MP
489 /*
490 * XXX: The 'use chroot' flag is a fairly reliable
491 * source of confusion, because it fails under two
492 * important circumstances: running as non-root,
493 * running on Win32 (or possibly others). On the
494 * other hand, if you are running as root, then it
495 * might be better to always use chroot.
496 *
497 * So, perhaps if we can't chroot we should just issue
498 * a warning, unless a "require chroot" flag is set,
499 * in which case we fail.
500 */
8638dd48 501 if (chroot(lp_path(i))) {
4875d6b6 502 rsyserr(FLOG, errno, "chroot %s failed",
45c49b52 503 lp_path(i));
68f40ebb 504 io_printf(f_out, "@ERROR: chroot failed\n");
8638dd48
DD
505 return -1;
506 }
3591c066 507
59187666 508 if (!push_dir("/")) {
4875d6b6 509 rsyserr(FLOG, errno, "chdir %s failed\n",
45c49b52 510 lp_path(i));
68f40ebb 511 io_printf(f_out, "@ERROR: chdir failed\n");
8638dd48
DD
512 return -1;
513 }
3591c066 514
716baed7 515 } else {
59187666 516 if (!push_dir(lp_path(i))) {
4875d6b6 517 rsyserr(FLOG, errno, "chdir %s failed\n",
45c49b52 518 lp_path(i));
68f40ebb 519 io_printf(f_out, "@ERROR: chdir failed\n");
716baed7
DD
520 return -1;
521 }
cb13abfe 522 sanitize_paths = 1;
716baed7
DD
523 }
524
525 if (am_root) {
715d1f45
MP
526 /* XXXX: You could argue that if the daemon is started
527 * by a non-root user and they explicitly specify a
528 * gid, then we should try to change to that gid --
529 * this could be possible if it's already in their
530 * supplementary groups. */
531
532 /* TODO: Perhaps we need to document that if rsyncd is
533 * started by somebody other than root it will inherit
534 * all their supplementary groups. */
535
716baed7 536 if (setgid(gid)) {
1732b6c0 537 rsyserr(FLOG, errno, "setgid %d failed", (int)gid);
68f40ebb 538 io_printf(f_out, "@ERROR: setgid failed\n");
8638dd48
DD
539 return -1;
540 }
4f5b0756 541#ifdef HAVE_SETGROUPS
6969ebcf
WD
542 /* Get rid of any supplementary groups this process
543 * might have inheristed. */
544 if (setgroups(1, &gid)) {
1732b6c0 545 rsyserr(FLOG, errno, "setgroups failed");
6969ebcf
WD
546 io_printf(f_out, "@ERROR: setgroups failed\n");
547 return -1;
548 }
549#endif
3591c066 550
716baed7 551 if (setuid(uid)) {
1732b6c0 552 rsyserr(FLOG, errno, "setuid %d failed", (int)uid);
68f40ebb 553 io_printf(f_out, "@ERROR: setuid failed\n");
8638dd48
DD
554 return -1;
555 }
556
6fe05820 557 am_root = (MY_UID() == 0);
3591c066
AT
558 }
559
fed1f3f4
WD
560 if (lp_temp_dir(i) && *lp_temp_dir(i)) {
561 tmpdir = lp_temp_dir(i);
562 if (strlen(tmpdir) >= MAXPATHLEN - 10) {
563 rprintf(FLOG,
564 "the 'temp dir' value for %s is WAY too long -- ignoring.\n",
565 name);
566 tmpdir = NULL;
567 }
568 }
569
68f40ebb 570 io_printf(f_out, "@RSYNCD: OK\n");
3591c066 571
b7061c82
WD
572 maxargs = MAX_ARGS;
573 if (!(argv = new_array(char *, maxargs)))
574 out_of_memory("rsync_module");
3591c066
AT
575 argv[argc++] = "rsyncd";
576
577 while (1) {
2e94e70e 578 if (!read_line(f_in, line, sizeof line - 1))
3591c066 579 return -1;
3591c066 580
2e94e70e
WD
581 if (!*line)
582 break;
3591c066 583
874895d5
AT
584 p = line;
585
b7061c82
WD
586 if (argc == maxargs) {
587 maxargs += MAX_ARGS;
588 if (!(argv = realloc_array(argv, char *, maxargs)))
589 out_of_memory("rsync_module");
590 }
2e94e70e 591 if (!(argv[argc] = strdup(p)))
b7061c82 592 out_of_memory("rsync_module");
3591c066 593
20accf4d
WD
594 switch (start_glob) {
595 case 0:
874895d5 596 argc++;
20accf4d
WD
597 if (strcmp(line, ".") == 0)
598 start_glob = 1;
599 break;
600 case 1:
c95ca2a2
WD
601 if (pre_exec_pid) {
602 err_msg = finish_pre_exec(pre_exec_pid,
141c6265
WD
603 pre_exec_fd, p,
604 argc, argv);
c95ca2a2
WD
605 pre_exec_pid = 0;
606 }
20accf4d
WD
607 request = strdup(p);
608 start_glob = 2;
609 /* FALL THROUGH */
610 default:
c95ca2a2
WD
611 if (!err_msg)
612 glob_expand(name, &argv, &argc, &maxargs);
20accf4d
WD
613 break;
614 }
3591c066
AT
615 }
616
141c6265
WD
617 if (pre_exec_pid) {
618 err_msg = finish_pre_exec(pre_exec_pid, pre_exec_fd, request,
619 argc, argv);
620 }
c95ca2a2 621
18638730 622 verbose = 0; /* future verbosity is controlled by client options */
c9dc1300 623 ret = parse_arguments(&argc, (const char ***) &argv, 0);
7a6653bc 624 quiet = 0; /* Don't let someone try to be tricky. */
3591c066 625
7c2a9e76
WD
626 if (filesfrom_fd == 0)
627 filesfrom_fd = f_in;
628
7b372642 629 if (request) {
97cb8dc2 630 if (*auth_user) {
9e5a5ddb
WD
631 rprintf(FLOG, "rsync %s %s from %s@%s (%s)\n",
632 am_sender ? "on" : "to",
97cb8dc2 633 request, auth_user, host, addr);
d0d56395 634 } else {
9e5a5ddb
WD
635 rprintf(FLOG, "rsync %s %s from %s (%s)\n",
636 am_sender ? "on" : "to",
d0d56395
AT
637 request, host, addr);
638 }
7b372642
AT
639 free(request);
640 }
641
15b7b73d 642#ifndef DEBUG
3591c066 643 /* don't allow the logs to be flooded too fast */
59b0e7a8
WD
644 if (verbose > lp_max_verbosity(i))
645 verbose = lp_max_verbosity(i);
0199b05f 646#endif
3591c066 647
1732b6c0
WD
648 if (protocol_version < 23
649 && (protocol_version == 22 || am_sender))
da3478b2 650 io_start_multiplex_out();
c95ca2a2 651 else if (!ret || err_msg) {
1732b6c0
WD
652 /* We have to get I/O multiplexing started so that we can
653 * get the error back to the client. This means getting
654 * the protocol setup finished first in later versions. */
655 setup_protocol(f_out, f_in);
b03c719f
WD
656 if (!am_sender) {
657 /* Since we failed in our option parsing, we may not
658 * have finished parsing that the client sent us a
659 * --files-from option, so look for it manually.
660 * Without this, the socket would be in the wrong
661 * state for the upcoming error message. */
662 if (!files_from) {
663 int i;
664 for (i = 0; i < argc; i++) {
665 if (strncmp(argv[i], "--files-from", 12) == 0) {
666 files_from = "";
667 break;
668 }
669 }
670 }
671 if (files_from)
672 write_byte(f_out, 0);
673 }
da3478b2 674 io_start_multiplex_out();
554e0a8d 675 }
15b7b73d 676
c95ca2a2
WD
677 if (!ret || err_msg) {
678 if (err_msg)
679 rprintf(FERROR, err_msg);
680 else
681 option_error();
7e561438 682 msleep(400);
4a7144ee 683 exit_cleanup(RERR_UNSUPPORTED);
41979ff8
AT
684 }
685
3e6ddb37
WD
686 if (lp_timeout(i) && lp_timeout(i) > io_timeout)
687 set_io_timeout(lp_timeout(i));
81791cfc 688
aaccaa88
WD
689 if (am_sender)
690 p = lp_outgoing_chmod(i);
691 else
692 p = lp_incoming_chmod(i);
8840ec0f 693 if (*p && !(daemon_chmod_modes = parse_chmod(p, &chmod_modes))) {
aaccaa88
WD
694 rprintf(FLOG, "Invalid \"%sing chmod\" directive: %s\n",
695 am_sender ? "outgo" : "incom", p);
696 }
730df9d2 697
c9dc1300 698 start_server(f_in, f_out, argc, argv);
3591c066
AT
699
700 return 0;
701}
702
7a6421fa
AT
703/* send a list of available modules to the client. Don't list those
704 with "list = False". */
3591c066
AT
705static void send_listing(int fd)
706{
707 int n = lp_numservices();
708 int i;
063393d6 709
c0422cea 710 for (i = 0; i < n; i++) {
3591c066 711 if (lp_list(i))
4a7144ee 712 io_printf(fd, "%-15s\t%s\n", lp_name(i), lp_comment(i));
c0422cea 713 }
8f04bb36 714
daa598df 715 if (protocol_version >= 25)
063393d6 716 io_printf(fd,"@RSYNCD: EXIT\n");
3591c066
AT
717}
718
68f40ebb 719/* this is called when a connection is established to a client
3591c066
AT
720 and we want to start talking. The setup of the system is done from
721 here */
68f40ebb 722int start_daemon(int f_in, int f_out)
3591c066 723{
20accf4d 724 char line[1024];
3591c066 725 char *motd;
c0422cea 726 int i;
4cdf25e4 727
da3478b2
WD
728 io_set_sock_fds(f_in, f_out);
729
2e94e70e 730 if (!lp_load(config_file, 0))
65417579 731 exit_cleanup(RERR_SYNTAX);
3591c066 732
30e8c8e1
DD
733 log_init();
734
68f40ebb
WD
735 if (!am_server) {
736 set_socket_options(f_in, "SO_KEEPALIVE");
34937987
WD
737 if (sockopts)
738 set_socket_options(f_in, sockopts);
739 else
740 set_socket_options(f_in, lp_socket_options());
68f40ebb
WD
741 set_nonblocking(f_in);
742 }
3591c066 743
5a8543b8 744 io_printf(f_out, "@RSYNCD: %d\n", protocol_version);
3591c066
AT
745
746 motd = lp_motd_file();
27d3cdbc 747 if (motd && *motd) {
0090cbdb 748 FILE *f = fopen(motd,"r");
3591c066 749 while (f && !feof(f)) {
2e94e70e 750 int len = fread(line, 1, sizeof line - 1, f);
3591c066
AT
751 if (len > 0) {
752 line[len] = 0;
68f40ebb 753 io_printf(f_out, "%s", line);
3591c066
AT
754 }
755 }
2e94e70e
WD
756 if (f)
757 fclose(f);
68f40ebb 758 io_printf(f_out, "\n");
3591c066
AT
759 }
760
2e94e70e 761 if (!read_line(f_in, line, sizeof line - 1))
91eee594 762 return -1;
91eee594 763
daa598df 764 if (sscanf(line,"@RSYNCD: %d", &remote_protocol) != 1) {
68f40ebb 765 io_printf(f_out, "@ERROR: protocol startup error\n");
91eee594 766 return -1;
4a7144ee 767 }
daa598df
WD
768 if (protocol_version > remote_protocol)
769 protocol_version = remote_protocol;
91eee594 770
c0422cea
WD
771 line[0] = 0;
772 if (!read_line(f_in, line, sizeof line - 1))
773 return -1;
3591c066 774
c0422cea 775 if (!*line || strcmp(line, "#list") == 0) {
b3e15181
WD
776 char *addr = client_addr(f_in);
777 char *host = client_name(f_in);
778 rprintf(FLOG, "module-list request from %s (%s)\n",
779 host, addr);
c0422cea
WD
780 send_listing(f_out);
781 return -1;
782 }
3591c066 783
c0422cea
WD
784 if (*line == '#') {
785 /* it's some sort of command that I don't understand */
786 io_printf(f_out, "@ERROR: Unknown command '%s'\n", line);
787 return -1;
788 }
3591c066 789
c0422cea
WD
790 if ((i = lp_number(line)) < 0) {
791 char *addr = client_addr(f_in);
792 char *host = client_name(f_in);
793 rprintf(FLOG, "unknown module '%s' tried from %s (%s)\n",
794 line, host, addr);
795 io_printf(f_out, "@ERROR: Unknown module '%s'\n", line);
796 return -1;
3591c066
AT
797 }
798
68f40ebb 799 return rsync_module(f_in, f_out, i);
3591c066
AT
800}
801
3591c066
AT
802int daemon_main(void)
803{
8638dd48 804 char *pid_file;
1a016bfd 805
3591c066 806 if (is_a_socket(STDIN_FILENO)) {
41979ff8
AT
807 int i;
808
809 /* we are running via inetd - close off stdout and
4a7144ee
WD
810 * stderr so that library functions (and getopt) don't
811 * try to use them. Redirect them to /dev/null */
2e94e70e 812 for (i = 1; i < 3; i++) {
4a7144ee 813 close(i);
41979ff8
AT
814 open("/dev/null", O_RDWR);
815 }
3eb38818 816
68f40ebb 817 return start_daemon(STDIN_FILENO, STDIN_FILENO);
3591c066
AT
818 }
819
13e29995 820 if (!no_detach)
4a7144ee 821 become_daemon();
3591c066 822
2e94e70e 823 if (!lp_load(config_file, 1))
65417579 824 exit_cleanup(RERR_SYNTAX);
f9e940ef 825
0c56b1ad
WD
826 if (rsync_port == 0 && (rsync_port = lp_rsync_port()) == 0)
827 rsync_port = RSYNC_PORT;
3dfe6e97 828 if (bind_address == NULL && *lp_bind_address())
986aaaaa 829 bind_address = lp_bind_address();
0c56b1ad 830
45a83540 831 log_init();
f9e940ef 832
9e5a5ddb 833 rprintf(FLOG, "rsyncd version %s starting, listening on port %d\n",
4a7144ee
WD
834 RSYNC_VERSION, rsync_port);
835 /* TODO: If listening on a particular address, then show that
836 * address too. In fact, why not just do inet_ntop on the
837 * local address??? */
e42c9458 838
8638dd48 839 if (((pid_file = lp_pid_file()) != NULL) && (*pid_file != '\0')) {
ad517ce5
DD
840 char pidbuf[16];
841 int fd;
05b7bab8 842 pid_t pid = getpid();
8638dd48 843 cleanup_set_pid(pid);
ad517ce5
DD
844 if ((fd = do_open(lp_pid_file(), O_WRONLY|O_CREAT|O_TRUNC,
845 0666 & ~orig_umask)) == -1) {
4a7144ee 846 cleanup_set_pid(0);
4875d6b6 847 rsyserr(FLOG, errno, "failed to create pid file %s",
45c49b52 848 pid_file);
4a7144ee 849 exit_cleanup(RERR_FILEIO);
8638dd48 850 }
05b7bab8 851 snprintf(pidbuf, sizeof pidbuf, "%ld\n", (long)pid);
ad517ce5
DD
852 write(fd, pidbuf, strlen(pidbuf));
853 close(fd);
8638dd48
DD
854 }
855
8ef4ffd6
AT
856 start_accept_loop(rsync_port, start_daemon);
857 return -1;
3591c066 858}