Added --log-file and several logfile_* support variables.
[rsync/rsync.git] / clientserver.c
... / ...
CommitLineData
1/*
2 * The socket based protocol for setting up a connection with rsyncd.
3 *
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
7 *
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.
12 *
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.
17 *
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.
21 */
22
23#include "rsync.h"
24
25extern int verbose;
26extern int quiet;
27extern int list_only;
28extern int am_sender;
29extern int am_server;
30extern int am_daemon;
31extern int am_root;
32extern int rsync_port;
33extern int kluge_around_eof;
34extern int daemon_over_rsh;
35extern int sanitize_paths;
36extern int filesfrom_fd;
37extern int remote_protocol;
38extern int protocol_version;
39extern int io_timeout;
40extern int no_detach;
41extern int default_af_hint;
42extern mode_t orig_umask;
43extern char *bind_address;
44extern char *sockopts;
45extern char *config_file;
46extern char *files_from;
47extern char *tmpdir;
48extern struct chmod_mode_struct *chmod_modes;
49extern struct filter_list_struct server_filter_list;
50
51char *auth_user;
52int read_only = 0;
53int daemon_log_format_has_i = 0;
54int daemon_log_format_has_o_or_i = 0;
55int module_id = -1;
56struct chmod_mode_struct *daemon_chmod_modes;
57
58/* Length of lp_path() string when in daemon mode & not chrooted, else 0. */
59unsigned int module_dirlen = 0;
60
61#ifdef HAVE_SIGACTION
62static struct sigaction sigact;
63#endif
64
65/**
66 * Run a client connected to an rsyncd. The alternative to this
67 * function for remote-shell connections is do_cmd().
68 *
69 * After negotiating which module to use and reading the server's
70 * motd, this hands over to client_run(). Telling the server the
71 * module will cause it to chroot/setuid/etc.
72 *
73 * Instead of doing a transfer, the client may at this stage instead
74 * get a listing of remote modules and exit.
75 *
76 * @return -1 for error in startup, or the result of client_run().
77 * Either way, it eventually gets passed to exit_cleanup().
78 **/
79int start_socket_client(char *host, char *path, int argc, char *argv[])
80{
81 int fd, ret;
82 char *p, *user = NULL;
83
84 /* This is redundant with code in start_inband_exchange(), but this
85 * short-circuits a problem in the client before we open a socket,
86 * and the extra check won't hurt. */
87 if (*path == '/') {
88 rprintf(FERROR,
89 "ERROR: The remote path must start with a module name not a /\n");
90 return -1;
91 }
92
93 if ((p = strrchr(host, '@')) != NULL) {
94 user = host;
95 host = p+1;
96 *p = '\0';
97 }
98
99 fd = open_socket_out_wrapped(host, rsync_port, bind_address,
100 default_af_hint);
101 if (fd == -1)
102 exit_cleanup(RERR_SOCKETIO);
103
104 set_socket_options(fd, sockopts);
105
106 ret = start_inband_exchange(user, path, fd, fd, argc);
107
108 return ret ? ret : client_run(fd, fd, -1, argc, argv);
109}
110
111int start_inband_exchange(char *user, char *path, int f_in, int f_out,
112 int argc)
113{
114 int i;
115 char *sargs[MAX_ARGS];
116 int sargc = 0;
117 char line[BIGPATHBUFLEN];
118 char *p;
119
120 if (argc == 0 && !am_sender)
121 list_only |= 1;
122
123 if (*path == '/') {
124 rprintf(FERROR,
125 "ERROR: The remote path must start with a module name\n");
126 return -1;
127 }
128
129 if (!user)
130 user = getenv("USER");
131 if (!user)
132 user = getenv("LOGNAME");
133
134 io_printf(f_out, "@RSYNCD: %d\n", protocol_version);
135
136 if (!read_line(f_in, line, sizeof line - 1)) {
137 rprintf(FERROR, "rsync: did not see server greeting\n");
138 return -1;
139 }
140
141 if (sscanf(line,"@RSYNCD: %d", &remote_protocol) != 1) {
142 /* note that read_line strips of \n or \r */
143 rprintf(FERROR, "rsync: server sent \"%s\" rather than greeting\n",
144 line);
145 return -1;
146 }
147 if (protocol_version > remote_protocol)
148 protocol_version = remote_protocol;
149
150 if (list_only && protocol_version >= 29)
151 list_only |= 2;
152
153 /* set daemon_over_rsh to false since we need to build the
154 * true set of args passed through the rsh/ssh connection;
155 * this is a no-op for direct-socket-connection mode */
156 daemon_over_rsh = 0;
157 server_options(sargs, &sargc);
158
159 sargs[sargc++] = ".";
160
161 if (path && *path)
162 sargs[sargc++] = path;
163
164 sargs[sargc] = NULL;
165
166 if (verbose > 1)
167 print_child_argv(sargs);
168
169 p = strchr(path,'/');
170 if (p) *p = 0;
171 io_printf(f_out, "%s\n", path);
172 if (p) *p = '/';
173
174 /* Old servers may just drop the connection here,
175 rather than sending a proper EXIT command. Yuck. */
176 kluge_around_eof = list_only && protocol_version < 25 ? 1 : 0;
177
178 while (1) {
179 if (!read_line(f_in, line, sizeof line - 1)) {
180 rprintf(FERROR, "rsync: didn't get server startup line\n");
181 return -1;
182 }
183
184 if (strncmp(line,"@RSYNCD: AUTHREQD ",18) == 0) {
185 auth_client(f_out, user, line+18);
186 continue;
187 }
188
189 if (strcmp(line,"@RSYNCD: OK") == 0)
190 break;
191
192 if (strcmp(line,"@RSYNCD: EXIT") == 0) {
193 /* This is sent by recent versions of the
194 * server to terminate the listing of modules.
195 * We don't want to go on and transfer
196 * anything; just exit. */
197 exit(0);
198 }
199
200 if (strncmp(line, "@ERROR", 6) == 0) {
201 rprintf(FERROR, "%s\n", line);
202 /* This is always fatal; the server will now
203 * close the socket. */
204 return -1;
205 }
206
207 rprintf(FINFO, "%s\n", line);
208 }
209 kluge_around_eof = 0;
210
211 for (i = 0; i < sargc; i++) {
212 io_printf(f_out, "%s\n", sargs[i]);
213 }
214 io_printf(f_out, "\n");
215
216 if (protocol_version < 23) {
217 if (protocol_version == 22 || !am_sender)
218 io_start_multiplex_in();
219 }
220
221 return 0;
222}
223
224static char *finish_pre_exec(pid_t pid, int fd, char *request,
225 int argc, char *argv[])
226{
227 int j, status = -1;
228
229 if (request) {
230 write_buf(fd, request, strlen(request)+1);
231 for (j = 0; j < argc; j++)
232 write_buf(fd, argv[j], strlen(argv[j])+1);
233 }
234
235 write_byte(fd, 0);
236
237 close(fd);
238
239 if (wait_process(pid, &status, 0) < 0
240 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
241 char *e;
242 if (asprintf(&e, "pre-xfer exec returned failure (%d)\n", status) < 0)
243 out_of_memory("finish_pre_exec");
244 return e;
245 }
246 return NULL;
247}
248
249static int read_arg_from_pipe(int fd, char *buf, int limit)
250{
251 char *bp = buf, *eob = buf + limit - 1;
252
253 while (1) {
254 if (read(fd, bp, 1) != 1)
255 return -1;
256 if (*bp == '\0')
257 break;
258 if (bp < eob)
259 bp++;
260 }
261 *bp = '\0';
262
263 return bp - buf;
264}
265
266static int rsync_module(int f_in, int f_out, int i, char *addr, char *host)
267{
268 int argc = 0;
269 int maxargs;
270 char **argv;
271 char line[BIGPATHBUFLEN];
272 uid_t uid = (uid_t)-2; /* canonically "nobody" */
273 gid_t gid = (gid_t)-2;
274 char *p, *err_msg = NULL;
275 char *name = lp_name(i);
276 int use_chroot = lp_use_chroot(i);
277 int start_glob = 0;
278 int ret, pre_exec_fd = -1;
279 pid_t pre_exec_pid = 0;
280 char *request = NULL;
281
282 if (!allow_access(addr, host, lp_hosts_allow(i), lp_hosts_deny(i))) {
283 rprintf(FLOG, "rsync denied on module %s from %s (%s)\n",
284 name, host, addr);
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 }
292 return -1;
293 }
294
295 if (am_daemon && am_server) {
296 rprintf(FLOG, "rsync allowed access on module %s from %s (%s)\n",
297 name, host, addr);
298 }
299
300 if (!claim_connection(lp_lock_file(i), lp_max_connections(i))) {
301 if (errno) {
302 rsyserr(FLOG, errno, "failed to open lock file %s",
303 lp_lock_file(i));
304 io_printf(f_out, "@ERROR: failed to open lock file\n");
305 } else {
306 rprintf(FLOG, "max connections (%d) reached\n",
307 lp_max_connections(i));
308 io_printf(f_out, "@ERROR: max connections (%d) reached -- try again later\n",
309 lp_max_connections(i));
310 }
311 return -1;
312 }
313
314 auth_user = auth_server(f_in, f_out, i, host, addr, "@RSYNCD: AUTHREQD ");
315
316 if (!auth_user) {
317 io_printf(f_out, "@ERROR: auth failed on module %s\n", name);
318 return -1;
319 }
320
321 module_id = i;
322
323 if (lp_read_only(i))
324 read_only = 1;
325
326 if (lp_transfer_logging(i)) {
327 if (log_format_has(lp_log_format(i), 'i'))
328 daemon_log_format_has_i = 1;
329 if (daemon_log_format_has_i
330 || log_format_has(lp_log_format(i), 'o'))
331 daemon_log_format_has_o_or_i = 1;
332 }
333
334 am_root = (MY_UID() == 0);
335
336 if (am_root) {
337 p = lp_uid(i);
338 if (!name_to_uid(p, &uid)) {
339 if (!isdigit(*(unsigned char *)p)) {
340 rprintf(FLOG, "Invalid uid %s\n", p);
341 io_printf(f_out, "@ERROR: invalid uid %s\n", p);
342 return -1;
343 }
344 uid = atoi(p);
345 }
346
347 p = lp_gid(i);
348 if (!name_to_gid(p, &gid)) {
349 if (!isdigit(*(unsigned char *)p)) {
350 rprintf(FLOG, "Invalid gid %s\n", p);
351 io_printf(f_out, "@ERROR: invalid gid %s\n", p);
352 return -1;
353 }
354 gid = atoi(p);
355 }
356 }
357
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. */
364
365 if (use_chroot || (module_dirlen = strlen(lp_path(i))) == 1) {
366 module_dirlen = 0;
367 set_filter_dir("/", 1);
368 } else
369 set_filter_dir(lp_path(i), module_dirlen);
370
371 p = lp_filter(i);
372 parse_rule(&server_filter_list, p, MATCHFLG_WORD_SPLIT,
373 XFLG_ABS_IF_SLASH);
374
375 p = lp_include_from(i);
376 parse_filter_file(&server_filter_list, p, MATCHFLG_INCLUDE,
377 XFLG_ABS_IF_SLASH | XFLG_OLD_PREFIXES | XFLG_FATAL_ERRORS);
378
379 p = lp_include(i);
380 parse_rule(&server_filter_list, p,
381 MATCHFLG_INCLUDE | MATCHFLG_WORD_SPLIT,
382 XFLG_ABS_IF_SLASH | XFLG_OLD_PREFIXES);
383
384 p = lp_exclude_from(i);
385 parse_filter_file(&server_filter_list, p, 0,
386 XFLG_ABS_IF_SLASH | XFLG_OLD_PREFIXES | XFLG_FATAL_ERRORS);
387
388 p = lp_exclude(i);
389 parse_rule(&server_filter_list, p, MATCHFLG_WORD_SPLIT,
390 XFLG_ABS_IF_SLASH | XFLG_OLD_PREFIXES);
391
392 log_init();
393
394#ifdef HAVE_PUTENV
395 if (*lp_prexfer_exec(i) || *lp_postxfer_exec(i)) {
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);
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)) {
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;
422 if (wait_process(pid, &status, 0) < 0)
423 status = -1;
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);
432 system(lp_postxfer_exec(i));
433 _exit(status);
434 }
435 }
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) {
447 char buf[BIGPATHBUFLEN];
448 int j, len;
449 close(fds[1]);
450 set_blocking(fds[0]);
451 len = read_arg_from_pipe(fds[0], buf, BIGPATHBUFLEN);
452 if (len <= 0)
453 _exit(1);
454 if (asprintf(&p, "RSYNC_REQUEST=%s", buf) < 0)
455 out_of_memory("rsync_module");
456 putenv(p);
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]);
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 }
481 umask(0);
482 }
483#endif
484
485 if (use_chroot) {
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 */
498 if (chroot(lp_path(i))) {
499 rsyserr(FLOG, errno, "chroot %s failed",
500 lp_path(i));
501 io_printf(f_out, "@ERROR: chroot failed\n");
502 return -1;
503 }
504
505 if (!push_dir("/")) {
506 rsyserr(FLOG, errno, "chdir %s failed\n",
507 lp_path(i));
508 io_printf(f_out, "@ERROR: chdir failed\n");
509 return -1;
510 }
511
512 } else {
513 if (!push_dir(lp_path(i))) {
514 rsyserr(FLOG, errno, "chdir %s failed\n",
515 lp_path(i));
516 io_printf(f_out, "@ERROR: chdir failed\n");
517 return -1;
518 }
519 sanitize_paths = 1;
520 }
521
522 if (am_root) {
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
533 if (setgid(gid)) {
534 rsyserr(FLOG, errno, "setgid %d failed", (int)gid);
535 io_printf(f_out, "@ERROR: setgid failed\n");
536 return -1;
537 }
538#ifdef HAVE_SETGROUPS
539 /* Get rid of any supplementary groups this process
540 * might have inheristed. */
541 if (setgroups(1, &gid)) {
542 rsyserr(FLOG, errno, "setgroups failed");
543 io_printf(f_out, "@ERROR: setgroups failed\n");
544 return -1;
545 }
546#endif
547
548 if (setuid(uid)) {
549 rsyserr(FLOG, errno, "setuid %d failed", (int)uid);
550 io_printf(f_out, "@ERROR: setuid failed\n");
551 return -1;
552 }
553
554 am_root = (MY_UID() == 0);
555 }
556
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
567 io_printf(f_out, "@RSYNCD: OK\n");
568
569 maxargs = MAX_ARGS;
570 if (!(argv = new_array(char *, maxargs)))
571 out_of_memory("rsync_module");
572 argv[argc++] = "rsyncd";
573
574 while (1) {
575 if (!read_line(f_in, line, sizeof line - 1))
576 return -1;
577
578 if (!*line)
579 break;
580
581 p = line;
582
583 if (argc == maxargs) {
584 maxargs += MAX_ARGS;
585 if (!(argv = realloc_array(argv, char *, maxargs)))
586 out_of_memory("rsync_module");
587 }
588 if (!(argv[argc] = strdup(p)))
589 out_of_memory("rsync_module");
590
591 switch (start_glob) {
592 case 0:
593 argc++;
594 if (strcmp(line, ".") == 0)
595 start_glob = 1;
596 break;
597 case 1:
598 if (pre_exec_pid) {
599 err_msg = finish_pre_exec(pre_exec_pid,
600 pre_exec_fd, p,
601 argc, argv);
602 pre_exec_pid = 0;
603 }
604 request = strdup(p);
605 start_glob = 2;
606 /* FALL THROUGH */
607 default:
608 if (!err_msg)
609 glob_expand(name, &argv, &argc, &maxargs);
610 break;
611 }
612 }
613
614 if (pre_exec_pid) {
615 err_msg = finish_pre_exec(pre_exec_pid, pre_exec_fd, request,
616 argc, argv);
617 }
618
619 verbose = 0; /* future verbosity is controlled by client options */
620 ret = parse_arguments(&argc, (const char ***) &argv, 0);
621 quiet = 0; /* Don't let someone try to be tricky. */
622
623 if (filesfrom_fd == 0)
624 filesfrom_fd = f_in;
625
626 if (request) {
627 if (*auth_user) {
628 rprintf(FLOG, "rsync %s %s from %s@%s (%s)\n",
629 am_sender ? "on" : "to",
630 request, auth_user, host, addr);
631 } else {
632 rprintf(FLOG, "rsync %s %s from %s (%s)\n",
633 am_sender ? "on" : "to",
634 request, host, addr);
635 }
636 free(request);
637 }
638
639#ifndef DEBUG
640 /* don't allow the logs to be flooded too fast */
641 if (verbose > lp_max_verbosity(i))
642 verbose = lp_max_verbosity(i);
643#endif
644
645 if (protocol_version < 23
646 && (protocol_version == 22 || am_sender))
647 io_start_multiplex_out();
648 else if (!ret || err_msg) {
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);
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 }
671 io_start_multiplex_out();
672 }
673
674 if (!ret || err_msg) {
675 if (err_msg)
676 rprintf(FERROR, err_msg);
677 else
678 option_error();
679 msleep(400);
680 exit_cleanup(RERR_UNSUPPORTED);
681 }
682
683 if (lp_timeout(i) && lp_timeout(i) > io_timeout)
684 set_io_timeout(lp_timeout(i));
685
686 /* If we have some incoming/outgoing chmod changes, append them to
687 * any user-specified changes (making our changes have priority).
688 * We also get a pointer to just our changes so that a receiver
689 * process can use them separately if --perms wasn't specified. */
690 if (am_sender)
691 p = lp_outgoing_chmod(i);
692 else
693 p = lp_incoming_chmod(i);
694 if (*p && !(daemon_chmod_modes = parse_chmod(p, &chmod_modes))) {
695 rprintf(FLOG, "Invalid \"%sing chmod\" directive: %s\n",
696 am_sender ? "outgo" : "incom", p);
697 }
698
699 start_server(f_in, f_out, argc, argv);
700
701 return 0;
702}
703
704/* send a list of available modules to the client. Don't list those
705 with "list = False". */
706static void send_listing(int fd)
707{
708 int n = lp_numservices();
709 int i;
710
711 for (i = 0; i < n; i++) {
712 if (lp_list(i))
713 io_printf(fd, "%-15s\t%s\n", lp_name(i), lp_comment(i));
714 }
715
716 if (protocol_version >= 25)
717 io_printf(fd,"@RSYNCD: EXIT\n");
718}
719
720/* this is called when a connection is established to a client
721 and we want to start talking. The setup of the system is done from
722 here */
723int start_daemon(int f_in, int f_out)
724{
725 char line[1024];
726 char *motd, *addr, *host;
727 int i;
728
729 io_set_sock_fds(f_in, f_out);
730
731 /* We must load the config file before calling any function that
732 * might cause log-file output to occur. This ensures that the
733 * "log file" param gets honored for the 2 non-forked use-cases
734 * (when rsync is run by init and run by a remote shell). */
735 if (!lp_load(config_file, 0))
736 exit_cleanup(RERR_SYNTAX);
737
738 addr = client_addr(f_in);
739 host = client_name(f_in);
740 rprintf(FLOG, "connect from %s (%s)\n", host, addr);
741
742 if (!am_server) {
743 set_socket_options(f_in, "SO_KEEPALIVE");
744 if (sockopts)
745 set_socket_options(f_in, sockopts);
746 else
747 set_socket_options(f_in, lp_socket_options());
748 set_nonblocking(f_in);
749 }
750
751 io_printf(f_out, "@RSYNCD: %d\n", protocol_version);
752
753 motd = lp_motd_file();
754 if (motd && *motd) {
755 FILE *f = fopen(motd,"r");
756 while (f && !feof(f)) {
757 int len = fread(line, 1, sizeof line - 1, f);
758 if (len > 0) {
759 line[len] = 0;
760 io_printf(f_out, "%s", line);
761 }
762 }
763 if (f)
764 fclose(f);
765 io_printf(f_out, "\n");
766 }
767
768 if (!read_line(f_in, line, sizeof line - 1))
769 return -1;
770
771 if (sscanf(line,"@RSYNCD: %d", &remote_protocol) != 1) {
772 io_printf(f_out, "@ERROR: protocol startup error\n");
773 return -1;
774 }
775 if (protocol_version > remote_protocol)
776 protocol_version = remote_protocol;
777
778 line[0] = 0;
779 if (!read_line(f_in, line, sizeof line - 1))
780 return -1;
781
782 if (!*line || strcmp(line, "#list") == 0) {
783 rprintf(FLOG, "module-list request from %s (%s)\n",
784 host, addr);
785 send_listing(f_out);
786 return -1;
787 }
788
789 if (*line == '#') {
790 /* it's some sort of command that I don't understand */
791 io_printf(f_out, "@ERROR: Unknown command '%s'\n", line);
792 return -1;
793 }
794
795 if ((i = lp_number(line)) < 0) {
796 rprintf(FLOG, "unknown module '%s' tried from %s (%s)\n",
797 line, host, addr);
798 io_printf(f_out, "@ERROR: Unknown module '%s'\n", line);
799 return -1;
800 }
801
802#ifdef HAVE_SIGACTION
803 sigact.sa_flags = SA_NOCLDSTOP;
804#endif
805 SIGACTION(SIGCHLD, remember_children);
806
807 return rsync_module(f_in, f_out, i, addr, host);
808}
809
810int daemon_main(void)
811{
812 char *pid_file;
813
814 if (is_a_socket(STDIN_FILENO)) {
815 int i;
816
817 /* we are running via inetd - close off stdout and
818 * stderr so that library functions (and getopt) don't
819 * try to use them. Redirect them to /dev/null */
820 for (i = 1; i < 3; i++) {
821 close(i);
822 open("/dev/null", O_RDWR);
823 }
824
825 return start_daemon(STDIN_FILENO, STDIN_FILENO);
826 }
827
828 if (!no_detach)
829 become_daemon();
830
831 if (!lp_load(config_file, 1))
832 exit_cleanup(RERR_SYNTAX);
833
834 if (rsync_port == 0 && (rsync_port = lp_rsync_port()) == 0)
835 rsync_port = RSYNC_PORT;
836 if (bind_address == NULL && *lp_bind_address())
837 bind_address = lp_bind_address();
838
839 log_init();
840
841 rprintf(FLOG, "rsyncd version %s starting, listening on port %d\n",
842 RSYNC_VERSION, rsync_port);
843 /* TODO: If listening on a particular address, then show that
844 * address too. In fact, why not just do inet_ntop on the
845 * local address??? */
846
847 if (((pid_file = lp_pid_file()) != NULL) && (*pid_file != '\0')) {
848 char pidbuf[16];
849 int fd;
850 pid_t pid = getpid();
851 cleanup_set_pid(pid);
852 if ((fd = do_open(lp_pid_file(), O_WRONLY|O_CREAT|O_TRUNC,
853 0666 & ~orig_umask)) == -1) {
854 cleanup_set_pid(0);
855 rsyserr(FLOG, errno, "failed to create pid file %s",
856 pid_file);
857 exit_cleanup(RERR_FILEIO);
858 }
859 snprintf(pidbuf, sizeof pidbuf, "%ld\n", (long)pid);
860 write(fd, pidbuf, strlen(pidbuf));
861 close(fd);
862 }
863
864 start_accept_loop(rsync_port, start_daemon);
865 return -1;
866}