+ if (verbose > 2) {
+ rprintf(FINFO, "server_recv(%d) starting pid=%ld\n",
+ argc, (long)getpid());
+ }
+
+ if (am_daemon && lp_read_only(module_id)) {
+ rprintf(FERROR,"ERROR: module is read only\n");
+ exit_cleanup(RERR_SYNTAX);
+ return;
+ }
+
+ if (argc > 0) {
+ char *dir = argv[0];
+ argc--;
+ argv++;
+ if (!am_daemon && !push_dir(dir, 0)) {
+ rsyserr(FERROR, errno, "push_dir#4 %s failed",
+ full_fname(dir));
+ exit_cleanup(RERR_FILESELECT);
+ }
+ }
+
+ if (protocol_version >= 30)
+ io_start_multiplex_in();
+ else
+ io_start_buffering_in(f_in);
+ recv_filter_list(f_in);
+
+ if (filesfrom_fd >= 0) {
+ /* We need to send the files-from names to the sender at the
+ * same time that we receive the file-list from them, so we
+ * need the IO routines to automatically write out the names
+ * onto our f_out socket as we read the file-list. This
+ * avoids both deadlock and extra delays/buffers. */
+ io_set_filesfrom_fds(filesfrom_fd, f_out);
+ filesfrom_fd = -1;
+ }
+
+ flist = recv_file_list(f_in);
+ if (!flist) {
+ rprintf(FERROR,"server_recv: recv_file_list error\n");
+ exit_cleanup(RERR_FILESELECT);
+ }
+ if (inc_recurse && file_total == 1)
+ recv_additional_file_list(f_in);
+ verbose = save_verbose;
+
+ if (argc > 0)
+ local_name = get_local_name(flist,argv[0]);
+
+ /* Now that we know what our destination directory turned out to be,
+ * we can sanitize the --link-/copy-/compare-dest args correctly. */
+ if (sanitize_paths) {
+ char **dir_p;
+ for (dir_p = basis_dir; *dir_p; dir_p++)
+ *dir_p = sanitize_path(NULL, *dir_p, NULL, curr_dir_depth);
+ if (partial_dir)
+ partial_dir = sanitize_path(NULL, partial_dir, NULL, curr_dir_depth);
+ }
+ check_alt_basis_dirs();
+
+ if (server_filter_list.head) {
+ char **dir_p;
+ struct filter_list_struct *elp = &server_filter_list;
+
+ for (dir_p = basis_dir; *dir_p; dir_p++) {
+ char *dir = *dir_p;
+ if (*dir == '/')
+ dir += module_dirlen;
+ if (check_filter(elp, dir, 1) < 0)
+ goto options_rejected;
+ }
+ if (partial_dir && *partial_dir == '/'
+ && check_filter(elp, partial_dir + module_dirlen, 1) < 0) {
+ options_rejected:
+ rprintf(FERROR,
+ "Your options have been rejected by the server.\n");
+ exit_cleanup(RERR_SYNTAX);
+ }
+ }
+
+ exit_code = do_recv(f_in, f_out, local_name);
+ exit_cleanup(exit_code);
+}
+
+
+int child_main(int argc, char *argv[])
+{
+ start_server(STDIN_FILENO, STDOUT_FILENO, argc, argv);
+ return 0;
+}
+
+
+void start_server(int f_in, int f_out, int argc, char *argv[])
+{
+ set_nonblocking(f_in);
+ set_nonblocking(f_out);
+
+ io_set_sock_fds(f_in, f_out);
+ setup_protocol(f_out, f_in);
+
+ if (protocol_version >= 23)
+ io_start_multiplex_out();
+
+ if (am_sender) {
+ keep_dirlinks = 0; /* Must be disabled on the sender. */
+ if (need_messages_from_generator)
+ io_start_multiplex_in();
+ recv_filter_list(f_in);
+ do_server_sender(f_in, f_out, argc, argv);
+ } else
+ do_server_recv(f_in, f_out, argc, argv);
+ exit_cleanup(0);
+}
+
+
+/*
+ * This is called once the connection has been negotiated. It is used
+ * for rsyncd, remote-shell, and local connections.
+ */
+int client_run(int f_in, int f_out, pid_t pid, int argc, char *argv[])
+{
+ struct file_list *flist = NULL;
+ int exit_code = 0, exit_code2 = 0;
+ char *local_name = NULL;
+
+ cleanup_child_pid = pid;
+ if (!read_batch) {
+ set_nonblocking(f_in);
+ set_nonblocking(f_out);
+ }
+
+ io_set_sock_fds(f_in, f_out);
+ setup_protocol(f_out,f_in);
+
+ /* We set our stderr file handle to blocking because ssh might have
+ * set it to non-blocking. This can be particularly troublesome if
+ * stderr is a clone of stdout, because ssh would have set our stdout
+ * to non-blocking at the same time (which can easily cause us to lose
+ * output from our print statements). This kluge shouldn't cause ssh
+ * any problems for how we use it. Note also that we delayed setting
+ * this until after the above protocol setup so that we know for sure
+ * that ssh is done twiddling its file descriptors. */
+ set_blocking(STDERR_FILENO);
+
+ if (am_sender) {
+ keep_dirlinks = 0; /* Must be disabled on the sender. */
+ if (protocol_version >= 30)
+ io_start_multiplex_out();
+ else
+ io_start_buffering_out(f_out);
+ if (!filesfrom_host)
+ set_msg_fd_in(f_in);
+ send_filter_list(f_out);
+ if (filesfrom_host)
+ filesfrom_fd = f_in;
+
+ if (write_batch && !am_server)
+ start_write_batch(f_out);
+ flist = send_file_list(f_out, argc, argv);
+ set_msg_fd_in(-1);
+ if (verbose > 3)
+ rprintf(FINFO,"file list sent\n");
+
+ if (protocol_version >= 23)
+ io_start_multiplex_in();
+
+ io_flush(NORMAL_FLUSH);
+ send_files(f_in, f_out);
+ io_flush(FULL_FLUSH);
+ handle_stats(-1);
+ if (protocol_version >= 24)
+ read_final_goodbye(f_in);
+ if (pid != -1) {
+ if (verbose > 3)
+ rprintf(FINFO,"client_run waiting on %d\n", (int) pid);
+ io_flush(FULL_FLUSH);
+ wait_process_with_flush(pid, &exit_code);
+ }
+ output_summary();
+ io_flush(FULL_FLUSH);
+ exit_cleanup(exit_code);
+ }
+
+ if (!read_batch) {
+ if (protocol_version >= 23)
+ io_start_multiplex_in();
+ if (need_messages_from_generator)
+ io_start_multiplex_out();
+ }
+
+ if (argc == 0) {
+ list_only |= 1;
+ xfer_dirs |= 1;
+ }
+
+ send_filter_list(read_batch ? -1 : f_out);
+
+ if (filesfrom_fd >= 0) {
+ io_set_filesfrom_fds(filesfrom_fd, f_out);
+ filesfrom_fd = -1;
+ }
+
+ if (write_batch && !am_server)
+ start_write_batch(f_in);
+ flist = recv_file_list(f_in);
+ if (inc_recurse && file_total == 1)
+ recv_additional_file_list(f_in);
+
+ if (flist && flist->used > 0) {
+ local_name = get_local_name(flist, argv[0]);
+
+ check_alt_basis_dirs();
+
+ exit_code2 = do_recv(f_in, f_out, local_name);
+ } else {
+ handle_stats(-1);
+ output_summary();
+ }
+
+ if (pid != -1) {
+ if (verbose > 3)
+ rprintf(FINFO,"client_run2 waiting on %d\n", (int) pid);
+ io_flush(FULL_FLUSH);
+ wait_process_with_flush(pid, &exit_code);
+ }
+
+ return MAX(exit_code, exit_code2);
+}
+
+static int copy_argv(char *argv[])
+{
+ int i;
+
+ for (i = 0; argv[i]; i++) {
+ if (!(argv[i] = strdup(argv[i]))) {
+ rprintf (FERROR, "out of memory at %s(%d)\n",
+ __FILE__, __LINE__);
+ return RERR_MALLOC;
+ }
+ }
+
+ return 0;
+}
+
+
+/**
+ * Start a client for either type of remote connection. Work out
+ * whether the arguments request a remote shell or rsyncd connection,
+ * and call the appropriate connection function, then run_client.
+ *
+ * Calls either start_socket_client (for sockets) or do_cmd and
+ * client_run (for ssh).
+ **/
+static int start_client(int argc, char *argv[])
+{
+ char *p, *shell_machine = NULL, *shell_user = NULL;
+ char **remote_argv;
+ int remote_argc;
+ int f_in, f_out;
+ int ret;
+ pid_t pid;
+
+ /* Don't clobber argv[] so that ps(1) can still show the right
+ * command line. */
+ if ((ret = copy_argv(argv)) != 0)
+ return ret;
+
+ if (!read_batch) { /* for read_batch, NO source is specified */
+ char *path = check_for_hostspec(argv[0], &shell_machine, &rsync_port);
+ if (path) { /* source is remote */
+ char *dummy_host;
+ int dummy_port = 0;
+ *argv = path;
+ remote_argv = argv;
+ remote_argc = argc;
+ argv += argc - 1;
+ if (argc == 1 || **argv == ':')
+ argc = 0; /* no dest arg */
+ else if (check_for_hostspec(*argv, &dummy_host, &dummy_port)) {
+ rprintf(FERROR,
+ "The source and destination cannot both be remote.\n");
+ exit_cleanup(RERR_SYNTAX);
+ } else {
+ remote_argc--; /* don't count dest */
+ argc = 1;
+ }
+ if (filesfrom_host && *filesfrom_host
+ && strcmp(filesfrom_host, shell_machine) != 0) {
+ rprintf(FERROR,
+ "--files-from hostname is not the same as the transfer hostname\n");
+ exit_cleanup(RERR_SYNTAX);
+ }
+ am_sender = 0;
+ if (rsync_port)
+ daemon_over_rsh = shell_cmd ? 1 : -1;
+ } else { /* source is local, check dest arg */
+ am_sender = 1;
+
+ if (argc > 1) {
+ p = argv[--argc];
+ remote_argv = argv + argc;
+ } else {
+ static char *dotarg[1] = { "." };
+ p = dotarg[0];
+ remote_argv = dotarg;
+ list_only |= 1;
+ xfer_dirs |= 1;
+ }
+ remote_argc = 1;
+
+ path = check_for_hostspec(p, &shell_machine, &rsync_port);
+ if (path && filesfrom_host && *filesfrom_host
+ && strcmp(filesfrom_host, shell_machine) != 0) {
+ rprintf(FERROR,
+ "--files-from hostname is not the same as the transfer hostname\n");
+ exit_cleanup(RERR_SYNTAX);
+ }
+ if (!path) { /* no hostspec found, so src & dest are local */
+ local_server = 1;
+ if (filesfrom_host) {
+ rprintf(FERROR,
+ "--files-from cannot be remote when the transfer is local\n");
+ exit_cleanup(RERR_SYNTAX);
+ }
+ shell_machine = NULL;
+ } else { /* hostspec was found, so dest is remote */
+ argv[argc] = path;
+ if (rsync_port)
+ daemon_over_rsh = shell_cmd ? 1 : -1;
+ }
+ }
+ } else { /* read_batch */
+ local_server = 1;
+ if (check_for_hostspec(argv[argc-1], &shell_machine, &rsync_port)) {
+ rprintf(FERROR, "remote destination is not allowed with --read-batch\n");
+ exit_cleanup(RERR_SYNTAX);
+ }
+ remote_argv = argv + argc - 1;
+ remote_argc = 1;
+ }
+
+ if (am_sender) {
+ char *dummy_host;
+ int dummy_port = rsync_port;
+ int i;
+ /* For local source, extra source args must not have hostspec. */
+ for (i = 1; i < argc; i++) {
+ if (check_for_hostspec(argv[i], &dummy_host, &dummy_port)) {
+ rprintf(FERROR, "Unexpected remote arg: %s\n", argv[i]);
+ exit_cleanup(RERR_SYNTAX);
+ }
+ }
+ } else {
+ char *dummy_host;
+ int dummy_port = rsync_port;
+ int i;
+ /* For remote source, any extra source args must have either
+ * the same hostname or an empty hostname. */
+ for (i = 1; i < remote_argc; i++) {
+ char *arg = check_for_hostspec(remote_argv[i], &dummy_host, &dummy_port);
+ if (!arg) {
+ rprintf(FERROR, "Unexpected local arg: %s\n", remote_argv[i]);
+ rprintf(FERROR, "If arg is a remote file/dir, prefix it with a colon (:).\n");
+ exit_cleanup(RERR_SYNTAX);
+ }
+ if (*dummy_host && strcmp(dummy_host, shell_machine) != 0) {
+ rprintf(FERROR, "All source args must come from the same machine.\n");
+ exit_cleanup(RERR_SYNTAX);
+ }
+ if (rsync_port != dummy_port) {
+ if (!rsync_port || !dummy_port)
+ rprintf(FERROR, "All source args must use the same hostspec format.\n");
+ else
+ rprintf(FERROR, "All source args must use the same port number.\n");
+ exit_cleanup(RERR_SYNTAX);
+ }
+ remote_argv[i] = arg;
+ }
+ if (argc == 0) {
+ list_only |= 1;
+ xfer_dirs |= 1;
+ }
+ }
+
+ if (daemon_over_rsh < 0)
+ return start_socket_client(shell_machine, remote_argc, remote_argv, argc, argv);
+
+ if (password_file && !daemon_over_rsh) {
+ rprintf(FERROR, "The --password-file option may only be "
+ "used when accessing an rsync daemon.\n");
+ exit_cleanup(RERR_SYNTAX);
+ }
+
+ if (connect_timeout) {
+ rprintf(FERROR, "The --contimeout option may only be "
+ "used when connecting to an rsync daemon.\n");
+ exit_cleanup(RERR_SYNTAX);
+ }
+
+ if (shell_machine) {
+ p = strrchr(shell_machine,'@');
+ if (p) {
+ *p = 0;
+ shell_user = shell_machine;
+ shell_machine = p+1;
+ }
+ }
+
+ if (verbose > 3) {
+ rprintf(FINFO,"cmd=%s machine=%s user=%s path=%s\n",
+ NS(shell_cmd), NS(shell_machine), NS(shell_user),
+ remote_argv ? NS(remote_argv[0]) : "");
+ }
+
+ pid = do_cmd(shell_cmd, shell_machine, shell_user, remote_argv, remote_argc,
+ &f_in, &f_out);
+
+ /* if we're running an rsync server on the remote host over a
+ * remote shell command, we need to do the RSYNCD protocol first */
+ if (daemon_over_rsh) {
+ int tmpret;
+ tmpret = start_inband_exchange(f_in, f_out, shell_user, remote_argc, remote_argv);
+ if (tmpret < 0)
+ return tmpret;
+ }
+
+ ret = client_run(f_in, f_out, pid, argc, argv);
+
+ fflush(stdout);
+ fflush(stderr);
+
+ return ret;
+}
+
+
+static RETSIGTYPE sigusr1_handler(UNUSED(int val))