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
37 extern int rsync_port;
38 extern int kluge_around_eof;
39 extern int daemon_over_rsh;
40 extern int sanitize_paths;
41 extern int filesfrom_fd;
42 extern int remote_protocol;
43 extern int protocol_version;
44 extern int io_timeout;
45 extern int orig_umask;
47 extern int default_af_hint;
48 extern char *bind_address;
49 extern char *sockopts;
50 extern char *config_file;
51 extern char *files_from;
53 extern struct chmod_mode_struct *chmod_modes;
54 extern struct filter_list_struct server_filter_list;
58 int daemon_log_format_has_i = 0;
59 int daemon_log_format_has_o_or_i = 0;
61 struct chmod_mode_struct *daemon_chmod_modes;
63 /* Length of lp_path() string when in daemon mode & not chrooted, else 0. */
64 unsigned int module_dirlen = 0;
67 * Run a client connected to an rsyncd. The alternative to this
68 * function for remote-shell connections is do_cmd().
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.
74 * Instead of doing a transfer, the client may at this stage instead
75 * get a listing of remote modules and exit.
77 * @return -1 for error in startup, or the result of client_run().
78 * Either way, it eventually gets passed to exit_cleanup().
80 int start_socket_client(char *host, char *path, int argc, char *argv[])
83 char *p, *user = NULL;
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. */
90 "ERROR: The remote path must start with a module name not a /\n");
94 if ((p = strrchr(host, '@')) != NULL) {
100 fd = open_socket_out_wrapped(host, rsync_port, bind_address,
103 exit_cleanup(RERR_SOCKETIO);
105 set_socket_options(fd, sockopts);
107 ret = start_inband_exchange(user, path, fd, fd, argc);
109 return ret ? ret : client_run(fd, fd, -1, argc, argv);
112 int start_inband_exchange(char *user, char *path, int f_in, int f_out,
116 char *sargs[MAX_ARGS];
118 char line[BIGPATHBUFLEN];
121 if (argc == 0 && !am_sender)
126 "ERROR: The remote path must start with a module name\n");
131 user = getenv("USER");
133 user = getenv("LOGNAME");
135 io_printf(f_out, "@RSYNCD: %d\n", protocol_version);
137 if (!read_line(f_in, line, sizeof line - 1)) {
138 rprintf(FERROR, "rsync: did not see server greeting\n");
142 if (sscanf(line,"@RSYNCD: %d", &remote_protocol) != 1) {
143 /* note that read_line strips of \n or \r */
144 rprintf(FERROR, "rsync: server sent \"%s\" rather than greeting\n",
148 if (protocol_version > remote_protocol)
149 protocol_version = remote_protocol;
151 if (list_only && protocol_version >= 29)
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 */
158 server_options(sargs, &sargc);
160 sargs[sargc++] = ".";
163 sargs[sargc++] = path;
168 print_child_argv(sargs);
170 p = strchr(path,'/');
172 io_printf(f_out, "%s\n", path);
175 /* Old servers may just drop the connection here,
176 rather than sending a proper EXIT command. Yuck. */
177 kluge_around_eof = list_only && protocol_version < 25 ? 1 : 0;
180 if (!read_line(f_in, line, sizeof line - 1)) {
181 rprintf(FERROR, "rsync: didn't get server startup line\n");
185 if (strncmp(line,"@RSYNCD: AUTHREQD ",18) == 0) {
186 auth_client(f_out, user, line+18);
190 if (strcmp(line,"@RSYNCD: OK") == 0)
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. */
201 if (strncmp(line, "@ERROR", 6) == 0) {
202 rprintf(FERROR, "%s\n", line);
203 /* This is always fatal; the server will now
204 * close the socket. */
208 rprintf(FINFO, "%s\n", line);
210 kluge_around_eof = 0;
212 for (i = 0; i < sargc; i++) {
213 io_printf(f_out, "%s\n", sargs[i]);
215 io_printf(f_out, "\n");
217 if (protocol_version < 23) {
218 if (protocol_version == 22 || !am_sender)
219 io_start_multiplex_in();
225 static char *finish_pre_exec(pid_t pid, int fd, char *request,
226 int argc, char *argv[])
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);
240 if (wait_process(pid, &status, 0) < 0
241 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
243 if (asprintf(&e, "pre-xfer exec returned failure (%d)\n", status) < 0)
244 out_of_memory("finish_pre_exec");
250 static int read_arg_from_pipe(int fd, char *buf, int limit)
252 char *bp = buf, *eob = buf + limit - 1;
255 if (read(fd, bp, 1) != 1)
267 static int rsync_module(int f_in, int f_out, int i)
272 char line[BIGPATHBUFLEN];
273 uid_t uid = (uid_t)-2; /* canonically "nobody" */
274 gid_t gid = (gid_t)-2;
275 char *p, *err_msg = NULL;
276 char *addr = client_addr(f_in);
277 char *host = client_name(f_in);
278 char *name = lp_name(i);
279 int use_chroot = lp_use_chroot(i);
281 int ret, pre_exec_fd = -1;
282 pid_t pre_exec_pid = 0;
283 char *request = NULL;
285 if (!allow_access(addr, host, lp_hosts_allow(i), lp_hosts_deny(i))) {
286 rprintf(FLOG, "rsync denied on module %s from %s (%s)\n",
289 io_printf(f_out, "@ERROR: Unknown module '%s'\n", name);
292 "@ERROR: access denied to %s from %s (%s)\n",
298 if (am_daemon && am_server) {
299 rprintf(FLOG, "rsync allowed access on module %s from %s (%s)\n",
303 if (!claim_connection(lp_lock_file(i), lp_max_connections(i))) {
305 rsyserr(FLOG, errno, "failed to open lock file %s",
307 io_printf(f_out, "@ERROR: failed to open lock file\n");
309 rprintf(FLOG, "max connections (%d) reached\n",
310 lp_max_connections(i));
311 io_printf(f_out, "@ERROR: max connections (%d) reached -- try again later\n",
312 lp_max_connections(i));
317 auth_user = auth_server(f_in, f_out, i, host, addr, "@RSYNCD: AUTHREQD ");
320 io_printf(f_out, "@ERROR: auth failed on module %s\n", name);
329 if (lp_transfer_logging(i)) {
330 if (log_format_has(lp_log_format(i), 'i'))
331 daemon_log_format_has_i = 1;
332 if (daemon_log_format_has_i
333 || log_format_has(lp_log_format(i), 'o'))
334 daemon_log_format_has_o_or_i = 1;
337 am_root = (MY_UID() == 0);
341 if (!name_to_uid(p, &uid)) {
342 if (!isdigit(*(unsigned char *)p)) {
343 rprintf(FLOG, "Invalid uid %s\n", p);
344 io_printf(f_out, "@ERROR: invalid uid %s\n", p);
351 if (!name_to_gid(p, &gid)) {
352 if (!isdigit(*(unsigned char *)p)) {
353 rprintf(FLOG, "Invalid gid %s\n", p);
354 io_printf(f_out, "@ERROR: invalid gid %s\n", p);
361 /* TODO: If we're not root, but the configuration requests
362 * that we change to some uid other than the current one, then
365 /* TODO: Perhaps take a list of gids, and make them into the
366 * supplementary groups. */
368 if (use_chroot || (module_dirlen = strlen(lp_path(i))) == 1) {
370 set_filter_dir("/", 1);
372 set_filter_dir(lp_path(i), module_dirlen);
375 parse_rule(&server_filter_list, p, MATCHFLG_WORD_SPLIT,
378 p = lp_include_from(i);
379 parse_filter_file(&server_filter_list, p, MATCHFLG_INCLUDE,
380 XFLG_ABS_IF_SLASH | XFLG_OLD_PREFIXES | XFLG_FATAL_ERRORS);
383 parse_rule(&server_filter_list, p,
384 MATCHFLG_INCLUDE | MATCHFLG_WORD_SPLIT,
385 XFLG_ABS_IF_SLASH | XFLG_OLD_PREFIXES);
387 p = lp_exclude_from(i);
388 parse_filter_file(&server_filter_list, p, 0,
389 XFLG_ABS_IF_SLASH | XFLG_OLD_PREFIXES | XFLG_FATAL_ERRORS);
392 parse_rule(&server_filter_list, p, MATCHFLG_WORD_SPLIT,
393 XFLG_ABS_IF_SLASH | XFLG_OLD_PREFIXES);
398 if (*lp_prexfer_exec(i) || *lp_postxfer_exec(i)) {
399 char *modname, *modpath, *hostaddr, *hostname, *username;
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");
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)) {
419 rsyserr(FLOG, errno, "fork failed");
420 io_printf(f_out, "@ERROR: fork failed\n");
425 if (wait_process(pid, &status, 0) < 0)
427 if (asprintf(&ret1, "RSYNC_RAW_STATUS=%d", status) > 0)
429 if (WIFEXITED(status))
430 status = WEXITSTATUS(status);
433 if (asprintf(&ret2, "RSYNC_EXIT_STATUS=%d", status) > 0)
435 system(lp_postxfer_exec(i));
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)) {
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");
449 if (pre_exec_pid == 0) {
450 char buf[BIGPATHBUFLEN];
453 set_blocking(fds[0]);
454 len = read_arg_from_pipe(fds[0], buf, BIGPATHBUFLEN);
457 if (asprintf(&p, "RSYNC_REQUEST=%s", buf) < 0)
458 out_of_memory("rsync_module");
461 len = read_arg_from_pipe(fds[0], buf,
468 if (asprintf(&p, "RSYNC_ARG%d=%s", j, buf) < 0)
469 out_of_memory("rsync_module");
474 close(STDOUT_FILENO);
475 status = system(lp_prexfer_exec(i));
476 if (!WIFEXITED(status))
478 _exit(WEXITSTATUS(status));
481 set_blocking(fds[1]);
482 pre_exec_fd = fds[1];
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.
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.
501 if (chroot(lp_path(i))) {
502 rsyserr(FLOG, errno, "chroot %s failed",
504 io_printf(f_out, "@ERROR: chroot failed\n");
508 if (!push_dir("/")) {
509 rsyserr(FLOG, errno, "chdir %s failed\n",
511 io_printf(f_out, "@ERROR: chdir failed\n");
516 if (!push_dir(lp_path(i))) {
517 rsyserr(FLOG, errno, "chdir %s failed\n",
519 io_printf(f_out, "@ERROR: chdir failed\n");
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. */
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. */
537 rsyserr(FLOG, errno, "setgid %d failed", (int)gid);
538 io_printf(f_out, "@ERROR: setgid failed\n");
541 #ifdef HAVE_SETGROUPS
542 /* Get rid of any supplementary groups this process
543 * might have inheristed. */
544 if (setgroups(1, &gid)) {
545 rsyserr(FLOG, errno, "setgroups failed");
546 io_printf(f_out, "@ERROR: setgroups failed\n");
552 rsyserr(FLOG, errno, "setuid %d failed", (int)uid);
553 io_printf(f_out, "@ERROR: setuid failed\n");
557 am_root = (MY_UID() == 0);
560 if (lp_temp_dir(i) && *lp_temp_dir(i)) {
561 tmpdir = lp_temp_dir(i);
562 if (strlen(tmpdir) >= MAXPATHLEN - 10) {
564 "the 'temp dir' value for %s is WAY too long -- ignoring.\n",
570 io_printf(f_out, "@RSYNCD: OK\n");
573 if (!(argv = new_array(char *, maxargs)))
574 out_of_memory("rsync_module");
575 argv[argc++] = "rsyncd";
578 if (!read_line(f_in, line, sizeof line - 1))
586 if (argc == maxargs) {
588 if (!(argv = realloc_array(argv, char *, maxargs)))
589 out_of_memory("rsync_module");
591 if (!(argv[argc] = strdup(p)))
592 out_of_memory("rsync_module");
594 switch (start_glob) {
597 if (strcmp(line, ".") == 0)
602 err_msg = finish_pre_exec(pre_exec_pid,
612 glob_expand(name, &argv, &argc, &maxargs);
618 err_msg = finish_pre_exec(pre_exec_pid, pre_exec_fd, request,
622 verbose = 0; /* future verbosity is controlled by client options */
623 ret = parse_arguments(&argc, (const char ***) &argv, 0);
624 quiet = 0; /* Don't let someone try to be tricky. */
626 if (filesfrom_fd == 0)
631 rprintf(FLOG, "rsync %s %s from %s@%s (%s)\n",
632 am_sender ? "on" : "to",
633 request, auth_user, host, addr);
635 rprintf(FLOG, "rsync %s %s from %s (%s)\n",
636 am_sender ? "on" : "to",
637 request, host, addr);
643 /* don't allow the logs to be flooded too fast */
644 if (verbose > lp_max_verbosity(i))
645 verbose = lp_max_verbosity(i);
648 if (protocol_version < 23
649 && (protocol_version == 22 || am_sender))
650 io_start_multiplex_out();
651 else if (!ret || err_msg) {
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);
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. */
664 for (i = 0; i < argc; i++) {
665 if (strncmp(argv[i], "--files-from", 12) == 0) {
672 write_byte(f_out, 0);
674 io_start_multiplex_out();
677 if (!ret || err_msg) {
679 rprintf(FERROR, err_msg);
683 exit_cleanup(RERR_UNSUPPORTED);
686 if (lp_timeout(i) && lp_timeout(i) > io_timeout)
687 set_io_timeout(lp_timeout(i));
690 p = lp_outgoing_chmod(i);
692 p = lp_incoming_chmod(i);
693 if (*p && !(daemon_chmod_modes = parse_chmod(p, &chmod_modes))) {
694 rprintf(FLOG, "Invalid \"%sing chmod\" directive: %s\n",
695 am_sender ? "outgo" : "incom", p);
698 start_server(f_in, f_out, argc, argv);
703 /* send a list of available modules to the client. Don't list those
704 with "list = False". */
705 static void send_listing(int fd)
707 int n = lp_numservices();
710 for (i = 0; i < n; i++) {
712 io_printf(fd, "%-15s\t%s\n", lp_name(i), lp_comment(i));
715 if (protocol_version >= 25)
716 io_printf(fd,"@RSYNCD: EXIT\n");
719 /* this is called when a connection is established to a client
720 and we want to start talking. The setup of the system is done from
722 int start_daemon(int f_in, int f_out)
728 io_set_sock_fds(f_in, f_out);
730 if (!lp_load(config_file, 0))
731 exit_cleanup(RERR_SYNTAX);
736 set_socket_options(f_in, "SO_KEEPALIVE");
738 set_socket_options(f_in, sockopts);
740 set_socket_options(f_in, lp_socket_options());
741 set_nonblocking(f_in);
744 io_printf(f_out, "@RSYNCD: %d\n", protocol_version);
746 motd = lp_motd_file();
748 FILE *f = fopen(motd,"r");
749 while (f && !feof(f)) {
750 int len = fread(line, 1, sizeof line - 1, f);
753 io_printf(f_out, "%s", line);
758 io_printf(f_out, "\n");
761 if (!read_line(f_in, line, sizeof line - 1))
764 if (sscanf(line,"@RSYNCD: %d", &remote_protocol) != 1) {
765 io_printf(f_out, "@ERROR: protocol startup error\n");
768 if (protocol_version > remote_protocol)
769 protocol_version = remote_protocol;
772 if (!read_line(f_in, line, sizeof line - 1))
775 if (!*line || strcmp(line, "#list") == 0) {
776 char *addr = client_addr(f_in);
777 char *host = client_name(f_in);
778 rprintf(FLOG, "module-list request from %s (%s)\n",
785 /* it's some sort of command that I don't understand */
786 io_printf(f_out, "@ERROR: Unknown command '%s'\n", line);
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",
795 io_printf(f_out, "@ERROR: Unknown module '%s'\n", line);
799 return rsync_module(f_in, f_out, i);
802 int daemon_main(void)
806 if (is_a_socket(STDIN_FILENO)) {
809 /* we are running via inetd - close off stdout and
810 * stderr so that library functions (and getopt) don't
811 * try to use them. Redirect them to /dev/null */
812 for (i = 1; i < 3; i++) {
814 open("/dev/null", O_RDWR);
817 return start_daemon(STDIN_FILENO, STDIN_FILENO);
823 if (!lp_load(config_file, 1))
824 exit_cleanup(RERR_SYNTAX);
826 if (rsync_port == 0 && (rsync_port = lp_rsync_port()) == 0)
827 rsync_port = RSYNC_PORT;
828 if (bind_address == NULL && *lp_bind_address())
829 bind_address = lp_bind_address();
833 rprintf(FLOG, "rsyncd version %s starting, listening on port %d\n",
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??? */
839 if (((pid_file = lp_pid_file()) != NULL) && (*pid_file != '\0')) {
842 pid_t pid = getpid();
843 cleanup_set_pid(pid);
844 if ((fd = do_open(lp_pid_file(), O_WRONLY|O_CREAT|O_TRUNC,
845 0666 & ~orig_umask)) == -1) {
847 rsyserr(FLOG, errno, "failed to create pid file %s",
849 exit_cleanup(RERR_FILEIO);
851 snprintf(pidbuf, sizeof pidbuf, "%ld\n", (long)pid);
852 write(fd, pidbuf, strlen(pidbuf));
856 start_accept_loop(rsync_port, start_daemon);