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