1 /* -*- c-file-style: "linux"; -*-
3 * Copyright (C) 1998-2001 by Andrew Tridgell <tridge@samba.org>
4 * Copyright (C) 2001-2002 by Martin Pool <mbp@samba.org>
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.
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.
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.
24 * The socket based protocol for setting up a connection with
36 extern int rsync_port;
37 extern int kluge_around_eof;
38 extern int daemon_over_rsh;
39 extern int sanitize_paths;
40 extern int filesfrom_fd;
41 extern int remote_protocol;
42 extern int protocol_version;
43 extern int io_timeout;
44 extern int orig_umask;
46 extern int default_af_hint;
47 extern char *bind_address;
48 extern char *sockopts;
49 extern struct filter_list_struct server_filter_list;
50 extern char *config_file;
51 extern char *files_from;
56 int daemon_log_format_has_i = 0;
57 int daemon_log_format_has_o_or_i = 0;
60 /* Length of lp_path() string when in daemon mode & not chrooted, else 0. */
61 unsigned int module_dirlen = 0;
64 * Run a client connected to an rsyncd. The alternative to this
65 * function for remote-shell connections is do_cmd().
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.
71 * Instead of doing a transfer, the client may at this stage instead
72 * get a listing of remote modules and exit.
74 * @return -1 for error in startup, or the result of client_run().
75 * Either way, it eventually gets passed to exit_cleanup().
77 int start_socket_client(char *host, char *path, int argc, char *argv[])
80 char *p, *user = NULL;
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. */
87 "ERROR: The remote path must start with a module name not a /\n");
91 if ((p = strrchr(host, '@')) != NULL) {
97 fd = open_socket_out_wrapped(host, rsync_port, bind_address,
100 exit_cleanup(RERR_SOCKETIO);
102 set_socket_options(fd, sockopts);
104 ret = start_inband_exchange(user, path, fd, fd, argc);
106 return ret ? ret : client_run(fd, fd, -1, argc, argv);
109 int start_inband_exchange(char *user, char *path, int f_in, int f_out,
113 char *sargs[MAX_ARGS];
115 char line[BIGPATHBUFLEN];
118 if (argc == 0 && !am_sender)
123 "ERROR: The remote path must start with a module name\n");
128 user = getenv("USER");
130 user = getenv("LOGNAME");
132 io_printf(f_out, "@RSYNCD: %d\n", protocol_version);
134 if (!read_line(f_in, line, sizeof line - 1)) {
135 rprintf(FERROR, "rsync: did not see server greeting\n");
139 if (sscanf(line,"@RSYNCD: %d", &remote_protocol) != 1) {
140 /* note that read_line strips of \n or \r */
141 rprintf(FERROR, "rsync: server sent \"%s\" rather than greeting\n",
145 if (protocol_version > remote_protocol)
146 protocol_version = remote_protocol;
148 if (list_only && protocol_version >= 29)
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 */
155 server_options(sargs, &sargc);
157 sargs[sargc++] = ".";
160 sargs[sargc++] = path;
165 print_child_argv(sargs);
167 p = strchr(path,'/');
169 io_printf(f_out, "%s\n", path);
172 /* Old servers may just drop the connection here,
173 rather than sending a proper EXIT command. Yuck. */
174 kluge_around_eof = list_only && protocol_version < 25 ? 1 : 0;
177 if (!read_line(f_in, line, sizeof line - 1)) {
178 rprintf(FERROR, "rsync: didn't get server startup line\n");
182 if (strncmp(line,"@RSYNCD: AUTHREQD ",18) == 0) {
183 auth_client(f_out, user, line+18);
187 if (strcmp(line,"@RSYNCD: OK") == 0)
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. */
198 if (strncmp(line, "@ERROR", 6) == 0) {
199 rprintf(FERROR, "%s\n", line);
200 /* This is always fatal; the server will now
201 * close the socket. */
205 rprintf(FINFO, "%s\n", line);
207 kluge_around_eof = 0;
209 for (i = 0; i < sargc; i++) {
210 io_printf(f_out, "%s\n", sargs[i]);
212 io_printf(f_out, "\n");
214 if (protocol_version < 23) {
215 if (protocol_version == 22 || !am_sender)
216 io_start_multiplex_in();
222 static char *finish_pre_exec(pid_t pid, int fd, char *request,
223 int argc, char *argv[])
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);
237 if (wait_process(pid, &status, 0) < 0
238 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
240 if (asprintf(&e, "pre-xfer exec returned failure (%d)\n", status) < 0)
241 out_of_memory("finish_pre_exec");
247 static int read_arg_from_pipe(int fd, char *buf, int limit)
249 char *bp = buf, *eob = buf + limit - 1;
252 if (read(fd, bp, 1) != 1)
264 static int rsync_module(int f_in, int f_out, int i)
269 char line[BIGPATHBUFLEN];
270 uid_t uid = (uid_t)-2; /* canonically "nobody" */
271 gid_t gid = (gid_t)-2;
272 char *p, *err_msg = NULL;
273 char *addr = client_addr(f_in);
274 char *host = client_name(f_in);
275 char *name = lp_name(i);
276 int use_chroot = lp_use_chroot(i);
278 int ret, pre_exec_fd = -1;
279 pid_t pre_exec_pid = 0;
280 char *request = NULL;
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",
286 io_printf(f_out, "@ERROR: Unknown module '%s'\n", name);
289 "@ERROR: access denied to %s from %s (%s)\n",
295 if (am_daemon && am_server) {
296 rprintf(FLOG, "rsync allowed access on module %s from %s (%s)\n",
300 if (!claim_connection(lp_lock_file(i), lp_max_connections(i))) {
302 rsyserr(FLOG, errno, "failed to open lock file %s",
304 io_printf(f_out, "@ERROR: failed to open lock file\n");
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));
314 auth_user = auth_server(f_in, f_out, i, host, addr, "@RSYNCD: AUTHREQD ");
317 io_printf(f_out, "@ERROR: auth failed on module %s\n", name);
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;
334 am_root = (MY_UID() == 0);
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);
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);
358 /* TODO: If we're not root, but the configuration requests
359 * that we change to some uid other than the current one, then
362 /* TODO: Perhaps take a list of gids, and make them into the
363 * supplementary groups. */
365 if (use_chroot || (module_dirlen = strlen(lp_path(i))) == 1) {
367 set_filter_dir("/", 1);
369 set_filter_dir(lp_path(i), module_dirlen);
372 parse_rule(&server_filter_list, p, MATCHFLG_WORD_SPLIT,
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);
380 parse_rule(&server_filter_list, p,
381 MATCHFLG_INCLUDE | MATCHFLG_WORD_SPLIT,
382 XFLG_ABS_IF_SLASH | XFLG_OLD_PREFIXES);
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);
389 parse_rule(&server_filter_list, p, MATCHFLG_WORD_SPLIT,
390 XFLG_ABS_IF_SLASH | XFLG_OLD_PREFIXES);
395 if (*lp_prexfer_exec(i) || *lp_postxfer_exec(i)) {
396 char *modname, *modpath, *hostaddr, *hostname, *username;
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");
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)) {
416 rsyserr(FLOG, errno, "fork failed");
417 io_printf(f_out, "@ERROR: fork failed\n");
422 if (wait_process(pid, &status, 0) < 0)
424 if (asprintf(&ret1, "RSYNC_RAW_STATUS=%d", status) > 0)
426 if (WIFEXITED(status))
427 status = WEXITSTATUS(status);
430 if (asprintf(&ret2, "RSYNC_EXIT_STATUS=%d", status) > 0)
432 system(lp_postxfer_exec(i));
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)) {
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");
446 if (pre_exec_pid == 0) {
447 char buf[BIGPATHBUFLEN];
450 set_blocking(fds[0]);
451 len = read_arg_from_pipe(fds[0], buf, BIGPATHBUFLEN);
454 if (asprintf(&p, "RSYNC_REQUEST=%s", buf) < 0)
455 out_of_memory("rsync_module");
458 len = read_arg_from_pipe(fds[0], buf,
465 if (asprintf(&p, "RSYNC_ARG%d=%s", j, buf) < 0)
466 out_of_memory("rsync_module");
471 close(STDOUT_FILENO);
472 status = system(lp_prexfer_exec(i));
473 if (!WIFEXITED(status))
475 _exit(WEXITSTATUS(status));
478 set_blocking(fds[1]);
479 pre_exec_fd = fds[1];
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.
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.
498 if (chroot(lp_path(i))) {
499 rsyserr(FLOG, errno, "chroot %s failed",
501 io_printf(f_out, "@ERROR: chroot failed\n");
505 if (!push_dir("/")) {
506 rsyserr(FLOG, errno, "chdir %s failed\n",
508 io_printf(f_out, "@ERROR: chdir failed\n");
513 if (!push_dir(lp_path(i))) {
514 rsyserr(FLOG, errno, "chdir %s failed\n",
516 io_printf(f_out, "@ERROR: chdir failed\n");
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. */
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. */
534 rsyserr(FLOG, errno, "setgid %d failed", (int)gid);
535 io_printf(f_out, "@ERROR: setgid failed\n");
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");
549 rsyserr(FLOG, errno, "setuid %d failed", (int)uid);
550 io_printf(f_out, "@ERROR: setuid failed\n");
554 am_root = (MY_UID() == 0);
557 if (lp_temp_dir(i) && *lp_temp_dir(i)) {
558 tmpdir = lp_temp_dir(i);
559 if (strlen(tmpdir) >= MAXPATHLEN - 10) {
561 "the 'temp dir' value for %s is WAY too long -- ignoring.\n",
567 io_printf(f_out, "@RSYNCD: OK\n");
570 if (!(argv = new_array(char *, maxargs)))
571 out_of_memory("rsync_module");
572 argv[argc++] = "rsyncd";
575 if (!read_line(f_in, line, sizeof line - 1))
583 if (argc == maxargs) {
585 if (!(argv = realloc_array(argv, char *, maxargs)))
586 out_of_memory("rsync_module");
588 if (!(argv[argc] = strdup(p)))
589 out_of_memory("rsync_module");
591 switch (start_glob) {
594 if (strcmp(line, ".") == 0)
599 err_msg = finish_pre_exec(pre_exec_pid,
609 glob_expand(name, &argv, &argc, &maxargs);
615 err_msg = finish_pre_exec(pre_exec_pid, pre_exec_fd, request,
619 verbose = 0; /* future verbosity is controlled by client options */
620 ret = parse_arguments(&argc, (const char ***) &argv, 0);
622 if (filesfrom_fd == 0)
627 rprintf(FLOG, "rsync %s %s from %s@%s (%s)\n",
628 am_sender ? "on" : "to",
629 request, auth_user, host, addr);
631 rprintf(FLOG, "rsync %s %s from %s (%s)\n",
632 am_sender ? "on" : "to",
633 request, host, addr);
639 /* don't allow the logs to be flooded too fast */
640 if (verbose > lp_max_verbosity(i))
641 verbose = lp_max_verbosity(i);
644 if (protocol_version < 23
645 && (protocol_version == 22 || am_sender))
646 io_start_multiplex_out();
647 else if (!ret || err_msg) {
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);
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. */
660 for (i = 0; i < argc; i++) {
661 if (strncmp(argv[i], "--files-from", 12) == 0) {
668 write_byte(f_out, 0);
670 io_start_multiplex_out();
673 if (!ret || err_msg) {
675 rprintf(FERROR, err_msg);
679 exit_cleanup(RERR_UNSUPPORTED);
682 if (lp_timeout(i) && lp_timeout(i) > io_timeout)
683 set_io_timeout(lp_timeout(i));
685 start_server(f_in, f_out, argc, argv);
690 /* send a list of available modules to the client. Don't list those
691 with "list = False". */
692 static void send_listing(int fd)
694 int n = lp_numservices();
697 for (i = 0; i < n; i++) {
699 io_printf(fd, "%-15s\t%s\n", lp_name(i), lp_comment(i));
702 if (protocol_version >= 25)
703 io_printf(fd,"@RSYNCD: EXIT\n");
706 /* this is called when a connection is established to a client
707 and we want to start talking. The setup of the system is done from
709 int start_daemon(int f_in, int f_out)
715 io_set_sock_fds(f_in, f_out);
717 if (!lp_load(config_file, 0))
718 exit_cleanup(RERR_SYNTAX);
723 set_socket_options(f_in, "SO_KEEPALIVE");
725 set_socket_options(f_in, sockopts);
727 set_socket_options(f_in, lp_socket_options());
728 set_nonblocking(f_in);
731 io_printf(f_out, "@RSYNCD: %d\n", protocol_version);
733 motd = lp_motd_file();
735 FILE *f = fopen(motd,"r");
736 while (f && !feof(f)) {
737 int len = fread(line, 1, sizeof line - 1, f);
740 io_printf(f_out, "%s", line);
745 io_printf(f_out, "\n");
748 if (!read_line(f_in, line, sizeof line - 1))
751 if (sscanf(line,"@RSYNCD: %d", &remote_protocol) != 1) {
752 io_printf(f_out, "@ERROR: protocol startup error\n");
755 if (protocol_version > remote_protocol)
756 protocol_version = remote_protocol;
759 if (!read_line(f_in, line, sizeof line - 1))
762 if (!*line || strcmp(line, "#list") == 0) {
768 /* it's some sort of command that I don't understand */
769 io_printf(f_out, "@ERROR: Unknown command '%s'\n", line);
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",
778 io_printf(f_out, "@ERROR: Unknown module '%s'\n", line);
782 return rsync_module(f_in, f_out, i);
785 int daemon_main(void)
789 if (is_a_socket(STDIN_FILENO)) {
792 /* we are running via inetd - close off stdout and
793 * stderr so that library functions (and getopt) don't
794 * try to use them. Redirect them to /dev/null */
795 for (i = 1; i < 3; i++) {
797 open("/dev/null", O_RDWR);
800 return start_daemon(STDIN_FILENO, STDIN_FILENO);
806 if (!lp_load(config_file, 1))
807 exit_cleanup(RERR_SYNTAX);
809 if (rsync_port == 0 && (rsync_port = lp_rsync_port()) == 0)
810 rsync_port = RSYNC_PORT;
811 if (bind_address == NULL && *lp_bind_address())
812 bind_address = lp_bind_address();
816 rprintf(FLOG, "rsyncd version %s starting, listening on port %d\n",
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??? */
822 if (((pid_file = lp_pid_file()) != NULL) && (*pid_file != '\0')) {
825 pid_t pid = getpid();
826 cleanup_set_pid(pid);
827 if ((fd = do_open(lp_pid_file(), O_WRONLY|O_CREAT|O_TRUNC,
828 0666 & ~orig_umask)) == -1) {
830 rsyserr(FLOG, errno, "failed to create pid file %s",
832 exit_cleanup(RERR_FILEIO);
834 snprintf(pidbuf, sizeof pidbuf, "%ld\n", (long)pid);
835 write(fd, pidbuf, strlen(pidbuf));
839 start_accept_loop(rsync_port, start_daemon);